Post SMTP Mailer/Email Log - Version 2.1.4

Version Description

Download this release

Release Info

Developer wpexpertsio
Plugin Icon 128x128 Post SMTP Mailer/Email Log
Version 2.1.4
Comparing to
See all releases

Code changes from version 2.1.4-rc.2 to 2.1.4

Postman/Postman-Mail/PostmanDefaultModuleTransport.php CHANGED
@@ -162,5 +162,18 @@ if (! class_exists ( 'PostmanSmtpModuleTransport' )) {
162
  public function getSocketsForSetupWizardToProbe($hostname, $smtpServerGuess) {
163
  return array ();
164
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  }
166
  }
162
  public function getSocketsForSetupWizardToProbe($hostname, $smtpServerGuess) {
163
  return array ();
164
  }
165
+
166
+
167
+ /**
168
+ * Returns true, to prevent from errors because it's default Module Transport.
169
+ *
170
+ * @since 2.1.4
171
+ * @version 1.0
172
+ */
173
+ public function has_granted() {
174
+
175
+ return true;
176
+
177
+ }
178
  }
179
  }
Postman/Postman-Mail/PostmanGmailApiModuleTransport.php CHANGED
@@ -272,4 +272,36 @@ class PostmanGmailApiModuleTransport extends PostmanAbstractZendModuleTransport
272
  return POST_SMTP_ASSETS . "images/logos/gmail.png";
273
 
274
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275
  }
272
  return POST_SMTP_ASSETS . "images/logos/gmail.png";
273
 
274
  }
275
+
276
+ /**
277
+ * Checks is granted or not
278
+ *
279
+ * @since 2.1.4
280
+ * @version 1.0
281
+ */
282
+ public function has_granted() {
283
+
284
+ if( $this->isPermissionNeeded() ) {
285
+ return false;
286
+ }
287
+
288
+ return true;
289
+
290
+ }
291
+
292
+ /**
293
+ * Returns the HTML of not granted
294
+ *
295
+ * @since 2.1.4
296
+ * @version 1.0
297
+ */
298
+ public function get_not_granted_notice() {
299
+
300
+ return array(
301
+ 'message' => __( ' You are just a step away to get started', 'post-smtp' ),
302
+ 'url_text' => $this->getScribe()->getRequestPermissionLinkText(),
303
+ 'url' => PostmanUtils::getGrantOAuthPermissionUrl()
304
+ );
305
+
306
+ }
307
  }
Postman/Postman-Mail/PostmanModuleTransport.php CHANGED
@@ -532,7 +532,7 @@ abstract class PostmanAbstractZendModuleTransport extends PostmanAbstractModuleT
532
  $deliveryDetails ['auth_desc'] = $this->getAuthenticationDescription ( $this->getAuthenticationType () );
533
 
534
  if ( $deliveryDetails ['host'] == 'localhost:25' ) {
535
- $deliveryDetails ['transport_name'] = __( 'Sendmail (server defualt - not SMTP)', 'post-smtp');
536
  }
537
 
538
  /* translators: where (1) is the transport type, (2) is the host, and (3) is the Authentication Type (e.g. Postman will send mail via smtp.gmail.com:465 using OAuth 2.0 authentication.) */
@@ -712,6 +712,16 @@ abstract class PostmanAbstractZendModuleTransport extends PostmanAbstractModuleT
712
  public function createOverrideMenu(PostmanWizardSocket $socket, $winningRecommendation, $userSocketOverride, $userAuthOverride) {
713
  $overrideItem = parent::createOverrideMenu ( $socket, $winningRecommendation, $userSocketOverride, $userAuthOverride );
714
  $selected = $overrideItem ['selected'];
 
 
 
 
 
 
 
 
 
 
715
 
716
  // only smtp can have multiple auth options
717
  $overrideAuthItems = array ();
@@ -747,7 +757,7 @@ abstract class PostmanAbstractZendModuleTransport extends PostmanAbstractModuleT
747
  if ($socket->auth_crammd5 || $socket->auth_login || $socket->authPlain) {
748
  array_push ( $overrideAuthItems, array (
749
  'selected' => $passwordMode,
750
- 'name' => __ ( 'Password (requires username and password) <span class=\'ps-less-secure\'>Not recommended </span>(Starting May 30, 2022, ​​Google will no longer support the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password.) <a href=\'https://postmansmtp.com/gmail-is-disabling-less-secure-apps\' target="_blank">Learn More</a>', 'post-smtp' ),
751
  'value' => 'password'
752
  ) );
753
  }
532
  $deliveryDetails ['auth_desc'] = $this->getAuthenticationDescription ( $this->getAuthenticationType () );
533
 
534
  if ( $deliveryDetails ['host'] == 'localhost:25' ) {
535
+ $deliveryDetails ['transport_name'] = __( 'Sendmail (server default - not SMTP)', 'post-smtp');
536
  }
537
 
538
  /* translators: where (1) is the transport type, (2) is the host, and (3) is the Authentication Type (e.g. Postman will send mail via smtp.gmail.com:465 using OAuth 2.0 authentication.) */
712
  public function createOverrideMenu(PostmanWizardSocket $socket, $winningRecommendation, $userSocketOverride, $userAuthOverride) {
713
  $overrideItem = parent::createOverrideMenu ( $socket, $winningRecommendation, $userSocketOverride, $userAuthOverride );
714
  $selected = $overrideItem ['selected'];
715
+ $password_field = __( 'Password', 'post-smtp' );
716
+
717
+ if( $winningRecommendation['hostname'] == 'smtp.gmail.com' ) {
718
+ $password_field = sprintf(
719
+ '%s <a href="%s" target="_blank">%s</a>',
720
+ __( 'App Password', 'post-smtp' ),
721
+ esc_url( 'https://postmansmtp.com/documentation/#setting-up-an-app-password-in-your-google-account' ),
722
+ __( 'How to Setup an App Password', 'post-smtp' )
723
+ );
724
+ }
725
 
726
  // only smtp can have multiple auth options
727
  $overrideAuthItems = array ();
757
  if ($socket->auth_crammd5 || $socket->auth_login || $socket->authPlain) {
758
  array_push ( $overrideAuthItems, array (
759
  'selected' => $passwordMode,
760
+ 'name' => $password_field,
761
  'value' => 'password'
762
  ) );
763
  }
Postman/Postman-Mail/PostmanMyMailConnector.php CHANGED
@@ -7,7 +7,7 @@ define( 'MAILSTER_POSTMAN_REQUIRED_VERSION', '2.0' );
7
  define( 'MAILSTER_POSTMAN_ID', 'postman' );
8
 
9
  /**
10
- * Enables MyMail to deliver via Postman
11
  *
12
  * @author jasonhendriks
13
  */
@@ -199,7 +199,7 @@ if ( ! class_exists( 'PostmanMyMailConnector' ) ) {
199
  */
200
  public function activate() {
201
  if ( defined( 'MAILSTER_VERSION' ) && version_compare( MAILSTER_POSTMAN_REQUIRED_VERSION, MAILSTER_VERSION, '<=' ) ) {
202
- mailster_notice( sprintf( __( 'MyMail: Change the delivery method in the %s!', 'post-smtp' ), sprintf( '<a href="edit.php?post_type=newsletter&page=mailster_settings&mailster_remove_notice=mailster_delivery_method#delivery">%s</a>', __( 'Settings', 'postman-smtp' ) ) ), '', false, 'delivery_method' );
203
  $this->reset();
204
  }
205
  }
@@ -215,7 +215,7 @@ if ( ! class_exists( 'PostmanMyMailConnector' ) ) {
215
  if ( mailster_option( 'deliverymethod' ) == MAILSTER_POSTMAN_ID ) {
216
  mailster_update_option( 'deliverymethod', 'simple' );
217
  /* Translators where %s is the name of the page */
218
- mailster_notice( sprintf( __( 'MyMail: Change the delivery method in the %s!', 'post-smtp' ), sprintf( '<a href="edit.php?post_type=newsletter&page=mailster_settings&mailster_remove_notice=mailster_delivery_method#delivery">%s</a>', __( 'Settings', 'postman-smtp' ) ) ), '', false, 'delivery_method' );
219
  }
220
  }
221
  }
7
  define( 'MAILSTER_POSTMAN_ID', 'postman' );
8
 
9
  /**
10
+ * Enables Mailster to deliver via Postman
11
  *
12
  * @author jasonhendriks
13
  */
199
  */
200
  public function activate() {
201
  if ( defined( 'MAILSTER_VERSION' ) && version_compare( MAILSTER_POSTMAN_REQUIRED_VERSION, MAILSTER_VERSION, '<=' ) ) {
202
+ mailster_notice( sprintf( __( 'Mailster: Change the delivery method in the %s!', 'post-smtp' ), sprintf( '<a href="edit.php?post_type=newsletter&page=mailster_settings&mailster_remove_notice=mailster_delivery_method#delivery">%s</a>', __( 'Settings', 'postman-smtp' ) ) ), '', false, 'delivery_method' );
203
  $this->reset();
204
  }
205
  }
215
  if ( mailster_option( 'deliverymethod' ) == MAILSTER_POSTMAN_ID ) {
216
  mailster_update_option( 'deliverymethod', 'simple' );
217
  /* Translators where %s is the name of the page */
218
+ mailster_notice( sprintf( __( 'Mailster: Change the delivery method in the %s!', 'post-smtp' ), sprintf( '<a href="edit.php?post_type=newsletter&page=mailster_settings&mailster_remove_notice=mailster_delivery_method#delivery">%s</a>', __( 'Settings', 'postman-smtp' ) ) ), '', false, 'delivery_method' );
219
  }
220
  }
221
  }
Postman/Postman-Mail/PostmanSmtpModuleTransport.php CHANGED
@@ -593,6 +593,18 @@ class PostmanSmtpModuleTransport extends PostmanAbstractZendModuleTransport impl
593
  print '</section>';
594
  }
595
 
 
 
 
 
 
 
 
 
 
 
 
 
596
  /**
597
  * Get Socket's logo
598
  *
593
  print '</section>';
594
  }
595
 
596
+ /**
597
+ * Returns true, to prevent from errors because it's default Module Transport.
598
+ *
599
+ * @since 2.1.4
600
+ * @version 1.0
601
+ */
602
+ public function has_granted() {
603
+
604
+ return true;
605
+
606
+ }
607
+
608
  /**
609
  * Get Socket's logo
610
  *
Postman/Postman-Mail/PostmanTransportRegistry.php CHANGED
@@ -74,9 +74,10 @@ class PostmanTransportRegistry {
74
 
75
  /**
76
  * Retrieve the transport Postman is currently configured with.
77
- *
78
  * @return PostmanModuleTransport
79
- * @deprecated
 
80
  */
81
  public function getCurrentTransport() {
82
  $selectedTransport = PostmanOptions::getInstance()->getTransportType();
74
 
75
  /**
76
  * Retrieve the transport Postman is currently configured with.
77
+ *
78
  * @return PostmanModuleTransport
79
+ * @deprecated 2.1.4 use getActiveTransport()
80
+ * @see getActiveTransport()
81
  */
82
  public function getCurrentTransport() {
83
  $selectedTransport = PostmanOptions::getInstance()->getTransportType();
Postman/PostmanAdminController.php CHANGED
@@ -342,6 +342,14 @@ if ( ! class_exists( 'PostmanAdminController' ) ) {
342
  public function handlePurgeDataAction() {
343
  $this->logger->debug( 'is wpnonce purge-data?' );
344
  if ( wp_verify_nonce( $_REQUEST ['_wpnonce'], PostmanAdminController::PURGE_DATA_SLUG ) ) {
 
 
 
 
 
 
 
 
345
  $this->logger->debug( 'Purging stored data' );
346
  delete_option( PostmanOptions::POSTMAN_OPTIONS );
347
  delete_option( PostmanOAuthToken::OPTIONS_NAME );
@@ -349,6 +357,14 @@ if ( ! class_exists( 'PostmanAdminController' ) ) {
349
  $logPurger = new PostmanEmailLogPurger();
350
  $logPurger->removeAll();
351
  $this->messageHandler->addMessage( __( 'Plugin data was removed.', 'post-smtp' ) );
 
 
 
 
 
 
 
 
352
  PostmanUtils::redirect( PostmanUtils::POSTMAN_HOME_PAGE_RELATIVE_URL );
353
  }
354
  }
342
  public function handlePurgeDataAction() {
343
  $this->logger->debug( 'is wpnonce purge-data?' );
344
  if ( wp_verify_nonce( $_REQUEST ['_wpnonce'], PostmanAdminController::PURGE_DATA_SLUG ) ) {
345
+
346
+ /**
347
+ * Fires before resetting pluign
348
+ *
349
+ * @since 2.1.4
350
+ */
351
+ do_action( 'post_smtp_before_reset_plugin' );
352
+
353
  $this->logger->debug( 'Purging stored data' );
354
  delete_option( PostmanOptions::POSTMAN_OPTIONS );
355
  delete_option( PostmanOAuthToken::OPTIONS_NAME );
357
  $logPurger = new PostmanEmailLogPurger();
358
  $logPurger->removeAll();
359
  $this->messageHandler->addMessage( __( 'Plugin data was removed.', 'post-smtp' ) );
360
+
361
+ /**
362
+ * Fires after resetting pluign
363
+ *
364
+ * @since 2.1.4
365
+ */
366
+ do_action( 'post_smtp_after_reset_plugin' );
367
+
368
  PostmanUtils::redirect( PostmanUtils::POSTMAN_HOME_PAGE_RELATIVE_URL );
369
  }
370
  }
Postman/PostmanViewController.php CHANGED
@@ -1,734 +1,763 @@
1
- <?php
2
- if ( ! defined( 'ABSPATH' ) ) {
3
- exit; // Exit if accessed directly
4
- }
5
- if ( ! class_exists( 'PostmanViewController' ) ) {
6
- class PostmanViewController {
7
- private $logger;
8
- private $rootPluginFilenameAndPath;
9
- private $options;
10
- private $authorizationToken;
11
- private $oauthScribe;
12
- private $importableConfiguration;
13
- private $adminController;
14
- const POSTMAN_MENU_SLUG = 'postman';
15
-
16
- // style sheets and scripts
17
- const POSTMAN_STYLE = 'postman_style';
18
- const JQUERY_SCRIPT = 'jquery';
19
- const POSTMAN_SCRIPT = 'postman_script';
20
-
21
- /**
22
- * Constructor
23
- *
24
- * @param PostmanOptions $options
25
- * @param PostmanOAuthToken $authorizationToken
26
- * @param PostmanConfigTextHelper $oauthScribe
27
- */
28
- function __construct( $rootPluginFilenameAndPath, PostmanOptions $options, PostmanOAuthToken $authorizationToken, PostmanConfigTextHelper $oauthScribe, PostmanAdminController $adminController ) {
29
- $this->options = $options;
30
- $this->rootPluginFilenameAndPath = $rootPluginFilenameAndPath;
31
- $this->authorizationToken = $authorizationToken;
32
- $this->oauthScribe = $oauthScribe;
33
- $this->adminController = $adminController;
34
- $this->logger = new PostmanLogger( get_class( $this ) );
35
- $hostname = PostmanOptions::getInstance()->getHostname();
36
- $transportType = PostmanOptions::getInstance()->getTransportType();
37
- $auth_type = PostmanOptions::getInstance()->getAuthenticationType();
38
-
39
- PostmanUtils::registerAdminMenu( $this, 'generateDefaultContent' );
40
- PostmanUtils::registerAdminMenu( $this, 'addPurgeDataSubmenu' );
41
-
42
- // initialize the scripts, stylesheets and form fields
43
- add_action( 'admin_init', array( $this, 'registerStylesAndScripts' ), 0 );
44
- add_action( 'wp_ajax_delete_lock_file', array( $this, 'delete_lock_file' ) );
45
- add_action( 'wp_ajax_dismiss_version_notify', array( $this, 'dismiss_version_notify' ) );
46
- add_action( 'wp_ajax_dismiss_donation_notify', array( $this, 'dismiss_donation_notify' ) );
47
- add_action( 'wp_ajax_ps-discard-less-secure-notification', array( $this, 'discard_less_secure_notification' ) );
48
-
49
- $show_less_secure_notification = get_option( 'ps_hide_less_secure' );
50
-
51
- if( !$show_less_secure_notification && $transportType == 'smtp' && $hostname == 'smtp.gmail.com' && ( $auth_type == 'plain' || $auth_type == 'login' ) ) {
52
- add_action( 'admin_notices', array( $this, 'google_less_secure_notice' ) );
53
- }
54
-
55
- //add_action( 'admin_init', array( $this, 'do_activation_redirect' ) );
56
-
57
- }
58
-
59
-
60
- function dismiss_version_notify() {
61
- check_admin_referer( 'postsmtp', 'security' );
62
-
63
- $result = update_option('postman_release_version', true );
64
- }
65
-
66
- function dismiss_donation_notify() {
67
- check_admin_referer( 'postsmtp', 'security' );
68
-
69
- $result = update_option('postman_dismiss_donation', true );
70
- }
71
-
72
- function delete_lock_file() {
73
- check_admin_referer( 'postman', 'security' );
74
-
75
- if ( ! PostmanUtils::lockFileExists() ) {
76
- echo esc_html__('No lock file found.', 'post-smtp' );
77
- die();
78
- }
79
-
80
- echo PostmanUtils::deleteLockFile() == true ? esc_html__('Success, try to send test email.', 'post-smtp' ) : esc_html__('Failed, try again.', 'post-smtp' );
81
- die();
82
- }
83
-
84
- function do_activation_redirect() {
85
-
86
- // Bail if no activation redirect
87
- if ( ! get_transient( '_post_activation_redirect' ) ) {
88
- return;
89
- }
90
-
91
- // Delete the redirect transient
92
- delete_transient( '_post_activation_redirect' );
93
-
94
- // Bail if activating from network, or bulk
95
- if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
96
- return;
97
- }
98
-
99
- // Bail if the current user cannot see the about page
100
- if ( ! current_user_can( 'manage_options' ) ) {
101
- return;
102
- }
103
-
104
- // Redirect to bbPress about page
105
- wp_safe_redirect( add_query_arg( array( 'page' => 'post-about' ), admin_url( 'index.php' ) ) );
106
- }
107
-
108
- public static function getPageUrl( $slug ) {
109
- return PostmanUtils::getPageUrl( $slug );
110
- }
111
-
112
- /**
113
- * Add options page
114
- *
115
- * @since 2.1 Added `add_submenu_page`
116
- */
117
- public function generateDefaultContent() {
118
- // This page will be under "Settings"
119
- $pageTitle = sprintf( __( '%s Setup', 'post-smtp' ), __( 'Post SMTP', 'post-smtp' ) );
120
- $pluginName = __( 'Post SMTP', 'post-smtp' );
121
- $uniqueId = self::POSTMAN_MENU_SLUG;
122
- $pageOptions = array(
123
- $this,
124
- 'outputDefaultContent',
125
- );
126
- $mainPostmanSettingsPage = add_menu_page( $pageTitle, $pluginName, Postman::MANAGE_POSTMAN_CAPABILITY_NAME, $uniqueId, $pageOptions, 'dashicons-email' );
127
-
128
- //To change the text of top level menu
129
- add_submenu_page( $uniqueId, $pageTitle, 'Dashboard', Postman::MANAGE_POSTMAN_CAPABILITY_NAME, $uniqueId, $pageOptions );
130
-
131
- // When the plugin options page is loaded, also load the stylesheet
132
- add_action( 'admin_print_styles-' . $mainPostmanSettingsPage, array(
133
- $this,
134
- 'enqueueHomeScreenStylesheet',
135
- ) );
136
- }
137
- function enqueueHomeScreenStylesheet() {
138
- wp_enqueue_style( PostmanViewController::POSTMAN_STYLE );
139
- wp_enqueue_script( PostmanViewController::POSTMAN_SCRIPT );
140
- }
141
-
142
- /**
143
- * Register the Email Test screen
144
- */
145
- public function addPurgeDataSubmenu() {
146
- $page = add_submenu_page( null, sprintf( __( '%s Setup', 'post-smtp' ), __( 'Post SMTP', 'post-smtp' ) ), __( 'Post SMTP', 'post-smtp' ), Postman::MANAGE_POSTMAN_CAPABILITY_NAME, PostmanAdminController::MANAGE_OPTIONS_PAGE_SLUG, array(
147
- $this,
148
- 'outputPurgeDataContent',
149
- ) );
150
- // When the plugin options page is loaded, also load the stylesheet
151
- add_action( 'admin_print_styles-' . $page, array(
152
- $this,
153
- 'enqueueHomeScreenStylesheet',
154
- ) );
155
- }
156
-
157
- /**
158
- * Register and add settings
159
- */
160
- public function registerStylesAndScripts() {
161
- if ( $this->logger->isTrace() ) {
162
- $this->logger->trace( 'registerStylesAndScripts()' );
163
- }
164
- // register the stylesheet and javascript external resources
165
- $pluginData = apply_filters( 'postman_get_plugin_metadata', null );
166
- wp_register_style( PostmanViewController::POSTMAN_STYLE, plugins_url( 'style/postman.css', $this->rootPluginFilenameAndPath ), null, $pluginData ['version'] );
167
- wp_register_style( 'jquery_ui_style', plugins_url( 'style/jquery-steps/jquery-ui.css', $this->rootPluginFilenameAndPath ), PostmanViewController::POSTMAN_STYLE, '1.1.0' );
168
- wp_register_style( 'jquery_steps_style', plugins_url( 'style/jquery-steps/jquery.steps.css', $this->rootPluginFilenameAndPath ), PostmanViewController::POSTMAN_STYLE, '1.1.0' );
169
-
170
- wp_register_script( PostmanViewController::POSTMAN_SCRIPT, plugins_url( 'script/postman.js', $this->rootPluginFilenameAndPath ), array(
171
- PostmanViewController::JQUERY_SCRIPT,
172
- 'jquery-ui-core',
173
- 'jquery-ui-datepicker',
174
- ), $pluginData ['version'] );
175
- wp_register_script( 'sprintf', plugins_url( 'script/sprintf/sprintf.min.js', $this->rootPluginFilenameAndPath ), null, '1.0.2' );
176
- wp_register_script( 'jquery_steps_script', plugins_url( 'script/jquery-steps/jquery.steps.min.js', $this->rootPluginFilenameAndPath ), array(
177
- PostmanViewController::JQUERY_SCRIPT
178
- ), '1.1.0' );
179
- wp_register_script( 'jquery_validation', plugins_url( 'script/jquery-validate/jquery.validate.min.js', $this->rootPluginFilenameAndPath ), array(
180
- PostmanViewController::JQUERY_SCRIPT
181
- ), '1.13.1' );
182
-
183
- wp_localize_script( PostmanViewController::POSTMAN_SCRIPT, 'postman_ajax_msg', array(
184
- 'bad_response' => __( 'An unexpected error occurred', 'post-smtp' ),
185
- 'corrupt_response' => __( 'Unexpected PHP messages corrupted the Ajax response', 'post-smtp' )
186
- ) );
187
-
188
- wp_localize_script( PostmanViewController::POSTMAN_SCRIPT, 'postman_ajax', array(
189
- 'lessSecureNotice' => wp_create_nonce( 'less-secure-security' )
190
- ) );
191
- }
192
-
193
- /**
194
- * Options page callback
195
- */
196
- public function outputDefaultContent() {
197
-
198
- // Set class property
199
- print '<div class="wrap">';
200
- print '<div class="ps-main-container-wrap">';
201
-
202
- $version = PostmanState::getInstance()->getVersion();
203
-
204
- printf(
205
- '<div class="ps-main-header post-smtp-welcome-panel"><h2>%s</h2></div>',
206
- esc_html__( 'Post SMTP Setup', 'post-smtp' )
207
- );
208
-
209
- //Top Notification message
210
- if( !PostmanPreRequisitesCheck::isReady() ) {
211
-
212
- printf(
213
- '<div class="ps-config-bar"><span>%s</span><span style="color: red" class="dashicons dashicons-dismiss"></span></div>',
214
- esc_html__( 'Postman is unable to run. Email delivery is being handled by WordPress (or another plugin).', 'post-smtp' )
215
- );
216
-
217
- }
218
- else {
219
-
220
- $ready_messsage = PostmanTransportRegistry::getInstance()->getReadyMessage();
221
- $statusMessage = $ready_messsage['message'];
222
-
223
- if ( PostmanTransportRegistry::getInstance()->getActiveTransport()->isConfiguredAndReady() ) {
224
-
225
- if ( $this->options->getRunMode() != PostmanOptions::RUN_MODE_PRODUCTION ) {
226
- printf(
227
- '<div class="ps-config-bar">
228
- <span>%s</span><span style="color: orange;" class="dashicons dashicons-yes-alt"></span>
229
- </div>',
230
- wp_kses_post( $statusMessage )
231
- );
232
- } else {
233
- printf(
234
- '<div class="ps-config-bar">
235
- <span>%s</span><span style="color: green" class="dashicons dashicons-yes-alt"></span>
236
- <div class="ps-right">
237
- What\'s Next? Get Started by Sending a Test Email! <a href="%s" class="ps-btn-orange"> Send a Test Email</a>
238
- </div>
239
- <div class="clear"></div>
240
- </div>',
241
- wp_kses_post( $statusMessage ),
242
- esc_url( $this->getPageUrl( PostmanSendTestEmailController::EMAIL_TEST_SLUG ) )
243
- );
244
- }
245
- } else {
246
- printf(
247
- '<div class="ps-config-bar">
248
- <span >%s</span>
249
- <span style="color: red" class="dashicons dashicons-dismiss"></span>
250
- <div class="ps-right">
251
- %s <a href="%s" class="ps-btn-orange">%s</a>
252
- </div>
253
- </div>',
254
- wp_kses_post( $statusMessage ),
255
- esc_html__( 'Get Started by Setup Wizard!', 'post-smtp' ),
256
- esc_attr( $this->getPageUrl( PostmanConfigurationController::CONFIGURATION_WIZARD_SLUG ) ),
257
- esc_html__( 'Start the Wizard', 'post-smtp' )
258
- );
259
- }
260
-
261
- }
262
-
263
- //Main Content
264
- ?>
265
- <div class="ps-flex ps-home-main">
266
- <div class="ps-setting-box">
267
- <div>
268
- <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/configuration.png' ) ?>" />
269
- <h3 class="ps-ib ps-vm"><?php esc_html_e( 'Configuration', 'post-smtp' ); ?></h3>
270
- </div>
271
- <div class="ps-wizard">
272
- <a href="<?php esc_attr_e( $this->getPageUrl( PostmanConfigurationController::CONFIGURATION_WIZARD_SLUG ) ) ?>" class="ps-btn-orange"><?php esc_html_e( 'Start the Wizard', 'post-smtp' ); ?></a>
273
- <h4><?php esc_html_e( 'OR', 'post-smtp' ); ?></h4>
274
- <div>
275
- <a href="<?php echo esc_url( $this->getPageUrl( PostmanConfigurationController::CONFIGURATION_SLUG ) ) ?>">
276
- <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
277
- <?php esc_html_e( 'Show All Settings', 'post-smtp' ) ?>
278
- </a>
279
- </div>
280
- </div>
281
- </div>
282
- <div class="ps-setting-box">
283
- <img src="<?php echo esc_attr( POST_SMTP_ASSETS . 'images/icons/action.png' ) ?>" />
284
- <h3 class="ps-ib ps-vm"><?php esc_html_e( 'Actions', 'post-smtp' ); ?></h3>
285
- <div>
286
- <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
287
- <?php
288
- // Grant permission with Google
289
- ob_start();
290
- PostmanTransportRegistry::getInstance()->getSelectedTransport()->printActionMenuItem();
291
- $oauth_link = ob_get_clean();
292
- $oauth_link = apply_filters( 'post_smtp_oauth_actions', $oauth_link );
293
- echo wp_kses_post( $oauth_link );
294
- ?>
295
- </div>
296
- <div>
297
- <?php
298
- if ( PostmanWpMailBinder::getInstance()->isBound() ) {
299
-
300
- echo '
301
- <div>
302
- <a href="'.esc_url( $this->getPageUrl( PostmanSendTestEmailController::EMAIL_TEST_SLUG ) ).'">
303
- <img src="'.esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ).'" width="15" />
304
- '.esc_html__( 'Send a Test Email', 'post-smtp' ).
305
- '</a>
306
- </div>';
307
-
308
- } else {
309
-
310
- echo '
311
- <div>
312
- <img src="'.esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ).'" width="15" />
313
- '.esc_html__( 'Send a Test Email', 'post-smtp' ) .'
314
- </div>
315
- ';
316
-
317
- }
318
- ?>
319
- </div>
320
- <div>
321
- <?php
322
- if ( ! $this->options->isNew() ) {
323
-
324
- $purgeLinkPattern = '
325
- <a href="%1$s">
326
- <img src="'.esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ).'" width="15" />
327
- %2$s
328
- </a>';
329
-
330
- }
331
- else {
332
-
333
- $purgeLinkPattern = '
334
- <img src="'.esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ).'" width="15" />
335
- %2$s
336
- ';
337
-
338
- }
339
-
340
- $importTitle = __( 'Import', 'post-smtp' );
341
- $exportTile = __( 'Export', 'post-smtp' );
342
- $resetTitle = __( 'Reset Plugin', 'post-smtp' );
343
- $importExportReset = sprintf( '%s/%s/%s', $importTitle, $exportTile, $resetTitle );
344
-
345
- printf(
346
- wp_kses_post( $purgeLinkPattern ),
347
- esc_url( $this->getPageUrl( PostmanAdminController::MANAGE_OPTIONS_PAGE_SLUG ) ),
348
- sprintf( '%s', esc_html( $importExportReset ) )
349
- );
350
-
351
- do_action( 'post_smtp_extension_reset_link' );
352
- ?>
353
- </div>
354
- </div>
355
- <div class="ps-setting-box">
356
- <div>
357
- <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/extentions.png' ) ?>" />
358
- <h3 class="ps-ib ps-vm"><?php esc_html_e( 'Extensions', 'post-smtp' ); ?></h3>
359
- </div>
360
- <div>
361
- <a href="<?php echo esc_url( 'https://postmansmtp.com/extensions/office-365-for-post-smtp-extension/' ); ?>" target="_blank">
362
- <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
363
- <?php echo esc_html( 'Office 365' ); ?>
364
- </a>
365
- </div>
366
- <div>
367
- <a href="<?php echo esc_url( 'https://postmansmtp.com/extensions/post-smtp-extension-for-amazon-ses/' ); ?>" target="_blank">
368
- <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
369
- <?php echo esc_html( 'Amazon SES' ); ?>
370
- </a>
371
- </div>
372
- <div>
373
- <a href="<?php echo esc_url( 'https://postmansmtp.com/extensions/the-better-email/' ); ?>" target="_blank">
374
- <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
375
- <?php echo esc_html( 'Better Email Logger' ); ?>
376
- </a>
377
- </div>
378
- </div>
379
- <div class="ps-setting-box">
380
- <div>
381
- <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/troubleshooting.png' ) ?>" />
382
- <h3 class="ps-ib ps-vm"><?php esc_html_e( 'Troubleshooting', 'post-smtp' ); ?></h3>
383
- </div>
384
- <div>
385
- <a href="<?php echo esc_url( 'https://postmansmtp.com/help-configure-post-smtp/' ); ?>" target="_blank" >
386
- <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
387
- <?php echo esc_html( 'Need help setup everything? (paid)' ); ?>
388
- </a>
389
- </div>
390
- <div>
391
- <a href="<?php echo $this->getPageUrl( PostmanConnectivityTestController::PORT_TEST_SLUG ); ?>">
392
- <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
393
- <?php echo esc_html( 'Connectivity Test' ); ?>
394
- </a>
395
- </div>
396
- <div>
397
- <a href="<?php echo $this->getPageUrl( PostmanDiagnosticTestController::DIAGNOSTICS_SLUG ); ?>">
398
- <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
399
- <?php echo esc_html( 'Diagnostic Test' ); ?>
400
- </a>
401
- </div>
402
- <div>
403
- <a href="#" class="release-lock-file" data-security="<?php esc_attr_e( wp_create_nonce( "postman" ) ); ?>" >
404
- <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
405
- <?php echo esc_html( 'Release Lock File Error' ); ?>
406
- </a>
407
- </div>
408
- <div>
409
- <a href="<?php echo esc_url( 'https://wordpress.org/support/plugin/post-smtp/' ); ?>" target="_blank">
410
- <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
411
- <?php echo esc_html( 'Online Support' ); ?>
412
- </a>
413
- </div>
414
- <div>
415
- <a href="<?php echo esc_url( 'https://www.facebook.com/groups/post.smtp' ); ?>" target="_blank">
416
- <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
417
- <?php echo esc_html( 'Facebook Group' ); ?>
418
- </a>
419
- </div>
420
- <div>
421
- <a href="<?php echo esc_url( 'https://postmansmtp.com/category/guides/' ); ?>" target="_blank">
422
- <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
423
- <?php echo esc_html( 'Guides' ); ?>
424
- </a>
425
- </div>
426
- </div>
427
- <div class="clear"></div>
428
- </div>
429
- <div class="ps-home-middle">
430
- <?php
431
-
432
- if ( PostmanPreRequisitesCheck::isReady() ) {
433
-
434
- $this->printDeliveryDetails();
435
- /* translators: where %d is the number of emails delivered */
436
- print '<p><span>';
437
- printf(
438
- wp_kses_post( _n(
439
- 'Postman has delivered <span style="color:green">%d</span> email.',
440
- 'Postman has delivered <span style="color:green">%d</span> emails.',
441
- esc_attr( PostmanState::getInstance()->getSuccessfulDeliveries() ) , 'post-smtp'
442
- ) ),
443
- esc_attr( PostmanState::getInstance()->getSuccessfulDeliveries() )
444
- );
445
- if ( $this->options->isMailLoggingEnabled() ) {
446
- print ' ';
447
- printf(
448
- wp_kses_post( __(
449
- 'The last %1$d email attempts are recorded <a href="%2$s">in the log</a>.', 'post-smtp'
450
- ) ),
451
- esc_attr( PostmanOptions::getInstance()->getMailLoggingMaxEntries() ),
452
- esc_attr( PostmanUtils::getEmailLogPageUrl() )
453
- );
454
- }
455
- print '</span></p>';
456
-
457
- }
458
-
459
- if ( $this->options->isNew() ) {
460
- printf(
461
- '<h3 style="padding-top:10px">%s</h3>',
462
- esc_html( 'Thank-you for choosing Postman!', 'post-smtp' )
463
- );
464
- /* translators: where %s is the URL of the Setup Wizard */
465
- printf(
466
- '<p><span>%s</span></p>',
467
- sprintf(
468
- wp_kses_post( 'Let\'s get started! All users are strongly encouraged to <a href="%s">run the Setup Wizard</a>.', 'post-smtp' ),
469
- esc_url( $this->getPageUrl( PostmanConfigurationController::CONFIGURATION_WIZARD_SLUG ) )
470
- )
471
- );
472
- printf(
473
- '<p><span>%s</span></p>',
474
- sprintf(
475
- wp_kses_post( 'Alternately, <a href="%s">manually configure</a> your own settings and/or modify advanced options.', 'post-smtp' ),
476
- esc_attr( $this->getPageUrl( PostmanConfigurationController::CONFIGURATION_SLUG ) )
477
- )
478
- );
479
- } else {
480
- if ( PostmanState::getInstance()->isTimeToReviewPostman() && ! PostmanOptions::getInstance()->isNew() ) {
481
- print '</br><hr width="70%"></br>';
482
- /* translators: where %s is the URL to the WordPress.org review and ratings page */
483
- printf(
484
- '<p>%s <a href="%s">%s</a>%s</p>',
485
- esc_html__( 'Please consider', 'post-smtp' ),
486
- esc_url( 'https://wordpress.org/support/plugin/post-smtp/reviews/?filter=5' ),
487
- esc_html__( 'leaving a review', 'post-smtp' ),
488
- esc_html( 'to help spread the word! :D', 'post-smtp' )
489
- );
490
- }
491
-
492
- printf(
493
- esc_html__( '%1$s Postman needs translators! Please take a moment to %2$s translate a few sentences on-line %3$s', 'post-smtp' ),
494
- '<p><span>',
495
- '<a href="https://translate.wordpress.org/projects/wp-plugins/post-smtp/stable">',
496
- '</a> :-)</span></p>'
497
- );
498
- }
499
- printf(
500
- '<p><span><b style="
501
- background-color:#2172b3; color: #fff;">%1$s</b>%2$s</span>&nbsp;<a target="_blank" href="%3$s">%4$s</a></p>',
502
- esc_html__( 'New for v1.9.8!', 'post-smtp' ),
503
- esc_html__( ' Fallback - setup a second delivery method when the first one is failing', 'post-smtp' ),
504
- esc_url( 'https://postmansmtp.com/post-smtp-1-9-7-the-smtp-fallback/' ),
505
- esc_html__( 'Check the detailes here', 'post-smtp')
506
- );
507
-
508
- print '</div>';
509
-
510
- //Temporary disabled
511
-
512
- // <div class="ps-home-bottom">
513
- // <div class="ps-config-bar">
514
- // <h1>Download Our Featured Plugins For Free</h1>
515
- // </div>
516
- // <div class="ps-flex">
517
- // <div class="ps-email-templates">
518
- // <h4>Create Fully Responsive Email Templates in Just a Few Minutes With Email Templates</h2>
519
- // <ul>
520
- // <li>A free WordPress email template plugin.</li>
521
- // <li>Quickest way to design elegant responsive emails.</li>
522
- // <li>Features fully compatible with Postnman SMTP.</li>
523
- // </ul>
524
- // <div class="ps-left">
525
- // <a href="https://wordpress.org/plugins/email-templates/>" class="ps-btn-light-blue" target="_blank">Free Download</a>
526
- // </div>
527
- // <div class="ps-right">
528
- // <img src="echo esc_url( POST_SMTP_ASSETS . 'images/logos/email-templates.jpg' )" />
529
- // </div>
530
- // <div class="clear"></div>
531
- // </div>
532
- // <div class="ps-login-designer">
533
- // <h4>Upgrade Your Custom Login Styling Experience With Login Designer</h2>
534
- // <ul>
535
- // <li>A free Login customizer plugin for WordPress.</li>
536
- // <li>Easiest way to customize your website's login page.</li>
537
- // <li>Installation is free, fun quick, and easy.</li>
538
- // </ul>
539
- // <div class="ps-left">
540
- // <a href="https://wordpress.org/plugins/login-designer/>" class="ps-btn-blue" target="_blank">Free Download</a>
541
- // </div>
542
- // <div class="ps-right">
543
- // <img src="<?php esc_url( POST_SMTP_ASSETS . 'images/logos/login-designer.jpg' )" />
544
- // </div>
545
- // <div class="clear"></div>
546
- // </div>
547
- // </div>
548
- // </div>
549
-
550
- print "</div>";
551
- print "</div>";
552
-
553
- }
554
-
555
- /**
556
- */
557
- private function printDeliveryDetails() {
558
- $currentTransport = PostmanTransportRegistry::getInstance()->getActiveTransport();
559
- $deliveryDetails = $currentTransport->getDeliveryDetails( $this->options );
560
- printf(
561
- '<p><span>%s</span></p>',
562
- wp_kses_post( $deliveryDetails )
563
- );
564
- }
565
-
566
- /**
567
- *
568
- * @param mixed $title
569
- * @param string $slug
570
- */
571
- public static function outputChildPageHeader( $title, $slug = '' ) {
572
-
573
- $content = '';
574
- $content .= sprintf( '<h2>%s</h2>', sprintf( __( '%s Setup', 'post-smtp' ), __( 'Post SMTP', 'post-smtp' ) ) );
575
- $content .= "
576
- <div id='postman-main-menu' class='post-smtp-welcome-panel {$slug}'>
577
- <div class='post-smtp-welcome-panel-content'>
578
- <div class='welcome-panel-column-container'>
579
- <div class='welcome-panel-last'>
580
- <div class='ps-left'>
581
- <h1>{$title}<h1/>
582
- </div>";
583
- $content .= sprintf( '<div class="ps-right"><div class="back-to-menu-link"><a href="%s" class="ps-btn-orange" >%s</a></div></div>', PostmanUtils::getSettingsPageUrl(), _x( 'Back To Main Menu', 'Return to main menu link', 'post-smtp' ) );
584
- $content .= '
585
- <div class="clear"></div>
586
- </div>
587
- </div>
588
- </div>
589
- </div>';
590
-
591
- echo wp_kses_post( $content );
592
-
593
- }
594
-
595
- /**
596
- */
597
- public function outputPurgeDataContent() {
598
- $importTitle = __( 'Import', 'post-smtp' );
599
- $exportTile = __( 'Export', 'post-smtp' );
600
- $resetTitle = __( 'Reset Plugin', 'post-smtp' );
601
- $options = $this->options;
602
- print '<div class="wrap">';
603
- PostmanViewController::outputChildPageHeader( sprintf( '%s/%s/%s', $importTitle, $exportTile, $resetTitle ) );
604
- print '<section id="export_settings" class="ps-left">';
605
- printf( '<h3><span>%s<span></h3>', esc_html( $exportTile ) );
606
- printf( '<p><span>%s</span></p>', esc_html__( 'Copy this data into another instance of Postman to duplicate the configuration.', 'post-smtp' ) );
607
- $data = '';
608
- if ( ! PostmanPreRequisitesCheck::checkZlibEncode() ) {
609
- $extraDeleteButtonAttributes = sprintf( 'disabled="true"' );
610
- $data = '';
611
- } else {
612
- $extraDeleteButtonAttributes = '';
613
- if ( ! $options->isNew() ) {
614
- $data = $options->export();
615
- }
616
- }
617
- printf(
618
- '<textarea cols="80" rows="10" class="ps-textarea" readonly="true" name="settings" %s>%s</textarea>',
619
- esc_attr( $extraDeleteButtonAttributes ), esc_textarea( $data )
620
- );
621
- print '</section>';
622
- print '<section id="import_settings" class="ps-right">';
623
- printf(
624
- '<h3><span>%s<span></h3>',
625
- esc_html( $importTitle )
626
- );
627
- print '<form method="POST" action="' . esc_attr( get_admin_url() ) . 'admin-post.php">';
628
- wp_nonce_field( PostmanAdminController::IMPORT_SETTINGS_SLUG );
629
- printf(
630
- '<input type="hidden" name="action" value="%s" />',
631
- esc_attr( PostmanAdminController::IMPORT_SETTINGS_SLUG )
632
- );
633
- print '<p>';
634
- printf(
635
- '<span>%s</span>',
636
- esc_html__( 'Paste data from another instance of Postman here to duplicate the configuration.', 'post-smtp' )
637
- );
638
- if ( PostmanTransportRegistry::getInstance()->getSelectedTransport()->isOAuthUsed( PostmanOptions::getInstance()->getAuthenticationType() ) ) {
639
- $warning = __( 'Warning', 'post-smtp' );
640
- $errorMessage = __( 'Using the same OAuth 2.0 Client ID and Client Secret from this site at the same time as another site will cause failures.', 'post-smtp' );
641
- printf( ' <span><b>%s</b>: %s</span>', esc_html( $warning ), esc_html( $errorMessage ) );
642
- }
643
- print '</p>';
644
- printf(
645
- '<textarea cols="80" rows="10" class="ps-textarea" name="settings" %s></textarea>',
646
- esc_textarea( $extraDeleteButtonAttributes )
647
- );
648
- submit_button( __( 'Import', 'post-smtp' ), 'ps-btn-orange', 'import', true, $extraDeleteButtonAttributes );
649
- print '</form>';
650
- print '</section>';
651
- print '<div class="clear"></div>';
652
- print '<section id="delete_settings">';
653
- printf( '<h3><span>%s<span></h3>', esc_html( $resetTitle ) );
654
- print '<form class="post-smtp-reset-options" method="POST" action="' . esc_attr( get_admin_url() ) . 'admin-post.php">';
655
- wp_nonce_field( PostmanAdminController::PURGE_DATA_SLUG );
656
- printf(
657
- '<input type="hidden" name="action" value="%s" />',
658
- esc_attr( PostmanAdminController::PURGE_DATA_SLUG )
659
- );
660
- printf(
661
- '<p><span>%s</span></p><p><span>%s</span></p>',
662
- esc_html__( 'This will purge all of Postman\'s settings, including account credentials and the email log.', 'post-smtp' ),
663
- esc_html__( 'Are you sure?', 'post-smtp' )
664
- );
665
-
666
- submit_button( $resetTitle, 'delete ps-btn-red', 'submit', true );
667
- print '</form>';
668
- print '</section>';
669
- print '</div>';
670
- }
671
-
672
- public function google_less_secure_notice() {
673
-
674
- ?>
675
- <div class="notice notice-error is-dismissible ps-less-secure-notice">
676
- <?php
677
- printf(
678
- '<p>
679
- %1$s
680
- <a href="%2$s" target="blank">%3$s</a>
681
- %4$s
682
- <a href="%5$s" target="blank">%6$s</a>
683
- %7$s
684
- <br />
685
- <a href="%8$s" target="_blank">%9$s</a>
686
- <br />
687
- <a href="" id="discard-less-secure-notification">%10$s</a>
688
- </p>',
689
- esc_html__( 'To help keep your account secure, Google will no longer support using third-party apps to sign in to your Google Account using only your username and primary password. You can ', 'post-smtp' ),
690
- esc_url( 'https://postmansmtp.com/gmail-is-disabling-less-secure-apps-feature-soon/' ),
691
- esc_html__( 'switch to the Auth 2.0', 'post-smtp' ),
692
- esc_html__( 'alternative or use your ', 'post-smtp' ),
693
- esc_url( 'https://postmansmtp.com/documentation/#setting-up-an-app-password-in-your-google-account' ),
694
- esc_html__( 'App Password', 'post-smtp' ),
695
- esc_html__( 'option to continue. ', 'post-smtp' ),
696
- esc_url( 'https://postmansmtp.com/gmail-is-disabling-less-secure-apps' ),
697
- esc_html__( 'Click here for more info', 'post-smtp' ),
698
- esc_html__( 'I understand and would like to discard this notice', 'post-smtp' )
699
- );
700
- ?>
701
- </div>
702
- <?php
703
-
704
- }
705
-
706
- /**
707
- * Discards less secure notification
708
- *
709
- * @since 2.1.2
710
- * @version 1.0
711
- */
712
- public function discard_less_secure_notification() {
713
-
714
- if( !wp_verify_nonce( $_POST['_wp_nonce'], 'less-secure-security' ) ) {
715
- die( 'Not Secure.' );
716
- }
717
-
718
- $result = update_option( 'ps_hide_less_secure', 1 );
719
-
720
- if( $result ) {
721
- wp_send_json_success(
722
- array( 'message' => 'Success' ),
723
- 200
724
- );
725
- }
726
-
727
- wp_send_json_error(
728
- array( 'message' => 'Something went wrong' ),
729
- 500
730
- );
731
-
732
- }
733
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
734
  }
1
+ <?php
2
+ if ( ! defined( 'ABSPATH' ) ) {
3
+ exit; // Exit if accessed directly
4
+ }
5
+ if ( ! class_exists( 'PostmanViewController' ) ) {
6
+ class PostmanViewController {
7
+ private $logger;
8
+ private $rootPluginFilenameAndPath;
9
+ private $options;
10
+ private $authorizationToken;
11
+ private $oauthScribe;
12
+ private $importableConfiguration;
13
+ private $adminController;
14
+ const POSTMAN_MENU_SLUG = 'postman';
15
+
16
+ // style sheets and scripts
17
+ const POSTMAN_STYLE = 'postman_style';
18
+ const JQUERY_SCRIPT = 'jquery';
19
+ const POSTMAN_SCRIPT = 'postman_script';
20
+
21
+ /**
22
+ * Constructor
23
+ *
24
+ * @param PostmanOptions $options
25
+ * @param PostmanOAuthToken $authorizationToken
26
+ * @param PostmanConfigTextHelper $oauthScribe
27
+ */
28
+ function __construct( $rootPluginFilenameAndPath, PostmanOptions $options, PostmanOAuthToken $authorizationToken, PostmanConfigTextHelper $oauthScribe, PostmanAdminController $adminController ) {
29
+ $this->options = $options;
30
+ $this->rootPluginFilenameAndPath = $rootPluginFilenameAndPath;
31
+ $this->authorizationToken = $authorizationToken;
32
+ $this->oauthScribe = $oauthScribe;
33
+ $this->adminController = $adminController;
34
+ $this->logger = new PostmanLogger( get_class( $this ) );
35
+ $hostname = PostmanOptions::getInstance()->getHostname();
36
+ $transportType = PostmanOptions::getInstance()->getTransportType();
37
+ $auth_type = PostmanOptions::getInstance()->getAuthenticationType();
38
+
39
+ PostmanUtils::registerAdminMenu( $this, 'generateDefaultContent' );
40
+ PostmanUtils::registerAdminMenu( $this, 'addPurgeDataSubmenu' );
41
+
42
+ // initialize the scripts, stylesheets and form fields
43
+ add_action( 'admin_init', array( $this, 'registerStylesAndScripts' ), 0 );
44
+ add_action( 'wp_ajax_delete_lock_file', array( $this, 'delete_lock_file' ) );
45
+ add_action( 'wp_ajax_dismiss_version_notify', array( $this, 'dismiss_version_notify' ) );
46
+ add_action( 'wp_ajax_dismiss_donation_notify', array( $this, 'dismiss_donation_notify' ) );
47
+ add_action( 'wp_ajax_ps-discard-less-secure-notification', array( $this, 'discard_less_secure_notification' ) );
48
+
49
+ $show_less_secure_notification = get_option( 'ps_hide_less_secure' );
50
+
51
+ if( !$show_less_secure_notification && $transportType == 'smtp' && $hostname == 'smtp.gmail.com' && ( $auth_type == 'plain' || $auth_type == 'login' ) ) {
52
+ add_action( 'admin_notices', array( $this, 'google_less_secure_notice' ) );
53
+ }
54
+
55
+ //add_action( 'admin_init', array( $this, 'do_activation_redirect' ) );
56
+
57
+ }
58
+
59
+
60
+ function dismiss_version_notify() {
61
+ check_admin_referer( 'postsmtp', 'security' );
62
+
63
+ $result = update_option('postman_release_version', true );
64
+ }
65
+
66
+ function dismiss_donation_notify() {
67
+ check_admin_referer( 'postsmtp', 'security' );
68
+
69
+ $result = update_option('postman_dismiss_donation', true );
70
+ }
71
+
72
+ function delete_lock_file() {
73
+ check_admin_referer( 'postman', 'security' );
74
+
75
+ if ( ! PostmanUtils::lockFileExists() ) {
76
+ echo esc_html__('No lock file found.', 'post-smtp' );
77
+ die();
78
+ }
79
+
80
+ echo PostmanUtils::deleteLockFile() == true ? esc_html__('Success, try to send test email.', 'post-smtp' ) : esc_html__('Failed, try again.', 'post-smtp' );
81
+ die();
82
+ }
83
+
84
+ function do_activation_redirect() {
85
+
86
+ // Bail if no activation redirect
87
+ if ( ! get_transient( '_post_activation_redirect' ) ) {
88
+ return;
89
+ }
90
+
91
+ // Delete the redirect transient
92
+ delete_transient( '_post_activation_redirect' );
93
+
94
+ // Bail if activating from network, or bulk
95
+ if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
96
+ return;
97
+ }
98
+
99
+ // Bail if the current user cannot see the about page
100
+ if ( ! current_user_can( 'manage_options' ) ) {
101
+ return;
102
+ }
103
+
104
+ // Redirect to bbPress about page
105
+ wp_safe_redirect( add_query_arg( array( 'page' => 'post-about' ), admin_url( 'index.php' ) ) );
106
+ }
107
+
108
+ public static function getPageUrl( $slug ) {
109
+ return PostmanUtils::getPageUrl( $slug );
110
+ }
111
+
112
+ /**
113
+ * Add options page
114
+ *
115
+ * @since 2.1 Added `add_submenu_page`
116
+ */
117
+ public function generateDefaultContent() {
118
+ // This page will be under "Settings"
119
+ $pageTitle = sprintf( __( '%s Setup', 'post-smtp' ), __( 'Post SMTP', 'post-smtp' ) );
120
+ $pluginName = __( 'Post SMTP', 'post-smtp' );
121
+ $uniqueId = self::POSTMAN_MENU_SLUG;
122
+ $pageOptions = array(
123
+ $this,
124
+ 'outputDefaultContent',
125
+ );
126
+ $mainPostmanSettingsPage = add_menu_page( $pageTitle, $pluginName, Postman::MANAGE_POSTMAN_CAPABILITY_NAME, $uniqueId, $pageOptions, 'dashicons-email' );
127
+
128
+ //To change the text of top level menu
129
+ add_submenu_page( $uniqueId, $pageTitle, 'Dashboard', Postman::MANAGE_POSTMAN_CAPABILITY_NAME, $uniqueId, $pageOptions );
130
+
131
+ // When the plugin options page is loaded, also load the stylesheet
132
+ add_action( 'admin_print_styles-' . $mainPostmanSettingsPage, array(
133
+ $this,
134
+ 'enqueueHomeScreenStylesheet',
135
+ ) );
136
+ }
137
+ function enqueueHomeScreenStylesheet() {
138
+ wp_enqueue_style( PostmanViewController::POSTMAN_STYLE );
139
+ wp_enqueue_script( PostmanViewController::POSTMAN_SCRIPT );
140
+ }
141
+
142
+ /**
143
+ * Register the Email Test screen
144
+ */
145
+ public function addPurgeDataSubmenu() {
146
+ $page = add_submenu_page( null, sprintf( __( '%s Setup', 'post-smtp' ), __( 'Post SMTP', 'post-smtp' ) ), __( 'Post SMTP', 'post-smtp' ), Postman::MANAGE_POSTMAN_CAPABILITY_NAME, PostmanAdminController::MANAGE_OPTIONS_PAGE_SLUG, array(
147
+ $this,
148
+ 'outputPurgeDataContent',
149
+ ) );
150
+ // When the plugin options page is loaded, also load the stylesheet
151
+ add_action( 'admin_print_styles-' . $page, array(
152
+ $this,
153
+ 'enqueueHomeScreenStylesheet',
154
+ ) );
155
+ }
156
+
157
+ /**
158
+ * Register and add settings
159
+ */
160
+ public function registerStylesAndScripts() {
161
+ if ( $this->logger->isTrace() ) {
162
+ $this->logger->trace( 'registerStylesAndScripts()' );
163
+ }
164
+ // register the stylesheet and javascript external resources
165
+ $pluginData = apply_filters( 'postman_get_plugin_metadata', null );
166
+ wp_register_style( PostmanViewController::POSTMAN_STYLE, plugins_url( 'style/postman.css', $this->rootPluginFilenameAndPath ), null, $pluginData ['version'] );
167
+ wp_register_style( 'jquery_ui_style', plugins_url( 'style/jquery-steps/jquery-ui.css', $this->rootPluginFilenameAndPath ), PostmanViewController::POSTMAN_STYLE, '1.1.0' );
168
+ wp_register_style( 'jquery_steps_style', plugins_url( 'style/jquery-steps/jquery.steps.css', $this->rootPluginFilenameAndPath ), PostmanViewController::POSTMAN_STYLE, '1.1.0' );
169
+
170
+ wp_register_script( PostmanViewController::POSTMAN_SCRIPT, plugins_url( 'script/postman.js', $this->rootPluginFilenameAndPath ), array(
171
+ PostmanViewController::JQUERY_SCRIPT,
172
+ 'jquery-ui-core',
173
+ 'jquery-ui-datepicker',
174
+ ), $pluginData ['version'] );
175
+ wp_register_script( 'sprintf', plugins_url( 'script/sprintf/sprintf.min.js', $this->rootPluginFilenameAndPath ), null, '1.0.2' );
176
+ wp_register_script( 'jquery_steps_script', plugins_url( 'script/jquery-steps/jquery.steps.min.js', $this->rootPluginFilenameAndPath ), array(
177
+ PostmanViewController::JQUERY_SCRIPT
178
+ ), '1.1.0' );
179
+ wp_register_script( 'jquery_validation', plugins_url( 'script/jquery-validate/jquery.validate.min.js', $this->rootPluginFilenameAndPath ), array(
180
+ PostmanViewController::JQUERY_SCRIPT
181
+ ), '1.13.1' );
182
+
183
+ wp_localize_script( PostmanViewController::POSTMAN_SCRIPT, 'postman_ajax_msg', array(
184
+ 'bad_response' => __( 'An unexpected error occurred', 'post-smtp' ),
185
+ 'corrupt_response' => __( 'Unexpected PHP messages corrupted the Ajax response', 'post-smtp' )
186
+ ) );
187
+
188
+ wp_localize_script( PostmanViewController::POSTMAN_SCRIPT, 'postman_ajax', array(
189
+ 'lessSecureNotice' => wp_create_nonce( 'less-secure-security' )
190
+ ) );
191
+ }
192
+
193
+ /**
194
+ * Options page callback
195
+ */
196
+ public function outputDefaultContent() {
197
+
198
+ // Set class property
199
+ print '<div class="wrap">';
200
+ print '<div class="ps-main-container-wrap">';
201
+
202
+ $version = PostmanState::getInstance()->getVersion();
203
+
204
+ printf(
205
+ '<div class="ps-main-header post-smtp-welcome-panel"><h2>%s</h2></div>',
206
+ esc_html__( 'Post SMTP Setup', 'post-smtp' )
207
+ );
208
+
209
+ //Top Notification message
210
+ if( !PostmanPreRequisitesCheck::isReady() ) {
211
+
212
+ printf(
213
+ '<div class="ps-config-bar"><span>%s</span><span style="color: red" class="dashicons dashicons-dismiss"></span></div>',
214
+ esc_html__( 'Postman is unable to run. Email delivery is being handled by WordPress (or another plugin).', 'post-smtp' )
215
+ );
216
+
217
+ }
218
+ else {
219
+
220
+ $ready_messsage = PostmanTransportRegistry::getInstance()->getReadyMessage();
221
+ $statusMessage = $ready_messsage['message'];
222
+
223
+ $transport = PostmanTransportRegistry::getInstance()->getSelectedTransport();
224
+
225
+ if ( PostmanTransportRegistry::getInstance()->getActiveTransport()->isConfiguredAndReady() ) {
226
+
227
+ if ( $this->options->getRunMode() != PostmanOptions::RUN_MODE_PRODUCTION ) {
228
+ printf(
229
+ '<div class="ps-config-bar">
230
+ <span>%s</span><span style="color: orange;" class="dashicons dashicons-yes-alt"></span>
231
+ </div>',
232
+ wp_kses_post( $statusMessage )
233
+ );
234
+ }
235
+ else {
236
+ printf(
237
+ '<div class="ps-config-bar">
238
+ <span>%s</span><span style="color: green" class="dashicons dashicons-yes-alt"></span>
239
+ <div class="ps-right">
240
+ What\'s Next? Get Started by Sending a Test Email! <a href="%s" class="ps-btn-orange"> Send a Test Email</a>
241
+ </div>
242
+ <div class="clear"></div>
243
+ </div>',
244
+ wp_kses_post( $statusMessage ),
245
+ esc_url( $this->getPageUrl( PostmanSendTestEmailController::EMAIL_TEST_SLUG ) )
246
+ );
247
+ }
248
+ }
249
+ elseif ( !$transport->has_granted() ) {
250
+
251
+ $notice = $transport->get_not_granted_notice();
252
+
253
+ printf(
254
+ '<div class="ps-config-bar">
255
+ <span >%s</span>
256
+ <div class="ps-right">
257
+ <img src="%s" style="vertical-align: middle;width: 30px;" />
258
+ <a href="%s" class="ps-btn-orange">%s</a>
259
+ </div>
260
+ </div>',
261
+ esc_html( $notice['message'] ),
262
+ esc_url( POST_SMTP_ASSETS . 'images/icons/hand.png' ),
263
+ esc_attr( $notice['url'] ),
264
+ esc_html( $notice['url_text'] )
265
+ );
266
+
267
+ }
268
+ else {
269
+ printf(
270
+ '<div class="ps-config-bar">
271
+ <span >%s</span>
272
+ <span style="color: red" class="dashicons dashicons-dismiss"></span>
273
+ <div class="ps-right">
274
+ %s <a href="%s" class="ps-btn-orange">%s</a>
275
+ </div>
276
+ </div>',
277
+ wp_kses_post( $statusMessage ),
278
+ esc_html__( 'Get Started by Setup Wizard!', 'post-smtp' ),
279
+ esc_attr( $this->getPageUrl( PostmanConfigurationController::CONFIGURATION_WIZARD_SLUG ) ),
280
+ esc_html__( 'Start the Wizard', 'post-smtp' )
281
+ );
282
+ }
283
+
284
+ }
285
+
286
+ //Main Content
287
+ ?>
288
+ <div class="ps-flex ps-home-main">
289
+ <div class="ps-setting-box">
290
+ <div>
291
+ <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/configuration.png' ) ?>" />
292
+ <h3 class="ps-ib ps-vm"><?php esc_html_e( 'Configuration', 'post-smtp' ); ?></h3>
293
+ </div>
294
+ <div class="ps-wizard">
295
+ <a href="<?php esc_attr_e( $this->getPageUrl( PostmanConfigurationController::CONFIGURATION_WIZARD_SLUG ) ) ?>" class="ps-btn-orange"><?php esc_html_e( 'Start the Wizard', 'post-smtp' ); ?></a>
296
+ <h4><?php esc_html_e( 'OR', 'post-smtp' ); ?></h4>
297
+ <div>
298
+ <a href="<?php echo esc_url( $this->getPageUrl( PostmanConfigurationController::CONFIGURATION_SLUG ) ) ?>">
299
+ <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
300
+ <?php esc_html_e( 'Show All Settings', 'post-smtp' ) ?>
301
+ </a>
302
+ </div>
303
+ </div>
304
+ </div>
305
+ <div class="ps-setting-box">
306
+ <img src="<?php echo esc_attr( POST_SMTP_ASSETS . 'images/icons/action.png' ) ?>" />
307
+ <h3 class="ps-ib ps-vm"><?php esc_html_e( 'Actions', 'post-smtp' ); ?></h3>
308
+ <div>
309
+ <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
310
+ <?php
311
+ // Grant permission with Google
312
+ ob_start();
313
+ PostmanTransportRegistry::getInstance()->getSelectedTransport()->printActionMenuItem();
314
+ $oauth_link = ob_get_clean();
315
+ $oauth_link = apply_filters( 'post_smtp_oauth_actions', $oauth_link );
316
+ echo wp_kses_post( $oauth_link );
317
+ ?>
318
+ </div>
319
+ <div>
320
+ <?php
321
+ if ( PostmanWpMailBinder::getInstance()->isBound() ) {
322
+
323
+ echo '
324
+ <div>
325
+ <a href="'.esc_url( $this->getPageUrl( PostmanSendTestEmailController::EMAIL_TEST_SLUG ) ).'">
326
+ <img src="'.esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ).'" width="15" />
327
+ '.esc_html__( 'Send a Test Email', 'post-smtp' ).
328
+ '</a>
329
+ </div>';
330
+
331
+ } else {
332
+
333
+ echo '
334
+ <div>
335
+ <img src="'.esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ).'" width="15" />
336
+ '.esc_html__( 'Send a Test Email', 'post-smtp' ) .'
337
+ </div>
338
+ ';
339
+
340
+ }
341
+ ?>
342
+ </div>
343
+ <div>
344
+ <?php
345
+ if ( ! $this->options->isNew() ) {
346
+
347
+ $purgeLinkPattern = '
348
+ <a href="%1$s">
349
+ <img src="'.esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ).'" width="15" />
350
+ %2$s
351
+ </a>';
352
+
353
+ }
354
+ else {
355
+
356
+ $purgeLinkPattern = '
357
+ <img src="'.esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ).'" width="15" />
358
+ %2$s
359
+ ';
360
+
361
+ }
362
+
363
+ $importTitle = __( 'Import', 'post-smtp' );
364
+ $exportTile = __( 'Export', 'post-smtp' );
365
+ $resetTitle = __( 'Reset Plugin', 'post-smtp' );
366
+ $importExportReset = sprintf( '%s/%s/%s', $importTitle, $exportTile, $resetTitle );
367
+
368
+ printf(
369
+ wp_kses_post( $purgeLinkPattern ),
370
+ esc_url( $this->getPageUrl( PostmanAdminController::MANAGE_OPTIONS_PAGE_SLUG ) ),
371
+ sprintf( '%s', esc_html( $importExportReset ) )
372
+ );
373
+
374
+ do_action( 'post_smtp_extension_reset_link' );
375
+ ?>
376
+ </div>
377
+ </div>
378
+ <div class="ps-setting-box">
379
+ <div>
380
+ <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/extentions.png' ) ?>" />
381
+ <h3 class="ps-ib ps-vm"><?php esc_html_e( 'Extensions', 'post-smtp' ); ?></h3>
382
+ </div>
383
+ <div>
384
+ <a href="<?php echo esc_url( 'https://postmansmtp.com/extensions/office-365-for-post-smtp-extension/' ); ?>" target="_blank">
385
+ <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
386
+ <?php echo esc_html( 'Office 365' ); ?>
387
+ </a>
388
+ </div>
389
+ <div>
390
+ <a href="<?php echo esc_url( 'https://postmansmtp.com/extensions/post-smtp-extension-for-amazon-ses/' ); ?>" target="_blank">
391
+ <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
392
+ <?php echo esc_html( 'Amazon SES' ); ?>
393
+ </a>
394
+ </div>
395
+ <div>
396
+ <a href="<?php echo esc_url( 'https://postmansmtp.com/extensions/the-better-email/' ); ?>" target="_blank">
397
+ <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
398
+ <?php echo esc_html( 'Better Email Logger' ); ?>
399
+ </a>
400
+ </div>
401
+ <div>
402
+ <a href="<?php echo esc_url( 'https://postmansmtp.com/extensions/twilio-extension-pro/' ); ?>" target="_blank">
403
+ <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
404
+ <?php echo esc_html( 'Twilio Notifications' ); ?>
405
+ </a>
406
+ </div>
407
+ </div>
408
+ <div class="ps-setting-box">
409
+ <div>
410
+ <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/troubleshooting.png' ) ?>" />
411
+ <h3 class="ps-ib ps-vm"><?php esc_html_e( 'Troubleshooting', 'post-smtp' ); ?></h3>
412
+ </div>
413
+ <div>
414
+ <a href="<?php echo esc_url( 'https://postmansmtp.com/help-configure-post-smtp/' ); ?>" target="_blank" >
415
+ <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
416
+ <?php echo esc_html( 'Need help setup everything? (paid)' ); ?>
417
+ </a>
418
+ </div>
419
+ <div>
420
+ <a href="<?php echo $this->getPageUrl( PostmanConnectivityTestController::PORT_TEST_SLUG ); ?>">
421
+ <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
422
+ <?php echo esc_html( 'Connectivity Test' ); ?>
423
+ </a>
424
+ </div>
425
+ <div>
426
+ <a href="<?php echo $this->getPageUrl( PostmanDiagnosticTestController::DIAGNOSTICS_SLUG ); ?>">
427
+ <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
428
+ <?php echo esc_html( 'Diagnostic Test' ); ?>
429
+ </a>
430
+ </div>
431
+ <div>
432
+ <a href="#" class="release-lock-file" data-security="<?php esc_attr_e( wp_create_nonce( "postman" ) ); ?>" >
433
+ <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
434
+ <?php echo esc_html( 'Release Lock File Error' ); ?>
435
+ </a>
436
+ </div>
437
+ <div>
438
+ <a href="<?php echo esc_url( 'https://wordpress.org/support/plugin/post-smtp/' ); ?>" target="_blank">
439
+ <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
440
+ <?php echo esc_html( 'Online Support' ); ?>
441
+ </a>
442
+ </div>
443
+ <div>
444
+ <a href="<?php echo esc_url( 'https://www.facebook.com/groups/post.smtp' ); ?>" target="_blank">
445
+ <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
446
+ <?php echo esc_html( 'Facebook Group' ); ?>
447
+ </a>
448
+ </div>
449
+ <div>
450
+ <a href="<?php echo esc_url( 'https://postmansmtp.com/category/guides/' ); ?>" target="_blank">
451
+ <img src="<?php echo esc_url( POST_SMTP_ASSETS . 'images/icons/finger.png' ) ?>" width="15" />
452
+ <?php echo esc_html( 'Guides' ); ?>
453
+ </a>
454
+ </div>
455
+ </div>
456
+ <div class="clear"></div>
457
+ </div>
458
+ <div class="ps-home-middle">
459
+ <?php
460
+
461
+ if ( PostmanPreRequisitesCheck::isReady() ) {
462
+
463
+ $this->printDeliveryDetails();
464
+ /* translators: where %d is the number of emails delivered */
465
+ print '<p><span>';
466
+ printf(
467
+ wp_kses_post( _n(
468
+ 'Postman has delivered <span style="color:green">%d</span> email.',
469
+ 'Postman has delivered <span style="color:green">%d</span> emails.',
470
+ esc_attr( PostmanState::getInstance()->getSuccessfulDeliveries() ) , 'post-smtp'
471
+ ) ),
472
+ esc_attr( PostmanState::getInstance()->getSuccessfulDeliveries() )
473
+ );
474
+ if ( $this->options->isMailLoggingEnabled() ) {
475
+ print ' ';
476
+ printf(
477
+ wp_kses_post( __(
478
+ 'The last %1$d email attempts are recorded <a href="%2$s">in the log</a>.', 'post-smtp'
479
+ ) ),
480
+ esc_attr( PostmanOptions::getInstance()->getMailLoggingMaxEntries() ),
481
+ esc_attr( PostmanUtils::getEmailLogPageUrl() )
482
+ );
483
+ }
484
+ print '</span></p>';
485
+
486
+ }
487
+
488
+ if ( $this->options->isNew() ) {
489
+ printf(
490
+ '<h3 style="padding-top:10px">%s</h3>',
491
+ esc_html( 'Thank-you for choosing Postman!', 'post-smtp' )
492
+ );
493
+ /* translators: where %s is the URL of the Setup Wizard */
494
+ printf(
495
+ '<p><span>%s</span></p>',
496
+ sprintf(
497
+ wp_kses_post( 'Let\'s get started! All users are strongly encouraged to <a href="%s">run the Setup Wizard</a>.', 'post-smtp' ),
498
+ esc_url( $this->getPageUrl( PostmanConfigurationController::CONFIGURATION_WIZARD_SLUG ) )
499
+ )
500
+ );
501
+ printf(
502
+ '<p><span>%s</span></p>',
503
+ sprintf(
504
+ wp_kses_post( 'Alternately, <a href="%s">manually configure</a> your own settings and/or modify advanced options.', 'post-smtp' ),
505
+ esc_attr( $this->getPageUrl( PostmanConfigurationController::CONFIGURATION_SLUG ) )
506
+ )
507
+ );
508
+ } else {
509
+ if ( PostmanState::getInstance()->isTimeToReviewPostman() && ! PostmanOptions::getInstance()->isNew() ) {
510
+ print '</br><hr width="70%"></br>';
511
+ /* translators: where %s is the URL to the WordPress.org review and ratings page */
512
+ printf(
513
+ '<p>%s <a href="%s">%s</a>%s</p>',
514
+ esc_html__( 'Please consider', 'post-smtp' ),
515
+ esc_url( 'https://wordpress.org/support/plugin/post-smtp/reviews/?filter=5' ),
516
+ esc_html__( 'leaving a review', 'post-smtp' ),
517
+ esc_html( 'to help spread the word! :D', 'post-smtp' )
518
+ );
519
+ }
520
+
521
+ printf(
522
+ esc_html__( '%1$s Postman needs translators! Please take a moment to %2$s translate a few sentences on-line %3$s', 'post-smtp' ),
523
+ '<p><span>',
524
+ '<a href="https://translate.wordpress.org/projects/wp-plugins/post-smtp/stable">',
525
+ '</a> :-)</span></p>'
526
+ );
527
+ }
528
+ printf(
529
+ '<p><span><b style="
530
+ background-color:#2172b3; color: #fff;">%1$s</b>%2$s</span>&nbsp;<a target="_blank" href="%3$s">%4$s</a></p>',
531
+ esc_html__( 'New for v1.9.8!', 'post-smtp' ),
532
+ esc_html__( ' Fallback - setup a second delivery method when the first one is failing', 'post-smtp' ),
533
+ esc_url( 'https://postmansmtp.com/post-smtp-1-9-7-the-smtp-fallback/' ),
534
+ esc_html__( 'Check the detailes here', 'post-smtp')
535
+ );
536
+
537
+ print '</div>';
538
+
539
+ //Temporary disabled
540
+
541
+ // <div class="ps-home-bottom">
542
+ // <div class="ps-config-bar">
543
+ // <h1>Download Our Featured Plugins For Free</h1>
544
+ // </div>
545
+ // <div class="ps-flex">
546
+ // <div class="ps-email-templates">
547
+ // <h4>Create Fully Responsive Email Templates in Just a Few Minutes With Email Templates</h2>
548
+ // <ul>
549
+ // <li>A free WordPress email template plugin.</li>
550
+ // <li>Quickest way to design elegant responsive emails.</li>
551
+ // <li>Features fully compatible with Postnman SMTP.</li>
552
+ // </ul>
553
+ // <div class="ps-left">
554
+ // <a href="https://wordpress.org/plugins/email-templates/>" class="ps-btn-light-blue" target="_blank">Free Download</a>
555
+ // </div>
556
+ // <div class="ps-right">
557
+ // <img src="echo esc_url( POST_SMTP_ASSETS . 'images/logos/email-templates.jpg' )" />
558
+ // </div>
559
+ // <div class="clear"></div>
560
+ // </div>
561
+ // <div class="ps-login-designer">
562
+ // <h4>Upgrade Your Custom Login Styling Experience With Login Designer</h2>
563
+ // <ul>
564
+ // <li>A free Login customizer plugin for WordPress.</li>
565
+ // <li>Easiest way to customize your website's login page.</li>
566
+ // <li>Installation is free, fun quick, and easy.</li>
567
+ // </ul>
568
+ // <div class="ps-left">
569
+ // <a href="https://wordpress.org/plugins/login-designer/>" class="ps-btn-blue" target="_blank">Free Download</a>
570
+ // </div>
571
+ // <div class="ps-right">
572
+ // <img src="<?php esc_url( POST_SMTP_ASSETS . 'images/logos/login-designer.jpg' )" />
573
+ // </div>
574
+ // <div class="clear"></div>
575
+ // </div>
576
+ // </div>
577
+ // </div>
578
+
579
+ print "</div>";
580
+ print "</div>";
581
+
582
+ }
583
+
584
+ /**
585
+ */
586
+ private function printDeliveryDetails() {
587
+ $currentTransport = PostmanTransportRegistry::getInstance()->getActiveTransport();
588
+ $deliveryDetails = $currentTransport->getDeliveryDetails( $this->options );
589
+ printf(
590
+ '<p><span>%s</span></p>',
591
+ wp_kses_post( $deliveryDetails )
592
+ );
593
+ }
594
+
595
+ /**
596
+ *
597
+ * @param mixed $title
598
+ * @param string $slug
599
+ */
600
+ public static function outputChildPageHeader( $title, $slug = '' ) {
601
+
602
+ $content = '';
603
+ $content .= sprintf( '<h2>%s</h2>', sprintf( __( '%s Setup', 'post-smtp' ), __( 'Post SMTP', 'post-smtp' ) ) );
604
+ $content .= "
605
+ <div id='postman-main-menu' class='post-smtp-welcome-panel {$slug}'>
606
+ <div class='post-smtp-welcome-panel-content'>
607
+ <div class='welcome-panel-column-container'>
608
+ <div class='welcome-panel-last'>
609
+ <div class='ps-left'>
610
+ <h1>{$title}<h1/>
611
+ </div>";
612
+ $content .= sprintf( '<div class="ps-right"><div class="back-to-menu-link"><a href="%s" class="ps-btn-orange" >%s</a></div></div>', PostmanUtils::getSettingsPageUrl(), _x( 'Back To Main Menu', 'Return to main menu link', 'post-smtp' ) );
613
+ $content .= '
614
+ <div class="clear"></div>
615
+ </div>
616
+ </div>
617
+ </div>
618
+ </div>';
619
+
620
+ echo wp_kses_post( $content );
621
+
622
+ }
623
+
624
+ /**
625
+ */
626
+ public function outputPurgeDataContent() {
627
+ $importTitle = __( 'Import', 'post-smtp' );
628
+ $exportTile = __( 'Export', 'post-smtp' );
629
+ $resetTitle = __( 'Reset Plugin', 'post-smtp' );
630
+ $options = $this->options;
631
+ print '<div class="wrap">';
632
+ PostmanViewController::outputChildPageHeader( sprintf( '%s/%s/%s', $importTitle, $exportTile, $resetTitle ) );
633
+ print '<section id="export_settings" class="ps-left">';
634
+ printf( '<h3><span>%s<span></h3>', esc_html( $exportTile ) );
635
+ printf( '<p><span>%s</span></p>', esc_html__( 'Copy this data into another instance of Postman to duplicate the configuration.', 'post-smtp' ) );
636
+ $data = '';
637
+ if ( ! PostmanPreRequisitesCheck::checkZlibEncode() ) {
638
+ $extraDeleteButtonAttributes = sprintf( 'disabled="true"' );
639
+ $data = '';
640
+ } else {
641
+ $extraDeleteButtonAttributes = '';
642
+ if ( ! $options->isNew() ) {
643
+ $data = $options->export();
644
+ }
645
+ }
646
+ printf(
647
+ '<textarea cols="80" rows="10" class="ps-textarea" readonly="true" name="settings" %s>%s</textarea>',
648
+ esc_attr( $extraDeleteButtonAttributes ), esc_textarea( $data )
649
+ );
650
+ print '</section>';
651
+ print '<section id="import_settings" class="ps-right">';
652
+ printf(
653
+ '<h3><span>%s<span></h3>',
654
+ esc_html( $importTitle )
655
+ );
656
+ print '<form method="POST" action="' . esc_attr( get_admin_url() ) . 'admin-post.php">';
657
+ wp_nonce_field( PostmanAdminController::IMPORT_SETTINGS_SLUG );
658
+ printf(
659
+ '<input type="hidden" name="action" value="%s" />',
660
+ esc_attr( PostmanAdminController::IMPORT_SETTINGS_SLUG )
661
+ );
662
+ print '<p>';
663
+ printf(
664
+ '<span>%s</span>',
665
+ esc_html__( 'Paste data from another instance of Postman here to duplicate the configuration.', 'post-smtp' )
666
+ );
667
+ if ( PostmanTransportRegistry::getInstance()->getSelectedTransport()->isOAuthUsed( PostmanOptions::getInstance()->getAuthenticationType() ) ) {
668
+ $warning = __( 'Warning', 'post-smtp' );
669
+ $errorMessage = __( 'Using the same OAuth 2.0 Client ID and Client Secret from this site at the same time as another site will cause failures.', 'post-smtp' );
670
+ printf( ' <span><b>%s</b>: %s</span>', esc_html( $warning ), esc_html( $errorMessage ) );
671
+ }
672
+ print '</p>';
673
+ printf(
674
+ '<textarea cols="80" rows="10" class="ps-textarea" name="settings" %s></textarea>',
675
+ esc_textarea( $extraDeleteButtonAttributes )
676
+ );
677
+ submit_button( __( 'Import', 'post-smtp' ), 'ps-btn-orange', 'import', true, $extraDeleteButtonAttributes );
678
+ print '</form>';
679
+ print '</section>';
680
+ print '<div class="clear"></div>';
681
+ print '<section id="delete_settings">';
682
+ printf( '<h3><span>%s<span></h3>', esc_html( $resetTitle ) );
683
+ print '<form class="post-smtp-reset-options" method="POST" action="' . esc_attr( get_admin_url() ) . 'admin-post.php">';
684
+ wp_nonce_field( PostmanAdminController::PURGE_DATA_SLUG );
685
+ printf(
686
+ '<input type="hidden" name="action" value="%s" />',
687
+ esc_attr( PostmanAdminController::PURGE_DATA_SLUG )
688
+ );
689
+ printf(
690
+ '<p><span>%s</span></p><p><span>%s</span></p>',
691
+ esc_html__( 'This will purge all of Postman\'s settings, including account credentials and the email log.', 'post-smtp' ),
692
+ esc_html__( 'Are you sure?', 'post-smtp' )
693
+ );
694
+
695
+ submit_button( $resetTitle, 'delete ps-btn-red', 'submit', true );
696
+ print '</form>';
697
+ print '</section>';
698
+ print '</div>';
699
+ }
700
+
701
+ public function google_less_secure_notice() {
702
+
703
+ ?>
704
+ <div class="notice notice-error is-dismissible ps-less-secure-notice">
705
+ <?php
706
+ printf(
707
+ '<p>
708
+ %1$s
709
+ <a href="%2$s" target="blank">%3$s</a>
710
+ %4$s
711
+ <a href="%5$s" target="blank">%6$s</a>
712
+ %7$s
713
+ <br />
714
+ <a href="%8$s" target="_blank">%9$s</a>
715
+ <br />
716
+ <a href="" id="discard-less-secure-notification">%10$s</a>
717
+ </p>',
718
+ esc_html__( 'To help keep your account secure, Google will no longer support using third-party apps to sign in to your Google Account using only your username and primary password. You can ', 'post-smtp' ),
719
+ esc_url( 'https://postmansmtp.com/gmail-is-disabling-less-secure-apps-feature-soon/' ),
720
+ esc_html__( 'switch to the Auth 2.0', 'post-smtp' ),
721
+ esc_html__( 'alternative or use your ', 'post-smtp' ),
722
+ esc_url( 'https://postmansmtp.com/documentation/#setting-up-an-app-password-in-your-google-account' ),
723
+ esc_html__( 'App Password', 'post-smtp' ),
724
+ esc_html__( 'option to continue. ', 'post-smtp' ),
725
+ esc_url( 'https://postmansmtp.com/gmail-is-disabling-less-secure-apps' ),
726
+ esc_html__( 'Click here for more info', 'post-smtp' ),
727
+ esc_html__( 'I understand and would like to discard this notice', 'post-smtp' )
728
+ );
729
+ ?>
730
+ </div>
731
+ <?php
732
+
733
+ }
734
+
735
+ /**
736
+ * Discards less secure notification
737
+ *
738
+ * @since 2.1.2
739
+ * @version 1.0
740
+ */
741
+ public function discard_less_secure_notification() {
742
+
743
+ if( !wp_verify_nonce( $_POST['_wp_nonce'], 'less-secure-security' ) ) {
744
+ die( 'Not Secure.' );
745
+ }
746
+
747
+ $result = update_option( 'ps_hide_less_secure', 1 );
748
+
749
+ if( $result ) {
750
+ wp_send_json_success(
751
+ array( 'message' => 'Success' ),
752
+ 200
753
+ );
754
+ }
755
+
756
+ wp_send_json_error(
757
+ array( 'message' => 'Something went wrong' ),
758
+ 500
759
+ );
760
+
761
+ }
762
+ }
763
  }
Postman/PostmanWpMail.php CHANGED
@@ -71,6 +71,8 @@ if ( ! class_exists( 'PostmanWpMail' ) ) {
71
 
72
  /**
73
  * Creates the Message-ID
 
 
74
  *
75
  * @return string
76
  */
@@ -84,7 +86,7 @@ if ( ! class_exists( 'PostmanWpMail' ) ) {
84
  $hostName = php_uname('n');
85
  }
86
 
87
- return $id . '@' . str_replace('www.', '', $hostName);
88
 
89
  }
90
 
71
 
72
  /**
73
  * Creates the Message-ID
74
+ *
75
+ * @since 2.1.4 Added '< >' to prevent from being spam
76
  *
77
  * @return string
78
  */
86
  $hostName = php_uname('n');
87
  }
88
 
89
+ return '<' . $id . '@' . str_replace('www.', '', $hostName) . '>';
90
 
91
  }
92
 
assets/images/icons/hand.png ADDED
Binary file
freemius/assets/img/post-smtp.gif ADDED
Binary file
freemius/includes/class-freemius.php CHANGED
@@ -1,26206 +1,26206 @@
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 2.2.1
38
- *
39
- * @var string
40
- */
41
- private $_premium_plugin_basename;
42
- /**
43
- * @since 1.0.0
44
- *
45
- * @var string
46
- */
47
- private $_free_plugin_basename;
48
- /**
49
- * @since 1.0.0
50
- *
51
- * @var string
52
- */
53
- private $_plugin_dir_path;
54
- /**
55
- * @since 1.0.0
56
- *
57
- * @var string
58
- */
59
- private $_plugin_dir_name;
60
- /**
61
- * @since 1.0.0
62
- *
63
- * @var string
64
- */
65
- private $_plugin_main_file_path;
66
- /**
67
- * @var string[]
68
- */
69
- private $_plugin_data;
70
- /**
71
- * @since 1.0.9
72
- *
73
- * @var string
74
- */
75
- private $_plugin_name;
76
- /**
77
- * @since 1.2.2
78
- *
79
- * @var string
80
- */
81
- private $_module_type;
82
-
83
- #endregion Plugin Info
84
-
85
- /**
86
- * @since 1.0.9
87
- *
88
- * @var bool If false, don't turn Freemius on.
89
- */
90
- private $_is_on;
91
-
92
- /**
93
- * @since 1.1.3
94
- *
95
- * @var bool If false, don't turn Freemius on.
96
- */
97
- private $_is_anonymous;
98
-
99
- /**
100
- * @since 1.0.9
101
- * @var bool If false, issues with connectivity to Freemius API.
102
- */
103
- private $_has_api_connection;
104
-
105
- /**
106
- * @since 1.0.9
107
- * @since 2.0.0 Default to true since we need the property during the instance construction, prior to the dynamic_init() execution.
108
- * @var bool Hints the SDK if plugin can support anonymous mode (if skip connect is visible).
109
- */
110
- private $_enable_anonymous = true;
111
-
112
- /**
113
- * @since 1.1.7.5
114
- * @var bool Hints the SDK if plugin should run in anonymous mode (only adds feedback form).
115
- */
116
- private $_anonymous_mode;
117
-
118
- /**
119
- * @since 1.1.9
120
- * @var bool Hints the SDK if plugin have any free plans.
121
- */
122
- private $_is_premium_only;
123
-
124
- /**
125
- * @since 1.2.1.6
126
- * @var bool Hints the SDK if plugin have premium code version at all.
127
- */
128
- private $_has_premium_version;
129
-
130
- /**
131
- * @since 1.2.1.6
132
- * @var bool Hints the SDK if plugin should ignore pending mode by simulating a skip.
133
- */
134
- private $_ignore_pending_mode;
135
-
136
- /**
137
- * @since 1.0.8
138
- * @var bool Hints the SDK if the plugin has any paid plans.
139
- */
140
- private $_has_paid_plans;
141
-
142
- /**
143
- * @since 1.2.1.5
144
- * @var int Hints the SDK if the plugin offers a trial period. If negative, no trial, if zero - has a trial but
145
- * without a specified period, if positive - the number of trial days.
146
- */
147
- private $_trial_days = - 1;
148
-
149
- /**
150
- * @since 1.2.1.5
151
- * @var bool Hints the SDK if the trial requires a payment method or not.
152
- */
153
- private $_is_trial_require_payment = false;
154
-
155
- /**
156
- * @since 1.0.7
157
- * @var bool Hints the SDK if the plugin is WordPress.org compliant.
158
- */
159
- private $_is_org_compliant;
160
-
161
- /**
162
- * @since 1.0.7
163
- * @var bool Hints the SDK if the plugin is has add-ons.
164
- */
165
- private $_has_addons;
166
-
167
- /**
168
- * @since 2.4.5
169
- * @var string Navigation type: 'menu' or 'tabs'.
170
- */
171
- private $_navigation;
172
-
173
- const NAVIGATION_MENU = 'menu';
174
- const NAVIGATION_TABS = 'tabs';
175
-
176
- /**
177
- * @since 1.1.6
178
- * @var string[]bool.
179
- */
180
- private $_permissions;
181
-
182
- /**
183
- * @var FS_Storage
184
- */
185
- private $_storage;
186
-
187
- /**
188
- * @since 1.2.2.7
189
- * @var FS_Cache_Manager
190
- */
191
- private $_cache;
192
-
193
- /**
194
- * @since 1.0.0
195
- *
196
- * @var FS_Logger
197
- */
198
- private $_logger;
199
- /**
200
- * @since 1.0.4
201
- *
202
- * @var FS_Plugin
203
- */
204
- private $_plugin = false;
205
- /**
206
- * @since 1.0.4
207
- *
208
- * @var FS_Plugin|false
209
- */
210
- private $_parent_plugin = false;
211
- /**
212
- * @since 1.1.1
213
- *
214
- * @var Freemius
215
- */
216
- private $_parent = false;
217
- /**
218
- * @since 1.0.1
219
- *
220
- * @var FS_User
221
- */
222
- private $_user = false;
223
- /**
224
- * @since 1.0.1
225
- *
226
- * @var FS_Site
227
- */
228
- private $_site = false;
229
- /**
230
- * @since 1.0.1
231
- *
232
- * @var FS_Plugin_License
233
- */
234
- private $_license;
235
- /**
236
- * @since 1.0.2
237
- *
238
- * @var FS_Plugin_Plan[]
239
- */
240
- private $_plans = false;
241
- /**
242
- * @var FS_Plugin_License[]
243
- * @since 1.0.5
244
- */
245
- private $_licenses = false;
246
-
247
- /**
248
- * @since 1.0.1
249
- *
250
- * @var FS_Admin_Menu_Manager
251
- */
252
- private $_menu;
253
-
254
- /**
255
- * @var FS_Admin_Notices
256
- */
257
- private $_admin_notices;
258
-
259
- /**
260
- * @since 1.1.6
261
- *
262
- * @var FS_Admin_Notices
263
- */
264
- private static $_global_admin_notices;
265
-
266
- /**
267
- * @var FS_Logger
268
- * @since 1.0.0
269
- */
270
- private static $_static_logger;
271
-
272
- /**
273
- * @var FS_Options
274
- * @since 1.0.2
275
- */
276
- private static $_accounts;
277
-
278
- /**
279
- * @since 1.2.2
280
- *
281
- * @var number
282
- */
283
- private $_module_id;
284
-
285
- /**
286
- * @var Freemius[]
287
- */
288
- private static $_instances = array();
289
-
290
- /**
291
- * @since 1.2.3
292
- *
293
- * @var FS_Affiliate
294
- */
295
- private $affiliate = null;
296
-
297
- /**
298
- * @since 1.2.3
299
- *
300
- * @var FS_AffiliateTerms
301
- */
302
- private $plugin_affiliate_terms = null;
303
-
304
- /**
305
- * @since 1.2.3
306
- *
307
- * @var FS_AffiliateTerms
308
- */
309
- private $custom_affiliate_terms = null;
310
-
311
- /**
312
- * @since 2.0.0
313
- *
314
- * @var bool
315
- */
316
- private $_is_multisite_integrated;
317
-
318
- /**
319
- * @since 2.0.0
320
- *
321
- * @var bool True if the current request is for a network admin screen and the plugin is network active.
322
- */
323
- private $_is_network_active;
324
-
325
- /**
326
- * @since 2.0.0
327
- *
328
- * @var int|null The original blog ID the plugin was loaded with.
329
- */
330
- private $_blog_id = null;
331
-
332
- /**
333
- * @since 2.0.0
334
- *
335
- * @var int|null The current execution context. When true, run on network context. When int, run on the specified blog context.
336
- */
337
- private $_context_is_network_or_blog_id = null;
338
-
339
- /**
340
- * @since 2.0.0
341
- *
342
- * @var string
343
- */
344
- private $_dynamically_added_top_level_page_hook_name = '';
345
-
346
- /**
347
- * @author Leo Fajardo (@leorw)
348
- * @since 2.3.1
349
- *
350
- * @var bool
351
- */
352
- private $is_whitelabeled;
353
-
354
- /**
355
- * @author Leo Fajardo (@leorw)
356
- * @since 2.4.0
357
- *
358
- * @var bool
359
- */
360
- private $_is_bundle_license_auto_activation_enabled = false;
361
-
362
- #region Uninstall Reasons IDs
363
-
364
- const REASON_NO_LONGER_NEEDED = 1;
365
- const REASON_FOUND_A_BETTER_PLUGIN = 2;
366
- const REASON_NEEDED_FOR_A_SHORT_PERIOD = 3;
367
- const REASON_BROKE_MY_SITE = 4;
368
- const REASON_SUDDENLY_STOPPED_WORKING = 5;
369
- const REASON_CANT_PAY_ANYMORE = 6;
370
- const REASON_OTHER = 7;
371
- const REASON_DIDNT_WORK = 8;
372
- const REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION = 9;
373
- const REASON_COULDNT_MAKE_IT_WORK = 10;
374
- const REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE = 11;
375
- const REASON_NOT_WORKING = 12;
376
- const REASON_NOT_WHAT_I_WAS_LOOKING_FOR = 13;
377
- const REASON_DIDNT_WORK_AS_EXPECTED = 14;
378
- const REASON_TEMPORARY_DEACTIVATION = 15;
379
-
380
- /**
381
- * @author Leo Fajardo (@leorw)
382
- * @since 2.3.1
383
- *
384
- * @var boolean|null
385
- */
386
- private $_use_external_pricing = null;
387
- /**
388
- * @author Leo Fajardo (@leorw)
389
- * @since 2.4.2
390
- *
391
- * @var string|null
392
- */
393
- private $_pricing_js_path = null;
394
-
395
- #endregion
396
-
397
- /* Ctor
398
- ------------------------------------------------------------------------------------------------------------------*/
399
-
400
- /**
401
- * Main singleton instance.
402
- *
403
- * @author Vova Feldman (@svovaf)
404
- * @since 1.0.0
405
- *
406
- * @param number $module_id
407
- * @param string|bool $slug
408
- * @param bool $is_init Since 1.2.1 Is initiation sequence.
409
- */
410
- private function __construct( $module_id, $slug = false, $is_init = false ) {
411
- $main_file = false;
412
-
413
- if ( $is_init && is_numeric( $module_id ) && is_string( $slug ) ) {
414
- $main_file = $this->store_id_slug_type_path_map( $module_id, $slug );
415
- }
416
-
417
- $this->_module_id = $module_id;
418
- $this->_slug = $this->get_slug();
419
- $this->_module_type = $this->get_module_type();
420
-
421
- $this->_blog_id = is_multisite() ? get_current_blog_id() : null;
422
-
423
- $this->_storage = FS_Storage::instance( $this->_module_type, $this->_slug );
424
-
425
- $this->_cache = FS_Cache_Manager::get_manager( WP_FS___OPTION_PREFIX . "cache_{$module_id}" );
426
-
427
- $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $this->get_unique_affix(), WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
428
-
429
- $this->_plugin_main_file_path = $this->_find_caller_plugin_file( $is_init, $main_file );
430
- $this->_plugin_dir_path = plugin_dir_path( $this->_plugin_main_file_path );
431
- $this->_plugin_basename = $this->get_plugin_basename();
432
- $this->_free_plugin_basename = str_replace( '-premium/', '/', $this->_plugin_basename );
433
-
434
- $this->_is_multisite_integrated = (
435
- defined( "WP_FS__PRODUCT_{$module_id}_MULTISITE" ) &&
436
- ( true === constant( "WP_FS__PRODUCT_{$module_id}_MULTISITE" ) )
437
- );
438
-
439
- $this->_is_network_active = (
440
- is_multisite() &&
441
- $this->_is_multisite_integrated &&
442
- // Themes are always network activated, but the ACTUAL activation is per site.
443
- $this->is_plugin() &&
444
- (
445
- is_plugin_active_for_network( $this->_plugin_basename ) ||
446
- // Plugin network level activation or uninstall.
447
- ( fs_is_network_admin() && is_plugin_inactive( $this->_plugin_basename ) )
448
- )
449
- );
450
-
451
- $this->_storage->set_network_active(
452
- $this->_is_network_active,
453
- $this->is_delegated_connection()
454
- );
455
-
456
- if ( ! isset( $this->_storage->is_network_activated ) ) {
457
- $this->_storage->is_network_activated = $this->_is_network_active;
458
- }
459
-
460
- if ( $this->_storage->is_network_activated != $this->_is_network_active ) {
461
- // Update last activation level.
462
- $this->_storage->is_network_activated = $this->_is_network_active;
463
-
464
- $this->maybe_adjust_storage();
465
- }
466
-
467
- #region Migration
468
-
469
- if ( is_multisite() ) {
470
- /**
471
- * If the install_timestamp exists on the site level but doesn't exist on the
472
- * network level storage, it means that we need to process the storage with migration.
473
- *
474
- * The code in this `if` scope will only be executed once and only for the first site that will execute it because once we migrate the storage data, install_timestamp will be already set in the network level storage.
475
- *
476
- * @author Vova Feldman (@svovaf)
477
- * @since 2.0.0
478
- */
479
- if ( false === $this->_storage->get( 'install_timestamp', false, true ) &&
480
- false !== $this->_storage->get( 'install_timestamp', false, false )
481
- ) {
482
- // Initiate storage migration.
483
- $this->_storage->migrate_to_network();
484
-
485
- // Migrate module cache to network level storage.
486
- $this->_cache->migrate_to_network();
487
- }
488
- }
489
-
490
- #endregion
491
-
492
- $base_name_split = explode( '/', $this->_plugin_basename );
493
- $this->_plugin_dir_name = $base_name_split[0];
494
-
495
- if ( $this->_logger->is_on() ) {
496
- $this->_logger->info( 'plugin_main_file_path = ' . $this->_plugin_main_file_path );
497
- $this->_logger->info( 'plugin_dir_path = ' . $this->_plugin_dir_path );
498
- $this->_logger->info( 'plugin_basename = ' . $this->_plugin_basename );
499
- $this->_logger->info( 'free_plugin_basename = ' . $this->_free_plugin_basename );
500
- $this->_logger->info( 'plugin_dir_name = ' . $this->_plugin_dir_name );
501
- }
502
-
503
- // Remember link between file to slug.
504
- $this->store_file_slug_map();
505
-
506
- // Store plugin's initial install timestamp.
507
- if ( ! isset( $this->_storage->install_timestamp ) ) {
508
- $this->_storage->install_timestamp = WP_FS__SCRIPT_START_TIME;
509
- }
510
-
511
- if ( ! is_object( $this->_plugin ) ) {
512
- $this->_plugin = FS_Plugin_Manager::instance( $this->_module_id )->get();
513
- }
514
-
515
- $this->_admin_notices = FS_Admin_Notices::instance(
516
- $this->_slug . ( $this->is_theme() ? ':theme' : '' ),
517
- /**
518
- * Ensure that the admin notice will always have a title by using the stored plugin title if available and
519
- * retrieving the title via the "get_plugin_name" method if there is no stored plugin title available.
520
- *
521
- * @author Leo Fajardo (@leorw)
522
- * @since 1.2.2
523
- */
524
- ( is_object( $this->_plugin ) && isset( $this->_plugin->title ) ?
525
- $this->_plugin->title :
526
- $this->get_plugin_name()
527
- ),
528
- $this->get_unique_affix()
529
- );
530
-
531
- if ( 'true' === fs_request_get( 'fs_clear_api_cache' ) ||
532
- fs_request_is_action( 'restart_freemius' )
533
- ) {
534
- FS_Api::clear_cache();
535
- $this->_cache->clear();
536
- }
537
-
538
- $this->register_constructor_hooks();
539
-
540
- /**
541
- * Starting from version 2.0.0, `FS_Site` entities no longer have the `plan` property and have `plan_id`
542
- * instead. This should be called before calling `_load_account()`, otherwise, `$this->_site` will not be
543
- * loaded in `_load_account` for versions of SDK starting from 2.0.0.
544
- *
545
- * @author Leo Fajardo (@leorw)
546
- */
547
- self::migrate_install_plan_to_plan_id( $this->_storage );
548
-
549
- $this->_load_account();
550
-
551
- $this->_version_updates_handler();
552
- }
553
-
554
- /**
555
- * @author Leo Fajardo (@leorw)
556
- * @since 2.3.0
557
- */
558
- private function maybe_adjust_storage() {
559
- $install_timestamp = null;
560
- $prev_is_premium = null;
561
-
562
- $options_to_update = array();
563
-
564
- $is_network_admin = fs_is_network_admin();
565
-
566
- $network_install_timestamp = $this->_storage->get( 'install_timestamp', null, true );
567
-
568
- if ( ! $is_network_admin ) {
569
- if ( is_null( $network_install_timestamp ) ) {
570
- // Plugin was not network-activated before.
571
- return;
572
- }
573
-
574
- if ( is_null( $this->_storage->get( 'install_timestamp', null, false ) ) ) {
575
- // Set the `install_timestamp` only if it's not yet set.
576
- $install_timestamp = $network_install_timestamp;
577
- }
578
-
579
- $prev_is_premium = $this->_storage->get( 'prev_is_premium', null, true );
580
- } else {
581
- $current_wp_user = self::_get_current_wp_user();
582
- $current_fs_user = self::_get_user_by_email( $current_wp_user->user_email );
583
- $network_user_info = array();
584
-
585
- $skips_count = 0;
586
-
587
- $sites = self::get_sites();
588
- $sites_count = count( $sites );
589
-
590
- $blog_id_2_install_map = array();
591
-
592
- $is_first_non_ignored_blog = true;
593
-
594
- foreach ( $sites as $site ) {
595
- $blog_id = self::get_site_blog_id( $site );
596
-
597
- $blog_install_timestamp = $this->_storage->get( 'install_timestamp', null, $blog_id );
598
-
599
- if ( is_null( $blog_install_timestamp ) ) {
600
- // Plugin has not been installed on this blog.
601
- continue;
602
- }
603
-
604
- $is_earlier_install = (
605
- ! is_null( $install_timestamp ) &&
606
- $blog_install_timestamp < $install_timestamp
607
- );
608
-
609
- $install = $this->get_install_by_blog_id( $blog_id );
610
-
611
- $update_network_user_info = false;
612
-
613
- if ( ! is_object( $install ) ) {
614
- if ( ! $this->_storage->get( 'is_anonymous', false, $blog_id ) ) {
615
- // The opt-in decision (whether to skip or opt in) is yet to be made.
616
- continue;
617
- }
618
-
619
- $skips_count ++;
620
- } else {
621
- $blog_id_2_install_map[ $blog_id ] = $install;
622
-
623
- if ( empty( $network_user_info ) ) {
624
- // Set the network user info for the 1st time. Choose any user information whether or not it is for the current WP user.
625
- $update_network_user_info = true;
626
- }
627
-
628
- if ( ! $update_network_user_info &&
629
- is_object( $current_fs_user ) &&
630
- $network_user_info['user_id'] != $current_fs_user->id &&
631
- $install->user_id == $current_fs_user->id
632
- ) {
633
- // If an install that is owned by the current WP user is found, use its user information instead.
634
- $update_network_user_info = true;
635
- }
636
-
637
- if ( ! $update_network_user_info &&
638
- $is_earlier_install &&
639
- ( ! is_object( $current_fs_user ) || $current_fs_user->id == $install->user_id )
640
- ) {
641
- // Update to the earliest install info if there's no install found so far that is owned by the current WP user; OR only if the found install is owned by the current WP user.
642
- $update_network_user_info = true;
643
- }
644
- }
645
-
646
- if ( $update_network_user_info ) {
647
- $network_user_info = array(
648
- 'user_id' => $install->user_id,
649
- 'blog_id' => $blog_id
650
- );
651
- }
652
-
653
- $site_prev_is_premium = $this->_storage->get( 'prev_is_premium', null, $blog_id );
654
-
655
- if ( $is_first_non_ignored_blog ) {
656
- $prev_is_premium = $site_prev_is_premium;
657
-
658
- if ( is_null( $network_install_timestamp ) ) {
659
- $install_timestamp = $blog_install_timestamp;
660
- }
661
-
662
- $is_first_non_ignored_blog = false;
663
-
664
- continue;
665
- }
666
-
667
- if ( ! is_null( $prev_is_premium ) && $prev_is_premium !== $site_prev_is_premium ) {
668
- // If a different `$site_prev_is_premium` value is found, do not include the option in the collection of options to update.
669
- $prev_is_premium = null;
670
- }
671
-
672
- if ( $is_earlier_install ) {
673
- // If an earlier install timestamp is found.
674
- $install_timestamp = $blog_install_timestamp;
675
- }
676
- }
677
-
678
- $installs_count = count( $blog_id_2_install_map );
679
-
680
- if ( $sites_count === ( $installs_count + $skips_count ) ) {
681
- if ( ! empty( $network_user_info ) ) {
682
- $options_to_update['network_user_id'] = $network_user_info['user_id'];
683
- $options_to_update['network_install_blog_id'] = $network_user_info['blog_id'];
684
-
685
- foreach ( $blog_id_2_install_map as $blog_id => $install ) {
686
- if ( $install->user_id == $network_user_info['user_id'] ) {
687
- continue;
688
- }
689
-
690
- $this->_storage->store( 'is_delegated_connection', true, $blog_id );
691
- }
692
- }
693
-
694
- if ( $sites_count === $skips_count ) {
695
- /**
696
- * Assume network-level skipping as the intended action if all actions identified were only
697
- * skipping of the connection (i.e., no opt-ins and delegated connections so far).
698
- */
699
- $options_to_update['is_anonymous_ms'] = true;
700
- } else if ( $sites_count === $installs_count ) {
701
- /**
702
- * Assume network-level opt-in as the intended action if all actions identified were only opt-ins
703
- * (i.e., no delegation and skipping of the connections so far).
704
- */
705
- $options_to_update['is_network_connected'] = true;
706
- }
707
- }
708
- }
709
-
710
- if ( ! is_null( $install_timestamp ) ) {
711
- $options_to_update['install_timestamp'] = $install_timestamp;
712
- }
713
-
714
- if ( ! is_null( $prev_is_premium ) ) {
715
- $options_to_update['prev_is_premium'] = $prev_is_premium;
716
- }
717
-
718
- if ( ! empty( $options_to_update ) ) {
719
- $this->adjust_storage( $options_to_update, $is_network_admin );
720
- }
721
- }
722
-
723
- /**
724
- * @author Leo Fajardo (@leorw)
725
- * @since 2.3.0
726
- *
727
- * @param array $options
728
- * @param bool $is_network_admin
729
- */
730
- private function adjust_storage( $options, $is_network_admin ) {
731
- foreach ( $options as $name => $value ) {
732
- $this->_storage->store( $name, $value, $is_network_admin ? true : null );
733
- }
734
- }
735
-
736
- /**
737
- * Checks whether this module has a settings menu.
738
- *
739
- * @author Leo Fajardo (@leorw)
740
- * @since 1.2.2
741
- *
742
- * @return bool
743
- */
744
- function has_settings_menu() {
745
- return ( $this->_is_network_active && fs_is_network_admin() ) ?
746
- $this->_menu->has_network_menu() :
747
- $this->_menu->has_menu();
748
- }
749
-
750
- /**
751
- * If `true` the opt-in should be shown as a modal dialog box on the themes.php page. WordPress.org themes guidelines prohibit from redirecting the user from the themes.php page after activating a theme.
752
- *
753
- * @author Vova Feldman (@svovaf)
754
- * @since 2.4.5
755
- *
756
- * @return bool
757
- */
758
- function show_opt_in_on_themes_page() {
759
- if ( ! $this->is_free_wp_org_theme() ) {
760
- return false;
761
- }
762
-
763
- if ( ! $this->has_settings_menu() ) {
764
- return true;
765
- }
766
-
767
- return $this->show_settings_with_tabs();
768
- }
769
-
770
- /**
771
- * If `true` the opt-in should be shown on the product's main setting page.
772
- *
773
- * @author Vova Feldman (@svovaf)
774
- * @since 2.4.5
775
- *
776
- * @return bool
777
- *
778
- * @uses show_opt_in_on_themes_page();
779
- */
780
- function show_opt_in_on_setting_page() {
781
- return ! $this->show_opt_in_on_themes_page();
782
- }
783
-
784
- /**
785
- * If `true` the settings should be shown using tabs.
786
- *
787
- * @author Vova Feldman (@svovaf)
788
- * @since 2.4.5
789
- *
790
- * @return bool
791
- */
792
- function show_settings_with_tabs() {
793
- return ( self::NAVIGATION_TABS === $this->_navigation );
794
- }
795
-
796
- /**
797
- * Check if the context module is free wp.org theme.
798
- *
799
- * This method is helpful because:
800
- * 1. wp.org themes are limited to a single submenu item,
801
- * and sub-submenu items are most likely not allowed (never verified).
802
- * 2. wp.org themes are not allowed to redirect the user
803
- * after the theme activation, therefore, the agreed UX
804
- * is showing the opt-in as a modal dialog box after
805
- * activation (approved by @otto42, @emiluzelac, @greenshady, @grapplerulrich).
806
- *
807
- * @author Vova Feldman (@svovaf)
808
- * @since 1.2.2.7
809
- *
810
- * @return bool
811
- */
812
- function is_free_wp_org_theme() {
813
- return (
814
- $this->is_theme() &&
815
- $this->is_org_repo_compliant() &&
816
- ! $this->is_premium()
817
- );
818
- }
819
-
820
- /**
821
- * Checks whether this a submenu item is visible.
822
- *
823
- * @author Vova Feldman (@svovaf)
824
- * @since 1.2.2.6
825
- * @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.
826
- *
827
- * @param string $slug
828
- * @param bool $is_tabs_visibility_check This is used to decide if the associated tab should be shown or hidden.
829
- *
830
- * @return bool
831
- */
832
- function is_submenu_item_visible( $slug, $is_tabs_visibility_check = false ) {
833
- if ( $this->is_admin_page( $slug ) ) {
834
- /**
835
- * It is the current context page, so show the submenu item
836
- * so the user will have the right context page, even if it
837
- * was set to hidden.
838
- */
839
- return true;
840
- }
841
-
842
- if ( ! $this->has_settings_menu() ) {
843
- // No menu settings at all.
844
- return false;
845
- }
846
-
847
- if (
848
- ! $is_tabs_visibility_check &&
849
- $this->is_org_repo_compliant() &&
850
- $this->show_settings_with_tabs()
851
- ) {
852
- /**
853
- * wp.org themes are limited to a single submenu item, and
854
- * sub-submenu items are most likely not allowed (never verified).
855
- */
856
- return false;
857
- }
858
-
859
- return $this->_menu->is_submenu_item_visible( $slug );
860
- }
861
-
862
- /**
863
- * Check if a Freemius page should be accessible via the UI.
864
- *
865
- * @author Vova Feldman (@svovaf)
866
- * @since 1.2.2.7
867
- *
868
- * @param string $slug
869
- *
870
- * @return bool
871
- */
872
- function is_page_visible( $slug ) {
873
- if ( $this->is_admin_page( $slug ) ) {
874
- return true;
875
- }
876
-
877
- return $this->_menu->is_submenu_item_visible( $slug, true, true );
878
- }
879
-
880
- /**
881
- * @author Vova Feldman (@svovaf)
882
- * @since 1.0.9
883
- */
884
- private function _version_updates_handler() {
885
- if ( ! isset( $this->_storage->sdk_version ) || $this->_storage->sdk_version != $this->version ) {
886
- // Freemius version upgrade mode.
887
- $this->_storage->sdk_last_version = $this->_storage->sdk_version;
888
- $this->_storage->sdk_version = $this->version;
889
-
890
- if ( empty( $this->_storage->sdk_last_version ) ||
891
- version_compare( $this->_storage->sdk_last_version, $this->version, '<' )
892
- ) {
893
- $this->_storage->sdk_upgrade_mode = true;
894
- $this->_storage->sdk_downgrade_mode = false;
895
- } else {
896
- $this->_storage->sdk_downgrade_mode = true;
897
- $this->_storage->sdk_upgrade_mode = false;
898
-
899
- }
900
-
901
- $this->do_action( 'sdk_version_update', $this->_storage->sdk_last_version, $this->version );
902
- }
903
-
904
- $plugin_version = $this->get_plugin_version();
905
- if ( ! isset( $this->_storage->plugin_version ) || $this->_storage->plugin_version != $plugin_version ) {
906
- // Plugin version upgrade mode.
907
- $this->_storage->plugin_last_version = $this->_storage->plugin_version;
908
- $this->_storage->plugin_version = $plugin_version;
909
-
910
- if ( empty( $this->_storage->plugin_last_version ) ||
911
- version_compare( $this->_storage->plugin_last_version, $plugin_version, '<' )
912
- ) {
913
- $this->_storage->plugin_upgrade_mode = true;
914
- $this->_storage->plugin_downgrade_mode = false;
915
- } else {
916
- $this->_storage->plugin_downgrade_mode = true;
917
- $this->_storage->plugin_upgrade_mode = false;
918
- }
919
-
920
- if ( ! empty( $this->_storage->plugin_last_version ) ) {
921
- // Different version of the plugin was installed before, therefore it's an update.
922
- $this->_storage->is_plugin_new_install = false;
923
- }
924
-
925
- $this->do_action( 'plugin_version_update', $this->_storage->plugin_last_version, $plugin_version );
926
- }
927
- }
928
-
929
- #--------------------------------------------------------------------------------
930
- #region Data Migration on SDK Update
931
- #--------------------------------------------------------------------------------
932
-
933
- /**
934
- * @author Vova Feldman (@svovaf)
935
- * @since 1.1.5
936
- *
937
- * @param string $sdk_prev_version
938
- * @param string $sdk_version
939
- */
940
- function _sdk_version_update( $sdk_prev_version, $sdk_version ) {
941
- /**
942
- * @since 1.1.7.3 Fixed unwanted connectivity test cleanup.
943
- */
944
- if ( empty( $sdk_prev_version ) ) {
945
- return;
946
- }
947
-
948
- if ( version_compare( $sdk_prev_version, '2.1.0', '<' ) &&
949
- version_compare( $sdk_version, '2.1.0', '>=' )
950
- ) {
951
- $this->_storage->handle_gdpr_admin_notice = true;
952
- }
953
-
954
- if ( version_compare( $sdk_prev_version, '2.0.0', '<' ) &&
955
- version_compare( $sdk_version, '2.0.0', '>=' )
956
- ) {
957
- $this->migrate_to_subscriptions_collection();
958
-
959
- $this->consolidate_licenses();
960
-
961
- // Clear trial_plan since it's now loaded from the plans collection when needed.
962
- $this->_storage->remove( 'trial_plan', true, false );
963
- }
964
-
965
- if ( version_compare( $sdk_prev_version, '1.2.3', '<' ) &&
966
- version_compare( $sdk_version, '1.2.3', '>=' )
967
- ) {
968
- /**
969
- * Starting from version 1.2.3, paths are stored as relative instead of absolute and some of them can be
970
- * invalid.
971
- *
972
- * @author Leo Fajardo (@leorw)
973
- */
974
- $this->remove_invalid_paths();
975
- }
976
-
977
- if ( version_compare( $sdk_prev_version, '1.1.5', '<' ) &&
978
- version_compare( $sdk_version, '1.1.5', '>=' )
979
- ) {
980
- // On version 1.1.5 merged connectivity and is_on data.
981
- if ( isset( $this->_storage->connectivity_test ) ) {
982
- if ( ! isset( $this->_storage->is_on ) ) {
983
- unset( $this->_storage->connectivity_test );
984
- } else {
985
- $connectivity_data = $this->_storage->connectivity_test;
986
- $connectivity_data['is_active'] = $this->_storage->is_on['is_active'];
987
- $connectivity_data['timestamp'] = $this->_storage->is_on['timestamp'];
988
-
989
- // Override.
990
- $this->_storage->connectivity_test = $connectivity_data;
991
-
992
- // Remove previous structure.
993
- unset( $this->_storage->is_on );
994
- }
995
-
996
- }
997
- }
998
-
999
- if (
1000
- version_compare( $sdk_prev_version, '2.2.1', '<' ) &&
1001
- version_compare( $sdk_version, '2.2.1', '>=' )
1002
- ) {
1003
- /**
1004
- * Clear the file cache without storing the previous path since it could be a wrong path. For example,
1005
- * in the versions of the SDK lower than 2.2.1, it's possible for the path of an add-on to be the same
1006
- * as the parent plugin's when the add-on was auto-installed since the relevant method names were not
1007
- * skipped in the logic that determines the right path in the `get_caller_main_file_and_type` method
1008
- * (e.g. `try_activate_plugin`). Since it was an auto-installation, the caller was the parent plugin
1009
- * and so its path was used. In case the stored path is wrong, clearing the cache will resolve issues
1010
- * related to data mix-up between plugins (e.g. titles and versions of an add-on and its parent plugin).
1011
- *
1012
- * @author Leo Fajardo (@leorw)
1013
- * @since 2.2.1
1014
- */
1015
- $this->clear_module_main_file_cache( false );
1016
- }
1017
- }
1018
-
1019
- /**
1020
- * @author Leo Fajardo (@leorw)
1021
- * @since 2.0.0
1022
- *
1023
- * @param \FS_Storage $storage
1024
- * @param bool|int|null $blog_id
1025
- */
1026
- private static function migrate_install_plan_to_plan_id( FS_Storage $storage, $blog_id = null ) {
1027
- if ( empty( $storage->sdk_version ) ) {
1028
- // New installation of the plugin, no need to upgrade.
1029
- return;
1030
- }
1031
-
1032
- if ( ! version_compare( $storage->sdk_version, '2.0.0', '<' ) ) {
1033
- // Previous version is >= 2.0.0, so no need to migrate.
1034
- return;
1035
- }
1036
-
1037
- // Alias.
1038
- $module_type = $storage->get_module_type();
1039
- $module_slug = $storage->get_module_slug();
1040
-
1041
- $installs = self::get_all_sites( $module_type, $blog_id );
1042
- $install = isset( $installs[ $module_slug ] ) ? $installs[ $module_slug ] : null;
1043
-
1044
- if ( ! is_object( $install ) ) {
1045
- return;
1046
- }
1047
-
1048
- if ( isset( $install->plan ) && is_object( $install->plan ) ) {
1049
- if ( isset( $install->plan->id ) && ! empty( $install->plan->id ) ) {
1050
- $install->plan_id = self::_decrypt( $install->plan->id );
1051
- }
1052
-
1053
- unset( $install->plan );
1054
-
1055
- $installs[ $module_slug ] = clone $install;
1056
-
1057
- self::set_account_option_by_module(
1058
- $module_type,
1059
- 'sites',
1060
- $installs,
1061
- true,
1062
- $blog_id
1063
- );
1064
- }
1065
- }
1066
-
1067
- /**
1068
- * @author Leo Fajardo (@leorw)
1069
- * @since 2.0.0
1070
- */
1071
- private function migrate_to_subscriptions_collection() {
1072
- if ( ! is_object( $this->_site ) ) {
1073
- return;
1074
- }
1075
-
1076
- if ( isset( $this->_storage->subscription ) && is_object( $this->_storage->subscription ) ) {
1077
- $this->_storage->subscriptions = array( fs_get_entity( $this->_storage->subscription, FS_Subscription::get_class_name() ) );
1078
- }
1079
- }
1080
-
1081
- /**
1082
- * @author Leo Fajardo (@leorw)
1083
- * @since 2.0.0
1084
- */
1085
- private function consolidate_licenses() {
1086
- $plugin_licenses = self::get_account_option( 'licenses', WP_FS__MODULE_TYPE_PLUGIN );
1087
- if ( isset( $plugin_licenses[ $this->_slug ] ) ) {
1088
- $plugin_licenses = $plugin_licenses[ $this->_slug ];
1089
- } else {
1090
- $plugin_licenses = array();
1091
- }
1092
-
1093
- $theme_licenses = self::get_account_option( 'licenses', WP_FS__MODULE_TYPE_THEME );
1094
- if ( isset( $theme_licenses[ $this->_slug ] ) ) {
1095
- $theme_licenses = $theme_licenses[ $this->_slug ];
1096
- } else {
1097
- $theme_licenses = array();
1098
- }
1099
-
1100
- if ( empty( $plugin_licenses ) && empty( $theme_licenses ) ) {
1101
- return;
1102
- }
1103
-
1104
- $all_licenses = array();
1105
- $user_id_license_ids_map = array();
1106
-
1107
- foreach ( $plugin_licenses as $user_id => $user_licenses ) {
1108
- if ( is_array( $user_licenses ) ) {
1109
- if ( ! isset( $user_license_ids[ $user_id ] ) ) {
1110
- $user_id_license_ids_map[ $user_id ] = array();
1111
- }
1112
-
1113
- foreach ( $user_licenses as $user_license ) {
1114
- $all_licenses[] = $user_license;
1115
- $user_id_license_ids_map[ $user_id ][] = $user_license->id;
1116
- }
1117
- }
1118
- }
1119
-
1120
- foreach ( $theme_licenses as $user_id => $user_licenses ) {
1121
- if ( is_array( $user_licenses ) ) {
1122
- if ( ! isset( $user_license_ids[ $user_id ] ) ) {
1123
- $user_id_license_ids_map[ $user_id ] = array();
1124
- }
1125
-
1126
- foreach ( $user_licenses as $user_license ) {
1127
- $all_licenses[] = $user_license;
1128
- $user_id_license_ids_map[ $user_id ][] = $user_license->id;
1129
- }
1130
- }
1131
- }
1132
-
1133
- self::store_user_id_license_ids_map(
1134
- $user_id_license_ids_map,
1135
- $this->_module_id
1136
- );
1137
-
1138
- $this->_store_licenses( true, $this->_module_id, $all_licenses );
1139
- }
1140
-
1141
- /**
1142
- * Remove invalid paths.
1143
- *
1144
- * @author Leo Fajardo (@leorw)
1145
- * @since 1.2.3
1146
- */
1147
- private function remove_invalid_paths() {
1148
- // Remove invalid path that is still associated with the current slug if there's any.
1149
- $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
1150
- foreach ( $file_slug_map as $plugin_basename => $slug ) {
1151
- if ( $slug === $this->_slug &&
1152
- $plugin_basename !== $this->_plugin_basename &&
1153
- ! file_exists( $this->get_absolute_path( $plugin_basename ) )
1154
- ) {
1155
- unset( $file_slug_map[ $plugin_basename ] );
1156
- self::$_accounts->set_option( 'file_slug_map', $file_slug_map, true );
1157
-
1158
- break;
1159
- }
1160
- }
1161
- }
1162
-
1163
- /**
1164
- * @author Vova Feldman (@svovaf)
1165
- * @since 1.2.2.7
1166
- *
1167
- * @param string $plugin_prev_version
1168
- * @param string $plugin_version
1169
- */
1170
- function _after_version_update( $plugin_prev_version, $plugin_version ) {
1171
- if ( $this->is_theme() ) {
1172
- // Expire the cache of the previous tabs since the theme may
1173
- // have setting updates.
1174
- $this->_cache->expire( 'tabs' );
1175
- $this->_cache->expire( 'tabs_stylesheets' );
1176
- }
1177
- }
1178
-
1179
- /**
1180
- * A special migration logic for the $_accounts, executed for all the plugins in the system:
1181
- * - Moves some data to the network level storage.
1182
- * - If the plugin's connection was skipped for all sites, set the plugin as if it was network skipped.
1183
- * - If the plugin's connection was ignored for all sites, don't do anything in terms of the network connection.
1184
- * - If the plugin was connected to all sites by the same super-admin, set the plugin as if was network opted-in for all sites.
1185
- * - If there's at least one site that was connected by a super-admin, find the "main super-admin" (the one that installed the majority of the plugin installs) and set the plugin as if was network activated with the main super-admin, set all the sites that were skipped or opted-in with a different user to delegated mode. Then, prompt the currently logged super-admin to choose what to do with the ignored sites.
1186
- * - If there are any sites in the network which the connection decision was not yet taken for, set this plugin into network activation mode so a super-admin can choose what to do with the rest of the sites.
1187
- *
1188
- * @author Vova Feldman (@svovaf)
1189
- * @since 2.0.0
1190
- */
1191
- private static function migrate_accounts_to_network() {
1192
- $sites = self::get_sites();
1193
- $sites_count = count( $sites );
1194
- $connection_status = array();
1195
- $plugin_slugs = array();
1196
- foreach ( $sites as $site ) {
1197
- $blog_id = self::get_site_blog_id( $site );
1198
-
1199
- self::$_accounts->migrate_to_network( $blog_id );
1200
-
1201
- /**
1202
- * Build a list of all Freemius powered plugins slugs.
1203
- */
1204
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array(), $blog_id );
1205
- foreach ( $id_slug_type_path_map as $module_id => $data ) {
1206
- if ( WP_FS__MODULE_TYPE_PLUGIN === $data['type'] ) {
1207
- $plugin_slugs[ $data['slug'] ] = true;
1208
- }
1209
- }
1210
-
1211
- $installs = self::get_account_option( 'sites', WP_FS__MODULE_TYPE_PLUGIN, $blog_id );
1212
-
1213
- if ( is_array( $installs ) ) {
1214
- foreach ( $installs as $slug => $install ) {
1215
- if ( ! isset( $connection_status[ $slug ] ) ) {
1216
- $connection_status[ $slug ] = array();
1217
- }
1218
-
1219
- if ( is_object( $install ) &&
1220
- FS_Site::is_valid_id( $install->id ) &&
1221
- FS_User::is_valid_id( $install->user_id )
1222
- ) {
1223
- $connection_status[ $slug ][ $blog_id ] = $install->user_id;
1224
- }
1225
- }
1226
- }
1227
- }
1228
-
1229
- foreach ( $plugin_slugs as $slug => $true ) {
1230
- if ( ! isset( $connection_status[ $slug ] ) ) {
1231
- $connection_status[ $slug ] = array();
1232
- }
1233
-
1234
- foreach ( $sites as $site ) {
1235
- $blog_id = self::get_site_blog_id( $site );
1236
-
1237
- if ( isset( $connection_status[ $slug ][ $blog_id ] ) ) {
1238
- continue;
1239
- }
1240
-
1241
- $storage = FS_Storage::instance( WP_FS__MODULE_TYPE_PLUGIN, $slug );
1242
-
1243
- $is_anonymous = $storage->get( 'is_anonymous', null, $blog_id );
1244
-
1245
- if ( ! is_null( $is_anonymous ) ) {
1246
- // Since 1.1.3 is_anonymous is an array.
1247
- if ( is_array( $is_anonymous ) && isset( $is_anonymous['is'] ) ) {
1248
- $is_anonymous = $is_anonymous['is'];
1249
- }
1250
-
1251
- if ( is_bool( $is_anonymous ) && true === $is_anonymous ) {
1252
- $connection_status[ $slug ][ $blog_id ] = 'skipped';
1253
- }
1254
- }
1255
-
1256
- if ( ! isset( $connection_status[ $slug ][ $blog_id ] ) ) {
1257
- $connection_status[ $slug ][ $blog_id ] = 'ignored';
1258
- }
1259
- }
1260
- }
1261
-
1262
- $super_admins = array();
1263
-
1264
- foreach ( $connection_status as $slug => $blogs_status ) {
1265
- $skips = 0;
1266
- $ignores = 0;
1267
- $connections = 0;
1268
- $opted_in_users = array();
1269
- $opted_in_super_admins = array();
1270
-
1271
- $storage = FS_Storage::instance( WP_FS__MODULE_TYPE_PLUGIN, $slug );
1272
-
1273
- foreach ( $blogs_status as $blog_id => $status_or_user_id ) {
1274
- if ( 'skipped' === $status_or_user_id ) {
1275
- $skips ++;
1276
- } else if ( 'ignored' === $status_or_user_id ) {
1277
- $ignores ++;
1278
- } else if ( FS_User::is_valid_id( $status_or_user_id ) ) {
1279
- $connections ++;
1280
-
1281
- if ( ! isset( $opted_in_users[ $status_or_user_id ] ) ) {
1282
- $opted_in_users[ $status_or_user_id ] = array();
1283
- }
1284
-
1285
- $opted_in_users[ $status_or_user_id ][] = $blog_id;
1286
-
1287
- if ( isset( $super_admins[ $status_or_user_id ] ) ||
1288
- self::is_super_admin( $status_or_user_id )
1289
- ) {
1290
- // Cache super-admin data.
1291
- $super_admins[ $status_or_user_id ] = true;
1292
-
1293
- // Remember opted-in super-admins for the plugin.
1294
- $opted_in_super_admins[ $status_or_user_id ] = true;
1295
- }
1296
- }
1297
- }
1298
-
1299
- $main_super_admin_user_id = null;
1300
- $all_migrated = false;
1301
- if ( $sites_count == $skips ) {
1302
- // All sites were skipped -> network skip by copying the anonymous mode from any of the sites.
1303
- $storage->is_anonymous_ms = $storage->is_anonymous;
1304
-
1305
- $all_migrated = true;
1306
- } else if ( $sites_count == $ignores ) {
1307
- // Don't do anything, still in activation mode.
1308
-
1309
- $all_migrated = true;
1310
- } else if ( 0 < count( $opted_in_super_admins ) ) {
1311
- // Find the super-admin with the majority of installs.
1312
- $max_installs_by_super_admin = 0;
1313
- foreach ( $opted_in_super_admins as $user_id => $true ) {
1314
- $installs_count = count( $opted_in_users[ $user_id ] );
1315
-
1316
- if ( $installs_count > $max_installs_by_super_admin ) {
1317
- $max_installs_by_super_admin = $installs_count;
1318
- $main_super_admin_user_id = $user_id;
1319
- }
1320
- }
1321
-
1322
- if ( $sites_count == $connections && 1 == count( $opted_in_super_admins ) ) {
1323
- // Super-admin opted-in for all sites in the network.
1324
- $storage->is_network_connected = true;
1325
-
1326
- $all_migrated = true;
1327
- }
1328
-
1329
- // Store network user.
1330
- $storage->network_user_id = $main_super_admin_user_id;
1331
-
1332
- $storage->network_install_blog_id = ( $sites_count == $connections ) ?
1333
- // Since all sites are opted-in, associating with the main site.
1334
- get_current_blog_id() :
1335
- // Associating with the 1st found opted-in site.
1336
- $opted_in_users[ $main_super_admin_user_id ][0];
1337
-
1338
- /**
1339
- * Make sure we migrate the plan ID of the network install, otherwise, if after the migration
1340
- * the 1st page that will be loaded is the network level WP Admin and $storage->network_install_blog_id
1341
- * is different than the main site of the network, the $this->_site will not be set since the plan_id
1342
- * will be empty.
1343
- */
1344
- $storage->migrate_to_network();
1345
- self::migrate_install_plan_to_plan_id( $storage, $storage->network_install_blog_id );
1346
- } else {
1347
- // At least one opt-in. All the opt-in were created by a non-super-admin.
1348
- if ( 0 == $ignores ) {
1349
- // All sites were opted-in or skipped, all by non-super-admin. So delegate all.
1350
- $storage->store( 'is_delegated_connection', true, true );
1351
-
1352
- $all_migrated = true;
1353
- }
1354
- }
1355
-
1356
- if ( ! $all_migrated ) {
1357
- /**
1358
- * Delegate all sites that were:
1359
- * 1) Opted-in by a user that is NOT the main-super-admin.
1360
- * 2) Skipped and non of the sites was opted-in by a super-admin. If any site was opted-in by a super-admin, there will be a main-super-admin, and we consider the skip as if it was done by that user.
1361
- */
1362
- foreach ( $blogs_status as $blog_id => $status_or_user_id ) {
1363
- if ( $status_or_user_id == $main_super_admin_user_id ) {
1364
- continue;
1365
- }
1366
-
1367
- if ( FS_User::is_valid_id( $status_or_user_id ) ||
1368
- ( 'skipped' === $status_or_user_id && is_null( $main_super_admin_user_id ) )
1369
- ) {
1370
- $storage->store( 'is_delegated_connection', true, $blog_id );
1371
- }
1372
- }
1373
- }
1374
-
1375
-
1376
- if ( ( $connections + $skips > 0 ) ) {
1377
- if ( $ignores > 0 ) {
1378
- /**
1379
- * If admin already opted-in or skipped in any of the network sites, and also
1380
- * have sites which the connection decision was not yet taken, set this plugin
1381
- * into network activation mode so the super-admin can choose what to do with
1382
- * the rest of the sites.
1383
- */
1384
- self::set_network_upgrade_mode( $storage );
1385
- }
1386
- }
1387
- }
1388
- }
1389
-
1390
- /**
1391
- * Set a module into network upgrade mode.
1392
- *
1393
- * @author Vova Feldman (@svovaf)
1394
- * @since 2.0.0
1395
- *
1396
- * @param \FS_Storage $storage
1397
- *
1398
- * @return bool
1399
- */
1400
- private static function set_network_upgrade_mode( FS_Storage $storage ) {
1401
- return $storage->is_network_activation = true;
1402
- }
1403
-
1404
- /**
1405
- * Will return true after upgrading to the SDK with the network level integration,
1406
- * when the super-admin involvement is required regarding the rest of the sites.
1407
- *
1408
- * @author Vova Feldman (@svovaf)
1409
- * @since 2.0.0
1410
- *
1411
- * @return bool
1412
- */
1413
- function is_network_upgrade_mode() {
1414
- return $this->_storage->get( 'is_network_activation' );
1415
- }
1416
-
1417
- /**
1418
- * Clear flag after the upgrade mode completion.
1419
- *
1420
- * @author Vova Feldman (@svovaf)
1421
- * @since 2.0.0
1422
- *
1423
- * @return bool True if network activation was on and now completed.
1424
- */
1425
- private function network_upgrade_mode_completed() {
1426
- if ( fs_is_network_admin() && $this->is_network_upgrade_mode() ) {
1427
- $this->_storage->remove( 'is_network_activation' );
1428
-
1429
- return true;
1430
- }
1431
-
1432
- return false;
1433
- }
1434
-
1435
- #endregion
1436
-
1437
- /**
1438
- * This action is connected to the 'plugins_loaded' hook and helps to determine
1439
- * if this is a new plugin installation or a plugin update.
1440
- *
1441
- * There are 3 different use-cases:
1442
- * 1) New plugin installation right with Freemius:
1443
- * 1.1 _activate_plugin_event_hook() will be executed first
1444
- * 1.2 Since $this->_storage->is_plugin_new_install is not set,
1445
- * and $this->_storage->plugin_last_version is not set,
1446
- * $this->_storage->is_plugin_new_install will be set to TRUE.
1447
- * 1.3 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will
1448
- * be already set to TRUE.
1449
- *
1450
- * 2) Plugin update, didn't have Freemius before, and now have the SDK:
1451
- * 2.1 _activate_plugin_event_hook() will not be executed, because
1452
- * the activation hook do NOT fires on updates since WP 3.1.
1453
- * 2.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will
1454
- * be empty, therefore, it will be set to FALSE.
1455
- *
1456
- * 3) Plugin update, had Freemius in prev version as well:
1457
- * 3.1 _version_updates_handler() will be executed 1st, since FS was installed
1458
- * before, $this->_storage->plugin_last_version will NOT be empty,
1459
- * therefore, $this->_storage->is_plugin_new_install will be set to FALSE.
1460
- * 3.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install is
1461
- * already set, therefore, it will not be modified.
1462
- *
1463
- * Use-case #3 is backward compatible, #3.1 will be executed since 1.0.9.
1464
- *
1465
- * NOTE:
1466
- * The only fallback of this mechanism is if an admin updates a plugin based on use-case #2,
1467
- * and then, the next immediate PageView is the plugin's main settings page, it will not
1468
- * show the opt-in right away. The reason it will happen is because Freemius execution
1469
- * will be turned off till the plugin is fully loaded at least once
1470
- * (till $this->_storage->was_plugin_loaded is TRUE).
1471
- *
1472
- * @author Vova Feldman (@svovaf)
1473
- * @since 1.1.9
1474
- *
1475
- */
1476
- function _plugins_loaded() {
1477
- // Update flag that plugin was loaded with Freemius at least once.
1478
- $this->_storage->was_plugin_loaded = true;
1479
-
1480
- /**
1481
- * Bug fix - only set to false when it's a plugin, due to the
1482
- * execution sequence of the theme hooks and our methods, if
1483
- * this will be set for themes, Freemius will always assume
1484
- * it's a theme update.
1485
- *
1486
- * @author Vova Feldman (@svovaf)
1487
- * @since 1.2.2.2
1488
- */
1489
- if ( $this->is_plugin() &&
1490
- ! isset( $this->_storage->is_plugin_new_install )
1491
- ) {
1492
- $this->_storage->is_plugin_new_install = (
1493
- ! is_plugin_active( $this->_plugin_basename ) &&
1494
- empty( $this->_storage->plugin_last_version )
1495
- );
1496
- }
1497
- }
1498
-
1499
- /**
1500
- * Add special parameter to WP admin AJAX calls so when we
1501
- * process AJAX calls we can identify its source properly.
1502
- *
1503
- * @author Leo Fajardo (@leorw)
1504
- * @since 2.0.0
1505
- */
1506
- static function _enrich_ajax_url() {
1507
- $admin_param = is_network_admin() ?
1508
- '_fs_network_admin' :
1509
- '_fs_blog_admin';
1510
- ?>
1511
- <script type="text/javascript">
1512
- (function ($) {
1513
- $(document).ajaxSend(function (event, jqxhr, settings) {
1514
- if (settings.url &&
1515
- -1 < settings.url.indexOf('admin-ajax.php') &&
1516
- ! ( settings.url.indexOf( '<?php echo $admin_param ?>' ) > 0 )
1517
- ) {
1518
- if (
1519
- 'string' === typeof settings.data &&
1520
- settings.data.indexOf( 'action=heartbeat' ) > 0
1521
- ) {
1522
- return;
1523
- }
1524
-
1525
- if (settings.url.indexOf('?') > 0) {
1526
- settings.url += '&';
1527
- } else {
1528
- settings.url += '?';
1529
- }
1530
-
1531
- settings.url += '<?php echo $admin_param ?>=true';
1532
- }
1533
- });
1534
- })(jQuery);
1535
- </script>
1536
- <?php
1537
- }
1538
-
1539
- /**
1540
- * Opens the support forum subemenu item in a new browser page.
1541
- *
1542
- * @author Vova Feldman (@svovaf)
1543
- * @since 2.1.4
1544
- */
1545
- static function _open_support_forum_in_new_page() {
1546
- ?>
1547
- <script type="text/javascript">
1548
- (function ($) {
1549
- $('.fs-submenu-item.wp-support-forum').parent().attr( { target: '_blank', rel: 'noopener noreferrer' } );
1550
- })(jQuery);
1551
- </script>
1552
- <?php
1553
- }
1554
-
1555
- /**
1556
- * @author Vova Feldman (@svovaf)
1557
- * @since 1.0.9
1558
- */
1559
- private function register_constructor_hooks() {
1560
- $this->_logger->entrance();
1561
-
1562
- if ( is_admin() ) {
1563
- add_action( 'admin_init', array( &$this, '_hook_action_links_and_register_account_hooks' ) );
1564
-
1565
- if ( $this->is_plugin() ) {
1566
- if ( self::is_plugin_install_page() && true !== fs_request_get_bool( 'fs_allow_updater_and_dialog' ) ) {
1567
- /**
1568
- * Unless the `fs_allow_updater_and_dialog` URL param exists and its value is `true`, make
1569
- * Freemius-related updates unavailable on the "Add Plugins" admin page (/plugin-install.php)
1570
- * so that they won't interfere with the .org plugins' functionalities on that page (e.g.
1571
- * updating of a .org plugin).
1572
- */
1573
- add_filter( 'site_transient_update_plugins', array( 'Freemius', '_remove_fs_updates_from_plugin_install_page' ), 10, 2 );
1574
- } else if ( self::is_plugins_page() || self::is_updates_page() ) {
1575
- /**
1576
- * On the "Plugins" and "Updates" admin pages, if there are premium or non–org-compliant plugins, modify their details dialog URLs (add a Freemius-specific param) so that the SDK can determine if the plugin information dialog should show information from Freemius.
1577
- *
1578
- * @author Leo Fajardo (@leorw)
1579
- * @since 2.2.3
1580
- */
1581
- add_action( 'admin_footer', array( 'Freemius', '_prepend_fs_allow_updater_and_dialog_flag_url_param' ) );
1582
- }
1583
-
1584
- $plugin_dir = dirname( $this->_plugin_dir_path ) . '/';
1585
-
1586
- /**
1587
- * @since 1.2.2
1588
- *
1589
- * Hook to both free and premium version activations to support
1590
- * auto deactivation on the other version activation.
1591
- */
1592
- register_activation_hook(
1593
- $plugin_dir . $this->_free_plugin_basename,
1594
- array( &$this, '_activate_plugin_event_hook' )
1595
- );
1596
-
1597
- register_activation_hook(
1598
- $plugin_dir . $this->premium_plugin_basename(),
1599
- array( &$this, '_activate_plugin_event_hook' )
1600
- );
1601
- } else {
1602
- add_action( 'after_switch_theme', array( &$this, '_activate_theme_event_hook' ), 10, 2 );
1603
-
1604
- add_action( 'admin_footer', array( &$this, '_style_premium_theme' ) );
1605
- }
1606
-
1607
- /**
1608
- * Part of the mechanism to identify new plugin install vs. plugin update.
1609
- *
1610
- * @author Vova Feldman (@svovaf)
1611
- * @since 1.1.9
1612
- */
1613
- if ( empty( $this->_storage->was_plugin_loaded ) ) {
1614
- /**
1615
- * During the plugin activation (not theme), 'plugins_loaded' will be already executed
1616
- * when the logic gets here since the activation logic first add the activate plugins,
1617
- * then triggers 'plugins_loaded', and only then include the code of the plugin that
1618
- * is activated. Which means that _plugins_loaded() will NOT be executed during the
1619
- * plugin activation, and that IS intentional.
1620
- *
1621
- * @author Vova Feldman (@svovaf)
1622
- */
1623
- if ( $this->is_plugin() &&
1624
- $this->is_activation_mode( false ) &&
1625
- 0 == did_action( 'plugins_loaded' )
1626
- ) {
1627
- add_action( 'plugins_loaded', array( &$this, '_plugins_loaded' ) );
1628
- } else {
1629
- // If was activated before, then it was already loaded before.
1630
- $this->_plugins_loaded();
1631
- }
1632
- }
1633
-
1634
- if ( ! self::is_ajax() ) {
1635
- if ( ! $this->is_addon() ) {
1636
- add_action( 'init', array( &$this, '_add_default_submenu_items' ), WP_FS__LOWEST_PRIORITY );
1637
- }
1638
- }
1639
-
1640
- if ( $this->_storage->handle_gdpr_admin_notice ) {
1641
- add_action( 'init', array( &$this, '_maybe_show_gdpr_admin_notice' ) );
1642
- }
1643
-
1644
- add_action( 'init', array( &$this, '_maybe_add_gdpr_optin_ajax_handler') );
1645
- add_action( 'init', array( &$this, '_maybe_add_pricing_ajax_handler' ) );
1646
- }
1647
-
1648
- if ( $this->is_plugin() ) {
1649
- if ( version_compare( $GLOBALS['wp_version'], '5.1', '<' ) ) {
1650
- add_action( 'wpmu_new_blog', array( $this, '_after_new_blog_callback' ), 10, 6 );
1651
- } else {
1652
- add_action( 'wp_initialize_site', array( $this, '_after_wp_initialize_site_callback' ), 11, 2 );
1653
- }
1654
-
1655
- register_deactivation_hook( $this->_plugin_main_file_path, array( &$this, '_deactivate_plugin_hook' ) );
1656
- }
1657
-
1658
- if ( is_multisite() ) {
1659
- add_action( 'deactivate_blog', array( &$this, '_after_site_deactivated_callback' ) );
1660
- add_action( 'archive_blog', array( &$this, '_after_site_deactivated_callback' ) );
1661
- add_action( 'make_spam_blog', array( &$this, '_after_site_deactivated_callback' ) );
1662
-
1663
- if ( version_compare( $GLOBALS['wp_version'], '5.1', '<' ) ) {
1664
- add_action( 'deleted_blog', array( $this, '_after_site_deleted_callback' ), 10, 2 );
1665
- } else {
1666
- add_action( 'wp_delete_site', array( $this, '_after_wpsite_deleted_callback' ) );
1667
- }
1668
-
1669
- add_action( 'activate_blog', array( &$this, '_after_site_reactivated_callback' ) );
1670
- add_action( 'unarchive_blog', array( &$this, '_after_site_reactivated_callback' ) );
1671
- add_action( 'make_ham_blog', array( &$this, '_after_site_reactivated_callback' ) );
1672
- }
1673
-
1674
- if ( $this->is_theme() &&
1675
- self::is_customizer() &&
1676
- $this->apply_filters( 'show_customizer_upsell', true )
1677
- ) {
1678
- // Register customizer upsell.
1679
- add_action( 'customize_register', array( &$this, '_customizer_register' ) );
1680
- }
1681
-
1682
- add_action( 'admin_init', array( &$this, '_redirect_on_clicked_menu_link' ), WP_FS__LOWEST_PRIORITY );
1683
-
1684
- if ( $this->is_theme() && ! $this->is_migration() ) {
1685
- add_action( 'admin_init', array( &$this, '_add_tracking_links' ) );
1686
- }
1687
-
1688
- add_action( 'admin_init', array( &$this, '_add_license_activation' ) );
1689
- add_action( 'admin_init', array( &$this, '_add_premium_version_upgrade_selection' ) );
1690
- add_action( 'admin_init', array( &$this, '_add_beta_mode_update_handler' ) );
1691
- add_action( 'admin_init', array( &$this, '_add_user_change_option' ) );
1692
- add_action( 'admin_init', array( &$this, '_add_email_address_update_option' ) );
1693
-
1694
- $this->add_ajax_action( 'update_billing', array( &$this, '_update_billing_ajax_action' ) );
1695
- $this->add_ajax_action( 'start_trial', array( &$this, '_start_trial_ajax_action' ) );
1696
- $this->add_ajax_action( 'set_data_debug_mode', array( &$this, '_set_data_debug_mode' ) );
1697
- $this->add_ajax_action( 'toggle_whitelabel_mode', array( &$this, '_toggle_whitelabel_mode_ajax_handler' ) );
1698
-
1699
- if ( $this->_is_network_active && fs_is_network_admin() ) {
1700
- $this->add_ajax_action( 'network_activate', array( &$this, '_network_activate_ajax_action' ) );
1701
- }
1702
-
1703
- $this->add_ajax_action( 'install_premium_version', array(
1704
- &$this,
1705
- '_install_premium_version_ajax_action'
1706
- ) );
1707
-
1708
- $this->add_ajax_action( 'submit_affiliate_application', array( &$this, '_submit_affiliate_application' ) );
1709
-
1710
- $this->add_action( 'after_plans_sync', array( &$this, '_check_for_trial_plans' ) );
1711
-
1712
- $this->add_action( 'sdk_version_update', array( &$this, '_sdk_version_update' ), WP_FS__DEFAULT_PRIORITY, 2 );
1713
-
1714
- $this->add_action(
1715
- 'plugin_version_update',
1716
- array( &$this, '_after_version_update' ),
1717
- WP_FS__DEFAULT_PRIORITY,
1718
- 2
1719
- );
1720
- $this->add_filter( 'after_code_type_change', array( &$this, '_after_code_type_change' ) );
1721
-
1722
- add_action( 'admin_init', array( &$this, '_add_trial_notice' ) );
1723
- add_action( 'admin_init', array( &$this, '_add_affiliate_program_notice' ) );
1724
- add_action( 'admin_enqueue_scripts', array( &$this, '_enqueue_common_css' ) );
1725
-
1726
- /**
1727
- * Handle request to reset anonymous mode for `get_reconnect_url()`.
1728
- *
1729
- * @author Vova Feldman (@svovaf)
1730
- * @since 1.2.1.5
1731
- */
1732
- if ( fs_request_is_action( 'reset_anonymous_mode' ) &&
1733
- $this->get_unique_affix() === fs_request_get( 'fs_unique_affix' )
1734
- ) {
1735
- add_action( 'admin_init', array( &$this, 'connect_again' ) );
1736
- }
1737
- }
1738
-
1739
- /**
1740
- * Register the required hooks right after the settings parse is completed.
1741
- *
1742
- * @author Vova Feldman (@svovaf)
1743
- * @since 2.3.1
1744
- */
1745
- private function register_after_settings_parse_hooks() {
1746
- if ( is_admin() &&
1747
- $this->is_theme() &&
1748
- $this->is_premium() &&
1749
- ! $this->has_active_valid_license()
1750
- ) {
1751
- $this->add_ajax_action(
1752
- 'delete_theme_update_data',
1753
- array( &$this, '_delete_theme_update_data_action' )
1754
- );
1755
- }
1756
-
1757
- if ( $this->show_settings_with_tabs() ) {
1758
- /**
1759
- * Include the required hooks to capture the theme settings' page tabs
1760
- * and cache them.
1761
- *
1762
- * @author Vova Feldman (@svovaf)
1763
- * @since 1.2.2.7
1764
- */
1765
- if ( ! $this->_cache->has_valid( 'tabs' ) ) {
1766
- add_action( 'admin_footer', array( &$this, '_tabs_capture' ) );
1767
- // Add license activation AJAX callback.
1768
- $this->add_ajax_action( 'store_tabs', array( &$this, '_store_tabs_ajax_action' ) );
1769
-
1770
- add_action( 'admin_enqueue_scripts', array( &$this, '_store_tabs_styles' ), 9999999 );
1771
- }
1772
-
1773
- add_action(
1774
- 'admin_footer',
1775
- array( &$this, '_add_freemius_tabs' ),
1776
- /**
1777
- * The tabs JS code must be executed after the tabs capture logic (_tabs_capture()).
1778
- * That's why the priority is 11 while the tabs capture logic is added
1779
- * with priority 10.
1780
- *
1781
- * @author Vova Feldman (@svovaf)
1782
- */
1783
- 11
1784
- );
1785
- }
1786
-
1787
- if ( ! self::is_ajax() ) {
1788
- if ( ! $this->is_addon() || $this->is_only_premium() ) {
1789
- add_action(
1790
- ( $this->_is_network_active && fs_is_network_admin() ? 'network_' : '' ) . 'admin_menu',
1791
- array( &$this, '_prepare_admin_menu' ),
1792
- WP_FS__LOWEST_PRIORITY
1793
- );
1794
- }
1795
- }
1796
- }
1797
-
1798
- /**
1799
- * Makes Freemius-related updates unavailable on the "Add Plugins" admin page (/plugin-install.php) so that
1800
- * they won't interfere with the .org plugins' functionalities on that page (e.g. updating of a .org plugin).
1801
- *
1802
- * @author Leo Fajardo (@leorw)
1803
- * @since 2.2.3
1804
- *
1805
- * @param object $updates
1806
- * @param string|null $transient
1807
- *
1808
- * @return object
1809
- */
1810
- static function _remove_fs_updates_from_plugin_install_page( $updates, $transient = null ) {
1811
- if ( is_object( $updates ) && isset( $updates->response ) ) {
1812
- foreach ( $updates->response as $file => $plugin ) {
1813
- if ( isset( $plugin->package ) && false !== strpos( $plugin->package, 'api.freemius' ) ) {
1814
- unset( $updates->response[ $file ] );
1815
- }
1816
- }
1817
- }
1818
-
1819
- return $updates;
1820
- }
1821
-
1822
- /**
1823
- * Prepends the `fs_allow_updater_and_dialog` param to the plugin information URLs to tell the SDK to handle
1824
- * the information that is shown on the plugin details dialog that is shown when the relevant link is clicked.
1825
- *
1826
- * @author Leo Fajardo (@leorw)
1827
- * @since 2.2.3
1828
- *
1829
- * @return string
1830
- */
1831
- static function _prepend_fs_allow_updater_and_dialog_flag_url_param() {
1832
- $slug_basename_map = array();
1833
- foreach ( self::$_instances as $instance ) {
1834
- if ( ! $instance->is_plugin() ) {
1835
- continue;
1836
- }
1837
-
1838
- $slug_basename_map[ $instance->get_slug() ] = $instance->premium_plugin_basename();
1839
- }
1840
- ?>
1841
- <script type="text/javascript">
1842
- (function( $ ) {
1843
- var slugBasenameMap = <?php echo json_encode( $slug_basename_map ) ?>;
1844
- for ( var slug in slugBasenameMap ) {
1845
- var basename = slugBasenameMap[ slug ];
1846
-
1847
- // Try to get the plugin rows if on the "Plugins" page.
1848
- var $pluginRows = $( '.wp-list-table.plugins tr[data-plugin="' + basename + '"]');
1849
-
1850
- if ( 0 === $pluginRows.length ) {
1851
- // Try to get the plugin rows if on the "Updates" page.
1852
- var $pluginCheckbox = $( '#update-plugins-table input[type="checkbox"][value="' + basename + '"]' );
1853
- if ( 0 !== $pluginCheckbox.length ) {
1854
- $pluginRows = $pluginCheckbox.parents( 'tr:first' );
1855
- }
1856
- }
1857
-
1858
- if ( 0 === $pluginRows.length ) {
1859
- // No plugin rows found.
1860
- continue;
1861
- }
1862
-
1863
- // Find the "View details" links and add the `fs_allow_updater_and_dialog` param to the URL.
1864
- $pluginRows.find( 'a[href*="plugin-install.php?tab=plugin-information"]' ).each(function() {
1865
- var $this = $( this ),
1866
- href = $this.attr( 'href' ).replace( '?tab=', '?fs_allow_updater_and_dialog=true&tab=');
1867
-
1868
- $this.attr( 'href', href );
1869
- });
1870
- }
1871
- })( jQuery );
1872
- </script>
1873
- <?php
1874
- }
1875
-
1876
- /**
1877
- * @author Leo Fajardo (@leorw)
1878
- * @since 2.3.0
1879
- */
1880
- static function _maybe_add_beta_label_styles() {
1881
- $has_any_beta_version = false;
1882
-
1883
- foreach ( self::$_instances as $instance ) {
1884
- if ( $instance->is_beta() ) {
1885
- $has_any_beta_version = true;
1886
- break;
1887
- }
1888
- }
1889
-
1890
- if ( $has_any_beta_version ) {
1891
- fs_enqueue_local_style( 'fs_plugins', '/admin/plugins.css' );
1892
- }
1893
- }
1894
-
1895
- /**
1896
- * @author Leo Fajardo (@leorw)
1897
- * @since 2.3.0
1898
- */
1899
- static function _maybe_add_beta_label_to_plugins_and_handle_confirmation() {
1900
- $beta_data = array();
1901
-
1902
- foreach ( self::$_instances as $instance ) {
1903
- if ( ! $instance->is_premium() ) {
1904
- continue;
1905
- }
1906
-
1907
- /**
1908
- * If there's an available beta version update, a confirmation message will be shown when the
1909
- * "Update now" link on the "Plugins" or "Themes" page is clicked.
1910
- */
1911
- $has_beta_update = $instance->has_beta_update();
1912
-
1913
- $is_beta = (
1914
- // The "Beta" label is added separately for themes.
1915
- $instance->is_plugin() &&
1916
- $instance->is_beta()
1917
- );
1918
-
1919
- if ( ! $is_beta && ! $has_beta_update ) {
1920
- continue;
1921
- }
1922
-
1923
- $beta_data[ $instance->get_plugin_basename() ] = array( 'is_installed_version_beta' => $is_beta );
1924
-
1925
- if ( ! $has_beta_update ) {
1926
- continue;
1927
- }
1928
-
1929
- $beta_data[ $instance->get_plugin_basename() ]['beta_version_update_confirmation_message'] = sprintf(
1930
- '%s %s',
1931
- sprintf(
1932
- fs_esc_attr_inline(
1933
- 'An update to a Beta version will replace your installed version of %s with the latest Beta release - use with caution, and not on production sites. You have been warned.',
1934
- 'beta-version-update-caution',
1935
- $instance->get_slug()
1936
- ),
1937
- $instance->get_plugin_title()
1938
- ),
1939
- fs_esc_attr_inline( 'Would you like to proceed with the update?', 'update-confirmation', $instance->get_slug() )
1940
- );
1941
- }
1942
-
1943
- if ( empty( $beta_data ) ) {
1944
- return;
1945
- }
1946
- ?>
1947
- <script type="text/javascript">
1948
- ( function( $ ) {
1949
- var betaData = <?php echo json_encode( $beta_data ) ?>;
1950
-
1951
- for ( var pluginBasename in betaData ) {
1952
- if ( ! betaData.hasOwnProperty( pluginBasename ) ) {
1953
- continue;
1954
- }
1955
-
1956
- if ( ! betaData[ pluginBasename ].is_installed_version_beta ) {
1957
- continue;
1958
- }
1959
-
1960
- var $parentContainer = $( '.wp-list-table.plugins tr[data-plugin="' + pluginBasename + '"]' );
1961
- if ( 0 === $parentContainer.length ) {
1962
- continue;
1963
- }
1964
-
1965
- $parentContainer.find( '.plugin-title > strong:first-child').append(
1966
- '<span class="fs-tag fs-info"><?php fs_esc_js_echo_inline( 'Beta', 'beta' ) ?></span>'
1967
- );
1968
- }
1969
-
1970
- setTimeout( function() {
1971
- // Wait a little bit before adding the event handler, otherwise, it will be overridden by the core WP logic.
1972
- $( '.plugins .update-message .update-link, .themes .theme .update-message' ).on( 'click', function() {
1973
- var $parentContainer = $( this ).parents( 'tr:first' );
1974
- pluginBasename = ( 0 !== $parentContainer.length ) ?
1975
- $parentContainer.data( 'plugin' ) :
1976
- $( this ).parents( '.theme:first' ).data( 'slug' );
1977
-
1978
- if (
1979
- betaData[ pluginBasename ] &&
1980
- betaData[ pluginBasename ].beta_version_update_confirmation_message &&
1981
- ! confirm( betaData[ pluginBasename ].beta_version_update_confirmation_message )
1982
- ) {
1983
- return false;
1984
- }
1985
- } );
1986
- }, 20 );
1987
- } )( jQuery );
1988
- </script>
1989
- <?php
1990
- }
1991
-
1992
- /**
1993
- * Keeping the uninstall hook registered for free or premium plugin version may result to a fatal error that
1994
- * could happen when a user tries to uninstall either version while one of them is still active. Uninstalling a
1995
- * plugin will trigger inclusion of the free or premium version and if one of them is active during the
1996
- * uninstallation, a fatal error may occur in case the plugin's class or functions are already defined.
1997
- *
1998
- * @author Leo Fajardo (@leorw)
1999
- *
2000
- * @since 1.2.0
2001
- */
2002
- private function unregister_uninstall_hook() {
2003
- $uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
2004
- unset( $uninstallable_plugins[ $this->_free_plugin_basename ] );
2005
- unset( $uninstallable_plugins[ $this->premium_plugin_basename() ] );
2006
-
2007
- update_option( 'uninstall_plugins', $uninstallable_plugins );
2008
- }
2009
-
2010
- /**
2011
- * @since 1.2.0 Invalidate module's main file cache, otherwise, FS_Plugin_Updater will not fetch updates.
2012
- *
2013
- * @param bool $store_prev_path
2014
- */
2015
- private function clear_module_main_file_cache( $store_prev_path = true ) {
2016
- if ( ! isset( $this->_storage->plugin_main_file ) ||
2017
- empty( $this->_storage->plugin_main_file->path )
2018
- ) {
2019
- return;
2020
- }
2021
-
2022
- if ( ! $store_prev_path ) {
2023
- /**
2024
- * Storing the previous path is not needed when clearing the cache after an SDK version update since
2025
- * the main purpose of the cache clearing in that event is to correct a wrong plugin main file path
2026
- * which causes data mix-up between plugins (e.g. titles and versions of an add-on and its parent plugin).
2027
- *
2028
- * @author Leo Fajardo (@leorw)
2029
- * @since 2.2.1
2030
- */
2031
- unset( $this->_storage->plugin_main_file->path );
2032
- } else {
2033
- $plugin_main_file = clone $this->_storage->plugin_main_file;
2034
-
2035
- // Store cached path (2nd layer cache).
2036
- $plugin_main_file->prev_path = $plugin_main_file->path;
2037
-
2038
- // Clear cached path.
2039
- unset( $plugin_main_file->path );
2040
-
2041
- $this->_storage->plugin_main_file = $plugin_main_file;
2042
- }
2043
-
2044
- /**
2045
- * Clear global cached path.
2046
- *
2047
- * @author Leo Fajardo (@leorw)
2048
- * @since 1.2.2
2049
- */
2050
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map' );
2051
- unset( $id_slug_type_path_map[ $this->_module_id ]['path'] );
2052
- self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
2053
- }
2054
-
2055
- /**
2056
- * @author Leo Fajardo (@leorw)
2057
- * @since 2.0.0
2058
- */
2059
- function _hook_action_links_and_register_account_hooks() {
2060
- if ( $this->is_migration() ) {
2061
- return;
2062
- }
2063
-
2064
- $this->_add_tracking_links();
2065
-
2066
- if ( self::is_plugins_page() && $this->is_plugin() ) {
2067
- $this->hook_plugin_action_links();
2068
- }
2069
-
2070
- $this->_register_account_hooks();
2071
- }
2072
-
2073
- /**
2074
- * @author Vova Feldman (@svovaf)
2075
- * @since 1.0.9
2076
- */
2077
- private function _register_account_hooks() {
2078
- if ( ! is_admin() ) {
2079
- return;
2080
- }
2081
-
2082
- /**
2083
- * Always show the deactivation feedback form since we added
2084
- * automatic free version deactivation upon premium code activation.
2085
- *
2086
- * @since 1.2.1.6
2087
- */
2088
- $this->add_ajax_action(
2089
- 'submit_uninstall_reason',
2090
- array( &$this, '_submit_uninstall_reason_action' )
2091
- );
2092
-
2093
- $this->add_ajax_action(
2094
- 'cancel_subscription_or_trial',
2095
- array( &$this, 'cancel_subscription_or_trial_ajax_action' )
2096
- );
2097
-
2098
- if ( ! $this->is_addon() || $this->is_parent_plugin_installed() ) {
2099
- if ( ( $this->is_plugin() && self::is_plugins_page() ) ||
2100
- ( $this->is_theme() && self::is_themes_page() )
2101
- ) {
2102
- add_action( 'admin_footer', array( &$this, '_add_deactivation_feedback_dialog_box' ) );
2103
- }
2104
- }
2105
- }
2106
-
2107
- /**
2108
- * Leverage backtrace to find caller plugin file path.
2109
- *
2110
- * @param bool $is_init Is initiation sequence.
2111
- * @param string $main_file Since 2.5.0 expects the module's main file path to potentially purge the cached path.
2112
- *
2113
- * @return string
2114
- * @since 1.0.6
2115
- *
2116
- * @author Vova Feldman (@svovaf)
2117
- */
2118
- private function _find_caller_plugin_file( $is_init = false, $main_file = '' ) {
2119
- // Try to load the cached value of the file path.
2120
- if ( isset( $this->_storage->plugin_main_file ) ) {
2121
- $plugin_main_file = $this->_storage->plugin_main_file;
2122
- if ( ! empty( $plugin_main_file->path ) ) {
2123
- $absolute_path = $this->get_absolute_path( $plugin_main_file->path );
2124
- if ( file_exists( $absolute_path ) ) {
2125
- if ( $is_init && $absolute_path !== $this->get_absolute_path( $main_file ) ) {
2126
- // Update cached path if not matching the actual path.
2127
- $plugin_main_file->path = $main_file;
2128
- $this->_storage->plugin_main_file = $plugin_main_file;
2129
- }
2130
-
2131
- return $absolute_path;
2132
- }
2133
- }
2134
- }
2135
-
2136
- /**
2137
- * @since 1.2.1
2138
- *
2139
- * `clear_module_main_file_cache()` is clearing the plugin's cached path on
2140
- * deactivation. Therefore, if any plugin/theme was initiating `Freemius`
2141
- * with that plugin's slug, it was overriding the empty plugin path with a wrong path.
2142
- *
2143
- * So, we've added a special mechanism with a 2nd layer of cache that uses `prev_path`
2144
- * when the class instantiator isn't the module.
2145
- */
2146
- if ( ! $is_init ) {
2147
- // Fetch prev path cache.
2148
- if ( isset( $this->_storage->plugin_main_file ) &&
2149
- ! empty( $this->_storage->plugin_main_file->prev_path )
2150
- ) {
2151
- $absolute_path = $this->get_absolute_path( $this->_storage->plugin_main_file->prev_path );
2152
- if ( file_exists( $absolute_path ) ) {
2153
- return $absolute_path;
2154
- }
2155
- }
2156
-
2157
- wp_die(
2158
- $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' ) .
2159
- " Module: {$this->_slug}; SDK: " . WP_FS__SDK_VERSION . ";",
2160
- $this->get_text_inline( 'Error', 'error' ),
2161
- array( 'back_link' => true )
2162
- );
2163
- }
2164
-
2165
- /**
2166
- * @since 1.2.1
2167
- *
2168
- * Only the original instantiator that calls dynamic_init can modify the module's path.
2169
- */
2170
- // Find caller module.
2171
- $this->_storage->plugin_main_file = (object) array(
2172
- 'path' => $main_file,
2173
- );
2174
-
2175
- return $this->get_absolute_path( $main_file );
2176
- }
2177
-
2178
- /**
2179
- * @author Leo Fajardo (@leorw)
2180
- * @since 1.2.3
2181
- *
2182
- * @param string $path
2183
- *
2184
- * @return string
2185
- */
2186
- private function get_relative_path( $path ) {
2187
- $module_root_dir = $this->get_module_root_dir_path();
2188
- if ( 0 === strpos( $path, $module_root_dir ) ) {
2189
- $path = substr( $path, strlen( $module_root_dir ) );
2190
- }
2191
-
2192
- return $path;
2193
- }
2194
-
2195
- /**
2196
- * @author Leo Fajardo (@leorw)
2197
- * @since 1.2.3
2198
- *
2199
- * @param string $path
2200
- * @param string|bool $module_type
2201
- *
2202
- * @return string
2203
- */
2204
- private function get_absolute_path( $path, $module_type = false ) {
2205
- $module_root_dir = $this->get_module_root_dir_path( $module_type );
2206
- if ( 0 !== strpos( $path, $module_root_dir ) ) {
2207
- $path = fs_normalize_path( $module_root_dir . $path );
2208
- }
2209
-
2210
- return $path;
2211
- }
2212
-
2213
- /**
2214
- * @author Leo Fajardo (@leorw)
2215
- * @since 1.2.3
2216
- *
2217
- * @param string|bool $module_type
2218
- *
2219
- * @return string
2220
- */
2221
- private function get_module_root_dir_path( $module_type = false ) {
2222
- $is_plugin = empty( $module_type ) ?
2223
- $this->is_plugin() :
2224
- ( WP_FS__MODULE_TYPE_PLUGIN === $module_type );
2225
-
2226
- return fs_normalize_path( trailingslashit( $is_plugin ?
2227
- WP_PLUGIN_DIR :
2228
- get_theme_root( get_stylesheet() ) ) );
2229
- }
2230
-
2231
- /**
2232
- * @author Leo Fajardo (@leorw)
2233
- *
2234
- * @param number $module_id
2235
- * @param string $slug
2236
- *
2237
- * @return string Since 2.5.0 return the module's main file path.
2238
- *
2239
- * @since 1.2.2
2240
- */
2241
- private function store_id_slug_type_path_map( $module_id, $slug ) {
2242
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
2243
-
2244
- $store_option = false;
2245
-
2246
- if ( ! isset( $id_slug_type_path_map[ $module_id ] ) ) {
2247
- $id_slug_type_path_map[ $module_id ] = array(
2248
- 'slug' => $slug
2249
- );
2250
-
2251
- $store_option = true;
2252
- } else if (
2253
- isset( $id_slug_type_path_map[ $module_id ]['slug'] ) &&
2254
- $slug !== $id_slug_type_path_map[ $module_id ]['slug']
2255
- ) {
2256
- $id_slug_type_path_map[ $module_id ]['slug'] = $slug;
2257
- $store_option = true;
2258
- }
2259
-
2260
- $find_caller = empty( $id_slug_type_path_map[ $module_id ]['path'] );
2261
-
2262
- if ( ! $find_caller ) {
2263
- /**
2264
- * This verification is for cases when suddenly the same module
2265
- * is installed but with a different folder name.
2266
- *
2267
- * @author Vova Feldman (@svovaf)
2268
- * @since 1.2.3
2269
- */
2270
- $find_caller = ! file_exists( $this->get_absolute_path(
2271
- $id_slug_type_path_map[ $module_id ]['path'],
2272
- $id_slug_type_path_map[ $module_id ]['type']
2273
- ) );
2274
- }
2275
-
2276
- foreach ( $id_slug_type_path_map as $id => $data ) {
2277
- if ( empty( $id ) ) {
2278
- // Remove maps with empty module ID.
2279
- unset( $id_slug_type_path_map[ $id ] );
2280
- $store_option = true;
2281
- continue;
2282
- }
2283
-
2284
- /**
2285
- * If the module's main file path is identical to the main file path of another module then it means that the cached path of the current module or the other one with the same path is wrong, and therefore, we need to recalculate those paths.
2286
- *
2287
- * @author Vova Feldman (@svovaf)
2288
- * @since 2.5.0
2289
- */
2290
- if ( ! $find_caller ) {
2291
- if ( $id == $module_id ) {
2292
- continue;
2293
- }
2294
-
2295
- if (
2296
- isset( $data['path'] ) &&
2297
- $data['path'] === $id_slug_type_path_map[ $module_id ]['path']
2298
- ) {
2299
- $find_caller = true;
2300
- }
2301
- }
2302
- }
2303
-
2304
- if ( $find_caller ) {
2305
- $caller_main_file_and_type = $this->get_caller_main_file_and_type( $module_id );
2306
-
2307
- $id_slug_type_path_map[ $module_id ]['type'] = $caller_main_file_and_type->module_type;
2308
- $id_slug_type_path_map[ $module_id ]['path'] = $caller_main_file_and_type->path;
2309
-
2310
- $store_option = true;
2311
- }
2312
-
2313
- if ( $store_option ) {
2314
- self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
2315
- }
2316
-
2317
- return $id_slug_type_path_map[ $module_id ]['path'];
2318
- }
2319
-
2320
- /**
2321
- * Identifies the caller type: plugin or theme.
2322
- *
2323
- * @author Leo Fajardo (@leorw)
2324
- * @since 1.2.2
2325
- *
2326
- * @author Vova Feldman (@svovaf)
2327
- * @since 1.2.2.3 Find the earliest module in the call stack that calls to the SDK. This fix is for cases when
2328
- * add-ons are relying on loading the SDK from the parent module, and also allows themes including the
2329
- * SDK an internal file instead of directly from functions.php.
2330
- * @since 1.2.1.7 Knows how to handle cases when an add-on includes the parent module logic.
2331
- *
2332
- * @param number $module_id @since 2.5.0
2333
- */
2334
- private function get_caller_main_file_and_type( $module_id ) {
2335
- self::require_plugin_essentials();
2336
-
2337
- $all_plugins = fs_get_plugins( true );
2338
- $all_plugins_paths = array();
2339
-
2340
- // Get active plugin's main files real full names (might be symlinks).
2341
- foreach ( $all_plugins as $relative_path => $data ) {
2342
- if ( false === strpos( fs_normalize_path( $relative_path ), '/' ) ) {
2343
- /**
2344
- * Ignore plugins that don't have a folder (e.g. Hello Dolly) since they
2345
- * can't really include the SDK.
2346
- *
2347
- * @author Vova Feldman
2348
- * @since 1.2.1.7
2349
- */
2350
- continue;
2351
- }
2352
-
2353
- $all_plugins_paths[] = fs_normalize_path( realpath( WP_PLUGIN_DIR . '/' . $relative_path ) );
2354
- }
2355
-
2356
- $caller_file_candidate = false;
2357
- $caller_map = array();
2358
- $module_type = WP_FS__MODULE_TYPE_PLUGIN;
2359
- $themes_dir = fs_normalize_path( get_theme_root( get_stylesheet() ) );
2360
- $plugin_dir_to_skip = false;
2361
-
2362
- for ( $i = 1, $bt = debug_backtrace(), $len = count( $bt ); $i < $len; $i ++ ) {
2363
- if ( empty( $bt[ $i ]['file'] ) ) {
2364
- continue;
2365
- }
2366
-
2367
- if ( $i > 1 && ! empty( $bt[ $i - 1 ]['file'] ) && $bt[ $i ]['file'] === $bt[ $i - 1 ]['file'] ) {
2368
- // If file same as the prev file in the stack, skip it.
2369
- continue;
2370
- }
2371
-
2372
- if ( ! empty( $bt[ $i ]['function'] ) && in_array( $bt[ $i ]['function'], array(
2373
- 'do_action',
2374
- 'apply_filter',
2375
- // The string split is stupid, but otherwise, theme check
2376
- // throws info notices.
2377
- 'requir' . 'e_once',
2378
- 'requir' . 'e',
2379
- 'includ' . 'e_once',
2380
- 'includ' . 'e',
2381
- 'install_and_activate_plugin',
2382
- 'try_activate_plugin',
2383
- 'activate_plugin'
2384
- ) )
2385
- ) {
2386
- if ( 'activate_plugin' === $bt[ $i ]['function'] ) {
2387
- /**
2388
- * Store the directory of the activator plugin so that any other file that starts with it
2389
- * cannot be mistakenly chosen as a candidate caller file.
2390
- *
2391
- * @author Leo Fajardo
2392
- *
2393
- * @since 2.3.0
2394
- */
2395
- $caller_file_path = fs_normalize_path( $bt[ $i ]['file'] );
2396
-
2397
- foreach ( $all_plugins_paths as $plugin_path ) {
2398
- $plugin_dir = fs_normalize_path( dirname( $plugin_path ) . '/' );
2399
- if ( false !== strpos( $caller_file_path, $plugin_dir ) ) {
2400
- $plugin_dir_to_skip = $plugin_dir;
2401
-
2402
- break;
2403
- }
2404
- }
2405
- }
2406
-
2407
- // Ignore call stack hooks and files inclusion.
2408
- continue;
2409
- }
2410
-
2411
- $caller_file_path = fs_normalize_path( $bt[ $i ]['file'] );
2412
-
2413
- if ( ! empty( $plugin_dir_to_skip ) ) {
2414
- /**
2415
- * Skip if it's an activator plugin file to avoid mistakenly choosing it as a candidate caller file.
2416
- *
2417
- * @author Leo Fajardo
2418
- *
2419
- * @since 2.3.0
2420
- */
2421
- if ( 0 === strpos( $caller_file_path, $plugin_dir_to_skip ) ) {
2422
- continue;
2423
- }
2424
- }
2425
-
2426
- if ( 'functions.php' === basename( $caller_file_path ) ) {
2427
- /**
2428
- * 1. Assumes that theme's starting execution file is functions.php.
2429
- * 2. This complex logic fixes symlink issues (e.g. with Vargant).
2430
- *
2431
- * @author Vova Feldman (@svovaf)
2432
- * @since 1.2.2.5
2433
- */
2434
-
2435
- if ( $caller_file_path == fs_normalize_path( realpath( trailingslashit( $themes_dir ) . basename( dirname( $caller_file_path ) ) . '/' . basename( $caller_file_path ) ) ) ) {
2436
- $module_type = WP_FS__MODULE_TYPE_THEME;
2437
-
2438
- /**
2439
- * Relative path of the theme, e.g.:
2440
- * `my-theme/functions.php`
2441
- *
2442
- * @author Leo Fajardo (@leorw)
2443
- */
2444
- $caller_file_candidate = basename( dirname( $caller_file_path ) ) .
2445
- '/' .
2446
- basename( $caller_file_path );
2447
-
2448
- continue;
2449
- }
2450
- }
2451
-
2452
- $caller_file_hash = md5( $caller_file_path );
2453
-
2454
- if ( ! isset( $caller_map[ $caller_file_hash ] ) ) {
2455
- foreach ( $all_plugins_paths as $plugin_path ) {
2456
- if ( empty( $plugin_path ) ) {
2457
- continue;
2458
- }
2459
-
2460
- if ( false !== strpos( $caller_file_path, fs_normalize_path( dirname( $plugin_path ) . '/' ) ) ) {
2461
- $caller_map[ $caller_file_hash ] = fs_normalize_path( $plugin_path );
2462
- break;
2463
- }
2464
- }
2465
- }
2466
-
2467
- if ( isset( $caller_map[ $caller_file_hash ] ) ) {
2468
- $module_type = WP_FS__MODULE_TYPE_PLUGIN;
2469
- $caller_file_candidate = plugin_basename( $caller_map[ $caller_file_hash ] );
2470
- }
2471
- }
2472
-
2473
- $caller_main_file_and_type = (object) array(
2474
- 'module_type' => $module_type,
2475
- 'path' => $caller_file_candidate
2476
- );
2477
-
2478
- return apply_filters( "fs_{$module_id}_caller_main_file_and_type", $caller_main_file_and_type );
2479
- }
2480
-
2481
- #----------------------------------------------------------------------------------
2482
- #region Deactivation Feedback Form
2483
- #----------------------------------------------------------------------------------
2484
-
2485
- /**
2486
- * Displays a confirmation and feedback dialog box when the user clicks on the "Deactivate" link on the plugins
2487
- * page.
2488
- *
2489
- * @author Vova Feldman (@svovaf)
2490
- * @author Leo Fajardo (@leorw)
2491
- *
2492
- * @since 1.1.2
2493
- */
2494
- function _add_deactivation_feedback_dialog_box() {
2495
- if (
2496
- $this->is_clone() ||
2497
- ( is_object( $this->_site ) && ! $this->is_registered() )
2498
- ) {
2499
- return;
2500
- }
2501
-
2502
- $subscription_cancellation_dialog_box_template_params = $this->apply_filters( 'show_deactivation_subscription_cancellation', true ) ?
2503
- $this->_get_subscription_cancellation_dialog_box_template_params() :
2504
- array();
2505
-
2506
- /**
2507
- * @since 2.3.0 Developers can optionally hide the deactivation feedback form using the 'show_deactivation_feedback_form' filter.
2508
- */
2509
- $show_deactivation_feedback_form = ! self::is_deactivation_snoozed();
2510
- if ( $this->has_filter( 'show_deactivation_feedback_form' ) ) {
2511
- $show_deactivation_feedback_form = $this->apply_filters( 'show_deactivation_feedback_form', true );
2512
- } else if ( $this->is_addon() ) {
2513
- /**
2514
- * If the add-on's 'show_deactivation_feedback_form' is not set, try to inherit the value from the parent.
2515
- */
2516
- $show_deactivation_feedback_form = $this->get_parent_instance()->apply_filters( 'show_deactivation_feedback_form', true );
2517
- }
2518
-
2519
- $uninstall_confirmation_message = $this->apply_filters( 'uninstall_confirmation_message', '' );
2520
-
2521
- if (
2522
- empty( $subscription_cancellation_dialog_box_template_params ) &&
2523
- ! $show_deactivation_feedback_form &&
2524
- empty( $uninstall_confirmation_message )
2525
- ) {
2526
- return;
2527
- }
2528
-
2529
- $vars = array( 'id' => $this->_module_id );
2530
-
2531
- if ( $show_deactivation_feedback_form ) {
2532
- /* Check the type of user:
2533
- * 1. Long-term (long-term)
2534
- * 2. Non-registered and non-anonymous short-term (non-registered-and-non-anonymous-short-term).
2535
- * 3. Short-term (short-term)
2536
- */
2537
- $is_long_term_user = true;
2538
-
2539
- // Check if the site is at least 2 days old.
2540
- $time_installed = $this->_storage->install_timestamp;
2541
-
2542
- // Difference in seconds.
2543
- $date_diff = time() - $time_installed;
2544
-
2545
- // Convert seconds to days.
2546
- $date_diff_days = floor( $date_diff / ( 60 * 60 * 24 ) );
2547
-
2548
- if ( $date_diff_days < 2 ) {
2549
- $is_long_term_user = false;
2550
- }
2551
-
2552
- $is_long_term_user = $this->apply_filters( 'is_long_term_user', $is_long_term_user );
2553
-
2554
- if ( $is_long_term_user ) {
2555
- $user_type = 'long-term';
2556
- } else {
2557
- if ( ! $this->is_registered() && ! $this->is_anonymous() ) {
2558
- $user_type = 'non-registered-and-non-anonymous-short-term';
2559
- } else {
2560
- $user_type = 'short-term';
2561
- }
2562
- }
2563
-
2564
- $uninstall_reasons = $this->_get_uninstall_reasons( $user_type );
2565
-
2566
- $vars['reasons'] = $uninstall_reasons;
2567
- }
2568
-
2569
- $vars['subscription_cancellation_dialog_box_template_params'] = &$subscription_cancellation_dialog_box_template_params;
2570
- $vars['show_deactivation_feedback_form'] = $show_deactivation_feedback_form;
2571
- $vars['uninstall_confirmation_message'] = $uninstall_confirmation_message;
2572
-
2573
- /**
2574
- * Load the HTML template for the deactivation feedback dialog box.
2575
- *
2576
- * @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.
2577
- */
2578
- fs_require_template( 'forms/deactivation/form.php', $vars );
2579
- }
2580
-
2581
- /**
2582
- * @author Leo Fajardo (@leorw)
2583
- * @since 1.1.2
2584
- *
2585
- * @param string $user_type
2586
- *
2587
- * @return array The uninstall reasons for the specified user type.
2588
- */
2589
- function _get_uninstall_reasons( $user_type = 'long-term' ) {
2590
- $module_type = $this->_module_type;
2591
-
2592
- $internal_message_template_var = array(
2593
- 'id' => $this->_module_id
2594
- );
2595
-
2596
- $plan = $this->get_plan();
2597
-
2598
- if ( $this->is_registered() && is_object( $plan ) && $plan->has_technical_support() ) {
2599
- $contact_support_template = fs_get_template( 'forms/deactivation/contact.php', $internal_message_template_var );
2600
- } else {
2601
- $contact_support_template = '';
2602
- }
2603
-
2604
- $reason_found_better_plugin = array(
2605
- 'id' => self::REASON_FOUND_A_BETTER_PLUGIN,
2606
- 'text' => sprintf( $this->get_text_inline( 'I found a better %s', 'reason-found-a-better-plugin' ), $module_type ),
2607
- 'input_type' => 'textfield',
2608
- 'input_placeholder' => sprintf( $this->get_text_inline( "What's the %s's name?", 'placeholder-plugin-name' ), $module_type ),
2609
- );
2610
-
2611
- $reason_temporary_deactivation = array(
2612
- 'id' => self::REASON_TEMPORARY_DEACTIVATION,
2613
- 'text' => sprintf(
2614
- $this->get_text_inline( "It's a temporary %s - I'm troubleshooting an issue", 'reason-temporary-x' ),
2615
- strtolower( $this->is_plugin() ?
2616
- $this->get_text_inline( 'Deactivation', 'deactivation' ) :
2617
- $this->get_text_inline( 'Theme Switch', 'theme-switch' )
2618
- )
2619
- ),
2620
- 'input_type' => '',
2621
- 'input_placeholder' => ''
2622
- );
2623
-
2624
- $reason_other = array(
2625
- 'id' => self::REASON_OTHER,
2626
- 'text' => $this->get_text_inline( 'Other', 'reason-other' ),
2627
- 'input_type' => 'textfield',
2628
- 'input_placeholder' => ''
2629
- );
2630
-
2631
- $long_term_user_reasons = array(
2632
- array(
2633
- 'id' => self::REASON_NO_LONGER_NEEDED,
2634
- 'text' => sprintf( $this->get_text_inline( 'I no longer need the %s', 'reason-no-longer-needed' ), $module_type ),
2635
- 'input_type' => '',
2636
- 'input_placeholder' => ''
2637
- ),
2638
- $reason_found_better_plugin,
2639
- array(
2640
- 'id' => self::REASON_NEEDED_FOR_A_SHORT_PERIOD,
2641
- 'text' => sprintf( $this->get_text_inline( 'I only needed the %s for a short period', 'reason-needed-for-a-short-period' ), $module_type ),
2642
- 'input_type' => '',
2643
- 'input_placeholder' => ''
2644
- ),
2645
- array(
2646
- 'id' => self::REASON_BROKE_MY_SITE,
2647
- 'text' => sprintf( $this->get_text_inline( 'The %s broke my site', 'reason-broke-my-site' ), $module_type ),
2648
- 'input_type' => '',
2649
- 'input_placeholder' => '',
2650
- 'internal_message' => $contact_support_template
2651
- ),
2652
- array(
2653
- 'id' => self::REASON_SUDDENLY_STOPPED_WORKING,
2654
- 'text' => sprintf( $this->get_text_inline( 'The %s suddenly stopped working', 'reason-suddenly-stopped-working' ), $module_type ),
2655
- 'input_type' => '',
2656
- 'input_placeholder' => '',
2657
- 'internal_message' => $contact_support_template
2658
- )
2659
- );
2660
-
2661
- if ( $this->is_paying() ) {
2662
- $long_term_user_reasons[] = array(
2663
- 'id' => self::REASON_CANT_PAY_ANYMORE,
2664
- 'text' => $this->get_text_inline( "I can't pay for it anymore", 'reason-cant-pay-anymore' ),
2665
- 'input_type' => 'textfield',
2666
- 'input_placeholder' => $this->get_text_inline( 'What price would you feel comfortable paying?', 'placeholder-comfortable-price' )
2667
- );
2668
- }
2669
-
2670
- $reason_dont_share_info = array(
2671
- 'id' => self::REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION,
2672
- 'text' => $this->get_text_inline( "I don't like to share my information with you", 'reason-dont-like-to-share-my-information' ),
2673
- 'input_type' => '',
2674
- 'input_placeholder' => ''
2675
- );
2676
-
2677
- /**
2678
- * If the current user has selected the "don't share data" reason in the deactivation feedback modal, inform the
2679
- * user by showing additional message that he doesn't have to share data and can just choose to skip the opt-in
2680
- * (the Skip button is included in the message to show). This message will only be shown if anonymous mode is
2681
- * enabled and the user's account is currently not in pending activation state (similar to the way the Skip
2682
- * button in the opt-in form is shown/hidden).
2683
- */
2684
- if ( $this->is_enable_anonymous() && ! $this->is_pending_activation() ) {
2685
- $reason_dont_share_info['internal_message'] = fs_get_template( 'forms/deactivation/retry-skip.php', $internal_message_template_var );
2686
- }
2687
-
2688
- $uninstall_reasons = array(
2689
- 'long-term' => $long_term_user_reasons,
2690
- 'non-registered-and-non-anonymous-short-term' => array(
2691
- array(
2692
- 'id' => self::REASON_DIDNT_WORK,
2693
- 'text' => sprintf( $this->get_text_inline( "The %s didn't work", 'reason-didnt-work' ), $module_type ),
2694
- 'input_type' => '',
2695
- 'input_placeholder' => ''
2696
- ),
2697
- $reason_dont_share_info,
2698
- $reason_found_better_plugin
2699
- ),
2700
- 'short-term' => array(
2701
- array(
2702
- 'id' => self::REASON_COULDNT_MAKE_IT_WORK,
2703
- 'text' => $this->get_text_inline( "I couldn't understand how to make it work", 'reason-couldnt-make-it-work' ),
2704
- 'input_type' => '',
2705
- 'input_placeholder' => '',
2706
- 'internal_message' => $contact_support_template
2707
- ),
2708
- $reason_found_better_plugin,
2709
- array(
2710
- 'id' => self::REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE,
2711
- '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 ),
2712
- 'input_type' => 'textarea',
2713
- 'input_placeholder' => $this->get_text_inline( 'What feature?', 'placeholder-feature' )
2714
- ),
2715
- array(
2716
- 'id' => self::REASON_NOT_WORKING,
2717
- 'text' => sprintf( $this->get_text_inline( 'The %s is not working', 'reason-not-working' ), $module_type ),
2718
- 'input_type' => 'textarea',
2719
- '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' )
2720
- ),
2721
- array(
2722
- 'id' => self::REASON_NOT_WHAT_I_WAS_LOOKING_FOR,
2723
- 'text' => $this->get_text_inline( "It's not what I was looking for", 'reason-not-what-i-was-looking-for' ),
2724
- 'input_type' => 'textarea',
2725
- 'input_placeholder' => $this->get_text_inline( "What you've been looking for?", 'placeholder-what-youve-been-looking-for' )
2726
- ),
2727
- array(
2728
- 'id' => self::REASON_DIDNT_WORK_AS_EXPECTED,
2729
- 'text' => sprintf( $this->get_text_inline( "The %s didn't work as expected", 'reason-didnt-work-as-expected' ), $module_type ),
2730
- 'input_type' => 'textarea',
2731
- 'input_placeholder' => $this->get_text_inline( 'What did you expect?', 'placeholder-what-did-you-expect' )
2732
- )
2733
- )
2734
- );
2735
-
2736
- // Randomize the reasons for the current user type.
2737
- shuffle( $uninstall_reasons[ $user_type ] );
2738
-
2739
- // Keep the following reasons as the last items in the list.
2740
- $uninstall_reasons[ $user_type ][] = $reason_temporary_deactivation;
2741
- $uninstall_reasons[ $user_type ][] = $reason_other;
2742
-
2743
- $uninstall_reasons = $this->apply_filters( 'uninstall_reasons', $uninstall_reasons );
2744
-
2745
- return $uninstall_reasons[ $user_type ];
2746
- }
2747
-
2748
- /**
2749
- * Called after the user has submitted his reason for deactivating the plugin.
2750
- *
2751
- * @author Leo Fajardo (@leorw)
2752
- * @since 1.1.2
2753
- */
2754
- function _submit_uninstall_reason_action() {
2755
- $this->_logger->entrance();
2756
-
2757
- $this->check_ajax_referer( 'submit_uninstall_reason' );
2758
-
2759
- $reason_id = fs_request_get( 'reason_id' );
2760
-
2761
- // Check if the given reason ID is an unsigned integer.
2762
- if ( ! ctype_digit( $reason_id ) ) {
2763
- exit;
2764
- }
2765
-
2766
- $reason_info = trim( fs_request_get( 'reason_info', '' ) );
2767
- if ( ! empty( $reason_info ) ) {
2768
- $reason_info = substr( $reason_info, 0, 128 );
2769
- }
2770
-
2771
- $reason = (object) array(
2772
- 'id' => $reason_id,
2773
- 'info' => $reason_info,
2774
- 'is_anonymous' => fs_request_get_bool( 'is_anonymous' )
2775
- );
2776
-
2777
- $this->_storage->store( 'uninstall_reason', $reason );
2778
-
2779
- if ( self::REASON_TEMPORARY_DEACTIVATION == $reason->id ) {
2780
- $snooze_period = fs_request_get( 'snooze_period' );
2781
-
2782
- if ( is_numeric( $snooze_period ) && 0 < $snooze_period ) {
2783
- self::snooze_deactivation_form( (int) $snooze_period );
2784
- }
2785
- }
2786
-
2787
- /**
2788
- * If the module type is "theme", trigger the uninstall event here (on theme deactivation) since themes do
2789
- * not support uninstall hook.
2790
- *
2791
- * @author Leo Fajardo (@leorw)
2792
- * @since 1.2.2
2793
- */
2794
- if ( $this->is_theme() ) {
2795
- if ( $this->is_premium() && ! $this->has_active_valid_license() ) {
2796
- FS_Plugin_Updater::instance( $this )->delete_update_data();
2797
- }
2798
-
2799
- $this->_uninstall_plugin_event( false );
2800
- $this->remove_sdk_reference();
2801
- }
2802
-
2803
- // Print '1' for successful operation.
2804
- echo 1;
2805
- exit;
2806
- }
2807
-
2808
- #--------------------------------------------------------------------------------
2809
- #region Deactivation Feedback Snoozing
2810
- #--------------------------------------------------------------------------------
2811
-
2812
- /**
2813
- * @author Vova Feldman (@svovaf)
2814
- * @since 2.4.3
2815
- *
2816
- * @param int $period
2817
- *
2818
- * @return bool True if the value was set, false otherwise.
2819
- */
2820
- private static function snooze_deactivation_form( $period ) {
2821
- return ( 0 < $period && self::reset_deactivation_snoozing( $period ) );
2822
- }
2823
-
2824
- /**
2825
- * Check if deactivation feedback form is snoozed.
2826
- *
2827
- * @author Vova Feldman (@svovaf)
2828
- * @since 2.4.3
2829
- *
2830
- * @return bool
2831
- */
2832
- static function is_deactivation_snoozed() {
2833
- $is_snoozed = ( ! is_multisite() || fs_is_network_admin() ) ?
2834
- get_transient( 'fs_snooze_period' ) :
2835
- get_site_transient( 'fs_snooze_period' );
2836
-
2837
-
2838
- return ( 'true' === $is_snoozed );
2839
- }
2840
-
2841
- /**
2842
- * Reset deactivation snoozing. When `$period` is `0` will stop deactivation snoozing by deleting the transients. Otherwise, will set the transients for the selected period.
2843
- *
2844
- * @param int $period Period in seconds.
2845
- *
2846
- * @author Vova Feldman (@svovaf)
2847
- * @since 2.4.3
2848
- */
2849
- private static function reset_deactivation_snoozing( $period = 0 ) {
2850
- $value = ( 0 === $period ) ? null : 'true';
2851
-
2852
- if ( ! is_multisite() || fs_is_network_admin() ) {
2853
- return set_transient( 'fs_snooze_period', $value, $period );
2854
- } else {
2855
- return set_site_transient( 'fs_snooze_period', $value, $period );
2856
- }
2857
- }
2858
-
2859
- /**
2860
- * The deactivation snooze expiration UNIX timestamp (in sec).
2861
- *
2862
- * @author Vova Feldman (@svovaf)
2863
- * @since 2.4.3
2864
- *
2865
- * @return int
2866
- */
2867
- static function deactivation_snooze_expires_at() {
2868
- return ( ! is_multisite() || fs_is_network_admin() ) ?
2869
- (int) get_option( '_transient_timeout_fs_snooze_period' ) :
2870
- (int) get_site_option( '_site_transient_timeout_fs_snooze_period' );
2871
- }
2872
-
2873
- #endregion
2874
-
2875
- /**
2876
- * @author Leo Fajardo (@leorw)
2877
- * @since 2.1.4
2878
- */
2879
- function cancel_subscription_or_trial_ajax_action() {
2880
- $this->_logger->entrance();
2881
-
2882
- $this->check_ajax_referer( 'cancel_subscription_or_trial' );
2883
-
2884
- $result = $this->cancel_subscription_or_trial( fs_request_get( 'plugin_id', $this->get_id() ), false );
2885
-
2886
- if ( $this->is_api_error( $result ) ) {
2887
- $this->shoot_ajax_failure( $result->error->message );
2888
- }
2889
-
2890
- $this->shoot_ajax_success();
2891
- }
2892
-
2893
- /**
2894
- * @author Leo Fajardo (@leorw)
2895
- * @since 2.1.4
2896
- *
2897
- * @param number $plugin_id
2898
- *
2899
- * @return object
2900
- */
2901
- private function cancel_subscription_or_trial( $plugin_id ) {
2902
- $fs = null;
2903
- if ( $plugin_id == $this->get_id() ) {
2904
- $fs = $this;
2905
- } else if ( $this->is_addon_activated( $plugin_id ) ) {
2906
- $fs = self::get_instance_by_id( $plugin_id );
2907
- }
2908
-
2909
- $result = null;
2910
-
2911
- if ( ! is_null( $fs ) ) {
2912
- $result = $fs->is_paid_trial() ?
2913
- $fs->_cancel_trial() :
2914
- $fs->_downgrade_site();
2915
- }
2916
-
2917
- return $result;
2918
- }
2919
-
2920
- /**
2921
- * @author Leo Fajardo (@leorw)
2922
- * @since 2.0.2
2923
- */
2924
- function _delete_theme_update_data_action() {
2925
- FS_Plugin_Updater::instance( $this )->delete_update_data();
2926
- }
2927
-
2928
- #endregion
2929
-
2930
- #----------------------------------------------------------------------------------
2931
- #region Instance
2932
- #----------------------------------------------------------------------------------
2933
-
2934
- /**
2935
- * Main singleton instance.
2936
- *
2937
- * @author Vova Feldman (@svovaf)
2938
- * @since 1.0.0
2939
- *
2940
- * @param number $module_id
2941
- * @param string|bool $slug
2942
- * @param bool $is_init Is initiation sequence.
2943
- *
2944
- * @return Freemius|false
2945
- */
2946
- static function instance( $module_id, $slug = false, $is_init = false ) {
2947
- if ( empty( $module_id ) ) {
2948
- return false;
2949
- }
2950
-
2951
- /**
2952
- * Load the essential static data prior to initiating FS_Plugin_Manager since there's an essential MS network migration logic that needs to be executed prior to the initiation.
2953
- */
2954
- self::_load_required_static();
2955
-
2956
- if ( ! is_numeric( $module_id ) ) {
2957
- if ( ! $is_init && true === $slug ) {
2958
- $is_init = true;
2959
- }
2960
-
2961
- $slug = $module_id;
2962
-
2963
- $module = FS_Plugin_Manager::instance( $slug )->get();
2964
-
2965
- if ( is_object( $module ) ) {
2966
- $module_id = $module->id;
2967
- }
2968
- }
2969
-
2970
- $key = 'm_' . $module_id;
2971
-
2972
- if ( ! isset( self::$_instances[ $key ] ) ) {
2973
- self::$_instances[ $key ] = new Freemius( $module_id, $slug, $is_init );
2974
- }
2975
-
2976
- return self::$_instances[ $key ];
2977
- }
2978
-
2979
- /**
2980
- * @author Vova Feldman (@svovaf)
2981
- * @since 1.0.6
2982
- *
2983
- * @param number $addon_id
2984
- *
2985
- * @return bool
2986
- */
2987
- private static function has_instance( $addon_id ) {
2988
- return isset( self::$_instances[ 'm_' . $addon_id ] );
2989
- }
2990
-
2991
- /**
2992
- * @author Leo Fajardo (@leorw)
2993
- * @since 1.2.2
2994
- *
2995
- * @param string|number $id_or_slug
2996
- * @param string $module_type
2997
- *
2998
- * @return number|false
2999
- */
3000
- private static function get_module_id( $id_or_slug, $module_type = WP_FS__MODULE_TYPE_PLUGIN ) {
3001
- if ( is_numeric( $id_or_slug ) ) {
3002
- return $id_or_slug;
3003
- }
3004
-
3005
- foreach ( self::$_instances as $instance ) {
3006
- // Also check the module type since there can be a plugin and a theme with the same slug.
3007
- if ( ( $module_type === $instance->get_module_type() ) && ( $id_or_slug === $instance->get_slug() ) ) {
3008
- return $instance->get_id();
3009
- }
3010
- }
3011
-
3012
- return false;
3013
- }
3014
-
3015
- /**
3016
- * @author Vova Feldman (@svovaf)
3017
- * @since 1.0.6
3018
- *
3019
- * @param number $id
3020
- *
3021
- * @return false|Freemius
3022
- */
3023
- static function get_instance_by_id( $id ) {
3024
- return isset ( self::$_instances[ 'm_' . $id ] ) ?
3025
- self::$_instances[ 'm_' . $id ] :
3026
- false;
3027
- }
3028
-
3029
- /**
3030
- *
3031
- * @author Vova Feldman (@svovaf)
3032
- * @since 1.0.1
3033
- *
3034
- * @param string $plugin_file
3035
- * @param string $module_type
3036
- *
3037
- * @return false|Freemius
3038
- */
3039
- static function get_instance_by_file( $plugin_file, $module_type = WP_FS__MODULE_TYPE_PLUGIN ) {
3040
- $slug = self::find_slug_by_basename( $plugin_file );
3041
-
3042
- return ( false !== $slug ) ?
3043
- self::instance( self::get_module_id( $slug, $module_type ) ) :
3044
- false;
3045
- }
3046
-
3047
- /**
3048
- * @author Vova Feldman (@svovaf)
3049
- * @since 1.0.6
3050
- *
3051
- * @return false|Freemius
3052
- */
3053
- function get_parent_instance() {
3054
- return self::get_instance_by_id( $this->_plugin->parent_plugin_id );
3055
- }
3056
-
3057
- /**
3058
- * @author Vova Feldman (@svovaf)
3059
- * @since 1.0.6
3060
- *
3061
- * @param string|number $id_or_slug
3062
- *
3063
- * @return false|Freemius
3064
- */
3065
- function get_addon_instance( $id_or_slug ) {
3066
- $addon_id = self::get_module_id( $id_or_slug );
3067
-
3068
- return self::instance( $addon_id );
3069
- }
3070
-
3071
- /**
3072
- * @return Freemius[]
3073
- */
3074
- static function _get_all_instances() {
3075
- return self::$_instances;
3076
- }
3077
-
3078
- #endregion ------------------------------------------------------------------
3079
-
3080
- /**
3081
- * @author Vova Feldman (@svovaf)
3082
- * @since 1.0.6
3083
- *
3084
- * @return bool
3085
- */
3086
- function is_parent_plugin_installed() {
3087
- $is_active = self::has_instance( $this->_plugin->parent_plugin_id );
3088
-
3089
- if ( $is_active ) {
3090
- return true;
3091
- }
3092
-
3093
- /**
3094
- * Parent module might be a theme. If that's the case, the add-on's FS
3095
- * instance will be loaded prior to the theme's FS instance, therefore,
3096
- * we need to check if it's active with a "look ahead".
3097
- *
3098
- * @author Vova Feldman
3099
- * @since 1.2.2.3
3100
- */
3101
- global $fs_active_plugins;
3102
- if ( is_object( $fs_active_plugins ) && is_array( $fs_active_plugins->plugins ) ) {
3103
- $active_theme = wp_get_theme();
3104
-
3105
- foreach ( $fs_active_plugins->plugins as $sdk => $module ) {
3106
- if ( WP_FS__MODULE_TYPE_THEME === $module->type ) {
3107
- if ( $module->plugin_path == $active_theme->get_stylesheet() ) {
3108
- // Parent module is a theme and it's currently active.
3109
- return true;
3110
- }
3111
- }
3112
- }
3113
- }
3114
-
3115
- return false;
3116
- }
3117
-
3118
- /**
3119
- * Check if add-on parent plugin in activation mode.
3120
- *
3121
- * @author Vova Feldman (@svovaf)
3122
- * @since 1.0.7
3123
- *
3124
- * @return bool
3125
- */
3126
- function is_parent_in_activation() {
3127
- $parent_fs = $this->get_parent_instance();
3128
- if ( ! is_object( $parent_fs ) ) {
3129
- return false;
3130
- }
3131
-
3132
- return ( $parent_fs->is_activation_mode() );
3133
- }
3134
-
3135
- /**
3136
- * Is plugin in activation mode.
3137
- *
3138
- * @author Vova Feldman (@svovaf)
3139
- * @since 1.0.7
3140
- *
3141
- * @param bool $and_on
3142
- *
3143
- * @return bool
3144
- */
3145
- function is_activation_mode( $and_on = true ) {
3146
- return fs_is_network_admin() ?
3147
- $this->is_network_activation_mode( $and_on ) :
3148
- $this->is_site_activation_mode( $and_on );
3149
- }
3150
-
3151
- /**
3152
- * Is plugin in activation mode.
3153
- *
3154
- * @author Vova Feldman (@svovaf)
3155
- * @since 1.0.7
3156
- *
3157
- * @param bool $and_on
3158
- *
3159
- * @return bool
3160
- */
3161
- function is_site_activation_mode( $and_on = true ) {
3162
- return (
3163
- ( $this->is_on() || ! $and_on ) &&
3164
- (
3165
- ( $this->is_premium() && true === $this->_storage->require_license_activation ) ||
3166
- (
3167
- ( ! $this->is_registered() ||
3168
- ( $this->is_only_premium() && ! $this->has_features_enabled_license() ) ) &&
3169
- ( ! $this->is_enable_anonymous() ||
3170
- ( ! $this->is_anonymous() && ! $this->is_pending_activation() ) )
3171
- )
3172
- )
3173
- );
3174
- }
3175
-
3176
- /**
3177
- * Checks if the SDK in network activation mode.
3178
- *
3179
- * @author Leo Fajardo (@leorw)
3180
- * @since 2.0.0
3181
- *
3182
- * @param bool $and_on
3183
- *
3184
- * @return bool
3185
- */
3186
- private function is_network_activation_mode( $and_on = true ) {
3187
- if ( ! $this->_is_network_active ) {
3188
- // Not network activated.
3189
- return false;
3190
- }
3191
-
3192
- if ( $this->is_network_upgrade_mode() ) {
3193
- // Special flag to enforce network activation mode to decide what to do with the sites that are not yet opted-in nor skipped.
3194
- return true;
3195
- }
3196
-
3197
- if ( ! $this->is_site_activation_mode( $and_on ) ) {
3198
- // Whether the context is single site or the network, if the plugin is no longer in activation mode then it is not in network activation mode as well.
3199
- return false;
3200
- }
3201
-
3202
- if ( $this->is_network_delegated_connection() ) {
3203
- // Super-admin delegated the connection to the site admins -> not activation mode.
3204
- return false;
3205
- }
3206
-
3207
- if ( $this->is_network_anonymous() && true !== $this->_storage->require_license_activation ) {
3208
- // Super-admin skipped the connection network wide -> not activation mode.
3209
- return false;
3210
- }
3211
-
3212
- if ( $this->is_network_registered() ) {
3213
- // Super-admin connected at least one site -> not activation mode.
3214
- return false;
3215
- }
3216
-
3217
- return true;
3218
- }
3219
-
3220
- /**
3221
- * Check if current page is the opt-in/pending-activation page.
3222
- *
3223
- * @author Vova Feldman (@svovaf)
3224
- * @since 1.2.1.7
3225
- *
3226
- * @return bool
3227
- */
3228
- function is_activation_page() {
3229
- if ( $this->_menu->is_activation_page( $this->show_opt_in_on_themes_page() ) ) {
3230
- return true;
3231
- }
3232
-
3233
- if ( ! $this->is_activation_mode() ) {
3234
- return false;
3235
- }
3236
-
3237
- // Check if current page is matching the activation page.
3238
- return $this->is_matching_url( $this->get_activation_url() );
3239
- }
3240
-
3241
- /**
3242
- * Check if URL path's are matching and that all querystring
3243
- * arguments of the $sub_url exist in the $url with the same values.
3244
- *
3245
- * WARNING:
3246
- * 1. This method doesn't check if the sub/domain are matching.
3247
- * 2. Ignore case sensitivity.
3248
- *
3249
- * @author Vova Feldman (@svovaf)
3250
- * @since 1.2.1.7
3251
- *
3252
- * @param string $sub_url
3253
- * @param string $url If argument is not set, check if the sub_url matching the current's page URL.
3254
- *
3255
- * @return bool
3256
- */
3257
- private function is_matching_url( $sub_url, $url = '' ) {
3258
- if ( empty( $url ) ) {
3259
- $url = $_SERVER['REQUEST_URI'];
3260
- }
3261
-
3262
- $url = strtolower( $url );
3263
- $sub_url = strtolower( $sub_url );
3264
-
3265
- if ( parse_url( $sub_url, PHP_URL_PATH ) !== parse_url( $url, PHP_URL_PATH ) ) {
3266
- // Different path - DO NOT OVERRIDE PAGE.
3267
- return false;
3268
- }
3269
-
3270
- $url_params = array();
3271
- parse_str( parse_url( $url, PHP_URL_QUERY ), $url_params );
3272
-
3273
- $sub_url_params = array();
3274
- parse_str( parse_url( $sub_url, PHP_URL_QUERY ), $sub_url_params );
3275
-
3276
- foreach ( $sub_url_params as $key => $val ) {
3277
- if ( ! isset( $url_params[ $key ] ) || $val != $url_params[ $key ] ) {
3278
- // Not matching query string - DO NOT OVERRIDE PAGE.
3279
- return false;
3280
- }
3281
- }
3282
-
3283
- return true;
3284
- }
3285
-
3286
- /**
3287
- * Get the basenames of all active plugins for specific blog. Including network activated plugins.
3288
- *
3289
- * @author Vova Feldman (@svovaf)
3290
- * @since 2.0.0
3291
- *
3292
- * @param int $blog_id
3293
- *
3294
- * @return string[]
3295
- */
3296
- private static function get_active_plugins_basenames( $blog_id = 0 ) {
3297
- if ( is_multisite() && $blog_id > 0 ) {
3298
- $active_basenames = get_blog_option( $blog_id, 'active_plugins' );
3299
- } else {
3300
- $active_basenames = get_option( 'active_plugins' );
3301
- }
3302
-
3303
- if ( ! is_array( $active_basenames ) ) {
3304
- $active_basenames = array();
3305
- }
3306
-
3307
- if ( is_multisite() ) {
3308
- $network_active_basenames = get_site_option( 'active_sitewide_plugins' );
3309
-
3310
- if ( is_array( $network_active_basenames ) && ! empty( $network_active_basenames ) ) {
3311
- $active_basenames = array_merge( $active_basenames, array_keys( $network_active_basenames ) );
3312
- }
3313
- }
3314
-
3315
- return $active_basenames;
3316
- }
3317
-
3318
- /**
3319
- * @author Leo Fajardo (@leorw)
3320
- * @since 2.3.0
3321
- *
3322
- * @param int $blog_id
3323
- *
3324
- * @return array
3325
- */
3326
- static function get_active_plugins_directories_map( $blog_id = 0 ) {
3327
- $active_basenames = self::get_active_plugins_basenames( $blog_id );
3328
-
3329
- $map = array();
3330
-
3331
- foreach ( $active_basenames as $active_basename ) {
3332
- $active_basename = fs_normalize_path( $active_basename );
3333
-
3334
- if ( false === strpos( $active_basename, '/' ) ) {
3335
- continue;
3336
- }
3337
-
3338
- $map[ dirname( $active_basename ) ] = true;
3339
- }
3340
-
3341
- return $map;
3342
- }
3343
-
3344
- /**
3345
- * Get collection of all active plugins. Including network activated plugins.
3346
- *
3347
- * @author Vova Feldman (@svovaf)
3348
- * @since 1.0.9
3349
- *
3350
- * @param int $blog_id Since 2.0.0
3351
- *
3352
- * @return array[string]array
3353
- */
3354
- private static function get_active_plugins( $blog_id = 0 ) {
3355
- self::require_plugin_essentials();
3356
-
3357
- $active_plugin = array();
3358
- $all_plugins = fs_get_plugins();
3359
- $active_plugins_basenames = self::get_active_plugins_basenames( $blog_id );
3360
-
3361
- foreach ( $active_plugins_basenames as $plugin_basename ) {
3362
- $active_plugin[ $plugin_basename ] = $all_plugins[ $plugin_basename ];
3363
- }
3364
-
3365
- return $active_plugin;
3366
- }
3367
-
3368
- /**
3369
- * Get collection of all site active plugins for a specified blog.
3370
- *
3371
- * @author Vova Feldman (@svovaf)
3372
- * @since 2.0.0
3373
- *
3374
- * @param int $blog_id
3375
- *
3376
- * @return array[string]array
3377
- */
3378
- private static function get_site_active_plugins( $blog_id = 0 ) {
3379
- $active_basenames = ( is_multisite() && $blog_id > 0 ) ?
3380
- get_blog_option( $blog_id, 'active_plugins' ) :
3381
- get_option( 'active_plugins' );
3382
-
3383
- $active = array();
3384
-
3385
- if ( ! is_array( $active_basenames ) ) {
3386
- return $active;
3387
- }
3388
-
3389
- foreach ( $active_basenames as $basename ) {
3390
- $active[ $basename ] = array(
3391
- 'is_active' => true,
3392
- 'Version' => '1.0', // Dummy version.
3393
- 'slug' => self::get_plugin_slug( $basename ),
3394
- );
3395
- }
3396
-
3397
- return $active;
3398
- }
3399
-
3400
- /**
3401
- * Get collection of all plugins with their activation status for a specified blog.
3402
- *
3403
- * @author Vova Feldman (@svovaf)
3404
- * @since 1.1.8
3405
- *
3406
- * @param int $blog_id Since 2.0.0
3407
- *
3408
- * @return array Key is the plugin file path and the value is an array of the plugin data.
3409
- */
3410
- private static function get_all_plugins( $blog_id = 0 ) {
3411
- self::require_plugin_essentials();
3412
-
3413
- $all_plugins = fs_get_plugins();
3414
-
3415
- $active_plugins_basenames = self::get_active_plugins_basenames( $blog_id );
3416
-
3417
- foreach ( $all_plugins as $basename => &$data ) {
3418
- // By default set to inactive (next foreach update the active plugins).
3419
- $data['is_active'] = false;
3420
- // Enrich with plugin slug.
3421
- $data['slug'] = self::get_plugin_slug( $basename );
3422
- }
3423
-
3424
- // Flag active plugins.
3425
- foreach ( $active_plugins_basenames as $basename ) {
3426
- if ( isset( $all_plugins[ $basename ] ) ) {
3427
- $all_plugins[ $basename ]['is_active'] = true;
3428
- }
3429
- }
3430
-
3431
- return $all_plugins;
3432
- }
3433
-
3434
- /**
3435
- * Get collection of all plugins and if they are network level activated.
3436
- *
3437
- * @author Vova Feldman (@svovaf)
3438
- * @since 2.0.0
3439
- *
3440
- * @return array Key is the plugin basename and the value is an array of the plugin data.
3441
- */
3442
- private static function get_network_plugins() {
3443
- self::require_plugin_essentials();
3444
-
3445
- $all_plugins = fs_get_plugins();
3446
-
3447
- $network_active_basenames = is_multisite() ?
3448
- get_site_option( 'active_sitewide_plugins' ) :
3449
- array();
3450
-
3451
- foreach ( $all_plugins as $basename => &$data ) {
3452
- // By default set to inactive (next foreach update the active plugins).
3453
- $data['is_active'] = false;
3454
- // Enrich with plugin slug.
3455
- $data['slug'] = self::get_plugin_slug( $basename );
3456
- }
3457
-
3458
- // Flag active plugins.
3459
- foreach ( $network_active_basenames as $basename ) {
3460
- if ( isset( $all_plugins[ $basename ] ) ) {
3461
- $all_plugins[ $basename ]['is_active'] = true;
3462
- }
3463
- }
3464
-
3465
- return $all_plugins;
3466
- }
3467
-
3468
- /**
3469
- * Cached result of get_site_transient( 'update_plugins' )
3470
- *
3471
- * @author Vova Feldman (@svovaf)
3472
- * @since 1.1.8
3473
- *
3474
- * @var object
3475
- */
3476
- private static $_plugins_info;
3477
-
3478
- /**
3479
- * Helper function to get specified plugin's slug.
3480
- *
3481
- * @author Vova Feldman (@svovaf)
3482
- * @since 1.1.8
3483
- *
3484
- * @param $basename
3485
- *
3486
- * @return string
3487
- */
3488
- private static function get_plugin_slug( $basename ) {
3489
- if ( ! isset( self::$_plugins_info ) ) {
3490
- self::$_plugins_info = get_site_transient( 'update_plugins' );
3491
- }
3492
-
3493
- $slug = '';
3494
-
3495
- if ( is_object( self::$_plugins_info ) ) {
3496
- if ( isset( self::$_plugins_info->no_update ) &&
3497
- isset( self::$_plugins_info->no_update[ $basename ] ) &&
3498
- ! empty( self::$_plugins_info->no_update[ $basename ]->slug )
3499
- ) {
3500
- $slug = self::$_plugins_info->no_update[ $basename ]->slug;
3501
- } else if ( isset( self::$_plugins_info->response ) &&
3502
- isset( self::$_plugins_info->response[ $basename ] ) &&
3503
- ! empty( self::$_plugins_info->response[ $basename ]->slug )
3504
- ) {
3505
- $slug = self::$_plugins_info->response[ $basename ]->slug;
3506
- }
3507
- }
3508
-
3509
- if ( empty( $slug ) ) {
3510
- // Try to find slug from FS data.
3511
- $slug = self::find_slug_by_basename( $basename );
3512
- }
3513
-
3514
- if ( empty( $slug ) ) {
3515
- // Fallback to plugin's folder name.
3516
- $slug = dirname( $basename );
3517
- }
3518
-
3519
- return $slug;
3520
- }
3521
-
3522
- private static $_statics_loaded = false;
3523
-
3524
- /**
3525
- * Load static resources.
3526
- *
3527
- * @author Vova Feldman (@svovaf)
3528
- * @since 1.0.1
3529
- */
3530
- private static function _load_required_static() {
3531
- if ( self::$_statics_loaded ) {
3532
- return;
3533
- }
3534
-
3535
- self::$_static_logger = FS_Logger::get_logger( WP_FS__SLUG, WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
3536
-
3537
- self::$_static_logger->entrance();
3538
-
3539
- self::$_accounts = FS_Options::instance( WP_FS__ACCOUNTS_OPTION_NAME, true );
3540
-
3541
- if ( is_multisite() ) {
3542
- $has_skipped_migration = (
3543
- // 'id_slug_type_path_map' - was never stored on older versions, therefore, not exists on the site level.
3544
- null === self::$_accounts->get_option( 'id_slug_type_path_map', null, false ) &&
3545
- // 'file_slug_map' stored on the site level, so it was running an SDK version before it was integrated with MS-network.
3546
- null !== self::$_accounts->get_option( 'file_slug_map', null, false )
3547
- );
3548
-
3549
- /**
3550
- * If the file_slug_map exists on the site level but doesn't exist on the
3551
- * network level storage, it means that we need to process the storage with migration.
3552
- *
3553
- * The code in this `if` scope will only be executed once and only for the first site that will execute it because once we migrate the storage data, file_slug_map will be already set in the network level storage.
3554
- *
3555
- * @author Vova Feldman (@svovaf)
3556
- * @since 2.0.0
3557
- */
3558
- if (
3559
- ( $has_skipped_migration && true !== self::$_accounts->get_option( 'ms_migration_complete', false, true ) ) ||
3560
- ( null === self::$_accounts->get_option( 'file_slug_map', null, true ) &&
3561
- null !== self::$_accounts->get_option( 'file_slug_map', null, false ) )
3562
- ) {
3563
- self::migrate_options_to_network();
3564
- }
3565
- }
3566
-
3567
- self::$_global_admin_notices = FS_Admin_Notices::instance( 'global' );
3568
-
3569
- if ( ! WP_FS__DEMO_MODE ) {
3570
- add_action( ( fs_is_network_admin() ? 'network_' : '' ) . 'admin_menu', array(
3571
- 'Freemius',
3572
- '_add_debug_section'
3573
- ) );
3574
- }
3575
-
3576
- add_action( "wp_ajax_fs_toggle_debug_mode", array( 'Freemius', '_toggle_debug_mode' ) );
3577
-
3578
- self::add_ajax_action_static( 'get_debug_log', array( 'Freemius', '_get_debug_log' ) );
3579
-
3580
- self::add_ajax_action_static( 'get_db_option', array( 'Freemius', '_get_db_option' ) );
3581
-
3582
- self::add_ajax_action_static( 'set_db_option', array( 'Freemius', '_set_db_option' ) );
3583
-
3584
- if ( 0 == did_action( 'plugins_loaded' ) ) {
3585
- add_action( 'plugins_loaded', array( 'Freemius', '_load_textdomain' ), 1 );
3586
- }
3587
-
3588
- $clone_manager = FS_Clone_Manager::instance();
3589
- add_action( 'init', array( $clone_manager, '_init' ) );
3590
-
3591
- add_action( 'admin_footer', array( 'Freemius', '_enrich_ajax_url' ) );
3592
- add_action( 'admin_footer', array( 'Freemius', '_open_support_forum_in_new_page' ) );
3593
-
3594
- if ( self::is_plugins_page() || self::is_themes_page() ) {
3595
- add_action( 'admin_print_footer_scripts', array( 'Freemius', '_maybe_add_beta_label_styles' ), 9 );
3596
-
3597
- /**
3598
- * Specifically use this hook so that the JS event handlers will work properly on the "Themes"
3599
- * page.
3600
- *
3601
- * @author Leo Fajardo (@leorw)
3602
- * @since 2.3.0
3603
- */
3604
- add_action( 'admin_footer-' . self::get_current_page(), array( 'Freemius', '_maybe_add_beta_label_to_plugins_and_handle_confirmation') );
3605
- }
3606
-
3607
- self::$_statics_loaded = true;
3608
- }
3609
-
3610
- #--------------------------------------------------------------------------------
3611
- #region Clone
3612
- #--------------------------------------------------------------------------------
3613
-
3614
- /**
3615
- * @author Leo Fajardo (@leorw)
3616
- * @since 2.5.0
3617
- *
3618
- * @return bool
3619
- */
3620
- private function is_unresolved_clone() {
3621
- if ( ! $this->is_clone() ) {
3622
- return false;
3623
- }
3624
-
3625
- return FS_Clone_Manager::instance()->has_temporary_duplicate_mode_expired();
3626
- }
3627
-
3628
- /**
3629
- * @author Leo Fajardo (@leorw)
3630
- * @since 2.5.0
3631
- */
3632
- function is_clone() {
3633
- if ( ! is_object( $this->_site ) ) {
3634
- return false;
3635
- }
3636
-
3637
- return (
3638
- fs_strip_url_protocol( trailingslashit( $this->_site->url ) ) !== fs_strip_url_protocol( trailingslashit( get_site_url() ) )
3639
- );
3640
- }
3641
-
3642
- /**
3643
- * @author Leo Fajardo (@leorw)
3644
- * @since 2.5.0
3645
- *
3646
- * @param number $site_id
3647
- */
3648
- function fetch_install_by_id( $site_id ) {
3649
- return $this->get_current_or_network_user_api_scope()->get( "/installs/{$site_id}.json" );
3650
- }
3651
-
3652
- /**
3653
- * @author Leo Fajardo (@leorw)
3654
- * @since 2.5.0
3655
- *
3656
- * @return string|object|bool
3657
- */
3658
- function _handle_long_term_duplicate() {
3659
- $this->_logger->entrance();
3660
-
3661
- $this->delete_current_install( false );
3662
-
3663
- $license_key = false;
3664
-
3665
- if (
3666
- is_object( $this->_license ) &&
3667
- ! $this->_license->is_utilized(
3668
- ( WP_FS__IS_LOCALHOST_FOR_SERVER || FS_Site::is_localhost_by_address( get_site_url() ) )
3669
- )
3670
- ) {
3671
- $license_key = $this->_license->secret_key;
3672
- }
3673
-
3674
- return $this->opt_in(
3675
- false,
3676
- false,
3677
- false,
3678
- $license_key,
3679
- false,
3680
- false,
3681
- false,
3682
- null,
3683
- array(),
3684
- false
3685
- );
3686
- }
3687
-
3688
- #endregion
3689
-
3690
- /**
3691
- * @author Leo Fajardo (@leorw)
3692
- *
3693
- * @since 2.1.3
3694
- */
3695
- private static function migrate_options_to_network() {
3696
- self::migrate_accounts_to_network();
3697
-
3698
- // Migrate API options from site level to network level.
3699
- $api_network_options = FS_Option_Manager::get_manager( WP_FS__OPTIONS_OPTION_NAME, true, true );
3700
- $api_network_options->migrate_to_network();
3701
-
3702
- // Migrate API cache to network level storage.
3703
- FS_Cache_Manager::get_manager( WP_FS__API_CACHE_OPTION_NAME )->migrate_to_network();
3704
-
3705
- self::$_accounts->set_option( 'ms_migration_complete', true, true );
3706
- }
3707
-
3708
- #----------------------------------------------------------------------------------
3709
- #region Localization
3710
- #----------------------------------------------------------------------------------
3711
-
3712
- /**
3713
- * Load framework's text domain.
3714
- *
3715
- * @author Vova Feldman (@svovaf)
3716
- * @since 1.2.1
3717
- */
3718
- static function _load_textdomain() {
3719
- if ( ! is_admin() ) {
3720
- return;
3721
- }
3722
-
3723
- global $fs_active_plugins;
3724
-
3725
- // Works both for plugins and themes.
3726
- load_plugin_textdomain(
3727
- 'freemius',
3728
- false,
3729
- $fs_active_plugins->newest->sdk_path . '/languages/'
3730
- );
3731
- }
3732
-
3733
- #endregion
3734
-
3735
- #----------------------------------------------------------------------------------
3736
- #region Debugging
3737
- #----------------------------------------------------------------------------------
3738
-
3739
- /**
3740
- * @author Vova Feldman (@svovaf)
3741
- * @since 1.0.8
3742
- */
3743
- static function _add_debug_section() {
3744
- if ( ! is_super_admin() ) {
3745
- // Add debug page only for super-admins.
3746
- return;
3747
- }
3748
-
3749
- self::$_static_logger->entrance();
3750
-
3751
- $title = sprintf( '%s [v.%s]', fs_text_inline( 'Freemius Debug' ), WP_FS__SDK_VERSION );
3752
-
3753
- if ( WP_FS__DEV_MODE ) {
3754
- // Add top-level debug menu item.
3755
- $hook = FS_Admin_Menu_Manager::add_page(
3756
- $title,
3757
- $title,
3758
- 'manage_options',
3759
- 'freemius',
3760
- array( 'Freemius', '_debug_page_render' )
3761
- );
3762
- } else {
3763
- // Add hidden debug page.
3764
- $hook = FS_Admin_Menu_Manager::add_subpage(
3765
- null,
3766
- $title,
3767
- $title,
3768
- 'manage_options',
3769
- 'freemius',
3770
- array( 'Freemius', '_debug_page_render' )
3771
- );
3772
- }
3773
-
3774
- if ( ! empty( $hook ) ) {
3775
- add_action( "load-$hook", array( 'Freemius', '_debug_page_actions' ) );
3776
- }
3777
- }
3778
-
3779
- /**
3780
- * @author Vova Feldman (@svovaf)
3781
- * @since 1.1.7.3
3782
- */
3783
- static function _toggle_debug_mode() {
3784
- check_admin_referer( 'fs_toggle_debug_mode' );
3785
-
3786
- if ( ! is_super_admin() ) {
3787
- return;
3788
- }
3789
-
3790
- $is_on = fs_request_get( 'is_on', false, 'post' );
3791
-
3792
- if ( fs_request_is_post() && in_array( $is_on, array( 0, 1 ) ) ) {
3793
- update_option( 'fs_debug_mode', $is_on );
3794
-
3795
- // Turn on/off storage logging.
3796
- FS_Logger::_set_storage_logging( ( 1 == $is_on ) );
3797
- }
3798
-
3799
- exit;
3800
- }
3801
-
3802
- /**
3803
- * @author Vova Feldman (@svovaf)
3804
- * @since 1.2.1.6
3805
- */
3806
- static function _get_debug_log() {
3807
- check_admin_referer( 'fs_get_debug_log' );
3808
-
3809
- if ( ! is_super_admin() ) {
3810
- return;
3811
- }
3812
-
3813
- $limit = min( ! empty( $_POST['limit'] ) ? absint( $_POST['limit'] ) : 200, 200 );
3814
- $offset = min( ! empty( $_POST['offset'] ) ? absint( $_POST['offset'] ) : 200, 200 );
3815
-
3816
- $logs = FS_Logger::load_db_logs(
3817
- fs_request_get( 'filters', false, 'post' ),
3818
- $limit,
3819
- $offset
3820
- );
3821
-
3822
- self::shoot_ajax_success( $logs );
3823
- }
3824
-
3825
- /**
3826
- * @author Vova Feldman (@svovaf)
3827
- * @since 1.2.1.7
3828
- */
3829
- static function _get_db_option() {
3830
- check_admin_referer( 'fs_get_db_option' );
3831
-
3832
- $option_name = fs_request_get( 'option_name' );
3833
-
3834
- if ( ! is_super_admin() ||
3835
- ! fs_starts_with( $option_name, 'fs_' )
3836
- ) {
3837
- self::shoot_ajax_failure();
3838
- }
3839
-
3840
- $value = get_option( $option_name );
3841
-
3842
- $result = array(
3843
- 'name' => $option_name,
3844
- );
3845
-
3846
- if ( false !== $value ) {
3847
- if ( ! is_string( $value ) ) {
3848
- $value = json_encode( $value );
3849
- }
3850
-
3851
- $result['value'] = $value;
3852
- }
3853
-
3854
- self::shoot_ajax_success( $result );
3855
- }
3856
-
3857
- /**
3858
- * @author Vova Feldman (@svovaf)
3859
- * @since 1.2.1.7
3860
- */
3861
- static function _set_db_option() {
3862
- check_admin_referer( 'fs_set_db_option' );
3863
-
3864
- $option_name = fs_request_get( 'option_name' );
3865
-
3866
- if ( ! is_super_admin() ||
3867
- ! fs_starts_with( $option_name, 'fs_' )
3868
- ) {
3869
- self::shoot_ajax_failure();
3870
- }
3871
-
3872
- $option_value = fs_request_get( 'option_value' );
3873
-
3874
- if ( ! empty( $option_value ) ) {
3875
- update_option( $option_name, $option_value );
3876
- }
3877
-
3878
- self::shoot_ajax_success();
3879
- }
3880
-
3881
- /**
3882
- * @author Vova Feldman (@svovaf)
3883
- * @since 1.0.8
3884
- */
3885
- static function _debug_page_actions() {
3886
- self::_clean_admin_content_section();
3887
-
3888
- if ( fs_request_is_action( 'restart_freemius' ) ) {
3889
- check_admin_referer( 'restart_freemius' );
3890
-
3891
- if ( ! is_multisite() ) {
3892
- // Clear accounts data.
3893
- self::$_accounts->clear( null, true );
3894
- } else {
3895
- $sites = self::get_sites();
3896
- foreach ( $sites as $site ) {
3897
- $blog_id = self::get_site_blog_id( $site );
3898
- self::$_accounts->clear( $blog_id, true );
3899
- }
3900
-
3901
- // Clear network level storage.
3902
- self::$_accounts->clear( true, true );
3903
- }
3904
-
3905
- // Clear SDK reference cache.
3906
- delete_option( 'fs_active_plugins' );
3907
- } else if ( fs_request_is_action( 'clear_updates_data' ) ) {
3908
- check_admin_referer( 'clear_updates_data' );
3909
-
3910
- if ( ! is_multisite() ) {
3911
- set_site_transient( 'update_plugins', null );
3912
- set_site_transient( 'update_themes', null );
3913
- } else {
3914
- $current_blog_id = get_current_blog_id();
3915
-
3916
- $sites = self::get_sites();
3917
- foreach ( $sites as $site ) {
3918
- switch_to_blog( self::get_site_blog_id( $site ) );
3919
-
3920
- set_site_transient( 'update_plugins', null );
3921
- set_site_transient( 'update_themes', null );
3922
- }
3923
-
3924
- switch_to_blog( $current_blog_id );
3925
- }
3926
- } else if ( fs_request_is_action( 'reset_deactivation_snoozing' ) ) {
3927
- check_admin_referer( 'reset_deactivation_snoozing' );
3928
-
3929
- self::reset_deactivation_snoozing();
3930
- } else if ( fs_request_is_action( 'simulate_trial' ) ) {
3931
- check_admin_referer( 'simulate_trial' );
3932
-
3933
- $fs = freemius( fs_request_get( 'module_id' ) );
3934
-
3935
- // Update SDK install to at least 24 hours before.
3936
- $fs->_storage->install_timestamp = ( time() - WP_FS__TIME_24_HOURS_IN_SEC );
3937
- // Unset the trial shown timestamp.
3938
- unset( $fs->_storage->trial_promotion_shown );
3939
- } else if ( fs_request_is_action( 'simulate_network_upgrade' ) ) {
3940
- check_admin_referer( 'simulate_network_upgrade' );
3941
-
3942
- $fs = freemius( fs_request_get( 'module_id' ) );
3943
-
3944
- self::set_network_upgrade_mode( $fs->_storage );
3945
- } else if ( fs_request_is_action( 'delete_install' ) ) {
3946
- check_admin_referer( 'delete_install' );
3947
-
3948
- self::_delete_site_by_slug(
3949
- fs_request_get( 'slug' ),
3950
- fs_request_get( 'module_type' ),
3951
- true,
3952
- fs_request_get( 'blog_id', null )
3953
- );
3954
- } else if ( fs_request_is_action( 'delete_user' ) ) {
3955
- check_admin_referer( 'delete_user' );
3956
-
3957
- self::delete_user( fs_request_get( 'user_id' ) );
3958
- } else if ( fs_request_is_action( 'download_logs' ) ) {
3959
- check_admin_referer( 'download_logs' );
3960
-
3961
- $download_url = FS_Logger::download_db_logs(
3962
- fs_request_get( 'filters', false, 'post' )
3963
- );
3964
-
3965
- if ( false === $download_url ) {
3966
- 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.' );
3967
- }
3968
-
3969
- fs_redirect( $download_url );
3970
- } else if ( fs_request_is_action( 'migrate_options_to_network' ) ) {
3971
- check_admin_referer( 'migrate_options_to_network' );
3972
-
3973
- self::migrate_options_to_network();
3974
- }
3975
- }
3976
-
3977
- /**
3978
- * @author Leo Fajardo (@leorw)
3979
- * @since 2.5.0
3980
- *
3981
- * @return array
3982
- */
3983
- static function get_all_modules_sites() {
3984
- self::$_static_logger->entrance();
3985
-
3986
- $sites_by_type = array(
3987
- WP_FS__MODULE_TYPE_PLUGIN => array(),
3988
- WP_FS__MODULE_TYPE_THEME => array(),
3989
- );
3990
-
3991
- $module_types = array_keys( $sites_by_type );
3992
-
3993
- if ( ! is_multisite() ) {
3994
- foreach ( $module_types as $type ) {
3995
- $sites_by_type[ $type ] = self::get_all_sites( $type );
3996
-
3997
- foreach ( $sites_by_type[ $type ] as $slug => $install ) {
3998
- $sites_by_type[ $type ][ $slug ] = array( $install );
3999
- }
4000
- }
4001
- } else {
4002
- $sites = self::get_sites();
4003
-
4004
- foreach ( $sites as $site ) {
4005
- $blog_id = self::get_site_blog_id( $site );
4006
-
4007
- foreach ( $module_types as $type ) {
4008
- $installs = self::get_all_sites( $type, $blog_id );
4009
-
4010
- foreach ( $installs as $slug => $install ) {
4011
- if ( ! isset( $sites_by_type[ $type ][ $slug ] ) ) {
4012
- $sites_by_type[ $type ][ $slug ] = array();
4013
- }
4014
-
4015
- $install->blog_id = $blog_id;
4016
-
4017
- $sites_by_type[ $type ][ $slug ][] = $install;
4018
- }
4019
-
4020
- }
4021
- }
4022
- }
4023
-
4024
- return $sites_by_type;
4025
- }
4026
-
4027
- /**
4028
- * @author Vova Feldman (@svovaf)
4029
- * @since 1.0.8
4030
- */
4031
- static function _debug_page_render() {
4032
- self::$_static_logger->entrance();
4033
-
4034
- $all_modules_sites = self::get_all_modules_sites();
4035
-
4036
- $licenses_by_module_type = self::get_all_licenses_by_module_type();
4037
-
4038
- $vars = array(
4039
- 'plugin_sites' => $all_modules_sites[ WP_FS__MODULE_TYPE_PLUGIN ],
4040
- 'theme_sites' => $all_modules_sites[ WP_FS__MODULE_TYPE_THEME ],
4041
- 'users' => self::get_all_users(),
4042
- 'addons' => self::get_all_addons(),
4043
- 'account_addons' => self::get_all_account_addons(),
4044
- 'plugin_licenses' => $licenses_by_module_type[ WP_FS__MODULE_TYPE_PLUGIN ],
4045
- 'theme_licenses' => $licenses_by_module_type[ WP_FS__MODULE_TYPE_THEME ]
4046
- );
4047
-
4048
- fs_enqueue_local_style( 'fs_debug', '/admin/debug.css' );
4049
- fs_require_once_template( 'debug.php', $vars );
4050
- }
4051
-
4052
- #endregion
4053
-
4054
- #----------------------------------------------------------------------------------
4055
- #region Connectivity Issues
4056
- #----------------------------------------------------------------------------------
4057
-
4058
- /**
4059
- * Check if Freemius should be turned on for the current plugin install.
4060
- *
4061
- * Note:
4062
- * $this->_is_on is updated in has_api_connectivity()
4063
- *
4064
- * @author Vova Feldman (@svovaf)
4065
- * @since 1.0.9
4066
- *
4067
- * @return bool
4068
- */
4069
- function is_on() {
4070
- self::$_static_logger->entrance();
4071
-
4072
- if ( is_object( $this->_site ) && ! $this->is_registered() ) {
4073
- return false;
4074
- }
4075
-
4076
- if ( isset( $this->_is_on ) ) {
4077
- return $this->_is_on;
4078
- }
4079
-
4080
- // If already installed or pending then sure it's on :)
4081
- if ( $this->is_registered() || $this->is_pending_activation() ) {
4082
- $this->_is_on = true;
4083
-
4084
- return true;
4085
- }
4086
-
4087
- return false;
4088
- }
4089
-
4090
- /**
4091
- * @author Vova Feldman (@svovaf)
4092
- * @since 1.1.7.3
4093
- *
4094
- * @param bool $flush_if_no_connectivity
4095
- *
4096
- * @return bool
4097
- */
4098
- private function should_run_connectivity_test( $flush_if_no_connectivity = false ) {
4099
- if ( ! isset( $this->_storage->connectivity_test ) ) {
4100
- // Connectivity test was never executed, or cache was cleared.
4101
- return true;
4102
- }
4103
-
4104
- if ( WP_FS__PING_API_ON_IP_OR_HOST_CHANGES ) {
4105
- if ( WP_FS__IS_HTTP_REQUEST ) {
4106
- if ( $_SERVER['HTTP_HOST'] != $this->_storage->connectivity_test['host'] ) {
4107
- // Domain changed.
4108
- return true;
4109
- }
4110
-
4111
- if ( WP_FS__REMOTE_ADDR != $this->_storage->connectivity_test['server_ip'] ) {
4112
- // Server IP changed.
4113
- return true;
4114
- }
4115
- }
4116
- }
4117
-
4118
- if ( $this->_storage->connectivity_test['is_connected'] &&
4119
- $this->_storage->connectivity_test['is_active']
4120
- ) {
4121
- // API connected and Freemius is active - no need to run connectivity check.
4122
- return false;
4123
- }
4124
-
4125
- if ( $flush_if_no_connectivity ) {
4126
- /**
4127
- * If explicitly asked to flush when no connectivity - do it only
4128
- * if at least 10 sec passed from the last API connectivity test.
4129
- */
4130
- return ( isset( $this->_storage->connectivity_test['timestamp'] ) &&
4131
- ( WP_FS__SCRIPT_START_TIME - $this->_storage->connectivity_test['timestamp'] ) > 10 );
4132
- }
4133
-
4134
- /**
4135
- * @since 1.1.7 Don't check for connectivity on plugin downgrade.
4136
- */
4137
- $version = $this->get_plugin_version();
4138
- if ( version_compare( $version, $this->_storage->connectivity_test['version'], '>' ) ) {
4139
- // If it's a plugin version upgrade and Freemius is off or no connectivity, run connectivity test.
4140
- return true;
4141
- }
4142
-
4143
- return false;
4144
- }
4145
-
4146
- /**
4147
- * @author Vova Feldman (@svovaf)
4148
- * @since 1.1.7.4
4149
- *
4150
- * @param int|null $blog_id Since 2.0.0.
4151
- * @param bool $is_gdpr_test Since 2.0.2. Perform only the GDPR test.
4152
- *
4153
- * @return object|false
4154
- */
4155
- private function ping( $blog_id = null, $is_gdpr_test = false ) {
4156
- if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY ) {
4157
- return false;
4158
- }
4159
-
4160
- $version = $this->get_plugin_version();
4161
-
4162
- $is_update = $this->apply_filters( 'is_plugin_update', $this->is_plugin_update() );
4163
-
4164
- $params = array(
4165
- 'is_update' => json_encode( $is_update ),
4166
- 'version' => $version,
4167
- 'sdk' => $this->version,
4168
- 'is_admin' => json_encode( is_admin() ),
4169
- 'is_ajax' => json_encode( self::is_ajax() ),
4170
- 'is_cron' => json_encode( self::is_cron() ),
4171
- 'is_gdpr_test' => $is_gdpr_test,
4172
- 'is_http' => json_encode( WP_FS__IS_HTTP_REQUEST ),
4173
- );
4174
-
4175
- if ( is_multisite() && function_exists( 'get_network' ) ) {
4176
- $params['network_uid'] = $this->get_anonymous_network_id();
4177
- }
4178
-
4179
- return $this->get_api_plugin_scope()->ping(
4180
- $this->get_anonymous_id( $blog_id ),
4181
- $params
4182
- );
4183
- }
4184
-
4185
- /**
4186
- * Check if there's any connectivity issue to Freemius API.
4187
- *
4188
- * @author Vova Feldman (@svovaf)
4189
- * @since 1.0.9
4190
- *
4191
- * @param bool $flush_if_no_connectivity
4192
- *
4193
- * @return bool
4194
- */
4195
- function has_api_connectivity( $flush_if_no_connectivity = false ) {
4196
- $this->_logger->entrance();
4197
-
4198
- if ( isset( $this->_has_api_connection ) && ( $this->_has_api_connection || ! $flush_if_no_connectivity ) ) {
4199
- return $this->_has_api_connection;
4200
- }
4201
-
4202
- if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY &&
4203
- isset( $this->_storage->connectivity_test ) &&
4204
- true === $this->_storage->connectivity_test['is_connected']
4205
- ) {
4206
- unset( $this->_storage->connectivity_test );
4207
- }
4208
-
4209
- if ( ! $this->should_run_connectivity_test( $flush_if_no_connectivity ) ) {
4210
- $this->_has_api_connection = $this->_storage->connectivity_test['is_connected'];
4211
- /**
4212
- * @since 1.1.6 During dev mode, if there's connectivity - turn Freemius on regardless the configuration.
4213
- *
4214
- * @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.
4215
- */
4216
- $this->_is_on = $this->_storage->connectivity_test['is_active'] ||
4217
- $this->is_premium() ||
4218
- ( WP_FS__DEV_MODE && $this->_has_api_connection && ! WP_FS__SIMULATE_FREEMIUS_OFF );
4219
-
4220
- return $this->_has_api_connection;
4221
- }
4222
-
4223
- $pong = $this->ping();
4224
- $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
4225
-
4226
- if ( ! $is_connected ) {
4227
- // API failure.
4228
- $this->_add_connectivity_issue_message( $pong );
4229
- }
4230
-
4231
- if ( $is_connected ) {
4232
- FS_GDPR_Manager::instance()->store_is_required( $pong->is_gdpr_required );
4233
- }
4234
-
4235
- $this->store_connectivity_info( $pong, $is_connected );
4236
-
4237
- return $this->_has_api_connection;
4238
- }
4239
-
4240
- /**
4241
- * @author Vova Feldman (@svovaf)
4242
- * @since 1.1.7.4
4243
- *
4244
- * @param object $pong
4245
- * @param bool $is_connected
4246
- */
4247
- private function store_connectivity_info( $pong, $is_connected ) {
4248
- $this->_logger->entrance();
4249
-
4250
- $version = $this->get_plugin_version();
4251
-
4252
- if ( ! $is_connected || WP_FS__SIMULATE_FREEMIUS_OFF ) {
4253
- $is_active = false;
4254
- } else {
4255
- $is_active = ( isset( $pong->is_active ) && true == $pong->is_active );
4256
- }
4257
-
4258
- $is_active = $this->apply_filters(
4259
- 'is_on',
4260
- $is_active,
4261
- $this->is_plugin_update(),
4262
- $version
4263
- );
4264
-
4265
- $this->_storage->connectivity_test = array(
4266
- 'is_connected' => $is_connected,
4267
- 'host' => $_SERVER['HTTP_HOST'],
4268
- 'server_ip' => WP_FS__REMOTE_ADDR,
4269
- 'is_active' => $is_active,
4270
- 'timestamp' => WP_FS__SCRIPT_START_TIME,
4271
- // Last version with connectivity attempt.
4272
- 'version' => $version,
4273
- );
4274
-
4275
- $this->_has_api_connection = $is_connected;
4276
- $this->_is_on = $is_active || ( WP_FS__DEV_MODE && $is_connected && ! WP_FS__SIMULATE_FREEMIUS_OFF );
4277
- }
4278
-
4279
- /**
4280
- * Force turning Freemius on.
4281
- *
4282
- * @author Vova Feldman (@svovaf)
4283
- * @since 1.1.8.1
4284
- *
4285
- * @return bool TRUE if successfully turned on.
4286
- */
4287
- private function turn_on() {
4288
- $this->_logger->entrance();
4289
-
4290
- if ( $this->is_on() || ! isset( $this->_storage->connectivity_test['is_active'] ) ) {
4291
- return false;
4292
- }
4293
-
4294
- $updated_connectivity = $this->_storage->connectivity_test;
4295
- $updated_connectivity['is_active'] = true;
4296
- $updated_connectivity['timestamp'] = WP_FS__SCRIPT_START_TIME;
4297
- $this->_storage->connectivity_test = $updated_connectivity;
4298
-
4299
- $this->_is_on = true;
4300
-
4301
- return true;
4302
- }
4303
-
4304
- /**
4305
- * Anonymous and unique site identifier (Hash).
4306
- *
4307
- * @author Vova Feldman (@svovaf)
4308
- * @since 1.1.0
4309
- *
4310
- * @param null|int $blog_id Since 2.0.0
4311
- *
4312
- * @return string
4313
- */
4314
- function get_anonymous_id( $blog_id = null ) {
4315
- $unique_id = self::$_accounts->get_option( 'unique_id', null, $blog_id );
4316
-
4317
- if ( empty( $unique_id ) || ! is_string( $unique_id ) ) {
4318
- $key = fs_strip_url_protocol( get_site_url( $blog_id ) );
4319
-
4320
- $secure_auth = defined( 'SECURE_AUTH_KEY' ) ? SECURE_AUTH_KEY : '';
4321
- if ( empty( $secure_auth ) ||
4322
- false !== strpos( $secure_auth, ' ' ) ||
4323
- 'put your unique phrase here' === $secure_auth
4324
- ) {
4325
- // Protect against default auth key.
4326
- $secure_auth = md5( microtime() );
4327
- }
4328
-
4329
- /**
4330
- * Base the unique identifier on the WP secure authentication key. Which
4331
- * turns the key into a secret anonymous identifier. This will help us
4332
- * to avoid duplicate installs generation on the backend upon opt-in.
4333
- *
4334
- * @author Vova Feldman (@svovaf)
4335
- * @since 1.2.3
4336
- */
4337
- $unique_id = md5( $key . $secure_auth );
4338
-
4339
- self::$_accounts->set_option( 'unique_id', $unique_id, true, $blog_id );
4340
- }
4341
-
4342
- $this->_logger->departure( $unique_id );
4343
-
4344
- return $unique_id;
4345
- }
4346
-
4347
- /**
4348
- * Returns anonymous network ID.
4349
- *
4350
- * @since 2.4.3
4351
- *
4352
- * @return string
4353
- */
4354
- function get_anonymous_network_id() {
4355
- return $this->get_anonymous_id( get_network()->site_id );
4356
- }
4357
-
4358
- /**
4359
- * @author Vova Feldman (@svovaf)
4360
- * @since 1.1.7.4
4361
- *
4362
- * @return \WP_User
4363
- */
4364
- static function _get_current_wp_user() {
4365
- self::require_pluggable_essentials();
4366
- self::wp_cookie_constants();
4367
-
4368
- return wp_get_current_user();
4369
- }
4370
-
4371
- /**
4372
- * Define cookie constants which are required by Freemius::_get_current_wp_user() since
4373
- * it uses wp_get_current_user() which needs the cookie constants set. When a plugin
4374
- * is network activated the cookie constants are only configured after the network
4375
- * plugins activation, therefore, if we don't define those constants WP will throw
4376
- * PHP warnings/notices.
4377
- *
4378
- * @author Vova Feldman (@svovaf)
4379
- * @since 2.1.1
4380
- */
4381
- private static function wp_cookie_constants() {
4382
- if ( defined( 'LOGGED_IN_COOKIE' ) &&
4383
- ( defined( 'AUTH_COOKIE' ) || defined( 'SECURE_AUTH_COOKIE' ) )
4384
- ) {
4385
- return;
4386
- }
4387
-
4388
- /**
4389
- * Used to guarantee unique hash cookies
4390
- *
4391
- * @since 1.5.0
4392
- */
4393
- if ( ! defined( 'COOKIEHASH' ) ) {
4394
- $siteurl = get_site_option( 'siteurl' );
4395
- if ( $siteurl ) {
4396
- define( 'COOKIEHASH', md5( $siteurl ) );
4397
- } else {
4398
- define( 'COOKIEHASH', '' );
4399
- }
4400
- }
4401
-
4402
- if ( ! defined( 'LOGGED_IN_COOKIE' ) ) {
4403
- define( 'LOGGED_IN_COOKIE', 'wordpress_logged_in_' . COOKIEHASH );
4404
- }
4405
-
4406
- /**
4407
- * @since 2.5.0
4408
- */
4409
- if ( ! defined( 'AUTH_COOKIE' ) ) {
4410
- define( 'AUTH_COOKIE', 'wordpress_' . COOKIEHASH );
4411
- }
4412
-
4413
- /**
4414
- * @since 2.6.0
4415
- */
4416
- if ( ! defined( 'SECURE_AUTH_COOKIE' ) ) {
4417
- define( 'SECURE_AUTH_COOKIE', 'wordpress_sec_' . COOKIEHASH );
4418
- }
4419
- }
4420
-
4421
- /**
4422
- * @author Vova Feldman (@svovaf)
4423
- * @since 2.1.0
4424
- *
4425
- * @return int
4426
- */
4427
- static function get_current_wp_user_id() {
4428
- $wp_user = self::_get_current_wp_user();
4429
-
4430
- return $wp_user->ID;
4431
- }
4432
-
4433
- /**
4434
- * @author Vova Feldman (@svovaf)
4435
- * @since 1.2.1.7
4436
- *
4437
- * @param string $email
4438
- *
4439
- * @return bool
4440
- */
4441
- static function is_valid_email( $email ) {
4442
- if ( false === filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
4443
- return false;
4444
- }
4445
-
4446
- $parts = explode( '@', $email );
4447
-
4448
- if ( 2 !== count( $parts ) || empty( $parts[1] ) ) {
4449
- return false;
4450
- }
4451
-
4452
- $blacklist = array(
4453
- 'admin.',
4454
- 'webmaster.',
4455
- 'localhost.',
4456
- 'dev.',
4457
- 'development.',
4458
- 'test.',
4459
- 'stage.',
4460
- 'staging.',
4461
- );
4462
-
4463
- // Make sure domain is not one of the blacklisted.
4464
- foreach ( $blacklist as $invalid ) {
4465
- if ( 0 === strpos( $parts[1], $invalid ) ) {
4466
- return false;
4467
- }
4468
- }
4469
-
4470
- // Get the UTF encoded domain name.
4471
- $domain = idn_to_ascii( $parts[1] ) . '.';
4472
-
4473
- return ( checkdnsrr( $domain, 'MX' ) || checkdnsrr( $domain, 'A' ) );
4474
- }
4475
-
4476
- /**
4477
- * Generate API connectivity issue message.
4478
- *
4479
- * @author Vova Feldman (@svovaf)
4480
- * @since 1.0.9
4481
- *
4482
- * @param mixed $api_result
4483
- * @param bool $is_first_failure
4484
- */
4485
- function _add_connectivity_issue_message( $api_result, $is_first_failure = true ) {
4486
- if ( ! $this->is_premium() && $this->_enable_anonymous ) {
4487
- // Don't add message if it's the free version and can run anonymously.
4488
- return;
4489
- }
4490
-
4491
- if ( ! function_exists( 'wp_nonce_url' ) ) {
4492
- require_once ABSPATH . 'wp-includes/functions.php';
4493
- }
4494
-
4495
- $current_user = self::_get_current_wp_user();
4496
- // $admin_email = get_option( 'admin_email' );
4497
- $admin_email = $current_user->user_email;
4498
-
4499
- // Aliases.
4500
- $deactivate_plugin_title = $this->esc_html_inline( 'That\'s exhausting, please deactivate', 'deactivate-plugin-title' );
4501
- $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' );
4502
- $install_previous_title = $this->esc_html_inline( 'Let\'s try your previous version', 'install-previous-title' );
4503
- $install_previous_desc = $this->esc_html_inline( 'Uninstall this version and install the previous one.', 'install-previous-desc' );
4504
- $fix_issue_title = $this->esc_html_inline( 'Yes - I\'m giving you a chance to fix it', 'fix-issue-title' );
4505
- $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' );
4506
- /* translators: %s: product title (e.g. "Awesome Plugin" requires an access to...) */
4507
- $x_requires_access_to_api = $this->esc_html_inline( '%s requires an access to our API.', 'x-requires-access-to-api' );
4508
- $sysadmin_title = $this->esc_html_inline( 'I\'m a system administrator', 'sysadmin-title' );
4509
- $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' );
4510
-
4511
- $message = false;
4512
- if ( is_object( $api_result ) &&
4513
- isset( $api_result->error ) &&
4514
- isset( $api_result->error->code )
4515
- ) {
4516
- switch ( $api_result->error->code ) {
4517
- case 'curl_missing':
4518
- $missing_methods = '';
4519
- if ( is_array( $api_result->missing_methods ) &&
4520
- ! empty( $api_result->missing_methods )
4521
- ) {
4522
- foreach ( $api_result->missing_methods as $m ) {
4523
- if ( 'curl_version' === $m ) {
4524
- continue;
4525
- }
4526
-
4527
- if ( ! empty( $missing_methods ) ) {
4528
- $missing_methods .= ', ';
4529
- }
4530
-
4531
- $missing_methods .= sprintf( '<code>%s</code>', $m );
4532
- }
4533
-
4534
- if ( ! empty( $missing_methods ) ) {
4535
- $missing_methods = sprintf(
4536
- '<br><br><b>%s</b> %s',
4537
- $this->esc_html_inline( 'Disabled method(s):', 'curl-disabled-methods' ),
4538
- $missing_methods
4539
- );
4540
- }
4541
- }
4542
-
4543
- $message = sprintf(
4544
- $x_requires_access_to_api . ' ' .
4545
- $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' ) . ' ' .
4546
- $missing_methods .
4547
- ' %s',
4548
- '<b>' . $this->get_plugin_name() . '</b>',
4549
- sprintf(
4550
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
4551
- sprintf(
4552
- '<a class="fs-resolve" data-type="curl" href="#"><b>%s</b></a>%s',
4553
- $this->get_text_inline( 'I don\'t know what is cURL or how to install it, help me!', 'curl-missing-no-clue-title' ),
4554
- ' - ' . sprintf(
4555
- $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' ),
4556
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
4557
- )
4558
- ),
4559
- sprintf(
4560
- '<b>%s</b> - %s',
4561
- $sysadmin_title,
4562
- 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 ) ) )
4563
- ),
4564
- sprintf(
4565
- '<a href="%s"><b>%s</b></a> - %s',
4566
- 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 ),
4567
- $deactivate_plugin_title,
4568
- $deactivate_plugin_desc
4569
- )
4570
- )
4571
- );
4572
- break;
4573
- case 'cloudflare_ddos_protection':
4574
- $message = sprintf(
4575
- $x_requires_access_to_api . ' ' .
4576
- $this->esc_html_inline( 'From unknown reason, CloudFlare, the firewall we use, blocks the connection.', 'cloudflare-blocks-connection-message' ) . ' ' .
4577
- $happy_to_resolve_issue_asap .
4578
- ' %s',
4579
- '<b>' . $this->get_plugin_name() . '</b>',
4580
- sprintf(
4581
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
4582
- sprintf(
4583
- '<a class="fs-resolve" data-type="cloudflare" href="#"><b>%s</b></a>%s',
4584
- $fix_issue_title,
4585
- ' - ' . sprintf(
4586
- $fix_issue_desc,
4587
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
4588
- )
4589
- ),
4590
- sprintf(
4591
- '<a href="%s" target="_blank" rel="noopener noreferrer"><b>%s</b></a> - %s',
4592
- sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),
4593
- $install_previous_title,
4594
- $install_previous_desc
4595
- ),
4596
- sprintf(
4597
- '<a href="%s"><b>%s</b></a> - %s',
4598
- 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 ),
4599
- $deactivate_plugin_title,
4600
- $deactivate_plugin_desc
4601
- )
4602
- )
4603
- );
4604
- break;
4605
- case 'squid_cache_block':
4606
- $message = sprintf(
4607
- $x_requires_access_to_api . ' ' .
4608
- $this->esc_html_inline( 'It looks like your server is using Squid ACL (access control lists), which blocks the connection.', 'squid-blocks-connection-message' ) .
4609
- ' %s',
4610
- '<b>' . $this->get_plugin_name() . '</b>',
4611
- sprintf(
4612
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
4613
- sprintf(
4614
- '<a class="fs-resolve" data-type="squid" href="#"><b>%s</b></a> - %s',
4615
- $this->esc_html_inline( 'I don\'t know what is Squid or ACL, help me!', 'squid-no-clue-title' ),
4616
- sprintf(
4617
- $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' ),
4618
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
4619
- )
4620
- ),
4621
- sprintf(
4622
- '<b>%s</b> - %s',
4623
- $sysadmin_title,
4624
- sprintf(
4625
- $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' ),
4626
- // We use a filter since the plugin might require additional API connectivity.
4627
- '<b>' . implode( ', ', $this->apply_filters( 'api_domains', array(
4628
- 'api.freemius.com',
4629
- 'wp.freemius.com'
4630
- ) ) ) . '</b>',
4631
- $this->_module_type
4632
- )
4633
- ),
4634
- sprintf(
4635
- '<a href="%s"><b>%s</b></a> - %s',
4636
- 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 ),
4637
- $deactivate_plugin_title,
4638
- $deactivate_plugin_desc
4639
- )
4640
- )
4641
- );
4642
- break;
4643
- // default:
4644
- // $message = $this->get_text_inline( 'connectivity-test-fails-message' );
4645
- // break;
4646
- }
4647
- }
4648
-
4649
- $message_id = 'failed_connect_api';
4650
- $type = 'error';
4651
-
4652
- $connectivity_test_fails_message = $this->esc_html_inline( 'From unknown reason, the API connectivity test failed.', 'connectivity-test-fails-message' );
4653
-
4654
- if ( false === $message ) {
4655
- if ( $is_first_failure ) {
4656
- // First attempt failed.
4657
- $message = sprintf(
4658
- $x_requires_access_to_api . ' ' .
4659
- $connectivity_test_fails_message . ' ' .
4660
- $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>' .
4661
- '%s',
4662
- '<b>' . $this->get_plugin_name() . '</b>',
4663
- sprintf(
4664
- '<div id="fs_firewall_issue_options">%s %s</div>',
4665
- sprintf(
4666
- '<a class="button button-primary fs-resolve" data-type="retry_ping" href="#">%s</a>',
4667
- $this->get_text_inline( 'Yes - do your thing', 'yes-do-your-thing' )
4668
- ),
4669
- sprintf(
4670
- '<a href="%s" class="button">%s</a>',
4671
- 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 ),
4672
- $this->get_text_inline( 'No - just deactivate', 'no-deactivate' )
4673
- )
4674
- )
4675
- );
4676
-
4677
- $message_id = 'failed_connect_api_first';
4678
- $type = 'promotion';
4679
- } else {
4680
- // Second connectivity attempt failed.
4681
- $message = sprintf(
4682
- $x_requires_access_to_api . ' ' .
4683
- $connectivity_test_fails_message . ' ' .
4684
- $happy_to_resolve_issue_asap .
4685
- ' %s',
4686
- '<b>' . $this->get_plugin_name() . '</b>',
4687
- sprintf(
4688
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
4689
- sprintf(
4690
- '<a class="fs-resolve" data-type="general" href="#"><b>%s</b></a>%s',
4691
- $fix_issue_title,
4692
- ' - ' . sprintf(
4693
- $fix_issue_desc,
4694
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
4695
- )
4696
- ),
4697
- sprintf(
4698
- '<a href="%s" target="_blank" rel="noopener noreferrer"><b>%s</b></a> - %s',
4699
- sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),
4700
- $install_previous_title,
4701
- $install_previous_desc
4702
- ),
4703
- sprintf(
4704
- '<a href="%s"><b>%s</b></a> - %s',
4705
- 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 ),
4706
- $deactivate_plugin_title,
4707
- $deactivate_plugin_desc
4708
- )
4709
- )
4710
- );
4711
- }
4712
- }
4713
-
4714
- $this->_admin_notices->add_sticky(
4715
- $message,
4716
- $message_id,
4717
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
4718
- $type
4719
- );
4720
- }
4721
-
4722
- /**
4723
- * Handle user request to resolve connectivity issue.
4724
- * This method will send an email to Freemius API technical staff for resolution.
4725
- * The email will contain server's info and installed plugins (might be caching issue).
4726
- *
4727
- * @author Vova Feldman (@svovaf)
4728
- * @since 1.0.9
4729
- */
4730
- function _email_about_firewall_issue() {
4731
- check_admin_referer( 'fs_resolve_firewall_issues' );
4732
-
4733
- if ( ! current_user_can( is_multisite() ? 'manage_options' : 'activate_plugins' ) ) {
4734
- return;
4735
- }
4736
-
4737
- $this->_admin_notices->remove_sticky( 'failed_connect_api' );
4738
-
4739
- $pong = $this->ping();
4740
-
4741
- $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
4742
-
4743
- if ( $is_connected ) {
4744
- FS_GDPR_Manager::instance()->store_is_required( $pong->is_gdpr_required );
4745
-
4746
- $this->store_connectivity_info( $pong, $is_connected );
4747
-
4748
- echo $this->get_after_plugin_activation_redirect_url();
4749
- exit;
4750
- }
4751
-
4752
- $current_user = self::_get_current_wp_user();
4753
- $admin_email = $current_user->user_email;
4754
-
4755
- $error_type = fs_request_get( 'error_type', 'general' );
4756
-
4757
- switch ( $error_type ) {
4758
- case 'squid':
4759
- $title = 'Squid ACL Blocking Issue';
4760
- break;
4761
- case 'cloudflare':
4762
- $title = 'CloudFlare Blocking Issue';
4763
- break;
4764
- default:
4765
- $title = 'API Connectivity Issue';
4766
- break;
4767
- }
4768
-
4769
- $custom_email_sections = array();
4770
-
4771
- // Add 'API Error' custom email section.
4772
- $custom_email_sections['api_error'] = array(
4773
- 'title' => 'API Error',
4774
- 'rows' => array(
4775
- 'ping' => array(
4776
- 'API Error',
4777
- is_string( $pong ) ? htmlentities( $pong ) : json_encode( $pong )
4778
- ),
4779
- )
4780
- );
4781
-
4782
- // Send email with technical details to resolve API connectivity issues.
4783
- $this->send_email(
4784
- 'api@freemius.com', // recipient
4785
- $title . ' [' . $this->get_plugin_name() . ']', // subject
4786
- $custom_email_sections,
4787
- array( "Reply-To: $admin_email <$admin_email>" ) // headers
4788
- );
4789
-
4790
- $this->_admin_notices->add_sticky(
4791
- sprintf(
4792
- $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' ),
4793
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
4794
- ),
4795
- 'server_details_sent'
4796
- );
4797
-
4798
- // Action was taken, tell that API connectivity troubleshooting should be off now.
4799
-
4800
- echo "1";
4801
- exit;
4802
- }
4803
-
4804
- /**
4805
- * Handle connectivity test retry approved by the user.
4806
- *
4807
- * @author Vova Feldman (@svovaf)
4808
- * @since 1.1.7.4
4809
- */
4810
- function _retry_connectivity_test() {
4811
- check_admin_referer( 'fs_retry_connectivity_test' );
4812
-
4813
- if ( ! current_user_can( is_multisite() ? 'manage_options' : 'activate_plugins' ) ) {
4814
- return;
4815
- }
4816
-
4817
- $this->_admin_notices->remove_sticky( 'failed_connect_api_first' );
4818
-
4819
- $pong = $this->ping();
4820
-
4821
- $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
4822
-
4823
- if ( $is_connected ) {
4824
- FS_GDPR_Manager::instance()->store_is_required( $pong->is_gdpr_required );
4825
-
4826
- $this->store_connectivity_info( $pong, $is_connected );
4827
-
4828
- echo $this->get_after_plugin_activation_redirect_url();
4829
- } else {
4830
- // Add connectivity issue message after 2nd failed attempt.
4831
- $this->_add_connectivity_issue_message( $pong, false );
4832
-
4833
- echo "1";
4834
- }
4835
-
4836
- exit;
4837
- }
4838
-
4839
- static function _add_firewall_issues_javascript() {
4840
- $params = array();
4841
- fs_require_once_template( 'firewall-issues-js.php', $params );
4842
- }
4843
-
4844
- #endregion
4845
-
4846
- #----------------------------------------------------------------------------------
4847
- #region Email
4848
- #----------------------------------------------------------------------------------
4849
-
4850
- /**
4851
- * Generates and sends an HTML email with customizable sections.
4852
- *
4853
- * @author Leo Fajardo (@leorw)
4854
- * @since 1.1.2
4855
- *
4856
- * @param string $to_address
4857
- * @param string $subject
4858
- * @param array $sections
4859
- * @param array $headers
4860
- *
4861
- * @return bool Whether the email contents were sent successfully.
4862
- */
4863
- private function send_email(
4864
- $to_address,
4865
- $subject,
4866
- $sections = array(),
4867
- $headers = array()
4868
- ) {
4869
- $default_sections = $this->get_email_sections();
4870
-
4871
- // Insert new sections or replace the default email sections.
4872
- if ( is_array( $sections ) && ! empty( $sections ) ) {
4873
- foreach ( $sections as $section_id => $custom_section ) {
4874
- if ( ! isset( $default_sections[ $section_id ] ) ) {
4875
- // If the section does not exist, add it.
4876
- $default_sections[ $section_id ] = $custom_section;
4877
- } else {
4878
- // If the section already exists, override it.
4879
- $current_section = $default_sections[ $section_id ];
4880
-
4881
- // Replace the current section's title if a custom section title exists.
4882
- if ( isset( $custom_section['title'] ) ) {
4883
- $current_section['title'] = $custom_section['title'];
4884
- }
4885
-
4886
- // Insert new rows under the current section or replace the default rows.
4887
- if ( isset( $custom_section['rows'] ) && is_array( $custom_section['rows'] ) && ! empty( $custom_section['rows'] ) ) {
4888
- foreach ( $custom_section['rows'] as $row_id => $row ) {
4889
- $current_section['rows'][ $row_id ] = $row;
4890
- }
4891
- }
4892
-
4893
- $default_sections[ $section_id ] = $current_section;
4894
- }
4895
- }
4896
- }
4897
-
4898
- $vars = array( 'sections' => $default_sections );
4899
- $message = fs_get_template( 'email.php', $vars );
4900
-
4901
- // Set the type of email to HTML.
4902
- $headers[] = 'Content-type: text/html; charset=UTF-8';
4903
-
4904
- $header_string = implode( "\r\n", $headers );
4905
-
4906
- return wp_mail(
4907
- $to_address,
4908
- $subject,
4909
- $message,
4910
- $header_string
4911
- );
4912
- }
4913
-
4914
- /**
4915
- * Generates the data for the sections of the email content.
4916
- *
4917
- * @author Leo Fajardo (@leorw)
4918
- * @since 1.1.2
4919
- *
4920
- * @return array
4921
- */
4922
- private function get_email_sections() {
4923
- // Retrieve the current user's information so that we can get the user's email, first name, and last name below.
4924
- $current_user = self::_get_current_wp_user();
4925
-
4926
- // Retrieve the cURL version information so that we can get the version number below.
4927
- $curl_version_information = curl_version();
4928
-
4929
- $active_plugin = self::get_active_plugins();
4930
-
4931
- // Generate the list of active plugins separated by new line.
4932
- $active_plugin_string = '';
4933
- foreach ( $active_plugin as $plugin ) {
4934
- $active_plugin_string .= sprintf(
4935
- '<a href="%s">%s</a> [v%s]<br>',
4936
- $plugin['PluginURI'],
4937
- $plugin['Name'],
4938
- $plugin['Version']
4939
- );
4940
- }
4941
-
4942
- $server_ip = WP_FS__REMOTE_ADDR;
4943
-
4944
- // Add PHP info for deeper investigation.
4945
- ob_start();
4946
- phpinfo();
4947
- $php_info = ob_get_clean();
4948
-
4949
- $api_domain = substr( FS_API__ADDRESS, strpos( FS_API__ADDRESS, ':' ) + 3 );
4950
-
4951
- // Generate the default email sections.
4952
- $sections = array(
4953
- 'sdk' => array(
4954
- 'title' => 'SDK',
4955
- 'rows' => array(
4956
- 'fs_version' => array( 'FS Version', $this->version ),
4957
- 'curl_version' => array( 'cURL Version', $curl_version_information['version'] )
4958
- )
4959
- ),
4960
- 'plugin' => array(
4961
- 'title' => ucfirst( $this->get_module_type() ),
4962
- 'rows' => array(
4963
- 'name' => array( 'Name', $this->get_plugin_name() ),
4964
- 'version' => array( 'Version', $this->get_plugin_version() )
4965
- )
4966
- ),
4967
- 'api' => array(
4968
- 'title' => 'API Subdomain',
4969
- 'rows' => array(
4970
- 'dns' => array(
4971
- 'DNS_CNAME',
4972
- function_exists( 'dns_get_record' ) ?
4973
- var_export( dns_get_record( $api_domain, DNS_CNAME ), true ) :
4974
- 'dns_get_record() disabled/blocked'
4975
- ),
4976
- 'ip' => array(
4977
- 'IP',
4978
- function_exists( 'gethostbyname' ) ?
4979
- gethostbyname( $api_domain ) :
4980
- 'gethostbyname() disabled/blocked'
4981
- ),
4982
- ),
4983
- ),
4984
- 'site' => array(
4985
- 'title' => 'Site',
4986
- 'rows' => array(
4987
- 'unique_id' => array( 'Unique ID', $this->get_anonymous_id() ),
4988
- 'address' => array( 'Address', site_url() ),
4989
- 'host' => array(
4990
- 'HTTP_HOST',
4991
- ( ! empty( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : '' )
4992
- ),
4993
- 'hosting' => array(
4994
- 'Hosting Company' => fs_request_has( 'hosting_company' ) ?
4995
- fs_request_get( 'hosting_company' ) :
4996
- 'Unknown',
4997
- ),
4998
- 'server_addr' => array(
4999
- 'SERVER_ADDR',
5000
- '<a href="http://www.projecthoneypot.org/ip_' . $server_ip . '">' . $server_ip . '</a>'
5001
- )
5002
- )
5003
- ),
5004
- 'user' => array(
5005
- 'title' => 'User',
5006
- 'rows' => array(
5007
- 'email' => array( 'Email', $current_user->user_email ),
5008
- 'first' => array( 'First', $current_user->user_firstname ),
5009
- 'last' => array( 'Last', $current_user->user_lastname )
5010
- )
5011
- ),
5012
- 'plugins' => array(
5013
- 'title' => 'Plugins',
5014
- 'rows' => array(
5015
- 'active_plugins' => array( 'Active Plugins', $active_plugin_string )
5016
- )
5017
- ),
5018
- 'php_info' => array(
5019
- 'title' => 'PHP Info',
5020
- 'rows' => array(
5021
- 'info' => array( $php_info )
5022
- ),
5023
- )
5024
- );
5025
-
5026
- // Allow the sections to be modified by other code.
5027
- $sections = $this->apply_filters( 'email_template_sections', $sections );
5028
-
5029
- return $sections;
5030
- }
5031
-
5032
- #endregion
5033
-
5034
- #----------------------------------------------------------------------------------
5035
- #region Initialization
5036
- #----------------------------------------------------------------------------------
5037
-
5038
- /**
5039
- * Init plugin's Freemius instance.
5040
- *
5041
- * @author Vova Feldman (@svovaf)
5042
- * @since 1.0.1
5043
- *
5044
- * @param number $id
5045
- * @param string $public_key
5046
- * @param bool $is_live
5047
- * @param bool $is_premium
5048
- */
5049
- function init( $id, $public_key, $is_live = true, $is_premium = true ) {
5050
- $this->_logger->entrance();
5051
-
5052
- $this->dynamic_init( array(
5053
- 'id' => $id,
5054
- 'public_key' => $public_key,
5055
- 'is_live' => $is_live,
5056
- 'is_premium' => $is_premium,
5057
- ) );
5058
- }
5059
-
5060
- /**
5061
- * Dynamic initiator, originally created to support initiation
5062
- * with parent_id for add-ons.
5063
- *
5064
- * @author Vova Feldman (@svovaf)
5065
- * @since 1.0.6
5066
- *
5067
- * @param array $plugin_info
5068
- *
5069
- * @throws Freemius_Exception
5070
- */
5071
- function dynamic_init( array $plugin_info ) {
5072
- $this->_logger->entrance();
5073
-
5074
- $this->parse_settings( $plugin_info );
5075
-
5076
- $this->register_after_settings_parse_hooks();
5077
-
5078
- if ( $this->should_stop_execution() ) {
5079
- return;
5080
- }
5081
-
5082
- if ( ! $this->is_registered() ) {
5083
- if ( $this->is_anonymous() ) {
5084
- // If user skipped, no need to test connectivity.
5085
- $this->_has_api_connection = true;
5086
- $this->_is_on = true;
5087
- } else {
5088
- if ( ! $this->has_api_connectivity() ) {
5089
- if ( $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) ||
5090
- $this->_admin_notices->has_sticky( 'failed_connect_api' )
5091
- ) {
5092
- if ( ! $this->_enable_anonymous || $this->is_premium() ) {
5093
- // If anonymous mode is disabled, add firewall admin-notice message.
5094
- add_action( 'admin_footer', array( 'Freemius', '_add_firewall_issues_javascript' ) );
5095
-
5096
- $ajax_action_suffix = $this->_slug . ( $this->is_theme() ? ':theme' : '' );
5097
- add_action( "wp_ajax_fs_resolve_firewall_issues_{$ajax_action_suffix}", array(
5098
- &$this,
5099
- '_email_about_firewall_issue'
5100
- ) );
5101
-
5102
- add_action( "wp_ajax_fs_retry_connectivity_test_{$ajax_action_suffix}", array(
5103
- &$this,
5104
- '_retry_connectivity_test'
5105
- ) );
5106
-
5107
- /**
5108
- * Currently the admin notice manager relies on the module's type and slug. The new AJAX actions manager uses module IDs, hence, consider to replace the if block above with the commented code below after adjusting the admin notices manager to work with module IDs.
5109
- *
5110
- * @author Vova Feldman (@svovaf)
5111
- * @since 2.0.0
5112
- */
5113
- /*$this->add_ajax_action( 'resolve_firewall_issues', array(
5114
- &$this,
5115
- '_email_about_firewall_issue'
5116
- ) );
5117
-
5118
- $this->add_ajax_action( 'retry_connectivity_test', array(
5119
- &$this,
5120
- '_retry_connectivity_test'
5121
- ) );*/
5122
- }
5123
- }
5124
-
5125
- return;
5126
- } else {
5127
- $this->_admin_notices->remove_sticky( array(
5128
- 'failed_connect_api_first',
5129
- 'failed_connect_api',
5130
- ) );
5131
-
5132
- if ( $this->_anonymous_mode ) {
5133
- // Simulate anonymous mode.
5134
- $this->_is_anonymous = true;
5135
- }
5136
- }
5137
- }
5138
- }
5139
-
5140
- /**
5141
- * This should be executed even if Freemius is off for the core module,
5142
- * otherwise, the add-ons dialogbox won't work properly. This is esepcially
5143
- * relevant when the developer decided to turn FS off for existing users.
5144
- *
5145
- * @author Vova Feldman (@svovaf)
5146
- */
5147
- if ( $this->is_user_in_admin() &&
5148
- 'plugin-information' === fs_request_get( 'tab', false ) &&
5149
- $this->should_use_freemius_updater_and_dialog() &&
5150
- (
5151
- ( $this->is_addon() && $this->get_slug() == fs_request_get( 'plugin', false ) ) ||
5152
- ( $this->has_addons() && $this->get_id() == fs_request_get( 'parent_plugin_id', false ) )
5153
- )
5154
- ) {
5155
- require_once WP_FS__DIR_INCLUDES . '/fs-plugin-info-dialog.php';
5156
-
5157
- new FS_Plugin_Info_Dialog( $this->is_addon() ? $this->get_parent_instance() : $this );
5158
- }
5159
-
5160
- // Check if Freemius is on for the current plugin.
5161
- // This MUST be executed after all the plugin variables has been loaded.
5162
- if ( ! $this->is_registered() && ! $this->is_on() ) {
5163
- return;
5164
- }
5165
-
5166
- if ( $this->has_api_connectivity() ) {
5167
- if ( self::is_cron() ) {
5168
- $this->hook_callback_to_sync_cron();
5169
- } else if ( $this->is_user_in_admin() ) {
5170
- /**
5171
- * Schedule daily data sync cron if:
5172
- *
5173
- * 1. User opted-in (for tracking).
5174
- * 2. If skipped, but later upgraded (opted-in via upgrade).
5175
- *
5176
- * @author Vova Feldman (@svovaf)
5177
- * @since 1.1.7.3
5178
- *
5179
- */
5180
- if ( $this->is_registered() && $this->is_tracking_allowed() ) {
5181
- $this->maybe_schedule_sync_cron();
5182
- }
5183
-
5184
- /**
5185
- * Check if requested for manual blocking background sync.
5186
- */
5187
- if ( fs_request_has( 'background_sync' ) ) {
5188
- $this->run_manual_sync();
5189
- }
5190
- }
5191
- }
5192
-
5193
- if ( $this->is_registered() ) {
5194
- FS_Clone_Manager::instance()->maybe_resolve_new_subsite_install_automatically( $this );
5195
-
5196
- $this->hook_callback_to_install_sync();
5197
- }
5198
-
5199
- if ( $this->is_addon() ) {
5200
- if ( $this->is_parent_plugin_installed() ) {
5201
- // Link to parent FS.
5202
- $this->_parent = self::get_instance_by_id( $this->_plugin->parent_plugin_id );
5203
-
5204
- // Get parent plugin reference.
5205
- $this->_parent_plugin = $this->_parent->get_plugin();
5206
- }
5207
- }
5208
-
5209
- if ( $this->is_user_in_admin() ) {
5210
- if ( $this->is_registered() && fs_request_has( 'purchase_completed' ) ) {
5211
- $this->_admin_notices->add_sticky(
5212
- sprintf(
5213
- /* translators: %s: License type (e.g. you have a professional license) */
5214
- $this->get_text_inline( 'You have purchased a %s license.', 'you-have-x-license' ),
5215
- fs_request_get( 'purchased_plan' )
5216
- ) .
5217
- sprintf(
5218
- $this->get_text_inline(" The %s's %sdownload link%s, license key, and installation instructions have been sent to %s. If you can't find the email after 5 min, please check your spam box.", 'post-purchase-email-sent-message' ),
5219
- $this->get_module_label( true ),
5220
- ( FS_Plugin::is_valid_id( $this->get_bundle_id() ) ? "products' " : '' ),
5221
- ( FS_Plugin::is_valid_id( $this->get_bundle_id() ) ? 's' : '' ),
5222
- sprintf(
5223
- '<strong>%s</strong>',
5224
- fs_request_get( 'purchase_email' )
5225
- )
5226
- ),
5227
- 'plan_purchased',
5228
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
5229
- );
5230
- }
5231
-
5232
- if ( $this->is_addon() ) {
5233
- if ( ! $this->is_parent_plugin_installed() ) {
5234
- $parent_name = $this->get_option( $plugin_info, 'parent_name', null );
5235
-
5236
- if ( isset( $plugin_info['parent'] ) ) {
5237
- $parent_name = $this->get_option( $plugin_info['parent'], 'name', null );
5238
- }
5239
-
5240
- $this->_admin_notices->add(
5241
- ( ! empty( $parent_name ) ?
5242
- 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 ) :
5243
- 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() )
5244
- ),
5245
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
5246
- 'error'
5247
- );
5248
-
5249
- return;
5250
- } else {
5251
- $is_network_admin = fs_is_network_admin();
5252
-
5253
- if ( ! $this->_parent->is_registered() && $this->is_registered() ) {
5254
- // If add-on activated and parent not, automatically install parent for the user.
5255
- $this->activate_parent_account( $this->_parent );
5256
- } else if (
5257
- $this->_parent->is_registered() &&
5258
- ! $this->is_registered() &&
5259
- /**
5260
- * If not registered for add-on and the following conditions for the add-on are met, activate add-on account.
5261
- * * Network active and in network admin - network activate add-on account.
5262
- * * Network active and not in network admin - activate add-on account for the current blog.
5263
- * * Not network active and not in network admin - activate add-on account for the current blog.
5264
- *
5265
- * If not registered for add-on, not network active, and in network admin, do not handle the add-on activation.
5266
- *
5267
- * @author Leo Fajardo (@leorw)
5268
- * @since 2.3.0
5269
- */
5270
- ( $this->is_network_active() || ! $is_network_admin )
5271
- ) {
5272
- $premium_license = null;
5273
-
5274
- if (
5275
- ! $this->has_free_plan() &&
5276
- $this->is_bundle_license_auto_activation_enabled() &&
5277
- $this->_parent->is_activated_with_bundle_license()
5278
- ) {
5279
- /**
5280
- * If the add-on has no free plan, try to activate the account only when there's a bundle license.
5281
- *
5282
- * @author Leo Fajardo (@leorw)
5283
- * @since 2.4.0
5284
- */
5285
- $bundle_license = $this->get_active_parent_license( $this->_parent->_get_license()->secret_key, false );
5286
-
5287
- if (
5288
- is_object( $bundle_license ) &&
5289
- ! empty( $bundle_license->products ) &&
5290
- in_array( $this->get_id(), $bundle_license->products )
5291
- ) {
5292
- $premium_license = $bundle_license;
5293
- }
5294
- }
5295
-
5296
- if ( $this->has_free_plan() || is_object( $premium_license) ) {
5297
- // If parent plugin activated, automatically install add-on for the user.
5298
- $this->_activate_addon_account(
5299
- $this->_parent,
5300
- ( $this->is_network_active() && $is_network_admin ) ?
5301
- true :
5302
- get_current_blog_id(),
5303
- $premium_license
5304
- );
5305
- }
5306
- }
5307
-
5308
- // @todo This should be only executed on activation. It should be migrated to register_activation_hook() together with other activation related logic.
5309
- if ( $this->is_premium() ) {
5310
- // Remove add-on download admin-notice.
5311
- $this->_parent->_admin_notices->remove_sticky( array(
5312
- 'addon_plan_upgraded_' . $this->_slug,
5313
- 'no_addon_license_' . $this->_slug,
5314
- ) );
5315
- }
5316
-
5317
- // $this->deactivate_premium_only_addon_without_license();
5318
- }
5319
- }
5320
-
5321
- add_action( 'admin_init', array( &$this, '_admin_init_action' ) );
5322
-
5323
- // if ( $this->is_registered() ||
5324
- // $this->is_anonymous() ||
5325
- // $this->is_pending_activation()
5326
- // ) {
5327
- // $this->_init_admin();
5328
- // }
5329
- }
5330
-
5331
- /**
5332
- * Should be called outside `$this->is_user_in_admin()` scope
5333
- * because the updater has some logic that needs to be executed
5334
- * during AJAX calls.
5335
- *
5336
- * Currently we need to hook to the `http_request_host_is_external` filter.
5337
- * In the future, there might be additional logic added.
5338
- *
5339
- * @author Vova Feldman
5340
- * @since 1.2.1.6
5341
- */
5342
- if (
5343
- $this->should_use_freemius_updater_and_dialog() &&
5344
- (
5345
- $this->is_premium() ||
5346
- /**
5347
- * If not premium but the premium version is installed, also instantiate the updater so that the
5348
- * plugin information dialog of the premium version will have the information from the server.
5349
- *
5350
- * @author Leo Fajardo (@leorw)
5351
- * @since 2.2.3
5352
- */
5353
- ( file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $this->premium_plugin_basename() ) ) )
5354
- ) &&
5355
- $this->has_release_on_freemius() &&
5356
- ( ! $this->is_unresolved_clone() )
5357
- ) {
5358
- FS_Plugin_Updater::instance( $this );
5359
- }
5360
-
5361
- $this->do_action( 'initiated' );
5362
-
5363
- if ( $this->_storage->prev_is_premium !== $this->_plugin->is_premium ) {
5364
- if ( isset( $this->_storage->prev_is_premium ) ) {
5365
- $this->apply_filters(
5366
- 'after_code_type_change',
5367
- // New code type.
5368
- $this->_plugin->is_premium
5369
- );
5370
- } else {
5371
- // Set for code type for the first time.
5372
- $this->_storage->prev_is_premium = $this->_plugin->is_premium;
5373
- }
5374
- }
5375
-
5376
- if ( ! $this->is_addon() ) {
5377
- if ( $this->is_registered() ) {
5378
- // Fix for upgrade from versions < 1.0.9.
5379
- if ( ! isset( $this->_storage->activation_timestamp ) ) {
5380
- $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME;
5381
- }
5382
-
5383
- $this->do_action( 'after_init_plugin_registered' );
5384
- } else if ( $this->is_anonymous() ) {
5385
- $this->do_action( 'after_init_plugin_anonymous' );
5386
- } else if ( $this->is_pending_activation() ) {
5387
- $this->do_action( 'after_init_plugin_pending_activations' );
5388
- }
5389
- } else {
5390
- if ( $this->is_registered() ) {
5391
- $this->do_action( 'after_init_addon_registered' );
5392
- } else if ( $this->is_anonymous() ) {
5393
- $this->do_action( 'after_init_addon_anonymous' );
5394
- } else if ( $this->is_pending_activation() ) {
5395
- $this->do_action( 'after_init_addon_pending_activations' );
5396
- }
5397
- }
5398
- }
5399
-
5400
- /**
5401
- * @author Leo Fajardo (@leorw)
5402
- * @since 2.2.3
5403
- *
5404
- * @return bool
5405
- */
5406
- private function should_use_freemius_updater_and_dialog() {
5407
- return (
5408
- /**
5409
- * Allow updater and dialog when the `fs_allow_updater_and_dialog` URL query param exists and has `true`
5410
- * value, or when the current page is not the "Add Plugins" page (/plugin-install.php) and the `action`
5411
- * URL query param doesn't exist or its value is not `install-plugin` so that there will be no conflicts
5412
- * with the .org plugins' functionalities (e.g. installation from the "Add Plugins" page and viewing
5413
- * plugin details from .org).
5414
- */
5415
- ( true === fs_request_get_bool( 'fs_allow_updater_and_dialog' ) ) ||
5416
- (
5417
- ! self::is_plugin_install_page() &&
5418
- // Disallow updater and dialog when installing a plugin, otherwise .org "add-on" plugins will be affected.
5419
- ( 'install-plugin' !== fs_request_get( 'action' ) )
5420
- )
5421
- );
5422
- }
5423
-
5424
- /**
5425
- * @author Leo Fajardo (@leorw)
5426
- *
5427
- * @since 1.2.1.5
5428
- */
5429
- function _stop_tracking_callback() {
5430
- $this->_logger->entrance();
5431
-
5432
- $this->check_ajax_referer( 'stop_tracking' );
5433
-
5434
- $result = $this->stop_tracking( fs_is_network_admin() );
5435
-
5436
- if ( true === $result ) {
5437
- self::shoot_ajax_success();
5438
- }
5439
-
5440
- $this->_logger->api_error( $result );
5441
-
5442
- self::shoot_ajax_failure(
5443
- sprintf( $this->get_text_inline( 'Unexpected API error. Please contact the %s\'s author with the following error.', 'unexpected-api-error' ), $this->_module_type ) .
5444
- ( $this->is_api_error( $result ) && isset( $result->error ) ?
5445
- $result->error->message :
5446
- var_export( $result, true ) )
5447
- );
5448
- }
5449
-
5450
- /**
5451
- * @author Leo Fajardo (@leorw)
5452
- * @since 1.2.1.5
5453
- */
5454
- function _allow_tracking_callback() {
5455
- $this->_logger->entrance();
5456
-
5457
- $this->check_ajax_referer( 'allow_tracking' );
5458
-
5459
- $result = $this->allow_tracking( fs_is_network_admin() );
5460
-
5461
- if ( true === $result ) {
5462
- self::shoot_ajax_success();
5463
- }
5464
-
5465
- $this->_logger->api_error( $result );
5466
-
5467
- self::shoot_ajax_failure(
5468
- sprintf( $this->get_text_inline( 'Unexpected API error. Please contact the %s\'s author with the following error.', 'unexpected-api-error' ), $this->_module_type ) .
5469
- ( $this->is_api_error( $result ) && isset( $result->error ) ?
5470
- $result->error->message :
5471
- var_export( $result, true ) )
5472
- );
5473
- }
5474
-
5475
- /**
5476
- * Opt-out from usage tracking.
5477
- *
5478
- * Note: This will not delete the account information but will stop all tracking.
5479
- *
5480
- * Returns:
5481
- * 1. FALSE - If the user never opted-in.
5482
- * 2. TRUE - If successfully opted-out.
5483
- * 3. object - API result on failure.
5484
- *
5485
- * @author Leo Fajardo (@leorw)
5486
- * @since 1.2.1.5
5487
- *
5488
- * @return bool|object
5489
- */
5490
- function stop_site_tracking() {
5491
- $this->_logger->entrance();
5492
-
5493
- if ( ! $this->is_registered() ) {
5494
- // User never opted-in.
5495
- return false;
5496
- }
5497
-
5498
- if ( $this->is_tracking_prohibited() ) {
5499
- // Already disconnected.
5500
- return true;
5501
- }
5502
-
5503
- // Send update to FS.
5504
- $result = $this->api_site_call( '/?fields=is_disconnected', 'put', array(
5505
- 'is_disconnected' => true
5506
- ) );
5507
-
5508
- if ( ! $this->is_api_result_entity( $result ) ||
5509
- ! isset( $result->is_disconnected ) ||
5510
- ! $result->is_disconnected
5511
- ) {
5512
- $this->_logger->api_error( $result );
5513
-
5514
- return $result;
5515
- }
5516
-
5517
- $this->_site->is_disconnected = $result->is_disconnected;
5518
- $this->_store_site();
5519
-
5520
- $this->clear_sync_cron();
5521
-
5522
- // Successfully disconnected.
5523
- return true;
5524
- }
5525
-
5526
- /**
5527
- * Opt-out network from usage tracking.
5528
- *
5529
- * Note: This will not delete the account information but will stop all tracking.
5530
- *
5531
- * Returns:
5532
- * 1. FALSE - If the user never opted-in.
5533
- * 2. TRUE - If successfully opted-out.
5534
- * 3. object - API result on failure.
5535
- *
5536
- * @author Leo Fajardo (@leorw)
5537
- * @since 1.2.1.5
5538
- *
5539
- * @return bool|object
5540
- */
5541
- function stop_network_tracking() {
5542
- $this->_logger->entrance();
5543
-
5544
- if ( ! $this->is_registered() ) {
5545
- // User never opted-in.
5546
- return false;
5547
- }
5548
-
5549
- $install_id_2_blog_id = array();
5550
- $installs_map = $this->get_blog_install_map();
5551
-
5552
- $opt_out_all = true;
5553
-
5554
- $params = array();
5555
- foreach ( $installs_map as $blog_id => $install ) {
5556
- if ( $install->is_tracking_prohibited() ) {
5557
- // Already opted-out.
5558
- continue;
5559
- }
5560
-
5561
- if ( $this->is_site_delegated_connection( $blog_id ) ) {
5562
- // Opt-out only from non-delegated installs.
5563
- $opt_out_all = false;
5564
- continue;
5565
- }
5566
-
5567
- $params[] = array( 'id' => $install->id );
5568
-
5569
- $install_id_2_blog_id[ $install->id ] = $blog_id;
5570
- }
5571
-
5572
- if ( empty( $install_id_2_blog_id ) ) {
5573
- return true;
5574
- }
5575
-
5576
- $params[] = array( 'is_disconnected' => true );
5577
-
5578
- // Send update to FS.
5579
- $result = $this->get_current_or_network_user_api_scope()->call( "/plugins/{$this->_module_id}/installs.json", 'put', $params );
5580
-
5581
- if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
5582
- $this->_logger->api_error( $result );
5583
-
5584
- return $result;
5585
- }
5586
-
5587
- foreach ( $result->installs as $r_install ) {
5588
- $blog_id = $install_id_2_blog_id[ $r_install->id ];
5589
- $install = $installs_map[ $blog_id ];
5590
- $install->is_disconnected = $r_install->is_disconnected;
5591
- $this->_store_site( true, $blog_id, $install );
5592
- }
5593
-
5594
- $this->clear_sync_cron( $opt_out_all );
5595
-
5596
- // Successfully disconnected.
5597
- return true;
5598
- }
5599
-
5600
- /**
5601
- * Opt-out from usage tracking.
5602
- *
5603
- * Note: This will not delete the account information but will stop all tracking.
5604
- *
5605
- * Returns:
5606
- * 1. FALSE - If the user never opted-in.
5607
- * 2. TRUE - If successfully opted-out.
5608
- * 3. object - API result on failure.
5609
- *
5610
- * @author Leo Fajardo (@leorw)
5611
- * @since 1.2.1.5
5612
- *
5613
- * @param bool $is_network_action
5614
- *
5615
- * @return bool|object
5616
- */
5617
- function stop_tracking( $is_network_action = false ) {
5618
- $this->_logger->entrance();
5619
-
5620
- return $is_network_action ?
5621
- $this->stop_network_tracking() :
5622
- $this->stop_site_tracking();
5623
- }
5624
-
5625
- /**
5626
- * Opt-in back into usage tracking.
5627
- *
5628
- * Note: This will only work if the user opted-in previously.
5629
- *
5630
- * Returns:
5631
- * 1. FALSE - If the user never opted-in.
5632
- * 2. TRUE - If successfully opted-in back to usage tracking.
5633
- * 3. object - API result on failure.
5634
- *
5635
- * @author Leo Fajardo (@leorw)
5636
- * @since 1.2.1.5
5637
- *
5638
- * @return bool|object
5639
- */
5640
- function allow_site_tracking() {
5641
- $this->_logger->entrance();
5642
-
5643
- if ( ! $this->is_registered() ) {
5644
- // User never opted-in.
5645
- return false;
5646
- }
5647
-
5648
- if ( $this->is_tracking_allowed() ) {
5649
- // Tracking already allowed.
5650
- return true;
5651
- }
5652
-
5653
- $result = $this->api_site_call( '/?is_disconnected', 'put', array(
5654
- 'is_disconnected' => false
5655
- ) );
5656
-
5657
- if ( ! $this->is_api_result_entity( $result ) ||
5658
- ! isset( $result->is_disconnected ) ||
5659
- $result->is_disconnected
5660
- ) {
5661
- $this->_logger->api_error( $result );
5662
-
5663
- return $result;
5664
- }
5665
-
5666
- $this->_site->is_disconnected = $result->is_disconnected;
5667
- $this->_store_site();
5668
-
5669
- $this->schedule_sync_cron();
5670
-
5671
- // Successfully reconnected.
5672
- return true;
5673
- }
5674
-
5675
- /**
5676
- * Opt-in network back into usage tracking.
5677
- *
5678
- * Note: This will only work if the user opted-in previously.
5679
- *
5680
- * Returns:
5681
- * 1. FALSE - If the user never opted-in.
5682
- * 2. TRUE - If successfully opted-in back to usage tracking.
5683
- * 3. object - API result on failure.
5684
- *
5685
- * @author Leo Fajardo (@leorw)
5686
- * @since 1.2.1.5
5687
- *
5688
- * @return bool|object
5689
- */
5690
- function allow_network_tracking() {
5691
- $this->_logger->entrance();
5692
-
5693
- if ( ! $this->is_registered() ) {
5694
- // User never opted-in.
5695
- return false;
5696
- }
5697
-
5698
- $install_id_2_blog_id = array();
5699
- $installs_map = $this->get_blog_install_map();
5700
-
5701
- $params = array();
5702
- foreach ( $installs_map as $blog_id => $install ) {
5703
- if ( $install->is_tracking_allowed() ) {
5704
- continue;
5705
- }
5706
-
5707
- $params[] = array( 'id' => $install->id );
5708
-
5709
- $install_id_2_blog_id[ $install->id ] = $blog_id;
5710
- }
5711
-
5712
- if ( empty( $install_id_2_blog_id ) ) {
5713
- return true;
5714
- }
5715
-
5716
- $params[] = array( 'is_disconnected' => false );
5717
-
5718
- // Send update to FS.
5719
- $result = $this->get_current_or_network_user_api_scope()->call( "/plugins/{$this->_module_id}/installs.json", 'put', $params );
5720
-
5721
-
5722
- if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
5723
- $this->_logger->api_error( $result );
5724
-
5725
- return $result;
5726
- }
5727
-
5728
- foreach ( $result->installs as $r_install ) {
5729
- $blog_id = $install_id_2_blog_id[ $r_install->id ];
5730
- $install = $installs_map[ $blog_id ];
5731
- $install->is_disconnected = $r_install->is_disconnected;
5732
- $this->_store_site( true, $blog_id, $install );
5733
- }
5734
-
5735
- $this->schedule_sync_cron();
5736
-
5737
- // Successfully reconnected.
5738
- return true;
5739
- }
5740
-
5741
- /**
5742
- * Opt-in back into usage tracking.
5743
- *
5744
- * Note: This will only work if the user opted-in previously.
5745
- *
5746
- * Returns:
5747
- * 1. FALSE - If the user never opted-in.
5748
- * 2. TRUE - If successfully opted-in back to usage tracking.
5749
- * 3. object - API result on failure.
5750
- *
5751
- * @author Leo Fajardo (@leorw)
5752
- * @since 1.2.1.5
5753
- *
5754
- * @param bool $is_network_action
5755
- *
5756
- * @return bool|object
5757
- */
5758
- function allow_tracking( $is_network_action = false ) {
5759
- $this->_logger->entrance();
5760
-
5761
- return $is_network_action ?
5762
- $this->allow_network_tracking() :
5763
- $this->allow_site_tracking();
5764
- }
5765
-
5766
- /**
5767
- * If user opted-in and later disabled usage-tracking,
5768
- * re-allow tracking for licensing and updates.
5769
- *
5770
- * @author Leo Fajardo (@leorw)
5771
- * @since 1.2.1.5
5772
- *
5773
- * @param bool $is_context_single_site
5774
- */
5775
- private function reconnect_locally( $is_context_single_site = false ) {
5776
- $this->_logger->entrance();
5777
-
5778
- if ( ! $this->is_registered() ) {
5779
- return;
5780
- }
5781
-
5782
- if ( ! fs_is_network_admin() || $is_context_single_site ) {
5783
- if ( $this->is_tracking_prohibited() ) {
5784
- $this->_site->is_disconnected = false;
5785
- $this->_store_site();
5786
- }
5787
- } else {
5788
- $installs_map = $this->get_blog_install_map();
5789
- foreach ( $installs_map as $blog_id => $install ) {
5790
- /**
5791
- * @var FS_Site $install
5792
- */
5793
- if ( $install->is_tracking_prohibited() ) {
5794
- $install->is_disconnected = false;
5795
- $this->_store_site( true, $blog_id, $install );
5796
- }
5797
- }
5798
- }
5799
- }
5800
-
5801
- /**
5802
- * @author Vova Feldman (@svovaf)
5803
- * @since 2.3.2
5804
- *
5805
- * @return bool
5806
- */
5807
- function is_extensions_tracking_allowed() {
5808
- return ( true === $this->apply_filters(
5809
- 'is_extensions_tracking_allowed',
5810
- $this->_storage->get( 'is_extensions_tracking_allowed', null )
5811
- ) );
5812
- }
5813
-
5814
- /**
5815
- * @author Vova Feldman (@svovaf)
5816
- * @since 2.3.2
5817
- */
5818
- function _update_tracking_permission_callback() {
5819
- $this->_logger->entrance();
5820
-
5821
- $this->check_ajax_referer( 'update_tracking_permission' );
5822
-
5823
- $is_enabled = fs_request_get_bool( 'is_enabled', null );
5824
-
5825
- if ( ! is_bool( $is_enabled ) ) {
5826
- self::shoot_ajax_failure();
5827
- }
5828
-
5829
- $permission = fs_request_get( 'permission' );
5830
-
5831
- switch ( $permission ) {
5832
- case 'extensions':
5833
- $this->update_extensions_tracking_flag( $is_enabled );
5834
- break;
5835
- default:
5836
- $permission = 'no_match';
5837
- }
5838
-
5839
- if ( 'no_match' === $permission ) {
5840
- self::shoot_ajax_failure();
5841
- }
5842
-
5843
- self::shoot_ajax_success( array(
5844
- 'permissions' => array(
5845
- $permission => $is_enabled,
5846
- )
5847
- ) );
5848
- }
5849
-
5850
- /**
5851
- * @author Leo Fajardo (@leorw)
5852
- * @since 2.3.2
5853
- *
5854
- * @param bool|null $is_enabled
5855
- */
5856
- function update_extensions_tracking_flag( $is_enabled ) {
5857
- if ( is_bool( $is_enabled ) ) {
5858
- $this->_storage->store( 'is_extensions_tracking_allowed', $is_enabled );
5859
- }
5860
- }
5861
-
5862
- /**
5863
- * Parse plugin's settings (as defined by the plugin dev).
5864
- *
5865
- * @author Vova Feldman (@svovaf)
5866
- * @since 1.1.7.3
5867
- *
5868
- * @param array $plugin_info
5869
- *
5870
- * @throws \Freemius_Exception
5871
- */
5872
- private function parse_settings( &$plugin_info ) {
5873
- $this->_logger->entrance();
5874
-
5875
- $id = $this->get_numeric_option( $plugin_info, 'id', false );
5876
- $public_key = $this->get_option( $plugin_info, 'public_key', false );
5877
- $secret_key = $this->get_option( $plugin_info, 'secret_key', null );
5878
- $parent_id = $this->get_numeric_option( $plugin_info, 'parent_id', null );
5879
- $parent_name = $this->get_option( $plugin_info, 'parent_name', null );
5880
-
5881
- /**
5882
- * @author Vova Feldman (@svovaf)
5883
- * @since 1.1.9 Try to pull secret key from external config.
5884
- */
5885
- if ( is_null( $secret_key ) && defined( "WP_FS__{$this->_slug}_SECRET_KEY" ) ) {
5886
- $secret_key = constant( "WP_FS__{$this->_slug}_SECRET_KEY" );
5887
- }
5888
-
5889
- if ( isset( $plugin_info['parent'] ) ) {
5890
- $parent_id = $this->get_numeric_option( $plugin_info['parent'], 'id', null );
5891
- // $parent_slug = $this->get_option( $plugin_info['parent'], 'slug', null );
5892
- // $parent_public_key = $this->get_option( $plugin_info['parent'], 'public_key', null );
5893
- // $parent_name = $this->get_option( $plugin_info['parent'], 'name', null );
5894
- }
5895
-
5896
- if ( false === $id ) {
5897
- throw new Freemius_Exception( array(
5898
- 'error' => array(
5899
- 'type' => 'ParameterNotSet',
5900
- 'message' => 'Plugin id parameter is not set.',
5901
- 'code' => 'plugin_id_not_set',
5902
- 'http' => 500,
5903
- )
5904
- ) );
5905
- }
5906
- if ( false === $public_key ) {
5907
- throw new Freemius_Exception( array(
5908
- 'error' => array(
5909
- 'type' => 'ParameterNotSet',
5910
- 'message' => 'Plugin public_key parameter is not set.',
5911
- 'code' => 'plugin_public_key_not_set',
5912
- 'http' => 500,
5913
- )
5914
- ) );
5915
- }
5916
-
5917
- $plugin = ( $this->_plugin instanceof FS_Plugin ) ?
5918
- $this->_plugin :
5919
- new FS_Plugin();
5920
-
5921
- $premium_suffix = $this->get_option( $plugin_info, 'premium_suffix', '(Premium)' );
5922
-
5923
- $plugin->update( array(
5924
- 'id' => $id,
5925
- 'type' => $this->get_option( $plugin_info, 'type', $this->_module_type ),
5926
- 'public_key' => $public_key,
5927
- 'slug' => $this->_slug,
5928
- 'premium_slug' => $this->get_option( $plugin_info, 'premium_slug', "{$this->_slug}-premium" ),
5929
- 'parent_plugin_id' => $parent_id,
5930
- 'version' => $this->get_plugin_version(),
5931
- 'title' => $this->get_plugin_name( $premium_suffix ),
5932
- 'file' => $this->_plugin_basename,
5933
- 'is_premium' => $this->get_bool_option( $plugin_info, 'is_premium', true ),
5934
- 'premium_suffix' => $premium_suffix,
5935
- 'is_live' => $this->get_bool_option( $plugin_info, 'is_live', true ),
5936
- 'affiliate_moderation' => $this->get_option( $plugin_info, 'has_affiliation' ),
5937
- 'bundle_id' => $this->get_option( $plugin_info, 'bundle_id', null ),
5938
- 'bundle_public_key' => $this->get_option( $plugin_info, 'bundle_public_key', null ),
5939
- ) );
5940
-
5941
- if ( $plugin->is_updated() ) {
5942
- // Update plugin details.
5943
- $this->_plugin = FS_Plugin_Manager::instance( $this->_module_id )->store( $plugin );
5944
- }
5945
- // Set the secret key after storing the plugin, we don't want to store the key in the storage.
5946
- $this->_plugin->secret_key = $secret_key;
5947
-
5948
- /**
5949
- * If the product is network integrated and activated and the current view is in the network level Admin dashboard, if the product's network-level menu located differently from the sub-site level, then use the network menu details (when set).
5950
- *
5951
- * @author Vova Feldman
5952
- * @since 2.4.5
5953
- */
5954
- if ( $this->is_network_active() && fs_is_network_admin() ) {
5955
- if ( isset( $plugin_info['menu_network'] ) &&
5956
- is_array( $plugin_info['menu_network'] ) &&
5957
- ! empty( $plugin_info['menu_network'] )
5958
- ) {
5959
- $plugin_info['menu'] = $plugin_info['menu_network'];
5960
- }
5961
- }
5962
-
5963
- if ( ! isset( $plugin_info['menu'] ) ) {
5964
- $plugin_info['menu'] = array();
5965
-
5966
- if ( ! empty( $this->_storage->sdk_last_version ) &&
5967
- version_compare( $this->_storage->sdk_last_version, '1.1.2', '<=' )
5968
- ) {
5969
- // Backward compatibility to 1.1.2
5970
- $plugin_info['menu']['slug'] = isset( $plugin_info['menu_slug'] ) ?
5971
- $plugin_info['menu_slug'] :
5972
- $this->_slug;
5973
- }
5974
- }
5975
-
5976
- $this->_menu = FS_Admin_Menu_Manager::instance(
5977
- $this->_module_id,
5978
- $this->_module_type,
5979
- $this->get_unique_affix()
5980
- );
5981
-
5982
- $this->_menu->init( $plugin_info['menu'], $this->is_addon() );
5983
-
5984
- $this->_has_addons = $this->get_bool_option( $plugin_info, 'has_addons', false );
5985
- $this->_has_paid_plans = $this->get_bool_option( $plugin_info, 'has_paid_plans', true );
5986
- $this->_has_premium_version = $this->get_bool_option( $plugin_info, 'has_premium_version', $this->_has_paid_plans );
5987
- $this->_ignore_pending_mode = $this->get_bool_option( $plugin_info, 'ignore_pending_mode', false );
5988
- $this->_is_org_compliant = $this->get_bool_option( $plugin_info, 'is_org_compliant', true );
5989
- $this->_is_premium_only = $this->get_bool_option( $plugin_info, 'is_premium_only', false );
5990
- if ( $this->_is_premium_only ) {
5991
- // If premium only plugin, disable anonymous mode.
5992
- $this->_enable_anonymous = false;
5993
- $this->_anonymous_mode = false;
5994
- } else {
5995
- $this->_enable_anonymous = $this->get_bool_option( $plugin_info, 'enable_anonymous', true );
5996
- $this->_anonymous_mode = $this->get_bool_option( $plugin_info, 'anonymous_mode', false );
5997
- }
5998
- $this->_permissions = $this->get_option( $plugin_info, 'permissions', array() );
5999
- $this->_is_bundle_license_auto_activation_enabled = $this->get_option( $plugin_info, 'bundle_license_auto_activation', false );
6000
-
6001
- if ( ! empty( $plugin_info['trial'] ) ) {
6002
- $this->_trial_days = $this->get_numeric_option(
6003
- $plugin_info['trial'],
6004
- 'days',
6005
- // Default to 0 - trial without days specification.
6006
- 0
6007
- );
6008
-
6009
- $this->_is_trial_require_payment = $this->get_bool_option( $plugin_info['trial'], 'is_require_payment', false );
6010
- }
6011
-
6012
- $this->_navigation = $this->get_option(
6013
- $plugin_info,
6014
- 'navigation',
6015
- $this->is_free_wp_org_theme() ?
6016
- self::NAVIGATION_TABS :
6017
- self::NAVIGATION_MENU
6018
- );
6019
- }
6020
-
6021
- /**
6022
- * @param string[] $options
6023
- * @param string $key
6024
- * @param mixed $default
6025
- *
6026
- * @return bool
6027
- */
6028
- private function get_option( &$options, $key, $default = false ) {
6029
- return ! empty( $options[ $key ] ) ? $options[ $key ] : $default;
6030
- }
6031
-
6032
- private function get_bool_option( &$options, $key, $default = false ) {
6033
- return isset( $options[ $key ] ) && is_bool( $options[ $key ] ) ? $options[ $key ] : $default;
6034
- }
6035
-
6036
- private function get_numeric_option( &$options, $key, $default = false ) {
6037
- return isset( $options[ $key ] ) && is_numeric( $options[ $key ] ) ? $options[ $key ] : $default;
6038
- }
6039
-
6040
- /**
6041
- * Gate keeper.
6042
- *
6043
- * @author Vova Feldman (@svovaf)
6044
- * @since 1.1.7.3
6045
- *
6046
- * @return bool
6047
- */
6048
- private function should_stop_execution() {
6049
- if ( empty( $this->_storage->was_plugin_loaded ) ) {
6050
- /**
6051
- * Don't execute Freemius until plugin was fully loaded at least once,
6052
- * to give the opportunity for the activation hook to run before pinging
6053
- * the API for connectivity test. This logic is relevant for the
6054
- * identification of new plugin install vs. plugin update.
6055
- *
6056
- * @author Vova Feldman (@svovaf)
6057
- * @since 1.1.9
6058
- */
6059
- return true;
6060
- }
6061
-
6062
- if ( $this->is_activation_mode() ) {
6063
- if ( ! is_admin() ) {
6064
- /**
6065
- * If in activation mode, don't execute Freemius outside of the
6066
- * admin dashboard.
6067
- *
6068
- * @author Vova Feldman (@svovaf)
6069
- * @since 1.1.7.3
6070
- */
6071
- return true;
6072
- }
6073
-
6074
- if ( ! WP_FS__IS_HTTP_REQUEST ) {
6075
- /**
6076
- * If in activation and executed without HTTP context (e.g. CLI, Cronjob),
6077
- * then don't start Freemius.
6078
- *
6079
- * @author Vova Feldman (@svovaf)
6080
- * @since 1.1.6.3
6081
- *
6082
- * @link https://wordpress.org/support/topic/errors-in-the-freemius-class-when-running-in-wordpress-in-cli
6083
- */
6084
- return true;
6085
- }
6086
-
6087
- if ( self::is_cron() ) {
6088
- /**
6089
- * If in activation mode, don't execute Freemius during wp crons
6090
- * (wp crons have HTTP context - called as HTTP request).
6091
- *
6092
- * @author Vova Feldman (@svovaf)
6093
- * @since 1.1.7.3
6094
- */
6095
- return true;
6096
- }
6097
-
6098
- if ( self::is_ajax() &&
6099
- ! $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) &&
6100
- ! $this->_admin_notices->has_sticky( 'failed_connect_api' )
6101
- ) {
6102
- /**
6103
- * During activation, if running in AJAX mode, unless there's a sticky
6104
- * connectivity issue notice, don't run Freemius.
6105
- *
6106
- * @author Vova Feldman (@svovaf)
6107
- * @since 1.1.7.3
6108
- */
6109
- return true;
6110
- }
6111
- }
6112
-
6113
- return false;
6114
- }
6115
-
6116
- /**
6117
- * Triggered after code type has changed.
6118
- *
6119
- * @author Vova Feldman (@svovaf)
6120
- * @since 1.1.9.1
6121
- */
6122
- function _after_code_type_change() {
6123
- $this->_logger->entrance();
6124
-
6125
- if ( $this->is_theme() ) {
6126
- // Expire the cache of the previous tabs since the theme may
6127
- // have setting updates after code type has changed.
6128
- $this->_cache->expire( 'tabs' );
6129
- $this->_cache->expire( 'tabs_stylesheets' );
6130
- }
6131
-
6132
- if ( $this->is_registered() ) {
6133
- if ( ! $this->is_addon() ) {
6134
- add_action(
6135
- is_admin() ? 'admin_init' : 'init',
6136
- array( &$this, '_plugin_code_type_changed' )
6137
- );
6138
- }
6139
-
6140
- if ( $this->is_premium() ) {
6141
- // Purge cached payments after switching to the premium version.
6142
- // @todo This logic doesn't handle purging the cache for serviceware module upgrade.
6143
- $this->get_api_user_scope()->purge_cache( "/plugins/{$this->_module_id}/payments.json?include_addons=true" );
6144
- }
6145
- }
6146
- }
6147
-
6148
- /**
6149
- * Handles plugin's code type change (free <--> premium).
6150
- *
6151
- * @author Vova Feldman (@svovaf)
6152
- * @since 1.0.9
6153
- */
6154
- function _plugin_code_type_changed() {
6155
- $this->_logger->entrance();
6156
-
6157
- if ( $this->is_premium() ) {
6158
- $this->reconnect_locally();
6159
-
6160
- // Activated premium code.
6161
- $this->do_action( 'after_premium_version_activation' );
6162
-
6163
- // Remove all sticky messages related to download of the premium version.
6164
- $this->_admin_notices->remove_sticky( array(
6165
- 'trial_started',
6166
- 'plan_upgraded',
6167
- 'plan_changed',
6168
- 'license_activated',
6169
- ) );
6170
-
6171
- $notice = '';
6172
- if ( ! $this->is_only_premium() ) {
6173
- $notice = sprintf( $this->get_text_inline( 'Premium %s version was successfully activated.', 'premium-activated-message' ), $this->_module_type );
6174
- }
6175
-
6176
- $license_notice = $this->get_license_network_activation_notice();
6177
- if ( ! empty( $license_notice ) ) {
6178
- $notice .= ' ' . $license_notice;
6179
- }
6180
-
6181
- if ( ! empty( $notice ) ) {
6182
- $this->_admin_notices->add_sticky(
6183
- trim( $notice ),
6184
- 'premium_activated',
6185
- $this->get_text_x_inline( 'W00t',
6186
- 'Used to express elation, enthusiasm, or triumph (especially in electronic communication).', 'woot' ) . '!'
6187
- );
6188
- }
6189
- } else {
6190
- // Remove sticky message related to premium code activation.
6191
- $this->_admin_notices->remove_sticky( 'premium_activated' );
6192
-
6193
- // Activated free code (after had the premium before).
6194
- $this->do_action( 'after_free_version_reactivation' );
6195
-
6196
- if ( $this->is_paying() && ! $this->is_premium() ) {
6197
- $this->_admin_notices->add_sticky(
6198
- sprintf(
6199
- /* translators: %s: License type (e.g. you have a professional license) */
6200
- $this->get_text_inline( 'You have a %s license.', 'you-have-x-license' ),
6201
- $this->get_plan_title()
6202
- ) . $this->get_complete_upgrade_instructions(),
6203
- 'plan_upgraded',
6204
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
6205
- );
6206
- }
6207
- }
6208
-
6209
- // Schedule code type changes event.
6210
- $this->schedule_install_sync();
6211
-
6212
- /**
6213
- * Unregister the uninstall hook for the other version of the plugin (with different code type) to avoid
6214
- * triggering a fatal error when uninstalling that plugin. For example, after deactivating the "free" version
6215
- * of a specific plugin, its uninstall hook should be unregistered after the "premium" version has been
6216
- * activated. If we don't do that, a fatal error will occur when we try to uninstall the "free" version since
6217
- * the main file of the "free" version will be loaded first before calling the hooked callback. Since the
6218
- * free and premium versions are almost identical (same class or have same functions), a fatal error like
6219
- * "Cannot redeclare class MyClass" or "Cannot redeclare my_function()" will occur.
6220
- */
6221
- $this->unregister_uninstall_hook();
6222
-
6223
- $this->clear_module_main_file_cache();
6224
-
6225
- // Update is_premium of latest version.
6226
- $this->_storage->prev_is_premium = $this->_plugin->is_premium;
6227
- }
6228
-
6229
- #endregion
6230
-
6231
- #----------------------------------------------------------------------------------
6232
- #region Add-ons
6233
- #----------------------------------------------------------------------------------
6234
-
6235
- /**
6236
- * Check if add-on installed and activated on site.
6237
- *
6238
- * @author Vova Feldman (@svovaf)
6239
- * @since 1.0.6
6240
- *
6241
- * @param string|number $id_or_slug
6242
- * @param bool|null $is_premium Since 1.2.1.7 can check for specified add-on version.
6243
- *
6244
- * @return bool
6245
- */
6246
- function is_addon_activated( $id_or_slug, $is_premium = null ) {
6247
- $this->_logger->entrance();
6248
-
6249
- $addon_id = self::get_module_id( $id_or_slug );
6250
- $is_activated = self::has_instance( $addon_id );
6251
-
6252
- if ( ! $is_activated ) {
6253
- return false;
6254
- }
6255
-
6256
- if ( is_bool( $is_premium ) ) {
6257
- // Check if the specified code version is activate.
6258
- $addon = $this->get_addon_instance( $addon_id );
6259
- $is_activated = ( $is_premium === $addon->is_premium() );
6260
- }
6261
-
6262
- return $is_activated;
6263
- }
6264
-
6265
- /**
6266
- * Check if add-on was connected to install
6267
- *
6268
- * @author Vova Feldman (@svovaf)
6269
- * @since 1.1.7
6270
- *
6271
- * @param string|number $id_or_slug
6272
- *
6273
- * @return bool
6274
- */
6275
- function is_addon_connected( $id_or_slug ) {
6276
- $this->_logger->entrance();
6277
-
6278
- $sites = self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN );
6279
-
6280
- $addon_id = self::get_module_id( $id_or_slug );
6281
- $addon = $this->get_addon( $addon_id );
6282
- $slug = $addon->slug;
6283
- if ( ! isset( $sites[ $slug ] ) ) {
6284
- return false;
6285
- }
6286
-
6287
- $site = $sites[ $slug ];
6288
-
6289
- $plugin = FS_Plugin_Manager::instance( $addon_id )->get();
6290
-
6291
- if ( $plugin->parent_plugin_id != $this->_plugin->id ) {
6292
- // The given slug do NOT belong to any of the plugin's add-ons.
6293
- return false;
6294
- }
6295
-
6296
- return ( is_object( $site ) &&
6297
- is_numeric( $site->id ) &&
6298
- is_numeric( $site->user_id ) &&
6299
- FS_Plugin_Plan::is_valid_id( $site->plan_id )
6300
- );
6301
- }
6302
-
6303
- /**
6304
- * Determines if add-on installed.
6305
- *
6306
- * NOTE: This is a heuristic and only works if the folder/file named as the slug.
6307
- *
6308
- * @author Vova Feldman (@svovaf)
6309
- * @since 1.0.6
6310
- *
6311
- * @param string|number $id_or_slug
6312
- *
6313
- * @return bool
6314
- */
6315
- function is_addon_installed( $id_or_slug ) {
6316
- $this->_logger->entrance();
6317
-
6318
- $addon_id = self::get_module_id( $id_or_slug );
6319
-
6320
- return file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $this->get_addon_basename( $addon_id ) ) );
6321
- }
6322
-
6323
- /**
6324
- * Get add-on basename.
6325
- *
6326
- * @author Vova Feldman (@svovaf)
6327
- * @since 1.0.6
6328
- *
6329
- * @param string|number $id_or_slug
6330
- *
6331
- * @return string
6332
- */
6333
- function get_addon_basename( $id_or_slug ) {
6334
- $addon_id = self::get_module_id( $id_or_slug );
6335
-
6336
- if ( $this->is_addon_activated( $addon_id ) ) {
6337
- return self::instance( $addon_id )->get_plugin_basename();
6338
- }
6339
-
6340
- $addon = $this->get_addon( $addon_id );
6341
- $premium_basename = "{$addon->premium_slug}/{$addon->slug}.php";
6342
-
6343
- if ( file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $premium_basename ) ) ) {
6344
- return $premium_basename;
6345
- }
6346
-
6347
- $all_plugins = $this->get_all_plugins();
6348
-
6349
- foreach ( $all_plugins as $basename => $data ) {
6350
- if ( $addon->slug === $data['slug'] ||
6351
- $addon->premium_slug === $data['slug']
6352
- ) {
6353
- return $basename;
6354
- }
6355
- }
6356
-
6357
- $free_basename = "{$addon->slug}/{$addon->slug}.php";
6358
-
6359
- return $free_basename;
6360
- }
6361
-
6362
- /**
6363
- * Get installed add-ons instances.
6364
- *
6365
- * @author Vova Feldman (@svovaf)
6366
- * @since 1.0.6
6367
- *
6368
- * @return Freemius[]
6369
- */
6370
- function get_installed_addons() {
6371
- if ( $this->is_addon() ) {
6372
- // Add-on cannot have add-ons.
6373
- return array();
6374
- }
6375
-
6376
- $installed_addons = array();
6377
-
6378
- foreach ( self::$_instances as $instance ) {
6379
- if ( $instance->is_addon_of( $this->_plugin->id ) ) {
6380
- $installed_addons[] = $instance;
6381
- }
6382
- }
6383
-
6384
- return $installed_addons;
6385
- }
6386
-
6387
- /**
6388
- * Check if any add-ons of the plugin are installed.
6389
- *
6390
- * @author Leo Fajardo (@leorw)
6391
- * @since 1.1.1
6392
- *
6393
- * @return bool
6394
- */
6395
- function has_installed_addons() {
6396
- if ( ! $this->has_addons() ) {
6397
- return false;
6398
- }
6399
-
6400
- foreach ( self::$_instances as $instance ) {
6401
- if ( $instance->is_addon() && is_object( $instance->_parent_plugin ) ) {
6402
- if ( $this->_plugin->id == $instance->_parent_plugin->id ) {
6403
- return true;
6404
- }
6405
- }
6406
- }
6407
-
6408
- return false;
6409
- }
6410
-
6411
- /**
6412
- * Tell Freemius that the current plugin is an add-on.
6413
- *
6414
- * @author Vova Feldman (@svovaf)
6415
- * @since 1.0.6
6416
- *
6417
- * @param number $parent_plugin_id The parent plugin ID
6418
- */
6419
- function init_addon( $parent_plugin_id ) {
6420
- $this->_plugin->parent_plugin_id = $parent_plugin_id;
6421
- }
6422
-
6423
- /**
6424
- * @author Vova Feldman (@svovaf)
6425
- * @since 1.0.6
6426
- *
6427
- * @return bool
6428
- */
6429
- function is_addon() {
6430
- return (
6431
- isset( $this->_plugin->parent_plugin_id ) &&
6432
- is_numeric( $this->_plugin->parent_plugin_id )
6433
- );
6434
- }
6435
-
6436
- /**
6437
- * @author Vova Feldman (@svovaf)
6438
- * @since 2.3.2
6439
- *
6440
- * @param number $parent_product_id
6441
- *
6442
- * @return bool
6443
- */
6444
- function is_addon_of( $parent_product_id ) {
6445
- return (
6446
- $this->is_addon() &&
6447
- $parent_product_id == $this->_plugin->parent_plugin_id
6448
- );
6449
- }
6450
-
6451
- /**
6452
- * Deactivate add-on if it's premium only and the user does't have a valid license.
6453
- *
6454
- * @param bool $is_after_trial_cancel
6455
- *
6456
- * @return bool If add-on was deactivated.
6457
- */
6458
- private function deactivate_premium_only_addon_without_license( $is_after_trial_cancel = false ) {
6459
- if ( ! $this->has_free_plan() &&
6460
- ! $this->has_features_enabled_license() &&
6461
- ! $this->_has_premium_license()
6462
- ) {
6463
- if ( $this->is_registered() ) {
6464
- // IF wrapper is turned off because activation_timestamp is currently only stored for plugins (not addons).
6465
- // if (empty($this->_storage->activation_timestamp) ||
6466
- // (WP_FS__SCRIPT_START_TIME - $this->_storage->activation_timestamp) > 30
6467
- // ) {
6468
- /**
6469
- * @todo When it's first fail, there's no reason to try and re-sync because the licenses were just synced after initial activation.
6470
- *
6471
- * Retry syncing the user add-on licenses.
6472
- */
6473
- // Sync licenses.
6474
- $this->_sync_licenses();
6475
- // }
6476
-
6477
- // Try to activate premium license.
6478
- $this->_activate_license( true );
6479
- }
6480
-
6481
- if ( ! $this->has_free_plan() &&
6482
- ! $this->has_features_enabled_license() &&
6483
- ! $this->_has_premium_license()
6484
- ) {
6485
- // @todo Check if deactivate plugins also call the deactivation hook.
6486
-
6487
- $this->_parent->_admin_notices->add_sticky(
6488
- sprintf(
6489
- ( $is_after_trial_cancel ?
6490
- $this->_parent->get_text_inline(
6491
- '%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.',
6492
- 'addon-trial-cancelled-message'
6493
- ) :
6494
- $this->_parent->get_text_inline(
6495
- '%s is a premium only add-on. You have to purchase a license first before activating the plugin.',
6496
- 'addon-no-license-message'
6497
- )
6498
- ),
6499
- '<b>' . $this->_plugin->title . '</b>'
6500
- ) . ' ' . sprintf(
6501
- '<a href="%s" aria-label="%s" class="button button-primary" style="margin-left: 10px; vertical-align: middle;">%s &nbsp;&#10140;</a>',
6502
- $this->_parent->addon_url( $this->_slug ),
6503
- esc_attr( sprintf( $this->_parent->get_text_inline( 'More information about %s', 'more-information-about-x' ), $this->_plugin->title ) ),
6504
- $this->_parent->get_text_inline( 'Purchase License', 'purchase-license' )
6505
- ),
6506
- 'no_addon_license_' . $this->_slug,
6507
- ( $is_after_trial_cancel ? '' : $this->_parent->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...' ),
6508
- ( $is_after_trial_cancel ? 'success' : 'error' )
6509
- );
6510
-
6511
- deactivate_plugins( array( $this->_plugin_basename ), true );
6512
-
6513
- return true;
6514
- }
6515
- }
6516
-
6517
- return false;
6518
- }
6519
-
6520
- #endregion
6521
-
6522
- #----------------------------------------------------------------------------------
6523
- #region Sandbox
6524
- #----------------------------------------------------------------------------------
6525
-
6526
- /**
6527
- * Set Freemius into sandbox mode for debugging.
6528
- *
6529
- * @author Vova Feldman (@svovaf)
6530
- * @since 1.0.4
6531
- *
6532
- * @param string $secret_key
6533
- */
6534
- function init_sandbox( $secret_key ) {
6535
- $this->_plugin->secret_key = $secret_key;
6536
-
6537
- // Update plugin details.
6538
- FS_Plugin_Manager::instance( $this->_module_id )->update( $this->_plugin, true );
6539
- }
6540
-
6541
- /**
6542
- * Check if running payments in sandbox mode.
6543
- *
6544
- * @author Vova Feldman (@svovaf)
6545
- * @since 1.0.4
6546
- *
6547
- * @return bool
6548
- */
6549
- function is_payments_sandbox() {
6550
- return ( ! $this->is_live() ) || isset( $this->_plugin->secret_key );
6551
- }
6552
-
6553
- #endregion
6554
-
6555
- /**
6556
- * Check if running test vs. live plugin.
6557
- *
6558
- * @author Vova Feldman (@svovaf)
6559
- * @since 1.0.5
6560
- *
6561
- * @return bool
6562
- */
6563
- function is_live() {
6564
- return $this->_plugin->is_live;
6565
- }
6566
-
6567
- /**
6568
- * Check if super-admin skipped connection for all sites in the network.
6569
- *
6570
- * @author Vova Feldman (@svovaf)
6571
- * @since 2.0.0
6572
- */
6573
- function is_network_anonymous() {
6574
- if ( ! $this->_is_network_active ) {
6575
- return false;
6576
- }
6577
-
6578
- $is_anonymous_ms = $this->_storage->get( 'is_anonymous_ms' );
6579
-
6580
- if ( empty( $is_anonymous_ms ) ) {
6581
- return false;
6582
- }
6583
-
6584
- return $is_anonymous_ms['is'];
6585
- }
6586
-
6587
- /**
6588
- * Check if super-admin opted-in for all sites in the network.
6589
- *
6590
- * @author Vova Feldman (@svovaf)
6591
- * @since 2.0.0
6592
- */
6593
- function is_network_connected() {
6594
- if ( ! $this->_is_network_active ) {
6595
- return false;
6596
- }
6597
-
6598
- return $this->_storage->get( 'is_network_connected' );
6599
- }
6600
-
6601
- /**
6602
- * Check if the user skipped connecting the account with Freemius.
6603
- *
6604
- * @author Vova Feldman (@svovaf)
6605
- * @since 1.0.7
6606
- *
6607
- * @return bool
6608
- */
6609
- function is_anonymous() {
6610
- if ( ! isset( $this->_is_anonymous ) ) {
6611
- if ( $this->is_network_anonymous() ) {
6612
- $this->_is_anonymous = true;
6613
- } else if ( ! fs_is_network_admin() ) {
6614
- if ( ! isset( $this->_storage->is_anonymous ) ) {
6615
- // Not skipped.
6616
- $this->_is_anonymous = false;
6617
- } else if ( is_bool( $this->_storage->is_anonymous ) ) {
6618
- // For back compatibility, since the variable was boolean before.
6619
- $this->_is_anonymous = $this->_storage->is_anonymous;
6620
-
6621
- // Upgrade stored data format to 1.1.3 format.
6622
- $this->set_anonymous_mode( $this->_storage->is_anonymous );
6623
- } else {
6624
- // Version 1.1.3 and later.
6625
- $this->_is_anonymous = $this->_storage->is_anonymous['is'];
6626
- }
6627
- }
6628
- }
6629
-
6630
- return $this->_is_anonymous;
6631
- }
6632
-
6633
- /**
6634
- * Check if the user skipped the connection of a specified site.
6635
- *
6636
- * @author Vova Feldman (@svovaf)
6637
- * @since 2.0.0
6638
- *
6639
- * @param int $blog_id
6640
- *
6641
- * @return bool
6642
- */
6643
- function is_anonymous_site( $blog_id = 0 ) {
6644
- if ( $this->is_network_anonymous() ) {
6645
- return true;
6646
- }
6647
-
6648
- $is_anonymous = $this->_storage->get( 'is_anonymous', false, $blog_id );
6649
-
6650
- if ( empty( $is_anonymous ) ) {
6651
- return false;
6652
- }
6653
-
6654
- return $is_anonymous['is'];
6655
- }
6656
-
6657
- /**
6658
- * Check if user connected his account and install pending email activation.
6659
- *
6660
- * @author Vova Feldman (@svovaf)
6661
- * @since 1.0.7
6662
- *
6663
- * @return bool
6664
- */
6665
- function is_pending_activation() {
6666
- return $this->_storage->get( 'is_pending_activation', false );
6667
- }
6668
-
6669
- /**
6670
- * Check if plugin must be WordPress.org compliant.
6671
- *
6672
- * @since 1.0.7
6673
- *
6674
- * @return bool
6675
- */
6676
- function is_org_repo_compliant() {
6677
- return $this->_is_org_compliant;
6678
- }
6679
-
6680
- #--------------------------------------------------------------------------------
6681
- #region WP Cron Common
6682
- #--------------------------------------------------------------------------------
6683
-
6684
- /**
6685
- * @author Vova Feldman (@svovaf)
6686
- * @since 2.0.0
6687
- *
6688
- * @param string $name Cron name.
6689
- *
6690
- * @return object
6691
- */
6692
- private function get_cron_data( $name ) {
6693
- $this->_logger->entrance( $name );
6694
-
6695
- /**
6696
- * @var object $cron_data
6697
- */
6698
- return $this->_storage->get( "{$name}_cron", null );
6699
- }
6700
-
6701
- /**
6702
- * @author Vova Feldman (@svovaf)
6703
- * @since 2.0.0
6704
- *
6705
- * @param string $name Cron name.
6706
- */
6707
- private function clear_cron_data( $name ) {
6708
- $this->_logger->entrance( $name );
6709
-
6710
- $this->_storage->remove( "{$name}_cron" );
6711
- }
6712
-
6713
- /**
6714
- * @author Vova Feldman (@svovaf)
6715
- * @since 2.0.0
6716
- *
6717
- * @param string $name Cron name.
6718
- * @param int $cron_blog_id The cron executing blog ID.
6719
- */
6720
- private function set_cron_data( $name, $cron_blog_id = 0 ) {
6721
- $this->_logger->entrance( $name );
6722
-
6723
- $this->_storage->store( "{$name}_cron", (object) array(
6724
- 'version' => $this->get_plugin_version(),
6725
- 'blog_id' => $cron_blog_id,
6726
- 'sdk_version' => $this->version,
6727
- 'timestamp' => WP_FS__SCRIPT_START_TIME,
6728
- 'on' => true,
6729
- ) );
6730
- }
6731
-
6732
- /**
6733
- * Get the cron's executing blog ID.
6734
- *
6735
- * @author Vova Feldman (@svovaf)
6736
- * @since 2.0.0
6737
- *
6738
- * @param string $name Cron name.
6739
- *
6740
- * @return int
6741
- */
6742
- private function get_cron_blog_id( $name ) {
6743
- $this->_logger->entrance( $name );
6744
-
6745
- /**
6746
- * @var object $cron_data
6747
- */
6748
- $cron_data = $this->get_cron_data( $name );
6749
-
6750
- return ( is_object( $cron_data ) && is_numeric( $cron_data->blog_id ) ) ?
6751
- $cron_data->blog_id :
6752
- 0;
6753
- }
6754
-
6755
- /**
6756
- * @author Vova Feldman (@svovaf)
6757
- * @since 2.0.0
6758
- *
6759
- * @param string $name Cron name.
6760
- *
6761
- * @return bool
6762
- */
6763
- private function is_cron_on( $name ) {
6764
- $this->_logger->entrance( $name );
6765
-
6766
- /**
6767
- * @var object $cron_data
6768
- */
6769
- $cron_data = $this->get_cron_data( $name );
6770
-
6771
- return ( ! is_null( $cron_data ) && true === $cron_data->on );
6772
- }
6773
-
6774
- /**
6775
- * Unix timestamp for previous cron execution or false if never executed.
6776
- *
6777
- * @author Vova Feldman (@svovaf)
6778
- * @since 2.0.0
6779
- *
6780
- * @param string $name Cron name.
6781
- *
6782
- * @return int|false
6783
- */
6784
- private function cron_last_execution( $name ) {
6785
- $this->_logger->entrance( $name );
6786
-
6787
- return $this->_storage->get( "{$name}_timestamp" );
6788
- }
6789
-
6790
- /**
6791
- * Set cron execution time to now.
6792
- *
6793
- * @author Vova Feldman (@svovaf)
6794
- * @since 2.0.0
6795
- *
6796
- * @param string $name Cron name.
6797
- */
6798
- private function set_cron_execution_timestamp( $name ) {
6799
- $this->_logger->entrance( $name );
6800
-
6801
- $this->_storage->store( "{$name}_timestamp", time() );
6802
- }
6803
-
6804
- /**
6805
- * Sets the keepalive time to now.
6806
- *
6807
- * @author Leo Fajardo (@leorw)
6808
- * @since 2.2.3
6809
- *
6810
- * @param bool|null $use_network_level_storage
6811
- */
6812
- private function set_keepalive_timestamp( $use_network_level_storage = null ) {
6813
- $this->_logger->entrance();
6814
-
6815
- $this->_storage->store( 'keepalive_timestamp', time(), $use_network_level_storage );
6816
- }
6817
-
6818
- /**
6819
- * Check if cron was executed in the last $period of seconds.
6820
- *
6821
- * @author Vova Feldman (@svovaf)
6822
- * @since 2.0.0
6823
- *
6824
- * @param string $name Cron name.
6825
- * @param int $period In seconds
6826
- *
6827
- * @return bool
6828
- */
6829
- private function is_cron_executed( $name, $period = WP_FS__TIME_24_HOURS_IN_SEC ) {
6830
- $this->_logger->entrance( $name );
6831
-
6832
- $last_execution = $this->cron_last_execution( $name );
6833
-
6834
- if ( ! is_numeric( $last_execution ) ) {
6835
- return false;
6836
- }
6837
-
6838
- return ( $last_execution > ( WP_FS__SCRIPT_START_TIME - $period ) );
6839
- }
6840
-
6841
- /**
6842
- * WP Cron is executed on a site level. When running in a multisite network environment
6843
- * with the network integration activated, for optimization reasons, we are consolidating
6844
- * the installs data sync cron to be executed only from a single site.
6845
- *
6846
- * @author Vova Feldman (@svovaf)
6847
- * @since 2.0.0
6848
- *
6849
- * @param int $except_blog_id Target any except the excluded blog ID.
6850
- *
6851
- * @return int
6852
- */
6853
- private function get_cron_target_blog_id( $except_blog_id = 0 ) {
6854
- if ( ! is_multisite() ) {
6855
- return 0;
6856
- }
6857
-
6858
- if ( $this->_is_network_active &&
6859
- is_numeric( $this->_storage->network_install_blog_id ) &&
6860
- $except_blog_id != $this->_storage->network_install_blog_id &&
6861
- self::is_site_active( $this->_storage->network_install_blog_id )
6862
- ) {
6863
- // Try to run cron from the main network blog.
6864
- $install = $this->get_install_by_blog_id( $this->_storage->network_install_blog_id );
6865
-
6866
- if ( is_object( $install ) &&
6867
- ( $this->is_premium() || $install->is_tracking_allowed() )
6868
- ) {
6869
- return $this->_storage->network_install_blog_id;
6870
- }
6871
- }
6872
-
6873
- // Get first opted-in blog ID with active tracking.
6874
- $installs = $this->get_blog_install_map();
6875
- foreach ( $installs as $blog_id => $install ) {
6876
- if ( $except_blog_id != $blog_id &&
6877
- self::is_site_active( $blog_id ) &&
6878
- ( $this->is_premium() || $install->is_tracking_allowed() )
6879
- ) {
6880
- return $blog_id;
6881
- }
6882
- }
6883
-
6884
- return 0;
6885
- }
6886
-
6887
- /**
6888
- * @author Vova Feldman (@svovaf)
6889
- * @since 2.0.0
6890
- *
6891
- * @param string $name Cron name.
6892
- * @param string $action_tag Callback action tag.
6893
- * @param bool $is_network_clear If set to TRUE, clear sync cron even if there are installs that are still connected.
6894
- */
6895
- private function clear_cron( $name, $action_tag = '', $is_network_clear = false ) {
6896
- $this->_logger->entrance( $name );
6897
-
6898
- if ( ! $this->is_cron_on( $name ) ) {
6899
- return;
6900
- }
6901
-
6902
- $clear_cron = true;
6903
- if ( ! $is_network_clear && $this->_is_network_active ) {
6904
- $installs = $this->get_blog_install_map();
6905
-
6906
- foreach ( $installs as $blog_id => $install ) {
6907
- /**
6908
- * @var FS_Site $install
6909
- */
6910
- if ( $install->is_tracking_allowed() ) {
6911
- $clear_cron = false;
6912
- break;
6913
- }
6914
- }
6915
- }
6916
-
6917
- if ( ! $clear_cron ) {
6918
- return;
6919
- }
6920
-
6921
- /**
6922
- * @var object $cron_data
6923
- */
6924
- $cron_data = $this->get_cron_data( $name );
6925
-
6926
- $cron_blog_id = is_object( $cron_data ) && isset( $cron_data->blog_id ) ?
6927
- $cron_data->blog_id :
6928
- 0;
6929
-
6930
- $this->clear_cron_data( $name );
6931
-
6932
- if ( 0 < $cron_blog_id ) {
6933
- switch_to_blog( $cron_blog_id );
6934
- }
6935
-
6936
- if ( empty( $action_tag ) ) {
6937
- $action_tag = $name;
6938
- }
6939
-
6940
- wp_clear_scheduled_hook( $this->get_action_tag( $action_tag ) );
6941
-
6942
- if ( 0 < $cron_blog_id ) {
6943
- restore_current_blog();
6944
- }
6945
- }
6946
-
6947
- /**
6948
- * Unix timestamp for next cron execution or false if not scheduled.
6949
- *
6950
- * @author Vova Feldman (@svovaf)
6951
- * @since 2.0.0
6952
- *
6953
- * @param string $name Cron name.
6954
- * @param string $action_tag Callback action tag.
6955
- *
6956
- * @return int|false
6957
- */
6958
- private function get_next_scheduled_cron( $name, $action_tag = '' ) {
6959
- $this->_logger->entrance( $name );
6960
-
6961
- if ( ! $this->is_cron_on( $name ) ) {
6962
- return false;
6963
- }
6964
-
6965
- /**
6966
- * @var object $cron_data
6967
- */
6968
- $cron_data = $this->get_cron_data( $name );
6969
-
6970
- $cron_blog_id = is_object( $cron_data ) && isset( $cron_data->blog_id ) ?
6971
- $cron_data->blog_id :
6972
- 0;
6973
-
6974
- if ( 0 < $cron_blog_id ) {
6975
- switch_to_blog( $cron_blog_id );
6976
- }
6977
-
6978
- if ( empty( $action_tag ) ) {
6979
- $action_tag = $name;
6980
- }
6981
-
6982
- $next_scheduled = wp_next_scheduled( $this->get_action_tag( $action_tag ) );
6983
-
6984
- if ( 0 < $cron_blog_id ) {
6985
- restore_current_blog();
6986
- }
6987
-
6988
- return $next_scheduled;
6989
- }
6990
-
6991
- /**
6992
- * @author Vova Feldman (@svovaf)
6993
- * @since 2.0.0
6994
- *
6995
- * @param string $name Cron name.
6996
- * @param string $action_tag Callback action tag.
6997
- * @param string $recurrence 'single' or 'daily'.
6998
- * @param int $start_at Defaults to now.
6999
- * @param bool $randomize_start If true, schedule first job randomly during the next 12 hours. Otherwise, schedule job to start right away.
7000
- * @param int $except_blog_id Target any except the excluded blog ID.
7001
- */
7002
- private function schedule_cron(
7003
- $name,
7004
- $action_tag = '',
7005
- $recurrence = 'single',
7006
- $start_at = WP_FS__SCRIPT_START_TIME,
7007
- $randomize_start = true,
7008
- $except_blog_id = 0
7009
- ) {
7010
- $this->_logger->entrance( $name );
7011
-
7012
- $this->clear_cron( $name, $action_tag, true );
7013
-
7014
- $cron_blog_id = $this->get_cron_target_blog_id( $except_blog_id );
7015
-
7016
- if ( is_multisite() && 0 == $cron_blog_id ) {
7017
- // Don't schedule cron since couldn't find a target blog.
7018
- return;
7019
- }
7020
-
7021
- if ( 0 < $cron_blog_id ) {
7022
- switch_to_blog( $cron_blog_id );
7023
- }
7024
-
7025
- if ( 'daily' === $recurrence ) {
7026
- if ( $randomize_start ) {
7027
- // Schedule first sync with a random 12 hour time range from now.
7028
- $start_at += rand( 0, ( WP_FS__TIME_24_HOURS_IN_SEC / 2 ) );
7029
- }
7030
-
7031
- // Schedule daily WP cron.
7032
- wp_schedule_event(
7033
- $start_at,
7034
- 'daily',
7035
- $this->get_action_tag( $action_tag )
7036
- );
7037
- } else if ( 'single' === $recurrence ) {
7038
- // Schedule single cron.
7039
- wp_schedule_single_event(
7040
- $start_at,
7041
- $this->get_action_tag( $action_tag )
7042
- );
7043
- }
7044
-
7045
- $this->set_cron_data( $name, $cron_blog_id );
7046
-
7047
- if ( 0 < $cron_blog_id ) {
7048
- restore_current_blog();
7049
- }
7050
- }
7051
-
7052
- /**
7053
- * Consolidated cron execution for performance optimization. The max number of API requests is based on the number of unique opted-in users.
7054
- * that doesn't halt page loading.
7055
- *
7056
- * @author Vova Feldman (@svovaf)
7057
- * @since 2.0.0
7058
- *
7059
- * @param string $name Cron name.
7060
- * @param callable $callable The function that should be executed.
7061
- */
7062
- private function execute_cron( $name, $callable ) {
7063
- $this->_logger->entrance( $name );
7064
-
7065
- // Store the last time data sync was executed.
7066
- $this->set_cron_execution_timestamp( $name );
7067
-
7068
- // Check if API is temporary down.
7069
- if ( FS_Api::is_temporary_down() ) {
7070
- return;
7071
- }
7072
-
7073
- // @todo Add logic that identifies API latency, and reschedule the next background sync randomly between 8-16 hours.
7074
-
7075
- $users_2_blog_ids = array();
7076
-
7077
- if ( ! is_multisite() ) {
7078
- // Add dummy blog.
7079
- $users_2_blog_ids[0] = array( 0 );
7080
- } else {
7081
- $installs = $this->get_blog_install_map();
7082
- foreach ( $installs as $blog_id => $install ) {
7083
- if ( $this->is_premium() || $install->is_tracking_allowed() ) {
7084
- if ( ! isset( $users_2_blog_ids[ $install->user_id ] ) ) {
7085
- $users_2_blog_ids[ $install->user_id ] = array();
7086
- }
7087
-
7088
- $users_2_blog_ids[ $install->user_id ][] = $blog_id;
7089
- }
7090
- }
7091
- }
7092
-
7093
- $current_blog_id = get_current_blog_id();
7094
-
7095
- foreach ( $users_2_blog_ids as $user_id => $blog_ids ) {
7096
- if ( 0 < $blog_ids[0] ) {
7097
- $this->switch_to_blog( $blog_ids[0] );
7098
- }
7099
-
7100
- call_user_func_array( $callable, array( $blog_ids, ( is_multisite() ? $current_blog_id : null ) ) );
7101
-
7102
- foreach ( $blog_ids as $blog_id ) {
7103
- $this->do_action( "after_{$name}_cron", $blog_id );
7104
- }
7105
- }
7106
-
7107
- if ( is_multisite() ) {
7108
- $this->switch_to_blog( $current_blog_id, fs_is_network_admin() ? $this->get_network_install() : null );
7109
-
7110
- $this->do_action( "after_{$name}_cron_multisite" );
7111
- }
7112
- }
7113
-
7114
- #endregion
7115
-
7116
- #----------------------------------------------------------------------------------
7117
- #region Daily Sync Cron
7118
- #----------------------------------------------------------------------------------
7119
-
7120
-
7121
- /**
7122
- * @author Vova Feldman (@svovaf)
7123
- * @since 2.0.0
7124
- *
7125
- * @return bool
7126
- */
7127
- private function is_sync_cron_scheduled() {
7128
- return $this->is_cron_on( 'sync' );
7129
- }
7130
-
7131
- /**
7132
- * Get the sync cron's executing blog ID.
7133
- *
7134
- * @author Vova Feldman (@svovaf)
7135
- * @since 2.0.0
7136
- *
7137
- * @return int
7138
- */
7139
- private function get_sync_cron_blog_id() {
7140
- return $this->get_cron_blog_id( 'sync' );
7141
- }
7142
-
7143
- /**
7144
- * @author Vova Feldman (@svovaf)
7145
- * @since 1.1.7.3
7146
- */
7147
- private function run_manual_sync() {
7148
- self::require_pluggable_essentials();
7149
-
7150
- if ( ! $this->is_user_admin() ) {
7151
- return;
7152
- }
7153
-
7154
- // Run manual sync.
7155
- $this->_sync_cron();
7156
-
7157
- // Reschedule next cron to run 24 hours from now (performance optimization).
7158
- $this->schedule_sync_cron( time() + WP_FS__TIME_24_HOURS_IN_SEC, false );
7159
- }
7160
-
7161
- /**
7162
- * Data sync cron job. Replaces the background sync non blocking HTTP request
7163
- * that doesn't halt page loading.
7164
- *
7165
- * @author Vova Feldman (@svovaf)
7166
- * @since 1.1.7.3
7167
- * @since 2.0.0 Consolidate all the data sync into the same cron for performance optimization. The max number of API requests is based on the number of unique opted-in users.
7168
- */
7169
- function _sync_cron() {
7170
- $this->_logger->entrance();
7171
-
7172
- $this->execute_cron( 'sync', array( &$this, '_sync_cron_method' ) );
7173
- }
7174
-
7175
- /**
7176
- * The actual data sync cron logic.
7177
- *
7178
- * @author Vova Feldman (@svovaf)
7179
- * @since 2.0.0
7180
- *
7181
- * @param int[] $blog_ids
7182
- * @param int|null $current_blog_id @since 2.2.3. This is passed from the `execute_cron` method and used by the
7183
- * `_sync_plugin_license` method in order to switch to the previous blog when sending
7184
- * updates for a single site in case `execute_cron` has switched to a different blog.
7185
- */
7186
- function _sync_cron_method( array $blog_ids, $current_blog_id = null ) {
7187
- if ( $this->is_registered() ) {
7188
- if ( $this->has_paid_plan() ) {
7189
- // Initiate background plan sync.
7190
- $this->_sync_license( true, false, $current_blog_id );
7191
-
7192
- if ( $this->is_paying() ) {
7193
- // Check for premium plugin updates.
7194
- $this->check_updates( true );
7195
- }
7196
- } else {
7197
- // Sync install(s) (only if something changed locally).
7198
- if ( 1 < count( $blog_ids ) ) {
7199
- $this->sync_installs();
7200
- } else {
7201
- $this->sync_install();
7202
- }
7203
-
7204
- $this->maybe_sync_install_user();
7205
- }
7206
- }
7207
- }
7208
-
7209
- /**
7210
- * Check if sync was executed in the last $period of seconds.
7211
- *
7212
- * @author Vova Feldman (@svovaf)
7213
- * @since 1.1.7.3
7214
- *
7215
- * @param int $period In seconds
7216
- *
7217
- * @return bool
7218
- */
7219
- private function is_sync_executed( $period = WP_FS__TIME_24_HOURS_IN_SEC ) {
7220
- return $this->is_cron_executed( 'sync', $period );
7221
- }
7222
-
7223
- /**
7224
- * @author Vova Feldman (@svovaf)
7225
- * @since 1.1.7.3
7226
- *
7227
- * @return bool
7228
- */
7229
- private function is_sync_cron_on() {
7230
- return $this->is_cron_on( 'sync' );
7231
- }
7232
-
7233
- /**
7234
- * @author Leo Fajardo (@leorw)
7235
- * @since 2.5.0
7236
- */
7237
- private function maybe_schedule_sync_cron() {
7238
- $next_schedule = $this->next_sync_cron();
7239
-
7240
- // The event is properly scheduled, so no need to reschedule it.
7241
- if (
7242
- is_numeric( $next_schedule ) &&
7243
- $next_schedule > time()
7244
- ) {
7245
- return;
7246
- }
7247
-
7248
- $this->schedule_sync_cron();
7249
- }
7250
-
7251
- /**
7252
- * @author Vova Feldman (@svovaf)
7253
- * @since 1.1.7.3
7254
- *
7255
- * @param int $start_at Defaults to now.
7256
- * @param bool $randomize_start If true, schedule first job randomly during the next 12 hours. Otherwise, schedule job to start right away.
7257
- * @param int $except_blog_id Since 2.0.0 when running in a multisite network environment, the cron execution is consolidated. This param allows excluding excluded specified blog ID from being the cron executor.
7258
- */
7259
- private function schedule_sync_cron(
7260
- $start_at = WP_FS__SCRIPT_START_TIME,
7261
- $randomize_start = true,
7262
- $except_blog_id = 0
7263
- ) {
7264
- $this->schedule_cron(
7265
- 'sync',
7266
- 'data_sync',
7267
- 'daily',
7268
- $start_at,
7269
- $randomize_start,
7270
- $except_blog_id
7271
- );
7272
- }
7273
-
7274
- /**
7275
- * Add the actual sync function to the cron job hook.
7276
- *
7277
- * @author Vova Feldman (@svovaf)
7278
- * @since 1.1.7.3
7279
- */
7280
- private function hook_callback_to_sync_cron() {
7281
- $this->add_action( 'data_sync', array( &$this, '_sync_cron' ) );
7282
- }
7283
-
7284
- /**
7285
- * @author Vova Feldman (@svovaf)
7286
- * @since 1.1.7.3
7287
- *
7288
- * @param bool $is_network_clear Since 2.0.0 If set to TRUE, clear sync cron even if there are installs that are still connected.
7289
- */
7290
- private function clear_sync_cron( $is_network_clear = false ) {
7291
- $this->_logger->entrance();
7292
-
7293
- $this->clear_cron( 'sync', 'data_sync', $is_network_clear );
7294
- }
7295
-
7296
- /**
7297
- * Unix timestamp for next sync cron execution or false if not scheduled.
7298
- *
7299
- * @author Vova Feldman (@svovaf)
7300
- * @since 1.1.7.3
7301
- *
7302
- * @return int|false
7303
- */
7304
- function next_sync_cron() {
7305
- return $this->get_next_scheduled_cron( 'sync', 'data_sync' );
7306
- }
7307
-
7308
- /**
7309
- * Unix timestamp for previous sync cron execution or false if never executed.
7310
- *
7311
- * @author Vova Feldman (@svovaf)
7312
- * @since 1.1.7.3
7313
- *
7314
- * @return int|false
7315
- */
7316
- function last_sync_cron() {
7317
- return $this->cron_last_execution( 'sync' );
7318
- }
7319
-
7320
- #endregion Daily Sync Cron ------------------------------------------------------------------
7321
-
7322
- #----------------------------------------------------------------------------------
7323
- #region Async Install Sync
7324
- #----------------------------------------------------------------------------------
7325
-
7326
- /**
7327
- * @author Vova Feldman (@svovaf)
7328
- * @since 1.1.7.3
7329
- *
7330
- * @return bool
7331
- */
7332
- private function is_install_sync_scheduled() {
7333
- return $this->is_cron_on( 'install_sync' );
7334
- }
7335
-
7336
- /**
7337
- * Get the sync cron's executing blog ID.
7338
- *
7339
- * @author Vova Feldman (@svovaf)
7340
- * @since 2.0.0
7341
- *
7342
- * @return int
7343
- */
7344
- private function get_install_sync_cron_blog_id() {
7345
- return $this->get_cron_blog_id( 'install_sync' );
7346
- }
7347
-
7348
- /**
7349
- * Instead of running blocking install sync event, execute non blocking scheduled wp-cron.
7350
- *
7351
- * @author Vova Feldman (@svovaf)
7352
- * @since 1.1.7.3
7353
- *
7354
- * @param int $except_blog_id Since 2.0.0 when running in a multisite network environment, the cron execution is consolidated. This param allows excluding excluded specified blog ID from being the cron executor.
7355
- */
7356
- private function schedule_install_sync( $except_blog_id = 0 ) {
7357
- $this->schedule_cron( 'install_sync', 'install_sync', 'single', WP_FS__SCRIPT_START_TIME, false, $except_blog_id );
7358
- }
7359
-
7360
- /**
7361
- * Unix timestamp for previous install sync cron execution or false if never executed.
7362
- *
7363
- * @todo There's some very strange bug that $this->_storage->install_sync_timestamp value is not being updated. But for sure the sync event is working.
7364
- *
7365
- * @author Vova Feldman (@svovaf)
7366
- * @since 1.1.7.3
7367
- *
7368
- * @return int|false
7369
- */
7370
- function last_install_sync() {
7371
- return $this->cron_last_execution( 'install_sync' );
7372
- }
7373
-
7374
- /**
7375
- * Unix timestamp for next install sync cron execution or false if not scheduled.
7376
- *
7377
- * @author Vova Feldman (@svovaf)
7378
- * @since 1.1.7.3
7379
- *
7380
- * @return int|false
7381
- */
7382
- function next_install_sync() {
7383
- return $this->get_next_scheduled_cron( 'install_sync', 'install_sync' );
7384
- }
7385
-
7386
- /**
7387
- * Add the actual install sync function to the cron job hook.
7388
- *
7389
- * @author Vova Feldman (@svovaf)
7390
- * @since 1.1.7.3
7391
- */
7392
- private function hook_callback_to_install_sync() {
7393
- $this->add_action( 'install_sync', array( &$this, '_run_sync_install' ) );
7394
- }
7395
-
7396
- /**
7397
- * @author Vova Feldman (@svovaf)
7398
- * @since 1.1.7.3
7399
- *
7400
- * @param bool $is_network_clear Since 2.0.0 If set to TRUE, clear sync cron even if there are installs that are still connected.
7401
- */
7402
- private function clear_install_sync_cron( $is_network_clear = false ) {
7403
- $this->_logger->entrance();
7404
-
7405
- $this->clear_cron( 'install_sync', 'install_sync', $is_network_clear );
7406
- }
7407
-
7408
- /**
7409
- * @author Vova Feldman (@svovaf)
7410
- * @since 1.1.7.3
7411
- * @since 2.0.0 Consolidate all the data sync into the same cron for performance optimization. The max number of API requests is based on the number of unique opted-in users.
7412
- */
7413
- public function _run_sync_install() {
7414
- $this->_logger->entrance();
7415
-
7416
- $this->execute_cron( 'sync', array( &$this, '_sync_install_cron_method' ) );
7417
- }
7418
-
7419
- /**
7420
- * The actual install(s) sync cron logic.
7421
- *
7422
- * @author Vova Feldman (@svovaf)
7423
- * @since 2.0.0
7424
- *
7425
- * @param int[] $blog_ids
7426
- * @param int|null $current_blog_id
7427
- */
7428
- function _sync_install_cron_method( array $blog_ids, $current_blog_id = null ) {
7429
- if ( $this->is_clone() ) {
7430
- return;
7431
- }
7432
-
7433
- if ( $this->is_registered() ) {
7434
- if ( 1 < count( $blog_ids ) ) {
7435
- $this->sync_installs( array(), true );
7436
- } else {
7437
- $this->sync_install( array(), true );
7438
- }
7439
-
7440
- $this->maybe_sync_install_user();
7441
- }
7442
- }
7443
-
7444
- #endregion Async Install Sync ------------------------------------------------------------------
7445
-
7446
- /**
7447
- * Show a notice that activation is currently pending.
7448
- *
7449
- * @todo Add some sort of mechanism to allow users to update the email address they would like to opt-in with when $is_suspicious_email is true.
7450
- *
7451
- * @author Vova Feldman (@svovaf)
7452
- * @since 1.0.7
7453
- *
7454
- * @param bool|string $email
7455
- * @param bool $is_pending_trial Since 1.2.1.5
7456
- * @param bool $is_suspicious_email Since 2.5.0 Set to true when there's an indication that email address the user opted in with is fake/dummy/placeholder.
7457
- */
7458
- function _add_pending_activation_notice(
7459
- $email = false,
7460
- $is_pending_trial = false,
7461
- $is_suspicious_email = false
7462
- ) {
7463
- if ( ! is_string( $email ) ) {
7464
- $current_user = self::_get_current_wp_user();
7465
- $email = $current_user->user_email;
7466
- }
7467
-
7468
- $this->_admin_notices->add_sticky(
7469
- sprintf(
7470
- $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' ),
7471
- '<b>' . $this->get_plugin_name() . '</b>',
7472
- '<b>' . $email . '</b>',
7473
- ( $is_pending_trial ?
7474
- $this->get_text_inline( 'start the trial', 'start-the-trial' ) :
7475
- $this->get_text_inline( 'complete the install', 'complete-the-install' ) )
7476
- ),
7477
- 'activation_pending',
7478
- 'Thanks!'
7479
- );
7480
- }
7481
-
7482
- /**
7483
- * Check if currently in plugin activation.
7484
- *
7485
- * @author Vova Feldman (@svovaf)
7486
- * @since 1.1.4
7487
- *
7488
- * @return bool
7489
- */
7490
- function is_plugin_activation() {
7491
- $result = get_transient( "fs_{$this->_module_type}_{$this->_slug}_activated" );
7492
-
7493
- return !empty($result);
7494
- }
7495
-
7496
- /**
7497
- *
7498
- * NOTE: admin_menu action executed before admin_init.
7499
- *
7500
- * @author Vova Feldman (@svovaf)
7501
- * @since 1.0.7
7502
- */
7503
- function _admin_init_action() {
7504
- $is_migration = $this->is_migration();
7505
-
7506
- /**
7507
- * Automatically redirect to connect/activation page after plugin activation.
7508
- *
7509
- * @since 1.1.7 Do NOT redirect to opt-in when running in network admin mode.
7510
- */
7511
- if ( $this->is_plugin_activation() ) {
7512
- delete_transient( "fs_{$this->_module_type}_{$this->_slug}_activated" );
7513
-
7514
- if ( isset( $_GET['activate-multi'] ) ) {
7515
- /**
7516
- * Don't redirect if activating multiple plugins at once (bulk activation).
7517
- */
7518
- } else if (
7519
- self::is_deactivation_snoozed() &&
7520
- (
7521
- // Either running the free code base.
7522
- ! $this->is_premium() ||
7523
- // Or if has a free version.
7524
- ! $this->is_only_premium() ||
7525
- // If premium only, don't redirect if license is activated.
7526
- ( $this->is_registered() && ! $this->can_use_premium_code() )
7527
- )
7528
- ) {
7529
- /**
7530
- * Don't redirect if activating during the deactivation snooze period (aka troubleshooting), unless activating a paid product version that the admin didn't enter its license key yet.
7531
- */
7532
- } else if ( ! $is_migration ) {
7533
- $this->_redirect_on_activation_hook();
7534
- return;
7535
- }
7536
- }
7537
-
7538
- if ( $is_migration ) {
7539
- return;
7540
- }
7541
-
7542
- if ( fs_request_is_action( $this->get_unique_affix() . '_skip_activation' ) ) {
7543
- check_admin_referer( $this->get_unique_affix() . '_skip_activation' );
7544
-
7545
- $this->skip_connection( null, fs_is_network_admin() );
7546
-
7547
- fs_redirect( $this->get_after_activation_url( 'after_skip_url' ) );
7548
- }
7549
-
7550
- if ( $this->is_network_activation_mode() &&
7551
- fs_request_is_action( $this->get_unique_affix() . '_delegate_activation' )
7552
- ) {
7553
- check_admin_referer( $this->get_unique_affix() . '_delegate_activation' );
7554
-
7555
- $this->delegate_connection();
7556
-
7557
- fs_redirect( $this->get_after_activation_url( 'after_delegation_url' ) );
7558
- }
7559
-
7560
- $this->_add_upgrade_action_link();
7561
-
7562
- if ( ! ( ! $this->_is_network_active && fs_is_network_admin() ) &&
7563
- (
7564
- ( true === $this->_storage->require_license_activation ) ||
7565
- // Not registered nor anonymous.
7566
- ( ! $this->is_registered() && ! $this->is_anonymous() ) ||
7567
- // OR, network level and in network upgrade mode.
7568
- ( fs_is_network_admin() && $this->_is_network_active && $this->is_network_upgrade_mode() )
7569
- )
7570
- ) {
7571
- if ( ! $this->is_pending_activation() ) {
7572
- if ( ! $this->is_activation_page() ) {
7573
- /**
7574
- * If a user visits any other admin page before activating the premium-only theme with a valid
7575
- * license, reactivate the previous theme.
7576
- *
7577
- * @author Leo Fajardo (@leorw)
7578
- * @since 1.2.2
7579
- */
7580
- if ( $this->is_theme() &&
7581
- ! $this->has_settings_menu() &&
7582
- ! isset( $_REQUEST['fs_action'] ) &&
7583
- $this->can_activate_previous_theme()
7584
- ) {
7585
- if ( $this->is_only_premium() ) {
7586
- $this->activate_previous_theme();
7587
- return;
7588
- }
7589
-
7590
- if ( true === $this->_storage->require_license_activation ) {
7591
- $this->_storage->require_license_activation = false;
7592
- }
7593
- }
7594
-
7595
- if ( ! fs_is_network_admin() &&
7596
- $this->is_network_activation_mode() &&
7597
- ! $this->is_delegated_connection()
7598
- ) {
7599
- return;
7600
- }
7601
-
7602
- if ( $this->is_plugin_new_install() || $this->is_only_premium() ) {
7603
- if ( ! $this->_anonymous_mode &&
7604
- ( ! $this->is_addon() || ! $this->_parent->is_anonymous() ) ) {
7605
- // Show notice for new plugin installations.
7606
- $this->_admin_notices->add(
7607
- sprintf(
7608
- $this->get_text_inline( 'You are just one step away - %s', 'you-are-step-away' ),
7609
- sprintf( '<b><a href="%s">%s</a></b>',
7610
- $this->get_activation_url( array(), ! $this->is_delegated_connection() ),
7611
- sprintf( $this->get_text_x_inline( 'Complete "%s" Activation Now',
7612
- '%s - plugin name. As complete "PluginX" activation now', 'activate-x-now' ), $this->get_plugin_name() )
7613
- )
7614
- ),
7615
- '',
7616
- 'update-nag'
7617
- );
7618
- }
7619
- } else {
7620
- if ( $this->should_add_sticky_optin_notice() ) {
7621
- $this->add_sticky_optin_admin_notice();
7622
- }
7623
-
7624
- if ( $this->has_filter( 'optin_pointer_element' ) ) {
7625
- // Don't show admin nag if plugin update.
7626
- wp_enqueue_script( 'wp-pointer' );
7627
- wp_enqueue_style( 'wp-pointer' );
7628
-
7629
- $this->_enqueue_connect_essentials();
7630
-
7631
- add_action( 'admin_print_footer_scripts', array(
7632
- $this,
7633
- '_add_connect_pointer_script'
7634
- ) );
7635
- }
7636
- }
7637
- }
7638
- }
7639
-
7640
- if ( $this->show_opt_in_on_themes_page() &&
7641
- $this->is_activation_page()
7642
- ) {
7643
- $this->_show_theme_activation_optin_dialog();
7644
- }
7645
- }
7646
- }
7647
-
7648
- /**
7649
- * @author Vova Feldman (@svovaf)
7650
- * @since 2.0.0
7651
- *
7652
- * @return bool
7653
- */
7654
- private function should_add_sticky_optin_notice() {
7655
- if ( $this->is_addon() && $this->_parent->is_anonymous() ) {
7656
- return false;
7657
- }
7658
-
7659
- if ( fs_is_network_admin() ) {
7660
- if ( ! $this->_is_network_active ) {
7661
- return false;
7662
- }
7663
-
7664
- if ( ! $this->is_network_activation_mode() ) {
7665
- return false;
7666
- }
7667
-
7668
- return ! isset( $this->_storage->sticky_optin_added_ms );
7669
- }
7670
-
7671
- if ( ! $this->is_activation_mode() ) {
7672
- return false;
7673
- }
7674
-
7675
- // If running from a blog admin and delegated the connection.
7676
- return ! isset( $this->_storage->sticky_optin_added );
7677
- }
7678
-
7679
- /**
7680
- * @author Leo Fajardo (@leorw)
7681
- * @since 2.0.0
7682
- */
7683
- private function add_sticky_optin_admin_notice() {
7684
- if ( ! $this->_is_network_active || ! fs_is_network_admin() ) {
7685
- $this->_storage->sticky_optin_added = true;
7686
- } else {
7687
- $this->_storage->sticky_optin_added_ms = true;
7688
- }
7689
-
7690
- // Show notice for new plugin installations.
7691
- $this->_admin_notices->add_sticky(
7692
- sprintf(
7693
- $this->get_text_inline( 'We made a few tweaks to the %s, %s', 'few-plugin-tweaks' ),
7694
- $this->_module_type,
7695
- sprintf( '<b><a href="%s">%s</a></b>',
7696
- $this->get_activation_url(),
7697
- sprintf( $this->get_text_inline( 'Opt in to make "%s" better!', 'optin-x-now' ), $this->get_plugin_name() )
7698
- )
7699
- ),
7700
- 'connect_account',
7701
- '',
7702
- 'update-nag'
7703
- );
7704
- }
7705
-
7706
- /**
7707
- * Enqueue connect requires scripts and styles.
7708
- *
7709
- * @author Vova Feldman (@svovaf)
7710
- * @since 1.1.4
7711
- */
7712
- function _enqueue_connect_essentials() {
7713
- wp_enqueue_script( 'jquery' );
7714
- wp_enqueue_script( 'json2' );
7715
-
7716
- fs_enqueue_local_script( 'postmessage', 'nojquery.ba-postmessage.min.js' );
7717
- fs_enqueue_local_script( 'fs-postmessage', 'postmessage.js' );
7718
- }
7719
-
7720
- /**
7721
- * Add connect / opt-in pointer.
7722
- *
7723
- * @author Vova Feldman (@svovaf)
7724
- * @since 1.1.4
7725
- */
7726
- function _add_connect_pointer_script() {
7727
- $vars = array( 'id' => $this->_module_id );
7728
- $pointer_content = fs_get_template( 'connect.php', $vars );
7729
- ?>
7730
- <script type="text/javascript">// <![CDATA[
7731
- jQuery(document).ready(function ($) {
7732
- if ('undefined' !== typeof(jQuery().pointer)) {
7733
-
7734
- var element = <?php echo $this->apply_filters( 'optin_pointer_element', '$("#non_existing_element");' ) ?>;
7735
-
7736
- if (element.length > 0) {
7737
- var optin = $(element).pointer($.extend(true, {}, {
7738
- content : <?php echo json_encode( $pointer_content ) ?>,
7739
- position : {
7740
- edge : 'left',
7741
- align: 'center'
7742
- },
7743
- buttons : function () {
7744
- // Don't show pointer buttons.
7745
- return '';
7746
- },
7747
- pointerWidth: 482
7748
- }, <?php echo $this->apply_filters( 'optin_pointer_options_json', '{}' ) ?>));
7749
-
7750
- <?php
7751
- echo $this->apply_filters( 'optin_pointer_execute', "
7752
-
7753
- optin.pointer('open');
7754
-
7755
- // Tag the opt-in pointer with custom class.
7756
- $('.wp-pointer #fs_connect')
7757
- .parents('.wp-pointer.wp-pointer-top')
7758
- .addClass('fs-opt-in-pointer');
7759
-
7760
- ", 'element', 'optin' ) ?>
7761
- }
7762
- }
7763
- });
7764
- // ]]></script>
7765
- <?php
7766
- }
7767
-
7768
- /**
7769
- * Return current page's URL.
7770
- *
7771
- * @author Vova Feldman (@svovaf)
7772
- * @since 1.0.7
7773
- *
7774
- * @return string
7775
- */
7776
- function current_page_url() {
7777
- $url = 'http';
7778
-
7779
- if ( isset( $_SERVER["HTTPS"] ) ) {
7780
- if ( $_SERVER["HTTPS"] == "on" ) {
7781
- $url .= "s";
7782
- }
7783
- }
7784
- $url .= "://";
7785
- if ( $_SERVER["SERVER_PORT"] != "80" ) {
7786
- $url .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
7787
- } else {
7788
- $url .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
7789
- }
7790
-
7791
- return esc_url( $url );
7792
- }
7793
-
7794
- /**
7795
- * Check if the current page is the plugin's main admin settings page.
7796
- *
7797
- * @author Vova Feldman (@svovaf)
7798
- * @since 1.0.7
7799
- *
7800
- * @return bool
7801
- */
7802
- function _is_plugin_page() {
7803
- return fs_is_plugin_page( $this->_menu->get_raw_slug() ) ||
7804
- fs_is_plugin_page( $this->_slug );
7805
- }
7806
-
7807
- /* Events
7808
- ------------------------------------------------------------------------------------------------------------------*/
7809
- /**
7810
- * Delete site install from Database.
7811
- *
7812
- * @author Vova Feldman (@svovaf)
7813
- * @since 1.0.1
7814
- *
7815
- * @param bool $store
7816
- * @param int|null $blog_id Since 2.0.0
7817
- *
7818
- * @return false|int The install ID if deleted. Otherwise, FALSE (when install not exist).
7819
- */
7820
- function _delete_site( $store = true, $blog_id = null ) {
7821
- return self::_delete_site_by_slug( $this->_slug, $this->_module_type, $store, $blog_id );
7822
- }
7823
-
7824
- /**
7825
- * Delete site install from Database.
7826
- *
7827
- * @author Vova Feldman (@svovaf)
7828
- * @since 1.2.2.7
7829
- *
7830
- * @param string $slug
7831
- * @param string $module_type
7832
- * @param bool $store
7833
- * @param int|null $blog_id Since 2.0.0
7834
- *
7835
- * @return false|int The install ID if deleted. Otherwise, FALSE (when install not exist).
7836
- */
7837
- static function _delete_site_by_slug( $slug, $module_type, $store = true, $blog_id = null ) {
7838
- $sites = self::get_all_sites( $module_type, $blog_id );
7839
-
7840
- $install_id = false;
7841
-
7842
- if ( isset( $sites[ $slug ] ) ) {
7843
- if ( is_object( $sites[ $slug ] ) ) {
7844
- $install_id = $sites[ $slug ]->id;
7845
- }
7846
-
7847
- unset( $sites[ $slug ] );
7848
-
7849
- self::set_account_option_by_module( $module_type, 'sites', $sites, $store, $blog_id );
7850
- }
7851
-
7852
- return $install_id;
7853
- }
7854
-
7855
- /**
7856
- * Delete user.
7857
- *
7858
- * @author Vova Feldman (@svovaf)
7859
- * @since 2.0.0
7860
- *
7861
- * @param number $user_id
7862
- * @param bool $store
7863
- *
7864
- * @return false|int The user ID if deleted. Otherwise, FALSE (when install not exist).
7865
- */
7866
- private static function delete_user( $user_id, $store = true ) {
7867
- $users = self::get_all_users();
7868
-
7869
- if ( ! is_array( $users ) || ! isset( $users[ $user_id ] ) ) {
7870
- return false;
7871
- }
7872
-
7873
- unset( $users[ $user_id ] );
7874
-
7875
- self::$_accounts->set_option( 'users', $users, $store );
7876
-
7877
- return $user_id;
7878
- }
7879
-
7880
- /**
7881
- * Delete plugin's plans information.
7882
- *
7883
- * @param bool $store Flush to Database if true.
7884
- * @param bool $keep_associated_plans If set to false, delete all plans, even if a plan is associated with an install.
7885
- *
7886
- * @author Vova Feldman (@svovaf)
7887
- * @since 1.0.9
7888
- */
7889
- private function _delete_plans( $store = true, $keep_associated_plans = true ) {
7890
- $this->_logger->entrance();
7891
-
7892
- $plans = self::get_all_plans( $this->_module_type );
7893
-
7894
- $plans_to_keep = array();
7895
-
7896
- if ( $keep_associated_plans ) {
7897
- $plans_ids_to_keep = $this->get_plans_ids_associated_with_installs();
7898
- foreach ( $plans_ids_to_keep as $plan_id ) {
7899
- $plan = self::_get_plan_by_id( $plan_id );
7900
- if ( is_object( $plan ) ) {
7901
- $plans_to_keep[] = self::_encrypt_entity( $plan );
7902
- }
7903
- }
7904
- }
7905
-
7906
- if ( ! empty( $plans_to_keep ) ) {
7907
- $plans[ $this->_slug ] = $plans_to_keep;
7908
- } else {
7909
- unset( $plans[ $this->_slug ] );
7910
- }
7911
-
7912
- $this->set_account_option( 'plans', $plans, $store );
7913
- }
7914
-
7915
- /**
7916
- * Delete all plugin licenses.
7917
- *
7918
- * @author Vova Feldman (@svovaf)
7919
- * @since 1.0.9
7920
- *
7921
- * @param bool $store
7922
- */
7923
- private function _delete_licenses( $store = true ) {
7924
- $this->_logger->entrance();
7925
-
7926
- $all_licenses = self::get_all_licenses();
7927
-
7928
- unset( $all_licenses[ $this->_module_id ] );
7929
-
7930
- self::$_accounts->set_option( 'all_licenses', $all_licenses, $store );
7931
- }
7932
-
7933
- /**
7934
- * Check if Freemius was added on new plugin installation.
7935
- *
7936
- * @author Vova Feldman (@svovaf)
7937
- * @since 1.1.5
7938
- *
7939
- * @return bool
7940
- */
7941
- function is_plugin_new_install() {
7942
- return isset( $this->_storage->is_plugin_new_install ) &&
7943
- $this->_storage->is_plugin_new_install;
7944
- }
7945
-
7946
- /**
7947
- * Check if it's the first plugin release that is running Freemius.
7948
- *
7949
- * @author Vova Feldman (@svovaf)
7950
- * @since 1.2.1.5
7951
- *
7952
- * @return bool
7953
- */
7954
- function is_first_freemius_powered_version() {
7955
- return empty( $this->_storage->plugin_last_version );
7956
- }
7957
-
7958
- /**
7959
- * @author Leo Fajardo (@leorw)
7960
- * @since 1.2.2
7961
- *
7962
- * @return bool|string
7963
- */
7964
- private function get_previous_theme_slug() {
7965
- return isset( $this->_storage->previous_theme ) ?
7966
- $this->_storage->previous_theme :
7967
- false;
7968
- }
7969
-
7970
- /**
7971
- * @author Leo Fajardo (@leorw)
7972
- * @since 1.2.2
7973
- *
7974
- * @return string
7975
- */
7976
- private function can_activate_previous_theme() {
7977
- $slug = $this->get_previous_theme_slug();
7978
- if ( false !== $slug && current_user_can( 'switch_themes' ) ) {
7979
- $theme_instance = wp_get_theme( $slug );
7980
-
7981
- return $theme_instance->exists();
7982
- }
7983
-
7984
- return false;
7985
- }
7986
-
7987
- /**
7988
- * @author Leo Fajardo (@leorw)
7989
- * @since 1.2.2
7990
- */
7991
- private function activate_previous_theme() {
7992
- switch_theme( $this->get_previous_theme_slug() );
7993
- unset( $this->_storage->previous_theme );
7994
-
7995
- global $pagenow;
7996
- if ( 'themes.php' === $pagenow ) {
7997
- /**
7998
- * Refresh the active theme information.
7999
- *
8000
- * @author Leo Fajardo (@leorw)
8001
- * @since 1.2.2
8002
- */
8003
- fs_redirect( $this->admin_url( $pagenow ) );
8004
- }
8005
- }
8006
-
8007
- /**
8008
- * @author Leo Fajardo (@leorw)
8009
- * @since 1.2.2
8010
- *
8011
- * @return string
8012
- */
8013
- function get_previous_theme_activation_url() {
8014
- if ( ! $this->can_activate_previous_theme() ) {
8015
- return '';
8016
- }
8017
-
8018
- /**
8019
- * Activation URL
8020
- *
8021
- * @author Leo Fajardo (@leorw)
8022
- * @since 1.2.2
8023
- */
8024
- return wp_nonce_url(
8025
- $this->admin_url( 'themes.php?action=activate&stylesheet=' . urlencode( $this->get_previous_theme_slug() ) ),
8026
- 'switch-theme_' . $this->get_previous_theme_slug()
8027
- );
8028
- }
8029
-
8030
- /**
8031
- * Saves the slug of the previous theme if it still exists so that it can be used by the logic in the opt-in
8032
- * form that decides whether to add a close button to the opt-in dialog or not. So after a premium-only theme is
8033
- * activated, the close button will appear and will reactivate the previous theme if clicked. If the previous
8034
- * theme doesn't exist, then there will be no close button.
8035
- *
8036
- * @author Leo Fajardo (@leorw)
8037
- * @since 1.2.2
8038
- *
8039
- * @param string $slug_or_name Old theme's slug or name.
8040
- * @param bool|WP_Theme $old_theme WP_Theme instance of the old theme if it still exists.
8041
- */
8042
- function _activate_theme_event_hook( $slug_or_name, $old_theme = false ) {
8043
- $this->_storage->previous_theme = ( false !== $old_theme ) ?
8044
- $old_theme->get_stylesheet() :
8045
- $slug_or_name;
8046
-
8047
- $this->_activate_plugin_event_hook();
8048
- }
8049
-
8050
- /**
8051
- * Plugin activated hook.
8052
- *
8053
- * @author Vova Feldman (@svovaf)
8054
- * @since 1.0.1
8055
- *
8056
- * @uses FS_Api
8057
- */
8058
- function _activate_plugin_event_hook() {
8059
- $this->_logger->entrance( 'slug = ' . $this->_slug );
8060
-
8061
- if ( ! $this->is_user_admin() ) {
8062
- return;
8063
- }
8064
-
8065
- $this->unregister_uninstall_hook();
8066
-
8067
- // Clear API cache on activation.
8068
- FS_Api::clear_cache();
8069
-
8070
- $is_premium_version_activation = $this->is_plugin() ?
8071
- ( current_filter() !== ( 'activate_' . $this->_free_plugin_basename ) ) :
8072
- $this->is_premium();
8073
-
8074
- $this->_logger->info( 'Activating ' . ( $is_premium_version_activation ? 'premium' : 'free' ) . ' plugin version.' );
8075
-
8076
- if ( $this->is_plugin() ) {
8077
- // This logic is relevant only to plugins since both the free and premium versions of a plugin can be active at the same time.
8078
- // 1. If running in the activation of the FREE module, get the basename of the PREMIUM.
8079
- // 2. If running in the activation of the PREMIUM module, get the basename of the FREE.
8080
- $other_version_basename = $is_premium_version_activation ?
8081
- $this->_free_plugin_basename :
8082
- $this->premium_plugin_basename();
8083
-
8084
- if ( ! $this->_is_network_active ) {
8085
- /**
8086
- * Themes are always network activated, but the ACTUAL activation is per site.
8087
- *
8088
- * During the activation, the plugin isn't yet active, therefore,
8089
- * _is_network_active will be set to false even if it's a network level
8090
- * activation. So we need to fix that by looking at the is_network_admin() value.
8091
- *
8092
- * @author Vova Feldman
8093
- */
8094
- $this->_is_network_active = (
8095
- $this->_is_multisite_integrated &&
8096
- fs_is_network_admin()
8097
- );
8098
- }
8099
-
8100
- /**
8101
- * If the other module version is active, deactivate it.
8102
- *
8103
- * is_plugin_active() checks if the plugin is active on the site or the network level and
8104
- * deactivate_plugins() deactivates the plugin whether it's activated on the site or network level.
8105
- *
8106
- * @author Leo Fajardo (@leorw)
8107
- * @since 1.2.2
8108
- */
8109
- if (
8110
- is_plugin_active( $other_version_basename ) &&
8111
- $this->apply_filters( 'deactivate_on_activation', true )
8112
- ) {
8113
- deactivate_plugins( $other_version_basename );
8114
- }
8115
- }
8116
-
8117
- if ( $this->is_registered() ) {
8118
- if ( $is_premium_version_activation ) {
8119
- $this->reconnect_locally();
8120
- }
8121
-
8122
-
8123
- // Schedule re-activation event and sync.
8124
- // $this->sync_install( array(), true );
8125
- $this->schedule_install_sync();
8126
-
8127
- // If activating the premium module version, add an admin notice to congratulate for an upgrade completion.
8128
- if ( $is_premium_version_activation ) {
8129
- $this->_admin_notices->add(
8130
- sprintf( $this->get_text_inline( 'The upgrade of %s was successfully completed.', 'successful-version-upgrade-message' ), sprintf( '<b>%s</b>', $this->_plugin->title ) ),
8131
- $this->get_text_x_inline( 'W00t',
8132
- 'Used to express elation, enthusiasm, or triumph (especially in electronic communication).', 'woot' ) . '!'
8133
- );
8134
- }
8135
- } else if ( $this->is_anonymous() ) {
8136
- if ( isset( $this->_storage->is_anonymous_ms ) && $this->_storage->is_anonymous_ms['is'] ) {
8137
- $plugin_version = $this->_storage->is_anonymous_ms['version'];
8138
- $network = true;
8139
- } else {
8140
- $plugin_version = isset( $this->_storage->is_anonymous ) ?
8141
- $this->_storage->is_anonymous['version'] :
8142
- null;
8143
- $network = false;
8144
- }
8145
-
8146
- /**
8147
- * Reset "skipped" click cache on the following:
8148
- * 1. Freemius DEV mode.
8149
- * 2. WordPress DEBUG mode.
8150
- * 3. If a plugin and the user skipped the exact same version before.
8151
- *
8152
- * @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).
8153
- *
8154
- * @todo 4. If explicitly asked to retry after every activation.
8155
- */
8156
- if ( WP_FS__DEV_MODE ||
8157
- (
8158
- ( $this->is_plugin() || ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ) &&
8159
- $this->get_plugin_version() == $plugin_version
8160
- )
8161
- ) {
8162
- $this->reset_anonymous_mode( $network );
8163
- }
8164
- }
8165
-
8166
- $is_trial_or_has_features_enabled_license = ( $this->is_trial() || $this->has_features_enabled_license() );
8167
-
8168
- if ( $this->is_addon() && ! $is_trial_or_has_features_enabled_license ) {
8169
- /**
8170
- * When activating an add-on, try to also activate a license.
8171
- *
8172
- * @author Leo Fajardo (@leorw)
8173
- * @since 2.3.0
8174
- */
8175
- if ( ! $this->_is_network_active ) {
8176
- $this->maybe_activate_addon_license();
8177
- } else {
8178
- $this->maybe_network_activate_addon_license();
8179
- }
8180
-
8181
- /**
8182
- * Avoid redirecting to the license activation screen after automatically activating an add-on license.
8183
- *
8184
- * @author Leo Fajardo (@leorw)
8185
- * @since 2.3.0
8186
- */
8187
- $is_trial_or_has_features_enabled_license = ( $this->is_trial() || $this->has_features_enabled_license() );
8188
-
8189
- if ( $is_trial_or_has_features_enabled_license && true === $this->_storage->require_license_activation ) {
8190
- $this->_storage->require_license_activation = false;
8191
- }
8192
- }
8193
-
8194
- if (
8195
- $is_premium_version_activation &&
8196
- (
8197
- ( ! $this->is_registered() && $this->is_anonymous() ) ||
8198
- (
8199
- $this->is_registered() &&
8200
- ! $is_trial_or_has_features_enabled_license
8201
- )
8202
- )
8203
- ) {
8204
- $this->_storage->require_license_activation = true;
8205
- }
8206
-
8207
- if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
8208
- /**
8209
- * If no previous version of plugin's version exist, it means that it's either
8210
- * the first time that the plugin installed on the site, or the plugin was installed
8211
- * before but didn't have Freemius integrated.
8212
- *
8213
- * Since register_activation_hook() do NOT fires on updates since 3.1, and only fires
8214
- * on manual activation via the dashboard, is_plugin_activation() is TRUE
8215
- * only after immediate activation.
8216
- *
8217
- * @since 1.1.4
8218
- * @link https://make.wordpress.org/core/2010/10/27/plugin-activation-hooks-no-longer-fire-for-updates/
8219
- */
8220
- $this->_storage->is_plugin_new_install = empty( $this->_storage->plugin_last_version );
8221
- }
8222
-
8223
- /**
8224
- * Also flush when activating the premium version so that even if Freemius was off before, the API
8225
- * connectivity test can be run again.
8226
- *
8227
- * @author Leo Fajardo (@leorw)
8228
- * @since 2.2.3.1
8229
- */
8230
- $has_api_connectivity = $this->has_api_connectivity( WP_FS__DEV_MODE || $is_premium_version_activation );
8231
-
8232
- if ( ! $this->_anonymous_mode &&
8233
- $has_api_connectivity &&
8234
- ! $this->_isAutoInstall
8235
- ) {
8236
- // Store hint that the plugin was just activated to enable auto-redirection to settings.
8237
- set_transient( "fs_{$this->_module_type}_{$this->_slug}_activated", true, 60 );
8238
- }
8239
-
8240
- /**
8241
- * Activation hook is executed after the plugin's main file is loaded, therefore,
8242
- * after the plugin was loaded. The logic is located at activate_plugin()
8243
- * ./wp-admin/includes/plugin.php.
8244
- *
8245
- * @author Vova Feldman (@svovaf)
8246
- * @since 1.1.9
8247
- */
8248
- $this->_storage->was_plugin_loaded = true;
8249
- }
8250
-
8251
- /**
8252
- * @author Leo Fajardo (@leorw)
8253
- * @since 2.3.0
8254
- */
8255
- private function maybe_activate_addon_license() {
8256
- $parent_fs = $this->get_parent_instance();
8257
-
8258
- if (
8259
- ! is_object( $parent_fs ) ||
8260
- ( ! $parent_fs->is_registered() && ! $parent_fs->is_network_registered() )
8261
- ) {
8262
- // Try to activate a license only if the parent plugin is active and has a valid `install`.
8263
- return;
8264
- }
8265
-
8266
- $license = $this->get_active_parent_license();
8267
- if ( ! is_object( $license ) ) {
8268
- return;
8269
- }
8270
-
8271
- if (
8272
- $this->is_bundle_license_auto_activation_enabled() &&
8273
- ! empty( $license->products )
8274
- ) {
8275
- $this->activate_bundle_license( $license );
8276
-
8277
- return;
8278
- }
8279
-
8280
- if ( ! $this->is_registered() ) {
8281
- // Opt in with a license key.
8282
- $this->opt_in(
8283
- $parent_fs->get_current_or_network_user()->email,
8284
- false,
8285
- false,
8286
- $license->secret_key
8287
- );
8288
- } else {
8289
- // Activate the license.
8290
- $install = $this->api_site_call(
8291
- '/',
8292
- 'put',
8293
- array( 'license_key' => $this->apply_filters( 'license_key', $license->secret_key ) )
8294
- );
8295
-
8296
- if ( ! FS_Api::is_api_error( $install ) ) {
8297
- $this->_sync_addon_license( $this->get_id(), true );
8298
- }
8299
- }
8300
- }
8301
-
8302
- /**
8303
- * @author Leo Fajardo (@leorw)
8304
- * @since 2.3.0
8305
- *
8306
- * @param FS_Plugin_License $license
8307
- */
8308
- private function maybe_network_activate_addon_license( $license = null ) {
8309
- $parent_fs = $this->get_parent_instance();
8310
- if ( ! is_object( $parent_fs ) || ( ! $parent_fs->is_registered() && ! $parent_fs->is_network_registered() ) ) {
8311
- // Try to activate a license only if the parent plugin is active and has a valid `install`.
8312
- return;
8313
- }
8314
-
8315
- $license = ( ! is_null( $license ) ) ?
8316
- $license :
8317
- $this->get_active_parent_license();
8318
-
8319
- if ( ! is_object( $license ) ) {
8320
- return;
8321
- }
8322
-
8323
- if (
8324
- $this->is_bundle_license_auto_activation_enabled() &&
8325
- ! empty( $license->products )
8326
- ) {
8327
- $this->activate_bundle_license( $license );
8328
-
8329
- return;
8330
- }
8331
-
8332
- if ( ! $this->is_network_registered() ) {
8333
- $sites = $this->get_sites_for_network_level_optin();
8334
-
8335
- if ( count( $sites ) > $license->left() ) {
8336
- // If the add-on is network active, try to activate the license only if it can be activated on all sites.
8337
- return;
8338
- }
8339
-
8340
- // Opt in with a license key.
8341
- $this->opt_in(
8342
- $parent_fs->get_user()->email,
8343
- false,
8344
- false,
8345
- $license->secret_key,
8346
- false,
8347
- false,
8348
- false,
8349
- null,
8350
- $sites
8351
- );
8352
- } else {
8353
- $blog_2_install_map = array();
8354
- $site_ids = array();
8355
-
8356
- $all_sites = Freemius::get_sites();
8357
-
8358
- foreach ( $all_sites as $site ) {
8359
- $blog_id = Freemius::get_site_blog_id( $site );
8360
- $install = $this->get_install_by_blog_id( $blog_id );
8361
-
8362
- if ( is_object( $install ) && FS_Plugin_License::is_valid_id( $install->license_id ) ) {
8363
- // Skip license activation for installs that are already associated with a license.
8364
- continue;
8365
- }
8366
-
8367
- if ( is_object( $install ) ) {
8368
- $blog_2_install_map[ $blog_id ] = $install;
8369
- } else {
8370
- $site_ids[] = $blog_id;
8371
- }
8372
- }
8373
-
8374
- if ( ( count( $blog_2_install_map ) + count( $site_ids ) ) > $license->left() ) {
8375
- return;
8376
- }
8377
-
8378
- $user = $this->get_current_or_network_user();
8379
-
8380
- if ( ! empty( $blog_2_install_map ) ) {
8381
- $result = $this->activate_license_on_many_installs( $user, $license->secret_key, $blog_2_install_map );
8382
-
8383
- if ( true !== $result ) {
8384
- return;
8385
- }
8386
- }
8387
-
8388
- if ( ! empty( $site_ids ) ) {
8389
- $this->activate_license_on_many_sites( $user, $license->secret_key, $site_ids );
8390
- }
8391
- }
8392
- }
8393
-
8394
- /**
8395
- * Tries to activate a bundle license for all supported products if the current product is activated with a bundle license. This is called after activating an available license (not via the license activation dialog but by clicking on a license activation button) for a product via its "Account" page.
8396
- *
8397
- * @author Leo Fajardo (@leorw)
8398
- * @since 2.4.0
8399
- *
8400
- * @param FS_Plugin_License $license
8401
- * @param array $sites
8402
- * @param int $blog_id
8403
- */
8404
- private function maybe_activate_bundle_license( FS_Plugin_License $license = null, $sites = array(), $blog_id = 0 ) {
8405
- if ( ! is_object( $license ) && $this->has_active_valid_license() ) {
8406
- $license = $this->_license;
8407
- }
8408
-
8409
- if ( ! is_object( $license ) ) {
8410
- return;
8411
- }
8412
-
8413
- $parent_license = ( ! empty( $license->products ) ) ?
8414
- $license :
8415
- $this->get_active_parent_license( $license->secret_key );
8416
-
8417
- if ( is_object( $parent_license ) ) {
8418
- $this->activate_bundle_license( $parent_license, $sites, $blog_id );
8419
- }
8420
- }
8421
-
8422
- /**
8423
- * Try to activate a bundle license for all the bundle products installed on the site.
8424
- * (1) If a child product install already has a license, the bundle license won't be activated.
8425
- * (2) On multi-site networks, if the attempt to activate the bundle license is triggered from the network admin, the bundle license activation will only work for non-delegated sites and only if none of them is associated with a license. Even if one of the sites has the product installed with a license key, skip the bundle license activation for the product.
8426
- * (3) On multi-site networks, if the attempt to activate the bundle license is triggered from a site-level admin, only activate the license if the product is site-level activated or delegated, and the product installation is not yet associated with a license.
8427
- *
8428
- * @author Leo Fajardo (@leorw)
8429
- * @since 2.4.0
8430
- *
8431
- * @param FS_Plugin_License $license
8432
- * @param array $sites
8433
- * @param int $current_blog_id
8434
- */
8435
- private function activate_bundle_license( $license, $sites = array(), $current_blog_id = 0 ) {
8436
- $is_network_admin = fs_is_network_admin();
8437
-
8438
- $installs_by_blog_map = array();
8439
- $site_info_by_blog_map = array();
8440
-
8441
- /**
8442
- * Try to activate the license for all supported products.
8443
- *
8444
- * @author Leo Fajardo
8445
- */
8446
- foreach ( $license->products as $product_id ) {
8447
- $fs = self::get_instance_by_id( $product_id );
8448
-
8449
- if ( ! is_object( $fs ) ) {
8450
- continue;
8451
- }
8452
-
8453
- if ( ! $fs->has_paid_plan() ) {
8454
- continue;
8455
- }
8456
-
8457
- if (
8458
- ! $fs->is_addon() &&
8459
- ! FS_Plan_Manager::instance()->has_paid_plan( $fs->_plans )
8460
- ) {
8461
- /**
8462
- * The parent product can be free-only but can have its `has_paid_plan` flag set to `true` when
8463
- * there is a context bundle.
8464
- */
8465
- continue;
8466
- }
8467
-
8468
- if ( $current_blog_id > 0 ) {
8469
- $fs->switch_to_blog( $current_blog_id );
8470
- }
8471
-
8472
- if ( $fs->has_active_valid_license() ) {
8473
- continue;
8474
- }
8475
-
8476
- if ( ! $is_network_admin || $current_blog_id > 0 ) {
8477
- if ( $fs->is_network_active() && ! $fs->is_delegated_connection( $current_blog_id ) ) {
8478
- // Do not try to activate the license in the site level if the product is network active and the connection was not delegated.
8479
- continue;
8480
- }
8481
- } else {
8482
- if ( ! $fs->is_network_active() ) {
8483
- // Do not try to activate the license in the network level if the product is not network active.
8484
- continue;
8485
- }
8486
-
8487
- if ( $fs->is_network_delegated_connection() ) {
8488
- // Do not try to activate the license in the network level if the activation has been delegated to site admins.
8489
- continue;
8490
- }
8491
-
8492
- $has_install_with_license = false;
8493
-
8494
- // Collection of sites that have an install entity that is not activated with a license or non-delegated sites that have no install entity, or both types of site.
8495
- $filtered_sites = array();
8496
-
8497
- if ( empty( $sites ) ) {
8498
- $all_sites = self::get_sites();
8499
-
8500
- foreach ( $all_sites as $site ) {
8501
- $sites[] = array( 'blog_id' => self::get_site_blog_id( $site ) );
8502
- }
8503
- } else {
8504
- // Populate the map here to avoid calling `$fs->get_site_info( $site );` in the other `for` loop below.
8505
- foreach ( $sites as $site ) {
8506
- if ( ! isset( $site['blog_id'] ) || ! is_numeric( $site['blog_id'] ) ) {
8507
- continue;
8508
- }
8509
-
8510
- $site_info_by_blog_map[ $site['blog_id'] ] = $site;
8511
- }
8512
- }
8513
-
8514
- foreach ( $sites as $site ) {
8515
- if ( ! isset( $site['blog_id'] ) || ! is_numeric( $site['blog_id'] ) ) {
8516
- continue;
8517
- }
8518
-
8519
- $blog_id = $site['blog_id'];
8520
-
8521
- if ( ! isset( $installs_by_blog_map[ $blog_id ] ) ) {
8522
- $installs_by_blog_map[ $blog_id ] = self::get_all_sites( $fs->get_module_type(), $blog_id );
8523
- }
8524
-
8525
- $installs = $installs_by_blog_map[ $blog_id ];
8526
- $install = null;
8527
-
8528
- if ( isset( $installs[ $fs->get_slug() ] ) ) {
8529
- $install = $installs[ $fs->get_slug() ];
8530
-
8531
- if (
8532
- is_object( $install ) &&
8533
- (
8534
- ! FS_Site::is_valid_id( $install->id ) ||
8535
- ! FS_User::is_valid_id( $install->user_id ) ||
8536
- ! FS_Plugin_Plan::is_valid_id( $install->plan_id )
8537
- )
8538
- ) {
8539
- $install = null;
8540
- }
8541
- }
8542
-
8543
- if (
8544
- is_object( $install ) &&
8545
- FS_Plugin_License::is_valid_id( $install->license_id )
8546
- ) {
8547
- $has_install_with_license = true;
8548
- break;
8549
- }
8550
-
8551
- if ( $fs->is_site_delegated_connection( $blog_id ) ) {
8552
- // Site activation delegated, don't activate bundle license on the site in the network admin.
8553
- continue;
8554
- }
8555
-
8556
- if ( ! isset( $site_info_by_blog_map[ $blog_id ] ) ) {
8557
- $site_info_by_blog_map[ $blog_id ] = $fs->get_site_info( $site );
8558
- }
8559
-
8560
- $filtered_sites[] = $site_info_by_blog_map[ $blog_id ];
8561
- }
8562
-
8563
- if ( $has_install_with_license || empty( $filtered_sites ) ) {
8564
- // Do not try to activate the license at the network level if there's any install with a license or there's no site to activate the license on.
8565
- continue;
8566
- }
8567
-
8568
- $sites = $filtered_sites;
8569
- }
8570
-
8571
- $fs->activate_migrated_license(
8572
- $license->secret_key,
8573
- null,
8574
- null,
8575
- $sites,
8576
- ( $current_blog_id > 0 ? $current_blog_id : null )
8577
- );
8578
- }
8579
- }
8580
-
8581
- /**
8582
- * Returns a parent license that can be activated for the context product.
8583
- *
8584
- * @author Leo Fajardo (@leorw)
8585
- * @since 2.3.0
8586
- *
8587
- * @param string|null $license_key
8588
- * @param bool $flush
8589
- *
8590
- * @return FS_Plugin_License
8591
- */
8592
- function get_active_parent_license( $license_key = null, $flush = true ) {
8593
- $parent_licenses_endpoint = "/plugins/{$this->get_id()}/parent_licenses.json?filter=activatable";
8594
-
8595
- $fs = $this;
8596
-
8597
- if ( $this->is_addon() ) {
8598
- $parent_instance = $this->get_parent_instance();
8599
-
8600
- if ( is_object( $parent_instance ) && $parent_instance->is_registered() ) {
8601
- $fs = $parent_instance;
8602
- }
8603
- }
8604
-
8605
- $foreign_licenses = $fs->get_foreign_licenses_info(
8606
- self::get_all_licenses( $this->get_parent_id() )
8607
- );
8608
-
8609
- if ( ! empty ( $foreign_licenses ) ) {
8610
- $foreign_licenses = array(
8611
- // Prefix with `+` to tell the server to include foreign licenses in the licenses collection.
8612
- 'ids' => ( urlencode( '+' ) . implode( ',', $foreign_licenses['ids'] ) ),
8613
- 'license_keys' => implode( ',', array_map( 'urlencode', $foreign_licenses['license_keys'] ) )
8614
- );
8615
-
8616
- $parent_licenses_endpoint = add_query_arg( $foreign_licenses, $parent_licenses_endpoint );
8617
- }
8618
-
8619
- $result = $fs->get_current_or_network_user_api_scope()->get( $parent_licenses_endpoint, $flush );
8620
-
8621
- if (
8622
- ! $this->is_api_result_object( $result, 'licenses' ) ||
8623
- ! is_array( $result->licenses ) ||
8624
- empty( $result->licenses )
8625
- ) {
8626
- return null;
8627
- }
8628
-
8629
- $parent_license = null;
8630
-
8631
- if ( empty( $license_key ) ) {
8632
- $parent_license = $result->licenses[0];
8633
- } else {
8634
- foreach ( $result->licenses as $license ) {
8635
- if ( $license_key === $license->secret_key ) {
8636
- $parent_license = $license;
8637
- break;
8638
- }
8639
- }
8640
- }
8641
-
8642
- if ( ! is_null( $parent_license ) ) {
8643
- $parent_license = new FS_Plugin_License( $parent_license );
8644
- }
8645
-
8646
- return $parent_license;
8647
- }
8648
-
8649
- /**
8650
- * @author Leo Fajardo (@leorw)
8651
- * @since 2.3.0
8652
- *
8653
- * @return array
8654
- */
8655
- function get_sites_for_network_level_optin() {
8656
- $sites = array();
8657
- $all_sites = self::get_sites();
8658
-
8659
- foreach ( $all_sites as $site ) {
8660
- $blog_id = self::get_site_blog_id( $site );
8661
-
8662
- if ( ! $this->is_site_delegated_connection( $blog_id ) &&
8663
- ! $this->is_installed_on_site( $blog_id )
8664
- ) {
8665
- $sites[] = $this->get_site_info( $site );
8666
- }
8667
- }
8668
-
8669
- return $sites;
8670
- }
8671
-
8672
- /**
8673
- * Delete account.
8674
- *
8675
- * @author Vova Feldman (@svovaf)
8676
- * @since 1.0.3
8677
- *
8678
- * @param bool $check_user Enforce checking if user have plugins activation privileges.
8679
- */
8680
- function delete_account_event( $check_user = true ) {
8681
- $this->_logger->entrance( 'slug = ' . $this->_slug );
8682
-
8683
- if ( $check_user && ! $this->is_user_admin() ) {
8684
- return;
8685
- }
8686
-
8687
- $this->do_action( 'before_account_delete' );
8688
-
8689
- // Clear all admin notices.
8690
- $this->_admin_notices->clear_all_sticky( false );
8691
-
8692
- $this->_delete_site( false );
8693
-
8694
- $delete_network_common_data = true;
8695
-
8696
- if ( $this->_is_network_active ) {
8697
- $installs = $this->get_blog_install_map();
8698
-
8699
- // Don't delete common network data unless no other installs left.
8700
- $delete_network_common_data = empty( $installs );
8701
- }
8702
-
8703
- if ( $delete_network_common_data ) {
8704
- $this->_delete_plans( false );
8705
-
8706
- $this->_delete_licenses( false );
8707
-
8708
- // Delete add-ons related to plugin's account.
8709
- $this->_delete_account_addons( false );
8710
- }
8711
-
8712
- // @todo Delete plans and licenses of add-ons.
8713
-
8714
- self::$_accounts->store();
8715
-
8716
- /**
8717
- * IMPORTANT:
8718
- * Clear crons must be executed before clearing all storage.
8719
- * Otherwise, the cron will not be cleared.
8720
- */
8721
- if ( $delete_network_common_data ) {
8722
- $this->clear_sync_cron();
8723
- }
8724
-
8725
- $this->clear_install_sync_cron();
8726
-
8727
- // Clear all storage data.
8728
- $this->_storage->clear_all( true, array(
8729
- 'is_delegated_connection',
8730
- 'connectivity_test',
8731
- 'is_on',
8732
- ), false );
8733
-
8734
- // Send delete event.
8735
- $this->get_api_site_scope()->call( '/', 'delete' );
8736
-
8737
- $this->do_action( 'after_account_delete' );
8738
- }
8739
-
8740
- /**
8741
- * Delete network level account.
8742
- *
8743
- * @author Vova Feldman (@svovaf)
8744
- * @since 2.0.0
8745
- *
8746
- * @param bool $check_user Enforce checking if user have plugins activation privileges.
8747
- */
8748
- function delete_network_account_event( $check_user = true ) {
8749
- $this->_logger->entrance( 'slug = ' . $this->_slug );
8750
-
8751
- if ( $check_user && ! $this->is_user_admin() ) {
8752
- return;
8753
- }
8754
-
8755
- $this->do_action( 'before_network_account_delete' );
8756
-
8757
- // Clear all admin notices.
8758
- $this->_admin_notices->clear_all_sticky();
8759
-
8760
- $this->_delete_plans( false, false );
8761
-
8762
- $this->_delete_licenses( false );
8763
-
8764
- // Delete add-ons related to plugin's account.
8765
- $this->_delete_account_addons( false );
8766
-
8767
- // @todo Delete plans and licenses of add-ons.
8768
-
8769
- self::$_accounts->store( true );
8770
-
8771
- /**
8772
- * IMPORTANT:
8773
- * Clear crons must be executed before clearing all storage.
8774
- * Otherwise, the cron will not be cleared.
8775
- */
8776
- $this->clear_sync_cron( true );
8777
- $this->clear_install_sync_cron( true );
8778
-
8779
- $sites = self::get_sites();
8780
-
8781
- $install_ids = array();
8782
- foreach ( $sites as $site ) {
8783
- $blog_id = self::get_site_blog_id( $site );
8784
-
8785
- if ( $this->is_site_delegated_connection( $blog_id ) ) {
8786
- continue;
8787
- }
8788
-
8789
- $install_id = $this->_delete_site( true, $blog_id );
8790
-
8791
- // Clear all storage data.
8792
- $this->_storage->clear_all( true, array( 'connectivity_test' ), $blog_id );
8793
-
8794
- if ( FS_Site::is_valid_id( $install_id ) ) {
8795
- $install_ids[] = $install_id;
8796
- }
8797
-
8798
- switch_to_blog( $blog_id );
8799
-
8800
- $this->do_action( 'after_account_delete' );
8801
-
8802
- restore_current_blog();
8803
- }
8804
-
8805
- $this->_storage->clear_all( true, array(
8806
- 'connectivity_test',
8807
- 'is_on',
8808
- ), true );
8809
-
8810
- // Send delete event.
8811
- if ( ! empty( $install_ids ) ) {
8812
- $result = $this->get_current_or_network_user_api_scope()->call( "/plugins/{$this->_module_id}/installs.json?ids=" . implode( ',', $install_ids ), 'delete' );
8813
- }
8814
-
8815
- $this->do_action( 'after_network_account_delete' );
8816
- }
8817
-
8818
- /**
8819
- * Plugin deactivation hook.
8820
- *
8821
- * @author Vova Feldman (@svovaf)
8822
- * @since 1.0.1
8823
- */
8824
- function _deactivate_plugin_hook() {
8825
- $this->_logger->entrance( 'slug = ' . $this->_slug );
8826
-
8827
- if ( ! $this->is_user_admin() ) {
8828
- return;
8829
- }
8830
-
8831
- $is_network_deactivation = fs_is_network_admin();
8832
- $storage_keys_for_removal = array();
8833
-
8834
- $this->_admin_notices->clear_all_sticky();
8835
-
8836
- $storage_keys_for_removal[] = 'sticky_optin_added';
8837
- if ( isset( $this->_storage->sticky_optin_added ) ) {
8838
- unset( $this->_storage->sticky_optin_added );
8839
- }
8840
-
8841
- if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
8842
- // Remember that plugin was already installed.
8843
- $this->_storage->is_plugin_new_install = false;
8844
- }
8845
-
8846
- // Hook to plugin uninstall.
8847
- register_uninstall_hook( $this->_plugin_main_file_path, array( 'Freemius', '_uninstall_plugin_hook' ) );
8848
-
8849
- $this->clear_module_main_file_cache();
8850
- $this->clear_sync_cron( $this->_is_network_active );
8851
- $this->clear_install_sync_cron();
8852
-
8853
- if ( $this->is_registered() ) {
8854
- if ( $this->is_premium() && ! $this->has_active_valid_license() ) {
8855
- FS_Plugin_Updater::instance( $this )->delete_update_data();
8856
- }
8857
-
8858
- if ( $is_network_deactivation ) {
8859
- // Send deactivation event.
8860
- $this->sync_installs( array(
8861
- 'is_active' => false,
8862
- ) );
8863
- } else {
8864
- // Send deactivation event.
8865
- $this->sync_install( array(
8866
- 'is_active' => false,
8867
- ) );
8868
- }
8869
- } else {
8870
- if ( ! $this->has_api_connectivity() ) {
8871
- // Reset connectivity test cache.
8872
- unset( $this->_storage->connectivity_test );
8873
-
8874
- $storage_keys_for_removal[] = 'connectivity_test';
8875
- }
8876
- }
8877
-
8878
- if ( $is_network_deactivation ) {
8879
- if ( isset( $this->_storage->sticky_optin_added_ms ) ) {
8880
- unset( $this->_storage->sticky_optin_added_ms );
8881
- }
8882
-
8883
- if ( ! empty( $storage_keys_for_removal ) ) {
8884
- $sites = self::get_sites();
8885
-
8886
- foreach ( $sites as $site ) {
8887
- $blog_id = self::get_site_blog_id( $site );
8888
-
8889
- foreach ( $storage_keys_for_removal as $key ) {
8890
- $this->_storage->remove( $key, false, $blog_id );
8891
- }
8892
-
8893
- $this->_storage->save( $blog_id );
8894
- }
8895
- }
8896
- }
8897
-
8898
- // Clear API cache on deactivation.
8899
- FS_Api::clear_cache();
8900
-
8901
- $this->remove_sdk_reference();
8902
- }
8903
-
8904
- /**
8905
- * @author Vova Feldman (@svovaf)
8906
- * @since 1.1.6
8907
- */
8908
- private function remove_sdk_reference() {
8909
- global $fs_active_plugins;
8910
-
8911
- foreach ( $fs_active_plugins->plugins as $sdk_path => $data ) {
8912
- if ( $this->_plugin_basename == $data->plugin_path ) {
8913
- unset( $fs_active_plugins->plugins[ $sdk_path ] );
8914
- break;
8915
- }
8916
- }
8917
-
8918
- fs_fallback_to_newest_active_sdk();
8919
- }
8920
-
8921
- /**
8922
- * @author Vova Feldman (@svovaf)
8923
- * @since 1.1.3
8924
- *
8925
- * @param bool $is_anonymous
8926
- * @param bool|int $network_or_blog_id Since 2.0.0
8927
- */
8928
- private function set_anonymous_mode( $is_anonymous = true, $network_or_blog_id = 0 ) {
8929
- // Store information regarding skip to try and opt-in the user
8930
- // again in the future.
8931
- $skip_info = array(
8932
- 'is' => $is_anonymous,
8933
- 'timestamp' => WP_FS__SCRIPT_START_TIME,
8934
- 'version' => $this->get_plugin_version(),
8935
- );
8936
-
8937
- if ( true === $network_or_blog_id ) {
8938
- $this->_storage->is_anonymous_ms = $skip_info;
8939
- } else {
8940
- $this->_storage->store( 'is_anonymous', $skip_info, $network_or_blog_id );
8941
- }
8942
-
8943
- $this->network_upgrade_mode_completed();
8944
-
8945
- // Update anonymous mode cache.
8946
- $this->_is_anonymous = $is_anonymous;
8947
- }
8948
-
8949
- /**
8950
- * @author Vova Feldman (@svovaf)
8951
- * @since 2.0.0
8952
- *
8953
- * @param int $blog_id Site ID.
8954
- * @param int $user_id User ID.
8955
- * @param string $domain Site domain.
8956
- * @param string $path Site path.
8957
- * @param int $network_id Network ID. Only relevant on multi-network installations.
8958
- * @param array $meta Metadata. Used to set initial site options.
8959
- *
8960
- * @uses Freemius::is_license_network_active() to check if the context license was network activated by the super-admin.
8961
- * @uses Freemius::is_network_connected() to check if the super-admin network opted-in.
8962
- * @uses Freemius::is_network_anonymous() to check if the super-admin network skipped.
8963
- * @uses Freemius::is_network_delegated_connection() to check if the super-admin network delegated the connection to the site admins.
8964
- */
8965
- public function _after_new_blog_callback( $blog_id, $user_id, $domain, $path, $network_id, $meta ) {
8966
- $this->_logger->entrance();
8967
-
8968
- if ( ! $this->_is_network_active ) {
8969
- FS_Clone_Manager::instance()->store_new_blog_install_info( $blog_id );
8970
- return;
8971
- }
8972
-
8973
- $site = null;
8974
- $new_blog_id = $blog_id;
8975
-
8976
- if ( $this->is_premium() &&
8977
- $this->is_network_connected() &&
8978
- is_object( $this->_license ) &&
8979
- $this->_license->can_activate( FS_Site::is_localhost_by_address( $domain ) ) &&
8980
- $this->is_license_network_active( $blog_id )
8981
- ) {
8982
- /**
8983
- * Running the premium version, the license was network activated, and the license can also be activated on the current site -> so try to opt-in with the license key.
8984
- */
8985
- $current_blog_id = get_current_blog_id();
8986
- $license = clone $this->_license;
8987
-
8988
- $this->switch_to_blog( $blog_id );
8989
-
8990
- // Opt-in with network user.
8991
- $this->install_with_user(
8992
- $this->get_network_user(),
8993
- $license->secret_key,
8994
- false,
8995
- false,
8996
- false
8997
- );
8998
-
8999
- if ( is_object( $this->_site ) ) {
9000
- if ( $this->_site->license_id == $license->id ) {
9001
- /**
9002
- * If the license was activated successfully, sync the license data from the remote server.
9003
- */
9004
- $this->_license = $license;
9005
- $this->sync_site_license();
9006
- }
9007
- }
9008
-
9009
- $site = $this->_site;
9010
-
9011
- $this->switch_to_blog( $current_blog_id );
9012
-
9013
- if ( is_object( $site ) ) {
9014
- FS_Clone_Manager::instance()->store_new_blog_install_info( $blog_id, $site );
9015
-
9016
- // Already connected (with or without a license), so no need to continue.
9017
- return;
9018
- }
9019
- }
9020
-
9021
- if ( $this->is_network_anonymous() ) {
9022
- /**
9023
- * Opt-in was network skipped so automatically skip the opt-in for the new site.
9024
- */
9025
- $this->skip_site_connection( $blog_id );
9026
- } else if ( $this->is_network_delegated_connection() ) {
9027
- /**
9028
- * Opt-in was network delegated so automatically delegate the opt-in for the new site's admin.
9029
- */
9030
- $this->delegate_site_connection( $blog_id );
9031
- } else if ( $this->is_network_connected() ) {
9032
- /**
9033
- * Opt-in was network activated so automatically opt-in with the network user and new site admin.
9034
- */
9035
- $current_blog_id = get_current_blog_id();
9036
-
9037
- $this->switch_to_blog( $blog_id );
9038
-
9039
- // Opt-in with network user.
9040
- $this->install_with_user(
9041
- $this->get_network_user(),
9042
- false,
9043
- false,
9044
- false,
9045
- false
9046
- );
9047
-
9048
- $site = $this->_site;
9049
-
9050
- $this->switch_to_blog( $current_blog_id );
9051
- } else {
9052
- /**
9053
- * If the super-admin mixed different options (connect, skip, delegated):
9054
- * a) If at least one site connection was delegated, then automatically delegate connection.
9055
- * b) Otherwise, it means that at least one site was skipped and at least one site was connected. For a simplified UX in the initial release of the multisite network integration, skip the connection for the newly created site. If the super-admin will want to opt-in they can still do that from the network level Account page.
9056
- */
9057
- $has_delegated_site = false;
9058
-
9059
- $sites = self::get_sites();
9060
- foreach ( $sites as $wp_site ) {
9061
- $blog_id = self::get_site_blog_id( $wp_site );
9062
-
9063
- if ( $this->is_site_delegated_connection( $blog_id ) ) {
9064
- $has_delegated_site = true;
9065
- break;
9066
- }
9067
- }
9068
-
9069
- if ( $has_delegated_site ) {
9070
- $this->delegate_site_connection( $blog_id );
9071
- } else {
9072
- $this->skip_site_connection( $blog_id );
9073
- }
9074
- }
9075
-
9076
- /**
9077
- * Store the new blog's information even if there's no install so that when a clone install is stored in the new blog's storage, we can try to resolve it automatically.
9078
- *
9079
- * @author Leo Fajardo (@leorw)
9080
- * @since 2.5.0
9081
- */
9082
- FS_Clone_Manager::instance()->store_new_blog_install_info( $new_blog_id, $site );
9083
- }
9084
-
9085
- /**
9086
- * @author Vova Feldman (@svovaf)
9087
- * @since 2.5.0
9088
- *
9089
- * @param \WP_Site $new_site
9090
- * @param array $args
9091
- */
9092
- public function _after_wp_initialize_site_callback( WP_Site $new_site, $args ) {
9093
- $this->_logger->entrance();
9094
-
9095
- $this->_after_new_blog_callback(
9096
- $new_site->id,
9097
- // Dummy user ID (not in use).
9098
- 0,
9099
- $new_site->domain,
9100
- $new_site->path,
9101
- $new_site->network_id,
9102
- // Dummy meta, not in use.
9103
- array()
9104
- );
9105
- }
9106
-
9107
- /**
9108
- * @author Vova Feldman (@svovaf)
9109
- * @since 1.1.3
9110
- *
9111
- * @param bool|int $network_or_blog_id Since 2.0.0.
9112
- */
9113
- private function reset_anonymous_mode( $network_or_blog_id = 0 ) {
9114
- if ( true === $network_or_blog_id ) {
9115
- unset( $this->_storage->is_anonymous_ms );
9116
- } else {
9117
- $this->_storage->remove( 'is_anonymous', true, $network_or_blog_id );
9118
- }
9119
-
9120
- /**
9121
- * Ensure that this field is also "false", otherwise, if the current module's type is "theme" and the module
9122
- * has no menus, the opt-in popup will not be shown immediately (in this case, the user will have to click
9123
- * on the admin notice that contains the opt-in link in order to trigger the opt-in popup).
9124
- *
9125
- * @author Leo Fajardo (@leorw)
9126
- * @since 1.2.2
9127
- */
9128
- if ( ! $this->_is_network_active ||
9129
- 0 === $network_or_blog_id ||
9130
- get_current_blog_id() == $network_or_blog_id ||
9131
- ( true === $network_or_blog_id && fs_is_network_admin() )
9132
- ) {
9133
- $this->_is_anonymous = null;
9134
- }
9135
- }
9136
-
9137
- /**
9138
- * This is used to ensure that before redirecting to the opt-in page after resetting the anonymous mode or
9139
- * deleting the account in the network level, the URL of the page to redirect to is correct.
9140
- *
9141
- * @author Leo Fajardo (@leorw)
9142
- *
9143
- * @since 2.1.3
9144
- */
9145
- private function maybe_set_slug_and_network_menu_exists_flag() {
9146
- if ( ! empty( $this->_dynamically_added_top_level_page_hook_name ) ) {
9147
- $this->_menu->set_slug_and_network_menu_exists_flag( $this->_menu->has_menu() ?
9148
- $this->_menu->get_slug() :
9149
- $this->_slug
9150
- );
9151
- }
9152
- }
9153
-
9154
- /**
9155
- * Clears the anonymous mode and redirects to the opt-in screen.
9156
- *
9157
- * @author Vova Feldman (@svovaf)
9158
- * @since 1.1.7
9159
- */
9160
- function connect_again() {
9161
- if ( ! $this->is_anonymous() ) {
9162
- return;
9163
- }
9164
-
9165
- $this->reset_anonymous_mode( fs_is_network_admin() );
9166
-
9167
- $this->maybe_set_slug_and_network_menu_exists_flag();
9168
-
9169
- fs_redirect( $this->get_activation_url() );
9170
- }
9171
-
9172
- /**
9173
- * Skip account connect, and set anonymous mode.
9174
- *
9175
- * @author Vova Feldman (@svovaf)
9176
- * @since 1.1.1
9177
- *
9178
- * @param array|null $sites Since 2.0.0. Specific sites.
9179
- * @param bool $skip_all_network Since 2.0.0. If true, skip connection for all sites.
9180
- */
9181
- function skip_connection( $sites = null, $skip_all_network = false ) {
9182
- $this->_logger->entrance();
9183
-
9184
- $this->_admin_notices->remove_sticky( 'connect_account' );
9185
-
9186
- if ( $skip_all_network ) {
9187
- $this->set_anonymous_mode( true, true );
9188
- }
9189
-
9190
- if ( ! $skip_all_network && empty( $sites ) ) {
9191
- $this->skip_site_connection();
9192
- } else {
9193
- $uids = array();
9194
-
9195
- if ( $skip_all_network ) {
9196
- $this->set_anonymous_mode( true, true );
9197
-
9198
- $sites = self::get_sites();
9199
- foreach ( $sites as $site ) {
9200
- $blog_id = self::get_site_blog_id( $site );
9201
- $this->skip_site_connection( $blog_id, false );
9202
- $uids[] = $this->get_anonymous_id( $blog_id );
9203
- }
9204
- } else if ( ! empty( $sites ) ) {
9205
- foreach ( $sites as $site ) {
9206
- $uids[] = $site['uid'];
9207
- $this->skip_site_connection( $site['blog_id'], false );
9208
- }
9209
- }
9210
-
9211
- // Send anonymous skip event.
9212
- // No user identified info nor any tracking will be sent after the user skips the opt-in.
9213
- $this->get_api_plugin_scope()->call( 'skip.json', 'put', array(
9214
- 'uids' => $uids,
9215
- ) );
9216
- }
9217
-
9218
- $this->network_upgrade_mode_completed();
9219
- }
9220
-
9221
- /**
9222
- * Skip connection for specific site in the network.
9223
- *
9224
- * @author Vova Feldman (@svovaf)
9225
- * @since 2.0.0
9226
- *
9227
- * @param int|null $blog_id
9228
- * @param bool $send_skip
9229
- */
9230
- private function skip_site_connection( $blog_id = null, $send_skip = true ) {
9231
- $this->_logger->entrance();
9232
-
9233
- $this->_admin_notices->remove_sticky( 'connect_account', $blog_id );
9234
-
9235
- $this->set_anonymous_mode( true, $blog_id );
9236
-
9237
- if ( $send_skip ) {
9238
- $this->get_api_plugin_scope()->call( 'skip.json', 'put', array(
9239
- 'uids' => array( $this->get_anonymous_id( $blog_id ) ),
9240
- ) );
9241
- }
9242
- }
9243
-
9244
- /**
9245
- * Plugin version update hook.
9246
- *
9247
- * @author Vova Feldman (@svovaf)
9248
- * @since 1.0.4
9249
- */
9250
- private function update_plugin_version_event() {
9251
- $this->_logger->entrance();
9252
-
9253
- if ( ! $this->is_registered() ) {
9254
- return;
9255
- }
9256
-
9257
- $this->schedule_install_sync();
9258
- // $this->sync_install( array(), true );
9259
- }
9260
-
9261
- /**
9262
- * Generate an MD5 signature of a plugins collection.
9263
- * This helper methods used to identify changes in a plugins collection.
9264
- *
9265
- * @author Vova Feldman (@svovaf)
9266
- * @since 2.0.0
9267
- *
9268
- * @param array [string]array $plugins
9269
- *
9270
- * @return string
9271
- */
9272
- private function get_plugins_thumbprint( $plugins ) {
9273
- ksort( $plugins );
9274
-
9275
- $thumbprint = '';
9276
- foreach ( $plugins as $basename => $data ) {
9277
- $thumbprint .= $data['slug'] . ',' .
9278
- $data['Version'] . ',' .
9279
- ( $data['is_active'] ? '1' : '0' ) . ';';
9280
- }
9281
-
9282
- return md5( $thumbprint );
9283
- }
9284
-
9285
- /**
9286
- * Return a list of modified plugins since the last sync.
9287
- *
9288
- * Note:
9289
- * There's no point to store a plugins counter since even if the number of
9290
- * plugins didn't change, we still need to check if the versions are all the
9291
- * same and the activity state is similar.
9292
- *
9293
- * @author Vova Feldman (@svovaf)
9294
- * @since 1.1.8
9295
- *
9296
- * @return array|false
9297
- */
9298
- private function get_plugins_data_for_api() {
9299
- // Alias.
9300
- $site_active_plugins_option_name = 'active_plugins';
9301
- $network_plugins_option_name = 'all_plugins';
9302
-
9303
- /**
9304
- * Collection of all site level active plugins.
9305
- */
9306
- $site_active_plugins_cache = self::$_accounts->get_option( $site_active_plugins_option_name );
9307
-
9308
- if ( ! is_object( $site_active_plugins_cache ) ) {
9309
- $site_active_plugins_cache = (object) array(
9310
- 'timestamp' => '',
9311
- 'md5' => '',
9312
- 'plugins' => array(),
9313
- );
9314
- }
9315
-
9316
- $time = time();
9317
-
9318
- if ( ! empty( $site_active_plugins_cache->timestamp ) &&
9319
- ( $time - $site_active_plugins_cache->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC
9320
- ) {
9321
- // Don't send plugin updates if last update was in the past 5 min.
9322
- return false;
9323
- }
9324
-
9325
- // Write timestamp to lock the logic.
9326
- $site_active_plugins_cache->timestamp = $time;
9327
- self::$_accounts->set_option( $site_active_plugins_option_name, $site_active_plugins_cache, true );
9328
-
9329
- // Reload options from DB.
9330
- self::$_accounts->load( true );
9331
- $site_active_plugins_cache = self::$_accounts->get_option( $site_active_plugins_option_name );
9332
-
9333
- if ( $time != $site_active_plugins_cache->timestamp ) {
9334
- // If timestamp is different, then another thread captured the lock.
9335
- return false;
9336
- }
9337
-
9338
- /**
9339
- * Collection of all plugins (network level).
9340
- */
9341
- $network_plugins_cache = self::$_accounts->get_option( $network_plugins_option_name );
9342
-
9343
- if ( ! is_object( $network_plugins_cache ) ) {
9344
- $network_plugins_cache = (object) array(
9345
- 'timestamp' => '',
9346
- 'md5' => '',
9347
- 'plugins' => array(),
9348
- );
9349
- }
9350
-
9351
- // Check if there's a change in plugins.
9352
- $network_plugins = self::get_network_plugins();
9353
- $site_active_plugins = self::get_site_active_plugins();
9354
-
9355
- $network_plugins_thumbprint = $this->get_plugins_thumbprint( $network_plugins );
9356
- $site_active_plugins_thumbprint = $this->get_plugins_thumbprint( $site_active_plugins );
9357
-
9358
- // Check if plugins status changed (version or active/inactive).
9359
- $network_plugins_changed = ( $network_plugins_cache->md5 !== $network_plugins_thumbprint );
9360
- $site_active_plugins_changed = ( $site_active_plugins_cache->md5 !== $site_active_plugins_thumbprint );
9361
-
9362
- if ( ! $network_plugins_changed &&
9363
- ! $site_active_plugins_changed
9364
- ) {
9365
- // No changes.
9366
- return array();
9367
- }
9368
-
9369
- $plugins_update_data = array();
9370
-
9371
- foreach ( $network_plugins_cache->plugins as $basename => $data ) {
9372
- if ( ! isset( $network_plugins[ $basename ] ) ) {
9373
- // Plugin uninstalled.
9374
- $uninstalled_plugin_data = $data;
9375
- $uninstalled_plugin_data['is_active'] = false;
9376
- $uninstalled_plugin_data['is_uninstalled'] = true;
9377
- $plugins_update_data[] = $uninstalled_plugin_data;
9378
-
9379
- unset( $network_plugins[ $basename ] );
9380
-
9381
- unset( $network_plugins_cache->plugins[ $basename ] );
9382
- unset( $site_active_plugins_cache->plugins[ $basename ] );
9383
-
9384
- continue;
9385
- }
9386
-
9387
- $was_active = $data['is_active'] ||
9388
- ( isset( $site_active_plugins_cache->plugins[ $basename ] ) &&
9389
- true === $site_active_plugins_cache->plugins[ $basename ]['is_active'] );
9390
- $is_active = $network_plugins[ $basename ]['is_active'] ||
9391
- ( isset( $site_active_plugins[ $basename ] ) &&
9392
- $site_active_plugins[ $basename ]['is_active'] );
9393
-
9394
- if ( ! isset( $site_active_plugins_cache->plugins[ $basename ] ) &&
9395
- isset( $site_active_plugins[ $basename ] )
9396
- ) {
9397
- // Plugin was site level activated.
9398
- $site_active_plugins_cache->plugins[ $basename ] = $network_plugins[ $basename ];
9399
- $site_active_plugins_cache->plugins[ $basename ]['is_active'] = true;
9400
- } else if ( isset( $site_active_plugins_cache->plugins[ $basename ] ) &&
9401
- ! isset( $site_active_plugins[ $basename ] )
9402
- ) {
9403
- // Plugin was site level deactivated.
9404
- unset( $site_active_plugins_cache->plugins[ $basename ] );
9405
- }
9406
-
9407
- $prev_version = $data['version'];
9408
- $current_version = $network_plugins[ $basename ]['Version'];
9409
-
9410
- if ( $was_active !== $is_active || $prev_version !== $current_version ) {
9411
- // Plugin activated or deactivated, or version changed.
9412
-
9413
- if ( $was_active !== $is_active ) {
9414
- if ( $data['is_active'] != $network_plugins[ $basename ]['is_active'] ) {
9415
- $network_plugins_cache->plugins[ $basename ]['is_active'] = $data['is_active'];
9416
- }
9417
- }
9418
-
9419
- if ( $prev_version !== $current_version ) {
9420
- $network_plugins_cache->plugins[ $basename ]['Version'] = $current_version;
9421
- }
9422
-
9423
- $updated_plugin_data = $data;
9424
- $updated_plugin_data['is_active'] = $is_active;
9425
- $updated_plugin_data['version'] = $current_version;
9426
- $updated_plugin_data['title'] = $network_plugins[ $basename ]['Name'];
9427
- $plugins_update_data[] = $updated_plugin_data;
9428
- }
9429
- }
9430
-
9431
- // Find new plugins that weren't yet seen before.
9432
- foreach ( $network_plugins as $basename => $data ) {
9433
- if ( ! isset( $network_plugins_cache->plugins[ $basename ] ) ) {
9434
- // New plugin.
9435
- $new_plugin = array(
9436
- 'slug' => $data['slug'],
9437
- 'version' => $data['Version'],
9438
- 'title' => $data['Name'],
9439
- 'is_active' => $data['is_active'],
9440
- 'is_uninstalled' => false,
9441
- );
9442
-
9443
- $network_plugins_cache->plugins[ $basename ] = $new_plugin;
9444
-
9445
- $is_site_level_active = (
9446
- isset( $site_active_plugins[ $basename ] ) &&
9447
- $site_active_plugins[ $basename ]['is_active']
9448
- );
9449
-
9450
- /**
9451
- * If not network active, set the activity status based on the site-level plugin status.
9452
- */
9453
- if ( ! $new_plugin['is_active'] ) {
9454
- $new_plugin['is_active'] = $is_site_level_active;
9455
- }
9456
-
9457
- $plugins_update_data[] = $new_plugin;
9458
-
9459
- if ( isset( $site_active_plugins[ $basename ] ) ) {
9460
- $site_active_plugins_cache->plugins[ $basename ] = $new_plugin;
9461
- $site_active_plugins_cache->plugins[ $basename ]['is_active'] = $is_site_level_active;
9462
- }
9463
- }
9464
- }
9465
-
9466
- $site_active_plugins_cache->md5 = $site_active_plugins_thumbprint;
9467
- $site_active_plugins_cache->timestamp = $time;
9468
- self::$_accounts->set_option( $site_active_plugins_option_name, $site_active_plugins_cache, true );
9469
-
9470
- $network_plugins_cache->md5 = $network_plugins_thumbprint;
9471
- $network_plugins_cache->timestamp = $time;
9472
- self::$_accounts->set_option( $network_plugins_option_name, $network_plugins_cache, true );
9473
-
9474
- return $plugins_update_data;
9475
- }
9476
-
9477
- /**
9478
- * Return a list of modified themes since the last sync.
9479
- *
9480
- * Note:
9481
- * There's no point to store a themes counter since even if the number of
9482
- * themes didn't change, we still need to check if the versions are all the
9483
- * same and the activity state is similar.
9484
- *
9485
- * @author Vova Feldman (@svovaf)
9486
- * @since 1.1.8
9487
- *
9488
- * @return array|false
9489
- */
9490
- private function get_themes_data_for_api() {
9491
- // Alias.
9492
- $option_name = 'all_themes';
9493
-
9494
- $all_cached_themes = self::$_accounts->get_option( $option_name );
9495
-
9496
- if ( ! is_object( $all_cached_themes ) ) {
9497
- $all_cached_themes = (object) array(
9498
- 'timestamp' => '',
9499
- 'md5' => '',
9500
- 'themes' => array(),
9501
- );
9502
- }
9503
-
9504
- $time = time();
9505
-
9506
- if ( ! empty( $all_cached_themes->timestamp ) &&
9507
- ( $time - $all_cached_themes->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC
9508
- ) {
9509
- // Don't send theme updates if last update was in the past 5 min.
9510
- return false;
9511
- }
9512
-
9513
- // Write timestamp to lock the logic.
9514
- $all_cached_themes->timestamp = $time;
9515
- self::$_accounts->set_option( $option_name, $all_cached_themes, true );
9516
-
9517
- // Reload options from DB.
9518
- self::$_accounts->load( true );
9519
- $all_cached_themes = self::$_accounts->get_option( $option_name );
9520
-
9521
- if ( $time != $all_cached_themes->timestamp ) {
9522
- // If timestamp is different, then another thread captured the lock.
9523
- return false;
9524
- }
9525
-
9526
- // Get active theme.
9527
- $active_theme = wp_get_theme();
9528
- $active_theme_stylesheet = $active_theme->get_stylesheet();
9529
-
9530
- // Check if there's a change in themes.
9531
- $all_themes = wp_get_themes();
9532
-
9533
- // Check if themes changed.
9534
- ksort( $all_themes );
9535
-
9536
- $themes_signature = '';
9537
- foreach ( $all_themes as $slug => $data ) {
9538
- $is_active = ( $slug === $active_theme_stylesheet );
9539
- $themes_signature .= $slug . ',' .
9540
- $data->version . ',' .
9541
- ( $is_active ? '1' : '0' ) . ';';
9542
- }
9543
-
9544
- // Check if themes status changed (version or active/inactive).
9545
- $themes_changed = ( $all_cached_themes->md5 !== md5( $themes_signature ) );
9546
-
9547
- $themes_update_data = array();
9548
-
9549
- if ( $themes_changed ) {
9550
- // Change in themes, report changes.
9551
-
9552
- // Update existing themes info.
9553
- foreach ( $all_cached_themes->themes as $slug => $data ) {
9554
- $is_active = ( $slug === $active_theme_stylesheet );
9555
-
9556
- if ( ! isset( $all_themes[ $slug ] ) ) {
9557
- // Plugin uninstalled.
9558
- $uninstalled_theme_data = $data;
9559
- $uninstalled_theme_data['is_active'] = false;
9560
- $uninstalled_theme_data['is_uninstalled'] = true;
9561
- $themes_update_data[] = $uninstalled_theme_data;
9562
-
9563
- unset( $all_themes[ $slug ] );
9564
- unset( $all_cached_themes->themes[ $slug ] );
9565
- } else if ( $data['is_active'] !== $is_active ||
9566
- $data['version'] !== $all_themes[ $slug ]->version
9567
- ) {
9568
- // Plugin activated or deactivated, or version changed.
9569
-
9570
- $all_cached_themes->themes[ $slug ]['is_active'] = $is_active;
9571
- $all_cached_themes->themes[ $slug ]['version'] = $all_themes[ $slug ]->version;
9572
-
9573
- $themes_update_data[] = $all_cached_themes->themes[ $slug ];
9574
- }
9575
- }
9576
-
9577
- // Find new themes that weren't yet seen before.
9578
- foreach ( $all_themes as $slug => $data ) {
9579
- if ( ! isset( $all_cached_themes->themes[ $slug ] ) ) {
9580
- $is_active = ( $slug === $active_theme_stylesheet );
9581
-
9582
- // New plugin.
9583
- $new_plugin = array(
9584
- 'slug' => $slug,
9585
- 'version' => $data->version,
9586
- 'title' => $data->name,
9587
- 'is_active' => $is_active,
9588
- 'is_uninstalled' => false,
9589
- );
9590
-
9591
- $themes_update_data[] = $new_plugin;
9592
- $all_cached_themes->themes[ $slug ] = $new_plugin;
9593
- }
9594
- }
9595
-
9596
- $all_cached_themes->md5 = md5( $themes_signature );
9597
- $all_cached_themes->timestamp = time();
9598
- self::$_accounts->set_option( $option_name, $all_cached_themes, true );
9599
- }
9600
-
9601
- return $themes_update_data;
9602
- }
9603
-
9604
- /**
9605
- * Get site data for API install request.
9606
- *
9607
- * @author Vova Feldman (@svovaf)
9608
- * @since 1.1.2
9609
- *
9610
- * @param string[] $override
9611
- * @param bool $include_plugins Since 1.1.8 by default include plugin changes.
9612
- * @param bool $include_themes Since 1.1.8 by default include plugin changes.
9613
- * @param bool $include_blog_data Since 2.3.0 by default include the current blog's data (language, charset, title, and URL).
9614
- *
9615
- * @return array
9616
- */
9617
- private function get_install_data_for_api(
9618
- array $override,
9619
- $include_plugins = true,
9620
- $include_themes = true,
9621
- $include_blog_data = true
9622
- ) {
9623
- if ( $this->is_extensions_tracking_allowed() ) {
9624
- if ( ! defined( 'WP_FS__TRACK_PLUGINS' ) || false !== WP_FS__TRACK_PLUGINS ) {
9625
- /**
9626
- * @since 1.1.8 Also send plugin updates.
9627
- */
9628
- if ( $include_plugins && ! isset( $override['plugins'] ) ) {
9629
- $plugins = $this->get_plugins_data_for_api();
9630
- if ( ! empty( $plugins ) ) {
9631
- $override['plugins'] = $plugins;
9632
- }
9633
- }
9634
- }
9635
-
9636
- if ( ! defined( 'WP_FS__TRACK_THEMES' ) || false !== WP_FS__TRACK_THEMES ) {
9637
- /**
9638
- * @since 1.1.8 Also send themes updates.
9639
- */
9640
- if ( $include_themes && ! isset( $override['themes'] ) ) {
9641
- $themes = $this->get_themes_data_for_api();
9642
- if ( ! empty( $themes ) ) {
9643
- $override['themes'] = $themes;
9644
- }
9645
- }
9646
- }
9647
- }
9648
-
9649
- $versions = $this->get_versions();
9650
-
9651
- $blog_data = $include_blog_data ?
9652
- array(
9653
- 'language' => get_bloginfo( 'language' ),
9654
- 'charset' => get_bloginfo( 'charset' ),
9655
- 'title' => get_bloginfo( 'name' ),
9656
- 'url' => get_site_url(),
9657
- ) :
9658
- array();
9659
-
9660
- return array_merge( $versions, $blog_data, array(
9661
- 'version' => $this->get_plugin_version(),
9662
- 'is_premium' => $this->is_premium(),
9663
- // Special params.
9664
- 'is_active' => true,
9665
- 'is_disconnected' => $this->is_tracking_prohibited(),
9666
- 'is_uninstalled' => false,
9667
- ), $override );
9668
- }
9669
-
9670
- /**
9671
- * Update installs details.
9672
- *
9673
- * @todo V1 of multiste network support doesn't support plugin and theme data sending.
9674
- *
9675
- * @author Vova Feldman (@svovaf)
9676
- * @since 2.0.0
9677
- *
9678
- * @param string[] string $override
9679
- * @param bool $only_diff
9680
- * @param bool $is_keepalive
9681
- * @param bool $include_plugins Since 1.1.8 by default include plugin changes.
9682
- * @param bool $include_themes Since 1.1.8 by default include plugin changes.
9683
- *
9684
- * @return array
9685
- */
9686
- private function get_installs_data_for_api(
9687
- array $override,
9688
- $only_diff = false,
9689
- $is_keepalive = false,
9690
- $include_plugins = true,
9691
- $include_themes = true
9692
- ) {
9693
- /**
9694
- * @since 1.1.8 Also send plugin updates.
9695
- */
9696
- // if ( $include_plugins && ! isset( $override['plugins'] ) ) {
9697
- // $plugins = $this->get_plugins_data_for_api();
9698
- // if ( ! empty( $plugins ) ) {
9699
- // $override['plugins'] = $plugins;
9700
- // }
9701
- // }
9702
- /**
9703
- * @since 1.1.8 Also send themes updates.
9704
- */
9705
- // if ( $include_themes && ! isset( $override['themes'] ) ) {
9706
- // $themes = $this->get_themes_data_for_api();
9707
- // if ( ! empty( $themes ) ) {
9708
- // $override['themes'] = $themes;
9709
- // }
9710
- // }
9711
-
9712
- // Common properties.
9713
- $versions = $this->get_versions();
9714
- $common = array_merge( $versions, array(
9715
- 'version' => $this->get_plugin_version(),
9716
- 'is_premium' => $this->is_premium(),
9717
- ), $override );
9718
-
9719
-
9720
- $is_common_diff_for_any_site = false;
9721
- $common_diff_union = array();
9722
-
9723
- $installs_data = array();
9724
-
9725
- $sites = self::get_sites();
9726
-
9727
- $subsite_data_by_install_id = array();
9728
- $install_url_by_install_id = array();
9729
-
9730
- foreach ( $sites as $site ) {
9731
- $blog_id = self::get_site_blog_id( $site );
9732
-
9733
- $install = $this->get_install_by_blog_id( $blog_id );
9734
-
9735
- if ( is_object( $install ) ) {
9736
- if ( $install->user_id != $this->_user->id ) {
9737
- // Install belongs to a different owner.
9738
- continue;
9739
- }
9740
-
9741
- if ( ! $this->is_premium() && $install->is_tracking_prohibited() ) {
9742
- // Don't send updates regarding opted-out installs.
9743
- continue;
9744
- }
9745
-
9746
- $install_data = $this->get_site_info( $site );
9747
-
9748
- if ( FS_Clone_Manager::instance()->is_temporary_duplicate_by_blog_id( $install_data['blog_id'] ) ) {
9749
- continue;
9750
- }
9751
-
9752
- $uid = $install_data['uid'];
9753
- $url = $install_data['url'];
9754
-
9755
- if ( isset( $subsite_data_by_install_id[ $install->id ] ) ) {
9756
- $clone_subsite_data = $subsite_data_by_install_id[ $install->id ];
9757
- $clone_install_url = $install_url_by_install_id[ $install->id ];
9758
-
9759
- if (
9760
- /**
9761
- * If we already have an install with the same URL as the subsite it's stored in, skip the current subsite. Otherwise, replace the existing install's data with the current subsite's install's data if the URLs match.
9762
- *
9763
- * @author Leo Fajardo (@leorw)
9764
- * @since 2.5.0
9765
- */
9766
- fs_strip_url_protocol( untrailingslashit( $clone_install_url ) ) === fs_strip_url_protocol( untrailingslashit( $clone_subsite_data['url'] ) ) ||
9767
- fs_strip_url_protocol( untrailingslashit( $install->url ) ) !== fs_strip_url_protocol( untrailingslashit( $url ) )
9768
- ) {
9769
- continue;
9770
- }
9771
- }
9772
-
9773
- unset( $install_data['blog_id'] );
9774
- unset( $install_data['uid'] );
9775
- unset( $install_data['url'] );
9776
-
9777
- $install_data['is_disconnected'] = $install->is_disconnected;
9778
- $install_data['is_active'] = $this->is_active_for_site( $blog_id );
9779
- $install_data['is_uninstalled'] = $install->is_uninstalled;
9780
-
9781
- $common_diff = null;
9782
- $is_common_diff = false;
9783
- if ( $only_diff ) {
9784
- $install_data = $this->get_install_diff_for_api( $install_data, $install, $override );
9785
- $common_diff = $this->get_install_diff_for_api( $common, $install, $override );
9786
-
9787
- $is_common_diff = ! empty( $common_diff );
9788
-
9789
- if ( $is_common_diff ) {
9790
- foreach ( $common_diff as $k => $v ) {
9791
- if ( ! isset( $common_diff_union[ $k ] ) ) {
9792
- $common_diff_union[ $k ] = $v;
9793
- }
9794
- }
9795
- }
9796
-
9797
- $is_common_diff_for_any_site = $is_common_diff_for_any_site || $is_common_diff;
9798
- }
9799
-
9800
- if ( ! empty( $install_data ) || $is_common_diff || $is_keepalive ) {
9801
- // Add install ID and site unique ID.
9802
- $install_data['id'] = $install->id;
9803
- $install_data['uid'] = $uid;
9804
- $install_data['url'] = $url;
9805
-
9806
- $subsite_data_by_install_id[ $install->id ] = $install_data;
9807
- $install_url_by_install_id[ $install->id ] = $install->url;
9808
- }
9809
- }
9810
- }
9811
-
9812
- restore_current_blog();
9813
-
9814
- $installs_data = array_merge(
9815
- $installs_data,
9816
- array_values( $subsite_data_by_install_id )
9817
- );
9818
-
9819
- if ( 0 < count( $installs_data ) && ( $is_common_diff_for_any_site || ! $only_diff ) ) {
9820
- if ( ! $only_diff ) {
9821
- $installs_data[] = $common;
9822
- } else if ( ! empty( $common_diff_union ) ) {
9823
- $installs_data[] = $common_diff_union;
9824
- }
9825
- }
9826
-
9827
- foreach ( $installs_data as &$data ) {
9828
- $data = (object) $data;
9829
- }
9830
-
9831
- return $installs_data;
9832
- }
9833
-
9834
- /**
9835
- * Compare site actual data to the stored install data and return the differences for an API data sync.
9836
- *
9837
- * @author Vova Feldman (@svovaf)
9838
- * @since 2.0.0
9839
- *
9840
- * @param array $site
9841
- * @param FS_Site $install
9842
- * @param string[] string $override
9843
- *
9844
- * @return array
9845
- */
9846
- private function get_install_diff_for_api( $site, $install, $override = array() ) {
9847
- $diff = array();
9848
- $special = array();
9849
- $special_override = false;
9850
-
9851
- foreach ( $site as $p => $v ) {
9852
- if ( property_exists( $install, $p ) ) {
9853
- if ( ( is_bool( $install->{$p} ) || ! empty( $install->{$p} ) ) &&
9854
- $install->{$p} != $v
9855
- ) {
9856
- $install->{$p} = $v;
9857
- $diff[ $p ] = $v;
9858
- }
9859
- } else {
9860
- $special[ $p ] = $v;
9861
-
9862
- if ( isset( $override[ $p ] ) ||
9863
- 'plugins' === $p ||
9864
- 'themes' === $p
9865
- ) {
9866
- $special_override = true;
9867
- }
9868
- }
9869
- }
9870
-
9871
- if ( $special_override || 0 < count( $diff ) ) {
9872
- // Add special params only if has at least one
9873
- // standard param, or if explicitly requested to
9874
- // override a special param or a param which is not exist
9875
- // in the install object.
9876
- $diff = array_merge( $diff, $special );
9877
- }
9878
-
9879
- return $diff;
9880
- }
9881
-
9882
- /**
9883
- * Update install only if changed.
9884
- *
9885
- * @author Vova Feldman (@svovaf)
9886
- * @since 1.0.9
9887
- *
9888
- * @param string[] string $override
9889
- * @param bool $flush
9890
- * @param bool $is_two_way_sync @since 2.5.0 If true and there's a successful API request, the install sync cron will be cleared.
9891
- *
9892
- * @return false|object|string
9893
- */
9894
- private function send_install_update( $override = array(), $flush = false, $is_two_way_sync = false ) {
9895
- $this->_logger->entrance();
9896
-
9897
- $check_properties = $this->get_install_data_for_api( $override );
9898
-
9899
- if ( $flush ) {
9900
- $params = $check_properties;
9901
- } else {
9902
- $params = $this->get_install_diff_for_api( $check_properties, $this->_site, $override );
9903
- }
9904
-
9905
- $keepalive_only_update = false;
9906
- if ( empty( $params ) ) {
9907
- $keepalive_only_update = $this->should_send_keepalive_update();
9908
-
9909
- if ( ! $keepalive_only_update ) {
9910
- /**
9911
- * There are no updates to send including keepalive.
9912
- *
9913
- * @author Leo Fajardo (@leorw)
9914
- * @since 2.2.3
9915
- */
9916
- return false;
9917
- }
9918
- }
9919
-
9920
- if ( $is_two_way_sync ) {
9921
- /**
9922
- * Update last install sync timestamp during a two-way sync call as we expect that updates are sent during this call.
9923
- *
9924
- * @author Leo Fajardo (@leorw)
9925
- * @since 2.2.3
9926
- */
9927
- if ( ! is_multisite() ) {
9928
- // Update last install sync timestamp.
9929
- $this->set_cron_execution_timestamp( 'install_sync' );
9930
- }
9931
-
9932
- $params['uid'] = $this->get_anonymous_id();
9933
- }
9934
-
9935
- $this->set_keepalive_timestamp();
9936
-
9937
- // Send updated values to FS.
9938
- $site = $this->api_site_call( '/', 'put', $params, true );
9939
-
9940
- if ( $is_two_way_sync && $this->is_api_result_entity( $site ) ) {
9941
- /**
9942
- * Clear scheduled install sync after a two-way sync call.
9943
- *
9944
- * @author Leo Fajardo (@leorw)
9945
- * @since 2.2.3
9946
- */
9947
- if ( ! is_multisite() ) {
9948
- // I successfully sent install update, clear scheduled sync if exist.
9949
- $this->clear_install_sync_cron();
9950
- }
9951
- }
9952
-
9953
- return $site;
9954
- }
9955
-
9956
- /**
9957
- * Update installs only if changed.
9958
- *
9959
- * @author Vova Feldman (@svovaf)
9960
- * @since 2.0.0
9961
- *
9962
- * @param string[] string $override
9963
- * @param bool $flush
9964
- * @param bool $is_two_way_sync @since 2.5.0 If true and there's a successful API request, the install sync cron will be cleared.
9965
- *
9966
- * @return false|object|string
9967
- */
9968
- private function send_installs_update( $override = array(), $flush = false, $is_two_way_sync = false ) {
9969
- $this->_logger->entrance();
9970
-
9971
- /**
9972
- * Pass `true` to use the network level storage since the update is for many installs.
9973
- *
9974
- * @author Leo Fajardo (@leorw)
9975
- * @since 2.2.3
9976
- */
9977
- $should_send_keepalive = $this->should_send_keepalive_update( true );
9978
-
9979
- $installs_data = $this->get_installs_data_for_api( $override, ! $flush, $should_send_keepalive );
9980
-
9981
- if ( empty( $installs_data ) ) {
9982
- return false;
9983
- }
9984
-
9985
- if ( $is_two_way_sync ) {
9986
- // Update last install sync timestamp during a two-way sync call as we expect that updates are sent during this call.
9987
- $this->set_cron_execution_timestamp( 'install_sync' );
9988
- }
9989
-
9990
- /**
9991
- * Pass `true` to use the network level storage since the update is for many installs.
9992
- *
9993
- * @author Leo Fajardo (@leorw)
9994
- * @since 2.2.3
9995
- */
9996
- $this->set_keepalive_timestamp( true );
9997
-
9998
- // Send updated values to FS.
9999
- $result = $this->get_api_user_scope()->call( "/plugins/{$this->_plugin->id}/installs.json", 'put', $installs_data );
10000
-
10001
- if ( $is_two_way_sync && $this->is_api_result_object( $result, 'installs' ) ) {
10002
- // I successfully sent a two-way installs update, clear the scheduled install sync if it exists.
10003
- $this->clear_install_sync_cron();
10004
- }
10005
-
10006
- return $result;
10007
- }
10008
-
10009
- /**
10010
- * @author Leo Fajardo (@leorw)
10011
- *
10012
- * @param bool|null $use_network_level_storage
10013
- *
10014
- * @return bool
10015
- */
10016
- private function should_send_keepalive_update( $use_network_level_storage = null ) {
10017
- $keepalive_timestamp = $this->_storage->get( 'keepalive_timestamp', 0, $use_network_level_storage );
10018
-
10019
- if ( $keepalive_timestamp < ( time() - WP_FS__TIME_WEEK_IN_SEC ) ) {
10020
- // If updated more than 7 days ago, trigger a keepalive and update the time it was triggered.
10021
- return true;
10022
- } else {
10023
- // If updated 7 days ago or less, "flip a coin", if the value is 7 trigger a keepalive and update the last time it was triggered.
10024
- return ( 7 == rand( 1, 7 ) );
10025
- }
10026
- }
10027
-
10028
- /**
10029
- * Syncs the install owner's data if needed (i.e., if the install owner is different from the loaded user).
10030
- *
10031
- * @author Leo Fajardo (@leorw)
10032
- * @since 2.3.2
10033
- */
10034
- private function maybe_sync_install_user() {
10035
- if ( $this->_user->id == $this->_site->user_id ) {
10036
- return;
10037
- }
10038
-
10039
- // Fetch user data and store if found.
10040
- $this->sync_user_by_current_install();
10041
- }
10042
-
10043
- /**
10044
- * Update install only if changed.
10045
- *
10046
- * @author Vova Feldman (@svovaf)
10047
- * @since 1.0.9
10048
- *
10049
- * @param string[] string $override
10050
- * @param bool $flush
10051
- */
10052
- function sync_install( $override = array(), $flush = false ) {
10053
- $this->_logger->entrance();
10054
-
10055
- $site = $this->send_install_update( $override, $flush, true );
10056
-
10057
- if ( false === $site ) {
10058
- // No sync required.
10059
- return;
10060
- }
10061
-
10062
- if ( ! $this->is_api_result_entity( $site ) ) {
10063
- // Failed to sync, don't update locally.
10064
- return;
10065
- }
10066
-
10067
- $this->_site = new FS_Site( $site );
10068
-
10069
- $this->_store_site( true );
10070
- }
10071
-
10072
- /**
10073
- * Update install only if changed.
10074
- *
10075
- * @author Vova Feldman (@svovaf)
10076
- * @since 1.0.9
10077
- *
10078
- * @param string[] string $override
10079
- * @param bool $flush
10080
- */
10081
- private function sync_installs( $override = array(), $flush = false ) {
10082
- $this->_logger->entrance();
10083
-
10084
- $result = $this->send_installs_update( $override, $flush, true );
10085
-
10086
- if ( false === $result ) {
10087
- // No sync required.
10088
- return;
10089
- }
10090
-
10091
- if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
10092
- // Failed to sync, don't update locally.
10093
- return;
10094
- }
10095
-
10096
- $address_to_blog_map = $this->get_address_to_blog_map();
10097
-
10098
- foreach ( $result->installs as $install ) {
10099
- $this->_site = new FS_Site( $install );
10100
-
10101
- $address = trailingslashit( fs_strip_url_protocol( $install->url ) );
10102
- $blog_id = $address_to_blog_map[ $address ];
10103
-
10104
- $this->_store_site( true, $blog_id );
10105
- }
10106
- }
10107
-
10108
- /**
10109
- * Track install's custom event.
10110
- *
10111
- * IMPORTANT:
10112
- * Custom event tracking is currently only supported for specific clients.
10113
- * If you are not one of them, please don't use this method. If you will,
10114
- * the API will simply ignore your request based on the plugin ID.
10115
- *
10116
- * Need custom tracking for your plugin or theme?
10117
- * If you are interested in custom event tracking please contact yo@freemius.com
10118
- * for further details.
10119
- *
10120
- * @author Vova Feldman (@svovaf)
10121
- * @since 1.2.1
10122
- *
10123
- * @param string $name Event name.
10124
- * @param array $properties Associative key/value array with primitive values only
10125
- * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s.
10126
- * @param bool $once If true, event will be tracked only once. IMPORTANT: Still trigger the API call.
10127
- *
10128
- * @return object|false Event data or FALSE on failure.
10129
- *
10130
- * @throws \Freemius_InvalidArgumentException
10131
- */
10132
- public function track_event( $name, $properties = array(), $process_at = false, $once = false ) {
10133
- $this->_logger->entrance( http_build_query( array( 'name' => $name, 'once' => $once ) ) );
10134
-
10135
- if ( ! $this->is_registered() ) {
10136
- return false;
10137
- }
10138
-
10139
- $event = array( 'type' => $name );
10140
-
10141
- if ( is_numeric( $process_at ) && $process_at > time() ) {
10142
- $event['process_at'] = $process_at;
10143
- }
10144
-
10145
- if ( $once ) {
10146
- $event['once'] = true;
10147
- }
10148
-
10149
- if ( ! empty( $properties ) ) {
10150
- // Verify associative array values are primitive.
10151
- foreach ( $properties as $k => $v ) {
10152
- if ( ! is_scalar( $v ) ) {
10153
- throw new Freemius_InvalidArgumentException( 'The $properties argument must be an associative key/value array with primitive values only.' );
10154
- }
10155
- }
10156
-
10157
- $event['properties'] = $properties;
10158
- }
10159
-
10160
- $result = $this->get_api_site_scope()->call( 'events.json', 'post', $event );
10161
-
10162
- return $this->is_api_error( $result ) ?
10163
- false :
10164
- $result;
10165
- }
10166
-
10167
- /**
10168
- * Track install's custom event only once, but it still triggers the API call.
10169
- *
10170
- * IMPORTANT:
10171
- * Custom event tracking is currently only supported for specific clients.
10172
- * If you are not one of them, please don't use this method. If you will,
10173
- * the API will simply ignore your request based on the plugin ID.
10174
- *
10175
- * Need custom tracking for your plugin or theme?
10176
- * If you are interested in custom event tracking please contact yo@freemius.com
10177
- * for further details.
10178
- *
10179
- * @author Vova Feldman (@svovaf)
10180
- * @since 1.2.1
10181
- *
10182
- * @param string $name Event name.
10183
- * @param array $properties Associative key/value array with primitive values only
10184
- * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s.
10185
- *
10186
- * @return object|false Event data or FALSE on failure.
10187
- *
10188
- * @throws \Freemius_InvalidArgumentException
10189
- *
10190
- * @user Freemius::track_event()
10191
- */
10192
- public function track_event_once( $name, $properties = array(), $process_at = false ) {
10193
- return $this->track_event( $name, $properties, $process_at, true );
10194
- }
10195
-
10196
- /**
10197
- * Plugin uninstall hook.
10198
- *
10199
- * @author Vova Feldman (@svovaf)
10200
- * @since 1.0.1
10201
- *
10202
- * @param bool $check_user Enforce checking if user have plugins activation privileges.
10203
- */
10204
- function _uninstall_plugin_event( $check_user = true ) {
10205
- $this->_logger->entrance( 'slug = ' . $this->_slug );
10206
-
10207
- if ( $check_user && ! current_user_can( 'activate_plugins' ) ) {
10208
- return;
10209
- }
10210
-
10211
- $params = array();
10212
- $uninstall_reason = null;
10213
- if ( isset( $this->_storage->uninstall_reason ) ) {
10214
- $uninstall_reason = $this->_storage->uninstall_reason;
10215
- $params['reason_id'] = $uninstall_reason->id;
10216
- $params['reason_info'] = $uninstall_reason->info;
10217
- }
10218
-
10219
- if ( ! $this->is_registered() ) {
10220
- // Send anonymous uninstall event only if user submitted a feedback.
10221
- if ( isset( $uninstall_reason ) ) {
10222
- if ( isset( $uninstall_reason->is_anonymous ) && ! $uninstall_reason->is_anonymous ) {
10223
- $this->opt_in( false, false, false, false, true );
10224
- } else {
10225
- $params['uid'] = $this->get_anonymous_id();
10226
- $this->get_api_plugin_scope()->call( 'uninstall.json', 'put', $params );
10227
- }
10228
- }
10229
- } else {
10230
- $params = array_merge( $params, array(
10231
- 'is_active' => false,
10232
- 'is_uninstalled' => true,
10233
- ) );
10234
-
10235
- if ( $this->_is_network_active ) {
10236
- // Send uninstall event.
10237
- $this->send_installs_update( $params );
10238
- } else {
10239
- // Send uninstall event.
10240
- $this->send_install_update( $params );
10241
- }
10242
- }
10243
-
10244
- // @todo Decide if we want to delete plugin information from db.
10245
- }
10246
-
10247
- /**
10248
- * Set the basename of the current product and hook _activate_plugin_event_hook() to the activation action.
10249
- *
10250
- * @author Vova Feldman (@svovaf)
10251
- * @since 2.2.1
10252
- *
10253
- * @param string $is_premium
10254
- * @param string $caller
10255
- *
10256
- * @return string
10257
- */
10258
- function set_basename( $is_premium, $caller ) {
10259
- $basename = plugin_basename( $caller );
10260
-
10261
- $current_basename = $is_premium ?
10262
- $this->_premium_plugin_basename :
10263
- $this->_free_plugin_basename;
10264
-
10265
- if ( $current_basename == $basename ) {
10266
- // Basename value set correctly.
10267
- return;
10268
- }
10269
-
10270
- if ( $is_premium ) {
10271
- $this->_premium_plugin_basename = $basename;
10272
- } else {
10273
- $this->_free_plugin_basename = $basename;
10274
- }
10275
-
10276
- $plugin_dir = dirname( $this->_plugin_dir_path ) . '/';
10277
-
10278
- register_activation_hook(
10279
- $plugin_dir . $basename,
10280
- array( &$this, '_activate_plugin_event_hook' )
10281
- );
10282
- }
10283
-
10284
- /**
10285
- * @author Vova Feldman (@svovaf)
10286
- * @since 1.1.1
10287
- * @since 2.2.1 If the context product is in its premium version, use the current module's basename, even if it was renamed.
10288
- *
10289
- * @return string
10290
- */
10291
- function premium_plugin_basename() {
10292
- if ( ! isset( $this->_premium_plugin_basename ) ) {
10293
- $this->_premium_plugin_basename = $this->is_premium() ?
10294
- // The product is premium, so use the current basename.
10295
- $this->_plugin_basename :
10296
- $this->get_premium_slug() . '/' . basename( $this->_free_plugin_basename );
10297
- }
10298
-
10299
- return $this->_premium_plugin_basename;
10300
- }
10301
-
10302
- /**
10303
- * Uninstall plugin hook. Called only when connected his account with Freemius for active sites tracking.
10304
- *
10305
- * @author Vova Feldman (@svovaf)
10306
- * @since 1.0.2
10307
- */
10308
- public static function _uninstall_plugin_hook() {
10309
- self::_load_required_static();
10310
-
10311
- self::$_static_logger->entrance();
10312
-
10313
- if ( ! current_user_can( 'activate_plugins' ) ) {
10314
- return;
10315
- }
10316
-
10317
- $plugin_file = substr( current_filter(), strlen( 'uninstall_' ) );
10318
-
10319
- self::$_static_logger->info( 'plugin = ' . $plugin_file );
10320
-
10321
- define( 'WP_FS__UNINSTALL_MODE', true );
10322
-
10323
- $fs = self::get_instance_by_file( $plugin_file );
10324
-
10325
- if ( is_object( $fs ) ) {
10326
- $fs->remove_sdk_reference();
10327
-
10328
- self::require_plugin_essentials();
10329
-
10330
- if ( is_plugin_active( $fs->_free_plugin_basename ) ||
10331
- is_plugin_active( $fs->premium_plugin_basename() )
10332
- ) {
10333
- // Deleting Free or Premium plugin version while the other version still installed.
10334
- return;
10335
- }
10336
-
10337
- if (
10338
- ! $fs->is_clone() &&
10339
- /**
10340
- * If there's a context install, run this method only when there's also a context user (e.g., when cloning a subsite of a multisite network into a single-site installation, it's possible for an install to be associated with a non-existing user entity; we want Freemius to be off in this case, while we are trying to recover the user).
10341
- *
10342
- * @author Leo Fajardo
10343
- */
10344
- ( ! is_object( $fs->_site ) || $fs->is_registered() )
10345
- ) {
10346
- $fs->_uninstall_plugin_event();
10347
- }
10348
-
10349
- $fs->do_action( 'after_uninstall' );
10350
- }
10351
- }
10352
-
10353
- #----------------------------------------------------------------------------------
10354
- #region Plugin Information
10355
- #----------------------------------------------------------------------------------
10356
-
10357
- /**
10358
- * Load WordPress core plugin.php essential module.
10359
- *
10360
- * @author Vova Feldman (@svovaf)
10361
- * @since 1.1.1
10362
- */
10363
- private static function require_plugin_essentials() {
10364
- if ( ! function_exists( 'get_plugins' ) ) {
10365
- self::$_static_logger->log( 'Including wp-admin/includes/plugin.php...' );
10366
-
10367
- require_once ABSPATH . 'wp-admin/includes/plugin.php';
10368
- }
10369
- }
10370
-
10371
- /**
10372
- * Load WordPress core pluggable.php module.
10373
- *
10374
- * @author Vova Feldman (@svovaf)
10375
- * @since 1.1.2
10376
- */
10377
- private static function require_pluggable_essentials() {
10378
- if ( ! function_exists( 'wp_get_current_user' ) ) {
10379
- require_once ABSPATH . 'wp-includes/pluggable.php';
10380
- }
10381
- }
10382
-
10383
- /**
10384
- * Return plugin data.
10385
- *
10386
- * @author Vova Feldman (@svovaf)
10387
- * @since 1.0.1
10388
- *
10389
- * @param bool $reparse_plugin_metadata
10390
- *
10391
- * @return array
10392
- */
10393
- function get_plugin_data( $reparse_plugin_metadata = false ) {
10394
- if ( ! isset( $this->_plugin_data ) || $reparse_plugin_metadata ) {
10395
- self::require_plugin_essentials();
10396
-
10397
- if ( $this->is_plugin() ) {
10398
- /**
10399
- * @author Vova Feldman (@svovaf)
10400
- * @since 1.2.0 When using get_plugin_data() do NOT translate plugin data.
10401
- *
10402
- * @link https://github.com/Freemius/wordpress-sdk/issues/77
10403
- */
10404
- $plugin_data = get_plugin_data(
10405
- $this->_plugin_main_file_path,
10406
- false,
10407
- false
10408
- );
10409
- } else {
10410
- $theme_data = wp_get_theme();
10411
-
10412
- if ( $this->_plugin_basename !== $theme_data->get_stylesheet() && is_child_theme() ) {
10413
- $parent_theme = $theme_data->parent();
10414
-
10415
- if ( ( $parent_theme instanceof WP_Theme ) && $this->_plugin_basename === $parent_theme->get_stylesheet() ) {
10416
- $theme_data = $parent_theme;
10417
- }
10418
- }
10419
-
10420
- $plugin_data = array(
10421
- 'Name' => $theme_data->get( 'Name' ),
10422
- 'Version' => $theme_data->get( 'Version' ),
10423
- 'Author' => $theme_data->get( 'Author' ),
10424
- 'Description' => $theme_data->get( 'Description' ),
10425
- 'PluginURI' => $theme_data->get( 'ThemeURI' ),
10426
- );
10427
- }
10428
-
10429
- $this->_plugin_data = $plugin_data;
10430
- }
10431
-
10432
- return $this->_plugin_data;
10433
- }
10434
-
10435
- /**
10436
- * @author Vova Feldman (@svovaf)
10437
- * @since 1.0.1
10438
- * @since 1.2.2.5 If slug not set load slug by module ID.
10439
- *
10440
- * @return string Plugin slug.
10441
- */
10442
- function get_slug() {
10443
- if ( ! isset( $this->_slug ) ) {
10444
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
10445
- $this->_slug = $id_slug_type_path_map[ $this->_module_id ]['slug'];
10446
- }
10447
-
10448
- return $this->_slug;
10449
- }
10450
-
10451
- /**
10452
- * @author Leo Fajardo (@leorw)
10453
- * @since 2.2.1
10454
- *
10455
- * @return string
10456
- */
10457
- function get_premium_slug() {
10458
- return is_object( $this->_plugin ) ?
10459
- $this->_plugin->premium_slug :
10460
- "{$this->_slug}-premium";
10461
- }
10462
-
10463
- /**
10464
- * Retrieve the desired folder name for the product.
10465
- *
10466
- * @author Vova Feldman (@svovaf)
10467
- * @since 1.2.1.7
10468
- *
10469
- * @return string Plugin slug.
10470
- */
10471
- function get_target_folder_name() {
10472
- return $this->can_use_premium_code() ?
10473
- $this->_plugin->premium_slug :
10474
- $this->_slug;
10475
- }
10476
-
10477
- /**
10478
- * @author Vova Feldman (@svovaf)
10479
- * @since 1.0.1
10480
- *
10481
- * @return number Plugin ID.
10482
- */
10483
- function get_id() {
10484
- return $this->_plugin->id;
10485
- }
10486
-
10487
- /**
10488
- * @author Leo Fajardo (@leorw)
10489
- * @since 2.2.4
10490
- *
10491
- * @return number|null Bundle ID.
10492
- */
10493
- function get_bundle_id() {
10494
- return ( isset( $this->_plugin->bundle_id ) && FS_Plugin::is_valid_id( $this->_plugin->bundle_id ) ) ?
10495
- $this->_plugin->bundle_id :
10496
- null;
10497
- }
10498
-
10499
- /**
10500
- * @author Vova Feldman (@svovaf)
10501
- * @since 2.3.1
10502
- *
10503
- * @return string|null Bundle public key.
10504
- */
10505
- function get_bundle_public_key() {
10506
- return isset( $this->_plugin->bundle_public_key ) ?
10507
- $this->_plugin->bundle_public_key :
10508
- null;
10509
- }
10510
-
10511
- /**
10512
- * @author Vova Feldman (@svovaf)
10513
- * @since 1.2.1.5
10514
- *
10515
- * @return string Freemius SDK version
10516
- */
10517
- function get_sdk_version() {
10518
- return $this->version;
10519
- }
10520
-
10521
- /**
10522
- * @author Vova Feldman (@svovaf)
10523
- * @since 1.2.1.5
10524
- *
10525
- * @return number Parent plugin ID (if parent exist).
10526
- */
10527
- function get_parent_id() {
10528
- return $this->is_addon() ?
10529
- $this->get_parent_instance()->get_id() :
10530
- $this->_plugin->id;
10531
- }
10532
-
10533
- /**
10534
- * @author Vova Feldman (@svovaf)
10535
- * @since 2.3.1
10536
- *
10537
- * @return string
10538
- */
10539
- function get_usage_tracking_terms_url() {
10540
- return $this->apply_filters(
10541
- 'usage_tracking_terms_url',
10542
- "https://freemius.com/wordpress/usage-tracking/{$this->_plugin->id}/{$this->_slug}/"
10543
- );
10544
- }
10545
-
10546
- /**
10547
- * @author Vova Feldman (@svovaf)
10548
- * @since 2.3.1
10549
- *
10550
- * @return string
10551
- */
10552
- function get_eula_url() {
10553
- return $this->apply_filters(
10554
- 'eula_url',
10555
- "https://freemius.com/terms/{$this->_plugin->id}/{$this->_slug}/"
10556
- );
10557
- }
10558
-
10559
- /**
10560
- * @author Vova Feldman (@svovaf)
10561
- * @since 1.0.1
10562
- *
10563
- * @return string Plugin public key.
10564
- */
10565
- function get_public_key() {
10566
- return $this->_plugin->public_key;
10567
- }
10568
-
10569
- /**
10570
- * Will be available only on sandbox mode.
10571
- *
10572
- * @author Vova Feldman (@svovaf)
10573
- * @since 1.0.4
10574
- *
10575
- * @return mixed Plugin secret key.
10576
- */
10577
- function get_secret_key() {
10578
- return $this->_plugin->secret_key;
10579
- }
10580
-
10581
- /**
10582
- * @author Vova Feldman (@svovaf)
10583
- * @since 1.1.1
10584
- *
10585
- * @return bool
10586
- */
10587
- function has_secret_key() {
10588
- return ! empty( $this->_plugin->secret_key );
10589
- }
10590
-
10591
- /**
10592
- * @author Vova Feldman (@svovaf)
10593
- * @since 1.0.9
10594
- *
10595
- * @param string|bool $premium_suffix
10596
- *
10597
- * @return string
10598
- */
10599
- function get_plugin_name( $premium_suffix = false ) {
10600
- $this->_logger->entrance();
10601
-
10602
- /**
10603
- * This `if-else` can be squeezed into a single `if` but I intentionally split it for code readability.
10604
- *
10605
- * @author Vova Feldman
10606
- */
10607
- if ( ! isset( $this->_plugin_name ) ) {
10608
- // Name is not yet set.
10609
- $this->set_name( $premium_suffix );
10610
- } else if (
10611
- ! empty( $premium_suffix ) &&
10612
- ( ! is_object( $this->_plugin ) || $this->_plugin->premium_suffix !== $premium_suffix )
10613
- ) {
10614
- // Name is already set, but there's a change in the premium suffix.
10615
- $this->set_name( $premium_suffix );
10616
- }
10617
-
10618
- return $this->_plugin_name;
10619
- }
10620
-
10621
- /**
10622
- * Calculates and stores the product's name. This helper function was created specifically for get_plugin_name() just to make the code clearer.
10623
- *
10624
- * @author Vova Feldman (@svovaf)
10625
- * @since 2.2.1
10626
- *
10627
- * @param string $premium_suffix
10628
- */
10629
- private function set_name( $premium_suffix = '' ) {
10630
- $plugin_data = $this->get_plugin_data();
10631
-
10632
- // Get name.
10633
- $this->_plugin_name = $plugin_data['Name'];
10634
-
10635
- if ( is_string( $premium_suffix ) ) {
10636
- $premium_suffix = trim( $premium_suffix );
10637
-
10638
- if ( ! empty( $premium_suffix ) ) {
10639
- // Check if plugin name contains " (premium)" or a custom suffix and remove it.
10640
- $suffix = ( ' ' . strtolower( $premium_suffix ) );
10641
- $suffix_len = strlen( $suffix );
10642
-
10643
- if ( strlen( $plugin_data['Name'] ) > $suffix_len &&
10644
- $suffix === substr( strtolower( $plugin_data['Name'] ), - $suffix_len )
10645
- ) {
10646
- $this->_plugin_name = substr( $plugin_data['Name'], 0, - $suffix_len );
10647
- }
10648
- }
10649
- }
10650
-
10651
- $this->_logger->departure( 'Name = ' . $this->_plugin_name );
10652
- }
10653
-
10654
- /**
10655
- * @author Vova Feldman (@svovaf)
10656
- * @since 1.0.0
10657
- *
10658
- * @param bool $reparse_plugin_metadata
10659
- *
10660
- * @return string
10661
- */
10662
- function get_plugin_version( $reparse_plugin_metadata = false ) {
10663
- $this->_logger->entrance();
10664
-
10665
- $plugin_data = $this->get_plugin_data( $reparse_plugin_metadata );
10666
-
10667
- $this->_logger->departure( 'Version = ' . $plugin_data['Version'] );
10668
-
10669
- return $this->apply_filters( 'plugin_version', $plugin_data['Version'] );
10670
- }
10671
-
10672
- /**
10673
- * @author Vova Feldman (@svovaf)
10674
- * @since 1.2.1.7
10675
- *
10676
- * @return string
10677
- */
10678
- function get_plugin_title() {
10679
- $this->_logger->entrance();
10680
-
10681
- $title = $this->_plugin->title;
10682
-
10683
- return $this->apply_filters( 'plugin_title', $title );
10684
- }
10685
-
10686
- /**
10687
- * @author Vova Feldman (@svovaf)
10688
- * @since 1.2.2.7
10689
- *
10690
- * @param bool $lowercase
10691
- *
10692
- * @return string
10693
- */
10694
- function get_module_label( $lowercase = false ) {
10695
- $label = $this->is_addon() ?
10696
- $this->get_text_inline( 'Add-On', 'addon' ) :
10697
- ( $this->is_plugin() ?
10698
- $this->get_text_inline( 'Plugin', 'plugin' ) :
10699
- $this->get_text_inline( 'Theme', 'theme' ) );
10700
-
10701
- if ( $lowercase ) {
10702
- $label = strtolower( $label );
10703
- }
10704
-
10705
- return $label;
10706
- }
10707
-
10708
- /**
10709
- * @author Vova Feldman (@svovaf)
10710
- * @since 1.0.4
10711
- *
10712
- * @return string
10713
- */
10714
- function get_plugin_basename() {
10715
- if ( ! isset( $this->_plugin_basename ) ) {
10716
- if ( $this->is_plugin() ) {
10717
- $this->_plugin_basename = plugin_basename( $this->_plugin_main_file_path );
10718
- } else {
10719
- $this->_plugin_basename = basename( dirname( $this->_plugin_main_file_path ) );
10720
- }
10721
- }
10722
-
10723
- return $this->_plugin_basename;
10724
- }
10725
-
10726
- function get_plugin_folder_name() {
10727
- $this->_logger->entrance();
10728
-
10729
- $plugin_folder = $this->_plugin_basename;
10730
-
10731
- while ( '.' !== dirname( $plugin_folder ) ) {
10732
- $plugin_folder = dirname( $plugin_folder );
10733
- }
10734
-
10735
- $this->_logger->departure( 'Folder Name = ' . $plugin_folder );
10736
-
10737
- return $plugin_folder;
10738
- }
10739
-
10740
- #endregion ------------------------------------------------------------------
10741
-
10742
- /* Account
10743
- ------------------------------------------------------------------------------------------------------------------*/
10744
-
10745
- /**
10746
- * Find plugin's slug by plugin's basename.
10747
- *
10748
- * @author Vova Feldman (@svovaf)
10749
- * @since 1.0.9
10750
- *
10751
- * @param string $plugin_base_name
10752
- *
10753
- * @return false|string
10754
- */
10755
- private static function find_slug_by_basename( $plugin_base_name ) {
10756
- $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
10757
-
10758
- if ( ! array( $file_slug_map ) || ! isset( $file_slug_map[ $plugin_base_name ] ) ) {
10759
- return false;
10760
- }
10761
-
10762
- return $file_slug_map[ $plugin_base_name ];
10763
- }
10764
-
10765
- /**
10766
- * Store the map between the plugin's basename to the slug.
10767
- *
10768
- * @author Vova Feldman (@svovaf)
10769
- * @since 1.0.9
10770
- */
10771
- private function store_file_slug_map() {
10772
- $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
10773
-
10774
- if ( ! array( $file_slug_map ) ) {
10775
- $file_slug_map = array();
10776
- }
10777
-
10778
- if ( ! isset( $file_slug_map[ $this->_plugin_basename ] ) ||
10779
- $file_slug_map[ $this->_plugin_basename ] !== $this->_slug
10780
- ) {
10781
- $file_slug_map[ $this->_plugin_basename ] = $this->_slug;
10782
- self::$_accounts->set_option( 'file_slug_map', $file_slug_map, true );
10783
- }
10784
- }
10785
-
10786
- /**
10787
- * @return array[number]FS_User
10788
- */
10789
- static function get_all_users() {
10790
- $users = self::maybe_get_entities_account_option( 'users', array() );
10791
-
10792
- if ( ! is_array( $users ) ) {
10793
- $users = array();
10794
- }
10795
-
10796
- return $users;
10797
- }
10798
-
10799
- /**
10800
- * @param string $module_type
10801
- * @param null|int $blog_id Since 2.0.0
10802
- *
10803
- * @return array[string]FS_Site
10804
- */
10805
- private static function get_all_sites(
10806
- $module_type = WP_FS__MODULE_TYPE_PLUGIN,
10807
- $blog_id = null,
10808
- $is_backup = false
10809
- ) {
10810
- $sites = self::get_account_option(
10811
- ( $is_backup ? 'prev_' : '' ) . 'sites',
10812
- $module_type,
10813
- $blog_id
10814
- );
10815
-
10816
- if ( ! is_array( $sites ) ) {
10817
- $sites = array();
10818
- }
10819
-
10820
- return $sites;
10821
- }
10822
-
10823
- /**
10824
- * @author Leo Fajardo (@leorw)
10825
- *
10826
- * @since 1.2.2
10827
- *
10828
- * @param string $option_name
10829
- * @param string $module_type
10830
- * @param null|int $network_level_or_blog_id Since 2.0.0
10831
- *
10832
- * @return mixed
10833
- */
10834
- private static function get_account_option( $option_name, $module_type = null, $network_level_or_blog_id = null ) {
10835
- if ( ! is_null( $module_type ) && WP_FS__MODULE_TYPE_PLUGIN !== $module_type ) {
10836
- $option_name = $module_type . '_' . $option_name;
10837
- }
10838
-
10839
- return self::maybe_get_entities_account_option( $option_name, array(), $network_level_or_blog_id );
10840
- }
10841
-
10842
- /**
10843
- * @author Leo Fajardo (@leorw)
10844
- *
10845
- * @since 1.2.2
10846
- *
10847
- * @param string $option_name
10848
- * @param mixed $option_value
10849
- * @param bool $store
10850
- * @param null|int $network_level_or_blog_id Since 2.0.0
10851
- */
10852
- private function set_account_option( $option_name, $option_value, $store, $network_level_or_blog_id = null ) {
10853
- self::set_account_option_by_module(
10854
- $this->_module_type,
10855
- $option_name,
10856
- $option_value,
10857
- $store,
10858
- $network_level_or_blog_id
10859
- );
10860
- }
10861
-
10862
- /**
10863
- * @author Vova Feldman (@svovaf)
10864
- *
10865
- * @since 1.2.2.7
10866
- *
10867
- * @param string $module_type
10868
- * @param string $option_name
10869
- * @param mixed $option_value
10870
- * @param bool $store
10871
- * @param null|int $network_level_or_blog_id Since 2.0.0
10872
- */
10873
- private static function set_account_option_by_module(
10874
- $module_type,
10875
- $option_name,
10876
- $option_value,
10877
- $store,
10878
- $network_level_or_blog_id = null
10879
- ) {
10880
- if ( WP_FS__MODULE_TYPE_PLUGIN != $module_type ) {
10881
- $option_name = $module_type . '_' . $option_name;
10882
- }
10883
-
10884
- self::$_accounts->set_option( $option_name, $option_value, $store, $network_level_or_blog_id );
10885
- }
10886
-
10887
- /**
10888
- * This method can also return non-entity or non-entities collection option like the `user_id_license_ids_map` option.
10889
- *
10890
- * @author Leo Fajardo (@leorw)
10891
- * @since 2.3.1
10892
- *
10893
- * @param string $option_name
10894
- * @param mixed $default
10895
- * @param null|bool|int $network_level_or_blog_id When an integer, use the given blog storage. When `true` use the multisite storage (if there's a network). When `false`, use the current context blog storage. When `null`, the decision which storage to use (MS vs. Current S) will be handled internally and determined based on the $option (based on self::$_SITE_LEVEL_PARAMS).
10896
- *
10897
- * @return mixed|FS_Plugin[]|FS_User[]|FS_Site[]|FS_Plugin_License[]|FS_Plugin_Plan[]|FS_Plugin_Tag[]
10898
- */
10899
- private static function maybe_get_entities_account_option( $option_name, $default = null, $network_level_or_blog_id = null ) {
10900
- $option = self::$_accounts->get_option( $option_name, $default, $network_level_or_blog_id );
10901
-
10902
- $class_name = '';
10903
-
10904
- if ( fs_starts_with( $option_name, WP_FS__MODULE_TYPE_THEME . '_' ) ) {
10905
- $option_name = str_replace( WP_FS__MODULE_TYPE_THEME . '_', '', $option_name );
10906
- }
10907
-
10908
- switch ( $option_name ) {
10909
- case 'plugins':
10910
- case 'themes':
10911
- case 'addons':
10912
- $class_name = FS_Plugin::get_class_name();
10913
- break;
10914
- case 'users':
10915
- $class_name = FS_User::get_class_name();
10916
- break;
10917
- case 'sites':
10918
- $class_name = FS_Site::get_class_name();
10919
- break;
10920
- case 'licenses':
10921
- case 'all_licenses':
10922
- $class_name = FS_Plugin_License::get_class_name();
10923
- break;
10924
- case 'plans':
10925
- $class_name = FS_Plugin_Plan::get_class_name();
10926
- break;
10927
- case 'updates':
10928
- $class_name = FS_Plugin_Tag::get_class_name();
10929
- break;
10930
- }
10931
-
10932
- if ( empty( $class_name ) ) {
10933
- return $option;
10934
- }
10935
-
10936
- return fs_get_entities( $option, $class_name );
10937
- }
10938
-
10939
- /**
10940
- * @author Vova Feldman (@svovaf)
10941
- * @since 1.0.6
10942
- *
10943
- * @param number|null $module_id
10944
- *
10945
- * @return FS_Plugin_License[]
10946
- */
10947
- private static function get_all_licenses( $module_id = null ) {
10948
- $licenses = self::get_account_option( 'all_licenses' );
10949
-
10950
- if ( ! is_array( $licenses ) ) {
10951
- $licenses = array();
10952
- }
10953
-
10954
- if ( is_null( $module_id ) ) {
10955
- return $licenses;
10956
- }
10957
-
10958
- $licenses = isset( $licenses[ $module_id ] ) ?
10959
- $licenses[ $module_id ] :
10960
- array();
10961
-
10962
- return $licenses;
10963
- }
10964
-
10965
- /**
10966
- * @author Leo Fajardo (@leorw)
10967
- * @since 2.0.0
10968
- *
10969
- * @return array
10970
- */
10971
- private static function get_all_licenses_by_module_type() {
10972
- $licenses = self::get_account_option( 'all_licenses' );
10973
-
10974
- $licenses_by_module_type = array(
10975
- WP_FS__MODULE_TYPE_PLUGIN => array(),
10976
- WP_FS__MODULE_TYPE_THEME => array()
10977
- );
10978
-
10979
- if ( ! is_array( $licenses ) ) {
10980
- return $licenses_by_module_type;
10981
- }
10982
-
10983
- foreach ( $licenses as $module_id => $module_licenses ) {
10984
- $fs = self::get_instance_by_id( $module_id );
10985
- if ( false === $fs ) {
10986
- continue;
10987
- }
10988
-
10989
- $licenses_by_module_type[ $fs->_module_type ] = array_merge( $licenses_by_module_type[ $fs->_module_type ], $module_licenses );
10990
- }
10991
-
10992
- return $licenses_by_module_type;
10993
- }
10994
-
10995
- /**
10996
- * @author Leo Fajardo (@leorw)
10997
- * @since 2.0.0
10998
- *
10999
- * @param number $module_id
11000
- * @param number|null $user_id
11001
- *
11002
- * @return array
11003
- */
11004
- private static function get_user_id_license_ids_map( $module_id, $user_id = null ) {
11005
- $all_modules_user_id_license_ids_map = self::get_account_option( 'user_id_license_ids_map' );
11006
-
11007
- if ( ! is_array( $all_modules_user_id_license_ids_map ) ) {
11008
- $all_modules_user_id_license_ids_map = array();
11009
- }
11010
-
11011
- $user_id_license_ids_map = isset( $all_modules_user_id_license_ids_map[ $module_id ] ) ?
11012
- $all_modules_user_id_license_ids_map[ $module_id ] :
11013
- array();
11014
-
11015
- if ( FS_User::is_valid_id( $user_id ) ) {
11016
- $user_id_license_ids_map = isset( $user_id_license_ids_map[ $user_id ] ) ?
11017
- $user_id_license_ids_map[ $user_id ] :
11018
- array();
11019
- }
11020
-
11021
- return $user_id_license_ids_map;
11022
- }
11023
-
11024
- /**
11025
- * @author Leo Fajardo (@leorw)
11026
- * @since 2.0.0
11027
- *
11028
- * @param array $new_user_id_license_ids_map
11029
- * @param number $module_id
11030
- * @param number|null $user_id
11031
- */
11032
- private static function store_user_id_license_ids_map( $new_user_id_license_ids_map, $module_id, $user_id = null ) {
11033
- $all_modules_user_id_license_ids_map = self::get_account_option( 'user_id_license_ids_map' );
11034
- if ( ! is_array( $all_modules_user_id_license_ids_map ) ) {
11035
- $all_modules_user_id_license_ids_map = array();
11036
- }
11037
-
11038
- if ( ! isset( $all_modules_user_id_license_ids_map[ $module_id ] ) ) {
11039
- $all_modules_user_id_license_ids_map[ $module_id ] = array();
11040
- }
11041
-
11042
- if ( FS_User::is_valid_id( $user_id ) ) {
11043
- $all_modules_user_id_license_ids_map[ $module_id ][ $user_id ] = $new_user_id_license_ids_map;
11044
- } else {
11045
- $all_modules_user_id_license_ids_map[ $module_id ] = $new_user_id_license_ids_map;
11046
- }
11047
-
11048
- self::$_accounts->set_option( 'user_id_license_ids_map', $all_modules_user_id_license_ids_map, true );
11049
- }
11050
-
11051
- /**
11052
- * Get a collection of the user's linked license IDs.
11053
- *
11054
- * @author Vova Feldman (@svovaf)
11055
- * @since 2.0.0
11056
- *
11057
- * @param number $user_id
11058
- *
11059
- * @return number[]
11060
- */
11061
- private function get_user_linked_license_ids( $user_id ) {
11062
- return self::get_user_id_license_ids_map( $this->_module_id, $user_id );
11063
- }
11064
-
11065
- /**
11066
- * Override the user's linked license IDs with a new IDs collection.
11067
- *
11068
- * @author Vova Feldman (@svovaf)
11069
- * @since 2.0.0
11070
- *
11071
- * @param number $user_id
11072
- * @param number[] $license_ids
11073
- */
11074
- private function set_user_linked_license_ids( $user_id, array $license_ids ) {
11075
- self::store_user_id_license_ids_map( $license_ids, $this->_module_id, $user_id );
11076
- }
11077
-
11078
- /**
11079
- * Link a specified license ID to a given user.
11080
- *
11081
- * @author Vova Feldman (@svovaf)
11082
- * @since 2.0.0
11083
- *
11084
- * @param number $license_id
11085
- * @param number $user_id
11086
- */
11087
- private function link_license_2_user( $license_id, $user_id ) {
11088
- $license_ids = $this->get_user_linked_license_ids( $user_id );
11089
-
11090
- if ( in_array( $license_id, $license_ids ) ) {
11091
- // License already linked.
11092
- return;
11093
- }
11094
-
11095
- $license_ids[] = $license_id;
11096
-
11097
- $this->set_user_linked_license_ids( $user_id, $license_ids );
11098
- }
11099
-
11100
- /**
11101
- * @param string|bool $module_type
11102
- *
11103
- * @return FS_Plugin_Plan[]
11104
- */
11105
- private static function get_all_plans( $module_type = false ) {
11106
- $plans = self::get_account_option( 'plans', $module_type );
11107
-
11108
- if ( ! is_array( $plans ) ) {
11109
- $plans = array();
11110
- }
11111
-
11112
- return $plans;
11113
- }
11114
-
11115
- /**
11116
- * @author Vova Feldman (@svovaf)
11117
- * @since 1.0.4
11118
- *
11119
- * @return FS_Plugin_Tag[]
11120
- */
11121
- private static function get_all_updates() {
11122
- $updates = self::maybe_get_entities_account_option( 'updates', array() );
11123
-
11124
- if ( ! is_array( $updates ) ) {
11125
- $updates = array();
11126
- }
11127
-
11128
- return $updates;
11129
- }
11130
-
11131
- /**
11132
- * @author Vova Feldman (@svovaf)
11133
- * @since 1.0.6
11134
- *
11135
- * @return array<number,FS_Plugin[]>|false
11136
- */
11137
- private static function get_all_addons() {
11138
- $addons = self::maybe_get_entities_account_option( 'addons', array() );
11139
-
11140
- if ( ! is_array( $addons ) ) {
11141
- $addons = array();
11142
- }
11143
-
11144
- return $addons;
11145
- }
11146
-
11147
- /**
11148
- * @author Vova Feldman (@svovaf)
11149
- * @since 1.0.6
11150
- *
11151
- * @return number[]|false
11152
- */
11153
- private static function get_all_account_addons() {
11154
- $addons = self::$_accounts->get_option( 'account_addons', array() );
11155
-
11156
- if ( ! is_array( $addons ) ) {
11157
- $addons = array();
11158
- }
11159
-
11160
- return $addons;
11161
- }
11162
-
11163
- /**
11164
- * Check if user has connected his account (opted-in).
11165
- *
11166
- * Note:
11167
- * If the user opted-in and opted-out on a later stage,
11168
- * this will still return true. If you want to check if the
11169
- * user is currently opted-in, use:
11170
- * `$fs->is_registered() && $fs->is_tracking_allowed()`
11171
- *
11172
- * @author Vova Feldman (@svovaf)
11173
- * @since 1.0.1
11174
- * @return bool
11175
- */
11176
- function is_registered() {
11177
- return is_object( $this->_user );
11178
- }
11179
-
11180
- /**
11181
- * Returns TRUE if the user opted-in and didn't disconnect (opt-out).
11182
- *
11183
- * @author Leo Fajardo (@leorw)
11184
- * @since 1.2.1.5
11185
- *
11186
- * @return bool
11187
- */
11188
- function is_tracking_allowed() {
11189
- return ( is_object( $this->_site ) && $this->_site->is_tracking_allowed() );
11190
- }
11191
-
11192
- /**
11193
- * @author Leo Fajardo (@leorw)
11194
- * @since 2.4.0
11195
- *
11196
- * @return bool
11197
- */
11198
- function is_bundle_license_auto_activation_enabled() {
11199
- return $this->is_addon() ?
11200
- ( is_object( $this->_parent ) && $this->_parent->is_bundle_license_auto_activation_enabled() ) :
11201
- $this->_is_bundle_license_auto_activation_enabled;
11202
- }
11203
-
11204
- /**
11205
- * @author Vova Feldman (@svovaf)
11206
- * @since 1.0.4
11207
- *
11208
- * @return FS_Plugin
11209
- */
11210
- function get_plugin() {
11211
- return $this->_plugin;
11212
- }
11213
-
11214
- /**
11215
- * @author Vova Feldman (@svovaf)
11216
- * @since 1.0.3
11217
- *
11218
- * @return FS_User
11219
- */
11220
- function get_user() {
11221
- return $this->_user;
11222
- }
11223
-
11224
- /**
11225
- * @author Vova Feldman (@svovaf)
11226
- * @since 1.0.3
11227
- *
11228
- * @return FS_Site
11229
- */
11230
- function get_site() {
11231
- return $this->_site;
11232
- }
11233
-
11234
- /**
11235
- * @author Leo Fajardo (@leorw)
11236
- * @since 2.5.0
11237
- */
11238
- function store_site( $site ) {
11239
- $this->_site = $site;
11240
- $this->_store_site( true );
11241
- }
11242
-
11243
- /**
11244
- * Deletes the current install with an option to back it up in case restoration will be needed (e.g., if the automatic clone resolution attempt fails).
11245
- *
11246
- * @author Leo Fajardo (@leorw)
11247
- * @since 2.5.0
11248
- */
11249
- function delete_current_install( $back_up ) {
11250
- // Back up and delete the unique ID.
11251
- if ( $back_up ) {
11252
- self::$_accounts->set_option( 'prev_unique_id', $this->get_anonymous_id() );
11253
- }
11254
-
11255
- self::$_accounts->set_option( 'unique_id', null );
11256
-
11257
- if ( $back_up ) {
11258
- // Back up the install before deleting it so that it can be restored later on if necessary (e.g., if the automatic clone resolution attempt fails).
11259
- $this->back_up_site();
11260
- }
11261
-
11262
- $this->_delete_site();
11263
- $this->_site = null;
11264
- }
11265
-
11266
- /**
11267
- * @author Leo Fajardo (@leorw)
11268
- * @since 2.5.0
11269
- */
11270
- function restore_backup_site() {
11271
- self::$_accounts->set_option(
11272
- 'unique_id',
11273
- self::$_accounts->get_option( 'prev_unique_id' )
11274
- );
11275
-
11276
- $sites = self::get_all_sites( $this->_module_type, null, true );
11277
- $this->store_site( clone $sites[ $this->_slug ] );
11278
- }
11279
-
11280
- /**
11281
- * Get plugin add-ons.
11282
- *
11283
- * @author Vova Feldman (@svovaf)
11284
- * @since 1.0.6
11285
- *
11286
- * @since 1.1.7.3 If not yet loaded, fetch data from the API.
11287
- *
11288
- * @param bool $flush
11289
- *
11290
- * @return FS_Plugin[]|false
11291
- */
11292
- function get_addons( $flush = false ) {
11293
- $this->_logger->entrance();
11294
-
11295
- if ( ! $this->_has_addons ) {
11296
- return false;
11297
- }
11298
-
11299
- $addons = $this->sync_addons( $flush );
11300
-
11301
- return ( ! is_array( $addons ) || empty( $addons ) ) ?
11302
- false :
11303
- $addons;
11304
- }
11305
-
11306
- /**
11307
- * @author Vova Feldman (@svovaf)
11308
- * @since 1.0.6
11309
- *
11310
- * @return number[]|false
11311
- */
11312
- function get_account_addons() {
11313
- $this->_logger->entrance();
11314
-
11315
- $addons = self::get_all_account_addons();
11316
-
11317
- if ( ! is_array( $addons ) ||
11318
- ! isset( $addons[ $this->_plugin->id ] ) ||
11319
- ! is_array( $addons[ $this->_plugin->id ] ) ||
11320
- 0 === count( $addons[ $this->_plugin->id ] )
11321
- ) {
11322
- return false;
11323
- }
11324
-
11325
- return $addons[ $this->_plugin->id ];
11326
- }
11327
-
11328
- /**
11329
- * Check if user has any
11330
- *
11331
- * @author Vova Feldman (@svovaf)
11332
- * @since 1.1.6
11333
- *
11334
- * @return bool
11335
- */
11336
- function has_account_addons() {
11337
- $addons = $this->get_account_addons();
11338
-
11339
- return is_array( $addons ) && ( 0 < count( $addons ) );
11340
- }
11341
-
11342
-
11343
- /**
11344
- * Get add-on by ID (from local data).
11345
- *
11346
- * @author Vova Feldman (@svovaf)
11347
- * @since 1.0.6
11348
- *
11349
- * @param number $id
11350
- *
11351
- * @return FS_Plugin|false
11352
- */
11353
- function get_addon( $id ) {
11354
- $this->_logger->entrance();
11355
-
11356
- $addons = $this->get_addons();
11357
-
11358
- if ( is_array( $addons ) ) {
11359
- foreach ( $addons as $addon ) {
11360
- if ( $id == $addon->id ) {
11361
- return $addon;
11362
- }
11363
- }
11364
- }
11365
-
11366
- return false;
11367
- }
11368
-
11369
- /**
11370
- * Get add-on by slug (from local data).
11371
- *
11372
- * @author Vova Feldman (@svovaf)
11373
- * @since 1.0.6
11374
- *
11375
- * @param string $slug
11376
- *
11377
- * @param bool $flush
11378
- *
11379
- * @return FS_Plugin|false
11380
- */
11381
- function get_addon_by_slug( $slug, $flush = false ) {
11382
- $this->_logger->entrance();
11383
-
11384
- $addons = $this->get_addons( $flush );
11385
-
11386
- if ( is_array( $addons ) ) {
11387
- foreach ( $addons as $addon ) {
11388
- if ( $slug === $addon->slug ) {
11389
- return $addon;
11390
- }
11391
- }
11392
- }
11393
-
11394
- return false;
11395
- }
11396
-
11397
- /**
11398
- * @var array<number,object[]> {
11399
- * @key number Add-on ID.
11400
- * @val object[] The add-on's plans and prices object.
11401
- * }
11402
- */
11403
- private $plans_and_pricing_by_addon_id;
11404
-
11405
- /**
11406
- * @author Leo Fajardo (@leorw)
11407
- * @since 2.3.0
11408
- *
11409
- * @return array<number,object[]> {
11410
- * @key number Add-on ID.
11411
- * @val object[] The add-on's plans and prices object.
11412
- * }
11413
- */
11414
- function _get_addons_plans_and_pricing_map_by_id() {
11415
- if ( ! isset( $this->plans_and_pricing_by_addon_id ) ) {
11416
- $result = $this->get_api_plugin_scope()->get( $this->add_show_pending( "/addons/pricing.json?type=visible" ) );
11417
-
11418
- $plans_and_pricing_by_addon_id = array();
11419
- if ( $this->is_api_result_object( $result, 'addons' ) ) {
11420
- foreach ( $result->addons as $addon ) {
11421
- $plans_and_pricing_by_addon_id[ $addon->id ] = $addon->plans;
11422
- }
11423
- }
11424
-
11425
- $this->plans_and_pricing_by_addon_id = $plans_and_pricing_by_addon_id;
11426
- }
11427
-
11428
- return $this->plans_and_pricing_by_addon_id;
11429
- }
11430
-
11431
- /**
11432
- * @author Leo Fajardo (@leorw)
11433
- * @since 2.3.0
11434
- *
11435
- * @param number $addon_id
11436
- * @param bool $is_installed
11437
- *
11438
- * @return array
11439
- */
11440
- function _get_addon_info( $addon_id, $is_installed ) {
11441
- $addon = $this->get_addon( $addon_id );
11442
-
11443
- if ( ! is_object( $addon ) ) {
11444
- // Unexpected call.
11445
- return array();
11446
- }
11447
-
11448
- $slug = $addon->slug;
11449
-
11450
- $addon_storage = FS_Storage::instance( WP_FS__MODULE_TYPE_PLUGIN, $slug );
11451
-
11452
- if ( ! fs_is_network_admin() ) {
11453
- // Get blog-level activated installations.
11454
- $sites = self::maybe_get_entities_account_option( 'sites', array() );
11455
- } else {
11456
- $sites = null;
11457
-
11458
- if ( $this->is_addon_activated( $addon_id ) &&
11459
- $this->get_addon_instance( $addon_id )->is_network_active()
11460
- ) {
11461
- if ( FS_Site::is_valid_id( $addon_storage->network_install_blog_id ) ) {
11462
- // Get network-level activated installations.
11463
- $sites = self::maybe_get_entities_account_option(
11464
- 'sites',
11465
- array(),
11466
- $addon_storage->network_install_blog_id
11467
- );
11468
- }
11469
- }
11470
- }
11471
-
11472
- $addon_info = array(
11473
- 'is_connected' => false,
11474
- 'slug' => $slug,
11475
- 'title' => $addon->title,
11476
- 'is_whitelabeled' => $addon_storage->is_whitelabeled
11477
- );
11478
-
11479
- if ( ! $is_installed ) {
11480
- $plans_and_pricing_by_addon_id = $this->_get_addons_plans_and_pricing_map_by_id();
11481
-
11482
- if ( isset( $plans_and_pricing_by_addon_id[ $addon_id ] ) ) {
11483
- $has_paid_plan = false;
11484
- $plans = $plans_and_pricing_by_addon_id[ $addon_id ];
11485
-
11486
- if ( is_array( $plans ) && count( $plans ) > 0 ) {
11487
- foreach ( $plans as $plan ) {
11488
- if ( isset( $plan->pricing ) &&
11489
- is_array( $plan->pricing ) &&
11490
- count( $plan->pricing ) > 0
11491
- ) {
11492
- $has_paid_plan = true;
11493
- break;
11494
- }
11495
- }
11496
- }
11497
-
11498
- $addon_info['has_paid_plan'] = $has_paid_plan;
11499
- }
11500
- }
11501
-
11502
- if ( ! is_array( $sites ) || ! isset( $sites[ $slug ] ) ) {
11503
- return $addon_info;
11504
- }
11505
-
11506
- $site = $sites[ $slug ];
11507
-
11508
- $addon_info['is_connected'] = (
11509
- ( $addon->parent_plugin_id == $this->get_id() ) &&
11510
- is_object( $site ) &&
11511
- FS_Site::is_valid_id( $site->id ) &&
11512
- FS_User::is_valid_id( $site->user_id ) &&
11513
- FS_Plugin_Plan::is_valid_id( $site->plan_id )
11514
- );
11515
-
11516
- if ( $addon_info['is_connected'] && $is_installed ) {
11517
- return $addon_info;
11518
- }
11519
-
11520
- $addon_info['site'] = $site;
11521
-
11522
- $plugins_data = self::maybe_get_entities_account_option( WP_FS__MODULE_TYPE_PLUGIN . 's', array() );
11523
- if ( isset( $plugins_data[ $slug ] ) ) {
11524
- $plugin_data = $plugins_data[ $slug ];
11525
-
11526
- $addon_info['version'] = $plugin_data->version;
11527
- }
11528
-
11529
- $all_plans = self::maybe_get_entities_account_option( 'plans', array() );
11530
- if ( isset( $all_plans[ $slug ] ) ) {
11531
- $plans = $all_plans[ $slug ];
11532
-
11533
- foreach ( $plans as $plan ) {
11534
- if ( $site->plan_id == Freemius::_decrypt( $plan->id ) ) {
11535
- $addon_info['plan_name'] = Freemius::_decrypt( $plan->name );
11536
- $addon_info['plan_title'] = Freemius::_decrypt( $plan->title );
11537
- break;
11538
- }
11539
- }
11540
- }
11541
-
11542
- $licenses = self::maybe_get_entities_account_option( 'all_licenses', array() );
11543
- if ( is_array( $licenses ) && isset( $licenses[ $addon_id ] ) ) {
11544
- foreach ( $licenses[ $addon_id ] as $license ) {
11545
- if ( $license->id == $site->license_id ) {
11546
- $addon_info['license'] = $license;
11547
- break;
11548
- }
11549
- }
11550
- }
11551
-
11552
- if ( isset( $addon_info['license'] ) ) {
11553
- if ( isset( $addon_storage->subscriptions ) &&
11554
- ! empty( $addon_storage->subscriptions )
11555
- ) {
11556
- $addon_subscriptions = fs_get_entities( $addon_storage->subscriptions, FS_Subscription::get_class_name() );
11557
-
11558
- foreach ( $addon_subscriptions as $subscription ) {
11559
- if ( $subscription->license_id == $site->license_id ) {
11560
- $addon_info['subscription'] = $subscription;
11561
- break;
11562
- }
11563
- }
11564
- }
11565
- }
11566
-
11567
- return $addon_info;
11568
- }
11569
-
11570
- /**
11571
- * @author Vova Feldman (@svovaf)
11572
- * @since 2.0.0
11573
- *
11574
- * @param number $user_id
11575
- *
11576
- * @return FS_User
11577
- */
11578
- static function _get_user_by_id( $user_id ) {
11579
- self::$_static_logger->entrance( "user_id = {$user_id}" );
11580
-
11581
- $users = self::get_all_users();
11582
-
11583
- if ( is_array( $users ) ) {
11584
- if ( isset( $users[ $user_id ] ) &&
11585
- $users[ $user_id ] instanceof FS_User &&
11586
- $user_id == $users[ $user_id ]->id
11587
- ) {
11588
- return $users[ $user_id ];
11589
- }
11590
-
11591
- // If user wasn't found by the key, iterate over all the users collection.
11592
- foreach ( $users as $user ) {
11593
- /**
11594
- * @var FS_User $user
11595
- */
11596
- if ( $user_id == $user->id ) {
11597
- return $user;
11598
- }
11599
- }
11600
- }
11601
-
11602
- return null;
11603
- }
11604
-
11605
- /**
11606
- * Checks if a Freemius user_id is associated with a super-admin.
11607
- *
11608
- * @author Vova Feldman (@svovaf)
11609
- * @since 2.0.0
11610
- *
11611
- * @param number $user_id
11612
- *
11613
- * @return bool
11614
- */
11615
- private static function is_super_admin( $user_id ) {
11616
- $is_super_admin = false;
11617
-
11618
- $user = self::_get_user_by_id( $user_id );
11619
-
11620
- if ( $user instanceof FS_User && ! empty( $user->email ) ) {
11621
- self::require_pluggable_essentials();
11622
-
11623
- $wp_user = get_user_by( 'email', $user->email );
11624
-
11625
- if ( $wp_user instanceof WP_User ) {
11626
- $super_admins = get_super_admins();
11627
- $is_super_admin = ( is_array( $super_admins ) && in_array( $wp_user->user_login, $super_admins ) );
11628
- }
11629
- }
11630
-
11631
- return $is_super_admin;
11632
- }
11633
-
11634
- #----------------------------------------------------------------------------------
11635
- #region Plans & Licensing
11636
- #----------------------------------------------------------------------------------
11637
-
11638
- /**
11639
- * Check if running premium plugin code.
11640
- *
11641
- * @author Vova Feldman (@svovaf)
11642
- * @since 1.0.5
11643
- *
11644
- * @return bool
11645
- */
11646
- function is_premium() {
11647
- /**
11648
- * `$this->_plugin` will be `false` when `is_activation_mode` calls this method directly from the
11649
- * `register_constructor_hooks` method.
11650
- *
11651
- * @author Leo Fajardo (@leorw)
11652
- * @since 2.2.3
11653
- */
11654
- return is_object( $this->_plugin ) ?
11655
- $this->_plugin->is_premium :
11656
- false;
11657
- }
11658
-
11659
- /**
11660
- * Get site's plan ID.
11661
- *
11662
- * @author Vova Feldman (@svovaf)
11663
- * @since 1.0.2
11664
- *
11665
- * @return number
11666
- */
11667
- function get_plan_id() {
11668
- return $this->_site->plan_id;
11669
- }
11670
-
11671
- /**
11672
- * Get site's plan title.
11673
- *
11674
- * @author Vova Feldman (@svovaf)
11675
- * @since 1.0.2
11676
- *
11677
- * @return string
11678
- */
11679
- function get_plan_title() {
11680
- $plan = $this->get_plan();
11681
-
11682
- return is_object( $plan ) ? $plan->title : 'PLAN_TITLE';
11683
- }
11684
-
11685
- /**
11686
- * Get site's plan name.
11687
- *
11688
- * @author Vova Feldman (@svovaf)
11689
- * @since 2.0.0
11690
- *
11691
- * @return string
11692
- */
11693
- function get_plan_name() {
11694
- $plan = $this->get_plan();
11695
-
11696
- return is_object( $plan ) ? $plan->name : 'PLAN_NAME';
11697
- }
11698
-
11699
- /**
11700
- * @author Vova Feldman (@svovaf)
11701
- * @since 1.0.9
11702
- *
11703
- * @return FS_Plugin_Plan|false
11704
- */
11705
- function get_plan() {
11706
- if ( ! is_object( $this->_site ) ) {
11707
- return false;
11708
- }
11709
-
11710
- return FS_Plugin_Plan::is_valid_id( $this->_site->plan_id ) ?
11711
- $this->_get_plan_by_id( $this->_site->plan_id ) :
11712
- false;
11713
- }
11714
-
11715
- /**
11716
- * @author Vova Feldman (@svovaf)
11717
- * @since 1.0.3
11718
- *
11719
- * @return bool
11720
- */
11721
- function is_trial() {
11722
- $this->_logger->entrance();
11723
-
11724
- if ( ! $this->is_registered() || ! is_object( $this->_site ) ) {
11725
- return false;
11726
- }
11727
-
11728
- return $this->_site->is_trial();
11729
- }
11730
-
11731
- /**
11732
- * Check if currently in a trial with payment method (credit card or paypal).
11733
- *
11734
- * @author Vova Feldman (@svovaf)
11735
- * @since 1.1.7
11736
- *
11737
- * @return bool
11738
- */
11739
- function is_paid_trial() {
11740
- $this->_logger->entrance();
11741
-
11742
- if ( ! $this->is_trial() ) {
11743
- return false;
11744
- }
11745
-
11746
- if ( ! $this->has_active_valid_license() ) {
11747
- return false;
11748
- }
11749
-
11750
- if ( $this->_site->trial_plan_id != $this->_license->plan_id ) {
11751
- return false;
11752
- }
11753
-
11754
- /**
11755
- * @var FS_Subscription $subscription
11756
- */
11757
- $subscription = $this->_get_subscription( $this->_license->id );
11758
-
11759
- return ( is_object( $subscription ) && $subscription->is_active() );
11760
- }
11761
-
11762
- /**
11763
- * Check if trial already utilized.
11764
- *
11765
- * @since 1.0.9
11766
- *
11767
- * @return bool
11768
- */
11769
- function is_trial_utilized() {
11770
- $this->_logger->entrance();
11771
-
11772
- if ( ! $this->is_registered() ) {
11773
- return false;
11774
- }
11775
-
11776
- return $this->_site->is_trial_utilized();
11777
- }
11778
-
11779
- /**
11780
- * Get trial plan information (if in trial).
11781
- *
11782
- * @author Vova Feldman (@svovaf)
11783
- * @since 1.0.9
11784
- *
11785
- * @return bool|FS_Plugin_Plan
11786
- */
11787
- function get_trial_plan() {
11788
- $this->_logger->entrance();
11789
-
11790
- if ( ! $this->is_trial() ) {
11791
- return false;
11792
- }
11793
-
11794
- // Try to load plan from local cache.
11795
- $trial_plan = $this->_get_plan_by_id( $this->_site->trial_plan_id );
11796
-
11797
- if ( ! is_object( $trial_plan ) ) {
11798
- $trial_plan = $this->_fetch_site_plan( $this->_site->trial_plan_id );
11799
-
11800
- /**
11801
- * If managed to fetch the plan, add it to the plans collection.
11802
- */
11803
- if ( $trial_plan instanceof FS_Plugin_Plan ) {
11804
- if ( ! is_array( $this->_plans ) ) {
11805
- $this->_plans = array();
11806
- }
11807
-
11808
- $this->_plans[] = $trial_plan;
11809
- $this->_store_plans();
11810
- }
11811
- }
11812
-
11813
- if ( $trial_plan instanceof FS_Plugin_Plan ) {
11814
- return $trial_plan;
11815
- }
11816
-
11817
- /**
11818
- * If for some reason failed to get the trial plan, fallback to a dummy name and title.
11819
- */
11820
- $trial_plan = new FS_Plugin_Plan();
11821
- $trial_plan->id = $this->_site->trial_plan_id;
11822
- $trial_plan->name = 'pro';
11823
- $trial_plan->title = 'Pro';
11824
-
11825
- return $trial_plan;
11826
- }
11827
-
11828
- /**
11829
- * Check if the user has an activate, non-expired license on current plugin's install.
11830
- *
11831
- * @since 1.0.9
11832
- *
11833
- * @return bool
11834
- */
11835
- function is_paying() {
11836
- $this->_logger->entrance();
11837
-
11838
- if ( ! $this->is_registered() ) {
11839
- return false;
11840
- }
11841
-
11842
- if ( ! $this->has_paid_plan() ) {
11843
- return false;
11844
- }
11845
-
11846
- return (
11847
- ! $this->is_trial() &&
11848
- 'free' !== $this->get_plan_name() &&
11849
- $this->has_active_valid_license()
11850
- );
11851
- }
11852
-
11853
- /**
11854
- * @author Vova Feldman (@svovaf)
11855
- * @since 1.0.4
11856
- *
11857
- * @return bool
11858
- */
11859
- function is_free_plan() {
11860
- if ( ! $this->is_registered() ) {
11861
- return true;
11862
- }
11863
-
11864
- if ( ! $this->has_paid_plan() ) {
11865
- return true;
11866
- }
11867
-
11868
- return (
11869
- 'free' === $this->get_plan_name() ||
11870
- ! $this->has_features_enabled_license()
11871
- );
11872
- }
11873
-
11874
- /**
11875
- * @author Vova Feldman (@svovaf)
11876
- * @since 1.0.5
11877
- *
11878
- * @return bool
11879
- */
11880
- function _has_premium_license() {
11881
- $this->_logger->entrance();
11882
-
11883
- $premium_license = $this->_get_available_premium_license();
11884
-
11885
- return ( false !== $premium_license );
11886
- }
11887
-
11888
- /**
11889
- * Check if user has any licenses associated with the plugin (including expired or blocking).
11890
- *
11891
- * @author Vova Feldman (@svovaf)
11892
- * @since 1.1.7.3
11893
- *
11894
- * @param bool $including_foreign
11895
- *
11896
- * @return bool
11897
- */
11898
- function has_any_license( $including_foreign = true ) {
11899
- if ( ! is_array( $this->_licenses ) || 0 === count( $this->_licenses ) ) {
11900
- return false;
11901
- }
11902
-
11903
- if ( $including_foreign ) {
11904
- return true;
11905
- }
11906
-
11907
- foreach ( $this->_licenses as $license ) {
11908
- if ( $this->_user->id == $license->user_id ) {
11909
- return true;
11910
- }
11911
- }
11912
-
11913
- return false;
11914
- }
11915
-
11916
- /**
11917
- * @author Vova Feldman (@svovaf)
11918
- * @since 1.0.5
11919
- *
11920
- * @param bool|null $is_localhost
11921
- *
11922
- * @return FS_Plugin_License|false
11923
- */
11924
- function _get_available_premium_license( $is_localhost = null ) {
11925
- $this->_logger->entrance();
11926
-
11927
- $licenses = $this->get_available_premium_licenses( $is_localhost );
11928
- if ( ! empty( $licenses ) ) {
11929
- return $licenses[0];
11930
- }
11931
-
11932
- return false;
11933
- }
11934
-
11935
- /**
11936
- * @author Vova Feldman (@svovaf)
11937
- * @since 1.0.5
11938
- *
11939
- * @param bool|null $is_localhost
11940
- *
11941
- * @return FS_Plugin_License[]
11942
- */
11943
- function get_available_premium_licenses( $is_localhost = null ) {
11944
- $this->_logger->entrance();
11945
-
11946
- $licenses = array();
11947
- if ( ! $this->has_paid_plan() ) {
11948
- return $licenses;
11949
- }
11950
-
11951
- if ( is_array( $this->_licenses ) ) {
11952
- foreach ( $this->_licenses as $license ) {
11953
- if ( ! $license->can_activate( $is_localhost ) ) {
11954
- continue;
11955
- }
11956
-
11957
- $licenses[] = $license;
11958
- }
11959
- }
11960
-
11961
- return $licenses;
11962
- }
11963
-
11964
- /**
11965
- * Sync local plugin plans with remote server.
11966
- *
11967
- * IMPORTANT: If for some reason a site is associated with deleted plan, we'll preserve the plan's information and append it as the last plan. This means that if plan is deleted, the is_plan() method will ALWAYS return true for any given argument (it becomes the most inclusive plan).
11968
- *
11969
- * @author Vova Feldman (@svovaf)
11970
- * @since 1.0.5
11971
- *
11972
- * @return FS_Plugin_Plan[]|object
11973
- */
11974
- function _sync_plans() {
11975
- $plans = $this->_fetch_plugin_plans();
11976
-
11977
- if ( $this->is_array_instanceof( $plans, 'FS_Plugin_Plan' ) ) {
11978
- $plans_map = array();
11979
- foreach ( $plans as $plan ) {
11980
- $plans_map[ $plan->id ] = true;
11981
- }
11982
-
11983
- $plans_ids_to_keep = $this->get_plans_ids_associated_with_installs();
11984
-
11985
- foreach ( $plans_ids_to_keep as $plan_id ) {
11986
- if ( isset( $plans_map[ $plan_id ] ) ) {
11987
- continue;
11988
- }
11989
-
11990
- $missing_plan = self::_get_plan_by_id( $plan_id );
11991
-
11992
- if ( is_object( $missing_plan ) ) {
11993
- $plans[] = $missing_plan;
11994
- }
11995
- }
11996
-
11997
- $this->_plans = $plans;
11998
- $this->_store_plans();
11999
- }
12000
-
12001
- $this->do_action( 'after_plans_sync', $plans );
12002
-
12003
- return $this->_plans;
12004
- }
12005
-
12006
- /**
12007
- * Check if specified plan exists locally. If not, fetch it and store it.
12008
- *
12009
- * @author Vova Feldman (@svovaf)
12010
- * @since 2.0.0
12011
- *
12012
- * @param number $plan_id
12013
- *
12014
- * @return \FS_Plugin_Plan|object The plan entity or the API error object on failure.
12015
- */
12016
- private function sync_plan_if_not_exist( $plan_id ) {
12017
- $plan = self::_get_plan_by_id( $plan_id );
12018
-
12019
- if ( is_object( $plan ) ) {
12020
- // Plan already exists.
12021
- return $plan;
12022
- }
12023
-
12024
- $plan = $this->fetch_plan_by_id( $plan_id );
12025
-
12026
- if ( $plan instanceof FS_Plugin_Plan ) {
12027
- $this->_plans[] = $plan;
12028
- $this->_store_plans();
12029
-
12030
- return $plan;
12031
- }
12032
-
12033
- return $plan;
12034
- }
12035
-
12036
- /**
12037
- * Check if specified license exists locally. If not, fetch it and store it.
12038
- *
12039
- * @author Vova Feldman (@svovaf)
12040
- * @since 2.0.0
12041
- *
12042
- * @param number $license_id
12043
- * @param string $license_key
12044
- *
12045
- * @return \FS_Plugin_Plan|object The plan entity or the API error object on failure.
12046
- */
12047
- private function sync_license_if_not_exist( $license_id, $license_key ) {
12048
- $license = $this->_get_license_by_id( $license_id );
12049
-
12050
- if ( is_object( $license ) ) {
12051
- // License already exists.
12052
- return $license;
12053
- }
12054
-
12055
- $license = $this->fetch_license_by_key( $license_id, $license_key );
12056
-
12057
- if ( $license instanceof FS_Plugin_License ) {
12058
- $this->_licenses[] = $license;
12059
-
12060
- $this->set_license( $license );
12061
-
12062
- $this->_store_licenses();
12063
-
12064
- return $license;
12065
- }
12066
-
12067
- return $license;
12068
- }
12069
-
12070
- /**
12071
- * Get a collection of unique plan IDs that are associated with any installs in the network.
12072
- *
12073
- * @author Leo Fajardo (@leorw)
12074
- * @since 2.0.0
12075
- *
12076
- * @return number[]
12077
- */
12078
- private function get_plans_ids_associated_with_installs() {
12079
- if ( ! is_multisite() ) {
12080
- if ( ! is_object( $this->_site ) ||
12081
- ! FS_Plugin_Plan::is_valid_id( $this->_site->plan_id )
12082
- ) {
12083
- return array();
12084
- }
12085
-
12086
- return array( $this->_site->plan_id );
12087
- }
12088
-
12089
- $plan_ids = array();
12090
- $sites = self::get_sites();
12091
- foreach ( $sites as $site ) {
12092
- $blog_id = self::get_site_blog_id( $site );
12093
- $install = $this->get_install_by_blog_id( $blog_id );
12094
-
12095
- if ( ! is_object( $install ) ||
12096
- ! FS_Plugin_Plan::is_valid_id( $install->plan_id )
12097
- ) {
12098
- continue;
12099
- }
12100
-
12101
- $plan_ids[ $install->plan_id ] = true;
12102
- }
12103
-
12104
- return array_keys( $plan_ids );
12105
- }
12106
-
12107
- /**
12108
- * Get a collection of unique license IDs that are associated with any installs in the network.
12109
- *
12110
- * @author Leo Fajardo (@leorw)
12111
- * @since 2.0.0
12112
- *
12113
- * @return number[]
12114
- */
12115
- private function get_license_ids_associated_with_installs() {
12116
- if ( ! $this->_is_network_active ) {
12117
- if ( ! is_object( $this->_site ) ||
12118
- ! FS_Plugin_License::is_valid_id( $this->_site->license_id )
12119
- ) {
12120
- return array();
12121
- }
12122
-
12123
- return array( $this->_site->license_id );
12124
- }
12125
-
12126
- $license_ids = array();
12127
- $sites = self::get_sites();
12128
- foreach ( $sites as $site ) {
12129
- $blog_id = self::get_site_blog_id( $site );
12130
- $install = $this->get_install_by_blog_id( $blog_id );
12131
-
12132
- if ( ! is_object( $install ) ||
12133
- ! FS_Plugin_License::is_valid_id( $install->license_id )
12134
- ) {
12135
- continue;
12136
- }
12137
-
12138
- $license_ids[ $install->license_id ] = true;
12139
- }
12140
-
12141
- return array_keys( $license_ids );
12142
- }
12143
-
12144
- /**
12145
- * @author Vova Feldman (@svovaf)
12146
- * @since 1.0.5
12147
- *
12148
- * @param number $id
12149
- *
12150
- * @return FS_Plugin_Plan|false
12151
- */
12152
- function _get_plan_by_id( $id ) {
12153
- $this->_logger->entrance();
12154
-
12155
- if ( ! is_array( $this->_plans ) || 0 === count( $this->_plans ) ) {
12156
- $this->_sync_plans();
12157
- }
12158
-
12159
- foreach ( $this->_plans as $plan ) {
12160
- if ( $id == $plan->id ) {
12161
- return $plan;
12162
- }
12163
- }
12164
-
12165
- return false;
12166
- }
12167
-
12168
- /**
12169
- * @author Vova Feldman (@svovaf)
12170
- * @since 1.1.8.1
12171
- *
12172
- * @param string $name
12173
- *
12174
- * @return FS_Plugin_Plan|false
12175
- */
12176
- private function get_plan_by_name( $name ) {
12177
- $this->_logger->entrance();
12178
-
12179
- if ( ! is_array( $this->_plans ) || 0 === count( $this->_plans ) ) {
12180
- $this->_sync_plans();
12181
- }
12182
-
12183
- foreach ( $this->_plans as $plan ) {
12184
- if ( $name == $plan->name ) {
12185
- return $plan;
12186
- }
12187
- }
12188
-
12189
- return false;
12190
- }
12191
-
12192
- /**
12193
- * Sync local licenses with remote server.
12194
- *
12195
- * @author Vova Feldman (@svovaf)
12196
- * @since 1.0.6
12197
- *
12198
- * @param number|bool $site_license_id
12199
- * @param number|null $blog_id
12200
- *
12201
- * @return FS_Plugin_License[]|object
12202
- */
12203
- function _sync_licenses( $site_license_id = false, $blog_id = null ) {
12204
- $this->_logger->entrance();
12205
-
12206
- $is_network_admin = fs_is_network_admin();
12207
-
12208
- if ( $is_network_admin && is_null( $blog_id ) ) {
12209
- $all_licenses = self::get_all_licenses( $this->_module_id );
12210
- } else {
12211
- $all_licenses = $this->get_user_licenses( $this->_user->id );
12212
- }
12213
-
12214
- $foreign_licenses = $this->get_foreign_licenses_info( $all_licenses, $site_license_id );
12215
-
12216
- $all_licenses_map = array();
12217
- foreach ( $all_licenses as $license ) {
12218
- $all_licenses_map[ $license->id ] = true;
12219
- }
12220
-
12221
- $licenses = $this->_fetch_licenses( false, $site_license_id, $foreign_licenses, $blog_id );
12222
-
12223
- if ( $this->is_array_instanceof( $licenses, 'FS_Plugin_License' ) ) {
12224
- $licenses_map = array();
12225
- foreach ( $licenses as $license ) {
12226
- $licenses_map[ $license->id ] = true;
12227
- }
12228
-
12229
- // $license_ids_to_keep = $this->get_license_ids_associated_with_installs();
12230
- // foreach ( $license_ids_to_keep as $license_id ) {
12231
- // if ( isset( $licenses_map[ $license_id ] ) ) {
12232
- // continue;
12233
- // }
12234
- //
12235
- // $missing_license = self::_get_license_by_id( $license_id, false );
12236
- // if ( is_object( $missing_license ) ) {
12237
- // $licenses[] = $missing_license;
12238
- // $licenses_map[ $missing_license->id ] = true;
12239
- // }
12240
- // }
12241
-
12242
- $user_license_ids = $this->get_user_linked_license_ids( $this->_user->id );
12243
-
12244
- foreach ( $user_license_ids as $key => $license_id ) {
12245
- if ( ! isset( $licenses_map[ $license_id ] ) ) {
12246
- // Remove access to licenses that no longer exist.
12247
- unset( $user_license_ids[ $key ] );
12248
- }
12249
- }
12250
-
12251
- if ( ! empty( $user_license_ids ) ) {
12252
- foreach ( $licenses_map as $license_id => $value ) {
12253
- if ( ! isset( $all_licenses_map[ $license_id ] ) ) {
12254
- // Associate new licenses with the user who triggered the license syncing.
12255
- $user_license_ids[] = $license_id;
12256
- }
12257
- }
12258
-
12259
- $user_license_ids = array_unique( $user_license_ids );
12260
- } else {
12261
- $user_license_ids = array_keys( $licenses_map );
12262
- }
12263
-
12264
- if ( ! $is_network_admin || ! is_null( $blog_id ) ) {
12265
- $user_licenses = array();
12266
- foreach ( $licenses as $license ) {
12267
- if ( ! in_array( $license->id, $user_license_ids ) ) {
12268
- continue;
12269
- }
12270
-
12271
- $user_licenses[] = $license;
12272
- }
12273
-
12274
- $this->_licenses = $user_licenses;
12275
- } else {
12276
- $this->_licenses = $licenses;
12277
- }
12278
-
12279
- $this->set_user_linked_license_ids( $this->_user->id, $user_license_ids );
12280
-
12281
- $this->_store_licenses( true, $this->_module_id, $licenses );
12282
- }
12283
-
12284
- // Update current license.
12285
- if ( is_object( $this->_license ) ) {
12286
- $license = $this->_get_license_by_id( $this->_license->id );
12287
-
12288
- if ( is_object( $license ) ) {
12289
- /**
12290
- * `$license` can be `false` in case a user change action has just been completed and this method
12291
- * has synced the `$this->_licenses` collection for the new user. In this case, the
12292
- * `$this->_licenses` collection may have only the newly activated license that is associated with
12293
- * the new user. `set_license` will eventually be called in the same request by the logic that
12294
- * follows outside this method which will detect that the install's license has been updated, and
12295
- * then `_update_site_license` will be called which in turn will call `set_license`.
12296
- *
12297
- * @author Leo Fajardo (@leorw)
12298
- * @since 2.3.2
12299
- */
12300
- $this->set_license( $license );
12301
- }
12302
- }
12303
-
12304
- return $this->_licenses;
12305
- }
12306
-
12307
- /**
12308
- * @author Vova Feldman (@svovaf)
12309
- * @since 1.0.5
12310
- *
12311
- * @param number $id
12312
- * @param bool $sync_licenses
12313
- *
12314
- * @return FS_Plugin_License|false
12315
- */
12316
- function _get_license_by_id( $id, $sync_licenses = true ) {
12317
- $this->_logger->entrance();
12318
-
12319
- if ( ! FS_Plugin_License::is_valid_id( $id ) ) {
12320
- return false;
12321
- }
12322
-
12323
- /**
12324
- * When running from the network level admin and opted-in from the network,
12325
- * check if the license exists in the network user licenses collection.
12326
- *
12327
- * @author Vova Feldman (@svovaf)
12328
- * @since 2.0.0
12329
- */
12330
- if ( fs_is_network_admin() &&
12331
- $this->is_network_registered() &&
12332
- ( ! is_object( $this->_user ) || $this->_storage->network_user_id != $this->_user->id )
12333
- ) {
12334
- $licenses = $this->get_user_licenses( $this->_storage->network_user_id );
12335
-
12336
- foreach ( $licenses as $license ) {
12337
- if ( $id == $license->id ) {
12338
- return $license;
12339
- }
12340
- }
12341
- }
12342
-
12343
- if ( ! $this->has_any_license() && $sync_licenses ) {
12344
- $this->_sync_licenses( $id );
12345
- }
12346
-
12347
- if ( is_array( $this->_licenses ) ) {
12348
- foreach ( $this->_licenses as $license ) {
12349
- if ( $id == $license->id ) {
12350
- return $license;
12351
- }
12352
- }
12353
- }
12354
-
12355
- return false;
12356
- }
12357
-
12358
- /**
12359
- * Get license by ID. Unlike _get_license_by_id(), this method only checks the local storage and return any license, whether it's associated with the current context user/install or not.
12360
- *
12361
- * @author Vova Feldman (@svovaf)
12362
- * @since 2.0.0
12363
- *
12364
- * @param number $id
12365
- *
12366
- * @return FS_Plugin_License
12367
- */
12368
- private function get_license_by_id( $id ) {
12369
- $licenses = self::get_all_licenses( $this->_module_id );
12370
-
12371
- if ( is_array( $licenses ) && ! empty( $licenses ) ) {
12372
- foreach ( $licenses as $license ) {
12373
- if ( $id == $license->id ) {
12374
- return $license;
12375
- }
12376
- }
12377
- }
12378
-
12379
- return null;
12380
- }
12381
-
12382
- /**
12383
- * Synchronize the site's context license by fetching the license form the API and updating the local data with it.
12384
- *
12385
- * @author Vova Feldman (@svovaf)
12386
- * @since 2.0.0
12387
- *
12388
- * @return \FS_Plugin_License|mixed
12389
- */
12390
- private function sync_site_license() {
12391
- $api = $this->get_api_user_scope();
12392
-
12393
- $result = $api->get( "/licenses/{$this->_license->id}.json?license_key=" . urlencode( $this->_license->secret_key ), true );
12394
-
12395
- if ( ! $this->is_api_result_entity( $result ) ) {
12396
- return $result;
12397
- }
12398
-
12399
- $license = $this->_update_site_license( new FS_Plugin_License( $result ) );
12400
- $this->_store_licenses();
12401
-
12402
- return $license;
12403
- }
12404
-
12405
- /**
12406
- * Get all user's available licenses for the current module.
12407
- *
12408
- * @author Vova Feldman (@svovaf)
12409
- * @since 2.0.0
12410
- *
12411
- * @param number $user_id
12412
- *
12413
- * @return FS_Plugin_License[]
12414
- */
12415
- private function get_user_licenses( $user_id ) {
12416
- $all_licenses = self::get_all_licenses( $this->_module_id );
12417
- if ( empty( $all_licenses ) ) {
12418
- return array();
12419
- }
12420
-
12421
- $user_license_ids = $this->get_user_linked_license_ids( $user_id );
12422
- if ( empty( $user_license_ids ) ) {
12423
- return array();
12424
- }
12425
-
12426
- $licenses = array();
12427
- foreach ( $all_licenses as $license ) {
12428
- if ( in_array( $license->id, $user_license_ids ) ) {
12429
- $licenses[] = $license;
12430
- }
12431
- }
12432
-
12433
- return $licenses;
12434
- }
12435
-
12436
- /**
12437
- * Checks if the context license is network activated except on the given blog ID.
12438
- *
12439
- * @author Vova Feldman (@svovaf)
12440
- * @since 2.0.0
12441
- *
12442
- * @param int $except_blog_id
12443
- *
12444
- * @return bool
12445
- */
12446
- private function is_license_network_active( $except_blog_id = 0 ) {
12447
- $this->_logger->entrance();
12448
-
12449
- if ( ! is_object( $this->_license ) ) {
12450
- return false;
12451
- }
12452
-
12453
- $sites = self::get_sites();
12454
-
12455
- if ( $this->_license->total_activations() < ( count( $sites ) - 1 ) ) {
12456
- // There are more sites than the number of activations, so license cannot be network activated.
12457
- return false;
12458
- }
12459
-
12460
- foreach ( $sites as $site ) {
12461
- $blog_id = self::get_site_blog_id( $site );
12462
-
12463
- if ( $except_blog_id == $blog_id ) {
12464
- // Skip excluded blog.
12465
- continue;
12466
- }
12467
-
12468
- $install = $this->get_install_by_blog_id( $blog_id );
12469
-
12470
- if ( is_object( $install ) && $install->license_id != $this->_license->id ) {
12471
- return false;
12472
- }
12473
- }
12474
-
12475
- return true;
12476
- }
12477
-
12478
- /**
12479
- * Checks if license can be activated on all the network sites (opted-in or skipped) that are not yet associated with a license. If possible, try to make the activation, if not return false.
12480
- *
12481
- * Notice: On success, this method will also update the license activations counters (without updating the license in the storage).
12482
- *
12483
- * @author Vova Feldman (@svovaf)
12484
- * @since 2.0.0
12485
- *
12486
- * @param \FS_User $user
12487
- * @param \FS_Plugin_License $license
12488
- *
12489
- * @return bool
12490
- */
12491
- private function try_activate_license_on_network( FS_User $user, FS_Plugin_License $license ) {
12492
- $this->_logger->entrance();
12493
-
12494
- $result = $this->can_activate_license_on_network( $license );
12495
-
12496
- if ( false === $result ) {
12497
- return false;
12498
- }
12499
-
12500
- $installs_without_license = $result['installs'];
12501
- if ( ! empty( $installs_without_license ) ) {
12502
- $this->activate_license_on_many_installs( $user, $license->secret_key, $installs_without_license );
12503
- }
12504
-
12505
- $disconnected_site_ids = $result['sites'];
12506
- if ( ! empty( $disconnected_site_ids ) ) {
12507
- $this->activate_license_on_many_sites( $user, $license->secret_key, $disconnected_site_ids );
12508
- }
12509
-
12510
- $this->link_license_2_user( $license->id, $user->id );
12511
-
12512
- // Sync license after activations.
12513
- $license->activated += $result['production_count'];
12514
- $license->activated_local += $result['localhost_count'];
12515
-
12516
- // $this->_store_licenses()
12517
-
12518
- return true;
12519
- }
12520
-
12521
- /**
12522
- * Checks if the given license can be activated on the whole network.
12523
- *
12524
- * @author Vova Feldman (@svovaf)
12525
- * @since 2.0.0
12526
- *
12527
- * @param \FS_Plugin_License $license
12528
- *
12529
- * @return false|array {
12530
- * @type array[int]FS_Site $installs Blog ID to install map.
12531
- * @type int[] $sites Non-connected blog IDs.
12532
- * @type int $production_count Production sites count.
12533
- * @type int $localhost_count Production sites count.
12534
- * }
12535
- */
12536
- private function can_activate_license_on_network( FS_Plugin_License $license ) {
12537
- $sites = self::get_sites();
12538
-
12539
- $production_count = 0;
12540
- $localhost_count = 0;
12541
-
12542
- $installs_without_license = array();
12543
- $disconnected_site_ids = array();
12544
-
12545
- foreach ( $sites as $site ) {
12546
- $blog_id = self::get_site_blog_id( $site );
12547
- $install = $this->get_install_by_blog_id( $blog_id );
12548
-
12549
- if ( is_object( $install ) ) {
12550
- if ( FS_Plugin_License::is_valid_id( $install->license_id ) ) {
12551
- // License already activated on the install.
12552
- continue;
12553
- }
12554
-
12555
- $url = $install->url;
12556
-
12557
- $installs_without_license[ $blog_id ] = $install;
12558
- } else {
12559
- $url = is_object( $site ) ?
12560
- $site->siteurl :
12561
- get_site_url( $blog_id );
12562
-
12563
- $disconnected_site_ids[] = $blog_id;
12564
- }
12565
-
12566
- if ( FS_Site::is_localhost_by_address( $url ) ) {
12567
- $localhost_count ++;
12568
- } else {
12569
- $production_count ++;
12570
- }
12571
- }
12572
-
12573
- if ( ! $license->can_activate_bulk( $production_count, $localhost_count ) ) {
12574
- return false;
12575
- }
12576
-
12577
- return array(
12578
- 'installs' => $installs_without_license,
12579
- 'sites' => $disconnected_site_ids,
12580
- 'production_count' => $production_count,
12581
- 'localhost_count' => $localhost_count,
12582
- );
12583
- }
12584
-
12585
- /**
12586
- * Activate a given license on a collection of installs.
12587
- *
12588
- * @author Vova Feldman (@svovaf)
12589
- * @since 2.0.0
12590
- *
12591
- * @param \FS_User $user
12592
- * @param string $license_key
12593
- * @param array $blog_2_install_map {
12594
- * @key int Blog ID.
12595
- * @value FS_Site Blog's associated install.
12596
- * }
12597
- *
12598
- * @return mixed|true
12599
- */
12600
- private function activate_license_on_many_installs(
12601
- FS_User $user,
12602
- $license_key,
12603
- array $blog_2_install_map
12604
- ) {
12605
- $params = array(
12606
- array( 'license_key' => $this->apply_filters( 'license_key', $license_key ) )
12607
- );
12608
-
12609
- $install_2_blog_map = array();
12610
- foreach ( $blog_2_install_map as $blog_id => $install ) {
12611
- $params[] = array( 'id' => $install->id );
12612
-
12613
- $install_2_blog_map[ $install->id ] = $blog_id;
12614
- }
12615
-
12616
- $result = $this->get_api_user_scope_by_user( $user )->call(
12617
- "plugins/{$this->_plugin->id}/installs.json",
12618
- 'PUT',
12619
- $params
12620
- );
12621
-
12622
- if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
12623
- return $result;
12624
- }
12625
-
12626
- foreach ( $result->installs as $r_install ) {
12627
- $install = new FS_Site( $r_install );
12628
- $install->is_disconnected = false;
12629
-
12630
- // Update install.
12631
- $this->_store_site(
12632
- true,
12633
- $install_2_blog_map[ $r_install->id ],
12634
- $install
12635
- );
12636
- }
12637
-
12638
- return true;
12639
- }
12640
-
12641
- /**
12642
- * Activate a given license on a collection of blogs/sites that are not yet opted-in.
12643
- *
12644
- * @author Vova Feldman (@svovaf)
12645
- * @since 2.3.1
12646
- *
12647
- * @param \FS_User $user
12648
- * @param string $license_key
12649
- *
12650
- * @return true|mixed True if successful, otherwise, the API result.
12651
- */
12652
- private function activate_license_on_site( FS_User $user, $license_key ) {
12653
- return $this->activate_license_on_many_sites( $user, $license_key );
12654
- }
12655
-
12656
- /**
12657
- * Activate a given license on a collection of blogs/sites that are not yet opted-in.
12658
- *
12659
- * @author Vova Feldman (@svovaf)
12660
- * @since 2.0.0
12661
- *
12662
- * @param \FS_User $user
12663
- * @param string $license_key
12664
- * @param int[] $site_ids
12665
- *
12666
- * @return true|mixed True if successful, otherwise, the API result.
12667
- */
12668
- private function activate_license_on_many_sites(
12669
- FS_User $user,
12670
- $license_key,
12671
- array $site_ids = array()
12672
- ) {
12673
- $sites = array();
12674
- foreach ( $site_ids as $site_id ) {
12675
- $sites[] = $this->get_site_info( array( 'blog_id' => $site_id ) );
12676
- }
12677
-
12678
- // Install the plugin.
12679
- $result = $this->create_installs_with_user(
12680
- $user,
12681
- $license_key,
12682
- false,
12683
- $sites,
12684
- false,
12685
- true
12686
- );
12687
-
12688
- if ( ! $this->is_api_result_entity( $result ) &&
12689
- ! $this->is_api_result_object( $result, 'installs' )
12690
- ) {
12691
- return $result;
12692
- }
12693
-
12694
- $installs = array();
12695
-
12696
- if ( $this->is_api_result_entity( $result ) ) {
12697
- $install = new FS_Site( $result );
12698
-
12699
- $this->_user = $user;
12700
-
12701
- $this->_store_site( true, null, $install );
12702
-
12703
- $this->_site = $install;
12704
-
12705
- $this->reset_anonymous_mode();
12706
- } else {
12707
- foreach ( $result->installs as $install ) {
12708
- $installs[] = new FS_Site( $install );
12709
- }
12710
-
12711
- // Map site addresses to their blog IDs.
12712
- $address_to_blog_map = $this->get_address_to_blog_map();
12713
-
12714
- $first_blog_id = null;
12715
-
12716
- foreach ( $installs as $install ) {
12717
- $address = trailingslashit( fs_strip_url_protocol( $install->url ) );
12718
- $blog_id = $address_to_blog_map[ $address ];
12719
-
12720
- $this->_store_site( true, $blog_id, $install );
12721
-
12722
- $this->reset_anonymous_mode( $blog_id );
12723
-
12724
- if ( is_null( $first_blog_id ) ) {
12725
- $first_blog_id = $blog_id;
12726
- }
12727
- }
12728
-
12729
- if ( ! FS_Site::is_valid_id( $this->_storage->network_install_blog_id ) ) {
12730
- $this->_storage->network_install_blog_id = $first_blog_id;
12731
- }
12732
- }
12733
-
12734
- return true;
12735
- }
12736
-
12737
- /**
12738
- * Sync site's license with user licenses.
12739
- *
12740
- * @author Vova Feldman (@svovaf)
12741
- * @since 1.0.6
12742
- *
12743
- * @param FS_Plugin_License|null $new_license
12744
- *
12745
- * @return FS_Plugin_License|null
12746
- */
12747
- function _update_site_license( $new_license ) {
12748
- $this->_logger->entrance();
12749
-
12750
- /**
12751
- * In case this call will be removed in the future, the `_sync_licenses()` method needs to be updated
12752
- * accordingly so that it will also handle the case when an ownership change is done via license
12753
- * activation.
12754
- *
12755
- * @author Leo Fajardo (@leorw)
12756
- * @since 2.3.2
12757
- */
12758
- $this->set_license( $new_license );
12759
-
12760
- if ( ! is_object( $new_license ) ) {
12761
- $this->_site->license_id = null;
12762
- $this->_sync_site_subscription( null );
12763
-
12764
- return $this->_license;
12765
- }
12766
-
12767
- $this->_site->license_id = $this->_license->id;
12768
-
12769
- if ( ! is_array( $this->_licenses ) ) {
12770
- $this->_licenses = array();
12771
- }
12772
-
12773
- $is_license_found = false;
12774
- for ( $i = 0, $len = count( $this->_licenses ); $i < $len; $i ++ ) {
12775
- if ( $new_license->id == $this->_licenses[ $i ]->id ) {
12776
- $this->_licenses[ $i ] = $new_license;
12777
-
12778
- $is_license_found = true;
12779
- break;
12780
- }
12781
- }
12782
-
12783
- // If new license just append.
12784
- if ( ! $is_license_found ) {
12785
- $this->_licenses[] = $new_license;
12786
- }
12787
-
12788
- $this->_sync_site_subscription( $new_license );
12789
-
12790
- return $this->_license;
12791
- }
12792
-
12793
- /**
12794
- * @author Vova Feldman (@svovaf)
12795
- * @since 2.3.1
12796
- *
12797
- * @param \FS_Plugin_License $license
12798
- */
12799
- private function set_license( FS_Plugin_License $license = null ) {
12800
- $this->_license = $license;
12801
-
12802
- $this->maybe_update_whitelabel_flag( $license );
12803
- }
12804
-
12805
- /**
12806
- * @author Leo Fajardo (@leorw)
12807
- * @since 2.3.1
12808
- *
12809
- * @param FS_Plugin_License $license
12810
- */
12811
- private function maybe_update_whitelabel_flag( $license ) {
12812
- $is_whitelabeled = isset( $this->_storage->is_whitelabeled ) ?
12813
- $this->_storage->is_whitelabeled :
12814
- false;
12815
-
12816
- if ( is_object( $license ) ) {
12817
- $license_user = self::_get_user_by_id( $license->user_id );
12818
-
12819
- if ( ! is_object( $license_user ) ) {
12820
- // If foreign license, do not update the `is_whitelabeled` flag.
12821
- return;
12822
- }
12823
-
12824
- if ( $this->is_addon() ) {
12825
- /**
12826
- * Store the last license data to the parent's storage since it's needed only when showing the
12827
- * "Start Debug" dialog which is triggered from the "Account" page. This way, there's no need to
12828
- * iterate over the add-ons just to get the last license data.
12829
- */
12830
- $this->get_parent_instance()->store_last_activated_license_data( $license, $license_user );
12831
- } else {
12832
- $this->store_last_activated_license_data( $license );
12833
- }
12834
-
12835
- if ( $license->is_whitelabeled ) {
12836
- // Activated a developer license, data should be hidden.
12837
- $is_whitelabeled = true;
12838
- } else if ( $this->is_registered() && $this->_user->id == $license->user_id ) {
12839
- // The account owner activated a regular license key, no need to hide the data.
12840
- $is_whitelabeled = false;
12841
- }
12842
- }
12843
-
12844
- $this->_storage->is_whitelabeled = $is_whitelabeled;
12845
-
12846
- // Reset the whitelabeled status after update.
12847
- $this->is_whitelabeled = null;
12848
- if ( $this->is_addon() ) {
12849
- $parent_fs = $this->get_parent_instance();
12850
-
12851
- if ( is_object( $parent_fs ) ) {
12852
- $parent_fs->is_whitelabeled = null;
12853
- }
12854
- }
12855
- }
12856
-
12857
- /**
12858
- * @author Leo Fajardo (@leorw)
12859
- * @since 2.3.1
12860
- *
12861
- * @param FS_Plugin_License $license
12862
- * @param FS_User $license_user
12863
- */
12864
- private function store_last_activated_license_data( FS_Plugin_License $license, $license_user = null ) {
12865
- if ( ! is_object( $license_user ) ) {
12866
- $this->_storage->last_license_key = md5( $license->secret_key );
12867
- $this->_storage->last_license_user_id = null;
12868
- } else {
12869
- $this->_storage->last_license_user_key = md5( $license_user->secret_key );
12870
- $this->_storage->last_license_user_id = $license_user->id;
12871
- }
12872
- }
12873
-
12874
- /**
12875
- * @author Leo Fajardo (@leorw)
12876
- * @since 2.3.1
12877
- *
12878
- * @param bool $ignore_data_debug_mode
12879
- *
12880
- * @return bool
12881
- */
12882
- function is_whitelabeled_by_flag( $ignore_data_debug_mode = false ) {
12883
- if ( true !== $this->_storage->is_whitelabeled ) {
12884
- return false;
12885
- } else if ( $ignore_data_debug_mode ) {
12886
- return true;
12887
- }
12888
-
12889
- $fs = $this->is_addon() ?
12890
- $this->get_parent_instance() :
12891
- $this;
12892
-
12893
- return ! $fs->is_data_debug_mode();
12894
- }
12895
-
12896
- /**
12897
- * @author Leo Fajardo (@leorw)
12898
- * @since 2.3.1
12899
- *
12900
- * @return number
12901
- */
12902
- function get_last_license_user_id() {
12903
- return ( FS_User::is_valid_id( $this->_storage->last_license_user_id ) ) ?
12904
- $this->_storage->last_license_user_id :
12905
- null;
12906
- }
12907
-
12908
- /**
12909
- * @author Leo Fajardo (@leorw)
12910
- * @since 2.3.1
12911
- *
12912
- * @param int $blog_id
12913
- * @param bool $ignore_data_debug_mode
12914
- *
12915
- * @return bool
12916
- */
12917
- function is_whitelabeled( $ignore_data_debug_mode = false, $blog_id = null ) {
12918
- if ( ! is_null( $blog_id ) ) {
12919
- $this->switch_to_blog( $blog_id );
12920
- }
12921
-
12922
- if ( ! is_null( $this->is_whitelabeled ) ) {
12923
- $is_whitelabeled = $this->is_whitelabeled;
12924
- } else {
12925
- $is_whitelabeled = false;
12926
-
12927
- $is_whitelabeled_flag = $this->is_whitelabeled_by_flag( true );
12928
-
12929
- if ( ! $this->has_addons() ) {
12930
- $is_whitelabeled = $is_whitelabeled_flag;
12931
- } else if ( $is_whitelabeled_flag ) {
12932
- $is_whitelabeled = true;
12933
- } else {
12934
- $addon_ids = $this->get_updated_account_addons();
12935
- $installed_addons = $this->get_installed_addons();
12936
- foreach ( $installed_addons as $fs_addon ) {
12937
- $addon_ids[] = $fs_addon->get_id();
12938
- }
12939
-
12940
- if ( ! empty( $addon_ids ) ) {
12941
- $addon_ids = array_unique( $addon_ids );
12942
-
12943
- $is_network_level = (
12944
- fs_is_network_admin() &&
12945
- $this->is_network_active()
12946
- );
12947
-
12948
- foreach ( $addon_ids as $addon_id ) {
12949
- $addon = $this->get_addon( $addon_id );
12950
-
12951
- if ( ! is_object( $addon ) ) {
12952
- continue;
12953
- }
12954
-
12955
- $addon_storage = FS_Storage::instance( WP_FS__MODULE_TYPE_PLUGIN, $addon->slug );
12956
- $fs_addon = $this->is_addon_activated( $addon_id ) ?
12957
- self::get_addon_instance( $addon_id ) :
12958
- null;
12959
-
12960
- $was_addon_network_activated = false;
12961
-
12962
- if ( is_object( $fs_addon ) ) {
12963
- $was_addon_network_activated = $fs_addon->is_network_active();
12964
- } else if ( $is_network_level ) {
12965
- $was_addon_network_activated = $addon_storage->get( 'was_plugin_loaded', false, true );
12966
- }
12967
-
12968
- $network_delegated_connection = (
12969
- $was_addon_network_activated &&
12970
- $addon_storage->get( 'is_delegated_connection', false, true )
12971
- );
12972
-
12973
- if (
12974
- $is_network_level &&
12975
- ( ! $was_addon_network_activated || $network_delegated_connection )
12976
- ) {
12977
- $sites = self::get_sites();
12978
-
12979
- /**
12980
- * If in network admin area and the add-on was not network-activated or network-activated
12981
- * and network-delegated, find any add-on whose is_whitelabeled flag is true.
12982
- */
12983
- foreach ( $sites as $site ) {
12984
- $site_info = $this->get_site_info( $site );
12985
-
12986
- if ( $addon_storage->get( 'is_whitelabeled', false, $site_info['blog_id'] ) ) {
12987
- $is_whitelabeled = true;
12988
- break;
12989
- }
12990
- }
12991
-
12992
- if ( $is_whitelabeled ) {
12993
- break;
12994
- }
12995
- } else {
12996
- /**
12997
- * This will be executed when any of the following is met:
12998
- * 1. Add-on was network-activated, not network-delegated, and in network admin area.
12999
- * 2. Add-on was network-activated, network-delegated, and in site admin area.
13000
- * 3. Add-on was not network-activated and in site admin area.
13001
- */
13002
- if ( true === $addon_storage->is_whitelabeled ) {
13003
- $is_whitelabeled = true;
13004
- break;
13005
- }
13006
- }
13007
- }
13008
- }
13009
- }
13010
-
13011
- $this->is_whitelabeled = $is_whitelabeled;
13012
-
13013
- if ( ! $is_whitelabeled || ! $this->is_data_debug_mode() ) {
13014
- $this->_admin_notices->remove_sticky( 'data_debug_mode_enabled' );
13015
- }
13016
-
13017
- if ( ! is_null( $blog_id ) ) {
13018
- $this->restore_current_blog();
13019
- }
13020
- }
13021
-
13022
- return (
13023
- $is_whitelabeled &&
13024
- ( $ignore_data_debug_mode || ! $this->is_data_debug_mode() )
13025
- );
13026
- }
13027
-
13028
- /**
13029
- * Sync site's subscription.
13030
- *
13031
- * @author Vova Feldman (@svovaf)
13032
- * @since 1.0.9
13033
- *
13034
- * @param FS_Plugin_License|null $license
13035
- *
13036
- * @return bool|\FS_Subscription
13037
- */
13038
- private function _sync_site_subscription( $license ) {
13039
- if ( ! is_object( $license ) ) {
13040
- $this->delete_unused_subscriptions();
13041
-
13042
- return false;
13043
- }
13044
-
13045
- // Load subscription details if not lifetime.
13046
- $subscription = $license->is_lifetime() ?
13047
- false :
13048
- $this->_fetch_site_license_subscription();
13049
-
13050
- if ( is_object( $subscription ) && ! isset( $subscription->error ) ) {
13051
- $this->store_subscription( $subscription );
13052
- } else {
13053
- $this->delete_unused_subscriptions();
13054
- }
13055
-
13056
- return $subscription;
13057
- }
13058
-
13059
- /**
13060
- * @author Vova Feldman (@svovaf)
13061
- * @since 1.0.6
13062
- *
13063
- * @return bool|\FS_Plugin_License
13064
- */
13065
- function _get_license() {
13066
- if ( ! fs_is_network_admin() || is_object( $this->_license ) ) {
13067
- return $this->_license;
13068
- }
13069
-
13070
- return $this->_get_available_premium_license();
13071
- }
13072
-
13073
- /**
13074
- * @param number $license_id
13075
- *
13076
- * @return null|\FS_Subscription
13077
- */
13078
- function _get_subscription( $license_id ) {
13079
- if ( ! isset( $this->_storage->subscriptions ) ||
13080
- empty( $this->_storage->subscriptions )
13081
- ) {
13082
- return null;
13083
- }
13084
-
13085
- foreach ( fs_get_entities( $this->_storage->subscriptions, FS_Subscription::get_class_name() ) as $subscription ) {
13086
- if ( $subscription->license_id == $license_id ) {
13087
- return $subscription;
13088
- }
13089
- }
13090
-
13091
- return null;
13092
- }
13093
-
13094
- /**
13095
- * @author Leo Fajardo (@leorw)
13096
- * @since 2.0.0
13097
- *
13098
- * @param FS_Subscription $subscription
13099
- */
13100
- function store_subscription( FS_Subscription $subscription ) {
13101
- if ( ! isset( $this->_storage->subscriptions ) ) {
13102
- $this->_storage->subscriptions = array();
13103
- }
13104
-
13105
- if ( empty( $this->_storage->subscriptions ) || ! is_multisite() ) {
13106
- $this->_storage->subscriptions = array( $subscription );
13107
-
13108
- return;
13109
- }
13110
-
13111
- $subscriptions = fs_get_entities( $this->_storage->subscriptions, FS_Subscription::get_class_name() );
13112
-
13113
- $updated_subscription = false;
13114
- foreach ( $subscriptions as $key => $existing_subscription ) {
13115
- if ( $existing_subscription->id == $subscription->id ) {
13116
- $subscriptions[ $key ] = $subscription;
13117
- $updated_subscription = true;
13118
- break;
13119
- }
13120
- }
13121
-
13122
- if ( ! $updated_subscription ) {
13123
- $subscriptions[] = $subscription;
13124
- }
13125
-
13126
- $this->_storage->subscriptions = $subscriptions;
13127
- }
13128
-
13129
- /**
13130
- * @author Leo Fajardo (@leorw)
13131
- * @since 2.0.0
13132
- */
13133
- function delete_unused_subscriptions() {
13134
- if ( ! isset( $this->_storage->subscriptions ) ||
13135
- empty( $this->_storage->subscriptions ) ||
13136
- // Clean up only if there are already at least 3 subscriptions.
13137
- ( count( $this->_storage->subscriptions ) < 3 )
13138
- ) {
13139
- return;
13140
- }
13141
-
13142
- if ( ! is_multisite() ) {
13143
- // If not multisite, there should only be 1 subscription, so just clear the array.
13144
- $this->_storage->subscriptions = array();
13145
-
13146
- return;
13147
- }
13148
-
13149
- $subscriptions_to_keep_by_license_id_map = array();
13150
- $sites = self::get_sites();
13151
- foreach ( $sites as $site ) {
13152
- $blog_id = self::get_site_blog_id( $site );
13153
- $install = $this->get_install_by_blog_id( $blog_id );
13154
-
13155
- if ( ! is_object( $install ) ||
13156
- ! FS_Plugin_License::is_valid_id( $install->license_id )
13157
- ) {
13158
- continue;
13159
- }
13160
-
13161
- $subscriptions_to_keep_by_license_id_map[ $install->license_id ] = true;
13162
- }
13163
-
13164
- if ( empty( $subscriptions_to_keep_by_license_id_map ) ) {
13165
- $this->_storage->subscriptions = array();
13166
-
13167
- return;
13168
- }
13169
-
13170
- foreach ( $this->_storage->subscriptions as $key => $subscription ) {
13171
- if ( ! isset( $subscriptions_to_keep_by_license_id_map[ $subscription->license_id ] ) ) {
13172
- unset( $this->_storage->subscriptions[ $key ] );
13173
- }
13174
- }
13175
- }
13176
-
13177
- /**
13178
- * @author Vova Feldman (@svovaf)
13179
- * @since 1.0.2
13180
- *
13181
- * @param string $plan Plan name
13182
- * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
13183
- *
13184
- * @return bool
13185
- */
13186
- function is_plan( $plan, $exact = false ) {
13187
- $this->_logger->entrance();
13188
-
13189
- if ( ! $this->is_registered() ) {
13190
- return false;
13191
- }
13192
-
13193
- $plan = strtolower( $plan );
13194
-
13195
- $current_plan_name = $this->get_plan_name();
13196
-
13197
- if ( $current_plan_name === $plan ) {
13198
- // Exact plan.
13199
- return true;
13200
- } else if ( $exact ) {
13201
- // Required exact, but plans are different.
13202
- return false;
13203
- }
13204
-
13205
- $current_plan_order = - 1;
13206
- $required_plan_order = PHP_INT_MAX;
13207
- for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
13208
- if ( $plan === $this->_plans[ $i ]->name ) {
13209
- $required_plan_order = $i;
13210
- } else if ( $current_plan_name === $this->_plans[ $i ]->name ) {
13211
- $current_plan_order = $i;
13212
- }
13213
- }
13214
-
13215
- return ( $current_plan_order > $required_plan_order );
13216
- }
13217
-
13218
- /**
13219
- * Check if module has only one plan.
13220
- *
13221
- * @author Vova Feldman (@svovaf)
13222
- * @since 1.2.1.7
13223
- *
13224
- * @param bool $double_check In some cases developers prefer to release their paid offering as premium-only, even though there is a free version. For those cases, looking at the 'is_premium_only' value isn't enough because the result will return false even when the product has only signle paid plan.
13225
- *
13226
- * @return bool
13227
- */
13228
- function is_single_plan( $double_check = false ) {
13229
- $this->_logger->entrance();
13230
-
13231
- if ( ! $this->is_registered() ||
13232
- ! is_array( $this->_plans ) ||
13233
- 0 === count( $this->_plans )
13234
- ) {
13235
- return true;
13236
- }
13237
-
13238
- $has_free_plan = $this->has_free_plan();
13239
-
13240
- if ( ! $has_free_plan && $double_check ) {
13241
- foreach ( $this->_plans as $plan ) {
13242
- if ( $plan->is_free() ) {
13243
- $has_free_plan = true;
13244
- break;
13245
- }
13246
- }
13247
- }
13248
-
13249
- return ( 1 === ( count( $this->_plans ) - ( $has_free_plan ? 1 : 0 ) ) );
13250
- }
13251
-
13252
- /**
13253
- * Check if plan based on trial. If not in trial mode, should return false.
13254
- *
13255
- * @since 1.0.9
13256
- *
13257
- * @param string $plan Plan name
13258
- * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
13259
- *
13260
- * @return bool
13261
- */
13262
- function is_trial_plan( $plan, $exact = false ) {
13263
- $this->_logger->entrance();
13264
-
13265
- if ( ! $this->is_registered() ) {
13266
- return false;
13267
- }
13268
-
13269
- if ( ! $this->is_trial() ) {
13270
- return false;
13271
- }
13272
-
13273
- $trial_plan = $this->get_trial_plan();
13274
-
13275
- if ( $trial_plan->name === $plan ) {
13276
- // Exact plan.
13277
- return true;
13278
- } else if ( $exact ) {
13279
- // Required exact, but plans are different.
13280
- return false;
13281
- }
13282
-
13283
- $current_plan_order = - 1;
13284
- $required_plan_order = - 1;
13285
- for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
13286
- if ( $plan === $this->_plans[ $i ]->name ) {
13287
- $required_plan_order = $i;
13288
- } else if ( $trial_plan->name === $this->_plans[ $i ]->name ) {
13289
- $current_plan_order = $i;
13290
- }
13291
- }
13292
-
13293
- return ( $current_plan_order > $required_plan_order );
13294
- }
13295
-
13296
- /**
13297
- * Check if plugin has any paid plans.
13298
- *
13299
- * @author Vova Feldman (@svovaf)
13300
- * @since 1.0.7
13301
- *
13302
- * @return bool
13303
- */
13304
- function has_paid_plan() {
13305
- return $this->_has_paid_plans ||
13306
- FS_Plan_Manager::instance()->has_paid_plan( $this->_plans );
13307
- }
13308
-
13309
- /**
13310
- * Check if plugin has any plan with a trail.
13311
- *
13312
- * @author Vova Feldman (@svovaf)
13313
- * @since 1.0.9
13314
- *
13315
- * @return bool
13316
- */
13317
- function has_trial_plan() {
13318
- /**
13319
- * @author Vova Feldman(@svovaf)
13320
- * @since 1.2.1.5
13321
- *
13322
- * Allow setting a trial from the SDK without calling the API.
13323
- * But, if the user did opt-in, continue using the real data from the API.
13324
- */
13325
- if ( $this->_trial_days >= 0 ) {
13326
- return true;
13327
- }
13328
-
13329
- return $this->_storage->get( 'has_trial_plan', false );
13330
- }
13331
-
13332
- /**
13333
- * Check if plugin has any free plan, or is it premium only.
13334
- *
13335
- * Note: If no plans configured, assume plugin is free.
13336
- *
13337
- * @author Vova Feldman (@svovaf)
13338
- * @since 1.0.7
13339
- *
13340
- * @return bool
13341
- */
13342
- function has_free_plan() {
13343
- return ! $this->is_only_premium();
13344
- }
13345
-
13346
- /**
13347
- * Displays a license activation dialog box when the user clicks on the "Activate License"
13348
- * or "Change License" link on the plugins
13349
- * page.
13350
- *
13351
- * @author Leo Fajardo (@leorw)
13352
- * @since 1.1.9
13353
- */
13354
- function _add_license_activation_dialog_box() {
13355
- $vars = array(
13356
- 'id' => $this->_module_id,
13357
- );
13358
-
13359
- fs_require_template( 'forms/license-activation.php', $vars );
13360
- fs_require_template( 'forms/resend-key.php', $vars );
13361
- }
13362
-
13363
- /**
13364
- * Displays an email address update dialog box when the user clicks on the email address "Edit" button on the "Account" page.
13365
- *
13366
- * @author Leo Fajardo (@leorw)
13367
- * @since 2.5.0
13368
- */
13369
- function _add_email_address_update_dialog_box() {
13370
- $vars = array( 'id' => $this->_module_id );
13371
-
13372
- fs_require_template( 'forms/email-address-update.php', $vars );
13373
- }
13374
-
13375
- /**
13376
- * @author Leo Fajardo (@leorw)
13377
- * @since 2.5.0
13378
- */
13379
- function _add_email_address_update_option() {
13380
- if ( ! $this->should_handle_user_change() ) {
13381
- return;
13382
- }
13383
-
13384
- // Add email address update AJAX handler.
13385
- $this->add_ajax_action( 'update_email_address', array( &$this, '_email_address_update_ajax_handler' ) );
13386
- }
13387
-
13388
- /**
13389
- * @author Leo Fajardo (@leorw)
13390
- * @since 2.5.0
13391
- */
13392
- function _email_address_update_ajax_handler() {
13393
- $this->check_ajax_referer( 'update_email_address' );
13394
-
13395
- $new_email_address = fs_request_get( 'email_address' );
13396
- $transfer_type = fs_request_get( 'transfer_type' );
13397
-
13398
- $result = $this->update_email( $new_email_address );
13399
-
13400
- if ( ! FS_Api::is_api_error( $result ) ) {
13401
- self::shoot_ajax_success();
13402
- }
13403
-
13404
- $error = '';
13405
-
13406
- if ( FS_Api::is_api_error_object( $result ) ) {
13407
- switch ( $result->error->code ) {
13408
- case 'user_exist':
13409
- case 'account_verification_required':
13410
- $error = array(
13411
- 'code' => 'change_ownership',
13412
- 'url' => $this->get_account_url( 'change_owner', array(
13413
- 'state' => 'init',
13414
- 'candidate_email' => $new_email_address,
13415
- 'transfer_type' => $transfer_type,
13416
- ) ),
13417
- );
13418
-
13419
- break;
13420
- }
13421
- }
13422
-
13423
- if ( empty( $error ) ) {
13424
- $error = is_object( $result ) ?
13425
- var_export( $result->error, true ) :
13426
- $result;
13427
- }
13428
-
13429
- self::shoot_ajax_failure( $error );
13430
- }
13431
-
13432
- /**
13433
- * Returns a collection of IDs of installs that are associated with the context product and its add-ons, and activated with foreign licenses.
13434
- *
13435
- * @author Leo Fajardo (@leorw)
13436
- * @since 2.3.2
13437
- *
13438
- * @return number[]
13439
- */
13440
- function get_installs_ids_with_foreign_licenses() {
13441
- $installs = array();
13442
-
13443
- if (
13444
- is_object( $this->_license ) &&
13445
- $this->_site->user_id != $this->_license->user_id
13446
- ) {
13447
- $installs[] = $this->_site->id;
13448
- }
13449
-
13450
- /**
13451
- * Also try to get foreign licenses for the context product's add-ons.
13452
- */
13453
- $installs_by_slug_map = $this->get_parent_and_addons_installs_info();
13454
-
13455
- foreach ( $installs_by_slug_map as $slug => $install_info ) {
13456
- if ( $slug == $this->get_slug() ) {
13457
- continue;
13458
- }
13459
-
13460
- $install = $install_info['install'];
13461
- $license = $install_info['license'];
13462
-
13463
- if (
13464
- is_object( $license ) &&
13465
- $install->user_id != $license->user_id
13466
- ) {
13467
- $installs[] = $install->id;
13468
- }
13469
- }
13470
-
13471
- return $installs;
13472
- }
13473
-
13474
- /**
13475
- * Displays the "Change User" dialog box when the user clicks on the "Change User" button on the "Account" page.
13476
- *
13477
- * @author Leo Fajardo (@leorw)
13478
- * @since 2.3.2
13479
- *
13480
- * @param number[] $install_ids
13481
- */
13482
- function _add_user_change_dialog_box( $install_ids ) {
13483
- $vars = array(
13484
- 'id' => $this->_module_id,
13485
- 'license_owners' => $this->fetch_installs_licenses_owners_data( $install_ids )
13486
- );
13487
-
13488
- fs_require_template( 'forms/user-change.php', $vars );
13489
- }
13490
-
13491
- /**
13492
- * @author Leo Fajardo (@leorw)
13493
- * @since 2.3.1
13494
- */
13495
- function _add_data_debug_mode_dialog_box() {
13496
- $vars = array(
13497
- 'id' => $this->_module_id,
13498
- );
13499
-
13500
- fs_require_template( 'forms/data-debug-mode.php', $vars );
13501
- }
13502
-
13503
- /**
13504
- * Displays a subscription cancellation dialog box when the user clicks on the "Deactivate License"
13505
- * link on the "Account" page or deactivates a plugin and there's an active subscription that is
13506
- * either associated with a non-lifetime single-site license or non-lifetime multisite license that
13507
- * is only activated on a single production site.
13508
- *
13509
- * @author Leo Fajardo (@leorw)
13510
- * @since 2.2.1
13511
- *
13512
- * @param bool $is_license_deactivation
13513
- *
13514
- * @return array
13515
- */
13516
- function _get_subscription_cancellation_dialog_box_template_params( $is_license_deactivation = false ) {
13517
- if ( fs_is_network_admin() ) {
13518
- // Subscription cancellation dialog box is currently not supported for multisite networks.
13519
- return array();
13520
- }
13521
-
13522
- if ( $this->is_whitelabeled() ) {
13523
- return array();
13524
- }
13525
-
13526
- $license = $this->_get_license();
13527
-
13528
- /**
13529
- * If the installation is associated with a non-lifetime license, which is either a single-site or only activated on a single production site (or zero), and connected to an active subscription, suggest the customer to cancel the subscription upon deactivation.
13530
- *
13531
- * @author Leo Fajardo (@leorw) (Comment added by Vova Feldman @svovaf)
13532
- * @since 2.2.1
13533
- */
13534
- if ( ! is_object( $license ) ||
13535
- $license->is_lifetime() ||
13536
- ( ! $license->is_single_site() && $license->activated > 1 )
13537
- ) {
13538
- return array();
13539
- }
13540
-
13541
- /**
13542
- * @var FS_Subscription $subscription
13543
- */
13544
- $subscription = $this->_get_subscription( $license->id );
13545
- if ( ! is_object( $subscription ) || ! $subscription->is_active() ) {
13546
- return array();
13547
- }
13548
-
13549
- return array(
13550
- 'id' => $this->_module_id,
13551
- 'license' => $license,
13552
- 'has_trial' => $this->is_paid_trial(),
13553
- 'is_license_deactivation' => $is_license_deactivation,
13554
- );
13555
- }
13556
-
13557
- /**
13558
- * @author Leo Fajardo (@leorw)
13559
- * @since 2.0.2
13560
- */
13561
- function _add_premium_version_upgrade_selection_dialog_box() {
13562
- $modules_update = get_site_transient( $this->is_theme() ? 'update_themes' : 'update_plugins' );
13563
- if ( ! isset( $modules_update->response[ $this->_plugin_basename ] ) ) {
13564
- return;
13565
- }
13566
-
13567
- $vars = array(
13568
- 'id' => $this->_module_id,
13569
- 'new_version' => is_object( $modules_update->response[ $this->_plugin_basename ] ) ?
13570
- $modules_update->response[ $this->_plugin_basename ]->new_version :
13571
- $modules_update->response[ $this->_plugin_basename ]['new_version']
13572
- );
13573
-
13574
- fs_require_template( 'forms/premium-versions-upgrade-metadata.php', $vars );
13575
- fs_require_once_template( 'forms/premium-versions-upgrade-handler.php', $vars );
13576
- }
13577
-
13578
- /**
13579
- * Displays the opt-out dialog box when the user clicks on the "Opt Out" link on the "Plugins"
13580
- * page.
13581
- *
13582
- * @author Leo Fajardo (@leorw)
13583
- * @since 1.2.1.5
13584
- */
13585
- function _add_optout_dialog() {
13586
- if ( $this->is_theme() ) {
13587
- $vars = null;
13588
- fs_require_once_template( '/js/jquery.content-change.php', $vars );
13589
- }
13590
-
13591
- $vars = array( 'id' => $this->_module_id );
13592
- fs_require_template( 'forms/optout.php', $vars );
13593
- }
13594
-
13595
- /**
13596
- * Prepare page to include all required UI and logic for the license activation dialog.
13597
- *
13598
- * @author Vova Feldman (@svovaf)
13599
- * @since 1.2.0
13600
- */
13601
- function _add_license_activation() {
13602
- if ( $this->is_migration() ) {
13603
- return;
13604
- }
13605
-
13606
- if ( ! $this->is_user_admin() ) {
13607
- // Only admins can activate a license.
13608
- return;
13609
- }
13610
-
13611
- if ( ! $this->has_paid_plan() ) {
13612
- // Module doesn't have any paid plans.
13613
- return;
13614
- }
13615
-
13616
- if (
13617
- $this->has_premium_version() &&
13618
- ! $this->is_premium() &&
13619
- /**
13620
- * Also handle the case when an upgrade was made using the free version.
13621
- *
13622
- * @author Leo Fajardo (@leorw)
13623
- * @since 2.3.2
13624
- */
13625
- ! is_object( $this->_get_license() )
13626
- ) {
13627
- // Only add license activation logic to the premium version, or in case of a serviceware plugin, also in the free version.
13628
- return;
13629
- }
13630
-
13631
- // Add license activation link and AJAX request handler.
13632
- if ( self::is_plugins_page() ) {
13633
- $is_network_admin = fs_is_network_admin();
13634
-
13635
- if (
13636
- ( $is_network_admin && $this->is_network_active() && ! $this->is_network_delegated_connection() ) ||
13637
- ( ! $is_network_admin && ( ! $this->is_network_active() || $this->is_delegated_connection() ) )
13638
- ) {
13639
- if (
13640
- $this->is_premium() ||
13641
- ( $this->has_paid_plan() && ! $this->has_premium_version() )
13642
- ) {
13643
- /**
13644
- * @since 1.2.0 Add license action link only on plugins page.
13645
- */
13646
- $this->_add_license_action_link();
13647
- }
13648
- }
13649
- }
13650
-
13651
- // Add license activation AJAX callback.
13652
- $this->add_ajax_action( 'activate_license', array( &$this, '_activate_license_ajax_action' ) );
13653
-
13654
- // Add resend license AJAX callback.
13655
- $this->add_ajax_action( 'resend_license_key', array( &$this, '_resend_license_key_ajax_action' ) );
13656
- }
13657
-
13658
- /**
13659
- * Prepares page to include all required UI and logic for the "Change User" dialog.
13660
- *
13661
- * @author Leo Fajardo (@leorw)
13662
- * @since 2.3.2
13663
- */
13664
- function _add_user_change_option() {
13665
- if ( ! $this->should_handle_user_change() ) {
13666
- return;
13667
- }
13668
-
13669
- $installs_ids_with_foreign_licenses = $this->get_installs_ids_with_foreign_licenses();
13670
-
13671
- if ( empty( $installs_ids_with_foreign_licenses ) ) {
13672
- // Handle user change only when the parent product or one of its add-ons is activated with a foreign license.
13673
- return;
13674
- }
13675
-
13676
- // Add user change AJAX handler.
13677
- $this->add_ajax_action( 'change_user', array( &$this, '_user_change_ajax_action' ) );
13678
- }
13679
-
13680
- /**
13681
- * @author Leo Fajardo (@leorw)
13682
- * @since 2.3.2
13683
- */
13684
- function should_handle_user_change() {
13685
- if ( ! $this->is_user_admin() ) {
13686
- // Only admins can change user.
13687
- return false;
13688
- }
13689
-
13690
- if ( $this->is_addon() ) {
13691
- return false;
13692
- }
13693
-
13694
- if ( ! $this->is_registered() ) {
13695
- return false;
13696
- }
13697
-
13698
- if (
13699
- $this->is_network_active() &&
13700
- ( fs_is_network_admin() || ! $this->is_site_delegated_connection() )
13701
- ) {
13702
- // Handle only on site-level "Account" section for now.
13703
- return false;
13704
- }
13705
-
13706
- return true;
13707
- }
13708
-
13709
- /**
13710
- * @author Leo Fajardo (@leorw)
13711
- * @since 2.0.2
13712
- */
13713
- function _add_premium_version_upgrade_selection() {
13714
- if ( ! $this->is_user_admin() ) {
13715
- return;
13716
- }
13717
-
13718
- if ( ! $this->is_premium() || $this->has_any_active_valid_license() ) {
13719
- // This is relevant only to the free versions and premium versions without an active license.
13720
- return;
13721
- }
13722
-
13723
- if ( self::is_updates_page() || ( $this->is_plugin() && self::is_plugins_page() ) ) {
13724
- $this->_add_premium_version_upgrade_selection_action();
13725
- }
13726
- }
13727
-
13728
- /**
13729
- * @author Edgar Melkonyan
13730
- * @since 2.4.1
13731
- *
13732
- * @throws Freemius_Exception
13733
- */
13734
- function _toggle_whitelabel_mode_ajax_handler() {
13735
- $this->_logger->entrance();
13736
-
13737
- $this->check_ajax_referer( 'toggle_whitelabel_mode' );
13738
-
13739
- if ( ! $this->is_user_admin() ) {
13740
- // Only for admins.
13741
- self::shoot_ajax_failure();
13742
- }
13743
-
13744
- $license = $this->get_api_user_scope()->call(
13745
- "/licenses/{$this->_site->license_id}.json",
13746
- 'put',
13747
- array( 'is_whitelabeled' => ! $this->_license->is_whitelabeled )
13748
- );
13749
-
13750
- if ( ! $this->is_api_result_entity( $license ) ) {
13751
- self::shoot_ajax_failure(
13752
- FS_Api::is_api_error_object( $license ) ?
13753
- $license->error->message :
13754
- fs_text_inline( "An unknown error has occurred while trying to toggle the license's white-label mode.", 'unknown-error-occurred', $this->get_slug() )
13755
- );
13756
- }
13757
-
13758
- $this->_license->is_whitelabeled = $license->is_whitelabeled;
13759
- $this->_store_licenses();
13760
-
13761
- $this->_sync_license();
13762
-
13763
- if ( ! $license->is_whitelabeled ) {
13764
- $this->_admin_notices->remove_sticky( 'license_whitelabeled' );
13765
- } else {
13766
- $this->_admin_notices->add_sticky(
13767
- sprintf(
13768
- $this->get_text_inline(
13769
- 'Your %s license was flagged as white-labeled to hide sensitive information from the WP Admin (e.g. your email, license key, prices, billing address & invoices). If you ever wish to revert it back, you can easily do it through your %s. If this was a mistake you can also %s.',
13770
- 'license_whitelabeled'
13771
- ),
13772
- "<strong>{$this->get_plugin_title()}</strong>",
13773
- sprintf( '<a href="https://users.freemius.com" target="_blank">%s</a>', $this->get_text_inline( 'User Dashboard', 'user-dashboard' ) ),
13774
- sprintf( '<a href="#" class="fs-toggle-whitelabel-mode">%s</a>', $this->get_text_inline( 'revert it now', 'revert-it-now' ) )
13775
- ),
13776
- 'license_whitelabeled'
13777
- );
13778
- }
13779
-
13780
- self::shoot_ajax_response( array( 'success' => true ) );
13781
- }
13782
-
13783
- /**
13784
- * @author Leo Fajardo (@leorw)
13785
- * @since 2.3.0
13786
- */
13787
- function _add_beta_mode_update_handler() {
13788
- if ( ! $this->is_user_admin() ) {
13789
- return;
13790
- }
13791
-
13792
- if ( ! $this->is_premium() ) {
13793
- return;
13794
- }
13795
-
13796
- $this->add_ajax_action( 'set_beta_mode', array( &$this, '_set_beta_mode_ajax_handler' ) );
13797
- }
13798
-
13799
- /**
13800
- * @author Leo Fajardo (@leorw)
13801
- * @since 2.3.0
13802
- */
13803
- function _set_beta_mode_ajax_handler() {
13804
- $this->_logger->entrance();
13805
-
13806
- $this->check_ajax_referer( 'set_beta_mode' );
13807
-
13808
- if ( ! $this->is_user_admin() ) {
13809
- // Only for admins.
13810
- self::shoot_ajax_failure();
13811
- }
13812
-
13813
- $is_beta = trim( fs_request_get( 'is_beta', '', 'post' ) );
13814
-
13815
- if ( empty( $is_beta ) || ! in_array( $is_beta, array( 'true', 'false' ) ) ) {
13816
- self::shoot_ajax_failure();
13817
- }
13818
-
13819
- $site = $this->api_site_call(
13820
- '',
13821
- 'put',
13822
- array(
13823
- 'is_beta' => ( 'true' == $is_beta ),
13824
- 'fields' => 'is_beta'
13825
- )
13826
- );
13827
-
13828
- if ( ! $this->is_api_result_entity( $site ) ) {
13829
- self::shoot_ajax_failure(
13830
- FS_Api::is_api_error_object( $site ) ?
13831
- $site->error->message :
13832
- fs_text_inline( "An unknown error has occurred while trying to set the user's beta mode.", 'unknown-error-occurred', $this->get_slug() )
13833
- );
13834
- }
13835
-
13836
- $this->_site->is_beta = $site->is_beta;
13837
- $this->_store_site();
13838
-
13839
- self::shoot_ajax_response( array( 'success' => true ) );
13840
- }
13841
-
13842
- /**
13843
- * License activation WP AJAX handler.
13844
- *
13845
- * @author Leo Fajardo (@leorw)
13846
- * @since 1.1.9
13847
- *
13848
- * @uses Freemius::activate_license()
13849
- */
13850
- function _activate_license_ajax_action() {
13851
- $this->_logger->entrance();
13852
-
13853
- $this->check_ajax_referer( 'activate_license' );
13854
-
13855
- $license_key = trim( fs_request_get( 'license_key' ) );
13856
-
13857
- if ( empty( $license_key ) ) {
13858
- exit;
13859
- }
13860
-
13861
- $sites = fs_is_network_admin() ?
13862
- fs_request_get( 'sites', array(), 'post' ) :
13863
- array();
13864
-
13865
- $result = $this->activate_license(
13866
- $license_key,
13867
- $sites,
13868
- fs_request_get_bool( 'is_marketing_allowed', null ),
13869
- fs_request_get( 'blog_id', null ),
13870
- fs_request_get( 'module_id', null, 'post' ),
13871
- fs_request_get( 'user_id', null ),
13872
- fs_request_get_bool( 'is_extensions_tracking_allowed', null )
13873
- );
13874
-
13875
- if (
13876
- $result['success'] &&
13877
- $this->is_bundle_license_auto_activation_enabled()
13878
- ) {
13879
- $license = new FS_Plugin_License();
13880
- $license->secret_key = $license_key;
13881
-
13882
- $this->maybe_activate_bundle_license( $license, $sites );
13883
- }
13884
-
13885
- echo json_encode( $result );
13886
-
13887
- exit;
13888
- }
13889
-
13890
- /**
13891
- * User change WP AJAX handler.
13892
- *
13893
- * @author Leo Fajardo (@leorw)
13894
- * @since 2.3.2
13895
- */
13896
- function _user_change_ajax_action() {
13897
- $this->_logger->entrance();
13898
-
13899
- $this->check_ajax_referer( 'change_user' );
13900
-
13901
- $new_email_address = trim( fs_request_get( 'email_address', '' ) );
13902
- $new_user_id = fs_request_get( 'user_id' );
13903
-
13904
- if ( empty( $new_email_address ) && ! FS_User::is_valid_id( $new_user_id ) ) {
13905
- self::shoot_ajax_failure( fs_text_inline( 'Invalid new user ID or email address.', 'invalid-new-user-id-or-email', $this->get_slug() ) );
13906
- }
13907
-
13908
- $params = array();
13909
-
13910
- if ( ! empty( $new_email_address ) ) {
13911
- $params['user_email'] = $new_email_address;
13912
- } else {
13913
- $params['user_id'] = $new_user_id;
13914
- }
13915
-
13916
- $installs_info_by_slug_map = $this->get_parent_and_addons_installs_info();
13917
- $install_ids = array();
13918
-
13919
- foreach ( $installs_info_by_slug_map as $slug => $install_info ) {
13920
- $install_ids[ $slug ] = $install_info['install']->id;
13921
- }
13922
-
13923
- $params['install_ids'] = implode( ',', array_values( $install_ids ) );
13924
-
13925
- $install = $this->get_api_site_scope()->call( $this->add_show_pending( '/' ), 'put', $params );
13926
-
13927
- if ( FS_Api::is_api_error( $install ) ) {
13928
- $error = '';
13929
-
13930
- if ( is_object( $install ) ) {
13931
- switch ( $install->error->code ) {
13932
- case 'user_exist':
13933
- $error = (
13934
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...' .
13935
- $this->get_text_inline( 'Sorry, we could not complete the email update. Another user with the same email is already registered.', 'user-exist-message' ) . ' ' .
13936
- 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_address . '</b>' ) .
13937
- sprintf(
13938
- '<a style="line-height: 40px;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
13939
- $this->get_account_url( 'change_owner', array(
13940
- 'state' => 'init',
13941
- 'candidate_email' => $new_email_address
13942
- ) ),
13943
- $this->get_text_inline( 'Change Ownership', 'change-ownership' )
13944
- )
13945
- );
13946
- break;
13947
- }
13948
- }
13949
-
13950
- if ( empty( $error ) ) {
13951
- $error = FS_Api::is_api_error_object( $install ) ?
13952
- $install->error->message :
13953
- var_export( $install->error, true );
13954
- }
13955
-
13956
- self::shoot_ajax_failure( $error );
13957
- } else {
13958
- if (
13959
- // If successful ownership change.
13960
- $this->get_user()->id != $install->user_id ||
13961
- ! empty( $new_email_address )
13962
- ) {
13963
- $this->complete_ownership_change_by_license( $install->user_id, $install_ids );
13964
- }
13965
- }
13966
-
13967
- self::shoot_ajax_success();
13968
- }
13969
-
13970
- /**
13971
- * @author Leo Fajardo (@leorw)
13972
- * @since 2.3.2.14
13973
- */
13974
- function starting_migration() {
13975
- if ( ! empty( $this->_storage->license_migration ) ) {
13976
- // Do not overwrite the data if already set.
13977
- return;
13978
- }
13979
-
13980
- $this->_storage->license_migration = array(
13981
- 'is_migrating' => true,
13982
- 'start_timestamp' => time()
13983
- );
13984
- }
13985
-
13986
- /**
13987
- * @author Leo Fajardo (@leorw)
13988
- * @since 2.3.2.14
13989
- */
13990
- function is_migration() {
13991
- if ( $this->is_addon() ) {
13992
- return $this->get_parent_instance()->is_migration();
13993
- }
13994
-
13995
- if ( empty( $this->_storage->license_migration ) ) {
13996
- return false;
13997
- }
13998
-
13999
- if ( ! $this->_storage->license_migration['is_migrating'] ) {
14000
- return false;
14001
- }
14002
-
14003
- return (
14004
- // Return `true` if the migration is within 5 minutes from the starting time.
14005
- ( time() - $this->_storage->license_migration['start_timestamp'] ) <= WP_FS__TIME_5_MIN_IN_SEC
14006
- );
14007
- }
14008
-
14009
- /**
14010
- *
14011
- * A helper method to activate migrated licenses. If the product is network activated and integrated, the method will network activate the license.
14012
- *
14013
- * @author Vova Feldman (@svovaf)
14014
- * @since 2.3.0
14015
- *
14016
- * @param string $license_key
14017
- * @param null|bool $is_marketing_allowed
14018
- * @param null|number $plugin_id
14019
- * @param array $sites
14020
- * @param int $blog_id
14021
- *
14022
- * @return array {
14023
- * @var bool $success
14024
- * @var string $error
14025
- * @var string $next_page
14026
- * }
14027
- *
14028
- * @uses Freemius::activate_license()
14029
- */
14030
- function activate_migrated_license(
14031
- $license_key,
14032
- $is_marketing_allowed = null,
14033
- $plugin_id = null,
14034
- $sites = array(),
14035
- $blog_id = null
14036
- ) {
14037
- $this->_logger->entrance();
14038
-
14039
- $result = $this->activate_license(
14040
- $license_key,
14041
- ( empty( $sites ) && is_null( $blog_id ) && $this->is_network_active() ) ?
14042
- $this->get_sites_for_network_level_optin() :
14043
- $sites,
14044
- $is_marketing_allowed,
14045
- $blog_id,
14046
- $plugin_id
14047
- );
14048
-
14049
- // No need to show the sticky after license activation notice after migrating a license.
14050
- $this->_admin_notices->remove_sticky( 'plan_upgraded' );
14051
-
14052
- return $result;
14053
- }
14054
-
14055
- /**
14056
- * @author Leo Fajardo (@leorw)
14057
- * @since 2.3.1
14058
- *
14059
- * @return string
14060
- */
14061
- function get_pricing_js_path() {
14062
- if ( ! isset( $this->_pricing_js_path ) ) {
14063
- $pricing_js_path = $this->apply_filters( 'freemius_pricing_js_path', '' );
14064
-
14065
- if ( empty( $pricing_js_path ) ) {
14066
- global $fs_active_plugins;
14067
-
14068
- foreach ( $fs_active_plugins->plugins as $sdk_path => $data ) {
14069
- if ( $data->plugin_path == $this->get_plugin_basename() ) {
14070
- $plugin_or_theme_root_dir = ( $this->is_plugin() ? WP_PLUGIN_DIR : get_theme_root( get_stylesheet() ) );
14071
-
14072
- $pricing_js_path = $plugin_or_theme_root_dir
14073
- . '/'
14074
- // The basename will be `plugins`, `themes`, or the basename of a custom plugins or themes directory.
14075
- . str_replace( '../' . basename( $plugin_or_theme_root_dir ) . '/', '', $sdk_path )
14076
- . '/includes/freemius-pricing/freemius-pricing.js';
14077
-
14078
- break;
14079
- }
14080
- }
14081
- }
14082
-
14083
- $this->_pricing_js_path = $pricing_js_path;
14084
- }
14085
-
14086
- return $this->_pricing_js_path;
14087
- }
14088
-
14089
- /**
14090
- * @author Leo Fajardo (@leorw)
14091
- * @since 2.3.1
14092
- *
14093
- * @return bool
14094
- */
14095
- function should_use_external_pricing() {
14096
- if ( is_null( $this->_use_external_pricing ) ) {
14097
- $pricing_js_path = $this->get_pricing_js_path();
14098
-
14099
- $this->_use_external_pricing = ( empty( $pricing_js_path ) || ! file_exists( $pricing_js_path ) );
14100
- }
14101
-
14102
- return $this->_use_external_pricing;
14103
- }
14104
-
14105
- /**
14106
- * The implementation of this method was previously in `_activate_license_ajax_action()`.
14107
- *
14108
- * @author Vova Feldman (@svovaf)
14109
- * @since 2.2.4
14110
- * @since 2.0.0 When a super-admin that hasn't connected before is network activating a license and excluding some of the sites for the license activation, go over the unselected sites in the network and if a site is not connected, skipped, nor delegated, if it's a freemium product then just skip the connection for the site, if it's a premium only product, delegate the connection and license activation to the site admin (Vova Feldman @svovaf).
14111
- * @param string $license_key
14112
- * @param array $sites
14113
- * @param null|bool $is_marketing_allowed
14114
- * @param null|int $blog_id
14115
- * @param null|number $plugin_id
14116
- * @param null|number $license_owner_id
14117
- *
14118
- * @return array {
14119
- * @var bool $success
14120
- * @var string $error
14121
- * @var string $next_page
14122
- * }
14123
- */
14124
- private function activate_license(
14125
- $license_key,
14126
- $sites = array(),
14127
- $is_marketing_allowed = null,
14128
- $blog_id = null,
14129
- $plugin_id = null,
14130
- $license_owner_id = null,
14131
- $is_extensions_tracking_allowed = null
14132
- ) {
14133
- $this->_logger->entrance();
14134
-
14135
- $license_key = trim( $license_key );
14136
-
14137
- $is_network_activation_or_migration = (
14138
- fs_is_network_admin() ||
14139
- ( ! empty( $sites ) && $this->is_migration() )
14140
- );
14141
-
14142
- if ( ! $is_network_activation_or_migration ) {
14143
- // If the license activation is executed outside the context of a network admin, ignore the sites collection.
14144
- $sites = array();
14145
- }
14146
-
14147
- $fs = ( empty($plugin_id) || $plugin_id == $this->_module_id ) ?
14148
- $this :
14149
- $this->get_addon_instance( $plugin_id );
14150
-
14151
- $this->update_extensions_tracking_flag( $is_extensions_tracking_allowed );
14152
-
14153
- $error = false;
14154
- $next_page = false;
14155
-
14156
- $has_valid_blog_id = is_numeric( $blog_id );
14157
-
14158
- $user = null;
14159
-
14160
- if ( $fs->is_addon() && $fs->get_parent_instance()->is_registered() ) {
14161
- /**
14162
- * When activating an add-on's license and the parent is opted-in, activate the license with the parent's opted-in user context.
14163
- *
14164
- * @author Vova Feldman (@svovaf)
14165
- */
14166
- $user = $fs->get_parent_instance()->get_current_or_network_user();
14167
- } else if ( $fs->is_registered() ) {
14168
- $user = $fs->get_current_or_network_user();
14169
- }
14170
-
14171
- if ( $has_valid_blog_id ) {
14172
- /**
14173
- * If a specific blog ID was provided, activate the license only on the specific blog that is associated with the given blog ID.
14174
- *
14175
- * @author Leo Fajardo (@leorw)
14176
- */
14177
- $fs->switch_to_blog( $blog_id );
14178
- }
14179
-
14180
- if ( is_object( $user ) ) {
14181
- if ( $is_network_activation_or_migration && ! $has_valid_blog_id ) {
14182
- // If no specific blog ID was provided, activate the license for all sites in the network.
14183
- $blog_2_install_map = array();
14184
- $site_ids = array();
14185
-
14186
- foreach ( $sites as $site ) {
14187
- if ( ! isset( $site['blog_id'] ) || ! is_numeric( $site['blog_id'] ) ) {
14188
- continue;
14189
- }
14190
-
14191
- $install = $fs->get_install_by_blog_id( $site['blog_id'] );
14192
-
14193
- if ( is_object( $install ) ) {
14194
- $blog_2_install_map[ $site['blog_id'] ] = $install;
14195
- } else {
14196
- $site_ids[] = $site['blog_id'];
14197
- }
14198
- }
14199
-
14200
- if ( ! empty( $blog_2_install_map ) ) {
14201
- $result = $fs->activate_license_on_many_installs( $user, $license_key, $blog_2_install_map );
14202
-
14203
- if ( true !== $result ) {
14204
- $error = FS_Api::is_api_error_object( $result ) ?
14205
- $result->error->message :
14206
- var_export( $result, true );
14207
- }
14208
- }
14209
-
14210
- if ( empty( $error ) && ! empty( $site_ids ) ) {
14211
- $result = $fs->activate_license_on_many_sites( $user, $license_key, $site_ids );
14212
-
14213
- if ( true !== $result ) {
14214
- $error = FS_Api::is_api_error_object( $result ) ?
14215
- $result->error->message :
14216
- var_export( $result, true );
14217
- }
14218
- }
14219
- } else {
14220
- if ( $fs->is_registered() ) {
14221
- $params = array(
14222
- 'license_key' => $fs->apply_filters( 'license_key', $license_key )
14223
- );
14224
-
14225
- $install_ids = array();
14226
-
14227
- $change_owner = FS_User::is_valid_id( $license_owner_id );
14228
-
14229
- if ( $change_owner ) {
14230
- $params['user_id'] = $license_owner_id;
14231
-
14232
- $installs_info_by_slug_map = $fs->get_parent_and_addons_installs_info();
14233
-
14234
- foreach ( $installs_info_by_slug_map as $slug => $install_info ) {
14235
- $install_ids[ $slug ] = $install_info['install']->id;
14236
- }
14237
-
14238
- $params['install_ids'] = implode( ',', array_values( $install_ids ) );
14239
- }
14240
-
14241
- $api = $fs->get_api_site_scope();
14242
-
14243
- $install = $api->call( $fs->add_show_pending( '/' ), 'put', $params );
14244
-
14245
- if ( FS_Api::is_api_error( $install ) ) {
14246
- $error = FS_Api::is_api_error_object( $install ) ?
14247
- $install->error->message :
14248
- var_export( $install->error, true );
14249
- } else {
14250
- $fs->reconnect_locally( $has_valid_blog_id );
14251
-
14252
- if (
14253
- $change_owner &&
14254
- // If successful ownership change.
14255
- $fs->get_user()->id != $install->user_id
14256
- ) {
14257
- $fs->complete_ownership_change_by_license( $install->user_id, $install_ids );
14258
- }
14259
- }
14260
- } else /* ( $fs->is_addon() && $fs->get_parent_instance()->is_registered() ) */ {
14261
- $result = $fs->activate_license_on_site( $user, $license_key );
14262
-
14263
- if ( true !== $result ) {
14264
- $error = FS_Api::is_api_error_object( $result ) ?
14265
- $result->error->message :
14266
- var_export( $result, true );
14267
- }
14268
- }
14269
- }
14270
-
14271
- if ( empty( $error ) ) {
14272
- $fs->network_upgrade_mode_completed();
14273
-
14274
- $fs->_user = $user;
14275
-
14276
- if ( fs_is_network_admin() && ! $has_valid_blog_id ) {
14277
- $fs->_site = $fs->get_network_install();
14278
- }
14279
-
14280
- $fs->_sync_license( true, $has_valid_blog_id );
14281
-
14282
- $this->maybe_sync_install_user();
14283
-
14284
- $next_page = $fs->is_addon() ?
14285
- $fs->get_parent_instance()->get_account_url() :
14286
- $fs->get_after_activation_url( 'after_connect_url' );
14287
- }
14288
- } else {
14289
- $next_page = $fs->opt_in(
14290
- false,
14291
- false,
14292
- false,
14293
- $license_key,
14294
- false,
14295
- false,
14296
- false,
14297
- $is_marketing_allowed,
14298
- $sites
14299
- );
14300
-
14301
- if ( isset( $next_page->error ) ) {
14302
- $error = $next_page->error;
14303
- } else {
14304
- if ( $is_network_activation_or_migration ) {
14305
- /**
14306
- * Get the list of sites that were just opted-in (and license activated).
14307
- * This is an optimization for the next part below saving some DB queries.
14308
- */
14309
- $connected_sites = array();
14310
- foreach ( $sites as $site ) {
14311
- if ( isset( $site['blog_id'] ) && is_numeric( $site['blog_id'] ) ) {
14312
- $connected_sites[ $site['blog_id'] ] = true;
14313
- }
14314
- }
14315
-
14316
- $all_sites = self::get_sites();
14317
- $pending_sites = array();
14318
-
14319
- /**
14320
- * Check if there are any sites that are not connected, skipped, nor delegated. For every site that falls into that category, if the product is freemium, skip the connection. If the product is premium only, delegate the connection to the site administrator.
14321
- *
14322
- * @author Vova Feldman (@svovaf)
14323
- */
14324
- foreach ( $all_sites as $site ) {
14325
- $blog_id = self::get_site_blog_id( $site );
14326
-
14327
- if ( isset( $connected_sites[ $blog_id ] ) ) {
14328
- // Site was just connected.
14329
- continue;
14330
- }
14331
-
14332
- if ( $fs->is_installed_on_site( $blog_id ) ) {
14333
- // Site was already connected before.
14334
- continue;
14335
- }
14336
-
14337
- if ( $fs->is_site_delegated_connection( $blog_id ) ) {
14338
- // Site's connection was delegated.
14339
- continue;
14340
- }
14341
-
14342
- if ( $fs->is_anonymous_site( $blog_id ) ) {
14343
- // Site connection was already skipped.
14344
- continue;
14345
- }
14346
-
14347
- $pending_sites[] = self::get_site_info( $site );
14348
- }
14349
-
14350
- if ( ! empty( $pending_sites ) ) {
14351
- if ( $fs->is_freemium() && $fs->is_enable_anonymous() ) {
14352
- $fs->skip_connection( $pending_sites );
14353
- } else {
14354
- $fs->delegate_connection( $pending_sites );
14355
- }
14356
- }
14357
- }
14358
- }
14359
- }
14360
-
14361
- if ( false === $error && true === $fs->_storage->require_license_activation ) {
14362
- $fs->_storage->require_license_activation = false;
14363
- }
14364
-
14365
- $result = array(
14366
- 'success' => ( false === $error )
14367
- );
14368
-
14369
- if ( false !== $error ) {
14370
- $result['error'] = $fs->apply_filters( 'opt_in_error_message', $error );
14371
- } else {
14372
- if ( $fs->is_addon() || $fs->has_addons() ) {
14373
- /**
14374
- * Purge the valid user licenses cache so that when the "Account" or the "Add-Ons" page is loaded,
14375
- * an updated valid user licenses collection will be fetched from the server which is used to also
14376
- * update the account add-ons (add-ons the user has licenses for).
14377
- *
14378
- * @author Leo Fajardo (@leorw)
14379
- * @since 2.2.4
14380
- */
14381
- $fs->purge_valid_user_licenses_cache();
14382
- }
14383
-
14384
- $result['next_page'] = $next_page;
14385
- }
14386
-
14387
- return $result;
14388
- }
14389
-
14390
- /**
14391
- * @author Leo Fajardo (@leorw)
14392
- * @since 2.3.2
14393
- *
14394
- * @return array {
14395
- * @key string Product slug.
14396
- * @value array {
14397
- * @property FS_Site $site
14398
- * @property FS_Plugin_License $license
14399
- * }
14400
- * }
14401
- */
14402
- private function get_parent_and_addons_installs_info() {
14403
- $fs = $this->is_addon() ?
14404
- $this->get_parent_instance() :
14405
- $this;
14406
-
14407
- $installed_addons_ids = array();
14408
-
14409
- $installed_addons_instances = $fs->get_installed_addons();
14410
- foreach ( $installed_addons_instances as $instance ) {
14411
- $installed_addons_ids[] = $instance->get_id();
14412
- }
14413
-
14414
- $addons_ids = array_unique( array_merge(
14415
- $installed_addons_ids,
14416
- $fs->get_updated_account_addons()
14417
- ) );
14418
-
14419
- // Add parent product info.
14420
- $installs_info_by_slug_map = array(
14421
- $fs->get_slug() => array(
14422
- 'install' => $fs->get_site(),
14423
- 'license' => $fs->_get_license()
14424
- )
14425
- );
14426
-
14427
- foreach ( $addons_ids as $addon_id ) {
14428
- $is_installed = isset( $installed_addons_ids_map[ $addon_id ] );
14429
-
14430
- $addon_info = $fs->_get_addon_info( $addon_id, $is_installed );
14431
-
14432
- if ( ! isset( $addon_info['is_connected'] ) || ! $addon_info['is_connected'] ) {
14433
- // Add-on is not associated with an install entity.
14434
- continue;
14435
- }
14436
-
14437
- $installs_info_by_slug_map[ $addon_info['slug'] ] = array(
14438
- 'install' => $addon_info['site'],
14439
- 'license' => isset( $addon_info['license'] ) ?
14440
- $addon_info['license'] :
14441
- null
14442
- );
14443
- }
14444
-
14445
- return $installs_info_by_slug_map;
14446
- }
14447
-
14448
- /**
14449
- * @author Leo Fajardo (@leorw)
14450
- * @since 1.2.3.1
14451
- */
14452
- function _network_activate_ajax_action() {
14453
- $this->_logger->entrance();
14454
-
14455
- $this->check_ajax_referer( 'network_activate' );
14456
-
14457
- $plugin_id = fs_request_get( 'module_id', '', 'post' );
14458
- $fs = ( $plugin_id == $this->_module_id ) ?
14459
- $this :
14460
- $this->get_addon_instance( $plugin_id );
14461
-
14462
- $error = false;
14463
-
14464
- $sites = fs_request_get( 'sites', array(), 'post' );
14465
- if ( is_array( $sites ) && ! empty( $sites ) ) {
14466
- $sites_by_action = array(
14467
- 'allow' => array(),
14468
- 'delegate' => array(),
14469
- 'skip' => array()
14470
- );
14471
-
14472
- foreach ( $sites as $site ) {
14473
- $sites_by_action[ $site['action'] ][] = $site;
14474
- }
14475
-
14476
- $total_sites = count( $sites );
14477
- $total_sites_to_delegate = count( $sites_by_action['delegate'] );
14478
-
14479
- $next_page = '';
14480
-
14481
- $has_any_install = fs_request_get_bool( 'has_any_install' );
14482
-
14483
- if ( $total_sites === $total_sites_to_delegate &&
14484
- ! $this->is_network_upgrade_mode() &&
14485
- ! $has_any_install
14486
- ) {
14487
- $this->delegate_connection();
14488
- } else {
14489
- if ( ! empty( $sites_by_action['delegate'] ) ) {
14490
- $this->delegate_connection( $sites_by_action['delegate'] );
14491
- }
14492
-
14493
- if ( ! empty( $sites_by_action['skip'] ) ) {
14494
- $this->skip_connection( $sites_by_action['skip'] );
14495
- }
14496
-
14497
- if ( empty( $sites_by_action['allow'] ) ) {
14498
- if ( $has_any_install ) {
14499
- $first_install = $fs->find_first_install();
14500
-
14501
- if ( ! is_null( $first_install ) ) {
14502
- $fs->_site = $first_install['install'];
14503
- $fs->_storage->network_install_blog_id = $first_install['blog_id'];
14504
-
14505
- $fs->_user = self::_get_user_by_id( $fs->_site->user_id );
14506
- $fs->_storage->network_user_id = $fs->_user->id;
14507
- }
14508
- }
14509
- } else {
14510
- if ( ! $fs->is_registered() || ! $this->_is_network_active ) {
14511
- $next_page = $fs->opt_in(
14512
- false,
14513
- false,
14514
- false,
14515
- false,
14516
- false,
14517
- false,
14518
- false,
14519
- fs_request_get_bool( 'is_marketing_allowed', null ),
14520
- $sites_by_action['allow']
14521
- );
14522
- } else {
14523
- $next_page = $fs->install_with_user(
14524
- $this->get_network_user(),
14525
- false,
14526
- false,
14527
- false,
14528
- true,
14529
- $sites_by_action['allow']
14530
- );
14531
- }
14532
-
14533
- if ( is_object( $next_page ) && isset( $next_page->error ) ) {
14534
- $error = $next_page->error;
14535
- }
14536
- }
14537
- }
14538
-
14539
- if ( empty( $next_page ) ) {
14540
- $next_page = $this->get_after_activation_url( 'after_network_activation_url' );
14541
- }
14542
- } else {
14543
- $error = $this->get_text_inline( 'Invalid site details collection.', 'invalid_site_details_collection' );
14544
- }
14545
-
14546
- $result = array(
14547
- 'success' => ( false === $error )
14548
- );
14549
-
14550
- if ( false !== $error ) {
14551
- $result['error'] = $error;
14552
- } else {
14553
- $result['next_page'] = $next_page;
14554
- }
14555
-
14556
- echo json_encode( $result );
14557
-
14558
- exit;
14559
- }
14560
-
14561
- /**
14562
- * Billing update AJAX callback.
14563
- *
14564
- * @author Vova Feldman (@svovaf)
14565
- * @since 1.2.1.5
14566
- */
14567
- function _update_billing_ajax_action() {
14568
- $this->_logger->entrance();
14569
-
14570
- $this->check_ajax_referer( 'update_billing' );
14571
-
14572
- if ( ! $this->is_user_admin() ) {
14573
- // Only for admins.
14574
- self::shoot_ajax_failure();
14575
- }
14576
-
14577
- $billing = fs_request_get( 'billing' );
14578
-
14579
- $api = $this->get_api_user_scope();
14580
- $result = $api->call( '/billing.json', 'put', array_merge( $billing, array(
14581
- 'plugin_id' => $this->get_parent_id(),
14582
- ) ) );
14583
-
14584
- if ( ! $this->is_api_result_entity( $result ) ) {
14585
- self::shoot_ajax_failure();
14586
- }
14587
-
14588
- // Purge cached billing.
14589
- $this->get_api_user_scope()->purge_cache( 'billing.json' );
14590
-
14591
- self::shoot_ajax_success();
14592
- }
14593
-
14594
- /**
14595
- * Trial start for anonymous users (AJAX callback).
14596
- *
14597
- * @author Vova Feldman (@svovaf)
14598
- * @since 1.2.1.5
14599
- */
14600
- function _start_trial_ajax_action() {
14601
- $this->_logger->entrance();
14602
-
14603
- $this->check_ajax_referer( 'start_trial' );
14604
-
14605
- if ( ! $this->is_user_admin() ) {
14606
- // Only for admins.
14607
- self::shoot_ajax_failure();
14608
- }
14609
-
14610
- $trial_data = fs_request_get( 'trial' );
14611
-
14612
- $next_page = $this->opt_in(
14613
- false,
14614
- false,
14615
- false,
14616
- false,
14617
- false,
14618
- $trial_data['plan_id']
14619
- );
14620
-
14621
- if ( is_object( $next_page ) && $this->is_api_error( $next_page ) ) {
14622
- self::shoot_ajax_failure(
14623
- isset( $next_page->error ) ?
14624
- $next_page->error->message :
14625
- var_export( $next_page, true )
14626
- );
14627
- }
14628
-
14629
- $this->shoot_ajax_success( array(
14630
- 'next_page' => $next_page,
14631
- ) );
14632
- }
14633
-
14634
- /**
14635
- * @author Leo Fajardo (@leorw)
14636
- * @since 1.2.0
14637
- */
14638
- function _resend_license_key_ajax_action() {
14639
- $this->_logger->entrance();
14640
-
14641
- $this->check_ajax_referer( 'resend_license_key' );
14642
-
14643
- $email_address = sanitize_email( trim( fs_request_get( 'email', '', 'post' ) ) );
14644
-
14645
- if ( empty( $email_address ) ) {
14646
- exit;
14647
- }
14648
-
14649
- $error = false;
14650
-
14651
- $api = $this->get_api_plugin_scope();
14652
- $result = $api->call( '/licenses/resend.json', 'post',
14653
- array(
14654
- 'email' => $email_address,
14655
- 'url' => home_url(),
14656
- )
14657
- );
14658
-
14659
- if ( is_object( $result ) && isset( $result->error ) ) {
14660
- $error = $result->error;
14661
-
14662
- if ( in_array( $error->code, array( 'invalid_email', 'no_user' ) ) ) {
14663
- $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' );
14664
- } else if ( 'no_license' === $error->code ) {
14665
- $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' );
14666
- } else {
14667
- $error = $error->message;
14668
- }
14669
- }
14670
-
14671
- $licenses = array(
14672
- 'success' => ( false === $error )
14673
- );
14674
-
14675
- if ( false !== $error ) {
14676
- $licenses['error'] = sprintf( '%s... %s', $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ), strtolower( $error ) );
14677
- }
14678
-
14679
- echo json_encode( $licenses );
14680
-
14681
- exit;
14682
- }
14683
-
14684
- /**
14685
- * @author Vova Feldman (@svovaf)
14686
- * @since 1.2.1.8
14687
- *
14688
- * @var string
14689
- */
14690
- private static $_pagenow;
14691
-
14692
- /**
14693
- * Get current page or the referer if executing a WP AJAX request.
14694
- *
14695
- * @author Vova Feldman (@svovaf)
14696
- * @since 1.2.1.8
14697
- *
14698
- * @return string
14699
- */
14700
- static function get_current_page() {
14701
- if ( ! isset( self::$_pagenow ) ) {
14702
- global $pagenow;
14703
- if ( empty( $pagenow ) && is_admin() && is_multisite() ) {
14704
- /**
14705
- * It appears that `$pagenow` is not yet initialized in some network admin pages when this method
14706
- * is called, so initialize it here using some pieces of code from `wp-includes/vars.php`.
14707
- *
14708
- * @author Leo Fajardo (@leorw)
14709
- * @since 2.2.3
14710
- */
14711
- if ( is_network_admin() ) {
14712
- preg_match( '#/wp-admin/network/?(.*?)$#i', $_SERVER['PHP_SELF'], $self_matches );
14713
- } else if ( is_user_admin() ) {
14714
- preg_match( '#/wp-admin/user/?(.*?)$#i', $_SERVER['PHP_SELF'], $self_matches );
14715
- } else {
14716
- preg_match( '#/wp-admin/?(.*?)$#i', $_SERVER['PHP_SELF'], $self_matches );
14717
- }
14718
-
14719
- $pagenow = $self_matches[1];
14720
- $pagenow = trim( $pagenow, '/' );
14721
- $pagenow = preg_replace( '#\?.*?$#', '', $pagenow );
14722
- if ( '' === $pagenow || 'index' === $pagenow || 'index.php' === $pagenow ) {
14723
- $pagenow = 'index.php';
14724
- } else {
14725
- preg_match( '#(.*?)(/|$)#', $pagenow, $self_matches );
14726
- $pagenow = strtolower( $self_matches[1] );
14727
- if ( '.php' !== substr($pagenow, -4, 4) )
14728
- $pagenow .= '.php'; // for Options +Multiviews: /wp-admin/themes/index.php (themes.php is queried)
14729
- }
14730
- }
14731
-
14732
- self::$_pagenow = $pagenow;
14733
-
14734
- if ( self::is_ajax() &&
14735
- 'admin-ajax.php' === $pagenow
14736
- ) {
14737
- $referer = fs_get_raw_referer();
14738
-
14739
- if ( is_string( $referer ) ) {
14740
- $parts = explode( '?', $referer );
14741
-
14742
- self::$_pagenow = basename( $parts[0] );
14743
- }
14744
- }
14745
- }
14746
-
14747
- return self::$_pagenow;
14748
- }
14749
-
14750
- /**
14751
- * Helper method to check if user in the plugins page.
14752
- *
14753
- * @author Vova Feldman (@svovaf)
14754
- * @since 1.2.1.5
14755
- *
14756
- * @return bool
14757
- */
14758
- static function is_plugins_page() {
14759
- return ( 'plugins.php' === self::get_current_page() );
14760
- }
14761
-
14762
- /**
14763
- * @author Leo Fajardo (@leorw)
14764
- * @since 2.2.3
14765
- *
14766
- * @return bool
14767
- */
14768
- static function is_plugin_install_page() {
14769
- return ( 'plugin-install.php' === self::get_current_page() );
14770
- }
14771
-
14772
- /**
14773
- * @author Leo Fajardo (@leorw)
14774
- * @since 2.0.2
14775
- *
14776
- * @return bool
14777
- */
14778
- static function is_updates_page() {
14779
- return ( 'update-core.php' === self::get_current_page() );
14780
- }
14781
-
14782
- /**
14783
- * Helper method to check if user in the themes page.
14784
- *
14785
- * @author Vova Feldman (@svovaf)
14786
- * @since 1.2.2.6
14787
- *
14788
- * @return bool
14789
- */
14790
- static function is_themes_page() {
14791
- return ( 'themes.php' === self::get_current_page() );
14792
- }
14793
-
14794
- #----------------------------------------------------------------------------------
14795
- #region Affiliation
14796
- #----------------------------------------------------------------------------------
14797
-
14798
- /**
14799
- * @author Leo Fajardo (@leorw)
14800
- * @since 1.2.3
14801
- *
14802
- * @return bool
14803
- */
14804
- function has_affiliate_program() {
14805
- if ( ! is_object( $this->_plugin ) ) {
14806
- return false;
14807
- }
14808
-
14809
- return $this->_plugin->has_affiliate_program();
14810
- }
14811
-
14812
- /**
14813
- * @author Leo Fajardo (@leorw)
14814
- * @since 1.2.4
14815
- */
14816
- private function fetch_affiliate_terms() {
14817
- if ( ! is_object( $this->plugin_affiliate_terms ) ) {
14818
- $plugins_api = $this->get_api_plugin_scope();
14819
- $affiliate_terms = $plugins_api->get( '/aff.json?type=affiliation', false );
14820
-
14821
- if ( ! $this->is_api_result_entity( $affiliate_terms ) ) {
14822
- return;
14823
- }
14824
-
14825
- $this->plugin_affiliate_terms = new FS_AffiliateTerms( $affiliate_terms );
14826
- }
14827
- }
14828
-
14829
- /**
14830
- * @author Leo Fajardo (@leorw)
14831
- * @since 1.2.4
14832
- */
14833
- private function fetch_affiliate_and_custom_terms() {
14834
- if ( ! empty( $this->_storage->affiliate_application_data ) ) {
14835
- $application_data = $this->_storage->affiliate_application_data;
14836
- $flush = ( ! isset( $application_data['status'] ) || 'pending' === $application_data['status'] );
14837
-
14838
- $users_api = $this->get_api_user_scope();
14839
- $result = $users_api->get( "/plugins/{$this->_plugin->id}/aff/{$this->plugin_affiliate_terms->id}/affiliates.json", $flush );
14840
- if ( $this->is_api_result_object( $result, 'affiliates' ) ) {
14841
- if ( ! empty( $result->affiliates ) ) {
14842
- $affiliate = new FS_Affiliate( $result->affiliates[0] );
14843
-
14844
- if ( ! isset( $application_data['status'] ) || $application_data['status'] !== $affiliate->status ) {
14845
- $application_data['status'] = $affiliate->status;
14846
- $this->_storage->affiliate_application_data = $application_data;
14847
- }
14848
-
14849
- if ( $affiliate->is_using_custom_terms ) {
14850
- $affiliate_terms = $users_api->get( "/plugins/{$this->_plugin->id}/affiliates/{$affiliate->id}/aff/{$affiliate->custom_affiliate_terms_id}.json", $flush );
14851
- if ( $this->is_api_result_entity( $affiliate_terms ) ) {
14852
- $this->custom_affiliate_terms = new FS_AffiliateTerms( $affiliate_terms );
14853
- }
14854
- }
14855
-
14856
- $this->affiliate = $affiliate;
14857
- }
14858
- }
14859
- }
14860
- }
14861
-
14862
- /**
14863
- * @author Leo Fajardo (@leorw)
14864
- * @since 1.2.3
14865
- */
14866
- private function fetch_affiliate_and_terms() {
14867
- $this->_logger->entrance();
14868
-
14869
- $this->fetch_affiliate_terms();
14870
- $this->fetch_affiliate_and_custom_terms();
14871
- }
14872
-
14873
- /**
14874
- * @author Leo Fajardo (@leorw)
14875
- * @since 1.2.3
14876
- *
14877
- * @return FS_Affiliate
14878
- */
14879
- function get_affiliate() {
14880
- return $this->affiliate;
14881
- }
14882
-
14883
-
14884
- /**
14885
- * @author Leo Fajardo (@leorw)
14886
- * @since 1.2.3
14887
- *
14888
- * @return FS_AffiliateTerms
14889
- */
14890
- function get_affiliate_terms() {
14891
- return is_object( $this->custom_affiliate_terms ) ?
14892
- $this->custom_affiliate_terms :
14893
- $this->plugin_affiliate_terms;
14894
- }
14895
-
14896
- /**
14897
- * @author Leo Fajardo (@leorw)
14898
- * @since 1.2.3
14899
- */
14900
- function _submit_affiliate_application() {
14901
- $this->_logger->entrance();
14902
-
14903
- $this->check_ajax_referer( 'submit_affiliate_application' );
14904
-
14905
- if ( ! $this->is_user_admin() ) {
14906
- // Only for admins.
14907
- self::shoot_ajax_failure();
14908
- }
14909
-
14910
- $affiliate = fs_request_get( 'affiliate' );
14911
-
14912
- if ( empty( $affiliate['promotion_methods'] ) ) {
14913
- unset( $affiliate['promotion_methods'] );
14914
- }
14915
-
14916
- if ( ! empty( $affiliate['additional_domains'] ) ) {
14917
- $affiliate['additional_domains'] = array_unique( $affiliate['additional_domains'] );
14918
- }
14919
-
14920
- if ( ! $this->is_registered() ) {
14921
- // Opt in but don't track usage.
14922
- $next_page = $this->opt_in(
14923
- false,
14924
- false,
14925
- false,
14926
- false,
14927
- false,
14928
- false,
14929
- true
14930
- );
14931
-
14932
- if ( is_object( $next_page ) && $this->is_api_error( $next_page ) ) {
14933
- self::shoot_ajax_failure(
14934
- isset( $next_page->error ) ?
14935
- $next_page->error->message :
14936
- var_export( $next_page, true )
14937
- );
14938
- } else if ( $this->is_pending_activation() ) {
14939
- self::shoot_ajax_failure( $this->get_text_inline( 'Account is pending activation.', 'account-is-pending-activation' ) );
14940
- }
14941
- }
14942
-
14943
- $this->fetch_affiliate_terms();
14944
-
14945
- $api = $this->get_api_user_scope();
14946
- $result = $api->call(
14947
- ( "/plugins/{$this->_plugin->id}/aff/{$this->plugin_affiliate_terms->id}/affiliates.json" ),
14948
- 'post',
14949
- $affiliate
14950
- );
14951
-
14952
- if ( $this->is_api_error( $result ) ) {
14953
- self::shoot_ajax_failure(
14954
- isset( $result->error ) ?
14955
- $result->error->message :
14956
- var_export( $result, true )
14957
- );
14958
- } else {
14959
- if ( $this->_admin_notices->has_sticky( 'affiliate_program' ) ) {
14960
- $this->_admin_notices->remove_sticky( 'affiliate_program' );
14961
- }
14962
-
14963
- $affiliate_application_data = array(
14964
- 'status' => 'pending',
14965
- 'stats_description' => $affiliate['stats_description'],
14966
- 'promotion_method_description' => $affiliate['promotion_method_description'],
14967
- );
14968
-
14969
- if ( ! empty( $affiliate['promotion_methods'] ) ) {
14970
- $affiliate_application_data['promotion_methods'] = $affiliate['promotion_methods'];
14971
- }
14972
-
14973
- if ( ! empty( $affiliate['domain'] ) ) {
14974
- $affiliate_application_data['domain'] = $affiliate['domain'];
14975
- }
14976
-
14977
- if ( ! empty( $affiliate['additional_domains'] ) ) {
14978
- $affiliate_application_data['additional_domains'] = $affiliate['additional_domains'];
14979
- }
14980
-
14981
- $this->_storage->affiliate_application_data = $affiliate_application_data;
14982
- }
14983
-
14984
- // Purge cached affiliate.
14985
- $api->purge_cache( 'affiliate.json' );
14986
-
14987
- self::shoot_ajax_success( $result );
14988
- }
14989
-
14990
- /**
14991
- * @author Leo Fajardo (@leorw)
14992
- * @since 1.2.3
14993
- *
14994
- * @return array|null
14995
- */
14996
- function get_affiliate_application_data() {
14997
- if ( empty( $this->_storage->affiliate_application_data ) ) {
14998
- return null;
14999
- }
15000
-
15001
- return $this->_storage->affiliate_application_data;
15002
- }
15003
-
15004
- #endregion Affiliation ------------------------------------------------------------
15005
-
15006
- #----------------------------------------------------------------------------------
15007
- #region URL Generators
15008
- #----------------------------------------------------------------------------------
15009
-
15010
- /**
15011
- * Alias to pricing_url().
15012
- *
15013
- * @author Vova Feldman (@svovaf)
15014
- * @since 1.0.2
15015
- *
15016
- * @uses pricing_url()
15017
- *
15018
- * @param string $period Billing cycle
15019
- * @param bool $is_trial
15020
- *
15021
- * @return string
15022
- */
15023
- function get_upgrade_url( $period = WP_FS__PERIOD_ANNUALLY, $is_trial = false ) {
15024
- return $this->pricing_url( $period, $is_trial );
15025
- }
15026
-
15027
- /**
15028
- * @author Vova Feldman (@svovaf)
15029
- * @since 1.0.9
15030
- *
15031
- * @uses get_upgrade_url()
15032
- *
15033
- * @return string
15034
- */
15035
- function get_trial_url() {
15036
- return $this->get_upgrade_url( WP_FS__PERIOD_ANNUALLY, true );
15037
- }
15038
-
15039
- /**
15040
- * @author Leo Fajardo (@leorw)
15041
- * @since 2.1.4
15042
- *
15043
- * @param string $new_version
15044
- *
15045
- * @return string
15046
- */
15047
- function version_upgrade_checkout_link( $new_version ) {
15048
- if ( ! is_object( $this->_license ) ) {
15049
- $url = $this->pricing_url();
15050
-
15051
- $purchase_license_text = $this->get_text_inline( 'Buy a license now', 'buy-license-now' );
15052
- } else {
15053
- $subscription = $this->_get_subscription( $this->_license->id );
15054
-
15055
- $url = $this->checkout_url(
15056
- is_object( $subscription ) ?
15057
- ( 1 == $subscription->billing_cycle ? WP_FS__PERIOD_MONTHLY : WP_FS__PERIOD_ANNUALLY ) :
15058
- WP_FS__PERIOD_LIFETIME,
15059
- false,
15060
- array( 'licenses' => $this->_license->quota )
15061
- );
15062
-
15063
- $purchase_license_text = $this->get_text_inline( 'Renew your license now', 'renew-license-now' );
15064
- }
15065
-
15066
- return sprintf(
15067
- $this->get_text_inline( '%s to access version %s security & feature updates, and support.', 'x-for-updates-and-support' ),
15068
- sprintf( '<a href="%s">%s</a>', $url, $purchase_license_text ),
15069
- $new_version
15070
- );
15071
- }
15072
-
15073
- /**
15074
- * Plugin's pricing URL.
15075
- *
15076
- * @author Vova Feldman (@svovaf)
15077
- * @since 1.0.4
15078
- *
15079
- * @param string $billing_cycle Billing cycle
15080
- *
15081
- * @param bool $is_trial
15082
- *
15083
- * @return string
15084
- */
15085
- function pricing_url( $billing_cycle = WP_FS__PERIOD_ANNUALLY, $is_trial = false ) {
15086
- $this->_logger->entrance();
15087
-
15088
- $params = array(
15089
- 'billing_cycle' => $billing_cycle
15090
- );
15091
-
15092
- if ( $is_trial ) {
15093
- $params['trial'] = 'true';
15094
- }
15095
-
15096
- $url = $this->is_addon() ?
15097
- $this->_parent->addon_url( $this->_slug ) :
15098
- $this->_get_admin_page_url( 'pricing', $params );
15099
-
15100
- return $this->apply_filters( 'pricing_url', $url );
15101
- }
15102
-
15103
- /**
15104
- * Checkout page URL.
15105
- *
15106
- * @author Vova Feldman (@svovaf)
15107
- * @since 1.0.6
15108
- *
15109
- * @param string $billing_cycle Billing cycle
15110
- * @param bool $is_trial
15111
- * @param array $extra (optional) Extra parameters, override other query params.
15112
- * @param bool|null $network
15113
- *
15114
- * @return string
15115
- */
15116
- function checkout_url(
15117
- $billing_cycle = WP_FS__PERIOD_ANNUALLY,
15118
- $is_trial = false,
15119
- $extra = array(),
15120
- $network = null
15121
- ) {
15122
- $this->_logger->entrance();
15123
-
15124
- $params = array(
15125
- 'checkout' => 'true',
15126
- 'billing_cycle' => $billing_cycle,
15127
- );
15128
-
15129
- if ( $is_trial ) {
15130
- $params['trial'] = 'true';
15131
- }
15132
-
15133
- /**
15134
- * Params in extra override other params.
15135
- */
15136
- $params = array_merge( $params, $extra );
15137
-
15138
- return $this->_get_admin_page_url( 'pricing', $params, $network );
15139
- }
15140
-
15141
- /**
15142
- * Add-on checkout URL.
15143
- *
15144
- * @author Vova Feldman (@svovaf)
15145
- * @since 1.1.7
15146
- *
15147
- * @param number $addon_id
15148
- * @param number $pricing_id
15149
- * @param string $billing_cycle
15150
- * @param bool $is_trial
15151
- * @param bool|null $network
15152
- *
15153
- * @return string
15154
- */
15155
- function addon_checkout_url(
15156
- $addon_id,
15157
- $pricing_id,
15158
- $billing_cycle = WP_FS__PERIOD_ANNUALLY,
15159
- $is_trial = false,
15160
- $network = null
15161
- ) {
15162
- return $this->checkout_url( $billing_cycle, $is_trial, array(
15163
- 'plugin_id' => $addon_id,
15164
- 'pricing_id' => $pricing_id,
15165
- ), $network );
15166
- }
15167
-
15168
- #endregion
15169
-
15170
- #endregion ------------------------------------------------------------------
15171
-
15172
- /**
15173
- * Check if plugin has any add-ons.
15174
- *
15175
- * @author Vova Feldman (@svovaf)
15176
- * @since 1.0.5
15177
- *
15178
- * @since 1.1.7.3 Base logic only on the parameter provided by the developer in the init function.
15179
- *
15180
- * @return bool
15181
- */
15182
- function has_addons() {
15183
- $this->_logger->entrance();
15184
-
15185
- return $this->_has_addons;
15186
- }
15187
-
15188
- /**
15189
- * Check if plugin can work in anonymous mode.
15190
- *
15191
- * @author Vova Feldman (@svovaf)
15192
- * @since 1.0.9
15193
- *
15194
- * @return bool
15195
- *
15196
- * @deprecated Please use is_enable_anonymous() instead.
15197
- */
15198
- function enable_anonymous() {
15199
- return $this->_enable_anonymous;
15200
- }
15201
-
15202
- /**
15203
- * Check if plugin can work in anonymous mode.
15204
- *
15205
- * @author Vova Feldman (@svovaf)
15206
- * @since 1.1.9
15207
- *
15208
- * @return bool
15209
- */
15210
- function is_enable_anonymous() {
15211
- return $this->_enable_anonymous;
15212
- }
15213
-
15214
- /**
15215
- * Check if plugin is premium only (no free plans).
15216
- *
15217
- * @author Vova Feldman (@svovaf)
15218
- * @since 1.1.9
15219
- *
15220
- * @return bool
15221
- */
15222
- function is_only_premium() {
15223
- return $this->_is_premium_only;
15224
- }
15225
-
15226
- /**
15227
- * Checks if the plugin's type is "plugin". The other type is "theme".
15228
- *
15229
- * @author Leo Fajardo (@leorw)
15230
- * @since 1.2.2
15231
- *
15232
- * @return bool
15233
- */
15234
- function is_plugin() {
15235
- return ( WP_FS__MODULE_TYPE_PLUGIN === $this->_module_type );
15236
- }
15237
-
15238
- /**
15239
- * @author Leo Fajardo (@leorw)
15240
- * @since 1.2.2
15241
- *
15242
- * @return string
15243
- */
15244
- function get_module_type() {
15245
- if ( ! isset( $this->_module_type ) ) {
15246
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
15247
- $this->_module_type = $id_slug_type_path_map[ $this->_module_id ]['type'];
15248
- }
15249
-
15250
- return $this->_module_type;
15251
- }
15252
-
15253
- /**
15254
- * @author Leo Fajardo (@leorw)
15255
- * @since 1.2.2
15256
- *
15257
- * @return string
15258
- */
15259
- function get_plugin_main_file_path() {
15260
- return $this->_plugin_main_file_path;
15261
- }
15262
-
15263
- /**
15264
- * Check if module has a premium code version.
15265
- *
15266
- * Serviceware module might be freemium without any
15267
- * premium code version, where the paid features
15268
- * are all part of the service.
15269
- *
15270
- * @author Vova Feldman (@svovaf)
15271
- * @since 1.2.1.6
15272
- *
15273
- * @return bool
15274
- */
15275
- function has_premium_version() {
15276
- return $this->_has_premium_version;
15277
- }
15278
-
15279
- /**
15280
- * Check if feature supported with current site's plan.
15281
- *
15282
- * @author Vova Feldman (@svovaf)
15283
- * @since 1.0.1
15284
- *
15285
- * @todo IMPLEMENT
15286
- *
15287
- * @param number $feature_id
15288
- *
15289
- * @throws Exception
15290
- */
15291
- function is_feature_supported( $feature_id ) {
15292
- throw new Exception( 'not implemented' );
15293
- }
15294
-
15295
- /**
15296
- * @author Vova Feldman (@svovaf)
15297
- * @since 1.0.1
15298
- *
15299
- * @return bool Is running in SSL/HTTPS
15300
- */
15301
- function is_ssl() {
15302
- return WP_FS__IS_HTTPS;
15303
- }
15304
-
15305
- /**
15306
- * @author Vova Feldman (@svovaf)
15307
- * @since 1.0.9
15308
- *
15309
- * @return bool Is running in AJAX call.
15310
- *
15311
- * @link http://wordpress.stackexchange.com/questions/70676/how-to-check-if-i-am-in-admin-ajax
15312
- */
15313
- static function is_ajax() {
15314
- return ( defined( 'DOING_AJAX' ) && DOING_AJAX );
15315
- }
15316
-
15317
- /**
15318
- * Check if it's an AJAX call targeted for the current module.
15319
- *
15320
- * @author Vova Feldman (@svovaf)
15321
- * @since 1.2.0
15322
- *
15323
- * @param array|string $actions Collection of AJAX actions.
15324
- *
15325
- * @return bool
15326
- */
15327
- function is_ajax_action( $actions ) {
15328
- // Verify it's an ajax call.
15329
- if ( ! self::is_ajax() ) {
15330
- return false;
15331
- }
15332
-
15333
- // Verify the call is relevant for the plugin.
15334
- if ( $this->_module_id != fs_request_get( 'module_id' ) ) {
15335
- return false;
15336
- }
15337
-
15338
- // Verify it's one of the specified actions.
15339
- if ( is_string( $actions ) ) {
15340
- $actions = explode( ',', $actions );
15341
- }
15342
-
15343
- if ( is_array( $actions ) && 0 < count( $actions ) ) {
15344
- $ajax_action = fs_request_get( 'action' );
15345
-
15346
- foreach ( $actions as $action ) {
15347
- if ( $ajax_action === $this->get_action_tag( $action ) ) {
15348
- return true;
15349
- }
15350
- }
15351
- }
15352
-
15353
- return false;
15354
- }
15355
-
15356
- /**
15357
- * Check if it's an AJAX call targeted for current request.
15358
- *
15359
- * @author Vova Feldman (@svovaf)
15360
- * @since 1.2.0
15361
- *
15362
- * @param array|string $actions Collection of AJAX actions.
15363
- * @param number|null $module_id
15364
- *
15365
- * @return bool
15366
- */
15367
- static function is_ajax_action_static( $actions, $module_id = null ) {
15368
- // Verify it's an ajax call.
15369
- if ( ! self::is_ajax() ) {
15370
- return false;
15371
- }
15372
-
15373
-
15374
- if ( ! empty( $module_id ) ) {
15375
- // Verify the call is relevant for the plugin.
15376
- if ( $module_id != fs_request_get( 'module_id' ) ) {
15377
- return false;
15378
- }
15379
- }
15380
-
15381
- // Verify it's one of the specified actions.
15382
- if ( is_string( $actions ) ) {
15383
- $actions = explode( ',', $actions );
15384
- }
15385
-
15386
- if ( is_array( $actions ) && 0 < count( $actions ) ) {
15387
- $ajax_action = fs_request_get( 'action' );
15388
-
15389
- foreach ( $actions as $action ) {
15390
- if ( $ajax_action === self::get_ajax_action_static( $action, $module_id ) ) {
15391
- return true;
15392
- }
15393
- }
15394
- }
15395
-
15396
- return false;
15397
- }
15398
-
15399
- /**
15400
- * @author Vova Feldman (@svovaf)
15401
- * @since 1.1.7
15402
- *
15403
- * @return bool
15404
- */
15405
- static function is_cron() {
15406
- return ( defined( 'DOING_CRON' ) && DOING_CRON );
15407
- }
15408
-
15409
- /**
15410
- * @author Leo Fajardo (@leorw)
15411
- * @since 2.5.0
15412
- *
15413
- * @return bool
15414
- */
15415
- static function is_admin_post() {
15416
- return ( 'admin-post.php' === self::get_current_page() );
15417
- }
15418
-
15419
- /**
15420
- * Check if a real user is visiting the admin dashboard.
15421
- *
15422
- * @author Vova Feldman (@svovaf)
15423
- * @since 1.1.7
15424
- *
15425
- * @return bool
15426
- */
15427
- function is_user_in_admin() {
15428
- return (
15429
- is_admin() &&
15430
- ! self::is_ajax() &&
15431
- ! self::is_cron() &&
15432
- ! self::is_admin_post()
15433
- );
15434
- }
15435
-
15436
- /**
15437
- * Check if a real user is in the customizer view.
15438
- *
15439
- * @author Vova Feldman (@svovaf)
15440
- * @since 1.2.2.7
15441
- *
15442
- * @return bool
15443
- */
15444
- static function is_customizer() {
15445
- return is_customize_preview();
15446
- }
15447
-
15448
- /**
15449
- * Check if running in HTTPS and if site's plan matching the specified plan.
15450
- *
15451
- * @param string $plan
15452
- * @param bool $exact
15453
- *
15454
- * @return bool
15455
- */
15456
- function is_ssl_and_plan( $plan, $exact = false ) {
15457
- return ( $this->is_ssl() && $this->is_plan( $plan, $exact ) );
15458
- }
15459
-
15460
- /**
15461
- * Construct plugin's settings page URL.
15462
- *
15463
- * @author Vova Feldman (@svovaf)
15464
- * @since 1.0.4
15465
- *
15466
- * @param string $page
15467
- * @param array $params
15468
- * @param bool|null $network
15469
- *
15470
- * @return string
15471
- */
15472
- function _get_admin_page_url( $page = '', $params = array(), $network = null ) {
15473
- if ( is_null( $network ) ) {
15474
- $network = (
15475
- $this->_is_network_active &&
15476
- ( fs_is_network_admin() || ! $this->is_delegated_connection() )
15477
- );
15478
- }
15479
-
15480
- if ( 0 < count( $params ) ) {
15481
- foreach ( $params as $k => $v ) {
15482
- $params[ $k ] = urlencode( $v );
15483
- }
15484
- }
15485
-
15486
- $page_param = $this->_menu->get_slug( $page );
15487
-
15488
- if ( empty( $page ) &&
15489
- // Show the opt-in as an overlay for free wp.org themes or themes without any settings page.
15490
- $this->show_opt_in_on_themes_page()
15491
- ) {
15492
- $params[ $this->get_unique_affix() . '_show_optin' ] = 'true';
15493
-
15494
- return add_query_arg(
15495
- $params,
15496
- $this->admin_url( 'themes.php', 'admin', $network )
15497
- );
15498
- }
15499
-
15500
- if ( ! $this->has_settings_menu() ) {
15501
- if ( ! empty( $page ) ) {
15502
- // Module doesn't have a setting page, but since the request is for
15503
- // a specific Freemius page, use the admin.php path.
15504
- return add_query_arg( array_merge( $params, array(
15505
- 'page' => $page_param,
15506
- ) ), $this->admin_url( 'admin.php', 'admin', $network ) );
15507
- } else {
15508
- if ( $this->is_activation_mode() ) {
15509
- /**
15510
- * @author Vova Feldman
15511
- * @since 1.2.1.6
15512
- *
15513
- * If plugin doesn't have a settings page, create one for the opt-in screen.
15514
- */
15515
- return add_query_arg( array_merge( $params, array(
15516
- 'page' => $this->_slug,
15517
- ) ), $this->admin_url( 'admin.php', 'admin', $network ) );
15518
- } else {
15519
- // Plugin without a settings page.
15520
- return add_query_arg(
15521
- $params,
15522
- $this->admin_url( 'plugins.php', 'admin', $network )
15523
- );
15524
- }
15525
- }
15526
- }
15527
-
15528
- // Module has a submenu settings page.
15529
- if ( ! $this->_menu->is_top_level() ) {
15530
- $parent_slug = $this->_menu->get_parent_slug();
15531
- $menu_file = ( false !== strpos( $parent_slug, '.php' ) ) ?
15532
- $parent_slug :
15533
- 'admin.php';
15534
-
15535
- return add_query_arg( array_merge( $params, array(
15536
- 'page' => $page_param,
15537
- ) ), $this->admin_url( $menu_file, 'admin', $network ) );
15538
- }
15539
-
15540
- // Module has a top level CPT settings page.
15541
- if ( $this->_menu->is_cpt() ) {
15542
- if ( empty( $page ) && $this->is_activation_mode() ) {
15543
- return add_query_arg( array_merge( $params, array(
15544
- 'page' => $page_param
15545
- ) ), $this->admin_url( 'admin.php', 'admin', $network ) );
15546
- } else {
15547
- if ( ! empty( $page ) ) {
15548
- $params['page'] = $page_param;
15549
- }
15550
-
15551
- return add_query_arg(
15552
- $params,
15553
- $this->admin_url( $this->_menu->get_raw_slug(), 'admin', $network )
15554
- );
15555
- }
15556
- }
15557
-
15558
- // Module has a custom top level settings page.
15559
- return add_query_arg( array_merge( $params, array(
15560
- 'page' => $page_param,
15561
- ) ), $this->admin_url( 'admin.php', 'admin', $network ) );
15562
- }
15563
-
15564
- #--------------------------------------------------------------------------------
15565
- #region Multisite
15566
- #--------------------------------------------------------------------------------
15567
-
15568
- /**
15569
- * @author Leo Fajardo (@leorw)
15570
- * @since 2.0.0
15571
- *
15572
- * @return bool
15573
- */
15574
- function is_network_active() {
15575
- return $this->_is_network_active;
15576
- }
15577
-
15578
- /**
15579
- * Delegate activation for the given sites in the network (or all sites if `null`) to site admins.
15580
- *
15581
- * @author Leo Fajardo (@leorw)
15582
- * @since 2.0.0
15583
- *
15584
- * @param array|null $sites
15585
- */
15586
- private function delegate_connection( $sites = null ) {
15587
- $this->_logger->entrance();
15588
-
15589
- $this->_admin_notices->remove_sticky( 'connect_account' );
15590
-
15591
- if ( is_null( $sites ) ) {
15592
- // All sites delegation.
15593
- $this->_storage->store( 'is_delegated_connection', true, true, true );
15594
- } else {
15595
- // Specified sites delegation.
15596
- foreach ( $sites as $site ) {
15597
- $this->delegate_site_connection( $site['blog_id'] );
15598
- }
15599
- }
15600
-
15601
- $this->network_upgrade_mode_completed();
15602
- }
15603
-
15604
- /**
15605
- * Delegate specific network site conncetion to the site admin.
15606
- *
15607
- * @author Vova Feldman (@svovaf)
15608
- * @since 2.0.0
15609
- *
15610
- * @param int $blog_id
15611
- */
15612
- private function delegate_site_connection( $blog_id ) {
15613
- $this->_storage->store( 'is_delegated_connection', true, $blog_id, true );
15614
- }
15615
-
15616
- /**
15617
- * Check if super-admin delegated the connection of ALL sites to the site admins.
15618
- *
15619
- * @author Vova Feldman (@svovaf)
15620
- * @since 2.0.0
15621
- *
15622
- * @return bool
15623
- */
15624
- function is_network_delegated_connection() {
15625
- if ( ! $this->_is_network_active ) {
15626
- return false;
15627
- }
15628
-
15629
- return $this->_storage->get( 'is_delegated_connection', false, true );
15630
- }
15631
-
15632
- /**
15633
- * @author Leo Fajardo (@leorw)
15634
- * @since 2.0.0
15635
- *
15636
- * @param int $blog_id
15637
- *
15638
- * @return bool
15639
- */
15640
- function is_site_delegated_connection( $blog_id = 0 ) {
15641
- if ( ! $this->_is_network_active ) {
15642
- return false;
15643
- }
15644
-
15645
- if ( 0 == $blog_id ) {
15646
- $blog_id = get_current_blog_id();
15647
- }
15648
-
15649
- return $this->_storage->get( 'is_delegated_connection', false, $blog_id );
15650
- }
15651
-
15652
- /**
15653
- * Check if delegated the connection. When running within the the network admin,
15654
- * and haven't specified the blog ID, checks if network level delegated. If running
15655
- * within a site admin or specified a blog ID, check if delegated the connection for
15656
- * the current context site.
15657
- *
15658
- * If executed outside the the admin, check if delegated the connection
15659
- * for the current context site OR the whole network.
15660
- *
15661
- * @author Vova Feldman (@svovaf)
15662
- * @since 2.0.0
15663
- *
15664
- * @param int $blog_id If set, checks if network delegated or blog specific delegated.
15665
- *
15666
- * @return bool
15667
- */
15668
- function is_delegated_connection( $blog_id = 0 ) {
15669
- if ( ! $this->_is_network_active ) {
15670
- return false;
15671
- }
15672
-
15673
- if ( fs_is_network_admin() && 0 == $blog_id ) {
15674
- return $this->is_network_delegated_connection();
15675
- }
15676
-
15677
- return (
15678
- $this->is_network_delegated_connection() ||
15679
- $this->is_site_delegated_connection( $blog_id )
15680
- );
15681
- }
15682
-
15683
- /**
15684
- * Check if the current module is active for the site.
15685
- *
15686
- * @author Vova Feldman (@svovaf)
15687
- * @since 2.0.0
15688
- *
15689
- * @param int $blog_id
15690
- *
15691
- * @return bool
15692
- */
15693
- function is_active_for_site( $blog_id ) {
15694
- if ( ! is_multisite() ) {
15695
- // Not a multisite and this code is executed, means that the plugin is active.
15696
- return true;
15697
- }
15698
-
15699
- if ( $this->is_theme() ) {
15700
- // All themes are site level activated.
15701
- return true;
15702
- }
15703
-
15704
- if ( $this->_is_network_active ) {
15705
- // Plugin was network activated so it's active.
15706
- return true;
15707
- }
15708
-
15709
- return in_array( $this->_plugin_basename, (array) get_blog_option( $blog_id, 'active_plugins', array() ) );
15710
- }
15711
-
15712
- /**
15713
- * @todo Implement pagination when accessing the subsites collection.
15714
- *
15715
- * @author Leo Fajardo (@leorw)
15716
- * @since 2.0.0
15717
- *
15718
- * @param int $limit Default to 1,000
15719
- * @param int $offset Default to 0
15720
- *
15721
- * @return array Active & public sites collection.
15722
- */
15723
- static function get_sites( $limit = 1000, $offset = 0 ) {
15724
- if ( ! is_multisite() ) {
15725
- return array();
15726
- }
15727
-
15728
- /**
15729
- * For consistency with get_blog_list() which only return active public sites.
15730
- *
15731
- * @author Vova Feldman (@svovaf)
15732
- */
15733
- $args = array(
15734
- /**
15735
- * Commented out in order to handle the migration of site options whether the site is public or not.
15736
- *
15737
- * @author Leo Fajardo (@leorw)
15738
- * @since 2.2.1
15739
- */
15740
- // 'public' => 1,
15741
- 'archived' => 0,
15742
- 'mature' => 0,
15743
- 'spam' => 0,
15744
- 'deleted' => 0,
15745
- 'number' => $limit,
15746
- 'offset' => $offset,
15747
- );
15748
-
15749
- if ( function_exists( 'get_sites' ) ) {
15750
- // For WP 4.6 and above.
15751
- return get_sites( $args );
15752
- } else if ( function_exists( 'wp_' . 'get_sites' ) ) {
15753
- // For WP 3.7 to WP 4.5.
15754
- /**
15755
- * This is a hack suggested previously proposed by the TRT. Our SDK is compliant with older WP versions and we'd like to keep it that way.
15756
- *
15757
- * @todo Remove this hack once this false-positive error is removed from the Theme Sniffer.
15758
- *
15759
- * @since 2.3.3
15760
- * @author Vova Feldman (@svovaf)
15761
- */
15762
- $fn = 'wp_' . 'get_sites';
15763
- return $fn( $args );
15764
- } else {
15765
- // For WP 3.6 and below.
15766
- return get_blog_list( 0, 'all' );
15767
- }
15768
- }
15769
-
15770
- /**
15771
- * Checks if a given blog is active.
15772
- *
15773
- * @author Vova Feldman (@svovaf)
15774
- * @since 2.0.0
15775
- *
15776
- * @param $blog_id
15777
- *
15778
- * @return bool
15779
- */
15780
- private static function is_site_active( $blog_id ) {
15781
- global $wpdb;
15782
-
15783
- $blog_info = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d", $blog_id ) );
15784
-
15785
- if ( ! is_object( $blog_info ) ) {
15786
- return false;
15787
- }
15788
-
15789
- return (
15790
- true == $blog_info->public &&
15791
- false == $blog_info->archived &&
15792
- false == $blog_info->mature &&
15793
- false == $blog_info->spam &&
15794
- false == $blog_info->deleted
15795
- );
15796
- }
15797
-
15798
- /**
15799
- * Get a mapping between the site addresses to their blog IDs.
15800
- *
15801
- * @author Vova Feldman (@svovaf)
15802
- * @since 2.0.0
15803
- *
15804
- * @return array {
15805
- * @key string Site address without protocol with a trailing slash.
15806
- * @value int Site's blog ID.
15807
- * }
15808
- */
15809
- private function get_address_to_blog_map() {
15810
- $sites = self::get_sites();
15811
-
15812
- // Map site addresses to their blog IDs.
15813
- $address_to_blog_map = array();
15814
- foreach ( $sites as $site ) {
15815
- $blog_id = self::get_site_blog_id( $site );
15816
- $address = trailingslashit( fs_strip_url_protocol( get_site_url( $blog_id ) ) );
15817
- $address_to_blog_map[ $address ] = $blog_id;
15818
- }
15819
-
15820
- return $address_to_blog_map;
15821
- }
15822
-
15823
- /**
15824
- * Get a mapping between the site addresses to their blog IDs.
15825
- *
15826
- * @author Vova Feldman (@svovaf)
15827
- * @since 2.0.0
15828
- *
15829
- * @return array {
15830
- * @key int Site's blog ID.
15831
- * @value FS_Site Associated install.
15832
- * }
15833
- */
15834
- function get_blog_install_map() {
15835
- $sites = self::get_sites();
15836
-
15837
- // Map site blog ID to its install.
15838
- $install_map = array();
15839
-
15840
- foreach ( $sites as $site ) {
15841
- $blog_id = self::get_site_blog_id( $site );
15842
- $install = $this->get_install_by_blog_id( $blog_id );
15843
-
15844
- if ( is_object( $install ) ) {
15845
- $install_map[ $blog_id ] = $install;
15846
- }
15847
- }
15848
-
15849
- return $install_map;
15850
- }
15851
-
15852
- /**
15853
- * Gets a map of module IDs that the given user has opted-in to.
15854
- *
15855
- * @author Leo Fajardo (@leorw)
15856
- * @since 2.1.0
15857
- *
15858
- * @param number $fs_user_id
15859
- *
15860
- * @return array {
15861
- * @key number $plugin_id
15862
- * @value bool Always true.
15863
- * }
15864
- */
15865
- private static function get_user_opted_in_module_ids_map( $fs_user_id ) {
15866
- self::$_static_logger->entrance();
15867
-
15868
- if ( ! is_multisite() ) {
15869
- $installs = array_merge(
15870
- self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN ),
15871
- self::get_all_sites( WP_FS__MODULE_TYPE_THEME )
15872
- );
15873
- } else {
15874
- $sites = self::get_sites();
15875
-
15876
- $installs = array();
15877
- foreach ( $sites as $site ) {
15878
- $blog_id = self::get_site_blog_id( $site );
15879
-
15880
- $installs = array_merge(
15881
- $installs,
15882
- self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN, $blog_id ),
15883
- self::get_all_sites( WP_FS__MODULE_TYPE_THEME, $blog_id )
15884
- );
15885
- }
15886
- }
15887
-
15888
- $module_ids_map = array();
15889
- foreach ( $installs as $install ) {
15890
- if ( is_object( $install ) &&
15891
- FS_Site::is_valid_id( $install->id ) &&
15892
- FS_User::is_valid_id( $install->user_id ) &&
15893
- ( $install->user_id == $fs_user_id )
15894
- ) {
15895
- $module_ids_map[ $install->plugin_id ] = true;
15896
- }
15897
- }
15898
-
15899
- return $module_ids_map;
15900
- }
15901
-
15902
- /**
15903
- * @author Leo Fajardo (@leorw)
15904
- *
15905
- * @return null|array {
15906
- * 'install' => FS_Site Module's install,
15907
- * 'blog_id' => string The associated blog ID.
15908
- * }
15909
- */
15910
- function find_first_install() {
15911
- $sites = self::get_sites();
15912
-
15913
- foreach ( $sites as $site ) {
15914
- $blog_id = self::get_site_blog_id( $site );
15915
- $install = $this->get_install_by_blog_id( $blog_id );
15916
-
15917
- if ( is_object( $install ) ) {
15918
- return array(
15919
- 'install' => $install,
15920
- 'blog_id' => $blog_id
15921
- );
15922
- }
15923
- }
15924
-
15925
- return null;
15926
- }
15927
-
15928
- /**
15929
- * Switches the Freemius site level context to a specified blog.
15930
- *
15931
- * @author Vova Feldman (@svovaf)
15932
- * @since 2.0.0
15933
- *
15934
- * @param int $blog_id
15935
- * @param FS_Site $install
15936
- * @param bool $flush
15937
- *
15938
- * @return bool Since 2.3.1 returns if a switch was made.
15939
- */
15940
- function switch_to_blog( $blog_id, FS_Site $install = null, $flush = false ) {
15941
- if ( ! is_numeric( $blog_id ) ) {
15942
- return false;
15943
- }
15944
-
15945
- if ( ! $flush && $blog_id == $this->_context_is_network_or_blog_id ) {
15946
- return false;
15947
- }
15948
-
15949
- switch_to_blog( $blog_id );
15950
- $this->_context_is_network_or_blog_id = $blog_id;
15951
-
15952
- self::$_accounts->set_site_blog_context( $blog_id );
15953
- $this->_storage->set_site_blog_context( $blog_id );
15954
- $this->_storage->set_network_active( $this->_is_network_active, $this->is_delegated_connection( $blog_id ) );
15955
-
15956
- $this->_site = is_object( $install ) ?
15957
- $install :
15958
- $this->get_install_by_blog_id( $blog_id );
15959
-
15960
- $this->_user = false;
15961
- $this->_licenses = false;
15962
- $this->_license = null;
15963
- $this->is_whitelabeled = null;
15964
-
15965
- if ( is_object( $this->_site ) ) {
15966
- // Try to fetch user from install.
15967
- $this->_user = self::_get_user_by_id( $this->_site->user_id );
15968
-
15969
- if ( ! is_object( $this->_user ) &&
15970
- FS_User::is_valid_id( $this->_storage->prev_user_id )
15971
- ) {
15972
- // Try to fetch previously saved user.
15973
- $this->_user = self::_get_user_by_id( $this->_storage->prev_user_id );
15974
-
15975
- if ( ! is_object( $this->_user ) ) {
15976
- // Fallback to network's user.
15977
- $this->_user = $this->get_network_user();
15978
- }
15979
- }
15980
-
15981
- $all_plugin_licenses = self::get_all_licenses( $this->_module_id );
15982
-
15983
- if ( ! empty( $all_plugin_licenses ) ) {
15984
- if ( ! FS_Plugin_License::is_valid_id( $this->_site->license_id ) ) {
15985
- $this->_license = null;
15986
- } else {
15987
- $license_found = false;
15988
- foreach ( $all_plugin_licenses as $license ) {
15989
- if ( $license->id == $this->_site->license_id ) {
15990
- // License found.
15991
- $this->_license = $license;
15992
- $license_found = true;
15993
- break;
15994
- }
15995
- }
15996
-
15997
- if ( $license_found ) {
15998
- $this->link_license_2_user( $this->_license->id, $this->_user->id );
15999
- }
16000
- }
16001
-
16002
- $this->_licenses = $this->get_user_licenses( $this->_user->id );
16003
- }
16004
- }
16005
-
16006
- unset( $this->_site_api );
16007
- unset( $this->_user_api );
16008
-
16009
- return true;
16010
- }
16011
-
16012
- /**
16013
- * Restore the blog context to the blog that originally loaded the module.
16014
- *
16015
- * @author Vova Feldman (@svovaf)
16016
- * @since 2.0.0
16017
- */
16018
- function restore_current_blog() {
16019
- $this->switch_to_blog( $this->_blog_id );
16020
- }
16021
-
16022
- /**
16023
- * @author Vova Feldman (@svovaf)
16024
- * @since 2.0.0
16025
- *
16026
- * @param array|WP_Site $site
16027
- *
16028
- * @return int
16029
- */
16030
- static function get_site_blog_id( &$site ) {
16031
- return ( $site instanceof WP_Site ) ?
16032
- $site->blog_id :
16033
- ( is_object( $site ) && isset( $site->userblog_id ) ?
16034
- $site->userblog_id :
16035
- $site['blog_id'] );
16036
- }
16037
-
16038
- /**
16039
- * @author Leo Fajardo (@leorw)
16040
- * @since 2.0.0
16041
- *
16042
- * @param array|WP_Site|null $site
16043
- *
16044
- * @return array
16045
- */
16046
- function get_site_info( $site = null ) {
16047
- $this->_logger->entrance();
16048
-
16049
- $switched = false;
16050
-
16051
- if ( is_null( $site ) ) {
16052
- $url = get_site_url();
16053
- $name = get_bloginfo( 'name' );
16054
- $blog_id = null;
16055
- } else {
16056
- $blog_id = self::get_site_blog_id( $site );
16057
-
16058
- if ( get_current_blog_id() != $blog_id ) {
16059
- switch_to_blog( $blog_id );
16060
- $switched = true;
16061
- }
16062
-
16063
- if ( $site instanceof WP_Site ) {
16064
- $url = $site->siteurl;
16065
- $name = $site->blogname;
16066
- } else {
16067
- $url = get_site_url( $blog_id );
16068
- $name = get_bloginfo( 'name' );
16069
- }
16070
- }
16071
-
16072
- $info = array(
16073
- 'uid' => $this->get_anonymous_id( $blog_id ),
16074
- 'url' => $url,
16075
- 'title' => $name,
16076
- 'language' => get_bloginfo( 'language' ),
16077
- 'charset' => get_bloginfo( 'charset' ),
16078
- );
16079
-
16080
- if ( is_numeric( $blog_id ) ) {
16081
- $info['blog_id'] = $blog_id;
16082
- }
16083
-
16084
- if ( $switched ) {
16085
- restore_current_blog();
16086
- }
16087
-
16088
- return $info;
16089
- }
16090
-
16091
- /**
16092
- * Load the module's install based on the blog ID.
16093
- *
16094
- * @author Vova Feldman (@svovaf)
16095
- * @since 2.0.0
16096
- *
16097
- * @param int|null $blog_id
16098
- *
16099
- * @return FS_Site
16100
- */
16101
- function get_install_by_blog_id( $blog_id = null ) {
16102
- $installs = self::get_all_sites( $this->_module_type, $blog_id );
16103
- $install = isset( $installs[ $this->_slug ] ) ? $installs[ $this->_slug ] : null;
16104
-
16105
- if ( is_object( $install ) &&
16106
- is_numeric( $install->id ) &&
16107
- is_numeric( $install->user_id ) &&
16108
- FS_Plugin_Plan::is_valid_id( $install->plan_id )
16109
- ) {
16110
- // Load site.
16111
- $install = clone $install;
16112
- }
16113
-
16114
- return $install;
16115
- }
16116
-
16117
- /**
16118
- * Check if module is installed on a specified site.
16119
- *
16120
- * @author Vova Feldman (@svovaf)
16121
- * @since 2.0.0
16122
- *
16123
- * @param int|null $blog_id
16124
- *
16125
- * @return bool
16126
- */
16127
- function is_installed_on_site( $blog_id = null ) {
16128
- $installs = self::get_all_sites( $this->_module_type, $blog_id );
16129
- $install = isset( $installs[ $this->_slug ] ) ? $installs[ $this->_slug ] : null;
16130
-
16131
- return (
16132
- is_object( $install ) &&
16133
- is_numeric( $install->id ) &&
16134
- is_numeric( $install->user_id ) &&
16135
- FS_Plugin_Plan::is_valid_id( $install->plan_id )
16136
- );
16137
- }
16138
-
16139
- /**
16140
- * Check if super-admin connected at least one site via the network opt-in.
16141
- *
16142
- * @author Vova Feldman (@svovaf)
16143
- * @since 2.0.0
16144
- *
16145
- * @return bool
16146
- */
16147
- function is_network_registered() {
16148
- if ( ! $this->_is_network_active ) {
16149
- return false;
16150
- }
16151
-
16152
- return FS_User::is_valid_id( $this->_storage->network_user_id );
16153
- }
16154
-
16155
- /**
16156
- * Returns the main user associated with the network.
16157
- *
16158
- * @author Vova Feldman (@svovaf)
16159
- * @since 2.0.0
16160
- *
16161
- * @return FS_User
16162
- */
16163
- function get_network_user() {
16164
- if ( ! $this->_is_network_active ) {
16165
- return null;
16166
- }
16167
-
16168
- return FS_User::is_valid_id( $this->_storage->network_user_id ) ?
16169
- self::_get_user_by_id( $this->_storage->network_user_id ) :
16170
- null;
16171
- }
16172
-
16173
- /**
16174
- * Returns the current context user or the network's main user.
16175
- *
16176
- * @author Vova Feldman (@svovaf)
16177
- * @since 2.0.0
16178
- *
16179
- * @return FS_User
16180
- */
16181
- function get_current_or_network_user() {
16182
- return ( $this->_user instanceof FS_User ) ?
16183
- $this->_user :
16184
- $this->get_network_user();
16185
- }
16186
-
16187
- /**
16188
- * Returns the main install associated with the network.
16189
- *
16190
- * @author Vova Feldman (@svovaf)
16191
- * @since 2.0.0
16192
- *
16193
- * @return FS_Site
16194
- */
16195
- function get_network_install() {
16196
- if ( ! $this->_is_network_active ) {
16197
- return null;
16198
- }
16199
-
16200
- return FS_Site::is_valid_id( $this->_storage->network_install_blog_id ) ?
16201
- $this->get_install_by_blog_id( $this->_storage->network_install_blog_id ) :
16202
- null;
16203
- }
16204
-
16205
- /**
16206
- * Returns the blog ID that is associated with the main install.
16207
- *
16208
- * @author Leo Fajardo (@leorw)
16209
- * @since 2.0.0
16210
- *
16211
- * @return int|null
16212
- */
16213
- function get_network_install_blog_id() {
16214
- if ( ! $this->_is_network_active ) {
16215
- return null;
16216
- }
16217
-
16218
- return FS_Site::is_valid_id( $this->_storage->network_install_blog_id ) ?
16219
- $this->_storage->network_install_blog_id :
16220
- null;
16221
- }
16222
-
16223
- /**
16224
- * Returns the current context install or the network's main install.
16225
- *
16226
- * @author Vova Feldman (@svovaf)
16227
- * @since 2.0.0
16228
- *
16229
- * @return FS_Site
16230
- */
16231
- function get_current_or_network_install() {
16232
- return ( $this->_site instanceof FS_Site ) ?
16233
- $this->_site :
16234
- $this->get_network_install();
16235
- }
16236
-
16237
- /**
16238
- * Check if executing a site level action from the network level admin.
16239
- *
16240
- * @author Vova Feldman (@svovaf)
16241
- * @since 2.0.0
16242
- *
16243
- * @return false|int If yes, return the requested blog ID.
16244
- */
16245
- private function is_network_level_site_specific_action() {
16246
- if ( ! $this->_is_network_active ) {
16247
- return false;
16248
- }
16249
-
16250
- if ( ! fs_is_network_admin() ) {
16251
- return false;
16252
- }
16253
-
16254
- $blog_id = fs_request_get( 'blog_id', '' );
16255
-
16256
- return is_numeric( $blog_id ) ? $blog_id : false;
16257
- }
16258
-
16259
- /**
16260
- * Check if executing an action from the network level admin.
16261
- *
16262
- * @author Vova Feldman (@svovaf)
16263
- * @since 2.0.0
16264
- *
16265
- * @return bool
16266
- */
16267
- private function is_network_level_action() {
16268
- return ( $this->_is_network_active && fs_is_network_admin() );
16269
- }
16270
-
16271
- /**
16272
- * Needs to be executed after site deactivation, archive, deletion, or flag as spam.
16273
- * The logic updates the network level user and blog, and reschedule the crons if the cron executing site matching the site that is no longer publicly active.
16274
- *
16275
- * @author Vova Feldman (@svovaf)
16276
- * @since 2.0.0
16277
- *
16278
- * @param int $context_blog_id
16279
- */
16280
- private function update_multisite_data_after_site_deactivation( $context_blog_id = 0 ) {
16281
- $this->_logger->entrance();
16282
-
16283
- if ( $this->_is_network_active ) {
16284
- if ( $context_blog_id == $this->_storage->network_install_blog_id ) {
16285
- $installs_map = $this->get_blog_install_map();
16286
-
16287
- foreach ( $installs_map as $blog_id => $install ) {
16288
- /**
16289
- * @var FS_Site $install
16290
- */
16291
- if ( $context_blog_id == $blog_id ) {
16292
- continue;
16293
- }
16294
-
16295
- if ( $install->user_id != $this->_storage->network_user_id ) {
16296
- continue;
16297
- }
16298
-
16299
- // Switch reference to a blog that is opted-in and belong to the same super-admin.
16300
- $this->_storage->network_install_blog_id = $blog_id;
16301
- break;
16302
- }
16303
- }
16304
- }
16305
-
16306
- if ( ! $this->is_registered() ) {
16307
- return;
16308
- }
16309
-
16310
- if ( $this->is_sync_cron_scheduled() &&
16311
- $context_blog_id == $this->get_sync_cron_blog_id()
16312
- ) {
16313
- $this->schedule_sync_cron( WP_FS__SCRIPT_START_TIME, true, $context_blog_id );
16314
- }
16315
-
16316
- if ( $this->is_install_sync_scheduled() &&
16317
- $context_blog_id == $this->get_install_sync_cron_blog_id()
16318
- ) {
16319
- $this->schedule_install_sync( $context_blog_id );
16320
- }
16321
- }
16322
-
16323
- /**
16324
- * Executed after site deactivation, archive, or flag as spam.
16325
- *
16326
- * @author Vova Feldman (@svovaf)
16327
- * @since 2.0.0
16328
- *
16329
- * @param int $context_blog_id
16330
- */
16331
- public function _after_site_deactivated_callback( $context_blog_id = 0 ) {
16332
- $this->_logger->entrance();
16333
-
16334
- $install = $this->get_install_by_blog_id( $context_blog_id );
16335
-
16336
- if ( ! is_object( $install ) ) {
16337
- // Site not connected.
16338
- return;
16339
- }
16340
-
16341
- $this->update_multisite_data_after_site_deactivation( $context_blog_id );
16342
-
16343
- if ( ! $this->is_registered() ) {
16344
- return;
16345
- }
16346
-
16347
- $current_blog_id = get_current_blog_id();
16348
-
16349
- $this->switch_to_blog( $context_blog_id );
16350
-
16351
- // Send deactivation event.
16352
- $this->sync_install( array(
16353
- 'is_active' => false,
16354
- ) );
16355
-
16356
- $this->switch_to_blog( $current_blog_id );
16357
- }
16358
-
16359
- /**
16360
- * Executed after site deletion.
16361
- *
16362
- * @author Vova Feldman (@svovaf)
16363
- * @since 2.0.0
16364
- *
16365
- * @param int $context_blog_id
16366
- * @param bool $drop True if site's database tables should be dropped. Default is false.
16367
- */
16368
- public function _after_site_deleted_callback( $context_blog_id = 0, $drop = false ) {
16369
- $this->_logger->entrance();
16370
-
16371
- $install = $this->get_install_by_blog_id( $context_blog_id );
16372
-
16373
- if ( ! is_object( $install ) ) {
16374
- // Site not connected.
16375
- return;
16376
- }
16377
-
16378
- $this->update_multisite_data_after_site_deactivation( $context_blog_id );
16379
-
16380
- if ( ! $this->is_registered() ) {
16381
- return;
16382
- }
16383
-
16384
- $current_blog_id = get_current_blog_id();
16385
-
16386
- $this->switch_to_blog( $context_blog_id );
16387
-
16388
- if ( $drop ) {
16389
- // Delete install if dropping site DB.
16390
- $this->delete_account_event();
16391
- } else {
16392
- // Send deactivation event.
16393
- $this->sync_install( array(
16394
- 'is_active' => false,
16395
- ) );
16396
- }
16397
-
16398
- $this->switch_to_blog( $current_blog_id );
16399
- }
16400
-
16401
- /**
16402
- * Executed after site deletion, called from wp_delete_site
16403
- *
16404
- * @author Dario Curvino (@dudo)
16405
- * @since 2.5.0
16406
- *
16407
- * @param WP_Site $old_site
16408
- */
16409
- public function _after_wpsite_deleted_callback( WP_Site $old_site ) {
16410
- $this->_logger->entrance();
16411
-
16412
- $this->_after_site_deleted_callback( $old_site->blog_id, true );
16413
- }
16414
-
16415
- /**
16416
- * Executed after site re-activation.
16417
- *
16418
- * @author Vova Feldman (@svovaf)
16419
- * @since 2.0.0
16420
- *
16421
- * @param int $context_blog_id
16422
- */
16423
- public function _after_site_reactivated_callback( $context_blog_id = 0 ) {
16424
- $this->_logger->entrance();
16425
-
16426
- $install = $this->get_install_by_blog_id( $context_blog_id );
16427
-
16428
- if ( ! is_object( $install ) ) {
16429
- // Site not connected.
16430
- return;
16431
- }
16432
-
16433
- if ( ! self::is_site_active( $context_blog_id ) ) {
16434
- // Site not yet active (can be in spam mode, archived, deleted...).
16435
- return;
16436
- }
16437
-
16438
- $current_blog_id = get_current_blog_id();
16439
-
16440
- $this->switch_to_blog( $context_blog_id );
16441
-
16442
- // Send re-activation event.
16443
- $this->sync_install( array(
16444
- 'is_active' => true,
16445
- ) );
16446
-
16447
- $this->switch_to_blog( $current_blog_id );
16448
- }
16449
-
16450
- #endregion Multisite
16451
-
16452
- /**
16453
- * @author Leo Fajardo (@leorw)
16454
- *
16455
- * @param string $path
16456
- * @param string $scheme
16457
- * @param bool $network
16458
- *
16459
- * @return string
16460
- */
16461
- private function admin_url( $path = '', $scheme = 'admin', $network = true ) {
16462
- return ( $this->_is_network_active && $network ) ?
16463
- network_admin_url( $path, $scheme ) :
16464
- admin_url( $path, $scheme );
16465
- }
16466
-
16467
- /**
16468
- * Check if currently in a specified admin page.
16469
- *
16470
- * @author Vova Feldman (@svovaf)
16471
- * @since 1.2.2.7
16472
- *
16473
- * @param string $page
16474
- *
16475
- * @return bool
16476
- */
16477
- function is_admin_page( $page ) {
16478
- return ( $this->_menu->get_slug( $page ) === fs_request_get( 'page', '', 'get' ) );
16479
- }
16480
-
16481
- /**
16482
- * Check if currently in the product's main admin page.
16483
- *
16484
- * @author Vova Feldman (@svovaf)
16485
- * @since 2.3.1
16486
- *
16487
- * @return bool
16488
- */
16489
- function is_main_admin_page() {
16490
- return $this->is_admin_page( '' );
16491
- }
16492
-
16493
- /**
16494
- * Get module's main admin setting page URL.
16495
- *
16496
- * @author Vova Feldman (@svovaf)
16497
- * @since 1.2.2.7
16498
- *
16499
- * @return string
16500
- */
16501
- function main_menu_url() {
16502
- return $this->_menu->main_menu_url();
16503
- }
16504
-
16505
- /**
16506
- * Check if currently on the theme's setting page or
16507
- * on any of the Freemius added pages (via tabs).
16508
- *
16509
- * @author Vova Feldman (@svovaf)
16510
- * @since 1.2.2.7
16511
- *
16512
- * @return bool
16513
- *
16514
- * @deprecated Please use is_product_settings_page() instead;
16515
- */
16516
- function is_theme_settings_page() {
16517
- return $this->is_product_settings_page();
16518
- }
16519
-
16520
- /**
16521
- * Check if currently on the product's main setting page or on any of the Freemius added pages (via tabs).
16522
- *
16523
- * @author Vova Feldman (@svovaf)
16524
- * @since 1.2.2.7
16525
- *
16526
- * @return bool
16527
- */
16528
- function is_product_settings_page() {
16529
- return fs_starts_with(
16530
- fs_request_get( 'page', '', 'get' ),
16531
- $this->_menu->get_slug()
16532
- );
16533
- }
16534
-
16535
- /**
16536
- * Plugin's account page + sync license URL.
16537
- *
16538
- * @author Vova Feldman (@svovaf)
16539
- * @since 1.1.9.1
16540
- *
16541
- * @param bool|number $plugin_id
16542
- * @param bool $add_action_nonce
16543
- * @param array $params
16544
- *
16545
- * @return string
16546
- */
16547
- function _get_sync_license_url( $plugin_id = false, $add_action_nonce = true, $params = array() ) {
16548
- if ( is_numeric( $plugin_id ) ) {
16549
- $params['plugin_id'] = $plugin_id;
16550
- }
16551
-
16552
- return $this->get_account_url(
16553
- $this->get_unique_affix() . '_sync_license',
16554
- $params,
16555
- $add_action_nonce
16556
- );
16557
- }
16558
-
16559
- /**
16560
- * Plugin's account URL.
16561
- *
16562
- * @author Vova Feldman (@svovaf)
16563
- * @since 1.0.4
16564
- *
16565
- * @param bool|string $action
16566
- * @param array $params
16567
- *
16568
- * @param bool $add_action_nonce
16569
- *
16570
- * @return string
16571
- */
16572
- function get_account_url( $action = false, $params = array(), $add_action_nonce = true ) {
16573
- if ( is_string( $action ) ) {
16574
- $params['fs_action'] = $action;
16575
- }
16576
-
16577
- self::require_pluggable_essentials();
16578
-
16579
- return ( $add_action_nonce && is_string( $action ) ) ?
16580
- fs_nonce_url( $this->_get_admin_page_url( 'account', $params ), $action ) :
16581
- $this->_get_admin_page_url( 'account', $params );
16582
- }
16583
-
16584
- /**
16585
- * @author Vova Feldman (@svovaf)
16586
- * @since 1.2.0
16587
- *
16588
- * @param string $tab
16589
- * @param bool $action
16590
- * @param array $params
16591
- * @param bool $add_action_nonce
16592
- *
16593
- * @return string
16594
- *
16595
- * @uses get_account_url()
16596
- */
16597
- function get_account_tab_url( $tab, $action = false, $params = array(), $add_action_nonce = true ) {
16598
- $params['tab'] = $tab;
16599
-
16600
- return $this->get_account_url( $action, $params, $add_action_nonce );
16601
- }
16602
-
16603
- /**
16604
- * Plugin's account URL.
16605
- *
16606
- * @author Vova Feldman (@svovaf)
16607
- * @since 1.0.4
16608
- *
16609
- * @param bool|string $topic
16610
- * @param bool|string $message
16611
- *
16612
- * @return string
16613
- */
16614
- function contact_url( $topic = false, $message = false ) {
16615
- $params = array();
16616
- if ( is_string( $topic ) ) {
16617
- $params['topic'] = $topic;
16618
- }
16619
- if ( is_string( $message ) ) {
16620
- $params['message'] = $message;
16621
- }
16622
-
16623
- if ( $this->is_addon() ) {
16624
- $params['addon_id'] = $this->get_id();
16625
-
16626
- return $this->get_parent_instance()->_get_admin_page_url( 'contact', $params );
16627
- } else {
16628
- return $this->_get_admin_page_url( 'contact', $params );
16629
- }
16630
- }
16631
-
16632
- /**
16633
- * Add-on direct info URL.
16634
- *
16635
- * @author Vova Feldman (@svovaf)
16636
- * @since 1.1.0
16637
- *
16638
- * @param string $slug
16639
- *
16640
- * @return string
16641
- */
16642
- function addon_url( $slug ) {
16643
- return $this->_get_admin_page_url( 'addons', array(
16644
- 'slug' => $slug
16645
- ) );
16646
- }
16647
-
16648
- /**
16649
- * Add-ons URL.
16650
- *
16651
- * @author Vova Feldman (@svovaf)
16652
- * @since 2.4.5
16653
- *
16654
- * @return string
16655
- */
16656
- function get_addons_url() {
16657
- return $this->_get_admin_page_url( 'addons' );
16658
- }
16659
-
16660
- /* Logger
16661
- ------------------------------------------------------------------------------------------------------------------*/
16662
- /**
16663
- * @param string $id
16664
- * @param bool $prefix_slug
16665
- *
16666
- * @return FS_Logger
16667
- */
16668
- function get_logger( $id = '', $prefix_slug = true ) {
16669
- return FS_Logger::get_logger( ( $prefix_slug ? $this->_slug : '' ) . ( ( ! $prefix_slug || empty( $id ) ) ? '' : '_' ) . $id );
16670
- }
16671
-
16672
- /**
16673
- * Note: This method is used externally so don't delete it.
16674
- *
16675
- * @param $id
16676
- * @param bool $load_options
16677
- * @param bool $prefix_slug
16678
- *
16679
- * @return FS_Option_Manager
16680
- */
16681
- function get_options_manager( $id, $load_options = false, $prefix_slug = true ) {
16682
- return FS_Option_Manager::get_manager( ( $prefix_slug ? $this->_slug : '' ) . ( ( ! $prefix_slug || empty( $id ) ) ? '' : '_' ) . $id, $load_options );
16683
- }
16684
-
16685
- /* Security
16686
- ------------------------------------------------------------------------------------------------------------------*/
16687
- private static function _encrypt( $str ) {
16688
- if ( is_null( $str ) ) {
16689
- return null;
16690
- }
16691
-
16692
- /**
16693
- * The encrypt/decrypt functions are used to protect
16694
- * the user from messing up with some of the sensitive
16695
- * data stored for the module as a JSON in the database.
16696
- *
16697
- * I used the same suggested hack by the theme review team.
16698
- * For more details, look at the function `Base64UrlDecode()`
16699
- * in `./sdk/FreemiusBase.php`.
16700
- *
16701
- * @todo Remove this hack once the base64 error is removed from the Theme Check.
16702
- *
16703
- * @author Vova Feldman (@svovaf)
16704
- * @since 1.2.2
16705
- */
16706
- $fn = 'base64' . '_encode';
16707
-
16708
- return $fn( $str );
16709
- }
16710
-
16711
- static function _decrypt( $str ) {
16712
- if ( is_null( $str ) ) {
16713
- return null;
16714
- }
16715
-
16716
- /**
16717
- * The encrypt/decrypt functions are used to protect
16718
- * the user from messing up with some of the sensitive
16719
- * data stored for the module as a JSON in the database.
16720
- *
16721
- * I used the same suggested hack by the theme review team.
16722
- * For more details, look at the function `Base64UrlDecode()`
16723
- * in `./sdk/FreemiusBase.php`.
16724
- *
16725
- * @todo Remove this hack once the base64 error is removed from the Theme Check.
16726
- *
16727
- * @author Vova Feldman (@svovaf)
16728
- * @since 1.2.2
16729
- */
16730
- $fn = 'base64' . '_decode';
16731
-
16732
- return $fn( $str );
16733
- }
16734
-
16735
- /**
16736
- * @author Vova Feldman (@svovaf)
16737
- * @since 1.0.5
16738
- *
16739
- * @param FS_Entity $entity
16740
- *
16741
- * @return FS_Entity Return an encrypted clone entity.
16742
- */
16743
- private static function _encrypt_entity( FS_Entity $entity ) {
16744
- $clone = clone $entity;
16745
- $props = get_object_vars( $entity );
16746
-
16747
- foreach ( $props as $key => $val ) {
16748
- $clone->{$key} = self::_encrypt( $val );
16749
- }
16750
-
16751
- return $clone;
16752
- }
16753
-
16754
- /**
16755
- * @author Vova Feldman (@svovaf)
16756
- * @since 1.0.5
16757
- *
16758
- * @param FS_Entity $entity
16759
- *
16760
- * @return FS_Entity Return an decrypted clone entity.
16761
- */
16762
- private static function decrypt_entity( FS_Entity $entity ) {
16763
- $clone = clone $entity;
16764
- $props = get_object_vars( $entity );
16765
-
16766
- foreach ( $props as $key => $val ) {
16767
- $clone->{$key} = self::_decrypt( $val );
16768
- }
16769
-
16770
- return $clone;
16771
- }
16772
-
16773
- /**
16774
- * Tries to activate account based on POST params.
16775
- *
16776
- * @author Vova Feldman (@svovaf)
16777
- * @since 1.0.2
16778
- *
16779
- * @deprecated Not in use, outdated.
16780
- */
16781
- function _activate_account() {
16782
- if ( $this->is_registered() ) {
16783
- // Already activated.
16784
- return;
16785
- }
16786
-
16787
- self::_clean_admin_content_section();
16788
-
16789
- if ( fs_request_is_action( 'activate' ) && fs_request_is_post() ) {
16790
- // check_admin_referer( 'activate_' . $this->_plugin->public_key );
16791
-
16792
- // Verify matching plugin details.
16793
- if ( $this->_plugin->id != fs_request_get( 'plugin_id' ) || $this->_slug != fs_request_get( 'plugin_slug' ) ) {
16794
- return;
16795
- }
16796
-
16797
- $user = new FS_User();
16798
- $user->id = fs_request_get( 'user_id' );
16799
- $user->public_key = fs_request_get( 'user_public_key' );
16800
- $user->secret_key = fs_request_get( 'user_secret_key' );
16801
- $user->email = fs_request_get( 'user_email' );
16802
- $user->first = fs_request_get( 'user_first' );
16803
- $user->last = fs_request_get( 'user_last' );
16804
- $user->is_verified = fs_request_get_bool( 'user_is_verified' );
16805
-
16806
- $site = new FS_Site();
16807
- $site->id = fs_request_get( 'install_id' );
16808
- $site->public_key = fs_request_get( 'install_public_key' );
16809
- $site->secret_key = fs_request_get( 'install_secret_key' );
16810
- $site->plan_id = fs_request_get( 'plan_id' );
16811
-
16812
- $plans = array();
16813
- $plans_data = json_decode( urldecode( fs_request_get( 'plans' ) ) );
16814
- foreach ( $plans_data as $p ) {
16815
- $plan = new FS_Plugin_Plan( $p );
16816
- if ( $site->plan_id == $plan->id ) {
16817
- $plan->title = fs_request_get( 'plan_title' );
16818
- $plan->name = fs_request_get( 'plan_name' );
16819
- }
16820
-
16821
- $plans[] = $plan;
16822
- }
16823
-
16824
- $this->_set_account( $user, $site, $plans );
16825
-
16826
- // Reload the page with the keys.
16827
- fs_redirect( $this->_get_admin_page_url() );
16828
- }
16829
- }
16830
-
16831
- /**
16832
- * @author Vova Feldman (@svovaf)
16833
- * @since 1.0.7
16834
- *
16835
- * @param string $email
16836
- *
16837
- * @return FS_User|false
16838
- */
16839
- static function _get_user_by_email( $email ) {
16840
- self::$_static_logger->entrance();
16841
-
16842
- $email = trim( strtolower( $email ) );
16843
-
16844
- $users = self::get_all_users();
16845
-
16846
- if ( is_array( $users ) ) {
16847
- foreach ( $users as $user ) {
16848
- if ( $email === trim( strtolower( $user->email ) ) ) {
16849
- return $user;
16850
- }
16851
- }
16852
- }
16853
-
16854
- return false;
16855
- }
16856
-
16857
- #----------------------------------------------------------------------------------
16858
- #region Account (Loading, Updates & Activation)
16859
- #----------------------------------------------------------------------------------
16860
-
16861
- /***
16862
- * Load account information (user + site).
16863
- *
16864
- * @author Vova Feldman (@svovaf)
16865
- * @since 1.0.1
16866
- */
16867
- private function _load_account() {
16868
- $this->_logger->entrance();
16869
-
16870
- $this->do_action( 'before_account_load' );
16871
-
16872
- $users = self::get_all_users();
16873
- $plans = self::get_all_plans( $this->_module_type );
16874
-
16875
- if ( $this->_logger->is_on() && is_admin() ) {
16876
- $this->_logger->log( 'users = ' . var_export( $users, true ) );
16877
- $this->_logger->log( 'plans = ' . var_export( $plans, true ) );
16878
- }
16879
-
16880
- $site = fs_is_network_admin() ?
16881
- $this->get_network_install() :
16882
- $this->get_install_by_blog_id();
16883
-
16884
- if ( fs_is_network_admin() &&
16885
- $this->is_network_active() &&
16886
- ! is_object( $site ) &&
16887
- FS_Site::is_valid_id( $this->_storage->network_install_blog_id )
16888
- ) {
16889
- $first_install = $this->find_first_install();
16890
-
16891
- if ( is_null( $first_install ) ) {
16892
- unset( $this->_storage->network_install_blog_id );
16893
- } else {
16894
- $site = $first_install['install'];
16895
- $this->_storage->network_install_blog_id = $first_install['blog_id'];
16896
- }
16897
- }
16898
-
16899
- if ( is_object( $site ) &&
16900
- is_numeric( $site->id ) &&
16901
- is_numeric( $site->user_id ) &&
16902
- FS_Plugin_Plan::is_valid_id( $site->plan_id )
16903
- ) {
16904
- // Load site.
16905
- $this->_site = $site;
16906
- }
16907
-
16908
- $user = null;
16909
- if ( fs_is_network_admin() && $this->_is_network_active ) {
16910
- $user = $this->get_network_user();
16911
- }
16912
-
16913
- if ( is_object( $user ) ) {
16914
- $this->_user = clone $user;
16915
- } else if ( $this->_site ) {
16916
- $user = self::_get_user_by_id( $this->_site->user_id );
16917
-
16918
- if ( ! is_object( $user ) && FS_User::is_valid_id( $this->_storage->prev_user_id ) ) {
16919
- /**
16920
- * Try to load the previous owner. This recovery is used for the following use-case:
16921
- * 1. Opt-in
16922
- * 2. Cloning site1 to site2
16923
- * 3. Ownership switch in site1 (same applies for site2)
16924
- * 4. Install data sync on site2
16925
- * 5. Now site2's install is associated with the new owner which does not exists locally.
16926
- */
16927
- $user = self::_get_user_by_id( $this->_storage->prev_user_id );
16928
- }
16929
-
16930
- if ( ! is_object( $user ) ) {
16931
- /**
16932
- * This is a special fault tolerance mechanism to handle a scenario that the user data is missing.
16933
- */
16934
- if (
16935
- ! isset( $this->_storage->user_recovery_from_install_last_attempt_timestamp ) ||
16936
- time() > ( $this->_storage->user_recovery_from_install_last_attempt_timestamp + FS_Clone_Manager::CLONE_RESOLUTION_MAX_EXECUTION_TIME )
16937
- ) {
16938
- $user = $this->sync_user_by_current_install();
16939
- } else {
16940
- return;
16941
- }
16942
-
16943
- if ( is_object( $user ) ) {
16944
- $this->_storage->user_was_recovered_from_install = true;
16945
- } else {
16946
- $this->_storage->user_recovery_from_install_attempts = isset( $this->_storage->user_recovery_from_install_attempts ) ?
16947
- ( $this->_storage->user_recovery_from_install_attempts + 1 ) :
16948
- 1;
16949
-
16950
- if ( $this->_storage->user_recovery_from_install_attempts >= 3 ) {
16951
- $this->delete_current_install( false );
16952
- } else {
16953
- $this->_storage->user_recovery_from_install_last_attempt_timestamp = time();
16954
-
16955
- return;
16956
- }
16957
- }
16958
- }
16959
-
16960
- $this->_user = ( $user instanceof FS_User ) ?
16961
- clone $user :
16962
- null;
16963
- }
16964
-
16965
- if ( is_object( $this->_user ) ) {
16966
- // Load licenses.
16967
- $this->_licenses = $this->get_user_licenses( $this->_user->id );
16968
- }
16969
-
16970
- if ( is_object( $this->_site ) ) {
16971
- // Load plans.
16972
- $this->_plans = isset( $plans[ $this->_slug ] ) ?
16973
- $plans[ $this->_slug ] :
16974
- array();
16975
-
16976
- if ( ! is_array( $this->_plans ) || empty( $this->_plans ) ) {
16977
- $this->_sync_plans();
16978
- } else {
16979
- for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
16980
- if ( $this->_plans[ $i ] instanceof FS_Plugin_Plan ) {
16981
- $this->_plans[ $i ] = self::decrypt_entity( $this->_plans[ $i ] );
16982
- } else {
16983
- unset( $this->_plans[ $i ] );
16984
- }
16985
- }
16986
- }
16987
-
16988
- $this->_license = $this->_get_license_by_id( $this->_site->license_id );
16989
-
16990
- if ( $this->_site->version != $this->get_plugin_version() ) {
16991
- // If stored install version is different than current installed plugin version,
16992
- // then update plugin version event.
16993
- $this->update_plugin_version_event();
16994
- }
16995
- }
16996
-
16997
- if ( true === $this->_storage->require_license_activation &&
16998
- ! fs_request_get_bool( 'require_license', true )
16999
- ) {
17000
- $this->_storage->require_license_activation = false;
17001
- }
17002
-
17003
- if ( $this->is_theme() ) {
17004
- $this->_register_account_hooks();
17005
- }
17006
-
17007
- if (
17008
- $this->is_clone() &&
17009
- empty( FS_Clone_Manager::instance()->get_clone_identification_timestamp() )
17010
- ) {
17011
- FS_Clone_Manager::instance()->store_clone_identification_timestamp();
17012
- }
17013
- }
17014
-
17015
- /**
17016
- * Special user recovery mechanism.
17017
- *
17018
- * @author Vova Feldman (@svovaf)
17019
- * @since 2.0.0
17020
- *
17021
- * @param number|null $site_user_id
17022
- *
17023
- * @return \FS_User|mixed
17024
- */
17025
- private function sync_user_by_current_install( $site_user_id = null ) {
17026
- $site_user_id = FS_Site::is_valid_id( $site_user_id ) ?
17027
- $site_user_id :
17028
- $this->_site->user_id;
17029
-
17030
- $api = $this->get_api_site_scope();
17031
-
17032
- $uid = $this->get_anonymous_id();
17033
- $request_path = "/users/{$site_user_id}.json?uid={$uid}";
17034
-
17035
- $result = $api->get( $request_path, false, WP_FS__TIME_10_MIN_IN_SEC );
17036
-
17037
- if ( $this->is_api_result_entity( $result ) ) {
17038
- $user = new FS_User( $result );
17039
- $this->_user = $user;
17040
- $this->_store_user();
17041
-
17042
- return $user;
17043
- }
17044
-
17045
- $error_code = FS_Api::get_error_code( $result );
17046
-
17047
- if ( in_array( $error_code, array( 'invalid_unique_id', 'user_cannot_be_recovered' ) ) ) {
17048
- /**
17049
- * Those API errors will continue coming and are not recoverable with the
17050
- * current site's data. Therefore, extend the API call's cached result to 7 days.
17051
- */
17052
- $api->update_cache_expiration( $request_path, WP_FS__TIME_WEEK_IN_SEC );
17053
- }
17054
-
17055
- return $result;
17056
- }
17057
-
17058
- /**
17059
- * @author Vova Feldman (@svovaf)
17060
- * @since 1.0.1
17061
- *
17062
- * @param FS_User $user
17063
- * @param FS_Site $site
17064
- * @param bool|array $plans
17065
- */
17066
- private function _set_account( FS_User $user, FS_Site $site, $plans = false ) {
17067
- $site->user_id = $user->id;
17068
-
17069
- $this->_site = $site;
17070
- $this->_user = $user;
17071
- if ( false !== $plans ) {
17072
- $this->_plans = $plans;
17073
- }
17074
-
17075
- $this->send_install_update();
17076
-
17077
- $this->_store_account();
17078
-
17079
- }
17080
-
17081
- /**
17082
- * Get a sanitized array with the WordPress version, SDK version, and PHP version.
17083
- * Each version is trimmed after the 16th char.
17084
- *
17085
- * @author Vova Feldman (@svovaf)
17086
- * @since 2.2.1
17087
- *
17088
- * @return array
17089
- */
17090
- private function get_versions() {
17091
- $versions = array();
17092
- $versions['platform_version'] = get_bloginfo( 'version' );
17093
- $versions['sdk_version'] = $this->version;
17094
- $versions['programming_language_version'] = phpversion();
17095
-
17096
- foreach ( $versions as $k => $version ) {
17097
- if ( is_string( $versions[ $k ] ) && ! empty( $versions[ $k ] ) ) {
17098
- $versions[ $k ] = substr( $versions[ $k ], 0, 16 );
17099
- }
17100
- }
17101
-
17102
- return $versions;
17103
- }
17104
-
17105
- /**
17106
- * @author Leo Fajardo (@leorw)
17107
- * @since 2.3.0
17108
- *
17109
- * @return bool
17110
- */
17111
- function has_beta_update() {
17112
- return (
17113
- ! empty( $this->_storage->beta_data ) &&
17114
- ( true === $this->_storage->beta_data['is_beta'] ) &&
17115
- version_compare( $this->_storage->beta_data['version'], $this->get_plugin_version(), '>' )
17116
- );
17117
- }
17118
-
17119
- /**
17120
- * @author Leo Fajardo (@leorw)
17121
- * @since 2.3.0
17122
- *
17123
- * @return bool
17124
- */
17125
- function is_beta() {
17126
- return (
17127
- ! empty( $this->_storage->beta_data ) &&
17128
- ( true === $this->_storage->beta_data['is_beta'] ) &&
17129
- ( $this->get_plugin_version() === $this->_storage->beta_data['version'] )
17130
- );
17131
- }
17132
-
17133
- /**
17134
- * @author Vova Feldman (@svovaf)
17135
- * @since 1.1.7.4
17136
- *
17137
- * @param array $override_with
17138
- * @param bool|int|null $network_level_or_blog_id If true, return params for network level opt-in. If integer, get params for specified blog in the network.
17139
- *
17140
- * @return array
17141
- */
17142
- function get_opt_in_params( $override_with = array(), $network_level_or_blog_id = null ) {
17143
- $this->_logger->entrance();
17144
-
17145
- $current_user = self::_get_current_wp_user();
17146
-
17147
- $activation_action = $this->get_unique_affix() . '_activate_new';
17148
- $return_url = $this->is_anonymous() ?
17149
- // If skipped already, then return to the account page.
17150
- $this->get_account_url( $activation_action, array(), false ) :
17151
- // Return to the module's main page.
17152
- $this->get_after_activation_url( 'after_connect_url', array( 'fs_action' => $activation_action ) );
17153
-
17154
- $versions = $this->get_versions();
17155
-
17156
- $params = array_merge( $versions, array(
17157
- 'user_firstname' => $current_user->user_firstname,
17158
- 'user_lastname' => $current_user->user_lastname,
17159
- 'user_nickname' => $current_user->user_nicename,
17160
- 'user_email' => $current_user->user_email,
17161
- 'user_ip' => WP_FS__REMOTE_ADDR,
17162
- 'plugin_slug' => $this->_slug,
17163
- 'plugin_id' => $this->get_id(),
17164
- 'plugin_public_key' => $this->get_public_key(),
17165
- 'plugin_version' => $this->get_plugin_version(),
17166
- 'return_url' => fs_nonce_url( $return_url, $activation_action ),
17167
- 'account_url' => fs_nonce_url( $this->_get_admin_page_url(
17168
- 'account',
17169
- array( 'fs_action' => 'sync_user' )
17170
- ), 'sync_user' ),
17171
- 'is_premium' => $this->is_premium(),
17172
- 'is_active' => true,
17173
- 'is_uninstalled' => false,
17174
- ) );
17175
-
17176
- if ( $this->is_addon() ) {
17177
- $parent_fs = $this->get_parent_instance();
17178
-
17179
- $params['parent_plugin_slug'] = $parent_fs->_slug;
17180
- $params['parent_plugin_id'] = $parent_fs->get_id();
17181
- }
17182
-
17183
- if ( true === $network_level_or_blog_id ) {
17184
- if ( ! isset( $override_with['sites'] ) ) {
17185
- $params['sites'] = $this->get_sites_for_network_level_optin();
17186
- }
17187
- } else {
17188
- $site = is_numeric( $network_level_or_blog_id ) ?
17189
- array( 'blog_id' => $network_level_or_blog_id ) :
17190
- null;
17191
-
17192
- $site = $this->get_site_info( $site );
17193
-
17194
- $params = array_merge( $params, array(
17195
- 'site_uid' => $site['uid'],
17196
- 'site_url' => $site['url'],
17197
- 'site_name' => $site['title'],
17198
- 'language' => $site['language'],
17199
- 'charset' => $site['charset'],
17200
- ) );
17201
- }
17202
-
17203
- if ( $this->is_pending_activation() &&
17204
- ! empty( $this->_storage->pending_license_key )
17205
- ) {
17206
- $params['license_key'] = $this->_storage->pending_license_key;
17207
- }
17208
-
17209
- if ( WP_FS__SKIP_EMAIL_ACTIVATION && $this->has_secret_key() ) {
17210
- // Even though rand() is known for its security issues,
17211
- // the timestamp adds another layer of protection.
17212
- // It would be very hard for an attacker to get the secret key form here.
17213
- // Plus, this should never run in production since the secret should never
17214
- // be included in the production version.
17215
- $params['ts'] = WP_FS__SCRIPT_START_TIME;
17216
- $params['salt'] = md5( uniqid( rand() ) );
17217
- $params['secure'] = md5(
17218
- $params['ts'] .
17219
- $params['salt'] .
17220
- $this->get_secret_key()
17221
- );
17222
- }
17223
-
17224
- if ( is_multisite() && function_exists( 'get_network' ) ) {
17225
- $params['network_uid'] = $this->get_anonymous_network_id();
17226
- }
17227
-
17228
- return array_merge( $params, $override_with );
17229
- }
17230
-
17231
- /**
17232
- * 1. If successful opt-in or pending activation returns the next page that the user should be redirected to.
17233
- * 2. If there was an API error, return the API result.
17234
- *
17235
- * @author Vova Feldman (@svovaf)
17236
- * @since 1.1.7.4
17237
- *
17238
- * @param string|bool $email
17239
- * @param string|bool $first
17240
- * @param string|bool $last
17241
- * @param string|bool $license_key
17242
- * @param bool $is_uninstall If "true", this means that the module is currently being uninstalled.
17243
- * In this case, the user and site info will be sent to the server but no
17244
- * data will be saved to the WP installation's database.
17245
- * @param number|bool $trial_plan_id
17246
- * @param bool $is_disconnected Whether or not to opt in without tracking.
17247
- * @param null|bool $is_marketing_allowed
17248
- * @param array $sites If network-level opt-in, an array of containing details of sites.
17249
- * @param bool $redirect
17250
- *
17251
- * @return string|object
17252
- * @use WP_Error
17253
- */
17254
- function opt_in(
17255
- $email = false,
17256
- $first = false,
17257
- $last = false,
17258
- $license_key = false,
17259
- $is_uninstall = false,
17260
- $trial_plan_id = false,
17261
- $is_disconnected = false,
17262
- $is_marketing_allowed = null,
17263
- $sites = array(),
17264
- $redirect = true
17265
- ) {
17266
- $this->_logger->entrance();
17267
-
17268
- if ( false === $email ) {
17269
- $current_user = self::_get_current_wp_user();
17270
- $email = $current_user->user_email;
17271
- }
17272
-
17273
- /**
17274
- * @since 1.2.1 If activating with license key, ignore the context-user
17275
- * since the user will be automatically loaded from the license.
17276
- */
17277
- if ( empty( $license_key ) ) {
17278
- // Clean up pending license if opt-ing in again.
17279
- $this->_storage->remove( 'pending_license_key' );
17280
-
17281
- if ( ! $is_uninstall ) {
17282
- $fs_user = Freemius::_get_user_by_email( $email );
17283
- if ( is_object( $fs_user ) && ! $this->is_pending_activation() ) {
17284
- return $this->install_with_user(
17285
- $fs_user,
17286
- false,
17287
- $trial_plan_id,
17288
- $redirect,
17289
- true,
17290
- $sites
17291
- );
17292
- }
17293
- }
17294
- }
17295
-
17296
- $user_info = array();
17297
- if ( ! empty( $email ) ) {
17298
- $user_info['user_email'] = $email;
17299
- }
17300
- if ( ! empty( $first ) ) {
17301
- $user_info['user_firstname'] = $first;
17302
- }
17303
- if ( ! empty( $last ) ) {
17304
- $user_info['user_lastname'] = $last;
17305
- }
17306
-
17307
- if ( ! empty( $sites ) ) {
17308
- $is_network = true;
17309
-
17310
- $user_info['sites'] = $sites;
17311
- } else {
17312
- $is_network = false;
17313
- }
17314
-
17315
- $params = $this->get_opt_in_params( $user_info, $is_network );
17316
-
17317
- $filtered_license_key = false;
17318
- if ( is_string( $license_key ) ) {
17319
- $filtered_license_key = $this->apply_filters( 'license_key', $license_key );
17320
- $params['license_key'] = $filtered_license_key;
17321
- } else if ( FS_Plugin_Plan::is_valid_id( $trial_plan_id ) ) {
17322
- $params['trial_plan_id'] = $trial_plan_id;
17323
- }
17324
-
17325
- if ( $is_uninstall ) {
17326
- $params['uninstall_params'] = array(
17327
- 'reason_id' => $this->_storage->uninstall_reason->id,
17328
- 'reason_info' => $this->_storage->uninstall_reason->info
17329
- );
17330
- }
17331
-
17332
- if ( isset( $params['license_key'] ) ) {
17333
- $fs_user = Freemius::_get_user_by_email( $email );
17334
-
17335
- if ( is_object( $fs_user ) ) {
17336
- /**
17337
- * If opting in with a context license and the context WP Admin user already opted in
17338
- * before from the current site, add the user context security params to avoid the
17339
- * unnecessary email activation when the context license is owned by the same context user.
17340
- *
17341
- * @author Leo Fajardo (@leorw)
17342
- * @since 1.2.3
17343
- */
17344
- $params = array_merge( $params, FS_Security::instance()->get_context_params(
17345
- $fs_user,
17346
- false,
17347
- 'install_with_existing_user'
17348
- ) );
17349
- }
17350
- }
17351
-
17352
- if ( is_bool( $is_marketing_allowed ) ) {
17353
- $params['is_marketing_allowed'] = $is_marketing_allowed;
17354
- }
17355
-
17356
- $params['is_disconnected'] = $is_disconnected;
17357
- $params['format'] = 'json';
17358
-
17359
- $request = array(
17360
- 'method' => 'POST',
17361
- 'body' => $params,
17362
- 'timeout' => 60,
17363
- );
17364
-
17365
- $url = $this->add_show_pending( WP_FS__ADDRESS . '/action/service/user/install/' );
17366
- $response = self::safe_remote_post( $url, $request );
17367
-
17368
- if ( is_wp_error( $response ) ) {
17369
- /**
17370
- * @var WP_Error $response
17371
- */
17372
- $result = new stdClass();
17373
-
17374
- $error_code = $response->get_error_code();
17375
- $error_type = str_replace( ' ', '', ucwords( str_replace( '_', ' ', $error_code ) ) );
17376
-
17377
- $result->error = (object) array(
17378
- 'type' => $error_type,
17379
- 'message' => $response->get_error_message(),
17380
- 'code' => $error_code,
17381
- 'http' => 402
17382
- );
17383
-
17384
- $this->maybe_modify_api_curl_error_message( $result );
17385
-
17386
- return $result;
17387
- }
17388
-
17389
- // Module is being uninstalled, don't handle the returned data.
17390
- if ( $is_uninstall ) {
17391
- return true;
17392
- }
17393
-
17394
- /**
17395
- * 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.
17396
- *
17397
- * @author Vova Feldman (@svovaf)
17398
- * @since 1.2.3
17399
- * @link https://themes.trac.wordpress.org/ticket/46134#comment:5
17400
- * @link https://themes.trac.wordpress.org/ticket/46134#comment:9
17401
- * @link https://themes.trac.wordpress.org/ticket/46134#comment:12
17402
- * @link https://themes.trac.wordpress.org/ticket/46134#comment:14
17403
- */
17404
- $decoded = is_string( $response['body'] ) ?
17405
- json_decode( $response['body'] ) :
17406
- null;
17407
-
17408
- if ( empty( $decoded ) ) {
17409
- return false;
17410
- }
17411
-
17412
- if ( ! $this->is_api_result_object( $decoded ) ) {
17413
- if ( ! empty( $params['license_key'] ) ) {
17414
- // Pass the fully entered license key to the failure handler.
17415
- $params['license_key'] = $license_key;
17416
- }
17417
-
17418
- return $is_uninstall ?
17419
- $decoded :
17420
- $this->apply_filters( 'after_install_failure', $decoded, $params );
17421
- } else if ( isset( $decoded->pending_activation ) && $decoded->pending_activation ) {
17422
- if ( $is_network ) {
17423
- $site_ids = array();
17424
- foreach ( $sites as $site ) {
17425
- $site_ids[] = $site['blog_id'];
17426
- }
17427
-
17428
- /**
17429
- * Store the sites so that they can be installed once the user has clicked on the activation link
17430
- * in the email.
17431
- *
17432
- * @author Leo Fajardo (@leorw)
17433
- */
17434
- $this->_storage->pending_sites_info = array(
17435
- 'blog_ids' => $site_ids,
17436
- 'license_key' => $license_key,
17437
- 'trial_plan_id' => $trial_plan_id
17438
- );
17439
- }
17440
-
17441
- // Pending activation, add message.
17442
- return $this->set_pending_confirmation(
17443
- ( isset( $decoded->email ) ?
17444
- $decoded->email :
17445
- true ),
17446
- false,
17447
- $filtered_license_key,
17448
- ! empty( $params['trial_plan_id'] ),
17449
- isset( $decoded->is_suspicious_email ) && $decoded->is_suspicious_email
17450
- );
17451
- } else if ( isset( $decoded->install_secret_key ) ) {
17452
- return $this->install_with_new_user(
17453
- $decoded->user_id,
17454
- $decoded->user_public_key,
17455
- $decoded->user_secret_key,
17456
- ( isset( $decoded->is_marketing_allowed ) && ! is_null( $decoded->is_marketing_allowed ) ?
17457
- $decoded->is_marketing_allowed :
17458
- null ),
17459
- ( isset( $decoded->is_extensions_tracking_allowed ) && ! is_null( $decoded->is_extensions_tracking_allowed ) ?
17460
- $decoded->is_extensions_tracking_allowed :
17461
- null ),
17462
- $decoded->install_id,
17463
- $decoded->install_public_key,
17464
- $decoded->install_secret_key,
17465
- false
17466
- );
17467
- } else if ( is_array( $decoded->installs ) ) {
17468
- return $this->install_many_with_new_user(
17469
- $decoded->user_id,
17470
- $decoded->user_public_key,
17471
- $decoded->user_secret_key,
17472
- ( isset( $decoded->is_marketing_allowed ) && ! is_null( $decoded->is_marketing_allowed ) ?
17473
- $decoded->is_marketing_allowed :
17474
- null ),
17475
- ( isset( $decoded->is_extensions_tracking_allowed ) && ! is_null( $decoded->is_extensions_tracking_allowed ) ?
17476
- $decoded->is_extensions_tracking_allowed :
17477
- null ),
17478
- $decoded->installs,
17479
- false
17480
- );
17481
- }
17482
-
17483
- return $decoded;
17484
- }
17485
-
17486
- /**
17487
- * Set user and site identities.
17488
- *
17489
- * @author Vova Feldman (@svovaf)
17490
- * @since 1.0.9
17491
- *
17492
- * @param FS_User $user
17493
- * @param FS_Site $site
17494
- * @param bool $redirect
17495
- * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will
17496
- * redirect (or return a URL) to the account page with a special parameter to
17497
- * trigger the auto installation processes.
17498
- *
17499
- * @return string If redirect is `false`, returns the next page the user should be redirected to.
17500
- */
17501
- function setup_account(
17502
- FS_User $user,
17503
- FS_Site $site,
17504
- $redirect = true,
17505
- $auto_install = false
17506
- ) {
17507
- return $this->setup_network_account(
17508
- $user,
17509
- array( $site ),
17510
- $redirect,
17511
- $auto_install,
17512
- false
17513
- );
17514
- }
17515
-
17516
- /**
17517
- * Set user and site identities.
17518
- *
17519
- * @author Vova Feldman (@svovaf)
17520
- * @since 2.0.0
17521
- *
17522
- * @param FS_User $user
17523
- * @param FS_Site[] $installs
17524
- * @param bool $redirect
17525
- * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will redirect (or return a URL) to the account page with a special parameter to trigger the auto installation processes.
17526
- * @param bool $is_network_level_opt_in
17527
- *
17528
- * @return string If redirect is `false`, returns the next page the user should be redirected to.
17529
- */
17530
- function setup_network_account(
17531
- FS_User $user,
17532
- array $installs,
17533
- $redirect = true,
17534
- $auto_install = false,
17535
- $is_network_level_opt_in = true
17536
- ) {
17537
- $first_install = $installs[0];
17538
-
17539
- $this->_user = $user;
17540
- $this->_site = $first_install;
17541
-
17542
- $this->_sync_plans();
17543
-
17544
- if ( $this->_storage->handle_gdpr_admin_notice &&
17545
- $this->should_handle_gdpr_admin_notice() &&
17546
- FS_GDPR_Manager::instance()->should_show_opt_in_notice()
17547
- ) {
17548
- /**
17549
- * Clear user lock after an opt-in.
17550
- */
17551
- require_once WP_FS__DIR_INCLUDES . '/class-fs-user-lock.php';
17552
- FS_User_Lock::instance()->unlock();
17553
- }
17554
-
17555
- if ( 1 < count( $installs ) ) {
17556
- // Only network level opt-in can have more than one install.
17557
- $is_network_level_opt_in = true;
17558
- }
17559
- // $is_network_level_opt_in = self::is_ajax_action_static( 'network_activate', $this->_module_id );
17560
- // If Freemius was OFF before, turn it on.
17561
- $this->turn_on();
17562
-
17563
- $this->handle_account_connection(
17564
- $installs,
17565
- ( ! $this->_is_network_active || ! $is_network_level_opt_in )
17566
- );
17567
-
17568
- if ( is_numeric( $first_install->license_id ) ) {
17569
- $this->set_license( $this->_get_license_by_id( $first_install->license_id ) );
17570
- }
17571
-
17572
- $this->_admin_notices->remove_sticky( 'connect_account' );
17573
-
17574
- if ( $this->is_pending_activation() || ! $this->has_settings_menu() ) {
17575
- // Remove pending activation sticky notice (if still exist).
17576
- $this->_admin_notices->remove_sticky( 'activation_pending' );
17577
-
17578
- // Remove plugin from pending activation mode.
17579
- unset( $this->_storage->is_pending_activation );
17580
-
17581
- if ( ! $this->is_paying_or_trial() ) {
17582
- $this->_admin_notices->add_sticky(
17583
- sprintf( $this->get_text_inline( '%s activation was successfully completed.', 'plugin-x-activation-message' ), '<b>' . $this->get_plugin_name() . '</b>' ),
17584
- 'activation_complete'
17585
- );
17586
- }
17587
- }
17588
-
17589
- if ( $this->is_paying_or_trial() ) {
17590
- if ( ! $this->is_premium() ||
17591
- ! $this->has_premium_version() ||
17592
- ! $this->has_settings_menu()
17593
- ) {
17594
- if ( $this->is_paying() ) {
17595
- $this->_admin_notices->add_sticky(
17596
- sprintf(
17597
- $this->get_text_inline( 'Your account was successfully activated with the %s plan.', 'activation-with-plan-x-message' ),
17598
- $this->get_plan_title()
17599
- ) . $this->get_complete_upgrade_instructions(),
17600
- 'plan_upgraded',
17601
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
17602
- );
17603
- } else {
17604
- $trial_plan = $this->get_trial_plan();
17605
-
17606
- $this->_admin_notices->add_sticky(
17607
- sprintf(
17608
- $this->get_text_inline( 'Your trial has been successfully started.', 'trial-started-message' ),
17609
- '<i>' . $this->get_plugin_name() . '</i>'
17610
- ) . $this->get_complete_upgrade_instructions( $trial_plan->title ),
17611
- 'trial_started',
17612
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
17613
- );
17614
- }
17615
- }
17616
-
17617
- $this->_admin_notices->remove_sticky( array(
17618
- 'trial_promotion',
17619
- ) );
17620
- }
17621
-
17622
- $plugin_id = fs_request_get( 'plugin_id', false );
17623
-
17624
- // Store activation time ONLY for plugins & themes (not add-ons).
17625
- if ( ! is_numeric( $plugin_id ) || ( $plugin_id == $this->_plugin->id ) ) {
17626
- if ( empty( $this->_storage->activation_timestamp ) ) {
17627
- $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME;
17628
- }
17629
- }
17630
-
17631
- $next_page = '';
17632
-
17633
- $extra = array();
17634
- if ( $auto_install ) {
17635
- $extra['auto_install'] = 'true';
17636
- }
17637
-
17638
- if ( is_numeric( $plugin_id ) ) {
17639
- /**
17640
- * @author Leo Fajardo (@leorw)
17641
- * @since 1.2.1.6
17642
- *
17643
- * Also sync the license after an anonymous user subscribes.
17644
- */
17645
- if ( $this->is_anonymous() || $plugin_id != $this->_plugin->id ) {
17646
- // Add-on was installed - sync license right after install.
17647
- $next_page = $this->_get_sync_license_url( $plugin_id, true, $extra );
17648
- }
17649
- } else {
17650
- /**
17651
- * @author Vova Feldman (@svovaf)
17652
- * @since 1.1.9 If site installed with a valid license, sync license.
17653
- */
17654
- if ( $this->is_paying() ) {
17655
- $this->_sync_plugin_license(
17656
- true,
17657
- // Installs data is already synced in the beginning of this method directly or via _set_account().
17658
- false
17659
- );
17660
- }
17661
-
17662
- // Reload the page with the keys.
17663
- $next_page = $this->is_anonymous() ?
17664
- // If user previously skipped, redirect to account page.
17665
- $this->get_account_url( false, $extra ) :
17666
- $this->get_after_activation_url( 'after_connect_url', array(), $is_network_level_opt_in );
17667
- }
17668
-
17669
- if ( ! empty( $next_page ) && $redirect ) {
17670
- fs_redirect( $next_page );
17671
- }
17672
-
17673
- return $next_page;
17674
- }
17675
-
17676
- /**
17677
- * Install plugin with new user information after approval.
17678
- *
17679
- * @author Vova Feldman (@svovaf)
17680
- * @since 1.0.7
17681
- */
17682
- function _install_with_new_user() {
17683
- $this->_logger->entrance();
17684
-
17685
- if ( $this->is_registered() ) {
17686
- return;
17687
- }
17688
-
17689
- if ( ( $this->is_plugin() && fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) ||
17690
- // @todo This logic should be improved because it's executed on every load of a theme.
17691
- $this->is_theme()
17692
- ) {
17693
- // check_admin_referer( $this->_slug . '_activate_new' );
17694
-
17695
- if ( fs_request_has( 'user_secret_key' ) ) {
17696
- if ( fs_is_network_admin() && isset( $this->_storage->pending_sites_info ) ) {
17697
- $pending_sites_info = $this->_storage->pending_sites_info;
17698
-
17699
- $this->install_many_pending_with_user(
17700
- fs_request_get( 'user_id' ),
17701
- fs_request_get( 'user_public_key' ),
17702
- fs_request_get( 'user_secret_key' ),
17703
- fs_request_get_bool( 'is_marketing_allowed', null ),
17704
- fs_request_get_bool( 'is_extensions_tracking_allowed', null ),
17705
- $pending_sites_info['blog_ids'],
17706
- $pending_sites_info['license_key'],
17707
- $pending_sites_info['trial_plan_id']
17708
- );
17709
- } else {
17710
- $this->install_with_new_user(
17711
- fs_request_get( 'user_id' ),
17712
- fs_request_get( 'user_public_key' ),
17713
- fs_request_get( 'user_secret_key' ),
17714
- fs_request_get_bool( 'is_marketing_allowed', null ),
17715
- fs_request_get_bool( 'is_extensions_tracking_allowed', null ),
17716
- fs_request_get( 'install_id' ),
17717
- fs_request_get( 'install_public_key' ),
17718
- fs_request_get( 'install_secret_key' ),
17719
- true,
17720
- fs_request_get_bool( 'auto_install' )
17721
- );
17722
- }
17723
- } else if ( fs_request_has( 'pending_activation' ) ) {
17724
- $this->set_pending_confirmation(
17725
- fs_request_get( 'user_email' ),
17726
- true,
17727
- false,
17728
- false,
17729
- fs_request_get_bool( 'is_suspicious_email' )
17730
- );
17731
- }
17732
- }
17733
- }
17734
-
17735
- /**
17736
- * @author Vova Feldman (@svovaf)
17737
- * @since 2.0.0
17738
- *
17739
- * @param number $id
17740
- * @param string $public_key
17741
- * @param string $secret_key
17742
- *
17743
- * @return \FS_User
17744
- */
17745
- private function setup_user( $id, $public_key, $secret_key ) {
17746
- $user = self::_get_user_by_id( $id );
17747
-
17748
- if ( is_object( $user ) ) {
17749
- $this->_user = $user;
17750
- } else {
17751
- $user = new FS_User();
17752
- $user->id = $id;
17753
- $user->public_key = $public_key;
17754
- $user->secret_key = $secret_key;
17755
-
17756
- $this->_user = $user;
17757
- $user_result = $this->get_api_user_scope()->get();
17758
- $user = new FS_User( $user_result );
17759
-
17760
- $this->_user = $user;
17761
- $this->_store_user();
17762
- }
17763
-
17764
- return $user;
17765
- }
17766
-
17767
- /**
17768
- * Install plugin with new user.
17769
- *
17770
- * @author Vova Feldman (@svovaf)
17771
- * @since 1.1.7.4
17772
- *
17773
- * @param number $user_id
17774
- * @param string $user_public_key
17775
- * @param string $user_secret_key
17776
- * @param bool|null $is_marketing_allowed
17777
- * @param bool|null $is_extensions_tracking_allowed Since 2.3.2
17778
- * @param number $install_id
17779
- * @param string $install_public_key
17780
- * @param string $install_secret_key
17781
- * @param bool $redirect
17782
- * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will redirect (or return a URL) to the account page with a special parameter to trigger the auto installation processes.
17783
- *
17784
- * @return string If redirect is `false`, returns the next page the user should be redirected to.
17785
- */
17786
- private function install_with_new_user(
17787
- $user_id,
17788
- $user_public_key,
17789
- $user_secret_key,
17790
- $is_marketing_allowed,
17791
- $is_extensions_tracking_allowed,
17792
- $install_id,
17793
- $install_public_key,
17794
- $install_secret_key,
17795
- $redirect = true,
17796
- $auto_install = false
17797
- ) {
17798
- /**
17799
- * This method is also executed after opting in with a license key since the
17800
- * license can be potentially associated with a different owner.
17801
- *
17802
- * @since 2.0.0
17803
- */
17804
- $user = self::_get_user_by_id( $user_id );
17805
-
17806
- if ( ! is_object( $user ) ) {
17807
- $user = new FS_User();
17808
- $user->id = $user_id;
17809
- $user->public_key = $user_public_key;
17810
- $user->secret_key = $user_secret_key;
17811
-
17812
- $this->_user = $user;
17813
- $user_result = $this->get_api_user_scope()->get();
17814
- $user = new FS_User( $user_result );
17815
- }
17816
-
17817
- $this->_user = $user;
17818
-
17819
- $site = new FS_Site();
17820
- $site->id = $install_id;
17821
- $site->public_key = $install_public_key;
17822
- $site->secret_key = $install_secret_key;
17823
-
17824
- $this->_site = $site;
17825
- $site_result = $this->get_api_site_scope( true )->get();
17826
- $site = new FS_Site( $site_result );
17827
- $this->_site = $site;
17828
-
17829
- if ( ! is_null( $is_marketing_allowed ) ) {
17830
- $this->disable_opt_in_notice_and_lock_user();
17831
- }
17832
-
17833
- $this->update_extensions_tracking_flag( $is_extensions_tracking_allowed );
17834
-
17835
- return $this->setup_account(
17836
- $this->_user,
17837
- $this->_site,
17838
- $redirect,
17839
- $auto_install
17840
- );
17841
- }
17842
-
17843
- /**
17844
- * Install plugin with user.
17845
- *
17846
- * @author Leo Fajardo (@leorw)
17847
- * @since 2.0.0
17848
- *
17849
- * @param number $user_id
17850
- * @param string $user_public_key
17851
- * @param string $user_secret_key
17852
- * @param bool|null $is_marketing_allowed
17853
- * @param bool|null $is_extensions_tracking_allowed Since 2.3.2
17854
- * @param array $site_ids
17855
- * @param bool $license_key
17856
- * @param bool $trial_plan_id
17857
- * @param bool $redirect
17858
- *
17859
- * @return string If redirect is `false`, returns the next page the user should be redirected to.
17860
- */
17861
- private function install_many_pending_with_user(
17862
- $user_id,
17863
- $user_public_key,
17864
- $user_secret_key,
17865
- $is_marketing_allowed,
17866
- $is_extensions_tracking_allowed,
17867
- $site_ids,
17868
- $license_key = false,
17869
- $trial_plan_id = false,
17870
- $redirect = true
17871
- ) {
17872
- $user = $this->setup_user( $user_id, $user_public_key, $user_secret_key );
17873
-
17874
- if ( ! is_null( $is_marketing_allowed ) ) {
17875
- $this->disable_opt_in_notice_and_lock_user();
17876
- }
17877
-
17878
- $this->update_extensions_tracking_flag( $is_extensions_tracking_allowed );
17879
-
17880
- $sites = array();
17881
- foreach ( $site_ids as $site_id ) {
17882
- $sites[] = $this->get_site_info( array( 'blog_id' => $site_id ) );
17883
- }
17884
-
17885
- $this->install_with_user( $user, $license_key, $trial_plan_id, $redirect, true, $sites );
17886
- }
17887
-
17888
- /**
17889
- * Multi-site install with a new user.
17890
- *
17891
- * @author Vova Feldman (@svovaf)
17892
- * @since 2.0.0
17893
- *
17894
- * @param number $user_id
17895
- * @param string $user_public_key
17896
- * @param string $user_secret_key
17897
- * @param bool|null $is_marketing_allowed
17898
- * @param bool|null $is_extensions_tracking_allowed Since 2.3.2
17899
- * @param object[] $installs
17900
- * @param bool $redirect
17901
- * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will redirect (or return a URL) to the account page with a special parameter to trigger the auto installation processes.
17902
- *
17903
- * @return string If redirect is `false`, returns the next page the user should be redirected to.
17904
- */
17905
- private function install_many_with_new_user(
17906
- $user_id,
17907
- $user_public_key,
17908
- $user_secret_key,
17909
- $is_marketing_allowed,
17910
- $is_extensions_tracking_allowed,
17911
- array $installs,
17912
- $redirect = true,
17913
- $auto_install = false
17914
- ) {
17915
- $this->setup_user( $user_id, $user_public_key, $user_secret_key );
17916
-
17917
- if ( ! is_null( $is_marketing_allowed ) ) {
17918
- $this->disable_opt_in_notice_and_lock_user();
17919
- }
17920
-
17921
- $this->update_extensions_tracking_flag( $is_extensions_tracking_allowed );
17922
-
17923
- $install_ids = array();
17924
-
17925
- foreach ( $installs as $install ) {
17926
- $install_ids[] = $install->id;
17927
- }
17928
-
17929
- $left = count( $install_ids );
17930
- $offset = 0;
17931
-
17932
- $installs = array();
17933
- while ( $left > 0 ) {
17934
- $result = $this->get_api_user_scope()->get( "/plugins/{$this->_module_id}/installs.json?ids=" . implode( ',', array_slice( $install_ids, $offset, 25 ) ) );
17935
-
17936
- if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
17937
- // @todo Handle API error.
17938
- }
17939
-
17940
- $installs = array_merge( $installs, $result->installs );
17941
-
17942
- $left -= 25;
17943
- }
17944
-
17945
- foreach ( $installs as &$install ) {
17946
- $install = new FS_Site( $install );
17947
- }
17948
-
17949
- return $this->setup_network_account(
17950
- $this->_user,
17951
- $installs,
17952
- $redirect,
17953
- $auto_install
17954
- );
17955
- }
17956
-
17957
- /**
17958
- * @author Vova Feldman (@svovaf)
17959
- * @since 1.1.7.4
17960
- *
17961
- * @param string|bool $email
17962
- * @param bool $redirect
17963
- * @param string|bool $license_key Since 1.2.1.5
17964
- * @param bool $is_pending_trial Since 1.2.1.5
17965
- *
17966
- * @return string Since 1.2.1.5 if $redirect is `false`, return the pending activation page.
17967
- */
17968
- private function set_pending_confirmation(
17969
- $email = false,
17970
- $redirect = true,
17971
- $license_key = false,
17972
- $is_pending_trial = false,
17973
- $is_suspicious_email = false
17974
- ) {
17975
- if ( $this->_ignore_pending_mode ) {
17976
- /**
17977
- * If explicitly asked to ignore pending mode, set to anonymous mode
17978
- * if require confirmation before finalizing the opt-in.
17979
- *
17980
- * @author Vova Feldman
17981
- * @since 1.2.1.6
17982
- */
17983
- $this->skip_connection( null, fs_is_network_admin() );
17984
- } else {
17985
- // Install must be activated via email since
17986
- // user with the same email already exist.
17987
- $this->_storage->is_pending_activation = true;
17988
- $this->_add_pending_activation_notice( $email, $is_pending_trial, $is_suspicious_email );
17989
- }
17990
-
17991
- if ( ! empty( $license_key ) ) {
17992
- $this->_storage->pending_license_key = $license_key;
17993
- }
17994
-
17995
- // Remove the opt-in sticky notice.
17996
- $this->_admin_notices->remove_sticky( array(
17997
- 'connect_account',
17998
- 'trial_promotion',
17999
- ) );
18000
-
18001
- $next_page = $this->get_after_activation_url( 'after_pending_connect_url' );
18002
-
18003
- // Reload the page with with pending activation message.
18004
- if ( $redirect ) {
18005
- fs_redirect( $next_page );
18006
- }
18007
-
18008
- return $next_page;
18009
- }
18010
-
18011
- /**
18012
- * Install plugin with current logged WP user info.
18013
- *
18014
- * @author Vova Feldman (@svovaf)
18015
- * @since 1.0.7
18016
- */
18017
- function _install_with_current_user() {
18018
- $this->_logger->entrance();
18019
-
18020
- if ( $this->is_registered() ) {
18021
- return;
18022
- }
18023
-
18024
- if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) && fs_request_is_post() ) {
18025
- // check_admin_referer( 'activate_existing_' . $this->_plugin->public_key );
18026
-
18027
- /**
18028
- * @author Vova Feldman (@svovaf)
18029
- * @since 1.1.9 Add license key if given.
18030
- */
18031
- $license_key = fs_request_get( 'license_secret_key' );
18032
-
18033
- $this->update_extensions_tracking_flag( fs_request_get_bool( 'is_extensions_tracking_allowed', null ) );
18034
-
18035
- $this->install_with_current_user( $license_key );
18036
- }
18037
- }
18038
-
18039
-
18040
- /**
18041
- * @author Vova Feldman (@svovaf)
18042
- * @since 1.1.7.4
18043
- *
18044
- * @param string|bool $license_key
18045
- * @param number|bool $trial_plan_id
18046
- * @param array $sites Since 2.0.0
18047
- * @param bool $redirect
18048
- *
18049
- * @return object|string If redirect is `false`, returns the next page the user should be redirected to, or the API error object if failed to install.
18050
- */
18051
- function install_with_current_user(
18052
- $license_key = false,
18053
- $trial_plan_id = false,
18054
- $sites = array(),
18055
- $redirect = true
18056
- ) {
18057
- // Get current logged WP user.
18058
- $current_user = self::_get_current_wp_user();
18059
-
18060
- // Find the relevant FS user by the email.
18061
- $user = self::_get_user_by_email( $current_user->user_email );
18062
-
18063
- return $this->install_with_user( $user, $license_key, $trial_plan_id, $redirect, true, $sites );
18064
- }
18065
-
18066
- /**
18067
- * @author Vova Feldman (@svovaf)
18068
- * @since 2.0.0
18069
- *
18070
- * @param \FS_User $user
18071
- * @param string|bool $license_key
18072
- * @param number|bool $trial_plan_id
18073
- * @param bool $redirect
18074
- * @param bool $setup_account Since 2.0.0. When set to FALSE, executes a light installation without setting up the account as if it's the first opt-in.
18075
- * @param array $sites Since 2.0.0. If not empty, should be a collection of site details for the bulk install API request.
18076
- *
18077
- * @return \FS_Site|object|string If redirect is `false`, returns the next page the user should be redirected to, or the API error object if failed to install. If $setup_account is set to `false`, return the newly created install.
18078
- */
18079
- function install_with_user(
18080
- FS_User $user,
18081
- $license_key = false,
18082
- $trial_plan_id = false,
18083
- $redirect = true,
18084
- $setup_account = true,
18085
- $sites = array()
18086
- ) {
18087
- // We have to set the user before getting user scope API handler.
18088
- $this->_user = $user;
18089
-
18090
- // Install the plugin.
18091
- $result = $this->create_installs_with_user(
18092
- $user,
18093
- $license_key,
18094
- $trial_plan_id,
18095
- $sites,
18096
- $redirect
18097
- );
18098
-
18099
- if ( ! $this->is_api_result_entity( $result ) &&
18100
- ! $this->is_api_result_object( $result, 'installs' )
18101
- ) {
18102
- // @todo Handler potential API error of the $result
18103
- }
18104
-
18105
- if ( empty( $sites ) ) {
18106
- $site = new FS_Site( $result );
18107
- $this->_site = $site;
18108
-
18109
- if ( ! $setup_account ) {
18110
- $this->_store_site();
18111
-
18112
- $this->sync_plan_if_not_exist( $site->plan_id );
18113
-
18114
- if ( ! empty( $license_key ) && FS_Plugin_License::is_valid_id( $site->license_id ) ) {
18115
- $this->sync_license_if_not_exist( $site->license_id, $license_key );
18116
- }
18117
-
18118
- $this->_admin_notices->remove_sticky( 'connect_account', false );
18119
-
18120
- return $site;
18121
- }
18122
-
18123
- return $this->setup_account( $this->_user, $this->_site, $redirect );
18124
- } else {
18125
- $installs = array();
18126
- foreach ( $result->installs as $install ) {
18127
- $installs[] = new FS_Site( $install );
18128
- }
18129
-
18130
- return $this->setup_network_account(
18131
- $user,
18132
- $installs,
18133
- $redirect
18134
- );
18135
- }
18136
- }
18137
-
18138
- /**
18139
- * Initiate an API request to create a collection of installs.
18140
- *
18141
- * @author Vova Feldman (@svovaf)
18142
- * @since 2.0.0
18143
- *
18144
- * @param \FS_User $user
18145
- * @param bool $license_key
18146
- * @param bool $trial_plan_id
18147
- * @param array $sites
18148
- * @param bool $redirect
18149
- * @param bool $silent
18150
- *
18151
- * @return object|mixed
18152
- */
18153
- private function create_installs_with_user(
18154
- FS_User $user,
18155
- $license_key = false,
18156
- $trial_plan_id = false,
18157
- $sites = array(),
18158
- $redirect = false,
18159
- $silent = false
18160
- ) {
18161
- $extra_install_params = array(
18162
- 'uid' => $this->get_anonymous_id(),
18163
- 'is_disconnected' => false,
18164
- );
18165
-
18166
- if ( ! empty( $license_key ) ) {
18167
- $extra_install_params['license_key'] = $this->apply_filters( 'license_key', $license_key );
18168
-
18169
- if ( $silent ) {
18170
- $extra_install_params['ignore_license_owner'] = true;
18171
- }
18172
- } else if ( FS_Plugin_Plan::is_valid_id( $trial_plan_id ) ) {
18173
- $extra_install_params['trial_plan_id'] = $trial_plan_id;
18174
- }
18175
-
18176
- if ( ! empty( $sites ) ) {
18177
- $extra_install_params['sites'] = $sites;
18178
- }
18179
-
18180
- $args = $this->get_install_data_for_api( $extra_install_params, false, false );
18181
-
18182
- // Install the plugin.
18183
- $result = $this->get_api_user_scope_by_user( $user )->call(
18184
- "/plugins/{$this->get_id()}/installs.json",
18185
- 'post',
18186
- $args
18187
- );
18188
-
18189
- if ( ! $this->is_api_result_entity( $result ) &&
18190
- ! $this->is_api_result_object( $result, 'installs' )
18191
- ) {
18192
- if ( ! empty( $args['license_key'] ) ) {
18193
- // Pass the fully entered license key to the failure handler.
18194
- $args['license_key'] = $license_key;
18195
- }
18196
-
18197
- $result = $this->apply_filters( 'after_install_failure', $result, $args );
18198
-
18199
- if ( ! $silent ) {
18200
- $this->_admin_notices->add(
18201
- sprintf( $this->get_text_inline( 'Couldn\'t activate %s.', 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
18202
- $this->get_text_inline( 'Please contact us with the following message:', 'contact-us-with-error-message' ) . ' ' . '<b>' . $result->error->message . '</b>',
18203
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
18204
- 'error'
18205
- );
18206
- }
18207
-
18208
- if ( $redirect ) {
18209
- /**
18210
- * We set the user before getting the user scope API handler, so the user became temporarily
18211
- * registered (`is_registered() = true`). Since the API returned an error and we will redirect,
18212
- * we have to set the user to `null`, otherwise, the user will be redirected to the wrong
18213
- * activation page based on the return value of `is_registered()`. In addition, in case the
18214
- * context plugin doesn't have a settings menu and the default page is the `Plugins` page,
18215
- * misleading plugin activation errors will be shown on the `Plugins` page.
18216
- *
18217
- * @author Leo Fajardo (@leorw)
18218
- */
18219
- $this->_user = null;
18220
-
18221
- fs_redirect( $this->get_activation_url( array( 'error' => $result->error->message ) ) );
18222
- }
18223
- }
18224
-
18225
- return $result;
18226
- }
18227
-
18228
- /**
18229
- * Tries to activate add-on account based on parent plugin info.
18230
- *
18231
- * @author Vova Feldman (@svovaf)
18232
- * @since 1.0.6
18233
- *
18234
- * @param Freemius $parent_fs
18235
- * @param bool|int|null $network_level_or_blog_id True for network level opt-in and integer for opt-in for specified blog in the network.
18236
- * @param FS_Plugin_License $bundle_license Since 2.4.0. If provided, this license will be activated for the add-on.
18237
- */
18238
- private function _activate_addon_account(
18239
- Freemius $parent_fs,
18240
- $network_level_or_blog_id = null,
18241
- FS_Plugin_License $bundle_license = null
18242
- ) {
18243
- if ( $this->is_registered() ) {
18244
- // Already activated.
18245
- return;
18246
- }
18247
-
18248
- /**
18249
- * Do not override the `uid` if network-level opt-in since the call to `get_sites_for_network_level_optin()`
18250
- * already returns the data for the current blog.
18251
- *
18252
- * @author Leo Fajardo (@leorw)
18253
- * @since 2.3.0
18254
- */
18255
- $uid_param_to_override = ( true === $network_level_or_blog_id ) ?
18256
- array() :
18257
- array( 'uid' => $this->get_anonymous_id() );
18258
-
18259
- $params = $this->get_install_data_for_api(
18260
- $uid_param_to_override,
18261
- false,
18262
- false,
18263
- /**
18264
- * Do not include the data for the current blog if network-level opt-in since the call to `get_sites_for_network_level_optin`
18265
- * already includes the data for it.
18266
- *
18267
- * @author Leo Fajardo (@leorw)
18268
- * @since 2.3.0
18269
- */
18270
- ( true !== $network_level_or_blog_id )
18271
- );
18272
-
18273
- if ( true === $network_level_or_blog_id ) {
18274
- $params['sites'] = $this->get_sites_for_network_level_optin();
18275
-
18276
- if ( empty( $params['sites'] ) ) {
18277
- return;
18278
- }
18279
- }
18280
-
18281
- if ( is_object( $bundle_license ) ) {
18282
- $params['license_key'] = $bundle_license->secret_key;
18283
- }
18284
-
18285
- // Activate add-on with parent plugin credentials.
18286
- $result = $parent_fs->get_api_site_scope()->call(
18287
- "/addons/{$this->_plugin->id}/installs.json",
18288
- 'post',
18289
- $params
18290
- );
18291
-
18292
- if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
18293
- if ( is_object( $bundle_license ) ) {
18294
- /**
18295
- * When a license object is provided, it's an attempt by the SDK to activate a bundle license and not a user-initiated action, therefore, do not show any admin notice to avoid confusion (e.g.: the notice will show up just above the opt-in link). If the license activation fails, the admin will see an opt-in link instead.
18296
- *
18297
- * @author Leo Fajardo (@leorw)
18298
- * @since 2.4.0
18299
- */
18300
- } else {
18301
- $error_message = FS_Api::is_api_error_object( $result ) ?
18302
- $result->error->message :
18303
- $this->get_text_inline( 'An unknown error has occurred.', 'unknown-error' );
18304
-
18305
- $this->_admin_notices->add(
18306
- sprintf( $this->get_text_inline( 'Couldn\'t activate %s.', 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
18307
- $this->get_text_inline( 'Please contact us with the following message:', 'contact-us-with-error-message' ) . ' ' . '<b>' . $error_message . '</b>',
18308
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
18309
- 'error'
18310
- );
18311
- }
18312
-
18313
- return;
18314
- }
18315
-
18316
- $addon_installs = $result->installs;
18317
- foreach ( $addon_installs as $key => $addon_install ) {
18318
- $addon_installs[ $key ] = new FS_Site( $addon_install );
18319
- }
18320
-
18321
- $first_install = $addon_installs[0];
18322
-
18323
- // Get user information based on parent's plugin.
18324
- $user = $parent_fs->get_user();
18325
-
18326
- // First of all, set site and user info - otherwise we won't
18327
- // be able to invoke API calls.
18328
- $this->_site = $first_install;
18329
- $this->_user = $user;
18330
-
18331
- // Sync add-on plans.
18332
- $this->_sync_plans();
18333
-
18334
- $this->handle_account_connection( $addon_installs, ! fs_is_network_admin() );
18335
-
18336
- // Get site's current plan.
18337
- //$this->_site->plan = $this->_get_plan_by_id( $this->_site->plan->id );
18338
-
18339
- // Sync licenses.
18340
- $this->_sync_licenses();
18341
-
18342
- if ( ! fs_is_network_admin() ) {
18343
- // Try to activate premium license.
18344
- $this->_activate_license( true, $bundle_license );
18345
-
18346
- if ( is_object( $bundle_license ) ) {
18347
- $this->maybe_activate_bundle_license( $bundle_license );
18348
- }
18349
- } else {
18350
- if ( is_object( $bundle_license ) ) {
18351
- $premium_license = $bundle_license;
18352
- } else {
18353
- $license_id = fs_request_get( 'license_id' );
18354
-
18355
- if ( is_object( $this->_site ) &&
18356
- FS_Plugin_License::is_valid_id( $license_id ) &&
18357
- $license_id == $this->_site->license_id
18358
- ) {
18359
- // License is already activated.
18360
- return;
18361
- }
18362
-
18363
- $premium_license = FS_Plugin_License::is_valid_id( $license_id ) ?
18364
- $this->_get_license_by_id( $license_id ) :
18365
- $this->_get_available_premium_license();
18366
- }
18367
-
18368
- if ( is_object( $premium_license ) ) {
18369
- $this->maybe_network_activate_addon_license( $premium_license );
18370
- }
18371
- }
18372
- }
18373
-
18374
- /**
18375
- * @author Leo Fajardo (@leorw)
18376
- * @since 2.3.0
18377
- *
18378
- * @param FS_Site[] $installs
18379
- * @param bool $is_site_level
18380
- */
18381
- private function handle_account_connection( $installs, $is_site_level ) {
18382
- $first_install = $installs[0];
18383
-
18384
- if ( $is_site_level ) {
18385
- $this->_set_account( $this->_user, $first_install );
18386
-
18387
- $this->do_action( 'after_account_connection', $this->_user, $first_install );
18388
- } else {
18389
- $this->_store_user();
18390
-
18391
- // Map site addresses to their blog IDs.
18392
- $address_to_blog_map = $this->get_address_to_blog_map();
18393
-
18394
- $first_blog_id = null;
18395
- $blog_2_install_map = array();
18396
- foreach ( $installs as $install ) {
18397
- $address = trailingslashit( fs_strip_url_protocol( $install->url ) );
18398
- $blog_id = $address_to_blog_map[ $address ];
18399
-
18400
- $this->_store_site( true, $blog_id, $install );
18401
-
18402
- if ( is_null( $first_blog_id ) ) {
18403
- $first_blog_id = $blog_id;
18404
- }
18405
-
18406
- $blog_2_install_map[ $blog_id ] = $install;
18407
- }
18408
-
18409
- if ( ! FS_User::is_valid_id( $this->_storage->network_user_id ) ||
18410
- ! is_object( self::_get_user_by_id( $this->_storage->network_user_id ) )
18411
- ) {
18412
- // Store network user.
18413
- $this->_storage->network_user_id = $this->_user->id;
18414
- }
18415
-
18416
- if ( ! FS_Site::is_valid_id( $this->_storage->network_install_blog_id ) ) {
18417
- $this->_storage->network_install_blog_id = $first_blog_id;
18418
- }
18419
-
18420
- if ( count( $installs ) === count( $address_to_blog_map ) ) {
18421
- // Super admin opted in for all sites in the network.
18422
- $this->_storage->is_network_connected = true;
18423
- }
18424
-
18425
- $this->_store_licenses( false );
18426
-
18427
- self::$_accounts->store();
18428
-
18429
- // Don't sync the installs data on network upgrade
18430
- if ( ! $this->network_upgrade_mode_completed() ) {
18431
- $this->send_installs_update();
18432
- }
18433
-
18434
- $current_blog = get_current_blog_id();
18435
-
18436
- foreach ( $blog_2_install_map as $blog_id => $install ) {
18437
- $this->switch_to_blog( $blog_id );
18438
-
18439
- $this->do_action( 'after_account_connection', $this->_user, $install );
18440
- }
18441
-
18442
- // Switch install context back to the first install.
18443
- $this->switch_to_blog(
18444
- $current_blog,
18445
- $first_install,
18446
- ( $this->_site->id != $first_install->id )
18447
- );
18448
-
18449
- $this->do_action( 'after_network_account_connection', $this->_user, $blog_2_install_map );
18450
- }
18451
- }
18452
-
18453
- /**
18454
- * Tries to activate parent account based on add-on's info.
18455
- *
18456
- * @author Vova Feldman (@svovaf)
18457
- * @since 1.2.2.7
18458
- *
18459
- * @param Freemius $parent_fs
18460
- */
18461
- private function activate_parent_account( Freemius $parent_fs ) {
18462
- if ( ! $this->is_addon() ) {
18463
- // This is not an add-on.
18464
- return;
18465
- }
18466
-
18467
- if ( $parent_fs->is_registered() ) {
18468
- // Already activated.
18469
- return;
18470
- }
18471
-
18472
- // Activate parent with add-on's user credentials.
18473
- $parent_install = $this->get_api_user_scope()->call(
18474
- "/plugins/{$parent_fs->_plugin->id}/installs.json",
18475
- 'post',
18476
- $parent_fs->get_install_data_for_api( array(
18477
- 'uid' => $parent_fs->get_anonymous_id(),
18478
- ), false, false )
18479
- );
18480
-
18481
- if ( isset( $parent_install->error ) ) {
18482
- $this->_admin_notices->add(
18483
- sprintf( $this->get_text_inline( 'Couldn\'t activate %s.', 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
18484
- $this->get_text_inline( 'Please contact us with the following message:', 'contact-us-with-error-message' ) . ' ' . '<b>' . $parent_install->error->message . '</b>',
18485
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
18486
- 'error'
18487
- );
18488
-
18489
- return;
18490
- }
18491
-
18492
- $parent_fs->_admin_notices->remove_sticky( 'connect_account' );
18493
-
18494
- if ( $parent_fs->is_pending_activation() ) {
18495
- $parent_fs->_admin_notices->remove_sticky( 'activation_pending' );
18496
-
18497
- unset( $parent_fs->_storage->is_pending_activation );
18498
- }
18499
-
18500
- // Get user information based on parent's plugin.
18501
- $user = $this->get_user();
18502
-
18503
- // First of all, set site info - otherwise we won't
18504
- // be able to invoke API calls.
18505
- $parent_fs->_site = new FS_Site( $parent_install );
18506
- $parent_fs->_user = $user;
18507
-
18508
- // Sync add-on plans.
18509
- $parent_fs->_sync_plans();
18510
-
18511
- $parent_fs->_set_account( $user, $parent_fs->_site );
18512
- }
18513
-
18514
- #endregion
18515
-
18516
- #----------------------------------------------------------------------------------
18517
- #region Admin Menu Items
18518
- #----------------------------------------------------------------------------------
18519
-
18520
- private $_menu_items = array();
18521
-
18522
- /**
18523
- * @author Vova Feldman (@svovaf)
18524
- * @since 1.2.1.8
18525
- *
18526
- * @return array
18527
- */
18528
- function get_menu_items() {
18529
- return $this->_menu_items;
18530
- }
18531
-
18532
- /**
18533
- * @author Vova Feldman (@svovaf)
18534
- * @since 1.0.7
18535
- *
18536
- * @return string
18537
- */
18538
- function get_menu_slug() {
18539
- return $this->_menu->get_slug();
18540
- }
18541
-
18542
- /**
18543
- * @author Vova Feldman (@svovaf)
18544
- * @since 1.0.9
18545
- */
18546
- function _prepare_admin_menu() {
18547
- // if ( ! $this->is_on() ) {
18548
- // return;
18549
- // }
18550
-
18551
- if ( is_object( $this->_site ) && ! $this->is_registered() ) {
18552
- return;
18553
- }
18554
-
18555
- /**
18556
- * When running from a site admin with a network activated module and the connection
18557
- * was NOT delegated and the user still haven't skipped or opted-in, then hide the
18558
- * site level settings.
18559
- *
18560
- * @author Vova Feldman (@svovaf)
18561
- * @since 2.0.0
18562
- */
18563
- $should_hide_site_admin_settings = (
18564
- $this->_is_network_active &&
18565
- ! fs_is_network_admin() &&
18566
- ! $this->is_delegated_connection() &&
18567
- ! $this->is_anonymous() &&
18568
- ! $this->is_registered()
18569
- );
18570
-
18571
- $should_hide_site_admin_settings = $this->apply_filters( 'should_hide_site_admin_settings_on_network_activation_mode', $should_hide_site_admin_settings );
18572
-
18573
- if ( ( ! $this->has_api_connectivity() && ! $this->is_enable_anonymous() ) ||
18574
- $should_hide_site_admin_settings
18575
- ) {
18576
- $this->_menu->remove_menu_item( $should_hide_site_admin_settings );
18577
- } else {
18578
- $this->do_action( fs_is_network_admin() ?
18579
- 'before_network_admin_menu_init' :
18580
- 'before_admin_menu_init'
18581
- );
18582
-
18583
- $this->add_menu_action();
18584
-
18585
- $this->add_network_menu_when_missing();
18586
-
18587
- $this->add_submenu_items();
18588
- }
18589
- }
18590
-
18591
- /**
18592
- * Admin dashboard menu items modifications.
18593
- *
18594
- * NOTE: admin_menu action executed before admin_init.
18595
- *
18596
- * @author Vova Feldman (@svovaf)
18597
- * @since 1.0.7
18598
- *
18599
- */
18600
- private function add_menu_action() {
18601
- if ( $this->is_activation_mode() ) {
18602
- if ( $this->show_opt_in_on_setting_page() ) {
18603
- $this->override_plugin_menu_with_activation();
18604
- } else {
18605
- /**
18606
- * Handle theme opt-in when the opt-in form shows as a dialog box in the themes page.
18607
- */
18608
- if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) ) {
18609
- add_action( 'load-themes.php', array( &$this, '_install_with_current_user' ) );
18610
- } else if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ||
18611
- fs_request_get_bool( 'pending_activation' )
18612
- ) {
18613
- add_action( 'load-themes.php', array( &$this, '_install_with_new_user' ) );
18614
- }
18615
- }
18616
- } else {
18617
- if ( ! $this->is_registered() ) {
18618
- // If not registered try to install user.
18619
- if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) {
18620
- $this->_install_with_new_user();
18621
- }
18622
- } else if (
18623
- fs_request_is_action( 'sync_user' ) &&
18624
- ( ! $this->has_settings_menu() || $this->show_opt_in_on_themes_page() )
18625
- ) {
18626
- $this->_handle_account_user_sync();
18627
- }
18628
- }
18629
- }
18630
-
18631
- /**
18632
- * @author Vova Feldman (@svovaf)
18633
- * @since 1.0.1
18634
- */
18635
- function _redirect_on_clicked_menu_link() {
18636
- $this->_logger->entrance();
18637
-
18638
- $page = fs_request_get('page');
18639
- $page = is_string($page) ? strtolower($page) : '';
18640
-
18641
- $this->_logger->log( 'page = ' . $page );
18642
-
18643
- foreach ( $this->_menu_items as $priority => $items ) {
18644
- foreach ( $items as $item ) {
18645
- if ( isset( $item['url'] ) ) {
18646
- if ( $page === $this->_menu->get_slug( strtolower( $item['menu_slug'] ) ) ) {
18647
- $this->_logger->log( 'Redirecting to ' . $item['url'] );
18648
-
18649
- fs_redirect( $item['url'] );
18650
- }
18651
- }
18652
- }
18653
- }
18654
- }
18655
-
18656
- /**
18657
- * Remove plugin's all admin menu items & pages, and replace with activation page.
18658
- *
18659
- * @author Vova Feldman (@svovaf)
18660
- * @since 1.0.1
18661
- */
18662
- private function override_plugin_menu_with_activation() {
18663
- $this->_logger->entrance();
18664
-
18665
- $hook = false;
18666
-
18667
- if ( ! $this->has_settings_menu() ) {
18668
- // Add the opt-in page without a menu item.
18669
- $hook = FS_Admin_Menu_Manager::add_subpage(
18670
- null,
18671
- $this->get_plugin_name(),
18672
- $this->get_plugin_name(),
18673
- 'manage_options',
18674
- $this->_slug,
18675
- array( &$this, '_connect_page_render' )
18676
- );
18677
- } else if ( $this->_menu->is_top_level() ) {
18678
- if ( $this->_menu->is_override_exact() ) {
18679
- // Make sure the current page is matching the activation page.
18680
- if ( ! $this->is_matching_url( $this->get_activation_url() ) ) {
18681
- return;
18682
- }
18683
- }
18684
-
18685
- $hook = $this->_menu->override_menu_item( array( &$this, '_connect_page_render' ) );
18686
-
18687
- if ( false === $hook ) {
18688
- // Create new menu item just for the opt-in.
18689
- $hook = FS_Admin_Menu_Manager::add_page(
18690
- $this->get_plugin_name(),
18691
- $this->get_plugin_name(),
18692
- 'manage_options',
18693
- $this->_menu->get_slug(),
18694
- array( &$this, '_connect_page_render' )
18695
- );
18696
- }
18697
- } else {
18698
- $menus = array( $this->_menu->get_parent_slug() );
18699
-
18700
- if ( $this->_menu->is_override_exact() ) {
18701
- // Make sure the current page is matching the activation page.
18702
- if ( ! $this->is_matching_url( $this->get_activation_url() ) ) {
18703
- return;
18704
- }
18705
- }
18706
-
18707
- foreach ( $menus as $parent_slug ) {
18708
- $hook = $this->_menu->override_submenu_action(
18709
- $parent_slug,
18710
- $this->_menu->get_raw_slug(),
18711
- array( &$this, '_connect_page_render' )
18712
- );
18713
-
18714
- if ( false !== $hook ) {
18715
- // Found plugin's submenu item.
18716
- break;
18717
- }
18718
- }
18719
- }
18720
-
18721
- if ( $this->is_activation_page() ) {
18722
- // Clean admin page from distracting content.
18723
- self::_clean_admin_content_section();
18724
- }
18725
-
18726
- if ( false !== $hook ) {
18727
- if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) ) {
18728
- $this->_install_with_current_user();
18729
- } else if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) {
18730
- $this->_install_with_new_user();
18731
- }
18732
- }
18733
- }
18734
-
18735
- /**
18736
- * If a plugin was network activated and connected but don't have a network
18737
- * level settings, then add an artificial menu item for the Account and other
18738
- * Freemius settings.
18739
- *
18740
- * @author Vova Feldman (@svovaf)
18741
- * @since 2.0.0
18742
- */
18743
- private function add_network_menu_when_missing() {
18744
- $this->_logger->entrance();
18745
-
18746
- if ( ! $this->_is_network_active ) {
18747
- // Plugin wasn't activated on the network level.
18748
- return;
18749
- }
18750
-
18751
- if ( ! fs_is_network_admin() ) {
18752
- // The context is not the network admin.
18753
- return;
18754
- }
18755
-
18756
- if ( $this->_menu->has_network_menu() ) {
18757
- // Plugin already has a network level menu.
18758
- return;
18759
- }
18760
-
18761
- if ( $this->is_network_activation_mode() ) {
18762
- /**
18763
- * Do not add during activation mode, otherwise, there will be duplicate menus while the opt-in
18764
- * screen is being shown.
18765
- *
18766
- * @author Leo Fajardo (@leorw)
18767
- */
18768
- return;
18769
- }
18770
-
18771
- if ( ! WP_FS__SHOW_NETWORK_EVEN_WHEN_DELEGATED ) {
18772
- if ( $this->is_network_delegated_connection() ) {
18773
- // Super-admin delegated the connection to the site admins.
18774
- return;
18775
- }
18776
- }
18777
-
18778
- if ( ! $this->_menu->has_menu() || $this->_menu->is_top_level() ) {
18779
-
18780
- if ( $this->_menu->has_menu() ||
18781
- ! $this->is_addon() ||
18782
- $this->is_activation_mode()
18783
- ) {
18784
- $this->_dynamically_added_top_level_page_hook_name = $this->_menu->add_page_and_update(
18785
- $this->get_plugin_name(),
18786
- $this->get_plugin_name(),
18787
- 'manage_options',
18788
- $this->_menu->has_menu() ? $this->_menu->get_slug() : $this->_slug
18789
- );
18790
- }
18791
- } else {
18792
- $this->_menu->add_subpage_and_update(
18793
- $this->_menu->get_parent_slug(),
18794
- $this->get_plugin_name(),
18795
- $this->get_plugin_name(),
18796
- 'manage_options',
18797
- $this->_menu->get_slug()
18798
- );
18799
- }
18800
- }
18801
-
18802
- /**
18803
- * @author Leo Fajardo (@leorw)
18804
- * @since 1.2.1
18805
- *
18806
- * return string
18807
- */
18808
- function get_top_level_menu_capability() {
18809
- global $menu;
18810
-
18811
- $top_level_menu_slug = $this->get_top_level_menu_slug();
18812
-
18813
- foreach ( $menu as $menu_info ) {
18814
- /**
18815
- * The second element in the menu info array is the capability/role that has access to the menu and the
18816
- * third element is the menu slug.
18817
- */
18818
- if ( $menu_info[2] === $top_level_menu_slug ) {
18819
- return $menu_info[1];
18820
- }
18821
- }
18822
-
18823
- return 'read';
18824
- }
18825
-
18826
- /**
18827
- * @author Vova Feldman (@svovaf)
18828
- * @since 1.0.0
18829
- *
18830
- * @return string
18831
- */
18832
- private function get_top_level_menu_slug() {
18833
- return ( $this->is_addon() ?
18834
- $this->get_parent_instance()->_menu->get_top_level_menu_slug() :
18835
- $this->_menu->get_top_level_menu_slug() );
18836
- }
18837
-
18838
- /**
18839
- * @author Vova Feldman (@svovaf)
18840
- * @since 1.2.2.7
18841
- *
18842
- * @return string
18843
- */
18844
- function get_pricing_cta_label() {
18845
- $label = $this->get_text_inline( 'Upgrade', 'upgrade' );
18846
-
18847
- if ( $this->is_in_trial_promotion() &&
18848
- ! $this->is_paying_or_trial()
18849
- ) {
18850
- // If running a trial promotion, modify the pricing to load the trial.
18851
- $label = $this->get_text_inline( 'Start Trial', 'start-trial' );
18852
- } else if ( $this->is_paying() ) {
18853
- $label = $this->get_text_inline( 'Pricing', 'pricing' );
18854
- }
18855
-
18856
- return $label;
18857
- }
18858
-
18859
- /**
18860
- * @author Vova Feldman (@svovaf)
18861
- * @since 1.2.2.7
18862
- *
18863
- * @return bool
18864
- */
18865
- function is_pricing_page_visible() {
18866
- return (
18867
- // Has at least one paid plan.
18868
- $this->has_paid_plan() &&
18869
- // Didn't ask to hide the pricing page.
18870
- $this->is_page_visible( 'pricing' ) &&
18871
- // Don't have a valid active license or has more than one plan.
18872
- ( ! $this->is_paying() || ! $this->is_single_plan( true ) )
18873
- );
18874
- }
18875
-
18876
- /**
18877
- * @author Leo Fajardo (@leorw)
18878
- * @since 2.3.0
18879
- *
18880
- * @param bool $is_activation_mode
18881
- *
18882
- * @return bool
18883
- */
18884
- private function should_add_submenu_or_action_links( $is_activation_mode ) {
18885
- if ( $this->is_addon() ) {
18886
- // No submenu items or action links for add-ons.
18887
- return false;
18888
- }
18889
-
18890
- if ( $this->show_opt_in_on_themes_page() ) {
18891
- if ( ! fs_is_network_admin() ) {
18892
- // Also add action links or submenu items when running in a free .org theme so the tabs will be visible.
18893
- return true;
18894
- }
18895
- } else if ( $is_activation_mode ) {
18896
- // Don't show submenu-items/tabs in activation mode, unless it's a wp.org theme.
18897
- return false;
18898
- }
18899
-
18900
- if ( fs_is_network_admin() ) {
18901
- /**
18902
- * Add submenu items or action links to network level when plugin was network activated and the super
18903
- * admin did NOT delegate the connection of all sites to site admins.
18904
- */
18905
- return (
18906
- $this->_is_network_active &&
18907
- ( WP_FS__SHOW_NETWORK_EVEN_WHEN_DELEGATED ||
18908
- ! $this->is_network_delegated_connection() )
18909
- );
18910
- }
18911
-
18912
- return ( ! $this->_is_network_active || $this->is_delegated_connection() );
18913
- }
18914
-
18915
- /**
18916
- * Add default Freemius menu items.
18917
- *
18918
- * @author Vova Feldman (@svovaf)
18919
- * @since 1.0.0
18920
- * @since 1.2.2.7 Also add submenu items when running in a free .org theme so the tabs will be visible.
18921
- */
18922
- private function add_submenu_items() {
18923
- $this->_logger->entrance();
18924
-
18925
- $is_activation_mode = $this->is_activation_mode();
18926
-
18927
- $add_submenu_items = $this->should_add_submenu_or_action_links( $is_activation_mode );
18928
-
18929
- if ( $add_submenu_items ) {
18930
- if ( $this->has_affiliate_program() ) {
18931
- // Add affiliation page.
18932
- $this->add_submenu_item(
18933
- $this->get_text_inline( 'Affiliation', 'affiliation' ),
18934
- array( &$this, '_affiliation_page_render' ),
18935
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Affiliation', 'affiliation' ),
18936
- 'manage_options',
18937
- 'affiliation',
18938
- 'Freemius::_clean_admin_content_section',
18939
- WP_FS__DEFAULT_PRIORITY,
18940
- $this->is_submenu_item_visible( 'affiliation' )
18941
- );
18942
- }
18943
- }
18944
-
18945
- if ( $add_submenu_items ||
18946
- ( $is_activation_mode &&
18947
- $this->is_only_premium() &&
18948
- $this->is_admin_page( 'account' ) &&
18949
- fs_request_is_action( $this->get_unique_affix() . '_sync_license' )
18950
- )
18951
- ) {
18952
- if ( ! WP_FS__DEMO_MODE && $this->is_registered() ) {
18953
- $show_account = (
18954
- $this->is_submenu_item_visible( 'account' ) &&
18955
- /**
18956
- * @since 1.2.2.7 Don't show the Account for free WP.org themes without any paid plans.
18957
- */
18958
- ( ! $this->is_free_wp_org_theme() || $this->has_paid_plan() )
18959
- );
18960
-
18961
- // Add user account page.
18962
- $this->add_submenu_item(
18963
- $this->get_text_inline( 'Account', 'account' ),
18964
- array( &$this, '_account_page_render' ),
18965
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Account', 'account' ),
18966
- 'manage_options',
18967
- 'account',
18968
- array( &$this, '_account_page_load' ),
18969
- WP_FS__DEFAULT_PRIORITY,
18970
- ( $add_submenu_items && $show_account )
18971
- );
18972
- }
18973
- }
18974
-
18975
- if ( $add_submenu_items ) {
18976
- if (! WP_FS__DEMO_MODE && ! $this->is_whitelabeled() ) {
18977
- // Add contact page.
18978
- $this->add_submenu_item(
18979
- $this->get_text_inline( 'Contact Us', 'contact-us' ),
18980
- array( &$this, '_contact_page_render' ),
18981
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Contact Us', 'contact-us' ),
18982
- 'manage_options',
18983
- 'contact',
18984
- 'Freemius::_clean_admin_content_section',
18985
- WP_FS__DEFAULT_PRIORITY,
18986
- $this->is_submenu_item_visible( 'contact' )
18987
- );
18988
- }
18989
-
18990
- if ( $this->has_addons() ) {
18991
- $this->add_submenu_item(
18992
- $this->get_text_inline( 'Add-Ons', 'add-ons' ),
18993
- array( &$this, '_addons_page_render' ),
18994
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Add-Ons', 'add-ons' ),
18995
- 'manage_options',
18996
- 'addons',
18997
- array( &$this, '_addons_page_load' ),
18998
- WP_FS__LOWEST_PRIORITY - 1,
18999
- $this->is_submenu_item_visible( 'addons' )
19000
- );
19001
- }
19002
- }
19003
-
19004
- if ( $add_submenu_items ||
19005
- ( $is_activation_mode && $this->is_only_premium() && $this->is_admin_page( 'pricing' ) )
19006
- ) {
19007
- if (! WP_FS__DEMO_MODE && ! $this->is_whitelabeled() ) {
19008
- $show_pricing = (
19009
- $this->is_submenu_item_visible( 'pricing' ) &&
19010
- $this->is_pricing_page_visible()
19011
- );
19012
-
19013
- $pricing_cta_text = $this->get_pricing_cta_label();
19014
- $pricing_class = 'upgrade-mode';
19015
- if ( $show_pricing ) {
19016
- if ( $this->is_in_trial_promotion() &&
19017
- ! $this->is_paying_or_trial()
19018
- ) {
19019
- // If running a trial promotion, modify the pricing to load the trial.
19020
- $pricing_class = 'trial-mode';
19021
- } else if ( $this->is_paying() ) {
19022
- $pricing_class = '';
19023
- }
19024
- }
19025
-
19026
- // Add upgrade/pricing page.
19027
- $this->add_submenu_item(
19028
- $pricing_cta_text . '&nbsp;&nbsp;' . ( is_rtl() ? $this->get_text_x_inline( '&#x2190;', 'ASCII arrow left icon', 'symbol_arrow-left' ) : $this->get_text_x_inline( '&#x27a4;', 'ASCII arrow right icon', 'symbol_arrow-right' ) ),
19029
- array( &$this, '_pricing_page_render' ),
19030
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text_x_inline( 'Pricing', 'noun', 'pricing' ),
19031
- 'manage_options',
19032
- 'pricing',
19033
- 'Freemius::_clean_admin_content_section',
19034
- WP_FS__LOWEST_PRIORITY,
19035
- ( $add_submenu_items && $show_pricing ),
19036
- $pricing_class
19037
- );
19038
- }
19039
- }
19040
-
19041
- if ( ! $is_activation_mode || ( true !== $this->_storage->require_license_activation ) ) {
19042
- /**
19043
- * Add the other menu items if there are any when not in activation mode or license activation is not
19044
- * required (license activation is required for registered or anonymous users after activating the
19045
- * premium version when the site is not in trial mode or there's no active valid license).
19046
- *
19047
- * @author Leo Fajardo (@leorw)
19048
- * @since 2.2.1
19049
- */
19050
- if ( 0 < count( $this->_menu_items ) ) {
19051
- if ( ! $this->_menu->is_top_level() ) {
19052
- fs_enqueue_local_style( 'fs_common', '/admin/common.css' );
19053
-
19054
- // Append submenu items right after the plugin's submenu item.
19055
- $this->order_sub_submenu_items();
19056
- } else {
19057
- // Append submenu items.
19058
- $this->embed_submenu_items();
19059
- }
19060
- }
19061
- }
19062
- }
19063
-
19064
- /**
19065
- * Moved the actual submenu item additions to a separated function,
19066
- * in order to support sub-submenu items when the plugin's settings
19067
- * only have a submenu and not top-level menu item.
19068
- *
19069
- * @author Vova Feldman (@svovaf)
19070
- * @since 1.1.4
19071
- */
19072
- private function embed_submenu_items() {
19073
- $item_template = $this->_menu->is_top_level() ?
19074
- '<span class="fs-submenu-item %s %s %s">%s</span>' :
19075
- '<span class="fs-submenu-item fs-sub %s %s %s">%s</span>';
19076
-
19077
- $top_level_menu_capability = $this->get_top_level_menu_capability();
19078
-
19079
- ksort( $this->_menu_items );
19080
-
19081
- $is_first_submenu_item = true;
19082
-
19083
- foreach ( $this->_menu_items as $priority => $items ) {
19084
- foreach ( $items as $item ) {
19085
- $capability = ( ! empty( $item['capability'] ) ? $item['capability'] : $top_level_menu_capability );
19086
-
19087
- $menu_item = sprintf(
19088
- $item_template,
19089
- $this->get_unique_affix(),
19090
- $item['menu_slug'],
19091
- ! empty( $item['class'] ) ? $item['class'] : '',
19092
- $item['menu_title']
19093
- );
19094
-
19095
- $top_level_menu_slug = $this->get_top_level_menu_slug();
19096
- $menu_slug = $this->_menu->get_slug( $item['menu_slug'] );
19097
-
19098
- if ( ! isset( $item['url'] ) ) {
19099
- $hook = FS_Admin_Menu_Manager::add_subpage(
19100
- $item['show_submenu'] ?
19101
- $top_level_menu_slug :
19102
- null,
19103
- $item['page_title'],
19104
- $menu_item,
19105
- $capability,
19106
- $menu_slug,
19107
- $item['render_function']
19108
- );
19109
-
19110
- if ( false !== $item['before_render_function'] ) {
19111
- add_action( "load-$hook", $item['before_render_function'] );
19112
- }
19113
- } else {
19114
- FS_Admin_Menu_Manager::add_subpage(
19115
- $item['show_submenu'] ?
19116
- $top_level_menu_slug :
19117
- null,
19118
- $item['page_title'],
19119
- $menu_item,
19120
- $capability,
19121
- $menu_slug,
19122
- array( $this, '' )
19123
- );
19124
- }
19125
-
19126
- if ( $item['show_submenu'] && $is_first_submenu_item ) {
19127
- if ( $this->_is_network_active && ! empty( $this->_dynamically_added_top_level_page_hook_name ) ) {
19128
- /**
19129
- * If the top-level menu has been dynamically created, remove the first submenu item that
19130
- * WordPress automatically creates when there's no submenu item whose slug matches the
19131
- * parent's. In the following example, the `Awesome Plugin` submenu item will be removed.
19132
- *
19133
- * Awesome Plugin
19134
- * - Awesome Plugin <-- we want to remove this since there's no real setting page for the top-level
19135
- *
19136
- * @author Leo Fajardo (@leorw)
19137
- */
19138
- remove_submenu_page( $top_level_menu_slug, $top_level_menu_slug );
19139
- }
19140
-
19141
- $is_first_submenu_item = false;
19142
- }
19143
- }
19144
- }
19145
- }
19146
-
19147
- /**
19148
- * Re-order the submenu items so all Freemius added new submenu items
19149
- * are added right after the plugin's settings submenu item.
19150
- *
19151
- * @author Vova Feldman (@svovaf)
19152
- * @since 1.1.4
19153
- */
19154
- private function order_sub_submenu_items() {
19155
- global $submenu;
19156
-
19157
- $menu_slug = $this->_menu->get_top_level_menu_slug();
19158
-
19159
- /**
19160
- * Before "admin_menu" fires, WordPress will loop over the default submenus and remove pages for which the user
19161
- * does not have permissions. So in case a plugin does not have top-level menu but does have submenus under any
19162
- * of the default menus, only users that have the right role can access its sub-submenus (Account, Contact Us,
19163
- * Support Forum, etc.) since $submenu[ $menu_slug ] will be empty if the user doesn't have permission.
19164
- *
19165
- * In case a plugin does not have submenus under any of the default menus but does have submenus under the menu
19166
- * of another plugin, only users that have the right role can access its sub-submenus since we will use the
19167
- * capability needed to access the parent menu as the capability for the submenus that we will add.
19168
- */
19169
- if ( empty( $submenu[ $menu_slug ] ) ) {
19170
- return;
19171
- }
19172
-
19173
- $top_level_menu = &$submenu[ $menu_slug ];
19174
-
19175
- $all_submenu_items_after = array();
19176
-
19177
- $found_submenu_item = false;
19178
-
19179
- foreach ( $top_level_menu as $submenu_id => $meta ) {
19180
- if ( $found_submenu_item ) {
19181
- // Remove all submenu items after the plugin's submenu item.
19182
- $all_submenu_items_after[] = $meta;
19183
- unset( $top_level_menu[ $submenu_id ] );
19184
- }
19185
-
19186
- if ( $this->_menu->get_raw_slug() === $meta[2] ) {
19187
- // Found the submenu item, put all below.
19188
- $found_submenu_item = true;
19189
- continue;
19190
- }
19191
- }
19192
-
19193
- // Embed all plugin's new submenu items.
19194
- $this->embed_submenu_items();
19195
-
19196
- // Start with specially high number to make sure it's appended.
19197
- $i = max( 10000, max( array_keys( $top_level_menu ) ) + 1 );
19198
- foreach ( $all_submenu_items_after as $meta ) {
19199
- $top_level_menu[ $i ] = $meta;
19200
- $i ++;
19201
- }
19202
-
19203
- // Sort submenu items.
19204
- ksort( $top_level_menu );
19205
- }
19206
-
19207
- /**
19208
- * Helper method to return the module's support forum URL.
19209
- *
19210
- * @author Vova Feldman (@svovaf)
19211
- * @since 1.2.2.7
19212
- *
19213
- * @return string
19214
- */
19215
- function get_support_forum_url() {
19216
- return $this->apply_filters( 'support_forum_url', "https://wordpress.org/support/{$this->_module_type}/{$this->_slug}" );
19217
- }
19218
-
19219
- /**
19220
- * Displays the Support Forum link when enabled.
19221
- *
19222
- * Can be filtered like so:
19223
- *
19224
- * function _fs_show_support_menu( $is_visible, $menu_id ) {
19225
- * if ( 'support' === $menu_id ) {
19226
- * return _fs->is_registered();
19227
- * }
19228
- * return $is_visible;
19229
- * }
19230
- * _fs()->add_filter('is_submenu_visible', '_fs_show_support_menu', 10, 2);
19231
- *
19232
- */
19233
- function _add_default_submenu_items() {
19234
- if ( ! $this->is_on() ) {
19235
- return;
19236
- }
19237
-
19238
- if ( ! $this->is_activation_mode() &&
19239
- ( ( $this->_is_network_active && fs_is_network_admin() ) ||
19240
- ( ! $this->_is_network_active && is_admin() ) )
19241
- ) {
19242
- $this->add_submenu_link_item(
19243
- $this->apply_filters( 'support_forum_submenu', $this->get_text_inline( 'Support Forum', 'support-forum' ) ),
19244
- $this->get_support_forum_url(),
19245
- 'wp-support-forum',
19246
- null,
19247
- 50,
19248
- $this->is_submenu_item_visible( 'support' )
19249
- );
19250
- }
19251
- }
19252
-
19253
- /**
19254
- * @author Vova Feldman (@svovaf)
19255
- * @since 1.0.1
19256
- *
19257
- * @param string $menu_title
19258
- * @param callable $render_function
19259
- * @param bool|string $page_title
19260
- * @param string $capability
19261
- * @param bool|string $menu_slug
19262
- * @param bool|callable $before_render_function
19263
- * @param int $priority
19264
- * @param bool $show_submenu
19265
- * @param string $class Since 1.2.1.5 can add custom classes to menu items.
19266
- */
19267
- function add_submenu_item(
19268
- $menu_title,
19269
- $render_function,
19270
- $page_title = false,
19271
- $capability = 'manage_options',
19272
- $menu_slug = false,
19273
- $before_render_function = false,
19274
- $priority = WP_FS__DEFAULT_PRIORITY,
19275
- $show_submenu = true,
19276
- $class = ''
19277
- ) {
19278
- $this->_logger->entrance( 'Title = ' . $menu_title );
19279
-
19280
- if ( $this->is_addon() ) {
19281
- $parent_fs = $this->get_parent_instance();
19282
-
19283
- if ( is_object( $parent_fs ) ) {
19284
- $parent_fs->add_submenu_item(
19285
- $menu_title,
19286
- $render_function,
19287
- $page_title,
19288
- $capability,
19289
- $menu_slug,
19290
- $before_render_function,
19291
- $priority,
19292
- $show_submenu,
19293
- $class
19294
- );
19295
-
19296
- return;
19297
- }
19298
- }
19299
-
19300
- if ( ! isset( $this->_menu_items[ $priority ] ) ) {
19301
- $this->_menu_items[ $priority ] = array();
19302
- }
19303
-
19304
- $this->_menu_items[ $priority ][] = array(
19305
- 'page_title' => is_string( $page_title ) ? $page_title : $menu_title,
19306
- 'menu_title' => $menu_title,
19307
- 'capability' => $capability,
19308
- 'menu_slug' => is_string( $menu_slug ) ? $menu_slug : strtolower( $menu_title ),
19309
- 'render_function' => $render_function,
19310
- 'before_render_function' => $before_render_function,
19311
- 'show_submenu' => $show_submenu,
19312
- 'class' => $class,
19313
- );
19314
- }
19315
-
19316
- /**
19317
- * @author Vova Feldman (@svovaf)
19318
- * @since 1.0.1
19319
- *
19320
- * @param string $menu_title
19321
- * @param string $url
19322
- * @param bool $menu_slug
19323
- * @param string $capability
19324
- * @param int $priority
19325
- * @param bool $show_submenu
19326
- */
19327
- function add_submenu_link_item(
19328
- $menu_title,
19329
- $url,
19330
- $menu_slug = false,
19331
- $capability = 'read',
19332
- $priority = WP_FS__DEFAULT_PRIORITY,
19333
- $show_submenu = true
19334
- ) {
19335
- $this->_logger->entrance( 'Title = ' . $menu_title . '; Url = ' . $url );
19336
-
19337
- if ( $this->is_addon() ) {
19338
- $parent_fs = $this->get_parent_instance();
19339
-
19340
- if ( is_object( $parent_fs ) ) {
19341
- $parent_fs->add_submenu_link_item(
19342
- $menu_title,
19343
- $url,
19344
- $menu_slug,
19345
- $capability,
19346
- $priority,
19347
- $show_submenu
19348
- );
19349
-
19350
- return;
19351
- }
19352
- }
19353
-
19354
- if ( ! isset( $this->_menu_items[ $priority ] ) ) {
19355
- $this->_menu_items[ $priority ] = array();
19356
- }
19357
-
19358
- $this->_menu_items[ $priority ][] = array(
19359
- 'menu_title' => $menu_title,
19360
- 'capability' => $capability,
19361
- 'menu_slug' => is_string( $menu_slug ) ? $menu_slug : strtolower( $menu_title ),
19362
- 'url' => $url,
19363
- 'page_title' => $menu_title,
19364
- 'render_function' => 'fs_dummy',
19365
- 'before_render_function' => '',
19366
- 'show_submenu' => $show_submenu,
19367
- );
19368
- }
19369
-
19370
- #endregion ------------------------------------------------------------------
19371
-
19372
- #--------------------------------------------------------------------------------
19373
- #region Admin Notices
19374
- #--------------------------------------------------------------------------------
19375
-
19376
- /**
19377
- * @author Vova Feldman (@svovaf)
19378
- * @since 2.3.1
19379
- *
19380
- * @param string|string[] $ids
19381
- * @param int|null $network_level_or_blog_id
19382
- *
19383
- * @uses FS_Admin_Notices::remove_sticky()
19384
- */
19385
- function remove_sticky( $ids, $network_level_or_blog_id = null ) {
19386
- $this->_admin_notices->remove_sticky( $ids, $network_level_or_blog_id );
19387
- }
19388
-
19389
- #endregion
19390
-
19391
- #--------------------------------------------------------------------------------
19392
- #region Actions / Hooks / Filters
19393
- #--------------------------------------------------------------------------------
19394
-
19395
- /**
19396
- * @author Vova Feldman (@svovaf)
19397
- * @since 1.1.7
19398
- *
19399
- * @param string $tag
19400
- *
19401
- * @return string
19402
- */
19403
- public function get_action_tag( $tag ) {
19404
- return self::get_action_tag_static( $tag, $this->_slug, $this->is_plugin() );
19405
- }
19406
-
19407
- /**
19408
- * @author Vova Feldman (@svovaf)
19409
- * @since 1.2.1.6
19410
- *
19411
- * @param string $tag
19412
- * @param string $slug
19413
- * @param bool $is_plugin
19414
- *
19415
- * @return string
19416
- */
19417
- static function get_action_tag_static( $tag, $slug = '', $is_plugin = true ) {
19418
- $action = "fs_{$tag}";
19419
-
19420
- if ( ! empty( $slug ) ) {
19421
- $action .= '_' . self::get_module_unique_affix( $slug, $is_plugin );
19422
- }
19423
-
19424
- return $action;
19425
- }
19426
-
19427
- /**
19428
- * Returns a string that can be used to generate a unique action name,
19429
- * option name, HTML element ID, or HTML element class.
19430
- *
19431
- * @author Leo Fajardo (@leorw)
19432
- * @since 1.2.2
19433
- *
19434
- * @return string
19435
- */
19436
- public function get_unique_affix() {
19437
- return self::get_module_unique_affix( $this->_slug, $this->is_plugin() );
19438
- }
19439
-
19440
- /**
19441
- * Returns a string that can be used to generate a unique action name,
19442
- * option name, HTML element ID, or HTML element class.
19443
- *
19444
- * @author Vova Feldman (@svovaf)
19445
- * @since 1.2.2.5
19446
- *
19447
- * @param string $slug
19448
- * @param bool $is_plugin
19449
- *
19450
- * @return string
19451
- */
19452
- static function get_module_unique_affix( $slug, $is_plugin = true ) {
19453
- $affix = $slug;
19454
-
19455
- if ( ! $is_plugin ) {
19456
- $affix .= '-' . WP_FS__MODULE_TYPE_THEME;
19457
- }
19458
-
19459
- return $affix;
19460
- }
19461
-
19462
- /**
19463
- * @author Vova Feldman (@svovaf)
19464
- * @since 1.2.1
19465
- * @since 1.2.2.5 The AJAX action names are based on the module ID, not like the non-AJAX actions that are
19466
- * based on the slug for backward compatibility.
19467
- *
19468
- * @param string $tag
19469
- *
19470
- * @return string
19471
- */
19472
- function get_ajax_action( $tag ) {
19473
- return self::get_ajax_action_static( $tag, $this->_module_id );
19474
- }
19475
-
19476
- /**
19477
- * @author Vova Feldman (@svovaf)
19478
- * @since 1.2.1.7
19479
- *
19480
- * @param string $tag
19481
- *
19482
- * @return string
19483
- */
19484
- function get_ajax_security( $tag ) {
19485
- return wp_create_nonce( $this->get_ajax_action( $tag ) );
19486
- }
19487
-
19488
- /**
19489
- * @author Vova Feldman (@svovaf)
19490
- * @since 1.2.1.7
19491
- *
19492
- * @param string $tag
19493
- */
19494
- function check_ajax_referer( $tag ) {
19495
- check_ajax_referer( $this->get_ajax_action( $tag ), 'security' );
19496
- }
19497
-
19498
- /**
19499
- * @author Vova Feldman (@svovaf)
19500
- * @since 1.2.1.6
19501
- * @since 1.2.2.5 The AJAX action names are based on the module ID, not like the non-AJAX actions that are
19502
- * based on the slug for backward compatibility.
19503
- *
19504
- * @param string $tag
19505
- * @param number|null $module_id
19506
- *
19507
- * @return string
19508
- */
19509
- static function get_ajax_action_static( $tag, $module_id = null ) {
19510
- $action = "fs_{$tag}";
19511
-
19512
- if ( ! empty( $module_id ) ) {
19513
- $action .= "_{$module_id}";
19514
- }
19515
-
19516
- return $action;
19517
- }
19518
-
19519
- /**
19520
- * Do action, specific for the current context plugin.
19521
- *
19522
- * @author Vova Feldman (@svovaf)
19523
- * @since 1.0.1
19524
- *
19525
- * @param string $tag The name of the action to be executed.
19526
- * @param mixed $arg,... Optional. Additional arguments which are passed on to the
19527
- * functions hooked to the action. Default empty.
19528
- *
19529
- * @uses do_action()
19530
- */
19531
- function do_action( $tag, $arg = '' ) {
19532
- $this->_logger->entrance( $tag );
19533
-
19534
- $args = func_get_args();
19535
-
19536
- call_user_func_array( 'do_action', array_merge(
19537
- array( $this->get_action_tag( $tag ) ),
19538
- array_slice( $args, 1 ) )
19539
- );
19540
- }
19541
-
19542
- /**
19543
- * Add action, specific for the current context plugin.
19544
- *
19545
- * @author Vova Feldman (@svovaf)
19546
- * @since 1.0.1
19547
- *
19548
- * @param string $tag
19549
- * @param callable $function_to_add
19550
- * @param int $priority
19551
- * @param int $accepted_args
19552
- *
19553
- * @uses add_action()
19554
- */
19555
- function add_action(
19556
- $tag,
19557
- $function_to_add,
19558
- $priority = WP_FS__DEFAULT_PRIORITY,
19559
- $accepted_args = 1
19560
- ) {
19561
- $this->_logger->entrance( $tag );
19562
-
19563
- add_action( $this->get_action_tag( $tag ), $function_to_add, $priority, $accepted_args );
19564
- }
19565
-
19566
- /**
19567
- * Add AJAX action, specific for the current context plugin.
19568
- *
19569
- * @author Vova Feldman (@svovaf)
19570
- * @since 1.2.1
19571
- *
19572
- * @param string $tag
19573
- * @param callable $function_to_add
19574
- * @param int $priority
19575
- *
19576
- * @uses add_action()
19577
- *
19578
- * @return bool True if action added, false if no need to add the action since the AJAX call isn't matching.
19579
- */
19580
- function add_ajax_action(
19581
- $tag,
19582
- $function_to_add,
19583
- $priority = WP_FS__DEFAULT_PRIORITY
19584
- ) {
19585
- $this->_logger->entrance( $tag );
19586
-
19587
- return self::add_ajax_action_static(
19588
- $tag,
19589
- $function_to_add,
19590
- $priority,
19591
- $this->_module_id
19592
- );
19593
- }
19594
-
19595
- /**
19596
- * Add AJAX action.
19597
- *
19598
- * @author Vova Feldman (@svovaf)
19599
- * @since 1.2.1.6
19600
- *
19601
- * @param string $tag
19602
- * @param callable $function_to_add
19603
- * @param int $priority
19604
- * @param number|null $module_id
19605
- *
19606
- * @return bool True if action added, false if no need to add the action since the AJAX call isn't matching.
19607
- * @uses add_action()
19608
- *
19609
- */
19610
- static function add_ajax_action_static(
19611
- $tag,
19612
- $function_to_add,
19613
- $priority = WP_FS__DEFAULT_PRIORITY,
19614
- $module_id = null
19615
- ) {
19616
- self::$_static_logger->entrance( $tag );
19617
-
19618
- if ( ! self::is_ajax_action_static( $tag, $module_id ) ) {
19619
- return false;
19620
- }
19621
-
19622
- add_action(
19623
- 'wp_ajax_' . self::get_ajax_action_static( $tag, $module_id ),
19624
- $function_to_add,
19625
- $priority,
19626
- 0
19627
- );
19628
-
19629
- self::$_static_logger->info( "$tag AJAX callback action added." );
19630
-
19631
- return true;
19632
- }
19633
-
19634
- /**
19635
- * Send a JSON response back to an Ajax request.
19636
- *
19637
- * @author Vova Feldman (@svovaf)
19638
- * @since 1.2.1.5
19639
- *
19640
- * @param mixed $response
19641
- */
19642
- static function shoot_ajax_response( $response ) {
19643
- wp_send_json( $response );
19644
- }
19645
-
19646
- /**
19647
- * Send a JSON response back to an Ajax request, indicating success.
19648
- *
19649
- * @author Vova Feldman (@svovaf)
19650
- * @since 1.2.1.5
19651
- *
19652
- * @param mixed $data Data to encode as JSON, then print and exit.
19653
- */
19654
- static function shoot_ajax_success( $data = null ) {
19655
- wp_send_json_success( $data );
19656
- }
19657
-
19658
- /**
19659
- * Send a JSON response back to an Ajax request, indicating failure.
19660
- *
19661
- * @author Vova Feldman (@svovaf)
19662
- * @since 1.2.1.5
19663
- *
19664
- * @param mixed $error Optional error message.
19665
- */
19666
- static function shoot_ajax_failure( $error = '' ) {
19667
- $result = array( 'success' => false );
19668
- if ( ! empty( $error ) ) {
19669
- $result['error'] = $error;
19670
- }
19671
-
19672
- wp_send_json( $result );
19673
- }
19674
-
19675
- /**
19676
- * Apply filter, specific for the current context plugin.
19677
- *
19678
- * @author Vova Feldman (@svovaf)
19679
- * @since 1.0.9
19680
- *
19681
- * @param string $tag The name of the filter hook.
19682
- * @param mixed $value The value on which the filters hooked to `$tag` are applied on.
19683
- *
19684
- * @return mixed The filtered value after all hooked functions are applied to it.
19685
- *
19686
- * @uses apply_filters()
19687
- */
19688
- function apply_filters( $tag, $value ) {
19689
- $this->_logger->entrance( $tag );
19690
-
19691
- $args = func_get_args();
19692
- array_unshift( $args, $this->get_unique_affix() );
19693
-
19694
- return call_user_func_array( 'fs_apply_filter', $args );
19695
- }
19696
-
19697
- /**
19698
- * Add filter, specific for the current context plugin.
19699
- *
19700
- * @author Vova Feldman (@svovaf)
19701
- * @since 1.0.9
19702
- *
19703
- * @param string $tag
19704
- * @param callable $function_to_add
19705
- * @param int $priority
19706
- * @param int $accepted_args
19707
- *
19708
- * @uses add_filter()
19709
- */
19710
- function add_filter( $tag, $function_to_add, $priority = WP_FS__DEFAULT_PRIORITY, $accepted_args = 1 ) {
19711
- $this->_logger->entrance( $tag );
19712
-
19713
- add_filter( $this->get_action_tag( $tag ), $function_to_add, $priority, $accepted_args );
19714
- }
19715
-
19716
- /**
19717
- * Check if has filter.
19718
- *
19719
- * @author Vova Feldman (@svovaf)
19720
- * @since 1.1.4
19721
- *
19722
- * @param string $tag
19723
- * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
19724
- *
19725
- * @return false|int
19726
- *
19727
- * @uses has_filter()
19728
- */
19729
- function has_filter( $tag, $function_to_check = false ) {
19730
- $this->_logger->entrance( $tag );
19731
-
19732
- return has_filter( $this->get_action_tag( $tag ), $function_to_check );
19733
- }
19734
-
19735
- #endregion
19736
-
19737
- /**
19738
- * Override default i18n text phrases.
19739
- *
19740
- * @author Vova Feldman (@svovaf)
19741
- * @since 1.1.6
19742
- *
19743
- * @param string[] string $key_value
19744
- *
19745
- * @uses fs_override_i18n()
19746
- */
19747
- function override_i18n( $key_value ) {
19748
- fs_override_i18n( $key_value, $this->_slug );
19749
- }
19750
-
19751
- /* Account Page
19752
- ------------------------------------------------------------------------------------------------------------------*/
19753
- /**
19754
- * Update site information.
19755
- *
19756
- * @author Vova Feldman (@svovaf)
19757
- * @since 1.0.1
19758
- *
19759
- * @param bool $store Flush to Database if true.
19760
- * @param null|int $network_level_or_blog_id Since 2.0.0
19761
- * @param \FS_Site $site Since 2.0.0
19762
- */
19763
- private function _store_site( $store = true, $network_level_or_blog_id = null, FS_Site $site = null, $is_backup = false ) {
19764
- $this->_logger->entrance();
19765
-
19766
- if ( is_null( $site ) ) {
19767
- $site = $this->_site;
19768
- }
19769
-
19770
- if ( !isset( $site ) || !is_object($site) || empty( $site->id ) ) {
19771
- $this->_logger->error( "Empty install ID, can't store site." );
19772
-
19773
- return;
19774
- }
19775
-
19776
- $site_clone = clone $site;
19777
-
19778
- $sites = self::get_all_sites( $this->_module_type, $network_level_or_blog_id, $is_backup );
19779
-
19780
- if (
19781
- ! $is_backup &&
19782
- is_object( $this->_user ) && $this->_user->id != $site->user_id
19783
- ) {
19784
- $this->sync_user_by_current_install( $site->user_id );
19785
-
19786
- $prev_stored_user_id = $this->_storage->get( 'prev_user_id', false, $network_level_or_blog_id );
19787
-
19788
- if ( empty( $prev_stored_user_id ) &&
19789
- is_object($this->_user) && $this->_user->id != $site->user_id
19790
- ) {
19791
- /**
19792
- * Store the current user ID as the previous user ID so that the previous user can be used
19793
- * as the install's owner while the new owner's details are not yet available.
19794
- *
19795
- * This will be executed only in the `replica` site. For example, there are 2 sites, namely `original`
19796
- * and `replica`, then an ownership change was initiated and completed in the `original`, the `replica`
19797
- * will be using the previous user until it is updated again (e.g.: until the next clone of `original`
19798
- * into `replica`.
19799
- *
19800
- * @author Leo Fajardo (@leorw)
19801
- */
19802
- $this->_storage->store( 'prev_user_id', $sites[ $this->_slug ]->user_id, $network_level_or_blog_id );
19803
- }
19804
- }
19805
-
19806
- $sites[ $this->_slug ] = $site_clone;
19807
-
19808
- $this->set_account_option(
19809
- ( $is_backup ? 'prev_' : '' ) . 'sites',
19810
- $sites,
19811
- $store,
19812
- $network_level_or_blog_id
19813
- );
19814
- }
19815
-
19816
- /**
19817
- * Stores the context site in the sites backup storage. This logic is used before deleting the site info so that it can be restored later on if necessary (e.g., if the automatic clone resolution attempt fails).
19818
- *
19819
- * @author Leo Fajardo (@leorw)
19820
- * @since 2.5.0
19821
- */
19822
- private function back_up_site() {
19823
- $this->_logger->entrance();
19824
-
19825
- $site_clone = clone $this->_site;
19826
-
19827
- $this->_store_site( true, null, $site_clone, true );
19828
- }
19829
-
19830
- /**
19831
- * Update plugin's plans information.
19832
- *
19833
- * @author Vova Feldman (@svovaf)
19834
- * @since 1.0.2
19835
- *
19836
- * @param bool $store Flush to Database if true.
19837
- */
19838
- private function _store_plans( $store = true ) {
19839
- $this->_logger->entrance();
19840
-
19841
- $plans = self::get_all_plans( $this->_module_type );
19842
-
19843
- // Copy plans.
19844
- $encrypted_plans = array();
19845
- for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
19846
- $encrypted_plans[] = self::_encrypt_entity( $this->_plans[ $i ] );
19847
- }
19848
-
19849
- $plans[ $this->_slug ] = $encrypted_plans;
19850
-
19851
- $this->set_account_option( 'plans', $plans, $store );
19852
- }
19853
-
19854
- /**
19855
- * Update user's plugin licenses.
19856
- *
19857
- * @author Vova Feldman (@svovaf)
19858
- * @since 1.0.5
19859
- *
19860
- * @param bool $store
19861
- * @param number|bool $module_id
19862
- * @param FS_Plugin_License[] $licenses
19863
- */
19864
- private function _store_licenses( $store = true, $module_id = false, $licenses = array() ) {
19865
- $this->_logger->entrance();
19866
-
19867
- $all_licenses = self::get_all_licenses();
19868
-
19869
- if ( ! FS_Plugin::is_valid_id( $module_id ) ) {
19870
- $module_id = $this->_module_id;
19871
-
19872
- $user_licenses = is_array( $this->_licenses ) ?
19873
- $this->_licenses :
19874
- array();
19875
-
19876
- if ( empty( $user_licenses ) ) {
19877
- // If the context user doesn't have any license, don't update the licenses collection.
19878
- return;
19879
- }
19880
-
19881
- $new_user_licenses_map = array();
19882
- foreach ( $user_licenses as $user_license ) {
19883
- $new_user_licenses_map[ $user_license->id ] = $user_license;
19884
- }
19885
-
19886
- self::store_user_id_license_ids_map( array_keys( $new_user_licenses_map ), $this->_module_id, $this->_user->id );
19887
-
19888
- // Update user licenses.
19889
- $licenses_to_update_count = count( $new_user_licenses_map );
19890
- foreach ( $all_licenses[ $module_id ] as $key => $license ) {
19891
- if ( 0 === $licenses_to_update_count ) {
19892
- break;
19893
- }
19894
-
19895
- if ( isset( $new_user_licenses_map[ $license->id ] ) ) {
19896
- // Update license.
19897
- $all_licenses[ $module_id ][ $key ] = $new_user_licenses_map[ $license->id ];
19898
- unset( $new_user_licenses_map[ $license->id ] );
19899
-
19900
- $licenses_to_update_count --;
19901
- }
19902
- }
19903
-
19904
- if ( ! empty( $new_user_licenses_map ) ) {
19905
- // Add new licenses.
19906
- $all_licenses[ $module_id ] = array_merge( array_values( $new_user_licenses_map ), $all_licenses[ $module_id ] );
19907
- }
19908
-
19909
- $licenses = $all_licenses[ $module_id ];
19910
- }
19911
-
19912
- if ( ! isset( $all_licenses[ $module_id ] ) ) {
19913
- $all_licenses[ $module_id ] = array();
19914
- }
19915
-
19916
- $all_licenses[ $module_id ] = $licenses;
19917
-
19918
- self::$_accounts->set_option( 'all_licenses', $all_licenses, $store );
19919
- }
19920
-
19921
- /**
19922
- * Update user information.
19923
- *
19924
- * @author Vova Feldman (@svovaf)
19925
- * @since 1.0.1
19926
- *
19927
- * @param bool $store Flush to Database if true.
19928
- */
19929
- private function _store_user( $store = true ) {
19930
- $this->_logger->entrance();
19931
-
19932
- if ( empty( $this->_user->id ) ) {
19933
- $this->_logger->error( "Empty user ID, can't store user." );
19934
-
19935
- return;
19936
- }
19937
-
19938
- $users = self::get_all_users();
19939
- $users[ $this->_user->id ] = $this->_user;
19940
- self::$_accounts->set_option( 'users', $users, $store );
19941
- }
19942
-
19943
- /**
19944
- * Update new updates information.
19945
- *
19946
- * @author Vova Feldman (@svovaf)
19947
- * @since 1.0.4
19948
- *
19949
- * @param FS_Plugin_Tag|null $update
19950
- * @param bool $store Flush to Database if true.
19951
- * @param bool|number $plugin_id
19952
- */
19953
- private function _store_update( $update, $store = true, $plugin_id = false ) {
19954
- $this->_logger->entrance();
19955
-
19956
- if ( $update instanceof FS_Plugin_Tag ) {
19957
- $update->updated = time();
19958
- }
19959
-
19960
- if ( ! is_numeric( $plugin_id ) ) {
19961
- $plugin_id = $this->_plugin->id;
19962
- }
19963
-
19964
- $updates = self::get_all_updates();
19965
- $updates[ $plugin_id ] = $update;
19966
- self::$_accounts->set_option( 'updates', $updates, $store );
19967
- }
19968
-
19969
- /**
19970
- * Update new updates information.
19971
- *
19972
- * @author Vova Feldman (@svovaf)
19973
- * @since 1.0.6
19974
- *
19975
- * @param FS_Plugin[] $plugin_addons
19976
- * @param bool $store Flush to Database if true.
19977
- */
19978
- private function _store_addons( $plugin_addons, $store = true ) {
19979
- $this->_logger->entrance();
19980
-
19981
- $addons = self::get_all_addons();
19982
- $addons[ $this->_plugin->id ] = $plugin_addons;
19983
- self::$_accounts->set_option( 'addons', $addons, $store );
19984
- }
19985
-
19986
- /**
19987
- * Delete plugin's associated add-ons.
19988
- *
19989
- * @author Vova Feldman (@svovaf)
19990
- * @since 1.0.8
19991
- *
19992
- * @param bool $store
19993
- *
19994
- * @return bool
19995
- */
19996
- private function _delete_account_addons( $store = true ) {
19997
- $all_addons = self::get_all_account_addons();
19998
-
19999
- if ( ! isset( $all_addons[ $this->_plugin->id ] ) ) {
20000
- return false;
20001
- }
20002
-
20003
- unset( $all_addons[ $this->_plugin->id ] );
20004
-
20005
- self::$_accounts->set_option( 'account_addons', $all_addons, $store );
20006
-
20007
- return true;
20008
- }
20009
-
20010
- /**
20011
- * Update account add-ons list.
20012
- *
20013
- * @author Vova Feldman (@svovaf)
20014
- * @since 1.0.6
20015
- *
20016
- * @param FS_Plugin[] $addons
20017
- * @param bool $store Flush to Database if true.
20018
- */
20019
- private function _store_account_addons( $addons, $store = true ) {
20020
- $this->_logger->entrance();
20021
-
20022
- $all_addons = self::get_all_account_addons();
20023
- $all_addons[ $this->_plugin->id ] = $addons;
20024
- self::$_accounts->set_option( 'account_addons', $all_addons, $store );
20025
- }
20026
-
20027
- /**
20028
- * Purges the cache for the valid user licenses API call so that when the `Account` or `Add-Ons` page is loaded,
20029
- * the valid user licenses will be fetched again and the account add-ons may be updated.
20030
- *
20031
- * @author Leo Fajardo (@leorw)
20032
- * @since 2.2.4
20033
- */
20034
- private function purge_valid_user_licenses_cache() {
20035
- if ( ! $this->is_registered() ) {
20036
- return;
20037
- }
20038
-
20039
- $this->get_api_user_scope()->purge_cache( $this->get_valid_user_licenses_endpoint() );
20040
- }
20041
-
20042
- /**
20043
- * @author Leo Fajardo (@leorw)
20044
- * @since 2.3.0
20045
- *
20046
- * @param array $all_licenses
20047
- * @param number|null $site_license_id
20048
- * @param bool $include_parent_licenses
20049
- *
20050
- * @return array
20051
- */
20052
- private function get_foreign_licenses_info( $all_licenses, $site_license_id = null, $include_parent_licenses = false ) {
20053
- $foreign_licenses = array(
20054
- 'ids' => array(),
20055
- 'license_keys' => array()
20056
- );
20057
-
20058
- $parent_license_ids_map = array();
20059
-
20060
- foreach ( $all_licenses as $license ) {
20061
- if ( $license->user_id == $this->_user->id || $license->id == $site_license_id ) {
20062
- continue;
20063
- }
20064
-
20065
- $foreign_licenses['ids'][] = $license->id;
20066
- $foreign_licenses['license_keys'][] = $license->secret_key;
20067
-
20068
- if (
20069
- $include_parent_licenses &&
20070
- is_object( $this->_license ) &&
20071
- FS_Plugin_License::is_valid_id( $this->_license->parent_license_id ) &&
20072
- ! isset( $parent_license_ids_map[ $this->_license->parent_license_id ] )
20073
- ) {
20074
- /**
20075
- * Include the parent license's info only if it has not been included before since child licenses
20076
- * can have the same parent license.
20077
- */
20078
- $foreign_licenses['ids'][] = $this->_license->parent_license_id;
20079
- $foreign_licenses['license_keys'][] = $license->secret_key;
20080
-
20081
- $parent_license_ids_map[ $this->_license->parent_license_id ] = true;
20082
- }
20083
- }
20084
-
20085
- if ( empty( $foreign_licenses['ids'] ) ) {
20086
- $foreign_licenses = array();
20087
- }
20088
-
20089
- return $foreign_licenses;
20090
- }
20091
-
20092
- /**
20093
- * @author Leo Fajardo (@leorw)
20094
- * @since 2.3.0
20095
- *
20096
- * @return string
20097
- */
20098
- private function get_valid_user_licenses_endpoint() {
20099
- $user_licenses_endpoint = '/licenses.json?type=active' .
20100
- ( FS_Plugin::is_valid_id( $this->get_bundle_id() ) ? '&is_enriched=true' : '' );
20101
-
20102
- $foreign_licenses = $this->get_foreign_licenses_info( self::get_all_licenses( $this->_module_id ), null, true );
20103
-
20104
- if ( ! empty ( $foreign_licenses ) ) {
20105
- $foreign_licenses = array(
20106
- // Prefix with `+` to tell the server to include foreign licenses in the licenses collection.
20107
- 'ids' => ( urlencode( '+' ) . implode( ',', $foreign_licenses['ids'] ) ),
20108
- 'license_keys' => implode( ',', array_map( 'urlencode', $foreign_licenses['license_keys'] ) )
20109
- );
20110
-
20111
- $user_licenses_endpoint = add_query_arg( $foreign_licenses, $user_licenses_endpoint );
20112
- }
20113
-
20114
- return $user_licenses_endpoint;
20115
- }
20116
-
20117
- /**
20118
- * Fetches active licenses that are enriched with product type if there's a context `bundle_id` and bundle
20119
- * licenses enriched with product IDs if there are any. From the licenses, the `get_updated_account_addons`
20120
- * method filters out non–add-on product IDs and stores the add-on IDs.
20121
- *
20122
- * @author Leo Fajardo (@leorw)
20123
- * @since 2.2.4
20124
- *
20125
- * @return stdClass[] array
20126
- */
20127
- private function fetch_valid_user_licenses() {
20128
- $this->_logger->entrance();
20129
-
20130
- $result = $this->get_api_user_scope()->get( $this->get_valid_user_licenses_endpoint() );
20131
-
20132
- if ( ! $this->is_api_result_object( $result, 'licenses' ) ||
20133
- ! is_array( $result->licenses )
20134
- ) {
20135
- return array();
20136
- }
20137
-
20138
- return $result->licenses;
20139
- }
20140
-
20141
- /**
20142
- * @author Leo Fajardo (@leorw)
20143
- * @since 2.2.4
20144
- *
20145
- * @return number[] Account add-on IDs.
20146
- */
20147
- function get_updated_account_addons() {
20148
- $addons = $this->get_addons();
20149
- if ( empty( $addons ) ) {
20150
- return array();
20151
- }
20152
-
20153
- $account_addons = $this->get_account_addons();
20154
- if ( ! is_array( $account_addons ) ) {
20155
- $account_addons = array();
20156
- }
20157
-
20158
- $user_licenses = $this->is_registered() ?
20159
- $this->fetch_valid_user_licenses() :
20160
- array();
20161
-
20162
- if ( empty( $user_licenses ) ) {
20163
- return $account_addons;
20164
- }
20165
-
20166
- $addon_ids = array();
20167
- foreach ( $addons as $addon ) {
20168
- $addon_ids[] = $addon->id;
20169
- }
20170
-
20171
- $license_product_ids = array();
20172
-
20173
- foreach ( $user_licenses as $license ) {
20174
- if ( isset( $license->plugin_type ) && 'bundle' === $license->plugin_type ) {
20175
- $license_product_ids = array_merge( $license_product_ids, $license->products );
20176
- } else {
20177
- $license_product_ids[] = $license->plugin_id;
20178
- }
20179
- }
20180
-
20181
- // Filter out non–add-on IDs.
20182
- $new_account_addons = array_intersect( $addon_ids, $license_product_ids );
20183
- if ( count( $new_account_addons ) !== count( $account_addons ) ) {
20184
- $this->_store_account_addons( array_unique( $new_account_addons ) );
20185
- }
20186
-
20187
- return $new_account_addons;
20188
- }
20189
-
20190
- /**
20191
- * Store account params in the Database.
20192
- *
20193
- * @author Vova Feldman (@svovaf)
20194
- * @since 1.0.1
20195
- *
20196
- * @param null|int $blog_id Since 2.0.0
20197
- */
20198
- private function _store_account( $blog_id = null ) {
20199
- $this->_logger->entrance();
20200
-
20201
- $this->_store_site( false, $blog_id );
20202
- $this->_store_user( false );
20203
- $this->_store_plans( false );
20204
- $this->_store_licenses( false );
20205
-
20206
- self::$_accounts->store( $blog_id );
20207
- }
20208
-
20209
- /**
20210
- * Sync user's information.
20211
- *
20212
- * @author Vova Feldman (@svovaf)
20213
- * @since 1.0.3
20214
- * @uses FS_Api
20215
- */
20216
- private function _handle_account_user_sync() {
20217
- $this->_logger->entrance();
20218
-
20219
- $api = $this->get_api_user_scope();
20220
-
20221
- // Get user's information.
20222
- $user = $api->get( '/', true );
20223
-
20224
- if ( isset( $user->id ) ) {
20225
- $this->_user->first = $user->first;
20226
- $this->_user->last = $user->last;
20227
- $this->_user->email = $user->email;
20228
-
20229
- $is_menu_item_account_visible = $this->is_submenu_item_visible( 'account' );
20230
-
20231
- if ( $user->is_verified &&
20232
- ( ! isset( $this->_user->is_verified ) || false === $this->_user->is_verified )
20233
- ) {
20234
- $this->_user->is_verified = true;
20235
-
20236
- $this->do_action( 'account_email_verified', $user->email );
20237
-
20238
- $this->_admin_notices->add(
20239
- $this->get_text_inline( 'Your email has been successfully verified - you are AWESOME!', 'email-verified-message' ),
20240
- $this->get_text_x_inline( 'Right on', 'a positive response', 'right-on' ) . '!',
20241
- 'success',
20242
- // Make admin sticky if account menu item is invisible,
20243
- // since the page will be auto redirected to the plugin's
20244
- // main settings page, and the non-sticky message
20245
- // will disappear.
20246
- ! $is_menu_item_account_visible,
20247
- 'email_verified'
20248
- );
20249
- }
20250
-
20251
- // Flush user details to DB.
20252
- $this->_store_user();
20253
-
20254
- $this->do_action( 'after_account_user_sync', $user );
20255
-
20256
- /**
20257
- * If account menu item is hidden, redirect to plugin's main settings page.
20258
- *
20259
- * @author Vova Feldman (@svovaf)
20260
- * @since 1.1.6
20261
- *
20262
- * @link https://github.com/Freemius/wordpress-sdk/issues/6
20263
- */
20264
- if ( ! $is_menu_item_account_visible ) {
20265
- fs_redirect( $this->_get_admin_page_url() );
20266
- }
20267
- }
20268
- }
20269
-
20270
- /**
20271
- * @author Vova Feldman (@svovaf)
20272
- * @since 1.0.9
20273
- * @uses FS_Api
20274
- *
20275
- * @param number|bool $license_id
20276
- *
20277
- * @return FS_Subscription|object|bool
20278
- */
20279
- private function _fetch_site_license_subscription( $license_id = false ) {
20280
- $this->_logger->entrance();
20281
- $api = $this->get_api_site_scope();
20282
-
20283
- if ( ! is_numeric( $license_id ) ) {
20284
- $license_id = FS_Plugin_License::is_valid_id( $this->_license->parent_license_id ) ?
20285
- $this->_license->parent_license_id :
20286
- $this->_license->id;
20287
- }
20288
-
20289
- $result = $api->get( "/licenses/{$license_id}/subscriptions.json", true );
20290
-
20291
- return ! isset( $result->error ) ?
20292
- ( ( is_array( $result->subscriptions ) && 0 < count( $result->subscriptions ) ) ?
20293
- new FS_Subscription( $result->subscriptions[0] ) :
20294
- false
20295
- ) :
20296
- $result;
20297
- }
20298
-
20299
- /**
20300
- * @author Vova Feldman (@svovaf)
20301
- * @since 1.0.4
20302
- * @uses FS_Api
20303
- *
20304
- * @param number|bool $plan_id
20305
- *
20306
- * @return FS_Plugin_Plan|object
20307
- */
20308
- private function _fetch_site_plan( $plan_id = false ) {
20309
- $this->_logger->entrance();
20310
- $api = $this->get_api_site_scope();
20311
-
20312
- if ( ! is_numeric( $plan_id ) ) {
20313
- $plan_id = $this->_site->plan_id;
20314
- }
20315
-
20316
- $plan = $api->get( "/plans/{$plan_id}.json", true );
20317
-
20318
- return ! isset( $plan->error ) ? new FS_Plugin_Plan( $plan ) : $plan;
20319
- }
20320
-
20321
- /**
20322
- * @author Vova Feldman (@svovaf)
20323
- * @since 1.0.5
20324
- * @uses FS_Api
20325
- *
20326
- * @return FS_Plugin_Plan[]|object
20327
- */
20328
- private function _fetch_plugin_plans() {
20329
- $this->_logger->entrance();
20330
- $api = $this->get_current_or_network_user_api_scope();
20331
-
20332
- /**
20333
- * @since 1.2.3 When running in DEV mode, retrieve pending plans as well.
20334
- */
20335
- $result = $api->get( $this->add_show_pending( "/plugins/{$this->_module_id}/plans.json" ), true );
20336
-
20337
- if ( $this->is_api_result_object( $result, 'plans' ) && is_array( $result->plans ) ) {
20338
- for ( $i = 0, $len = count( $result->plans ); $i < $len; $i ++ ) {
20339
- $result->plans[ $i ] = new FS_Plugin_Plan( $result->plans[ $i ] );
20340
- }
20341
-
20342
- $result = $result->plans;
20343
- }
20344
-
20345
- return $result;
20346
- }
20347
-
20348
- /**
20349
- * @author Vova Feldman (@svovaf)
20350
- * @since 2.0.0
20351
- *
20352
- * @param number $plan_id
20353
- *
20354
- * @return \FS_Plugin_Plan|object
20355
- */
20356
- private function fetch_plan_by_id( $plan_id ) {
20357
- $this->_logger->entrance();
20358
- $api = $this->get_current_or_network_user_api_scope();
20359
-
20360
- $result = $api->get( "/plugins/{$this->_module_id}/plans/{$plan_id}.json", true );
20361
-
20362
- return $this->is_api_result_entity( $result ) ?
20363
- new FS_Plugin_Plan( $result ) :
20364
- $result;
20365
- }
20366
-
20367
- /**
20368
- * @author Vova Feldman (@svovaf)
20369
- * @since 1.0.5
20370
- * @uses FS_Api
20371
- *
20372
- * @param number|bool $plugin_id
20373
- * @param number|bool $site_license_id
20374
- * @param array $foreign_licenses @since 2.0.0. This is used by network-activated plugins.
20375
- * @param number|null $blog_id
20376
- *
20377
- * @return FS_Plugin_License[]|object
20378
- */
20379
- private function _fetch_licenses(
20380
- $plugin_id = false,
20381
- $site_license_id = false,
20382
- $foreign_licenses = array(),
20383
- $blog_id = null
20384
- ) {
20385
- $this->_logger->entrance();
20386
-
20387
- $api = $this->get_api_user_scope();
20388
-
20389
- if ( ! is_numeric( $plugin_id ) ) {
20390
- $plugin_id = $this->_plugin->id;
20391
- }
20392
-
20393
- $user_licenses_endpoint = "/plugins/{$plugin_id}/licenses.json?is_enriched=true";
20394
- if ( ! empty ( $foreign_licenses ) ) {
20395
- $foreign_licenses = array(
20396
- // Prefix with `+` to tell the server to include foreign licenses in the licenses collection.
20397
- 'ids' => ( urlencode( '+' ) . implode( ',', $foreign_licenses['ids'] ) ),
20398
- 'license_keys' => implode( ',', array_map( 'urlencode', $foreign_licenses['license_keys'] ) )
20399
- );
20400
-
20401
- $user_licenses_endpoint = add_query_arg( $foreign_licenses, $user_licenses_endpoint );
20402
- }
20403
-
20404
- $result = $api->get( $user_licenses_endpoint, true );
20405
-
20406
- $is_site_license_synced = false;
20407
-
20408
- $api_errors = array();
20409
-
20410
- if ( $this->is_api_result_object( $result, 'licenses' ) &&
20411
- is_array( $result->licenses )
20412
- ) {
20413
- for ( $i = 0, $len = count( $result->licenses ); $i < $len; $i ++ ) {
20414
- $result->licenses[ $i ] = new FS_Plugin_License( $result->licenses[ $i ] );
20415
-
20416
- if ( ( ! $is_site_license_synced ) && is_numeric( $site_license_id ) ) {
20417
- $is_site_license_synced = ( $site_license_id == $result->licenses[ $i ]->id );
20418
- }
20419
- }
20420
-
20421
- $result = $result->licenses;
20422
- } else {
20423
- $api_errors[] = $result;
20424
- $result = array();
20425
- }
20426
-
20427
- if ( ! $is_site_license_synced ) {
20428
- if ( ! is_null( $blog_id ) ) {
20429
- /**
20430
- * If blog ID is not null, the request is for syncing of the license of a single site via the
20431
- * network-level "Account" page.
20432
- *
20433
- * @author Leo Fajardo (@leorw)
20434
- */
20435
- $this->switch_to_blog( $blog_id );
20436
- }
20437
-
20438
- $api = $this->get_api_site_scope();
20439
-
20440
- if ( is_numeric( $site_license_id ) ) {
20441
- // Try to retrieve a foreign license that is linked to the install.
20442
- $api_result = $api->call( '/licenses.json?is_enriched=true' );
20443
-
20444
- if ( $this->is_api_result_object( $api_result, 'licenses' ) &&
20445
- is_array( $api_result->licenses )
20446
- ) {
20447
- $licenses = $api_result->licenses;
20448
-
20449
- if ( ! empty( $licenses ) ) {
20450
- $result[] = new FS_Plugin_License( $licenses[0] );
20451
- }
20452
- } else {
20453
- $api_errors[] = $api_result;
20454
- }
20455
- } else if (
20456
- is_object( $this->_license ) &&
20457
- /**
20458
- * Sync only if the license belongs to the context plugin. `$plugin_id` can be an add-on ID while
20459
- * the FS instance that does the syncing is the parent FS instance.
20460
- *
20461
- * @author Leo Fajardo (@leorw)
20462
- * @since 2.3.0
20463
- */
20464
- $this->_license->plugin_id == $plugin_id
20465
- ) {
20466
- $is_license_in_result = false;
20467
- if ( ! empty( $result ) ) {
20468
- foreach ( $result as $license ) {
20469
- if ( $license->id == $this->_license->id ) {
20470
- $is_license_in_result = true;
20471
- break;
20472
- }
20473
- }
20474
- }
20475
-
20476
- if ( ! $is_license_in_result ) {
20477
- // Fetch foreign license by ID and license key.
20478
- $license = $api->get( "/licenses/{$this->_license->id}.json?license_key=" .
20479
- urlencode( $this->_license->secret_key ) . '&is_enriched=true' );
20480
-
20481
- if ( $this->is_api_result_entity( $license ) ) {
20482
- $result[] = new FS_Plugin_License( $license );
20483
- } else {
20484
- $api_errors[] = $license;
20485
- }
20486
- }
20487
- }
20488
-
20489
- if ( ! is_null( $blog_id ) ) {
20490
- $this->switch_to_blog( $this->_storage->network_install_blog_id );
20491
- }
20492
- }
20493
-
20494
- if ( is_array( $result ) && 0 < count( $result ) ) {
20495
- // If found at least one license, return license collection even if there are errors.
20496
- return $result;
20497
- }
20498
-
20499
- if ( ! empty( $api_errors ) ) {
20500
- // If found any errors and no licenses, return first error.
20501
- return $api_errors[0];
20502
- }
20503
-
20504
- // Fallback to empty licenses list.
20505
- return $result;
20506
- }
20507
-
20508
- /**
20509
- * @author Vova Feldman (@svovaf)
20510
- * @since 2.0.0
20511
- *
20512
- * @param number $license_id
20513
- * @param string $license_key
20514
- *
20515
- * @return \FS_Plugin_License|object
20516
- */
20517
- private function fetch_license_by_key( $license_id, $license_key ) {
20518
- $this->_logger->entrance();
20519
-
20520
- $api = $this->get_current_or_network_user_api_scope();
20521
-
20522
- $result = $api->get( "/licenses/{$license_id}.json?license_key=" . urlencode( $license_key ) );
20523
-
20524
- return $this->is_api_result_entity( $result ) ?
20525
- new FS_Plugin_License( $result ) :
20526
- $result;
20527
- }
20528
-
20529
- /**
20530
- * @author Vova Feldman (@svovaf)
20531
- * @since 1.2.0
20532
- * @uses FS_Api
20533
- *
20534
- * @param number|bool $plugin_id
20535
- * @param bool $flush
20536
- *
20537
- * @return FS_Payment[]|object
20538
- */
20539
- function _fetch_payments( $plugin_id = false, $flush = false ) {
20540
- $this->_logger->entrance();
20541
-
20542
- $api = $this->get_api_user_scope();
20543
-
20544
- if ( ! is_numeric( $plugin_id ) ) {
20545
- $plugin_id = $this->_plugin->id;
20546
- }
20547
-
20548
- $include_bundles = (
20549
- is_object( $this->_plugin ) &&
20550
- FS_Plugin::is_valid_id( $this->_plugin->bundle_id )
20551
- );
20552
-
20553
- $result = $api->get(
20554
- "/plugins/{$plugin_id}/payments.json?include_addons=true" . ($include_bundles ? '&include_bundles=true' : ''),
20555
- $flush
20556
- );
20557
-
20558
- if ( ! isset( $result->error ) ) {
20559
- for ( $i = 0, $len = count( $result->payments ); $i < $len; $i ++ ) {
20560
- $result->payments[ $i ] = new FS_Payment( $result->payments[ $i ] );
20561
- }
20562
- $result = $result->payments;
20563
- }
20564
-
20565
- return $result;
20566
- }
20567
-
20568
- /**
20569
- * @author Vova Feldman (@svovaf)
20570
- * @since 1.2.1.5
20571
- * @uses FS_Api
20572
- *
20573
- * @param bool $flush
20574
- *
20575
- * @return \FS_Billing|mixed
20576
- */
20577
- function _fetch_billing( $flush = false ) {
20578
- require_once WP_FS__DIR_INCLUDES . '/entities/class-fs-billing.php';
20579
-
20580
- $billing = $this->get_api_user_scope()->get( 'billing.json', $flush );
20581
-
20582
- if ( $this->is_api_result_entity( $billing ) ) {
20583
- $billing = new FS_Billing( $billing );
20584
- }
20585
-
20586
- return $billing;
20587
- }
20588
-
20589
- /**
20590
- * @author Vova Feldman (@svovaf)
20591
- * @since 1.0.5
20592
- *
20593
- * @param FS_Plugin_License[] $licenses
20594
- * @param number $module_id
20595
- */
20596
- private function _update_licenses( $licenses, $module_id ) {
20597
- $this->_logger->entrance();
20598
-
20599
- if ( is_array( $licenses ) ) {
20600
- for ( $i = 0, $len = count( $licenses ); $i < $len; $i ++ ) {
20601
- $licenses[ $i ]->updated = time();
20602
- }
20603
- }
20604
-
20605
- $this->_store_licenses( true, $module_id, $licenses );
20606
- }
20607
-
20608
- /**
20609
- * @author Vova Feldman (@svovaf)
20610
- * @since 1.0.4
20611
- *
20612
- * @param bool|number $plugin_id
20613
- * @param bool $flush Since 1.1.7.3
20614
- * @param int $expiration Since 1.2.2.7
20615
- * @param bool|string $newer_than Since 2.2.1
20616
- *
20617
- * @return object|false New plugin tag info if exist.
20618
- */
20619
- private function _fetch_newer_version( $plugin_id = false, $flush = true, $expiration = WP_FS__TIME_24_HOURS_IN_SEC, $newer_than = false ) {
20620
- $latest_tag = $this->_fetch_latest_version( $plugin_id, $flush, $expiration, $newer_than );
20621
-
20622
- if ( ! is_object( $latest_tag ) ) {
20623
- return false;
20624
- }
20625
-
20626
- $plugin_version = $this->get_plugin_version();
20627
-
20628
- // Check if version is actually newer.
20629
- $has_new_version =
20630
- // If it's an non-installed add-on then always return latest.
20631
- ( $this->_is_addon_id( $plugin_id ) && ! $this->is_addon_activated( $plugin_id ) ) ||
20632
- // Compare versions.
20633
- version_compare( $plugin_version, $latest_tag->version, '<' );
20634
-
20635
- $this->_logger->departure( $has_new_version ? 'Found newer plugin version ' . $latest_tag->version : 'No new version' );
20636
-
20637
- $is_latest_version_beta = ( 'beta' === $latest_tag->release_mode );
20638
-
20639
- $this->_storage->beta_data = array(
20640
- 'is_beta' => $is_latest_version_beta,
20641
- 'version' => $latest_tag->version
20642
- );
20643
-
20644
- return $has_new_version ? $latest_tag : false;
20645
- }
20646
-
20647
- /**
20648
- * @author Vova Feldman (@svovaf)
20649
- * @since 1.0.5
20650
- *
20651
- * @param bool|number $plugin_id
20652
- * @param bool $flush Since 1.1.7.3
20653
- * @param int $expiration Since 1.2.2.7
20654
- * @param bool|string $newer_than Since 2.2.1
20655
- *
20656
- * @return bool|FS_Plugin_Tag
20657
- */
20658
- function get_update( $plugin_id = false, $flush = true, $expiration = WP_FS__TIME_24_HOURS_IN_SEC, $newer_than = false ) {
20659
- $this->_logger->entrance();
20660
-
20661
- if ( ! is_numeric( $plugin_id ) ) {
20662
- $plugin_id = $this->_plugin->id;
20663
- }
20664
-
20665
- $this->check_updates( true, $plugin_id, $flush, $expiration, $newer_than );
20666
- $updates = $this->get_all_updates();
20667
-
20668
- return isset( $updates[ $plugin_id ] ) && is_object( $updates[ $plugin_id ] ) ? $updates[ $plugin_id ] : false;
20669
- }
20670
-
20671
- /**
20672
- * Check if site assigned with active license.
20673
- *
20674
- * @author Vova Feldman (@svovaf)
20675
- * @since 1.0.6
20676
- *
20677
- * @deprecated Please use has_active_valid_license() instead because license can be cancelled.
20678
- */
20679
- function has_active_license() {
20680
- return (
20681
- is_object( $this->_license ) &&
20682
- is_numeric( $this->_license->id ) &&
20683
- ! $this->_license->is_expired()
20684
- );
20685
- }
20686
-
20687
- /**
20688
- * Check if site assigned with active & valid (not expired) license.
20689
- *
20690
- * @author Vova Feldman (@svovaf)
20691
- * @since 1.2.1
20692
- *
20693
- * @param bool $check_expiration
20694
- */
20695
- function has_active_valid_license( $check_expiration = true ) {
20696
- return self::is_active_valid_license( $this->_license, $check_expiration );
20697
- }
20698
-
20699
- /**
20700
- * @author Leo Fajardo (@leorw)
20701
- * @since 2.3.1
20702
- */
20703
- function is_data_debug_mode() {
20704
- if ( is_null( $this->is_whitelabeled ) || ! $this->is_whitelabeled ) {
20705
- return false;
20706
- }
20707
-
20708
- $fs = $this->is_addon() ?
20709
- $this->get_parent_instance() :
20710
- $this;
20711
-
20712
- if ( $fs->is_network_active() && fs_is_network_admin() ) {
20713
- $is_developer_license_debug_mode = get_site_transient( "fs_{$this->get_id()}_data_debug_mode" );
20714
- } else {
20715
- $is_developer_license_debug_mode = get_transient( "fs_{$this->get_id()}_data_debug_mode" );
20716
- }
20717
-
20718
- return ( 'true' === $is_developer_license_debug_mode );
20719
- }
20720
-
20721
- /**
20722
- * @author Leo Fajardo (@leorw)
20723
- * @since 2.3.1
20724
- */
20725
- function _set_data_debug_mode() {
20726
- if ( ! $this->is_whitelabeled( true ) ) {
20727
- return;
20728
- }
20729
-
20730
- $license_or_user_key = fs_request_get( 'license_or_user_key' );
20731
-
20732
- $transient_value = ( ! empty( $license_or_user_key ) ) ?
20733
- 'true' :
20734
- 'false';
20735
-
20736
- if ( 'true' === $transient_value ) {
20737
- $stored_key = $this->_storage->get( ! FS_User::is_valid_id( $this->_storage->last_license_user_id ) ?
20738
- 'last_license_key' :
20739
- 'last_license_user_key'
20740
- );
20741
-
20742
- if ( md5( $license_or_user_key ) !== $stored_key ) {
20743
- $this->shoot_ajax_failure( sprintf(
20744
- '%s... %s',
20745
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ),
20746
- $this->get_text_inline(
20747
- 'seems like the key you entered doesn\'t match our records.',
20748
- 'developer-or-license-not-found'
20749
- )
20750
- ) );
20751
- }
20752
- }
20753
-
20754
- if ( $this->is_network_active() && fs_is_network_admin() ) {
20755
- set_site_transient(
20756
- "fs_{$this->get_id()}_data_debug_mode",
20757
- $transient_value,
20758
- WP_FS__TIME_24_HOURS_IN_SEC / 24
20759
- );
20760
- } else {
20761
- set_transient(
20762
- "fs_{$this->get_id()}_data_debug_mode",
20763
- $transient_value,
20764
- WP_FS__TIME_24_HOURS_IN_SEC / 24
20765
- );
20766
- }
20767
-
20768
- if ( 'true' === $transient_value ) {
20769
- $this->_admin_notices->add_sticky(
20770
- $this->get_text_inline(
20771
- 'Debug mode was successfully enabled and will be automatically disabled in 60 min. You can also disable it earlier by clicking the "Stop Debug" link.',
20772
- 'data_debug_mode_enabled'
20773
- ),
20774
- 'data_debug_mode_enabled'
20775
- );
20776
- }
20777
-
20778
- $this->shoot_ajax_success();
20779
- }
20780
-
20781
- /**
20782
- * Check if a given license is active & valid (not expired).
20783
- *
20784
- * @author Vova Feldman (@svovaf)
20785
- * @since 2.1.3
20786
- *
20787
- * @param FS_Plugin_License $license
20788
- * @param bool $check_expiration
20789
- *
20790
- * @return bool
20791
- */
20792
- private static function is_active_valid_license( $license, $check_expiration = true ) {
20793
- return (
20794
- is_object( $license ) &&
20795
- FS_Plugin_License::is_valid_id( $license->id ) &&
20796
- $license->is_active() &&
20797
- ( ! $check_expiration || $license->is_valid() )
20798
- );
20799
- }
20800
-
20801
- /**
20802
- * Checks if there's any site that is associated with an active & valid license.
20803
- * This logic is used to determine if the admin can download the premium code base from a network level admin.
20804
- *
20805
- * @author Vova Feldman (@svovaf)
20806
- * @since 2.1.3
20807
- *
20808
- * @return bool
20809
- */
20810
- function has_any_active_valid_license() {
20811
- if ( ! fs_is_network_admin() ) {
20812
- return $this->has_active_valid_license();
20813
- }
20814
-
20815
- $installs = $this->get_blog_install_map();
20816
- $all_plugin_licenses = self::get_all_licenses( $this->_module_id );
20817
-
20818
- foreach ( $installs as $blog_id => $install ) {
20819
- if ( ! FS_Plugin_License::is_valid_id( $install->license_id ) ) {
20820
- continue;
20821
- }
20822
-
20823
- foreach ( $all_plugin_licenses as $license ) {
20824
- if ( $license->id == $install->license_id ) {
20825
- if ( self::is_active_valid_license( $license ) ) {
20826
- return true;
20827
- }
20828
- }
20829
- }
20830
- }
20831
-
20832
- return false;
20833
- }
20834
-
20835
- /**
20836
- * Check if site assigned with license with enabled features.
20837
- *
20838
- * @author Vova Feldman (@svovaf)
20839
- * @since 1.0.6
20840
- *
20841
- * @return bool
20842
- */
20843
- function has_features_enabled_license() {
20844
- return (
20845
- is_object( $this->_license ) &&
20846
- is_numeric( $this->_license->id ) &&
20847
- $this->_license->is_features_enabled()
20848
- );
20849
- }
20850
-
20851
- /**
20852
- * Checks if the product is activated with a bundle license.
20853
- *
20854
- * @author Leo Fajardo (@leorw)
20855
- * @since 2.4.0
20856
- *
20857
- * @return bool
20858
- */
20859
- function is_activated_with_bundle_license() {
20860
- if ( ! $this->has_features_enabled_license() ) {
20861
- return false;
20862
- }
20863
-
20864
- return FS_Plugin_License::is_valid_id( $this->_license->parent_license_id );
20865
- }
20866
-
20867
- /**
20868
- * Check if user is a trial or have feature enabled license.
20869
- *
20870
- * @author Vova Feldman (@svovaf)
20871
- * @since 1.1.7
20872
- *
20873
- * @return bool
20874
- */
20875
- function can_use_premium_code() {
20876
- return $this->is_trial() || $this->has_features_enabled_license();
20877
- }
20878
-
20879
- /**
20880
- * Checks if the current user can activate plugins or switch themes. Note that this method should only be used
20881
- * after the `init` action is triggered because it is using `current_user_can()` which is only functional after
20882
- * the context user is authenticated.
20883
- *
20884
- * @author Leo Fajardo (@leorw)
20885
- * @since 1.2.2
20886
- *
20887
- * @return bool
20888
- */
20889
- function is_user_admin() {
20890
- /**
20891
- * Require a super-admin when network activated, running from the network level OR if
20892
- * running from the site level but not delegated the opt-in.
20893
- *
20894
- * @author Vova Feldman (@svovaf)
20895
- * @since 2.0.0
20896
- */
20897
- if ( $this->_is_network_active &&
20898
- ( fs_is_network_admin() || ! $this->is_delegated_connection() )
20899
- ) {
20900
- return is_super_admin();
20901
- }
20902
-
20903
- return ( $this->is_plugin() && current_user_can( is_multisite() ? 'manage_options' : 'activate_plugins' ) )
20904
- || ( $this->is_theme() && current_user_can( 'switch_themes' ) );
20905
- }
20906
-
20907
- /**
20908
- * Sync site's plan.
20909
- *
20910
- * @author Vova Feldman (@svovaf)
20911
- * @since 1.0.3
20912
- *
20913
- * @uses FS_Api
20914
- *
20915
- * @param bool $background Hints the method if it's a background sync. If false, it means that was initiated by
20916
- * the admin.
20917
- * @param bool $is_context_single_site @since 2.0.0. This is used when syncing a license for a single install from the
20918
- * network-level "Account" page.
20919
- * @param int|null $current_blog_id @since 2.2.3. This is passed from the `execute_cron` method and used by the
20920
- * `_sync_plugin_license` method in order to switch to the previous blog when sending
20921
- * updates for a single site in case `execute_cron` has switched to a different blog.
20922
- */
20923
- private function _sync_license( $background = false, $is_context_single_site = false, $current_blog_id = null ) {
20924
- $this->_logger->entrance();
20925
-
20926
- $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
20927
-
20928
- $is_addon_sync = ( ! $this->_plugin->is_addon() && $plugin_id != $this->get_id() );
20929
-
20930
- if ( $is_addon_sync ) {
20931
- $this->_sync_addon_license( $plugin_id, $background );
20932
- } else {
20933
- $this->_sync_plugin_license( $background, true, $is_context_single_site, $current_blog_id );
20934
- }
20935
-
20936
- $this->do_action( 'after_account_plan_sync', $this->get_plan_name() );
20937
- }
20938
-
20939
- /**
20940
- * Sync plugin's add-on license.
20941
- *
20942
- * @author Vova Feldman (@svovaf)
20943
- * @since 1.0.6
20944
- * @uses FS_Api
20945
- *
20946
- * @param number $addon_id
20947
- * @param bool $background
20948
- */
20949
- private function _sync_addon_license( $addon_id, $background ) {
20950
- $this->_logger->entrance();
20951
-
20952
- if ( $this->is_addon_activated( $addon_id ) ) {
20953
- // If already installed, use add-on sync.
20954
- $fs_addon = self::get_instance_by_id( $addon_id );
20955
-
20956
- if (
20957
- // Add-on is network activated and network integrated.
20958
- $fs_addon->is_network_active() ||
20959
- // Background sync cron.
20960
- self::is_cron() ||
20961
- // Add-on is not network activated or not network integrated.
20962
- ! fs_is_network_admin()
20963
- ) {
20964
- $fs_addon->_sync_license( $background );
20965
-
20966
- return;
20967
- }
20968
- }
20969
-
20970
- // Validate add-on exists.
20971
- $addon = $this->get_addon( $addon_id );
20972
-
20973
- if ( ! is_object( $addon ) ) {
20974
- return;
20975
- }
20976
-
20977
- // Add add-on into account add-ons.
20978
- $account_addons = $this->get_account_addons();
20979
- if ( ! is_array( $account_addons ) ) {
20980
- $account_addons = array();
20981
- }
20982
- $account_addons[] = $addon->id;
20983
- $account_addons = array_unique( $account_addons );
20984
- $this->_store_account_addons( $account_addons );
20985
-
20986
- // Load add-on licenses.
20987
- $licenses = $this->_fetch_licenses( $addon->id );
20988
-
20989
- // Sync add-on licenses.
20990
- if ( $this->is_array_instanceof( $licenses, 'FS_Plugin_License' ) ) {
20991
- $this->_update_licenses( $licenses, $addon->id );
20992
-
20993
- if ( ! $this->is_addon_installed( $addon->id ) && FS_License_Manager::has_premium_license( $licenses ) ) {
20994
- $plans_result = $this->get_api_site_or_plugin_scope()->get( $this->add_show_pending( "/addons/{$addon_id}/plans.json" ) );
20995
-
20996
- if ( ! isset( $plans_result->error ) ) {
20997
- $plans = array();
20998
- foreach ( $plans_result->plans as $plan ) {
20999
- $plans[] = new FS_Plugin_Plan( $plan );
21000
- }
21001
-
21002
- $this->_admin_notices->add_sticky(
21003
- sprintf(
21004
- ( FS_Plan_Manager::instance()->has_free_plan( $plans ) ?
21005
- $this->get_text_inline( 'Your %s Add-on plan was successfully upgraded.', 'addon-successfully-upgraded-message' ) :
21006
- /* translators: %s:product name, e.g. Facebook add-on was successfully... */
21007
- $this->get_text_inline( '%s Add-on was successfully purchased.', 'addon-successfully-purchased-message' ) ),
21008
- $addon->title
21009
- ) . ' ' . $this->get_latest_download_link(
21010
- $this->get_text_inline( 'Download the latest version', 'download-latest-version' ),
21011
- $addon_id
21012
- ),
21013
- 'addon_plan_upgraded_' . $addon->slug,
21014
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
21015
- );
21016
- }
21017
- }
21018
- }
21019
- }
21020
-
21021
- /**
21022
- * Sync site's plugin plan.
21023
- *
21024
- * @author Vova Feldman (@svovaf)
21025
- * @since 1.0.6
21026
- * @uses FS_Api
21027
- *
21028
- * @param bool $background Hints the method if it's a background sync. If false, it means that was initiated by the admin.
21029
- * @param bool $send_installs_update Since 2.0.0
21030
- * @param bool $is_context_single_site Since 2.0.0. This is used when sending an update for a single install and
21031
- * syncing its license from the network-level "Account" page (e.g.: after
21032
- * activating a license only for the single install).
21033
- * @param int|null $current_blog_id Since 2.2.3. This is passed from the `execute_cron` method so that it
21034
- * can be used here to switch to the previous blog in case `execute_cron`
21035
- * has switched to a different blog.
21036
- */
21037
- private function _sync_plugin_license(
21038
- $background = false,
21039
- $send_installs_update = true,
21040
- $is_context_single_site = false,
21041
- $current_blog_id = null
21042
- ) {
21043
- $this->_logger->entrance();
21044
-
21045
- $plan_change = 'none';
21046
-
21047
- $is_site_level_sync = ( $is_context_single_site || fs_is_blog_admin() || ! $this->_is_network_active );
21048
-
21049
- if ( ! $send_installs_update ) {
21050
- $site = $this->_site;
21051
- } else {
21052
- /**
21053
- * Sync site info.
21054
- *
21055
- * @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.
21056
- */
21057
- if ( $is_site_level_sync ) {
21058
- /**
21059
- * Switch to the previous blog since `execute_cron` may have switched to a different blog.
21060
- *
21061
- * @author Leo Fajardo (@leorw)
21062
- * @since 2.2.3
21063
- */
21064
- if ( is_numeric( $current_blog_id ) ) {
21065
- $this->switch_to_blog( $current_blog_id );
21066
- }
21067
-
21068
- $result = $this->send_install_update( array(), true, true );
21069
- $is_valid = $this->is_api_result_entity( $result );
21070
- } else {
21071
- $result = $this->send_installs_update( array(), true, true );
21072
- $is_valid = $this->is_api_result_object( $result, 'installs' );
21073
- }
21074
-
21075
- if ( ! $is_valid ) {
21076
- if ( $is_context_single_site ) {
21077
- // Switch back to the main blog so that the following logic will have the right entities.
21078
- $this->switch_to_blog( $this->_storage->network_install_blog_id );
21079
- }
21080
-
21081
- // Show API message only if not background sync or if paying customer.
21082
- if ( ! $background || $this->is_paying() ) {
21083
- // Try to ping API to see if not blocked.
21084
- if ( ! FS_Api::test() ) {
21085
- /**
21086
- * Failed to ping API - blocked!
21087
- *
21088
- * @author Vova Feldman (@svovaf)
21089
- * @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.
21090
- */
21091
-
21092
- if ( ! self::$_global_admin_notices->has_sticky( 'api_blocked' ) ) {
21093
- // Add notice immediately if not a background sync.
21094
- $add_notice = ( ! $background );
21095
-
21096
- if ( ! $add_notice ) {
21097
- $counter = (int) get_transient( '_fs_api_connection_retry_counter' );
21098
-
21099
- // We only want to add the notice after 3 consecutive failures.
21100
- $add_notice = ( 3 <= $counter );
21101
-
21102
- if ( ! $add_notice ) {
21103
- /**
21104
- * Update counter transient only if notice shouldn't be added. If it is added the transient will be reset anyway, because the retries mechanism should only start counting if the admin isn't aware of the connectivity issue.
21105
- *
21106
- * Also, since the background sync happens once a day, setting the transient expiration for a week should be enough to count 3 failures, if there's an actual connectivity issue.
21107
- */
21108
- set_transient( '_fs_api_connection_retry_counter', $counter + 1, WP_FS__TIME_WEEK_IN_SEC );
21109
- }
21110
- }
21111
-
21112
- // Add notice instantly for not-background sync and only after 3 failed attempts for background sync.
21113
- if ( $add_notice ) {
21114
- self::$_global_admin_notices->add(
21115
- sprintf(
21116
- $this->get_text_inline( 'Your server is blocking the access to Freemius\' API, which is crucial for %1$s synchronization. Please contact your host to whitelist %2$s', 'server-blocking-access' ),
21117
- $this->get_plugin_name(),
21118
- '<b>' . implode( ', ', $this->apply_filters( 'api_domains', array(
21119
- 'api.freemius.com',
21120
- 'wp.freemius.com'
21121
- ) ) ) . '</b>'
21122
- ) . '<br> ' . $this->get_text_inline( 'Error received from the server:', 'server-error-message' ) . var_export( $result->error, true ),
21123
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
21124
- 'error',
21125
- $background,
21126
- 'api_blocked'
21127
- );
21128
-
21129
- // Notice was just shown, reset connectivity counter.
21130
- delete_transient( '_fs_api_connection_retry_counter' );
21131
- }
21132
- }
21133
- } else if ( is_object( $result ) ) {
21134
- // Authentication params are broken.
21135
- $this->_admin_notices->add(
21136
- $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' ) . '<br> ' . $this->get_text_inline( 'Error received from the server:', 'server-error-message' ) . var_export( $result->error, true ),
21137
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
21138
- 'error'
21139
- );
21140
- }
21141
- }
21142
-
21143
- // No reason to continue with license sync while there are API issues.
21144
- return;
21145
- }
21146
-
21147
- // API is working now. Delete the transient and start afresh.
21148
- delete_transient('_fs_api_connection_retry_counter');
21149
-
21150
- if ( $is_site_level_sync ) {
21151
- $site = new FS_Site( $result );
21152
- } else {
21153
- // Map site addresses to their blog IDs.
21154
- $address_to_blog_map = $this->get_address_to_blog_map();
21155
-
21156
- // Find the current context install.
21157
- $site = null;
21158
- foreach ( $result->installs as $install ) {
21159
- if ( $install->id == $this->_site->id ) {
21160
- $site = new FS_Site( $install );
21161
- } else {
21162
- $address = trailingslashit( fs_strip_url_protocol( $install->url ) );
21163
- $blog_id = $address_to_blog_map[ $address ];
21164
-
21165
- $this->_store_site( true, $blog_id, new FS_Site( $install ) );
21166
- }
21167
- }
21168
- }
21169
-
21170
- // Sync plans.
21171
- $this->_sync_plans();
21172
- }
21173
-
21174
- // Remove sticky API connectivity message.
21175
- self::$_global_admin_notices->remove_sticky( 'api_blocked' );
21176
-
21177
- if ( ! $this->has_paid_plan() ) {
21178
- $this->_site = $site;
21179
- $this->_store_site(
21180
- true,
21181
- $is_site_level_sync ?
21182
- null :
21183
- $this->get_network_install_blog_id()
21184
- );
21185
- } else {
21186
- $context_blog_id = 0;
21187
-
21188
- if ( $is_context_single_site ) {
21189
- $context_blog_id = get_current_blog_id();
21190
-
21191
- // Switch back to the main blog in order to properly sync the license.
21192
- $this->switch_to_blog( $this->_storage->network_install_blog_id );
21193
- }
21194
-
21195
- /**
21196
- * Sync licenses. Pass the site's license ID so that the foreign licenses will be fetched if the license
21197
- * associated with that ID is not included in the user's licenses collection.
21198
- */
21199
- $this->_sync_licenses(
21200
- $site->license_id,
21201
- ( $is_context_single_site ?
21202
- $context_blog_id :
21203
- null
21204
- )
21205
- );
21206
-
21207
- if ( $is_context_single_site ) {
21208
- $this->switch_to_blog( $context_blog_id );
21209
- }
21210
-
21211
- // Check if plan / license changed.
21212
- if ( $site->plan_id != $this->_site->plan_id ||
21213
- // Check if trial started.
21214
- $site->trial_plan_id != $this->_site->trial_plan_id ||
21215
- $site->trial_ends != $this->_site->trial_ends ||
21216
- // Check if license changed.
21217
- $site->license_id != $this->_site->license_id
21218
- ) {
21219
- if ( $site->is_trial() && ( ! $this->_site->is_trial() || $site->trial_ends != $this->_site->trial_ends ) ) {
21220
- // New trial started.
21221
- $this->_site = $site;
21222
- $plan_change = 'trial_started';
21223
-
21224
- // For trial with subscription use-case.
21225
- $new_license = is_null( $site->license_id ) ? null : $this->_get_license_by_id( $site->license_id );
21226
-
21227
- if ( is_object( $new_license ) && $new_license->is_valid() ) {
21228
- $this->_site = $site;
21229
- $this->_update_site_license( $new_license );
21230
- $this->_store_licenses();
21231
-
21232
- $this->_sync_site_subscription( $this->_license );
21233
- }
21234
- } else if ( $this->_site->is_trial() && ! $site->is_trial() && ! is_numeric( $site->license_id ) ) {
21235
- // Was in trial, but now trial expired and no license ID.
21236
- // New trial started.
21237
- $this->_site = $site;
21238
- $plan_change = 'trial_expired';
21239
- } else {
21240
- $is_free = $this->is_free_plan();
21241
-
21242
- // Make sure license exist and not expired.
21243
- $new_license = is_null( $site->license_id ) ?
21244
- null :
21245
- $this->_get_license_by_id( $site->license_id );
21246
-
21247
- if ( $is_free && is_null( $new_license ) && $this->has_any_license() && $this->_license->is_cancelled ) {
21248
- // License cancelled.
21249
- $this->_site = $site;
21250
- $this->_update_site_license( $new_license );
21251
- $this->_store_licenses();
21252
-
21253
- $plan_change = 'cancelled';
21254
- } else if ( $is_free && ( ( ! is_object( $new_license ) || $new_license->is_expired() ) ) ) {
21255
- // The license is expired, so ignore upgrade method.
21256
- $this->_site = $site;
21257
- } else {
21258
- // License changed.
21259
- $this->_site = $site;
21260
-
21261
- /**
21262
- * IMPORTANT:
21263
- * The line below should be executed before trying to activate the license on the rest of the network, otherwise, the license' activation counters may be out of sync + there's no need to activate the license on the context site since it's already activated on it.
21264
- *
21265
- * @author Vova Feldman (@svovaf)
21266
- * @since 2.0.0
21267
- */
21268
- $this->_update_site_license( $new_license );
21269
-
21270
- if ( ! $is_context_single_site &&
21271
- fs_is_network_admin() &&
21272
- $this->_is_network_active &&
21273
- $new_license->quota > 1 &&
21274
- get_blog_count() > 1
21275
- ) {
21276
- // See if license can activated on all sites.
21277
- if ( ! $this->try_activate_license_on_network( $this->_user, $new_license ) ) {
21278
- if ( ! fs_request_get_bool( 'auto_install' ) ) {
21279
- // Open the license activation dialog box on the account page.
21280
- add_action( 'admin_footer', array(
21281
- &$this,
21282
- '_open_license_activation_dialog_box'
21283
- ) );
21284
- }
21285
- }
21286
- }
21287
-
21288
- $this->_store_licenses();
21289
-
21290
- $plan_change = $is_free ?
21291
- ( $this->is_only_premium() ? 'activated' : 'upgraded' ) :
21292
- ( is_object( $new_license ) ?
21293
- 'changed' :
21294
- 'downgraded' );
21295
- }
21296
- }
21297
-
21298
- // Store updated site info.
21299
- $this->_store_site(
21300
- true,
21301
- $is_site_level_sync ?
21302
- null :
21303
- $this->get_network_install_blog_id()
21304
- );
21305
- } else {
21306
- if ( ! is_object( $this->_license ) ) {
21307
- $this->maybe_update_whitelabel_flag(
21308
- FS_Plugin_License::is_valid_id( $site->license_id ) ?
21309
- $this->get_license_by_id( $site->license_id ) :
21310
- null
21311
- );
21312
- } else {
21313
- $this->maybe_update_whitelabel_flag( $this->_license );
21314
-
21315
- if ( $this->_license->is_expired() ) {
21316
- if ( ! $this->has_features_enabled_license() ) {
21317
- $this->_deactivate_license();
21318
- $plan_change = 'downgraded';
21319
- } else {
21320
- $last_time_expired_license_notice_was_shown = $this->_storage->get( 'expired_license_notice_shown', 0 );
21321
-
21322
- if ( time() - ( 14 * WP_FS__TIME_24_HOURS_IN_SEC ) >= $last_time_expired_license_notice_was_shown ) {
21323
- /**
21324
- * Show the expired license notice every 14 days.
21325
- *
21326
- * @author Leo Fajardo (@leorw)
21327
- * @since 2.3.1
21328
- */
21329
- $plan_change = 'expired';
21330
- }
21331
- }
21332
- }
21333
- }
21334
-
21335
- if ( is_numeric( $site->license_id ) && is_object( $this->_license ) ) {
21336
- $this->_sync_site_subscription( $this->_license );
21337
- }
21338
- }
21339
-
21340
- if ( ! $this->is_addon() &&
21341
- $this->_site->is_beta() !== $site->is_beta()
21342
- ) {
21343
- // Beta flag updated.
21344
- $this->_site = $site;
21345
-
21346
- $this->_store_site(
21347
- true,
21348
- $is_site_level_sync ?
21349
- null :
21350
- $this->get_network_install_blog_id()
21351
- );
21352
- }
21353
-
21354
- if ( $this->is_addon() || $this->has_addons() ) {
21355
- /**
21356
- * Purge the valid user licenses cache so that when the "Account" or the "Add-Ons" page is loaded,
21357
- * an updated valid user licenses collection will be fetched from the server which is used to also
21358
- * update the account add-ons (add-ons the user has licenses for).
21359
- *
21360
- * @author Leo Fajardo (@leorw)
21361
- * @since 2.2.4
21362
- */
21363
- $this->purge_valid_user_licenses_cache();
21364
- }
21365
- }
21366
-
21367
- $hmm_text = $this->get_text_x_inline( 'Hmm', 'something somebody says when they are thinking about what you have just said.', 'hmm' ) . '...';
21368
-
21369
- if ( $this->apply_filters( 'has_paid_plan_account', $this->has_paid_plan() ) ) {
21370
- switch ( $plan_change ) {
21371
- case 'none':
21372
- if ( ! $background && is_admin() ) {
21373
- $plan = $this->is_trial() ?
21374
- $this->get_trial_plan() :
21375
- $this->get_plan();
21376
-
21377
- if ( $plan->is_free() ) {
21378
- $this->_admin_notices->add(
21379
- sprintf(
21380
- $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' ),
21381
- '<i><b>' . $plan->title . ( $this->is_trial() ? ' ' . $this->get_text_x_inline( 'Trial', 'trial period', 'trial' ) : '' ) . '</b></i>'
21382
- ) . ' ' . sprintf(
21383
- '<a href="%s">%s</a>',
21384
- $this->contact_url(
21385
- 'bug',
21386
- 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' ),
21387
- strtoupper( $plan->name )
21388
- )
21389
- ),
21390
- $this->get_text_inline( 'Please contact us here', 'contact-us-here' )
21391
- ),
21392
- $hmm_text
21393
- );
21394
- }
21395
- }
21396
- break;
21397
- case 'upgraded':
21398
- case 'activated':
21399
- $this->_admin_notices->add_sticky(
21400
- ( 'activated' === $plan_change ) ?
21401
- $this->get_text_inline( 'Your plan was successfully activated.', 'plan-activated-message' ) :
21402
- $this->get_text_inline( 'Your plan was successfully upgraded.', 'plan-upgraded-message' ) .
21403
- $this->get_complete_upgrade_instructions(),
21404
- 'plan_upgraded',
21405
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
21406
- );
21407
-
21408
- $this->_admin_notices->remove_sticky( array(
21409
- 'trial_started',
21410
- 'trial_promotion',
21411
- 'trial_expired',
21412
- 'activation_complete',
21413
- 'license_expired',
21414
- ) );
21415
- break;
21416
- case 'changed':
21417
- $this->_admin_notices->add_sticky(
21418
- sprintf(
21419
- $this->get_text_inline( 'Your plan was successfully changed to %s.', 'plan-changed-to-x-message' ),
21420
- $this->get_plan_title()
21421
- ),
21422
- 'plan_changed'
21423
- );
21424
-
21425
- $this->_admin_notices->remove_sticky( array(
21426
- 'trial_started',
21427
- 'trial_promotion',
21428
- 'trial_expired',
21429
- 'activation_complete',
21430
- ) );
21431
- break;
21432
- case 'downgraded':
21433
- $this->_admin_notices->add_sticky(
21434
- ($this->has_free_plan() ?
21435
- 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 ) :
21436
- /* translators: %1$s: product title; %2$s, %3$s: wrapping HTML anchor element; %4$s: 'plugin', 'theme', or 'add-on'. */
21437
- sprintf( $this->get_text_inline( 'Your license has expired. %1$sUpgrade now%2$s to continue using the %3$s without interruptions.', 'license-expired-blocking-message_premium-only' ), sprintf('<a href="%s">', $this->pricing_url()), '</a>', $this->get_module_label(true) ) ),
21438
- 'license_expired',
21439
- $hmm_text
21440
- );
21441
- $this->_admin_notices->remove_sticky( 'plan_upgraded' );
21442
- break;
21443
- case 'cancelled':
21444
- $this->_admin_notices->add(
21445
- $this->get_text_inline( 'Your license has been cancelled. If you think it\'s a mistake, please contact support.', 'license-cancelled' ) . ' ' .
21446
- sprintf(
21447
- '<a href="%s">%s</a>',
21448
- $this->contact_url( 'bug' ),
21449
- $this->get_text_inline( 'Please contact us here', 'contact-us-here' )
21450
- ),
21451
- $hmm_text,
21452
- 'error'
21453
- );
21454
- $this->_admin_notices->remove_sticky( 'plan_upgraded' );
21455
- break;
21456
- case 'expired':
21457
- $this->_admin_notices->add_sticky(
21458
- 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->get_plan()->title ),
21459
- 'license_expired',
21460
- $hmm_text
21461
- );
21462
-
21463
- $this->_storage->expired_license_notice_shown = WP_FS__SCRIPT_START_TIME;
21464
-
21465
- $this->_admin_notices->remove_sticky( 'plan_upgraded' );
21466
- break;
21467
- case 'trial_started':
21468
- $this->_admin_notices->add_sticky(
21469
- sprintf(
21470
- $this->get_text_inline( 'Your trial has been successfully started.', 'trial-started-message' ),
21471
- '<i>' . $this->get_plugin_name() . '</i>'
21472
- ) . $this->get_complete_upgrade_instructions( $this->get_trial_plan()->title ),
21473
- 'trial_started',
21474
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
21475
- );
21476
-
21477
- $this->_admin_notices->remove_sticky( array(
21478
- 'trial_promotion',
21479
- ) );
21480
- break;
21481
- case 'trial_expired':
21482
- $this->_admin_notices->add_sticky(
21483
- ($this->has_free_plan() ?
21484
- $this->get_text_inline( 'Your free trial has expired. You can still continue using all our free features.', 'trial-expired-message' ) :
21485
- /* translators: %1$s: product title; %2$s, %3$s: wrapping HTML anchor element; %4$s: 'plugin', 'theme', or 'add-on'. */
21486
- sprintf( $this->get_text_inline( 'Your free trial has expired. %1$sUpgrade now%2$s to continue using the %3$s without interruptions.', 'trial-expired-message_premium-only' ), sprintf('<a href="%s">', $this->pricing_url()), '</a>', $this->get_module_label(true))),
21487
- 'trial_expired',
21488
- $hmm_text
21489
- );
21490
- $this->_admin_notices->remove_sticky( array(
21491
- 'trial_started',
21492
- 'trial_promotion',
21493
- 'plan_upgraded',
21494
- ) );
21495
- break;
21496
- }
21497
- }
21498
-
21499
- if ( 'none' !== $plan_change ) {
21500
- if (
21501
- ! is_object( $this->_license ) ||
21502
- ! $this->_license->is_whitelabeled
21503
- ) {
21504
- $this->_admin_notices->remove_sticky( 'license_whitelabeled' );
21505
- }
21506
-
21507
- $this->do_action( 'after_license_change', $plan_change, $this->get_plan() );
21508
- }
21509
- }
21510
-
21511
- /**
21512
- * Include the required JS at the footer of the admin to trigger the license activation dialog box.
21513
- *
21514
- * @author Vova Feldman (@svovaf)
21515
- * @since 2.0.0
21516
- */
21517
- public function _open_license_activation_dialog_box() {
21518
- $vars = array( 'license_id' => $this->_site->license_id );
21519
- fs_require_once_template( 'js/open-license-activation.php', $vars );
21520
- }
21521
-
21522
- /**
21523
- * @author Vova Feldman (@svovaf)
21524
- * @since 1.0.5
21525
- *
21526
- * @param bool $background
21527
- * @param FS_Plugin_License|null $premium_license
21528
- */
21529
- protected function _activate_license( $background = false, $premium_license = null ) {
21530
- $this->_logger->entrance();
21531
-
21532
- if ( is_null( $premium_license ) ) {
21533
- $license_id = fs_request_get( 'license_id' );
21534
-
21535
- if ( is_object( $this->_site ) &&
21536
- FS_Plugin_License::is_valid_id( $license_id ) &&
21537
- $license_id == $this->_site->license_id
21538
- ) {
21539
- // License is already activated.
21540
- return;
21541
- }
21542
-
21543
- $premium_license = FS_Plugin_License::is_valid_id( $license_id ) ?
21544
- $this->_get_license_by_id( $license_id ) :
21545
- $this->_get_available_premium_license();
21546
- }
21547
-
21548
- if ( ! is_object( $premium_license ) ) {
21549
- return;
21550
- }
21551
-
21552
- if ( ! is_object( $this->_site ) ) {
21553
- // Not yet opted-in.
21554
- $user = $this->get_current_or_network_user();
21555
- if ( ! is_object( $user ) ) {
21556
- $user = self::_get_user_by_id( $premium_license->user_id );
21557
- }
21558
-
21559
- if ( is_object( $user ) ) {
21560
- $this->install_with_user( $user, $premium_license->secret_key, false, false, false );
21561
- } else {
21562
- $this->opt_in(
21563
- false,
21564
- false,
21565
- false,
21566
- $premium_license->secret_key
21567
- );
21568
-
21569
- return;
21570
- }
21571
- }
21572
-
21573
-
21574
- /**
21575
- * If the premium license is already associated with the install, just
21576
- * update the license reference (activation is not required).
21577
- *
21578
- * @since 1.1.9
21579
- */
21580
- if ( $premium_license->id == $this->_site->license_id ) {
21581
- // License is already activated.
21582
- $this->_update_site_license( $premium_license );
21583
- $this->_store_account();
21584
-
21585
- return;
21586
- }
21587
-
21588
- if ( $this->_site->user_id != $premium_license->user_id ) {
21589
- $api_request_params = array( 'license_key' => $premium_license->secret_key );
21590
- } else {
21591
- $api_request_params = array();
21592
- }
21593
-
21594
- $api = $this->get_api_site_scope();
21595
- $license = $api->call( "/licenses/{$premium_license->id}.json?is_enriched=true", 'put', $api_request_params );
21596
-
21597
- if ( ! $this->is_api_result_entity( $license ) ) {
21598
- if ( ! $background ) {
21599
- $this->_admin_notices->add( sprintf(
21600
- '%s %s',
21601
- $this->get_text_inline( 'It looks like the license could not be activated.', 'license-activation-failed-message' ),
21602
- ( is_object( $license ) && isset( $license->error ) ?
21603
- $license->error->message :
21604
- sprintf( '%s<br><code>%s</code>',
21605
- $this->get_text_inline( 'Error received from the server:', 'server-error-message' ),
21606
- var_export( $license, true )
21607
- )
21608
- )
21609
- ),
21610
- $this->get_text_x_inline( 'Hmm', 'something somebody says when they are thinking about what you have just said.', 'hmm' ) . '...',
21611
- 'error'
21612
- );
21613
- }
21614
-
21615
- return;
21616
- }
21617
-
21618
- $premium_license = new FS_Plugin_License( $license );
21619
-
21620
- // Updated site plan.
21621
- $site = $this->get_api_site_scope()->get( '/', true );
21622
- if ( $this->is_api_result_entity( $site ) ) {
21623
- $this->_site = new FS_Site( $site );
21624
- }
21625
- $this->_update_site_license( $premium_license );
21626
-
21627
- $this->_store_account();
21628
-
21629
- if ( $this->is_addon() || $this->has_addons() ) {
21630
- /**
21631
- * Purge the valid user licenses cache so that when the "Account" or the "Add-Ons" page is loaded,
21632
- * an updated valid user licenses collection will be fetched from the server which is used to also
21633
- * update the account add-ons (add-ons the user has licenses for).
21634
- *
21635
- * @author Leo Fajardo (@leorw)
21636
- * @since 2.2.4
21637
- */
21638
- $this->purge_valid_user_licenses_cache();
21639
- }
21640
-
21641
- if ( ! $background ) {
21642
- $this->_admin_notices->add_sticky(
21643
- $this->get_text_inline( 'Your license was successfully activated.', 'license-activated-message' ) .
21644
- $this->get_complete_upgrade_instructions(),
21645
- 'license_activated',
21646
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
21647
- );
21648
- }
21649
-
21650
- $this->_admin_notices->remove_sticky( array(
21651
- 'trial_promotion',
21652
- 'license_expired',
21653
- ) );
21654
- }
21655
-
21656
- /**
21657
- * @author Vova Feldman (@svovaf)
21658
- * @since 1.0.5
21659
- *
21660
- * @param bool $show_notice
21661
- */
21662
- protected function _deactivate_license( $show_notice = true ) {
21663
- $this->_logger->entrance();
21664
-
21665
- $hmm_text = $this->get_text_x_inline( 'Hmm', 'something somebody says when they are thinking about what you have just said.', 'hmm' ) . '...';
21666
-
21667
- if ( ! FS_Plugin_License::is_valid_id( $this->_site->license_id ) ) {
21668
- $this->_admin_notices->add(
21669
- sprintf( $this->get_text_inline( 'It looks like your site currently doesn\'t have an active license.', 'no-active-license-message' ), $this->get_plan_title() ),
21670
- $hmm_text
21671
- );
21672
-
21673
- return;
21674
- }
21675
-
21676
- $api = $this->get_api_site_scope();
21677
- $license = $api->call( "/licenses/{$this->_site->license_id}.json", 'delete' );
21678
-
21679
- $this->handle_license_deactivation_result( $license, $hmm_text, $show_notice );
21680
- }
21681
-
21682
- /**
21683
- * @author Leo Fajardo (@leorw)
21684
- * @since 2.2.1
21685
- *
21686
- * @param FS_Plugin_License $license
21687
- * @param bool|string $hmm_text
21688
- * @param bool $show_notice
21689
- */
21690
- private function handle_license_deactivation_result( $license, $hmm_text = false, $show_notice = true ) {
21691
- if ( isset( $license->error ) ) {
21692
- $this->_admin_notices->add(
21693
- $this->get_text_inline( 'It looks like the license deactivation failed.', 'license-deactivation-failed-message' ) . '<br> ' .
21694
- $this->get_text_inline( 'Error received from the server:', 'server-error-message' ) . ' ' . var_export( $license->error, true ),
21695
- $hmm_text,
21696
- 'error'
21697
- );
21698
-
21699
- return;
21700
- }
21701
-
21702
- // Update license cache.
21703
- if ( is_array( $this->_licenses ) ) {
21704
- for ( $i = 0, $len = count( $this->_licenses ); $i < $len; $i ++ ) {
21705
- if ( $license->id == $this->_licenses[ $i ]->id ) {
21706
- $this->_licenses[ $i ] = new FS_Plugin_License( $license );
21707
- }
21708
- }
21709
- }
21710
-
21711
- // Update site plan to default.
21712
- $this->_sync_plans();
21713
- $this->_site->plan_id = $this->_plans[0]->id;
21714
- // Unlink license from site.
21715
- $this->_update_site_license( null );
21716
-
21717
- $this->_store_account();
21718
-
21719
- if ( $show_notice ) {
21720
- $this->_admin_notices->add(
21721
- sprintf( $this->is_only_premium() ?
21722
- $this->get_text_inline( 'Your %s license was successfully deactivated.', 'license-deactivation-message_premium-only' ) :
21723
- $this->get_text_inline( 'Your license was successfully deactivated, you are back to the %s plan.', 'license-deactivation-message' ),
21724
- $this->get_plan_title()
21725
- ),
21726
- $this->get_text_inline( 'O.K', 'ok' )
21727
- );
21728
- }
21729
-
21730
- $this->_admin_notices->remove_sticky( array(
21731
- 'plan_upgraded',
21732
- 'license_activated',
21733
- ) );
21734
- }
21735
-
21736
- /**
21737
- * Site plan downgrade.
21738
- *
21739
- * @author Vova Feldman (@svovaf)
21740
- * @since 1.0.4
21741
- *
21742
- * @return object
21743
- *
21744
- * @uses FS_Api
21745
- */
21746
- private function _downgrade_site() {
21747
- $this->_logger->entrance();
21748
-
21749
- $deactivate_license = fs_request_get_bool( 'deactivate_license' );
21750
-
21751
- $api = $this->get_api_site_scope();
21752
- $site = $api->call( 'downgrade.json', 'put', array( 'deactivate_license' => $deactivate_license ) );
21753
-
21754
- $plan_downgraded = false;
21755
- $plan = false;
21756
- if ( $this->is_api_result_entity( $site ) ) {
21757
- $prev_plan_id = $this->_site->plan_id;
21758
-
21759
- // Update new site plan id.
21760
- $this->_site->plan_id = $site->plan_id;
21761
-
21762
- $plan = $this->get_plan();
21763
- $subscription = $this->_sync_site_subscription( $this->_license );
21764
-
21765
- // Plan downgraded if plan was changed or subscription was cancelled.
21766
- $plan_downgraded = ( $plan instanceof FS_Plugin_Plan && $prev_plan_id != $plan->id ) ||
21767
- ( is_object( $subscription ) && ! isset( $subscription->error ) && ! $subscription->is_active() );
21768
- } else {
21769
- // handle different error cases.
21770
- $this->handle_license_deactivation_result(
21771
- $site,
21772
- $this->get_text_x_inline( 'Hmm', 'something somebody says when they are thinking about what you have just said.', 'hmm' ) . '...'
21773
- );
21774
- }
21775
-
21776
- if ( ! $plan_downgraded ) {
21777
- return (object) array(
21778
- 'error' => (object) array(
21779
- 'message' => $this->get_text_inline( 'Seems like we are having some temporary issue with your subscription cancellation. Please try again in few minutes.', 'subscription-cancellation-failure-message' )
21780
- )
21781
- );
21782
- }
21783
-
21784
- // Remove previous sticky message about upgrade (if exist).
21785
- $this->_admin_notices->remove_sticky( 'plan_upgraded' );
21786
-
21787
- $this->_admin_notices->add(
21788
- sprintf( $this->get_text_inline( 'Your subscription was successfully cancelled. Your %s plan license will expire in %s.', 'plan-x-downgraded-message' ),
21789
- $plan->title,
21790
- human_time_diff( time(), strtotime( $this->_license->expiration ) )
21791
- )
21792
- );
21793
-
21794
- // Store site updates.
21795
- $this->_store_site();
21796
-
21797
- if ( $deactivate_license &&
21798
- ! FS_Plugin_License::is_valid_id( $site->license_id )
21799
- ) {
21800
- if ( $this->_site->is_localhost() ) {
21801
- $this->_license->activated_local = max( 0, $this->_license->activated_local - 1 );
21802
- } else {
21803
- $this->_license->activated = max( 0, $this->_license->activated - 1 );
21804
- }
21805
-
21806
- // Handle successful license deactivation result.
21807
- $this->handle_license_deactivation_result( $this->_license );
21808
- }
21809
-
21810
- return $site;
21811
- }
21812
-
21813
- /**
21814
- * @author Vova Feldman (@svovaf)
21815
- * @since 1.1.8.1
21816
- *
21817
- * @param bool|string $plan_name
21818
- *
21819
- * @return bool If trial was successfully started.
21820
- */
21821
- function start_trial( $plan_name = false ) {
21822
- $this->_logger->entrance();
21823
-
21824
- // Alias.
21825
- $oops_text = $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...';
21826
-
21827
- if ( $this->is_trial() ) {
21828
- // Already in trial mode.
21829
- $this->_admin_notices->add(
21830
- sprintf( $this->get_text_inline( 'You are already running the %s in a trial mode.', 'in-trial-mode' ), $this->_module_type ),
21831
- $oops_text,
21832
- 'error'
21833
- );
21834
-
21835
- return false;
21836
- }
21837
-
21838
- if ( $this->_site->is_trial_utilized() ) {
21839
- // Trial was already utilized.
21840
- $this->_admin_notices->add(
21841
- $this->get_text_inline( 'You already utilized a trial before.', 'trial-utilized' ),
21842
- $oops_text,
21843
- 'error'
21844
- );
21845
-
21846
- return false;
21847
- }
21848
-
21849
- if ( false !== $plan_name ) {
21850
- $plan = $this->get_plan_by_name( $plan_name );
21851
-
21852
- if ( false === $plan ) {
21853
- // Plan doesn't exist.
21854
- $this->_admin_notices->add(
21855
- sprintf( $this->get_text_inline( 'Plan %s do not exist, therefore, can\'t start a trial.', 'trial-plan-x-not-exist' ), $plan_name ),
21856
- $oops_text,
21857
- 'error'
21858
- );
21859
-
21860
- return false;
21861
- }
21862
-
21863
- if ( ! $plan->has_trial() ) {
21864
- // Plan doesn't exist.
21865
- $this->_admin_notices->add(
21866
- sprintf( $this->get_text_inline( 'Plan %s does not support a trial period.', 'plan-x-no-trial' ), $plan_name ),
21867
- $oops_text,
21868
- 'error'
21869
- );
21870
-
21871
- return false;
21872
- }
21873
- } else {
21874
- if ( ! $this->has_trial_plan() ) {
21875
- // None of the plans have a trial.
21876
- $this->_admin_notices->add(
21877
- sprintf( $this->get_text_inline( 'None of the %s\'s plans supports a trial period.', 'no-trials' ), $this->_module_type ),
21878
- $oops_text,
21879
- 'error'
21880
- );
21881
-
21882
- return false;
21883
- }
21884
-
21885
- $plans_with_trial = FS_Plan_Manager::instance()->get_trial_plans( $this->_plans );
21886
-
21887
- $plan = $plans_with_trial[0];
21888
- }
21889
-
21890
- $api = $this->get_api_site_scope();
21891
- $plan = $api->call( "plans/{$plan->id}/trials.json", 'post' );
21892
-
21893
- if ( ! $this->is_api_result_entity( $plan ) ) {
21894
- // Some API error while trying to start the trial.
21895
- $this->_admin_notices->add(
21896
- sprintf( $this->get_text_inline( 'Unexpected API error. Please contact the %s\'s author with the following error.', 'unexpected-api-error' ), $this->_module_type )
21897
- . ' ' . var_export( $plan, true ),
21898
- $oops_text,
21899
- 'error'
21900
- );
21901
-
21902
- return false;
21903
- }
21904
-
21905
- // Sync license.
21906
- $this->_sync_license();
21907
-
21908
- return $this->is_trial();
21909
- }
21910
-
21911
- /**
21912
- * Cancel site trial.
21913
- *
21914
- * @author Vova Feldman (@svovaf)
21915
- * @since 1.0.9
21916
- *
21917
- * @return object
21918
- *
21919
- * @uses FS_Api
21920
- */
21921
- private function _cancel_trial() {
21922
- $this->_logger->entrance();
21923
-
21924
- if ( ! $this->is_trial() ) {
21925
- return (object) array(
21926
- 'error' => (object) array(
21927
- 'message' => $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' )
21928
- )
21929
- );
21930
- }
21931
-
21932
- $trial_plan = $this->get_trial_plan();
21933
-
21934
- $api = $this->get_api_site_scope();
21935
- $site = $api->call( 'trials.json', 'delete' );
21936
-
21937
- $trial_cancelled = false;
21938
-
21939
- if ( $this->is_api_result_entity( $site ) ) {
21940
- $prev_trial_ends = $this->_site->trial_ends;
21941
-
21942
- if ( $this->is_paid_trial() ) {
21943
- $this->_license->expiration = $site->trial_ends;
21944
- $this->_license->is_cancelled = true;
21945
- $this->_update_site_license( $this->_license );
21946
- $this->_store_licenses();
21947
-
21948
- // Clear subscription reference.
21949
- $this->_sync_site_subscription( null );
21950
- }
21951
-
21952
- // Update site info.
21953
- $this->_site = new FS_Site( $site );
21954
-
21955
- $trial_cancelled = ( $prev_trial_ends != $site->trial_ends );
21956
- } else {
21957
- // @todo handle different error cases.
21958
- }
21959
-
21960
- if ( ! $trial_cancelled ) {
21961
- return (object) array(
21962
- 'error' => (object) array(
21963
- 'message' => $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' )
21964
- )
21965
- );
21966
- }
21967
-
21968
- // Remove previous sticky messages about upgrade or trial (if exist).
21969
- $this->_admin_notices->remove_sticky( array(
21970
- 'trial_started',
21971
- 'trial_promotion',
21972
- 'plan_upgraded',
21973
- ) );
21974
-
21975
- // Store site updates.
21976
- $this->_store_site();
21977
-
21978
- if ( ! $this->is_addon() ||
21979
- ! $this->deactivate_premium_only_addon_without_license( true )
21980
- ) {
21981
- $this->_admin_notices->add(
21982
- sprintf( $this->get_text_inline( 'Your %s free trial was successfully cancelled.', 'trial-cancel-message' ), $trial_plan->title )
21983
- );
21984
- }
21985
-
21986
- return $site;
21987
- }
21988
-
21989
- /**
21990
- * @author Vova Feldman (@svovaf)
21991
- * @since 1.0.6
21992
- *
21993
- * @param bool|number $plugin_id
21994
- *
21995
- * @return bool
21996
- */
21997
- private function _is_addon_id( $plugin_id ) {
21998
- return is_numeric( $plugin_id ) && ( $this->get_id() != $plugin_id );
21999
- }
22000
-
22001
- /**
22002
- * Check if user eligible to download premium version updates.
22003
- *
22004
- * @author Vova Feldman (@svovaf)
22005
- * @since 1.0.6
22006
- *
22007
- * @return bool
22008
- */
22009
- private function _can_download_premium() {
22010
- return $this->has_any_active_valid_license() ||
22011
- ( $this->is_trial() && ! $this->get_trial_plan()->is_free() );
22012
- }
22013
-
22014
- /**
22015
- *
22016
- * @author Vova Feldman (@svovaf)
22017
- * @since 1.0.6
22018
- *
22019
- * @param bool|number $addon_id
22020
- * @param string $type "json" or "zip"
22021
- *
22022
- * @return string
22023
- */
22024
- private function _get_latest_version_endpoint( $addon_id = false, $type = 'json' ) {
22025
-
22026
- $is_addon = $this->_is_addon_id( $addon_id );
22027
-
22028
- $is_premium = null;
22029
- if ( ! $is_addon ) {
22030
- $is_premium = ( $this->is_premium() || $this->_can_download_premium() );
22031
- } else if ( $this->is_addon_activated( $addon_id ) ) {
22032
- $fs_addon = self::get_instance_by_id( $addon_id );
22033
- $is_premium = ( $fs_addon->is_premium() || $fs_addon->_can_download_premium() );
22034
- }
22035
-
22036
- // If add-on, then append add-on ID.
22037
- $endpoint = ( $is_addon ? "/addons/$addon_id" : '' ) .
22038
- '/updates/latest.' . $type;
22039
-
22040
- // If add-on and not yet activated, try to fetch based on server licensing.
22041
- if ( is_bool( $is_premium ) ) {
22042
- $endpoint = add_query_arg( 'is_premium', json_encode( $is_premium ), $endpoint );
22043
- }
22044
-
22045
- if ( $this->has_secret_key() ) {
22046
- $endpoint = add_query_arg( 'type', 'all', $endpoint );
22047
- } else if ( is_object( $this->_site ) && $this->_site->is_beta() ) {
22048
- $endpoint = add_query_arg( 'type', 'beta', $endpoint );
22049
- }
22050
-
22051
- return $endpoint;
22052
- }
22053
-
22054
- /**
22055
- * @author Vova Feldman (@svovaf)
22056
- * @since 1.0.4
22057
- *
22058
- * @param bool|number $addon_id
22059
- * @param bool $flush Since 1.1.7.3
22060
- * @param int $expiration Since 1.2.2.7
22061
- * @param bool|string $newer_than Since 2.2.1
22062
- * @param bool|string $fetch_readme Since 2.2.1
22063
- *
22064
- * @return object|false Plugin latest tag info.
22065
- */
22066
- function _fetch_latest_version(
22067
- $addon_id = false,
22068
- $flush = true,
22069
- $expiration = WP_FS__TIME_24_HOURS_IN_SEC,
22070
- $newer_than = false,
22071
- $fetch_readme = true
22072
- ) {
22073
- $this->_logger->entrance();
22074
-
22075
- if ( $this->is_unresolved_clone() ) {
22076
- return false;
22077
- }
22078
-
22079
- $switch_to_blog_id = null;
22080
-
22081
- /**
22082
- * @since 1.1.7.3 Check for plugin updates from Freemius only if opted-in.
22083
- * @since 1.1.7.4 Also check updates for add-ons.
22084
- */
22085
- if ( ! $this->is_registered() &&
22086
- ! $this->_is_addon_id( $addon_id )
22087
- ) {
22088
- if ( ! is_multisite() ) {
22089
- return false;
22090
- }
22091
-
22092
- $installs_map = $this->get_blog_install_map();
22093
-
22094
- foreach ( $installs_map as $blog_id => $install ) {
22095
- /**
22096
- * @var FS_Site $install
22097
- */
22098
- if ( $install->is_trial() ) {
22099
- $switch_to_blog_id = $blog_id;
22100
- break;
22101
- }
22102
-
22103
- if ( FS_Plugin_License::is_valid_id( $install->license_id ) ) {
22104
- $license = $this->get_license_by_id( $install->license_id );
22105
-
22106
- if ( is_object( $license ) && $license->is_features_enabled() ) {
22107
- $switch_to_blog_id = $blog_id;
22108
- break;
22109
- }
22110
- }
22111
- }
22112
-
22113
- if ( is_null( $switch_to_blog_id ) ) {
22114
- return false;
22115
- }
22116
- }
22117
-
22118
- $current_blog_id = is_numeric( $switch_to_blog_id ) ?
22119
- get_current_blog_id() :
22120
- 0;
22121
-
22122
- if ( is_numeric( $switch_to_blog_id ) ) {
22123
- $this->switch_to_blog( $switch_to_blog_id );
22124
- }
22125
-
22126
- $latest_version_endpoint = $this->_get_latest_version_endpoint( $addon_id, 'json' );
22127
-
22128
- if ( ! empty( $newer_than ) ) {
22129
- $latest_version_endpoint = add_query_arg( 'newer_than', $newer_than, $latest_version_endpoint );
22130
- }
22131
-
22132
- if ( true === $fetch_readme ) {
22133
- $latest_version_endpoint = add_query_arg( 'readme', 'true', $latest_version_endpoint );
22134
- }
22135
-
22136
- $tag = $this->get_api_site_or_plugin_scope()->get(
22137
- $latest_version_endpoint,
22138
- $flush,
22139
- $expiration
22140
- );
22141
-
22142
- if ( is_numeric( $switch_to_blog_id ) ) {
22143
- $this->switch_to_blog( $current_blog_id );
22144
- }
22145
-
22146
- $latest_version = ( is_object( $tag ) && isset( $tag->version ) ) ? $tag->version : 'couldn\'t get';
22147
-
22148
- $this->_logger->departure( 'Latest version ' . $latest_version );
22149
-
22150
- return ( is_object( $tag ) && isset( $tag->version ) ) ? $tag : false;
22151
- }
22152
-
22153
- #----------------------------------------------------------------------------------
22154
- #region Download Plugin
22155
- #----------------------------------------------------------------------------------
22156
-
22157
- /**
22158
- * Download latest plugin version, based on plan.
22159
- *
22160
- * Not like _download_latest(), this will redirect the page
22161
- * to secure download url to prevent dual download (from FS to WP server,
22162
- * and then from WP server to the client / browser).
22163
- *
22164
- * @author Vova Feldman (@svovaf)
22165
- * @since 1.0.9
22166
- *
22167
- * @param bool|number $plugin_id
22168
- *
22169
- * @uses FS_Api
22170
- * @uses wp_redirect()
22171
- */
22172
- private function download_latest_directly( $plugin_id = false ) {
22173
- $this->_logger->entrance();
22174
-
22175
- wp_redirect( $this->get_latest_download_api_url( $plugin_id ) );
22176
- }
22177
-
22178
- /**
22179
- * Get latest plugin FS API download URL.
22180
- *
22181
- * @author Vova Feldman (@svovaf)
22182
- * @since 1.0.9
22183
- *
22184
- * @param bool|number $plugin_id
22185
- *
22186
- * @return string
22187
- */
22188
- private function get_latest_download_api_url( $plugin_id = false ) {
22189
- $this->_logger->entrance();
22190
-
22191
- $download_api_url = $this->get_api_site_scope()->get_signed_url(
22192
- $this->_get_latest_version_endpoint( $plugin_id, 'zip' )
22193
- );
22194
-
22195
- return str_replace( 'http:', 'https:', $download_api_url );
22196
- }
22197
-
22198
- /**
22199
- * Get payment invoice URL.
22200
- *
22201
- * @author Vova Feldman (@svovaf)
22202
- * @since 1.2.0
22203
- *
22204
- * @param bool|number $payment_id
22205
- *
22206
- * @return string
22207
- */
22208
- function _get_invoice_api_url( $payment_id = false ) {
22209
- $this->_logger->entrance();
22210
-
22211
- $url = $this->get_api_user_scope()->get_signed_url(
22212
- "/payments/{$payment_id}/invoice.pdf"
22213
- );
22214
-
22215
- if ( ! fs_starts_with( $url, 'https://' ) ) {
22216
- // Always use HTTPS for invoices.
22217
- $url = 'https' . substr( $url, 4 );
22218
- }
22219
-
22220
- return $url;
22221
- }
22222
-
22223
- /**
22224
- * Get latest plugin download link.
22225
- *
22226
- * @author Vova Feldman (@svovaf)
22227
- * @since 1.0.9
22228
- *
22229
- * @param string $label
22230
- * @param bool|number $plugin_id
22231
- *
22232
- * @return string
22233
- */
22234
- private function get_latest_download_link( $label, $plugin_id = false ) {
22235
- return sprintf(
22236
- '<a target="_blank" rel="noopener" href="%s">%s</a>',
22237
- $this->_get_latest_download_local_url( $plugin_id ),
22238
- $label
22239
- );
22240
- }
22241
-
22242
- /**
22243
- * Get latest plugin download local URL.
22244
- *
22245
- * @author Vova Feldman (@svovaf)
22246
- * @since 1.0.9
22247
- *
22248
- * @param bool|number $plugin_id
22249
- *
22250
- * @return string
22251
- */
22252
- function _get_latest_download_local_url( $plugin_id = false ) {
22253
- // Add timestamp to protect from caching.
22254
- $params = array( 'ts' => WP_FS__SCRIPT_START_TIME );
22255
-
22256
- if ( ! empty( $plugin_id ) ) {
22257
- $params['plugin_id'] = $plugin_id;
22258
- } else if ( $this->is_addon() ) {
22259
- $params['plugin_id'] = $this->get_id();
22260
- }
22261
-
22262
- $fs = $this->is_addon() ?
22263
- $this->get_parent_instance() :
22264
- $this;
22265
-
22266
- return $this->apply_filters( 'download_latest_url', $fs->get_account_url( 'download_latest', $params ) );
22267
- }
22268
-
22269
- #endregion Download Plugin ------------------------------------------------------------------
22270
-
22271
- /**
22272
- * @author Vova Feldman (@svovaf)
22273
- * @since 1.0.4
22274
- *
22275
- * @uses FS_Api
22276
- *
22277
- * @param bool $background Hints the method if it's a background updates check. If false, it means that
22278
- * was initiated by the admin.
22279
- * @param bool|number $plugin_id
22280
- * @param bool $flush Since 1.1.7.3
22281
- * @param int $expiration Since 1.2.2.7
22282
- * @param bool|string $newer_than Since 2.2.1
22283
- */
22284
- private function check_updates(
22285
- $background = false,
22286
- $plugin_id = false,
22287
- $flush = true,
22288
- $expiration = WP_FS__TIME_24_HOURS_IN_SEC,
22289
- $newer_than = false
22290
- ) {
22291
- $this->_logger->entrance();
22292
-
22293
- // Check if there's a newer version for download.
22294
- $new_version = $this->_fetch_newer_version( $plugin_id, $flush, $expiration, $newer_than );
22295
-
22296
- $update = null;
22297
- if ( is_object( $new_version ) ) {
22298
- $update = new FS_Plugin_Tag( $new_version );
22299
-
22300
- if ( ! $background ) {
22301
- $this->_admin_notices->add(
22302
- sprintf(
22303
- /* translators: %s: Numeric version number (e.g. '2.1.9' */
22304
- $this->get_text_inline( 'Version %s was released.', 'version-x-released' ) . ' ' . $this->get_text_inline( 'Please download %s.', 'please-download-x' ),
22305
- $update->version,
22306
- sprintf(
22307
- '<a href="%s" target="_blank" rel="noopener">%s</a>',
22308
- $this->get_account_url( 'download_latest' ),
22309
- sprintf(
22310
- /* translators: %s: plan name (e.g. latest "Professional" version) */
22311
- $this->get_text_inline( 'the latest %s version here', 'latest-x-version' ),
22312
- $this->get_plan_title()
22313
- )
22314
- )
22315
- ),
22316
- $this->get_text_inline( 'New', 'new' ) . '!'
22317
- );
22318
- }
22319
- } else if ( false === $new_version && ! $background ) {
22320
- $this->_admin_notices->add(
22321
- $this->get_text_inline( 'Seems like you got the latest release.', 'you-have-latest' ),
22322
- $this->get_text_inline( 'You are all good!', 'you-are-good' )
22323
- );
22324
- }
22325
-
22326
- $this->_store_update( $update, true, $plugin_id );
22327
- }
22328
-
22329
- /**
22330
- * @author Vova Feldman (@svovaf)
22331
- * @since 1.0.4
22332
- *
22333
- * @param bool $flush Since 1.1.7.3 add 24 hour cache by default.
22334
- *
22335
- * @return FS_Plugin[]
22336
- *
22337
- * @uses FS_Api
22338
- */
22339
- private function sync_addons( $flush = false ) {
22340
- $this->_logger->entrance();
22341
-
22342
- $api = $this->get_api_site_or_plugin_scope();
22343
-
22344
- $path = $this->add_show_pending( '/addons.json?enriched=true&count=50' );
22345
-
22346
- /**
22347
- * @since 1.2.1
22348
- *
22349
- * If there's a cached version of the add-ons and not asking
22350
- * for a flush, just use the currently stored add-ons.
22351
- */
22352
- if ( ! $flush && $api->is_cached( $path ) ) {
22353
- $addons = self::get_all_addons();
22354
-
22355
- return isset( $addons[ $this->_plugin->id ] ) ?
22356
- $addons[ $this->_plugin->id ] :
22357
- array();
22358
- }
22359
-
22360
- $result = $api->get( $path, $flush );
22361
-
22362
- $addons = array();
22363
- if ( $this->is_api_result_object( $result, 'plugins' ) &&
22364
- is_array( $result->plugins )
22365
- ) {
22366
- for ( $i = 0, $len = count( $result->plugins ); $i < $len; $i ++ ) {
22367
- $addons[ $i ] = new FS_Plugin( $result->plugins[ $i ] );
22368
- }
22369
-
22370
- $this->_store_addons( $addons, true );
22371
- }
22372
-
22373
- return $addons;
22374
- }
22375
-
22376
- /**
22377
- * Handle user email update.
22378
- *
22379
- * @author Vova Feldman (@svovaf)
22380
- * @since 1.0.3
22381
- * @uses FS_Api
22382
- *
22383
- * @param string $new_email
22384
- *
22385
- * @return object
22386
- */
22387
- private function update_email( $new_email ) {
22388
- $this->_logger->entrance();
22389
-
22390
- $api = $this->get_api_user_scope();
22391
- $user = $api->call( "?plugin_id={$this->_plugin->id}&fields=id,email,is_verified", 'put', array(
22392
- 'email' => $new_email,
22393
- 'after_email_confirm_url' => $this->_get_admin_page_url(
22394
- 'account',
22395
- array( 'fs_action' => 'sync_user' )
22396
- ),
22397
- ) );
22398
-
22399
- if ( ! isset( $user->error ) ) {
22400
- $this->_user->email = $user->email;
22401
- $this->_user->is_verified = $user->is_verified;
22402
- $this->_store_user();
22403
- } else {
22404
- // handle different error cases.
22405
- }
22406
-
22407
- return $user;
22408
- }
22409
-
22410
- #----------------------------------------------------------------------------------
22411
- #region API Error Handling
22412
- #----------------------------------------------------------------------------------
22413
-
22414
- /**
22415
- * @author Vova Feldman (@svovaf)
22416
- * @since 1.1.1
22417
- *
22418
- * @param mixed $result
22419
- *
22420
- * @return bool Is API result contains an error.
22421
- */
22422
- private function is_api_error( $result ) {
22423
- return FS_Api::is_api_error( $result );
22424
- }
22425
-
22426
- /**
22427
- * Checks if given API result is a non-empty and not an error object.
22428
- *
22429
- * @author Vova Feldman (@svovaf)
22430
- * @since 1.2.1.5
22431
- *
22432
- * @param mixed $result
22433
- * @param string|null $required_property Optional property we want to verify that is set.
22434
- *
22435
- * @return bool
22436
- */
22437
- function is_api_result_object( $result, $required_property = null ) {
22438
- return FS_Api::is_api_result_object( $result, $required_property );
22439
- }
22440
-
22441
- /**
22442
- * Checks if given API result is a non-empty entity object with non-empty ID.
22443
- *
22444
- * @author Vova Feldman (@svovaf)
22445
- * @since 1.2.1.5
22446
- *
22447
- * @param mixed $result
22448
- *
22449
- * @return bool
22450
- */
22451
- private function is_api_result_entity( $result ) {
22452
- return FS_Api::is_api_result_entity( $result );
22453
- }
22454
-
22455
- #endregion
22456
-
22457
- /**
22458
- * Make sure a given argument is an array of a specific type.
22459
- *
22460
- * @author Vova Feldman (@svovaf)
22461
- * @since 1.2.1.5
22462
- *
22463
- * @param mixed $array
22464
- * @param string $class
22465
- *
22466
- * @return bool
22467
- */
22468
- private function is_array_instanceof( $array, $class ) {
22469
- return ( is_array( $array ) && ( empty( $array ) || $array[0] instanceof $class ) );
22470
- }
22471
-
22472
- /**
22473
- * Start install ownership change.
22474
- *
22475
- * @author Vova Feldman (@svovaf)
22476
- * @since 1.1.1
22477
- * @uses FS_Api
22478
- *
22479
- * @param string $candidate_email
22480
- * @param string $transfer_type
22481
- *
22482
- * @return bool Is ownership change successfully initiated.
22483
- */
22484
- private function init_change_owner( $candidate_email, $transfer_type ) {
22485
- $this->_logger->entrance();
22486
-
22487
- $installs_info_by_slug_map = $this->get_parent_and_addons_installs_info();
22488
- $install_ids = array();
22489
-
22490
- foreach ( $installs_info_by_slug_map as $slug => $install_info ) {
22491
- $install = $install_info['install'];
22492
-
22493
- if ( $this->_user->id != $install->user_id ) {
22494
- // Skip add-on installs that are not owned by the parent product's install's owner.
22495
- continue;
22496
- }
22497
-
22498
- $install_ids[ $slug ] = $install->id;
22499
- }
22500
-
22501
- $api = $this->get_api_site_scope();
22502
- $result = $api->call( "/users/{$this->_user->id}.json", 'put', array(
22503
- 'email' => $candidate_email,
22504
- 'transfer_type' => $transfer_type,
22505
- 'install_ids' => implode( ',', array_values( $install_ids ) ),
22506
- 'after_confirm_url' => $this->_get_admin_page_url(
22507
- 'account',
22508
- array( 'fs_action' => 'change_owner' )
22509
- ),
22510
- ) );
22511
-
22512
- return ! $this->is_api_error( $result );
22513
- }
22514
-
22515
- /**
22516
- * Handle install ownership change.
22517
- *
22518
- * @author Vova Feldman (@svovaf)
22519
- * @since 1.1.1
22520
- * @uses FS_Api
22521
- *
22522
- * @return bool Was ownership change successfully complete.
22523
- */
22524
- private function complete_change_owner() {
22525
- $this->_logger->entrance();
22526
-
22527
- $install_ids = fs_request_get( 'install_ids' );
22528
-
22529
- if ( ! empty( $install_ids ) ) {
22530
- $install_ids = explode( ',', $install_ids );
22531
-
22532
- foreach ( $install_ids as $key => $install_id ) {
22533
- if ( ! FS_Site::is_valid_id( $install_id ) ) {
22534
- unset( $install_ids[ $key ] );
22535
- }
22536
- }
22537
- }
22538
-
22539
- if ( ! is_array( $install_ids ) ) {
22540
- $install_ids = array();
22541
- }
22542
-
22543
- $user = new FS_User();
22544
- $user->id = fs_request_get( 'user_id' );
22545
- $user->public_key = fs_request_get( 'user_public_key' );
22546
- $user->secret_key = fs_request_get( 'user_secret_key' );
22547
-
22548
- $prev_user = $this->_user;
22549
- $this->_user = $user;
22550
-
22551
- $result = $this->get_api_user_scope( true )->get(
22552
- "/installs.json?install_ids=" . implode( ',', $install_ids )
22553
- );
22554
-
22555
- $current_blog_sites = self::get_all_sites( $this->get_module_type() );
22556
-
22557
- if ( $this->is_api_result_object( $result, 'installs' ) ) {
22558
- $site_id_slug_map = array();
22559
-
22560
- foreach ( $current_blog_sites as $slug => $site ) {
22561
- $site_id_slug_map[ $site->id ] = $slug;
22562
- }
22563
-
22564
- foreach ( $result->installs as $install ) {
22565
- $site = new FS_Site( $install );
22566
-
22567
- if ( ! isset( $site_id_slug_map[ $install->id ] ) ) {
22568
- continue;
22569
- }
22570
-
22571
- $current_blog_sites[ $site_id_slug_map[ $install->id ] ] = clone $site;
22572
-
22573
- if ( $this->_site->id == $site->id ) {
22574
- $this->_site = $site;
22575
- }
22576
- }
22577
- }
22578
-
22579
- // Validate install's user and given user.
22580
- if ( $user->id != $this->_site->user_id ) {
22581
- $this->_user = $prev_user;
22582
-
22583
- return false;
22584
- }
22585
-
22586
- $this->set_account_option( 'sites', $current_blog_sites, true );
22587
-
22588
- // Fetch new user information.
22589
- $user_result = $this->get_api_user_scope( true )->get();
22590
- $user = new FS_User( $user_result );
22591
- $this->_user = $user;
22592
-
22593
- $this->_set_account( $user, $this->_site );
22594
-
22595
- $remove_user = true;
22596
- $all_modules_sites = self::get_all_modules_sites();
22597
-
22598
- foreach ( $all_modules_sites as $sites_by_module_type ) {
22599
- foreach ( $sites_by_module_type as $sites_by_slug ) {
22600
- foreach ( $sites_by_slug as $site ) {
22601
- if ( $prev_user->id == $site->user_id ) {
22602
- $remove_user = false;
22603
- break;
22604
- }
22605
- }
22606
-
22607
- if ( ! $remove_user ) {
22608
- break;
22609
- }
22610
- }
22611
-
22612
- if ( ! $remove_user ) {
22613
- break;
22614
- }
22615
- }
22616
-
22617
- if ( $remove_user ) {
22618
- $users = self::get_all_users();
22619
-
22620
- if ( isset( $users[ $prev_user->id ] ) ) {
22621
- unset( $users[ $prev_user->id ] );
22622
- } else {
22623
- // If the prev user wasn't found by the key, iterate over the users collection.
22624
- foreach ( $users as $key => $user ) {
22625
- if ( $user->id == $prev_user->id ) {
22626
- unset( $users[ $key ] );
22627
- break;
22628
- }
22629
- }
22630
- }
22631
-
22632
- $this->set_account_option( 'users', $users, true );
22633
- }
22634
-
22635
- return true;
22636
- }
22637
-
22638
- /**
22639
- * Completes ownership change by license.
22640
- *
22641
- * @author Leo Fajardo (@leorw)
22642
- * @since 2.3.2
22643
- *
22644
- * @param number $user_id
22645
- * @param array[string]number $install_ids_by_slug_map
22646
- *
22647
- */
22648
- private function complete_ownership_change_by_license( $user_id, $install_ids_by_slug_map ) {
22649
- $this->_logger->entrance();
22650
-
22651
- $this->sync_user_by_current_install( $user_id );
22652
-
22653
- $result = $this->get_api_user_scope( true )->get(
22654
- "/installs.json?install_ids=" . implode( ',', $install_ids_by_slug_map )
22655
- );
22656
-
22657
- if ( $this->is_api_result_object( $result, 'installs' ) ) {
22658
- $sites = self::get_all_sites( $this->get_module_type() );
22659
- $install_ids_by_slug_map = array_flip( $install_ids_by_slug_map );
22660
-
22661
- foreach ( $result->installs as $install ) {
22662
- $site = new FS_Site( $install );
22663
-
22664
- $sites[ $install_ids_by_slug_map[ $site->id ] ] = clone $site;
22665
- }
22666
-
22667
- $this->set_account_option( 'sites', $sites, true );
22668
- }
22669
- }
22670
-
22671
- /**
22672
- * Handle user name update.
22673
- *
22674
- * @author Vova Feldman (@svovaf)
22675
- * @since 1.0.9
22676
- * @uses FS_Api
22677
- *
22678
- * @return object
22679
- */
22680
- private function update_user_name() {
22681
- $this->_logger->entrance();
22682
- $name = fs_request_get( 'fs_user_name_' . $this->get_unique_affix(), '' );
22683
-
22684
- $api = $this->get_api_user_scope();
22685
- $user = $api->call( "?plugin_id={$this->_plugin->id}&fields=id,first,last", 'put', array(
22686
- 'name' => $name,
22687
- ) );
22688
-
22689
- if ( ! isset( $user->error ) ) {
22690
- $this->_user->first = $user->first;
22691
- $this->_user->last = $user->last;
22692
- $this->_store_user();
22693
- } else {
22694
- // handle different error cases.
22695
-
22696
- }
22697
-
22698
- return $user;
22699
- }
22700
-
22701
- /**
22702
- * Verify user email.
22703
- *
22704
- * @author Vova Feldman (@svovaf)
22705
- * @since 1.0.3
22706
- * @uses FS_Api
22707
- */
22708
- private function verify_email() {
22709
- $this->_handle_account_user_sync();
22710
-
22711
- if ( $this->_user->is_verified() ) {
22712
- return;
22713
- }
22714
-
22715
- $api = $this->get_api_site_scope();
22716
- $result = $api->call( "/users/{$this->_user->id}/verify.json", 'put', array(
22717
- 'after_email_confirm_url' => $this->_get_admin_page_url(
22718
- 'account',
22719
- array( 'fs_action' => 'sync_user' )
22720
- )
22721
- ) );
22722
-
22723
- if ( ! isset( $result->error ) ) {
22724
- $this->_admin_notices->add( sprintf(
22725
- $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' ),
22726
- sprintf( '<a href="mailto:%1$s">%2$s</a>', esc_url( $this->_user->email ), $this->_user->email )
22727
- ) );
22728
- } else {
22729
- // handle different error cases.
22730
-
22731
- }
22732
- }
22733
-
22734
- /**
22735
- * @author Vova Feldman (@svovaf)
22736
- * @since 1.1.2
22737
- *
22738
- * @param array $params
22739
- * @param bool|null $network
22740
- *
22741
- * @return string
22742
- */
22743
- function get_activation_url( $params = array(), $network = null ) {
22744
- if ( $this->is_addon() && $this->has_free_plan() ) {
22745
- /**
22746
- * @author Vova Feldman (@svovaf)
22747
- * @since 1.2.1.7 Add-on's activation is the parent's module activation.
22748
- */
22749
- return $this->get_parent_instance()->get_activation_url( $params );
22750
- }
22751
-
22752
- return $this->apply_filters( 'connect_url', $this->_get_admin_page_url( '', $params, $network ) );
22753
- }
22754
-
22755
- /**
22756
- * @author Vova Feldman (@svovaf)
22757
- * @since 1.2.1.5
22758
- *
22759
- * @param array $params
22760
- *
22761
- * @return string
22762
- */
22763
- function get_reconnect_url( $params = array() ) {
22764
- $params['fs_action'] = 'reset_anonymous_mode';
22765
- $params['fs_unique_affix'] = $this->get_unique_affix();
22766
-
22767
- return $this->get_activation_url( $params );
22768
- }
22769
-
22770
- /**
22771
- * Get the URL of the page that should be loaded after the user connect
22772
- * or skip in the opt-in screen.
22773
- *
22774
- * @author Vova Feldman (@svovaf)
22775
- * @since 1.1.3
22776
- *
22777
- * @param string $filter Filter name.
22778
- * @param array $params Since 1.2.2.7
22779
- * @param bool|null $network
22780
- *
22781
- * @return string
22782
- */
22783
- function get_after_activation_url( $filter, $params = array(), $network = null ) {
22784
- if ( $this->show_opt_in_on_themes_page() &&
22785
- ( fs_request_has( 'pending_activation' ) ||
22786
- // For cases when the first time path is set, even though it's a WP.org theme.
22787
- fs_request_get_bool( $this->get_unique_affix() . '_show_optin' ) )
22788
- ) {
22789
- $first_time_path = '';
22790
- } else {
22791
- $first_time_path = $this->_menu->get_first_time_path(
22792
- fs_is_network_admin() && $this->_is_network_active
22793
- );
22794
- }
22795
-
22796
- if ( $this->_is_network_active &&
22797
- fs_is_network_admin() &&
22798
- ! $this->_menu->has_network_menu() &&
22799
- $this->is_network_registered()
22800
- ) {
22801
- $target_url = $this->get_account_url();
22802
- } else {
22803
- // Default plugin's page.
22804
- $target_url = $this->_get_admin_page_url( '', array(), $network );
22805
- }
22806
-
22807
- return add_query_arg( $params, $this->apply_filters(
22808
- $filter,
22809
- empty( $first_time_path ) ?
22810
- $target_url :
22811
- $first_time_path
22812
- ) );
22813
- }
22814
-
22815
- /**
22816
- * Handle account page updates / edits / actions.
22817
- *
22818
- * @author Vova Feldman (@svovaf)
22819
- * @since 1.0.2
22820
- *
22821
- */
22822
- private function _handle_account_edits() {
22823
- if ( ! $this->is_user_admin() ) {
22824
- return;
22825
- }
22826
-
22827
- $action = fs_get_action();
22828
-
22829
- if ( empty( $action ) ) {
22830
- return;
22831
- }
22832
-
22833
- $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
22834
- $install_id = fs_request_get( 'install_id', '' );
22835
-
22836
- // Alias.
22837
- $oops_text = $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...';
22838
-
22839
- $is_network_action = $this->is_network_level_action();
22840
- $blog_id = $this->is_network_level_site_specific_action();
22841
- $is_parent_plugin_action = ( $plugin_id == $this->get_id() );
22842
-
22843
- if ( is_numeric( $blog_id ) ) {
22844
- $this->switch_to_blog( $blog_id );
22845
- } else {
22846
- $blog_id = '';
22847
- }
22848
-
22849
- switch ( $action ) {
22850
- case 'opt_in':
22851
- check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
22852
-
22853
- if ( $is_parent_plugin_action ) {
22854
- if ( $is_network_action && ! empty( $blog_id ) ) {
22855
- if ( ! $this->is_registered() ) {
22856
- $this->install_with_user(
22857
- $this->get_network_user(),
22858
- false,
22859
- false,
22860
- false,
22861
- false
22862
- );
22863
-
22864
- $this->_admin_notices->add(
22865
- $this->get_text_inline( 'Site successfully opted in.', 'successful-opt-in' ),
22866
- $this->get_text_inline( 'Awesome', 'awesome' )
22867
- );
22868
- }
22869
- }
22870
- }
22871
- break;
22872
-
22873
- case 'toggle_tracking':
22874
- check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
22875
-
22876
- if ( $is_parent_plugin_action ) {
22877
- if ( $is_network_action && ! empty( $blog_id ) ) {
22878
- if ( $this->is_registered() ) {
22879
- if ( $this->is_tracking_prohibited() ) {
22880
- if ( $this->allow_site_tracking() ) {
22881
- $this->_admin_notices->add(
22882
- sprintf( $this->get_text_inline( 'We appreciate your help in making the %s better by letting us track some usage data.', 'opt-out-message-appreciation' ), $this->_module_type ),
22883
- $this->get_text_inline( 'Thank you!', 'thank-you' )
22884
- );
22885
- }
22886
- } else {
22887
- if ( $this->stop_site_tracking() ) {
22888
- $this->_admin_notices->add(
22889
- sprintf(
22890
- $this->get_text_inline( 'We will no longer be sending any usage data of %s on %s to %s.', 'opted-out-successfully' ),
22891
- $this->get_plugin_title(),
22892
- fs_strip_url_protocol( get_site_url( $blog_id ) ),
22893
- sprintf(
22894
- '<a href="%s" target="_blank" rel="noopener">%s</a>',
22895
- 'https://freemius.com',
22896
- 'freemius.com'
22897
- )
22898
- )
22899
- );
22900
- }
22901
- }
22902
- }
22903
- }
22904
- }
22905
-
22906
- break;
22907
-
22908
- case 'delete_account':
22909
- check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
22910
-
22911
- $is_network_deletion = $is_network_action && empty( $blog_id );
22912
-
22913
- if ( $is_parent_plugin_action ) {
22914
- // Delete add-on installs if have any.
22915
- $installed_addons = $this->get_installed_addons();
22916
- foreach ( $installed_addons as $fs_addon ) {
22917
- if ( $is_network_deletion ) {
22918
- $fs_addon->delete_network_account_event();
22919
- } else {
22920
- $fs_addon->delete_account_event();
22921
- }
22922
- }
22923
-
22924
- if ( $is_network_deletion ) {
22925
- $this->delete_network_account_event();
22926
- } else {
22927
- $this->delete_account_event();
22928
- }
22929
-
22930
- // Clear user and site.
22931
- $this->_site = null;
22932
- $this->_user = null;
22933
-
22934
- $this->maybe_set_slug_and_network_menu_exists_flag();
22935
-
22936
- fs_redirect( $this->get_activation_url() );
22937
- } else {
22938
- if ( $this->is_addon_activated( $plugin_id ) ) {
22939
- $fs_addon = self::get_instance_by_id( $plugin_id );
22940
-
22941
- if ( $is_network_deletion ) {
22942
- $fs_addon->delete_network_account_event();
22943
- } else {
22944
- $fs_addon->delete_account_event();
22945
- }
22946
-
22947
- fs_redirect( $this->_get_admin_page_url( 'account' ) );
22948
- }
22949
- }
22950
-
22951
- return;
22952
-
22953
- case 'downgrade_account':
22954
- if ( is_numeric( $blog_id ) ) {
22955
- check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
22956
- } else {
22957
- check_admin_referer( $action );
22958
- }
22959
-
22960
- $switch_to_network_install_blog_after_cancellation = (
22961
- is_numeric( $blog_id ) &&
22962
- $plugin_id == $this->get_id() &&
22963
- ! $this->is_trial()
22964
- );
22965
-
22966
- $result = $this->cancel_subscription_or_trial( $plugin_id );
22967
- if ( $this->is_api_error( $result ) ) {
22968
- $this->_admin_notices->add(
22969
- $result->error->message,
22970
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
22971
- 'error'
22972
- );
22973
- }
22974
-
22975
- if ( $switch_to_network_install_blog_after_cancellation ) {
22976
- $this->switch_to_blog( $this->_storage->network_install_blog_id );
22977
- }
22978
-
22979
- return;
22980
-
22981
- case 'activate_license':
22982
- check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
22983
-
22984
- $fs = $this;
22985
- if ( $plugin_id != $this->get_id() ) {
22986
- $fs = $this->is_addon_activated( $plugin_id ) ?
22987
- self::get_instance_by_id( $plugin_id ) :
22988
- null;
22989
- }
22990
-
22991
- if ( is_object( $fs ) ) {
22992
- $fs->_activate_license();
22993
-
22994
- /**
22995
- * Remove the product ID from `$_REQUEST` so that the syncing of the license for the other products will work properly.
22996
- *
22997
- * @author Leo Fajardo (@leorw)
22998
- * @since 2.4.0
22999
- */
23000
- unset( $_REQUEST['plugin_id'] );
23001
-
23002
- if ( $this->is_bundle_license_auto_activation_enabled() ) {
23003
- $fs->maybe_activate_bundle_license( null, array(), is_numeric( $blog_id ) ? $blog_id : 0 );
23004
- }
23005
- }
23006
-
23007
- return;
23008
-
23009
- case 'deactivate_license':
23010
- check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
23011
-
23012
- if ( $plugin_id == $this->get_id() ) {
23013
- $this->_deactivate_license();
23014
-
23015
- if ( $this->is_only_premium() ) {
23016
- // Clear user and site.
23017
- $this->_site = null;
23018
- $this->_user = null;
23019
-
23020
- if ( ! $is_network_action ) {
23021
- fs_redirect( $this->get_activation_url() );
23022
- } else if ( is_numeric( $blog_id ) ) {
23023
- $this->switch_to_blog( $this->_storage->network_install_blog_id );
23024
- }
23025
- }
23026
- } else {
23027
- if ( $this->is_addon_activated( $plugin_id ) ) {
23028
- $fs_addon = self::get_instance_by_id( $plugin_id );
23029
- $fs_addon->_deactivate_license();
23030
- }
23031
- }
23032
-
23033
- return;
23034
-
23035
- case 'check_updates':
23036
- check_admin_referer( $action );
23037
- $this->check_updates();
23038
-
23039
- return;
23040
-
23041
- case 'change_owner':
23042
- $state = fs_request_get( 'state', 'init' );
23043
- switch ( $state ) {
23044
- case 'init':
23045
- $candidate_email = fs_request_get( 'candidate_email' );
23046
- $transfer_type = fs_request_get( 'transfer_type' );
23047
-
23048
- if ( $this->init_change_owner( $candidate_email, $transfer_type ) ) {
23049
- if ( 'transfer' === $transfer_type ) {
23050
- $this->_admin_notices->add( sprintf( $this->get_text_inline( 'A confirmation email was just sent to %s. The email owner must confirm the update within the next 4 hours.', 'change-owner-request-sent-x-transfer' ), '<b>' . $this->_user->email . '</b>' ) );
23051
- } else {
23052
- $this->_admin_notices->add( sprintf( $this->get_text_inline( 'A confirmation email was just sent to %s. You must confirm the update within the next 4 hours. If you cannot find the email, please check your spam folder.', 'change-owner-request-sent-x' ), '<b>' . $this->_user->email . '</b>' ) );
23053
- }
23054
- }
23055
- break;
23056
- case 'owner_confirmed':
23057
- $candidate_email = fs_request_get( 'candidate_email', '' );
23058
-
23059
- $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>' ) );
23060
- break;
23061
- case 'candidate_confirmed':
23062
- if ( $this->complete_change_owner() ) {
23063
- $this->_admin_notices->add_sticky(
23064
- sprintf( $this->get_text_inline( '%s is the new owner of the account.', 'change-owner-request_candidate-confirmed' ), '<b>' . $this->_user->email . '</b>' ),
23065
- 'ownership_changed',
23066
- $this->get_text_x_inline( 'Congrats', 'as congratulations', 'congrats' ) . '!'
23067
- );
23068
- } else {
23069
- // @todo Handle failed ownership change message.
23070
- }
23071
- break;
23072
- }
23073
-
23074
- return;
23075
-
23076
- case 'update_user_name':
23077
- check_admin_referer( 'update_user_name' );
23078
-
23079
- $result = $this->update_user_name();
23080
-
23081
- if ( isset( $result->error ) ) {
23082
- $this->_admin_notices->add(
23083
- $this->get_text_inline( 'Please provide your full name.', 'name-update-failed-message' ),
23084
- $oops_text,
23085
- 'error'
23086
- );
23087
- } else {
23088
- $this->_admin_notices->add( $this->get_text_inline( 'Your name was successfully updated.', 'name-updated-message' ) );
23089
- }
23090
-
23091
- return;
23092
-
23093
- #region Actions that might be called from external links (e.g. email)
23094
-
23095
- case 'cancel_trial':
23096
- $result = $this->cancel_subscription_or_trial( $plugin_id );
23097
- if ( $this->is_api_error( $result ) ) {
23098
- $this->_admin_notices->add(
23099
- $result->error->message,
23100
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
23101
- 'error'
23102
- );
23103
- }
23104
-
23105
- return;
23106
-
23107
- case 'verify_email':
23108
- $this->verify_email();
23109
-
23110
- return;
23111
-
23112
- case 'sync_user':
23113
- $this->_handle_account_user_sync();
23114
-
23115
- return;
23116
-
23117
- case $this->get_unique_affix() . '_sync_license':
23118
- $this->_sync_license();
23119
-
23120
- return;
23121
-
23122
- case 'download_latest':
23123
- $this->download_latest_directly( $plugin_id );
23124
-
23125
- return;
23126
-
23127
- #endregion
23128
- }
23129
-
23130
- if ( WP_FS__IS_POST_REQUEST ) {
23131
- $properties = array( 'site_secret_key', 'site_id', 'site_public_key' );
23132
- foreach ( $properties as $p ) {
23133
- if ( 'update_' . $p === $action ) {
23134
- check_admin_referer( $action );
23135
-
23136
- $this->_logger->log( $action );
23137
-
23138
- $site_property = substr( $p, strlen( 'site_' ) );
23139
- $site_property_value = fs_request_get( 'fs_' . $p . '_' . $this->get_unique_affix(), '' );
23140
- $this->get_site()->{$site_property} = $site_property_value;
23141
-
23142
- // Store account after modification.
23143
- $this->_store_site();
23144
-
23145
- $this->do_action( 'account_property_edit', 'site', $site_property, $site_property_value );
23146
-
23147
- $this->_admin_notices->add( sprintf(
23148
- /* translators: %s: User's account property (e.g. email address, name) */
23149
- $this->get_text_inline( 'You have successfully updated your %s.', 'x-updated' ),
23150
- '<b>' . str_replace( '_', ' ', $p ) . '</b>'
23151
- ) );
23152
-
23153
- return;
23154
- }
23155
- }
23156
- }
23157
- }
23158
-
23159
- /**
23160
- * Account page resources load.
23161
- *
23162
- * @author Vova Feldman (@svovaf)
23163
- * @since 1.0.6
23164
- */
23165
- function _account_page_load() {
23166
- $this->_logger->entrance();
23167
-
23168
- $this->_logger->info( var_export( $_REQUEST, true ) );
23169
-
23170
- fs_enqueue_local_style( 'fs_account', '/admin/account.css' );
23171
-
23172
- if ( $this->has_addons() ) {
23173
- wp_enqueue_script( 'plugin-install' );
23174
- add_thickbox();
23175
-
23176
- function fs_addons_body_class( $classes ) {
23177
- $classes .= ' plugins-php';
23178
-
23179
- return $classes;
23180
- }
23181
-
23182
- add_filter( 'admin_body_class', 'fs_addons_body_class' );
23183
- }
23184
-
23185
- if ( $this->has_paid_plan() &&
23186
- ! $this->has_any_license() &&
23187
- ! $this->is_sync_executed() &&
23188
- $this->is_tracking_allowed()
23189
- ) {
23190
- /**
23191
- * If no licenses found and no sync job was executed during the last 24 hours,
23192
- * just execute the sync job right away (blocking execution).
23193
- *
23194
- * @since 1.1.7.3
23195
- */
23196
- $this->run_manual_sync();
23197
- }
23198
-
23199
- $this->_handle_account_edits();
23200
-
23201
- if (
23202
- is_object( $this->_license ) &&
23203
- $this->_license->user_id == $this->_user->id &&
23204
- ! $this->is_whitelabeled( true )
23205
- ) {
23206
- $this->_admin_notices->add(
23207
- sprintf(
23208
- $this->get_text_inline( "Is this your client's site? %s if you wish to hide sensitive info like your email, license key, prices, billing address & invoices from the WP Admin.", 'license_not_whitelabeled' ),
23209
- sprintf(
23210
- '<a href="#" class="fs-toggle-whitelabel-mode">%s</a>',
23211
- $this->get_text_inline( 'Click here', 'click-here' )
23212
- )
23213
- ),
23214
- '',
23215
- 'success',
23216
- false,
23217
- 'license_not_whitelabeled'
23218
- );
23219
- }
23220
-
23221
- $this->do_action( 'account_page_load_before_departure' );
23222
- }
23223
-
23224
- /**
23225
- * Renders the "Affiliation" page.
23226
- *
23227
- * @author Leo Fajardo (@leorw)
23228
- * @since 1.2.3
23229
- */
23230
- function _affiliation_page_render() {
23231
- $this->_logger->entrance();
23232
-
23233
- $this->fetch_affiliate_and_terms();
23234
-
23235
- fs_enqueue_local_style( 'fs_affiliation', '/admin/affiliation.css' );
23236
-
23237
- $vars = array( 'id' => $this->_module_id );
23238
- echo $this->apply_filters( "/forms/affiliation.php", fs_get_template( '/forms/affiliation.php', $vars ) );
23239
- }
23240
-
23241
-
23242
- /**
23243
- * Render account page.
23244
- *
23245
- * @author Vova Feldman (@svovaf)
23246
- * @since 1.0.0
23247
- */
23248
- function _account_page_render() {
23249
- $this->_logger->entrance();
23250
-
23251
- $template = 'account.php';
23252
- $vars = array( 'id' => $this->_module_id );
23253
-
23254
- /**
23255
- * Added filter to the template to allow developers wrapping the template
23256
- * in custom HTML (e.g. within a wizard/tabs).
23257
- *
23258
- * @author Vova Feldman (@svovaf)
23259
- * @since 1.2.1.6
23260
- */
23261
- echo $this->apply_filters( "templates/{$template}", fs_get_template( $template, $vars ) );
23262
- }
23263
-
23264
- /**
23265
- * Render account connect page.
23266
- *
23267
- * @author Vova Feldman (@svovaf)
23268
- * @since 1.0.7
23269
- */
23270
- function _connect_page_render() {
23271
- $this->_logger->entrance();
23272
-
23273
- $vars = array( 'id' => $this->_module_id );
23274
-
23275
- /**
23276
- * Added filter to the template to allow developers wrapping the template
23277
- * in custom HTML (e.g. within a wizard/tabs).
23278
- *
23279
- * @author Vova Feldman (@svovaf)
23280
- * @since 1.2.1.6
23281
- */
23282
- echo $this->apply_filters( 'templates/connect.php', fs_get_template( 'connect.php', $vars ) );
23283
- }
23284
-
23285
- /**
23286
- * Load required resources before add-ons page render.
23287
- *
23288
- * @author Vova Feldman (@svovaf)
23289
- * @since 1.0.6
23290
- */
23291
- function _addons_page_load() {
23292
- $this->_logger->entrance();
23293
-
23294
- fs_enqueue_local_style( 'fs_addons', '/admin/add-ons.css' );
23295
-
23296
- wp_enqueue_script( 'plugin-install' );
23297
- add_thickbox();
23298
-
23299
- function fs_addons_body_class( $classes ) {
23300
- $classes .= ' plugins-php';
23301
-
23302
- return $classes;
23303
- }
23304
-
23305
- add_filter( 'admin_body_class', 'fs_addons_body_class' );
23306
-
23307
- if ( ! $this->is_registered() && $this->is_org_repo_compliant() ) {
23308
- $this->_admin_notices->add(
23309
- 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>' ),
23310
- $this->get_text_x_inline( 'Heads up', 'advance notice of something that will need attention.', 'heads-up' ),
23311
- 'update-nag'
23312
- );
23313
- }
23314
- }
23315
-
23316
- /**
23317
- * Render add-ons page.
23318
- *
23319
- * @author Vova Feldman (@svovaf)
23320
- * @since 1.0.6
23321
- */
23322
- function _addons_page_render() {
23323
- $this->_logger->entrance();
23324
-
23325
- $vars = array( 'id' => $this->_module_id );
23326
-
23327
- /**
23328
- * Added filter to the template to allow developers wrapping the template
23329
- * in custom HTML (e.g. within a wizard/tabs).
23330
- *
23331
- * @author Vova Feldman (@svovaf)
23332
- * @since 1.2.1.6
23333
- */
23334
- echo $this->apply_filters( 'templates/add-ons.php', fs_get_template( 'add-ons.php', $vars ) );
23335
- }
23336
-
23337
- /* Pricing & Upgrade
23338
- ------------------------------------------------------------------------------------------------------------------*/
23339
- /**
23340
- * Render pricing page.
23341
- *
23342
- * @author Vova Feldman (@svovaf)
23343
- * @since 1.0.0
23344
- */
23345
- function _pricing_page_render() {
23346
- $this->_logger->entrance();
23347
-
23348
- $vars = array( 'id' => $this->_module_id );
23349
-
23350
- if ( 'true' === fs_request_get( 'checkout', false ) ) {
23351
- echo $this->apply_filters( 'templates/checkout.php', fs_get_template( 'checkout.php', $vars ) );
23352
- } else {
23353
- echo $this->apply_filters( 'templates/pricing.php', fs_get_template( 'pricing.php', $vars ) );
23354
- }
23355
- }
23356
-
23357
- /**
23358
- * @author Leo Fajardo (@leorw)
23359
- * @since 2.3.1
23360
- */
23361
- function _maybe_add_pricing_ajax_handler() {
23362
- if ( ! $this->should_use_external_pricing() ) {
23363
- $this->add_ajax_action( 'pricing_ajax_action', array( &$this, '_fs_pricing_ajax_action_handler' ) );
23364
- }
23365
- }
23366
-
23367
- /**
23368
- * @author Leo Fajardo (@leorw)
23369
- * @since 2.3.1
23370
- */
23371
- function _fs_pricing_ajax_action_handler() {
23372
- $this->check_ajax_referer( 'pricing_ajax_action' );
23373
-
23374
- $result = null;
23375
- $pricing_action = fs_request_get( 'pricing_action' );
23376
-
23377
- switch ( $pricing_action ) {
23378
- case 'fetch_pricing_data':
23379
- $params = array(
23380
- 'is_enriched' => true,
23381
- 'trial' => fs_request_get_bool( 'trial' ),
23382
- 'sandbox' => fs_request_get( 'sandbox' ),
23383
- 's_ctx_type' => fs_request_get( 's_ctx_type' ),
23384
- 's_ctx_id' => fs_request_get( 's_ctx_id' ),
23385
- 's_ctx_ts' => fs_request_get( 's_ctx_ts' ),
23386
- 's_ctx_secure' => fs_request_get( 's_ctx_secure' ),
23387
- );
23388
-
23389
- $bundle_id = $this->get_bundle_id();
23390
- $bundle_public_key = $this->get_bundle_public_key();
23391
-
23392
- $has_bundle_context = ( FS_Plugin::is_valid_id( $bundle_id ) && ! empty( $bundle_public_key ) );
23393
-
23394
- if ( ! $has_bundle_context ) {
23395
- $api = $this->get_api_plugin_scope();
23396
- } else {
23397
- $api = FS_Api::instance(
23398
- $bundle_id,
23399
- 'plugin',
23400
- $bundle_id,
23401
- $bundle_public_key,
23402
- ! $this->is_live(),
23403
- false,
23404
- $this->get_sdk_version()
23405
- );
23406
-
23407
- $params['plugin_id'] = $this->get_id();
23408
- $params['plugin_public_key'] = $this->get_public_key();
23409
- }
23410
-
23411
- $result = $api->get( 'pricing.json?' . http_build_query( $params ) );
23412
- break;
23413
- case 'start_trial':
23414
- $result = $this->opt_in(
23415
- false,
23416
- false,
23417
- false,
23418
- false,
23419
- false,
23420
- fs_request_get( 'plan_id' )
23421
- );
23422
- }
23423
-
23424
- if ( is_object( $result ) && $this->is_api_error( $result ) ) {
23425
- $this->_logger->api_error( $result );
23426
-
23427
- self::shoot_ajax_failure(
23428
- isset( $result->error ) ?
23429
- ( is_string( $result->error ) ? $result->error : $result->error->message ) :
23430
- var_export( $result, true )
23431
- );
23432
- }
23433
-
23434
- $this->shoot_ajax_success( $result );
23435
- }
23436
-
23437
- #----------------------------------------------------------------------------------
23438
- #region Contact Us
23439
- #----------------------------------------------------------------------------------
23440
-
23441
- /**
23442
- * Render contact-us page.
23443
- *
23444
- * @author Vova Feldman (@svovaf)
23445
- * @since 1.0.3
23446
- */
23447
- function _contact_page_render() {
23448
- $this->_logger->entrance();
23449
-
23450
- $vars = array( 'id' => $this->_module_id );
23451
-
23452
- /**
23453
- * Added filter to the template to allow developers wrapping the template
23454
- * in custom HTML (e.g. within a wizard/tabs).
23455
- *
23456
- * @author Vova Feldman (@svovaf)
23457
- * @since 2.1.3
23458
- */
23459
- echo $this->apply_filters( 'templates/contact.php', fs_get_template( 'contact.php', $vars ) );
23460
- }
23461
-
23462
- #endregion ------------------------------------------------------------------------
23463
-
23464
- /**
23465
- * Hide all admin notices to prevent distractions.
23466
- *
23467
- * @author Vova Feldman (@svovaf)
23468
- * @since 1.0.3
23469
- *
23470
- * @uses remove_all_actions()
23471
- */
23472
- private static function _hide_admin_notices() {
23473
- remove_all_actions( 'admin_notices' );
23474
- remove_all_actions( 'network_admin_notices' );
23475
- remove_all_actions( 'all_admin_notices' );
23476
- remove_all_actions( 'user_admin_notices' );
23477
- }
23478
-
23479
- static function _clean_admin_content_section_hook() {
23480
- self::_hide_admin_notices();
23481
-
23482
- // Hide footer.
23483
- echo '<style>#wpfooter { display: none !important; }</style>';
23484
- }
23485
-
23486
- /**
23487
- * Attach to admin_head hook to hide all admin notices.
23488
- *
23489
- * @author Vova Feldman (@svovaf)
23490
- * @since 1.0.3
23491
- */
23492
- static function _clean_admin_content_section() {
23493
- add_action( 'admin_head', 'Freemius::_clean_admin_content_section_hook' );
23494
- }
23495
-
23496
- /* CSS & JavaScript
23497
- ------------------------------------------------------------------------------------------------------------------*/
23498
- /* function _enqueue_script($handle, $src) {
23499
- $url = plugins_url( substr( WP_FS__DIR_JS, strlen( $this->_plugin_dir_path ) ) . '/assets/js/' . $src );
23500
-
23501
- $this->_logger->entrance( 'script = ' . $url );
23502
-
23503
- wp_enqueue_script( $handle, $url );
23504
- }*/
23505
-
23506
- /* SDK
23507
- ------------------------------------------------------------------------------------------------------------------*/
23508
- private $_user_api;
23509
-
23510
- /**
23511
- *
23512
- * @author Vova Feldman (@svovaf)
23513
- * @since 1.0.2
23514
- *
23515
- * @param bool $flush
23516
- *
23517
- * @return FS_Api
23518
- */
23519
- function get_api_user_scope( $flush = false ) {
23520
- if ( ! isset( $this->_user_api ) || $flush ) {
23521
- $this->_user_api = $this->get_api_user_scope_by_user( $this->_user );
23522
- }
23523
-
23524
- return $this->_user_api;
23525
- }
23526
-
23527
- /**
23528
- * @author Vova Feldman (@svovaf)
23529
- * @since 2.0.0
23530
- *
23531
- * @param \FS_User $user
23532
- *
23533
- * @return \FS_Api
23534
- */
23535
- private function get_api_user_scope_by_user( FS_User $user ) {
23536
- return FS_Api::instance(
23537
- $this->_module_id,
23538
- 'user',
23539
- $user->id,
23540
- $user->public_key,
23541
- ! $this->is_live(),
23542
- $user->secret_key,
23543
- $this->get_sdk_version()
23544
- );
23545
- }
23546
-
23547
- /**
23548
- *
23549
- * @author Leo Fajardo (@leorw)
23550
- * @since 2.0.0
23551
- *
23552
- * @param bool $flush
23553
- *
23554
- * @return FS_Api
23555
- */
23556
- private function get_current_or_network_user_api_scope( $flush = false ) {
23557
- if ( ! $this->_is_network_active ||
23558
- ( isset( $this->_user ) && $this->_user instanceof FS_User )
23559
- ) {
23560
- return $this->get_api_user_scope( $flush );
23561
- }
23562
-
23563
- $user = $this->get_current_or_network_user();
23564
-
23565
- $this->_user_api = FS_Api::instance(
23566
- $this->_module_id,
23567
- 'user',
23568
- $user->id,
23569
- $user->public_key,
23570
- ! $this->is_live(),
23571
- $user->secret_key,
23572
- $this->get_sdk_version()
23573
- );
23574
-
23575
- return $this->_user_api;
23576
- }
23577
-
23578
- private $_site_api;
23579
-
23580
- /**
23581
- *
23582
- * @author Vova Feldman (@svovaf)
23583
- * @since 1.0.2
23584
- *
23585
- * @param bool $flush
23586
- *
23587
- * @return FS_Api
23588
- */
23589
- private function get_api_site_scope( $flush = false ) {
23590
- if ( ! isset( $this->_site_api ) || $flush ) {
23591
- $this->_site_api = FS_Api::instance(
23592
- $this->_module_id,
23593
- 'install',
23594
- $this->_site->id,
23595
- $this->_site->public_key,
23596
- ! $this->is_live(),
23597
- $this->_site->secret_key,
23598
- $this->get_sdk_version(),
23599
- get_site_url()
23600
- );
23601
- }
23602
-
23603
- return $this->_site_api;
23604
- }
23605
-
23606
- /**
23607
- * @author Leo Fajardo (@leorw)
23608
- * @since 2.5.0
23609
- *
23610
- * @param string $path
23611
- * @param string $method
23612
- * @param array $params
23613
- * @param bool $flush_instance
23614
- *
23615
- * @return array|mixed|string|void
23616
- * @throws Freemius_Exception
23617
- */
23618
- private function api_site_call( $path, $method = 'GET', $params = array(), $flush_instance = false ) {
23619
- $result = $this->get_api_site_scope( $flush_instance )->call( $path, $method, $params );
23620
-
23621
- /**
23622
- * Checks if the local install's URL is different from the remote install's URL, update the local install if necessary, and then run the clone handler if the install's URL is different from the URL of the site.
23623
- *
23624
- * @author Leo Fajardo (@leorw)
23625
- * @since 2.5.0
23626
- */
23627
- if (
23628
- $this->is_registered() &&
23629
- FS_Api::is_api_result_entity( $result ) &&
23630
- isset( $result->url )
23631
- ) {
23632
- $stored_local_url = trailingslashit( $this->_site->url );
23633
- $stored_remote_url = trailingslashit( $result->url );
23634
-
23635
- if ( $stored_local_url !== $stored_remote_url ) {
23636
- $this->_site->url = $result->url;
23637
- $this->_store_site();
23638
- }
23639
-
23640
- if ( fs_strip_url_protocol( $stored_remote_url ) !== fs_strip_url_protocol( trailingslashit( get_site_url() ) ) ) {
23641
- FS_Clone_Manager::instance()->maybe_run_clone_resolution();
23642
- }
23643
- }
23644
-
23645
- return $result;
23646
- }
23647
-
23648
- private $_plugin_api;
23649
-
23650
- /**
23651
- * Get plugin public API scope.
23652
- *
23653
- * @author Vova Feldman (@svovaf)
23654
- * @since 1.0.7
23655
- *
23656
- * @return FS_Api
23657
- */
23658
- function get_api_plugin_scope() {
23659
- if ( ! isset( $this->_plugin_api ) ) {
23660
- $this->_plugin_api = FS_Api::instance(
23661
- $this->_module_id,
23662
- 'plugin',
23663
- $this->_plugin->id,
23664
- $this->_plugin->public_key,
23665
- ! $this->is_live(),
23666
- false,
23667
- $this->get_sdk_version()
23668
- );
23669
- }
23670
-
23671
- return $this->_plugin_api;
23672
- }
23673
-
23674
- /**
23675
- * Get bundle public API scope.
23676
- *
23677
- * @author Vova Feldman (@svovaf)
23678
- * @since 2.3.1
23679
- *
23680
- * @return FS_Api
23681
- */
23682
- function get_api_bundle_scope() {
23683
- return FS_Api::instance(
23684
- $this->get_bundle_id(),
23685
- 'plugin',
23686
- $this->get_bundle_id(),
23687
- $this->get_bundle_public_key(),
23688
- ! $this->is_live(),
23689
- false,
23690
- $this->get_sdk_version()
23691
- );
23692
- }
23693
-
23694
- /**
23695
- * Get site API scope object (fallback to public plugin scope when not registered).
23696
- *
23697
- * @author Vova Feldman (@svovaf)
23698
- * @since 1.0.7
23699
- *
23700
- * @return FS_Api
23701
- */
23702
- function get_api_site_or_plugin_scope() {
23703
- return $this->is_registered() ?
23704
- $this->get_api_site_scope() :
23705
- $this->get_api_plugin_scope();
23706
- }
23707
-
23708
- /**
23709
- * @author Leo Fajardo (@leorw)
23710
- * @since 2.2.3.1
23711
- *
23712
- * @param object $result
23713
- */
23714
- private function maybe_modify_api_curl_error_message( $result ) {
23715
- if (
23716
- 'cUrlMissing' !== $result->error->type &&
23717
- ( 'CurlException' !== $result->error->type || CURLE_COULDNT_CONNECT != $result->error->code ) &&
23718
- ( 'HttpRequestFailed' !== $result->error->type || false === strpos( $result->error->message, 'cURL error ' . CURLE_COULDNT_CONNECT ) )
23719
- ) {
23720
- return;
23721
- }
23722
-
23723
- $result->error->message = $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' ) .
23724
- ' ' .
23725
- $this->esc_html_inline(
23726
- sprintf(
23727
- 'Please contact your hosting provider and ask them to whitelist %s for external connection.',
23728
- implode(
23729
- ', ',
23730
- $this->apply_filters( 'api_domains', array(
23731
- 'api.freemius.com',
23732
- 'wp.freemius.com'
23733
- ) )
23734
- )
23735
- ),
23736
- 'connectivity-whitelist'
23737
- ) .
23738
- ' ' .
23739
- sprintf(
23740
- $this->esc_html_inline( 'Once you are done, deactivate the %s and activate it again.', 'connectivity-reactivate-module' ),
23741
- $this->get_module_type()
23742
- );
23743
- }
23744
-
23745
- /**
23746
- * Show trial promotional notice (if any trial exist).
23747
- *
23748
- * @author Vova Feldman (@svovaf)
23749
- * @since 1.0.9
23750
- *
23751
- * @param FS_Plugin_Plan[] $plans
23752
- */
23753
- function _check_for_trial_plans( $plans ) {
23754
- /**
23755
- * For some reason core's do_action() flattens arrays when it has a single object item. Therefore, we need to restructure the array as expected.
23756
- *
23757
- * @author Vova Feldman (@svovaf)
23758
- * @since 2.1.2
23759
- */
23760
- if ( ! is_array( $plans ) && is_object( $plans ) ) {
23761
- $plans = array( $plans );
23762
- }
23763
-
23764
- if ( ! $this->is_array_instanceof( $plans, 'FS_Plugin_Plan' ) ) {
23765
- $plans = array();
23766
- }
23767
-
23768
- $this->_storage->has_trial_plan = FS_Plan_Manager::instance()->has_trial_plan( $plans );
23769
- }
23770
-
23771
- /**
23772
- * During trial promotion the "upgrade" submenu item turns to
23773
- * "start trial" to encourage the trial. Since we want to keep
23774
- * the same menu item handler and there's no robust way to
23775
- * add new arguments to the menu item link's querystring,
23776
- * use JavaScript to find the menu item and update the href of
23777
- * the link.
23778
- *
23779
- * @author Vova Feldman (@svovaf)
23780
- * @since 1.2.1.5
23781
- */
23782
- function _fix_start_trial_menu_item_url() {
23783
- $template_args = array( 'id' => $this->_module_id );
23784
- fs_require_template( 'add-trial-to-pricing.php', $template_args );
23785
- }
23786
-
23787
- /**
23788
- * Check if module is currently in a trial promotion mode.
23789
- *
23790
- * @author Vova Feldman (@svovaf)
23791
- * @since 1.2.2.7
23792
- *
23793
- * @return bool
23794
- */
23795
- function is_in_trial_promotion() {
23796
- return $this->_admin_notices->has_sticky( 'trial_promotion' );
23797
- }
23798
-
23799
- /**
23800
- * Show trial promotional notice (if any trial exist).
23801
- *
23802
- * @author Vova Feldman (@svovaf)
23803
- * @since 1.0.9
23804
- *
23805
- * @return bool If trial notice added.
23806
- */
23807
- function _add_trial_notice() {
23808
- if ( ! $this->is_user_admin() ) {
23809
- return false;
23810
- }
23811
-
23812
- if ( ! $this->is_user_in_admin() ) {
23813
- return false;
23814
- }
23815
-
23816
- if ( $this->_is_network_active ) {
23817
- if ( fs_is_network_admin() ) {
23818
- // Network level trial is disabled at the moment.
23819
- return false;
23820
- }
23821
-
23822
- if ( ! $this->is_delegated_connection() ) {
23823
- // Only delegated sites should support trials.
23824
- return false;
23825
- }
23826
- }
23827
-
23828
- // Check if trial message is already shown.
23829
- if ( $this->is_in_trial_promotion() ) {
23830
- add_action( 'admin_footer', array( &$this, '_fix_start_trial_menu_item_url' ) );
23831
-
23832
- $this->_menu->add_counter_to_menu_item( 1, 'fs-trial' );
23833
-
23834
- return false;
23835
- }
23836
-
23837
- if ( $this->is_premium() && ! WP_FS__DEV_MODE ) {
23838
- // Don't show trial if running the premium code, unless running in DEV mode.
23839
- return false;
23840
- }
23841
-
23842
- if ( ! $this->has_trial_plan() ) {
23843
- // No plans with trial.
23844
- return false;
23845
- }
23846
-
23847
- if ( ! $this->apply_filters( 'show_trial', true ) ) {
23848
- // Developer explicitly asked not to show the trial promo.
23849
- return false;
23850
- }
23851
-
23852
- if ( $this->is_registered() ) {
23853
- // Check if trial already utilized.
23854
- if ( $this->_site->is_trial_utilized() ) {
23855
- return false;
23856
- }
23857
-
23858
- if ( $this->is_paying_or_trial() ) {
23859
- // Don't show trial if paying or already in trial.
23860
- return false;
23861
- }
23862
- }
23863
-
23864
- if ( $this->is_activation_mode() || $this->is_pending_activation() ) {
23865
- // If not yet opted-in/skipped, or pending activation, don't show trial.
23866
- return false;
23867
- }
23868
-
23869
- $last_time_trial_promotion_shown = $this->_storage->get( 'trial_promotion_shown', false );
23870
- $was_promotion_shown_before = ( false !== $last_time_trial_promotion_shown );
23871
-
23872
- // Show promotion if never shown before and 24 hours after initial activation with FS.
23873
- if ( ! $was_promotion_shown_before &&
23874
- $this->_storage->install_timestamp > ( time() - $this->apply_filters( 'show_first_trial_after_n_sec', WP_FS__TIME_24_HOURS_IN_SEC ) )
23875
- ) {
23876
- return false;
23877
- }
23878
-
23879
- // OR if promotion was shown before, try showing it every 30 days.
23880
- if ( $was_promotion_shown_before &&
23881
- $this->apply_filters( 'reshow_trial_after_every_n_sec', 30 * WP_FS__TIME_24_HOURS_IN_SEC ) > time() - $last_time_trial_promotion_shown
23882
- ) {
23883
- return false;
23884
- }
23885
-
23886
- $trial_period = $this->_trial_days;
23887
- $require_payment = $this->_is_trial_require_payment;
23888
- $trial_url = $this->get_trial_url();
23889
- $plans_string = strtolower( $this->get_text_inline( 'Awesome', 'awesome' ) );
23890
-
23891
- if ( $this->is_registered() ) {
23892
- // If opted-in, override trial with up to date data from API.
23893
- $trial_plans = FS_Plan_Manager::instance()->get_trial_plans( $this->_plans );
23894
- $trial_plans_count = count( $trial_plans );
23895
-
23896
- if ( 0 === $trial_plans_count ) {
23897
- // If there's no plans with a trial just exit.
23898
- return false;
23899
- }
23900
-
23901
- /**
23902
- * @var FS_Plugin_Plan $paid_plan
23903
- */
23904
- $paid_plan = $trial_plans[0];
23905
- $require_payment = $paid_plan->is_require_subscription;
23906
- $trial_period = $paid_plan->trial_period;
23907
-
23908
- $total_paid_plans = count( $this->_plans ) - ( FS_Plan_Manager::instance()->has_free_plan( $this->_plans ) ? 1 : 0 );
23909
-
23910
- if ( $total_paid_plans !== $trial_plans_count ) {
23911
- // Not all paid plans have a trial - generate a string of those that have it.
23912
- for ( $i = 0; $i < $trial_plans_count; $i ++ ) {
23913
- $plans_string .= sprintf(
23914
- ' <a href="%s">%s</a>',
23915
- $trial_url,
23916
- $trial_plans[ $i ]->title
23917
- );
23918
-
23919
- if ( $i < $trial_plans_count - 2 ) {
23920
- $plans_string .= ', ';
23921
- } else if ( $i == $trial_plans_count - 2 ) {
23922
- $plans_string .= ' and ';
23923
- }
23924
- }
23925
- }
23926
- }
23927
-
23928
- $message = sprintf(
23929
- $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' ),
23930
- sprintf( '<b>%s</b>', $this->get_plugin_name() ),
23931
- $plans_string,
23932
- $trial_period
23933
- );
23934
-
23935
- // "No Credit-Card Required" or "No Commitment for N Days".
23936
- $cc_string = $require_payment ?
23937
- sprintf( $this->get_text_inline( 'No commitment for %s days - cancel anytime!', 'no-commitment-for-x-days' ), $trial_period ) :
23938
- $this->get_text_inline( 'No credit card required', 'no-cc-required' ) . '!';
23939
-
23940
-
23941
- // Start trial button.
23942
- $button = ' ' . sprintf(
23943
- '<a style="margin-left: 10px; vertical-align: super;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
23944
- $trial_url,
23945
- $this->get_text_x_inline( 'Start free trial', 'call to action', 'start-free-trial' )
23946
- );
23947
-
23948
- $this->_admin_notices->add_sticky(
23949
- $this->apply_filters( 'trial_promotion_message', "{$message} {$cc_string} {$button}" ),
23950
- 'trial_promotion',
23951
- '',
23952
- 'promotion'
23953
- );
23954
-
23955
- $this->_storage->trial_promotion_shown = WP_FS__SCRIPT_START_TIME;
23956
-
23957
- return true;
23958
- }
23959
-
23960
- /**
23961
- * Lets users/customers know that the product has an affiliate program.
23962
- *
23963
- * @author Leo Fajardo (@leorw)
23964
- * @since 1.2.2.11
23965
- *
23966
- * @return bool Returns true if the notice has been added.
23967
- */
23968
- function _add_affiliate_program_notice() {
23969
- if ( ! $this->is_user_admin() ) {
23970
- return false;
23971
- }
23972
-
23973
- if ( ! $this->is_user_in_admin() ) {
23974
- return false;
23975
- }
23976
-
23977
- // Check if the notice is already shown.
23978
- if ( $this->_admin_notices->has_sticky( 'affiliate_program' ) ) {
23979
- return false;
23980
- }
23981
-
23982
- if (
23983
- // Product has no affiliate program.
23984
- ! $this->has_affiliate_program() ||
23985
- // User has applied for an affiliate account.
23986
- ! empty( $this->_storage->affiliate_application_data )
23987
- ) {
23988
- return false;
23989
- }
23990
-
23991
- if ( ! $this->apply_filters( 'show_affiliate_program_notice', true ) ) {
23992
- // Developer explicitly asked not to show the notice about the affiliate program.
23993
- return false;
23994
- }
23995
-
23996
- if ( $this->is_activation_mode() || $this->is_pending_activation() ) {
23997
- // If not yet opted in/skipped, or pending activation, don't show the notice.
23998
- return false;
23999
- }
24000
-
24001
- $last_time_notice_was_shown = $this->_storage->get( 'affiliate_program_notice_shown', false );
24002
- $was_notice_shown_before = ( false !== $last_time_notice_was_shown );
24003
-
24004
- /**
24005
- * Do not show the notice if it was already shown before or less than 30 days have passed since the initial
24006
- * activation with FS.
24007
- */
24008
- if ( $was_notice_shown_before ||
24009
- $this->_storage->install_timestamp > ( time() - ( WP_FS__TIME_24_HOURS_IN_SEC * 30 ) )
24010
- ) {
24011
- return false;
24012
- }
24013
-
24014
- if ( ! $this->is_paying() &&
24015
- FS_Plugin::AFFILIATE_MODERATION_CUSTOMERS == $this->_plugin->affiliate_moderation
24016
- ) {
24017
- // If the user is not a customer and the affiliate program is only for customers, don't show the notice.
24018
- return false;
24019
- }
24020
-
24021
- $message = sprintf(
24022
- $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' ),
24023
- sprintf( '<strong>%s</strong>', $this->get_plugin_name() ),
24024
- $this->get_module_label( true )
24025
- );
24026
-
24027
- // HTML code for the "Learn more..." button.
24028
- $button = ' ' . sprintf(
24029
- '<a style="display: block; margin-top: 10px;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
24030
- $this->_get_admin_page_url( 'affiliation' ),
24031
- $this->get_text_inline( 'Learn more', 'learn-more' ) . '...'
24032
- );
24033
-
24034
- $this->_admin_notices->add_sticky(
24035
- $this->apply_filters( 'affiliate_program_notice', "{$message} {$button}" ),
24036
- 'affiliate_program',
24037
- '',
24038
- 'promotion'
24039
- );
24040
-
24041
- $this->_storage->affiliate_program_notice_shown = WP_FS__SCRIPT_START_TIME;
24042
-
24043
- return true;
24044
- }
24045
-
24046
- /**
24047
- * @author Vova Feldman (@svovaf)
24048
- * @since 1.2.1.5
24049
- */
24050
- function _enqueue_common_css() {
24051
- if ( $this->has_paid_plan() && ! $this->is_paying() ) {
24052
- // Add basic CSS for admin-notices and menu-item colors.
24053
- fs_enqueue_local_style( 'fs_common', '/admin/common.css' );
24054
- }
24055
- }
24056
-
24057
- /**
24058
- * @author Leo Fajardo (@leorw)
24059
- * @since 1.2.2
24060
- */
24061
- function _show_theme_activation_optin_dialog() {
24062
- fs_enqueue_local_style( 'fs_connect', '/admin/connect.css' );
24063
-
24064
- add_action( 'admin_footer', array( &$this, '_add_fs_theme_activation_dialog' ) );
24065
- }
24066
-
24067
- /**
24068
- * @author Leo Fajardo (@leorw)
24069
- * @since 1.2.2
24070
- */
24071
- function _add_fs_theme_activation_dialog() {
24072
- global $pagenow;
24073
-
24074
- if ( 'themes.php' !== $pagenow ) {
24075
- return;
24076
- }
24077
-
24078
- $vars = array( 'id' => $this->_module_id );
24079
- fs_require_once_template( 'connect.php', $vars );
24080
- }
24081
-
24082
- /* Action Links
24083
- ------------------------------------------------------------------------------------------------------------------*/
24084
- private $_action_links_hooked = false;
24085
- private $_action_links = array();
24086
-
24087
- /**
24088
- * Hook to plugin action links filter.
24089
- *
24090
- * @author Vova Feldman (@svovaf)
24091
- * @since 1.0.0
24092
- */
24093
- private function hook_plugin_action_links() {
24094
- $this->_logger->entrance();
24095
-
24096
- $this->_action_links_hooked = true;
24097
-
24098
- $this->_logger->log( 'Adding action links hooks.' );
24099
-
24100
- // Add action link to settings page.
24101
- add_filter( 'plugin_action_links_' . $this->_plugin_basename, array(
24102
- &$this,
24103
- '_modify_plugin_action_links_hook'
24104
- ), WP_FS__DEFAULT_PRIORITY, 2 );
24105
- add_filter( 'network_admin_plugin_action_links_' . $this->_plugin_basename, array(
24106
- &$this,
24107
- '_modify_plugin_action_links_hook'
24108
- ), WP_FS__DEFAULT_PRIORITY, 2 );
24109
- }
24110
-
24111
- /**
24112
- * Add plugin action link.
24113
- *
24114
- * @author Vova Feldman (@svovaf)
24115
- * @since 1.0.0
24116
- *
24117
- * @param $label
24118
- * @param $url
24119
- * @param bool $external
24120
- * @param int $priority
24121
- * @param bool $key
24122
- */
24123
- function add_plugin_action_link( $label, $url, $external = false, $priority = WP_FS__DEFAULT_PRIORITY, $key = false ) {
24124
- $this->_logger->entrance();
24125
-
24126
- if ( ! isset( $this->_action_links[ $priority ] ) ) {
24127
- $this->_action_links[ $priority ] = array();
24128
- }
24129
-
24130
- if ( false === $key ) {
24131
- $key = preg_replace( "/[^A-Za-z0-9 ]/", '', strtolower( $label ) );
24132
- }
24133
-
24134
- $this->_action_links[ $priority ][] = array(
24135
- 'label' => $label,
24136
- 'href' => $url,
24137
- 'key' => $key,
24138
- 'external' => $external
24139
- );
24140
- }
24141
-
24142
- /**
24143
- * Adds Upgrade and Add-Ons links to the main Plugins page link actions collection.
24144
- *
24145
- * @author Vova Feldman (@svovaf)
24146
- * @since 1.0.0
24147
- */
24148
- function _add_upgrade_action_link() {
24149
- $this->_logger->entrance();
24150
-
24151
- $is_activation_mode = $this->is_activation_mode();
24152
-
24153
- $add_action_links = $this->should_add_submenu_or_action_links( $is_activation_mode );
24154
-
24155
- /**
24156
- * The following logic is based on the logic in `add_submenu_items()` method that decides when the "Upgrade"
24157
- * and "Add-Ons" menus should be added.
24158
- *
24159
- * @author Leo Fajardo (@leorw)
24160
- * @since 2.3.0
24161
- */
24162
- $add_upgrade_link = (
24163
- $add_action_links ||
24164
- ( $is_activation_mode && $this->is_only_premium() )
24165
- ) && ! WP_FS__DEMO_MODE && ( ! $this->is_whitelabeled() );
24166
-
24167
- $add_addons_link = ( $add_action_links && $this->has_addons() );
24168
-
24169
- if ( ! $add_upgrade_link && ! $add_addons_link ) {
24170
- return;
24171
- }
24172
-
24173
- if (
24174
- $add_upgrade_link &&
24175
- $this->is_pricing_page_visible() &&
24176
- $this->is_submenu_item_visible( 'pricing' )
24177
- ) {
24178
- $this->add_plugin_action_link(
24179
- $this->get_text_inline( 'Upgrade', 'upgrade' ),
24180
- $this->get_upgrade_url(),
24181
- false,
24182
- 7,
24183
- 'upgrade'
24184
- );
24185
- }
24186
-
24187
- if (
24188
- $add_addons_link &&
24189
- $this->has_addons() &&
24190
- $this->is_submenu_item_visible( 'addons' )
24191
- ) {
24192
- $this->add_plugin_action_link(
24193
- $this->get_text_inline( 'Add-Ons', 'add-ons' ),
24194
- $this->_get_admin_page_url( 'addons' ),
24195
- false,
24196
- 9,
24197
- 'addons'
24198
- );
24199
- }
24200
- }
24201
-
24202
- /**
24203
- * Adds "Activate License" or "Change License" link to the main Plugins page link actions collection.
24204
- *
24205
- * @author Leo Fajardo (@leorw)
24206
- * @since 1.1.9
24207
- */
24208
- function _add_license_action_link() {
24209
- $this->_logger->entrance();
24210
-
24211
- if ( ! self::is_ajax() ) {
24212
- // Inject license activation dialog UI and client side code.
24213
- add_action( 'admin_footer', array( &$this, '_add_license_activation_dialog_box' ) );
24214
- }
24215
-
24216
- $link_text = $this->is_free_plan() ?
24217
- $this->get_text_inline( 'Activate License', 'activate-license' ) :
24218
- $this->get_text_inline( 'Change License', 'change-license' );
24219
-
24220
- $this->add_plugin_action_link(
24221
- $link_text,
24222
- '#',
24223
- false,
24224
- 11,
24225
- ( 'activate-license ' . $this->get_unique_affix() )
24226
- );
24227
- }
24228
-
24229
- /**
24230
- * @author Leo Fajardo (@leorw)
24231
- * @since 2.0.2
24232
- */
24233
- function _add_premium_version_upgrade_selection_action() {
24234
- $this->_logger->entrance();
24235
-
24236
- if ( ! self::is_ajax() ) {
24237
- add_action( 'admin_footer', array( &$this, '_add_premium_version_upgrade_selection_dialog_box' ) );
24238
- }
24239
- }
24240
-
24241
- /**
24242
- * Adds "Opt In" or "Opt Out" link to the main "Plugins" page link actions collection.
24243
- *
24244
- * @author Leo Fajardo (@leorw)
24245
- * @since 1.2.1.5
24246
- */
24247
- function _add_tracking_links() {
24248
- if ( ! current_user_can( 'manage_options' ) ) {
24249
- return;
24250
- }
24251
-
24252
- $this->_logger->entrance();
24253
-
24254
- /**
24255
- * @author Vova Feldman (@svovaf)
24256
- * @since 2.3.2 Allow opting out from usage-tracking for paid products too by giving the appropriate warning letting the user know the automatic updates mechanism cannot function without an ongoing connection to the licensing and updates engine.
24257
- */
24258
- /*if ( $this->is_premium() ) {
24259
- // Don't add opt-in/out for premium code base.
24260
- return;
24261
- }*/
24262
-
24263
- if ( $this->is_only_premium() && $this->is_free_plan() ) {
24264
- // Don't add tracking links for premium-only products that were opted-in by relation (add-on or a parent product) before activating any license.
24265
- return;
24266
- }
24267
-
24268
- if (
24269
- $this->is_addon() &&
24270
- ! $this->is_only_premium()
24271
- ) {
24272
- $parent = $this->get_parent_instance();
24273
-
24274
- if ( is_object( $parent ) && $parent->is_anonymous() ) {
24275
- return;
24276
- }
24277
- }
24278
-
24279
- if ( fs_is_network_admin() ) {
24280
- if ( ! $this->_is_network_active ) {
24281
- // Don't add tracking links when browsing the network WP Admin and the plugin is not network active.
24282
- return;
24283
- } else if ( $this->is_network_delegated_connection() ) {
24284
- // Don't add tracking links when browsing the network WP Admin and the activation has been delegated to site admins.
24285
- return;
24286
- }
24287
- } else {
24288
- if ( $this->_is_network_active && ! $this->is_delegated_connection() ) {
24289
- // Don't add tracking links when browsing the sub-site WP Admin, the plugin is network active, and the connection was not delegated.
24290
- return;
24291
- }
24292
- }
24293
-
24294
- if ( fs_request_is_action_secure( $this->get_unique_affix() . '_reconnect' ) ) {
24295
- if ( ! $this->is_registered() && $this->is_anonymous() ) {
24296
- $this->connect_again();
24297
-
24298
- return;
24299
- }
24300
- }
24301
-
24302
- if ( ( $this->is_plugin() && ! self::is_plugins_page() ) ||
24303
- ( $this->is_theme() && ! self::is_themes_page() )
24304
- ) {
24305
- // Only show tracking links on the plugins and themes pages.
24306
- return;
24307
- }
24308
-
24309
- if (
24310
- $this->is_activation_mode() &&
24311
- $this->is_premium() &&
24312
- ! $this->is_registered()
24313
- ) {
24314
- // If not yet registered and running the premium code base, a license activation link will already be shown.
24315
- return;
24316
- }
24317
-
24318
- if ( $this->is_registered() && $this->is_tracking_allowed() ) {
24319
- if ( ! $this->is_premium() && ! $this->is_enable_anonymous() ) {
24320
- // If opted in and tracking is allowed, don't allow to opt out if not premium and anonymous mode is disabled.
24321
- return;
24322
- }
24323
- }
24324
-
24325
- if ( $this->add_ajax_action( 'stop_tracking', array( &$this, '_stop_tracking_callback' ) ) ) {
24326
- return;
24327
- }
24328
-
24329
- if ( $this->add_ajax_action( 'allow_tracking', array( &$this, '_allow_tracking_callback' ) ) ) {
24330
- return;
24331
- }
24332
-
24333
- if ( $this->add_ajax_action( 'update_tracking_permission', array( &$this, '_update_tracking_permission_callback' ) ) ) {
24334
- return;
24335
- }
24336
-
24337
- $link_text_id = '';
24338
- $url = '#';
24339
-
24340
- if ( $this->is_registered() ) {
24341
- if ( $this->is_tracking_allowed() ) {
24342
- $link_text_id = $this->get_text_inline( 'Opt Out', 'opt-out' );
24343
- } else {
24344
- $link_text_id = $this->get_text_inline( 'Opt In', 'opt-in' );
24345
- }
24346
- } else if ( $this->is_anonymous() || $this->is_activation_mode() ) {
24347
- /**
24348
- * Show opt-in link only if skipped or in activation mode.
24349
- */
24350
- $link_text_id = $this->get_text_inline( 'Opt In', 'opt-in' );
24351
-
24352
- $params = ! $this->is_anonymous() ?
24353
- array() :
24354
- array(
24355
- 'nonce' => wp_create_nonce( $this->get_unique_affix() . '_reconnect' ),
24356
- 'fs_action' => ( $this->get_unique_affix() . '_reconnect' ),
24357
- );
24358
-
24359
- $url = $this->get_activation_url( $params );
24360
- }
24361
-
24362
- add_action( 'admin_footer', array( &$this, '_add_optout_dialog' ) );
24363
-
24364
- if ( ! empty( $link_text_id ) && $this->is_plugin() && self::is_plugins_page() ) {
24365
- $this->add_plugin_action_link(
24366
- $link_text_id,
24367
- $url,
24368
- false,
24369
- 13,
24370
- "opt-in-or-opt-out {$this->_slug}"
24371
- );
24372
- }
24373
- }
24374
-
24375
- /**
24376
- * Get the URL of the page that should be loaded right after the plugin activation.
24377
- *
24378
- * @author Vova Feldman (@svovaf)
24379
- * @since 1.1.7.4
24380
- *
24381
- * @return string
24382
- */
24383
- function get_after_plugin_activation_redirect_url() {
24384
- $url = false;
24385
-
24386
- if ( ! $this->is_addon() || ! $this->has_free_plan() ) {
24387
- $first_time_path = $this->_menu->get_first_time_path(
24388
- fs_is_network_admin() && $this->_is_network_active
24389
- );
24390
-
24391
- if ( $this->is_activation_mode() ) {
24392
- $url = $this->get_activation_url();
24393
- } else if ( ! empty( $first_time_path ) ) {
24394
- $url = $first_time_path;
24395
- } else {
24396
- $page = '';
24397
- if ( ! empty( $this->_dynamically_added_top_level_page_hook_name ) ) {
24398
- if ( $this->is_network_registered() ) {
24399
- $page = 'account';
24400
- } else if ( $this->is_pending_activation() || $this->is_network_anonymous() ) {
24401
- $this->maybe_set_slug_and_network_menu_exists_flag();
24402
- }
24403
- }
24404
-
24405
- $url = $this->_get_admin_page_url( $page );
24406
- }
24407
- } else {
24408
- $plugin_fs = false;
24409
-
24410
- if ( $this->is_parent_plugin_installed() ) {
24411
- $plugin_fs = self::get_parent_instance();
24412
- }
24413
-
24414
- if ( is_object( $plugin_fs ) ) {
24415
- if ( ! $plugin_fs->is_registered() ) {
24416
- // Forward to parent plugin connect when parent not registered.
24417
- $url = $plugin_fs->get_activation_url();
24418
- } else {
24419
- // Forward to account page.
24420
- $url = $plugin_fs->_get_admin_page_url( 'account' );
24421
- }
24422
- }
24423
- }
24424
-
24425
- return $url;
24426
- }
24427
-
24428
- /**
24429
- * Forward page to activation page.
24430
- *
24431
- * @author Vova Feldman (@svovaf)
24432
- * @since 1.0.3
24433
- */
24434
- function _redirect_on_activation_hook() {
24435
- if ( $this->apply_filters( 'redirect_on_activation', true ) ) {
24436
- $url = $this->get_after_plugin_activation_redirect_url();
24437
-
24438
- if ( is_string( $url ) ) {
24439
- fs_redirect( $url );
24440
- }
24441
- }
24442
- }
24443
-
24444
- /**
24445
- * Modify plugin's page action links collection.
24446
- *
24447
- * @author Vova Feldman (@svovaf)
24448
- * @since 1.0.0
24449
- *
24450
- * @param array $links
24451
- * @param $file
24452
- *
24453
- * @return array
24454
- */
24455
- function _modify_plugin_action_links_hook( $links, $file ) {
24456
- $this->_logger->entrance();
24457
-
24458
- $passed_deactivate = false;
24459
- $deactivate_link = '';
24460
- $before_deactivate = array();
24461
- $after_deactivate = array();
24462
- foreach ( $links as $key => $link ) {
24463
- if ( 'deactivate' === $key ) {
24464
- $deactivate_link = $link;
24465
- $passed_deactivate = true;
24466
- continue;
24467
- }
24468
-
24469
- if ( ! $passed_deactivate ) {
24470
- $before_deactivate[ $key ] = $link;
24471
- } else {
24472
- $after_deactivate[ $key ] = $link;
24473
- }
24474
- }
24475
-
24476
- ksort( $this->_action_links );
24477
-
24478
- foreach ( $this->_action_links as $new_links ) {
24479
- foreach ( $new_links as $link ) {
24480
- $before_deactivate[ $link['key'] ] = '<a href="' . $link['href'] . '"' . ( $link['external'] ? ' target="_blank" rel="noopener"' : '' ) . '>' . $link['label'] . '</a>';
24481
- }
24482
- }
24483
-
24484
- if ( ! empty( $deactivate_link ) ) {
24485
- /**
24486
- * This HTML element is used to identify the correct plugin when attaching an event to its Deactivate link.
24487
- *
24488
- * @since 1.2.1.6 Always show the deactivation feedback form since we added automatic free version deactivation upon premium code activation.
24489
- */
24490
- $deactivate_link .= '<i class="fs-module-id" data-module-id="' . $this->_module_id . '"></i>';
24491
-
24492
- // Append deactivation link.
24493
- $before_deactivate['deactivate'] = $deactivate_link;
24494
- }
24495
-
24496
- return array_merge( $before_deactivate, $after_deactivate );
24497
- }
24498
-
24499
- /**
24500
- * Adds admin message.
24501
- *
24502
- * @author Vova Feldman (@svovaf)
24503
- * @since 1.0.4
24504
- *
24505
- * @param string $message
24506
- * @param string $title
24507
- * @param string $type
24508
- */
24509
- function add_admin_message( $message, $title = '', $type = 'success' ) {
24510
- $this->_admin_notices->add( $message, $title, $type );
24511
- }
24512
-
24513
- /**
24514
- * Adds sticky admin message.
24515
- *
24516
- * @author Vova Feldman (@svovaf)
24517
- * @since 1.1.0
24518
- *
24519
- * @param string $message
24520
- * @param string $id
24521
- * @param string $title
24522
- * @param string $type
24523
- */
24524
- function add_sticky_admin_message( $message, $id, $title = '', $type = 'success' ) {
24525
- $this->_admin_notices->add_sticky( $message, $id, $title, $type );
24526
- }
24527
-
24528
- /**
24529
- * Check if the paid version of the module is installed.
24530
- *
24531
- * @author Vova Feldman (@svovaf)
24532
- * @since 2.2.0
24533
- *
24534
- * @return bool
24535
- */
24536
- private function is_premium_version_installed() {
24537
- $premium_plugin_basename = $this->premium_plugin_basename();
24538
- $premium_plugin = get_plugins( '/' . dirname( $premium_plugin_basename ) );
24539
-
24540
- return ! empty( $premium_plugin );
24541
- }
24542
-
24543
- /**
24544
- * Helper function that returns the final steps for the upgrade completion.
24545
- *
24546
- * If the module is already running the premium code, returns an empty string.
24547
- *
24548
- * @author Vova Feldman (@svovaf)
24549
- * @since 1.2.1
24550
- *
24551
- * @param string $plan_title
24552
- *
24553
- * @return string
24554
- */
24555
- private function get_complete_upgrade_instructions( $plan_title = '' ) {
24556
- $this->_logger->entrance();
24557
-
24558
- $activate_license_string = $this->get_license_network_activation_notice();
24559
-
24560
- if ( ! $this->has_premium_version() || $this->is_premium() ) {
24561
- return '' . $activate_license_string;
24562
- }
24563
-
24564
- if ( empty( $plan_title ) ) {
24565
- $plan_title = $this->get_plan_title();
24566
- }
24567
-
24568
- if ( $this->is_premium_version_installed() ) {
24569
- /**
24570
- * If the premium version is already installed, instead of showing the installation instructions,
24571
- * tell the current user to activate it.
24572
- *
24573
- * @author Leo Fajardo (@leorw)
24574
- * @since 2.2.1
24575
- */
24576
- $premium_plugin_basename = $this->premium_plugin_basename();
24577
-
24578
- return sprintf(
24579
- /* translators: %1$s: Product title; %2$s: Plan title */
24580
- $this->get_text_inline( ' The paid version of %1$s is already installed. Please activate it to start benefiting the %2$s features. %3$s', 'activate-premium-version' ),
24581
- sprintf( '<em>%s</em>', esc_html( $this->get_plugin_title() ) ),
24582
- $plan_title,
24583
- sprintf(
24584
- '<a style="margin-left: 10px;" href="%s"><button class="button button-primary">%s</button></a>',
24585
- wp_nonce_url( 'plugins.php?action=activate&amp;plugin=' . $premium_plugin_basename, 'activate-plugin_' . $premium_plugin_basename ),
24586
- esc_html( sprintf(
24587
- /* translators: %s: Plan title */
24588
- $this->get_text_inline( 'Activate %s features', 'activate-x-features' ),
24589
- $plan_title
24590
- ) )
24591
- )
24592
- );
24593
- } else {
24594
- // @since 1.2.1.5 The free version is auto deactivated.
24595
- $deactivation_step = version_compare( $this->version, '1.2.1.5', '<' ) ?
24596
- ( '<li>' . $this->esc_html_inline( 'Deactivate the free version', 'deactivate-free-version' ) . '.</li>' ) :
24597
- '';
24598
-
24599
- return sprintf(
24600
- ' %s: <ol><li>%s.</li>%s<li>%s (<a href="%s" target="_blank" rel="noopener">%s</a>).</li></ol>',
24601
- $this->get_text_inline( 'Please follow these steps to complete the upgrade', 'follow-steps-to-complete-upgrade' ),
24602
- ( empty( $activate_license_string ) ? '' : $activate_license_string . '</li><li>' ) .
24603
- $this->get_latest_download_link( sprintf(
24604
- /* translators: %s: Plan title */
24605
- $this->get_text_inline( 'Download the latest %s version', 'download-latest-x-version' ),
24606
- $plan_title
24607
- ) ),
24608
- $deactivation_step,
24609
- $this->get_text_inline( 'Upload and activate the downloaded version', 'upload-and-activate' ),
24610
- $this->apply_filters( 'upload_and_install_video_url', '//bit.ly/upload-wp-' . $this->_module_type . 's' ),
24611
- $this->get_text_inline( 'How to upload and activate?', 'howto-upload-activate' )
24612
- );
24613
- }
24614
- }
24615
-
24616
- /**
24617
- * @author Leo Fajardo (@leorw)
24618
- * @since 2.1.0
24619
- *
24620
- * @param string $url
24621
- * @param array $request
24622
- */
24623
- private static function enrich_request_for_debug( &$url, &$request ) {
24624
- if ( WP_FS__DEBUG_SDK || isset( $_COOKIE['XDEBUG_SESSION'] ) ) {
24625
- $url = add_query_arg( 'XDEBUG_SESSION_START', rand( 0, 9999999 ), $url );
24626
- $url = add_query_arg( 'XDEBUG_SESSION', 'PHPSTORM', $url );
24627
-
24628
- $request['cookies'] = array(
24629
- new WP_Http_Cookie( array(
24630
- 'name' => 'XDEBUG_SESSION',
24631
- 'value' => 'PHPSTORM',
24632
- ) )
24633
- );
24634
- }
24635
- }
24636
-
24637
- /**
24638
- * @author Leo Fajardo (@leorw)
24639
- * @since 2.1.0
24640
- *
24641
- * @param string $url
24642
- * @param array $request
24643
- * @param int $success_cache_expiration
24644
- * @param int $failure_cache_expiration
24645
- * @param bool $maybe_enrich_request_for_debug
24646
- *
24647
- * @return WP_Error|array
24648
- */
24649
- static function safe_remote_post(
24650
- &$url,
24651
- $request,
24652
- $success_cache_expiration = 0,
24653
- $failure_cache_expiration = 0,
24654
- $maybe_enrich_request_for_debug = true
24655
- ) {
24656
- $should_cache = ($success_cache_expiration + $failure_cache_expiration > 0);
24657
-
24658
- $cache_key = $should_cache ? md5( fs_strip_url_protocol($url) . json_encode( $request ) ) : false;
24659
-
24660
- $response = (!WP_FS__DEBUG_SDK && ( false !== $cache_key )) ?
24661
- get_transient( $cache_key ) :
24662
- false;
24663
-
24664
- if ( false === $response ) {
24665
- if ( $maybe_enrich_request_for_debug ) {
24666
- self::enrich_request_for_debug( $url, $request );
24667
- }
24668
-
24669
- $response = wp_remote_post( $url, $request );
24670
-
24671
- if ( $response instanceof WP_Error ) {
24672
- if ( 'https://' === substr( $url, 0, 8 ) &&
24673
- isset( $response->errors ) &&
24674
- isset( $response->errors['http_request_failed'] )
24675
- ) {
24676
- $http_error = strtolower( $response->errors['http_request_failed'][0] );
24677
-
24678
- if ( false !== strpos( $http_error, 'ssl' ) ||
24679
- false !== strpos( $http_error, 'curl error 35' )
24680
- ) {
24681
- // Failed due to old version of cURL or Open SSL (SSLv3 is not supported by CloudFlare).
24682
- $url = 'http://' . substr( $url, 8 );
24683
-
24684
- $request['timeout'] = 15;
24685
- $response = wp_remote_post( $url, $request );
24686
- }
24687
- }
24688
- }
24689
-
24690
- if ( false !== $cache_key ) {
24691
- set_transient(
24692
- $cache_key,
24693
- $response,
24694
- ( ( $response instanceof WP_Error ) ?
24695
- $failure_cache_expiration :
24696
- $success_cache_expiration )
24697
- );
24698
- }
24699
- }
24700
-
24701
- return $response;
24702
- }
24703
-
24704
- /**
24705
- * This method is used to enrich the after upgrade notice instructions when the upgraded
24706
- * license cannot be activated network wide (license quota isn't large enough).
24707
- *
24708
- * @author Vova Feldman (@svovaf)
24709
- * @since 2.0.0
24710
- *
24711
- * @return string
24712
- */
24713
- private function get_license_network_activation_notice() {
24714
- if ( ! $this->_is_network_active ) {
24715
- // Module isn't network level activated.
24716
- return '';
24717
- }
24718
-
24719
- if ( ! fs_is_network_admin() ) {
24720
- // Not network level admin.
24721
- return '';
24722
- }
24723
-
24724
- if ( get_blog_count() == 1 ) {
24725
- // There's only a single site in the network so if there's a context license it was already activated.
24726
- return '';
24727
- }
24728
-
24729
- if ( ! is_object( $this->_license ) ) {
24730
- // No context license.
24731
- return '';
24732
- }
24733
-
24734
- if ( $this->_license->is_single_site() && 0 < $this->_license->activated ) {
24735
- // License was already utilized (this is not 100% the case if all the network is localhost sites and the license can be utilized on unlimited localhost sites).
24736
- return '';
24737
- }
24738
-
24739
- if ( $this->can_activate_license_on_network( $this->_license ) ) {
24740
- // License can be activated on all the network, so probably, the license is already activate on all the network (that's how the after upgrade sync works).
24741
- return '';
24742
- }
24743
-
24744
- return sprintf(
24745
- $this->get_text_inline( '%sClick here%s to choose the sites where you\'d like to activate the license on.', 'network-choose-sites-for-license' ),
24746
- '<a href="' . $this->get_account_url( false, array( 'activate_license' => 'true' ) ) . '">',
24747
- '</a>'
24748
- );
24749
- }
24750
-
24751
- /**
24752
- * @author Vova Feldman (@svovaf)
24753
- * @since 1.2.1.7
24754
- *
24755
- * @param string $key
24756
- *
24757
- * @return string
24758
- */
24759
- function get_text( $key ) {
24760
- return fs_tex