WP Security Audit Log - Version 3.1.1

Version Description

Download this release

Release Info

Developer WPWhiteSecurity
Plugin Icon 128x128 WP Security Audit Log
Version 3.1.1
Comparing to
See all releases

Code changes from version 3.1.0 to 3.1.1

classes/AuditLogListView.php CHANGED
@@ -584,6 +584,16 @@ class WSAL_AuditLogListView extends WP_List_Table {
584
  return '<strong>' . esc_html( $value ) . '</strong>';
585
  }
586
 
 
 
 
 
 
 
 
 
 
 
587
  default:
588
  return '<strong>' . esc_html( $value ) . '</strong>';
589
  }
584
  return '<strong>' . esc_html( $value ) . '</strong>';
585
  }
586
 
587
+ case '%multisite_text%' === $name:
588
+ if ( $this->is_multisite() && $value ) {
589
+ $site_info = get_blog_details( $value, true );
590
+ if ( $site_info ) {
591
+ return ' on site <a href="' . esc_url( $site_info->siteurl ) . '">' . esc_html( $site_info->blogname ) . '</a>';
592
+ }
593
+ return;
594
+ }
595
+ return;
596
+
597
  default:
598
  return '<strong>' . esc_html( $value ) . '</strong>';
599
  }
classes/Sensors/Comments.php CHANGED
@@ -151,13 +151,17 @@ class WSAL_Sensors_Comments extends WSAL_AbstractSensor {
151
  'Date' => $comment->comment_date,
152
  'CommentLink' => '<a target="_blank" href="' . $comment_link . '">' . $comment->comment_date . '</a>',
153
  );
154
- if ( ! username_exists( $comment->comment_author ) ) {
 
 
 
 
155
  // Set the fields.
156
  $fields['CommentMsg'] = sprintf( 'A comment was posted in response to the post <strong>%s</strong>. The comment was posted by <strong>%s</strong>', $post->post_title, $this->CheckAuthor( $comment ) );
157
  $fields['Username'] = 'Website Visitor';
 
158
  } else {
159
  // Get user roles.
160
- $user_data = get_user_by( 'login', $comment->comment_author );
161
  $user_roles = $user_data->roles;
162
 
163
  // Check if superadmin.
@@ -166,12 +170,11 @@ class WSAL_Sensors_Comments extends WSAL_AbstractSensor {
166
  }
167
 
168
  // Set the fields.
169
- $fields['Username'] = $comment->comment_author;
170
  $fields['CurrentUserRoles'] = $user_roles;
171
  $fields['CommentMsg'] = sprintf( 'Posted a comment in response to the post <strong>%s</strong>', $post->post_title );
 
172
  }
173
-
174
- $this->plugin->alerts->Trigger( 2099, $fields );
175
  }
176
  }
177
  }
151
  'Date' => $comment->comment_date,
152
  'CommentLink' => '<a target="_blank" href="' . $comment_link . '">' . $comment->comment_date . '</a>',
153
  );
154
+
155
+ // Get user data.
156
+ $user_data = get_user_by( 'email', $comment->comment_author_email );
157
+
158
+ if ( ! $user_data ) {
159
  // Set the fields.
160
  $fields['CommentMsg'] = sprintf( 'A comment was posted in response to the post <strong>%s</strong>. The comment was posted by <strong>%s</strong>', $post->post_title, $this->CheckAuthor( $comment ) );
161
  $fields['Username'] = 'Website Visitor';
162
+ $this->plugin->alerts->Trigger( 2126, $fields );
163
  } else {
164
  // Get user roles.
 
165
  $user_roles = $user_data->roles;
166
 
167
  // Check if superadmin.
170
  }
171
 
172
  // Set the fields.
173
+ $fields['Username'] = $user_data->user_login;
174
  $fields['CurrentUserRoles'] = $user_roles;
175
  $fields['CommentMsg'] = sprintf( 'Posted a comment in response to the post <strong>%s</strong>', $post->post_title );
176
+ $this->plugin->alerts->Trigger( 2099, $fields );
177
  }
 
 
178
  }
179
  }
180
  }
classes/Sensors/Content.php CHANGED
@@ -439,29 +439,20 @@ class WSAL_Sensors_Content extends WSAL_AbstractSensor {
439
  $this->CheckPostCreation( $this->_old_post, $post );
440
  } else {
441
  // Handle update post events.
442
- $changes = 0
443
- + $this->CheckAuthorChange( $this->_old_post, $post )
444
  + $this->CheckStatusChange( $this->_old_post, $post )
445
  + $this->CheckParentChange( $this->_old_post, $post )
446
  + $this->CheckStickyChange( $this->_old_stky, $sticky, $post )
447
  + $this->CheckVisibilityChange( $this->_old_post, $post, $old_status, $new_status )
448
  + $this->CheckTemplateChange( $this->_old_tmpl, $this->GetPostTemplate( $post ), $post )
449
  + $this->CheckCategoriesChange( $this->_old_cats, $this->GetPostCategories( $post ), $post )
450
- + $this->check_tags_change( $this->_old_tags, $this->get_post_tags( $post ), $post );
451
-
452
- if ( ! $changes ) {
453
- $changes = $this->CheckDateChange( $this->_old_post, $post );
454
- if ( ! $changes ) {
455
- $changes = $this->CheckPermalinkChange( $this->_old_link, get_permalink( $post->ID ), $post );
456
- // Comments/Trackbacks and Pingbacks.
457
- if ( ! $changes ) {
458
- $changes = $this->CheckCommentsPings( $this->_old_post, $post );
459
- if ( ! $changes ) {
460
- $changes = $this->CheckModificationChange( $post->ID, $this->_old_post, $post );
461
- }
462
- }
463
- }
464
- }
465
  }
466
  }
467
  }
@@ -909,7 +900,7 @@ class WSAL_Sensors_Content extends WSAL_AbstractSensor {
909
  * @param stdClass $post - The post.
910
  */
911
  protected function CheckPermalinkChange( $old_link, $new_link, $post ) {
912
- if ( $old_link != $new_link ) {
913
  $editor_link = $this->GetEditorLink( $post );
914
  $this->plugin->alerts->Trigger(
915
  2017, array(
@@ -1039,11 +1030,12 @@ class WSAL_Sensors_Content extends WSAL_AbstractSensor {
1039
  /**
1040
  * Post modified content.
1041
  *
1042
- * @param integer $post_id - Post ID.
1043
- * @param stdClass $oldpost - Old post.
1044
- * @param stdClass $newpost - New post.
 
1045
  */
1046
- public function CheckModificationChange( $post_id, $oldpost, $newpost ) {
1047
  if ( $this->CheckOtherSensors( $oldpost ) ) {
1048
  return;
1049
  }
@@ -1057,7 +1049,7 @@ class WSAL_Sensors_Content extends WSAL_AbstractSensor {
1057
  // Check if content changed.
1058
  if ( $content_changed ) {
1059
  $event = 2065;
1060
- } else {
1061
  $event = 2002;
1062
  }
1063
  if ( $event ) {
@@ -1541,6 +1533,8 @@ class WSAL_Sensors_Content extends WSAL_AbstractSensor {
1541
  */
1542
  private function CheckCommentsPings( $oldpost, $newpost ) {
1543
  $result = 0;
 
 
1544
  // Comments.
1545
  if ( $oldpost->comment_status != $newpost->comment_status ) {
1546
  $type = 'Comments';
439
  $this->CheckPostCreation( $this->_old_post, $post );
440
  } else {
441
  // Handle update post events.
442
+ $changes = 0;
443
+ $changes = $this->CheckAuthorChange( $this->_old_post, $post )
444
  + $this->CheckStatusChange( $this->_old_post, $post )
445
  + $this->CheckParentChange( $this->_old_post, $post )
446
  + $this->CheckStickyChange( $this->_old_stky, $sticky, $post )
447
  + $this->CheckVisibilityChange( $this->_old_post, $post, $old_status, $new_status )
448
  + $this->CheckTemplateChange( $this->_old_tmpl, $this->GetPostTemplate( $post ), $post )
449
  + $this->CheckCategoriesChange( $this->_old_cats, $this->GetPostCategories( $post ), $post )
450
+ + $this->check_tags_change( $this->_old_tags, $this->get_post_tags( $post ), $post )
451
+ + $this->CheckDateChange( $this->_old_post, $post )
452
+ + $this->CheckPermalinkChange( $this->_old_link, get_permalink( $post->ID ), $post )
453
+ + $this->CheckCommentsPings( $this->_old_post, $post );
454
+
455
+ $this->CheckModificationChange( $post->ID, $this->_old_post, $post, $changes );
 
 
 
 
 
 
 
 
 
456
  }
457
  }
458
  }
900
  * @param stdClass $post - The post.
901
  */
902
  protected function CheckPermalinkChange( $old_link, $new_link, $post ) {
903
+ if ( $old_link !== $new_link ) {
904
  $editor_link = $this->GetEditorLink( $post );
905
  $this->plugin->alerts->Trigger(
906
  2017, array(
1030
  /**
1031
  * Post modified content.
1032
  *
1033
+ * @param integer $post_id – Post ID.
1034
+ * @param stdClass $oldpost – Old post.
1035
+ * @param stdClass $newpost – New post.
1036
+ * @param int $modified – Set to 0 if no changes done to the post.
1037
  */
1038
+ public function CheckModificationChange( $post_id, $oldpost, $newpost, $modified ) {
1039
  if ( $this->CheckOtherSensors( $oldpost ) ) {
1040
  return;
1041
  }
1049
  // Check if content changed.
1050
  if ( $content_changed ) {
1051
  $event = 2065;
1052
+ } elseif ( ! $modified ) {
1053
  $event = 2002;
1054
  }
1055
  if ( $event ) {
1533
  */
1534
  private function CheckCommentsPings( $oldpost, $newpost ) {
1535
  $result = 0;
1536
+ $editor_link = $this->GetEditorLink( $newpost );
1537
+
1538
  // Comments.
1539
  if ( $oldpost->comment_status != $newpost->comment_status ) {
1540
  $type = 'Comments';
classes/Sensors/UserProfile.php CHANGED
@@ -101,7 +101,15 @@ class WSAL_Sensors_UserProfile extends WSAL_AbstractSensor {
101
  // Filter $_POST array for security.
102
  $post_array = filter_input_array( INPUT_POST );
103
 
104
- if ( isset( $post_array['_wpnonce'] ) && ! wp_verify_nonce( $post_array['_wpnonce'], 'update-user_' . $user_id ) ) {
 
 
 
 
 
 
 
 
105
  return false;
106
  }
107
 
@@ -126,8 +134,17 @@ class WSAL_Sensors_UserProfile extends WSAL_AbstractSensor {
126
  }
127
  $old_roles = array_diff( $old_roles, $bbpress_roles );
128
  }
 
 
129
  $old_role = count( $old_roles ) ? implode( ', ', $old_roles ) : '';
130
  $new_role = $role;
 
 
 
 
 
 
 
131
  if ( $old_role != $new_role ) {
132
  $this->plugin->alerts->TriggerIf(
133
  4002, array(
@@ -135,6 +152,7 @@ class WSAL_Sensors_UserProfile extends WSAL_AbstractSensor {
135
  'TargetUsername' => $user->user_login,
136
  'OldRole' => $old_role,
137
  'NewRole' => $new_role,
 
138
  ), array( $this, 'MustNotContainUserChanges' )
139
  );
140
  }
101
  // Filter $_POST array for security.
102
  $post_array = filter_input_array( INPUT_POST );
103
 
104
+ if ( ! isset( $post_array['changeit'] ) ) {
105
+ if ( isset( $post_array['_wpnonce'] )
106
+ && ! wp_verify_nonce( $post_array['_wpnonce'], 'update-user_' . $user_id ) ) {
107
+ return false;
108
+ }
109
+ } elseif ( isset( $post_array['changeit'] )
110
+ && 'Change' === $post_array['changeit']
111
+ && isset( $post_array['_wpnonce'] )
112
+ && ! wp_verify_nonce( $post_array['_wpnonce'], 'bulk-users' ) ) {
113
  return false;
114
  }
115
 
134
  }
135
  $old_roles = array_diff( $old_roles, $bbpress_roles );
136
  }
137
+
138
+ // Get roles.
139
  $old_role = count( $old_roles ) ? implode( ', ', $old_roles ) : '';
140
  $new_role = $role;
141
+
142
+ // If multisite, then get its URL.
143
+ if ( $this->plugin->IsMultisite() ) {
144
+ $site_id = get_current_blog_id();
145
+ }
146
+
147
+ // Alert if roles are changed.
148
  if ( $old_role != $new_role ) {
149
  $this->plugin->alerts->TriggerIf(
150
  4002, array(
152
  'TargetUsername' => $user->user_login,
153
  'OldRole' => $old_role,
154
  'NewRole' => $new_role,
155
+ 'multisite_text' => $this->plugin->IsMultisite() ? $site_id : false,
156
  ), array( $this, 'MustNotContainUserChanges' )
157
  );
158
  }
classes/ViewManager.php CHANGED
@@ -153,6 +153,14 @@ class WSAL_ViewManager {
153
  continue;
154
  }
155
 
 
 
 
 
 
 
 
 
156
  $view->hook_suffix = add_submenu_page(
157
  $view->IsVisible() ? $this->views[0]->GetSafeViewName() : null,
158
  $view->GetTitle(),
153
  continue;
154
  }
155
 
156
+ if ( ( 'wsal-togglealerts' === $view->GetSafeViewName()
157
+ || 'wsal-settings' === $view->GetSafeViewName()
158
+ || 'wsal-ext-settings' === $view->GetSafeViewName()
159
+ )
160
+ && ! $this->_plugin->settings->CurrentUserCan( 'edit' ) ) {
161
+ continue;
162
+ }
163
+
164
  $view->hook_suffix = add_submenu_page(
165
  $view->IsVisible() ? $this->views[0]->GetSafeViewName() : null,
166
  $view->GetTitle(),
classes/Views/AuditLog.php CHANGED
@@ -79,7 +79,16 @@ class WSAL_Views_AuditLog extends WSAL_AbstractView {
79
  <!-- /.wsal_notice__wrapper -->
80
  <div class="wsal_notice__btns">
81
  <?php
 
82
  $buy_now = add_query_arg( 'page', 'wsal-auditlog-pricing', admin_url( 'admin.php' ) );
 
 
 
 
 
 
 
 
83
  $more_info = add_query_arg(
84
  array(
85
  'utm_source' => 'plugin',
79
  <!-- /.wsal_notice__wrapper -->
80
  <div class="wsal_notice__btns">
81
  <?php
82
+ // Buy Now button link.
83
  $buy_now = add_query_arg( 'page', 'wsal-auditlog-pricing', admin_url( 'admin.php' ) );
84
+
85
+ // If user is not super admin and website is multisite then change the URL.
86
+ if ( $this->_plugin->IsMultisite() && ! is_super_admin() ) {
87
+ $buy_now = 'https://www.wpsecurityauditlog.com/pricing/';
88
+ } elseif ( $this->_plugin->IsMultisite() && is_super_admin() ) {
89
+ $buy_now = add_query_arg( 'page', 'wsal-auditlog-pricing', network_admin_url( 'admin.php' ) );
90
+ }
91
+
92
  $more_info = add_query_arg(
93
  array(
94
  'utm_source' => 'plugin',
classes/Views/EmailNotifications.php CHANGED
@@ -124,7 +124,21 @@ class WSAL_Views_EmailNotifications extends WSAL_AbstractView {
124
  </ul>
125
  </p>
126
  <?php
 
127
  $buy_now = add_query_arg( 'page', 'wsal-auditlog-pricing', admin_url( 'admin.php' ) );
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  $more_info = add_query_arg(
129
  array(
130
  'utm_source' => 'plugin',
@@ -136,7 +150,7 @@ class WSAL_Views_EmailNotifications extends WSAL_AbstractView {
136
  );
137
  ?>
138
  <p>
139
- <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $buy_now ); ?>"><?php esc_html_e( 'Upgrade to Premium', 'wp-security-audit-log' ); ?></a>
140
  <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $more_info ); ?>" target="_blank"><?php esc_html_e( 'More Information', 'wp-security-audit-log' ); ?></a>
141
  </p>
142
  </div>
@@ -166,6 +180,11 @@ class WSAL_Views_EmailNotifications extends WSAL_AbstractView {
166
  </li>
167
  </ul>
168
  </p>
 
 
 
 
 
169
  </div>
170
  </div>
171
  <!-- /.wsal-row -->
124
  </ul>
125
  </p>
126
  <?php
127
+ // Buy Now button link.
128
  $buy_now = add_query_arg( 'page', 'wsal-auditlog-pricing', admin_url( 'admin.php' ) );
129
+ $buy_now_target = '';
130
+
131
+ // If user is not super admin and website is multisite then change the URL.
132
+ if ( $this->_plugin->IsMultisite() && ! is_super_admin() ) {
133
+ $buy_now = 'https://www.wpsecurityauditlog.com/pricing/';
134
+ $buy_now_target = 'target="_blank"';
135
+ } elseif ( $this->_plugin->IsMultisite() && is_super_admin() ) {
136
+ $buy_now = add_query_arg( 'page', 'wsal-auditlog-pricing', network_admin_url( 'admin.php' ) );
137
+ } elseif ( ! $this->_plugin->IsMultisite() && ! current_user_can( 'manage_options' ) ) {
138
+ $buy_now = 'https://www.wpsecurityauditlog.com/pricing/';
139
+ $buy_now_target = 'target="_blank"';
140
+ }
141
+
142
  $more_info = add_query_arg(
143
  array(
144
  'utm_source' => 'plugin',
150
  );
151
  ?>
152
  <p>
153
+ <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $buy_now ); ?>" <?php echo esc_attr( $buy_now_target ); ?>><?php esc_html_e( 'Upgrade to Premium', 'wp-security-audit-log' ); ?></a>
154
  <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $more_info ); ?>" target="_blank"><?php esc_html_e( 'More Information', 'wp-security-audit-log' ); ?></a>
155
  </p>
156
  </div>
180
  </li>
181
  </ul>
182
  </p>
183
+
184
+ <p>
185
+ <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $buy_now ); ?>" <?php echo esc_attr( $buy_now_target ); ?>><?php esc_html_e( 'Upgrade to Premium', 'wp-security-audit-log' ); ?></a>
186
+ <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $more_info ); ?>" target="_blank"><?php esc_html_e( 'More Information', 'wp-security-audit-log' ); ?></a>
187
+ </p>
188
  </div>
189
  </div>
190
  <!-- /.wsal-row -->
classes/Views/ExternalDB.php CHANGED
@@ -123,7 +123,21 @@ class WSAL_Views_ExternalDB extends WSAL_AbstractView {
123
  </ul>
124
  </p>
125
  <?php
 
126
  $buy_now = add_query_arg( 'page', 'wsal-auditlog-pricing', admin_url( 'admin.php' ) );
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  $more_info = add_query_arg(
128
  array(
129
  'utm_source' => 'plugin',
@@ -135,7 +149,7 @@ class WSAL_Views_ExternalDB extends WSAL_AbstractView {
135
  );
136
  ?>
137
  <p>
138
- <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $buy_now ); ?>"><?php esc_html_e( 'Upgrade to Premium', 'wp-security-audit-log' ); ?></a>
139
  <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $more_info ); ?>" target="_blank"><?php esc_html_e( 'More Information', 'wp-security-audit-log' ); ?></a>
140
  </p>
141
  </div>
@@ -165,6 +179,11 @@ class WSAL_Views_ExternalDB extends WSAL_AbstractView {
165
  </li>
166
  </ul>
167
  </p>
 
 
 
 
 
168
  </div>
169
  </div>
170
  <!-- /.wsal-row -->
123
  </ul>
124
  </p>
125
  <?php
126
+ // Buy Now button link.
127
  $buy_now = add_query_arg( 'page', 'wsal-auditlog-pricing', admin_url( 'admin.php' ) );
128
+ $buy_now_target = '';
129
+
130
+ // If user is not super admin and website is multisite then change the URL.
131
+ if ( $this->_plugin->IsMultisite() && ! is_super_admin() ) {
132
+ $buy_now = 'https://www.wpsecurityauditlog.com/pricing/';
133
+ $buy_now_target = 'target="_blank"';
134
+ } elseif ( $this->_plugin->IsMultisite() && is_super_admin() ) {
135
+ $buy_now = add_query_arg( 'page', 'wsal-auditlog-pricing', network_admin_url( 'admin.php' ) );
136
+ } elseif ( ! $this->_plugin->IsMultisite() && ! current_user_can( 'manage_options' ) ) {
137
+ $buy_now = 'https://www.wpsecurityauditlog.com/pricing/';
138
+ $buy_now_target = 'target="_blank"';
139
+ }
140
+
141
  $more_info = add_query_arg(
142
  array(
143
  'utm_source' => 'plugin',
149
  );
150
  ?>
151
  <p>
152
+ <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $buy_now ); ?>" <?php echo esc_attr( $buy_now_target ); ?>><?php esc_html_e( 'Upgrade to Premium', 'wp-security-audit-log' ); ?></a>
153
  <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $more_info ); ?>" target="_blank"><?php esc_html_e( 'More Information', 'wp-security-audit-log' ); ?></a>
154
  </p>
155
  </div>
179
  </li>
180
  </ul>
181
  </p>
182
+
183
+ <p>
184
+ <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $buy_now ); ?>" <?php echo esc_attr( $buy_now_target ); ?>><?php esc_html_e( 'Upgrade to Premium', 'wp-security-audit-log' ); ?></a>
185
+ <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $more_info ); ?>" target="_blank"><?php esc_html_e( 'More Information', 'wp-security-audit-log' ); ?></a>
186
+ </p>
187
  </div>
188
  </div>
189
  <!-- /.wsal-row -->
classes/Views/Help.php CHANGED
@@ -166,7 +166,21 @@ class WSAL_Views_Help extends WSAL_AbstractView {
166
  </li>
167
  </ul>
168
  <?php
 
169
  $buy_now = add_query_arg( 'page', 'wsal-auditlog-pricing', admin_url( 'admin.php' ) );
 
 
 
 
 
 
 
 
 
 
 
 
 
170
  $more_info = add_query_arg(
171
  array(
172
  'utm_source' => 'plugin',
@@ -178,7 +192,7 @@ class WSAL_Views_Help extends WSAL_AbstractView {
178
  );
179
  ?>
180
  <p>
181
- <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $buy_now ); ?>"><?php esc_html_e( 'Upgrade to Premium', 'wp-security-audit-log' ); ?></a>
182
  <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $more_info ); ?>" target="_blank"><?php esc_html_e( 'More Information', 'wp-security-audit-log' ); ?></a>
183
  </p>
184
  </div>
166
  </li>
167
  </ul>
168
  <?php
169
+ // Buy Now button link.
170
  $buy_now = add_query_arg( 'page', 'wsal-auditlog-pricing', admin_url( 'admin.php' ) );
171
+ $buy_now_target = '';
172
+
173
+ // If user is not super admin and website is multisite then change the URL.
174
+ if ( $this->_plugin->IsMultisite() && ! is_super_admin() ) {
175
+ $buy_now = 'https://www.wpsecurityauditlog.com/pricing/';
176
+ $buy_now_target = 'target="_blank"';
177
+ } elseif ( $this->_plugin->IsMultisite() && is_super_admin() ) {
178
+ $buy_now = add_query_arg( 'page', 'wsal-auditlog-pricing', network_admin_url( 'admin.php' ) );
179
+ } elseif ( ! $this->_plugin->IsMultisite() && ! current_user_can( 'manage_options' ) ) {
180
+ $buy_now = 'https://www.wpsecurityauditlog.com/pricing/';
181
+ $buy_now_target = 'target="_blank"';
182
+ }
183
+
184
  $more_info = add_query_arg(
185
  array(
186
  'utm_source' => 'plugin',
192
  );
193
  ?>
194
  <p>
195
+ <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $buy_now ); ?>" <?php echo esc_attr( $buy_now_target ); ?>><?php esc_html_e( 'Upgrade to Premium', 'wp-security-audit-log' ); ?></a>
196
  <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $more_info ); ?>" target="_blank"><?php esc_html_e( 'More Information', 'wp-security-audit-log' ); ?></a>
197
  </p>
198
  </div>
classes/Views/LogInUsers.php CHANGED
@@ -125,7 +125,21 @@ class WSAL_Views_LogInUsers extends WSAL_AbstractView {
125
  </ul>
126
  </p>
127
  <?php
 
128
  $buy_now = add_query_arg( 'page', 'wsal-auditlog-pricing', admin_url( 'admin.php' ) );
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  $more_info = add_query_arg(
130
  array(
131
  'utm_source' => 'plugin',
@@ -137,7 +151,7 @@ class WSAL_Views_LogInUsers extends WSAL_AbstractView {
137
  );
138
  ?>
139
  <p>
140
- <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $buy_now ); ?>"><?php esc_html_e( 'Upgrade to Premium', 'wp-security-audit-log' ); ?></a>
141
  <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $more_info ); ?>" target="_blank"><?php esc_html_e( 'More Information', 'wp-security-audit-log' ); ?></a>
142
  </p>
143
  </div>
@@ -167,6 +181,11 @@ class WSAL_Views_LogInUsers extends WSAL_AbstractView {
167
  </li>
168
  </ul>
169
  </p>
 
 
 
 
 
170
  </div>
171
  </div>
172
  <!-- /.wsal-row -->
125
  </ul>
126
  </p>
127
  <?php
128
+ // Buy Now button link.
129
  $buy_now = add_query_arg( 'page', 'wsal-auditlog-pricing', admin_url( 'admin.php' ) );
130
+ $buy_now_target = '';
131
+
132
+ // If user is not super admin and website is multisite then change the URL.
133
+ if ( $this->_plugin->IsMultisite() && ! is_super_admin() ) {
134
+ $buy_now = 'https://www.wpsecurityauditlog.com/pricing/';
135
+ $buy_now_target = 'target="_blank"';
136
+ } elseif ( $this->_plugin->IsMultisite() && is_super_admin() ) {
137
+ $buy_now = add_query_arg( 'page', 'wsal-auditlog-pricing', network_admin_url( 'admin.php' ) );
138
+ } elseif ( ! $this->_plugin->IsMultisite() && ! current_user_can( 'manage_options' ) ) {
139
+ $buy_now = 'https://www.wpsecurityauditlog.com/pricing/';
140
+ $buy_now_target = 'target="_blank"';
141
+ }
142
+
143
  $more_info = add_query_arg(
144
  array(
145
  'utm_source' => 'plugin',
151
  );
152
  ?>
153
  <p>
154
+ <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $buy_now ); ?>" <?php echo esc_attr( $buy_now_target ); ?>><?php esc_html_e( 'Upgrade to Premium', 'wp-security-audit-log' ); ?></a>
155
  <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $more_info ); ?>" target="_blank"><?php esc_html_e( 'More Information', 'wp-security-audit-log' ); ?></a>
156
  </p>
157
  </div>
181
  </li>
182
  </ul>
183
  </p>
184
+
185
+ <p>
186
+ <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $buy_now ); ?>" <?php echo esc_attr( $buy_now_target ); ?>><?php esc_html_e( 'Upgrade to Premium', 'wp-security-audit-log' ); ?></a>
187
+ <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $more_info ); ?>" target="_blank"><?php esc_html_e( 'More Information', 'wp-security-audit-log' ); ?></a>
188
+ </p>
189
  </div>
190
  </div>
191
  <!-- /.wsal-row -->
classes/Views/Reports.php CHANGED
@@ -124,7 +124,21 @@ class WSAL_Views_Reports extends WSAL_AbstractView {
124
  </ul>
125
  </p>
126
  <?php
 
127
  $buy_now = add_query_arg( 'page', 'wsal-auditlog-pricing', admin_url( 'admin.php' ) );
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  $more_info = add_query_arg(
129
  array(
130
  'utm_source' => 'plugin',
@@ -136,7 +150,7 @@ class WSAL_Views_Reports extends WSAL_AbstractView {
136
  );
137
  ?>
138
  <p>
139
- <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $buy_now ); ?>"><?php esc_html_e( 'Upgrade to Premium', 'wp-security-audit-log' ); ?></a>
140
  <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $more_info ); ?>" target="_blank"><?php esc_html_e( 'More Information', 'wp-security-audit-log' ); ?></a>
141
  </p>
142
  </div>
@@ -166,6 +180,11 @@ class WSAL_Views_Reports extends WSAL_AbstractView {
166
  </li>
167
  </ul>
168
  </p>
 
 
 
 
 
169
  </div>
170
  </div>
171
  <!-- /.wsal-row -->
124
  </ul>
125
  </p>
126
  <?php
127
+ // Buy Now button link.
128
  $buy_now = add_query_arg( 'page', 'wsal-auditlog-pricing', admin_url( 'admin.php' ) );
129
+ $buy_now_target = '';
130
+
131
+ // If user is not super admin and website is multisite then change the URL.
132
+ if ( $this->_plugin->IsMultisite() && ! is_super_admin() ) {
133
+ $buy_now = 'https://www.wpsecurityauditlog.com/pricing/';
134
+ $buy_now_target = 'target="_blank"';
135
+ } elseif ( $this->_plugin->IsMultisite() && is_super_admin() ) {
136
+ $buy_now = add_query_arg( 'page', 'wsal-auditlog-pricing', network_admin_url( 'admin.php' ) );
137
+ } elseif ( ! $this->_plugin->IsMultisite() && ! current_user_can( 'manage_options' ) ) {
138
+ $buy_now = 'https://www.wpsecurityauditlog.com/pricing/';
139
+ $buy_now_target = 'target="_blank"';
140
+ }
141
+
142
  $more_info = add_query_arg(
143
  array(
144
  'utm_source' => 'plugin',
150
  );
151
  ?>
152
  <p>
153
+ <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $buy_now ); ?>" <?php echo esc_attr( $buy_now_target ); ?>><?php esc_html_e( 'Upgrade to Premium', 'wp-security-audit-log' ); ?></a>
154
  <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $more_info ); ?>" target="_blank"><?php esc_html_e( 'More Information', 'wp-security-audit-log' ); ?></a>
155
  </p>
156
  </div>
180
  </li>
181
  </ul>
182
  </p>
183
+
184
+ <p>
185
+ <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $buy_now ); ?>" <?php echo esc_attr( $buy_now_target ); ?>><?php esc_html_e( 'Upgrade to Premium', 'wp-security-audit-log' ); ?></a>
186
+ <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $more_info ); ?>" target="_blank"><?php esc_html_e( 'More Information', 'wp-security-audit-log' ); ?></a>
187
+ </p>
188
  </div>
189
  </div>
190
  <!-- /.wsal-row -->
classes/Views/Search.php CHANGED
@@ -124,7 +124,21 @@ class WSAL_Views_Search extends WSAL_AbstractView {
124
  </ul>
125
  </p>
126
  <?php
 
127
  $buy_now = add_query_arg( 'page', 'wsal-auditlog-pricing', admin_url( 'admin.php' ) );
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  $more_info = add_query_arg(
129
  array(
130
  'utm_source' => 'plugin',
@@ -136,7 +150,7 @@ class WSAL_Views_Search extends WSAL_AbstractView {
136
  );
137
  ?>
138
  <p>
139
- <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $buy_now ); ?>"><?php esc_html_e( 'Upgrade to Premium', 'wp-security-audit-log' ); ?></a>
140
  <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $more_info ); ?>" target="_blank"><?php esc_html_e( 'More Information', 'wp-security-audit-log' ); ?></a>
141
  </p>
142
  </div>
@@ -159,6 +173,11 @@ class WSAL_Views_Search extends WSAL_AbstractView {
159
  </li>
160
  </ul>
161
  </p>
 
 
 
 
 
162
  </div>
163
  </div>
164
  <!-- /.wsal-row -->
124
  </ul>
125
  </p>
126
  <?php
127
+ // Buy Now button link.
128
  $buy_now = add_query_arg( 'page', 'wsal-auditlog-pricing', admin_url( 'admin.php' ) );
129
+ $buy_now_target = '';
130
+
131
+ // If user is not super admin and website is multisite then change the URL.
132
+ if ( $this->_plugin->IsMultisite() && ! is_super_admin() ) {
133
+ $buy_now = 'https://www.wpsecurityauditlog.com/pricing/';
134
+ $buy_now_target = 'target="_blank"';
135
+ } elseif ( $this->_plugin->IsMultisite() && is_super_admin() ) {
136
+ $buy_now = add_query_arg( 'page', 'wsal-auditlog-pricing', network_admin_url( 'admin.php' ) );
137
+ } elseif ( ! $this->_plugin->IsMultisite() && ! current_user_can( 'manage_options' ) ) {
138
+ $buy_now = 'https://www.wpsecurityauditlog.com/pricing/';
139
+ $buy_now_target = 'target="_blank"';
140
+ }
141
+
142
  $more_info = add_query_arg(
143
  array(
144
  'utm_source' => 'plugin',
150
  );
151
  ?>
152
  <p>
153
+ <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $buy_now ); ?>" <?php echo esc_attr( $buy_now_target ); ?>><?php esc_html_e( 'Upgrade to Premium', 'wp-security-audit-log' ); ?></a>
154
  <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $more_info ); ?>" target="_blank"><?php esc_html_e( 'More Information', 'wp-security-audit-log' ); ?></a>
155
  </p>
156
  </div>
173
  </li>
174
  </ul>
175
  </p>
176
+
177
+ <p>
178
+ <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $buy_now ); ?>" <?php echo esc_attr( $buy_now_target ); ?>><?php esc_html_e( 'Upgrade to Premium', 'wp-security-audit-log' ); ?></a>
179
+ <a class="button-primary wsal-extension-btn" href="<?php echo esc_attr( $more_info ); ?>" target="_blank"><?php esc_html_e( 'More Information', 'wp-security-audit-log' ); ?></a>
180
+ </p>
181
  </div>
182
  </div>
183
  <!-- /.wsal-row -->
defaults.php CHANGED
@@ -129,7 +129,13 @@ function wsaldefaults_wsal_init( WpSecurityAuditLog $wsal ) {
129
  // Create list of default alerts.
130
  $wsal->alerts->RegisterGroup(
131
  array(
 
 
 
132
  __( 'Content & Comments', 'wp-security-audit-log' ) => array(
 
 
 
133
  __( 'Content', 'wp-security-audit-log' ) => array(
134
  array( 2000, E_NOTICE, __( 'User created a new post and saved it as draft', 'wp-security-audit-log' ), __( 'Created a new %PostType% titled %PostTitle% and saved it as draft. %EditorLinkPost%.', 'wp-security-audit-log' ) ),
135
  array( 2001, E_NOTICE, __( 'User published a post', 'wp-security-audit-log' ), __( 'Published a %PostType% titled %PostTitle%. URL is %PostUrl%. %EditorLinkPost%.', 'wp-security-audit-log' ) ),
@@ -171,6 +177,10 @@ function wsaldefaults_wsal_init( WpSecurityAuditLog $wsal ) {
171
  array( 2124, E_NOTICE, __( 'User changed tag slug', 'wp-security-audit-log' ), __( 'Changed the slug of tag %tag% from %old_slug% to %new_slug%. View the tag: %TagLink%.', 'wp-security-audit-log' ) ),
172
  array( 2125, E_NOTICE, __( 'User changed tag description', 'wp-security-audit-log' ), __( 'Changed the description of the tag %tag% from %old_desc% to %new_desc%. View the tag: %TagLink%.', 'wp-security-audit-log' ) ),
173
  ),
 
 
 
 
174
  __( 'Comments', 'wp-security-audit-log' ) => array(
175
  array( 2090, E_NOTICE, __( 'User approved a comment', 'wp-security-audit-log' ), __( 'Approved the comment posted in response to the post %PostTitle% by %Author% on %CommentLink%.', 'wp-security-audit-log' ) ),
176
  array( 2091, E_NOTICE, __( 'User unapproved a comment', 'wp-security-audit-log' ), __( 'Unapproved the comment posted in response to the post %PostTitle% by %Author% on %CommentLink%.', 'wp-security-audit-log' ) ),
@@ -182,7 +192,17 @@ function wsaldefaults_wsal_init( WpSecurityAuditLog $wsal ) {
182
  array( 2097, E_NOTICE, __( 'User restored a comment from the trash', 'wp-security-audit-log' ), __( 'Restored the comment posted in response to the post %PostTitle% by %Author% on %CommentLink% from the trash.', 'wp-security-audit-log' ) ),
183
  array( 2098, E_NOTICE, __( 'User permanently deleted a comment', 'wp-security-audit-log' ), __( 'Permanently deleted the comment posted in response to the post %PostTitle% by %Author% on %Date%.', 'wp-security-audit-log' ) ),
184
  array( 2099, E_NOTICE, __( 'User posted a comment', 'wp-security-audit-log' ), __( '%CommentMsg% on %CommentLink%.', 'wp-security-audit-log' ) ),
 
185
  ),
 
 
 
 
 
 
 
 
 
186
  __( 'Custom Post Types', 'wp-security-audit-log' ) => array(
187
  array( 2003, E_NOTICE, __( 'User modified a draft blog post', 'wp-security-audit-log' ), __( 'Modified the draft post with the %PostTitle%. %EditorLinkPost%.', 'wp-security-audit-log' ) ),
188
  array( 2029, E_NOTICE, __( 'User created a new post with custom post type and saved it as draft', 'wp-security-audit-log' ), __( 'Created a new custom post called %PostTitle% of type %PostType%. %EditorLinkPost%.', 'wp-security-audit-log' ) ),
@@ -211,6 +231,15 @@ function wsaldefaults_wsal_init( WpSecurityAuditLog $wsal ) {
211
  array( 2104, E_NOTICE, __( 'User opened a custom post type in the editor', 'wp-security-audit-log' ), __( 'Opened the custom post %PostTitle% of type %PostType% in the editor. View the post: %EditorLinkPost%.', 'wp-security-audit-log' ) ),
212
  array( 2105, E_NOTICE, __( 'User viewed a custom post type', 'wp-security-audit-log' ), __( 'Viewed the custom post %PostTitle% of type %PostType%. View the post: %PostUrl%.', 'wp-security-audit-log' ) ),
213
  ),
 
 
 
 
 
 
 
 
 
214
  __( 'Pages', 'wp-security-audit-log' ) => array(
215
  array( 2004, E_NOTICE, __( 'User created a new WordPress page and saved it as draft', 'wp-security-audit-log' ), __( 'Created a new page called %PostTitle% and saved it as draft. %EditorLinkPage%.', 'wp-security-audit-log' ) ),
216
  array( 2005, E_NOTICE, __( 'User published a WordPress page', 'wp-security-audit-log' ), __( 'Published a page called %PostTitle%. Page URL is %PostUrl%. %EditorLinkPage%.', 'wp-security-audit-log' ) ),
@@ -242,7 +271,14 @@ function wsaldefaults_wsal_init( WpSecurityAuditLog $wsal ) {
242
  array( 2118, E_NOTICE, __( 'User enabled Comments/Trackbacks and Pingbacks on a draft page', 'wp-security-audit-log' ), __( 'Enabled %Type% on the draft page %PostTitle%. View the page: %PostUrl%.', 'wp-security-audit-log' ) ),
243
  ),
244
  ),
 
 
 
 
245
  __( 'WordPress & Multisite Management', 'wp-security-audit-log' ) => array(
 
 
 
246
  __( 'Database', 'wp-security-audit-log' ) => array(
247
  array( 5010, E_CRITICAL, __( 'Plugin created tables', 'wp-security-audit-log' ), __( 'Plugin %Plugin->Name% created these tables in the database: %TableNames%.', 'wp-security-audit-log' ) ),
248
  array( 5011, E_CRITICAL, __( 'Plugin modified tables structure', 'wp-security-audit-log' ), __( 'Plugin %Plugin->Name% modified the structure of these database tables: %TableNames%.', 'wp-security-audit-log' ) ),
@@ -254,6 +290,10 @@ function wsaldefaults_wsal_init( WpSecurityAuditLog $wsal ) {
254
  array( 5017, E_CRITICAL, __( 'Unknown component modified tables structure', 'wp-security-audit-log' ), __( 'An unknown component modified the structure of these database tables: %TableNames%.', 'wp-security-audit-log' ) ),
255
  array( 5018, E_CRITICAL, __( 'Unknown component deleted tables', 'wp-security-audit-log' ), __( 'An unknown component deleted the following tables from the database: %TableNames%.', 'wp-security-audit-log' ) ),
256
  ),
 
 
 
 
257
  __( 'MultiSite', 'wp-security-audit-log' ) => array(
258
  array( 4008, E_CRITICAL, __( 'User granted Super Admin privileges', 'wp-security-audit-log' ), __( 'Granted Super Admin privileges to %TargetUsername%.', 'wp-security-audit-log' ) ),
259
  array( 4009, E_CRITICAL, __( 'User revoked from Super Admin privileges', 'wp-security-audit-log' ), __( 'Revoked Super Admin privileges from %TargetUsername%.', 'wp-security-audit-log' ) ),
@@ -270,6 +310,10 @@ function wsaldefaults_wsal_init( WpSecurityAuditLog $wsal ) {
270
  array( 5008, E_CRITICAL, __( 'Activated theme on network', 'wp-security-audit-log' ), __( 'Network activated the theme %Theme->Name% installed in %Theme->get_template_directory%.', 'wp-security-audit-log' ) ),
271
  array( 5009, E_CRITICAL, __( 'Deactivated theme from network', 'wp-security-audit-log' ), __( 'Network deactivated the theme %Theme->Name% installed in %Theme->get_template_directory%.', 'wp-security-audit-log' ) ),
272
  ),
 
 
 
 
273
  __( 'Plugins & Themes', 'wp-security-audit-log' ) => array(
274
  array( 5000, E_CRITICAL, __( 'User installed a plugin', 'wp-security-audit-log' ), __( 'Installed the plugin %Plugin->Name% in %Plugin->plugin_dir_path%.', 'wp-security-audit-log' ) ),
275
  array( 5001, E_CRITICAL, __( 'User activated a WordPress plugin', 'wp-security-audit-log' ), __( 'Activated the plugin %PluginData->Name% installed in %PluginFile%.', 'wp-security-audit-log' ) ),
@@ -291,6 +335,10 @@ function wsaldefaults_wsal_init( WpSecurityAuditLog $wsal ) {
291
  array( 2107, E_NOTICE, __( 'A plugin modified a page', 'wp-security-audit-log' ), __( 'Plugin modified the page %PostTitle%. View the page: %EditorLinkPage%.', 'wp-security-audit-log' ) ),
292
  array( 2108, E_NOTICE, __( 'A plugin modified a custom post', 'wp-security-audit-log' ), __( 'Plugin modified the custom post %PostTitle%. View the post: %EditorLinkPost%.', 'wp-security-audit-log' ) ),
293
  ),
 
 
 
 
294
  __( 'System Activity', 'wp-security-audit-log' ) => array(
295
  array( 0000, E_CRITICAL, __( 'Unknown Error', 'wp-security-audit-log' ), __( 'An unexpected error has occurred .', 'wp-security-audit-log' ) ),
296
  array( 0001, E_CRITICAL, __( 'PHP error', 'wp-security-audit-log' ), __( '%Message%.', 'wp-security-audit-log' ) ),
@@ -304,12 +352,16 @@ function wsaldefaults_wsal_init( WpSecurityAuditLog $wsal ) {
304
  array( 6003, E_CRITICAL, __( 'WordPress Administrator Notification email changed', 'wp-security-audit-log' ), __( 'Changed the WordPress administrator notifications email address from %OldEmail% to %NewEmail%.', 'wp-security-audit-log' ) ),
305
  array( 6004, E_CRITICAL, __( 'WordPress was updated', 'wp-security-audit-log' ), __( 'Updated WordPress from version %OldVersion% to %NewVersion%.', 'wp-security-audit-log' ) ),
306
  array( 6005, E_CRITICAL, __( 'User changes the WordPress Permalinks', 'wp-security-audit-log' ), __( 'Changed the WordPress permalinks from %OldPattern% to %NewPattern%.', 'wp-security-audit-log' ) ),
307
- array( 6007, E_CRITICAL, __( 'User requests non-existing pages (404 Error Pages)', 'wp-security-audit-log' ), __( 'Has requested a non existing page (404 Error Pages) %Attempts% %Msg%. %LinkFile%.', 'wp-security-audit-log' ) ),
308
- array( 6023, E_CRITICAL, __( 'Website Visitor User requests non-existing pages (404 Error Pages)', 'wp-security-audit-log' ), __( 'Website Visitor Has requested a non existing page (404 Error Pages) %Attempts% %Msg%. %LinkFile%.', 'wp-security-audit-log' ) ),
309
  array( 6024, E_CRITICAL, __( 'Option WordPress Address (URL) in WordPress settings changed', 'wp-security-audit-log' ), __( 'Changed the WordPress address (URL) from %old_url% to %new_url%.', 'wp-security-audit-log' ) ),
310
  array( 6025, E_CRITICAL, __( 'Option Site Address (URL) in WordPress settings changed', 'wp-security-audit-log' ), __( 'Changed the site address (URL) from %old_url% to %new_url%.', 'wp-security-audit-log' ) ),
311
  array( 9999, E_CRITICAL, __( 'Advertising Add-ons.', 'wp-security-audit-log' ), __( '%PromoName% %PromoMessage%', 'wp-security-audit-log' ) ),
312
  ),
 
 
 
 
313
  __( 'Menus', 'wp-security-audit-log' ) => array(
314
  array( 2078, E_NOTICE, __( 'User created new menu', 'wp-security-audit-log' ), __( 'Created a new menu called %MenuName%.', 'wp-security-audit-log' ) ),
315
  array( 2079, E_WARNING, __( 'User added content to a menu', 'wp-security-audit-log' ), __( 'Added the %ContentType% called %ContentName% to menu %MenuName%.', 'wp-security-audit-log' ) ),
@@ -321,6 +373,10 @@ function wsaldefaults_wsal_init( WpSecurityAuditLog $wsal ) {
321
  array( 2085, E_NOTICE, __( 'User changed order of the objects in a menu', 'wp-security-audit-log' ), __( 'Changed the order of the %ItemName% in menu %MenuName%.', 'wp-security-audit-log' ) ),
322
  array( 2089, E_NOTICE, __( 'User moved objects as a sub-item', 'wp-security-audit-log' ), __( 'Moved %ItemName% as a sub-item of %ParentName% in menu %MenuName%.', 'wp-security-audit-log' ) ),
323
  ),
 
 
 
 
324
  __( 'Widgets', 'wp-security-audit-log' ) => array(
325
  array( 2042, E_CRITICAL, __( 'User added a new widget', 'wp-security-audit-log' ), __( 'Added a new %WidgetName% widget in %Sidebar%.', 'wp-security-audit-log' ) ),
326
  array( 2043, E_WARNING, __( 'User modified a widget', 'wp-security-audit-log' ), __( 'Modified the %WidgetName% widget in %Sidebar%.', 'wp-security-audit-log' ) ),
@@ -328,6 +384,10 @@ function wsaldefaults_wsal_init( WpSecurityAuditLog $wsal ) {
328
  array( 2045, E_NOTICE, __( 'User moved widget', 'wp-security-audit-log' ), __( 'Moved the %WidgetName% widget from %OldSidebar% to %NewSidebar%.', 'wp-security-audit-log' ) ),
329
  array( 2071, E_NOTICE, __( 'User changed widget position', 'wp-security-audit-log' ), __( 'Changed the position of the widget %WidgetName% in sidebar %Sidebar%.', 'wp-security-audit-log' ) ),
330
  ),
 
 
 
 
331
  __( 'Site Settings', 'wp-security-audit-log' ) => array(
332
  array( 6008, E_CRITICAL, __( 'Enabled/Disabled the option Discourage search engines from indexing this site', 'wp-security-audit-log' ), __( '%Status% the option Discourage search engines from indexing this site.', 'wp-security-audit-log' ) ),
333
  array( 6009, E_CRITICAL, __( 'Enabled/Disabled comments on all the website', 'wp-security-audit-log' ), __( '%Status% comments on all the website.', 'wp-security-audit-log' ) ),
@@ -346,7 +406,14 @@ function wsaldefaults_wsal_init( WpSecurityAuditLog $wsal ) {
346
  array( 6022, E_NOTICE, __( 'Started the cron job', 'wp-security-audit-log' ), __( 'The cron job %name% has just started.', 'wp-security-audit-log' ) ),
347
  ),
348
  ),
 
 
 
 
349
  __( 'Users Profiles & Activity', 'wp-security-audit-log' ) => array(
 
 
 
350
  __( 'Other User Activity', 'wp-security-audit-log' ) => array(
351
  array( 1000, E_NOTICE, __( 'User logged in', 'wp-security-audit-log' ), __( 'Successfully logged in.', 'wp-security-audit-log' ) ),
352
  array( 1001, E_NOTICE, __( 'User logged out', 'wp-security-audit-log' ), __( 'Successfully logged out.', 'wp-security-audit-log' ) ),
@@ -359,10 +426,14 @@ function wsaldefaults_wsal_init( WpSecurityAuditLog $wsal ) {
359
  array( 2010, E_NOTICE, __( 'User uploaded file from Uploads directory', 'wp-security-audit-log' ), __( 'Uploaded the file %FileName% in %FilePath%.', 'wp-security-audit-log' ) ),
360
  array( 2011, E_WARNING, __( 'User deleted file from Uploads directory', 'wp-security-audit-log' ), __( 'Deleted the file %FileName% from %FilePath%.', 'wp-security-audit-log' ) ),
361
  ),
 
 
 
 
362
  __( 'User Profiles', 'wp-security-audit-log' ) => array(
363
  array( 4000, E_CRITICAL, __( 'New user was created on WordPress', 'wp-security-audit-log' ), __( 'A new user %NewUserData->Username% was created with role of %NewUserData->Roles%.', 'wp-security-audit-log' ) ),
364
  array( 4001, E_CRITICAL, __( 'User created another WordPress user', 'wp-security-audit-log' ), __( '%UserChanger% created a new user %NewUserData->Username% with the role of %NewUserData->Roles%.', 'wp-security-audit-log' ) ),
365
- array( 4002, E_CRITICAL, __( 'The role of a user was changed by another WordPress user', 'wp-security-audit-log' ), __( 'Changed the role of the user %TargetUsername% from %OldRole% to %NewRole%.', 'wp-security-audit-log' ) ),
366
  array( 4003, E_CRITICAL, __( 'User has changed his or her password', 'wp-security-audit-log' ), __( 'Changed the password.', 'wp-security-audit-log' ) ),
367
  array( 4004, E_CRITICAL, __( 'User changed another user\'s password', 'wp-security-audit-log' ), __( 'Changed the password for the user %TargetUserData->Username% with the role of %TargetUserData->Roles%.', 'wp-security-audit-log' ) ),
368
  array( 4005, E_NOTICE, __( 'User changed his or her email address', 'wp-security-audit-log' ), __( 'Changed the email address from %OldEmail% to %NewEmail%.', 'wp-security-audit-log' ) ),
@@ -377,7 +448,14 @@ function wsaldefaults_wsal_init( WpSecurityAuditLog $wsal ) {
377
  array( 4020, E_WARNING, __( 'User changed the display name for a user', 'wp-security-audit-log' ), __( 'Changed the Display name publicly of user %TargetUsername% from %old_displayname% to %new_displayname%', 'wp-security-audit-log' ) ),
378
  ),
379
  ),
 
 
 
 
380
  __( 'Third Party Support', 'wp-security-audit-log' ) => array(
 
 
 
381
  __( 'BBPress Forum', 'wp-security-audit-log' ) => array(
382
  array( 8000, E_CRITICAL, __( 'User created new forum', 'wp-security-audit-log' ), __( 'Created new forum %ForumName%. Forum URL is %ForumURL%.' . ' %EditorLinkForum%.', 'wp-security-audit-log' ) ),
383
  array( 8001, E_NOTICE, __( 'User changed status of a forum', 'wp-security-audit-log' ), __( 'Changed the status of the forum %ForumName% from %OldStatus% to %NewStatus%.' . ' %EditorLinkForum%.', 'wp-security-audit-log' ) ),
@@ -403,6 +481,10 @@ function wsaldefaults_wsal_init( WpSecurityAuditLog $wsal ) {
403
  array( 8021, E_WARNING, __( 'User restored topic from trash', 'wp-security-audit-log' ), __( 'Restored the topic %TopicName% from trash.' . ' %EditorLinkTopic%.', 'wp-security-audit-log' ) ),
404
  array( 8022, E_NOTICE, __( 'User changed visibility of a topic', 'wp-security-audit-log' ), __( 'Changed the visibility of the topic %TopicName% from %OldVisibility% to %NewVisibility%.' . ' %EditorLinkTopic%.', 'wp-security-audit-log' ) ),
405
  ),
 
 
 
 
406
  __( 'WooCommerce', 'wp-security-audit-log' ) => array(
407
  array( 9000, E_NOTICE, __( 'User created a new product', 'wp-security-audit-log' ), __( 'Created a new product called %ProductTitle% and saved it as draft. View the product: %EditorLinkProduct%.', 'wp-security-audit-log' ) ),
408
  array( 9001, E_NOTICE, __( 'User published a product', 'wp-security-audit-log' ), __( 'Published a product called %ProductTitle%. Product URL is %ProductUrl%. View the product: %EditorLinkProduct%.', 'wp-security-audit-log' ) ),
@@ -423,7 +505,7 @@ function wsaldefaults_wsal_init( WpSecurityAuditLog $wsal ) {
423
  array( 9016, E_WARNING, __( 'User changed type of a price', 'wp-security-audit-log' ), __( 'Changed the %PriceType% of the product %ProductTitle% from %OldPrice% to %NewPrice%. View the product: %EditorLinkProduct%.', 'wp-security-audit-log' ) ),
424
  array( 9017, E_WARNING, __( 'User changed the SKU of a product', 'wp-security-audit-log' ), __( 'Changed the SKU of the product %ProductTitle% from %OldSku% to %NewSku%. View the product: %EditorLinkProduct%.', 'wp-security-audit-log' ) ),
425
  array( 9018, E_CRITICAL, __( 'User changed the stock status of a product', 'wp-security-audit-log' ), __( 'Changed the stock status of the product %ProductTitle% from %OldStatus% to %NewStatus%. View the product: %EditorLinkProduct%.', 'wp-security-audit-log' ) ),
426
- array( 9019, E_WARNING, __( 'User changed the stock quantity', 'wp-security-audit-log' ), __( 'Changed the stock quantity from %OldValue% to %NewValue%. View the product: %EditorLinkProduct%', 'wp-security-audit-log' ) ),
427
  array( 9020, E_WARNING, __( 'User set a product type', 'wp-security-audit-log' ), __( 'Set the product %ProductTitle% as %Type%. View the product: %EditorLinkProduct%.', 'wp-security-audit-log' ) ),
428
  array( 9021, E_WARNING, __( 'User changed the weight of a product', 'wp-security-audit-log' ), __( 'Changed the weight of the product %ProductTitle% from %OldWeight% to %NewWeight%. View the product: %EditorLinkProduct%.', 'wp-security-audit-log' ) ),
429
  array( 9022, E_WARNING, __( 'User changed the dimensions of a product', 'wp-security-audit-log' ), __( 'Changed the %DimensionType% dimensions of the product %ProductTitle% from %OldDimension% to %NewDimension%. View the product: %EditorLinkProduct%.', 'wp-security-audit-log' ) ),
129
  // Create list of default alerts.
130
  $wsal->alerts->RegisterGroup(
131
  array(
132
+ /**
133
+ * Section: Content & Comments
134
+ */
135
  __( 'Content & Comments', 'wp-security-audit-log' ) => array(
136
+ /**
137
+ * Alerts: Content
138
+ */
139
  __( 'Content', 'wp-security-audit-log' ) => array(
140
  array( 2000, E_NOTICE, __( 'User created a new post and saved it as draft', 'wp-security-audit-log' ), __( 'Created a new %PostType% titled %PostTitle% and saved it as draft. %EditorLinkPost%.', 'wp-security-audit-log' ) ),
141
  array( 2001, E_NOTICE, __( 'User published a post', 'wp-security-audit-log' ), __( 'Published a %PostType% titled %PostTitle%. URL is %PostUrl%. %EditorLinkPost%.', 'wp-security-audit-log' ) ),
177
  array( 2124, E_NOTICE, __( 'User changed tag slug', 'wp-security-audit-log' ), __( 'Changed the slug of tag %tag% from %old_slug% to %new_slug%. View the tag: %TagLink%.', 'wp-security-audit-log' ) ),
178
  array( 2125, E_NOTICE, __( 'User changed tag description', 'wp-security-audit-log' ), __( 'Changed the description of the tag %tag% from %old_desc% to %new_desc%. View the tag: %TagLink%.', 'wp-security-audit-log' ) ),
179
  ),
180
+
181
+ /**
182
+ * Alerts: Comments
183
+ */
184
  __( 'Comments', 'wp-security-audit-log' ) => array(
185
  array( 2090, E_NOTICE, __( 'User approved a comment', 'wp-security-audit-log' ), __( 'Approved the comment posted in response to the post %PostTitle% by %Author% on %CommentLink%.', 'wp-security-audit-log' ) ),
186
  array( 2091, E_NOTICE, __( 'User unapproved a comment', 'wp-security-audit-log' ), __( 'Unapproved the comment posted in response to the post %PostTitle% by %Author% on %CommentLink%.', 'wp-security-audit-log' ) ),
192
  array( 2097, E_NOTICE, __( 'User restored a comment from the trash', 'wp-security-audit-log' ), __( 'Restored the comment posted in response to the post %PostTitle% by %Author% on %CommentLink% from the trash.', 'wp-security-audit-log' ) ),
193
  array( 2098, E_NOTICE, __( 'User permanently deleted a comment', 'wp-security-audit-log' ), __( 'Permanently deleted the comment posted in response to the post %PostTitle% by %Author% on %Date%.', 'wp-security-audit-log' ) ),
194
  array( 2099, E_NOTICE, __( 'User posted a comment', 'wp-security-audit-log' ), __( '%CommentMsg% on %CommentLink%.', 'wp-security-audit-log' ) ),
195
+ array( 2126, E_NOTICE, __( 'Visitor posted a comment', 'wp-security-audit-log' ), __( '%CommentMsg% on %CommentLink%.', 'wp-security-audit-log' ) ),
196
  ),
197
+
198
+ /**
199
+ * Alerts: Custom Post Types
200
+ *
201
+ * IMPORTANT: These alerts should not be removed from here
202
+ * for backwards compatibilty.
203
+ *
204
+ * @deprecated 3.1.0
205
+ */
206
  __( 'Custom Post Types', 'wp-security-audit-log' ) => array(
207
  array( 2003, E_NOTICE, __( 'User modified a draft blog post', 'wp-security-audit-log' ), __( 'Modified the draft post with the %PostTitle%. %EditorLinkPost%.', 'wp-security-audit-log' ) ),
208
  array( 2029, E_NOTICE, __( 'User created a new post with custom post type and saved it as draft', 'wp-security-audit-log' ), __( 'Created a new custom post called %PostTitle% of type %PostType%. %EditorLinkPost%.', 'wp-security-audit-log' ) ),
231
  array( 2104, E_NOTICE, __( 'User opened a custom post type in the editor', 'wp-security-audit-log' ), __( 'Opened the custom post %PostTitle% of type %PostType% in the editor. View the post: %EditorLinkPost%.', 'wp-security-audit-log' ) ),
232
  array( 2105, E_NOTICE, __( 'User viewed a custom post type', 'wp-security-audit-log' ), __( 'Viewed the custom post %PostTitle% of type %PostType%. View the post: %PostUrl%.', 'wp-security-audit-log' ) ),
233
  ),
234
+
235
+ /**
236
+ * Alerts: Pages
237
+ *
238
+ * IMPORTANT: These alerts should not be removed from here
239
+ * for backwards compatibilty.
240
+ *
241
+ * @deprecated 3.1.0
242
+ */
243
  __( 'Pages', 'wp-security-audit-log' ) => array(
244
  array( 2004, E_NOTICE, __( 'User created a new WordPress page and saved it as draft', 'wp-security-audit-log' ), __( 'Created a new page called %PostTitle% and saved it as draft. %EditorLinkPage%.', 'wp-security-audit-log' ) ),
245
  array( 2005, E_NOTICE, __( 'User published a WordPress page', 'wp-security-audit-log' ), __( 'Published a page called %PostTitle%. Page URL is %PostUrl%. %EditorLinkPage%.', 'wp-security-audit-log' ) ),
271
  array( 2118, E_NOTICE, __( 'User enabled Comments/Trackbacks and Pingbacks on a draft page', 'wp-security-audit-log' ), __( 'Enabled %Type% on the draft page %PostTitle%. View the page: %PostUrl%.', 'wp-security-audit-log' ) ),
272
  ),
273
  ),
274
+
275
+ /**
276
+ * Section: WordPress & Multisite Management
277
+ */
278
  __( 'WordPress & Multisite Management', 'wp-security-audit-log' ) => array(
279
+ /**
280
+ * Alerts: Database
281
+ */
282
  __( 'Database', 'wp-security-audit-log' ) => array(
283
  array( 5010, E_CRITICAL, __( 'Plugin created tables', 'wp-security-audit-log' ), __( 'Plugin %Plugin->Name% created these tables in the database: %TableNames%.', 'wp-security-audit-log' ) ),
284
  array( 5011, E_CRITICAL, __( 'Plugin modified tables structure', 'wp-security-audit-log' ), __( 'Plugin %Plugin->Name% modified the structure of these database tables: %TableNames%.', 'wp-security-audit-log' ) ),
290
  array( 5017, E_CRITICAL, __( 'Unknown component modified tables structure', 'wp-security-audit-log' ), __( 'An unknown component modified the structure of these database tables: %TableNames%.', 'wp-security-audit-log' ) ),
291
  array( 5018, E_CRITICAL, __( 'Unknown component deleted tables', 'wp-security-audit-log' ), __( 'An unknown component deleted the following tables from the database: %TableNames%.', 'wp-security-audit-log' ) ),
292
  ),
293
+
294
+ /**
295
+ * Alerts: MultiSite
296
+ */
297
  __( 'MultiSite', 'wp-security-audit-log' ) => array(
298
  array( 4008, E_CRITICAL, __( 'User granted Super Admin privileges', 'wp-security-audit-log' ), __( 'Granted Super Admin privileges to %TargetUsername%.', 'wp-security-audit-log' ) ),
299
  array( 4009, E_CRITICAL, __( 'User revoked from Super Admin privileges', 'wp-security-audit-log' ), __( 'Revoked Super Admin privileges from %TargetUsername%.', 'wp-security-audit-log' ) ),
310
  array( 5008, E_CRITICAL, __( 'Activated theme on network', 'wp-security-audit-log' ), __( 'Network activated the theme %Theme->Name% installed in %Theme->get_template_directory%.', 'wp-security-audit-log' ) ),
311
  array( 5009, E_CRITICAL, __( 'Deactivated theme from network', 'wp-security-audit-log' ), __( 'Network deactivated the theme %Theme->Name% installed in %Theme->get_template_directory%.', 'wp-security-audit-log' ) ),
312
  ),
313
+
314
+ /**
315
+ * Alerts: Plugins & Themes
316
+ */
317
  __( 'Plugins & Themes', 'wp-security-audit-log' ) => array(
318
  array( 5000, E_CRITICAL, __( 'User installed a plugin', 'wp-security-audit-log' ), __( 'Installed the plugin %Plugin->Name% in %Plugin->plugin_dir_path%.', 'wp-security-audit-log' ) ),
319
  array( 5001, E_CRITICAL, __( 'User activated a WordPress plugin', 'wp-security-audit-log' ), __( 'Activated the plugin %PluginData->Name% installed in %PluginFile%.', 'wp-security-audit-log' ) ),
335
  array( 2107, E_NOTICE, __( 'A plugin modified a page', 'wp-security-audit-log' ), __( 'Plugin modified the page %PostTitle%. View the page: %EditorLinkPage%.', 'wp-security-audit-log' ) ),
336
  array( 2108, E_NOTICE, __( 'A plugin modified a custom post', 'wp-security-audit-log' ), __( 'Plugin modified the custom post %PostTitle%. View the post: %EditorLinkPost%.', 'wp-security-audit-log' ) ),
337
  ),
338
+
339
+ /**
340
+ * Alerts: System Activity
341
+ */
342
  __( 'System Activity', 'wp-security-audit-log' ) => array(
343
  array( 0000, E_CRITICAL, __( 'Unknown Error', 'wp-security-audit-log' ), __( 'An unexpected error has occurred .', 'wp-security-audit-log' ) ),
344
  array( 0001, E_CRITICAL, __( 'PHP error', 'wp-security-audit-log' ), __( '%Message%.', 'wp-security-audit-log' ) ),
352
  array( 6003, E_CRITICAL, __( 'WordPress Administrator Notification email changed', 'wp-security-audit-log' ), __( 'Changed the WordPress administrator notifications email address from %OldEmail% to %NewEmail%.', 'wp-security-audit-log' ) ),
353
  array( 6004, E_CRITICAL, __( 'WordPress was updated', 'wp-security-audit-log' ), __( 'Updated WordPress from version %OldVersion% to %NewVersion%.', 'wp-security-audit-log' ) ),
354
  array( 6005, E_CRITICAL, __( 'User changes the WordPress Permalinks', 'wp-security-audit-log' ), __( 'Changed the WordPress permalinks from %OldPattern% to %NewPattern%.', 'wp-security-audit-log' ) ),
355
+ array( 6007, E_NOTICE, __( 'User requests non-existing pages (404 Error Pages)', 'wp-security-audit-log' ), __( 'Has requested a non existing page (404 Error Pages) %Attempts% %Msg%. %LinkFile%.', 'wp-security-audit-log' ) ),
356
+ array( 6023, E_NOTICE, __( 'Website Visitor User requests non-existing pages (404 Error Pages)', 'wp-security-audit-log' ), __( 'Website Visitor Has requested a non existing page (404 Error Pages) %Attempts% %Msg%. %LinkFile%.', 'wp-security-audit-log' ) ),
357
  array( 6024, E_CRITICAL, __( 'Option WordPress Address (URL) in WordPress settings changed', 'wp-security-audit-log' ), __( 'Changed the WordPress address (URL) from %old_url% to %new_url%.', 'wp-security-audit-log' ) ),
358
  array( 6025, E_CRITICAL, __( 'Option Site Address (URL) in WordPress settings changed', 'wp-security-audit-log' ), __( 'Changed the site address (URL) from %old_url% to %new_url%.', 'wp-security-audit-log' ) ),
359
  array( 9999, E_CRITICAL, __( 'Advertising Add-ons.', 'wp-security-audit-log' ), __( '%PromoName% %PromoMessage%', 'wp-security-audit-log' ) ),
360
  ),
361
+
362
+ /**
363
+ * Alerts: Menus
364
+ */
365
  __( 'Menus', 'wp-security-audit-log' ) => array(
366
  array( 2078, E_NOTICE, __( 'User created new menu', 'wp-security-audit-log' ), __( 'Created a new menu called %MenuName%.', 'wp-security-audit-log' ) ),
367
  array( 2079, E_WARNING, __( 'User added content to a menu', 'wp-security-audit-log' ), __( 'Added the %ContentType% called %ContentName% to menu %MenuName%.', 'wp-security-audit-log' ) ),
373
  array( 2085, E_NOTICE, __( 'User changed order of the objects in a menu', 'wp-security-audit-log' ), __( 'Changed the order of the %ItemName% in menu %MenuName%.', 'wp-security-audit-log' ) ),
374
  array( 2089, E_NOTICE, __( 'User moved objects as a sub-item', 'wp-security-audit-log' ), __( 'Moved %ItemName% as a sub-item of %ParentName% in menu %MenuName%.', 'wp-security-audit-log' ) ),
375
  ),
376
+
377
+ /**
378
+ * Alerts: Widgets
379
+ */
380
  __( 'Widgets', 'wp-security-audit-log' ) => array(
381
  array( 2042, E_CRITICAL, __( 'User added a new widget', 'wp-security-audit-log' ), __( 'Added a new %WidgetName% widget in %Sidebar%.', 'wp-security-audit-log' ) ),
382
  array( 2043, E_WARNING, __( 'User modified a widget', 'wp-security-audit-log' ), __( 'Modified the %WidgetName% widget in %Sidebar%.', 'wp-security-audit-log' ) ),
384
  array( 2045, E_NOTICE, __( 'User moved widget', 'wp-security-audit-log' ), __( 'Moved the %WidgetName% widget from %OldSidebar% to %NewSidebar%.', 'wp-security-audit-log' ) ),
385
  array( 2071, E_NOTICE, __( 'User changed widget position', 'wp-security-audit-log' ), __( 'Changed the position of the widget %WidgetName% in sidebar %Sidebar%.', 'wp-security-audit-log' ) ),
386
  ),
387
+
388
+ /**
389
+ * Alerts: Site Settings
390
+ */
391
  __( 'Site Settings', 'wp-security-audit-log' ) => array(
392
  array( 6008, E_CRITICAL, __( 'Enabled/Disabled the option Discourage search engines from indexing this site', 'wp-security-audit-log' ), __( '%Status% the option Discourage search engines from indexing this site.', 'wp-security-audit-log' ) ),
393
  array( 6009, E_CRITICAL, __( 'Enabled/Disabled comments on all the website', 'wp-security-audit-log' ), __( '%Status% comments on all the website.', 'wp-security-audit-log' ) ),
406
  array( 6022, E_NOTICE, __( 'Started the cron job', 'wp-security-audit-log' ), __( 'The cron job %name% has just started.', 'wp-security-audit-log' ) ),
407
  ),
408
  ),
409
+
410
+ /**
411
+ * Section: Users Profiles & Activity
412
+ */
413
  __( 'Users Profiles & Activity', 'wp-security-audit-log' ) => array(
414
+ /**
415
+ * Alerts: Other User Activity
416
+ */
417
  __( 'Other User Activity', 'wp-security-audit-log' ) => array(
418
  array( 1000, E_NOTICE, __( 'User logged in', 'wp-security-audit-log' ), __( 'Successfully logged in.', 'wp-security-audit-log' ) ),
419
  array( 1001, E_NOTICE, __( 'User logged out', 'wp-security-audit-log' ), __( 'Successfully logged out.', 'wp-security-audit-log' ) ),
426
  array( 2010, E_NOTICE, __( 'User uploaded file from Uploads directory', 'wp-security-audit-log' ), __( 'Uploaded the file %FileName% in %FilePath%.', 'wp-security-audit-log' ) ),
427
  array( 2011, E_WARNING, __( 'User deleted file from Uploads directory', 'wp-security-audit-log' ), __( 'Deleted the file %FileName% from %FilePath%.', 'wp-security-audit-log' ) ),
428
  ),
429
+
430
+ /**
431
+ * Alerts: User Profiles
432
+ */
433
  __( 'User Profiles', 'wp-security-audit-log' ) => array(
434
  array( 4000, E_CRITICAL, __( 'New user was created on WordPress', 'wp-security-audit-log' ), __( 'A new user %NewUserData->Username% was created with role of %NewUserData->Roles%.', 'wp-security-audit-log' ) ),
435
  array( 4001, E_CRITICAL, __( 'User created another WordPress user', 'wp-security-audit-log' ), __( '%UserChanger% created a new user %NewUserData->Username% with the role of %NewUserData->Roles%.', 'wp-security-audit-log' ) ),
436
+ array( 4002, E_CRITICAL, __( 'The role of a user was changed by another WordPress user', 'wp-security-audit-log' ), __( 'Changed the role of the user %TargetUsername% from %OldRole% to %NewRole%%multisite_text%.', 'wp-security-audit-log' ) ),
437
  array( 4003, E_CRITICAL, __( 'User has changed his or her password', 'wp-security-audit-log' ), __( 'Changed the password.', 'wp-security-audit-log' ) ),
438
  array( 4004, E_CRITICAL, __( 'User changed another user\'s password', 'wp-security-audit-log' ), __( 'Changed the password for the user %TargetUserData->Username% with the role of %TargetUserData->Roles%.', 'wp-security-audit-log' ) ),
439
  array( 4005, E_NOTICE, __( 'User changed his or her email address', 'wp-security-audit-log' ), __( 'Changed the email address from %OldEmail% to %NewEmail%.', 'wp-security-audit-log' ) ),
448
  array( 4020, E_WARNING, __( 'User changed the display name for a user', 'wp-security-audit-log' ), __( 'Changed the Display name publicly of user %TargetUsername% from %old_displayname% to %new_displayname%', 'wp-security-audit-log' ) ),
449
  ),
450
  ),
451
+
452
+ /**
453
+ * Section: Third Party Support
454
+ */
455
  __( 'Third Party Support', 'wp-security-audit-log' ) => array(
456
+ /**
457
+ * Alerts: BBPress Forum
458
+ */
459
  __( 'BBPress Forum', 'wp-security-audit-log' ) => array(
460
  array( 8000, E_CRITICAL, __( 'User created new forum', 'wp-security-audit-log' ), __( 'Created new forum %ForumName%. Forum URL is %ForumURL%.' . ' %EditorLinkForum%.', 'wp-security-audit-log' ) ),
461
  array( 8001, E_NOTICE, __( 'User changed status of a forum', 'wp-security-audit-log' ), __( 'Changed the status of the forum %ForumName% from %OldStatus% to %NewStatus%.' . ' %EditorLinkForum%.', 'wp-security-audit-log' ) ),
481
  array( 8021, E_WARNING, __( 'User restored topic from trash', 'wp-security-audit-log' ), __( 'Restored the topic %TopicName% from trash.' . ' %EditorLinkTopic%.', 'wp-security-audit-log' ) ),
482
  array( 8022, E_NOTICE, __( 'User changed visibility of a topic', 'wp-security-audit-log' ), __( 'Changed the visibility of the topic %TopicName% from %OldVisibility% to %NewVisibility%.' . ' %EditorLinkTopic%.', 'wp-security-audit-log' ) ),
483
  ),
484
+
485
+ /**
486
+ * Alerts: WooCommerce
487
+ */
488
  __( 'WooCommerce', 'wp-security-audit-log' ) => array(
489
  array( 9000, E_NOTICE, __( 'User created a new product', 'wp-security-audit-log' ), __( 'Created a new product called %ProductTitle% and saved it as draft. View the product: %EditorLinkProduct%.', 'wp-security-audit-log' ) ),
490
  array( 9001, E_NOTICE, __( 'User published a product', 'wp-security-audit-log' ), __( 'Published a product called %ProductTitle%. Product URL is %ProductUrl%. View the product: %EditorLinkProduct%.', 'wp-security-audit-log' ) ),
505
  array( 9016, E_WARNING, __( 'User changed type of a price', 'wp-security-audit-log' ), __( 'Changed the %PriceType% of the product %ProductTitle% from %OldPrice% to %NewPrice%. View the product: %EditorLinkProduct%.', 'wp-security-audit-log' ) ),
506
  array( 9017, E_WARNING, __( 'User changed the SKU of a product', 'wp-security-audit-log' ), __( 'Changed the SKU of the product %ProductTitle% from %OldSku% to %NewSku%. View the product: %EditorLinkProduct%.', 'wp-security-audit-log' ) ),
507
  array( 9018, E_CRITICAL, __( 'User changed the stock status of a product', 'wp-security-audit-log' ), __( 'Changed the stock status of the product %ProductTitle% from %OldStatus% to %NewStatus%. View the product: %EditorLinkProduct%.', 'wp-security-audit-log' ) ),
508
+ array( 9019, E_WARNING, __( 'User changed the stock quantity', 'wp-security-audit-log' ), __( 'Changed the stock quantity from %OldValue% to %NewValue% of the product %ProductTitle%. View the product: %EditorLinkProduct%', 'wp-security-audit-log' ) ),
509
  array( 9020, E_WARNING, __( 'User set a product type', 'wp-security-audit-log' ), __( 'Set the product %ProductTitle% as %Type%. View the product: %EditorLinkProduct%.', 'wp-security-audit-log' ) ),
510
  array( 9021, E_WARNING, __( 'User changed the weight of a product', 'wp-security-audit-log' ), __( 'Changed the weight of the product %ProductTitle% from %OldWeight% to %NewWeight%. View the product: %EditorLinkProduct%.', 'wp-security-audit-log' ) ),
511
  array( 9022, E_WARNING, __( 'User changed the dimensions of a product', 'wp-security-audit-log' ), __( 'Changed the %DimensionType% dimensions of the product %ProductTitle% from %OldDimension% to %NewDimension%. View the product: %EditorLinkProduct%.', 'wp-security-audit-log' ) ),
readme.txt CHANGED
@@ -6,7 +6,7 @@ License URI: http://www.gnu.org/licenses/gpl.html
6
  Tags: wordpress security plugin, wordpress security audit log, audit log, event log wordpress, wordpress user tracking, wordpress activity log, wordpress audit, security event log, audit trail, wordpress security monitor, wordpress admin, wordpress admin monitoring, user activity, admin, multisite, dashboard, notification, wordpress monitoring, email notification, wordpress email alerts, tracking, user tracking, user activity report, wordpress audit trail
7
  Requires at least: 3.6
8
  Tested up to: 4.9.4
9
- Stable tag: 3.1.0
10
  Requires PHP: 5.3
11
 
12
  Keep an audit trail of all changes on your WordPress - ensure productivity & thwart attacks with the most comprehensive audit trail plugin.
@@ -179,29 +179,23 @@ Please refer to our [Support & Documentation pages](https://www.wpsecurityauditl
179
 
180
  == Changelog ==
181
 
182
- = 3.1.0(2018-02-28)
 
 
 
183
 
184
  * **New Features**
185
- * Added Post Status and Post Type in alerts.
186
- * Consolidated all Posts / Pages / Custom Post alerts [Read more](https://www.wpsecurityauditlog.com/wordpress-user-monitoring-plugin-releases/version-3-1-post-type-status-alerts/).
187
- * Added Post Status and Post Type filters in search.
188
- * Added Post Status and Post Type criteria in Email Notifications trigger builder.
189
- * Added Post Status and Post Type criteria in Reports.
190
- * Fallback system for display names - when user does not have first and last name, the username will be used.
191
 
192
  * **Improvements**
193
- * Improved the priority of our hooks so logins from custom forms are captured (e.g. better support for Restrict Content PRO and similar plugins).
194
- * Improved the tabs / UI of the Enable/Disable Alerts section.
195
- * Changed the column Type to Severity in Audit Log viewer.
196
- * Better handling of errors and variables during plugin activation.
197
- * Consolidated Post ID, Page ID and Custom Post ID in just Post ID in Email notifications trigger builder.
198
- * Improved the look & feel of the login page notification (GDPR compliance).
199
- * Improved the UI and queries used for the Users Sessions management.
200
- * Added the IP address requesting the non-existing page in the 404 log files.
201
- * Users can now specify the number of 404 and failed login alerts before being alerted in the built-in alerts section.
202
-
203
- * **Bug Fixes**
204
- * Removed a Disable All Logging option from plugin settings - was redundant.
205
- * Improved handling of global arrays [support ticket](https://wordpress.org/support/topic/notice-undefined-index-script_name-sensors-content-php-on-line-1499/#post-10004967)
206
 
207
  Refer to the [WP Security Audit Log change log](https://www.wpsecurityauditlog.com/plugin-change-log/) page for the complete change log.
6
  Tags: wordpress security plugin, wordpress security audit log, audit log, event log wordpress, wordpress user tracking, wordpress activity log, wordpress audit, security event log, audit trail, wordpress security monitor, wordpress admin, wordpress admin monitoring, user activity, admin, multisite, dashboard, notification, wordpress monitoring, email notification, wordpress email alerts, tracking, user tracking, user activity report, wordpress audit trail
7
  Requires at least: 3.6
8
  Tested up to: 4.9.4
9
+ Stable tag: 3.1.1
10
  Requires PHP: 5.3
11
 
12
  Keep an audit trail of all changes on your WordPress - ensure productivity & thwart attacks with the most comprehensive audit trail plugin.
179
 
180
  == Changelog ==
181
 
182
+ = 3.1.1(2018-03-14) =
183
+
184
+ * **New WordPress Activity Log Alert**
185
+ * Alert 2126: Website visitor posted a comment (segregated from alert 2099 which was used for both logged in users and website visitors).
186
 
187
  * **New Features**
188
+ * Logging of user role change at WordPress multisite network level.
189
+ * New site selection menu for opting-in to sending diagnostic data or activating licenses on WordPress multisite network installations.
190
+ * New account page for installs on multisite network.
 
 
 
191
 
192
  * **Improvements**
193
+ * Logging of posting of comments from logged in users and website visitors now reported by different alerts.
194
+ * Improved the logging of multiple post changes that were done at the same time on a post / page / post with custom post type - previously only the last change was being reported.
195
+ * Post Type selection menu in Email Notifications is not automatically populated on multisite network install so users can specify their custom post type.
196
+ * Changed severity of alerts 6007 and 6023 (404 errors) from High to Notification.
197
+ * Improved the plugin menu node for sites on multisite network (added messages on nodes users do not have access to, removed nodes that admins on sites should not have access to).
198
+ * Added responsiveness to the Archive Now and Mirror Now buttons in the integration tools.
199
+ * Added product name in alert 9019 (when product stock quantity is updated in WooCommerce).
 
 
 
 
 
 
200
 
201
  Refer to the [WP Security Audit Log change log](https://www.wpsecurityauditlog.com/plugin-change-log/) page for the complete change log.
sdk/freemius/assets/css/admin/account.css CHANGED
@@ -1 +1 @@
1
- #fs_account .postbox,#fs_account .widefat{max-width:700px}#fs_account h3{font-size:1.3em;padding:12px 15px;margin:0 0 12px 0;line-height:1.4;border-bottom:1px solid #F1F1F1}#fs_account h3 .dashicons{width:26px;height:26px;font-size:1.3em}#fs_account i.dashicons{font-size:1.2em;height:1.2em;width:1.2em}#fs_account .button i.dashicons{vertical-align:middle}#fs_account .fs-header-actions{position:absolute;top:17px;right:15px;font-size:0.9em}#fs_account .fs-header-actions ul{margin:0}#fs_account .fs-header-actions li{float:left}#fs_account .fs-header-actions li form{display:inline-block}#fs_account .fs-header-actions li a{text-decoration:none}#fs_account_details .button-group{float:right}.rtl #fs_account .fs-header-actions{left:15px;right:auto}.fs-key-value-table{width:100%}.fs-key-value-table form{display:inline-block}.fs-key-value-table tr td:first-child{text-align:right}.fs-key-value-table tr td:first-child nobr{font-weight:bold}.fs-key-value-table tr td:first-child form{display:block}.fs-key-value-table tr td.fs-right{text-align:right}.fs-key-value-table tr.fs-odd{background:#ebebeb}.fs-key-value-table td,.fs-key-value-table th{padding:10px}.fs-key-value-table code{line-height:28px}.fs-key-value-table var,.fs-key-value-table code,.fs-key-value-table input[type="text"]{color:#0073AA;font-size:16px;background:none}.fs-key-value-table input[type="text"]{width:100%;font-weight:bold}label.fs-tag{background:#ffba00;color:#fff;display:inline-block;border-radius:3px;padding:5px;font-size:11px;line-height:11px;vertical-align:baseline}label.fs-tag.fs-warn{background:#ffba00}label.fs-tag.fs-success{background:#46b450}label.fs-tag.fs-error{background:#dc3232}#fs_addons h3{border:none;margin-bottom:0;padding:4px 5px}#fs_addons td{vertical-align:middle}#fs_addons thead{white-space:nowrap}#fs_addons td:first-child,#fs_addons th:first-child{text-align:left;font-weight:bold}#fs_addons td:last-child,#fs_addons th:last-child{text-align:right}#fs_addons th{font-weight:bold}#fs_billing_address{width:100%}#fs_billing_address tr td{width:50%;padding:5px}#fs_billing_address tr:first-of-type td{padding-top:0}#fs_billing_address span{font-weight:bold}#fs_billing_address input,#fs_billing_address select{display:block;width:100%;margin-top:5px}#fs_billing_address input::-moz-placeholder,#fs_billing_address select::-moz-placeholder{color:transparent;opacity:1}#fs_billing_address input:-ms-input-placeholder,#fs_billing_address select:-ms-input-placeholder{color:transparent}#fs_billing_address input::-webkit-input-placeholder,#fs_billing_address select::-webkit-input-placeholder{color:transparent}#fs_billing_address input.fs-read-mode,#fs_billing_address select.fs-read-mode{border-color:transparent;color:#777;border-bottom:1px dashed #ccc;padding-left:0;background:none}#fs_billing_address.fs-read-mode td span{display:none}#fs_billing_address.fs-read-mode input,#fs_billing_address.fs-read-mode select{border-color:transparent;color:#777;border-bottom:1px dashed #ccc;padding-left:0;background:none}#fs_billing_address.fs-read-mode input::-moz-placeholder,#fs_billing_address.fs-read-mode select::-moz-placeholder{color:#ccc;opacity:1}#fs_billing_address.fs-read-mode input:-ms-input-placeholder,#fs_billing_address.fs-read-mode select:-ms-input-placeholder{color:#ccc}#fs_billing_address.fs-read-mode input::-webkit-input-placeholder,#fs_billing_address.fs-read-mode select::-webkit-input-placeholder{color:#ccc}#fs_billing_address button{display:block;width:100%}
1
+ #fs_account .postbox,#fs_account .widefat{max-width:700px}#fs_account h3{font-size:1.3em;padding:12px 15px;margin:0 0 12px 0;line-height:1.4;border-bottom:1px solid #F1F1F1}#fs_account h3 .dashicons{width:26px;height:26px;font-size:1.3em}#fs_account i.dashicons{font-size:1.2em;height:1.2em;width:1.2em}#fs_account .dashicons{vertical-align:middle}#fs_account .fs-header-actions{position:absolute;top:17px;right:15px;font-size:0.9em}#fs_account .fs-header-actions ul{margin:0}#fs_account .fs-header-actions li{float:left}#fs_account .fs-header-actions li form{display:inline-block}#fs_account .fs-header-actions li a{text-decoration:none}#fs_account_details .button-group{float:right}.rtl #fs_account .fs-header-actions{left:15px;right:auto}.fs-key-value-table{width:100%}.fs-key-value-table form{display:inline-block}.fs-key-value-table tr td:first-child{text-align:right}.fs-key-value-table tr td:first-child nobr{font-weight:bold}.fs-key-value-table tr td:first-child form{display:block}.fs-key-value-table tr td.fs-right{text-align:right}.fs-key-value-table tr.fs-odd{background:#ebebeb}.fs-key-value-table td,.fs-key-value-table th{padding:10px}.fs-key-value-table code{line-height:28px}.fs-key-value-table var,.fs-key-value-table code,.fs-key-value-table input[type="text"]{color:#0073AA;font-size:16px;background:none}.fs-key-value-table input[type="text"]{width:100%;font-weight:bold}label.fs-tag{background:#ffba00;color:#fff;display:inline-block;border-radius:3px;padding:5px;font-size:11px;line-height:11px;vertical-align:baseline}label.fs-tag.fs-warn{background:#ffba00}label.fs-tag.fs-success{background:#46b450}label.fs-tag.fs-error{background:#dc3232}#fs_sites .fs-scrollable-table .fs-table-body{max-height:200px;overflow:auto;border:1px solid #e5e5e5}#fs_sites .fs-scrollable-table .fs-table-body>table.widefat{border:none !important}#fs_sites .fs-scrollable-table .fs-main-column{width:100%}#fs_sites .fs-scrollable-table .fs-site-details td:first-of-type{text-align:right;color:grey;width:1px}#fs_sites .fs-scrollable-table .fs-site-details td:last-of-type{text-align:right}#fs_sites .fs-scrollable-table .fs-install-details table tr td{width:1px;white-space:nowrap}#fs_sites .fs-scrollable-table .fs-install-details table tr td:last-of-type{width:auto}#fs_addons h3{border:none;margin-bottom:0;padding:4px 5px}#fs_addons td{vertical-align:middle}#fs_addons thead{white-space:nowrap}#fs_addons td:first-child,#fs_addons th:first-child{text-align:left;font-weight:bold}#fs_addons td:last-child,#fs_addons th:last-child{text-align:right}#fs_addons th{font-weight:bold}#fs_billing_address{width:100%}#fs_billing_address tr td{width:50%;padding:5px}#fs_billing_address tr:first-of-type td{padding-top:0}#fs_billing_address span{font-weight:bold}#fs_billing_address input,#fs_billing_address select{display:block;width:100%;margin-top:5px}#fs_billing_address input::-moz-placeholder,#fs_billing_address select::-moz-placeholder{color:transparent;opacity:1}#fs_billing_address input:-ms-input-placeholder,#fs_billing_address select:-ms-input-placeholder{color:transparent}#fs_billing_address input::-webkit-input-placeholder,#fs_billing_address select::-webkit-input-placeholder{color:transparent}#fs_billing_address input.fs-read-mode,#fs_billing_address select.fs-read-mode{border-color:transparent;color:#777;border-bottom:1px dashed #ccc;padding-left:0;background:none}#fs_billing_address.fs-read-mode td span{display:none}#fs_billing_address.fs-read-mode input,#fs_billing_address.fs-read-mode select{border-color:transparent;color:#777;border-bottom:1px dashed #ccc;padding-left:0;background:none}#fs_billing_address.fs-read-mode input::-moz-placeholder,#fs_billing_address.fs-read-mode select::-moz-placeholder{color:#ccc;opacity:1}#fs_billing_address.fs-read-mode input:-ms-input-placeholder,#fs_billing_address.fs-read-mode select:-ms-input-placeholder{color:#ccc}#fs_billing_address.fs-read-mode input::-webkit-input-placeholder,#fs_billing_address.fs-read-mode select::-webkit-input-placeholder{color:#ccc}#fs_billing_address button{display:block;width:100%}
sdk/freemius/assets/css/admin/affiliation.css CHANGED
@@ -1 +1 @@
1
- @charset "UTF-8";#fs_affiliation_content_wrapper #messages{margin-top:25px}#fs_affiliation_content_wrapper h3{font-size:24px;padding:0;margin-left:0}#fs_affiliation_content_wrapper ul li{box-sizing:border-box;list-style-type:none}#fs_affiliation_content_wrapper ul li:before{content:'✓';margin-right:10px;font-weight:bold}#fs_affiliation_content_wrapper p:not(.description),#fs_affiliation_content_wrapper li,#fs_affiliation_content_wrapper label{font-size:16px !important;line-height:26px !important}#fs_affiliation_content_wrapper .button{margin-top:20px;margin-bottom:7px;line-height:35px;height:40px;font-size:16px}#fs_affiliation_content_wrapper .button#cancel_button{margin-right:5px}#fs_affiliation_content_wrapper form .input-container{margin-bottom:15px}#fs_affiliation_content_wrapper form .input-container .input-label{font-weight:bold;display:block;width:100%}#fs_affiliation_content_wrapper form .input-container.input-container-text label,#fs_affiliation_content_wrapper form .input-container.input-container-text input,#fs_affiliation_content_wrapper form .input-container.input-container-text textarea{display:block}#fs_affiliation_content_wrapper form .input-container #add_domain,#fs_affiliation_content_wrapper form .input-container .remove-domain{text-decoration:none;display:inline-block;margin-top:3px}#fs_affiliation_content_wrapper form .input-container #add_domain:focus,#fs_affiliation_content_wrapper form .input-container .remove-domain:focus{box-shadow:none}#fs_affiliation_content_wrapper form .input-container #add_domain.disabled,#fs_affiliation_content_wrapper form .input-container .remove-domain.disabled{color:#aaa;cursor:default}#fs_affiliation_content_wrapper form #extra_domains_container .description{margin-top:0;position:relative;top:-4px}#fs_affiliation_content_wrapper form #extra_domains_container .extra-domain-input-container{margin-bottom:15px}#fs_affiliation_content_wrapper form #extra_domains_container .extra-domain-input-container .domain{display:inline-block;margin-right:5px}#fs_affiliation_content_wrapper form #extra_domains_container .extra-domain-input-container .domain:last-of-type{margin-bottom:0}
1
+ @charset "UTF-8";#fs_affiliation_content_wrapper #messages{margin-top:25px}#fs_affiliation_content_wrapper h3{font-size:24px;padding:0;margin-left:0}#fs_affiliation_content_wrapper ul li{-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box;list-style-type:none}#fs_affiliation_content_wrapper ul li:before{content:'✓';margin-right:10px;font-weight:bold}#fs_affiliation_content_wrapper p:not(.description),#fs_affiliation_content_wrapper li,#fs_affiliation_content_wrapper label{font-size:16px !important;line-height:26px !important}#fs_affiliation_content_wrapper .button{margin-top:20px;margin-bottom:7px;line-height:35px;height:40px;font-size:16px}#fs_affiliation_content_wrapper .button#cancel_button{margin-right:5px}#fs_affiliation_content_wrapper form .input-container{margin-bottom:15px}#fs_affiliation_content_wrapper form .input-container .input-label{font-weight:bold;display:block;width:100%}#fs_affiliation_content_wrapper form .input-container.input-container-text label,#fs_affiliation_content_wrapper form .input-container.input-container-text input,#fs_affiliation_content_wrapper form .input-container.input-container-text textarea{display:block}#fs_affiliation_content_wrapper form .input-container #add_domain,#fs_affiliation_content_wrapper form .input-container .remove-domain{text-decoration:none;display:inline-block;margin-top:3px}#fs_affiliation_content_wrapper form .input-container #add_domain:focus,#fs_affiliation_content_wrapper form .input-container .remove-domain:focus{box-shadow:none}#fs_affiliation_content_wrapper form .input-container #add_domain.disabled,#fs_affiliation_content_wrapper form .input-container .remove-domain.disabled{color:#aaa;cursor:default}#fs_affiliation_content_wrapper form #extra_domains_container .description{margin-top:0;position:relative;top:-4px}#fs_affiliation_content_wrapper form #extra_domains_container .extra-domain-input-container{margin-bottom:15px}#fs_affiliation_content_wrapper form #extra_domains_container .extra-domain-input-container .domain{display:inline-block;margin-right:5px}#fs_affiliation_content_wrapper form #extra_domains_container .extra-domain-input-container .domain:last-of-type{margin-bottom:0}
sdk/freemius/assets/css/admin/connect.css CHANGED
@@ -1 +1 @@
1
- #fs_connect{width:480px;-moz-box-shadow:0px 1px 2px rgba(0,0,0,0.3);-webkit-box-shadow:0px 1px 2px rgba(0,0,0,0.3);box-shadow:0px 1px 2px rgba(0,0,0,0.3);margin:20px 0}@media screen and (max-width: 479px){#fs_connect{-moz-box-shadow:none;-webkit-box-shadow:none;box-shadow:none;width:auto;margin:0 0 0 -10px}}#fs_connect .fs-content{background:#fff;padding:15px 20px}#fs_connect .fs-content .fs-error{background:snow;color:#d3135a;border:1px solid #d3135a;-moz-box-shadow:0 1px 1px 0 rgba(0,0,0,0.1);-webkit-box-shadow:0 1px 1px 0 rgba(0,0,0,0.1);box-shadow:0 1px 1px 0 rgba(0,0,0,0.1);text-align:center;padding:5px;margin-bottom:10px}#fs_connect .fs-content p{margin:0;padding:0;font-size:1.2em}#fs_connect .fs-license-key-container{position:relative;width:280px;margin:10px auto 0 auto}#fs_connect .fs-license-key-container input{width:100%}#fs_connect .fs-license-key-container .dashicons{position:absolute;top:5px;right:5px}#fs_connect .fs-actions{padding:10px 20px;background:#C0C7CA}#fs_connect .fs-actions .button{padding:0 10px 1px;line-height:35px;height:37px;font-size:16px;margin-bottom:0}#fs_connect .fs-actions .button .dashicons{font-size:37px;margin-left:-8px;margin-right:12px}#fs_connect .fs-actions .button.button-primary{padding-right:15px;padding-left:15px}#fs_connect .fs-actions .button.button-primary:after{content:' \279C'}#fs_connect .fs-actions .button.button-primary.fs-loading:after{content:''}#fs_connect .fs-actions .button.button-secondary{float:right}#fs_connect.fs-anonymous-disabled .fs-actions .button.button-primary{width:100%}#fs_connect .fs-permissions{padding:10px 20px;background:#FEFEFE;-moz-transition:background 0.5s ease;-o-transition:background 0.5s ease;-ms-transition:background 0.5s ease;-webkit-transition:background 0.5s ease;transition:background 0.5s ease}#fs_connect .fs-permissions .fs-license-sync-disclaimer{text-align:center;margin-top:0}#fs_connect .fs-permissions .fs-trigger{font-size:0.9em;text-decoration:none;text-align:center;display:block}#fs_connect .fs-permissions ul{height:0;overflow:hidden;margin:0}#fs_connect .fs-permissions ul li{margin-bottom:12px}#fs_connect .fs-permissions ul li:last-child{margin-bottom:0}#fs_connect .fs-permissions ul li i.dashicons{float:left;font-size:40px;width:40px;height:40px}#fs_connect .fs-permissions ul li div{margin-left:55px}#fs_connect .fs-permissions ul li div span{font-weight:bold;text-transform:uppercase;color:#23282d}#fs_connect .fs-permissions ul li div p{margin:2px 0 0 0}#fs_connect .fs-permissions.fs-open{background:#fff}#fs_connect .fs-permissions.fs-open ul{height:auto;margin:20px 20px 10px 20px}@media screen and (max-width: 479px){#fs_connect .fs-permissions{background:#fff}#fs_connect .fs-permissions .fs-trigger{display:none}#fs_connect .fs-permissions ul{height:auto;margin:20px}}#fs_connect .fs-freemium-licensing{padding:8px;background:#777;color:#fff}#fs_connect .fs-freemium-licensing p{text-align:center;display:block;margin:0;padding:0}#fs_connect .fs-freemium-licensing a{color:#C2EEFF;text-decoration:underline}#fs_connect .fs-visual{padding:12px;line-height:0;background:#fafafa;height:80px;position:relative}#fs_connect .fs-visual .fs-site-icon{position:absolute;left:20px;top:10px}#fs_connect .fs-visual .fs-connect-logo{position:absolute;right:20px;top:10px}#fs_connect .fs-visual .fs-plugin-icon{position:absolute;top:10px;left:50%;margin-left:-40px}#fs_connect .fs-visual .fs-plugin-icon,#fs_connect .fs-visual .fs-site-icon,#fs_connect .fs-visual img,#fs_connect .fs-visual object{width:80px;height:80px}#fs_connect .fs-visual .dashicons-wordpress{font-size:64px;background:#01749a;color:#fff;width:64px;height:64px;padding:8px}#fs_connect .fs-visual .dashicons-plus{position:absolute;top:50%;font-size:30px;margin-top:-10px;color:#bbb}#fs_connect .fs-visual .dashicons-plus.fs-first{left:28%}#fs_connect .fs-visual .dashicons-plus.fs-second{left:65%}#fs_connect .fs-visual .fs-plugin-icon,#fs_connect .fs-visual .fs-connect-logo,#fs_connect .fs-visual .fs-site-icon{border:1px solid #ccc;padding:1px;background:#fff}#fs_connect .fs-terms{text-align:center;font-size:0.85em;padding:5px;background:rgba(0,0,0,0.05)}#fs_connect .fs-terms,#fs_connect .fs-terms a{color:#999}#fs_connect .fs-terms a{text-decoration:none}.rtl #fs_connect .fs-actions{padding:10px 20px;background:#C0C7CA}.rtl #fs_connect .fs-actions .button .dashicons{font-size:37px;margin-left:-8px;margin-right:12px}.rtl #fs_connect .fs-actions .button.button-primary:after{content:' \000bb'}.rtl #fs_connect .fs-actions .button.button-primary.fs-loading:after{content:''}.rtl #fs_connect .fs-actions .button.button-secondary{float:left}.rtl #fs_connect .fs-permissions ul li div{margin-right:55px;margin-left:0}.rtl #fs_connect .fs-permissions ul li i.dashicons{float:right}.rtl #fs_connect .fs-visual .fs-site-icon{right:20px;left:auto}.rtl #fs_connect .fs-visual .fs-connect-logo{right:auto;left:20px}#fs_theme_connect_wrapper{position:fixed;top:0;height:100%;width:100%;z-index:99990;background:rgba(0,0,0,0.75);text-align:center;overflow-y:auto}#fs_theme_connect_wrapper:before{content:"";display:inline-block;vertical-align:middle;height:100%}#fs_theme_connect_wrapper>button.close{color:white;cursor:pointer;height:40px;width:40px;position:absolute;right:0;border:0;background-color:transparent;top:32px}#fs_theme_connect_wrapper #fs_connect{top:0;text-align:left;display:inline-block;vertical-align:middle;margin-top:52px;margin-bottom:20px}#fs_theme_connect_wrapper #fs_connect .fs-terms{background:rgba(140,140,140,0.64)}#fs_theme_connect_wrapper #fs_connect .fs-terms,#fs_theme_connect_wrapper #fs_connect .fs-terms a{color:#c5c5c5}.wp-pointer-content #fs_connect{margin:0;-moz-box-shadow:none;-webkit-box-shadow:none;box-shadow:none}.fs-opt-in-pointer .wp-pointer-content{padding:0}.fs-opt-in-pointer.wp-pointer-top .wp-pointer-arrow{border-bottom-color:#dfdfdf}.fs-opt-in-pointer.wp-pointer-top .wp-pointer-arrow-inner{border-bottom-color:#fafafa}.fs-opt-in-pointer.wp-pointer-bottom .wp-pointer-arrow{border-top-color:#dfdfdf}.fs-opt-in-pointer.wp-pointer-bottom .wp-pointer-arrow-inner{border-top-color:#fafafa}.fs-opt-in-pointer.wp-pointer-left .wp-pointer-arrow{border-right-color:#dfdfdf}.fs-opt-in-pointer.wp-pointer-left .wp-pointer-arrow-inner{border-right-color:#fafafa}.fs-opt-in-pointer.wp-pointer-right .wp-pointer-arrow{border-left-color:#dfdfdf}.fs-opt-in-pointer.wp-pointer-right .wp-pointer-arrow-inner{border-left-color:#fafafa}
1
+ #fs_connect{width:480px;-moz-box-shadow:0px 1px 2px rgba(0,0,0,0.3);-webkit-box-shadow:0px 1px 2px rgba(0,0,0,0.3);box-shadow:0px 1px 2px rgba(0,0,0,0.3);margin:20px 0}@media screen and (max-width: 479px){#fs_connect{-moz-box-shadow:none;-webkit-box-shadow:none;box-shadow:none;width:auto;margin:0 0 0 -10px}}#fs_connect .fs-content{background:#fff;padding:15px 20px}#fs_connect .fs-content .fs-error{background:snow;color:#d3135a;border:1px solid #d3135a;-moz-box-shadow:0 1px 1px 0 rgba(0,0,0,0.1);-webkit-box-shadow:0 1px 1px 0 rgba(0,0,0,0.1);box-shadow:0 1px 1px 0 rgba(0,0,0,0.1);text-align:center;padding:5px;margin-bottom:10px}#fs_connect .fs-content p{margin:0;padding:0;font-size:1.2em}#fs_connect .fs-license-key-container{position:relative;width:280px;margin:10px auto 0 auto}#fs_connect .fs-license-key-container input{width:100%}#fs_connect .fs-license-key-container .dashicons{position:absolute;top:5px;right:5px}#fs_connect.require-license-key #sites_list_container td{cursor:pointer}#fs_connect #delegate_to_site_admins{margin-right:15px;float:right;height:26px;vertical-align:middle;line-height:37px;font-weight:bold;border-bottom:1px dashed;text-decoration:none}#fs_connect #delegate_to_site_admins.rtl{margin-left:15px;margin-right:0}#fs_connect .fs-actions{padding:10px 20px;background:#C0C7CA}#fs_connect .fs-actions .button{padding:0 10px 1px;line-height:35px;height:37px;font-size:16px;margin-bottom:0}#fs_connect .fs-actions .button .dashicons{font-size:37px;margin-left:-8px;margin-right:12px}#fs_connect .fs-actions .button.button-primary{padding-right:15px;padding-left:15px}#fs_connect .fs-actions .button.button-primary:after{content:' \279C'}#fs_connect .fs-actions .button.button-primary.fs-loading:after{content:''}#fs_connect .fs-actions .button.button-secondary{float:right}#fs_connect.fs-anonymous-disabled .fs-actions .button.button-primary{width:100%}#fs_connect .fs-permissions{padding:10px 20px;background:#FEFEFE;-moz-transition:background 0.5s ease;-o-transition:background 0.5s ease;-ms-transition:background 0.5s ease;-webkit-transition:background 0.5s ease;transition:background 0.5s ease}#fs_connect .fs-permissions .fs-license-sync-disclaimer{text-align:center;margin-top:0}#fs_connect .fs-permissions .fs-trigger{font-size:0.9em;text-decoration:none;text-align:center;display:block}#fs_connect .fs-permissions ul{height:0;overflow:hidden;margin:0}#fs_connect .fs-permissions ul li{margin-bottom:12px}#fs_connect .fs-permissions ul li:last-child{margin-bottom:0}#fs_connect .fs-permissions ul li i.dashicons{float:left;font-size:40px;width:40px;height:40px}#fs_connect .fs-permissions ul li div{margin-left:55px}#fs_connect .fs-permissions ul li div span{font-weight:bold;text-transform:uppercase;color:#23282d}#fs_connect .fs-permissions ul li div p{margin:2px 0 0 0}#fs_connect .fs-permissions.fs-open{background:#fff}#fs_connect .fs-permissions.fs-open ul{height:auto;margin:20px 20px 10px 20px}@media screen and (max-width: 479px){#fs_connect .fs-permissions{background:#fff}#fs_connect .fs-permissions .fs-trigger{display:none}#fs_connect .fs-permissions ul{height:auto;margin:20px}}#fs_connect .fs-freemium-licensing{padding:8px;background:#777;color:#fff}#fs_connect .fs-freemium-licensing p{text-align:center;display:block;margin:0;padding:0}#fs_connect .fs-freemium-licensing a{color:#C2EEFF;text-decoration:underline}#fs_connect .fs-visual{padding:12px;line-height:0;background:#fafafa;height:80px;position:relative}#fs_connect .fs-visual .fs-site-icon{position:absolute;left:20px;top:10px}#fs_connect .fs-visual .fs-connect-logo{position:absolute;right:20px;top:10px}#fs_connect .fs-visual .fs-plugin-icon{position:absolute;top:10px;left:50%;margin-left:-40px}#fs_connect .fs-visual .fs-plugin-icon,#fs_connect .fs-visual .fs-site-icon,#fs_connect .fs-visual img,#fs_connect .fs-visual object{width:80px;height:80px}#fs_connect .fs-visual .dashicons-wordpress{font-size:64px;background:#01749a;color:#fff;width:64px;height:64px;padding:8px}#fs_connect .fs-visual .dashicons-plus{position:absolute;top:50%;font-size:30px;margin-top:-10px;color:#bbb}#fs_connect .fs-visual .dashicons-plus.fs-first{left:28%}#fs_connect .fs-visual .dashicons-plus.fs-second{left:65%}#fs_connect .fs-visual .fs-plugin-icon,#fs_connect .fs-visual .fs-connect-logo,#fs_connect .fs-visual .fs-site-icon{border:1px solid #ccc;padding:1px;background:#fff}#fs_connect .fs-terms{text-align:center;font-size:0.85em;padding:5px;background:rgba(0,0,0,0.05)}#fs_connect .fs-terms,#fs_connect .fs-terms a{color:#999}#fs_connect .fs-terms a{text-decoration:none}#multisite_options_container{margin-top:10px;border:1px solid #ccc;padding:5px}#multisite_options_container a{text-decoration:none}#multisite_options_container a:focus{box-shadow:none}#multisite_options_container a.selected{font-weight:bold}#multisite_options_container.apply-on-all-sites{border:0 none;padding:0}#multisite_options_container.apply-on-all-sites #all_sites_options{border-spacing:0}#multisite_options_container.apply-on-all-sites #all_sites_options td:not(:first-child){display:none}#multisite_options_container #sites_list_container{display:none;overflow:auto}#multisite_options_container #sites_list_container table td{border-top:1px solid #ccc;padding:4px 2px}.fs-tooltip-trigger{position:relative}.fs-tooltip-trigger:not(a){cursor:help}.fs-tooltip-trigger .fs-tooltip{opacity:0;visibility:hidden;-moz-transition:opacity 0.3s ease-in-out;-o-transition:opacity 0.3s ease-in-out;-ms-transition:opacity 0.3s ease-in-out;-webkit-transition:opacity 0.3s ease-in-out;transition:opacity 0.3s ease-in-out;position:absolute;background:rgba(0,0,0,0.8);color:#fff;font-family:'arial', serif;font-size:12px;padding:10px;z-index:999999;bottom:100%;margin-bottom:5px;left:0;right:0;-moz-border-radius:5px;-webkit-border-radius:5px;border-radius:5px;-moz-box-shadow:1px 1px 1px rgba(0,0,0,0.2);-webkit-box-shadow:1px 1px 1px rgba(0,0,0,0.2);box-shadow:1px 1px 1px rgba(0,0,0,0.2);line-height:1.3em;font-weight:bold;text-align:left}.rtl .fs-tooltip-trigger .fs-tooltip{text-align:right}.fs-tooltip-trigger .fs-tooltip::after{content:' ';display:block;width:0;height:0;border-style:solid;border-width:5px 5px 0 5px;border-color:rgba(0,0,0,0.8) transparent transparent transparent;position:absolute;top:100%;left:21px}.rtl .fs-tooltip-trigger .fs-tooltip::after{right:21px;left:auto}.fs-tooltip-trigger:hover .fs-tooltip{visibility:visible;opacity:1}.rtl #fs_connect .fs-actions{padding:10px 20px;background:#C0C7CA}.rtl #fs_connect .fs-actions .button .dashicons{font-size:37px;margin-left:-8px;margin-right:12px}.rtl #fs_connect .fs-actions .button.button-primary:after{content:' \000bb'}.rtl #fs_connect .fs-actions .button.button-primary.fs-loading:after{content:''}.rtl #fs_connect .fs-actions .button.button-secondary{float:left}.rtl #fs_connect .fs-permissions ul li div{margin-right:55px;margin-left:0}.rtl #fs_connect .fs-permissions ul li i.dashicons{float:right}.rtl #fs_connect .fs-visual .fs-site-icon{right:20px;left:auto}.rtl #fs_connect .fs-visual .fs-connect-logo{right:auto;left:20px}#fs_theme_connect_wrapper{position:fixed;top:0;height:100%;width:100%;z-index:99990;background:rgba(0,0,0,0.75);text-align:center;overflow-y:auto}#fs_theme_connect_wrapper:before{content:"";display:inline-block;vertical-align:middle;height:100%}#fs_theme_connect_wrapper>button.close{color:white;cursor:pointer;height:40px;width:40px;position:absolute;right:0;border:0;background-color:transparent;top:32px}#fs_theme_connect_wrapper #fs_connect{top:0;text-align:left;display:inline-block;vertical-align:middle;margin-top:52px;margin-bottom:20px}#fs_theme_connect_wrapper #fs_connect .fs-terms{background:rgba(140,140,140,0.64)}#fs_theme_connect_wrapper #fs_connect .fs-terms,#fs_theme_connect_wrapper #fs_connect .fs-terms a{color:#c5c5c5}.wp-pointer-content #fs_connect{margin:0;-moz-box-shadow:none;-webkit-box-shadow:none;box-shadow:none}.fs-opt-in-pointer .wp-pointer-content{padding:0}.fs-opt-in-pointer.wp-pointer-top .wp-pointer-arrow{border-bottom-color:#dfdfdf}.fs-opt-in-pointer.wp-pointer-top .wp-pointer-arrow-inner{border-bottom-color:#fafafa}.fs-opt-in-pointer.wp-pointer-bottom .wp-pointer-arrow{border-top-color:#dfdfdf}.fs-opt-in-pointer.wp-pointer-bottom .wp-pointer-arrow-inner{border-top-color:#fafafa}.fs-opt-in-pointer.wp-pointer-left .wp-pointer-arrow{border-right-color:#dfdfdf}.fs-opt-in-pointer.wp-pointer-left .wp-pointer-arrow-inner{border-right-color:#fafafa}.fs-opt-in-pointer.wp-pointer-right .wp-pointer-arrow{border-left-color:#dfdfdf}.fs-opt-in-pointer.wp-pointer-right .wp-pointer-arrow-inner{border-left-color:#fafafa}
sdk/freemius/assets/css/admin/dialog-boxes.css CHANGED
@@ -1,2 +1,2 @@
1
- .fs-modal{position:fixed;overflow:auto;height:100%;width:100%;top:0;z-index:100000;display:none;background:rgba(0,0,0,0.6)}.fs-modal .fs-modal-dialog{background:transparent;position:absolute;left:50%;margin-left:-298px;padding-bottom:30px;top:-100%;z-index:100001;width:596px}@media (max-width: 650px){.fs-modal .fs-modal-dialog{margin-left:-50%;box-sizing:border-box;padding-left:10px;padding-right:10px;width:100%}.fs-modal .fs-modal-dialog .fs-modal-panel>h3>strong{font-size:1.3em}}.fs-modal.active{display:block}.fs-modal.active:before{display:block}.fs-modal.active .fs-modal-dialog{top:10%}.fs-modal.fs-success .fs-modal-header{border-bottom-color:#46b450}.fs-modal.fs-success .fs-modal-body{background-color:#f7fff7}.fs-modal.fs-warn .fs-modal-header{border-bottom-color:#ffb900}.fs-modal.fs-warn .fs-modal-body{background-color:#fff8e5}.fs-modal.fs-error .fs-modal-header{border-bottom-color:#dc3232}.fs-modal.fs-error .fs-modal-body{background-color:#ffeaea}.fs-modal .fs-modal-body,.fs-modal .fs-modal-footer{border:0;background:#fefefe;padding:20px}.fs-modal .fs-modal-header{border-bottom:#eeeeee solid 1px;background:#fbfbfb;padding:15px 20px;position:relative;margin-bottom:-10px}.fs-modal .fs-modal-header h4{margin:0;padding:0;text-transform:uppercase;font-size:1.2em;font-weight:bold;color:#cacaca;text-shadow:1px 1px 1px #fff;letter-spacing:0.6px;-webkit-font-smoothing:antialiased}.fs-modal .fs-modal-header .fs-close{position:absolute;right:10px;top:12px;cursor:pointer;color:#bbb;-moz-border-radius:20px;-webkit-border-radius:20px;border-radius:20px;padding:3px;-moz-transition:all 0.2s ease-in-out;-o-transition:all 0.2s ease-in-out;-ms-transition:all 0.2s ease-in-out;-webkit-transition:all 0.2s ease-in-out;transition:all 0.2s ease-in-out}.fs-modal .fs-modal-header .fs-close:hover{color:#fff;background:#aaa}.fs-modal .fs-modal-body{border-bottom:0}.fs-modal .fs-modal-body p{font-size:14px}.fs-modal .fs-modal-body h2{font-size:20px;line-height:1.5em}.fs-modal .fs-modal-body>div{margin-top:10px}.fs-modal .fs-modal-body>div h2{font-weight:bold;font-size:20px;margin-top:0}.fs-modal .fs-modal-footer{border-top:#eeeeee solid 1px;text-align:right}.fs-modal .fs-modal-footer>.button{margin:0 7px}.fs-modal .fs-modal-footer>.button:first-child{margin:0}.fs-modal .fs-modal-panel>.notice.inline{margin:0;display:none}.fs-modal .fs-modal-panel:not(.active){display:none}.rtl .fs-modal .fs-modal-header .fs-close{right:auto;left:20px}body.has-fs-modal{overflow:hidden}.fs-modal.fs-modal-deactivation-feedback .reason-input,.fs-modal.fs-modal-deactivation-feedback .internal-message{margin:3px 0 3px 22px}.fs-modal.fs-modal-deactivation-feedback .reason-input input,.fs-modal.fs-modal-deactivation-feedback .reason-input textarea,.fs-modal.fs-modal-deactivation-feedback .internal-message input,.fs-modal.fs-modal-deactivation-feedback .internal-message textarea{width:100%}.fs-modal.fs-modal-deactivation-feedback li.reason.has-internal-message .internal-message{border:1px solid #ccc;padding:7px;display:none}@media (max-width: 650px){.fs-modal.fs-modal-deactivation-feedback li.reason li.reason{margin-bottom:10px}.fs-modal.fs-modal-deactivation-feedback li.reason li.reason .reason-input,.fs-modal.fs-modal-deactivation-feedback li.reason li.reason .internal-message{margin-left:29px}.fs-modal.fs-modal-deactivation-feedback li.reason li.reason label{display:table}.fs-modal.fs-modal-deactivation-feedback li.reason li.reason label>span{display:table-cell;font-size:1.3em}}.fs-modal.fs-modal-deactivation-feedback .anonymous-feedback-label{float:left}.fs-modal.fs-modal-deactivation-feedback .fs-modal-panel{margin-top:0 !important}.fs-modal.fs-modal-deactivation-feedback .fs-modal-panel h3{margin-top:0;line-height:1.5em}#the-list .deactivate>.fs-slug{display:none}.fs-modal.fs-modal-license-activation .fs-modal-body input.license_key{width:100%}.fs-modal.fs-modal-license-key-resend .email-address-container{overflow:hidden;padding-right:2px}.fs-modal.fs-modal-license-key-resend.fs-freemium input.email-address{width:300px}.fs-modal.fs-modal-license-key-resend.fs-freemium label{display:block;margin-bottom:10px}.fs-modal.fs-modal-license-key-resend.fs-premium input.email-address{width:100%}.fs-modal.fs-modal-license-key-resend.fs-premium .button-container{float:right;margin-left:7px}@media (max-width: 650px){.fs-modal.fs-modal-license-key-resend.fs-premium .button-container{margin-top:2px}}
2
  .rtl .fs-modal.fs-modal-license-key-resend .fs-modal-body .input-container>.email-address-container{padding-left:2px;padding-right:0}.rtl .fs-modal.fs-modal-license-key-resend .fs-modal-body .button-container{float:left;margin-right:7px;margin-left:0}a.show-license-resend-modal{margin-top:4px;display:inline-block}.fs-ajax-loader{position:relative;width:170px;height:20px;margin:auto}.fs-ajax-loader .fs-ajax-loader-bar{position:absolute;top:0;background-color:#0074a3;width:20px;height:20px;-webkit-animation-name:bounce_ajaxLoader;-moz-animation-name:bounce_ajaxLoader;-ms-animation-name:bounce_ajaxLoader;-o-animation-name:bounce_ajaxLoader;animation-name:bounce_ajaxLoader;-webkit-animation-duration:1.5s;-moz-animation-duration:1.5s;-ms-animation-duration:1.5s;-o-animation-duration:1.5s;animation-duration:1.5s;animation-iteration-count:infinite;-o-animation-iteration-count:infinite;-ms-animation-iteration-count:infinite;-webkit-animation-iteration-count:infinite;-moz-animation-iteration-count:infinite;-webkit-animation-direction:normal;-moz-animation-direction:normal;-ms-animation-direction:normal;-o-animation-direction:normal;animation-direction:normal;-moz-transform:0.3;-o-transform:0.3;-ms-transform:0.3;-webkit-transform:0.3;transform:0.3}.fs-ajax-loader .fs-ajax-loader-bar-1{left:0px;animation-delay:0.6s;-o-animation-delay:0.6s;-ms-animation-delay:0.6s;-webkit-animation-delay:0.6s;-moz-animation-delay:0.6s}.fs-ajax-loader .fs-ajax-loader-bar-2{left:19px;animation-delay:0.75s;-o-animation-delay:0.75s;-ms-animation-delay:0.75s;-webkit-animation-delay:0.75s;-moz-animation-delay:0.75s}.fs-ajax-loader .fs-ajax-loader-bar-3{left:38px;animation-delay:0.9s;-o-animation-delay:0.9s;-ms-animation-delay:0.9s;-webkit-animation-delay:0.9s;-moz-animation-delay:0.9s}.fs-ajax-loader .fs-ajax-loader-bar-4{left:57px;animation-delay:1.05s;-o-animation-delay:1.05s;-ms-animation-delay:1.05s;-webkit-animation-delay:1.05s;-moz-animation-delay:1.05s}.fs-ajax-loader .fs-ajax-loader-bar-5{left:76px;animation-delay:1.2s;-o-animation-delay:1.2s;-ms-animation-delay:1.2s;-webkit-animation-delay:1.2s;-moz-animation-delay:1.2s}.fs-ajax-loader .fs-ajax-loader-bar-6{left:95px;animation-delay:1.35s;-o-animation-delay:1.35s;-ms-animation-delay:1.35s;-webkit-animation-delay:1.35s;-moz-animation-delay:1.35s}.fs-ajax-loader .fs-ajax-loader-bar-7{left:114px;animation-delay:1.5s;-o-animation-delay:1.5s;-ms-animation-delay:1.5s;-webkit-animation-delay:1.5s;-moz-animation-delay:1.5s}.fs-ajax-loader .fs-ajax-loader-bar-8{left:133px;animation-delay:1.65s;-o-animation-delay:1.65s;-ms-animation-delay:1.65s;-webkit-animation-delay:1.65s;-moz-animation-delay:1.65s}@-moz-keyframes bounce_ajaxLoader{0%{-moz-transform:scale(1);-o-transform:scale(1);-ms-transform:scale(1);-webkit-transform:scale(1);transform:scale(1);background-color:#0074a3}100%{-moz-transform:scale(0.3);-o-transform:scale(0.3);-ms-transform:scale(0.3);-webkit-transform:scale(0.3);transform:scale(0.3);background-color:#fff}}@-ms-keyframes bounce_ajaxLoader{0%{-moz-transform:scale(1);-o-transform:scale(1);-ms-transform:scale(1);-webkit-transform:scale(1);transform:scale(1);background-color:#0074a3}100%{-moz-transform:scale(0.3);-o-transform:scale(0.3);-ms-transform:scale(0.3);-webkit-transform:scale(0.3);transform:scale(0.3);background-color:#fff}}@-o-keyframes bounce_ajaxLoader{0%{-moz-transform:scale(1);-o-transform:scale(1);-ms-transform:scale(1);-webkit-transform:scale(1);transform:scale(1);background-color:#0074a3}100%{-moz-transform:scale(0.3);-o-transform:scale(0.3);-ms-transform:scale(0.3);-webkit-transform:scale(0.3);transform:scale(0.3);background-color:#fff}}@-webkit-keyframes bounce_ajaxLoader{0%{-moz-transform:scale(1);-o-transform:scale(1);-ms-transform:scale(1);-webkit-transform:scale(1);transform:scale(1);background-color:#0074a3}100%{-moz-transform:scale(0.3);-o-transform:scale(0.3);-ms-transform:scale(0.3);-webkit-transform:scale(0.3);transform:scale(0.3);background-color:#fff}}@keyframes bounce_ajaxLoader{0%{-moz-transform:scale(1);-o-transform:scale(1);-ms-transform:scale(1);-webkit-transform:scale(1);transform:scale(1);background-color:#0074a3}100%{-moz-transform:scale(0.3);-o-transform:scale(0.3);-ms-transform:scale(0.3);-webkit-transform:scale(0.3);transform:scale(0.3);background-color:#fff}}.fs-modal-auto-install #request-filesystem-credentials-form h2,.fs-modal-auto-install #request-filesystem-credentials-form .request-filesystem-credentials-action-buttons{display:none}.fs-modal-auto-install #request-filesystem-credentials-form input[type=password],.fs-modal-auto-install #request-filesystem-credentials-form input[type=email],.fs-modal-auto-install #request-filesystem-credentials-form input[type=text]{-webkit-appearance:none;padding:10px 10px 5px 10px;width:300px;max-width:100%}.fs-modal-auto-install #request-filesystem-credentials-form>div,.fs-modal-auto-install #request-filesystem-credentials-form label,.fs-modal-auto-install #request-filesystem-credentials-form fieldset{width:300px;max-width:100%;margin:0 auto;display:block}
1
+ .fs-modal{position:fixed;overflow:auto;height:100%;width:100%;top:0;z-index:100000;display:none;background:rgba(0,0,0,0.6)}.fs-modal .fs-modal-dialog{background:transparent;position:absolute;left:50%;margin-left:-298px;padding-bottom:30px;top:-100%;z-index:100001;width:596px}@media (max-width: 650px){.fs-modal .fs-modal-dialog{margin-left:-50%;box-sizing:border-box;padding-left:10px;padding-right:10px;width:100%}.fs-modal .fs-modal-dialog .fs-modal-panel>h3>strong{font-size:1.3em}}.fs-modal.active{display:block}.fs-modal.active:before{display:block}.fs-modal.active .fs-modal-dialog{top:10%}.fs-modal.fs-success .fs-modal-header{border-bottom-color:#46b450}.fs-modal.fs-success .fs-modal-body{background-color:#f7fff7}.fs-modal.fs-warn .fs-modal-header{border-bottom-color:#ffb900}.fs-modal.fs-warn .fs-modal-body{background-color:#fff8e5}.fs-modal.fs-error .fs-modal-header{border-bottom-color:#dc3232}.fs-modal.fs-error .fs-modal-body{background-color:#ffeaea}.fs-modal .fs-modal-body,.fs-modal .fs-modal-footer{border:0;background:#fefefe;padding:20px}.fs-modal .fs-modal-header{border-bottom:#eeeeee solid 1px;background:#fbfbfb;padding:15px 20px;position:relative;margin-bottom:-10px}.fs-modal .fs-modal-header h4{margin:0;padding:0;text-transform:uppercase;font-size:1.2em;font-weight:bold;color:#cacaca;text-shadow:1px 1px 1px #fff;letter-spacing:0.6px;-webkit-font-smoothing:antialiased}.fs-modal .fs-modal-header .fs-close{position:absolute;right:10px;top:12px;cursor:pointer;color:#bbb;-moz-border-radius:20px;-webkit-border-radius:20px;border-radius:20px;padding:3px;-moz-transition:all 0.2s ease-in-out;-o-transition:all 0.2s ease-in-out;-ms-transition:all 0.2s ease-in-out;-webkit-transition:all 0.2s ease-in-out;transition:all 0.2s ease-in-out}.fs-modal .fs-modal-header .fs-close:hover{color:#fff;background:#aaa}.fs-modal .fs-modal-header .fs-close .dashicons,.fs-modal .fs-modal-header .fs-close:hover .dashicons{text-decoration:none}.fs-modal .fs-modal-body{border-bottom:0}.fs-modal .fs-modal-body p{font-size:14px}.fs-modal .fs-modal-body h2{font-size:20px;line-height:1.5em}.fs-modal .fs-modal-body>div{margin-top:10px}.fs-modal .fs-modal-body>div h2{font-weight:bold;font-size:20px;margin-top:0}.fs-modal .fs-modal-footer{border-top:#eeeeee solid 1px;text-align:right}.fs-modal .fs-modal-footer>.button{margin:0 7px}.fs-modal .fs-modal-footer>.button:first-child{margin:0}.fs-modal .fs-modal-panel>.notice.inline{margin:0;display:none}.fs-modal .fs-modal-panel:not(.active){display:none}.rtl .fs-modal .fs-modal-header .fs-close{right:auto;left:20px}body.has-fs-modal{overflow:hidden}.fs-modal.fs-modal-deactivation-feedback .reason-input,.fs-modal.fs-modal-deactivation-feedback .internal-message{margin:3px 0 3px 22px}.fs-modal.fs-modal-deactivation-feedback .reason-input input,.fs-modal.fs-modal-deactivation-feedback .reason-input textarea,.fs-modal.fs-modal-deactivation-feedback .internal-message input,.fs-modal.fs-modal-deactivation-feedback .internal-message textarea{width:100%}.fs-modal.fs-modal-deactivation-feedback li.reason.has-internal-message .internal-message{border:1px solid #ccc;padding:7px;display:none}@media (max-width: 650px){.fs-modal.fs-modal-deactivation-feedback li.reason li.reason{margin-bottom:10px}.fs-modal.fs-modal-deactivation-feedback li.reason li.reason .reason-input,.fs-modal.fs-modal-deactivation-feedback li.reason li.reason .internal-message{margin-left:29px}.fs-modal.fs-modal-deactivation-feedback li.reason li.reason label{display:table}.fs-modal.fs-modal-deactivation-feedback li.reason li.reason label>span{display:table-cell;font-size:1.3em}}.fs-modal.fs-modal-deactivation-feedback .anonymous-feedback-label{float:left}.fs-modal.fs-modal-deactivation-feedback .fs-modal-panel{margin-top:0 !important}.fs-modal.fs-modal-deactivation-feedback .fs-modal-panel h3{margin-top:0;line-height:1.5em}#the-list .deactivate>.fs-slug{display:none}.fs-modal.fs-modal-license-activation .fs-modal-body input.license_key{width:100%}#license_options_container table,#license_options_container table select,#license_options_container table #available_license_key{width:100%}#license_options_container table td:first-child{width:1%}#license_options_container table #other_license_key_container label{position:relative;top:6px;float:left;margin-right:5px}#license_options_container table #other_license_key_container div{overflow:hidden;width:auto;height:30px;display:block;top:2px;position:relative}#license_options_container table #other_license_key_container div input{margin:0}#sites_list_container td{cursor:pointer}#multisite_options_container{margin-top:10px;border:1px solid #ccc;padding:5px}#multisite_options_container a{text-decoration:none}#multisite_options_container a:focus{box-shadow:none}#multisite_options_container a.selected{font-weight:bold}#multisite_options_container.apply-on-all-sites{border:0 none;padding:0}#multisite_options_container.apply-on-all-sites #all_sites_options{border-spacing:0}#multisite_options_container.apply-on-all-sites #all_sites_options td:not(:first-child){display:none}#multisite_options_container #sites_list_container{display:none;overflow:auto}#multisite_options_container #sites_list_container table td{border-top:1px solid #ccc;padding:4px 2px}.fs-modal.fs-modal-license-key-resend .email-address-container{overflow:hidden;padding-right:2px}.fs-modal.fs-modal-license-key-resend.fs-freemium input.email-address{width:300px}.fs-modal.fs-modal-license-key-resend.fs-freemium label{display:block;margin-bottom:10px}.fs-modal.fs-modal-license-key-resend.fs-premium input.email-address{width:100%}.fs-modal.fs-modal-license-key-resend.fs-premium .button-container{float:right;margin-left:7px}@media (max-width: 650px){.fs-modal.fs-modal-license-key-resend.fs-premium .button-container{margin-top:2px}}
2
  .rtl .fs-modal.fs-modal-license-key-resend .fs-modal-body .input-container>.email-address-container{padding-left:2px;padding-right:0}.rtl .fs-modal.fs-modal-license-key-resend .fs-modal-body .button-container{float:left;margin-right:7px;margin-left:0}a.show-license-resend-modal{margin-top:4px;display:inline-block}.fs-ajax-loader{position:relative;width:170px;height:20px;margin:auto}.fs-ajax-loader .fs-ajax-loader-bar{position:absolute;top:0;background-color:#0074a3;width:20px;height:20px;-webkit-animation-name:bounce_ajaxLoader;-moz-animation-name:bounce_ajaxLoader;-ms-animation-name:bounce_ajaxLoader;-o-animation-name:bounce_ajaxLoader;animation-name:bounce_ajaxLoader;-webkit-animation-duration:1.5s;-moz-animation-duration:1.5s;-ms-animation-duration:1.5s;-o-animation-duration:1.5s;animation-duration:1.5s;animation-iteration-count:infinite;-o-animation-iteration-count:infinite;-ms-animation-iteration-count:infinite;-webkit-animation-iteration-count:infinite;-moz-animation-iteration-count:infinite;-webkit-animation-direction:normal;-moz-animation-direction:normal;-ms-animation-direction:normal;-o-animation-direction:normal;animation-direction:normal;-moz-transform:0.3;-o-transform:0.3;-ms-transform:0.3;-webkit-transform:0.3;transform:0.3}.fs-ajax-loader .fs-ajax-loader-bar-1{left:0px;animation-delay:0.6s;-o-animation-delay:0.6s;-ms-animation-delay:0.6s;-webkit-animation-delay:0.6s;-moz-animation-delay:0.6s}.fs-ajax-loader .fs-ajax-loader-bar-2{left:19px;animation-delay:0.75s;-o-animation-delay:0.75s;-ms-animation-delay:0.75s;-webkit-animation-delay:0.75s;-moz-animation-delay:0.75s}.fs-ajax-loader .fs-ajax-loader-bar-3{left:38px;animation-delay:0.9s;-o-animation-delay:0.9s;-ms-animation-delay:0.9s;-webkit-animation-delay:0.9s;-moz-animation-delay:0.9s}.fs-ajax-loader .fs-ajax-loader-bar-4{left:57px;animation-delay:1.05s;-o-animation-delay:1.05s;-ms-animation-delay:1.05s;-webkit-animation-delay:1.05s;-moz-animation-delay:1.05s}.fs-ajax-loader .fs-ajax-loader-bar-5{left:76px;animation-delay:1.2s;-o-animation-delay:1.2s;-ms-animation-delay:1.2s;-webkit-animation-delay:1.2s;-moz-animation-delay:1.2s}.fs-ajax-loader .fs-ajax-loader-bar-6{left:95px;animation-delay:1.35s;-o-animation-delay:1.35s;-ms-animation-delay:1.35s;-webkit-animation-delay:1.35s;-moz-animation-delay:1.35s}.fs-ajax-loader .fs-ajax-loader-bar-7{left:114px;animation-delay:1.5s;-o-animation-delay:1.5s;-ms-animation-delay:1.5s;-webkit-animation-delay:1.5s;-moz-animation-delay:1.5s}.fs-ajax-loader .fs-ajax-loader-bar-8{left:133px;animation-delay:1.65s;-o-animation-delay:1.65s;-ms-animation-delay:1.65s;-webkit-animation-delay:1.65s;-moz-animation-delay:1.65s}@-moz-keyframes bounce_ajaxLoader{0%{-moz-transform:scale(1);-o-transform:scale(1);-ms-transform:scale(1);-webkit-transform:scale(1);transform:scale(1);background-color:#0074a3}100%{-moz-transform:scale(0.3);-o-transform:scale(0.3);-ms-transform:scale(0.3);-webkit-transform:scale(0.3);transform:scale(0.3);background-color:#fff}}@-ms-keyframes bounce_ajaxLoader{0%{-moz-transform:scale(1);-o-transform:scale(1);-ms-transform:scale(1);-webkit-transform:scale(1);transform:scale(1);background-color:#0074a3}100%{-moz-transform:scale(0.3);-o-transform:scale(0.3);-ms-transform:scale(0.3);-webkit-transform:scale(0.3);transform:scale(0.3);background-color:#fff}}@-o-keyframes bounce_ajaxLoader{0%{-moz-transform:scale(1);-o-transform:scale(1);-ms-transform:scale(1);-webkit-transform:scale(1);transform:scale(1);background-color:#0074a3}100%{-moz-transform:scale(0.3);-o-transform:scale(0.3);-ms-transform:scale(0.3);-webkit-transform:scale(0.3);transform:scale(0.3);background-color:#fff}}@-webkit-keyframes bounce_ajaxLoader{0%{-moz-transform:scale(1);-o-transform:scale(1);-ms-transform:scale(1);-webkit-transform:scale(1);transform:scale(1);background-color:#0074a3}100%{-moz-transform:scale(0.3);-o-transform:scale(0.3);-ms-transform:scale(0.3);-webkit-transform:scale(0.3);transform:scale(0.3);background-color:#fff}}@keyframes bounce_ajaxLoader{0%{-moz-transform:scale(1);-o-transform:scale(1);-ms-transform:scale(1);-webkit-transform:scale(1);transform:scale(1);background-color:#0074a3}100%{-moz-transform:scale(0.3);-o-transform:scale(0.3);-ms-transform:scale(0.3);-webkit-transform:scale(0.3);transform:scale(0.3);background-color:#fff}}.fs-modal-auto-install #request-filesystem-credentials-form h2,.fs-modal-auto-install #request-filesystem-credentials-form .request-filesystem-credentials-action-buttons{display:none}.fs-modal-auto-install #request-filesystem-credentials-form input[type=password],.fs-modal-auto-install #request-filesystem-credentials-form input[type=email],.fs-modal-auto-install #request-filesystem-credentials-form input[type=text]{-webkit-appearance:none;padding:10px 10px 5px 10px;width:300px;max-width:100%}.fs-modal-auto-install #request-filesystem-credentials-form>div,.fs-modal-auto-install #request-filesystem-credentials-form label,.fs-modal-auto-install #request-filesystem-credentials-form fieldset{width:300px;max-width:100%;margin:0 auto;display:block}
sdk/freemius/assets/scss/_colors.scss CHANGED
@@ -63,3 +63,6 @@ $linkedin_color: #4875b4;
63
  $youtube_color: #ff3333;
64
  $gplus_color: #c63d2d;
65
 
 
 
 
63
  $youtube_color: #ff3333;
64
  $gplus_color: #c63d2d;
65
 
66
+ // Tooltip
67
+ $tooltip-color: #fff;
68
+ $tooltip-bkg-color: rgba(0,0,0,0.8);
sdk/freemius/assets/scss/admin/_license-activation.scss CHANGED
@@ -4,4 +4,44 @@
4
  width: 100%;
5
  }
6
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  }
4
  width: 100%;
5
  }
6
  }
7
+ }
8
+
9
+ #license_options_container {
10
+ table {
11
+ &, select, #available_license_key {
12
+ width: 100%;
13
+ }
14
+
15
+ td:first-child {
16
+ width: 1%;
17
+ }
18
+
19
+ #other_license_key_container {
20
+ label {
21
+ position: relative;
22
+ top: 6px;
23
+ float: left;
24
+ margin-right: 5px;
25
+ }
26
+
27
+ div {
28
+ overflow: hidden;
29
+ width: auto;
30
+ height: 30px;
31
+ display: block;
32
+ top: 2px;
33
+ position: relative;
34
+
35
+ input {
36
+ margin: 0;
37
+ }
38
+ }
39
+ }
40
+ }
41
+ }
42
+
43
+ #sites_list_container {
44
+ td {
45
+ cursor: pointer;
46
+ }
47
  }
sdk/freemius/assets/scss/admin/_modal-common.scss CHANGED
@@ -118,6 +118,14 @@
118
  color: #fff;
119
  background: #aaa;
120
  }
 
 
 
 
 
 
 
 
121
  }
122
  }
123
 
118
  color: #fff;
119
  background: #aaa;
120
  }
121
+
122
+ &, &:hover
123
+ {
124
+ .dashicons
125
+ {
126
+ text-decoration: none;
127
+ }
128
+ }
129
  }
130
  }
131
 
sdk/freemius/assets/scss/admin/_multisite-options.scss ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #multisite_options_container {
2
+ margin-top: 10px;
3
+ border: 1px solid #ccc;
4
+ padding: 5px;
5
+
6
+ a {
7
+ text-decoration: none;
8
+
9
+ &:focus {
10
+ box-shadow: none;
11
+ }
12
+
13
+ &.selected {
14
+ font-weight: bold;
15
+ }
16
+ }
17
+
18
+ &.apply-on-all-sites {
19
+ border: 0 none;
20
+ padding: 0;
21
+
22
+ #all_sites_options {
23
+ border-spacing: 0;
24
+
25
+ td:not(:first-child) {
26
+ display: none;
27
+ }
28
+ }
29
+ }
30
+
31
+ #sites_list_container {
32
+ display: none;
33
+ overflow: auto;
34
+
35
+ table td {
36
+ border-top: 1px solid #ccc;
37
+ padding: 4px 2px;
38
+ }
39
+ }
40
+ }
sdk/freemius/assets/scss/admin/_tooltip.scss ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .fs-tooltip-trigger
2
+ {
3
+ &:not(a)
4
+ {
5
+ cursor: help;
6
+ }
7
+
8
+ position: relative;
9
+
10
+ .fs-tooltip
11
+ {
12
+ opacity: 0;
13
+ visibility: hidden;
14
+ @include transition(opacity 0.3s ease-in-out);
15
+ position: absolute;
16
+ background: $tooltip-bkg-color;
17
+ color: $tooltip-color;
18
+ font-family: 'arial', serif;
19
+ font-size: 12px;
20
+ padding: 10px;
21
+ z-index: 999999;
22
+ bottom: 100%;
23
+ margin-bottom: 5px;
24
+ left: 0;
25
+ right: 0;
26
+ @include border-radius(5px);
27
+ @include box-shadow(1px 1px 1px rgba(0, 0, 0, 0.2));
28
+ line-height: 1.3em;
29
+ font-weight: bold;
30
+ text-align: left;
31
+
32
+ .rtl &
33
+ {
34
+ text-align: right;
35
+ }
36
+
37
+ &::after
38
+ {
39
+ content: ' ';
40
+ display: block;
41
+ width: 0;
42
+ height: 0;
43
+ border-style: solid;
44
+ border-width: 5px 5px 0 5px;
45
+ border-color: $tooltip-bkg-color transparent transparent transparent;
46
+ position: absolute;
47
+ top: 100%;
48
+ left: 21px;
49
+
50
+ .rtl &
51
+ {
52
+ right: 21px;
53
+ left: auto;
54
+ }
55
+ }
56
+ }
57
+
58
+ &:hover
59
+ {
60
+ .fs-tooltip
61
+ {
62
+ visibility: visible;
63
+ opacity: 1;
64
+ }
65
+ }
66
+ }
sdk/freemius/assets/scss/admin/account.scss CHANGED
@@ -30,12 +30,9 @@
30
  width: 1.2em;
31
  }
32
 
33
- .button
34
  {
35
- i.dashicons
36
- {
37
- vertical-align: middle;
38
- }
39
  }
40
 
41
  .fs-header-actions
@@ -160,6 +157,55 @@ label.fs-tag
160
  }
161
  }
162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  #fs_addons
164
  {
165
  h3
30
  width: 1.2em;
31
  }
32
 
33
+ .dashicons
34
  {
35
+ vertical-align: middle;
 
 
 
36
  }
37
 
38
  .fs-header-actions
157
  }
158
  }
159
 
160
+ #fs_sites
161
+ {
162
+ .fs-scrollable-table
163
+ {
164
+ .fs-table-body {
165
+ max-height: 200px;
166
+ overflow: auto;
167
+ border: 1px solid #e5e5e5;
168
+
169
+ & > table.widefat {
170
+ border: none !important;
171
+ }
172
+ }
173
+
174
+ .fs-main-column {
175
+ width: 100%;
176
+ }
177
+
178
+ .fs-site-details
179
+ {
180
+ td:first-of-type
181
+ {
182
+ text-align: right;
183
+ color: grey;
184
+ width: 1px;
185
+ }
186
+
187
+ td:last-of-type
188
+ {
189
+ text-align: right;
190
+ }
191
+ }
192
+
193
+ .fs-install-details table
194
+ {
195
+ tr td
196
+ {
197
+ width: 1px;
198
+ white-space: nowrap;
199
+
200
+ &:last-of-type
201
+ {
202
+ width: auto;
203
+ }
204
+ }
205
+ }
206
+ }
207
+ }
208
+
209
  #fs_addons
210
  {
211
  h3
sdk/freemius/assets/scss/admin/connect.scss CHANGED
@@ -53,6 +53,30 @@ $form_width: 480px;
53
  }
54
  }
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  .fs-actions
57
  {
58
  padding: 10px 20px;
@@ -326,6 +350,9 @@ $form_width: 480px;
326
  }
327
  }
328
 
 
 
 
329
  .rtl
330
  {
331
  #fs_connect
53
  }
54
  }
55
 
56
+ &.require-license-key {
57
+ #sites_list_container {
58
+ td {
59
+ cursor: pointer;
60
+ }
61
+ }
62
+ }
63
+
64
+ #delegate_to_site_admins {
65
+ margin-right: 15px;
66
+ float: right;
67
+ height: 26px;
68
+ vertical-align: middle;
69
+ line-height: 37px;
70
+ font-weight: bold;
71
+ border-bottom: 1px dashed;
72
+ text-decoration: none;
73
+
74
+ &.rtl {
75
+ margin-left: 15px;
76
+ margin-right: 0;
77
+ }
78
+ }
79
+
80
  .fs-actions
81
  {
82
  padding: 10px 20px;
350
  }
351
  }
352
 
353
+ @import "multisite-options";
354
+ @import "tooltip";
355
+
356
  .rtl
357
  {
358
  #fs_connect
sdk/freemius/assets/scss/admin/dialog-boxes.scss CHANGED
@@ -2,6 +2,7 @@
2
  @import "modal-common";
3
  @import "deactivation-feedback";
4
  @import "license-activation";
 
5
  @import "license-key-resend";
6
  @import "ajax-loader";
7
  @import "auto-install";
2
  @import "modal-common";
3
  @import "deactivation-feedback";
4
  @import "license-activation";
5
+ @import "multisite-options";
6
  @import "license-key-resend";
7
  @import "ajax-loader";
8
  @import "auto-install";
sdk/freemius/config.php CHANGED
@@ -1,328 +1,378 @@
1
  <?php
2
- /**
3
- * @package Freemius
4
- * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
6
- * @since 1.0.4
7
- */
8
-
9
- if ( ! defined( 'ABSPATH' ) ) {
10
- exit;
11
- }
12
-
13
- if ( ! defined( 'WP_FS__SLUG' ) ) {
14
- define( 'WP_FS__SLUG', 'freemius' );
15
- }
16
- if ( ! defined( 'WP_FS__DEV_MODE' ) ) {
17
- define( 'WP_FS__DEV_MODE', false );
18
- }
19
-
20
- #--------------------------------------------------------------------------------
21
- #region API Connectivity Issues Simulation
22
- #--------------------------------------------------------------------------------
23
-
24
- if ( ! defined( 'WP_FS__SIMULATE_NO_API_CONNECTIVITY' ) ) {
25
- define( 'WP_FS__SIMULATE_NO_API_CONNECTIVITY', false );
26
- }
27
- if ( ! defined( 'WP_FS__SIMULATE_NO_CURL' ) ) {
28
- define( 'WP_FS__SIMULATE_NO_CURL', false );
29
- }
30
- if ( ! defined( 'WP_FS__SIMULATE_NO_API_CONNECTIVITY_CLOUDFLARE' ) ) {
31
- define( 'WP_FS__SIMULATE_NO_API_CONNECTIVITY_CLOUDFLARE', false );
32
- }
33
- if ( ! defined( 'WP_FS__SIMULATE_NO_API_CONNECTIVITY_SQUID_ACL' ) ) {
34
- define( 'WP_FS__SIMULATE_NO_API_CONNECTIVITY_SQUID_ACL', false );
35
- }
36
- if ( WP_FS__SIMULATE_NO_CURL ) {
37
- define( 'FS_SDK__SIMULATE_NO_CURL', true );
38
- }
39
- if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY_CLOUDFLARE ) {
40
- define( 'FS_SDK__SIMULATE_NO_API_CONNECTIVITY_CLOUDFLARE', true );
41
- }
42
- if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY_SQUID_ACL ) {
43
- define( 'FS_SDK__SIMULATE_NO_API_CONNECTIVITY_SQUID_ACL', true );
44
- }
45
-
46
- #endregion
47
-
48
- if ( ! defined( 'WP_FS__SIMULATE_FREEMIUS_OFF' ) ) {
49
- define( 'WP_FS__SIMULATE_FREEMIUS_OFF', false );
50
- }
51
-
52
- if ( ! defined( 'WP_FS__PING_API_ON_IP_OR_HOST_CHANGES' ) ) {
53
- /**
54
- * @since 1.1.7.3
55
- * @author Vova Feldman (@svovaf)
56
- *
57
- * I'm not sure if shared servers periodically change IP, or the subdomain of the
58
- * admin dashboard. Also, I've seen sites that have strange loop of switching
59
- * between domains on a daily basis. Therefore, to eliminate the risk of
60
- * multiple unwanted connectivity test pings, temporary ignore domain or
61
- * server IP changes.
62
- */
63
- define( 'WP_FS__PING_API_ON_IP_OR_HOST_CHANGES', false );
64
- }
65
-
66
- /**
67
- * If your dev environment supports custom public network IP setup
68
- * like VVV, please update WP_FS__LOCALHOST_IP with your public IP
69
- * and uncomment it during dev.
70
- */
71
- if ( ! defined( 'WP_FS__LOCALHOST_IP' ) ) {
72
- // VVV default public network IP.
73
- define( 'WP_FS__VVV_DEFAULT_PUBLIC_IP', '192.168.50.4' );
74
 
75
  // define( 'WP_FS__LOCALHOST_IP', WP_FS__VVV_DEFAULT_PUBLIC_IP );
76
- }
77
-
78
- /**
79
- * If true and running with secret key, the opt-in process
80
- * will skip the email activation process which is invoked
81
- * when the email of the context user already exist in Freemius
82
- * database (as a security precaution, to prevent sharing user
83
- * secret with unauthorized entity).
84
- *
85
- * IMPORTANT:
86
- * AS A SECURITY PRECAUTION, WE VALIDATE THE TIMESTAMP OF THE OPT-IN REQUEST.
87
- * THEREFORE, MAKE SURE THAT WHEN USING THIS PARAMETER,YOUR TESTING ENVIRONMENT'S
88
- * CLOCK IS SYNCED.
89
- */
90
- if ( ! defined( 'WP_FS__SKIP_EMAIL_ACTIVATION' ) ) {
91
- define( 'WP_FS__SKIP_EMAIL_ACTIVATION', false );
92
- }
93
-
94
-
95
- #--------------------------------------------------------------------------------
96
- #region Directories
97
- #--------------------------------------------------------------------------------
98
-
99
- if ( ! defined( 'WP_FS__DIR' ) ) {
100
- define( 'WP_FS__DIR', dirname( __FILE__ ) );
101
- }
102
- if ( ! defined( 'WP_FS__DIR_INCLUDES' ) ) {
103
- define( 'WP_FS__DIR_INCLUDES', WP_FS__DIR . '/includes' );
104
- }
105
- if ( ! defined( 'WP_FS__DIR_TEMPLATES' ) ) {
106
- define( 'WP_FS__DIR_TEMPLATES', WP_FS__DIR . '/templates' );
107
- }
108
- if ( ! defined( 'WP_FS__DIR_ASSETS' ) ) {
109
- define( 'WP_FS__DIR_ASSETS', WP_FS__DIR . '/assets' );
110
- }
111
- if ( ! defined( 'WP_FS__DIR_CSS' ) ) {
112
- define( 'WP_FS__DIR_CSS', WP_FS__DIR_ASSETS . '/css' );
113
- }
114
- if ( ! defined( 'WP_FS__DIR_JS' ) ) {
115
- define( 'WP_FS__DIR_JS', WP_FS__DIR_ASSETS . '/js' );
116
- }
117
- if ( ! defined( 'WP_FS__DIR_IMG' ) ) {
118
- define( 'WP_FS__DIR_IMG', WP_FS__DIR_ASSETS . '/img' );
119
- }
120
- if ( ! defined( 'WP_FS__DIR_SDK' ) ) {
121
- define( 'WP_FS__DIR_SDK', WP_FS__DIR_INCLUDES . '/sdk' );
122
- }
123
-
124
- #endregion
125
-
126
- /**
127
- * Domain / URL / Address
128
- */
129
- define( 'WP_FS__ROOT_DOMAIN_PRODUCTION', 'freemius.com' );
130
- define( 'WP_FS__DOMAIN_PRODUCTION', 'wp.freemius.com' );
131
- define( 'WP_FS__ADDRESS_PRODUCTION', 'https://' . WP_FS__DOMAIN_PRODUCTION );
132
-
133
- if ( ! defined( 'WP_FS__DOMAIN_LOCALHOST' ) ) {
134
- define( 'WP_FS__DOMAIN_LOCALHOST', 'wp.freemius' );
135
- }
136
- if ( ! defined( 'WP_FS__ADDRESS_LOCALHOST' ) ) {
137
- define( 'WP_FS__ADDRESS_LOCALHOST', 'http://' . WP_FS__DOMAIN_LOCALHOST . ':8080' );
138
- }
139
-
140
- if ( ! defined( 'WP_FS__TESTING_DOMAIN' ) ) {
141
- define( 'WP_FS__TESTING_DOMAIN', 'fswp' );
142
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
 
144
  #--------------------------------------------------------------------------------
145
- #region HTTP
146
- #--------------------------------------------------------------------------------
147
-
148
- if ( ! defined( 'WP_FS__IS_HTTP_REQUEST' ) ) {
149
- define( 'WP_FS__IS_HTTP_REQUEST', isset( $_SERVER['HTTP_HOST'] ) );
150
- }
151
-
152
- if ( ! defined( 'WP_FS__IS_HTTPS' ) ) {
153
- define( 'WP_FS__IS_HTTPS', ( WP_FS__IS_HTTP_REQUEST &&
154
- // Checks if CloudFlare's HTTPS (Flexible SSL support).
155
- isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) &&
156
- 'https' === strtolower( $_SERVER['HTTP_X_FORWARDED_PROTO'] )
157
- ) ||
158
- // Check if HTTPS request.
159
- ( isset( $_SERVER['HTTPS'] ) && 'on' == $_SERVER['HTTPS'] ) ||
160
- ( isset( $_SERVER['SERVER_PORT'] ) && 443 == $_SERVER['SERVER_PORT'] )
161
- );
162
- }
163
-
164
- if ( ! defined( 'WP_FS__IS_POST_REQUEST' ) ) {
165
- define( 'WP_FS__IS_POST_REQUEST', ( WP_FS__IS_HTTP_REQUEST &&
166
- strtoupper( $_SERVER['REQUEST_METHOD'] ) == 'POST' ) );
167
- }
168
-
169
- if ( ! defined( 'WP_FS__REMOTE_ADDR' ) ) {
170
- define( 'WP_FS__REMOTE_ADDR', fs_get_ip() );
171
- }
172
-
173
- if ( ! defined( 'WP_FS__IS_LOCALHOST' ) ) {
174
- if ( defined( 'WP_FS__LOCALHOST_IP' ) ) {
175
- define( 'WP_FS__IS_LOCALHOST', ( WP_FS__LOCALHOST_IP === WP_FS__REMOTE_ADDR ) );
176
- } else {
177
- define( 'WP_FS__IS_LOCALHOST', WP_FS__IS_HTTP_REQUEST &&
178
- is_string( WP_FS__REMOTE_ADDR ) &&
179
- ( substr( WP_FS__REMOTE_ADDR, 0, 4 ) === '127.' ||
180
- WP_FS__REMOTE_ADDR === '::1' )
181
- );
182
- }
183
- }
184
-
185
- if ( ! defined( 'WP_FS__IS_LOCALHOST_FOR_SERVER' ) ) {
186
- define( 'WP_FS__IS_LOCALHOST_FOR_SERVER', ( ! WP_FS__IS_HTTP_REQUEST ||
187
- false !== strpos( $_SERVER['HTTP_HOST'], 'localhost' ) ) );
188
- }
189
-
190
- #endregion
191
-
192
- if ( ! defined( 'WP_FS__IS_PRODUCTION_MODE' ) ) {
193
- // By default, run with Freemius production servers.
194
- define( 'WP_FS__IS_PRODUCTION_MODE', true );
195
- }
196
-
197
- if ( ! defined( 'WP_FS__ADDRESS' ) ) {
198
- define( 'WP_FS__ADDRESS', ( WP_FS__IS_PRODUCTION_MODE ? WP_FS__ADDRESS_PRODUCTION : WP_FS__ADDRESS_LOCALHOST ) );
199
- }
200
-
201
-
202
- #--------------------------------------------------------------------------------
203
- #region API
204
- #--------------------------------------------------------------------------------
205
-
206
- if ( ! defined( 'WP_FS__API_ADDRESS_LOCALHOST' ) ) {
207
- define( 'WP_FS__API_ADDRESS_LOCALHOST', 'http://api.freemius:8080' );
208
- }
209
- if ( ! defined( 'WP_FS__API_SANDBOX_ADDRESS_LOCALHOST' ) ) {
210
- define( 'WP_FS__API_SANDBOX_ADDRESS_LOCALHOST', 'http://sandbox-api.freemius:8080' );
211
- }
212
-
213
- // Set API address for local testing.
214
- if ( ! WP_FS__IS_PRODUCTION_MODE ) {
215
- if ( ! defined( 'FS_API__ADDRESS' ) ) {
216
- define( 'FS_API__ADDRESS', WP_FS__API_ADDRESS_LOCALHOST );
217
- }
218
- if ( ! defined( 'FS_API__SANDBOX_ADDRESS' ) ) {
219
- define( 'FS_API__SANDBOX_ADDRESS', WP_FS__API_SANDBOX_ADDRESS_LOCALHOST );
220
- }
221
- }
222
-
223
- #endregion
224
-
225
- #--------------------------------------------------------------------------------
226
- #region Checkout
227
- #--------------------------------------------------------------------------------
228
-
229
- if ( ! defined( 'FS_CHECKOUT__ADDRESS_PRODUCTION' ) ) {
230
- define( 'FS_CHECKOUT__ADDRESS_PRODUCTION', 'https://checkout.freemius.com' );
231
- }
232
-
233
- if ( ! defined( 'FS_CHECKOUT__ADDRESS_LOCALHOST' ) ) {
234
- define( 'FS_CHECKOUT__ADDRESS_LOCALHOST', 'http://checkout.freemius-local.com:8080' );
235
- }
236
-
237
- if ( ! defined( 'FS_CHECKOUT__ADDRESS' ) ) {
238
- define( 'FS_CHECKOUT__ADDRESS', ( WP_FS__IS_PRODUCTION_MODE ? FS_CHECKOUT__ADDRESS_PRODUCTION : FS_CHECKOUT__ADDRESS_LOCALHOST ) );
239
- }
240
-
241
- #endregion
242
-
243
- define( 'WP_FS___OPTION_PREFIX', 'fs' . ( WP_FS__IS_PRODUCTION_MODE ? '' : '_dbg' ) . '_' );
244
-
245
- if ( ! defined( 'WP_FS__ACCOUNTS_OPTION_NAME' ) ) {
246
- define( 'WP_FS__ACCOUNTS_OPTION_NAME', WP_FS___OPTION_PREFIX . 'accounts' );
247
- }
248
- if ( ! defined( 'WP_FS__API_CACHE_OPTION_NAME' ) ) {
249
- define( 'WP_FS__API_CACHE_OPTION_NAME', WP_FS___OPTION_PREFIX . 'api_cache' );
250
- }
251
- define( 'WP_FS__OPTIONS_OPTION_NAME', WP_FS___OPTION_PREFIX . 'options' );
252
-
253
- /**
254
- * Module types
255
- *
256
- * @since 1.2.2
257
- */
258
- define( 'WP_FS__MODULE_TYPE_PLUGIN', 'plugin' );
259
- define( 'WP_FS__MODULE_TYPE_THEME', 'theme' );
260
-
261
- /**
262
- * Billing Frequencies
263
- */
264
- define( 'WP_FS__PERIOD_ANNUALLY', 'annual' );
265
- define( 'WP_FS__PERIOD_MONTHLY', 'monthly' );
266
- define( 'WP_FS__PERIOD_LIFETIME', 'lifetime' );
267
-
268
- /**
269
- * Plans
270
- */
271
- define( 'WP_FS__PLAN_DEFAULT_PAID', false );
272
- define( 'WP_FS__PLAN_FREE', 'free' );
273
- define( 'WP_FS__PLAN_TRIAL', 'trial' );
274
-
275
- /**
276
- * Times in seconds
277
- */
278
- if ( ! defined( 'WP_FS__TIME_5_MIN_IN_SEC' ) ) {
279
- define( 'WP_FS__TIME_5_MIN_IN_SEC', 300 );
280
- }
281
- if ( ! defined( 'WP_FS__TIME_10_MIN_IN_SEC' ) ) {
282
- define( 'WP_FS__TIME_10_MIN_IN_SEC', 600 );
283
- }
284
  // define( 'WP_FS__TIME_15_MIN_IN_SEC', 900 );
285
- if ( ! defined( 'WP_FS__TIME_24_HOURS_IN_SEC' ) ) {
286
- define( 'WP_FS__TIME_24_HOURS_IN_SEC', 86400 );
287
- }
288
-
289
- #--------------------------------------------------------------------------------
290
- #region Debugging
291
- #--------------------------------------------------------------------------------
292
-
293
- if ( ! defined( 'WP_FS__DEBUG_SDK' ) ) {
294
- $debug_mode = get_option( 'fs_debug_mode', null );
295
-
296
- if ( $debug_mode === null ) {
297
- $debug_mode = false;
298
- add_option( 'fs_debug_mode', $debug_mode );
299
- }
300
-
301
- define( 'WP_FS__DEBUG_SDK', is_numeric( $debug_mode ) ? ( 0 < $debug_mode ) : WP_FS__DEV_MODE );
302
- }
303
-
304
- if ( ! defined( 'WP_FS__ECHO_DEBUG_SDK' ) ) {
305
- define( 'WP_FS__ECHO_DEBUG_SDK', WP_FS__DEV_MODE && ! empty( $_GET['fs_dbg_echo'] ) );
306
- }
307
- if ( ! defined( 'WP_FS__LOG_DATETIME_FORMAT' ) ) {
308
- define( 'WP_FS__LOG_DATETIME_FORMAT', 'Y-m-d H:i:s' );
309
- }
310
- if ( ! defined( 'FS_API__LOGGER_ON' ) ) {
311
- define( 'FS_API__LOGGER_ON', WP_FS__DEBUG_SDK );
312
- }
313
-
314
- if ( WP_FS__ECHO_DEBUG_SDK ) {
315
- error_reporting( E_ALL );
316
- }
317
-
318
- #endregion
319
-
320
- if ( ! defined( 'WP_FS__SCRIPT_START_TIME' ) ) {
321
- define( 'WP_FS__SCRIPT_START_TIME', time() );
322
- }
323
- if ( ! defined( 'WP_FS__DEFAULT_PRIORITY' ) ) {
324
- define( 'WP_FS__DEFAULT_PRIORITY', 10 );
325
- }
326
- if ( ! defined( 'WP_FS__LOWEST_PRIORITY' ) ) {
327
- define( 'WP_FS__LOWEST_PRIORITY', 999999999 );
328
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  <?php
2
+ /**
3
+ * @package Freemius
4
+ * @copyright Copyright (c) 2015, Freemius, Inc.
5
+ * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
6
+ * @since 1.0.4
7
+ */
8
+
9
+ if ( ! defined( 'ABSPATH' ) ) {
10
+ exit;
11
+ }
12
+
13
+ if ( ! defined( 'WP_FS__SLUG' ) ) {
14
+ define( 'WP_FS__SLUG', 'freemius' );
15
+ }
16
+ if ( ! defined( 'WP_FS__DEV_MODE' ) ) {
17
+ define( 'WP_FS__DEV_MODE', false );
18
+ }
19
+
20
+ #--------------------------------------------------------------------------------
21
+ #region API Connectivity Issues Simulation
22
+ #--------------------------------------------------------------------------------
23
+
24
+ if ( ! defined( 'WP_FS__SIMULATE_NO_API_CONNECTIVITY' ) ) {
25
+ define( 'WP_FS__SIMULATE_NO_API_CONNECTIVITY', false );
26
+ }
27
+ if ( ! defined( 'WP_FS__SIMULATE_NO_CURL' ) ) {
28
+ define( 'WP_FS__SIMULATE_NO_CURL', false );
29
+ }
30
+ if ( ! defined( 'WP_FS__SIMULATE_NO_API_CONNECTIVITY_CLOUDFLARE' ) ) {
31
+ define( 'WP_FS__SIMULATE_NO_API_CONNECTIVITY_CLOUDFLARE', false );
32
+ }
33
+ if ( ! defined( 'WP_FS__SIMULATE_NO_API_CONNECTIVITY_SQUID_ACL' ) ) {
34
+ define( 'WP_FS__SIMULATE_NO_API_CONNECTIVITY_SQUID_ACL', false );
35
+ }
36
+ if ( WP_FS__SIMULATE_NO_CURL ) {
37
+ define( 'FS_SDK__SIMULATE_NO_CURL', true );
38
+ }
39
+ if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY_CLOUDFLARE ) {
40
+ define( 'FS_SDK__SIMULATE_NO_API_CONNECTIVITY_CLOUDFLARE', true );
41
+ }
42
+ if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY_SQUID_ACL ) {
43
+ define( 'FS_SDK__SIMULATE_NO_API_CONNECTIVITY_SQUID_ACL', true );
44
+ }
45
+
46
+ #endregion
47
+
48
+ if ( ! defined( 'WP_FS__SIMULATE_FREEMIUS_OFF' ) ) {
49
+ define( 'WP_FS__SIMULATE_FREEMIUS_OFF', false );
50
+ }
51
+
52
+ if ( ! defined( 'WP_FS__PING_API_ON_IP_OR_HOST_CHANGES' ) ) {
53
+ /**
54
+ * @since 1.1.7.3
55
+ * @author Vova Feldman (@svovaf)
56
+ *
57
+ * I'm not sure if shared servers periodically change IP, or the subdomain of the
58
+ * admin dashboard. Also, I've seen sites that have strange loop of switching
59
+ * between domains on a daily basis. Therefore, to eliminate the risk of
60
+ * multiple unwanted connectivity test pings, temporary ignore domain or
61
+ * server IP changes.
62
+ */
63
+ define( 'WP_FS__PING_API_ON_IP_OR_HOST_CHANGES', false );
64
+ }
65
+
66
+ /**
67
+ * If your dev environment supports custom public network IP setup
68
+ * like VVV, please update WP_FS__LOCALHOST_IP with your public IP
69
+ * and uncomment it during dev.
70
+ */
71
+ if ( ! defined( 'WP_FS__LOCALHOST_IP' ) ) {
72
+ // VVV default public network IP.
73
+ define( 'WP_FS__VVV_DEFAULT_PUBLIC_IP', '192.168.50.4' );
74
 
75
  // define( 'WP_FS__LOCALHOST_IP', WP_FS__VVV_DEFAULT_PUBLIC_IP );
76
+ }
77
+
78
+ /**
79
+ * If true and running with secret key, the opt-in process
80
+ * will skip the email activation process which is invoked
81
+ * when the email of the context user already exist in Freemius
82
+ * database (as a security precaution, to prevent sharing user
83
+ * secret with unauthorized entity).
84
+ *
85
+ * IMPORTANT:
86
+ * AS A SECURITY PRECAUTION, WE VALIDATE THE TIMESTAMP OF THE OPT-IN REQUEST.
87
+ * THEREFORE, MAKE SURE THAT WHEN USING THIS PARAMETER,YOUR TESTING ENVIRONMENT'S
88
+ * CLOCK IS SYNCED.
89
+ */
90
+ if ( ! defined( 'WP_FS__SKIP_EMAIL_ACTIVATION' ) ) {
91
+ define( 'WP_FS__SKIP_EMAIL_ACTIVATION', false );
92
+ }
93
+
94
+
95
+ #--------------------------------------------------------------------------------
96
+ #region Directories
97
+ #--------------------------------------------------------------------------------
98
+
99
+ if ( ! defined( 'WP_FS__DIR' ) ) {
100
+ define( 'WP_FS__DIR', dirname( __FILE__ ) );
101
+ }
102
+ if ( ! defined( 'WP_FS__DIR_INCLUDES' ) ) {
103
+ define( 'WP_FS__DIR_INCLUDES', WP_FS__DIR . '/includes' );
104
+ }
105
+ if ( ! defined( 'WP_FS__DIR_TEMPLATES' ) ) {
106
+ define( 'WP_FS__DIR_TEMPLATES', WP_FS__DIR . '/templates' );
107
+ }
108
+ if ( ! defined( 'WP_FS__DIR_ASSETS' ) ) {
109
+ define( 'WP_FS__DIR_ASSETS', WP_FS__DIR . '/assets' );
110
+ }
111
+ if ( ! defined( 'WP_FS__DIR_CSS' ) ) {
112
+ define( 'WP_FS__DIR_CSS', WP_FS__DIR_ASSETS . '/css' );
113
+ }
114
+ if ( ! defined( 'WP_FS__DIR_JS' ) ) {
115
+ define( 'WP_FS__DIR_JS', WP_FS__DIR_ASSETS . '/js' );
116
+ }
117
+ if ( ! defined( 'WP_FS__DIR_IMG' ) ) {
118
+ define( 'WP_FS__DIR_IMG', WP_FS__DIR_ASSETS . '/img' );
119
+ }
120
+ if ( ! defined( 'WP_FS__DIR_SDK' ) ) {
121
+ define( 'WP_FS__DIR_SDK', WP_FS__DIR_INCLUDES . '/sdk' );
122
+ }
123
+
124
+ #endregion
125
+
126
+ /**
127
+ * Domain / URL / Address
128
+ */
129
+ define( 'WP_FS__ROOT_DOMAIN_PRODUCTION', 'freemius.com' );
130
+ define( 'WP_FS__DOMAIN_PRODUCTION', 'wp.freemius.com' );
131
+ define( 'WP_FS__ADDRESS_PRODUCTION', 'https://' . WP_FS__DOMAIN_PRODUCTION );
132
+
133
+ if ( ! defined( 'WP_FS__DOMAIN_LOCALHOST' ) ) {
134
+ define( 'WP_FS__DOMAIN_LOCALHOST', 'wp.freemius' );
135
+ }
136
+ if ( ! defined( 'WP_FS__ADDRESS_LOCALHOST' ) ) {
137
+ define( 'WP_FS__ADDRESS_LOCALHOST', 'http://' . WP_FS__DOMAIN_LOCALHOST . ':8080' );
138
+ }
139
+
140
+ if ( ! defined( 'WP_FS__TESTING_DOMAIN' ) ) {
141
+ define( 'WP_FS__TESTING_DOMAIN', 'fswp' );
142
+ }
143
+
144
+ #--------------------------------------------------------------------------------
145
+ #region HTTP
146
+ #--------------------------------------------------------------------------------
147
+
148
+ if ( ! defined( 'WP_FS__IS_HTTP_REQUEST' ) ) {
149
+ define( 'WP_FS__IS_HTTP_REQUEST', isset( $_SERVER['HTTP_HOST'] ) );
150
+ }
151
+
152
+ if ( ! defined( 'WP_FS__IS_HTTPS' ) ) {
153
+ define( 'WP_FS__IS_HTTPS', ( WP_FS__IS_HTTP_REQUEST &&
154
+ // Checks if CloudFlare's HTTPS (Flexible SSL support).
155
+ isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) &&
156
+ 'https' === strtolower( $_SERVER['HTTP_X_FORWARDED_PROTO'] )
157
+ ) ||
158
+ // Check if HTTPS request.
159
+ ( isset( $_SERVER['HTTPS'] ) && 'on' == $_SERVER['HTTPS'] ) ||
160
+ ( isset( $_SERVER['SERVER_PORT'] ) && 443 == $_SERVER['SERVER_PORT'] )
161
+ );
162
+ }
163
+
164
+ if ( ! defined( 'WP_FS__IS_POST_REQUEST' ) ) {
165
+ define( 'WP_FS__IS_POST_REQUEST', ( WP_FS__IS_HTTP_REQUEST &&
166
+ strtoupper( $_SERVER['REQUEST_METHOD'] ) == 'POST' ) );
167
+ }
168
+
169
+ if ( ! defined( 'WP_FS__REMOTE_ADDR' ) ) {
170
+ define( 'WP_FS__REMOTE_ADDR', fs_get_ip() );
171
+ }
172
+
173
+ if ( ! defined( 'WP_FS__IS_LOCALHOST' ) ) {
174
+ if ( defined( 'WP_FS__LOCALHOST_IP' ) ) {
175
+ define( 'WP_FS__IS_LOCALHOST', ( WP_FS__LOCALHOST_IP === WP_FS__REMOTE_ADDR ) );
176
+ } else {
177
+ define( 'WP_FS__IS_LOCALHOST', WP_FS__IS_HTTP_REQUEST &&
178
+ is_string( WP_FS__REMOTE_ADDR ) &&
179
+ ( substr( WP_FS__REMOTE_ADDR, 0, 4 ) === '127.' ||
180
+ WP_FS__REMOTE_ADDR === '::1' )
181
+ );
182
+ }
183
+ }
184
+
185
+ if ( ! defined( 'WP_FS__IS_LOCALHOST_FOR_SERVER' ) ) {
186
+ define( 'WP_FS__IS_LOCALHOST_FOR_SERVER', ( ! WP_FS__IS_HTTP_REQUEST ||
187
+ false !== strpos( $_SERVER['HTTP_HOST'], 'localhost' ) ) );
188
+ }
189
+
190
+ #endregion
191
+
192
+ if ( ! defined( 'WP_FS__IS_PRODUCTION_MODE' ) ) {
193
+ // By default, run with Freemius production servers.
194
+ define( 'WP_FS__IS_PRODUCTION_MODE', true );
195
+ }
196
+
197
+ if ( ! defined( 'WP_FS__ADDRESS' ) ) {
198
+ define( 'WP_FS__ADDRESS', ( WP_FS__IS_PRODUCTION_MODE ? WP_FS__ADDRESS_PRODUCTION : WP_FS__ADDRESS_LOCALHOST ) );
199
+ }
200
+
201
 
202
  #--------------------------------------------------------------------------------
203
+ #region API
204
+ #--------------------------------------------------------------------------------
205
+
206
+ if ( ! defined( 'WP_FS__API_ADDRESS_LOCALHOST' ) ) {
207
+ define( 'WP_FS__API_ADDRESS_LOCALHOST', 'http://api.freemius:8080' );
208
+ }
209
+ if ( ! defined( 'WP_FS__API_SANDBOX_ADDRESS_LOCALHOST' ) ) {
210
+ define( 'WP_FS__API_SANDBOX_ADDRESS_LOCALHOST', 'http://sandbox-api.freemius:8080' );
211
+ }
212
+
213
+ // Set API address for local testing.
214
+ if ( ! WP_FS__IS_PRODUCTION_MODE ) {
215
+ if ( ! defined( 'FS_API__ADDRESS' ) ) {
216
+ define( 'FS_API__ADDRESS', WP_FS__API_ADDRESS_LOCALHOST );
217
+ }
218
+ if ( ! defined( 'FS_API__SANDBOX_ADDRESS' ) ) {
219
+ define( 'FS_API__SANDBOX_ADDRESS', WP_FS__API_SANDBOX_ADDRESS_LOCALHOST );
220
+ }
221
+ }
222
+
223
+ #endregion
224
+
225
+ #--------------------------------------------------------------------------------
226
+ #region Checkout
227
+ #--------------------------------------------------------------------------------
228
+
229
+ if ( ! defined( 'FS_CHECKOUT__ADDRESS_PRODUCTION' ) ) {
230
+ define( 'FS_CHECKOUT__ADDRESS_PRODUCTION', 'https://checkout.freemius.com' );
231
+ }
232
+
233
+ if ( ! defined( 'FS_CHECKOUT__ADDRESS_LOCALHOST' ) ) {
234
+ define( 'FS_CHECKOUT__ADDRESS_LOCALHOST', 'http://checkout.freemius-local.com:8080' );
235
+ }
236
+
237
+ if ( ! defined( 'FS_CHECKOUT__ADDRESS' ) ) {
238
+ define( 'FS_CHECKOUT__ADDRESS', ( WP_FS__IS_PRODUCTION_MODE ? FS_CHECKOUT__ADDRESS_PRODUCTION : FS_CHECKOUT__ADDRESS_LOCALHOST ) );
239
+ }
240
+
241
+ #endregion
242
+
243
+ define( 'WP_FS___OPTION_PREFIX', 'fs' . ( WP_FS__IS_PRODUCTION_MODE ? '' : '_dbg' ) . '_' );
244
+
245
+ if ( ! defined( 'WP_FS__ACCOUNTS_OPTION_NAME' ) ) {
246
+ define( 'WP_FS__ACCOUNTS_OPTION_NAME', WP_FS___OPTION_PREFIX . 'accounts' );
247
+ }
248
+ if ( ! defined( 'WP_FS__API_CACHE_OPTION_NAME' ) ) {
249
+ define( 'WP_FS__API_CACHE_OPTION_NAME', WP_FS___OPTION_PREFIX . 'api_cache' );
250
+ }
251
+ define( 'WP_FS__OPTIONS_OPTION_NAME', WP_FS___OPTION_PREFIX . 'options' );
252
+
253
+ /**
254
+ * Module types
255
+ *
256
+ * @since 1.2.2
257
+ */
258
+ define( 'WP_FS__MODULE_TYPE_PLUGIN', 'plugin' );
259
+ define( 'WP_FS__MODULE_TYPE_THEME', 'theme' );
260
+
261
+ /**
262
+ * Billing Frequencies
263
+ */
264
+ define( 'WP_FS__PERIOD_ANNUALLY', 'annual' );
265
+ define( 'WP_FS__PERIOD_MONTHLY', 'monthly' );
266
+ define( 'WP_FS__PERIOD_LIFETIME', 'lifetime' );
267
+
268
+ /**
269
+ * Plans
270
+ */
271
+ define( 'WP_FS__PLAN_DEFAULT_PAID', false );
272
+ define( 'WP_FS__PLAN_FREE', 'free' );
273
+ define( 'WP_FS__PLAN_TRIAL', 'trial' );
274
+
275
+ /**
276
+ * Times in seconds
277
+ */
278
+ if ( ! defined( 'WP_FS__TIME_5_MIN_IN_SEC' ) ) {
279
+ define( 'WP_FS__TIME_5_MIN_IN_SEC', 300 );
280
+ }
281
+ if ( ! defined( 'WP_FS__TIME_10_MIN_IN_SEC' ) ) {
282
+ define( 'WP_FS__TIME_10_MIN_IN_SEC', 600 );
283
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284
  // define( 'WP_FS__TIME_15_MIN_IN_SEC', 900 );
285
+ if ( ! defined( 'WP_FS__TIME_24_HOURS_IN_SEC' ) ) {
286
+ define( 'WP_FS__TIME_24_HOURS_IN_SEC', 86400 );
287
+ }
288
+ if ( ! defined( 'WP_FS__TIME_WEEK_IN_SEC' ) ) {
289
+ define( 'WP_FS__TIME_WEEK_IN_SEC', 7 * WP_FS__TIME_24_HOURS_IN_SEC );
290
+ }
291
+
292
+ #--------------------------------------------------------------------------------
293
+ #region Debugging
294
+ #--------------------------------------------------------------------------------
295
+
296
+ if ( ! defined( 'WP_FS__DEBUG_SDK' ) ) {
297
+ $debug_mode = get_option( 'fs_debug_mode', null );
298
+
299
+ if ( $debug_mode === null ) {
300
+ $debug_mode = false;
301
+ add_option( 'fs_debug_mode', $debug_mode );
302
+ }
303
+
304
+ define( 'WP_FS__DEBUG_SDK', is_numeric( $debug_mode ) ? ( 0 < $debug_mode ) : WP_FS__DEV_MODE );
305
+ }
306
+
307
+ if ( ! defined( 'WP_FS__ECHO_DEBUG_SDK' ) ) {
308
+ define( 'WP_FS__ECHO_DEBUG_SDK', WP_FS__DEV_MODE && ! empty( $_GET['fs_dbg_echo'] ) );
309
+ }
310
+ if ( ! defined( 'WP_FS__LOG_DATETIME_FORMAT' ) ) {
311
+ define( 'WP_FS__LOG_DATETIME_FORMAT', 'Y-m-d H:i:s' );
312
+ }
313
+ if ( ! defined( 'FS_API__LOGGER_ON' ) ) {
314
+ define( 'FS_API__LOGGER_ON', WP_FS__DEBUG_SDK );
315
+ }
316
+
317
+ if ( WP_FS__ECHO_DEBUG_SDK ) {
318
+ error_reporting( E_ALL );
319
+ }
320
+
321
+ #endregion
322
+
323
+ if ( ! defined( 'WP_FS__SCRIPT_START_TIME' ) ) {
324
+ define( 'WP_FS__SCRIPT_START_TIME', time() );
325
+ }
326
+ if ( ! defined( 'WP_FS__DEFAULT_PRIORITY' ) ) {
327
+ define( 'WP_FS__DEFAULT_PRIORITY', 10 );
328
+ }
329
+ if ( ! defined( 'WP_FS__LOWEST_PRIORITY' ) ) {
330
+ define( 'WP_FS__LOWEST_PRIORITY', 999999999 );
331
+ }
332
+
333
+ #--------------------------------------------------------------------------------
334
+ #region Multisite Network
335
+ #--------------------------------------------------------------------------------
336
+
337
+ /**
338
+ * Do not use this define directly, it will have the wrong value
339
+ * during plugin uninstall/deletion when the inclusion of the plugin
340
+ * is triggered due to registration with register_uninstall_hook().
341
+ *
342
+ * Instead, use fs_is_network_admin().
343
+ *
344
+ * @author Vova Feldman (@svovaf)
345
+ */
346
+ if ( ! defined( 'WP_FS__IS_NETWORK_ADMIN' ) ) {
347
+ define( 'WP_FS__IS_NETWORK_ADMIN',
348
+ is_network_admin() ||
349
+ ( is_multisite() &&
350
+ ( ( defined( 'DOING_AJAX' ) && DOING_AJAX &&
351
+ ( isset( $_REQUEST['_fs_network_admin'] ) /*||
352
+ ( ! empty( $_REQUEST['action'] ) && 'delete-plugin' === $_REQUEST['action'] )*/ )
353
+ ) ||
354
+ // Plugin uninstall.
355
+ defined( 'WP_UNINSTALL_PLUGIN' ) )
356
+ )
357
+ );
358
+ }
359
+
360
+ /**
361
+ * Do not use this define directly, it will have the wrong value
362
+ * during plugin uninstall/deletion when the inclusion of the plugin
363
+ * is triggered due to registration with register_uninstall_hook().
364
+ *
365
+ * Instead, use fs_is_blog_admin().
366
+ *
367
+ * @author Vova Feldman (@svovaf)
368
+ */
369
+ if ( ! defined( 'WP_FS__IS_BLOG_ADMIN' ) ) {
370
+ define( 'WP_FS__IS_BLOG_ADMIN', is_blog_admin() || ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_REQUEST['_fs_blog_admin'] ) ) );
371
+ }
372
+
373
+ if ( ! defined( 'WP_FS__SHOW_NETWORK_EVEN_WHEN_DELEGATED' ) ) {
374
+ // Set to true to show network level settings even if delegated to site admins.
375
+ define( 'WP_FS__SHOW_NETWORK_EVEN_WHEN_DELEGATED', false );
376
+ }
377
+
378
+ #endregion
sdk/freemius/includes/class-freemius.php CHANGED
@@ -1,14304 +1,19900 @@
1
  <?php
2
- /**
3
- * @package Freemius
4
- * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
6
- * @since 1.0.3
7
- */
8
- if ( ! defined( 'ABSPATH' ) ) {
9
- exit;
10
- }
11
-
12
- // "final class"
13
- class Freemius extends Freemius_Abstract {
14
- /**
15
- * SDK Version
16
- *
17
- * @var string
18
- */
19
- public $version = WP_FS__SDK_VERSION;
20
-
21
- #region Plugin Info
22
-
23
- /**
24
- * @since 1.0.1
25
- *
26
- * @var string
27
- */
28
- private $_slug;
29
-
30
- /**
31
- * @since 1.0.0
32
- *
33
- * @var string
34
- */
35
- private $_plugin_basename;
36
- /**
37
- * @since 1.0.0
38
- *
39
- * @var string
40
- */
41
- private $_free_plugin_basename;
42
- /**
43
- * @since 1.0.0
44
- *
45
- * @var string
46
- */
47
- private $_plugin_dir_path;
48
- /**
49
- * @since 1.0.0
50
- *
51
- * @var string
52
- */
53
- private $_plugin_dir_name;
54
- /**
55
- * @since 1.0.0
56
- *
57
- * @var string
58
- */
59
- private $_plugin_main_file_path;
60
- /**
61
- * @var string[]
62
- */
63
- private $_plugin_data;
64
- /**
65
- * @since 1.0.9
66
- *
67
- * @var string
68
- */
69
- private $_plugin_name;
70
- /**
71
- * @since 1.2.2
72
- *
73
- * @var string
74
- */
75
- private $_module_type;
76
-
77
- #endregion Plugin Info
78
-
79
- /**
80
- * @since 1.0.9
81
- *
82
- * @var bool If false, don't turn Freemius on.
83
- */
84
- private $_is_on;
85
-
86
- /**
87
- * @since 1.1.3
88
- *
89
- * @var bool If false, don't turn Freemius on.
90
- */
91
- private $_is_anonymous;
92
-
93
- /**
94
- * @since 1.0.9
95
- * @var bool If false, issues with connectivity to Freemius API.
96
- */
97
- private $_has_api_connection;
98
-
99
- /**
100
- * @since 1.0.9
101
- * @var bool Hints the SDK if plugin can support anonymous mode (if skip connect is visible).
102
- */
103
- private $_enable_anonymous;
104
-
105
- /**
106
- * @since 1.1.7.5
107
- * @var bool Hints the SDK if plugin should run in anonymous mode (only adds feedback form).
108
- */
109
- private $_anonymous_mode;
110
-
111
- /**
112
- * @since 1.1.9
113
- * @var bool Hints the SDK if plugin have any free plans.
114
- */
115
- private $_is_premium_only;
116
-
117
- /**
118
- * @since 1.2.1.6
119
- * @var bool Hints the SDK if plugin have premium code version at all.
120
- */
121
- private $_has_premium_version;
122
-
123
- /**
124
- * @since 1.2.1.6
125
- * @var bool Hints the SDK if plugin should ignore pending mode by simulating a skip.
126
- */
127
- private $_ignore_pending_mode;
128
-
129
- /**
130
- * @since 1.0.8
131
- * @var bool Hints the SDK if the plugin has any paid plans.
132
- */
133
- private $_has_paid_plans;
134
-
135
- /**
136
- * @since 1.2.1.5
137
- * @var int Hints the SDK if the plugin offers a trial period. If negative, no trial, if zero - has a trial but
138
- * without a specified period, if positive - the number of trial days.
139
- */
140
- private $_trial_days = - 1;
141
-
142
- /**
143
- * @since 1.2.1.5
144
- * @var bool Hints the SDK if the trial requires a payment method or not.
145
- */
146
- private $_is_trial_require_payment = false;
147
-
148
- /**
149
- * @since 1.0.7
150
- * @var bool Hints the SDK if the plugin is WordPress.org compliant.
151
- */
152
- private $_is_org_compliant;
153
-
154
- /**
155
- * @since 1.0.7
156
- * @var bool Hints the SDK if the plugin is has add-ons.
157
- */
158
- private $_has_addons;
159
-
160
- /**
161
- * @since 1.1.6
162
- * @var string[]bool.
163
- */
164
- private $_permissions;
165
-
166
- /**
167
- * @var FS_Key_Value_Storage
168
- */
169
- private $_storage;
170
-
171
- /**
172
- * @since 1.2.2.7
173
- * @var FS_Cache_Manager
174
- */
175
- private $_cache;
176
-
177
- /**
178
- * @since 1.0.0
179
- *
180
- * @var FS_Logger
181
- */
182
- private $_logger;
183
- /**
184
- * @since 1.0.4
185
- *
186
- * @var FS_Plugin
187
- */
188
- private $_plugin = false;
189
- /**
190
- * @since 1.0.4
191
- *
192
- * @var FS_Plugin|false
193
- */
194
- private $_parent_plugin = false;
195
- /**
196
- * @since 1.1.1
197
- *
198
- * @var Freemius
199
- */
200
- private $_parent = false;
201
- /**
202
- * @since 1.0.1
203
- *
204
- * @var FS_User
205
- */
206
- private $_user = false;
207
- /**
208
- * @since 1.0.1
209
- *
210
- * @var FS_Site
211
- */
212
- private $_site = false;
213
- /**
214
- * @since 1.0.1
215
- *
216
- * @var FS_Plugin_License
217
- */
218
- private $_license;
219
- /**
220
- * @since 1.0.2
221
- *
222
- * @var FS_Plugin_Plan[]
223
- */
224
- private $_plans = false;
225
- /**
226
- * @var FS_Plugin_License[]
227
- * @since 1.0.5
228
- */
229
- private $_licenses = false;
230
-
231
- /**
232
- * @since 1.0.1
233
- *
234
- * @var FS_Admin_Menu_Manager
235
- */
236
- private $_menu;
237
-
238
- /**
239
- * @var FS_Admin_Notice_Manager
240
- */
241
- private $_admin_notices;
242
-
243
- /**
244
- * @since 1.1.6
245
- *
246
- * @var FS_Admin_Notice_Manager
247
- */
248
- private static $_global_admin_notices;
249
-
250
- /**
251
- * @var FS_Logger
252
- * @since 1.0.0
253
- */
254
- private static $_static_logger;
255
-
256
- /**
257
- * @var FS_Option_Manager
258
- * @since 1.0.2
259
- */
260
- private static $_accounts;
261
-
262
- /**
263
- * @since 1.2.2
264
- *
265
- * @var number
266
- */
267
- private $_module_id;
268
-
269
- /**
270
- * @var Freemius[]
271
- */
272
- private static $_instances = array();
273
-
274
  /**
275
- * @author Leo Fajardo (@leorw)
276
  *
277
- * @since 1.2.3
 
 
 
 
 
 
 
278
  *
279
- * @var FS_Affiliate
280
  */
281
- private $affiliate = null;
282
 
283
  /**
284
- * @author Leo Fajardo (@leorw)
285
  *
286
- * @since 1.2.3
 
 
 
 
287
  *
288
- * @var FS_AffiliateTerms
289
  */
290
- private $plugin_affiliate_terms = null;
291
-
292
  /**
293
- * @author Leo Fajardo (@leorw)
 
 
 
 
 
 
294
  *
295
- * @since 1.2.3
 
 
 
 
296
  *
297
- * @var FS_AffiliateTerms
298
  */
299
- private $custom_affiliate_terms = null;
300
-
301
- #region Uninstall Reasons IDs
302
-
303
- const REASON_NO_LONGER_NEEDED = 1;
304
- const REASON_FOUND_A_BETTER_PLUGIN = 2;
305
- const REASON_NEEDED_FOR_A_SHORT_PERIOD = 3;
306
- const REASON_BROKE_MY_SITE = 4;
307
- const REASON_SUDDENLY_STOPPED_WORKING = 5;
308
- const REASON_CANT_PAY_ANYMORE = 6;
309
- const REASON_OTHER = 7;
310
- const REASON_DIDNT_WORK = 8;
311
- const REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION = 9;
312
- const REASON_COULDNT_MAKE_IT_WORK = 10;
313
- const REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE = 11;
314
- const REASON_NOT_WORKING = 12;
315
- const REASON_NOT_WHAT_I_WAS_LOOKING_FOR = 13;
316
- const REASON_DIDNT_WORK_AS_EXPECTED = 14;
317
- const REASON_TEMPORARY_DEACTIVATION = 15;
318
-
319
- #endregion
320
-
321
- /* Ctor
322
- ------------------------------------------------------------------------------------------------------------------*/
323
 
324
- /**
325
- * Main singleton instance.
326
- *
327
- * @author Vova Feldman (@svovaf)
328
- * @since 1.0.0
329
- *
330
- * @param number $module_id
331
- * @param string|bool $slug
332
- * @param bool $is_init Since 1.2.1 Is initiation sequence.
333
- */
334
- private function __construct( $module_id, $slug = false, $is_init = false ) {
335
- if ( $is_init && is_numeric( $module_id ) && is_string( $slug ) ) {
336
- $this->store_id_slug_type_path_map( $module_id, $slug );
337
- }
338
-
339
- $this->_module_id = $module_id;
340
- $this->_slug = $this->get_slug();
341
- $this->_module_type = $this->get_module_type();
342
-
343
- $this->_storage = FS_Key_Value_Storage::instance( $this->_module_type . '_data', $this->_slug );
344
- $this->_cache = FS_Cache_Manager::get_manager( WP_FS___OPTION_PREFIX . "cache_{$module_id}" );
345
-
346
- $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $this->get_unique_affix(), WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
347
-
348
- $this->_plugin_main_file_path = $this->_find_caller_plugin_file( $is_init );
349
- $this->_plugin_dir_path = plugin_dir_path( $this->_plugin_main_file_path );
350
- $this->_plugin_basename = $this->get_plugin_basename();
351
- $this->_free_plugin_basename = str_replace( '-premium/', '/', $this->_plugin_basename );
352
-
353
- $base_name_split = explode( '/', $this->_plugin_basename );
354
- $this->_plugin_dir_name = $base_name_split[0];
355
-
356
- if ( $this->_logger->is_on() ) {
357
- $this->_logger->info( 'plugin_main_file_path = ' . $this->_plugin_main_file_path );
358
- $this->_logger->info( 'plugin_dir_path = ' . $this->_plugin_dir_path );
359
- $this->_logger->info( 'plugin_basename = ' . $this->_plugin_basename );
360
- $this->_logger->info( 'free_plugin_basename = ' . $this->_free_plugin_basename );
361
- $this->_logger->info( 'plugin_dir_name = ' . $this->_plugin_dir_name );
362
- }
363
-
364
- // Remember link between file to slug.
365
- $this->store_file_slug_map();
366
-
367
- // Store plugin's initial install timestamp.
368
- if ( ! isset( $this->_storage->install_timestamp ) ) {
369
- $this->_storage->install_timestamp = WP_FS__SCRIPT_START_TIME;
370
- }
371
-
372
- if ( ! is_object( $this->_plugin ) ) {
373
- $this->_plugin = FS_Plugin_Manager::instance( $this->_module_id )->get();
374
- }
375
-
376
- $this->_admin_notices = FS_Admin_Notice_Manager::instance(
377
- $this->_slug . ( $this->is_theme() ? ':theme' : '' ),
378
- /**
379
- * Ensure that the admin notice will always have a title by using the stored plugin title if available and
380
- * retrieving the title via the "get_plugin_name" method if there is no stored plugin title available.
381
- *
382
- * @author Leo Fajardo (@leorw)
383
- * @since 1.2.2
384
- */
385
- ( is_object( $this->_plugin ) ? $this->_plugin->title : $this->get_plugin_name() ),
386
- $this->get_unique_affix()
387
- );
388
-
389
- if ( 'true' === fs_request_get( 'fs_clear_api_cache' ) ||
390
- 'true' === fs_request_is_action( 'restart_freemius' )
391
- ) {
392
- FS_Api::clear_cache();
393
- $this->_cache->clear();
394
- }
395
-
396
- $this->_register_hooks();
397
-
398
- $this->_load_account();
399
-
400
- $this->_version_updates_handler();
401
- }
402
-
403
- /**
404
- * Checks whether this module has a settings menu.
405
- *
406
- * @author Leo Fajardo (@leorw)
407
- * @since 1.2.2
408
- *
409
- * @return bool
410
- */
411
- function has_settings_menu() {
412
- return $this->_menu->has_menu();
413
- }
414
-
415
- /**
416
- * Check if the context module is free wp.org theme.
417
- *
418
- * This method is helpful because:
419
- * 1. wp.org themes are limited to a single submenu item,
420
- * and sub-submenu items are most likely not allowed (never verified).
421
- * 2. wp.org themes are not allowed to redirect the user
422
- * after the theme activation, therefore, the agreed UX
423
- * is showing the opt-in as a modal dialog box after
424
- * activation (approved by @otto42, @emiluzelac, @greenshady, @grapplerulrich).
425
- *
426
- * @author Vova Feldman (@svovaf)
427
- * @since 1.2.2.7
428
- *
429
- * @return bool
430
- */
431
- function is_free_wp_org_theme() {
432
- return (
433
- $this->is_theme() &&
434
- $this->is_org_repo_compliant() &&
435
- ! $this->is_premium()
436
- );
437
- }
438
-
439
- /**
440
- * Checks whether this a submenu item is visible.
441
- *
442
- * @author Vova Feldman (@svovaf)
443
- * @since 1.2.2.6
444
- * @since 1.2.2.7 Even if the menu item was specified to be hidden, when it is the context page, then show the submenu item so the user will have the right context page.
445
- *
446
- * @param string $slug
447
- *
448
- * @return bool
449
- */
450
- function is_submenu_item_visible( $slug ) {
451
- if ( $this->is_admin_page( $slug ) ) {
452
- /**
453
- * It is the current context page, so show the submenu item
454
- * so the user will have the right context page, even if it
455
- * was set to hidden.
456
- */
457
- return true;
458
- }
459
-
460
- if ( ! $this->has_settings_menu() ) {
461
- // No menu settings at all.
462
- return false;
463
- }
464
-
465
- if ( $this->is_free_wp_org_theme() ) {
466
- /**
467
- * wp.org themes are limited to a single submenu item, and
468
- * sub-submenu items are most likely not allowed (never verified).
469
- */
470
- return false;
471
- }
472
-
473
- return $this->_menu->is_submenu_item_visible( $slug );
474
- }
475
-
476
- /**
477
- * Check if a Freemius page should be accessible via the UI.
478
- *
479
- * @author Vova Feldman (@svovaf)
480
- * @since 1.2.2.7
481
- *
482
- * @param string $slug
483
- *
484
- * @return bool
485
- */
486
- function is_page_visible( $slug ) {
487
- if ( $this->is_admin_page( $slug ) ) {
488
- return true;
489
- }
490
-
491
- return $this->_menu->is_submenu_item_visible( $slug, true, true );
492
- }
493
-
494
- /**
495
- * @author Vova Feldman (@svovaf)
496
- * @since 1.0.9
497
- */
498
- private function _version_updates_handler() {
499
- if ( ! isset( $this->_storage->sdk_version ) || $this->_storage->sdk_version != $this->version ) {
500
- // Freemius version upgrade mode.
501
- $this->_storage->sdk_last_version = $this->_storage->sdk_version;
502
- $this->_storage->sdk_version = $this->version;
503
-
504
- if ( empty( $this->_storage->sdk_last_version ) ||
505
- version_compare( $this->_storage->sdk_last_version, $this->version, '<' )
506
- ) {
507
- $this->_storage->sdk_upgrade_mode = true;
508
- $this->_storage->sdk_downgrade_mode = false;
509
- } else {
510
- $this->_storage->sdk_downgrade_mode = true;
511
- $this->_storage->sdk_upgrade_mode = false;
512
-
513
- }
514
-
515
- $this->do_action( 'sdk_version_update', $this->_storage->sdk_last_version, $this->version );
516
- }
517
-
518
- $plugin_version = $this->get_plugin_version();
519
- if ( ! isset( $this->_storage->plugin_version ) || $this->_storage->plugin_version != $plugin_version ) {
520
- // Plugin version upgrade mode.
521
- $this->_storage->plugin_last_version = $this->_storage->plugin_version;
522
- $this->_storage->plugin_version = $plugin_version;
523
-
524
- if ( empty( $this->_storage->plugin_last_version ) ||
525
- version_compare( $this->_storage->plugin_last_version, $plugin_version, '<' )
526
- ) {
527
- $this->_storage->plugin_upgrade_mode = true;
528
- $this->_storage->plugin_downgrade_mode = false;
529
- } else {
530
- $this->_storage->plugin_downgrade_mode = true;
531
- $this->_storage->plugin_upgrade_mode = false;
532
- }
533
-
534
- if ( ! empty( $this->_storage->plugin_last_version ) ) {
535
- // Different version of the plugin was installed before, therefore it's an update.
536
- $this->_storage->is_plugin_new_install = false;
537
- }
538
-
539
- $this->do_action( 'plugin_version_update', $this->_storage->plugin_last_version, $plugin_version );
540
- }
541
- }
542
-
543
- /**
544
- * @author Vova Feldman (@svovaf)
545
- * @since 1.1.5
546
- *
547
- * @param string $sdk_prev_version
548
- * @param string $sdk_version
549
- */
550
- function _data_migration( $sdk_prev_version, $sdk_version ) {
551
- /**
552
- * @since 1.1.7.3 Fixed unwanted connectivity test cleanup.
553
- */
554
- if ( empty( $sdk_prev_version ) ) {
555
- return;
556
- }
557
 
558
- if ( version_compare( $sdk_prev_version, '1.2.3', '<' ) &&
559
- version_compare( $sdk_version, '1.2.3', '>=' )
560
- ) {
561
- /**
562
- * Starting from version 1.2.3, paths are stored as relative paths and not absolute paths; so when
563
- * upgrading to 1.2.3, make paths relative.
564
- *
565
- * @author Leo Fajardo (@leorw)
566
- */
567
- $this->make_paths_relative();
568
- }
569
 
570
- if ( version_compare( $sdk_prev_version, '1.1.5', '<' ) &&
571
- version_compare( $sdk_version, '1.1.5', '>=' )
572
- ) {
573
- // On version 1.1.5 merged connectivity and is_on data.
574
- if ( isset( $this->_storage->connectivity_test ) ) {
575
- if ( ! isset( $this->_storage->is_on ) ) {
576
- unset( $this->_storage->connectivity_test );
577
- } else {
578
- $connectivity_data = $this->_storage->connectivity_test;
579
- $connectivity_data['is_active'] = $this->_storage->is_on['is_active'];
580
- $connectivity_data['timestamp'] = $this->_storage->is_on['timestamp'];
581
 
582
- // Override.
583
- $this->_storage->connectivity_test = $connectivity_data;
 
 
 
584
 
585
- // Remove previous structure.
586
- unset( $this->_storage->is_on );
587
- }
 
 
 
588
 
589
- }
590
- }
591
- }
 
 
592
 
593
  /**
594
- * Makes paths relative.
595
- *
596
- * @author Leo Fajardo
597
- * @since 1.2.3
598
  */
599
- private function make_paths_relative() {
600
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
601
 
602
- if ( isset( $id_slug_type_path_map[ $this->_module_id ]['path'] ) ) {
603
- $id_slug_type_path_map[ $this->_module_id ]['path'] = $this->get_relative_path( $id_slug_type_path_map[ $this->_module_id ]['path'] );
 
 
 
604
 
605
- self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
606
- }
 
 
 
607
 
608
- if ( isset( $this->_storage->plugin_main_file ) ) {
609
- $plugin_main_file = $this->_storage->plugin_main_file;
 
 
 
610
 
611
- if ( isset( $plugin_main_file->path ) ) {
612
- $this->_storage->plugin_main_file->path = $this->get_relative_path( $this->_storage->plugin_main_file->path );
613
- } else if ( isset( $plugin_main_file->prev_path ) ) {
614
- $this->_storage->plugin_main_file->prev_path = $this->get_relative_path( $this->_storage->plugin_main_file->prev_path );
615
- }
616
- }
617
 
618
- // Remove invalid path that is still associated with the current slug if there's any.
619
- $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
620
- foreach ( $file_slug_map as $plugin_basename => $slug ) {
621
- if ( $slug === $this->_slug &&
622
- $plugin_basename !== $this->_plugin_basename &&
623
- ! file_exists( $this->get_absolute_path( $plugin_basename ) )
624
- ) {
625
- unset( $file_slug_map[ $plugin_basename ] );
626
- self::$_accounts->set_option( 'file_slug_map', $file_slug_map, true );
627
 
628
- break;
629
- }
630
- }
631
- }
 
632
 
633
- /**
634
- * @author Vova Feldman (@svovaf)
635
- * @since 1.2.2.7
636
- *
637
- * @param string $plugin_prev_version
638
- * @param string $plugin_version
639
- */
640
- function _after_version_update( $plugin_prev_version, $plugin_version ) {
641
- if ( $this->is_theme() ) {
642
- // Expire the cache of the previous tabs since the theme may
643
- // have setting updates.
644
- $this->_cache->expire( 'tabs' );
645
- $this->_cache->expire( 'tabs_stylesheets' );
646
- }
647
- }
648
-
649
- /**
650
- * This action is connected to the 'plugins_loaded' hook and helps to determine
651
- * if this is a new plugin installation or a plugin update.
652
- *
653
- * There are 3 different use-cases:
654
- * 1) New plugin installation right with Freemius:
655
- * 1.1 _activate_plugin_event_hook() will be executed first
656
- * 1.2 Since $this->_storage->is_plugin_new_install is not set,
657
- * and $this->_storage->plugin_last_version is not set,
658
- * $this->_storage->is_plugin_new_install will be set to TRUE.
659
- * 1.3 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will
660
- * be already set to TRUE.
661
- *
662
- * 2) Plugin update, didn't have Freemius before, and now have the SDK:
663
- * 2.1 _activate_plugin_event_hook() will not be executed, because
664
- * the activation hook do NOT fires on updates since WP 3.1.
665
- * 2.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will
666
- * be empty, therefore, it will be set to FALSE.
667
- *
668
- * 3) Plugin update, had Freemius in prev version as well:
669
- * 3.1 _version_updates_handler() will be executed 1st, since FS was installed
670
- * before, $this->_storage->plugin_last_version will NOT be empty,
671
- * therefore, $this->_storage->is_plugin_new_install will be set to FALSE.
672
- * 3.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install is
673
- * already set, therefore, it will not be modified.
674
- *
675
- * Use-case #3 is backward compatible, #3.1 will be executed since 1.0.9.
676
- *
677
- * NOTE:
678
- * The only fallback of this mechanism is if an admin updates a plugin based on use-case #2,
679
- * and then, the next immediate PageView is the plugin's main settings page, it will not
680
- * show the opt-in right away. The reason it will happen is because Freemius execution
681
- * will be turned off till the plugin is fully loaded at least once
682
- * (till $this->_storage->was_plugin_loaded is TRUE).
683
- *
684
- * @author Vova Feldman (@svovaf)
685
- * @since 1.1.9
686
- *
687
- */
688
- function _plugins_loaded() {
689
- // Update flag that plugin was loaded with Freemius at least once.
690
- $this->_storage->was_plugin_loaded = true;
691
-
692
- /**
693
- * Bug fix - only set to false when it's a plugin, due to the
694
- * execution sequence of the theme hooks and our methods, if
695
- * this will be set for themes, Freemius will always assume
696
- * it's a theme update.
697
- *
698
- * @author Vova Feldman (@svovaf)
699
- * @since 1.2.2.2
700
- */
701
- if ( $this->is_plugin() &&
702
- ! isset( $this->_storage->is_plugin_new_install )
703
- ) {
704
- $this->_storage->is_plugin_new_install = false;
705
- }
706
- }
707
-
708
- /**
709
- * @author Vova Feldman (@svovaf)
710
- * @since 1.0.9
711
- */
712
- private function _register_hooks() {
713
- $this->_logger->entrance();
714
-
715
- if ( is_admin() ) {
716
- if ( $this->is_plugin() ) {
717
- $plugin_dir = dirname( $this->_plugin_dir_path ) . '/';
718
-
719
- /**
720
- * @since 1.2.2
721
- *
722
- * Hook to both free and premium version activations to support
723
- * auto deactivation on the other version activation.
724
- */
725
- register_activation_hook(
726
- $plugin_dir . $this->_free_plugin_basename,
727
- array( &$this, '_activate_plugin_event_hook' )
728
- );
729
-
730
- register_activation_hook(
731
- $plugin_dir . $this->premium_plugin_basename(),
732
- array( &$this, '_activate_plugin_event_hook' )
733
- );
734
- } else {
735
- add_action( 'after_switch_theme', array( &$this, '_activate_theme_event_hook' ), 10, 2 );
736
-
737
- /**
738
- * Include the required hooks to capture the theme settings' page tabs
739
- * and cache them.
740
- *
741
- * @author Vova Feldman (@svovaf)
742
- * @since 1.2.2.7
743
- */
744
- if ( ! $this->_cache->has_valid( 'tabs' ) ) {
745
- add_action( 'admin_footer', array( &$this, '_tabs_capture' ) );
746
- // Add license activation AJAX callback.
747
- $this->add_ajax_action( 'store_tabs', array( &$this, '_store_tabs_ajax_action' ) );
748
-
749
- add_action( 'admin_enqueue_scripts', array( &$this, '_store_tabs_styles' ), 9999999 );
750
- }
751
-
752
- add_action(
753
- 'admin_footer',
754
- array( &$this, '_add_freemius_tabs' ),
755
- /**
756
- * The tabs JS code must be executed after the tabs capture logic (_tabs_capture()).
757
- * That's why the priority is 11 while the tabs capture logic is added
758
- * with priority 10.
759
- *
760
- * @author Vova Feldman (@svovaf)
761
- */
762
- 11
763
- );
764
-
765
- add_action( 'admin_footer', array( &$this, '_style_premium_theme' ) );
766
- }
767
-
768
- /**
769
- * Part of the mechanism to identify new plugin install vs. plugin update.
770
- *
771
- * @author Vova Feldman (@svovaf)
772
- * @since 1.1.9
773
- */
774
- if ( empty( $this->_storage->was_plugin_loaded ) ) {
775
- if ( $this->is_plugin() &&
776
- $this->is_activation_mode( false )
777
- ) {
778
- add_action( 'plugins_loaded', array( &$this, '_plugins_loaded' ) );
779
- } else {
780
- // If was activated before, then it was already loaded before.
781
- $this->_plugins_loaded();
782
- }
783
- }
784
-
785
- if ( ! self::is_ajax() ) {
786
- if ( ! $this->is_addon() ) {
787
- add_action( 'init', array( &$this, '_add_default_submenu_items' ), WP_FS__LOWEST_PRIORITY );
788
- }
789
- }
790
- }
791
-
792
- if ( $this->is_plugin() ) {
793
- register_deactivation_hook( $this->_plugin_main_file_path, array( &$this, '_deactivate_plugin_hook' ) );
794
- }
795
-
796
- if ( $this->is_theme() && self::is_customizer() ) {
797
- // Register customizer upsell.
798
- add_action( 'customize_register', array( &$this, '_customizer_register' ) );
799
- }
800
-
801
- add_action( 'init', array( &$this, '_redirect_on_clicked_menu_link' ), WP_FS__LOWEST_PRIORITY );
802
-
803
- add_action( 'admin_init', array( &$this, '_add_tracking_links' ) );
804
- add_action( 'admin_init', array( &$this, '_add_license_activation' ) );
805
- $this->add_ajax_action( 'update_billing', array( &$this, '_update_billing_ajax_action' ) );
806
- $this->add_ajax_action( 'start_trial', array( &$this, '_start_trial_ajax_action' ) );
807
-
808
- $this->add_ajax_action( 'install_premium_version', array(
809
- &$this,
810
- '_install_premium_version_ajax_action'
811
- ) );
812
 
813
- $this->add_ajax_action( 'submit_affiliate_application', array( &$this, '_submit_affiliate_application' ) );
 
 
 
 
814
 
815
- $this->add_action( 'after_plans_sync', array( &$this, '_check_for_trial_plans' ) );
 
 
 
816
 
817
- $this->add_action( 'sdk_version_update', array( &$this, '_data_migration' ), WP_FS__DEFAULT_PRIORITY, 2 );
818
- $this->add_action( 'plugin_version_update', array( &$this, '_after_version_update' ), WP_FS__DEFAULT_PRIORITY, 2 );
819
- $this->add_filter( 'after_code_type_change', array( &$this, '_after_code_type_change' ) );
 
 
820
 
821
- add_action( 'admin_init', array( &$this, '_add_trial_notice' ) );
822
- add_action( 'admin_init', array( &$this, '_add_affiliate_program_notice' ) );
823
- add_action( 'admin_init', array( &$this, '_enqueue_common_css' ) );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
824
 
825
- /**
826
- * Handle request to reset anonymous mode for `get_reconnect_url()`.
827
- *
828
- * @author Vova Feldman (@svovaf)
829
- * @since 1.2.1.5
830
- */
831
- if ( fs_request_is_action( 'reset_anonymous_mode' ) &&
832
- $this->get_unique_affix() === fs_request_get( 'fs_unique_affix' )
833
- ) {
834
- add_action( 'admin_init', array( &$this, 'connect_again' ) );
835
- }
836
- }
837
-
838
- /**
839
- * Keeping the uninstall hook registered for free or premium plugin version may result to a fatal error that
840
- * could happen when a user tries to uninstall either version while one of them is still active. Uninstalling a
841
- * plugin will trigger inclusion of the free or premium version and if one of them is active during the
842
- * uninstallation, a fatal error may occur in case the plugin's class or functions are already defined.
843
- *
844
- * @author Leo Fajardo (leorw)
845
- *
846
- * @since 1.2.0
847
- */
848
- private function unregister_uninstall_hook() {
849
- $uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
850
- unset( $uninstallable_plugins[ $this->_free_plugin_basename ] );
851
- unset( $uninstallable_plugins[ $this->premium_plugin_basename() ] );
852
-
853
- update_option( 'uninstall_plugins', $uninstallable_plugins );
854
- }
855
-
856
- /**
857
- * @since 1.2.0 Invalidate module's main file cache, otherwise, FS_Plugin_Updater will not fetch updates.
858
- */
859
- private function clear_module_main_file_cache() {
860
- if ( ! isset( $this->_storage->plugin_main_file ) ||
861
- empty( $this->_storage->plugin_main_file->path )
862
- ) {
863
- return;
864
- }
865
-
866
- $plugin_main_file = clone $this->_storage->plugin_main_file;
867
-
868
- // Store cached path (2nd layer cache).
869
- $plugin_main_file->prev_path = $plugin_main_file->path;
870
-
871
- // Clear cached path.
872
- unset( $plugin_main_file->path );
873
-
874
- $this->_storage->plugin_main_file = $plugin_main_file;
875
-
876
- /**
877
- * Clear global cached path.
878
- *
879
- * @author Leo Fajardo (@leorw)
880
- * @since 1.2.2
881
- */
882
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map' );
883
- unset( $id_slug_type_path_map[ $this->_module_id ]['path'] );
884
- self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
885
- }
886
-
887
- /**
888
- * @author Vova Feldman (@svovaf)
889
- * @since 1.0.9
890
- */
891
- private function _register_account_hooks() {
892
- if ( ! is_admin() ) {
893
- return;
894
- }
895
-
896
- /**
897
- * Always show the deactivation feedback form since we added
898
- * automatic free version deactivation upon premium code activation.
899
- *
900
- * @since 1.2.1.6
901
- */
902
- $this->add_ajax_action(
903
- 'submit_uninstall_reason',
904
- array( &$this, '_submit_uninstall_reason_action' )
905
- );
906
-
907
- if ( ( $this->is_plugin() && self::is_plugins_page() ) ||
908
- ( $this->is_theme() && self::is_themes_page() )
909
- ) {
910
- add_action( 'admin_footer', array( &$this, '_add_deactivation_feedback_dialog_box' ) );
911
- }
912
- }
913
-
914
- /**
915
- * Leverage backtrace to find caller plugin file path.
916
- *
917
- * @author Vova Feldman (@svovaf)
918
- * @since 1.0.6
919
- *
920
- * @param bool $is_init Is initiation sequence.
921
- *
922
- * @return string
923
- */
924
- private function _find_caller_plugin_file( $is_init = false ) {
925
- // Try to load the cached value of the file path.
926
- if ( isset( $this->_storage->plugin_main_file ) ) {
927
- $plugin_main_file = $this->_storage->plugin_main_file;
928
- if ( isset( $plugin_main_file->path ) ) {
929
- $absolute_path = $this->get_absolute_path( $plugin_main_file->path );
930
- if ( file_exists( $absolute_path ) ) {
931
- return $absolute_path;
932
- }
933
- }
934
- }
935
-
936
- /**
937
- * @since 1.2.1
938
- *
939
- * `clear_module_main_file_cache()` is clearing the plugin's cached path on
940
- * deactivation. Therefore, if any plugin/theme was initiating `Freemius`
941
- * with that plugin's slug, it was overriding the empty plugin path with a wrong path.
942
- *
943
- * So, we've added a special mechanism with a 2nd layer of cache that uses `prev_path`
944
- * when the class instantiator isn't the module.
945
- */
946
- if ( ! $is_init ) {
947
- // Fetch prev path cache.
948
- if ( isset( $this->_storage->plugin_main_file ) &&
949
- isset( $this->_storage->plugin_main_file->prev_path )
950
- ) {
951
- $absolute_path = $this->get_absolute_path( $this->_storage->plugin_main_file->prev_path );
952
- if ( file_exists( $absolute_path ) ) {
953
- return $absolute_path;
954
- }
955
- }
956
-
957
- wp_die(
958
- $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' ) .
959
- " Module: {$this->_slug}; SDK: " . WP_FS__SDK_VERSION . ";",
960
- $this->get_text_inline( 'Error', 'error' ),
961
- array( 'back_link' => true )
962
- );
963
- }
964
-
965
- /**
966
- * @since 1.2.1
967
- *
968
- * Only the original instantiator that calls dynamic_init can modify the module's path.
969
- */
970
- // Find caller module.
971
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
972
- $this->_storage->plugin_main_file = (object) array(
973
- 'path' => $id_slug_type_path_map[ $this->_module_id ]['path'],
974
- );
975
 
976
- return $this->get_absolute_path( $id_slug_type_path_map[ $this->_module_id ]['path'] );
977
- }
 
 
978
 
979
  /**
980
- * @author Leo Fajardo (@leorw)
981
- * @since 1.2.3
982
  *
983
- * @param string $path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
984
  *
985
- * @return string
986
  */
987
- private function get_relative_path( $path ) {
988
- $module_root_dir = $this->get_module_root_dir_path();
989
- if ( 0 === strpos( $path, $module_root_dir ) ) {
990
- $path = substr( $path, strlen( $module_root_dir ) );
991
- }
992
 
993
- return $path;
994
- }
 
 
995
 
996
  /**
997
- * @author Leo Fajardo (@leorw)
998
- * @since 1.2.3
999
  *
1000
- * @param string $path
1001
- * @param string|bool $module_type
 
 
 
 
1002
  *
1003
- * @return string
1004
  */
1005
- private function get_absolute_path( $path, $module_type = false ) {
1006
- $module_root_dir = $this->get_module_root_dir_path( $module_type );
1007
- if ( 0 !== strpos( $path, $module_root_dir ) ) {
1008
- $path = fs_normalize_path( $module_root_dir . $path );
1009
- }
1010
 
1011
- return $path;
1012
- }
 
 
 
 
1013
 
1014
  /**
1015
- * @author Leo Fajardo (@leorw)
1016
- * @since 1.2.3
1017
  *
1018
- * @param string|bool $module_type
 
 
 
 
 
1019
  *
1020
- * @return string
1021
  */
1022
- private function get_module_root_dir_path( $module_type = false ) {
1023
- $is_plugin = empty( $module_type ) ?
1024
- $this->is_plugin() :
1025
- ( WP_FS__MODULE_TYPE_PLUGIN === $module_type );
1026
 
1027
- return fs_normalize_path( trailingslashit( $is_plugin ?
1028
- WP_PLUGIN_DIR :
1029
- get_theme_root() ) );
1030
- }
 
 
1031
 
1032
- /**
1033
- * @author Leo Fajardo (@leorw)
1034
- *
1035
- * @param number $module_id
1036
- * @param string $slug
1037
- *
1038
- * @since 1.2.2
1039
- */
1040
- private function store_id_slug_type_path_map( $module_id, $slug ) {
1041
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
1042
-
1043
- $store_option = false;
1044
-
1045
- if ( ! isset( $id_slug_type_path_map[ $module_id ] ) ) {
1046
- $id_slug_type_path_map[ $module_id ] = array(
1047
- 'slug' => $slug
1048
- );
1049
-
1050
- $store_option = true;
1051
- }
1052
-
1053
- if ( ! isset( $id_slug_type_path_map[ $module_id ]['path'] ) ||
1054
- /**
1055
- * This verification is for cases when suddenly the same module
1056
- * is installed but with a different folder name.
1057
- *
1058
- * @author Vova Feldman (@svovaf)
1059
- * @since 1.2.3
1060
- */
1061
- ! file_exists( $this->get_absolute_path(
1062
- $id_slug_type_path_map[ $module_id ]['path'],
1063
- $id_slug_type_path_map[ $module_id ]['type']
1064
- ) )
1065
- ) {
1066
- $caller_main_file_and_type = $this->get_caller_main_file_and_type();
1067
-
1068
- $id_slug_type_path_map[ $module_id ]['type'] = $caller_main_file_and_type->module_type;
1069
- $id_slug_type_path_map[ $module_id ]['path'] = $caller_main_file_and_type->path;
1070
-
1071
- $store_option = true;
1072
- }
1073
-
1074
- if ( $store_option ) {
1075
- self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
1076
- }
1077
- }
1078
-
1079
- /**
1080
- * Identifies the caller type: plugin or theme.
1081
- *
1082
- * @author Leo Fajardo (@leorw)
1083
- * @since 1.2.2
1084
- *
1085
- * @author Vova Feldman (@svovaf)
1086
- * @since 1.2.2.3 Find the earliest module in the call stack that calls to the SDK. This fix is for cases when
1087
- * add-ons are relying on loading the SDK from the parent module, and also allows themes including the
1088
- * SDK an internal file instead of directly from functions.php.
1089
- * @since 1.2.1.7 Knows how to handle cases when an add-on includes the parent module logic.
1090
- */
1091
- private function get_caller_main_file_and_type() {
1092
- self::require_plugin_essentials();
1093
-
1094
- $all_plugins = get_plugins();
1095
- $all_plugins_paths = array();
1096
-
1097
- // Get active plugin's main files real full names (might be symlinks).
1098
- foreach ( $all_plugins as $relative_path => &$data ) {
1099
- if ( false === strpos( fs_normalize_path( $relative_path ), '/' ) ) {
1100
- /**
1101
- * Ignore plugins that don't have a folder (e.g. Hello Dolly) since they
1102
- * can't really include the SDK.
1103
- *
1104
- * @author Vova Feldman
1105
- * @since 1.2.1.7
1106
- */
1107
- continue;
1108
- }
1109
-
1110
- $all_plugins_paths[] = fs_normalize_path( realpath( WP_PLUGIN_DIR . '/' . $relative_path ) );
1111
- }
1112
-
1113
- $caller_file_candidate = false;
1114
- $caller_map = array();
1115
- $module_type = WP_FS__MODULE_TYPE_PLUGIN;
1116
- $themes_dir = fs_normalize_path( get_theme_root() );
1117
-
1118
- for ( $i = 1, $bt = debug_backtrace(), $len = count( $bt ); $i < $len; $i ++ ) {
1119
- if ( empty( $bt[ $i ]['file'] ) ) {
1120
- continue;
1121
- }
1122
-
1123
- if ( $i > 1 && ! empty( $bt[ $i - 1 ]['file'] ) && $bt[ $i ]['file'] === $bt[ $i - 1 ]['file'] ) {
1124
- // If file same as the prev file in the stack, skip it.
1125
- continue;
1126
- }
1127
-
1128
- if ( ! empty( $bt[ $i ]['function'] ) && in_array( $bt[ $i ]['function'], array(
1129
- 'do_action',
1130
- 'apply_filter',
1131
- // The string split is stupid, but otherwise, theme check
1132
- // throws info notices.
1133
- 'requir' . 'e_once',
1134
- 'requir' . 'e',
1135
- 'includ' . 'e_once',
1136
- 'includ' . 'e'
1137
- ) )
1138
- ) {
1139
- // Ignore call stack hooks and files inclusion.
1140
- continue;
1141
- }
1142
-
1143
- $caller_file_path = fs_normalize_path( $bt[ $i ]['file'] );
1144
-
1145
- if ( 'functions.php' === basename( $caller_file_path ) ) {
1146
- /**
1147
- * 1. Assumes that theme's starting execution file is functions.php.
1148
- * 2. This complex logic fixes symlink issues (e.g. with Vargant).
1149
- *
1150
- * @author Vova Feldman (@svovaf)
1151
- * @since 1.2.2.5
1152
- */
1153
-
1154
- if ( $caller_file_path == fs_normalize_path( realpath( trailingslashit( $themes_dir ) . basename( dirname( $caller_file_path ) ) . '/' . basename( $caller_file_path ) ) ) ) {
1155
- $module_type = WP_FS__MODULE_TYPE_THEME;
1156
 
1157
- /**
1158
- * Relative path of the theme, e.g.:
1159
- * `my-theme/functions.php`
1160
- *
1161
- * @author Leo Fajardo (@leorw)
1162
- */
1163
- $caller_file_candidate = basename( dirname( $caller_file_path ) ) .
1164
- '/' .
1165
- basename( $caller_file_path );
1166
-
1167
- continue;
1168
- }
1169
- }
1170
-
1171
- $caller_file_hash = md5( $caller_file_path );
1172
-
1173
- if ( ! isset( $caller_map[ $caller_file_hash ] ) ) {
1174
- foreach ( $all_plugins_paths as $plugin_path ) {
1175
- if ( false !== strpos( $caller_file_path, fs_normalize_path( dirname( $plugin_path ) . '/' ) ) ) {
1176
- $caller_map[ $caller_file_hash ] = fs_normalize_path( $plugin_path );
1177
- break;
1178
- }
1179
- }
1180
- }
1181
-
1182
- if ( isset( $caller_map[ $caller_file_hash ] ) ) {
1183
- $module_type = WP_FS__MODULE_TYPE_PLUGIN;
1184
- $caller_file_candidate = plugin_basename( $caller_map[ $caller_file_hash ] );
1185
- }
1186
- }
1187
-
1188
- return (object) array(
1189
- 'module_type' => $module_type,
1190
- 'path' => $caller_file_candidate
1191
- );
1192
- }
1193
-
1194
- #----------------------------------------------------------------------------------
1195
- #region Deactivation Feedback Form
1196
- #----------------------------------------------------------------------------------
1197
-
1198
- /**
1199
- * Displays a confirmation and feedback dialog box when the user clicks on the "Deactivate" link on the plugins
1200
- * page.
1201
- *
1202
- * @author Vova Feldman (@svovaf)
1203
- * @author Leo Fajardo (@leorw)
1204
- * @since 1.1.2
1205
- */
1206
- function _add_deactivation_feedback_dialog_box() {
1207
- /* Check the type of user:
1208
- * 1. Long-term (long-term)
1209
- * 2. Non-registered and non-anonymous short-term (non-registered-and-non-anonymous-short-term).
1210
- * 3. Short-term (short-term)
1211
- */
1212
- $is_long_term_user = true;
1213
 
1214
- // Check if the site is at least 2 days old.
1215
- $time_installed = $this->_storage->install_timestamp;
 
 
 
 
 
 
 
 
 
 
 
 
1216
 
1217
- // Difference in seconds.
1218
- $date_diff = time() - $time_installed;
 
1219
 
1220
- // Convert seconds to days.
1221
- $date_diff_days = floor( $date_diff / ( 60 * 60 * 24 ) );
1222
 
1223
- if ( $date_diff_days < 2 ) {
1224
- $is_long_term_user = false;
1225
- }
1226
 
1227
- $is_long_term_user = $this->apply_filters( 'is_long_term_user', $is_long_term_user );
1228
 
1229
- if ( $is_long_term_user ) {
1230
- $user_type = 'long-term';
1231
- } else {
1232
- if ( ! $this->is_registered() && ! $this->is_anonymous() ) {
1233
- $user_type = 'non-registered-and-non-anonymous-short-term';
1234
- } else {
1235
- $user_type = 'short-term';
1236
- }
1237
- }
1238
 
1239
- $uninstall_reasons = $this->_get_uninstall_reasons( $user_type );
 
 
 
1240
 
1241
- // Load the HTML template for the deactivation feedback dialog box.
1242
- $vars = array(
1243
- 'reasons' => $uninstall_reasons,
1244
- 'id' => $this->_module_id
1245
- );
1246
 
1247
- /**
1248
- * @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.
1249
- */
1250
- fs_require_template( 'forms/deactivation/form.php', $vars );
1251
- }
1252
-
1253
- /**
1254
- * @author Leo Fajardo (leorw)
1255
- * @since 1.1.2
1256
- *
1257
- * @param string $user_type
1258
- *
1259
- * @return array The uninstall reasons for the specified user type.
1260
- */
1261
- function _get_uninstall_reasons( $user_type = 'long-term' ) {
1262
- $module_type = $this->_module_type;
1263
-
1264
- $internal_message_template_var = array(
1265
- 'id' => $this->_module_id
1266
- );
1267
-
1268
- if ( $this->is_registered() && false !== $this->get_plan() && $this->get_plan()->has_technical_support() ) {
1269
- $contact_support_template = fs_get_template( 'forms/deactivation/contact.php', $internal_message_template_var );
1270
- } else {
1271
- $contact_support_template = '';
1272
- }
1273
-
1274
- $reason_found_better_plugin = array(
1275
- 'id' => self::REASON_FOUND_A_BETTER_PLUGIN,
1276
- 'text' => sprintf( $this->get_text_inline( 'I found a better %s', 'reason-found-a-better-plugin' ), $module_type ),
1277
- 'input_type' => 'textfield',
1278
- 'input_placeholder' => sprintf( $this->get_text_inline( "What's the %s's name?", 'placeholder-plugin-name' ), $module_type ),
1279
- );
1280
-
1281
- $reason_temporary_deactivation = array(
1282
- 'id' => self::REASON_TEMPORARY_DEACTIVATION,
1283
- 'text' => sprintf(
1284
- $this->get_text_inline( "It's a temporary %s. I'm just debugging an issue.", 'reason-temporary-x' ),
1285
- strtolower( $this->is_plugin() ?
1286
- $this->get_text_inline( 'Deactivation', 'deactivation' ) :
1287
- $this->get_text_inline( 'Theme Switch', 'theme-switch' )
1288
- )
1289
- ),
1290
- 'input_type' => '',
1291
- 'input_placeholder' => ''
1292
- );
1293
-
1294
- $reason_other = array(
1295
- 'id' => self::REASON_OTHER,
1296
- 'text' => $this->get_text_inline( 'Other', 'reason-other' ),
1297
- 'input_type' => 'textfield',
1298
- 'input_placeholder' => ''
1299
- );
1300
-
1301
- $long_term_user_reasons = array(
1302
- array(
1303
- 'id' => self::REASON_NO_LONGER_NEEDED,
1304
- 'text' => sprintf( $this->get_text_inline( 'I no longer need the %s', 'reason-no-longer-needed' ), $module_type ),
1305
- 'input_type' => '',
1306
- 'input_placeholder' => ''
1307
- ),
1308
- $reason_found_better_plugin,
1309
- array(
1310
- 'id' => self::REASON_NEEDED_FOR_A_SHORT_PERIOD,
1311
- 'text' => sprintf( $this->get_text_inline( 'I only needed the %s for a short period', 'reason-needed-for-a-short-period' ), $module_type ),
1312
- 'input_type' => '',
1313
- 'input_placeholder' => ''
1314
- ),
1315
- array(
1316
- 'id' => self::REASON_BROKE_MY_SITE,
1317
- 'text' => sprintf( $this->get_text_inline( 'The %s broke my site', 'reason-broke-my-site' ), $module_type ),
1318
- 'input_type' => '',
1319
- 'input_placeholder' => '',
1320
- 'internal_message' => $contact_support_template
1321
- ),
1322
- array(
1323
- 'id' => self::REASON_SUDDENLY_STOPPED_WORKING,
1324
- 'text' => sprintf( $this->get_text_inline( 'The %s suddenly stopped working', 'reason-suddenly-stopped-working' ), $module_type ),
1325
- 'input_type' => '',
1326
- 'input_placeholder' => '',
1327
- 'internal_message' => $contact_support_template
1328
- )
1329
- );
1330
-
1331
- if ( $this->is_paying() ) {
1332
- $long_term_user_reasons[] = array(
1333
- 'id' => self::REASON_CANT_PAY_ANYMORE,
1334
- 'text' => $this->get_text_inline( "I can't pay for it anymore", 'reason-cant-pay-anymore' ),
1335
- 'input_type' => 'textfield',
1336
- 'input_placeholder' => $this->get_text_inline( 'What price would you feel comfortable paying?', 'placeholder-comfortable-price' )
1337
- );
1338
- }
1339
-
1340
- $reason_dont_share_info = array(
1341
- 'id' => self::REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION,
1342
- 'text' => $this->get_text_inline( "I don't like to share my information with you", 'reason-dont-like-to-share-my-information' ),
1343
- 'input_type' => '',
1344
- 'input_placeholder' => ''
1345
- );
1346
-
1347
- /**
1348
- * If the current user has selected the "don't share data" reason in the deactivation feedback modal, inform the
1349
- * user by showing additional message that he doesn't have to share data and can just choose to skip the opt-in
1350
- * (the Skip button is included in the message to show). This message will only be shown if anonymous mode is
1351
- * enabled and the user's account is currently not in pending activation state (similar to the way the Skip
1352
- * button in the opt-in form is shown/hidden).
1353
- */
1354
- if ( $this->is_enable_anonymous() && ! $this->is_pending_activation() ) {
1355
- $reason_dont_share_info['internal_message'] = fs_get_template( 'forms/deactivation/retry-skip.php', $internal_message_template_var );
1356
- }
1357
-
1358
- $uninstall_reasons = array(
1359
- 'long-term' => $long_term_user_reasons,
1360
- 'non-registered-and-non-anonymous-short-term' => array(
1361
- array(
1362
- 'id' => self::REASON_DIDNT_WORK,
1363
- 'text' => sprintf( $this->get_text_inline( "The %s didn't work", 'reason-didnt-work' ), $module_type ),
1364
- 'input_type' => '',
1365
- 'input_placeholder' => ''
1366
- ),
1367
- $reason_dont_share_info,
1368
- $reason_found_better_plugin
1369
- ),
1370
- 'short-term' => array(
1371
- array(
1372
- 'id' => self::REASON_COULDNT_MAKE_IT_WORK,
1373
- 'text' => $this->get_text_inline( "I couldn't understand how to make it work", 'reason-couldnt-make-it-work' ),
1374
- 'input_type' => '',
1375
- 'input_placeholder' => '',
1376
- 'internal_message' => $contact_support_template
1377
- ),
1378
- $reason_found_better_plugin,
1379
- array(
1380
- 'id' => self::REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE,
1381
- '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 ),
1382
- 'input_type' => 'textarea',
1383
- 'input_placeholder' => $this->get_text_inline( 'What feature?', 'placeholder-feature' )
1384
- ),
1385
- array(
1386
- 'id' => self::REASON_NOT_WORKING,
1387
- 'text' => sprintf( $this->get_text_inline( 'The %s is not working', 'reason-not-working' ), $module_type ),
1388
- 'input_type' => 'textarea',
1389
- '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' )
1390
- ),
1391
- array(
1392
- 'id' => self::REASON_NOT_WHAT_I_WAS_LOOKING_FOR,
1393
- 'text' => $this->get_text_inline( "It's not what I was looking for", 'reason-not-what-i-was-looking-for' ),
1394
- 'input_type' => 'textarea',
1395
- 'input_placeholder' => $this->get_text_inline( "What you've been looking for?", 'placeholder-what-youve-been-looking-for' )
1396
- ),
1397
- array(
1398
- 'id' => self::REASON_DIDNT_WORK_AS_EXPECTED,
1399
- 'text' => sprintf( $this->get_text_inline( "The %s didn't work as expected", 'reason-didnt-work-as-expected' ), $module_type ),
1400
- 'input_type' => 'textarea',
1401
- 'input_placeholder' => $this->get_text_inline( 'What did you expect?', 'placeholder-what-did-you-expect' )
1402
- )
1403
- )
1404
- );
1405
-
1406
- // Randomize the reasons for the current user type.
1407
- shuffle( $uninstall_reasons[ $user_type ] );
1408
-
1409
- // Keep the following reasons as the last items in the list.
1410
- $uninstall_reasons[ $user_type ][] = $reason_temporary_deactivation;
1411
- $uninstall_reasons[ $user_type ][] = $reason_other;
1412
-
1413
- $uninstall_reasons = $this->apply_filters( 'uninstall_reasons', $uninstall_reasons );
1414
-
1415
- return $uninstall_reasons[ $user_type ];
1416
- }
1417
-
1418
- /**
1419
- * Called after the user has submitted his reason for deactivating the plugin.
1420
- *
1421
- * @author Leo Fajardo (@leorw)
1422
- * @since 1.1.2
1423
- */
1424
- function _submit_uninstall_reason_action() {
1425
- $this->_logger->entrance();
1426
-
1427
- $this->check_ajax_referer( 'submit_uninstall_reason' );
1428
-
1429
- $reason_id = fs_request_get( 'reason_id' );
1430
-
1431
- // Check if the given reason ID is an unsigned integer.
1432
- if ( ! ctype_digit( $reason_id ) ) {
1433
- exit;
1434
- }
1435
-
1436
- $reason_info = trim( fs_request_get( 'reason_info', '' ) );
1437
- if ( ! empty( $reason_info ) ) {
1438
- $reason_info = substr( $reason_info, 0, 128 );
1439
- }
1440
-
1441
- $reason = (object) array(
1442
- 'id' => $reason_id,
1443
- 'info' => $reason_info,
1444
- 'is_anonymous' => fs_request_get_bool( 'is_anonymous' )
1445
- );
1446
-
1447
- $this->_storage->store( 'uninstall_reason', $reason );
1448
-
1449
- /**
1450
- * If the module type is "theme", trigger the uninstall event here (on theme deactivation) since themes do
1451
- * not support uninstall hook.
1452
- *
1453
- * @author Leo Fajardo (@leorw)
1454
- * @since 1.2.2
1455
- */
1456
- if ( $this->is_theme() ) {
1457
- $this->_uninstall_plugin_event( false );
1458
- $this->remove_sdk_reference();
1459
- }
1460
-
1461
- // Print '1' for successful operation.
1462
- echo 1;
1463
- exit;
1464
- }
1465
-
1466
- #endregion
1467
-
1468
- #----------------------------------------------------------------------------------
1469
- #region Instance
1470
- #----------------------------------------------------------------------------------
1471
-
1472
- /**
1473
- * Main singleton instance.
1474
- *
1475
- * @author Vova Feldman (@svovaf)
1476
- * @since 1.0.0
1477
- *
1478
- * @param number $module_id
1479
- * @param string|bool $slug
1480
- * @param bool $is_init Is initiation sequence.
1481
- *
1482
- * @return Freemius|false
1483
- */
1484
- static function instance( $module_id, $slug = false, $is_init = false ) {
1485
- if ( empty( $module_id ) ) {
1486
- return false;
1487
- }
1488
-
1489
- if ( ! is_numeric( $module_id ) ) {
1490
- if ( ! $is_init && true === $slug ) {
1491
- $is_init = true;
1492
- }
1493
-
1494
- $slug = $module_id;
1495
-
1496
- $module = FS_Plugin_Manager::instance( $slug )->get();
1497
-
1498
- if ( is_object( $module ) ) {
1499
- $module_id = $module->id;
1500
- }
1501
- }
1502
-
1503
- $key = 'm_' . $module_id;
1504
-
1505
- if ( ! isset( self::$_instances[ $key ] ) ) {
1506
- if ( 0 === count( self::$_instances ) ) {
1507
- self::_load_required_static();
1508
- }
1509
-
1510
- self::$_instances[ $key ] = new Freemius( $module_id, $slug, $is_init );
1511
- }
1512
-
1513
- return self::$_instances[ $key ];
1514
- }
1515
-
1516
- /**
1517
- * @author Vova Feldman (@svovaf)
1518
- * @since 1.0.6
1519
- *
1520
- * @param number $addon_id
1521
- *
1522
- * @return bool
1523
- */
1524
- private static function has_instance( $addon_id ) {
1525
- return isset( self::$_instances[ 'm_' . $addon_id ] );
1526
- }
1527
-
1528
- /**
1529
- * @author Leo Fajardo (@leorw)
1530
- * @since 1.2.2
1531
- *
1532
- * @param string|number $id_or_slug
1533
- *
1534
- * @return number|false
1535
- */
1536
- private static function get_module_id( $id_or_slug ) {
1537
- if ( is_numeric( $id_or_slug ) ) {
1538
- return $id_or_slug;
1539
- }
1540
-
1541
- foreach ( self::$_instances as $instance ) {
1542
- if ( $instance->is_plugin() && ( $id_or_slug === $instance->get_slug() ) ) {
1543
- return $instance->get_id();
1544
- }
1545
- }
1546
-
1547
- return false;
1548
- }
1549
-
1550
- /**
1551
- * @author Vova Feldman (@svovaf)
1552
- * @since 1.0.6
1553
- *
1554
- * @param number $id
1555
- *
1556
- * @return false|Freemius
1557
- */
1558
- static function get_instance_by_id( $id ) {
1559
- return isset ( self::$_instances[ 'm_' . $id ] ) ?
1560
- self::$_instances[ 'm_' . $id ] :
1561
- false;
1562
- }
1563
-
1564
- /**
1565
- *
1566
- * @author Vova Feldman (@svovaf)
1567
- * @since 1.0.1
1568
- *
1569
- * @param $plugin_file
1570
- *
1571
- * @return false|Freemius
1572
- */
1573
- static function get_instance_by_file( $plugin_file ) {
1574
- $slug = self::find_slug_by_basename( $plugin_file );
1575
-
1576
- return ( false !== $slug ) ?
1577
- self::instance( self::get_module_id( $slug ) ) :
1578
- false;
1579
- }
1580
-
1581
- /**
1582
- * @author Vova Feldman (@svovaf)
1583
- * @since 1.0.6
1584
- *
1585
- * @return false|Freemius
1586
- */
1587
- function get_parent_instance() {
1588
- return self::get_instance_by_id( $this->_plugin->parent_plugin_id );
1589
- }
1590
-
1591
- /**
1592
- * @author Vova Feldman (@svovaf)
1593
- * @since 1.0.6
1594
- *
1595
- * @param string|number $id_or_slug
1596
- *
1597
- * @return false|Freemius
1598
- */
1599
- function get_addon_instance( $id_or_slug ) {
1600
- $addon_id = self::get_module_id( $id_or_slug );
1601
-
1602
- return self::instance( $addon_id );
1603
- }
1604
-
1605
- #endregion ------------------------------------------------------------------
1606
-
1607
- /**
1608
- * @author Vova Feldman (@svovaf)
1609
- * @since 1.0.6
1610
- *
1611
- * @return bool
1612
- */
1613
- function is_parent_plugin_installed() {
1614
- $is_active = self::has_instance( $this->_plugin->parent_plugin_id );
1615
-
1616
- if ( $is_active ) {
1617
- return true;
1618
- }
1619
-
1620
- /**
1621
- * Parent module might be a theme. If that's the case, the add-on's FS
1622
- * instance will be loaded prior to the theme's FS instance, therefore,
1623
- * we need to check if it's active with a "look ahead".
1624
- *
1625
- * @author Vova Feldman
1626
- * @since 1.2.2.3
1627
- */
1628
- global $fs_active_plugins;
1629
- if ( is_object( $fs_active_plugins ) && is_array( $fs_active_plugins->plugins ) ) {
1630
- $active_theme = wp_get_theme();
1631
-
1632
- foreach ( $fs_active_plugins->plugins as $sdk => $module ) {
1633
- if ( WP_FS__MODULE_TYPE_THEME === $module->type ) {
1634
- if ( $module->plugin_path == $active_theme->get_stylesheet() ) {
1635
- // Parent module is a theme and it's currently active.
1636
- return true;
1637
- }
1638
- }
1639
- }
1640
- }
1641
-
1642
- return false;
1643
- }
1644
-
1645
- /**
1646
- * Check if add-on parent plugin in activation mode.
1647
- *
1648
- * @author Vova Feldman (@svovaf)
1649
- * @since 1.0.7
1650
- *
1651
- * @return bool
1652
- */
1653
- function is_parent_in_activation() {
1654
- $parent_fs = $this->get_parent_instance();
1655
- if ( ! is_object( $parent_fs ) ) {
1656
- return false;
1657
- }
1658
-
1659
- return ( $parent_fs->is_activation_mode() );
1660
- }
1661
-
1662
- /**
1663
- * Is plugin in activation mode.
1664
- *
1665
- * @author Vova Feldman (@svovaf)
1666
- * @since 1.0.7
1667
- *
1668
- * @param bool $and_on
1669
- *
1670
- * @return bool
1671
- */
1672
- function is_activation_mode( $and_on = true ) {
1673
- return (
1674
- ( $this->is_on() || ! $and_on ) &&
1675
- ( ! $this->is_registered() || ( $this->is_only_premium() && ! $this->has_features_enabled_license() ) ) &&
1676
- ( ! $this->is_enable_anonymous() ||
1677
- ( ! $this->is_anonymous() && ! $this->is_pending_activation() ) )
1678
- );
1679
- }
1680
-
1681
- /**
1682
- * Check if current page is the opt-in/pending-activation page.
1683
- *
1684
- * @author Vova Feldman (@svovaf)
1685
- * @since 1.2.1.7
1686
- *
1687
- * @return bool
1688
- */
1689
- function is_activation_page() {
1690
- if ( $this->_menu->is_main_settings_page() ) {
1691
- return true;
1692
- }
1693
-
1694
- if ( ! $this->is_activation_mode() ) {
1695
- return false;
1696
- }
1697
-
1698
- // Check if current page is matching the activation page.
1699
- return $this->is_matching_url( $this->get_activation_url() );
1700
- }
1701
-
1702
- /**
1703
- * Check if URL path's are matching and that all querystring
1704
- * arguments of the $sub_url exist in the $url with the same values.
1705
- *
1706
- * WARNING:
1707
- * 1. This method doesn't check if the sub/domain are matching.
1708
- * 2. Ignore case sensitivity.
1709
- *
1710
- * @author Vova Feldman (@svovaf)
1711
- * @since 1.2.1.7
1712
- *
1713
- * @param string $sub_url
1714
- * @param string $url If argument is not set, check if the sub_url matching the current's page URL.
1715
- *
1716
- * @return bool
1717
- */
1718
- private function is_matching_url( $sub_url, $url = '' ) {
1719
- if ( empty( $url ) ) {
1720
- $url = $_SERVER['REQUEST_URI'];
1721
- }
1722
-
1723
- $url = strtolower( $url );
1724
- $sub_url = strtolower( $sub_url );
1725
-
1726
- if ( parse_url( $sub_url, PHP_URL_PATH ) !== parse_url( $url, PHP_URL_PATH ) ) {
1727
- // Different path - DO NOT OVERRIDE PAGE.
1728
- return false;
1729
- }
1730
-
1731
- $url_params = array();
1732
- parse_str( parse_url( $url, PHP_URL_QUERY ), $url_params );
1733
-
1734
- $sub_url_params = array();
1735
- parse_str( parse_url( $sub_url, PHP_URL_QUERY ), $sub_url_params );
1736
-
1737
- foreach ( $sub_url_params as $key => $val ) {
1738
- if ( ! isset( $url_params[ $key ] ) || $val != $url_params[ $key ] ) {
1739
- // Not matching query string - DO NOT OVERRIDE PAGE.
1740
- return false;
1741
- }
1742
- }
1743
-
1744
- return true;
1745
- }
1746
-
1747
- /**
1748
- * Get collection of all active plugins.
1749
- *
1750
- * @author Vova Feldman (@svovaf)
1751
- * @since 1.0.9
1752
- *
1753
- * @return array[string]array
1754
- */
1755
- private static function get_active_plugins() {
1756
- self::require_plugin_essentials();
1757
-
1758
- $active_plugin = array();
1759
- $all_plugins = get_plugins();
1760
- $active_plugins_basenames = get_option( 'active_plugins' );
1761
-
1762
- foreach ( $active_plugins_basenames as $plugin_basename ) {
1763
- $active_plugin[ $plugin_basename ] = $all_plugins[ $plugin_basename ];
1764
- }
1765
-
1766
- return $active_plugin;
1767
- }
1768
-
1769
- /**
1770
- * Get collection of all plugins.
1771
- *
1772
- * @author Vova Feldman (@svovaf)
1773
- * @since 1.1.8
1774
- *
1775
- * @return array Key is the plugin file path and the value is an array of the plugin data.
1776
- */
1777
- private static function get_all_plugins() {
1778
- self::require_plugin_essentials();
1779
-
1780
- $all_plugins = get_plugins();
1781
- $active_plugins_basenames = get_option( 'active_plugins' );
1782
-
1783
- foreach ( $all_plugins as $basename => &$data ) {
1784
- // By default set to inactive (next foreach update the active plugins).
1785
- $data['is_active'] = false;
1786
- // Enrich with plugin slug.
1787
- $data['slug'] = self::get_plugin_slug( $basename );
1788
- }
1789
-
1790
- // Flag active plugins.
1791
- foreach ( $active_plugins_basenames as $basename ) {
1792
- if ( isset( $all_plugins[ $basename ] ) ) {
1793
- $all_plugins[ $basename ]['is_active'] = true;
1794
- }
1795
- }
1796
-
1797
- return $all_plugins;
1798
- }
1799
-
1800
-
1801
- /**
1802
- * Cached result of get_site_transient( 'update_plugins' )
1803
- *
1804
- * @author Vova Feldman (@svovaf)
1805
- * @since 1.1.8
1806
- *
1807
- * @var object
1808
- */
1809
- private static $_plugins_info;
1810
-
1811
- /**
1812
- * Helper function to get specified plugin's slug.
1813
- *
1814
- * @author Vova Feldman (@svovaf)
1815
- * @since 1.1.8
1816
- *
1817
- * @param $basename
1818
- *
1819
- * @return string
1820
- */
1821
- private static function get_plugin_slug( $basename ) {
1822
- if ( ! isset( self::$_plugins_info ) ) {
1823
- self::$_plugins_info = get_site_transient( 'update_plugins' );
1824
- }
1825
-
1826
- $slug = '';
1827
-
1828
- if ( is_object( self::$_plugins_info ) ) {
1829
- if ( isset( self::$_plugins_info->no_update ) &&
1830
- isset( self::$_plugins_info->no_update[ $basename ] ) &&
1831
- ! empty( self::$_plugins_info->no_update[ $basename ]->slug )
1832
- ) {
1833
- $slug = self::$_plugins_info->no_update[ $basename ]->slug;
1834
- } else if ( isset( self::$_plugins_info->response ) &&
1835
- isset( self::$_plugins_info->response[ $basename ] ) &&
1836
- ! empty( self::$_plugins_info->response[ $basename ]->slug )
1837
- ) {
1838
- $slug = self::$_plugins_info->response[ $basename ]->slug;
1839
- }
1840
- }
1841
-
1842
- if ( empty( $slug ) ) {
1843
- // Try to find slug from FS data.
1844
- $slug = self::find_slug_by_basename( $basename );
1845
- }
1846
-
1847
- if ( empty( $slug ) ) {
1848
- // Fallback to plugin's folder name.
1849
- $slug = dirname( $basename );
1850
- }
1851
-
1852
- return $slug;
1853
- }
1854
-
1855
- private static $_statics_loaded = false;
1856
-
1857
- /**
1858
- * Load static resources.
1859
- *
1860
- * @author Vova Feldman (@svovaf)
1861
- * @since 1.0.1
1862
- */
1863
- private static function _load_required_static() {
1864
- if ( self::$_statics_loaded ) {
1865
- return;
1866
- }
1867
-
1868
- self::$_static_logger = FS_Logger::get_logger( WP_FS__SLUG, WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
1869
-
1870
- self::$_static_logger->entrance();
1871
-
1872
- self::$_accounts = FS_Option_Manager::get_manager( WP_FS__ACCOUNTS_OPTION_NAME, true );
1873
-
1874
- self::$_global_admin_notices = FS_Admin_Notice_Manager::instance( 'global' );
1875
-
1876
- add_action( 'admin_menu', array( 'Freemius', '_add_debug_section' ) );
1877
-
1878
- add_action( "wp_ajax_fs_toggle_debug_mode", array( 'Freemius', '_toggle_debug_mode' ) );
1879
-
1880
- self::add_ajax_action_static( 'get_debug_log', array( 'Freemius', '_get_debug_log' ) );
1881
-
1882
- self::add_ajax_action_static( 'get_db_option', array( 'Freemius', '_get_db_option' ) );
1883
-
1884
- self::add_ajax_action_static( 'set_db_option', array( 'Freemius', '_set_db_option' ) );
1885
-
1886
- if ( 0 == did_action( 'plugins_loaded' ) ) {
1887
- add_action( 'plugins_loaded', array( 'Freemius', '_load_textdomain' ), 1 );
1888
- }
1889
-
1890
- self::$_statics_loaded = true;
1891
- }
1892
-
1893
- #----------------------------------------------------------------------------------
1894
- #region Localization
1895
- #----------------------------------------------------------------------------------
1896
-
1897
- /**
1898
- * Load framework's text domain.
1899
- *
1900
- * @author Vova Feldman (@svovaf)
1901
- * @since 1.2.1
1902
- */
1903
- static function _load_textdomain() {
1904
- if ( ! is_admin() ) {
1905
- return;
1906
- }
1907
-
1908
- global $fs_active_plugins;
1909
-
1910
- // Works both for plugins and themes.
1911
- load_plugin_textdomain(
1912
- 'freemius',
1913
- false,
1914
- $fs_active_plugins->newest->sdk_path . '/languages/'
1915
- );
1916
- }
1917
-
1918
- #endregion
1919
-
1920
- #----------------------------------------------------------------------------------
1921
- #region Debugging
1922
- #----------------------------------------------------------------------------------
1923
-
1924
- /**
1925
- * @author Vova Feldman (@svovaf)
1926
- * @since 1.0.8
1927
- */
1928
- static function _add_debug_section() {
1929
- if ( ! current_user_can( 'activate_plugins' )
1930
- && ! current_user_can( 'switch_themes' )
1931
- ) {
1932
- return;
1933
- }
1934
-
1935
- self::$_static_logger->entrance();
1936
-
1937
- $title = sprintf( '%s [v.%s]', fs_text_inline( 'Freemius Debug' ), WP_FS__SDK_VERSION );
1938
-
1939
- if ( WP_FS__DEV_MODE ) {
1940
- // Add top-level debug menu item.
1941
- $hook = FS_Admin_Menu_Manager::add_page(
1942
- $title,
1943
- $title,
1944
- 'manage_options',
1945
- 'freemius',
1946
- array( 'Freemius', '_debug_page_render' )
1947
- );
1948
- } else {
1949
- // Add hidden debug page.
1950
- $hook = FS_Admin_Menu_Manager::add_subpage(
1951
- null,
1952
- $title,
1953
- $title,
1954
- 'manage_options',
1955
- 'freemius',
1956
- array( 'Freemius', '_debug_page_render' )
1957
- );
1958
- }
1959
-
1960
- if ( ! empty( $hook ) ) {
1961
- add_action( "load-$hook", array( 'Freemius', '_debug_page_actions' ) );
1962
- }
1963
- }
1964
-
1965
- /**
1966
- * @author Vova Feldman (@svovaf)
1967
- * @since 1.1.7.3
1968
- */
1969
- static function _toggle_debug_mode() {
1970
- $is_on = fs_request_get( 'is_on', false, 'post' );
1971
-
1972
- if ( fs_request_is_post() && in_array( $is_on, array( 0, 1 ) ) ) {
1973
- update_option( 'fs_debug_mode', $is_on );
1974
-
1975
- // Turn on/off storage logging.
1976
- FS_Logger::_set_storage_logging( ( 1 == $is_on ) );
1977
- }
1978
-
1979
- exit;
1980
- }
1981
-
1982
- /**
1983
- * @author Vova Feldman (@svovaf)
1984
- * @since 1.2.1.6
1985
- */
1986
- static function _get_debug_log() {
1987
- $logs = FS_Logger::load_db_logs(
1988
- fs_request_get( 'filters', false, 'post' ),
1989
- ! empty( $_POST['limit'] ) && is_numeric( $_POST['limit'] ) ? $_POST['limit'] : 200,
1990
- ! empty( $_POST['offset'] ) && is_numeric( $_POST['offset'] ) ? $_POST['offset'] : 0
1991
- );
1992
-
1993
- self::shoot_ajax_success( $logs );
1994
- }
1995
-
1996
- /**
1997
- * @author Vova Feldman (@svovaf)
1998
- * @since 1.2.1.7
1999
- */
2000
- static function _get_db_option() {
2001
- $option_name = fs_request_get( 'option_name' );
2002
-
2003
- $value = get_option( $option_name );
2004
-
2005
- $result = array(
2006
- 'name' => $option_name,
2007
- );
2008
-
2009
- if ( false !== $value ) {
2010
- if ( ! is_string( $value ) ) {
2011
- $value = json_encode( $value );
2012
- }
2013
-
2014
- $result['value'] = $value;
2015
- }
2016
-
2017
- self::shoot_ajax_success( $result );
2018
- }
2019
-
2020
- /**
2021
- * @author Vova Feldman (@svovaf)
2022
- * @since 1.2.1.7
2023
- */
2024
- static function _set_db_option() {
2025
- $option_name = fs_request_get( 'option_name' );
2026
- $option_value = fs_request_get( 'option_value' );
2027
-
2028
- if ( ! empty( $option_value ) ) {
2029
- update_option( $option_name, $option_value );
2030
- }
2031
-
2032
- self::shoot_ajax_success();
2033
- }
2034
-
2035
- /**
2036
- * @author Vova Feldman (@svovaf)
2037
- * @since 1.0.8
2038
- */
2039
- static function _debug_page_actions() {
2040
- self::_clean_admin_content_section();
2041
-
2042
- if ( fs_request_is_action( 'restart_freemius' ) ) {
2043
- check_admin_referer( 'restart_freemius' );
2044
-
2045
- // Clear accounts data.
2046
- self::$_accounts->clear( true );
2047
-
2048
- // Clear SDK reference cache.
2049
- delete_option( 'fs_active_plugins' );
2050
- } else if ( fs_request_is_action( 'simulate_trial' ) ) {
2051
- check_admin_referer( 'simulate_trial' );
2052
-
2053
- $fs = freemius( fs_request_get( 'module_id' ) );
2054
-
2055
- // Update SDK install to at least 24 hours before.
2056
- $fs->_storage->install_timestamp = ( time() - WP_FS__TIME_24_HOURS_IN_SEC );
2057
- // Unset the trial shown timestamp.
2058
- unset( $fs->_storage->trial_promotion_shown );
2059
- } else if ( fs_request_is_action( 'delete_install' ) ) {
2060
- check_admin_referer( 'delete_install' );
2061
-
2062
- self::_delete_site_by_slug(
2063
- fs_request_get( 'slug' ),
2064
- fs_request_get( 'module_type' )
2065
- );
2066
- } else if ( fs_request_is_action( 'download_logs' ) ) {
2067
- check_admin_referer( 'download_logs' );
2068
-
2069
- $download_url = FS_Logger::download_db_logs(
2070
- fs_request_get( 'filters', false, 'post' )
2071
- );
2072
-
2073
- if ( false === $download_url ) {
2074
- 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.' );
2075
- }
2076
-
2077
- fs_redirect( $download_url );
2078
- }
2079
- }
2080
-
2081
- /**
2082
- * @author Vova Feldman (@svovaf)
2083
- * @since 1.0.8
2084
- */
2085
- static function _debug_page_render() {
2086
- self::$_static_logger->entrance();
2087
-
2088
- $vars = array(
2089
- 'plugin_sites' => self::get_all_sites(),
2090
- 'theme_sites' => self::get_all_sites( WP_FS__MODULE_TYPE_THEME ),
2091
- 'users' => self::get_all_users(),
2092
- 'addons' => self::get_all_addons(),
2093
- 'account_addons' => self::get_all_account_addons(),
2094
- 'plugin_licenses' => self::get_all_licenses(),
2095
- 'theme_licenses' => self::get_all_licenses( WP_FS__MODULE_TYPE_THEME )
2096
- );
2097
-
2098
- fs_enqueue_local_style( 'fs_debug', '/admin/debug.css' );
2099
- fs_require_once_template( 'debug.php', $vars );
2100
- }
2101
-
2102
- #endregion
2103
-
2104
- #----------------------------------------------------------------------------------
2105
- #region Connectivity Issues
2106
- #----------------------------------------------------------------------------------
2107
-
2108
- /**
2109
- * Check if Freemius should be turned on for the current plugin install.
2110
- *
2111
- * Note:
2112
- * $this->_is_on is updated in has_api_connectivity()
2113
- *
2114
- * @author Vova Feldman (@svovaf)
2115
- * @since 1.0.9
2116
- *
2117
- * @return bool
2118
- */
2119
- function is_on() {
2120
- self::$_static_logger->entrance();
2121
-
2122
- if ( isset( $this->_is_on ) ) {
2123
- return $this->_is_on;
2124
- }
2125
-
2126
- // If already installed or pending then sure it's on :)
2127
- if ( $this->is_registered() || $this->is_pending_activation() ) {
2128
- $this->_is_on = true;
2129
-
2130
- return true;
2131
- }
2132
-
2133
- return false;
2134
- }
2135
-
2136
- /**
2137
- * @author Vova Feldman (@svovaf)
2138
- * @since 1.1.7.3
2139
- *
2140
- * @param bool $flush_if_no_connectivity
2141
- *
2142
- * @return bool
2143
- */
2144
- private function should_run_connectivity_test( $flush_if_no_connectivity = false ) {
2145
- if ( ! isset( $this->_storage->connectivity_test ) ) {
2146
- // Connectivity test was never executed, or cache was cleared.
2147
- return true;
2148
- }
2149
-
2150
- if ( WP_FS__PING_API_ON_IP_OR_HOST_CHANGES ) {
2151
- if ( WP_FS__IS_HTTP_REQUEST ) {
2152
- if ( $_SERVER['HTTP_HOST'] != $this->_storage->connectivity_test['host'] ) {
2153
- // Domain changed.
2154
- return true;
2155
- }
2156
-
2157
- if ( WP_FS__REMOTE_ADDR != $this->_storage->connectivity_test['server_ip'] ) {
2158
- // Server IP changed.
2159
- return true;
2160
- }
2161
- }
2162
- }
2163
-
2164
- if ( $this->_storage->connectivity_test['is_connected'] &&
2165
- $this->_storage->connectivity_test['is_active']
2166
- ) {
2167
- // API connected and Freemius is active - no need to run connectivity check.
2168
- return false;
2169
- }
2170
-
2171
- if ( $flush_if_no_connectivity ) {
2172
- /**
2173
- * If explicitly asked to flush when no connectivity - do it only
2174
- * if at least 10 sec passed from the last API connectivity test.
2175
- */
2176
- return ( isset( $this->_storage->connectivity_test['timestamp'] ) &&
2177
- ( WP_FS__SCRIPT_START_TIME - $this->_storage->connectivity_test['timestamp'] ) > 10 );
2178
- }
2179
-
2180
- /**
2181
- * @since 1.1.7 Don't check for connectivity on plugin downgrade.
2182
- */
2183
- $version = $this->get_plugin_version();
2184
- if ( version_compare( $version, $this->_storage->connectivity_test['version'], '>' ) ) {
2185
- // If it's a plugin version upgrade and Freemius is off or no connectivity, run connectivity test.
2186
- return true;
2187
- }
2188
-
2189
- return false;
2190
- }
2191
-
2192
- /**
2193
- * @author Vova Feldman (@svovaf)
2194
- * @since 1.1.7.4
2195
- *
2196
- * @return object|false
2197
- */
2198
- private function ping() {
2199
- if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY ) {
2200
- return false;
2201
- }
2202
-
2203
- $version = $this->get_plugin_version();
2204
-
2205
- $is_update = $this->apply_filters( 'is_plugin_update', $this->is_plugin_update() );
2206
-
2207
- return $this->get_api_plugin_scope()->ping(
2208
- $this->get_anonymous_id(),
2209
- array(
2210
- 'is_update' => json_encode( $is_update ),
2211
- 'version' => $version,
2212
- 'sdk' => $this->version,
2213
- 'is_admin' => json_encode( is_admin() ),
2214
- 'is_ajax' => json_encode( self::is_ajax() ),
2215
- 'is_cron' => json_encode( self::is_cron() ),
2216
- 'is_http' => json_encode( WP_FS__IS_HTTP_REQUEST ),
2217
- )
2218
- );
2219
- }
2220
-
2221
- /**
2222
- * Check if there's any connectivity issue to Freemius API.
2223
- *
2224
- * @author Vova Feldman (@svovaf)
2225
- * @since 1.0.9
2226
- *
2227
- * @param bool $flush_if_no_connectivity
2228
- *
2229
- * @return bool
2230
- */
2231
- function has_api_connectivity( $flush_if_no_connectivity = false ) {
2232
- $this->_logger->entrance();
2233
-
2234
- if ( isset( $this->_has_api_connection ) && ( $this->_has_api_connection || ! $flush_if_no_connectivity ) ) {
2235
- return $this->_has_api_connection;
2236
- }
2237
-
2238
- if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY &&
2239
- isset( $this->_storage->connectivity_test ) &&
2240
- true === $this->_storage->connectivity_test['is_connected']
2241
- ) {
2242
- unset( $this->_storage->connectivity_test );
2243
- }
2244
-
2245
- if ( ! $this->should_run_connectivity_test( $flush_if_no_connectivity ) ) {
2246
- $this->_has_api_connection = $this->_storage->connectivity_test['is_connected'];
2247
- /**
2248
- * @since 1.1.6 During dev mode, if there's connectivity - turn Freemius on regardless the configuration.
2249
- *
2250
- * @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.
2251
- */
2252
- $this->_is_on = $this->_storage->connectivity_test['is_active'] ||
2253
- $this->is_premium() ||
2254
- ( WP_FS__DEV_MODE && $this->_has_api_connection && ! WP_FS__SIMULATE_FREEMIUS_OFF );
2255
-
2256
- return $this->_has_api_connection;
2257
- }
2258
-
2259
- $pong = $this->ping();
2260
- $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
2261
-
2262
- if ( ! $is_connected ) {
2263
- // API failure.
2264
- $this->_add_connectivity_issue_message( $pong );
2265
- }
2266
-
2267
- $this->store_connectivity_info( $pong, $is_connected );
2268
-
2269
- return $this->_has_api_connection;
2270
- }
2271
-
2272
- /**
2273
- * @author Vova Feldman (@svovaf)
2274
- * @since 1.1.7.4
2275
- *
2276
- * @param object $pong
2277
- * @param bool $is_connected
2278
- */
2279
- private function store_connectivity_info( $pong, $is_connected ) {
2280
- $this->_logger->entrance();
2281
-
2282
- $version = $this->get_plugin_version();
2283
-
2284
- if ( ! $is_connected || WP_FS__SIMULATE_FREEMIUS_OFF ) {
2285
- $is_active = false;
2286
- } else {
2287
- $is_active = ( isset( $pong->is_active ) && true == $pong->is_active );
2288
- }
2289
-
2290
- $is_active = $this->apply_filters(
2291
- 'is_on',
2292
- $is_active,
2293
- $this->is_plugin_update(),
2294
- $version
2295
- );
2296
-
2297
- $this->_storage->connectivity_test = array(
2298
- 'is_connected' => $is_connected,
2299
- 'host' => $_SERVER['HTTP_HOST'],
2300
- 'server_ip' => WP_FS__REMOTE_ADDR,
2301
- 'is_active' => $is_active,
2302
- 'timestamp' => WP_FS__SCRIPT_START_TIME,
2303
- // Last version with connectivity attempt.
2304
- 'version' => $version,
2305
- );
2306
-
2307
- $this->_has_api_connection = $is_connected;
2308
- $this->_is_on = $is_active || ( WP_FS__DEV_MODE && $is_connected && ! WP_FS__SIMULATE_FREEMIUS_OFF );
2309
- }
2310
-
2311
- /**
2312
- * Force turning Freemius on.
2313
- *
2314
- * @author Vova Feldman (@svovaf)
2315
- * @since 1.1.8.1
2316
- *
2317
- * @return bool TRUE if successfully turned on.
2318
- */
2319
- private function turn_on() {
2320
- $this->_logger->entrance();
2321
-
2322
- if ( $this->is_on() || ! isset( $this->_storage->connectivity_test['is_active'] ) ) {
2323
- return false;
2324
- }
2325
-
2326
- $updated_connectivity = $this->_storage->connectivity_test;
2327
- $updated_connectivity['is_active'] = true;
2328
- $updated_connectivity['timestamp'] = WP_FS__SCRIPT_START_TIME;
2329
- $this->_storage->connectivity_test = $updated_connectivity;
2330
-
2331
- $this->_is_on = true;
2332
-
2333
- return true;
2334
- }
2335
-
2336
- /**
2337
- * Anonymous and unique site identifier (Hash).
2338
- *
2339
- * @author Vova Feldman (@svovaf)
2340
- * @since 1.1.0
2341
- *
2342
- * @return string
2343
- */
2344
- function get_anonymous_id() {
2345
- $unique_id = self::$_accounts->get_option( 'unique_id' );
2346
 
2347
- if ( empty( $unique_id ) || ! is_string( $unique_id ) ) {
2348
- $key = get_site_url();
 
 
2349
 
2350
- // If localhost, assign microtime instead of domain.
2351
- if ( WP_FS__IS_LOCALHOST ||
2352
- false !== strpos( $key, 'localhost' ) ||
2353
- false === strpos( $key, '.' )
2354
- ) {
2355
- $key = microtime();
2356
- }
2357
 
 
2358
  /**
2359
- * Base the unique identifier on the WP secure authentication key. Which
2360
- * turns the key into a secret anonymous identifier.
 
 
2361
  *
2362
  * @author Vova Feldman (@svovaf)
2363
- * @since 1.2.3
2364
  */
2365
- $unique_id = md5( $key . SECURE_AUTH_KEY );
 
 
 
 
2366
 
2367
- self::$_accounts->set_option( 'unique_id', $unique_id, true );
 
 
2368
  }
2369
 
2370
- $this->_logger->departure( $unique_id );
2371
 
2372
- return $unique_id;
2373
- }
2374
 
2375
- /**
2376
- * @author Vova Feldman (@svovaf)
2377
- * @since 1.1.7.4
2378
- *
2379
- * @return \WP_User
2380
- */
2381
- static function _get_current_wp_user() {
2382
- self::require_pluggable_essentials();
2383
-
2384
- return wp_get_current_user();
2385
- }
2386
-
2387
- /**
2388
- * @author Vova Feldman (@svovaf)
2389
- * @since 1.2.1.7
2390
- *
2391
- * @param string $email
2392
- *
2393
- * @return bool
2394
- */
2395
- static function is_valid_email( $email ) {
2396
- if ( false === filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
2397
- return false;
2398
- }
2399
-
2400
- $parts = explode( '@', $email );
2401
-
2402
- if ( 2 !== count( $parts ) || empty( $parts[1] ) ) {
2403
- return false;
2404
- }
2405
-
2406
- $blacklist = array(
2407
- 'admin.',
2408
- 'webmaster.',
2409
- 'localhost.',
2410
- 'dev.',
2411
- 'development.',
2412
- 'test.',
2413
- 'stage.',
2414
- 'staging.',
2415
- );
2416
-
2417
- // Make sure domain is not one of the blacklisted.
2418
- foreach ( $blacklist as $invalid ) {
2419
- if ( 0 === strpos( $parts[1], $invalid ) ) {
2420
- return false;
2421
- }
2422
- }
2423
-
2424
- // Get the UTF encoded domain name.
2425
- $domain = idn_to_ascii( $parts[1] ) . '.';
2426
-
2427
- return ( checkdnsrr( $domain, 'MX' ) || checkdnsrr( $domain, 'A' ) );
2428
- }
2429
-
2430
- /**
2431
- * Generate API connectivity issue message.
2432
- *
2433
- * @author Vova Feldman (@svovaf)
2434
- * @since 1.0.9
2435
- *
2436
- * @param mixed $api_result
2437
- * @param bool $is_first_failure
2438
- */
2439
- function _add_connectivity_issue_message( $api_result, $is_first_failure = true ) {
2440
- if ( ! $this->is_premium() && $this->_enable_anonymous ) {
2441
- // Don't add message if it's the free version and can run anonymously.
2442
- return;
2443
- }
2444
-
2445
- if ( ! function_exists( 'wp_nonce_url' ) ) {
2446
- require_once ABSPATH . 'wp-includes/functions.php';
2447
- }
2448
-
2449
- $current_user = self::_get_current_wp_user();
2450
- // $admin_email = get_option( 'admin_email' );
2451
- $admin_email = $current_user->user_email;
2452
-
2453
- // Aliases.
2454
- $deactivate_plugin_title = $this->esc_html_inline( 'That\'s exhausting, please deactivate', 'deactivate-plugin-title' );
2455
- $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' );
2456
- $install_previous_title = $this->esc_html_inline( 'Let\'s try your previous version', 'install-previous-title' );
2457
- $install_previous_desc = $this->esc_html_inline( 'Uninstall this version and install the previous one.', 'install-previous-desc' );
2458
- $fix_issue_title = $this->esc_html_inline( 'Yes - I\'m giving you a chance to fix it', 'fix-issue-title' );
2459
- $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' );
2460
- /* translators: %s: product title (e.g. "Awesome Plugin" requires an access to...) */
2461
- $x_requires_access_to_api = $this->esc_html_inline( '%s requires an access to our API.', 'x-requires-access-to-api' );
2462
- $sysadmin_title = $this->esc_html_inline( 'I\'m a system administrator', 'sysadmin-title' );
2463
- $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' );
2464
 
2465
- $message = false;
2466
- if ( is_object( $api_result ) &&
2467
- isset( $api_result->error ) &&
2468
- isset( $api_result->error->code )
2469
- ) {
2470
- switch ( $api_result->error->code ) {
2471
- case 'curl_missing':
2472
- $missing_methods = '';
2473
- if ( is_array( $api_result->missing_methods ) &&
2474
- ! empty( $api_result->missing_methods )
2475
- ) {
2476
- foreach ( $api_result->missing_methods as $m ) {
2477
- if ( 'curl_version' === $m ) {
2478
- continue;
2479
- }
2480
-
2481
- if ( ! empty( $missing_methods ) ) {
2482
- $missing_methods .= ', ';
2483
- }
2484
-
2485
- $missing_methods .= sprintf( '<code>%s</code>', $m );
2486
- }
2487
-
2488
- if ( ! empty( $missing_methods ) ) {
2489
- $missing_methods = sprintf(
2490
- '<br><br><b>%s</b> %s',
2491
- $this->esc_html_inline( 'Disabled method(s):', 'curl-disabled-methods' ),
2492
- $missing_methods
2493
- );
2494
- }
2495
- }
2496
-
2497
- $message = sprintf(
2498
- $x_requires_access_to_api . ' ' .
2499
- $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' ) . ' ' .
2500
- $missing_methods .
2501
- ' %s',
2502
- '<b>' . $this->get_plugin_name() . '</b>',
2503
- sprintf(
2504
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2505
- sprintf(
2506
- '<a class="fs-resolve" data-type="curl" href="#"><b>%s</b></a>%s',
2507
- $this->get_text_inline( 'I don\'t know what is cURL or how to install it, help me!', 'curl-missing-no-clue-title' ),
2508
- ' - ' . sprintf(
2509
- $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' ),
2510
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2511
- )
2512
- ),
2513
- sprintf(
2514
- '<b>%s</b> - %s',
2515
- $sysadmin_title,
2516
- 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 ) ) )
2517
- ),
2518
- sprintf(
2519
- '<a href="%s"><b>%s</b></a> - %s',
2520
- 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 ),
2521
- $deactivate_plugin_title,
2522
- $deactivate_plugin_desc
2523
- )
2524
- )
2525
- );
2526
- break;
2527
- case 'cloudflare_ddos_protection':
2528
- $message = sprintf(
2529
- $x_requires_access_to_api . ' ' .
2530
- $this->esc_html_inline( 'From unknown reason, CloudFlare, the firewall we use, blocks the connection.', 'cloudflare-blocks-connection-message' ) . ' ' .
2531
- $happy_to_resolve_issue_asap .
2532
- ' %s',
2533
- '<b>' . $this->get_plugin_name() . '</b>',
2534
- sprintf(
2535
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2536
- sprintf(
2537
- '<a class="fs-resolve" data-type="cloudflare" href="#"><b>%s</b></a>%s',
2538
- $fix_issue_title,
2539
- ' - ' . sprintf(
2540
- $fix_issue_desc,
2541
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2542
- )
2543
- ),
2544
- sprintf(
2545
- '<a href="%s" target="_blank"><b>%s</b></a> - %s',
2546
- sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),
2547
- $install_previous_title,
2548
- $install_previous_desc
2549
- ),
2550
- sprintf(
2551
- '<a href="%s"><b>%s</b></a> - %s',
2552
- 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 ),
2553
- $deactivate_plugin_title,
2554
- $deactivate_plugin_desc
2555
- )
2556
- )
2557
- );
2558
- break;
2559
- case 'squid_cache_block':
2560
- $message = sprintf(
2561
- $x_requires_access_to_api . ' ' .
2562
- $this->esc_html_inline( 'It looks like your server is using Squid ACL (access control lists), which blocks the connection.', 'squid-blocks-connection-message' ) .
2563
- ' %s',
2564
- '<b>' . $this->get_plugin_name() . '</b>',
2565
- sprintf(
2566
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2567
- sprintf(
2568
- '<a class="fs-resolve" data-type="squid" href="#"><b>%s</b></a> - %s',
2569
- $this->esc_html_inline( 'I don\'t know what is Squid or ACL, help me!', 'squid-no-clue-title' ),
2570
- sprintf(
2571
- $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' ),
2572
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2573
- )
2574
- ),
2575
- sprintf(
2576
- '<b>%s</b> - %s',
2577
- $sysadmin_title,
2578
- sprintf(
2579
- $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' ),
2580
- // We use a filter since the plugin might require additional API connectivity.
2581
- '<b>' . implode( ', ', $this->apply_filters( 'api_domains', array( 'api.freemius.com', 'wp.freemius.com' ) ) ) . '</b>',
2582
- $this->_module_type
2583
- )
2584
- ),
2585
- sprintf(
2586
- '<a href="%s"><b>%s</b></a> - %s',
2587
- 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 ),
2588
- $deactivate_plugin_title,
2589
- $deactivate_plugin_desc
2590
- )
2591
- )
2592
- );
2593
- break;
2594
- // default:
2595
- // $message = $this->get_text_inline( 'connectivity-test-fails-message' );
2596
- // break;
2597
- }
2598
- }
2599
-
2600
- $message_id = 'failed_connect_api';
2601
- $type = 'error';
2602
 
2603
- $connectivity_test_fails_message = $this->esc_html_inline( 'From unknown reason, the API connectivity test failed.', 'connectivity-test-fails-message' );
2604
-
2605
- if ( false === $message ) {
2606
- if ( $is_first_failure ) {
2607
- // First attempt failed.
2608
- $message = sprintf(
2609
- $x_requires_access_to_api . ' ' .
2610
- $connectivity_test_fails_message . ' ' .
2611
- $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>' .
2612
- '%s',
2613
- '<b>' . $this->get_plugin_name() . '</b>',
2614
- sprintf(
2615
- '<div id="fs_firewall_issue_options">%s %s</div>',
2616
- sprintf(
2617
- '<a class="button button-primary fs-resolve" data-type="retry_ping" href="#">%s</a>',
2618
- $this->get_text_inline( 'Yes - do your thing', 'yes-do-your-thing' )
2619
- ),
2620
- sprintf(
2621
- '<a href="%s" class="button">%s</a>',
2622
- 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 ),
2623
- $this->get_text_inline( 'No - just deactivate', 'no-deactivate' )
2624
- )
2625
- )
2626
- );
2627
-
2628
- $message_id = 'failed_connect_api_first';
2629
- $type = 'promotion';
2630
- } else {
2631
- // Second connectivity attempt failed.
2632
- $message = sprintf(
2633
- $x_requires_access_to_api . ' ' .
2634
- $connectivity_test_fails_message . ' ' .
2635
- $happy_to_resolve_issue_asap .
2636
- ' %s',
2637
- '<b>' . $this->get_plugin_name() . '</b>',
2638
- sprintf(
2639
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2640
- sprintf(
2641
- '<a class="fs-resolve" data-type="general" href="#"><b>%s</b></a>%s',
2642
- $fix_issue_title,
2643
- ' - ' . sprintf(
2644
- $fix_issue_desc,
2645
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2646
- )
2647
- ),
2648
- sprintf(
2649
- '<a href="%s" target="_blank"><b>%s</b></a> - %s',
2650
- sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),
2651
- $install_previous_title,
2652
- $install_previous_desc
2653
- ),
2654
- sprintf(
2655
- '<a href="%s"><b>%s</b></a> - %s',
2656
- 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 ),
2657
- $deactivate_plugin_title,
2658
- $deactivate_plugin_desc
2659
- )
2660
- )
2661
- );
2662
- }
2663
- }
2664
-
2665
- $this->_admin_notices->add_sticky(
2666
- $message,
2667
- $message_id,
2668
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
2669
- $type
2670
- );
2671
- }
2672
-
2673
- /**
2674
- * Handle user request to resolve connectivity issue.
2675
- * This method will send an email to Freemius API technical staff for resolution.
2676
- * The email will contain server's info and installed plugins (might be caching issue).
2677
- *
2678
- * @author Vova Feldman (@svovaf)
2679
- * @since 1.0.9
2680
- */
2681
- function _email_about_firewall_issue() {
2682
- $this->_admin_notices->remove_sticky( 'failed_connect_api' );
2683
-
2684
- $pong = $this->ping();
2685
-
2686
- $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
2687
-
2688
- if ( $is_connected ) {
2689
- $this->store_connectivity_info( $pong, $is_connected );
2690
-
2691
- echo $this->get_after_plugin_activation_redirect_url();
2692
- exit;
2693
- }
2694
-
2695
- $current_user = self::_get_current_wp_user();
2696
- $admin_email = $current_user->user_email;
2697
-
2698
- $error_type = fs_request_get( 'error_type', 'general' );
2699
-
2700
- switch ( $error_type ) {
2701
- case 'squid':
2702
- $title = 'Squid ACL Blocking Issue';
2703
- break;
2704
- case 'cloudflare':
2705
- $title = 'CloudFlare Blocking Issue';
2706
- break;
2707
- default:
2708
- $title = 'API Connectivity Issue';
2709
- break;
2710
- }
2711
-
2712
- $custom_email_sections = array();
2713
-
2714
- // Add 'API Error' custom email section.
2715
- $custom_email_sections['api_error'] = array(
2716
- 'title' => 'API Error',
2717
- 'rows' => array(
2718
- 'ping' => array(
2719
- 'API Error',
2720
- is_string( $pong ) ? htmlentities( $pong ) : json_encode( $pong )
2721
- ),
2722
- )
2723
- );
2724
-
2725
- // Send email with technical details to resolve API connectivity issues.
2726
- $this->send_email(
2727
- 'api@freemius.com', // recipient
2728
- $title . ' [' . $this->get_plugin_name() . ']', // subject
2729
- $custom_email_sections,
2730
- array( "Reply-To: $admin_email <$admin_email>" ) // headers
2731
- );
2732
-
2733
- $this->_admin_notices->add_sticky(
2734
- sprintf(
2735
- $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' ),
2736
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2737
- ),
2738
- 'server_details_sent'
2739
- );
2740
-
2741
- // Action was taken, tell that API connectivity troubleshooting should be off now.
2742
-
2743
- echo "1";
2744
- exit;
2745
- }
2746
-
2747
- /**
2748
- * Handle connectivity test retry approved by the user.
2749
- *
2750
- * @author Vova Feldman (@svovaf)
2751
- * @since 1.1.7.4
2752
- */
2753
- function _retry_connectivity_test() {
2754
- $this->_admin_notices->remove_sticky( 'failed_connect_api_first' );
2755
-
2756
- $pong = $this->ping();
2757
-
2758
- $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
2759
-
2760
- if ( $is_connected ) {
2761
- $this->store_connectivity_info( $pong, $is_connected );
2762
-
2763
- echo $this->get_after_plugin_activation_redirect_url();
2764
- } else {
2765
- // Add connectivity issue message after 2nd failed attempt.
2766
- $this->_add_connectivity_issue_message( $pong, false );
2767
-
2768
- echo "1";
2769
- }
2770
-
2771
- exit;
2772
- }
2773
-
2774
- static function _add_firewall_issues_javascript() {
2775
- $params = array();
2776
- fs_require_once_template( 'firewall-issues-js.php', $params );
2777
- }
2778
-
2779
- #endregion
2780
-
2781
- #----------------------------------------------------------------------------------
2782
- #region Email
2783
- #----------------------------------------------------------------------------------
2784
-
2785
- /**
2786
- * Generates and sends an HTML email with customizable sections.
2787
- *
2788
- * @author Leo Fajardo (@leorw)
2789
- * @since 1.1.2
2790
- *
2791
- * @param string $to_address
2792
- * @param string $subject
2793
- * @param array $sections
2794
- * @param array $headers
2795
- *
2796
- * @return bool Whether the email contents were sent successfully.
2797
- */
2798
- private function send_email(
2799
- $to_address,
2800
- $subject,
2801
- $sections = array(),
2802
- $headers = array()
2803
- ) {
2804
- $default_sections = $this->get_email_sections();
2805
-
2806
- // Insert new sections or replace the default email sections.
2807
- if ( is_array( $sections ) && ! empty( $sections ) ) {
2808
- foreach ( $sections as $section_id => $custom_section ) {
2809
- if ( ! isset( $default_sections[ $section_id ] ) ) {
2810
- // If the section does not exist, add it.
2811
- $default_sections[ $section_id ] = $custom_section;
2812
- } else {
2813
- // If the section already exists, override it.
2814
- $current_section = $default_sections[ $section_id ];
2815
-
2816
- // Replace the current section's title if a custom section title exists.
2817
- if ( isset( $custom_section['title'] ) ) {
2818
- $current_section['title'] = $custom_section['title'];
2819
- }
2820
-
2821
- // Insert new rows under the current section or replace the default rows.
2822
- if ( isset( $custom_section['rows'] ) && is_array( $custom_section['rows'] ) && ! empty( $custom_section['rows'] ) ) {
2823
- foreach ( $custom_section['rows'] as $row_id => $row ) {
2824
- $current_section['rows'][ $row_id ] = $row;
2825
- }
2826
- }
2827
-
2828
- $default_sections[ $section_id ] = $current_section;
2829
- }
2830
- }
2831
- }
2832
-
2833
- $vars = array( 'sections' => $default_sections );
2834
- $message = fs_get_template( 'email.php', $vars );
2835
-
2836
- // Set the type of email to HTML.
2837
- $headers[] = 'Content-type: text/html; charset=UTF-8';
2838
-
2839
- $header_string = implode( "\r\n", $headers );
2840
-
2841
- return wp_mail(
2842
- $to_address,
2843
- $subject,
2844
- $message,
2845
- $header_string
2846
- );
2847
- }
2848
-
2849
- /**
2850
- * Generates the data for the sections of the email content.
2851
- *
2852
- * @author Leo Fajardo (@leorw)
2853
- * @since 1.1.2
2854
- *
2855
- * @return array
2856
- */
2857
- private function get_email_sections() {
2858
- // Retrieve the current user's information so that we can get the user's email, first name, and last name below.
2859
- $current_user = self::_get_current_wp_user();
2860
-
2861
- // Retrieve the cURL version information so that we can get the version number below.
2862
- $curl_version_information = curl_version();
2863
-
2864
- $active_plugin = self::get_active_plugins();
2865
-
2866
- // Generate the list of active plugins separated by new line.
2867
- $active_plugin_string = '';
2868
- foreach ( $active_plugin as $plugin ) {
2869
- $active_plugin_string .= sprintf(
2870
- '<a href="%s">%s</a> [v%s]<br>',
2871
- $plugin['PluginURI'],
2872
- $plugin['Name'],
2873
- $plugin['Version']
2874
- );
2875
- }
2876
-
2877
- $server_ip = WP_FS__REMOTE_ADDR;
2878
-
2879
- // Add PHP info for deeper investigation.
2880
- ob_start();
2881
- phpinfo();
2882
- $php_info = ob_get_clean();
2883
-
2884
- $api_domain = substr( FS_API__ADDRESS, strpos( FS_API__ADDRESS, ':' ) + 3 );
2885
-
2886
- // Generate the default email sections.
2887
- $sections = array(
2888
- 'sdk' => array(
2889
- 'title' => 'SDK',
2890
- 'rows' => array(
2891
- 'fs_version' => array( 'FS Version', $this->version ),
2892
- 'curl_version' => array( 'cURL Version', $curl_version_information['version'] )
2893
- )
2894
- ),
2895
- 'plugin' => array(
2896
- 'title' => ucfirst( $this->get_module_type() ),
2897
- 'rows' => array(
2898
- 'name' => array( 'Name', $this->get_plugin_name() ),
2899
- 'version' => array( 'Version', $this->get_plugin_version() )
2900
- )
2901
- ),
2902
- 'api' => array(
2903
- 'title' => 'API Subdomain',
2904
- 'rows' => array(
2905
- 'dns' => array(
2906
- 'DNS_CNAME',
2907
- function_exists( 'dns_get_record' ) ?
2908
- var_export( dns_get_record( $api_domain, DNS_CNAME ), true ) :
2909
- 'dns_get_record() disabled/blocked'
2910
- ),
2911
- 'ip' => array(
2912
- 'IP',
2913
- function_exists( 'gethostbyname' ) ?
2914
- gethostbyname( $api_domain ) :
2915
- 'gethostbyname() disabled/blocked'
2916
- ),
2917
- ),
2918
- ),
2919
- 'site' => array(
2920
- 'title' => 'Site',
2921
- 'rows' => array(
2922
- 'unique_id' => array( 'Unique ID', $this->get_anonymous_id() ),
2923
- 'address' => array( 'Address', site_url() ),
2924
- 'host' => array(
2925
- 'HTTP_HOST',
2926
- ( ! empty( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : '' )
2927
- ),
2928
- 'hosting' => array(
2929
- 'Hosting Company' => fs_request_has( 'hosting_company' ) ?
2930
- fs_request_get( 'hosting_company' ) :
2931
- 'Unknown',
2932
- ),
2933
- 'server_addr' => array(
2934
- 'SERVER_ADDR',
2935
- '<a href="http://www.projecthoneypot.org/ip_' . $server_ip . '">' . $server_ip . '</a>'
2936
- )
2937
- )
2938
- ),
2939
- 'user' => array(
2940
- 'title' => 'User',
2941
- 'rows' => array(
2942
- 'email' => array( 'Email', $current_user->user_email ),
2943
- 'first' => array( 'First', $current_user->user_firstname ),
2944
- 'last' => array( 'Last', $current_user->user_lastname )
2945
- )
2946
- ),
2947
- 'plugins' => array(
2948
- 'title' => 'Plugins',
2949
- 'rows' => array(
2950
- 'active_plugins' => array( 'Active Plugins', $active_plugin_string )
2951
- )
2952
- ),
2953
- 'php_info' => array(
2954
- 'title' => 'PHP Info',
2955
- 'rows' => array(
2956
- 'info' => array( $php_info )
2957
- ),
2958
- )
2959
- );
2960
-
2961
- // Allow the sections to be modified by other code.
2962
- $sections = $this->apply_filters( 'email_template_sections', $sections );
2963
-
2964
- return $sections;
2965
- }
2966
-
2967
- #endregion
2968
-
2969
- #----------------------------------------------------------------------------------
2970
- #region Initialization
2971
- #----------------------------------------------------------------------------------
2972
-
2973
- /**
2974
- * Init plugin's Freemius instance.
2975
- *
2976
- * @author Vova Feldman (@svovaf)
2977
- * @since 1.0.1
2978
- *
2979
- * @param number $id
2980
- * @param string $public_key
2981
- * @param bool $is_live
2982
- * @param bool $is_premium
2983
- */
2984
- function init( $id, $public_key, $is_live = true, $is_premium = true ) {
2985
- $this->_logger->entrance();
2986
-
2987
- $this->dynamic_init( array(
2988
- 'id' => $id,
2989
- 'public_key' => $public_key,
2990
- 'is_live' => $is_live,
2991
- 'is_premium' => $is_premium,
2992
- ) );
2993
- }
2994
-
2995
- /**
2996
- * Dynamic initiator, originally created to support initiation
2997
- * with parent_id for add-ons.
2998
- *
2999
- * @author Vova Feldman (@svovaf)
3000
- * @since 1.0.6
3001
- *
3002
- * @param array $plugin_info
3003
- *
3004
- * @throws Freemius_Exception
3005
- */
3006
- function dynamic_init( array $plugin_info ) {
3007
- $this->_logger->entrance();
3008
-
3009
- $this->parse_settings( $plugin_info );
3010
-
3011
- if ( $this->has_affiliate_program() ) {
3012
- $this->fetch_affiliate_and_terms();
3013
  }
3014
 
3015
- if ( ! self::is_ajax() ) {
3016
- if ( ! $this->is_addon() || $this->is_only_premium() ) {
3017
- add_action( 'admin_menu', array( &$this, '_prepare_admin_menu' ), WP_FS__LOWEST_PRIORITY );
3018
- }
3019
  }
3020
 
3021
- if ( $this->should_stop_execution() ) {
3022
- return;
3023
- }
3024
-
3025
- if ( ! $this->is_registered() ) {
3026
- if ( $this->is_anonymous() ) {
3027
- // If user skipped, no need to test connectivity.
3028
- $this->_has_api_connection = true;
3029
- $this->_is_on = true;
3030
- } else {
3031
- if ( ! $this->has_api_connectivity() ) {
3032
- if ( $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) ||
3033
- $this->_admin_notices->has_sticky( 'failed_connect_api' )
3034
- ) {
3035
- if ( ! $this->_enable_anonymous || $this->is_premium() ) {
3036
- // If anonymous mode is disabled, add firewall admin-notice message.
3037
- add_action( 'admin_footer', array( 'Freemius', '_add_firewall_issues_javascript' ) );
3038
-
3039
- $this->add_ajax_action( 'resolve_firewall_issues', array(
3040
- &$this,
3041
- '_email_about_firewall_issue'
3042
- ) );
3043
-
3044
- $this->add_ajax_action( 'retry_connectivity_test', array(
3045
- &$this,
3046
- '_retry_connectivity_test'
3047
- ) );
3048
- }
3049
- }
3050
-
3051
- return;
3052
- } else {
3053
- $this->_admin_notices->remove_sticky( array(
3054
- 'failed_connect_api_first',
3055
- 'failed_connect_api',
3056
- ) );
3057
-
3058
- if ( $this->_anonymous_mode ) {
3059
- // Simulate anonymous mode.
3060
- $this->_is_anonymous = true;
3061
- }
3062
- }
3063
- }
3064
-
3065
- // Check if Freemius is on for the current plugin.
3066
- // This MUST be executed after all the plugin variables has been loaded.
3067
- if ( ! $this->is_on() ) {
3068
- return;
3069
- }
3070
- }
3071
-
3072
- if ( $this->has_api_connectivity() ) {
3073
- if ( self::is_cron() ) {
3074
- $this->hook_callback_to_sync_cron();
3075
- } else if ( $this->is_user_in_admin() ) {
3076
- /**
3077
- * Schedule daily data sync cron if:
3078
- *
3079
- * 1. User opted-in (for tracking).
3080
- * 2. If skipped, but later upgraded (opted-in via upgrade).
3081
- *
3082
- * @author Vova Feldman (@svovaf)
3083
- * @since 1.1.7.3
3084
- *
3085
- */
3086
- if ( $this->is_registered() ) {
3087
- if ( ! $this->is_sync_cron_on() && $this->is_tracking_allowed() ) {
3088
- $this->schedule_sync_cron();
3089
- }
3090
- }
3091
-
3092
- /**
3093
- * Check if requested for manual blocking background sync.
3094
- */
3095
- if ( fs_request_has( 'background_sync' ) ) {
3096
- $this->run_manual_sync();
3097
- }
3098
- }
3099
- }
3100
-
3101
- if ( $this->is_registered() ) {
3102
- $this->hook_callback_to_install_sync();
3103
- }
3104
-
3105
- if ( $this->is_addon() ) {
3106
- if ( $this->is_parent_plugin_installed() ) {
3107
- // Link to parent FS.
3108
- $this->_parent = self::get_instance_by_id( $this->_plugin->parent_plugin_id );
3109
-
3110
- // Get parent plugin reference.
3111
- $this->_parent_plugin = $this->_parent->get_plugin();
3112
- }
3113
- }
3114
-
3115
- if ( $this->is_user_in_admin() ) {
3116
- if ( self::is_plugins_page() && $this->is_plugin() ) {
3117
- $this->hook_plugin_action_links();
3118
- }
3119
-
3120
- if ( $this->is_addon() ) {
3121
- if ( ! $this->is_parent_plugin_installed() ) {
3122
- $parent_name = $this->get_option( $plugin_info, 'parent_name', null );
3123
-
3124
- if ( isset( $plugin_info['parent'] ) ) {
3125
- $parent_name = $this->get_option( $plugin_info['parent'], 'name', null );
3126
- }
3127
-
3128
- $this->_admin_notices->add(
3129
- ( ! empty( $parent_name ) ?
3130
- 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 ) :
3131
- 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() )
3132
- ),
3133
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
3134
- 'error'
3135
- );
3136
-
3137
- return;
3138
- } else {
3139
- if ( $this->_parent->is_registered() && ! $this->is_registered() ) {
3140
- // If parent plugin activated, automatically install add-on for the user.
3141
- $this->_activate_addon_account( $this->_parent );
3142
- } else if ( ! $this->_parent->is_registered() && $this->is_registered() ) {
3143
- // If add-on activated and parent not, automatically install parent for the user.
3144
- $this->activate_parent_account( $this->_parent );
3145
- }
3146
-
3147
- // @todo This should be only executed on activation. It should be migrated to register_activation_hook() together with other activation related logic.
3148
- if ( $this->is_premium() ) {
3149
- // Remove add-on download admin-notice.
3150
- $this->_parent->_admin_notices->remove_sticky( array(
3151
- 'addon_plan_upgraded_' . $this->_slug,
3152
- 'no_addon_license_' . $this->_slug,
3153
- ) );
3154
- }
3155
 
3156
- // $this->deactivate_premium_only_addon_without_license();
3157
- }
3158
- } else {
3159
- if ( $this->has_addons() &&
3160
- 'plugin-information' === fs_request_get( 'tab', false ) &&
3161
- $this->get_id() == fs_request_get( 'parent_plugin_id', false )
3162
- ) {
3163
- require_once WP_FS__DIR_INCLUDES . '/fs-plugin-info-dialog.php';
3164
 
3165
- new FS_Plugin_Info_Dialog( $this );
3166
- }
3167
- }
3168
 
3169
- add_action( 'admin_init', array( &$this, '_admin_init_action' ) );
 
 
 
 
 
 
 
3170
 
3171
- // if ( $this->is_registered() ||
3172
- // $this->is_anonymous() ||
3173
- // $this->is_pending_activation()
3174
- // ) {
3175
- // $this->_init_admin();
3176
- // }
3177
- }
3178
-
3179
- /**
3180
- * Should be called outside `$this->is_user_in_admin()` scope
3181
- * because the updater has some logic that needs to be executed
3182
- * during AJAX calls.
3183
- *
3184
- * Currently we need to hook to the `http_request_host_is_external` filter.
3185
- * In the future, there might be additional logic added.
3186
- *
3187
- * @author Vova Feldman
3188
- * @since 1.2.1.6
3189
- */
3190
- if ( $this->is_premium() && $this->has_release_on_freemius() ) {
3191
- new FS_Plugin_Updater( $this );
3192
- }
3193
-
3194
- $this->do_action( 'initiated' );
3195
-
3196
- if ( $this->_storage->prev_is_premium !== $this->_plugin->is_premium ) {
3197
- if ( isset( $this->_storage->prev_is_premium ) ) {
3198
- $this->apply_filters(
3199
- 'after_code_type_change',
3200
- // New code type.
3201
- $this->_plugin->is_premium
3202
- );
3203
- } else {
3204
- // Set for code type for the first time.
3205
- $this->_storage->prev_is_premium = $this->_plugin->is_premium;
3206
- }
3207
- }
3208
-
3209
- if ( ! $this->is_addon() ) {
3210
- if ( $this->is_registered() ) {
3211
- // Fix for upgrade from versions < 1.0.9.
3212
- if ( ! isset( $this->_storage->activation_timestamp ) ) {
3213
- $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME;
3214
- }
3215
-
3216
- $this->do_action( 'after_init_plugin_registered' );
3217
- } else if ( $this->is_anonymous() ) {
3218
- $this->do_action( 'after_init_plugin_anonymous' );
3219
- } else if ( $this->is_pending_activation() ) {
3220
- $this->do_action( 'after_init_plugin_pending_activations' );
3221
- }
3222
- } else {
3223
- if ( $this->is_registered() ) {
3224
- $this->do_action( 'after_init_addon_registered' );
3225
- } else if ( $this->is_anonymous() ) {
3226
- $this->do_action( 'after_init_addon_anonymous' );
3227
- } else if ( $this->is_pending_activation() ) {
3228
- $this->do_action( 'after_init_addon_pending_activations' );
3229
- }
3230
- }
3231
- }
3232
-
3233
- /**
3234
- * @author Leo Fajardo (@leorw)
3235
- *
3236
- * @since 1.2.1.5
3237
- */
3238
- function _stop_tracking_callback() {
3239
- $this->_logger->entrance();
3240
-
3241
- $this->check_ajax_referer( 'stop_tracking' );
3242
-
3243
- $result = $this->stop_tracking();
3244
-
3245
- if ( true === $result ) {
3246
- self::shoot_ajax_success();
3247
- }
3248
-
3249
- $this->_logger->api_error( $result );
3250
-
3251
- self::shoot_ajax_failure(
3252
- sprintf( $this->get_text_inline( 'Unexpected API error. Please contact the %s\'s author with the following error.', 'unexpected-api-error' ), $this->_module_type ) .
3253
- ( $this->is_api_error( $result ) && isset( $result->error ) ?
3254
- $result->error->message :
3255
- var_export( $result, true ) )
3256
- );
3257
- }
3258
-
3259
- /**
3260
- * @author Leo Fajardo (@leorw)
3261
- * @since 1.2.1.5
3262
- */
3263
- function _allow_tracking_callback() {
3264
- $this->_logger->entrance();
3265
-
3266
- $this->check_ajax_referer( 'allow_tracking' );
3267
-
3268
- $result = $this->allow_tracking();
3269
-
3270
- if ( true === $result ) {
3271
- self::shoot_ajax_success();
3272
- }
3273
-
3274
- $this->_logger->api_error( $result );
3275
-
3276
- self::shoot_ajax_failure(
3277
- sprintf( $this->get_text_inline( 'Unexpected API error. Please contact the %s\'s author with the following error.', 'unexpected-api-error' ), $this->_module_type ) .
3278
- ( $this->is_api_error( $result ) && isset( $result->error ) ?
3279
- $result->error->message :
3280
- var_export( $result, true ) )
3281
- );
3282
- }
3283
-
3284
- /**
3285
- * Opt-out from usage tracking.
3286
- *
3287
- * Note: This will not delete the account information but will stop all tracking.
3288
- *
3289
- * Returns:
3290
- * 1. FALSE - If the user never opted-in.
3291
- * 2. TRUE - If successfully opted-out.
3292
- * 3. object - API result on failure.
3293
- *
3294
- * @author Leo Fajardo (@leorw)
3295
- * @since 1.2.1.5
3296
- *
3297
- * @return bool|object
3298
- */
3299
- function stop_tracking() {
3300
- $this->_logger->entrance();
3301
-
3302
- if ( ! $this->is_registered() ) {
3303
- // User never opted-in.
3304
- return false;
3305
- }
3306
-
3307
- if ( $this->is_tracking_prohibited() ) {
3308
- // Already disconnected.
3309
- return true;
3310
- }
3311
-
3312
- // Send update to FS.
3313
- $result = $this->get_api_site_scope()->call( '/?fields=is_disconnected', 'put', array(
3314
- 'is_disconnected' => true
3315
- ) );
3316
-
3317
- if ( ! $this->is_api_result_entity( $result ) ||
3318
- ! isset( $result->is_disconnected ) ||
3319
- ! $result->is_disconnected
3320
- ) {
3321
- $this->_logger->api_error( $result );
3322
-
3323
- return $result;
3324
- }
3325
-
3326
- $this->_site->is_disconnected = $result->is_disconnected;
3327
- $this->_store_site();
3328
-
3329
- $this->clear_sync_cron();
3330
-
3331
- // Successfully disconnected.
3332
- return true;
3333
- }
3334
-
3335
- /**
3336
- * Opt-in back into usage tracking.
3337
- *
3338
- * Note: This will only work if the user opted-in previously.
3339
- *
3340
- * Returns:
3341
- * 1. FALSE - If the user never opted-in.
3342
- * 2. TRUE - If successfully opted-in back to usage tracking.
3343
- * 3. object - API result on failure.
3344
- *
3345
- * @author Leo Fajardo (@leorw)
3346
- * @since 1.2.1.5
3347
- *
3348
- * @return bool|object
3349
- */
3350
- function allow_tracking() {
3351
- $this->_logger->entrance();
3352
-
3353
- if ( ! $this->is_registered() ) {
3354
- // User never opted-in.
3355
- return false;
3356
- }
3357
-
3358
- if ( $this->is_tracking_allowed() ) {
3359
- // Tracking already allowed.
3360
- return true;
3361
- }
3362
-
3363
- $result = $this->get_api_site_scope()->call( '/?is_disconnected', 'put', array(
3364
- 'is_disconnected' => false
3365
- ) );
3366
-
3367
- if ( ! $this->is_api_result_entity( $result ) ||
3368
- ! isset( $result->is_disconnected ) ||
3369
- $result->is_disconnected
3370
- ) {
3371
- $this->_logger->api_error( $result );
3372
-
3373
- return $result;
3374
- }
3375
-
3376
- $this->_site->is_disconnected = $result->is_disconnected;
3377
- $this->_store_site();
3378
-
3379
- $this->schedule_sync_cron();
3380
-
3381
- // Successfully reconnected.
3382
- return true;
3383
- }
3384
-
3385
- /**
3386
- * If user opted-in and later disabled usage-tracking,
3387
- * re-allow tracking for licensing and updates.
3388
- *
3389
- * @author Leo Fajardo (@leorw)
3390
- *
3391
- * @since 1.2.1.5
3392
- */
3393
- private function reconnect_locally() {
3394
- $this->_logger->entrance();
3395
-
3396
- if ( $this->is_tracking_prohibited() &&
3397
- $this->is_registered()
3398
- ) {
3399
- $this->_site->is_disconnected = false;
3400
- $this->_store_site();
3401
- }
3402
- }
3403
-
3404
- /**
3405
- * Parse plugin's settings (as defined by the plugin dev).
3406
- *
3407
- * @author Vova Feldman (@svovaf)
3408
- * @since 1.1.7.3
3409
- *
3410
- * @param array $plugin_info
3411
- *
3412
- * @throws \Freemius_Exception
3413
- */
3414
- private function parse_settings( &$plugin_info ) {
3415
- $this->_logger->entrance();
3416
-
3417
- $id = $this->get_numeric_option( $plugin_info, 'id', false );
3418
- $public_key = $this->get_option( $plugin_info, 'public_key', false );
3419
- $secret_key = $this->get_option( $plugin_info, 'secret_key', null );
3420
- $parent_id = $this->get_numeric_option( $plugin_info, 'parent_id', null );
3421
- $parent_name = $this->get_option( $plugin_info, 'parent_name', null );
3422
-
3423
- /**
3424
- * @author Vova Feldman (@svovaf)
3425
- * @since 1.1.9 Try to pull secret key from external config.
3426
- */
3427
- if ( is_null( $secret_key ) && defined( "WP_FS__{$this->_slug}_SECRET_KEY" ) ) {
3428
- $secret_key = constant( "WP_FS__{$this->_slug}_SECRET_KEY" );
3429
- }
3430
 
3431
- if ( isset( $plugin_info['parent'] ) ) {
3432
- $parent_id = $this->get_numeric_option( $plugin_info['parent'], 'id', null );
3433
- // $parent_slug = $this->get_option( $plugin_info['parent'], 'slug', null );
3434
- // $parent_public_key = $this->get_option( $plugin_info['parent'], 'public_key', null );
3435
- // $parent_name = $this->get_option( $plugin_info['parent'], 'name', null );
3436
- }
3437
-
3438
- if ( false === $id ) {
3439
- throw new Freemius_Exception( array(
3440
- 'error' => array(
3441
- 'type' => 'ParameterNotSet',
3442
- 'message' => 'Plugin id parameter is not set.',
3443
- 'code' => 'plugin_id_not_set',
3444
- 'http' => 500,
3445
- )
3446
- ) );
3447
- }
3448
- if ( false === $public_key ) {
3449
- throw new Freemius_Exception( array(
3450
- 'error' => array(
3451
- 'type' => 'ParameterNotSet',
3452
- 'message' => 'Plugin public_key parameter is not set.',
3453
- 'code' => 'plugin_public_key_not_set',
3454
- 'http' => 500,
3455
- )
3456
- ) );
3457
- }
3458
-
3459
- $plugin = ( $this->_plugin instanceof FS_Plugin ) ?
3460
- $this->_plugin :
3461
- new FS_Plugin();
3462
-
3463
- $plugin->update( array(
3464
- 'id' => $id,
3465
- 'type' => $this->get_option( $plugin_info, 'type', $this->_module_type),
3466
- 'public_key' => $public_key,
3467
- 'slug' => $this->_slug,
3468
- 'parent_plugin_id' => $parent_id,
3469
- 'version' => $this->get_plugin_version(),
3470
- 'title' => $this->get_plugin_name(),
3471
- 'file' => $this->_plugin_basename,
3472
- 'is_premium' => $this->get_bool_option( $plugin_info, 'is_premium', true ),
3473
- 'is_live' => $this->get_bool_option( $plugin_info, 'is_live', true ),
3474
- 'affiliate_moderation' => $this->get_option( $plugin_info, 'has_affiliation' ),
3475
- ) );
3476
-
3477
- if ( $plugin->is_updated() ) {
3478
- // Update plugin details.
3479
- $this->_plugin = FS_Plugin_Manager::instance( $this->_module_id )->store( $plugin );
3480
- }
3481
- // Set the secret key after storing the plugin, we don't want to store the key in the storage.
3482
- $this->_plugin->secret_key = $secret_key;
3483
-
3484
- if ( ! isset( $plugin_info['menu'] ) ) {
3485
- $plugin_info['menu'] = array();
3486
-
3487
- if ( ! empty( $this->_storage->sdk_last_version ) &&
3488
- version_compare( $this->_storage->sdk_last_version, '1.1.2', '<=' )
3489
- ) {
3490
- // Backward compatibility to 1.1.2
3491
- $plugin_info['menu']['slug'] = isset( $plugin_info['menu_slug'] ) ?
3492
- $plugin_info['menu_slug'] :
3493
- $this->_slug;
3494
- }
3495
- }
3496
-
3497
- $this->_menu = FS_Admin_Menu_Manager::instance(
3498
- $this->_module_id,
3499
- $this->_module_type,
3500
- $this->get_unique_affix()
3501
- );
3502
-
3503
- $this->_menu->init( $plugin_info['menu'], $this->is_addon() );
3504
-
3505
- $this->_has_addons = $this->get_bool_option( $plugin_info, 'has_addons', false );
3506
- $this->_has_paid_plans = $this->get_bool_option( $plugin_info, 'has_paid_plans', true );
3507
- $this->_has_premium_version = $this->get_bool_option( $plugin_info, 'has_premium_version', $this->_has_paid_plans );
3508
- $this->_ignore_pending_mode = $this->get_bool_option( $plugin_info, 'ignore_pending_mode', false );
3509
- $this->_is_org_compliant = $this->get_bool_option( $plugin_info, 'is_org_compliant', true );
3510
- $this->_is_premium_only = $this->get_bool_option( $plugin_info, 'is_premium_only', false );
3511
- if ( $this->_is_premium_only ) {
3512
- // If premium only plugin, disable anonymous mode.
3513
- $this->_enable_anonymous = false;
3514
- $this->_anonymous_mode = false;
3515
- } else {
3516
- $this->_enable_anonymous = $this->get_bool_option( $plugin_info, 'enable_anonymous', true );
3517
- $this->_anonymous_mode = $this->get_bool_option( $plugin_info, 'anonymous_mode', false );
3518
- }
3519
- $this->_permissions = $this->get_option( $plugin_info, 'permissions', array() );
3520
-
3521
- if ( ! empty( $plugin_info['trial'] ) ) {
3522
- $this->_trial_days = $this->get_numeric_option(
3523
- $plugin_info['trial'],
3524
- 'days',
3525
- // Default to 0 - trial without days specification.
3526
- 0
3527
- );
3528
-
3529
- $this->_is_trial_require_payment = $this->get_bool_option( $plugin_info['trial'], 'is_require_payment', false );
3530
- }
3531
- }
3532
-
3533
- /**
3534
- * @param string[] $options
3535
- * @param string $key
3536
- * @param mixed $default
3537
- *
3538
- * @return bool
3539
- */
3540
- private function get_option( &$options, $key, $default = false ) {
3541
- return ! empty( $options[ $key ] ) ? $options[ $key ] : $default;
3542
- }
3543
-
3544
- private function get_bool_option( &$options, $key, $default = false ) {
3545
- return isset( $options[ $key ] ) && is_bool( $options[ $key ] ) ? $options[ $key ] : $default;
3546
- }
3547
-
3548
- private function get_numeric_option( &$options, $key, $default = false ) {
3549
- return isset( $options[ $key ] ) && is_numeric( $options[ $key ] ) ? $options[ $key ] : $default;
3550
- }
3551
-
3552
- /**
3553
- * Gate keeper.
3554
- *
3555
- * @author Vova Feldman (@svovaf)
3556
- * @since 1.1.7.3
3557
- *
3558
- * @return bool
3559
- */
3560
- private function should_stop_execution() {
3561
- if ( empty( $this->_storage->was_plugin_loaded ) ) {
3562
- /**
3563
- * Don't execute Freemius until plugin was fully loaded at least once,
3564
- * to give the opportunity for the activation hook to run before pinging
3565
- * the API for connectivity test. This logic is relevant for the
3566
- * identification of new plugin install vs. plugin update.
3567
- *
3568
- * @author Vova Feldman (@svovaf)
3569
- * @since 1.1.9
3570
- */
3571
- return true;
3572
- }
3573
-
3574
- if ( $this->is_activation_mode() ) {
3575
- if ( ! is_admin() ) {
3576
- /**
3577
- * If in activation mode, don't execute Freemius outside of the
3578
- * admin dashboard.
3579
- *
3580
- * @author Vova Feldman (@svovaf)
3581
- * @since 1.1.7.3
3582
- */
3583
- return true;
3584
- }
3585
-
3586
- if ( ! WP_FS__IS_HTTP_REQUEST ) {
3587
- /**
3588
- * If in activation and executed without HTTP context (e.g. CLI, Cronjob),
3589
- * then don't start Freemius.
3590
- *
3591
- * @author Vova Feldman (@svovaf)
3592
- * @since 1.1.6.3
3593
- *
3594
- * @link https://wordpress.org/support/topic/errors-in-the-freemius-class-when-running-in-wordpress-in-cli
3595
- */
3596
- return true;
3597
- }
3598
-
3599
- if ( self::is_cron() ) {
3600
- /**
3601
- * If in activation mode, don't execute Freemius during wp crons
3602
- * (wp crons have HTTP context - called as HTTP request).
3603
- *
3604
- * @author Vova Feldman (@svovaf)
3605
- * @since 1.1.7.3
3606
- */
3607
- return true;
3608
- }
3609
-
3610
- if ( self::is_ajax() &&
3611
- ! $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) &&
3612
- ! $this->_admin_notices->has_sticky( 'failed_connect_api' )
3613
- ) {
3614
- /**
3615
- * During activation, if running in AJAX mode, unless there's a sticky
3616
- * connectivity issue notice, don't run Freemius.
3617
- *
3618
- * @author Vova Feldman (@svovaf)
3619
- * @since 1.1.7.3
3620
- */
3621
- return true;
3622
- }
3623
- }
3624
-
3625
- return false;
3626
- }
3627
-
3628
- /**
3629
- * Triggered after code type has changed.
3630
- *
3631
- * @author Vova Feldman (@svovaf)
3632
- * @since 1.1.9.1
3633
- */
3634
- function _after_code_type_change() {
3635
- $this->_logger->entrance();
3636
-
3637
- if ( $this->is_theme() ) {
3638
- // Expire the cache of the previous tabs since the theme may
3639
- // have setting updates after code type has changed.
3640
- $this->_cache->expire( 'tabs' );
3641
- $this->_cache->expire( 'tabs_stylesheets' );
3642
- }
3643
-
3644
- if ( $this->is_registered() ) {
3645
- if ( ! $this->is_addon() ) {
3646
- add_action(
3647
- is_admin() ? 'admin_init' : 'init',
3648
- array( &$this, '_plugin_code_type_changed' )
3649
- );
3650
- }
3651
-
3652
- if ( $this->is_premium() ) {
3653
- // Purge cached payments after switching to the premium version.
3654
- // @todo This logic doesn't handle purging the cache for serviceware module upgrade.
3655
- $this->get_api_user_scope()->purge_cache( "/plugins/{$this->_module_id}/payments.json?include_addons=true" );
3656
- }
3657
- }
3658
- }
3659
-
3660
- /**
3661
- * Handles plugin's code type change (free <--> premium).
3662
- *
3663
- * @author Vova Feldman (@svovaf)
3664
- * @since 1.0.9
3665
- */
3666
- function _plugin_code_type_changed() {
3667
- $this->_logger->entrance();
3668
-
3669
- if ( $this->is_premium() ) {
3670
- $this->reconnect_locally();
3671
-
3672
- // Activated premium code.
3673
- $this->do_action( 'after_premium_version_activation' );
3674
-
3675
- // Remove all sticky messages related to download of the premium version.
3676
- $this->_admin_notices->remove_sticky( array(
3677
- 'trial_started',
3678
- 'plan_upgraded',
3679
- 'plan_changed',
3680
- 'license_activated',
3681
- ) );
3682
-
3683
- if ( ! $this->is_only_premium() ) {
3684
- $this->_admin_notices->add_sticky(
3685
- sprintf( $this->get_text_inline( 'Premium %s version was successfully activated.', 'premium-activated-message' ), $this->_module_type ),
3686
- 'premium_activated',
3687
- $this->get_text_x_inline( 'W00t',
3688
- 'Used to express elation, enthusiasm, or triumph (especially in electronic communication).', 'woot' ) . '!'
3689
- );
3690
- }
3691
- } else {
3692
- // Remove sticky message related to premium code activation.
3693
- $this->_admin_notices->remove_sticky( 'premium_activated' );
3694
-
3695
- // Activated free code (after had the premium before).
3696
- $this->do_action( 'after_free_version_reactivation' );
3697
-
3698
- if ( $this->is_paying() && ! $this->is_premium() ) {
3699
- $this->_admin_notices->add_sticky(
3700
- sprintf(
3701
- /* translators: %s: License type (e.g. you have a professional license) */
3702
- $this->get_text_inline( 'You have a %s license.', 'you-have-x-license' ),
3703
- $this->_site->plan->title
3704
- ) . $this->get_complete_upgrade_instructions(),
3705
- 'plan_upgraded',
3706
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
3707
- );
3708
- }
3709
- }
3710
-
3711
- // Schedule code type changes event.
3712
- $this->schedule_install_sync();
3713
-
3714
- /**
3715
- * Unregister the uninstall hook for the other version of the plugin (with different code type) to avoid
3716
- * triggering a fatal error when uninstalling that plugin. For example, after deactivating the "free" version
3717
- * of a specific plugin, its uninstall hook should be unregistered after the "premium" version has been
3718
- * activated. If we don't do that, a fatal error will occur when we try to uninstall the "free" version since
3719
- * the main file of the "free" version will be loaded first before calling the hooked callback. Since the
3720
- * free and premium versions are almost identical (same class or have same functions), a fatal error like
3721
- * "Cannot redeclare class MyClass" or "Cannot redeclare my_function()" will occur.
3722
- */
3723
- $this->unregister_uninstall_hook();
3724
-
3725
- $this->clear_module_main_file_cache();
3726
-
3727
- // Update is_premium of latest version.
3728
- $this->_storage->prev_is_premium = $this->_plugin->is_premium;
3729
- }
3730
-
3731
- #endregion
3732
-
3733
- #----------------------------------------------------------------------------------
3734
- #region Add-ons
3735
- #----------------------------------------------------------------------------------
3736
-
3737
- /**
3738
- * Check if add-on installed and activated on site.
3739
- *
3740
- * @author Vova Feldman (@svovaf)
3741
- * @since 1.0.6
3742
- *
3743
- * @param string|number $id_or_slug
3744
- * @param bool|null $is_premium Since 1.2.1.7 can check for specified add-on version.
3745
- *
3746
- * @return bool
3747
- */
3748
- function is_addon_activated( $id_or_slug, $is_premium = null ) {
3749
- $this->_logger->entrance();
3750
-
3751
- $addon_id = self::get_module_id( $id_or_slug );
3752
- $is_activated = self::has_instance( $addon_id );
3753
-
3754
- if ( ! $is_activated ) {
3755
- return false;
3756
- }
3757
-
3758
- if ( is_bool( $is_premium ) ) {
3759
- // Check if the specified code version is activate.
3760
- $addon = $this->get_addon_instance( $addon_id );
3761
- $is_activated = ( $is_premium === $addon->is_premium() );
3762
- }
3763
-
3764
- return $is_activated;
3765
- }
3766
-
3767
- /**
3768
- * Check if add-on was connected to install
3769
- *
3770
- * @author Vova Feldman (@svovaf)
3771
- * @since 1.1.7
3772
- *
3773
- * @param string|number $id_or_slug
3774
- *
3775
- * @return bool
3776
- */
3777
- function is_addon_connected( $id_or_slug ) {
3778
- $this->_logger->entrance();
3779
-
3780
- $sites = self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN );
3781
-
3782
- $addon_id = self::get_module_id( $id_or_slug );
3783
- $addon = $this->get_addon( $addon_id );
3784
- $slug = $addon->slug;
3785
- if ( ! isset( $sites[ $slug ] ) ) {
3786
- return false;
3787
- }
3788
-
3789
- $site = $sites[ $slug ];
3790
-
3791
- $plugin = FS_Plugin_Manager::instance( $addon_id )->get();
3792
-
3793
- if ( $plugin->parent_plugin_id != $this->_plugin->id ) {
3794
- // The given slug do NOT belong to any of the plugin's add-ons.
3795
- return false;
3796
- }
3797
-
3798
- return ( is_object( $site ) &&
3799
- is_numeric( $site->id ) &&
3800
- is_numeric( $site->user_id ) &&
3801
- is_object( $site->plan )
3802
- );
3803
- }
3804
-
3805
- /**
3806
- * Determines if add-on installed.
3807
- *
3808
- * NOTE: This is a heuristic and only works if the folder/file named as the slug.
3809
- *
3810
- * @author Vova Feldman (@svovaf)
3811
- * @since 1.0.6
3812
- *
3813
- * @param string|number $id_or_slug
3814
- *
3815
- * @return bool
3816
- */
3817
- function is_addon_installed( $id_or_slug ) {
3818
- $this->_logger->entrance();
3819
-
3820
- $addon_id = self::get_module_id( $id_or_slug );
3821
-
3822
- return file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $this->get_addon_basename( $addon_id ) ) );
3823
- }
3824
-
3825
- /**
3826
- * Get add-on basename.
3827
- *
3828
- * @author Vova Feldman (@svovaf)
3829
- * @since 1.0.6
3830
- *
3831
- * @param string|number $id_or_slug
3832
- *
3833
- * @return string
3834
- */
3835
- function get_addon_basename( $id_or_slug ) {
3836
- $addon_id = self::get_module_id( $id_or_slug );
3837
-
3838
- if ( $this->is_addon_activated( $addon_id ) ) {
3839
- return self::instance( $addon_id )->get_plugin_basename();
3840
- }
3841
-
3842
- $addon = $this->get_addon( $addon_id );
3843
- $premium_basename = "{$addon->slug}-premium/{$addon->slug}.php";
3844
-
3845
- if ( file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $premium_basename ) ) ) {
3846
- return $premium_basename;
3847
- }
3848
-
3849
- $all_plugins = $this->get_all_plugins();
3850
-
3851
- foreach ( $all_plugins as $basename => &$data ) {
3852
- if ( $addon->slug === $data['slug'] ||
3853
- $addon->slug . '-premium' === $data['slug']
3854
- ) {
3855
- return $basename;
3856
- }
3857
- }
3858
-
3859
- $free_basename = "{$addon->slug}/{$addon->slug}.php";
3860
-
3861
- return $free_basename;
3862
- }
3863
-
3864
- /**
3865
- * Get installed add-ons instances.
3866
- *
3867
- * @author Vova Feldman (@svovaf)
3868
- * @since 1.0.6
3869
- *
3870
- * @return Freemius[]
3871
- */
3872
- function get_installed_addons() {
3873
- $installed_addons = array();
3874
- foreach ( self::$_instances as $instance ) {
3875
- if ( $instance->is_addon() && is_object( $instance->_parent_plugin ) ) {
3876
- if ( $this->_plugin->id == $instance->_parent_plugin->id ) {
3877
- $installed_addons[] = $instance;
3878
- }
3879
- }
3880
- }
3881
-
3882
- return $installed_addons;
3883
- }
3884
-
3885
- /**
3886
- * Check if any add-ons of the plugin are installed.
3887
- *
3888
- * @author Leo Fajardo (@leorw)
3889
- * @since 1.1.1
3890
- *
3891
- * @return bool
3892
- */
3893
- function has_installed_addons() {
3894
- if ( ! $this->has_addons() ) {
3895
- return false;
3896
- }
3897
-
3898
- foreach ( self::$_instances as $instance ) {
3899
- if ( $instance->is_addon() && is_object( $instance->_parent_plugin ) ) {
3900
- if ( $this->_plugin->id == $instance->_parent_plugin->id ) {
3901
- return true;
3902
- }
3903
- }
3904
- }
3905
-
3906
- return false;
3907
- }
3908
-
3909
- /**
3910
- * Tell Freemius that the current plugin is an add-on.
3911
- *
3912
- * @author Vova Feldman (@svovaf)
3913
- * @since 1.0.6
3914
- *
3915
- * @param number $parent_plugin_id The parent plugin ID
3916
- */
3917
- function init_addon( $parent_plugin_id ) {
3918
- $this->_plugin->parent_plugin_id = $parent_plugin_id;
3919
- }
3920
-
3921
- /**
3922
- * @author Vova Feldman (@svovaf)
3923
- * @since 1.0.6
3924
- *
3925
- * @return bool
3926
- */
3927
- function is_addon() {
3928
- return isset( $this->_plugin->parent_plugin_id ) && is_numeric( $this->_plugin->parent_plugin_id );
3929
- }
3930
-
3931
- /**
3932
- * Deactivate add-on if it's premium only and the user does't have a valid license.
3933
- *
3934
- * @param bool $is_after_trial_cancel
3935
- *
3936
- * @return bool If add-on was deactivated.
3937
- */
3938
- private function deactivate_premium_only_addon_without_license( $is_after_trial_cancel = false ) {
3939
- if ( ! $this->has_free_plan() &&
3940
- ! $this->has_features_enabled_license() &&
3941
- ! $this->_has_premium_license()
3942
- ) {
3943
- if ( $this->is_registered() ) {
3944
- // IF wrapper is turned off because activation_timestamp is currently only stored for plugins (not addons).
3945
- // if (empty($this->_storage->activation_timestamp) ||
3946
- // (WP_FS__SCRIPT_START_TIME - $this->_storage->activation_timestamp) > 30
3947
- // ) {
3948
- /**
3949
- * @todo When it's first fail, there's no reason to try and re-sync because the licenses were just synced after initial activation.
3950
- *
3951
- * Retry syncing the user add-on licenses.
3952
- */
3953
- // Sync licenses.
3954
- $this->_sync_licenses();
3955
- // }
3956
-
3957
- // Try to activate premium license.
3958
- $this->_activate_license( true );
3959
- }
3960
-
3961
- if ( ! $this->has_free_plan() &&
3962
- ! $this->has_features_enabled_license() &&
3963
- ! $this->_has_premium_license()
3964
- ) {
3965
- // @todo Check if deactivate plugins also call the deactivation hook.
3966
-
3967
- $this->_parent->_admin_notices->add_sticky(
3968
- sprintf(
3969
- ($is_after_trial_cancel ?
3970
- $this->_parent->get_text_inline(
3971
- '%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.',
3972
- 'addon-trial-cancelled-message'
3973
- ) :
3974
- $this->_parent->get_text_inline(
3975
- '%s is a premium only add-on. You have to purchase a license first before activating the plugin.',
3976
- 'addon-no-license-message'
3977
- )
3978
- ),
3979
- '<b>' . $this->_plugin->title . '</b>'
3980
- ) . ' ' . sprintf(
3981
- '<a href="%s" aria-label="%s" class="button button-primary" style="margin-left: 10px; vertical-align: middle;">%s &nbsp;&#10140;</a>',
3982
- $this->_parent->addon_url( $this->_slug ),
3983
- esc_attr( sprintf( $this->_parent->get_text_inline( 'More information about %s', 'more-information-about-x' ), $this->_plugin->title ) ),
3984
- $this->_parent->get_text_inline( 'Purchase License', 'purchase-license' )
3985
- ),
3986
- 'no_addon_license_' . $this->_slug,
3987
- ( $is_after_trial_cancel ? '' : $this->_parent->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...' ),
3988
- ( $is_after_trial_cancel ? 'success' : 'error' )
3989
- );
3990
-
3991
- deactivate_plugins( array( $this->_plugin_basename ), true );
3992
-
3993
- return true;
3994
- }
3995
- }
3996
-
3997
- return false;
3998
- }
3999
-
4000
- #endregion
4001
-
4002
- #----------------------------------------------------------------------------------
4003
- #region Sandbox
4004
- #----------------------------------------------------------------------------------
4005
-
4006
- /**
4007
- * Set Freemius into sandbox mode for debugging.
4008
- *
4009
- * @author Vova Feldman (@svovaf)
4010
- * @since 1.0.4
4011
- *
4012
- * @param string $secret_key
4013
- */
4014
- function init_sandbox( $secret_key ) {
4015
- $this->_plugin->secret_key = $secret_key;
4016
-
4017
- // Update plugin details.
4018
- FS_Plugin_Manager::instance( $this->_module_id )->update( $this->_plugin, true );
4019
- }
4020
-
4021
- /**
4022
- * Check if running payments in sandbox mode.
4023
- *
4024
- * @author Vova Feldman (@svovaf)
4025
- * @since 1.0.4
4026
- *
4027
- * @return bool
4028
- */
4029
- function is_payments_sandbox() {
4030
- return ( ! $this->is_live() ) || isset( $this->_plugin->secret_key );
4031
- }
4032
-
4033
- #endregion
4034
-
4035
- /**
4036
- * Check if running test vs. live plugin.
4037
- *
4038
- * @author Vova Feldman (@svovaf)
4039
- * @since 1.0.5
4040
- *
4041
- * @return bool
4042
- */
4043
- function is_live() {
4044
- return $this->_plugin->is_live;
4045
- }
4046
-
4047
- /**
4048
- * Check if the user skipped connecting the account with Freemius.
4049
- *
4050
- * @author Vova Feldman (@svovaf)
4051
- * @since 1.0.7
4052
- *
4053
- * @return bool
4054
- */
4055
- function is_anonymous() {
4056
- if ( ! isset( $this->_is_anonymous ) ) {
4057
- if ( ! isset( $this->_storage->is_anonymous ) ) {
4058
- // Not skipped.
4059
- $this->_is_anonymous = false;
4060
- } else if ( is_bool( $this->_storage->is_anonymous ) ) {
4061
- // For back compatibility, since the variable was boolean before.
4062
- $this->_is_anonymous = $this->_storage->is_anonymous;
4063
-
4064
- // Upgrade stored data format to 1.1.3 format.
4065
- $this->set_anonymous_mode( $this->_storage->is_anonymous );
4066
- } else {
4067
- // Version 1.1.3 and later.
4068
- $this->_is_anonymous = $this->_storage->is_anonymous['is'];
4069
- }
4070
- }
4071
-
4072
- return $this->_is_anonymous;
4073
- }
4074
-
4075
- /**
4076
- * Check if user connected his account and install pending email activation.
4077
- *
4078
- * @author Vova Feldman (@svovaf)
4079
- * @since 1.0.7
4080
- *
4081
- * @return bool
4082
- */
4083
- function is_pending_activation() {
4084
- return $this->_storage->get( 'is_pending_activation', false );
4085
- }
4086
-
4087
- /**
4088
- * Check if plugin must be WordPress.org compliant.
4089
- *
4090
- * @since 1.0.7
4091
- *
4092
- * @return bool
4093
- */
4094
- function is_org_repo_compliant() {
4095
- return $this->_is_org_compliant;
4096
- }
4097
-
4098
- #----------------------------------------------------------------------------------
4099
- #region Daily Sync Cron
4100
- #----------------------------------------------------------------------------------
4101
-
4102
- /**
4103
- * @author Vova Feldman (@svovaf)
4104
- * @since 1.1.7.3
4105
- */
4106
- private function run_manual_sync() {
4107
- self::require_pluggable_essentials();
4108
-
4109
- if ( ! $this->is_user_admin() ) {
4110
- return;
4111
- }
4112
-
4113
- // Run manual sync.
4114
- $this->_sync_cron();
4115
-
4116
- // Reschedule next cron to run 24 hours from now (performance optimization).
4117
- $this->clear_sync_cron();
4118
-
4119
- $this->schedule_sync_cron( time() + WP_FS__TIME_24_HOURS_IN_SEC, false );
4120
- }
4121
-
4122
- /**
4123
- * Data sync cron job. Replaces the background sync non blocking HTTP request
4124
- * that doesn't halt page loading.
4125
- *
4126
- * @author Vova Feldman (@svovaf)
4127
- * @since 1.1.7.3
4128
- */
4129
- function _sync_cron() {
4130
- $this->_logger->entrance();
4131
-
4132
- // Store the last time data sync was executed.
4133
- $this->_storage->sync_timestamp = time();
4134
-
4135
- // Check if API is temporary down.
4136
- if ( FS_Api::is_temporary_down() ) {
4137
- return;
4138
- }
4139
-
4140
- // @todo Add logic that identifies API latency, and reschedule the next background sync randomly between 8-16 hours.
4141
-
4142
- if ( $this->is_registered() ) {
4143
- if ( $this->has_paid_plan() ) {
4144
- // Initiate background plan sync.
4145
- $this->_sync_license( true );
4146
-
4147
- if ( $this->is_paying() ) {
4148
- // Check for premium plugin updates.
4149
- $this->check_updates( true );
4150
- }
4151
- } else {
4152
- // Sync install (only if something changed locally).
4153
- $this->sync_install();
4154
- }
4155
- }
4156
-
4157
- $this->do_action( 'after_sync_cron' );
4158
- }
4159
-
4160
- /**
4161
- * Check if sync was executed in the last $period of seconds.
4162
- *
4163
- * @author Vova Feldman (@svovaf)
4164
- * @since 1.1.7.3
4165
- *
4166
- * @param int $period In seconds
4167
- *
4168
- * @return bool
4169
- */
4170
- private function is_sync_executed( $period = WP_FS__TIME_24_HOURS_IN_SEC ) {
4171
- if ( ! isset( $this->_storage->sync_timestamp ) ) {
4172
- return false;
4173
- }
4174
-
4175
- return ( $this->_storage->sync_timestamp > ( WP_FS__SCRIPT_START_TIME - $period ) );
4176
- }
4177
-
4178
- /**
4179
- * @author Vova Feldman (@svovaf)
4180
- * @since 1.1.7.3
4181
- *
4182
- * @return bool
4183
- */
4184
- private function is_sync_cron_on() {
4185
- /**
4186
- * @var object $sync_cron_data
4187
- */
4188
- $sync_cron_data = $this->_storage->get( 'sync_cron', null );
4189
-
4190
- return ( ! is_null( $sync_cron_data ) && true === $sync_cron_data->on );
4191
- }
4192
-
4193
- /**
4194
- * @author Vova Feldman (@svovaf)
4195
- * @since 1.1.7.3
4196
- *
4197
- * @param int $start_at Defaults to now.
4198
- * @param bool $randomize_start If true, schedule first job randomly during the next 12 hours. Otherwise,
4199
- * schedule job to start right away.
4200
- */
4201
- private function schedule_sync_cron( $start_at = WP_FS__SCRIPT_START_TIME, $randomize_start = true ) {
4202
- $this->_logger->entrance();
4203
-
4204
- if ( $randomize_start ) {
4205
- // Schedule first sync with a random 12 hour time range from now.
4206
- $start_at += rand( 0, ( WP_FS__TIME_24_HOURS_IN_SEC / 2 ) );
4207
- }
4208
-
4209
- // Schedule daily WP cron.
4210
- wp_schedule_event(
4211
- $start_at,
4212
- 'daily',
4213
- $this->get_action_tag( 'data_sync' )
4214
- );
4215
-
4216
- $this->_storage->store( 'sync_cron', (object) array(
4217
- 'version' => $this->get_plugin_version(),
4218
- 'sdk_version' => $this->version,
4219
- 'timestamp' => WP_FS__SCRIPT_START_TIME,
4220
- 'on' => true,
4221
- ) );
4222
- }
4223
-
4224
- /**
4225
- * Add the actual sync function to the cron job hook.
4226
- *
4227
- * @author Vova Feldman (@svovaf)
4228
- * @since 1.1.7.3
4229
- */
4230
- private function hook_callback_to_sync_cron() {
4231
- $this->add_action( 'data_sync', array( &$this, '_sync_cron' ) );
4232
- }
4233
-
4234
- /**
4235
- * @author Vova Feldman (@svovaf)
4236
- * @since 1.1.7.3
4237
- */
4238
- private function clear_sync_cron() {
4239
- $this->_logger->entrance();
4240
-
4241
- if ( ! $this->is_sync_cron_on() ) {
4242
- return;
4243
- }
4244
-
4245
- $this->_storage->remove( 'sync_cron' );
4246
-
4247
- wp_clear_scheduled_hook( $this->get_action_tag( 'data_sync' ) );
4248
- }
4249
-
4250
- /**
4251
- * Unix timestamp for next sync cron execution or false if not scheduled.
4252
- *
4253
- * @author Vova Feldman (@svovaf)
4254
- * @since 1.1.7.3
4255
- *
4256
- * @return int|false
4257
- */
4258
- function next_sync_cron() {
4259
- $this->_logger->entrance();
4260
-
4261
- if ( ! $this->is_sync_cron_on() ) {
4262
- return false;
4263
- }
4264
-
4265
- return wp_next_scheduled( $this->get_action_tag( 'data_sync' ) );
4266
- }
4267
-
4268
- /**
4269
- * Unix timestamp for previous sync cron execution or false if never executed.
4270
- *
4271
- * @author Vova Feldman (@svovaf)
4272
- * @since 1.1.7.3
4273
- *
4274
- * @return int|false
4275
- */
4276
- function last_sync_cron() {
4277
- $this->_logger->entrance();
4278
-
4279
- return $this->_storage->get( 'sync_timestamp' );
4280
- }
4281
-
4282
- #endregion Daily Sync Cron ------------------------------------------------------------------
4283
-
4284
- #----------------------------------------------------------------------------------
4285
- #region Async Install Sync
4286
- #----------------------------------------------------------------------------------
4287
-
4288
- /**
4289
- * @author Vova Feldman (@svovaf)
4290
- * @since 1.1.7.3
4291
- *
4292
- * @return bool
4293
- */
4294
- private function is_install_sync_scheduled() {
4295
- /**
4296
- * @var object $cron_data
4297
- */
4298
- $cron_data = $this->_storage->get( 'install_sync_cron', null );
4299
-
4300
- return ( ! is_null( $cron_data ) && true === $cron_data->on );
4301
- }
4302
-
4303
- /**
4304
- * Instead of running blocking install sync event, execute non blocking scheduled wp-cron.
4305
- *
4306
- * @author Vova Feldman (@svovaf)
4307
- * @since 1.1.7.3
4308
- */
4309
- private function schedule_install_sync() {
4310
- $this->_logger->entrance();
4311
-
4312
- $this->clear_install_sync_cron();
4313
-
4314
- // Schedule immediate install sync.
4315
- wp_schedule_single_event(
4316
- WP_FS__SCRIPT_START_TIME,
4317
- $this->get_action_tag( 'install_sync' )
4318
- );
4319
-
4320
- $this->_storage->store( 'install_sync_cron', (object) array(
4321
- 'version' => $this->get_plugin_version(),
4322
- 'sdk_version' => $this->version,
4323
- 'timestamp' => WP_FS__SCRIPT_START_TIME,
4324
- 'on' => true,
4325
- ) );
4326
- }
4327
-
4328
- /**
4329
- * Unix timestamp for previous install sync cron execution or false if never executed.
4330
- *
4331
- * @todo There's some very strange bug that $this->_storage->install_sync_timestamp value is not being
4332
- * updated. But for sure the sync event is working.
4333
- *
4334
- * @author Vova Feldman (@svovaf)
4335
- * @since 1.1.7.3
4336
- *
4337
- * @return int|false
4338
- */
4339
- function last_install_sync() {
4340
- $this->_logger->entrance();
4341
-
4342
- return $this->_storage->get( 'install_sync_timestamp' );
4343
- }
4344
-
4345
- /**
4346
- * Unix timestamp for next install sync cron execution or false if not scheduled.
4347
- *
4348
- * @author Vova Feldman (@svovaf)
4349
- * @since 1.1.7.3
4350
- *
4351
- * @return int|false
4352
- */
4353
- function next_install_sync() {
4354
- $this->_logger->entrance();
4355
-
4356
- if ( ! $this->is_install_sync_scheduled() ) {
4357
- return false;
4358
- }
4359
-
4360
- return wp_next_scheduled( $this->get_action_tag( 'install_sync' ) );
4361
- }
4362
-
4363
- /**
4364
- * Add the actual install sync function to the cron job hook.
4365
- *
4366
- * @author Vova Feldman (@svovaf)
4367
- * @since 1.1.7.3
4368
- */
4369
- private function hook_callback_to_install_sync() {
4370
- $this->add_action( 'install_sync', array( &$this, '_run_sync_install' ) );
4371
- }
4372
-
4373
- /**
4374
- * @author Vova Feldman (@svovaf)
4375
- * @since 1.1.7.3
4376
- */
4377
- private function clear_install_sync_cron() {
4378
- $this->_logger->entrance();
4379
-
4380
- if ( ! $this->is_install_sync_scheduled() ) {
4381
- return;
4382
- }
4383
-
4384
- $this->_storage->remove( 'install_sync_cron' );
4385
-
4386
- wp_clear_scheduled_hook( $this->get_action_tag( 'install_sync' ) );
4387
- }
4388
-
4389
- /**
4390
- * @author Vova Feldman (@svovaf)
4391
- * @since 1.1.7.3
4392
- */
4393
- public function _run_sync_install() {
4394
- $this->_logger->entrance();
4395
-
4396
- // Update last install sync timestamp.
4397
- $this->_storage->install_sync_timestamp = time();
4398
-
4399
- $this->sync_install( array(), true );
4400
- }
4401
-
4402
- #endregion Async Install Sync ------------------------------------------------------------------
4403
-
4404
- /**
4405
- * Show a notice that activation is currently pending.
4406
- *
4407
- * @author Vova Feldman (@svovaf)
4408
- * @since 1.0.7
4409
- *
4410
- * @param bool|string $email
4411
- * @param bool $is_pending_trial Since 1.2.1.5
4412
- */
4413
- function _add_pending_activation_notice( $email = false, $is_pending_trial = false ) {
4414
- if ( ! is_string( $email ) ) {
4415
- $current_user = self::_get_current_wp_user();
4416
- $email = $current_user->user_email;
4417
- }
4418
-
4419
- $this->_admin_notices->add_sticky(
4420
- sprintf(
4421
- $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' ),
4422
- '<b>' . $this->get_plugin_name() . '</b>',
4423
- '<b>' . $email . '</b>',
4424
- ( $is_pending_trial ?
4425
- $this->get_text_inline( 'start the trial', 'start-the-trial' ) :
4426
- $this->get_text_inline( 'complete the install', 'complete-the-install' ) )
4427
- ),
4428
- 'activation_pending',
4429
- 'Thanks!'
4430
- );
4431
- }
4432
-
4433
- /**
4434
- * Check if currently in plugin activation.
4435
- *
4436
- * @author Vova Feldman (@svovaf)
4437
- * @since 1.1.4
4438
- *
4439
- * @return bool
4440
- */
4441
- function is_plugin_activation() {
4442
- return get_option( 'fs_'
4443
- . ( $this->is_plugin() ? '' : $this->_module_type . '_' )
4444
- . "{$this->_slug}_activated", false );
4445
- }
4446
-
4447
- /**
4448
- *
4449
- * NOTE: admin_menu action executed before admin_init.
4450
- *
4451
- * @author Vova Feldman (@svovaf)
4452
- * @since 1.0.7
4453
- */
4454
- function _admin_init_action() {
4455
- /**
4456
- * Automatically redirect to connect/activation page after plugin activation.
4457
- *
4458
- * @since 1.1.7 Do NOT redirect to opt-in when running in network admin mode.
4459
- */
4460
- if ( $this->is_plugin_activation() ) {
4461
- delete_option( 'fs_'
4462
- . ( $this->is_plugin() ? '' : $this->_module_type . '_' )
4463
- . "{$this->_slug}_activated" );
4464
-
4465
- if ( ! function_exists( 'is_network_admin' ) || ! is_network_admin() ) {
4466
- $this->_redirect_on_activation_hook();
4467
-
4468
- return;
4469
- }
4470
- }
4471
-
4472
- if ( fs_request_is_action( $this->get_unique_affix() . '_skip_activation' ) ) {
4473
- check_admin_referer( $this->get_unique_affix() . '_skip_activation' );
4474
-
4475
- $this->skip_connection();
4476
-
4477
- fs_redirect( $this->get_after_activation_url( 'after_skip_url' ) );
4478
- }
4479
-
4480
- if ( ! $this->is_addon() && ! $this->is_registered() && ! $this->is_anonymous() ) {
4481
- if ( ! $this->is_pending_activation() ) {
4482
- if ( ! $this->_menu->is_main_settings_page() ) {
4483
- /**
4484
- * If a user visits any other admin page before activating the premium-only theme with a valid
4485
- * license, reactivate the previous theme.
4486
- *
4487
- * @author Leo Fajardo (@leorw)
4488
- * @since 1.2.2
4489
- */
4490
- if ( $this->is_theme()
4491
- && $this->is_only_premium()
4492
- && ! $this->has_settings_menu()
4493
- && ! isset( $_REQUEST['fs_action'] )
4494
- && $this->can_activate_previous_theme()
4495
- ) {
4496
- $this->activate_previous_theme();
4497
-
4498
- return;
4499
- }
4500
-
4501
- if ( $this->is_plugin_new_install() || $this->is_only_premium() ) {
4502
- // Show notice for new plugin installations.
4503
- $this->_admin_notices->add(
4504
- sprintf(
4505
- $this->get_text_inline( 'You are just one step away - %s', 'you-are-step-away' ),
4506
- sprintf( '<b><a href="%s">%s</a></b>',
4507
- $this->get_activation_url(),
4508
- sprintf( $this->get_text_x_inline( 'Complete "%s" Activation Now',
4509
- '%s - plugin name. As complete "PluginX" activation now', 'activate-x-now' ), $this->get_plugin_name() )
4510
- )
4511
- ),
4512
- '',
4513
- 'update-nag'
4514
- );
4515
- } else {
4516
- if ( ! isset( $this->_storage->sticky_optin_added ) ) {
4517
- $this->_storage->sticky_optin_added = true;
4518
-
4519
- // Show notice for new plugin installations.
4520
- $this->_admin_notices->add_sticky(
4521
- sprintf(
4522
- $this->get_text_inline( 'We made a few tweaks to the %s, %s', 'few-plugin-tweaks' ),
4523
- $this->_module_type,
4524
- sprintf( '<b><a href="%s">%s</a></b>',
4525
- $this->get_activation_url(),
4526
- sprintf( $this->get_text_inline( 'Opt in to make "%s" Better!', 'optin-x-now' ), $this->get_plugin_name() )
4527
- )
4528
- ),
4529
- 'connect_account',
4530
- '',
4531
- 'update-nag'
4532
- );
4533
- }
4534
-
4535
- if ( $this->has_filter( 'optin_pointer_element' ) ) {
4536
- // Don't show admin nag if plugin update.
4537
- wp_enqueue_script( 'wp-pointer' );
4538
- wp_enqueue_style( 'wp-pointer' );
4539
-
4540
- $this->_enqueue_connect_essentials();
4541
-
4542
- add_action( 'admin_print_footer_scripts', array(
4543
- $this,
4544
- '_add_connect_pointer_script'
4545
- ) );
4546
- }
4547
- }
4548
- }
4549
- }
4550
-
4551
- if ( $this->is_theme() &&
4552
- $this->_menu->is_main_settings_page()
4553
- ) {
4554
- $this->_show_theme_activation_optin_dialog();
4555
- }
4556
- }
4557
-
4558
- $this->_add_upgrade_action_link();
4559
- }
4560
-
4561
- /**
4562
- * Enqueue connect requires scripts and styles.
4563
- *
4564
- * @author Vova Feldman (@svovaf)
4565
- * @since 1.1.4
4566
- */
4567
- function _enqueue_connect_essentials() {
4568
- wp_enqueue_script( 'jquery' );
4569
- wp_enqueue_script( 'json2' );
4570
-
4571
- fs_enqueue_local_script( 'postmessage', 'nojquery.ba-postmessage.min.js' );
4572
- fs_enqueue_local_script( 'fs-postmessage', 'postmessage.js' );
4573
-
4574
- fs_enqueue_local_style( 'fs_connect', '/admin/connect.css' );
4575
- }
4576
-
4577
- /**
4578
- * Add connect / opt-in pointer.
4579
- *
4580
- * @author Vova Feldman (@svovaf)
4581
- * @since 1.1.4
4582
- */
4583
- function _add_connect_pointer_script() {
4584
- $vars = array( 'id' => $this->_module_id );
4585
- $pointer_content = fs_get_template( 'connect.php', $vars );
4586
- ?>
4587
- <script type="text/javascript">// <![CDATA[
4588
- jQuery(document).ready(function ($) {
4589
- if ('undefined' !== typeof(jQuery().pointer)) {
4590
-
4591
- var element = <?php echo $this->apply_filters( 'optin_pointer_element', '$("#non_existing_element");' ) ?>;
4592
-
4593
- if (element.length > 0) {
4594
- var optin = $(element).pointer($.extend(true, {}, {
4595
- content : <?php echo json_encode( $pointer_content ) ?>,
4596
- position : {
4597
- edge : 'left',
4598
- align: 'center'
4599
- },
4600
- buttons : function () {
4601
- // Don't show pointer buttons.
4602
- return '';
4603
- },
4604
- pointerWidth: 482
4605
- }, <?php echo $this->apply_filters( 'optin_pointer_options_json', '{}' ) ?>));
4606
-
4607
- <?php
4608
- echo $this->apply_filters( 'optin_pointer_execute', "
4609
 
4610
- optin.pointer('open');
 
 
 
 
 
 
 
 
 
 
 
 
4611
 
4612
- // Tag the opt-in pointer with custom class.
4613
- $('.wp-pointer #fs_connect')
4614
- .parents('.wp-pointer.wp-pointer-top')
4615
- .addClass('fs-opt-in-pointer');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4616
 
4617
- ", 'element', 'optin' ) ?>
4618
- }
4619
- }
4620
- });
4621
- // ]]></script>
4622
- <?php
4623
- }
4624
-
4625
- /**
4626
- * Return current page's URL.
4627
- *
4628
- * @author Vova Feldman (@svovaf)
4629
- * @since 1.0.7
4630
- *
4631
- * @return string
4632
- */
4633
- function current_page_url() {
4634
- $url = 'http';
4635
-
4636
- if ( isset( $_SERVER["HTTPS"] ) ) {
4637
- if ( $_SERVER["HTTPS"] == "on" ) {
4638
- $url .= "s";
4639
- }
4640
- }
4641
- $url .= "://";
4642
- if ( $_SERVER["SERVER_PORT"] != "80" ) {
4643
- $url .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
4644
- } else {
4645
- $url .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
4646
- }
4647
-
4648
- return esc_url( $url );
4649
- }
4650
-
4651
- /**
4652
- * Check if the current page is the plugin's main admin settings page.
4653
- *
4654
- * @author Vova Feldman (@svovaf)
4655
- * @since 1.0.7
4656
- *
4657
- * @return bool
4658
- */
4659
- function _is_plugin_page() {
4660
- return fs_is_plugin_page( $this->_menu->get_raw_slug() ) ||
4661
- fs_is_plugin_page( $this->_slug );
4662
- }
4663
-
4664
- /* Events
4665
- ------------------------------------------------------------------------------------------------------------------*/
4666
- /**
4667
- * Delete site install from Database.
4668
- *
4669
- * @author Vova Feldman (@svovaf)
4670
- * @since 1.0.1
4671
- *
4672
- * @param bool $store
4673
- */
4674
- function _delete_site( $store = true ) {
4675
- self::_delete_site_by_slug( $this->_slug, $this->_module_type, $store );
4676
- }
4677
-
4678
- /**
4679
- * Delete site install from Database.
4680
- *
4681
- * @author Vova Feldman (@svovaf)
4682
- * @since 1.2.2.7
4683
- *
4684
- * @param string $slug
4685
- * @param string $module_type
4686
- * @param bool $store
4687
- */
4688
- static function _delete_site_by_slug($slug, $module_type, $store = true ) {
4689
- $sites = self::get_all_sites( $module_type );
4690
-
4691
- if ( isset( $sites[ $slug ] ) ) {
4692
- unset( $sites[ $slug ] );
4693
- }
4694
-
4695
- self::set_account_option_by_module( $module_type, 'sites', $sites, $store );
4696
- }
4697
-
4698
- /**
4699
- * Delete plugin's plans information.
4700
- *
4701
- * @param bool $store Flush to Database if true.
4702
- *
4703
- * @author Vova Feldman (@svovaf)
4704
- * @since 1.0.9
4705
- */
4706
- private function _delete_plans( $store = true ) {
4707
- $this->_logger->entrance();
4708
-
4709
- $plans = self::get_all_plans( $this->_module_type );
4710
-
4711
- unset( $plans[ $this->_slug ] );
4712
-
4713
- $this->set_account_option( 'plans', $plans, $store );
4714
- }
4715
-
4716
- /**
4717
- * Delete all plugin licenses.
4718
- *
4719
- * @author Vova Feldman (@svovaf)
4720
- * @since 1.0.9
4721
- *
4722
- * @param bool $store
4723
- * @param string|bool $plugin_slug
4724
- */
4725
- private function _delete_licenses( $store = true, $plugin_slug = false ) {
4726
- $this->_logger->entrance();
4727
-
4728
- $all_licenses = self::get_all_licenses( $this->_module_type );
4729
-
4730
- if ( ! is_string( $plugin_slug ) ) {
4731
- $plugin_slug = $this->_slug;
4732
- }
4733
-
4734
- unset( $all_licenses[ $plugin_slug ] );
4735
-
4736
- $this->set_account_option( 'licenses', $all_licenses, $store );
4737
- }
4738
-
4739
- /**
4740
- * Check if Freemius was added on new plugin installation.
4741
- *
4742
- * @author Vova Feldman (@svovaf)
4743
- * @since 1.1.5
4744
- *
4745
- * @return bool
4746
- */
4747
- function is_plugin_new_install() {
4748
- return isset( $this->_storage->is_plugin_new_install ) &&
4749
- $this->_storage->is_plugin_new_install;
4750
- }
4751
-
4752
- /**
4753
- * Check if it's the first plugin release that is running Freemius.
4754
- *
4755
- * @author Vova Feldman (@svovaf)
4756
- * @since 1.2.1.5
4757
- *
4758
- * @return bool
4759
- */
4760
- function is_first_freemius_powered_version() {
4761
- return empty( $this->_storage->plugin_last_version );
4762
- }
4763
-
4764
- /**
4765
- * @author Leo Fajardo (@leorw)
4766
- * @since 1.2.2
4767
- *
4768
- * @return bool|string
4769
- */
4770
- private function get_previous_theme_slug() {
4771
- return isset( $this->_storage->previous_theme ) ?
4772
- $this->_storage->previous_theme :
4773
- false;
4774
- }
4775
-
4776
- /**
4777
- * @author Leo Fajardo (@leorw)
4778
- * @since 1.2.2
4779
- *
4780
- * @return string
4781
- */
4782
- private function can_activate_previous_theme() {
4783
- $slug = $this->get_previous_theme_slug();
4784
- if ( false !== $slug && current_user_can( 'switch_themes' ) ) {
4785
- $theme_instance = wp_get_theme( $slug );
4786
-
4787
- return $theme_instance->exists();
4788
- }
4789
-
4790
- return false;
4791
- }
4792
-
4793
- /**
4794
- * @author Leo Fajardo (@leorw)
4795
- * @since 1.2.2
4796
- *
4797
- * @return string
4798
- */
4799
- private function activate_previous_theme() {
4800
- switch_theme( $this->get_previous_theme_slug() );
4801
- unset( $this->_storage->previous_theme );
4802
-
4803
- global $pagenow;
4804
- if ( 'themes.php' === $pagenow ) {
4805
- /**
4806
- * Refresh the active theme information.
4807
- *
4808
- * @author Leo Fajardo (@leorw)
4809
- * @since 1.2.2
4810
- */
4811
- fs_redirect( admin_url( $pagenow ) );
4812
- }
4813
- }
4814
-
4815
- /**
4816
- * @author Leo Fajardo (@leorw)
4817
- * @since 1.2.2
4818
- *
4819
- * @return string
4820
- */
4821
- function get_previous_theme_activation_url() {
4822
- if ( ! $this->can_activate_previous_theme() ) {
4823
- return '';
4824
- }
4825
-
4826
- /**
4827
- * Activation URL
4828
- *
4829
- * @author Leo Fajardo (@leorw)
4830
- * @since 1.2.2
4831
- */
4832
- return wp_nonce_url(
4833
- admin_url( 'themes.php?action=activate&stylesheet=' . urlencode( $this->get_previous_theme_slug() ) ),
4834
- 'switch-theme_' . $this->get_previous_theme_slug()
4835
- );
4836
- }
4837
-
4838
- /**
4839
- * Saves the slug of the previous theme if it still exists so that it can be used by the logic in the opt-in
4840
- * form that decides whether to add a close button to the opt-in dialog or not. So after a premium-only theme is
4841
- * activated, the close button will appear and will reactivate the previous theme if clicked. If the previous
4842
- * theme doesn't exist, then there will be no close button.
4843
- *
4844
- * @author Leo Fajardo (@leorw)
4845
- * @since 1.2.2
4846
- *
4847
- * @param string $slug_or_name Old theme's slug or name.
4848
- * @param bool|WP_Theme $old_theme WP_Theme instance of the old theme if it still exists.
4849
- */
4850
- function _activate_theme_event_hook( $slug_or_name, $old_theme = false ) {
4851
- $this->_storage->previous_theme = ( false !== $old_theme ) ?
4852
- $old_theme->get_stylesheet() :
4853
- $slug_or_name;
4854
-
4855
- $this->_activate_plugin_event_hook();
4856
- }
4857
-
4858
- /**
4859
- * Plugin activated hook.
4860
- *
4861
- * @author Vova Feldman (@svovaf)
4862
- * @since 1.0.1
4863
- *
4864
- * @uses FS_Api
4865
- */
4866
- function _activate_plugin_event_hook() {
4867
- $this->_logger->entrance( 'slug = ' . $this->_slug );
4868
-
4869
- if ( ! $this->is_user_admin() ) {
4870
- return;
4871
- }
4872
-
4873
- $this->unregister_uninstall_hook();
4874
-
4875
- // Clear API cache on activation.
4876
- FS_Api::clear_cache();
4877
-
4878
- $is_premium_version_activation = ( current_filter() !== ( 'activate_' . $this->_free_plugin_basename ) );
4879
-
4880
- $this->_logger->info( 'Activating ' . ( $is_premium_version_activation ? 'premium' : 'free' ) . ' plugin version.' );
4881
-
4882
- // 1. If running in the activation of the FREE module, get the basename of the PREMIUM.
4883
- // 2. If running in the activation of the PREMIUM module, get the basename of the FREE.
4884
- $other_version_basename = $is_premium_version_activation ?
4885
- $this->_free_plugin_basename :
4886
- $this->premium_plugin_basename();
4887
-
4888
- /**
4889
- * If the other module version is activate, deactivate it.
4890
- *
4891
- * @author Leo Fajardo (@leorw)
4892
- * @since 1.2.2
4893
- */
4894
- if ( is_plugin_active( $other_version_basename ) ) {
4895
- deactivate_plugins( $other_version_basename );
4896
- }
4897
 
4898
- if ( $this->is_registered() ) {
4899
- if ( $is_premium_version_activation ) {
4900
- $this->reconnect_locally();
4901
- }
4902
 
 
 
 
 
 
 
 
4903
 
4904
- // Schedule re-activation event and sync.
4905
- // $this->sync_install( array(), true );
4906
- $this->schedule_install_sync();
4907
-
4908
- // If activating the premium module version, add an admin notice to congratulate for an upgrade completion.
4909
- if ( $is_premium_version_activation ) {
4910
- $this->_admin_notices->add(
4911
- sprintf( $this->get_text_inline( 'The upgrade of %s was successfully completed.', 'successful-version-upgrade-message' ), sprintf( '<b>%s</b>', $this->_plugin->title ) ),
4912
- $this->get_text_x_inline( 'W00t',
4913
- 'Used to express elation, enthusiasm, or triumph (especially in electronic communication).', 'woot' ) . '!'
4914
- );
4915
- }
4916
- } else if ( $this->is_anonymous() ) {
4917
- /**
4918
- * Reset "skipped" click cache on the following:
4919
- * 1. Freemius DEV mode.
4920
- * 2. WordPress DEBUG mode.
4921
- * 3. If a plugin and the user skipped the exact same version before.
4922
- *
4923
- * @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).
4924
- *
4925
- * @todo 4. If explicitly asked to retry after every activation.
4926
- */
4927
- if ( WP_FS__DEV_MODE ||
4928
- (
4929
- ( $this->is_plugin() || ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ) &&
4930
- $this->get_plugin_version() == $this->_storage->is_anonymous['version']
4931
- )
4932
- ) {
4933
- $this->reset_anonymous_mode();
4934
- }
4935
- }
4936
-
4937
- if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
4938
- /**
4939
- * If no previous version of plugin's version exist, it means that it's either
4940
- * the first time that the plugin installed on the site, or the plugin was installed
4941
- * before but didn't have Freemius integrated.
4942
- *
4943
- * Since register_activation_hook() do NOT fires on updates since 3.1, and only fires
4944
- * on manual activation via the dashboard, is_plugin_activation() is TRUE
4945
- * only after immediate activation.
4946
- *
4947
- * @since 1.1.4
4948
- * @link https://make.wordpress.org/core/2010/10/27/plugin-activation-hooks-no-longer-fire-for-updates/
4949
- */
4950
- $this->_storage->is_plugin_new_install = empty( $this->_storage->plugin_last_version );
4951
- }
4952
-
4953
- if ( ! $this->_anonymous_mode &&
4954
- $this->has_api_connectivity( WP_FS__DEV_MODE ) &&
4955
- ! $this->_isAutoInstall
4956
- ) {
4957
- // Store hint that the plugin was just activated to enable auto-redirection to settings.
4958
- add_option( 'fs_'
4959
- . ( $this->is_plugin() ? '' : $this->_module_type . '_' )
4960
- . "{$this->_slug}_activated", true );
4961
- }
4962
-
4963
- /**
4964
- * Activation hook is executed after the plugin's main file is loaded, therefore,
4965
- * after the plugin was loaded. The logic is located at activate_plugin()
4966
- * ./wp-admin/includes/plugin.php.
4967
- *
4968
- * @author Vova Feldman (@svovaf)
4969
- * @since 1.1.9
4970
- */
4971
- $this->_storage->was_plugin_loaded = true;
4972
- }
4973
 
4974
- /**
4975
- * Delete account.
4976
- *
4977
- * @author Vova Feldman (@svovaf)
4978
- * @since 1.0.3
4979
- *
4980
- * @param bool $check_user Enforce checking if user have plugins activation privileges.
4981
- */
4982
- function delete_account_event( $check_user = true ) {
4983
- $this->_logger->entrance( 'slug = ' . $this->_slug );
 
 
 
 
4984
 
4985
- if ( $check_user && ! $this->is_user_admin() ) {
4986
- return;
4987
- }
4988
 
4989
- $this->do_action( 'before_account_delete' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4990
 
4991
- // Clear all admin notices.
4992
- $this->_admin_notices->clear_all_sticky();
4993
 
4994
- $this->_delete_site( false );
 
4995
 
4996
- $this->_delete_plans( false );
 
 
 
 
4997
 
4998
- $this->_delete_licenses( false );
 
 
 
 
 
 
 
 
4999
 
5000
- // Delete add-ons related to plugin's account.
5001
- $this->_delete_account_addons( false );
 
 
5002
 
5003
- // @todo Delete plans and licenses of add-ons.
 
 
5004
 
5005
- self::$_accounts->store();
 
 
5006
 
5007
- /**
5008
- * IMPORTANT:
5009
- * Clear crons must be executed before clearing all storage.
5010
- * Otherwise, the cron will not be cleared.
5011
- */
5012
- $this->clear_sync_cron();
5013
- $this->clear_install_sync_cron();
5014
-
5015
- // Clear all storage data.
5016
- $this->_storage->clear_all( true, array(
5017
- 'connectivity_test',
5018
- 'is_on',
5019
- ) );
5020
-
5021
- // Send delete event.
5022
- $this->get_api_site_scope()->call( '/', 'delete' );
5023
-
5024
- $this->do_action( 'after_account_delete' );
5025
- }
5026
-
5027
- /**
5028
- * Plugin deactivation hook.
5029
- *
5030
- * @author Vova Feldman (@svovaf)
5031
- * @since 1.0.1
5032
- */
5033
- function _deactivate_plugin_hook() {
5034
- $this->_logger->entrance( 'slug = ' . $this->_slug );
5035
-
5036
- if ( ! current_user_can( 'activate_plugins' ) ) {
5037
- return;
5038
- }
5039
-
5040
- $this->_admin_notices->clear_all_sticky();
5041
- if ( isset( $this->_storage->sticky_optin_added ) ) {
5042
- unset( $this->_storage->sticky_optin_added );
5043
- }
5044
-
5045
- if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
5046
- // Remember that plugin was already installed.
5047
- $this->_storage->is_plugin_new_install = false;
5048
- }
5049
-
5050
- // Hook to plugin uninstall.
5051
- register_uninstall_hook( $this->_plugin_main_file_path, array( 'Freemius', '_uninstall_plugin_hook' ) );
5052
-
5053
- $this->clear_module_main_file_cache();
5054
- $this->clear_sync_cron();
5055
- $this->clear_install_sync_cron();
5056
-
5057
- if ( $this->is_registered() ) {
5058
- // Send deactivation event.
5059
- $this->sync_install( array(
5060
- 'is_active' => false,
5061
- ) );
5062
- } else {
5063
- if ( ! $this->has_api_connectivity() ) {
5064
- // Reset connectivity test cache.
5065
- unset( $this->_storage->connectivity_test );
5066
- }
5067
- }
5068
-
5069
- // Clear API cache on deactivation.
5070
- FS_Api::clear_cache();
5071
-
5072
- $this->remove_sdk_reference();
5073
- }
5074
-
5075
- /**
5076
- * @author Vova Feldman (@svovaf)
5077
- * @since 1.1.6
5078
- */
5079
- private function remove_sdk_reference() {
5080
- global $fs_active_plugins;
5081
-
5082
- foreach ( $fs_active_plugins->plugins as $sdk_path => &$data ) {
5083
- if ( $this->_plugin_basename == $data->plugin_path ) {
5084
- unset( $fs_active_plugins->plugins[ $sdk_path ] );
5085
- break;
5086
- }
5087
- }
5088
-
5089
- fs_fallback_to_newest_active_sdk();
5090
- }
5091
-
5092
- /**
5093
- * @author Vova Feldman (@svovaf)
5094
- * @since 1.1.3
5095
- *
5096
- * @param bool $is_anonymous
5097
- */
5098
- private function set_anonymous_mode( $is_anonymous = true ) {
5099
- // Store information regarding skip to try and opt-in the user
5100
- // again in the future.
5101
- $this->_storage->is_anonymous = array(
5102
- 'is' => $is_anonymous,
5103
- 'timestamp' => WP_FS__SCRIPT_START_TIME,
5104
- 'version' => $this->get_plugin_version(),
5105
- );
5106
-
5107
- // Update anonymous mode cache.
5108
- $this->_is_anonymous = $is_anonymous;
5109
- }
5110
-
5111
- /**
5112
- * @author Vova Feldman (@svovaf)
5113
- * @since 1.1.3
5114
- */
5115
- private function reset_anonymous_mode() {
5116
- unset( $this->_storage->is_anonymous );
5117
-
5118
- /**
5119
- * Ensure that this field is also "false", otherwise, if the current module's type is "theme" and the module
5120
- * has no menus, the opt-in popup will not be shown immediately (in this case, the user will have to click
5121
- * on the admin notice that contains the opt-in link in order to trigger the opt-in popup).
5122
- *
5123
- * @author Leo Fajardo (@leorw)
5124
- * @since 1.2.2
5125
- */
5126
- unset( $this->_is_anonymous );
5127
- }
5128
-
5129
- /**
5130
- * Clears the anonymous mode and redirects to the opt-in screen.
5131
- *
5132
- * @author Vova Feldman (@svovaf)
5133
- * @since 1.1.7
5134
- */
5135
- function connect_again() {
5136
- if ( ! $this->is_anonymous() ) {
5137
- return;
5138
- }
5139
-
5140
- $this->reset_anonymous_mode();
5141
-
5142
- fs_redirect( $this->get_activation_url() );
5143
- }
5144
-
5145
- /**
5146
- * Skip account connect, and set anonymous mode.
5147
- *
5148
- * @author Vova Feldman (@svovaf)
5149
- * @since 1.1.1
5150
- */
5151
- private function skip_connection() {
5152
- $this->_logger->entrance();
5153
-
5154
- $this->_admin_notices->remove_sticky( 'connect_account' );
5155
-
5156
- $this->set_anonymous_mode();
5157
-
5158
- // Send anonymous skip event.
5159
- // No user identified info nor any tracking will be sent after the user skips the opt-in.
5160
- $this->get_api_plugin_scope()->call( 'skip.json', 'put', array(
5161
- 'uid' => $this->get_anonymous_id(),
5162
- ) );
5163
- }
5164
-
5165
- /**
5166
- * Plugin version update hook.
5167
- *
5168
- * @author Vova Feldman (@svovaf)
5169
- * @since 1.0.4
5170
- */
5171
- private function update_plugin_version_event() {
5172
- $this->_logger->entrance();
5173
-
5174
- if ( ! $this->is_registered() ) {
5175
- return;
5176
- }
5177
-
5178
- $this->schedule_install_sync();
5179
- // $this->sync_install( array(), true );
5180
- }
5181
-
5182
- /**
5183
- * Return a list of modified plugins since the last sync.
5184
- *
5185
- * Note:
5186
- * There's no point to store a plugins counter since even if the number of
5187
- * plugins didn't change, we still need to check if the versions are all the
5188
- * same and the activity state is similar.
5189
- *
5190
- * @author Vova Feldman (@svovaf)
5191
- * @since 1.1.8
5192
- *
5193
- * @return array|false
5194
- */
5195
- private function get_plugins_data_for_api() {
5196
- // Alias.
5197
- $option_name = 'all_plugins';
5198
-
5199
- $all_cached_plugins = self::$_accounts->get_option( $option_name );
5200
-
5201
- if ( ! is_object( $all_cached_plugins ) ) {
5202
- $all_cached_plugins = (object) array(
5203
- 'timestamp' => '',
5204
- 'md5' => '',
5205
- 'plugins' => array(),
5206
- );
5207
- }
5208
-
5209
- $time = time();
5210
-
5211
- if ( ! empty( $all_cached_plugins->timestamp ) &&
5212
- ( $time - $all_cached_plugins->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC
5213
- ) {
5214
- // Don't send plugin updates if last update was in the past 5 min.
5215
- return false;
5216
- }
5217
-
5218
- // Write timestamp to lock the logic.
5219
- $all_cached_plugins->timestamp = $time;
5220
- self::$_accounts->set_option( $option_name, $all_cached_plugins, true );
5221
-
5222
- // Reload options from DB.
5223
- self::$_accounts->load( true );
5224
- $all_cached_plugins = self::$_accounts->get_option( $option_name );
5225
-
5226
- if ( $time != $all_cached_plugins->timestamp ) {
5227
- // If timestamp is different, then another thread captured the lock.
5228
- return false;
5229
- }
5230
-
5231
- // Check if there's a change in plugins.
5232
- $all_plugins = self::get_all_plugins();
5233
-
5234
- // Check if plugins changed.
5235
- ksort( $all_plugins );
5236
-
5237
- $plugins_signature = '';
5238
- foreach ( $all_plugins as $basename => $data ) {
5239
- $plugins_signature .= $data['slug'] . ',' .
5240
- $data['Version'] . ',' .
5241
- ( $data['is_active'] ? '1' : '0' ) . ';';
5242
- }
5243
-
5244
- // Check if plugins status changed (version or active/inactive).
5245
- $plugins_changed = ( $all_cached_plugins->md5 !== md5( $plugins_signature ) );
5246
-
5247
- $plugins_update_data = array();
5248
-
5249
- if ( $plugins_changed ) {
5250
- // Change in plugins, report changes.
5251
-
5252
- // Update existing plugins info.
5253
- foreach ( $all_cached_plugins->plugins as $basename => $data ) {
5254
- if ( ! isset( $all_plugins[ $basename ] ) ) {
5255
- // Plugin uninstalled.
5256
- $uninstalled_plugin_data = $data;
5257
- $uninstalled_plugin_data['is_active'] = false;
5258
- $uninstalled_plugin_data['is_uninstalled'] = true;
5259
- $plugins_update_data[] = $uninstalled_plugin_data;
5260
-
5261
- unset( $all_plugins[ $basename ] );
5262
- unset( $all_cached_plugins->plugins[ $basename ] );
5263
- } else if ( $data['is_active'] !== $all_plugins[ $basename ]['is_active'] ||
5264
- $data['version'] !== $all_plugins[ $basename ]['Version']
5265
- ) {
5266
- // Plugin activated or deactivated, or version changed.
5267
- $all_cached_plugins->plugins[ $basename ]['is_active'] = $all_plugins[ $basename ]['is_active'];
5268
- $all_cached_plugins->plugins[ $basename ]['version'] = $all_plugins[ $basename ]['Version'];
5269
-
5270
- $plugins_update_data[] = $all_cached_plugins->plugins[ $basename ];
5271
- }
5272
- }
5273
-
5274
- // Find new plugins that weren't yet seen before.
5275
- foreach ( $all_plugins as $basename => $data ) {
5276
- if ( ! isset( $all_cached_plugins->plugins[ $basename ] ) ) {
5277
- // New plugin.
5278
- $new_plugin = array(
5279
- 'slug' => $data['slug'],
5280
- 'version' => $data['Version'],
5281
- 'title' => $data['Name'],
5282
- 'is_active' => $data['is_active'],
5283
- 'is_uninstalled' => false,
5284
- );
5285
-
5286
- $plugins_update_data[] = $new_plugin;
5287
- $all_cached_plugins->plugins[ $basename ] = $new_plugin;
5288
- }
5289
- }
5290
-
5291
- $all_cached_plugins->md5 = md5( $plugins_signature );
5292
- $all_cached_plugins->timestamp = $time;
5293
- self::$_accounts->set_option( $option_name, $all_cached_plugins, true );
5294
- }
5295
-
5296
- return $plugins_update_data;
5297
- }
5298
-
5299
- /**
5300
- * Return a list of modified themes since the last sync.
5301
- *
5302
- * Note:
5303
- * There's no point to store a themes counter since even if the number of
5304
- * themes didn't change, we still need to check if the versions are all the
5305
- * same and the activity state is similar.
5306
- *
5307
- * @author Vova Feldman (@svovaf)
5308
- * @since 1.1.8
5309
- *
5310
- * @return array|false
5311
- */
5312
- private function get_themes_data_for_api() {
5313
- // Alias.
5314
- $option_name = 'all_themes';
5315
-
5316
- $all_cached_themes = self::$_accounts->get_option( $option_name );
5317
-
5318
- if ( ! is_object( $all_cached_themes ) ) {
5319
- $all_cached_themes = (object) array(
5320
- 'timestamp' => '',
5321
- 'md5' => '',
5322
- 'themes' => array(),
5323
- );
5324
- }
5325
-
5326
- $time = time();
5327
-
5328
- if ( ! empty( $all_cached_themes->timestamp ) &&
5329
- ( $time - $all_cached_themes->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC
5330
- ) {
5331
- // Don't send theme updates if last update was in the past 5 min.
5332
- return false;
5333
- }
5334
-
5335
- // Write timestamp to lock the logic.
5336
- $all_cached_themes->timestamp = $time;
5337
- self::$_accounts->set_option( $option_name, $all_cached_themes, true );
5338
-
5339
- // Reload options from DB.
5340
- self::$_accounts->load( true );
5341
- $all_cached_themes = self::$_accounts->get_option( $option_name );
5342
-
5343
- if ( $time != $all_cached_themes->timestamp ) {
5344
- // If timestamp is different, then another thread captured the lock.
5345
- return false;
5346
- }
5347
-
5348
- // Get active theme.
5349
- $active_theme = wp_get_theme();
5350
- $active_theme_stylesheet = $active_theme->get_stylesheet();
5351
-
5352
- // Check if there's a change in themes.
5353
- $all_themes = wp_get_themes();
5354
-
5355
- // Check if themes changed.
5356
- ksort( $all_themes );
5357
-
5358
- $themes_signature = '';
5359
- foreach ( $all_themes as $slug => $data ) {
5360
- $is_active = ( $slug === $active_theme_stylesheet );
5361
- $themes_signature .= $slug . ',' .
5362
- $data->version . ',' .
5363
- ( $is_active ? '1' : '0' ) . ';';
5364
- }
5365
-
5366
- // Check if themes status changed (version or active/inactive).
5367
- $themes_changed = ( $all_cached_themes->md5 !== md5( $themes_signature ) );
5368
-
5369
- $themes_update_data = array();
5370
-
5371
- if ( $themes_changed ) {
5372
- // Change in themes, report changes.
5373
-
5374
- // Update existing themes info.
5375
- foreach ( $all_cached_themes->themes as $slug => $data ) {
5376
- $is_active = ( $slug === $active_theme_stylesheet );
5377
-
5378
- if ( ! isset( $all_themes[ $slug ] ) ) {
5379
- // Plugin uninstalled.
5380
- $uninstalled_theme_data = $data;
5381
- $uninstalled_theme_data['is_active'] = false;
5382
- $uninstalled_theme_data['is_uninstalled'] = true;
5383
- $themes_update_data[] = $uninstalled_theme_data;
5384
-
5385
- unset( $all_themes[ $slug ] );
5386
- unset( $all_cached_themes->themes[ $slug ] );
5387
- } else if ( $data['is_active'] !== $is_active ||
5388
- $data['version'] !== $all_themes[ $slug ]->version
5389
- ) {
5390
- // Plugin activated or deactivated, or version changed.
5391
-
5392
- $all_cached_themes->themes[ $slug ]['is_active'] = $is_active;
5393
- $all_cached_themes->themes[ $slug ]['version'] = $all_themes[ $slug ]->version;
5394
-
5395
- $themes_update_data[] = $all_cached_themes->themes[ $slug ];
5396
- }
5397
- }
5398
-
5399
- // Find new themes that weren't yet seen before.
5400
- foreach ( $all_themes as $slug => $data ) {
5401
- if ( ! isset( $all_cached_themes->themes[ $slug ] ) ) {
5402
- $is_active = ( $slug === $active_theme_stylesheet );
5403
-
5404
- // New plugin.
5405
- $new_plugin = array(
5406
- 'slug' => $slug,
5407
- 'version' => $data->version,
5408
- 'title' => $data->name,
5409
- 'is_active' => $is_active,
5410
- 'is_uninstalled' => false,
5411
- );
5412
-
5413
- $themes_update_data[] = $new_plugin;
5414
- $all_cached_themes->themes[ $slug ] = $new_plugin;
5415
- }
5416
- }
5417
-
5418
- $all_cached_themes->md5 = md5( $themes_signature );
5419
- $all_cached_themes->timestamp = time();
5420
- self::$_accounts->set_option( $option_name, $all_cached_themes, true );
5421
- }
5422
-
5423
- return $themes_update_data;
5424
- }
5425
-
5426
- /**
5427
- * Update install details.
5428
- *
5429
- * @author Vova Feldman (@svovaf)
5430
- * @since 1.1.2
5431
- *
5432
- * @param string[] string $override
5433
- * @param bool $include_plugins Since 1.1.8 by default include plugin changes.
5434
- * @param bool $include_themes Since 1.1.8 by default include plugin changes.
5435
- *
5436
- * @return array
5437
- */
5438
- private function get_install_data_for_api(
5439
- array $override,
5440
- $include_plugins = true,
5441
- $include_themes = true
5442
- ) {
5443
- /**
5444
- * @since 1.1.8 Also send plugin updates.
5445
- */
5446
- if ( $include_plugins && ! isset( $override['plugins'] ) ) {
5447
- $plugins = $this->get_plugins_data_for_api();
5448
- if ( ! empty( $plugins ) ) {
5449
- $override['plugins'] = $plugins;
5450
- }
5451
- }
5452
- /**
5453
- * @since 1.1.8 Also send themes updates.
5454
- */
5455
- if ( $include_themes && ! isset( $override['themes'] ) ) {
5456
- $themes = $this->get_themes_data_for_api();
5457
- if ( ! empty( $themes ) ) {
5458
- $override['themes'] = $themes;
5459
- }
5460
- }
5461
-
5462
- return array_merge( array(
5463
- 'version' => $this->get_plugin_version(),
5464
- 'is_premium' => $this->is_premium(),
5465
- 'language' => get_bloginfo( 'language' ),
5466
- 'charset' => get_bloginfo( 'charset' ),
5467
- 'platform_version' => get_bloginfo( 'version' ),
5468
- 'sdk_version' => $this->version,
5469
- 'programming_language_version' => phpversion(),
5470
- 'title' => get_bloginfo( 'name' ),
5471
- 'url' => get_site_url(),
5472
- // Special params.
5473
- 'is_active' => true,
5474
- 'is_disconnected' => $this->is_tracking_prohibited(),
5475
- 'is_uninstalled' => false,
5476
- ), $override );
5477
- }
5478
-
5479
- /**
5480
- * Update install only if changed.
5481
- *
5482
- * @author Vova Feldman (@svovaf)
5483
- * @since 1.0.9
5484
- *
5485
- * @param string[] string $override
5486
- * @param bool $flush
5487
- *
5488
- * @return false|object|string
5489
- */
5490
- private function send_install_update( $override = array(), $flush = false ) {
5491
- $this->_logger->entrance();
5492
-
5493
- $check_properties = $this->get_install_data_for_api( $override );
5494
-
5495
- if ( $flush ) {
5496
- $params = $check_properties;
5497
- } else {
5498
- $params = array();
5499
- $special = array();
5500
- $special_override = false;
5501
-
5502
- foreach ( $check_properties as $p => $v ) {
5503
- if ( property_exists( $this->_site, $p ) ) {
5504
- if ( ( is_bool( $this->_site->{$p} ) || ! empty( $this->_site->{$p} ) ) &&
5505
- $this->_site->{$p} != $v
5506
- ) {
5507
- $this->_site->{$p} = $v;
5508
- $params[ $p ] = $v;
5509
- }
5510
- } else {
5511
- $special[ $p ] = $v;
5512
-
5513
- if ( isset( $override[ $p ] ) ||
5514
- 'plugins' === $p ||
5515
- 'themes' === $p
5516
- ) {
5517
- $special_override = true;
5518
- }
5519
- }
5520
- }
5521
-
5522
- if ( $special_override || 0 < count( $params ) ) {
5523
- // Add special params only if has at least one
5524
- // standard param, or if explicitly requested to
5525
- // override a special param or a param which is not exist
5526
- // in the install object.
5527
- $params = array_merge( $params, $special );
5528
- }
5529
- }
5530
-
5531
- if ( 0 < count( $params ) ) {
5532
- // Update last install sync timestamp.
5533
- $this->_storage->install_sync_timestamp = time();
5534
-
5535
- $params['uid'] = $this->get_anonymous_id();
5536
-
5537
- // Send updated values to FS.
5538
- $site = $this->get_api_site_scope()->call( '/', 'put', $params );
5539
-
5540
- if ( $this->is_api_result_entity( $site ) ) {
5541
- // I successfully sent install update, clear scheduled sync if exist.
5542
- $this->clear_install_sync_cron();
5543
- }
5544
-
5545
- return $site;
5546
- }
5547
-
5548
- return false;
5549
- }
5550
-
5551
- /**
5552
- * Update install only if changed.
5553
- *
5554
- * @author Vova Feldman (@svovaf)
5555
- * @since 1.0.9
5556
- *
5557
- * @param string[] string $override
5558
- * @param bool $flush
5559
- */
5560
- private function sync_install( $override = array(), $flush = false ) {
5561
- $this->_logger->entrance();
5562
-
5563
- $site = $this->send_install_update( $override, $flush );
5564
-
5565
- if ( false === $site ) {
5566
- // No sync required.
5567
- return;
5568
- }
5569
-
5570
- if ( ! $this->is_api_result_entity( $site ) ) {
5571
- // Failed to sync, don't update locally.
5572
- return;
5573
- }
5574
-
5575
- $plan = $this->get_plan();
5576
- $this->_site = new FS_Site( $site );
5577
- $this->_site->plan = $plan;
5578
-
5579
- $this->_store_site( true );
5580
- }
5581
-
5582
- /**
5583
- * Track install's custom event.
5584
- *
5585
- * IMPORTANT:
5586
- * Custom event tracking is currently only supported for specific clients.
5587
- * If you are not one of them, please don't use this method. If you will,
5588
- * the API will simply ignore your request based on the plugin ID.
5589
- *
5590
- * Need custom tracking for your plugin or theme?
5591
- * If you are interested in custom event tracking please contact yo@freemius.com
5592
- * for further details.
5593
- *
5594
- * @author Vova Feldman (@svovaf)
5595
- * @since 1.2.1
5596
- *
5597
- * @param string $name Event name.
5598
- * @param array $properties Associative key/value array with primitive values only
5599
- * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s.
5600
- * @param bool $once If true, event will be tracked only once. IMPORTANT: Still trigger the API call.
5601
- *
5602
- * @return object|false Event data or FALSE on failure.
5603
- *
5604
- * @throws \Freemius_InvalidArgumentException
5605
- */
5606
- public function track_event( $name, $properties = array(), $process_at = false, $once = false ) {
5607
- $this->_logger->entrance( http_build_query( array( 'name' => $name, 'once' => $once ) ) );
5608
-
5609
- if ( ! $this->is_registered() ) {
5610
- return false;
5611
- }
5612
-
5613
- $event = array( 'type' => $name );
5614
-
5615
- if ( is_numeric( $process_at ) && $process_at > time() ) {
5616
- $event['process_at'] = $process_at;
5617
- }
5618
-
5619
- if ( $once ) {
5620
- $event['once'] = true;
5621
- }
5622
-
5623
- if ( ! empty( $properties ) ) {
5624
- // Verify associative array values are primitive.
5625
- foreach ( $properties as $k => $v ) {
5626
- if ( ! is_scalar( $v ) ) {
5627
- throw new Freemius_InvalidArgumentException( 'The $properties argument must be an associative key/value array with primitive values only.' );
5628
- }
5629
- }
5630
-
5631
- $event['properties'] = $properties;
5632
- }
5633
-
5634
- $result = $this->get_api_site_scope()->call( 'events.json', 'post', $event );
5635
-
5636
- return $this->is_api_error( $result ) ?
5637
- false :
5638
- $result;
5639
- }
5640
-
5641
- /**
5642
- * Track install's custom event only once, but it still triggers the API call.
5643
- *
5644
- * IMPORTANT:
5645
- * Custom event tracking is currently only supported for specific clients.
5646
- * If you are not one of them, please don't use this method. If you will,
5647
- * the API will simply ignore your request based on the plugin ID.
5648
- *
5649
- * Need custom tracking for your plugin or theme?
5650
- * If you are interested in custom event tracking please contact yo@freemius.com
5651
- * for further details.
5652
- *
5653
- * @author Vova Feldman (@svovaf)
5654
- * @since 1.2.1
5655
- *
5656
- * @param string $name Event name.
5657
- * @param array $properties Associative key/value array with primitive values only
5658
- * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s.
5659
- *
5660
- * @return object|false Event data or FALSE on failure.
5661
- *
5662
- * @throws \Freemius_InvalidArgumentException
5663
- *
5664
- * @user Freemius::track_event()
5665
- */
5666
- public function track_event_once( $name, $properties = array(), $process_at = false ) {
5667
- return $this->track_event( $name, $properties, $process_at, true );
5668
- }
5669
-
5670
- /**
5671
- * Plugin uninstall hook.
5672
- *
5673
- * @author Vova Feldman (@svovaf)
5674
- * @since 1.0.1
5675
- *
5676
- * @param bool $check_user Enforce checking if user have plugins activation privileges.
5677
- */
5678
- function _uninstall_plugin_event( $check_user = true ) {
5679
- $this->_logger->entrance( 'slug = ' . $this->_slug );
5680
-
5681
- if ( $check_user && ! current_user_can( 'activate_plugins' ) ) {
5682
- return;
5683
- }
5684
-
5685
- $params = array();
5686
- $uninstall_reason = null;
5687
- if ( isset( $this->_storage->uninstall_reason ) ) {
5688
- $uninstall_reason = $this->_storage->uninstall_reason;
5689
- $params['reason_id'] = $uninstall_reason->id;
5690
- $params['reason_info'] = $uninstall_reason->info;
5691
- }
5692
-
5693
- if ( ! $this->is_registered() ) {
5694
- // Send anonymous uninstall event only if user submitted a feedback.
5695
- if ( isset( $uninstall_reason ) ) {
5696
- if ( isset( $uninstall_reason->is_anonymous ) && ! $uninstall_reason->is_anonymous ) {
5697
- $this->opt_in( false, false, false, false, true );
5698
- } else {
5699
- $params['uid'] = $this->get_anonymous_id();
5700
- $this->get_api_plugin_scope()->call( 'uninstall.json', 'put', $params );
5701
- }
5702
- }
5703
- } else {
5704
- // Send uninstall event.
5705
- $this->send_install_update( array_merge( $params, array(
5706
- 'is_active' => false,
5707
- 'is_uninstalled' => true,
5708
- ) ) );
5709
- }
5710
-
5711
- // @todo Decide if we want to delete plugin information from db.
5712
- }
5713
-
5714
- /**
5715
- * @author Vova Feldman (@svovaf)
5716
- * @since 1.1.1
5717
- *
5718
- * @return string
5719
- */
5720
- function premium_plugin_basename() {
5721
- return "{$this->_slug}-premium/" . basename( $this->_free_plugin_basename );
5722
- }
5723
-
5724
- /**
5725
- * Uninstall plugin hook. Called only when connected his account with Freemius for active sites tracking.
5726
- *
5727
- * @author Vova Feldman (@svovaf)
5728
- * @since 1.0.2
5729
- */
5730
- public static function _uninstall_plugin_hook() {
5731
- self::_load_required_static();
5732
-
5733
- self::$_static_logger->entrance();
5734
-
5735
- if ( ! current_user_can( 'activate_plugins' ) ) {
5736
- return;
5737
- }
5738
-
5739
- $plugin_file = substr( current_filter(), strlen( 'uninstall_' ) );
5740
-
5741
- self::$_static_logger->info( 'plugin = ' . $plugin_file );
5742
-
5743
- define( 'WP_FS__UNINSTALL_MODE', true );
5744
-
5745
- $fs = self::get_instance_by_file( $plugin_file );
5746
-
5747
- if ( is_object( $fs ) ) {
5748
- self::require_plugin_essentials();
5749
-
5750
- if ( is_plugin_active( $fs->_free_plugin_basename ) ||
5751
- is_plugin_active( $fs->premium_plugin_basename() )
5752
- ) {
5753
- // Deleting Free or Premium plugin version while the other version still installed.
5754
- return;
5755
- }
5756
-
5757
- $fs->_uninstall_plugin_event();
5758
-
5759
- $fs->do_action( 'after_uninstall' );
5760
- }
5761
- }
5762
-
5763
- #----------------------------------------------------------------------------------
5764
- #region Plugin Information
5765
- #----------------------------------------------------------------------------------
5766
-
5767
- /**
5768
- * Load WordPress core plugin.php essential module.
5769
- *
5770
- * @author Vova Feldman (@svovaf)
5771
- * @since 1.1.1
5772
- */
5773
- private static function require_plugin_essentials() {
5774
- if ( ! function_exists( 'get_plugins' ) ) {
5775
- self::$_static_logger->log( 'Including wp-admin/includes/plugin.php...' );
5776
-
5777
- require_once ABSPATH . 'wp-admin/includes/plugin.php';
5778
- }
5779
- }
5780
-
5781
- /**
5782
- * Load WordPress core pluggable.php module.
5783
- *
5784
- * @author Vova Feldman (@svovaf)
5785
- * @since 1.1.2
5786
- */
5787
- private static function require_pluggable_essentials() {
5788
- if ( ! function_exists( 'wp_get_current_user' ) ) {
5789
- require_once ABSPATH . 'wp-includes/pluggable.php';
5790
- }
5791
- }
5792
-
5793
- /**
5794
- * Return plugin data.
5795
- *
5796
- * @author Vova Feldman (@svovaf)
5797
- * @since 1.0.1
5798
- *
5799
- * @return array
5800
- */
5801
- function get_plugin_data() {
5802
- if ( ! isset( $this->_plugin_data ) ) {
5803
- self::require_plugin_essentials();
5804
-
5805
- if ( $this->is_plugin() ) {
5806
- /**
5807
- * @author Vova Feldman (@svovaf)
5808
- * @since 1.2.0 When using get_plugin_data() do NOT translate plugin data.
5809
- *
5810
- * @link https://github.com/Freemius/wordpress-sdk/issues/77
5811
- */
5812
- $plugin_data = get_plugin_data(
5813
- $this->_plugin_main_file_path,
5814
- false,
5815
- false
5816
- );
5817
- } else {
5818
- $theme_data = wp_get_theme();
5819
-
5820
- $plugin_data = array(
5821
- 'Name' => $theme_data->get( 'Name' ),
5822
- 'Version' => $theme_data->get( 'Version' ),
5823
- 'Author' => $theme_data->get( 'Author' ),
5824
- 'Description' => $theme_data->get( 'Description' ),
5825
- 'PluginURI' => $theme_data->get( 'ThemeURI' ),
5826
- );
5827
- }
5828
-
5829
- $this->_plugin_data = $plugin_data;
5830
- }
5831
-
5832
- return $this->_plugin_data;
5833
- }
5834
-
5835
- /**
5836
- * @author Vova Feldman (@svovaf)
5837
- * @since 1.0.1
5838
- * @since 1.2.2.5 If slug not set load slug by module ID.
5839
- *
5840
- * @return string Plugin slug.
5841
- */
5842
- function get_slug() {
5843
- if ( ! isset( $this->_slug ) ) {
5844
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
5845
- $this->_slug = $id_slug_type_path_map[ $this->_module_id ]['slug'];
5846
- }
5847
-
5848
- return $this->_slug;
5849
- }
5850
-
5851
- /**
5852
- * @author Vova Feldman (@svovaf)
5853
- * @since 1.2.1.7
5854
- *
5855
- * @return string Plugin slug.
5856
- */
5857
- function get_target_folder_name() {
5858
- return $this->_slug . ( $this->can_use_premium_code() ? '-premium' : '' );
5859
- }
5860
-
5861
- /**
5862
- * @author Vova Feldman (@svovaf)
5863
- * @since 1.0.1
5864
- *
5865
- * @return number Plugin ID.
5866
- */
5867
- function get_id() {
5868
- return $this->_plugin->id;
5869
- }
5870
-
5871
- /**
5872
- * @author Vova Feldman (@svovaf)
5873
- * @since 1.2.1.5
5874
- *
5875
- * @return string Freemius SDK version
5876
- */
5877
- function get_sdk_version() {
5878
- return $this->version;
5879
- }
5880
-
5881
- /**
5882
- * @author Vova Feldman (@svovaf)
5883
- * @since 1.2.1.5
5884
- *
5885
- * @return number Parent plugin ID (if parent exist).
5886
- */
5887
- function get_parent_id() {
5888
- return $this->is_addon() ?
5889
- $this->get_parent_instance()->get_id() :
5890
- $this->_plugin->id;
5891
- }
5892
-
5893
- /**
5894
- * @author Vova Feldman (@svovaf)
5895
- * @since 1.0.1
5896
- *
5897
- * @return string Plugin public key.
5898
- */
5899
- function get_public_key() {
5900
- return $this->_plugin->public_key;
5901
- }
5902
-
5903
- /**
5904
- * Will be available only on sandbox mode.
5905
- *
5906
- * @author Vova Feldman (@svovaf)
5907
- * @since 1.0.4
5908
- *
5909
- * @return mixed Plugin secret key.
5910
- */
5911
- function get_secret_key() {
5912
- return $this->_plugin->secret_key;
5913
- }
5914
-
5915
- /**
5916
- * @author Vova Feldman (@svovaf)
5917
- * @since 1.1.1
5918
- *
5919
- * @return bool
5920
- */
5921
- function has_secret_key() {
5922
- return ! empty( $this->_plugin->secret_key );
5923
- }
5924
-
5925
- /**
5926
- * @author Vova Feldman (@svovaf)
5927
- * @since 1.0.9
5928
- *
5929
- * @return string
5930
- */
5931
- function get_plugin_name() {
5932
- $this->_logger->entrance();
5933
-
5934
- if ( ! isset( $this->_plugin_name ) ) {
5935
- $plugin_data = $this->get_plugin_data();
5936
-
5937
- // Get name.
5938
- $this->_plugin_name = $plugin_data['Name'];
5939
-
5940
- // Check if plugin name contains "(Premium)" suffix and remove it.
5941
- $suffix = ' (premium)';
5942
- $suffix_len = strlen( $suffix );
5943
-
5944
- if ( strlen( $plugin_data['Name'] ) > $suffix_len &&
5945
- $suffix === substr( strtolower( $plugin_data['Name'] ), - $suffix_len )
5946
- ) {
5947
- $this->_plugin_name = substr( $plugin_data['Name'], 0, - $suffix_len );
5948
- }
5949
-
5950
- $this->_logger->departure( 'Name = ' . $this->_plugin_name );
5951
- }
5952
-
5953
- return $this->_plugin_name;
5954
- }
5955
-
5956
- /**
5957
- * @author Vova Feldman (@svovaf)
5958
- * @since 1.0.0
5959
- *
5960
- * @return string
5961
- */
5962
- function get_plugin_version() {
5963
- $this->_logger->entrance();
5964
-
5965
- $plugin_data = $this->get_plugin_data();
5966
-
5967
- $this->_logger->departure( 'Version = ' . $plugin_data['Version'] );
5968
-
5969
- return $this->apply_filters( 'plugin_version', $plugin_data['Version'] );
5970
- }
5971
-
5972
- /**
5973
- * @author Vova Feldman (@svovaf)
5974
- * @since 1.2.1.7
5975
- *
5976
- * @return string
5977
- */
5978
- function get_plugin_title() {
5979
- $this->_logger->entrance();
5980
-
5981
- $title = $this->_plugin->title;
5982
-
5983
- return $this->apply_filters( 'plugin_title', $title );
5984
- }
5985
-
5986
- /**
5987
- * @author Vova Feldman (@svovaf)
5988
- * @since 1.2.2.7
5989
- *
5990
- * @param bool $lowercase
5991
- *
5992
- * @return string
5993
- */
5994
- function get_module_label( $lowercase = false ) {
5995
- $label = $this->is_addon() ?
5996
- $this->get_text_inline( 'Add-On', 'addon' ) :
5997
- ( $this->is_plugin() ?
5998
- $this->get_text_inline( 'Plugin', 'plugin' ) :
5999
- $this->get_text_inline( 'Theme', 'theme' ) );
6000
-
6001
- if ( $lowercase ) {
6002
- $label = strtolower( $label );
6003
- }
6004
-
6005
- return $label;
6006
- }
6007
-
6008
- /**
6009
- * @author Vova Feldman (@svovaf)
6010
- * @since 1.0.4
6011
- *
6012
- * @return string
6013
- */
6014
- function get_plugin_basename() {
6015
- if ( ! isset( $this->_plugin_basename ) ) {
6016
- if ( $this->is_plugin() ) {
6017
- $this->_plugin_basename = plugin_basename( $this->_plugin_main_file_path );
6018
- } else {
6019
- $this->_plugin_basename = basename( dirname( $this->_plugin_main_file_path ) );
6020
- }
6021
- }
6022
-
6023
- return $this->_plugin_basename;
6024
- }
6025
-
6026
- function get_plugin_folder_name() {
6027
- $this->_logger->entrance();
6028
-
6029
- $plugin_folder = $this->_plugin_basename;
6030
-
6031
- while ( '.' !== dirname( $plugin_folder ) ) {
6032
- $plugin_folder = dirname( $plugin_folder );
6033
- }
6034
-
6035
- $this->_logger->departure( 'Folder Name = ' . $plugin_folder );
6036
-
6037
- return $plugin_folder;
6038
- }
6039
-
6040
- #endregion ------------------------------------------------------------------
6041
-
6042
- /* Account
6043
- ------------------------------------------------------------------------------------------------------------------*/
6044
 
6045
- /**
6046
- * Find plugin's slug by plugin's basename.
6047
- *
6048
- * @author Vova Feldman (@svovaf)
6049
- * @since 1.0.9
6050
- *
6051
- * @param string $plugin_base_name
6052
- *
6053
- * @return false|string
6054
- */
6055
- private static function find_slug_by_basename( $plugin_base_name ) {
6056
- $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
6057
-
6058
- if ( ! array( $file_slug_map ) || ! isset( $file_slug_map[ $plugin_base_name ] ) ) {
6059
- return false;
6060
- }
6061
-
6062
- return $file_slug_map[ $plugin_base_name ];
6063
- }
6064
-
6065
- /**
6066
- * Store the map between the plugin's basename to the slug.
6067
- *
6068
- * @author Vova Feldman (@svovaf)
6069
- * @since 1.0.9
6070
- */
6071
- private function store_file_slug_map() {
6072
- $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
6073
-
6074
- if ( ! array( $file_slug_map ) ) {
6075
- $file_slug_map = array();
6076
- }
6077
-
6078
- if ( ! isset( $file_slug_map[ $this->_plugin_basename ] ) ||
6079
- $file_slug_map[ $this->_plugin_basename ] !== $this->_slug
6080
- ) {
6081
- $file_slug_map[ $this->_plugin_basename ] = $this->_slug;
6082
- self::$_accounts->set_option( 'file_slug_map', $file_slug_map, true );
6083
- }
6084
- }
6085
-
6086
- /**
6087
- * @return FS_User[]
6088
- */
6089
- static function get_all_users() {
6090
- $users = self::$_accounts->get_option( 'users', array() );
6091
-
6092
- if ( ! is_array( $users ) ) {
6093
- $users = array();
6094
- }
6095
-
6096
- return $users;
6097
- }
6098
-
6099
- /**
6100
- * @param string $module_type
6101
- *
6102
- * @return FS_Site[]
6103
- */
6104
- private static function get_all_sites( $module_type = WP_FS__MODULE_TYPE_PLUGIN ) {
6105
- $sites = self::get_account_option( 'sites', $module_type );
6106
-
6107
- if ( ! is_array( $sites ) ) {
6108
- $sites = array();
6109
- }
6110
-
6111
- return $sites;
6112
- }
6113
-
6114
- /**
6115
- * @author Leo Fajardo (@leorw)
6116
- *
6117
- * @since 1.2.2
6118
- *
6119
- * @param string $option_name
6120
- * @param string $module_type
6121
- *
6122
- * @return mixed
6123
- */
6124
- private static function get_account_option( $option_name, $module_type ) {
6125
- if ( WP_FS__MODULE_TYPE_PLUGIN !== $module_type ) {
6126
- $option_name = $module_type . '_' . $option_name;
6127
- }
6128
-
6129
- return self::$_accounts->get_option( $option_name, array() );
6130
- }
6131
-
6132
- /**
6133
- * @author Leo Fajardo (@leorw)
6134
- *
6135
- * @since 1.2.2
6136
- *
6137
- * @param string $option_name
6138
- * @param mixed $option_value
6139
- * @param bool $store
6140
- */
6141
- private function set_account_option( $option_name, $option_value, $store ) {
6142
- self::set_account_option_by_module(
6143
- $this->_module_type,
6144
- $option_name,
6145
- $option_value,
6146
- $store
6147
- );
6148
- }
6149
-
6150
- /**
6151
- * @author Vova Feldman (@svovaf)
6152
- *
6153
- * @since 1.2.2.7
6154
- *
6155
- * @param string $module_type
6156
- * @param string $option_name
6157
- * @param mixed $option_value
6158
- * @param bool $store
6159
- */
6160
- private static function set_account_option_by_module( $module_type, $option_name, $option_value, $store ) {
6161
- if ( WP_FS__MODULE_TYPE_PLUGIN != $module_type ) {
6162
- $option_name = $module_type . '_' . $option_name;
6163
- }
6164
-
6165
- self::$_accounts->set_option( $option_name, $option_value, $store );
6166
- }
6167
-
6168
- /**
6169
- * @author Vova Feldman (@svovaf)
6170
- * @since 1.0.6
6171
- *
6172
- * @param string $module_type
6173
- *
6174
- * @return FS_Plugin_License[]
6175
- */
6176
- private static function get_all_licenses( $module_type = WP_FS__MODULE_TYPE_PLUGIN ) {
6177
- $licenses = self::get_account_option( 'licenses', $module_type );
6178
-
6179
- if ( ! is_array( $licenses ) ) {
6180
- $licenses = array();
6181
- }
6182
-
6183
- return $licenses;
6184
- }
6185
-
6186
- /**
6187
- * @param string|bool $module_type
6188
- *
6189
- * @return FS_Plugin_Plan[]
6190
- */
6191
- private static function get_all_plans( $module_type = false ) {
6192
- $plans = self::get_account_option( 'plans', $module_type );
6193
-
6194
- if ( ! is_array( $plans ) ) {
6195
- $plans = array();
6196
- }
6197
-
6198
- return $plans;
6199
- }
6200
-
6201
- /**
6202
- * @author Vova Feldman (@svovaf)
6203
- * @since 1.0.4
6204
- *
6205
- * @return FS_Plugin_Tag[]
6206
- */
6207
- private static function get_all_updates() {
6208
- $updates = self::$_accounts->get_option( 'updates', array() );
6209
-
6210
- if ( ! is_array( $updates ) ) {
6211
- $updates = array();
6212
- }
6213
-
6214
- return $updates;
6215
- }
6216
-
6217
- /**
6218
- * @author Vova Feldman (@svovaf)
6219
- * @since 1.0.6
6220
- *
6221
- * @return array<number,FS_Plugin[]>|false
6222
- */
6223
- private static function get_all_addons() {
6224
- $addons = self::$_accounts->get_option( 'addons', array() );
6225
-
6226
- if ( ! is_array( $addons ) ) {
6227
- $addons = array();
6228
- }
6229
-
6230
- return $addons;
6231
- }
6232
-
6233
- /**
6234
- * @author Vova Feldman (@svovaf)
6235
- * @since 1.0.6
6236
- *
6237
- * @return FS_Plugin[]|false
6238
- */
6239
- private static function get_all_account_addons() {
6240
- $addons = self::$_accounts->get_option( 'account_addons', array() );
6241
-
6242
- if ( ! is_array( $addons ) ) {
6243
- $addons = array();
6244
- }
6245
-
6246
- return $addons;
6247
- }
6248
-
6249
- /**
6250
- * Check if user has connected his account (opted-in).
6251
- *
6252
- * Note:
6253
- * If the user opted-in and opted-out on a later stage,
6254
- * this will still return true. If you want to check if the
6255
- * user is currently opted-in, use:
6256
- * `$fs->is_registered() && $fs->is_tracking_allowed()`
6257
- *
6258
- * @author Vova Feldman (@svovaf)
6259
- * @since 1.0.1
6260
- * @return bool
6261
- */
6262
- function is_registered() {
6263
- return is_object( $this->_user );
6264
- }
6265
-
6266
- /**
6267
- * Returns TRUE if the user opted-in and didn't disconnect (opt-out).
6268
- *
6269
- * @author Leo Fajardo (@leorw)
6270
- * @since 1.2.1.5
6271
- *
6272
- * @return bool
6273
- */
6274
- function is_tracking_allowed() {
6275
- return ( is_object( $this->_site ) && true !== $this->_site->is_disconnected );
6276
- }
6277
-
6278
- /**
6279
- * @author Vova Feldman (@svovaf)
6280
- * @since 1.0.4
6281
- *
6282
- * @return FS_Plugin
6283
- */
6284
- function get_plugin() {
6285
- return $this->_plugin;
6286
- }
6287
-
6288
- /**
6289
- * @author Vova Feldman (@svovaf)
6290
- * @since 1.0.3
6291
- *
6292
- * @return FS_User
6293
- */
6294
- function get_user() {
6295
- return $this->_user;
6296
- }
6297
-
6298
- /**
6299
- * @author Vova Feldman (@svovaf)
6300
- * @since 1.0.3
6301
- *
6302
- * @return FS_Site
6303
- */
6304
- function get_site() {
6305
- return $this->_site;
6306
- }
6307
-
6308
- /**
6309
- * Get plugin add-ons.
6310
- *
6311
- * @author Vova Feldman (@svovaf)
6312
- * @since 1.0.6
6313
- *
6314
- * @since 1.1.7.3 If not yet loaded, fetch data from the API.
6315
- *
6316
- * @param bool $flush
6317
- *
6318
- * @return FS_Plugin[]|false
6319
- */
6320
- function get_addons( $flush = false ) {
6321
- $this->_logger->entrance();
6322
-
6323
- if ( ! $this->_has_addons ) {
6324
- return false;
6325
- }
6326
-
6327
- $addons = $this->sync_addons( $flush );
6328
-
6329
- return ( ! is_array( $addons ) || empty( $addons ) ) ?
6330
- false :
6331
- $addons;
6332
- }
6333
-
6334
- /**
6335
- * @author Vova Feldman (@svovaf)
6336
- * @since 1.0.6
6337
- *
6338
- * @return FS_Plugin[]|false
6339
- */
6340
- function get_account_addons() {
6341
- $this->_logger->entrance();
6342
-
6343
- $addons = self::get_all_account_addons();
6344
-
6345
- if ( ! is_array( $addons ) ||
6346
- ! isset( $addons[ $this->_plugin->id ] ) ||
6347
- ! is_array( $addons[ $this->_plugin->id ] ) ||
6348
- 0 === count( $addons[ $this->_plugin->id ] )
6349
- ) {
6350
- return false;
6351
- }
6352
-
6353
- return $addons[ $this->_plugin->id ];
6354
- }
6355
-
6356
- /**
6357
- * Check if user has any
6358
- *
6359
- * @author Vova Feldman (@svovaf)
6360
- * @since 1.1.6
6361
- *
6362
- * @return bool
6363
- */
6364
- function has_account_addons() {
6365
- $addons = $this->get_account_addons();
6366
-
6367
- return is_array( $addons ) && ( 0 < count( $addons ) );
6368
- }
6369
-
6370
-
6371
- /**
6372
- * Get add-on by ID (from local data).
6373
- *
6374
- * @author Vova Feldman (@svovaf)
6375
- * @since 1.0.6
6376
- *
6377
- * @param number $id
6378
- *
6379
- * @return FS_Plugin|false
6380
- */
6381
- function get_addon( $id ) {
6382
- $this->_logger->entrance();
6383
-
6384
- $addons = $this->get_addons();
6385
-
6386
- if ( is_array( $addons ) ) {
6387
- foreach ( $addons as $addon ) {
6388
- if ( $id == $addon->id ) {
6389
- return $addon;
6390
- }
6391
- }
6392
- }
6393
-
6394
- return false;
6395
- }
6396
-
6397
- /**
6398
- * Get add-on by slug (from local data).
6399
- *
6400
- * @author Vova Feldman (@svovaf)
6401
- * @since 1.0.6
6402
- *
6403
- * @param string $slug
6404
- *
6405
- * @param bool $flush
6406
- *
6407
- * @return FS_Plugin|false
6408
- */
6409
- function get_addon_by_slug( $slug, $flush = false ) {
6410
- $this->_logger->entrance();
6411
-
6412
- $addons = $this->get_addons( $flush );
6413
-
6414
- if ( is_array( $addons ) ) {
6415
- foreach ( $addons as $addon ) {
6416
- if ( $slug === $addon->slug ) {
6417
- return $addon;
6418
- }
6419
- }
6420
- }
6421
-
6422
- return false;
6423
- }
6424
-
6425
- #----------------------------------------------------------------------------------
6426
- #region Plans & Licensing
6427
- #----------------------------------------------------------------------------------
6428
-
6429
- /**
6430
- * Check if running premium plugin code.
6431
- *
6432
- * @author Vova Feldman (@svovaf)
6433
- * @since 1.0.5
6434
- *
6435
- * @return bool
6436
- */
6437
- function is_premium() {
6438
- return $this->_plugin->is_premium;
6439
- }
6440
-
6441
- /**
6442
- * Get site's plan ID.
6443
- *
6444
- * @author Vova Feldman (@svovaf)
6445
- * @since 1.0.2
6446
- *
6447
- * @return number
6448
- */
6449
- function get_plan_id() {
6450
- return $this->_site->plan->id;
6451
- }
6452
-
6453
- /**
6454
- * Get site's plan title.
6455
- *
6456
- * @author Vova Feldman (@svovaf)
6457
- * @since 1.0.2
6458
- *
6459
- * @return string
6460
- */
6461
- function get_plan_title() {
6462
- return $this->_site->plan->title;
6463
- }
6464
-
6465
- /**
6466
- * @author Vova Feldman (@svovaf)
6467
- * @since 1.0.9
6468
- *
6469
- * @return FS_Plugin_Plan|false
6470
- */
6471
- function get_plan() {
6472
- return is_object( $this->_site->plan ) ?
6473
- $this->_site->plan :
6474
- false;
6475
- }
6476
-
6477
- /**
6478
- * @author Vova Feldman (@svovaf)
6479
- * @since 1.0.3
6480
- *
6481
- * @return bool
6482
- */
6483
- function is_trial() {
6484
- $this->_logger->entrance();
6485
-
6486
- if ( ! $this->is_registered() ) {
6487
- return false;
6488
- }
6489
-
6490
- return $this->_site->is_trial();
6491
- }
6492
-
6493
- /**
6494
- * Check if currently in a trial with payment method (credit card or paypal).
6495
- *
6496
- * @author Vova Feldman (@svovaf)
6497
- * @since 1.1.7
6498
- *
6499
- * @return bool
6500
- */
6501
- function is_paid_trial() {
6502
- $this->_logger->entrance();
6503
-
6504
- if ( ! $this->is_trial() ) {
6505
- return false;
6506
- }
6507
-
6508
- return $this->has_active_valid_license() && ( $this->_site->trial_plan_id == $this->_license->plan_id );
6509
- }
6510
-
6511
- /**
6512
- * Check if trial already utilized.
6513
- *
6514
- * @since 1.0.9
6515
- *
6516
- * @return bool
6517
- */
6518
- function is_trial_utilized() {
6519
- $this->_logger->entrance();
6520
-
6521
- if ( ! $this->is_registered() ) {
6522
- return false;
6523
- }
6524
-
6525
- return $this->_site->is_trial_utilized();
6526
- }
6527
-
6528
- /**
6529
- * Get trial plan information (if in trial).
6530
- *
6531
- * @author Vova Feldman (@svovaf)
6532
- * @since 1.0.9
6533
- *
6534
- * @return bool|FS_Plugin_Plan
6535
- */
6536
- function get_trial_plan() {
6537
- $this->_logger->entrance();
6538
-
6539
- if ( ! $this->is_trial() ) {
6540
- return false;
6541
- }
6542
-
6543
- return $this->_storage->trial_plan;
6544
- }
6545
-
6546
- /**
6547
- * Check if the user has an activate, non-expired license on current plugin's install.
6548
- *
6549
- * @since 1.0.9
6550
- *
6551
- * @return bool
6552
- */
6553
- function is_paying() {
6554
- $this->_logger->entrance();
6555
-
6556
- if ( ! $this->is_registered() ) {
6557
- return false;
6558
- }
6559
-
6560
- if ( ! $this->has_paid_plan() ) {
6561
- return false;
6562
- }
6563
-
6564
- return (
6565
- ! $this->is_trial() &&
6566
- 'free' !== $this->_site->plan->name &&
6567
- $this->has_active_valid_license()
6568
- );
6569
- }
6570
-
6571
- /**
6572
- * @author Vova Feldman (@svovaf)
6573
- * @since 1.0.4
6574
- *
6575
- * @return bool
6576
- */
6577
- function is_free_plan() {
6578
- if ( ! $this->is_registered() ) {
6579
- return true;
6580
- }
6581
-
6582
- if ( ! $this->has_paid_plan() ) {
6583
- return true;
6584
- }
6585
-
6586
- return (
6587
- 'free' === $this->_site->plan->name ||
6588
- ! $this->has_features_enabled_license()
6589
- );
6590
- }
6591
-
6592
- /**
6593
- * @author Vova Feldman (@svovaf)
6594
- * @since 1.0.5
6595
- *
6596
- * @return bool
6597
- */
6598
- function _has_premium_license() {
6599
- $this->_logger->entrance();
6600
-
6601
- $premium_license = $this->_get_available_premium_license();
6602
-
6603
- return ( false !== $premium_license );
6604
- }
6605
-
6606
- /**
6607
- * Check if user has any licenses associated with the plugin (including expired or blocking).
6608
- *
6609
- * @author Vova Feldman (@svovaf)
6610
- * @since 1.1.7.3
6611
- *
6612
- * @return bool
6613
- */
6614
- private function has_any_license() {
6615
- return is_array( $this->_licenses ) && ( 0 < count( $this->_licenses ) );
6616
- }
6617
-
6618
- /**
6619
- * @author Vova Feldman (@svovaf)
6620
- * @since 1.0.5
6621
- *
6622
- * @return FS_Plugin_License|false
6623
- */
6624
- function _get_available_premium_license() {
6625
- $this->_logger->entrance();
6626
-
6627
- if ( ! $this->has_paid_plan() ) {
6628
- return false;
6629
- }
6630
-
6631
- if ( is_array( $this->_licenses ) ) {
6632
- foreach ( $this->_licenses as $license ) {
6633
- if ( ! $license->is_utilized() && $license->is_features_enabled() ) {
6634
- return $license;
6635
- }
6636
- }
6637
- }
6638
-
6639
- return false;
6640
- }
6641
-
6642
- /**
6643
- * Sync local plugin plans with remote server.
6644
- *
6645
- * @author Vova Feldman (@svovaf)
6646
- * @since 1.0.5
6647
- *
6648
- * @return FS_Plugin_Plan[]|object
6649
- */
6650
- function _sync_plans() {
6651
- $plans = $this->_fetch_plugin_plans();
6652
-
6653
- if ( $this->is_array_instanceof( $plans, 'FS_Plugin_Plan' ) ) {
6654
- $this->_plans = $plans;
6655
- $this->_store_plans();
6656
- }
6657
-
6658
- $this->do_action( 'after_plans_sync', $plans );
6659
-
6660
- return $this->_plans;
6661
- }
6662
-
6663
- /**
6664
- * @author Vova Feldman (@svovaf)
6665
- * @since 1.0.5
6666
- *
6667
- * @param number $id
6668
- *
6669
- * @return FS_Plugin_Plan|false
6670
- */
6671
- function _get_plan_by_id( $id ) {
6672
- $this->_logger->entrance();
6673
-
6674
- if ( ! is_array( $this->_plans ) || 0 === count( $this->_plans ) ) {
6675
- $this->_sync_plans();
6676
- }
6677
-
6678
- foreach ( $this->_plans as $plan ) {
6679
- if ( $id == $plan->id ) {
6680
- return $plan;
6681
- }
6682
- }
6683
-
6684
- return false;
6685
- }
6686
-
6687
- /**
6688
- * @author Vova Feldman (@svovaf)
6689
- * @since 1.1.8.1
6690
- *
6691
- * @param string $name
6692
- *
6693
- * @return FS_Plugin_Plan|false
6694
- */
6695
- private function get_plan_by_name( $name ) {
6696
- $this->_logger->entrance();
6697
-
6698
- if ( ! is_array( $this->_plans ) || 0 === count( $this->_plans ) ) {
6699
- $this->_sync_plans();
6700
- }
6701
-
6702
- foreach ( $this->_plans as $plan ) {
6703
- if ( $name == $plan->name ) {
6704
- return $plan;
6705
- }
6706
- }
6707
-
6708
- return false;
6709
- }
6710
-
6711
- /**
6712
- * Sync local plugin plans with remote server.
6713
- *
6714
- * @author Vova Feldman (@svovaf)
6715
- * @since 1.0.6
6716
- *
6717
- * @param number|bool $site_license_id
6718
- *
6719
- * @return FS_Plugin_License[]|object
6720
- */
6721
- function _sync_licenses( $site_license_id = false ) {
6722
- $licenses = $this->_fetch_licenses( false, $site_license_id );
6723
-
6724
- if ( $this->is_array_instanceof( $licenses, 'FS_Plugin_License' ) ) {
6725
- $this->_licenses = $licenses;
6726
- $this->_store_licenses();
6727
- }
6728
-
6729
- // Update current license.
6730
- if ( is_object( $this->_license ) ) {
6731
- $this->_license = $this->_get_license_by_id( $this->_license->id );
6732
- }
6733
-
6734
- return $this->_licenses;
6735
- }
6736
-
6737
- /**
6738
- * @author Vova Feldman (@svovaf)
6739
- * @since 1.0.5
6740
- *
6741
- * @param number $id
6742
- *
6743
- * @return FS_Plugin_License|false
6744
- */
6745
- function _get_license_by_id( $id ) {
6746
- $this->_logger->entrance();
6747
-
6748
- if ( ! is_numeric( $id ) ) {
6749
- return false;
6750
- }
6751
-
6752
- if ( ! $this->has_any_license() ) {
6753
- $this->_sync_licenses();
6754
- }
6755
-
6756
- foreach ( $this->_licenses as $license ) {
6757
- if ( $id == $license->id ) {
6758
- return $license;
6759
- }
6760
- }
6761
-
6762
- return false;
6763
- }
6764
-
6765
- /**
6766
- * Sync site's license with user licenses.
6767
- *
6768
- * @author Vova Feldman (@svovaf)
6769
- * @since 1.0.6
6770
- *
6771
- * @param FS_Plugin_License|null $new_license
6772
- */
6773
- function _update_site_license( $new_license ) {
6774
- $this->_logger->entrance();
6775
-
6776
- $this->_license = $new_license;
6777
-
6778
- if ( ! is_object( $new_license ) ) {
6779
- $this->_site->license_id = null;
6780
- $this->_sync_site_subscription( null );
6781
-
6782
- return;
6783
- }
6784
-
6785
- $this->_site->license_id = $this->_license->id;
6786
-
6787
- if ( ! is_array( $this->_licenses ) ) {
6788
- $this->_licenses = array();
6789
- }
6790
-
6791
- $is_license_found = false;
6792
- for ( $i = 0, $len = count( $this->_licenses ); $i < $len; $i ++ ) {
6793
- if ( $new_license->id == $this->_licenses[ $i ]->id ) {
6794
- $this->_licenses[ $i ] = $new_license;
6795
-
6796
- $is_license_found = true;
6797
- break;
6798
- }
6799
- }
6800
-
6801
- // If new license just append.
6802
- if ( ! $is_license_found ) {
6803
- $this->_licenses[] = $new_license;
6804
- }
6805
-
6806
- $this->_sync_site_subscription( $new_license );
6807
- }
6808
-
6809
- /**
6810
- * Sync site's subscription.
6811
- *
6812
- * @author Vova Feldman (@svovaf)
6813
- * @since 1.0.9
6814
- *
6815
- * @param FS_Plugin_License|null $license
6816
- *
6817
- * @return bool|\FS_Subscription
6818
- */
6819
- private function _sync_site_subscription( $license ) {
6820
- if ( ! is_object( $license ) ) {
6821
- unset( $this->_storage->subscription );
6822
-
6823
- return false;
6824
- }
6825
-
6826
- // Load subscription details if not lifetime.
6827
- $subscription = $license->is_lifetime() ?
6828
- false :
6829
- $this->_fetch_site_license_subscription();
6830
-
6831
- if ( is_object( $subscription ) && ! isset( $subscription->error ) ) {
6832
- $this->_storage->subscription = $subscription;
6833
- } else {
6834
- unset( $this->_storage->subscription );
6835
- }
6836
-
6837
- return $subscription;
6838
- }
6839
-
6840
- /**
6841
- * @author Vova Feldman (@svovaf)
6842
- * @since 1.0.6
6843
- *
6844
- * @return bool|\FS_Plugin_License
6845
- */
6846
- function _get_license() {
6847
- return $this->_license;
6848
- }
6849
-
6850
- /**
6851
- * @return bool|\FS_Subscription
6852
- */
6853
- function _get_subscription() {
6854
- return isset( $this->_storage->subscription ) ?
6855
- $this->_storage->subscription :
6856
- false;
6857
- }
6858
-
6859
- /**
6860
- * @author Vova Feldman (@svovaf)
6861
- * @since 1.0.2
6862
- *
6863
- * @param string $plan Plan name
6864
- * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
6865
- *
6866
- * @return bool
6867
- */
6868
- function is_plan( $plan, $exact = false ) {
6869
- $this->_logger->entrance();
6870
-
6871
- if ( ! $this->is_registered() ) {
6872
- return false;
6873
- }
6874
-
6875
- $plan = strtolower( $plan );
6876
-
6877
- if ( $this->_site->plan->name === $plan ) // Exact plan.
6878
- {
6879
- return true;
6880
- } else if ( $exact ) // Required exact, but plans are different.
6881
- {
6882
- return false;
6883
- }
6884
-
6885
- $current_plan_order = - 1;
6886
- $required_plan_order = - 1;
6887
- for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
6888
- if ( $plan === $this->_plans[ $i ]->name ) {
6889
- $required_plan_order = $i;
6890
- } else if ( $this->_site->plan->name === $this->_plans[ $i ]->name ) {
6891
- $current_plan_order = $i;
6892
- }
6893
- }
6894
-
6895
- return ( $current_plan_order > $required_plan_order );
6896
- }
6897
-
6898
- /**
6899
- * Check if module has only one plan.
6900
- *
6901
- * @author Vova Feldman (@svovaf)
6902
- * @since 1.2.1.7
6903
- *
6904
- * @return bool
6905
- */
6906
- function is_single_plan() {
6907
- $this->_logger->entrance();
6908
-
6909
- if ( ! $this->is_registered() ||
6910
- ! is_array( $this->_plans ) ||
6911
- 0 === count( $this->_plans )
6912
- ) {
6913
- return true;
6914
- }
6915
-
6916
- return ( 1 === count( $this->_plans ) );
6917
- }
6918
-
6919
- /**
6920
- * Check if plan based on trial. If not in trial mode, should return false.
6921
- *
6922
- * @since 1.0.9
6923
- *
6924
- * @param string $plan Plan name
6925
- * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
6926
- *
6927
- * @return bool
6928
- */
6929
- function is_trial_plan( $plan, $exact = false ) {
6930
- $this->_logger->entrance();
6931
-
6932
- if ( ! $this->is_registered() ) {
6933
- return false;
6934
- }
6935
-
6936
- if ( ! $this->is_trial() ) {
6937
- return false;
6938
- }
6939
-
6940
- if ( ! isset( $this->_storage->trial_plan ) ) {
6941
- // Store trial plan information.
6942
- $this->_enrich_site_trial_plan( true );
6943
- }
6944
-
6945
- if ( $this->_storage->trial_plan->name === $plan ) // Exact plan.
6946
- {
6947
- return true;
6948
- } else if ( $exact ) // Required exact, but plans are different.
6949
- {
6950
- return false;
6951
- }
6952
-
6953
- $current_plan_order = - 1;
6954
- $required_plan_order = - 1;
6955
- for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
6956
- if ( $plan === $this->_plans[ $i ]->name ) {
6957
- $required_plan_order = $i;
6958
- } else if ( $this->_storage->trial_plan->name === $this->_plans[ $i ]->name ) {
6959
- $current_plan_order = $i;
6960
- }
6961
- }
6962
-
6963
- return ( $current_plan_order > $required_plan_order );
6964
- }
6965
-
6966
- /**
6967
- * Check if plugin has any paid plans.
6968
- *
6969
- * @author Vova Feldman (@svovaf)
6970
- * @since 1.0.7
6971
- *
6972
- * @return bool
6973
- */
6974
- function has_paid_plan() {
6975
- return $this->_has_paid_plans ||
6976
- FS_Plan_Manager::instance()->has_paid_plan( $this->_plans );
6977
- }
6978
-
6979
- /**
6980
- * Check if plugin has any plan with a trail.
6981
- *
6982
- * @author Vova Feldman (@svovaf)
6983
- * @since 1.0.9
6984
- *
6985
- * @return bool
6986
- */
6987
- function has_trial_plan() {
6988
- if ( ! $this->is_registered() ) {
6989
- /**
6990
- * @author Vova Feldman(@svovaf)
6991
- * @since 1.2.1.5
6992
- *
6993
- * Allow setting a trial from the SDK without calling the API.
6994
- * But, if the user did opt-in, continue using the real data from the API.
6995
- */
6996
- if ( $this->_trial_days >= 0 ) {
6997
- return true;
6998
- }
6999
-
7000
- return false;
7001
- }
7002
-
7003
- return $this->_storage->get( 'has_trial_plan', false );
7004
- }
7005
-
7006
- /**
7007
- * Check if plugin has any free plan, or is it premium only.
7008
- *
7009
- * Note: If no plans configured, assume plugin is free.
7010
- *
7011
- * @author Vova Feldman (@svovaf)
7012
- * @since 1.0.7
7013
- *
7014
- * @return bool
7015
- */
7016
- function has_free_plan() {
7017
- return ! $this->is_only_premium();
7018
- }
7019
-
7020
- /**
7021
- * Displays a license activation dialog box when the user clicks on the "Activate License"
7022
- * or "Change License" link on the plugins
7023
- * page.
7024
- *
7025
- * @author Leo Fajardo (@leorw)
7026
- * @since 1.1.9
7027
- */
7028
- function _add_license_activation_dialog_box() {
7029
- $vars = array(
7030
- 'id' => $this->_module_id,
7031
- );
7032
-
7033
- fs_require_template( 'forms/license-activation.php', $vars );
7034
- fs_require_template( 'forms/resend-key.php', $vars );
7035
- }
7036
-
7037
- /**
7038
- * Displays the opt-out dialog box when the user clicks on the "Opt Out" link on the "Plugins"
7039
- * page.
7040
- *
7041
- * @author Leo Fajardo (@leorw)
7042
- * @since 1.2.1.5
7043
- */
7044
- function _add_optout_dialog() {
7045
- if ( $this->is_theme() ) {
7046
- $vars = null;
7047
- fs_require_once_template( '/js/jquery.content-change.php', $vars );
7048
- }
7049
-
7050
- $vars = array( 'id' => $this->_module_id );
7051
- fs_require_template( 'forms/optout.php', $vars );
7052
- }
7053
-
7054
- /**
7055
- * Prepare page to include all required UI and logic for the license activation dialog.
7056
- *
7057
- * @author Vova Feldman (@svovaf)
7058
- * @since 1.2.0
7059
- */
7060
- function _add_license_activation() {
7061
- if ( ! $this->is_user_admin() ) {
7062
- // Only admins can activate a license.
7063
- return;
7064
- }
7065
-
7066
- if ( ! $this->has_paid_plan() ) {
7067
- // Module doesn't have any paid plans.
7068
- return;
7069
- }
7070
-
7071
- if ( ! $this->is_premium() ) {
7072
- // Only add license activation logic to the premium version.
7073
- return;
7074
- }
7075
-
7076
- // Add license activation link and AJAX request handler.
7077
- if ( self::is_plugins_page() ) {
7078
- /**
7079
- * @since 1.2.0 Add license action link only on plugins page.
7080
- */
7081
- $this->_add_license_action_link();
7082
- }
7083
-
7084
- // Add license activation AJAX callback.
7085
- $this->add_ajax_action( 'activate_license', array( &$this, '_activate_license_ajax_action' ) );
7086
-
7087
- // Add resend license AJAX callback.
7088
- $this->add_ajax_action( 'resend_license_key', array( &$this, '_resend_license_key_ajax_action' ) );
7089
- }
7090
-
7091
- /**
7092
- * @author Leo Fajardo (@leorw)
7093
- * @since 1.1.9
7094
- */
7095
- function _activate_license_ajax_action() {
7096
- $this->_logger->entrance();
7097
-
7098
- $this->check_ajax_referer( 'activate_license' );
7099
-
7100
- $license_key = trim( fs_request_get( 'license_key' ) );
7101
-
7102
- if ( empty( $license_key ) ) {
7103
- exit;
7104
- }
7105
-
7106
- $plugin_id = fs_request_get( 'module_id', '', 'post' );
7107
- $fs = ( $plugin_id == $this->_module_id ) ?
7108
- $this :
7109
- $this->get_addon_instance( $plugin_id );
7110
-
7111
- $error = false;
7112
- $next_page = false;
7113
-
7114
- if ( $fs->is_registered() ) {
7115
- $api = $fs->get_api_site_scope();
7116
- $install = $api->call( '/', 'put', array(
7117
- 'license_key' => $fs->apply_filters( 'license_key', $license_key )
7118
- ) );
7119
-
7120
- if ( isset( $install->error ) ) {
7121
- $error = $install->error->message;
7122
- } else {
7123
- $fs->_sync_license( true );
7124
 
7125
- $next_page = $fs->is_addon() ?
7126
- $fs->get_parent_instance()->get_account_url() :
7127
- $fs->get_account_url();
7128
 
7129
- $fs->reconnect_locally();
7130
- }
7131
- } else {
7132
- $next_page = $fs->opt_in( false, false, false, $license_key );
7133
-
7134
- if ( isset( $next_page->error ) ) {
7135
- $error = $next_page->error;
7136
- }
7137
- }
7138
-
7139
- $result = array(
7140
- 'success' => ( false === $error )
7141
- );
7142
-
7143
- if ( false !== $error ) {
7144
- $result['error'] = $error;
7145
- } else {
7146
- $result['next_page'] = $next_page;
7147
- }
7148
-
7149
- echo json_encode( $result );
7150
-
7151
- exit;
7152
- }
7153
-
7154
- /**
7155
- * Billing update AJAX callback.
7156
- *
7157
- * @author Vova Feldman (@svovaf)
7158
- * @since 1.2.1.5
7159
- */
7160
- function _update_billing_ajax_action() {
7161
- $this->_logger->entrance();
7162
-
7163
- $this->check_ajax_referer( 'update_billing' );
7164
-
7165
- if ( ! $this->is_user_admin() ) {
7166
- // Only for admins.
7167
- self::shoot_ajax_failure();
7168
- }
7169
-
7170
- $billing = fs_request_get( 'billing' );
7171
-
7172
- $api = $this->get_api_user_scope();
7173
- $result = $api->call( '/billing.json', 'put', array_merge( $billing, array(
7174
- 'plugin_id' => $this->get_parent_id(),
7175
- ) ) );
7176
-
7177
- if ( ! $this->is_api_result_entity( $result ) ) {
7178
- self::shoot_ajax_failure();
7179
- }
7180
-
7181
- // Purge cached billing.
7182
- $this->get_api_user_scope()->purge_cache( 'billing.json' );
7183
-
7184
- self::shoot_ajax_success();
7185
- }
7186
-
7187
- /**
7188
- * Trial start for anonymous users (AJAX callback).
7189
- *
7190
- * @author Vova Feldman (@svovaf)
7191
- * @since 1.2.1.5
7192
- */
7193
- function _start_trial_ajax_action() {
7194
- $this->_logger->entrance();
7195
-
7196
- $this->check_ajax_referer( 'start_trial' );
7197
-
7198
- if ( ! $this->is_user_admin() ) {
7199
- // Only for admins.
7200
- self::shoot_ajax_failure();
7201
- }
7202
-
7203
- $trial_data = fs_request_get( 'trial' );
7204
-
7205
- $next_page = $this->opt_in(
7206
- false,
7207
- false,
7208
- false,
7209
- false,
7210
- false,
7211
- $trial_data['plan_id']
7212
- );
7213
-
7214
- if ( is_object( $next_page ) && $this->is_api_error( $next_page ) ) {
7215
- self::shoot_ajax_failure(
7216
- isset( $next_page->error ) ?
7217
- $next_page->error->message :
7218
- var_export( $next_page, true )
7219
- );
7220
- }
7221
-
7222
- $this->shoot_ajax_success( array(
7223
- 'next_page' => $next_page,
7224
- ) );
7225
- }
7226
-
7227
- /**
7228
- * @author Leo Fajardo (@leorw)
7229
- * @since 1.2.0
7230
- */
7231
- function _resend_license_key_ajax_action() {
7232
- $this->_logger->entrance();
7233
-
7234
- $this->check_ajax_referer( 'resend_license_key' );
7235
-
7236
- $email_address = sanitize_email( trim( fs_request_get( 'email', '', 'post' ) ) );
7237
-
7238
- if ( empty( $email_address ) ) {
7239
- exit;
7240
- }
7241
-
7242
- $error = false;
7243
-
7244
- $api = $this->get_api_plugin_scope();
7245
- $result = $api->call( '/licenses/resend.json', 'post',
7246
- array(
7247
- 'email' => $email_address,
7248
- 'url' => home_url(),
7249
- )
7250
- );
7251
-
7252
- if ( is_object( $result ) && isset( $result->error ) ) {
7253
- $error = $result->error;
7254
-
7255
- if ( in_array( $error->code, array( 'invalid_email', 'no_user' ) ) ) {
7256
- $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' );
7257
- } else if ( 'no_license' === $error->code ) {
7258
- $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' );
7259
- } else {
7260
- $error = $error->message;
7261
- }
7262
- }
7263
-
7264
- $licenses = array(
7265
- 'success' => ( false === $error )
7266
- );
7267
-
7268
- if ( false !== $error ) {
7269
- $licenses['error'] = sprintf( '%s... %s', $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ), strtolower( $error ) );
7270
- }
7271
-
7272
- echo json_encode( $licenses );
7273
-
7274
- exit;
7275
- }
7276
-
7277
- /**
7278
- * @author Vova Feldman (@svovaf)
7279
- * @since 1.2.1.8
7280
- *
7281
- * @var string
7282
- */
7283
- private static $_pagenow;
7284
-
7285
- /**
7286
- * Get current page or the referer if executing a WP AJAX request.
7287
- *
7288
- * @author Vova Feldman (@svovaf)
7289
- * @since 1.2.1.8
7290
- *
7291
- * @return string
7292
- */
7293
- static function get_current_page() {
7294
- if ( ! isset( self::$_pagenow ) ) {
7295
- global $pagenow;
7296
-
7297
- self::$_pagenow = $pagenow;
7298
-
7299
- if ( self::is_ajax() &&
7300
- 'admin-ajax.php' === $pagenow
7301
- ) {
7302
- $referer = fs_get_raw_referer();
7303
-
7304
- if ( is_string( $referer ) ) {
7305
- $parts = explode( '?', $referer );
7306
-
7307
- self::$_pagenow = basename( $parts[0] );
7308
- }
7309
- }
7310
- }
7311
-
7312
- return self::$_pagenow;
7313
- }
7314
-
7315
- /**
7316
- * Helper method to check if user in the plugins page.
7317
- *
7318
- * @author Vova Feldman (@svovaf)
7319
- * @since 1.2.1.5
7320
- *
7321
- * @return bool
7322
- */
7323
- static function is_plugins_page() {
7324
- return ( 'plugins.php' === self::get_current_page() );
7325
- }
7326
-
7327
- /**
7328
- * Helper method to check if user in the themes page.
7329
- *
7330
- * @author Vova Feldman (@svovaf)
7331
- * @since 1.2.2.6
7332
- *
7333
- * @return bool
7334
- */
7335
- static function is_themes_page() {
7336
- return ( 'themes.php' === self::get_current_page() );
7337
- }
7338
-
7339
- #----------------------------------------------------------------------------------
7340
- #region Affiliation
7341
- #----------------------------------------------------------------------------------
7342
-
7343
- /**
7344
- * @author Leo Fajardo
7345
- * @since 1.2.3
7346
- *
7347
- * @return bool
7348
- */
7349
- function has_affiliate_program() {
7350
- if ( ! is_object( $this->_plugin ) ) {
7351
- return false;
7352
  }
7353
 
7354
- return $this->_plugin->has_affiliate_program();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7355
  }
7356
 
7357
  /**
7358
  * @author Leo Fajardo (@leorw)
7359
- * @since 1.2.3
 
 
 
7360
  */
7361
- private function fetch_affiliate_and_terms() {
7362
- $this->_logger->entrance();
 
 
 
7363
 
7364
- if ( ! is_object( $this->plugin_affiliate_terms ) ) {
7365
- $plugins_api = $this->get_api_plugin_scope();
7366
- $affiliate_terms = $plugins_api->get( '/aff.json?type=affiliation', true );
 
7367
 
7368
- if ( ! $this->is_api_result_entity( $affiliate_terms ) ) {
7369
- return;
7370
- }
7371
 
7372
- $this->plugin_affiliate_terms = new FS_AffiliateTerms( $affiliate_terms );
 
 
 
 
7373
  }
7374
 
7375
- if ( $this->is_registered() ) {
7376
- $users_api = $this->get_api_user_scope();
7377
- $result = $users_api->get( "/plugins/{$this->_plugin->id}/aff/{$this->plugin_affiliate_terms->id}/affiliates.json", true );
7378
- if ( $this->is_api_result_object( $result, 'affiliates' ) ) {
7379
- if ( ! empty( $result->affiliates ) ) {
7380
- $affiliate = new FS_Affiliate( $result->affiliates[0] );
7381
 
7382
- if ( ! $affiliate->is_pending() && ! empty( $this->_storage->affiliate_application_data ) ) {
7383
- unset( $this->_storage->affiliate_application_data );
7384
- }
7385
 
7386
- if ( $affiliate->is_using_custom_terms ) {
7387
- $affiliate_terms = $users_api->get( "/plugins/{$this->_plugin->id}/affiliates/{$affiliate->id}/aff/{$affiliate->custom_affiliate_terms_id}.json", true );
7388
- if ( $this->is_api_result_entity( $affiliate_terms ) ) {
7389
- $this->custom_affiliate_terms = new FS_AffiliateTerms( $affiliate_terms );
7390
- }
7391
- }
7392
 
7393
- $this->affiliate = $affiliate;
7394
- }
7395
- }
 
 
 
 
7396
  }
7397
  }
7398
 
7399
  /**
7400
- * @author Leo Fajardo
7401
- * @since 1.2.3
7402
- *
7403
- * @return FS_Affiliate
7404
  */
7405
- function get_affiliate() {
7406
- return $this->affiliate;
7407
- }
7408
-
7409
 
7410
- /**
7411
- * @author Leo Fajardo
7412
- * @since 1.2.3
7413
- *
7414
- * @return FS_AffiliateTerms
7415
- */
7416
- function get_affiliate_terms() {
7417
- return is_object( $this->custom_affiliate_terms ) ?
7418
- $this->custom_affiliate_terms :
7419
- $this->plugin_affiliate_terms;
7420
  }
7421
 
7422
  /**
7423
- * @author Leo Fajardo
7424
- * @since 1.2.3
7425
- *
7426
- * @return FS_Affiliate|null
7427
  */
7428
- function _submit_affiliate_application() {
7429
- $this->_logger->entrance();
 
 
 
 
 
7430
 
7431
- $this->check_ajax_referer( 'submit_affiliate_application' );
 
 
 
 
 
7432
 
7433
- if ( ! $this->is_user_admin() ) {
7434
- // Only for admins.
7435
- self::shoot_ajax_failure();
7436
  }
7437
 
7438
- $affiliate = fs_request_get( 'affiliate' );
 
7439
 
7440
- if ( empty( $affiliate['promotion_methods'] ) ) {
7441
- unset( $affiliate['promotion_methods'] );
7442
- }
 
 
7443
 
7444
- if ( ! empty( $affiliate['additional_domains'] ) ) {
7445
- $affiliate['additional_domains'] = array_unique( $affiliate['additional_domains'] );
 
 
 
7446
  }
7447
 
7448
- if ( ! $this->is_registered() ) {
7449
- // Opt in but don't track usage.
7450
- $next_page = $this->opt_in(
7451
- false,
7452
- false,
7453
- false,
7454
- false,
7455
- false,
7456
- false,
7457
- true
7458
- );
7459
 
7460
- if ( is_object( $next_page ) && $this->is_api_error( $next_page ) ) {
7461
- self::shoot_ajax_failure(
7462
- isset( $next_page->error ) ?
7463
- $next_page->error->message :
7464
- var_export( $next_page, true )
7465
- );
7466
- } else if ( $this->is_pending_activation() ) {
7467
- self::shoot_ajax_failure( $this->get_text_inline( 'Account is pending activation.', 'account-is-pending-activation' ) );
7468
  }
7469
  }
7470
 
7471
- $api = $this->get_api_user_scope();
7472
- $result = $api->call(
7473
- ( "/plugins/{$this->_plugin->id}/aff/{$this->plugin_affiliate_terms->id}/affiliates.json" ),
7474
- 'post',
7475
- $affiliate
7476
  );
7477
 
7478
- if ( $this->is_api_error( $result ) ) {
7479
- self::shoot_ajax_failure(
7480
- isset( $result->error ) ?
7481
- $result->error->message :
7482
- var_export( $result, true )
7483
- );
7484
- }
7485
- else
7486
- {
7487
- if ( $this->_admin_notices->has_sticky( 'affiliate_program' ) ) {
7488
- $this->_admin_notices->remove_sticky( 'affiliate_program' );
7489
- }
7490
-
7491
- $affiliate_application_data = array(
7492
- 'stats_description' => $affiliate['stats_description'],
7493
- 'promotion_method_description' => $affiliate['promotion_method_description'],
7494
- );
7495
 
7496
- if ( ! empty( $affiliate['promotion_methods'] ) ) {
7497
- $affiliate_application_data['promotion_methods'] = $affiliate['promotion_methods'];
7498
- }
 
 
 
 
 
7499
 
7500
- if ( ! empty( $affiliate['domain'] ) ) {
7501
- $affiliate_application_data['domain'] = $affiliate['domain'];
7502
- }
7503
 
7504
- if ( ! empty( $affiliate['additional_domains'] ) ) {
7505
- $affiliate_application_data['additional_domains'] = $affiliate['additional_domains'];
7506
- }
7507
 
7508
- $this->_storage->affiliate_application_data = $affiliate_application_data;
 
 
 
 
 
 
 
7509
  }
7510
 
7511
- // Purge cached affiliate.
7512
- $api->purge_cache( 'affiliate.json' );
 
 
 
 
 
 
 
7513
 
7514
- self::shoot_ajax_success( $result );
 
 
7515
  }
7516
 
7517
  /**
7518
- * @author Leo Fajardo
7519
- * @since 1.2.3
7520
  *
7521
- * @return array|null
 
7522
  */
7523
- function get_affiliate_application_data() {
7524
- if ( empty( $this->_storage->affiliate_application_data ) ) {
7525
- return null;
 
 
 
7526
  }
7527
-
7528
- return $this->_storage->affiliate_application_data;
7529
  }
7530
 
7531
- #endregion Affiliation ------------------------------------------------------------
7532
-
7533
- #----------------------------------------------------------------------------------
7534
- #region URL Generators
7535
- #----------------------------------------------------------------------------------
7536
-
7537
- /**
7538
- * Alias to pricing_url().
7539
- *
7540
- * @author Vova Feldman (@svovaf)
7541
- * @since 1.0.2
7542
- *
7543
- * @uses pricing_url()
7544
- *
7545
- * @param string $period Billing cycle
7546
- * @param bool $is_trial
7547
- *
7548
- * @return string
7549
- */
7550
- function get_upgrade_url( $period = WP_FS__PERIOD_ANNUALLY, $is_trial = false ) {
7551
- return $this->pricing_url( $period, $is_trial );
7552
- }
7553
-
7554
- /**
7555
- * @author Vova Feldman (@svovaf)
7556
- * @since 1.0.9
7557
- *
7558
- * @uses get_upgrade_url()
7559
- *
7560
- * @return string
7561
- */
7562
- function get_trial_url() {
7563
- return $this->get_upgrade_url( WP_FS__PERIOD_ANNUALLY, true );
7564
- }
7565
-
7566
- /**
7567
- * Plugin's pricing URL.
7568
- *
7569
- * @author Vova Feldman (@svovaf)
7570
- * @since 1.0.4
7571
- *
7572
- * @param string $billing_cycle Billing cycle
7573
- *
7574
- * @param bool $is_trial
7575
- *
7576
- * @return string
7577
- */
7578
- function pricing_url( $billing_cycle = WP_FS__PERIOD_ANNUALLY, $is_trial = false ) {
7579
- $this->_logger->entrance();
7580
-
7581
- $params = array(
7582
- 'billing_cycle' => $billing_cycle
7583
- );
7584
-
7585
- if ( $is_trial ) {
7586
- $params['trial'] = 'true';
7587
- }
7588
-
7589
- return $this->_get_admin_page_url( 'pricing', $params );
7590
- }
7591
-
7592
- /**
7593
- * Checkout page URL.
7594
- *
7595
- * @author Vova Feldman (@svovaf)
7596
- * @since 1.0.6
7597
- *
7598
- * @param string $billing_cycle Billing cycle
7599
- * @param bool $is_trial
7600
- * @param array $extra (optional) Extra parameters, override other query params.
7601
- *
7602
- * @return string
7603
- */
7604
- function checkout_url(
7605
- $billing_cycle = WP_FS__PERIOD_ANNUALLY,
7606
- $is_trial = false,
7607
- $extra = array()
7608
- ) {
7609
- $this->_logger->entrance();
7610
-
7611
- $params = array(
7612
- 'checkout' => 'true',
7613
- 'billing_cycle' => $billing_cycle,
7614
- );
7615
-
7616
- if ( $is_trial ) {
7617
- $params['trial'] = 'true';
7618
- }
7619
-
7620
- /**
7621
- * Params in extra override other params.
7622
- */
7623
- $params = array_merge( $params, $extra );
7624
-
7625
- return $this->_get_admin_page_url( 'pricing', $params );
7626
- }
7627
-
7628
- /**
7629
- * Add-on checkout URL.
7630
- *
7631
- * @author Vova Feldman (@svovaf)
7632
- * @since 1.1.7
7633
- *
7634
- * @param number $addon_id
7635
- * @param number $pricing_id
7636
- * @param string $billing_cycle
7637
- * @param bool $is_trial
7638
- *
7639
- * @return string
7640
- */
7641
- function addon_checkout_url(
7642
- $addon_id,
7643
- $pricing_id,
7644
- $billing_cycle = WP_FS__PERIOD_ANNUALLY,
7645
- $is_trial = false
7646
- ) {
7647
- return $this->checkout_url( $billing_cycle, $is_trial, array(
7648
- 'plugin_id' => $addon_id,
7649
- 'pricing_id' => $pricing_id,
7650
- ) );
7651
- }
7652
-
7653
- #endregion
7654
-
7655
- #endregion ------------------------------------------------------------------
7656
-
7657
- /**
7658
- * Check if plugin has any add-ons.
7659
- *
7660
- * @author Vova Feldman (@svovaf)
7661
- * @since 1.0.5
7662
- *
7663
- * @since 1.1.7.3 Base logic only on the parameter provided by the developer in the init function.
7664
- *
7665
- * @return bool
7666
- */
7667
- function has_addons() {
7668
- $this->_logger->entrance();
7669
-
7670
- return $this->_has_addons;
7671
- }
7672
-
7673
- /**
7674
- * Check if plugin can work in anonymous mode.
7675
- *
7676
- * @author Vova Feldman (@svovaf)
7677
- * @since 1.0.9
7678
- *
7679
- * @return bool
7680
- *
7681
- * @deprecated Please use is_enable_anonymous() instead
7682
- */
7683
- function enable_anonymous() {
7684
- return $this->_enable_anonymous;
7685
- }
7686
-
7687
- /**
7688
- * Check if plugin can work in anonymous mode.
7689
- *
7690
- * @author Vova Feldman (@svovaf)
7691
- * @since 1.1.9
7692
- *
7693
- * @return bool
7694
- */
7695
- function is_enable_anonymous() {
7696
- return $this->_enable_anonymous;
7697
- }
7698
-
7699
- /**
7700
- * Check if plugin is premium only (no free plans).
7701
- *
7702
- * @author Vova Feldman (@svovaf)
7703
- * @since 1.1.9
7704
- *
7705
- * @return bool
7706
- */
7707
- function is_only_premium() {
7708
- return $this->_is_premium_only;
7709
- }
7710
-
7711
- /**
7712
- * Checks if the plugin's type is "plugin". The other type is "theme".
7713
- *
7714
- * @author Leo Fajardo (@leorw)
7715
- * @since 1.2.2
7716
- *
7717
- * @return bool
7718
- */
7719
- function is_plugin() {
7720
- return ( WP_FS__MODULE_TYPE_PLUGIN === $this->_module_type );
7721
- }
7722
-
7723
- /**
7724
- * @author Leo Fajardo (@leorw)
7725
- * @since 1.2.2
7726
- *
7727
- * @return string
7728
- */
7729
- function get_module_type() {
7730
- if ( ! isset( $this->_module_type ) ) {
7731
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
7732
- $this->_module_type = $id_slug_type_path_map[ $this->_module_id ]['type'];
7733
- }
7734
-
7735
- return $this->_module_type;
7736
- }
7737
-
7738
- /**
7739
- * @author Leo Fajardo (@leorw)
7740
- * @since 1.2.2
7741
- *
7742
- * @return string
7743
- */
7744
- function get_plugin_main_file_path() {
7745
- return $this->_plugin_main_file_path;
7746
- }
7747
-
7748
- /**
7749
- * Check if module has a premium code version.
7750
- *
7751
- * Serviceware module might be freemium without any
7752
- * premium code version, where the paid features
7753
- * are all part of the service.
7754
- *
7755
- * @author Vova Feldman (@svovaf)
7756
- * @since 1.2.1.6
7757
- *
7758
- * @return bool
7759
- */
7760
- function has_premium_version() {
7761
- return $this->_has_premium_version;
7762
- }
7763
-
7764
- /**
7765
- * Check if feature supported with current site's plan.
7766
- *
7767
- * @author Vova Feldman (@svovaf)
7768
- * @since 1.0.1
7769
- *
7770
- * @todo IMPLEMENT
7771
- *
7772
- * @param number $feature_id
7773
- *
7774
- * @throws Exception
7775
- */
7776
- function is_feature_supported( $feature_id ) {
7777
- throw new Exception( 'not implemented' );
7778
- }
7779
-
7780
- /**
7781
- * @author Vova Feldman (@svovaf)
7782
- * @since 1.0.1
7783
- *
7784
- * @return bool Is running in SSL/HTTPS
7785
- */
7786
- function is_ssl() {
7787
- return WP_FS__IS_HTTPS;
7788
- }
7789
-
7790
- /**
7791
- * @author Vova Feldman (@svovaf)
7792
- * @since 1.0.9
7793
- *
7794
- * @return bool Is running in AJAX call.
7795
- *
7796
- * @link http://wordpress.stackexchange.com/questions/70676/how-to-check-if-i-am-in-admin-ajax
7797
- */
7798
- static function is_ajax() {
7799
- return ( defined( 'DOING_AJAX' ) && DOING_AJAX );
7800
- }
7801
-
7802
- /**
7803
- * Check if it's an AJAX call targeted for the current module.
7804
- *
7805
- * @author Vova Feldman (@svovaf)
7806
- * @since 1.2.0
7807
- *
7808
- * @param array|string $actions Collection of AJAX actions.
7809
- *
7810
- * @return bool
7811
- */
7812
- function is_ajax_action( $actions ) {
7813
- // Verify it's an ajax call.
7814
- if ( ! self::is_ajax() ) {
7815
- return false;
7816
- }
7817
-
7818
- // Verify the call is relevant for the plugin.
7819
- if ( $this->_module_id != fs_request_get( 'module_id' ) ) {
7820
- return false;
7821
- }
7822
-
7823
- // Verify it's one of the specified actions.
7824
- if ( is_string( $actions ) ) {
7825
- $actions = explode( ',', $actions );
7826
- }
7827
-
7828
- if ( is_array( $actions ) && 0 < count( $actions ) ) {
7829
- $ajax_action = fs_request_get( 'action' );
7830
-
7831
- foreach ( $actions as $action ) {
7832
- if ( $ajax_action === $this->get_action_tag( $action ) ) {
7833
- return true;
7834
- }
7835
- }
7836
- }
7837
-
7838
- return false;
7839
- }
7840
-
7841
- /**
7842
- * Check if it's an AJAX call targeted for current request.
7843
- *
7844
- * @author Vova Feldman (@svovaf)
7845
- * @since 1.2.0
7846
- *
7847
- * @param array|string $actions Collection of AJAX actions.
7848
- * @param number|null $module_id
7849
- *
7850
- * @return bool
7851
- */
7852
- static function is_ajax_action_static( $actions, $module_id = null ) {
7853
- // Verify it's an ajax call.
7854
- if ( ! self::is_ajax() ) {
7855
- return false;
7856
- }
7857
-
7858
-
7859
- if ( ! empty( $module_id ) ) {
7860
- // Verify the call is relevant for the plugin.
7861
- if ( $module_id != fs_request_get( 'module_id' ) ) {
7862
- return false;
7863
- }
7864
- }
7865
-
7866
- // Verify it's one of the specified actions.
7867
- if ( is_string( $actions ) ) {
7868
- $actions = explode( ',', $actions );
7869
- }
7870
-
7871
- if ( is_array( $actions ) && 0 < count( $actions ) ) {
7872
- $ajax_action = fs_request_get( 'action' );
7873
-
7874
- foreach ( $actions as $action ) {
7875
- if ( $ajax_action === self::get_ajax_action_static( $action, $module_id ) ) {
7876
- return true;
7877
- }
7878
- }
7879
- }
7880
-
7881
- return false;
7882
- }
7883
-
7884
- /**
7885
- * @author Vova Feldman (@svovaf)
7886
- * @since 1.1.7
7887
- *
7888
- * @return bool
7889
- */
7890
- static function is_cron() {
7891
- return ( defined( 'DOING_CRON' ) && DOING_CRON );
7892
- }
7893
-
7894
- /**
7895
- * Check if a real user is visiting the admin dashboard.
7896
- *
7897
- * @author Vova Feldman (@svovaf)
7898
- * @since 1.1.7
7899
- *
7900
- * @return bool
7901
- */
7902
- function is_user_in_admin() {
7903
- return is_admin() && ! self::is_ajax() && ! self::is_cron();
7904
- }
7905
-
7906
- /**
7907
- * Check if a real user is in the customizer view.
7908
- *
7909
- * @author Vova Feldman (@svovaf)
7910
- * @since 1.2.2.7
7911
- *
7912
- * @return bool
7913
- */
7914
- static function is_customizer() {
7915
- return is_customize_preview();
7916
- }
7917
-
7918
- /**
7919
- * Check if running in HTTPS and if site's plan matching the specified plan.
7920
- *
7921
- * @param string $plan
7922
- * @param bool $exact
7923
- *
7924
- * @return bool
7925
- */
7926
- function is_ssl_and_plan( $plan, $exact = false ) {
7927
- return ( $this->is_ssl() && $this->is_plan( $plan, $exact ) );
7928
- }
7929
-
7930
- /**
7931
- * Construct plugin's settings page URL.
7932
- *
7933
- * @author Vova Feldman (@svovaf)
7934
- * @since 1.0.4
7935
- *
7936
- * @param string $page
7937
- * @param array $params
7938
- *
7939
- * @return string
7940
- */
7941
- function _get_admin_page_url( $page = '', $params = array() ) {
7942
- if ( 0 < count( $params ) ) {
7943
- foreach ( $params as $k => $v ) {
7944
- $params[ $k ] = urlencode( $v );
7945
- }
7946
- }
7947
-
7948
- $page_param = $this->_menu->get_slug( $page );
7949
-
7950
- if ( empty( $page ) &&
7951
- $this->is_theme() &&
7952
- // Show the opt-in as an overlay for free wp.org themes or themes without any settings page.
7953
- ( $this->is_free_wp_org_theme() || ! $this->has_settings_menu() ) ) {
7954
- $params[ $this->get_unique_affix() . '_show_optin' ] = 'true';
7955
-
7956
- return add_query_arg(
7957
- $params,
7958
- admin_url( 'themes.php' )
7959
- );
7960
- }
7961
-
7962
- if ( ! $this->has_settings_menu() ) {
7963
- if ( ! empty( $page ) ) {
7964
- // Module doesn't have a setting page, but since the request is for
7965
- // a specific Freemius page, use the admin.php path.
7966
- return add_query_arg( array_merge( $params, array(
7967
- 'page' => $page_param,
7968
- ) ), admin_url( 'admin.php' ) );
7969
- } else {
7970
- if ( $this->is_activation_mode() ) {
7971
- /**
7972
- * @author Vova Feldman
7973
- * @since 1.2.1.6
7974
- *
7975
- * If plugin doesn't have a settings page, create one for the opt-in screen.
7976
- */
7977
- return add_query_arg( array_merge( $params, array(
7978
- 'page' => $this->_slug,
7979
- ) ), admin_url( 'admin.php', 'admin' ) );
7980
- } else {
7981
- // Plugin without a settings page.
7982
- return add_query_arg(
7983
- $params,
7984
- admin_url( 'plugins.php' )
7985
- );
7986
- }
7987
- }
7988
- }
7989
-
7990
- // Module has a submenu settings page.
7991
- if ( ! $this->_menu->is_top_level() ) {
7992
- $parent_slug = $this->_menu->get_parent_slug();
7993
- $menu_file = ( false !== strpos( $parent_slug, '.php' ) ) ?
7994
- $parent_slug :
7995
- 'admin.php';
7996
-
7997
- return add_query_arg( array_merge( $params, array(
7998
- 'page' => $page_param,
7999
- ) ), admin_url( $menu_file, 'admin' ) );
8000
- }
8001
-
8002
- // Module has a top level CPT settings page.
8003
- if ( $this->_menu->is_cpt() ) {
8004
- if ( empty( $page ) && $this->is_activation_mode() ) {
8005
- return add_query_arg( array_merge( $params, array(
8006
- 'page' => $page_param
8007
- ) ), admin_url( 'admin.php', 'admin' ) );
8008
- } else {
8009
- if ( ! empty( $page ) ) {
8010
- $params['page'] = $page_param;
8011
- }
8012
-
8013
- return add_query_arg(
8014
- $params,
8015
- admin_url( $this->_menu->get_raw_slug(), 'admin' )
8016
- );
8017
- }
8018
- }
8019
-
8020
- // Module has a custom top level settings page.
8021
- return add_query_arg( array_merge( $params, array(
8022
- 'page' => $page_param,
8023
- ) ), admin_url( 'admin.php', 'admin' ) );
8024
- }
8025
-
8026
- /**
8027
- * Check if currently in a specified admin page.
8028
- *
8029
- * @author Vova Feldman (@svovaf)
8030
- * @since 1.2.2.7
8031
- *
8032
- * @param string $page
8033
- *
8034
- * @return bool
8035
- */
8036
- function is_admin_page( $page ) {
8037
- return ( $this->_menu->get_slug( $page ) === fs_request_get( 'page', '', 'get' ) );
8038
- }
8039
-
8040
- /**
8041
- * Get module's main admin setting page URL.
8042
- *
8043
- * @author Vova Feldman (@svovaf)
8044
- * @since 1.2.2.7
8045
- *
8046
- * @return string
8047
- */
8048
- function main_menu_url() {
8049
- return $this->_menu->main_menu_url();
8050
- }
8051
-
8052
- /**
8053
- * Check if currently on the theme's setting page or
8054
- * on any of the Freemius added pages (via tabs).
8055
- *
8056
- * @author Vova Feldman (@svovaf)
8057
- * @since 1.2.2.7
8058
- *
8059
- * @return bool
8060
- */
8061
- function is_theme_settings_page() {
8062
- return fs_starts_with(
8063
- fs_request_get( 'page', '', 'get' ),
8064
- $this->_menu->get_slug()
8065
- );
8066
- }
8067
-
8068
- /**
8069
- * Plugin's account page + sync license URL.
8070
- *
8071
- * @author Vova Feldman (@svovaf)
8072
- * @since 1.1.9.1
8073
- *
8074
- * @param bool|number $plugin_id
8075
- * @param bool $add_action_nonce
8076
- * @param array $params
8077
- *
8078
- * @return string
8079
- */
8080
- function _get_sync_license_url( $plugin_id = false, $add_action_nonce = true, $params = array() ) {
8081
- if ( is_numeric( $plugin_id ) ) {
8082
- $params['plugin_id'] = $plugin_id;
8083
- }
8084
-
8085
- return $this->get_account_url(
8086
- $this->get_unique_affix() . '_sync_license',
8087
- $params,
8088
- $add_action_nonce
8089
- );
8090
- }
8091
-
8092
- /**
8093
- * Plugin's account URL.
8094
- *
8095
- * @author Vova Feldman (@svovaf)
8096
- * @since 1.0.4
8097
- *
8098
- * @param bool|string $action
8099
- * @param array $params
8100
- *
8101
- * @param bool $add_action_nonce
8102
- *
8103
- * @return string
8104
- */
8105
- function get_account_url( $action = false, $params = array(), $add_action_nonce = true ) {
8106
- if ( is_string( $action ) ) {
8107
- $params['fs_action'] = $action;
8108
- }
8109
-
8110
- self::require_pluggable_essentials();
8111
-
8112
- return ( $add_action_nonce && is_string( $action ) ) ?
8113
- fs_nonce_url( $this->_get_admin_page_url( 'account', $params ), $action ) :
8114
- $this->_get_admin_page_url( 'account', $params );
8115
- }
8116
-
8117
- /**
8118
- * @author Vova Feldman (@svovaf)
8119
- * @since 1.2.0
8120
- *
8121
- * @param string $tab
8122
- * @param bool $action
8123
- * @param array $params
8124
- * @param bool $add_action_nonce
8125
- *
8126
- * @return string
8127
- *
8128
- * @uses get_account_url()
8129
- */
8130
- function get_account_tab_url( $tab, $action = false, $params = array(), $add_action_nonce = true ) {
8131
- $params['tab'] = $tab;
8132
-
8133
- return $this->get_account_url( $action, $params, $add_action_nonce );
8134
- }
8135
-
8136
- /**
8137
- * Plugin's account URL.
8138
- *
8139
- * @author Vova Feldman (@svovaf)
8140
- * @since 1.0.4
8141
- *
8142
- * @param bool|string $topic
8143
- * @param bool|string $message
8144
- *
8145
- * @return string
8146
- */
8147
- function contact_url( $topic = false, $message = false ) {
8148
- $params = array();
8149
- if ( is_string( $topic ) ) {
8150
- $params['topic'] = $topic;
8151
- }
8152
- if ( is_string( $message ) ) {
8153
- $params['message'] = $message;
8154
- }
8155
-
8156
- if ( $this->is_addon() ) {
8157
- $params['addon_id'] = $this->get_id();
8158
-
8159
- return $this->get_parent_instance()->_get_admin_page_url( 'contact', $params );
8160
- } else {
8161
- return $this->_get_admin_page_url( 'contact', $params );
8162
- }
8163
- }
8164
-
8165
- /**
8166
- * Add-on direct info URL.
8167
- *
8168
- * @author Vova Feldman (@svovaf)
8169
- * @since 1.1.0
8170
- *
8171
- * @param string $slug
8172
- *
8173
- * @return string
8174
- */
8175
- function addon_url( $slug ) {
8176
- return $this->_get_admin_page_url( 'addons', array(
8177
- 'slug' => $slug
8178
- ) );
8179
- }
8180
-
8181
- /* Logger
8182
- ------------------------------------------------------------------------------------------------------------------*/
8183
- /**
8184
- * @param string $id
8185
- * @param bool $prefix_slug
8186
- *
8187
- * @return FS_Logger
8188
- */
8189
- function get_logger( $id = '', $prefix_slug = true ) {
8190
- return FS_Logger::get_logger( ( $prefix_slug ? $this->_slug : '' ) . ( ( ! $prefix_slug || empty( $id ) ) ? '' : '_' ) . $id );
8191
- }
8192
-
8193
- /**
8194
- * @param $id
8195
- * @param bool $load_options
8196
- * @param bool $prefix_slug
8197
- *
8198
- * @return FS_Option_Manager
8199
- */
8200
- function get_options_manager( $id, $load_options = false, $prefix_slug = true ) {
8201
- return FS_Option_Manager::get_manager( ( $prefix_slug ? $this->_slug : '' ) . ( ( ! $prefix_slug || empty( $id ) ) ? '' : '_' ) . $id, $load_options );
8202
- }
8203
-
8204
- /* Security
8205
- ------------------------------------------------------------------------------------------------------------------*/
8206
- private static function _encrypt( $str ) {
8207
- if ( is_null( $str ) ) {
8208
- return null;
8209
- }
8210
-
8211
- /**
8212
- * The encrypt/decrypt functions are used to protect
8213
- * the user from messing up with some of the sensitive
8214
- * data stored for the module as a JSON in the database.
8215
- *
8216
- * I used the same suggested hack by the theme review team.
8217
- * For more details, look at the function `Base64UrlDecode()`
8218
- * in `./sdk/FreemiusBase.php`.
8219
- *
8220
- * @todo Remove this hack once the base64 error is removed from the Theme Check.
8221
- *
8222
- * @author Vova Feldman (@svovaf)
8223
- * @since 1.2.2
8224
- */
8225
- $fn = 'base64' . '_encode';
8226
-
8227
- return $fn( $str );
8228
- }
8229
-
8230
- static function _decrypt( $str ) {
8231
- if ( is_null( $str ) ) {
8232
- return null;
8233
- }
8234
-
8235
- /**
8236
- * The encrypt/decrypt functions are used to protect
8237
- * the user from messing up with some of the sensitive
8238
- * data stored for the module as a JSON in the database.
8239
- *
8240
- * I used the same suggested hack by the theme review team.
8241
- * For more details, look at the function `Base64UrlDecode()`
8242
- * in `./sdk/FreemiusBase.php`.
8243
- *
8244
- * @todo Remove this hack once the base64 error is removed from the Theme Check.
8245
- *
8246
- * @author Vova Feldman (@svovaf)
8247
- * @since 1.2.2
8248
- */
8249
- $fn = 'base64' . '_decode';
8250
-
8251
- return $fn( $str );
8252
- }
8253
-
8254
- /**
8255
- * @author Vova Feldman (@svovaf)
8256
- * @since 1.0.5
8257
- *
8258
- * @param FS_Entity $entity
8259
- *
8260
- * @return FS_Entity Return an encrypted clone entity.
8261
- */
8262
- private static function _encrypt_entity( FS_Entity $entity ) {
8263
- $clone = clone $entity;
8264
- $props = get_object_vars( $entity );
8265
-
8266
- foreach ( $props as $key => $val ) {
8267
- $clone->{$key} = self::_encrypt( $val );
8268
- }
8269
-
8270
- return $clone;
8271
- }
8272
-
8273
- /**
8274
- * @author Vova Feldman (@svovaf)
8275
- * @since 1.0.5
8276
- *
8277
- * @param FS_Entity $entity
8278
- *
8279
- * @return FS_Entity Return an decrypted clone entity.
8280
- */
8281
- private static function decrypt_entity( FS_Entity $entity ) {
8282
- $clone = clone $entity;
8283
- $props = get_object_vars( $entity );
8284
-
8285
- foreach ( $props as $key => $val ) {
8286
- $clone->{$key} = self::_decrypt( $val );
8287
- }
8288
-
8289
- return $clone;
8290
- }
8291
-
8292
- /**
8293
- * Tries to activate account based on POST params.
8294
- *
8295
- * @author Vova Feldman (@svovaf)
8296
- * @since 1.0.2
8297
- */
8298
- function _activate_account() {
8299
- if ( $this->is_registered() ) {
8300
- // Already activated.
8301
- return;
8302
- }
8303
-
8304
- self::_clean_admin_content_section();
8305
-
8306
- if ( fs_request_is_action( 'activate' ) && fs_request_is_post() ) {
8307
- // check_admin_referer( 'activate_' . $this->_plugin->public_key );
8308
 
8309
- // Verify matching plugin details.
8310
- if ( $this->_plugin->id != fs_request_get( 'plugin_id' ) || $this->_slug != fs_request_get( 'plugin_slug' ) ) {
8311
- return;
8312
- }
8313
-
8314
- $user = new FS_User();
8315
- $user->id = fs_request_get( 'user_id' );
8316
- $user->public_key = fs_request_get( 'user_public_key' );
8317
- $user->secret_key = fs_request_get( 'user_secret_key' );
8318
- $user->email = fs_request_get( 'user_email' );
8319
- $user->first = fs_request_get( 'user_first' );
8320
- $user->last = fs_request_get( 'user_last' );
8321
- $user->is_verified = fs_request_get_bool( 'user_is_verified' );
8322
-
8323
- $site = new FS_Site();
8324
- $site->id = fs_request_get( 'install_id' );
8325
- $site->public_key = fs_request_get( 'install_public_key' );
8326
- $site->secret_key = fs_request_get( 'install_secret_key' );
8327
- $site->plan->id = fs_request_get( 'plan_id' );
8328
- $site->plan->title = fs_request_get( 'plan_title' );
8329
- $site->plan->name = fs_request_get( 'plan_name' );
8330
-
8331
- $plans = array();
8332
- $plans_data = json_decode( urldecode( fs_request_get( 'plans' ) ) );
8333
- foreach ( $plans_data as $p ) {
8334
- $plans[] = new FS_Plugin_Plan( $p );
8335
- }
8336
-
8337
- $this->_set_account( $user, $site, $plans );
8338
-
8339
- // Reload the page with the keys.
8340
- fs_redirect( $this->_get_admin_page_url() );
8341
- }
8342
- }
8343
-
8344
- /**
8345
- * @author Vova Feldman (@svovaf)
8346
- * @since 1.0.7
8347
- *
8348
- * @param string $email
8349
- *
8350
- * @return FS_User|bool
8351
- */
8352
- static function _get_user_by_email( $email ) {
8353
- self::$_static_logger->entrance();
8354
-
8355
- $email = trim( strtolower( $email ) );
8356
- $users = self::get_all_users();
8357
- if ( is_array( $users ) ) {
8358
- foreach ( $users as $u ) {
8359
- if ( $email === trim( strtolower( $u->email ) ) ) {
8360
- return $u;
8361
- }
8362
- }
8363
- }
8364
-
8365
- return false;
8366
- }
8367
-
8368
- #----------------------------------------------------------------------------------
8369
- #region Account (Loading, Updates & Activation)
8370
- #----------------------------------------------------------------------------------
8371
-
8372
- /***
8373
- * Load account information (user + site).
8374
- *
8375
- * @author Vova Feldman (@svovaf)
8376
- * @since 1.0.1
8377
- */
8378
- private function _load_account() {
8379
- $this->_logger->entrance();
8380
-
8381
- $this->do_action( 'before_account_load' );
8382
-
8383
- $sites = self::get_all_sites( $this->_module_type );
8384
- $users = self::get_all_users();
8385
- $plans = self::get_all_plans( $this->_module_type );
8386
- $licenses = self::get_all_licenses( $this->_module_type );
8387
-
8388
- if ( $this->_logger->is_on() && is_admin() ) {
8389
- $this->_logger->log( 'sites = ' . var_export( $sites, true ) );
8390
- $this->_logger->log( 'users = ' . var_export( $users, true ) );
8391
- $this->_logger->log( 'plans = ' . var_export( $plans, true ) );
8392
- $this->_logger->log( 'licenses = ' . var_export( $licenses, true ) );
8393
- }
8394
-
8395
- $site = isset( $sites[ $this->_slug ] ) ? $sites[ $this->_slug ] : false;
8396
-
8397
- if ( is_object( $site ) &&
8398
- is_numeric( $site->id ) &&
8399
- is_numeric( $site->user_id ) &&
8400
- is_object( $site->plan )
8401
- ) {
8402
- // Load site.
8403
- $this->_site = clone $site;
8404
- $this->_site->plan = self::decrypt_entity( $this->_site->plan );
8405
 
8406
  /**
8407
- * If the install owner's details are not stored locally, use the previous user's details if available.
8408
- *
8409
- * @author Leo Fajardo (@leorw)
8410
  */
8411
- if ( ! isset( $users[ $this->_site->user_id ] ) && FS_User::is_valid_id( $this->_storage->prev_user_id ) ) {
8412
- $user_id = $this->_storage->prev_user_id;
8413
- } else {
8414
- $user_id = $this->_site->user_id;
8415
- }
8416
-
8417
- // Load relevant user.
8418
- $this->_user = clone $users[ $user_id ];
8419
-
8420
- // Load plans.
8421
- $this->_plans = $plans[ $this->_slug ];
8422
- if ( ! is_array( $this->_plans ) || empty( $this->_plans ) ) {
8423
- $this->_sync_plans();
8424
- } else {
8425
- for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
8426
- if ( $this->_plans[ $i ] instanceof FS_Plugin_Plan ) {
8427
- $this->_plans[ $i ] = self::decrypt_entity( $this->_plans[ $i ] );
8428
- } else {
8429
- unset( $this->_plans[ $i ] );
8430
- }
8431
- }
8432
- }
8433
-
8434
- // Load licenses.
8435
- $this->_licenses = array();
8436
- if ( is_array( $licenses ) &&
8437
- isset( $licenses[ $this->_slug ] ) &&
8438
- isset( $licenses[ $this->_slug ][ $this->_user->id ] )
8439
- ) {
8440
- $this->_licenses = $licenses[ $this->_slug ][ $this->_user->id ];
8441
- }
8442
-
8443
- $this->_license = $this->_get_license_by_id( $this->_site->license_id );
8444
-
8445
- if ( $this->_site->version != $this->get_plugin_version() ) {
8446
- // If stored install version is different than current installed plugin version,
8447
- // then update plugin version event.
8448
- $this->update_plugin_version_event();
8449
- }
8450
- }
8451
-
8452
- $this->_register_account_hooks();
8453
- }
8454
-
8455
- /**
8456
- * @author Vova Feldman (@svovaf)
8457
- * @since 1.0.1
8458
- *
8459
- * @param FS_User $user
8460
- * @param FS_Site $site
8461
- * @param bool|array $plans
8462
- */
8463
- private function _set_account( FS_User $user, FS_Site $site, $plans = false ) {
8464
- $site->slug = $this->_slug;
8465
- $site->user_id = $user->id;
8466
-
8467
- $this->_site = $site;
8468
- $this->_user = $user;
8469
- if ( false !== $plans ) {
8470
- $this->_plans = $plans;
8471
- }
8472
-
8473
- $this->send_install_update();
8474
-
8475
- $this->_store_account();
8476
-
8477
- }
8478
-
8479
- /**
8480
- * @author Vova Feldman (@svovaf)
8481
- * @since 1.1.7.4
8482
- *
8483
- * @param array $override_with
8484
- *
8485
- * @return array
8486
- */
8487
- function get_opt_in_params( $override_with = array() ) {
8488
- $this->_logger->entrance();
8489
-
8490
- $current_user = self::_get_current_wp_user();
8491
-
8492
- $activation_action = $this->get_unique_affix() . '_activate_new';
8493
- $return_url = $this->is_anonymous() ?
8494
- // If skipped already, then return to the account page.
8495
- $this->get_account_url( $activation_action, array(), false ) :
8496
- // Return to the module's main page.
8497
- $this->get_after_activation_url( 'after_connect_url', array( 'fs_action' => $activation_action ) );
8498
-
8499
- $params = array(
8500
- 'user_firstname' => $current_user->user_firstname,
8501
- 'user_lastname' => $current_user->user_lastname,
8502
- 'user_nickname' => $current_user->user_nicename,
8503
- 'user_email' => $current_user->user_email,
8504
- 'user_ip' => WP_FS__REMOTE_ADDR,
8505
- 'plugin_slug' => $this->_slug,
8506
- 'plugin_id' => $this->get_id(),
8507
- 'plugin_public_key' => $this->get_public_key(),
8508
- 'plugin_version' => $this->get_plugin_version(),
8509
- 'return_url' => fs_nonce_url( $return_url, $activation_action ),
8510
- 'account_url' => fs_nonce_url( $this->_get_admin_page_url(
8511
- 'account',
8512
- array( 'fs_action' => 'sync_user' )
8513
- ), 'sync_user' ),
8514
- 'site_uid' => $this->get_anonymous_id(),
8515
- 'site_url' => get_site_url(),
8516
- 'site_name' => get_bloginfo( 'name' ),
8517
- 'platform_version' => get_bloginfo( 'version' ),
8518
- 'sdk_version' => $this->version,
8519
- 'programming_language_version' => phpversion(),
8520
- 'language' => get_bloginfo( 'language' ),
8521
- 'charset' => get_bloginfo( 'charset' ),
8522
- 'is_premium' => $this->is_premium(),
8523
- 'is_active' => true,
8524
- 'is_uninstalled' => false,
8525
- );
8526
-
8527
- if ( $this->is_pending_activation() &&
8528
- ! empty( $this->_storage->pending_license_key )
8529
- ) {
8530
- $params['license_key'] = $this->_storage->pending_license_key;
8531
- }
8532
-
8533
- if ( WP_FS__SKIP_EMAIL_ACTIVATION && $this->has_secret_key() ) {
8534
- // Even though rand() is known for its security issues,
8535
- // the timestamp adds another layer of protection.
8536
- // It would be very hard for an attacker to get the secret key form here.
8537
- // Plus, this should never run in production since the secret should never
8538
- // be included in the production version.
8539
- $params['ts'] = WP_FS__SCRIPT_START_TIME;
8540
- $params['salt'] = md5( uniqid( rand() ) );
8541
- $params['secure'] = md5(
8542
- $params['ts'] .
8543
- $params['salt'] .
8544
- $this->get_secret_key()
8545
- );
8546
- }
8547
-
8548
- return array_merge( $params, $override_with );
8549
- }
8550
-
8551
- /**
8552
- * 1. If successful opt-in or pending activation returns the next page that the user should be redirected to.
8553
- * 2. If there was an API error, return the API result.
8554
- *
8555
- * @author Vova Feldman (@svovaf)
8556
- * @since 1.1.7.4
8557
- *
8558
- * @param string|bool $email
8559
- * @param string|bool $first
8560
- * @param string|bool $last
8561
- * @param string|bool $license_key
8562
- * @param bool $is_uninstall If "true", this means that the module is currently being uninstalled.
8563
- * In this case, the user and site info will be sent to the server but no
8564
- * data will be saved to the WP installation's database.
8565
- * @param number|bool $trial_plan_id
8566
- * @param bool $is_disconnected Whether or not to opt in without tracking.
8567
- *
8568
- * @return string|object
8569
- * @use WP_Error
8570
- */
8571
- function opt_in(
8572
- $email = false,
8573
- $first = false,
8574
- $last = false,
8575
- $license_key = false,
8576
- $is_uninstall = false,
8577
- $trial_plan_id = false,
8578
- $is_disconnected = false
8579
- ) {
8580
- $this->_logger->entrance();
8581
-
8582
- if ( false === $email ) {
8583
- $current_user = self::_get_current_wp_user();
8584
- $email = $current_user->user_email;
8585
- }
8586
-
8587
- /**
8588
- * @since 1.2.1 If activating with license key, ignore the context-user
8589
- * since the user will be automatically loaded from the license.
8590
- */
8591
- if ( empty( $license_key ) ) {
8592
- // Clean up pending license if opt-ing in again.
8593
- $this->_storage->remove( 'pending_license_key' );
8594
-
8595
- if ( ! $is_uninstall ) {
8596
- $fs_user = Freemius::_get_user_by_email( $email );
8597
- if ( is_object( $fs_user ) && ! $this->is_pending_activation() ) {
8598
- return $this->install_with_current_user( false, $trial_plan_id );
8599
- }
8600
- }
8601
- }
8602
-
8603
- $user_info = array();
8604
- if ( ! empty( $email ) ) {
8605
- $user_info['user_email'] = $email;
8606
- }
8607
- if ( ! empty( $first ) ) {
8608
- $user_info['user_firstname'] = $first;
8609
- }
8610
- if ( ! empty( $last ) ) {
8611
- $user_info['user_lastname'] = $last;
8612
- }
8613
-
8614
- $params = $this->get_opt_in_params( $user_info );
8615
-
8616
- $filtered_license_key = false;
8617
- if ( is_string( $license_key ) ) {
8618
- $filtered_license_key = $this->apply_filters( 'license_key', $license_key );
8619
- $params['license_key'] = $filtered_license_key;
8620
- } else if ( FS_Plugin_Plan::is_valid_id( $trial_plan_id ) ) {
8621
- $params['trial_plan_id'] = $trial_plan_id;
8622
- }
8623
-
8624
- if ( $is_uninstall ) {
8625
- $params['uninstall_params'] = array(
8626
- 'reason_id' => $this->_storage->uninstall_reason->id,
8627
- 'reason_info' => $this->_storage->uninstall_reason->info
8628
- );
8629
- }
8630
-
8631
- if ( isset( $params['license_key'] ) ) {
8632
- $fs_user = Freemius::_get_user_by_email( $email );
8633
-
8634
- if ( is_object( $fs_user ) ) {
8635
- /**
8636
- * If opting in with a context license and the context WP Admin user already opted in
8637
- * before from the current site, add the user context security params to avoid the
8638
- * unnecessry email activation when the context license is owned by the same context user.
8639
- *
8640
- * @author Leo Fajardo (@leorw)
8641
- * @since 1.2.3
8642
- */
8643
- $params = array_merge( $params, FS_Security::instance()->get_context_params(
8644
- $fs_user,
8645
- false,
8646
- 'install_with_existing_user'
8647
- ) );
8648
- }
8649
- }
8650
-
8651
- $params['is_disconnected'] = $is_disconnected;
8652
- $params['format'] = 'json';
8653
-
8654
- $url = WP_FS__ADDRESS . '/action/service/user/install/';
8655
- if ( isset( $_COOKIE['XDEBUG_SESSION'] ) ) {
8656
- $url = add_query_arg( 'XDEBUG_SESSION', 'PHPSTORM', $url );
8657
- }
8658
-
8659
- $response = wp_remote_post( $url, array(
8660
- 'method' => 'POST',
8661
- 'body' => $params,
8662
- 'timeout' => 15,
8663
- ) );
8664
-
8665
- if ( $response instanceof WP_Error ) {
8666
- if ( 'https://' === substr( $url, 0, 8 ) &&
8667
- isset( $response->errors ) &&
8668
- isset( $response->errors['http_request_failed'] )
8669
- ) {
8670
- $http_error = strtolower( $response->errors['http_request_failed'][0] );
8671
-
8672
- if ( false !== strpos( $http_error, 'ssl' ) ) {
8673
- // Failed due to old version of cURL or Open SSL (SSLv3 is not supported by CloudFlare).
8674
- $url = 'http://' . substr( $url, 8 );
8675
-
8676
- $response = wp_remote_post( $url, array(
8677
- 'method' => 'POST',
8678
- 'body' => $params,
8679
- 'timeout' => 15,
8680
- ) );
8681
- }
8682
- }
8683
- }
8684
-
8685
- if ( is_wp_error( $response ) ) {
8686
- /**
8687
- * @var WP_Error $response
8688
- */
8689
- $result = new stdClass();
8690
-
8691
- $error_code = $response->get_error_code();
8692
- $error_type = str_replace( ' ', '', ucwords( str_replace( '_', ' ', $error_code ) ) );
8693
-
8694
- $result->error = (object) array(
8695
- 'type' => $error_type,
8696
- 'message' => $response->get_error_message(),
8697
- 'code' => $error_code,
8698
- 'http' => 402
8699
- );
8700
-
8701
- return $result;
8702
- }
8703
-
8704
- // Module is being uninstalled, don't handle the returned data.
8705
- if ( $is_uninstall ) {
8706
- return true;
8707
- }
8708
 
8709
- /**
8710
- * 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.
8711
- *
8712
- * @author Vova Feldman (@svovaf)
8713
- * @since 1.2.3
8714
- * @link https://themes.trac.wordpress.org/ticket/46134#comment:5
8715
- * @link https://themes.trac.wordpress.org/ticket/46134#comment:9
8716
- * @link https://themes.trac.wordpress.org/ticket/46134#comment:12
8717
- * @link https://themes.trac.wordpress.org/ticket/46134#comment:14
8718
- */
8719
- $decoded = is_string( $response['body'] ) ?
8720
- json_decode( $response['body'] ) :
8721
- null;
8722
 
8723
- if ( empty( $decoded ) ) {
8724
- return false;
8725
- }
8726
-
8727
- if ( ! $this->is_api_result_object( $decoded ) ) {
8728
- if ( ! empty( $params['license_key'] ) ) {
8729
- // Pass the fully entered license key to the failure handler.
8730
- $params['license_key'] = $license_key;
8731
- }
8732
-
8733
- return $is_uninstall ?
8734
- $decoded :
8735
- $this->apply_filters( 'after_install_failure', $decoded, $params );
8736
- } else if ( isset( $decoded->pending_activation ) && $decoded->pending_activation ) {
8737
- // Pending activation, add message.
8738
- return $this->set_pending_confirmation(
8739
- ( isset( $decoded->email ) ?
8740
- $decoded->email :
8741
- true ),
8742
- false,
8743
- $filtered_license_key,
8744
- ! empty( $params['trial_plan_id'] )
8745
- );
8746
- } else if ( isset( $decoded->install_secret_key ) ) {
8747
- return $this->install_with_new_user(
8748
- $decoded->user_id,
8749
- $decoded->user_public_key,
8750
- $decoded->user_secret_key,
8751
- $decoded->install_id,
8752
- $decoded->install_public_key,
8753
- $decoded->install_secret_key,
8754
- false
8755
- );
8756
- }
8757
-
8758
- return $decoded;
8759
- }
8760
-
8761
- /**
8762
- * Set user and site identities.
8763
- *
8764
- * @author Vova Feldman (@svovaf)
8765
- * @since 1.0.9
8766
- *
8767
- * @param FS_User $user
8768
- * @param FS_Site $site
8769
- * @param bool $redirect
8770
- * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will
8771
- * redirect (or return a URL) to the account page with a special parameter to
8772
- * trigger the auto installation processes.
8773
- *
8774
- * @return string If redirect is `false`, returns the next page the user should be redirected to.
8775
- */
8776
- function setup_account(
8777
- FS_User $user,
8778
- FS_Site $site,
8779
- $redirect = true,
8780
- $auto_install = false
8781
- ) {
8782
- $this->_user = $user;
8783
- $this->_site = $site;
8784
-
8785
- $this->_sync_plans();
8786
-
8787
- $this->_enrich_site_plan( false );
8788
-
8789
- $this->_set_account( $user, $site );
8790
-
8791
- if ( $this->is_trial() ) {
8792
- // Store trial plan information.
8793
- $this->_enrich_site_trial_plan( true );
8794
- }
8795
-
8796
- // If Freemius was OFF before, turn it on.
8797
- $this->turn_on();
8798
-
8799
- $this->do_action( 'after_account_connection', $user, $site );
8800
-
8801
- if ( is_numeric( $site->license_id ) ) {
8802
- $this->_license = $this->_get_license_by_id( $site->license_id );
8803
- }
8804
-
8805
- $this->_admin_notices->remove_sticky( 'connect_account' );
8806
-
8807
- if ( $this->is_pending_activation() || ! $this->has_settings_menu() ) {
8808
- // Remove pending activation sticky notice (if still exist).
8809
- $this->_admin_notices->remove_sticky( 'activation_pending' );
8810
-
8811
- // Remove plugin from pending activation mode.
8812
- unset( $this->_storage->is_pending_activation );
8813
-
8814
- if ( ! $this->is_paying_or_trial() ) {
8815
- $this->_admin_notices->add_sticky(
8816
- sprintf( $this->get_text_x_inline( '%s activation was successfully completed.',
8817
- 'pluginX activation was successfully...', 'plugin-x-activation-message' ), '<b>' . $this->get_plugin_name() . '</b>' ),
8818
- 'activation_complete'
8819
- );
8820
- }
8821
- }
8822
-
8823
- if ( $this->is_paying_or_trial() ) {
8824
- if ( ! $this->is_premium() || ! $this->has_premium_version() || ! $this->has_settings_menu() ) {
8825
- if ( $this->is_paying() ) {
8826
- $this->_admin_notices->add_sticky(
8827
- sprintf(
8828
- $this->get_text_inline( 'Your account was successfully activated with the %s plan.', 'activation-with-plan-x-message' ),
8829
- $this->_site->plan->title
8830
- ) . $this->get_complete_upgrade_instructions(),
8831
- 'plan_upgraded',
8832
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
8833
- );
8834
- } else {
8835
- $this->_admin_notices->add_sticky(
8836
- sprintf(
8837
- $this->get_text_inline( 'Your trial has been successfully started.', 'trial-started-message' ),
8838
- '<i>' . $this->get_plugin_name() . '</i>'
8839
- ) . $this->get_complete_upgrade_instructions( $this->_storage->trial_plan->title ),
8840
- 'trial_started',
8841
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
8842
- );
8843
- }
8844
- }
8845
-
8846
- $this->_admin_notices->remove_sticky( array(
8847
- 'trial_promotion',
8848
- ) );
8849
- }
8850
-
8851
- $plugin_id = fs_request_get( 'plugin_id', false );
8852
-
8853
- // Store activation time ONLY for plugins (not add-ons).
8854
- if ( ! is_numeric( $plugin_id ) || ( $plugin_id == $this->_plugin->id ) ) {
8855
- $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME;
8856
- }
8857
-
8858
- $next_page = '';
8859
-
8860
- $extra = array();
8861
- if ( $auto_install ) {
8862
- $extra['auto_install'] = 'true';
8863
- }
8864
-
8865
- if ( is_numeric( $plugin_id ) ) {
8866
- /**
8867
- * @author Leo Fajardo
8868
- * @since 1.2.1.6
8869
- *
8870
- * Also sync the license after an anonymous user subscribes.
8871
- */
8872
- if ( $this->is_anonymous() || $plugin_id != $this->_plugin->id ) {
8873
- // Add-on was installed - sync license right after install.
8874
- $next_page = $this->_get_sync_license_url( $plugin_id, true, $extra );
8875
- }
8876
- } else {
8877
- /**
8878
- * @author Vova Feldman (@svovaf)
8879
- * @since 1.1.9 If site installed with a valid license, sync license.
8880
- */
8881
- if ( $this->is_paying() ) {
8882
- $this->_sync_plugin_license( true );
8883
- }
8884
-
8885
- // Reload the page with the keys.
8886
- $next_page = $this->is_anonymous() ?
8887
- // If user previously skipped, redirect to account page.
8888
- $this->get_account_url( false, $extra ) :
8889
- $this->get_after_activation_url( 'after_connect_url' );
8890
- }
8891
-
8892
- if ( ! empty( $next_page ) && $redirect ) {
8893
- fs_redirect( $next_page );
8894
- }
8895
-
8896
- return $next_page;
8897
- }
8898
-
8899
- /**
8900
- * Install plugin with new user information after approval.
8901
- *
8902
- * @author Vova Feldman (@svovaf)
8903
- * @since 1.0.7
8904
- */
8905
- function _install_with_new_user() {
8906
- $this->_logger->entrance();
8907
-
8908
- if ( $this->is_registered() ) {
8909
- return;
8910
- }
8911
-
8912
- if ( ( $this->is_plugin() && fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) ||
8913
- // @todo This logic should be improved because it's executed on every load of a theme.
8914
- $this->is_theme()
8915
- ) {
8916
- // check_admin_referer( $this->_slug . '_activate_new' );
8917
 
8918
- if ( fs_request_has( 'user_secret_key' ) ) {
8919
- $this->install_with_new_user(
8920
- fs_request_get( 'user_id' ),
8921
- fs_request_get( 'user_public_key' ),
8922
- fs_request_get( 'user_secret_key' ),
8923
- fs_request_get( 'install_id' ),
8924
- fs_request_get( 'install_public_key' ),
8925
- fs_request_get( 'install_secret_key' ),
8926
- true,
8927
- fs_request_get_bool( 'auto_install' )
8928
- );
8929
- } else if ( fs_request_has( 'pending_activation' ) ) {
8930
- $this->set_pending_confirmation( fs_request_get( 'user_email' ), true );
8931
- }
8932
- }
8933
- }
8934
-
8935
- /**
8936
- * Install plugin with new user.
8937
- *
8938
- * @author Vova Feldman (@svovaf)
8939
- * @since 1.1.7.4
8940
- *
8941
- * @param number $user_id
8942
- * @param string $user_public_key
8943
- * @param string $user_secret_key
8944
- * @param number $install_id
8945
- * @param string $install_public_key
8946
- * @param string $install_secret_key
8947
- * @param bool $redirect
8948
- * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will
8949
- * redirect (or return a URL) to the account page with a special parameter to
8950
- * trigger the auto installation processes.
8951
- *
8952
- * @return string If redirect is `false`, returns the next page the user should be redirected to.
8953
- */
8954
- private function install_with_new_user(
8955
- $user_id,
8956
- $user_public_key,
8957
- $user_secret_key,
8958
- $install_id,
8959
- $install_public_key,
8960
- $install_secret_key,
8961
- $redirect = true,
8962
- $auto_install = false
8963
- ) {
8964
- $user = new FS_User();
8965
- $user->id = $user_id;
8966
- $user->public_key = $user_public_key;
8967
- $user->secret_key = $user_secret_key;
8968
-
8969
- $this->_user = $user;
8970
- $user_result = $this->get_api_user_scope()->get();
8971
- $user = new FS_User( $user_result );
8972
- $this->_user = $user;
8973
-
8974
- $site = new FS_Site();
8975
- $site->id = $install_id;
8976
- $site->public_key = $install_public_key;
8977
- $site->secret_key = $install_secret_key;
8978
-
8979
- $this->_site = $site;
8980
- $site_result = $this->get_api_site_scope()->get();
8981
- $site = new FS_Site( $site_result );
8982
- $this->_site = $site;
8983
-
8984
- return $this->setup_account(
8985
- $this->_user,
8986
- $this->_site,
8987
- $redirect,
8988
- $auto_install
8989
- );
8990
- }
8991
-
8992
- /**
8993
- * @author Vova Feldman (@svovaf)
8994
- * @since 1.1.7.4
8995
- *
8996
- * @param string|bool $email
8997
- * @param bool $redirect
8998
- * @param string|bool $license_key Since 1.2.1.5
8999
- * @param bool $is_pending_trial Since 1.2.1.5
9000
- *
9001
- * @return string Since 1.2.1.5 if $redirect is `false`, return the pending activation page.
9002
- */
9003
- private function set_pending_confirmation(
9004
- $email = false,
9005
- $redirect = true,
9006
- $license_key = false,
9007
- $is_pending_trial = false
9008
- ) {
9009
- if ( $this->_ignore_pending_mode ) {
9010
- /**
9011
- * If explicitly asked to ignore pending mode, set to anonymous mode
9012
- * if require confirmation before finalizing the opt-in.
9013
- *
9014
- * @author Vova Feldman
9015
- * @since 1.2.1.6
9016
- */
9017
- $this->skip_connection();
9018
- } else {
9019
- // Install must be activated via email since
9020
- // user with the same email already exist.
9021
- $this->_storage->is_pending_activation = true;
9022
- $this->_add_pending_activation_notice( $email, $is_pending_trial );
9023
- }
9024
-
9025
- if ( ! empty( $license_key ) ) {
9026
- $this->_storage->pending_license_key = $license_key;
9027
- }
9028
-
9029
- // Remove the opt-in sticky notice.
9030
- $this->_admin_notices->remove_sticky( array(
9031
- 'connect_account',
9032
- 'trial_promotion',
9033
- ) );
9034
-
9035
- $next_page = $this->get_after_activation_url( 'after_pending_connect_url' );
9036
-
9037
- // Reload the page with with pending activation message.
9038
- if ( $redirect ) {
9039
- fs_redirect( $next_page );
9040
- }
9041
-
9042
- return $next_page;
9043
- }
9044
-
9045
- /**
9046
- * Install plugin with current logged WP user info.
9047
- *
9048
- * @author Vova Feldman (@svovaf)
9049
- * @since 1.0.7
9050
- */
9051
- function _install_with_current_user() {
9052
- $this->_logger->entrance();
9053
-
9054
- if ( $this->is_registered() ) {
9055
- return;
9056
- }
9057
-
9058
- if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) && fs_request_is_post() ) {
9059
- // check_admin_referer( 'activate_existing_' . $this->_plugin->public_key );
9060
 
9061
- /**
9062
- * @author Vova Feldman (@svovaf)
9063
- * @since 1.1.9 Add license key if given.
9064
- */
9065
- $license_key = fs_request_get( 'license_secret_key' );
9066
-
9067
- $this->install_with_current_user( $license_key );
9068
- }
9069
- }
9070
-
9071
-
9072
- /**
9073
- * @author Vova Feldman (@svovaf)
9074
- * @since 1.1.7.4
9075
- *
9076
- * @param string|bool $license_key
9077
- * @param number|bool $trial_plan_id
9078
- * @param bool $redirect
9079
- *
9080
- * @return string|object If redirect is `false`, returns the next page the user should be redirected to, or the
9081
- * API error object if failed to install.
9082
- */
9083
- private function install_with_current_user(
9084
- $license_key = false,
9085
- $trial_plan_id = false,
9086
- $redirect = true
9087
- ) {
9088
- // Get current logged WP user.
9089
- $current_user = self::_get_current_wp_user();
9090
-
9091
- // Find the relevant FS user by the email.
9092
- $user = self::_get_user_by_email( $current_user->user_email );
9093
-
9094
- // We have to set the user before getting user scope API handler.
9095
- $this->_user = $user;
9096
-
9097
- $extra_install_params = array(
9098
- 'uid' => $this->get_anonymous_id(),
9099
- );
9100
-
9101
- if ( ! empty( $license_key ) ) {
9102
- $filtered_license_key = $this->apply_filters( 'license_key', $license_key );
9103
- $extra_install_params['license_key'] = $filtered_license_key;
9104
- } else if ( FS_Plugin_Plan::is_valid_id( $trial_plan_id ) ) {
9105
- $extra_install_params['trial_plan_id'] = $trial_plan_id;
9106
- }
9107
-
9108
- $args = $this->get_install_data_for_api( $extra_install_params, false, false );
9109
-
9110
- // Install the plugin.
9111
- $install = $this->get_api_user_scope()->call(
9112
- "/plugins/{$this->get_id()}/installs.json",
9113
- 'post',
9114
- $args
9115
- );
9116
-
9117
- if ( ! $this->is_api_result_entity( $install ) ) {
9118
- if ( ! empty( $args['license_key'] ) ) {
9119
- // Pass full the fully entered license key to the failure handler.
9120
- $args['license_key'] = $license_key;
9121
- }
9122
-
9123
- $install = $this->apply_filters( 'after_install_failure', $install, $args );
9124
-
9125
- $this->_admin_notices->add(
9126
- sprintf( $this->get_text_inline( 'Couldn\'t activate %s.', 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
9127
- $this->get_text_inline( 'Please contact us with the following message:', 'contact-us-with-error-message' ) . ' ' . '<b>' . $install->error->message . '</b>',
9128
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
9129
- 'error'
9130
- );
9131
-
9132
- if ( $redirect ) {
9133
- /**
9134
- * We set the user before getting the user scope API handler, so the user became temporarily
9135
- * registered (`is_registered() = true`). Since the API returned an error and we will redirect,
9136
- * we have to set the user to `null`, otherwise, the user will be redirected to the wrong
9137
- * activation page based on the return value of `is_registered()`. In addition, in case the
9138
- * context plugin doesn't have a settings menu and the default page is the `Plugins` page,
9139
- * misleading plugin activation errors will be shown on the `Plugins` page.
9140
- *
9141
- * @author Leo Fajardo (@leorw)
9142
- */
9143
- $this->_user = null;
9144
 
9145
- fs_redirect( $this->get_activation_url( array( 'error' => $install->error->message ) ) );
9146
- }
9147
-
9148
- return $install;
9149
- }
9150
-
9151
- $site = new FS_Site( $install );
9152
- $this->_site = $site;
9153
-
9154
- return $this->setup_account( $this->_user, $this->_site, $redirect );
9155
- }
9156
-
9157
- /**
9158
- * Tries to activate add-on account based on parent plugin info.
9159
- *
9160
- * @author Vova Feldman (@svovaf)
9161
- * @since 1.0.6
9162
- *
9163
- * @param Freemius $parent_fs
9164
- */
9165
- private function _activate_addon_account( Freemius $parent_fs ) {
9166
- if ( $this->is_registered() ) {
9167
- // Already activated.
9168
- return;
9169
- }
9170
-
9171
- // Activate add-on with parent plugin credentials.
9172
- $addon_install = $parent_fs->get_api_site_scope()->call(
9173
- "/addons/{$this->_plugin->id}/installs.json",
9174
- 'post',
9175
- $this->get_install_data_for_api( array(
9176
- 'uid' => $this->get_anonymous_id(),
9177
- ), false, false )
9178
- );
9179
-
9180
- if ( isset( $addon_install->error ) ) {
9181
- $this->_admin_notices->add(
9182
- sprintf( $this->get_text_inline( 'Couldn\'t activate %s.', 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
9183
- $this->get_text_inline( 'Please contact us with the following message:', 'contact-us-with-error-message' ) . ' ' . '<b>' . $addon_install->error->message . '</b>',
9184
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
9185
- 'error'
9186
- );
9187
-
9188
- return;
9189
- }
9190
-
9191
- // First of all, set site info - otherwise we won't
9192
- // be able to invoke API calls.
9193
- $this->_site = new FS_Site( $addon_install );
9194
-
9195
- // Sync add-on plans.
9196
- $this->_sync_plans();
9197
-
9198
- // Get site's current plan.
9199
- $this->_site->plan = $this->_get_plan_by_id( $this->_site->plan->id );
9200
-
9201
- // Get user information based on parent's plugin.
9202
- $user = $parent_fs->get_user();
9203
-
9204
- $this->_set_account( $user, $this->_site );
9205
-
9206
- // Sync licenses.
9207
- $this->_sync_licenses();
9208
-
9209
- // Try to activate premium license.
9210
- $this->_activate_license( true );
9211
- }
9212
-
9213
- /**
9214
- * Tries to activate parent account based on add-on's info.
9215
- *
9216
- * @author Vova Feldman (@svovaf)
9217
- * @since 1.2.2.7
9218
- *
9219
- * @param Freemius $parent_fs
9220
- */
9221
- private function activate_parent_account( Freemius $parent_fs ) {
9222
- if ( ! $this->is_addon() ) {
9223
- // This is not an add-on.
9224
- return;
9225
- }
9226
-
9227
- if ( $parent_fs->is_registered() ) {
9228
- // Already activated.
9229
- return;
9230
- }
9231
-
9232
- // Activate parent with add-on's user credentials.
9233
- $parent_install = $this->get_api_user_scope()->call(
9234
- "/plugins/{$parent_fs->_plugin->id}/installs.json",
9235
- 'post',
9236
- $parent_fs->get_install_data_for_api( array(
9237
- 'uid' => $parent_fs->get_anonymous_id(),
9238
- ), false, false )
9239
- );
9240
-
9241
- if ( isset( $parent_install->error ) ) {
9242
- $this->_admin_notices->add(
9243
- sprintf( $this->get_text_inline( 'Couldn\'t activate %s.', 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
9244
- $this->get_text_inline( 'Please contact us with the following message:', 'contact-us-with-error-message' ) . ' ' . '<b>' . $parent_install->error->message . '</b>',
9245
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
9246
- 'error'
9247
- );
9248
-
9249
- return;
9250
- }
9251
 
9252
- $parent_fs->_admin_notices->remove_sticky( 'connect_account' );
 
 
9253
 
9254
- if ( $parent_fs->is_pending_activation() ) {
9255
- $parent_fs->_admin_notices->remove_sticky( 'activation_pending' );
9256
 
9257
- unset( $parent_fs->_storage->is_pending_activation );
9258
- }
9259
 
9260
- // First of all, set site info - otherwise we won't
9261
- // be able to invoke API calls.
9262
- $parent_fs->_site = new FS_Site( $parent_install );
 
 
9263
 
9264
- // Sync add-on plans.
9265
- $parent_fs->_sync_plans();
 
 
9266
 
9267
- // Get site's current plan.
9268
- $parent_fs->_site->plan = $parent_fs->_get_plan_by_id( $parent_fs->_site->plan->id );
 
 
 
9269
 
9270
- // Get user information based on parent's plugin.
9271
- $user = $this->get_user();
9272
 
9273
- $parent_fs->_set_account( $user, $parent_fs->_site );
9274
- }
 
 
 
 
9275
 
9276
- #endregion
9277
 
9278
- #----------------------------------------------------------------------------------
9279
- #region Admin Menu Items
9280
- #----------------------------------------------------------------------------------
 
 
 
 
9281
 
9282
- private $_menu_items = array();
 
 
9283
 
9284
- /**
9285
- * @author Vova Feldman (@svovaf)
9286
- * @since 1.2.1.8
9287
- *
9288
- * @return array
9289
- */
9290
- function get_menu_items() {
9291
- return $this->_menu_items;
9292
- }
9293
 
9294
- /**
9295
- * @author Vova Feldman (@svovaf)
9296
- * @since 1.0.7
9297
- *
9298
- * @return string
9299
- */
9300
- function get_menu_slug() {
9301
- return $this->_menu->get_slug();
9302
- }
9303
 
9304
- /**
9305
- * @author Vova Feldman (@svovaf)
9306
- * @since 1.0.9
9307
- */
9308
- function _prepare_admin_menu() {
9309
- // if ( ! $this->is_on() ) {
9310
- // return;
9311
- // }
9312
 
9313
- if ( ! $this->has_api_connectivity() && ! $this->is_enable_anonymous() ) {
9314
- $this->_menu->remove_menu_item();
9315
- } else {
9316
- $this->do_action( 'before_admin_menu_init' );
9317
-
9318
- $this->add_menu_action();
9319
- $this->add_submenu_items();
9320
- }
9321
- }
9322
-
9323
- /**
9324
- * Admin dashboard menu items modifications.
9325
- *
9326
- * NOTE: admin_menu action executed before admin_init.
9327
- *
9328
- * @author Vova Feldman (@svovaf)
9329
- * @since 1.0.7
9330
- *
9331
- */
9332
- private function add_menu_action() {
9333
- if ( $this->is_activation_mode() ) {
9334
- if ( $this->is_plugin() || ( $this->has_settings_menu() && ! $this->is_free_wp_org_theme() ) ) {
9335
- $this->override_plugin_menu_with_activation();
9336
- } else {
9337
- /**
9338
- * Handle theme opt-in when the opt-in form shows as a dialog box in the themes page.
9339
- */
9340
- if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) ) {
9341
- add_action( 'load-themes.php', array( &$this, '_install_with_current_user' ) );
9342
- } else if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ||
9343
- fs_request_get_bool( 'pending_activation' )
9344
- ) {
9345
- add_action( 'load-themes.php', array( &$this, '_install_with_new_user' ) );
9346
- }
9347
- }
9348
- } else {
9349
- if ( ! $this->is_registered() ) {
9350
- // If not registered try to install user.
9351
- if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) {
9352
- $this->_install_with_new_user();
9353
- }
9354
- } else if (
9355
- fs_request_is_action( 'sync_user' ) &&
9356
- ( ! $this->has_settings_menu() || $this->is_free_wp_org_theme() )
9357
- ) {
9358
- $this->_handle_account_user_sync();
9359
- }
9360
- }
9361
- }
9362
-
9363
- /**
9364
- * @author Vova Feldman (@svovaf)
9365
- * @since 1.0.1
9366
- */
9367
- function _redirect_on_clicked_menu_link() {
9368
- $this->_logger->entrance();
9369
-
9370
- $page = strtolower( isset( $_REQUEST['page'] ) ? $_REQUEST['page'] : '' );
9371
-
9372
- $this->_logger->log( 'page = ' . $page );
9373
-
9374
- foreach ( $this->_menu_items as $priority => $items ) {
9375
- foreach ( $items as $item ) {
9376
- if ( isset( $item['url'] ) ) {
9377
- if ( $page === $this->_menu->get_slug( strtolower( $item['menu_slug'] ) ) ) {
9378
- $this->_logger->log( 'Redirecting to ' . $item['url'] );
9379
-
9380
- fs_redirect( $item['url'] );
9381
- }
9382
- }
9383
- }
9384
- }
9385
- }
9386
-
9387
- /**
9388
- * Remove plugin's all admin menu items & pages, and replace with activation page.
9389
- *
9390
- * @author Vova Feldman (@svovaf)
9391
- * @since 1.0.1
9392
- */
9393
- private function override_plugin_menu_with_activation() {
9394
- $this->_logger->entrance();
9395
-
9396
- $hook = false;
9397
-
9398
- if ( ! $this->_menu->has_menu() ) {
9399
- // Add the opt-in page without a menu item.
9400
- $hook = FS_Admin_Menu_Manager::add_subpage(
9401
- null,
9402
- $this->get_plugin_name(),
9403
- $this->get_plugin_name(),
9404
- 'manage_options',
9405
- $this->_slug,
9406
- array( &$this, '_connect_page_render' )
9407
- );
9408
- } else if ( $this->_menu->is_top_level() ) {
9409
- $hook = $this->_menu->override_menu_item( array( &$this, '_connect_page_render' ) );
9410
-
9411
- if ( false === $hook ) {
9412
- // Create new menu item just for the opt-in.
9413
- $hook = FS_Admin_Menu_Manager::add_page(
9414
- $this->get_plugin_name(),
9415
- $this->get_plugin_name(),
9416
- 'manage_options',
9417
- $this->_menu->get_slug(),
9418
- array( &$this, '_connect_page_render' )
9419
- );
9420
- }
9421
- } else {
9422
- $menus = array( $this->_menu->get_parent_slug() );
9423
-
9424
- if ( $this->_menu->is_override_exact() ) {
9425
- // Make sure the current page is matching the activation page.
9426
- if ( ! $this->is_matching_url( $this->get_activation_url() ) ) {
9427
- return;
9428
- }
9429
- }
9430
-
9431
- foreach ( $menus as $parent_slug ) {
9432
- $hook = $this->_menu->override_submenu_action(
9433
- $parent_slug,
9434
- $this->_menu->get_raw_slug(),
9435
- array( &$this, '_connect_page_render' )
9436
- );
9437
-
9438
- if ( false !== $hook ) {
9439
- // Found plugin's submenu item.
9440
- break;
9441
- }
9442
- }
9443
- }
9444
-
9445
- if ( $this->is_activation_page() ) {
9446
- // Clean admin page from distracting content.
9447
- self::_clean_admin_content_section();
9448
- }
9449
-
9450
- if ( false !== $hook ) {
9451
- if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) ) {
9452
- $this->_install_with_current_user();
9453
- } else if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) {
9454
- $this->_install_with_new_user();
9455
- }
9456
- }
9457
- }
9458
-
9459
- /**
9460
- * @author Leo Fajardo (leorw)
9461
- * @since 1.2.1
9462
- *
9463
- * return string
9464
- */
9465
- function get_top_level_menu_capability() {
9466
- global $menu;
9467
-
9468
- $top_level_menu_slug = $this->get_top_level_menu_slug();
9469
-
9470
- foreach ( $menu as $menu_info ) {
9471
- /**
9472
- * The second element in the menu info array is the capability/role that has access to the menu and the
9473
- * third element is the menu slug.
9474
- */
9475
- if ( $menu_info[2] === $top_level_menu_slug ) {
9476
- return $menu_info[1];
9477
- }
9478
- }
9479
-
9480
- return 'read';
9481
- }
9482
-
9483
- /**
9484
- * @author Vova Feldman (@svovaf)
9485
- * @since 1.0.0
9486
- *
9487
- * @return string
9488
- */
9489
- private function get_top_level_menu_slug() {
9490
- return ( $this->is_addon() ?
9491
- $this->get_parent_instance()->_menu->get_top_level_menu_slug() :
9492
- $this->_menu->get_top_level_menu_slug() );
9493
- }
9494
-
9495
- /**
9496
- * @author Vova Feldman (@svovaf)
9497
- * @since 1.2.2.7
9498
- *
9499
- * @return string
9500
- */
9501
- function get_pricing_cta_label() {
9502
- $label = $this->get_text_inline( 'Upgrade', 'upgrade' );
9503
-
9504
- if ( $this->is_in_trial_promotion() &&
9505
- ! $this->is_paying_or_trial()
9506
- ) {
9507
- // If running a trial promotion, modify the pricing to load the trial.
9508
- $label = $this->get_text_inline( 'Start Trial', 'start-trial' );
9509
- } else if ( $this->is_paying() ) {
9510
- $label = $this->get_text_inline( 'Pricing', 'pricing' );
9511
- }
9512
-
9513
- return $label;
9514
- }
9515
-
9516
- /**
9517
- * @author Vova Feldman (@svovaf)
9518
- * @since 1.2.2.7
9519
- *
9520
- * @return bool
9521
- */
9522
- function is_pricing_page_visible() {
9523
- return (
9524
- // Has at least one paid plan.
9525
- $this->has_paid_plan() &&
9526
- // Didn't ask to hide the pricing page.
9527
- $this->is_page_visible( 'pricing' ) &&
9528
- // Don't have a valid active license or has more than one plan.
9529
- ( ! $this->is_paying() || ! $this->is_single_plan() )
9530
- );
9531
- }
9532
-
9533
- /**
9534
- * Add default Freemius menu items.
9535
- *
9536
- * @author Vova Feldman (@svovaf)
9537
- * @since 1.0.0
9538
- */
9539
- private function add_submenu_items() {
9540
- $this->_logger->entrance();
9541
-
9542
- if ( ! $this->is_addon() ) {
9543
- /**
9544
- * @since 1.2.2.7 Also add submenu items when running in a free .org theme so the tabs will be visible.
9545
- */
9546
- if ( ! $this->is_activation_mode() || $this->is_free_wp_org_theme() ) {
9547
- if ( $this->has_affiliate_program() ) {
9548
- // Add affiliation page.
9549
- $this->add_submenu_item(
9550
- $this->get_text_inline( 'Affiliation', 'affiliation' ),
9551
- array( &$this, '_affiliation_page_render' ),
9552
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Affiliation', 'affiliation' ),
9553
- 'manage_options',
9554
- 'affiliation',
9555
- 'Freemius::_clean_admin_content_section',
9556
- WP_FS__DEFAULT_PRIORITY,
9557
- $this->is_submenu_item_visible( 'affiliation' )
9558
- );
9559
  }
9560
 
9561
- if ( $this->is_registered() ) {
9562
- $show_account = (
9563
- $this->is_submenu_item_visible( 'account' ) &&
9564
- /**
9565
- * @since 1.2.2.7 Don't show the Account for free WP.org themes without any paid plans.
9566
- */
9567
- ( ! $this->is_free_wp_org_theme() || $this->has_paid_plan() )
9568
- );
9569
-
9570
- // Add user account page.
9571
- $this->add_submenu_item(
9572
- $this->get_text_inline( 'Account', 'account' ),
9573
- array( &$this, '_account_page_render' ),
9574
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Account', 'account' ),
9575
- 'manage_options',
9576
- 'account',
9577
- array( &$this, '_account_page_load' ),
9578
- WP_FS__DEFAULT_PRIORITY,
9579
- $show_account
9580
- );
9581
- }
9582
-
9583
- // Add contact page.
9584
- $this->add_submenu_item(
9585
- $this->get_text_inline( 'Contact Us', 'contact-us' ),
9586
- array( &$this, '_contact_page_render' ),
9587
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Contact Us', 'contact-us' ),
9588
- 'manage_options',
9589
- 'contact',
9590
- 'Freemius::_clean_admin_content_section',
9591
- WP_FS__DEFAULT_PRIORITY,
9592
- $this->is_submenu_item_visible( 'contact' )
9593
- );
9594
-
9595
- if ( $this->has_addons() ) {
9596
- $this->add_submenu_item(
9597
- $this->get_text_inline( 'Add-Ons', 'add-ons' ),
9598
- array( &$this, '_addons_page_render' ),
9599
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Add-Ons', 'add-ons' ),
9600
- 'manage_options',
9601
- 'addons',
9602
- array( &$this, '_addons_page_load' ),
9603
- WP_FS__LOWEST_PRIORITY - 1,
9604
- $this->is_submenu_item_visible( 'addons' )
9605
- );
9606
- }
9607
-
9608
- $show_pricing = (
9609
- $this->is_submenu_item_visible( 'pricing' ) &&
9610
- $this->is_pricing_page_visible()
9611
- );
9612
-
9613
- $pricing_cta_text = $this->get_pricing_cta_label();
9614
- $pricing_class = 'upgrade-mode';
9615
- if ( $show_pricing ) {
9616
- if ( $this->is_in_trial_promotion() &&
9617
- ! $this->is_paying_or_trial()
9618
- ) {
9619
- // If running a trial promotion, modify the pricing to load the trial.
9620
- $pricing_class = 'trial-mode';
9621
- } else if ( $this->is_paying() ) {
9622
- $pricing_class = '';
9623
- }
9624
- }
9625
-
9626
- // Add upgrade/pricing page.
9627
- $this->add_submenu_item(
9628
- $pricing_cta_text . '&nbsp;&nbsp;' . ( is_rtl() ? '&#x2190;' : '&#x27a4;' ),
9629
- array( &$this, '_pricing_page_render' ),
9630
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text_x_inline( 'Pricing', 'noun', 'pricing' ),
9631
- 'manage_options',
9632
- 'pricing',
9633
- 'Freemius::_clean_admin_content_section',
9634
- WP_FS__LOWEST_PRIORITY,
9635
- $show_pricing,
9636
- $pricing_class
9637
- );
9638
- }
9639
- }
9640
-
9641
-
9642
- if ( 0 < count( $this->_menu_items ) ) {
9643
- if ( ! $this->_menu->is_top_level() ) {
9644
- fs_enqueue_local_style( 'fs_common', '/admin/common.css' );
9645
-
9646
- // Append submenu items right after the plugin's submenu item.
9647
- $this->order_sub_submenu_items();
9648
- } else {
9649
- // Append submenu items.
9650
- $this->embed_submenu_items();
9651
- }
9652
- }
9653
- }
9654
-
9655
- /**
9656
- * Moved the actual submenu item additions to a separated function,
9657
- * in order to support sub-submenu items when the plugin's settings
9658
- * only have a submenu and not top-level menu item.
9659
- *
9660
- * @author Vova Feldman (@svovaf)
9661
- * @since 1.1.4
9662
- */
9663
- private function embed_submenu_items() {
9664
- $item_template = $this->_menu->is_top_level() ?
9665
- '<span class="fs-submenu-item %s %s %s">%s</span>' :
9666
- '<span class="fs-submenu-item fs-sub %s %s %s">%s</span>';
9667
-
9668
- $top_level_menu_capability = $this->get_top_level_menu_capability();
9669
-
9670
- ksort( $this->_menu_items );
9671
-
9672
- foreach ( $this->_menu_items as $priority => $items ) {
9673
- foreach ( $items as $item ) {
9674
- $capability = ( ! empty( $item['capability'] ) ? $item['capability'] : $top_level_menu_capability );
9675
-
9676
- $menu_item = sprintf(
9677
- $item_template,
9678
- $this->get_unique_affix(),
9679
- $item['menu_slug'],
9680
- ! empty( $item['class'] ) ? $item['class'] : '',
9681
- $item['menu_title']
9682
- );
9683
-
9684
- $menu_slug = $this->_menu->get_slug( $item['menu_slug'] );
9685
-
9686
- if ( ! isset( $item['url'] ) ) {
9687
- $hook = FS_Admin_Menu_Manager::add_subpage(
9688
- $item['show_submenu'] ?
9689
- $this->get_top_level_menu_slug() :
9690
- null,
9691
- $item['page_title'],
9692
- $menu_item,
9693
- $capability,
9694
- $menu_slug,
9695
- $item['render_function']
9696
- );
9697
-
9698
- if ( false !== $item['before_render_function'] ) {
9699
- add_action( "load-$hook", $item['before_render_function'] );
9700
- }
9701
- } else {
9702
- FS_Admin_Menu_Manager::add_subpage(
9703
- $item['show_submenu'] ?
9704
- $this->get_top_level_menu_slug() :
9705
- null,
9706
- $item['page_title'],
9707
- $menu_item,
9708
- $capability,
9709
- $menu_slug,
9710
- array( $this, '' )
9711
- );
9712
- }
9713
- }
9714
- }
9715
- }
9716
-
9717
- /**
9718
- * Re-order the submenu items so all Freemius added new submenu items
9719
- * are added right after the plugin's settings submenu item.
9720
- *
9721
- * @author Vova Feldman (@svovaf)
9722
- * @since 1.1.4
9723
- */
9724
- private function order_sub_submenu_items() {
9725
- global $submenu;
9726
-
9727
- $menu_slug = $this->_menu->get_top_level_menu_slug();
9728
-
9729
- /**
9730
- * Before "admin_menu" fires, WordPress will loop over the default submenus and remove pages for which the user
9731
- * does not have permissions. So in case a plugin does not have top-level menu but does have submenus under any
9732
- * of the default menus, only users that have the right role can access its sub-submenus (Account, Contact Us,
9733
- * Support Forum, etc.) since $submenu[ $menu_slug ] will be empty if the user doesn't have permission.
9734
- *
9735
- * In case a plugin does not have submenus under any of the default menus but does have submenus under the menu
9736
- * of another plugin, only users that have the right role can access its sub-submenus since we will use the
9737
- * capability needed to access the parent menu as the capability for the submenus that we will add.
9738
- */
9739
- if ( empty( $submenu[ $menu_slug ] ) ) {
9740
- return;
9741
- }
9742
-
9743
- $top_level_menu = &$submenu[ $menu_slug ];
9744
-
9745
- $all_submenu_items_after = array();
9746
-
9747
- $found_submenu_item = false;
9748
-
9749
- foreach ( $top_level_menu as $submenu_id => $meta ) {
9750
- if ( $found_submenu_item ) {
9751
- // Remove all submenu items after the plugin's submenu item.
9752
- $all_submenu_items_after[] = $meta;
9753
- unset( $top_level_menu[ $submenu_id ] );
9754
- }
9755
-
9756
- if ( $this->_menu->get_raw_slug() === $meta[2] ) {
9757
- // Found the submenu item, put all below.
9758
- $found_submenu_item = true;
9759
- continue;
9760
- }
9761
- }
9762
-
9763
- // Embed all plugin's new submenu items.
9764
- $this->embed_submenu_items();
9765
-
9766
- // Start with specially high number to make sure it's appended.
9767
- $i = max( 10000, max( array_keys( $top_level_menu ) ) + 1 );
9768
- foreach ( $all_submenu_items_after as $meta ) {
9769
- $top_level_menu[ $i ] = $meta;
9770
- $i ++;
9771
- }
9772
-
9773
- // Sort submenu items.
9774
- ksort( $top_level_menu );
9775
- }
9776
-
9777
- /**
9778
- * Helper method to return the module's support forum URL.
9779
- *
9780
- * @author Vova Feldman (@svovaf)
9781
- * @since 1.2.2.7
9782
- *
9783
- * @return string
9784
- */
9785
- function get_support_forum_url() {
9786
- return $this->apply_filters( 'support_forum_url', "https://wordpress.org/support/{$this->_module_type}/{$this->_slug}" );
9787
- }
9788
-
9789
- /**
9790
- * Displays the Support Forum link when enabled.
9791
- *
9792
- * Can be filtered like so:
9793
- *
9794
- * function _fs_show_support_menu( $is_visible, $menu_id ) {
9795
- * if ( 'support' === $menu_id ) {
9796
- * return _fs->is_registered();
9797
- * }
9798
- * return $is_visible;
9799
- * }
9800
- * _fs()->add_filter('is_submenu_visible', '_fs_show_support_menu', 10, 2);
9801
- *
9802
- */
9803
- function _add_default_submenu_items() {
9804
- if ( ! $this->is_on() ) {
9805
- return;
9806
- }
9807
-
9808
- if ( ! $this->is_activation_mode() ) {
9809
- $this->add_submenu_link_item(
9810
- $this->apply_filters( 'support_forum_submenu', $this->get_text_inline( 'Support Forum', 'support-forum' ) ),
9811
- $this->get_support_forum_url(),
9812
- 'wp-support-forum',
9813
- null,
9814
- 50,
9815
- $this->is_submenu_item_visible( 'support' )
9816
- );
9817
- }
9818
- }
9819
-
9820
- /**
9821
- * @author Vova Feldman (@svovaf)
9822
- * @since 1.0.1
9823
- *
9824
- * @param string $menu_title
9825
- * @param callable $render_function
9826
- * @param bool|string $page_title
9827
- * @param string $capability
9828
- * @param bool|string $menu_slug
9829
- * @param bool|callable $before_render_function
9830
- * @param int $priority
9831
- * @param bool $show_submenu
9832
- * @param string $class Since 1.2.1.5 can add custom classes to menu items.
9833
- */
9834
- function add_submenu_item(
9835
- $menu_title,
9836
- $render_function,
9837
- $page_title = false,
9838
- $capability = 'manage_options',
9839
- $menu_slug = false,
9840
- $before_render_function = false,
9841
- $priority = WP_FS__DEFAULT_PRIORITY,
9842
- $show_submenu = true,
9843
- $class = ''
9844
- ) {
9845
- $this->_logger->entrance( 'Title = ' . $menu_title );
9846
-
9847
- if ( $this->is_addon() ) {
9848
- $parent_fs = $this->get_parent_instance();
9849
-
9850
- if ( is_object( $parent_fs ) ) {
9851
- $parent_fs->add_submenu_item(
9852
- $menu_title,
9853
- $render_function,
9854
- $page_title,
9855
- $capability,
9856
- $menu_slug,
9857
- $before_render_function,
9858
- $priority,
9859
- $show_submenu,
9860
- $class
9861
- );
9862
-
9863
- return;
9864
- }
9865
- }
9866
-
9867
- if ( ! isset( $this->_menu_items[ $priority ] ) ) {
9868
- $this->_menu_items[ $priority ] = array();
9869
- }
9870
-
9871
- $this->_menu_items[ $priority ][] = array(
9872
- 'page_title' => is_string( $page_title ) ? $page_title : $menu_title,
9873
- 'menu_title' => $menu_title,
9874
- 'capability' => $capability,
9875
- 'menu_slug' => is_string( $menu_slug ) ? $menu_slug : strtolower( $menu_title ),
9876
- 'render_function' => $render_function,
9877
- 'before_render_function' => $before_render_function,
9878
- 'show_submenu' => $show_submenu,
9879
- 'class' => $class,
9880
- );
9881
- }
9882
-
9883
- /**
9884
- * @author Vova Feldman (@svovaf)
9885
- * @since 1.0.1
9886
- *
9887
- * @param string $menu_title
9888
- * @param string $url
9889
- * @param bool $menu_slug
9890
- * @param string $capability
9891
- * @param int $priority
9892
- * @param bool $show_submenu
9893
- */
9894
- function add_submenu_link_item(
9895
- $menu_title,
9896
- $url,
9897
- $menu_slug = false,
9898
- $capability = 'read',
9899
- $priority = WP_FS__DEFAULT_PRIORITY,
9900
- $show_submenu = true
9901
- ) {
9902
- $this->_logger->entrance( 'Title = ' . $menu_title . '; Url = ' . $url );
9903
-
9904
- if ( $this->is_addon() ) {
9905
- $parent_fs = $this->get_parent_instance();
9906
-
9907
- if ( is_object( $parent_fs ) ) {
9908
- $parent_fs->add_submenu_link_item(
9909
- $menu_title,
9910
- $url,
9911
- $menu_slug,
9912
- $capability,
9913
- $priority,
9914
- $show_submenu
9915
- );
9916
-
9917
- return;
9918
- }
9919
- }
9920
-
9921
- if ( ! isset( $this->_menu_items[ $priority ] ) ) {
9922
- $this->_menu_items[ $priority ] = array();
9923
- }
9924
-
9925
- $this->_menu_items[ $priority ][] = array(
9926
- 'menu_title' => $menu_title,
9927
- 'capability' => $capability,
9928
- 'menu_slug' => is_string( $menu_slug ) ? $menu_slug : strtolower( $menu_title ),
9929
- 'url' => $url,
9930
- 'page_title' => $menu_title,
9931
- 'render_function' => 'fs_dummy',
9932
- 'before_render_function' => '',
9933
- 'show_submenu' => $show_submenu,
9934
- );
9935
- }
9936
-
9937
- #endregion ------------------------------------------------------------------
9938
-
9939
-
9940
- #--------------------------------------------------------------------------------
9941
- #region Actions / Hooks / Filters
9942
- #--------------------------------------------------------------------------------
9943
-
9944
- /**
9945
- * @author Vova Feldman (@svovaf)
9946
- * @since 1.1.7
9947
- *
9948
- * @param string $tag
9949
- *
9950
- * @return string
9951
- */
9952
- public function get_action_tag( $tag ) {
9953
- return self::get_action_tag_static( $tag, $this->_slug, $this->is_plugin() );
9954
- }
9955
-
9956
- /**
9957
- * @author Vova Feldman (@svovaf)
9958
- * @since 1.2.1.6
9959
- *
9960
- * @param string $tag
9961
- * @param string $slug
9962
- * @param bool $is_plugin
9963
- *
9964
- * @return string
9965
- */
9966
- static function get_action_tag_static( $tag, $slug = '', $is_plugin = true ) {
9967
- $action = "fs_{$tag}";
9968
-
9969
- if ( ! empty( $slug ) ) {
9970
- $action .= '_' . self::get_module_unique_affix( $slug, $is_plugin );
9971
- }
9972
-
9973
- return $action;
9974
- }
9975
-
9976
- /**
9977
- * Returns a string that can be used to generate a unique action name,
9978
- * option name, HTML element ID, or HTML element class.
9979
- *
9980
- * @author Leo Fajardo (@leorw)
9981
- * @since 1.2.2
9982
- *
9983
- * @return string
9984
- */
9985
- public function get_unique_affix() {
9986
- return self::get_module_unique_affix( $this->_slug, $this->is_plugin() );
9987
- }
9988
-
9989
- /**
9990
- * Returns a string that can be used to generate a unique action name,
9991
- * option name, HTML element ID, or HTML element class.
9992
- *
9993
- * @author Vova Feldman (@svovaf)
9994
- * @since 1.2.2.5
9995
- *
9996
- * @param string $slug
9997
- * @param bool $is_plugin
9998
- *
9999
- * @return string
10000
- */
10001
- static function get_module_unique_affix( $slug, $is_plugin = true ) {
10002
- $affix = $slug;
10003
-
10004
- if ( ! $is_plugin ) {
10005
- $affix .= '-' . WP_FS__MODULE_TYPE_THEME;
10006
- }
10007
-
10008
- return $affix;
10009
- }
10010
-
10011
- /**
10012
- * @author Vova Feldman (@svovaf)
10013
- * @since 1.2.1
10014
- * @since 1.2.2.5 The AJAX action names are based on the module ID, not like the non-AJAX actions that are
10015
- * based on the slug for backward compatibility.
10016
- *
10017
- * @param string $tag
10018
- *
10019
- * @return string
10020
- */
10021
- function get_ajax_action( $tag ) {
10022
- return self::get_ajax_action_static( $tag, $this->_module_id );
10023
- }
10024
-
10025
- /**
10026
- * @author Vova Feldman (@svovaf)
10027
- * @since 1.2.1.7
10028
- *
10029
- * @param string $tag
10030
- *
10031
- * @return string
10032
- */
10033
- function get_ajax_security( $tag ) {
10034
- return wp_create_nonce( $this->get_ajax_action( $tag ) );
10035
- }
10036
-
10037
- /**
10038
- * @author Vova Feldman (@svovaf)
10039
- * @since 1.2.1.7
10040
- *
10041
- * @param string $tag
10042
- */
10043
- function check_ajax_referer( $tag ) {
10044
- check_ajax_referer( $this->get_ajax_action( $tag ), 'security' );
10045
- }
10046
-
10047
- /**
10048
- * @author Vova Feldman (@svovaf)
10049
- * @since 1.2.1.6
10050
- * @since 1.2.2.5 The AJAX action names are based on the module ID, not like the non-AJAX actions that are
10051
- * based on the slug for backward compatibility.
10052
- *
10053
- * @param string $tag
10054
- * @param number|null $module_id
10055
- *
10056
- * @return string
10057
- */
10058
- private static function get_ajax_action_static( $tag, $module_id = null ) {
10059
- $action = "fs_{$tag}";
10060
-
10061
- if ( ! empty( $module_id ) ) {
10062
- $action .= "_{$module_id}";
10063
- }
10064
-
10065
- return $action;
10066
- }
10067
-
10068
- /**
10069
- * Do action, specific for the current context plugin.
10070
- *
10071
- * @author Vova Feldman (@svovaf)
10072
- * @since 1.0.1
10073
- *
10074
- * @param string $tag The name of the action to be executed.
10075
- * @param mixed $arg,... Optional. Additional arguments which are passed on to the
10076
- * functions hooked to the action. Default empty.
10077
- *
10078
- * @uses do_action()
10079
- */
10080
- function do_action( $tag, $arg = '' ) {
10081
- $this->_logger->entrance( $tag );
10082
-
10083
- $args = func_get_args();
10084
-
10085
- call_user_func_array( 'do_action', array_merge(
10086
- array( $this->get_action_tag( $tag ) ),
10087
- array_slice( $args, 1 ) )
10088
- );
10089
- }
10090
-
10091
- /**
10092
- * Add action, specific for the current context plugin.
10093
- *
10094
- * @author Vova Feldman (@svovaf)
10095
- * @since 1.0.1
10096
- *
10097
- * @param string $tag
10098
- * @param callable $function_to_add
10099
- * @param int $priority
10100
- * @param int $accepted_args
10101
- *
10102
- * @uses add_action()
10103
- */
10104
- function add_action(
10105
- $tag,
10106
- $function_to_add,
10107
- $priority = WP_FS__DEFAULT_PRIORITY,
10108
- $accepted_args = 1
10109
- ) {
10110
- $this->_logger->entrance( $tag );
10111
-
10112
- add_action( $this->get_action_tag( $tag ), $function_to_add, $priority, $accepted_args );
10113
- }
10114
-
10115
- /**
10116
- * Add AJAX action, specific for the current context plugin.
10117
- *
10118
- * @author Vova Feldman (@svovaf)
10119
- * @since 1.2.1
10120
- *
10121
- * @param string $tag
10122
- * @param callable $function_to_add
10123
- * @param int $priority
10124
- *
10125
- * @uses add_action()
10126
- *
10127
- * @return bool True if action added, false if no need to add the action since the AJAX call isn't matching.
10128
- */
10129
- function add_ajax_action(
10130
- $tag,
10131
- $function_to_add,
10132
- $priority = WP_FS__DEFAULT_PRIORITY
10133
- ) {
10134
- $this->_logger->entrance( $tag );
10135
-
10136
- return self::add_ajax_action_static(
10137
- $tag,
10138
- $function_to_add,
10139
- $priority,
10140
- $this->_module_id
10141
- );
10142
- }
10143
-
10144
- /**
10145
- * Add AJAX action.
10146
- *
10147
- * @author Vova Feldman (@svovaf)
10148
- * @since 1.2.1.6
10149
- *
10150
- * @param string $tag
10151
- * @param callable $function_to_add
10152
- * @param int $priority
10153
- * @param number|null $module_id
10154
- *
10155
- * @return bool True if action added, false if no need to add the action since the AJAX call isn't matching.
10156
- * @uses add_action()
10157
- *
10158
- */
10159
- static function add_ajax_action_static(
10160
- $tag,
10161
- $function_to_add,
10162
- $priority = WP_FS__DEFAULT_PRIORITY,
10163
- $module_id = null
10164
- ) {
10165
- self::$_static_logger->entrance( $tag );
10166
-
10167
- if ( ! self::is_ajax_action_static( $tag, $module_id ) ) {
10168
- return false;
10169
- }
10170
-
10171
- add_action(
10172
- 'wp_ajax_' . self::get_ajax_action_static( $tag, $module_id ),
10173
- $function_to_add,
10174
- $priority,
10175
- 0
10176
- );
10177
-
10178
- self::$_static_logger->info( "$tag AJAX callback action added." );
10179
-
10180
- return true;
10181
- }
10182
-
10183
- /**
10184
- * Send a JSON response back to an Ajax request.
10185
- *
10186
- * @author Vova Feldman (@svovaf)
10187
- * @since 1.2.1.5
10188
- *
10189
- * @param mixed $response
10190
- */
10191
- static function shoot_ajax_response( $response ) {
10192
- wp_send_json( $response );
10193
- }
10194
-
10195
- /**
10196
- * Send a JSON response back to an Ajax request, indicating success.
10197
- *
10198
- * @author Vova Feldman (@svovaf)
10199
- * @since 1.2.1.5
10200
- *
10201
- * @param mixed $data Data to encode as JSON, then print and exit.
10202
- */
10203
- static function shoot_ajax_success( $data = null ) {
10204
- wp_send_json_success( $data );
10205
- }
10206
-
10207
- /**
10208
- * Send a JSON response back to an Ajax request, indicating failure.
10209
- *
10210
- * @author Vova Feldman (@svovaf)
10211
- * @since 1.2.1.5
10212
- *
10213
- * @param mixed $error Optional error message.
10214
- */
10215
- static function shoot_ajax_failure( $error = '' ) {
10216
- $result = array( 'success' => false );
10217
- if ( ! empty( $error ) ) {
10218
- $result['error'] = $error;
10219
- }
10220
-
10221
- wp_send_json( $result );
10222
- }
10223
-
10224
- /**
10225
- * Apply filter, specific for the current context plugin.
10226
- *
10227
- * @author Vova Feldman (@svovaf)
10228
- * @since 1.0.9
10229
- *
10230
- * @param string $tag The name of the filter hook.
10231
- * @param mixed $value The value on which the filters hooked to `$tag` are applied on.
10232
- *
10233
- * @return mixed The filtered value after all hooked functions are applied to it.
10234
- *
10235
- * @uses apply_filters()
10236
- */
10237
- function apply_filters( $tag, $value ) {
10238
- $this->_logger->entrance( $tag );
10239
-
10240
- $args = func_get_args();
10241
- array_unshift( $args, $this->get_unique_affix() );
10242
-
10243
- return call_user_func_array( 'fs_apply_filter', $args );
10244
- }
10245
-
10246
- /**
10247
- * Add filter, specific for the current context plugin.
10248
- *
10249
- * @author Vova Feldman (@svovaf)
10250
- * @since 1.0.9
10251
- *
10252
- * @param string $tag
10253
- * @param callable $function_to_add
10254
- * @param int $priority
10255
- * @param int $accepted_args
10256
- *
10257
- * @uses add_filter()
10258
- */
10259
- function add_filter( $tag, $function_to_add, $priority = WP_FS__DEFAULT_PRIORITY, $accepted_args = 1 ) {
10260
- $this->_logger->entrance( $tag );
10261
-
10262
- add_filter( $this->get_action_tag( $tag ), $function_to_add, $priority, $accepted_args );
10263
- }
10264
-
10265
- /**
10266
- * Check if has filter.
10267
- *
10268
- * @author Vova Feldman (@svovaf)
10269
- * @since 1.1.4
10270
- *
10271
- * @param string $tag
10272
- * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
10273
- *
10274
- * @return false|int
10275
- *
10276
- * @uses has_filter()
10277
- */
10278
- function has_filter( $tag, $function_to_check = false ) {
10279
- $this->_logger->entrance( $tag );
10280
-
10281
- return has_filter( $this->get_action_tag( $tag ), $function_to_check );
10282
- }
10283
-
10284
- #endregion
10285
-
10286
- /**
10287
- * Override default i18n text phrases.
10288
- *
10289
- * @author Vova Feldman (@svovaf)
10290
- * @since 1.1.6
10291
- *
10292
- * @param string[] string $key_value
10293
- *
10294
- * @uses fs_override_i18n()
10295
- */
10296
- function override_i18n( $key_value ) {
10297
- fs_override_i18n( $key_value, $this->_slug );
10298
- }
10299
-
10300
- /* Account Page
10301
- ------------------------------------------------------------------------------------------------------------------*/
10302
- /**
10303
- * Update site information.
10304
- *
10305
- * @author Vova Feldman (@svovaf)
10306
- * @since 1.0.1
10307
- *
10308
- * @param bool $store Flush to Database if true.
10309
- */
10310
- private function _store_site( $store = true ) {
10311
- $this->_logger->entrance();
10312
 
10313
- if ( empty( $this->_site->id ) ) {
10314
- $this->_logger->error( "Empty install ID, can't store site." );
10315
 
10316
- return;
10317
- }
10318
 
10319
- $encrypted_site = clone $this->_site;
10320
- $encrypted_site->plan = self::_encrypt_entity( $this->_site->plan );
 
 
 
10321
 
10322
- $sites = self::get_all_sites( $this->_module_type );
 
 
 
 
 
 
 
 
 
 
 
 
10323
 
10324
- if ( empty( $this->_storage->prev_user_id ) && $this->_user->id != $this->_site->user_id ) {
10325
- /**
10326
- * Store the current user ID as the previous user ID so that the previous user can be used
10327
- * as the install's owner while the new owner's details are not yet available.
10328
- *
10329
- * This will be executed only in the `replica` site. For example, there are 2 sites, namely `original`
10330
- * and `replica`, then an ownership change was initiated and completed in the `original`, the `replica`
10331
- * will be using the previous user until it is updated again (e.g.: until the next clone of `original`
10332
- * into `replica`.
10333
- *
10334
- * @author Leo Fajardo (@leorw)
10335
- */
10336
- $this->_storage->prev_user_id = $sites[ $this->_slug ]->user_id;
10337
- }
10338
-
10339
- $sites[ $this->_slug ] = $encrypted_site;
10340
-
10341
- $this->set_account_option( 'sites', $sites, $store );
10342
- }
10343
-
10344
- /**
10345
- * Update plugin's plans information.
10346
- *
10347
- * @author Vova Feldman (@svovaf)
10348
- * @since 1.0.2
10349
- *
10350
- * @param bool $store Flush to Database if true.
10351
- */
10352
- private function _store_plans( $store = true ) {
10353
- $this->_logger->entrance();
10354
-
10355
- $plans = self::get_all_plans( $this->_module_type );
10356
-
10357
- // Copy plans.
10358
- $encrypted_plans = array();
10359
- for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
10360
- $encrypted_plans[] = self::_encrypt_entity( $this->_plans[ $i ] );
10361
- }
10362
-
10363
- $plans[ $this->_slug ] = $encrypted_plans;
10364
-
10365
- $this->set_account_option( 'plans', $plans, $store );
10366
- }
10367
-
10368
- /**
10369
- * Update user's plugin licenses.
10370
- *
10371
- * @author Vova Feldman (@svovaf)
10372
- * @since 1.0.5
10373
- *
10374
- * @param bool $store
10375
- * @param string|bool $plugin_slug
10376
- * @param FS_Plugin_License[] $licenses
10377
- */
10378
- private function _store_licenses( $store = true, $plugin_slug = false, $licenses = array() ) {
10379
- $this->_logger->entrance();
10380
-
10381
- $all_licenses = self::get_all_licenses( $this->_module_type );
10382
-
10383
- if ( ! is_string( $plugin_slug ) ) {
10384
- $plugin_slug = $this->_slug;
10385
- $licenses = $this->_licenses;
10386
- }
10387
-
10388
- if ( ! isset( $all_licenses[ $plugin_slug ] ) ) {
10389
- $all_licenses[ $plugin_slug ] = array();
10390
- }
10391
-
10392
- $all_licenses[ $plugin_slug ][ $this->_user->id ] = $licenses;
10393
-
10394
- $this->set_account_option( 'licenses', $all_licenses, $store );
10395
- }
10396
-
10397
- /**
10398
- * Update user information.
10399
- *
10400
- * @author Vova Feldman (@svovaf)
10401
- * @since 1.0.1
10402
- *
10403
- * @param bool $store Flush to Database if true.
10404
- */
10405
- private function _store_user( $store = true ) {
10406
- $this->_logger->entrance();
10407
-
10408
- if ( empty( $this->_user->id ) ) {
10409
- $this->_logger->error( "Empty user ID, can't store user." );
10410
-
10411
- return;
10412
- }
10413
-
10414
- $users = self::get_all_users();
10415
- $users[ $this->_user->id ] = $this->_user;
10416
- self::$_accounts->set_option( 'users', $users, $store );
10417
- }
10418
-
10419
- /**
10420
- * Update new updates information.
10421
- *
10422
- * @author Vova Feldman (@svovaf)
10423
- * @since 1.0.4
10424
- *
10425
- * @param FS_Plugin_Tag|null $update
10426
- * @param bool $store Flush to Database if true.
10427
- * @param bool|number $plugin_id
10428
- */
10429
- private function _store_update( $update, $store = true, $plugin_id = false ) {
10430
- $this->_logger->entrance();
10431
-
10432
- if ( $update instanceof FS_Plugin_Tag ) {
10433
- $update->updated = time();
10434
- }
10435
-
10436
- if ( ! is_numeric( $plugin_id ) ) {
10437
- $plugin_id = $this->_plugin->id;
10438
- }
10439
-
10440
- $updates = self::get_all_updates();
10441
- $updates[ $plugin_id ] = $update;
10442
- self::$_accounts->set_option( 'updates', $updates, $store );
10443
- }
10444
-
10445
- /**
10446
- * Update new updates information.
10447
- *
10448
- * @author Vova Feldman (@svovaf)
10449
- * @since 1.0.6
10450
- *
10451
- * @param FS_Plugin[] $plugin_addons
10452
- * @param bool $store Flush to Database if true.
10453
- */
10454
- private function _store_addons( $plugin_addons, $store = true ) {
10455
- $this->_logger->entrance();
10456
-
10457
- $addons = self::get_all_addons();
10458
- $addons[ $this->_plugin->id ] = $plugin_addons;
10459
- self::$_accounts->set_option( 'addons', $addons, $store );
10460
- }
10461
-
10462
- /**
10463
- * Delete plugin's associated add-ons.
10464
- *
10465
- * @author Vova Feldman (@svovaf)
10466
- * @since 1.0.8
10467
- *
10468
- * @param bool $store
10469
- *
10470
- * @return bool
10471
- */
10472
- private function _delete_account_addons( $store = true ) {
10473
- $all_addons = self::get_all_account_addons();
10474
-
10475
- if ( ! isset( $all_addons[ $this->_plugin->id ] ) ) {
10476
- return false;
10477
- }
10478
-
10479
- unset( $all_addons[ $this->_plugin->id ] );
10480
-
10481
- self::$_accounts->set_option( 'account_addons', $all_addons, $store );
10482
-
10483
- return true;
10484
- }
10485
-
10486
- /**
10487
- * Update account add-ons list.
10488
- *
10489
- * @author Vova Feldman (@svovaf)
10490
- * @since 1.0.6
10491
- *
10492
- * @param FS_Plugin[] $addons
10493
- * @param bool $store Flush to Database if true.
10494
- */
10495
- private function _store_account_addons( $addons, $store = true ) {
10496
- $this->_logger->entrance();
10497
-
10498
- $all_addons = self::get_all_account_addons();
10499
- $all_addons[ $this->_plugin->id ] = $addons;
10500
- self::$_accounts->set_option( 'account_addons', $all_addons, $store );
10501
- }
10502
-
10503
- /**
10504
- * Store account params in the Database.
10505
- *
10506
- * @author Vova Feldman (@svovaf)
10507
- * @since 1.0.1
10508
- */
10509
- private function _store_account() {
10510
- $this->_logger->entrance();
10511
-
10512
- $this->_store_site( false );
10513
- $this->_store_user( false );
10514
- $this->_store_plans( false );
10515
- $this->_store_licenses( false );
10516
-
10517
- self::$_accounts->store();
10518
- }
10519
-
10520
- /**
10521
- * Sync user's information.
10522
- *
10523
- * @author Vova Feldman (@svovaf)
10524
- * @since 1.0.3
10525
- * @uses FS_Api
10526
- */
10527
- private function _handle_account_user_sync() {
10528
- $this->_logger->entrance();
10529
-
10530
- $api = $this->get_api_user_scope();
10531
-
10532
- // Get user's information.
10533
- $user = $api->get( '/', true );
10534
-
10535
- if ( isset( $user->id ) ) {
10536
- $this->_user->first = $user->first;
10537
- $this->_user->last = $user->last;
10538
- $this->_user->email = $user->email;
10539
-
10540
- $is_menu_item_account_visible = $this->is_submenu_item_visible( 'account' );
10541
-
10542
- if ( $user->is_verified &&
10543
- ( ! isset( $this->_user->is_verified ) || false === $this->_user->is_verified )
10544
- ) {
10545
- $this->_user->is_verified = true;
10546
-
10547
- $this->do_action( 'account_email_verified', $user->email );
10548
-
10549
- $this->_admin_notices->add(
10550
- $this->get_text_inline( 'Your email has been successfully verified - you are AWESOME!', 'email-verified-message' ),
10551
- $this->get_text_x_inline( 'Right on', 'a positive response', 'right-on' ) . '!',
10552
- 'success',
10553
- // Make admin sticky if account menu item is invisible,
10554
- // since the page will be auto redirected to the plugin's
10555
- // main settings page, and the non-sticky message
10556
- // will disappear.
10557
- ! $is_menu_item_account_visible,
10558
- false,
10559
- 'email_verified'
10560
- );
10561
- }
10562
-
10563
- // Flush user details to DB.
10564
- $this->_store_user();
10565
-
10566
- $this->do_action( 'after_account_user_sync', $user );
10567
-
10568
- /**
10569
- * If account menu item is hidden, redirect to plugin's main settings page.
10570
- *
10571
- * @author Vova Feldman (@svovaf)
10572
- * @since 1.1.6
10573
- *
10574
- * @link https://github.com/Freemius/wordpress-sdk/issues/6
10575
- */
10576
- if ( ! $is_menu_item_account_visible ) {
10577
- fs_redirect( $this->_get_admin_page_url() );
10578
- }
10579
- }
10580
- }
10581
-
10582
- /**
10583
- * @param bool $store
10584
- *
10585
- * @return FS_Plugin_Plan|object|false
10586
- */
10587
- private function _enrich_site_plan( $store = true ) {
10588
- // Try to load plan from local cache.
10589
- $plan = $this->_get_plan_by_id( $this->_site->plan->id );
10590
-
10591
- if ( false === $plan ) {
10592
- $plan = $this->_fetch_site_plan();
10593
- }
10594
-
10595
- if ( $plan instanceof FS_Plugin_Plan ) {
10596
- $this->_update_plan( $plan, $store );
10597
- }
10598
-
10599
- return $plan;
10600
- }
10601
-
10602
- /**
10603
- * @author Vova Feldman (@svovaf)
10604
- * @since 1.0.9
10605
- * @uses FS_Api
10606
- *
10607
- * @param bool $store
10608
- *
10609
- * @return FS_Plugin_Plan|object|false
10610
- */
10611
- private function _enrich_site_trial_plan( $store = true ) {
10612
- // Try to load plan from local cache.
10613
- $trial_plan = $this->_get_plan_by_id( $this->_site->trial_plan_id );
10614
-
10615
- if ( false === $trial_plan ) {
10616
- $trial_plan = $this->_fetch_site_plan( $this->_site->trial_plan_id );
10617
- }
10618
-
10619
- if ( $trial_plan instanceof FS_Plugin_Plan ) {
10620
- $this->_storage->store( 'trial_plan', $trial_plan, $store );
10621
- }
10622
-
10623
- return $trial_plan;
10624
- }
10625
-
10626
- /**
10627
- * @author Vova Feldman (@svovaf)
10628
- * @since 1.0.9
10629
- * @uses FS_Api
10630
- *
10631
- * @param number|bool $license_id
10632
- *
10633
- * @return FS_Subscription|object|bool
10634
- */
10635
- private function _fetch_site_license_subscription( $license_id = false ) {
10636
- $this->_logger->entrance();
10637
- $api = $this->get_api_site_scope();
10638
-
10639
- if ( ! is_numeric( $license_id ) ) {
10640
- $license_id = $this->_license->id;
10641
- }
10642
-
10643
- $result = $api->get( "/licenses/{$license_id}/subscriptions.json", true );
10644
-
10645
- return ! isset( $result->error ) ?
10646
- ( ( is_array( $result->subscriptions ) && 0 < count( $result->subscriptions ) ) ?
10647
- new FS_Subscription( $result->subscriptions[0] ) :
10648
- false
10649
- ) :
10650
- $result;
10651
- }
10652
-
10653
- /**
10654
- * @author Vova Feldman (@svovaf)
10655
- * @since 1.0.4
10656
- * @uses FS_Api
10657
- *
10658
- * @param number|bool $plan_id
10659
- *
10660
- * @return FS_Plugin_Plan|object
10661
- */
10662
- private function _fetch_site_plan( $plan_id = false ) {
10663
- $this->_logger->entrance();
10664
- $api = $this->get_api_site_scope();
10665
-
10666
- if ( ! is_numeric( $plan_id ) ) {
10667
- $plan_id = $this->_site->plan->id;
10668
- }
10669
-
10670
- $plan = $api->get( "/plans/{$plan_id}.json", true );
10671
-
10672
- return ! isset( $plan->error ) ? new FS_Plugin_Plan( $plan ) : $plan;
10673
- }
10674
-
10675
- /**
10676
- * @author Vova Feldman (@svovaf)
10677
- * @since 1.0.5
10678
- * @uses FS_Api
10679
- *
10680
- * @return FS_Plugin_Plan[]|object
10681
- */
10682
- private function _fetch_plugin_plans() {
10683
- $this->_logger->entrance();
10684
- $api = $this->get_api_site_scope();
10685
 
10686
- /**
10687
- * @since 1.2.3 When running in DEV mode, retrieve pending plans as well.
10688
- */
10689
- $result = $api->get( '/plans.json?show_pending=' . ( $this->has_secret_key() ? 'true' : 'false' ), true );
 
 
 
 
 
 
10690
 
10691
- if ( $this->is_api_result_object( $result, 'plans' ) && is_array( $result->plans ) ) {
10692
- for ( $i = 0, $len = count( $result->plans ); $i < $len; $i ++ ) {
10693
- $result->plans[ $i ] = new FS_Plugin_Plan( $result->plans[ $i ] );
 
 
 
10694
  }
10695
 
10696
- $result = $result->plans;
 
 
 
 
 
 
 
 
 
 
 
10697
  }
 
10698
 
10699
- return $result;
 
 
 
 
 
 
 
 
 
 
 
10700
  }
10701
 
10702
- /**
10703
- * @author Vova Feldman (@svovaf)
10704
- * @since 1.0.5
10705
- * @uses FS_Api
10706
- *
10707
- * @param number|bool $plugin_id
10708
- * @param number|bool $site_license_id
10709
- *
10710
- * @return FS_Plugin_License[]|object
10711
- */
10712
- private function _fetch_licenses( $plugin_id = false, $site_license_id = false ) {
10713
- $this->_logger->entrance();
10714
-
10715
- $api = $this->get_api_user_scope();
10716
-
10717
- if ( ! is_numeric( $plugin_id ) ) {
10718
- $plugin_id = $this->_plugin->id;
10719
- }
10720
-
10721
- $result = $api->get( "/plugins/{$plugin_id}/licenses.json", true );
10722
-
10723
- $is_site_license_synced = false;
10724
-
10725
- $api_errors = array();
10726
-
10727
- if ( $this->is_api_result_object( $result, 'licenses' ) &&
10728
- is_array( $result->licenses )
10729
- ) {
10730
- for ( $i = 0, $len = count( $result->licenses ); $i < $len; $i ++ ) {
10731
- $result->licenses[ $i ] = new FS_Plugin_License( $result->licenses[ $i ] );
10732
-
10733
- if ( ( ! $is_site_license_synced ) && is_numeric( $site_license_id ) ) {
10734
- $is_site_license_synced = ( $site_license_id == $result->licenses[ $i ]->id );
10735
- }
10736
- }
10737
-
10738
- $result = $result->licenses;
10739
- } else {
10740
- $api_errors[] = $result;
10741
- $result = array();
10742
- }
10743
-
10744
- if ( ! $is_site_license_synced ) {
10745
- $api = $this->get_api_site_scope();
10746
-
10747
- if ( is_numeric( $site_license_id ) ) {
10748
- // Try to retrieve a foreign license that is linked to the install.
10749
- $api_result = $api->call( '/licenses.json' );
10750
-
10751
- if ( $this->is_api_result_object( $api_result, 'licenses' ) &&
10752
- is_array( $api_result->licenses )
10753
- ) {
10754
- $licenses = $api_result->licenses;
10755
-
10756
- if ( ! empty( $licenses ) ) {
10757
- $result[] = new FS_Plugin_License( $licenses[0] );
10758
- }
10759
- } else {
10760
- $api_errors[] = $api_result;
10761
- }
10762
- } else if ( is_object( $this->_license ) ) {
10763
- // Fetch foreign license by ID and license key.
10764
- $license = $api->get( "/licenses/{$this->_license->id}.json?license_key=" .
10765
- urlencode( $this->_license->secret_key ) );
10766
-
10767
- if ( $this->is_api_result_entity( $license ) ) {
10768
- $result[] = new FS_Plugin_License( $license );
10769
- } else {
10770
- $api_errors[] = $license;
10771
- }
10772
- }
10773
- }
10774
-
10775
- if ( is_array( $result ) && 0 < count( $result ) ) {
10776
- // If found at least one license, return license collection even if there are errors.
10777
- return $result;
10778
- }
10779
-
10780
- if ( ! empty( $api_errors ) ) {
10781
- // If found any errors and no licenses, return first error.
10782
- return $api_errors[0];
10783
- }
10784
-
10785
- // Fallback to empty licenses list.
10786
- return $result;
10787
- }
10788
-
10789
- /**
10790
- * @author Vova Feldman (@svovaf)
10791
- * @since 1.2.0
10792
- * @uses FS_Api
10793
- *
10794
- * @param number|bool $plugin_id
10795
- * @param bool $flush
10796
- *
10797
- * @return FS_Payment[]|object
10798
- */
10799
- function _fetch_payments( $plugin_id = false, $flush = false ) {
10800
- $this->_logger->entrance();
10801
-
10802
- $api = $this->get_api_user_scope();
10803
-
10804
- if ( ! is_numeric( $plugin_id ) ) {
10805
- $plugin_id = $this->_plugin->id;
10806
- }
10807
-
10808
- $result = $api->get( "/plugins/{$plugin_id}/payments.json?include_addons=true", $flush );
10809
-
10810
- if ( ! isset( $result->error ) ) {
10811
- for ( $i = 0, $len = count( $result->payments ); $i < $len; $i ++ ) {
10812
- $result->payments[ $i ] = new FS_Payment( $result->payments[ $i ] );
10813
- }
10814
- $result = $result->payments;
10815
- }
10816
-
10817
- return $result;
10818
- }
10819
-
10820
- /**
10821
- * @author Vova Feldman (@svovaf)
10822
- * @since 1.2.1.5
10823
- * @uses FS_Api
10824
- *
10825
- * @param bool $flush
10826
- *
10827
- * @return \FS_Billing|mixed
10828
- */
10829
- function _fetch_billing( $flush = false ) {
10830
- require_once WP_FS__DIR_INCLUDES . '/entities/class-fs-billing.php';
10831
-
10832
- $billing = $this->get_api_user_scope()->get( 'billing.json', $flush );
10833
-
10834
- if ( $this->is_api_result_entity( $billing ) ) {
10835
- $billing = new FS_Billing( $billing );
10836
- }
10837
-
10838
- return $billing;
10839
- }
10840
-
10841
- /**
10842
- * @author Vova Feldman (@svovaf)
10843
- * @since 1.0.4
10844
- *
10845
- * @param FS_Plugin_Plan $plan
10846
- * @param bool $store
10847
- */
10848
- private function _update_plan( $plan, $store = false ) {
10849
- $this->_logger->entrance();
10850
-
10851
- $this->_site->plan = $plan;
10852
- $this->_store_site( $store );
10853
- }
10854
-
10855
- /**
10856
- * @author Vova Feldman (@svovaf)
10857
- * @since 1.0.5
10858
- *
10859
- * @param FS_Plugin_License[] $licenses
10860
- * @param string|bool $plugin_slug
10861
- */
10862
- private function _update_licenses( $licenses, $plugin_slug = false ) {
10863
- $this->_logger->entrance();
10864
-
10865
- if ( is_array( $licenses ) ) {
10866
- for ( $i = 0, $len = count( $licenses ); $i < $len; $i ++ ) {
10867
- $licenses[ $i ]->updated = time();
10868
- }
10869
- }
10870
-
10871
- if ( ! is_string( $plugin_slug ) ) {
10872
- $this->_licenses = $licenses;
10873
- }
10874
-
10875
- $this->_store_licenses( true, $plugin_slug, $licenses );
10876
- }
10877
-
10878
- /**
10879
- * @author Vova Feldman (@svovaf)
10880
- * @since 1.0.4
10881
- *
10882
- * @param bool|number $plugin_id
10883
- * @param bool $flush Since 1.1.7.3
10884
- * @param int $expiration Since 1.2.2.7
10885
- *
10886
- * @return object|false New plugin tag info if exist.
10887
- */
10888
- private function _fetch_newer_version( $plugin_id = false, $flush = true, $expiration = WP_FS__TIME_24_HOURS_IN_SEC ) {
10889
- $latest_tag = $this->_fetch_latest_version( $plugin_id, $flush, $expiration );
10890
-
10891
- if ( ! is_object( $latest_tag ) ) {
10892
- return false;
10893
- }
10894
-
10895
- // Check if version is actually newer.
10896
- $has_new_version =
10897
- // If it's an non-installed add-on then always return latest.
10898
- ( $this->_is_addon_id( $plugin_id ) && ! $this->is_addon_activated( $plugin_id ) ) ||
10899
- // Compare versions.
10900
- version_compare( $this->get_plugin_version(), $latest_tag->version, '<' );
10901
-
10902
- $this->_logger->departure( $has_new_version ? 'Found newer plugin version ' . $latest_tag->version : 'No new version' );
10903
-
10904
- return $has_new_version ? $latest_tag : false;
10905
- }
10906
-
10907
- /**
10908
- * @author Vova Feldman (@svovaf)
10909
- * @since 1.0.5
10910
- *
10911
- * @param bool|number $plugin_id
10912
- * @param bool $flush Since 1.1.7.3
10913
- * @param int $expiration Since 1.2.2.7
10914
- *
10915
- * @return bool|FS_Plugin_Tag
10916
- */
10917
- function get_update( $plugin_id = false, $flush = true, $expiration = WP_FS__TIME_24_HOURS_IN_SEC ) {
10918
- $this->_logger->entrance();
10919
-
10920
- if ( ! is_numeric( $plugin_id ) ) {
10921
- $plugin_id = $this->_plugin->id;
10922
- }
10923
-
10924
- $this->check_updates( true, $plugin_id, $flush, $expiration );
10925
- $updates = $this->get_all_updates();
10926
-
10927
- return isset( $updates[ $plugin_id ] ) && is_object( $updates[ $plugin_id ] ) ? $updates[ $plugin_id ] : false;
10928
- }
10929
-
10930
- /**
10931
- * Check if site assigned with active license.
10932
- *
10933
- * @author Vova Feldman (@svovaf)
10934
- * @since 1.0.6
10935
- *
10936
- * @deprecated Please use has_active_valid_license() instead because license can be cancelled.
10937
- */
10938
- function has_active_license() {
10939
- return (
10940
- is_object( $this->_license ) &&
10941
- is_numeric( $this->_license->id ) &&
10942
- ! $this->_license->is_expired()
10943
- );
10944
- }
10945
-
10946
- /**
10947
- * Check if site assigned with active & valid (not expired) license.
10948
- *
10949
- * @author Vova Feldman (@svovaf)
10950
- * @since 1.2.1
10951
- */
10952
- function has_active_valid_license() {
10953
- return (
10954
- is_object( $this->_license ) &&
10955
- is_numeric( $this->_license->id ) &&
10956
- $this->_license->is_active() &&
10957
- $this->_license->is_valid()
10958
- );
10959
- }
10960
-
10961
- /**
10962
- * Check if site assigned with license with enabled features.
10963
- *
10964
- * @author Vova Feldman (@svovaf)
10965
- * @since 1.0.6
10966
- *
10967
- * @return bool
10968
- */
10969
- function has_features_enabled_license() {
10970
- return (
10971
- is_object( $this->_license ) &&
10972
- is_numeric( $this->_license->id ) &&
10973
- $this->_license->is_features_enabled()
10974
- );
10975
- }
10976
-
10977
- /**
10978
- * Check if user is a trial or have feature enabled license.
10979
- *
10980
- * @author Vova Feldman (@svovaf)
10981
- * @since 1.1.7
10982
- *
10983
- * @return bool
10984
- */
10985
- function can_use_premium_code() {
10986
- return $this->is_trial() || $this->has_features_enabled_license();
10987
- }
10988
-
10989
- /**
10990
- * Checks if the current user can activate plugins or switch themes. Note that this method should only be used
10991
- * after the `init` action is triggered because it is using `current_user_can()` which is only functional after
10992
- * the context user is authenticated.
10993
- *
10994
- * @author Leo Fajardo (@leorw)
10995
- * @since 1.2.2
10996
- *
10997
- * @return bool
10998
- */
10999
- function is_user_admin() {
11000
- return ( $this->is_plugin() && current_user_can( 'activate_plugins' ) )
11001
- || ( $this->is_theme() && current_user_can( 'switch_themes' ) );
11002
- }
11003
-
11004
- /**
11005
- * Sync site's plan.
11006
- *
11007
- * @author Vova Feldman (@svovaf)
11008
- * @since 1.0.3
11009
- *
11010
- * @uses FS_Api
11011
- *
11012
- * @param bool $background Hints the method if it's a background sync. If false, it means that was initiated by
11013
- * the admin.
11014
- */
11015
- private function _sync_license( $background = false ) {
11016
- $this->_logger->entrance();
11017
-
11018
- $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
11019
-
11020
- $is_addon_sync = ( ! $this->_plugin->is_addon() && $plugin_id != $this->get_id() );
11021
-
11022
- if ( $is_addon_sync ) {
11023
- $this->_sync_addon_license( $plugin_id, $background );
11024
- } else {
11025
- $this->_sync_plugin_license( $background );
11026
- }
11027
-
11028
- $this->do_action( 'after_account_plan_sync', $this->_site->plan->name );
11029
- }
11030
-
11031
- /**
11032
- * Sync plugin's add-on license.
11033
- *
11034
- * @author Vova Feldman (@svovaf)
11035
- * @since 1.0.6
11036
- * @uses FS_Api
11037
- *
11038
- * @param number $addon_id
11039
- * @param bool $background
11040
- */
11041
- private function _sync_addon_license( $addon_id, $background ) {
11042
- $this->_logger->entrance();
11043
-
11044
- if ( $this->is_addon_activated( $addon_id ) ) {
11045
- // If already installed, use add-on sync.
11046
- $fs_addon = self::get_instance_by_id( $addon_id );
11047
- $fs_addon->_sync_license( $background );
11048
-
11049
- return;
11050
- }
11051
-
11052
- // Validate add-on exists.
11053
- $addon = $this->get_addon( $addon_id );
11054
-
11055
- if ( ! is_object( $addon ) ) {
11056
- return;
11057
- }
11058
-
11059
- // Add add-on into account add-ons.
11060
- $account_addons = $this->get_account_addons();
11061
- if ( ! is_array( $account_addons ) ) {
11062
- $account_addons = array();
11063
- }
11064
- $account_addons[] = $addon->id;
11065
- $account_addons = array_unique( $account_addons );
11066
- $this->_store_account_addons( $account_addons );
11067
-
11068
- // Load add-on licenses.
11069
- $licenses = $this->_fetch_licenses( $addon->id );
11070
-
11071
- // Sync add-on licenses.
11072
- if ( $this->is_array_instanceof( $licenses, 'FS_Plugin_License' ) ) {
11073
- $this->_update_licenses( $licenses, $addon->slug );
11074
-
11075
- if ( ! $this->is_addon_installed( $addon->id ) && FS_License_Manager::has_premium_license( $licenses ) ) {
11076
- $plans_result = $this->get_api_site_or_plugin_scope()->get( "/addons/{$addon_id}/plans.json" );
11077
-
11078
- if ( ! isset( $plans_result->error ) ) {
11079
- $plans = array();
11080
- foreach ( $plans_result->plans as $plan ) {
11081
- $plans[] = new FS_Plugin_Plan( $plan );
11082
- }
11083
-
11084
- $this->_admin_notices->add_sticky(
11085
- sprintf(
11086
- ( FS_Plan_Manager::instance()->has_free_plan( $plans ) ?
11087
- $this->get_text_inline( 'Your %s Add-on plan was successfully upgraded.', 'addon-successfully-upgraded-message' ) :
11088
- /* translators: %s:product name, e.g. Facebook add-on was successfully... */
11089
- $this->get_text_inline( '%s Add-on was successfully purchased.', 'addon-successfully-purchased-message' ) ),
11090
- $addon->title
11091
- ) . ' ' . $this->get_latest_download_link(
11092
- $this->get_text_inline( 'Download the latest version', 'download-latest-version' ),
11093
- $addon_id
11094
- ),
11095
- 'addon_plan_upgraded_' . $addon->slug,
11096
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
11097
- );
11098
- }
11099
- }
11100
- }
11101
- }
11102
-
11103
- /**
11104
- * Sync site's plugin plan.
11105
- *
11106
- * @author Vova Feldman (@svovaf)
11107
- * @since 1.0.6
11108
- * @uses FS_Api
11109
- *
11110
- * @param bool $background Hints the method if it's a background sync. If false, it means that was initiated by
11111
- * the admin.
11112
- */
11113
- private function _sync_plugin_license( $background = false ) {
11114
- $this->_logger->entrance();
11115
-
11116
- /**
11117
- * Sync site info.
11118
- *
11119
- * @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.
11120
- */
11121
- $site = $this->send_install_update( array(), true );
11122
-
11123
- $plan_change = 'none';
11124
-
11125
- if ( ! $this->is_api_result_entity( $site ) ) {
11126
- // Show API messages only if not background sync or if paying customer.
11127
- if ( ! $background || $this->is_paying() ) {
11128
- // Try to ping API to see if not blocked.
11129
- if ( ! FS_Api::test() ) {
11130
- /**
11131
- * Failed to ping API - blocked!
11132
- *
11133
- * @author Vova Feldman (@svovaf)
11134
- * @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.
11135
- */
11136
- $api = $this->get_api_site_scope();
11137
-
11138
- if ( ! self::$_global_admin_notices->has_sticky( 'api_blocked' ) ) {
11139
- self::$_global_admin_notices->add(
11140
- sprintf(
11141
- $this->get_text_x_inline( 'Your server is blocking the access to Freemius\' API, which is crucial for %1s synchronization. Please contact your host to whitelist %2s', '%1s - plugin title, %2s - API domain', 'server-blocking-access' ),
11142
- $this->get_plugin_name(),
11143
- '<a href="' . $api->get_url() . '" target="_blank">' . $api->get_url() . '</a>'
11144
- ) . '<br> ' . $this->get_text_inline( 'Error received from the server:', 'server-error-message' ) . var_export( $site->error, true ),
11145
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
11146
- 'error',
11147
- $background,
11148
- false,
11149
- 'api_blocked'
11150
- );
11151
- }
11152
- } else {
11153
- // Authentication params are broken.
11154
- $this->_admin_notices->add(
11155
- $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' ),
11156
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
11157
- 'error'
11158
- );
11159
- }
11160
- }
11161
-
11162
- // No reason to continue with license sync while there are API issues.
11163
- return;
11164
- }
11165
-
11166
- // Remove sticky API connectivity message.
11167
- self::$_global_admin_notices->remove_sticky( 'api_blocked' );
11168
-
11169
- $site = new FS_Site( $site );
11170
-
11171
- // Sync plans.
11172
- $this->_sync_plans();
11173
-
11174
- if ( ! $this->has_paid_plan() ) {
11175
- $this->_site = $site;
11176
- $this->_enrich_site_plan( true );
11177
- $this->_store_site();
11178
- } else {
11179
- /**
11180
- * Sync licenses. Pass the site's license ID so that the foreign licenses will be fetched if the license
11181
- * associated with that ID is not included in the user's licenses collection.
11182
- */
11183
- $this->_sync_licenses( $site->license_id );
11184
-
11185
- // Check if plan / license changed.
11186
- if ( ! FS_Entity::equals( $site->plan, $this->_site->plan ) ||
11187
- // Check if trial started.
11188
- $site->trial_plan_id != $this->_site->trial_plan_id ||
11189
- $site->trial_ends != $this->_site->trial_ends ||
11190
- // Check if license changed.
11191
- $site->license_id != $this->_site->license_id
11192
- ) {
11193
- if ( $site->is_trial() && ( ! $this->_site->is_trial() || $site->trial_ends != $this->_site->trial_ends ) ) {
11194
- // New trial started.
11195
- $this->_site = $site;
11196
- $plan_change = 'trial_started';
11197
-
11198
- // Store trial plan information.
11199
- $this->_enrich_site_trial_plan( true );
11200
-
11201
- // For trial with subscription use-case.
11202
- $new_license = is_null( $site->license_id ) ? null : $this->_get_license_by_id( $site->license_id );
11203
-
11204
- if ( is_object( $new_license ) && $new_license->is_valid() ) {
11205
- $this->_site = $site;
11206
- $this->_update_site_license( $new_license );
11207
- $this->_store_licenses();
11208
- $this->_enrich_site_plan( true );
11209
-
11210
- $this->_sync_site_subscription( $this->_license );
11211
- }
11212
- } else if ( $this->_site->is_trial() && ! $site->is_trial() && ! is_numeric( $site->license_id ) ) {
11213
- // Was in trial, but now trial expired and no license ID.
11214
- // New trial started.
11215
- $this->_site = $site;
11216
- $plan_change = 'trial_expired';
11217
-
11218
- // Clear trial plan information.
11219
- $this->_storage->trial_plan = null;
11220
-
11221
- } else {
11222
- $is_free = $this->is_free_plan();
11223
-
11224
- // Make sure license exist and not expired.
11225
- $new_license = is_null( $site->license_id ) ?
11226
- null :
11227
- $this->_get_license_by_id( $site->license_id );
11228
-
11229
- if ( $is_free && is_null( $new_license ) && $this->has_any_license() && $this->_license->is_cancelled ) {
11230
- // License cancelled.
11231
- $this->_site = $site;
11232
- $this->_update_site_license( $new_license );
11233
- $this->_store_licenses();
11234
- $this->_enrich_site_plan( true );
11235
-
11236
- $plan_change = 'cancelled';
11237
- } else if ( $is_free && ( ( ! is_object( $new_license ) || $new_license->is_expired() ) ) ) {
11238
- // The license is expired, so ignore upgrade method.
11239
- } else {
11240
- // License changed.
11241
- $this->_site = $site;
11242
- $this->_update_site_license( $new_license );
11243
- $this->_store_licenses();
11244
- $this->_enrich_site_plan( true );
11245
-
11246
- $plan_change = $is_free ?
11247
- 'upgraded' :
11248
- ( is_object( $new_license ) ?
11249
- 'changed' :
11250
- 'downgraded' );
11251
- }
11252
- }
11253
-
11254
- // Store updated site info.
11255
- $this->_store_site();
11256
- } else {
11257
- if ( is_object( $this->_license ) && $this->_license->is_expired() ) {
11258
- if ( ! $this->has_features_enabled_license() ) {
11259
- $this->_deactivate_license();
11260
- $plan_change = 'downgraded';
11261
- } else {
11262
- $plan_change = 'expired';
11263
- }
11264
- }
11265
-
11266
- if ( is_numeric( $site->license_id ) && is_object( $this->_license ) ) {
11267
- $this->_sync_site_subscription( $this->_license );
11268
- }
11269
- }
11270
- }
11271
-
11272
- $hmm_text = $this->get_text_x_inline( 'Hmm', 'something somebody says when they are thinking about what you have just said.', 'hmm' ) . '...';
11273
-
11274
- if ( $this->has_paid_plan() ) {
11275
- switch ( $plan_change ) {
11276
- case 'none':
11277
- if ( ! $background && is_admin() ) {
11278
- $plan = $this->is_trial() ?
11279
- $this->_storage->trial_plan :
11280
- $this->_site->plan;
11281
-
11282
- if ( $plan->is_free() ) {
11283
- $this->_admin_notices->add(
11284
- sprintf(
11285
- $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' ),
11286
- '<i><b>' . $plan->title . ( $this->is_trial() ? ' ' . $this->get_text_x_inline( 'Trial', 'trial period', 'trial' ) : '' ) . '</b></i>'
11287
- ) . ' ' . sprintf(
11288
- '<a href="%s">%s</a>',
11289
- $this->contact_url(
11290
- 'bug',
11291
- 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' ),
11292
- strtoupper( $plan->name )
11293
- )
11294
- ),
11295
- $this->get_text_inline( 'Please contact us here', 'contact-us-here' )
11296
- ),
11297
- $hmm_text
11298
- );
11299
- }
11300
- }
11301
- break;
11302
- case 'upgraded':
11303
- $this->_admin_notices->add_sticky(
11304
- sprintf(
11305
- $this->get_text_inline( 'Your plan was successfully upgraded.', 'plan-upgraded-message' ),
11306
- '<i>' . $this->get_plugin_name() . '</i>'
11307
- ) . $this->get_complete_upgrade_instructions(),
11308
- 'plan_upgraded',
11309
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
11310
- );
11311
-
11312
- $this->_admin_notices->remove_sticky( array(
11313
- 'trial_started',
11314
- 'trial_promotion',
11315
- 'trial_expired',
11316
- 'activation_complete',
11317
- ) );
11318
- break;
11319
- case 'changed':
11320
- $this->_admin_notices->add_sticky(
11321
- sprintf(
11322
- $this->get_text_inline( 'Your plan was successfully changed to %s.', 'plan-changed-to-x-message' ),
11323
- $this->_site->plan->title
11324
- ),
11325
- 'plan_changed'
11326
- );
11327
-
11328
- $this->_admin_notices->remove_sticky( array(
11329
- 'trial_started',
11330
- 'trial_promotion',
11331
- 'trial_expired',
11332
- 'activation_complete',
11333
- ) );
11334
- break;
11335
- case 'downgraded':
11336
- $this->_admin_notices->add_sticky(
11337
- 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 ),
11338
- 'license_expired',
11339
- $hmm_text
11340
- );
11341
- $this->_admin_notices->remove_sticky( 'plan_upgraded' );
11342
- break;
11343
- case 'cancelled':
11344
- $this->_admin_notices->add(
11345
- $this->get_text_inline( 'Your license has been cancelled. If you think it\'s a mistake, please contact support.', 'license-cancelled' ) . ' ' .
11346
- sprintf(
11347
- '<a href="%s">%s</a>',
11348
- $this->contact_url( 'bug' ),
11349
- $this->get_text_inline( 'Please contact us here', 'contact-us-here' )
11350
- ),
11351
- $hmm_text,
11352
- 'error'
11353
- );
11354
- $this->_admin_notices->remove_sticky( 'plan_upgraded' );
11355
- break;
11356
- case 'expired':
11357
- $this->_admin_notices->add_sticky(
11358
- sprintf( $this->get_text_inline( 'Your license has expired. You can still continue using all the %s features, but you\'ll need to renew your license to continue getting updates and support.', 'license-expired-non-blocking-message' ), $this->_site->plan->title ),
11359
- 'license_expired',
11360
- $hmm_text
11361
- );
11362
- $this->_admin_notices->remove_sticky( 'plan_upgraded' );
11363
- break;
11364
- case 'trial_started':
11365
- $this->_admin_notices->add_sticky(
11366
- sprintf(
11367
- $this->get_text_inline( 'Your trial has been successfully started.', 'trial-started-message' ),
11368
- '<i>' . $this->get_plugin_name() . '</i>'
11369
- ) . $this->get_complete_upgrade_instructions( $this->_storage->trial_plan->title ),
11370
- 'trial_started',
11371
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
11372
- );
11373
-
11374
- $this->_admin_notices->remove_sticky( array(
11375
- 'trial_promotion',
11376
- ) );
11377
- break;
11378
- case 'trial_expired':
11379
- $this->_admin_notices->add_sticky(
11380
- $this->get_text_inline( 'Your trial has expired. You can still continue using all our free features.', 'trial-expired-message' ),
11381
- 'trial_expired',
11382
- $hmm_text
11383
- );
11384
- $this->_admin_notices->remove_sticky( array(
11385
- 'trial_started',
11386
- 'trial_promotion',
11387
- 'plan_upgraded',
11388
- ) );
11389
- break;
11390
- }
11391
- }
11392
-
11393
- if ( 'none' !== $plan_change ) {
11394
- $this->do_action( 'after_license_change', $plan_change, $this->_site->plan );
11395
- }
11396
- }
11397
-
11398
- /**
11399
- * @author Vova Feldman (@svovaf)
11400
- * @since 1.0.5
11401
- *
11402
- * @param bool $background
11403
- */
11404
- protected function _activate_license( $background = false ) {
11405
- $this->_logger->entrance();
11406
-
11407
- $license_id = fs_request_get( 'license_id' );
11408
-
11409
- if ( FS_Plugin_License::is_valid_id( $license_id ) && $license_id == $this->_site->license_id ) {
11410
- // License is already activated.
11411
- return;
11412
- }
11413
-
11414
- $premium_license = FS_Plugin_License::is_valid_id( $license_id ) ?
11415
- $this->_get_license_by_id( $license_id ) :
11416
- $this->_get_available_premium_license();
11417
-
11418
- if ( ! is_object( $premium_license ) ) {
11419
- return;
11420
- }
11421
-
11422
- /**
11423
- * If the premium license is already associated with the install, just
11424
- * update the license reference (activation is not required).
11425
- *
11426
- * @since 1.1.9
11427
- */
11428
- if ( $premium_license->id == $this->_site->license_id ) {
11429
- // License is already activated.
11430
- $this->_update_site_license( $premium_license );
11431
- $this->_enrich_site_plan( false );
11432
- $this->_store_account();
11433
-
11434
- return;
11435
- }
11436
-
11437
- if ( $this->_site->user_id != $premium_license->user_id ) {
11438
- $api_request_params = array( 'license_key' => $premium_license->secret_key );
11439
- } else {
11440
- $api_request_params = array();
11441
- }
11442
-
11443
- $api = $this->get_api_site_scope();
11444
- $license = $api->call( "/licenses/{$premium_license->id}.json", 'put', $api_request_params );
11445
-
11446
- if ( ! $this->is_api_result_entity( $license ) ) {
11447
- if ( ! $background ) {
11448
- $this->_admin_notices->add( sprintf(
11449
- '%s %s',
11450
- $this->get_text_inline( 'It looks like the license could not be activated.', 'license-activation-failed-message' ),
11451
- ( is_object( $license ) && isset( $license->error ) ?
11452
- $license->error->message :
11453
- sprintf( '%s<br><code>%s</code>',
11454
- $this->get_text_inline( 'Error received from the server:', 'server-error-message' ),
11455
- var_export( $license, true )
11456
- )
11457
- )
11458
- ),
11459
- $this->get_text_x_inline( 'Hmm', 'something somebody says when they are thinking about what you have just said.', 'hmm' ) . '...',
11460
- 'error'
11461
- );
11462
- }
11463
-
11464
- return;
11465
- }
11466
- $premium_license = new FS_Plugin_License( $license );
11467
-
11468
- // Updated site plan.
11469
- $site = $this->get_api_site_scope()->get( '/', true );
11470
- if ( $this->is_api_result_entity( $site ) ) {
11471
- $this->_site = new FS_Site( $site );
11472
- }
11473
- $this->_update_site_license( $premium_license );
11474
- $this->_enrich_site_plan( false );
11475
-
11476
- $this->_store_account();
11477
-
11478
- if ( ! $background ) {
11479
- $this->_admin_notices->add_sticky(
11480
- $this->get_text_inline( 'Your license was successfully activated.', 'license-activated-message' ) .
11481
- $this->get_complete_upgrade_instructions(),
11482
- 'license_activated',
11483
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
11484
- );
11485
- }
11486
-
11487
- $this->_admin_notices->remove_sticky( array(
11488
- 'trial_promotion',
11489
- 'license_expired',
11490
- ) );
11491
- }
11492
-
11493
- /**
11494
- * @author Vova Feldman (@svovaf)
11495
- * @since 1.0.5
11496
- *
11497
- * @param bool $show_notice
11498
- */
11499
- protected function _deactivate_license( $show_notice = true ) {
11500
- $this->_logger->entrance();
11501
-
11502
- $hmm_text = $this->get_text_x_inline( 'Hmm', 'something somebody says when they are thinking about what you have just said.', 'hmm' ) . '...';
11503
-
11504
- if ( ! is_object( $this->_license ) ) {
11505
- $this->_admin_notices->add(
11506
- sprintf( $this->get_text_inline( 'It looks like your site currently doesn\'t have an active license.', 'no-active-license-message' ), $this->_site->plan->title ),
11507
- $hmm_text
11508
- );
11509
-
11510
- return;
11511
- }
11512
-
11513
- $api = $this->get_api_site_scope();
11514
- $license = $api->call( "/licenses/{$this->_site->license_id}.json", 'delete' );
11515
-
11516
- if ( isset( $license->error ) ) {
11517
- $this->_admin_notices->add(
11518
- $this->get_text_inline( 'It looks like the license deactivation failed.', 'license-deactivation-failed-message' ) . '<br> ' .
11519
- $this->get_text_inline( 'Error received from the server:', 'server-error-message' ) . ' ' . var_export( $license->error, true ),
11520
- $hmm_text,
11521
- 'error'
11522
- );
11523
-
11524
- return;
11525
- }
11526
-
11527
- // Update license cache.
11528
- for ( $i = 0, $len = count( $this->_licenses ); $i < $len; $i ++ ) {
11529
- if ( $license->id == $this->_licenses[ $i ]->id ) {
11530
- $this->_licenses[ $i ] = new FS_Plugin_License( $license );
11531
- }
11532
- }
11533
-
11534
- // Updated site plan to default.
11535
- $this->_sync_plans();
11536
- $this->_site->plan->id = $this->_plans[0]->id;
11537
- // Unlink license from site.
11538
- $this->_update_site_license( null );
11539
- $this->_enrich_site_plan( false );
11540
-
11541
- $this->_store_account();
11542
-
11543
- if ( $show_notice ) {
11544
- $this->_admin_notices->add(
11545
- sprintf( $this->get_text_inline( 'Your license was successfully deactivated, you are back to the %s plan.', 'license-deactivation-message' ), $this->_site->plan->title ),
11546
- $this->get_text_inline( 'O.K', 'ok' )
11547
- );
11548
- }
11549
-
11550
- $this->_admin_notices->remove_sticky( array(
11551
- 'plan_upgraded',
11552
- 'license_activated',
11553
- ) );
11554
- }
11555
-
11556
- /**
11557
- * Site plan downgrade.
11558
- *
11559
- * @author Vova Feldman (@svovaf)
11560
- * @since 1.0.4
11561
- *
11562
- * @uses FS_Api
11563
- */
11564
- private function _downgrade_site() {
11565
- $this->_logger->entrance();
11566
-
11567
- $api = $this->get_api_site_scope();
11568
- $site = $api->call( 'downgrade.json', 'put' );
11569
-
11570
- $plan_downgraded = false;
11571
- $plan = false;
11572
- if ( $this->is_api_result_entity( $site ) ) {
11573
- $prev_plan_id = $this->_site->plan->id;
11574
-
11575
- // Update new site plan id.
11576
- $this->_site->plan->id = $site->plan_id;
11577
-
11578
- $plan = $this->_enrich_site_plan();
11579
- $subscription = $this->_sync_site_subscription( $this->_license );
11580
-
11581
- // Plan downgraded if plan was changed or subscription was cancelled.
11582
- $plan_downgraded = ( $plan instanceof FS_Plugin_Plan && $prev_plan_id != $plan->id ) ||
11583
- ( is_object( $subscription ) && ! isset( $subscription->error ) && ! $subscription->is_active() );
11584
- } else {
11585
- // handle different error cases.
11586
-
11587
- }
11588
-
11589
- if ( $plan_downgraded ) {
11590
- // Remove previous sticky message about upgrade (if exist).
11591
- $this->_admin_notices->remove_sticky( 'plan_upgraded' );
11592
-
11593
- $this->_admin_notices->add(
11594
- sprintf( $this->get_text_inline( 'Your plan was successfully downgraded. Your %s plan license will expire in %s.', 'plan-x-downgraded-message' ),
11595
- $plan->title,
11596
- human_time_diff( time(), strtotime( $this->_license->expiration ) )
11597
- )
11598
- );
11599
-
11600
- // Store site updates.
11601
- $this->_store_site();
11602
- } else {
11603
- $this->_admin_notices->add(
11604
- $this->get_text_inline( 'Seems like we are having some temporary issue with your plan downgrade. Please try again in few minutes.', 'plan-downgraded-failure-message' ),
11605
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
11606
- 'error'
11607
- );
11608
- }
11609
- }
11610
-
11611
- /**
11612
- * @author Vova Feldman (@svovaf)
11613
- * @since 1.1.8.1
11614
- *
11615
- * @param bool|string $plan_name
11616
- *
11617
- * @return bool If trial was successfully started.
11618
- */
11619
- function start_trial( $plan_name = false ) {
11620
- $this->_logger->entrance();
11621
-
11622
- // Alias.
11623
- $oops_text = $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...';
11624
-
11625
- if ( $this->is_trial() ) {
11626
- // Already in trial mode.
11627
- $this->_admin_notices->add(
11628
- sprintf( $this->get_text_inline( 'You are already running the %s in a trial mode.', 'in-trial-mode' ), $this->_module_type ),
11629
- $oops_text,
11630
- 'error'
11631
- );
11632
-
11633
- return false;
11634
- }
11635
-
11636
- if ( $this->_site->is_trial_utilized() ) {
11637
- // Trial was already utilized.
11638
- $this->_admin_notices->add(
11639
- $this->get_text_inline( 'You already utilized a trial before.', 'trial-utilized' ),
11640
- $oops_text,
11641
- 'error'
11642
- );
11643
-
11644
- return false;
11645
- }
11646
-
11647
- if ( false !== $plan_name ) {
11648
- $plan = $this->get_plan_by_name( $plan_name );
11649
-
11650
- if ( false === $plan ) {
11651
- // Plan doesn't exist.
11652
- $this->_admin_notices->add(
11653
- sprintf( $this->get_text_inline( 'Plan %s do not exist, therefore, can\'t start a trial.', 'trial-plan-x-not-exist' ), $plan_name ),
11654
- $oops_text,
11655
- 'error'
11656
- );
11657
-
11658
- return false;
11659
- }
11660
-
11661
- if ( ! $plan->has_trial() ) {
11662
- // Plan doesn't exist.
11663
- $this->_admin_notices->add(
11664
- sprintf( $this->get_text_inline( 'Plan %s does not support a trial period.', 'plan-x-no-trial' ), $plan_name ),
11665
- $oops_text,
11666
- 'error'
11667
- );
11668
-
11669
- return false;
11670
- }
11671
- } else {
11672
- if ( ! $this->has_trial_plan() ) {
11673
- // None of the plans have a trial.
11674
- $this->_admin_notices->add(
11675
- sprintf( $this->get_text_inline( 'None of the %s\'s plans supports a trial period.', 'no-trials' ), $this->_module_type ),
11676
- $oops_text,
11677
- 'error'
11678
- );
11679
-
11680
- return false;
11681
- }
11682
-
11683
- $plans_with_trial = FS_Plan_Manager::instance()->get_trial_plans( $this->_plans );
11684
-
11685
- $plan = $plans_with_trial[0];
11686
- }
11687
-
11688
- $api = $this->get_api_site_scope();
11689
- $plan = $api->call( "plans/{$plan->id}/trials.json", 'post' );
11690
-
11691
- if ( ! $this->is_api_result_entity( $plan ) ) {
11692
- // Some API error while trying to start the trial.
11693
- $this->_admin_notices->add(
11694
- sprintf( $this->get_text_inline( 'Unexpected API error. Please contact the %s\'s author with the following error.', 'unexpected-api-error' ), $this->_module_type )
11695
- . ' ' . var_export( $plan, true ),
11696
- $oops_text,
11697
- 'error'
11698
- );
11699
-
11700
- return false;
11701
- }
11702
-
11703
- // Sync license.
11704
- $this->_sync_license();
11705
-
11706
- return $this->is_trial();
11707
- }
11708
-
11709
- /**
11710
- * Cancel site trial.
11711
- *
11712
- * @author Vova Feldman (@svovaf)
11713
- * @since 1.0.9
11714
- *
11715
- * @uses FS_Api
11716
- */
11717
- private function _cancel_trial() {
11718
- $this->_logger->entrance();
11719
-
11720
- // Alias.
11721
- $oops_text = $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...';
11722
-
11723
- if ( ! $this->is_trial() ) {
11724
- $this->_admin_notices->add(
11725
- $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' ),
11726
- $oops_text,
11727
- 'error'
11728
- );
11729
-
11730
- return;
11731
- }
11732
-
11733
- $api = $this->get_api_site_scope();
11734
- $site = $api->call( 'trials.json', 'delete' );
11735
-
11736
- $trial_cancelled = false;
11737
-
11738
- if ( $this->is_api_result_entity( $site ) ) {
11739
- $prev_trial_ends = $this->_site->trial_ends;
11740
-
11741
- if ( $this->is_paid_trial() ) {
11742
- $this->_license->expiration = $site->trial_ends;
11743
- $this->_license->is_cancelled = true;
11744
- $this->_update_site_license( $this->_license );
11745
- $this->_store_licenses();
11746
-
11747
- // Clear subscription reference.
11748
- $this->_sync_site_subscription( null );
11749
- }
11750
-
11751
- // Update site info.
11752
- $this->_site = new FS_Site( $site );
11753
- $this->_enrich_site_plan();
11754
-
11755
- $trial_cancelled = ( $prev_trial_ends != $site->trial_ends );
11756
- } else {
11757
- // handle different error cases.
11758
-
11759
- }
11760
-
11761
- if ( $trial_cancelled ) {
11762
- // Remove previous sticky messages about upgrade or trial (if exist).
11763
- $this->_admin_notices->remove_sticky( array(
11764
- 'trial_started',
11765
- 'trial_promotion',
11766
- 'plan_upgraded',
11767
- ) );
11768
-
11769
- // Store site updates.
11770
- $this->_store_site();
11771
-
11772
- if ( ! $this->is_addon() ||
11773
- ! $this->deactivate_premium_only_addon_without_license( true )
11774
- ) {
11775
- $this->_admin_notices->add(
11776
- sprintf( $this->get_text_inline( 'Your %s free trial was successfully cancelled.', 'trial-cancel-message' ), $this->_storage->trial_plan->title )
11777
- );
11778
- }
11779
-
11780
- // Clear trial plan information.
11781
- unset( $this->_storage->trial_plan );
11782
- } else {
11783
- $this->_admin_notices->add(
11784
- $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' ),
11785
- $oops_text,
11786
- 'error'
11787
- );
11788
- }
11789
- }
11790
-
11791
- /**
11792
- * @author Vova Feldman (@svovaf)
11793
- * @since 1.0.6
11794
- *
11795
- * @param bool|number $plugin_id
11796
- *
11797
- * @return bool
11798
- */
11799
- private function _is_addon_id( $plugin_id ) {
11800
- return is_numeric( $plugin_id ) && ( $this->get_id() != $plugin_id );
11801
- }
11802
-
11803
- /**
11804
- * Check if user eligible to download premium version updates.
11805
- *
11806
- * @author Vova Feldman (@svovaf)
11807
- * @since 1.0.6
11808
- *
11809
- * @return bool
11810
- */
11811
- private function _can_download_premium() {
11812
- return $this->has_active_valid_license() ||
11813
- ( $this->is_trial() && ! $this->get_trial_plan()->is_free() );
11814
- }
11815
-
11816
- /**
11817
- *
11818
- * @author Vova Feldman (@svovaf)
11819
- * @since 1.0.6
11820
- *
11821
- * @param bool|number $addon_id
11822
- * @param string $type "json" or "zip"
11823
- *
11824
- * @return string
11825
- */
11826
- private function _get_latest_version_endpoint( $addon_id = false, $type = 'json' ) {
11827
-
11828
- $is_addon = $this->_is_addon_id( $addon_id );
11829
-
11830
- $is_premium = null;
11831
- if ( ! $is_addon ) {
11832
- $is_premium = $this->_can_download_premium();
11833
- } else if ( $this->is_addon_activated( $addon_id ) ) {
11834
- $is_premium = self::get_instance_by_id( $addon_id )->_can_download_premium();
11835
- }
11836
-
11837
- // If add-on, then append add-on ID.
11838
- $endpoint = ( $is_addon ? "/addons/$addon_id" : '' ) .
11839
- '/updates/latest.' . $type;
11840
-
11841
- // If add-on and not yet activated, try to fetch based on server licensing.
11842
- if ( is_bool( $is_premium ) ) {
11843
- $endpoint = add_query_arg( 'is_premium', json_encode( $is_premium ), $endpoint );
11844
- }
11845
-
11846
- if ( $this->has_secret_key() ) {
11847
- $endpoint = add_query_arg( 'type', 'all', $endpoint );
11848
- }
11849
-
11850
- return $endpoint;
11851
- }
11852
-
11853
- /**
11854
- * @author Vova Feldman (@svovaf)
11855
- * @since 1.0.4
11856
- *
11857
- * @param bool|number $addon_id
11858
- * @param bool $flush Since 1.1.7.3
11859
- * @param int $expiration Since 1.2.2.7
11860
- *
11861
- * @return object|false Plugin latest tag info.
11862
- */
11863
- function _fetch_latest_version( $addon_id = false, $flush = true, $expiration = WP_FS__TIME_24_HOURS_IN_SEC ) {
11864
- $this->_logger->entrance();
11865
-
11866
- /**
11867
- * @since 1.1.7.3 Check for plugin updates from Freemius only if opted-in.
11868
- * @since 1.1.7.4 Also check updates for add-ons.
11869
- */
11870
- if ( ! $this->is_registered() &&
11871
- ! $this->_is_addon_id( $addon_id )
11872
- ) {
11873
- return false;
11874
- }
11875
-
11876
- $tag = $this->get_api_site_or_plugin_scope()->get(
11877
- $this->_get_latest_version_endpoint( $addon_id, 'json' ),
11878
- $flush,
11879
- $expiration
11880
- );
11881
-
11882
- $latest_version = ( is_object( $tag ) && isset( $tag->version ) ) ? $tag->version : 'couldn\'t get';
11883
-
11884
- $this->_logger->departure( 'Latest version ' . $latest_version );
11885
-
11886
- return ( is_object( $tag ) && isset( $tag->version ) ) ? $tag : false;
11887
- }
11888
-
11889
- #----------------------------------------------------------------------------------
11890
- #region Download Plugin
11891
- #----------------------------------------------------------------------------------
11892
-
11893
- /**
11894
- * Download latest plugin version, based on plan.
11895
- *
11896
- * Not like _download_latest(), this will redirect the page
11897
- * to secure download url to prevent dual download (from FS to WP server,
11898
- * and then from WP server to the client / browser).
11899
- *
11900
- * @author Vova Feldman (@svovaf)
11901
- * @since 1.0.9
11902
- *
11903
- * @param bool|number $plugin_id
11904
- *
11905
- * @uses FS_Api
11906
- * @uses wp_redirect()
11907
- */
11908
- private function download_latest_directly( $plugin_id = false ) {
11909
- $this->_logger->entrance();
11910
-
11911
- wp_redirect( $this->get_latest_download_api_url( $plugin_id ) );
11912
- }
11913
-
11914
- /**
11915
- * Get latest plugin FS API download URL.
11916
- *
11917
- * @author Vova Feldman (@svovaf)
11918
- * @since 1.0.9
11919
- *
11920
- * @param bool|number $plugin_id
11921
- *
11922
- * @return string
11923
- */
11924
- private function get_latest_download_api_url( $plugin_id = false ) {
11925
- $this->_logger->entrance();
11926
-
11927
- return $this->get_api_site_scope()->get_signed_url(
11928
- $this->_get_latest_version_endpoint( $plugin_id, 'zip' )
11929
- );
11930
- }
11931
-
11932
- /**
11933
- * Get payment invoice URL.
11934
- *
11935
- * @author Vova Feldman (@svovaf)
11936
- * @since 1.2.0
11937
- *
11938
- * @param bool|number $payment_id
11939
- *
11940
- * @return string
11941
- */
11942
- function _get_invoice_api_url( $payment_id = false ) {
11943
- $this->_logger->entrance();
11944
-
11945
- return $this->get_api_user_scope()->get_signed_url(
11946
- "/payments/{$payment_id}/invoice.pdf"
11947
- );
11948
- }
11949
-
11950
- /**
11951
- * Get latest plugin download link.
11952
- *
11953
- * @author Vova Feldman (@svovaf)
11954
- * @since 1.0.9
11955
- *
11956
- * @param string $label
11957
- * @param bool|number $plugin_id
11958
- *
11959
- * @return string
11960
- */
11961
- private function get_latest_download_link( $label, $plugin_id = false ) {
11962
- return sprintf(
11963
- '<a target="_blank" href="%s">%s</a>',
11964
- $this->_get_latest_download_local_url( $plugin_id ),
11965
- $label
11966
- );
11967
- }
11968
-
11969
- /**
11970
- * Get latest plugin download local URL.
11971
- *
11972
- * @author Vova Feldman (@svovaf)
11973
- * @since 1.0.9
11974
- *
11975
- * @param bool|number $plugin_id
11976
- *
11977
- * @return string
11978
- */
11979
- function _get_latest_download_local_url( $plugin_id = false ) {
11980
- // Add timestamp to protect from caching.
11981
- $params = array( 'ts' => WP_FS__SCRIPT_START_TIME );
11982
-
11983
- if ( ! empty( $plugin_id ) ) {
11984
- $params['plugin_id'] = $plugin_id;
11985
- }
11986
-
11987
- return $this->get_account_url( 'download_latest', $params );
11988
- }
11989
-
11990
- #endregion Download Plugin ------------------------------------------------------------------
11991
-
11992
- /**
11993
- * @author Vova Feldman (@svovaf)
11994
- * @since 1.0.4
11995
- *
11996
- * @uses FS_Api
11997
- *
11998
- * @param bool $background Hints the method if it's a background updates check. If false, it means that
11999
- * was initiated by the admin.
12000
- * @param bool|number $plugin_id
12001
- * @param bool $flush Since 1.1.7.3
12002
- * @param int $expiration Since 1.2.2.7
12003
- */
12004
- private function check_updates(
12005
- $background = false,
12006
- $plugin_id = false,
12007
- $flush = true,
12008
- $expiration = WP_FS__TIME_24_HOURS_IN_SEC
12009
- ) {
12010
- $this->_logger->entrance();
12011
-
12012
- // Check if there's a newer version for download.
12013
- $new_version = $this->_fetch_newer_version( $plugin_id, $flush, $expiration );
12014
-
12015
- $update = null;
12016
- if ( is_object( $new_version ) ) {
12017
- $update = new FS_Plugin_Tag( $new_version );
12018
-
12019
- if ( ! $background ) {
12020
- $this->_admin_notices->add(
12021
- sprintf(
12022
- /* translators: %s: Numeric version number (e.g. '2.1.9' */
12023
- $this->get_text_inline( 'Version %s was released.', 'version-x-released' ) . ' ' . $this->get_text_inline( 'Please download %s.', 'please-download-x' ),
12024
- $update->version,
12025
- sprintf(
12026
- '<a href="%s" target="_blank">%s</a>',
12027
- $this->get_account_url( 'download_latest' ),
12028
- sprintf(
12029
- /* translators: %s: plan name (e.g. latest "Professional" version) */
12030
- $this->get_text_inline( 'the latest %s version here', 'latest-x-version' ),
12031
- $this->_site->plan->title
12032
- )
12033
- )
12034
- ),
12035
- $this->get_text_inline( 'New', 'new' ) . '!'
12036
- );
12037
- }
12038
- } else if ( false === $new_version && ! $background ) {
12039
- $this->_admin_notices->add(
12040
- $this->get_text_inline( 'Seems like you got the latest release.', 'you-have-latest' ),
12041
- $this->get_text_inline( 'You are all good!', 'you-are-good' )
12042
- );
12043
- }
12044
-
12045
- $this->_store_update( $update, true, $plugin_id );
12046
- }
12047
-
12048
- /**
12049
- * @author Vova Feldman (@svovaf)
12050
- * @since 1.0.4
12051
- *
12052
- * @param bool $flush Since 1.1.7.3 add 24 hour cache by default.
12053
- *
12054
- * @return FS_Plugin[]
12055
- *
12056
- * @uses FS_Api
12057
- */
12058
- private function sync_addons( $flush = false ) {
12059
- $this->_logger->entrance();
12060
-
12061
- $api = $this->get_api_site_or_plugin_scope();
12062
-
12063
- /**
12064
- * @since 1.2.1
12065
- *
12066
- * If there's a cached version of the add-ons and not asking
12067
- * for a flush, just use the currently stored add-ons.
12068
- */
12069
- if ( ! $flush && $api->is_cached( '/addons.json?enriched=true' ) ) {
12070
- $addons = self::get_all_addons();
12071
-
12072
- return $addons[ $this->_plugin->id ];
12073
- }
12074
-
12075
- $result = $api->get( '/addons.json?enriched=true', $flush );
12076
-
12077
- $addons = array();
12078
- if ( $this->is_api_result_object( $result, 'plugins' ) &&
12079
- is_array( $result->plugins )
12080
- ) {
12081
- for ( $i = 0, $len = count( $result->plugins ); $i < $len; $i ++ ) {
12082
- $addons[ $i ] = new FS_Plugin( $result->plugins[ $i ] );
12083
- }
12084
-
12085
- $this->_store_addons( $addons, true );
12086
- }
12087
-
12088
- return $addons;
12089
- }
12090
-
12091
- /**
12092
- * Handle user email update.
12093
- *
12094
- * @author Vova Feldman (@svovaf)
12095
- * @since 1.0.3
12096
- * @uses FS_Api
12097
- *
12098
- * @param string $new_email
12099
- *
12100
- * @return object
12101
- */
12102
- private function update_email( $new_email ) {
12103
- $this->_logger->entrance();
12104
-
12105
-
12106
- $api = $this->get_api_user_scope();
12107
- $user = $api->call( "?plugin_id={$this->_plugin->id}&fields=id,email,is_verified", 'put', array(
12108
- 'email' => $new_email,
12109
- 'after_email_confirm_url' => $this->_get_admin_page_url(
12110
- 'account',
12111
- array( 'fs_action' => 'sync_user' )
12112
- ),
12113
- ) );
12114
-
12115
- if ( ! isset( $user->error ) ) {
12116
- $this->_user->email = $user->email;
12117
- $this->_user->is_verified = $user->is_verified;
12118
- $this->_store_user();
12119
- } else {
12120
- // handle different error cases.
12121
-
12122
- }
12123
-
12124
- return $user;
12125
- }
12126
-
12127
- #----------------------------------------------------------------------------------
12128
- #region API Error Handling
12129
- #----------------------------------------------------------------------------------
12130
-
12131
- /**
12132
- * @author Vova Feldman (@svovaf)
12133
- * @since 1.1.1
12134
- *
12135
- * @param mixed $result
12136
- *
12137
- * @return bool Is API result contains an error.
12138
- */
12139
- private function is_api_error( $result ) {
12140
- return FS_Api::is_api_error( $result );
12141
- }
12142
-
12143
- /**
12144
- * Checks if given API result is a non-empty and not an error object.
12145
- *
12146
- * @author Vova Feldman (@svovaf)
12147
- * @since 1.2.1.5
12148
- *
12149
- * @param mixed $result
12150
- * @param string|null $required_property Optional property we want to verify that is set.
12151
- *
12152
- * @return bool
12153
- */
12154
- function is_api_result_object( $result, $required_property = null ) {
12155
- return FS_Api::is_api_result_object( $result, $required_property );
12156
- }
12157
-
12158
- /**
12159
- * Checks if given API result is a non-empty entity object with non-empty ID.
12160
- *
12161
- * @author Vova Feldman (@svovaf)
12162
- * @since 1.2.1.5
12163
- *
12164
- * @param mixed $result
12165
- *
12166
- * @return bool
12167
- */
12168
- private function is_api_result_entity( $result ) {
12169
- return FS_Api::is_api_result_entity( $result );
12170
- }
12171
-
12172
- #endregion
12173
-
12174
- /**
12175
- * Make sure a given argument is an array of a specific type.
12176
- *
12177
- * @author Vova Feldman (@svovaf)
12178
- * @since 1.2.1.5
12179
- *
12180
- * @param mixed $array
12181
- * @param string $class
12182
- *
12183
- * @return bool
12184
- */
12185
- private function is_array_instanceof( $array, $class ) {
12186
- return ( is_array( $array ) && ( empty( $array ) || $array[0] instanceof $class ) );
12187
- }
12188
-
12189
- /**
12190
- * Start install ownership change.
12191
- *
12192
- * @author Vova Feldman (@svovaf)
12193
- * @since 1.1.1
12194
- * @uses FS_Api
12195
- *
12196
- * @param string $candidate_email
12197
- *
12198
- * @return bool Is ownership change successfully initiated.
12199
- */
12200
- private function init_change_owner( $candidate_email ) {
12201
- $this->_logger->entrance();
12202
-
12203
- $api = $this->get_api_site_scope();
12204
- $result = $api->call( "/users/{$this->_user->id}.json", 'put', array(
12205
- 'email' => $candidate_email,
12206
- 'after_confirm_url' => $this->_get_admin_page_url(
12207
- 'account',
12208
- array( 'fs_action' => 'change_owner' )
12209
- ),
12210
- ) );
12211
-
12212
- return ! $this->is_api_error( $result );
12213
- }
12214
-
12215
- /**
12216
- * Handle install ownership change.
12217
- *
12218
- * @author Vova Feldman (@svovaf)
12219
- * @since 1.1.1
12220
- * @uses FS_Api
12221
- *
12222
- * @return bool Was ownership change successfully complete.
12223
- */
12224
- private function complete_change_owner() {
12225
- $this->_logger->entrance();
12226
-
12227
- $site_result = $this->get_api_site_scope( true )->get();
12228
- $site = new FS_Site( $site_result );
12229
- $this->_site = $site;
12230
-
12231
- $user = new FS_User();
12232
- $user->id = fs_request_get( 'user_id' );
12233
-
12234
- // Validate install's user and given user.
12235
- if ( $user->id != $this->_site->user_id ) {
12236
- return false;
12237
- }
12238
-
12239
- $user->public_key = fs_request_get( 'user_public_key' );
12240
- $user->secret_key = fs_request_get( 'user_secret_key' );
12241
-
12242
- // Fetch new user information.
12243
- $this->_user = $user;
12244
- $user_result = $this->get_api_user_scope( true )->get();
12245
- $user = new FS_User( $user_result );
12246
- $this->_user = $user;
12247
-
12248
- $this->_set_account( $user, $site );
12249
-
12250
- return true;
12251
- }
12252
-
12253
- /**
12254
- * Handle user name update.
12255
- *
12256
- * @author Vova Feldman (@svovaf)
12257
- * @since 1.0.9
12258
- * @uses FS_Api
12259
- *
12260
- * @return object
12261
- */
12262
- private function update_user_name() {
12263
- $this->_logger->entrance();
12264
- $name = fs_request_get( 'fs_user_name_' . $this->get_unique_affix(), '' );
12265
-
12266
- $api = $this->get_api_user_scope();
12267
- $user = $api->call( "?plugin_id={$this->_plugin->id}&fields=id,first,last", 'put', array(
12268
- 'name' => $name,
12269
- ) );
12270
-
12271
- if ( ! isset( $user->error ) ) {
12272
- $this->_user->first = $user->first;
12273
- $this->_user->last = $user->last;
12274
- $this->_store_user();
12275
- } else {
12276
- // handle different error cases.
12277
-
12278
- }
12279
-
12280
- return $user;
12281
- }
12282
-
12283
- /**
12284
- * Verify user email.
12285
- *
12286
- * @author Vova Feldman (@svovaf)
12287
- * @since 1.0.3
12288
- * @uses FS_Api
12289
- */
12290
- private function verify_email() {
12291
- $this->_handle_account_user_sync();
12292
-
12293
- if ( $this->_user->is_verified() ) {
12294
- return;
12295
- }
12296
-
12297
- $api = $this->get_api_site_scope();
12298
- $result = $api->call( "/users/{$this->_user->id}/verify.json", 'put', array(
12299
- 'after_email_confirm_url' => $this->_get_admin_page_url(
12300
- 'account',
12301
- array( 'fs_action' => 'sync_user' )
12302
- )
12303
- ) );
12304
-
12305
- if ( ! isset( $result->error ) ) {
12306
- $this->_admin_notices->add( sprintf(
12307
- $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' ),
12308
- sprintf( '<a href="mailto:%1s">%2s</a>', esc_url( $this->_user->email ), $this->_user->email )
12309
- ) );
12310
- } else {
12311
- // handle different error cases.
12312
-
12313
- }
12314
- }
12315
-
12316
- /**
12317
- * @author Vova Feldman (@svovaf)
12318
- * @since 1.1.2
12319
- *
12320
- * @param array $params
12321
- *
12322
- * @return string
12323
- */
12324
- function get_activation_url( $params = array() ) {
12325
- if ( $this->is_addon() && $this->has_free_plan() ) {
12326
- /**
12327
- * @author Vova Feldman (@svovaf)
12328
- * @since 1.2.1.7 Add-on's activation is the parent's module activation.
12329
- */
12330
- return $this->get_parent_instance()->get_activation_url( $params );
12331
- }
12332
-
12333
- return $this->apply_filters( 'connect_url', $this->_get_admin_page_url( '', $params ) );
12334
- }
12335
-
12336
- /**
12337
- * @author Vova Feldman (@svovaf)
12338
- * @since 1.2.1.5
12339
- *
12340
- * @param array $params
12341
- *
12342
- * @return string
12343
- */
12344
- function get_reconnect_url( $params = array() ) {
12345
- $params['fs_action'] = 'reset_anonymous_mode';
12346
- $params['fs_unique_affix'] = $this->get_unique_affix();
12347
-
12348
- return $this->get_activation_url( $params );
12349
- }
12350
-
12351
- /**
12352
- * Get the URL of the page that should be loaded after the user connect
12353
- * or skip in the opt-in screen.
12354
- *
12355
- * @author Vova Feldman (@svovaf)
12356
- * @since 1.1.3
12357
- *
12358
- * @param string $filter Filter name.
12359
- * @param array $params Since 1.2.2.7
12360
- *
12361
- * @return string
12362
- */
12363
- function get_after_activation_url( $filter, $params = array() ) {
12364
- if ( $this->is_free_wp_org_theme() &&
12365
- fs_request_has( 'pending_activation' )
12366
- ) {
12367
- $first_time_path = '';
12368
- } else {
12369
- $first_time_path = $this->_menu->get_first_time_path();
12370
- }
12371
-
12372
- return add_query_arg( $params, $this->apply_filters(
12373
- $filter,
12374
- empty( $first_time_path ) ?
12375
- $this->_get_admin_page_url() :
12376
- $first_time_path
12377
- ) );
12378
- }
12379
-
12380
- /**
12381
- * Handle account page updates / edits / actions.
12382
- *
12383
- * @author Vova Feldman (@svovaf)
12384
- * @since 1.0.2
12385
- *
12386
- */
12387
- private function _handle_account_edits() {
12388
- if ( ! $this->is_user_admin() ) {
12389
- return;
12390
- }
12391
-
12392
- $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
12393
- $action = fs_get_action();
12394
-
12395
- // Alias.
12396
- $oops_text = $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...';
12397
-
12398
- switch ( $action ) {
12399
- case 'delete_account':
12400
- check_admin_referer( $action );
12401
-
12402
- if ( $plugin_id == $this->get_id() ) {
12403
- $this->delete_account_event();
12404
-
12405
- // Clear user and site.
12406
- $this->_site = null;
12407
- $this->_user = null;
12408
-
12409
- fs_redirect( $this->get_activation_url() );
12410
- } else {
12411
- if ( $this->is_addon_activated( $plugin_id ) ) {
12412
- $fs_addon = self::get_instance_by_id( $plugin_id );
12413
- $fs_addon->delete_account_event();
12414
-
12415
- fs_redirect( $this->_get_admin_page_url( 'account' ) );
12416
- }
12417
- }
12418
-
12419
- return;
12420
-
12421
- case 'downgrade_account':
12422
- check_admin_referer( $action );
12423
-
12424
- if ( $plugin_id == $this->get_id() ) {
12425
- $this->_downgrade_site();
12426
- } else if ( $this->is_addon_activated( $plugin_id ) ) {
12427
- $fs_addon = self::get_instance_by_id( $plugin_id );
12428
- $fs_addon->_downgrade_site();
12429
- }
12430
-
12431
- return;
12432
-
12433
- case 'activate_license':
12434
- check_admin_referer( $action );
12435
-
12436
- if ( $plugin_id == $this->get_id() ) {
12437
- $this->_activate_license();
12438
- } else {
12439
- if ( $this->is_addon_activated( $plugin_id ) ) {
12440
- $fs_addon = self::get_instance_by_id( $plugin_id );
12441
- $fs_addon->_activate_license();
12442
- }
12443
- }
12444
-
12445
- return;
12446
-
12447
- case 'deactivate_license':
12448
- check_admin_referer( $action );
12449
-
12450
- if ( $plugin_id == $this->get_id() ) {
12451
- $this->_deactivate_license();
12452
 
12453
- if ( $this->is_only_premium() ) {
12454
- // Clear user and site.
12455
- $this->_site = null;
12456
- $this->_user = null;
 
 
 
 
 
 
 
12457
 
12458
- fs_redirect( $this->get_activation_url() );
12459
- }
12460
- } else {
12461
- if ( $this->is_addon_activated( $plugin_id ) ) {
12462
- $fs_addon = self::get_instance_by_id( $plugin_id );
12463
- $fs_addon->_deactivate_license();
12464
- }
12465
- }
12466
-
12467
- return;
12468
-
12469
- case 'check_updates':
12470
- check_admin_referer( $action );
12471
- $this->check_updates();
12472
-
12473
- return;
12474
-
12475
- case 'change_owner':
12476
- $state = fs_request_get( 'state', 'init' );
12477
- switch ( $state ) {
12478
- case 'init':
12479
- $candidate_email = fs_request_get( 'candidate_email', '' );
12480
-
12481
- if ( $this->init_change_owner( $candidate_email ) ) {
12482
- $this->_admin_notices->add( sprintf( $this->get_text_inline( 'Please check your mailbox, you should receive an email via %s to confirm the ownership change. From security reasons, you must confirm the change within the next 15 min. If you cannot find the email, please check your spam folder.', 'change-owner-request-sent-x' ), '<b>' . $this->_user->email . '</b>' ) );
12483
- }
12484
- break;
12485
- case 'owner_confirmed':
12486
- $candidate_email = fs_request_get( 'candidate_email', '' );
12487
-
12488
- $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>' ) );
12489
- break;
12490
- case 'candidate_confirmed':
12491
- if ( $this->complete_change_owner() ) {
12492
- $this->_admin_notices->add_sticky(
12493
- sprintf( $this->get_text_inline( '%s is the new owner of the account.', 'change-owner-request_candidate-confirmed' ), '<b>' . $this->_user->email . '</b>' ),
12494
- 'ownership_changed',
12495
- $this->get_text_x_inline( 'Congrats', 'as congratulations', 'congrats' ) . '!'
12496
- );
12497
- } else {
12498
- // @todo Handle failed ownership change message.
12499
- }
12500
- break;
12501
- }
12502
-
12503
- return;
12504
-
12505
- case 'update_email':
12506
- check_admin_referer( 'update_email' );
12507
-
12508
- $new_email = fs_request_get( 'fs_email_' . $this->get_unique_affix(), '' );
12509
- $result = $this->update_email( $new_email );
12510
-
12511
- if ( isset( $result->error ) ) {
12512
- switch ( $result->error->code ) {
12513
- case 'user_exist':
12514
- $this->_admin_notices->add(
12515
- $this->get_text_inline( 'Sorry, we could not complete the email update. Another user with the same email is already registered.', 'user-exist-message' ) . ' ' .
12516
- sprintf( $this->get_text_inline( 'If you would like to give up the ownership of the %s\'s account to %s click the Change Ownership button.', 'user-exist-message_ownership' ), $this->_module_type, '<b>' . $new_email . '</b>' ) .
12517
- sprintf(
12518
- '<a style="margin-left: 10px;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
12519
- $this->get_account_url( 'change_owner', array(
12520
- 'state' => 'init',
12521
- 'candidate_email' => $new_email
12522
- ) ),
12523
- $this->get_text_inline( 'Change Ownership', 'change-ownership' )
12524
- ),
12525
- $oops_text,
12526
- 'error'
12527
- );
12528
- break;
12529
- }
12530
- } else {
12531
- $this->_admin_notices->add( $this->get_text_inline( 'Your email was successfully updated. You should receive an email with confirmation instructions in few moments.', 'email-updated-message' ) );
12532
- }
12533
-
12534
- return;
12535
-
12536
- case 'update_user_name':
12537
- check_admin_referer( 'update_user_name' );
12538
-
12539
- $result = $this->update_user_name();
12540
-
12541
- if ( isset( $result->error ) ) {
12542
- $this->_admin_notices->add(
12543
- $this->get_text_inline( 'Please provide your full name.', 'name-update-failed-message' ),
12544
- $oops_text,
12545
- 'error'
12546
- );
12547
- } else {
12548
- $this->_admin_notices->add( $this->get_text_inline( 'Your name was successfully updated.', 'name-updated-message' ) );
12549
- }
12550
-
12551
- return;
12552
-
12553
- #region Actions that might be called from external links (e.g. email)
12554
-
12555
- case 'cancel_trial':
12556
- if ( $plugin_id == $this->get_id() ) {
12557
- $this->_cancel_trial();
12558
- } else {
12559
- if ( $this->is_addon_activated( $plugin_id ) ) {
12560
- $fs_addon = self::get_instance_by_id( $plugin_id );
12561
- $fs_addon->_cancel_trial();
12562
- }
12563
- }
12564
-
12565
- return;
12566
-
12567
- case 'verify_email':
12568
- $this->verify_email();
12569
-
12570
- return;
12571
-
12572
- case 'sync_user':
12573
- $this->_handle_account_user_sync();
12574
-
12575
- return;
12576
-
12577
- case $this->get_unique_affix() . '_sync_license':
12578
- $this->_sync_license();
12579
-
12580
- return;
12581
-
12582
- case 'download_latest':
12583
- $this->download_latest_directly( $plugin_id );
12584
-
12585
- return;
12586
-
12587
- #endregion
12588
- }
12589
-
12590
- if ( WP_FS__IS_POST_REQUEST ) {
12591
- $properties = array( 'site_secret_key', 'site_id', 'site_public_key' );
12592
- foreach ( $properties as $p ) {
12593
- if ( 'update_' . $p === $action ) {
12594
- check_admin_referer( $action );
12595
-
12596
- $this->_logger->log( $action );
12597
-
12598
- $site_property = substr( $p, strlen( 'site_' ) );
12599
- $site_property_value = fs_request_get( 'fs_' . $p . '_' . $this->get_unique_affix(), '' );
12600
- $this->get_site()->{$site_property} = $site_property_value;
12601
-
12602
- // Store account after modification.
12603
- $this->_store_site();
12604
-
12605
- $this->do_action( 'account_property_edit', 'site', $site_property, $site_property_value );
12606
-
12607
- $this->_admin_notices->add( sprintf(
12608
- /* translators: %s: User's account property (e.g. email address, name) */
12609
- $this->get_text_inline( 'You have successfully updated your %s.', 'x-updated' ),
12610
- '<b>' . str_replace( '_', ' ', $p ) . '</b>'
12611
- ) );
12612
-
12613
- return;
12614
- }
12615
- }
12616
- }
12617
- }
12618
-
12619
- /**
12620
- * Account page resources load.
12621
- *
12622
- * @author Vova Feldman (@svovaf)
12623
- * @since 1.0.6
12624
- */
12625
- function _account_page_load() {
12626
- $this->_logger->entrance();
12627
 
12628
- $this->_logger->info( var_export( $_REQUEST, true ) );
 
12629
 
12630
- fs_enqueue_local_style( 'fs_account', '/admin/account.css' );
12631
 
12632
- if ( $this->has_addons() ) {
12633
- wp_enqueue_script( 'plugin-install' );
12634
- add_thickbox();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12635
 
12636
- function fs_addons_body_class( $classes ) {
12637
- $classes .= ' plugins-php';
 
 
 
 
 
 
 
 
 
 
 
 
 
12638
 
12639
- return $classes;
12640
- }
12641
-
12642
- add_filter( 'admin_body_class', 'fs_addons_body_class' );
12643
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12644
 
12645
- if ( $this->has_paid_plan() &&
12646
- ! $this->has_any_license() &&
12647
- ! $this->is_sync_executed() &&
12648
- $this->is_tracking_allowed()
12649
- ) {
12650
- /**
12651
- * If no licenses found and no sync job was executed during the last 24 hours,
12652
- * just execute the sync job right away (blocking execution).
12653
- *
12654
- * @since 1.1.7.3
12655
- */
12656
- $this->run_manual_sync();
12657
- }
12658
 
12659
- $this->_handle_account_edits();
 
 
 
 
 
12660
 
12661
- $this->do_action( 'account_page_load_before_departure' );
12662
- }
 
 
 
 
12663
 
12664
- /**
12665
- * Renders the "Affiliation" page.
12666
- *
12667
- * @author Leo Fajardo (@leorw)
12668
- * @since 1.2.3
12669
- */
12670
- function _affiliation_page_render() {
12671
- $this->_logger->entrance();
12672
 
12673
- fs_enqueue_local_style( 'fs_affiliation', '/admin/affiliation.css' );
 
12674
 
12675
- $vars = array( 'id' => $this->_module_id );
12676
- echo $this->apply_filters( "/forms/affiliation.php", fs_get_template( '/forms/affiliation.php', $vars ) );
12677
- }
12678
-
12679
-
12680
- /**
12681
- * Render account page.
12682
- *
12683
- * @author Vova Feldman (@svovaf)
12684
- * @since 1.0.0
12685
- */
12686
- function _account_page_render() {
12687
- $this->_logger->entrance();
12688
-
12689
- $template = 'account.php';
12690
- $vars = array( 'id' => $this->_module_id );
12691
-
12692
- /**
12693
- * Added filter to the template to allow developers wrapping the template
12694
- * in custom HTML (e.g. within a wizard/tabs).
12695
- *
12696
- * @author Vova Feldman (@svovaf)
12697
- * @since 1.2.1.6
12698
- */
12699
- echo $this->apply_filters( "templates/{$template}", fs_get_template( $template, $vars ) );
12700
- }
12701
-
12702
- /**
12703
- * Render account connect page.
12704
- *
12705
- * @author Vova Feldman (@svovaf)
12706
- * @since 1.0.7
12707
- */
12708
- function _connect_page_render() {
12709
- $this->_logger->entrance();
12710
-
12711
- $vars = array( 'id' => $this->_module_id );
12712
-
12713
- /**
12714
- * Added filter to the template to allow developers wrapping the template
12715
- * in custom HTML (e.g. within a wizard/tabs).
12716
- *
12717
- * @author Vova Feldman (@svovaf)
12718
- * @since 1.2.1.6
12719
- */
12720
- echo $this->apply_filters( 'templates/connect.php', fs_get_template( 'connect.php', $vars ) );
12721
- }
12722
-
12723
- /**
12724
- * Load required resources before add-ons page render.
12725
- *
12726
- * @author Vova Feldman (@svovaf)
12727
- * @since 1.0.6
12728
- */
12729
- function _addons_page_load() {
12730
- $this->_logger->entrance();
12731
-
12732
- fs_enqueue_local_style( 'fs_addons', '/admin/add-ons.css' );
12733
-
12734
- wp_enqueue_script( 'plugin-install' );
12735
- add_thickbox();
12736
-
12737
- function fs_addons_body_class( $classes ) {
12738
- $classes .= ' plugins-php';
12739
-
12740
- return $classes;
12741
- }
12742
-
12743
- add_filter( 'admin_body_class', 'fs_addons_body_class' );
12744
-
12745
- if ( ! $this->is_registered() && $this->is_org_repo_compliant() ) {
12746
- $this->_admin_notices->add(
12747
- 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>' ),
12748
- $this->get_text_x_inline( 'Heads up', 'advance notice of something that will need attention.', 'heads-up' ),
12749
- 'update-nag'
12750
- );
12751
- }
12752
- }
12753
-
12754
- /**
12755
- * Render add-ons page.
12756
- *
12757
- * @author Vova Feldman (@svovaf)
12758
- * @since 1.0.6
12759
- */
12760
- function _addons_page_render() {
12761
- $this->_logger->entrance();
12762
-
12763
- $vars = array( 'id' => $this->_module_id );
12764
-
12765
- /**
12766
- * Added filter to the template to allow developers wrapping the template
12767
- * in custom HTML (e.g. within a wizard/tabs).
12768
- *
12769
- * @author Vova Feldman (@svovaf)
12770
- * @since 1.2.1.6
12771
- */
12772
- echo $this->apply_filters( 'templates/add-ons.php', fs_get_template( 'add-ons.php', $vars ) );
12773
- }
12774
 
12775
- /* Pricing & Upgrade
12776
- ------------------------------------------------------------------------------------------------------------------*/
12777
- /**
12778
- * Render pricing page.
12779
- *
12780
- * @author Vova Feldman (@svovaf)
12781
- * @since 1.0.0
12782
- */
12783
- function _pricing_page_render() {
12784
- $this->_logger->entrance();
12785
-
12786
- $vars = array( 'id' => $this->_module_id );
12787
-
12788
- if ( 'true' === fs_request_get( 'checkout', false ) ) {
12789
- fs_require_once_template( 'checkout.php', $vars );
12790
- } else {
12791
- fs_require_once_template( 'pricing.php', $vars );
12792
- }
12793
- }
12794
-
12795
- #----------------------------------------------------------------------------------
12796
- #region Contact Us
12797
- #----------------------------------------------------------------------------------
12798
-
12799
- /**
12800
- * Render contact-us page.
12801
- *
12802
- * @author Vova Feldman (@svovaf)
12803
- * @since 1.0.3
12804
- */
12805
- function _contact_page_render() {
12806
- $this->_logger->entrance();
12807
-
12808
- $vars = array( 'id' => $this->_module_id );
12809
- fs_require_once_template( 'contact.php', $vars );
12810
- }
12811
-
12812
- #endregion ------------------------------------------------------------------------
12813
-
12814
- /**
12815
- * Hide all admin notices to prevent distractions.
12816
- *
12817
- * @author Vova Feldman (@svovaf)
12818
- * @since 1.0.3
12819
- *
12820
- * @uses remove_all_actions()
12821
- */
12822
- private static function _hide_admin_notices() {
12823
- remove_all_actions( 'admin_notices' );
12824
- remove_all_actions( 'network_admin_notices' );
12825
- remove_all_actions( 'all_admin_notices' );
12826
- remove_all_actions( 'user_admin_notices' );
12827
- }
12828
-
12829
- static function _clean_admin_content_section_hook() {
12830
- self::_hide_admin_notices();
12831
-
12832
- // Hide footer.
12833
- echo '<style>#wpfooter { display: none !important; }</style>';
12834
- }
12835
-
12836
- /**
12837
- * Attach to admin_head hook to hide all admin notices.
12838
- *
12839
- * @author Vova Feldman (@svovaf)
12840
- * @since 1.0.3
12841
- */
12842
- static function _clean_admin_content_section() {
12843
- add_action( 'admin_head', 'Freemius::_clean_admin_content_section_hook' );
12844
- }
12845
-
12846
- /* CSS & JavaScript
12847
- ------------------------------------------------------------------------------------------------------------------*/
12848
- /* function _enqueue_script($handle, $src) {
12849
- $url = plugins_url( substr( WP_FS__DIR_JS, strlen( $this->_plugin_dir_path ) ) . '/assets/js/' . $src );
12850
 
12851
- $this->_logger->entrance( 'script = ' . $url );
 
 
 
 
 
 
 
 
 
 
12852
 
12853
- wp_enqueue_script( $handle, $url );
12854
- }*/
12855
 
12856
- /* SDK
12857
- ------------------------------------------------------------------------------------------------------------------*/
12858
- private $_user_api;
12859
-
12860
- /**
12861
- *
12862
- * @author Vova Feldman (@svovaf)
12863
- * @since 1.0.2
12864
- *
12865
- * @param bool $flush
12866
- *
12867
- * @return FS_Api
12868
- */
12869
- function get_api_user_scope( $flush = false ) {
12870
- if ( ! isset( $this->_user_api ) || $flush ) {
12871
- $this->_user_api = FS_Api::instance(
12872
- $this->_module_id,
12873
- 'user',
12874
- $this->_user->id,
12875
- $this->_user->public_key,
12876
- ! $this->is_live(),
12877
- $this->_user->secret_key
12878
- );
12879
- }
12880
-
12881
- return $this->_user_api;
12882
- }
12883
-
12884
- private $_site_api;
12885
-
12886
- /**
12887
- *
12888
- * @author Vova Feldman (@svovaf)
12889
- * @since 1.0.2
12890
- *
12891
- * @param bool $flush
12892
- *
12893
- * @return FS_Api
12894
- */
12895
- function get_api_site_scope( $flush = false ) {
12896
- if ( ! isset( $this->_site_api ) || $flush ) {
12897
- $this->_site_api = FS_Api::instance(
12898
- $this->_module_id,
12899
- 'install',
12900
- $this->_site->id,
12901
- $this->_site->public_key,
12902
- ! $this->is_live(),
12903
- $this->_site->secret_key
12904
- );
12905
- }
12906
-
12907
- return $this->_site_api;
12908
- }
12909
-
12910
- private $_plugin_api;
12911
-
12912
- /**
12913
- * Get plugin public API scope.
12914
- *
12915
- * @author Vova Feldman (@svovaf)
12916
- * @since 1.0.7
12917
- *
12918
- * @return FS_Api
12919
- */
12920
- function get_api_plugin_scope() {
12921
- if ( ! isset( $this->_plugin_api ) ) {
12922
- $this->_plugin_api = FS_Api::instance(
12923
- $this->_module_id,
12924
- 'plugin',
12925
- $this->_plugin->id,
12926
- $this->_plugin->public_key,
12927
- ! $this->is_live()
12928
- );
12929
- }
12930
-
12931
- return $this->_plugin_api;
12932
- }
12933
-
12934
- /**
12935
- * Get site API scope object (fallback to public plugin scope when not registered).
12936
- *
12937
- * @author Vova Feldman (@svovaf)
12938
- * @since 1.0.7
12939
- *
12940
- * @return FS_Api
12941
- */
12942
- function get_api_site_or_plugin_scope() {
12943
- return $this->is_registered() ?
12944
- $this->get_api_site_scope() :
12945
- $this->get_api_plugin_scope();
12946
- }
12947
-
12948
- /**
12949
- * Show trial promotional notice (if any trial exist).
12950
- *
12951
- * @author Vova Feldman (@svovaf)
12952
- * @since 1.0.9
12953
- *
12954
- * @param $plans
12955
- */
12956
- function _check_for_trial_plans( $plans ) {
12957
- $this->_storage->has_trial_plan = FS_Plan_Manager::instance()->has_trial_plan( $plans );
12958
- }
12959
-
12960
- /**
12961
- * During trial promotion the "upgrade" submenu item turns to
12962
- * "start trial" to encourage the trial. Since we want to keep
12963
- * the same menu item handler and there's no robust way to
12964
- * add new arguments to the menu item link's querystring,
12965
- * use JavaScript to find the menu item and update the href of
12966
- * the link.
12967
- *
12968
- * @author Vova Feldman (@svovaf)
12969
- * @since 1.2.1.5
12970
- */
12971
- function _fix_start_trial_menu_item_url() {
12972
- $template_args = array( 'id' => $this->_module_id );
12973
- fs_require_template( 'add-trial-to-pricing.php', $template_args );
12974
- }
12975
-
12976
- /**
12977
- * Check if module is currently in a trial promotion mode.
12978
- *
12979
- * @author Vova Feldman (@svovaf)
12980
- * @since 1.2.2.7
12981
- *
12982
- * @return bool
12983
- */
12984
- function is_in_trial_promotion() {
12985
- return $this->_admin_notices->has_sticky( 'trial_promotion' );
12986
- }
12987
-
12988
- /**
12989
- * Show trial promotional notice (if any trial exist).
12990
- *
12991
- * @author Vova Feldman (@svovaf)
12992
- * @since 1.0.9
12993
- *
12994
- * @return bool If trial notice added.
12995
- */
12996
- function _add_trial_notice() {
12997
- if ( ! $this->is_user_admin() ) {
12998
- return false;
12999
- }
13000
-
13001
- if ( ! $this->is_user_in_admin() ) {
13002
- return false;
13003
- }
13004
-
13005
- // Check if trial message is already shown.
13006
- if ( $this->is_in_trial_promotion() ) {
13007
- add_action( 'admin_footer', array( &$this, '_fix_start_trial_menu_item_url' ) );
13008
-
13009
- $this->_menu->add_counter_to_menu_item( 1, 'fs-trial' );
13010
-
13011
- return false;
13012
- }
13013
-
13014
- if ( $this->is_premium() && ! WP_FS__DEV_MODE ) {
13015
- // Don't show trial if running the premium code, unless running in DEV mode.
13016
- return false;
13017
- }
13018
-
13019
- if ( ! $this->has_trial_plan() ) {
13020
- // No plans with trial.
13021
- return false;
13022
- }
13023
-
13024
- if ( ! $this->apply_filters( 'show_trial', true ) ) {
13025
- // Developer explicitly asked not to show the trial promo.
13026
- return false;
13027
- }
13028
-
13029
- if ( $this->is_registered() ) {
13030
- // Check if trial already utilized.
13031
- if ( $this->_site->is_trial_utilized() ) {
13032
- return false;
13033
- }
13034
-
13035
- if ( $this->is_paying_or_trial() ) {
13036
- // Don't show trial if paying or already in trial.
13037
- return false;
13038
- }
13039
- }
13040
-
13041
- if ( $this->is_activation_mode() || $this->is_pending_activation() ) {
13042
- // If not yet opted-in/skipped, or pending activation, don't show trial.
13043
- return false;
13044
- }
13045
-
13046
- $last_time_trial_promotion_shown = $this->_storage->get( 'trial_promotion_shown', false );
13047
- $was_promotion_shown_before = ( false !== $last_time_trial_promotion_shown );
13048
-
13049
- // Show promotion if never shown before and 24 hours after initial activation with FS.
13050
- if ( ! $was_promotion_shown_before &&
13051
- $this->_storage->install_timestamp > ( time() - WP_FS__TIME_24_HOURS_IN_SEC )
13052
- ) {
13053
- return false;
13054
- }
13055
-
13056
- // OR if promotion was shown before, try showing it every 30 days.
13057
- if ( $was_promotion_shown_before &&
13058
- 30 * WP_FS__TIME_24_HOURS_IN_SEC > time() - $last_time_trial_promotion_shown
13059
- ) {
13060
- return false;
13061
- }
13062
-
13063
- $trial_period = $this->_trial_days;
13064
- $require_payment = $this->_is_trial_require_payment;
13065
- $trial_url = $this->get_trial_url();
13066
- $plans_string = strtolower( $this->get_text_inline( 'Awesome', 'awesome' ) );
13067
-
13068
- if ( $this->is_registered() ) {
13069
- // If opted-in, override trial with up to date data from API.
13070
- $trial_plans = FS_Plan_Manager::instance()->get_trial_plans( $this->_plans );
13071
- $trial_plans_count = count( $trial_plans );
13072
-
13073
- if ( 0 === $trial_plans_count ) {
13074
- // If there's no plans with a trial just exit.
13075
- return false;
13076
- }
13077
-
13078
- /**
13079
- * @var FS_Plugin_Plan $paid_plan
13080
- */
13081
- $paid_plan = $trial_plans[0];
13082
- $require_payment = $paid_plan->is_require_subscription;
13083
- $trial_period = $paid_plan->trial_period;
13084
-
13085
- $total_paid_plans = count( $this->_plans ) - ( FS_Plan_Manager::instance()->has_free_plan( $this->_plans ) ? 1 : 0 );
13086
-
13087
- if ( $total_paid_plans !== $trial_plans_count ) {
13088
- // Not all paid plans have a trial - generate a string of those that have it.
13089
- for ( $i = 0; $i < $trial_plans_count; $i ++ ) {
13090
- $plans_string .= sprintf(
13091
- ' <a href="%s">%s</a>',
13092
- $trial_url,
13093
- $trial_plans[ $i ]->title
13094
- );
13095
-
13096
- if ( $i < $trial_plans_count - 2 ) {
13097
- $plans_string .= ', ';
13098
- } else if ( $i == $trial_plans_count - 2 ) {
13099
- $plans_string .= ' and ';
13100
- }
13101
- }
13102
- }
13103
- }
13104
-
13105
- $message = sprintf(
13106
- $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' ),
13107
- sprintf( '<b>%s</b>', $this->get_plugin_name() ),
13108
- $plans_string,
13109
- $trial_period
13110
- );
13111
-
13112
- // "No Credit-Card Required" or "No Commitment for N Days".
13113
- $cc_string = $require_payment ?
13114
- sprintf( $this->get_text_inline( 'No commitment for %s days - cancel anytime!', 'no-commitment-for-x-days' ), $trial_period ) :
13115
- $this->get_text_inline( 'No credit card required', 'no-cc-required' ) . '!';
13116
-
13117
-
13118
- // Start trial button.
13119
- $button = ' ' . sprintf(
13120
- '<a style="margin-left: 10px; vertical-align: super;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
13121
- $trial_url,
13122
- $this->get_text_x_inline( 'Start free trial', 'call to action', 'start-free-trial' )
13123
- );
13124
-
13125
- $this->_admin_notices->add_sticky(
13126
- $this->apply_filters( 'trial_promotion_message', "{$message} {$cc_string} {$button}" ),
13127
- 'trial_promotion',
13128
- '',
13129
- 'promotion'
13130
- );
13131
-
13132
- $this->_storage->trial_promotion_shown = WP_FS__SCRIPT_START_TIME;
13133
-
13134
- return true;
13135
- }
13136
-
13137
- /**
13138
- * Lets users/customers know that the product has an affiliate program.
13139
- *
13140
- * @author Leo Fajardo (@leorw)
13141
- * @since 1.2.2.11
13142
- *
13143
- * @return bool Returns true if the notice has been added.
13144
- */
13145
- function _add_affiliate_program_notice() {
13146
- if ( ! $this->is_user_admin() ) {
13147
- return false;
13148
- }
13149
-
13150
- if ( ! $this->is_user_in_admin() ) {
13151
- return false;
13152
- }
13153
-
13154
- // Check if the notice is already shown.
13155
- if ( $this->_admin_notices->has_sticky( 'affiliate_program' ) ) {
13156
- return false;
13157
- }
13158
 
13159
- if (
13160
- // Product has no affiliate program.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  <?php
2
+ /**
3
+ * @package Freemius
4
+ * @copyright Copyright (c) 2015, Freemius, Inc.
5
+ * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
6
+ * @since 1.0.3
7
+ */
8
+ if ( ! defined( 'ABSPATH' ) ) {
9
+ exit;
10
+ }
11
+
12
+ // "final class"
13
+ class Freemius extends Freemius_Abstract {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  /**
15
+ * SDK Version
16
  *
17
+ * @var string
18
+ */
19
+ public $version = WP_FS__SDK_VERSION;
20
+
21
+ #region Plugin Info
22
+
23
+ /**
24
+ * @since 1.0.1
25
  *
26
+ * @var string
27
  */
28
+ private $_slug;
29
 
30
  /**
31
+ * @since 1.0.0
32
  *
33
+ * @var string
34
+ */
35
+ private $_plugin_basename;
36
+ /**
37
+ * @since 1.0.0
38
  *
39
+ * @var string
40
  */
41
+ private $_free_plugin_basename;
 
42
  /**
43
+ * @since 1.0.0
44
+ *
45
+ * @var string
46
+ */
47
+ private $_plugin_dir_path;
48
+ /**
49
+ * @since 1.0.0
50
  *
51
+ * @var string
52
+ */
53
+ private $_plugin_dir_name;
54
+ /**
55
+ * @since 1.0.0
56
  *
57
+ * @var string
58
  */
59
+ private $_plugin_main_file_path;
60
+ /**
61
+ * @var string[]
62
+ */
63
+ private $_plugin_data;
64
+ /**
65
+ * @since 1.0.9
66
+ *
67
+ * @var string
68
+ */
69
+ private $_plugin_name;
70
+ /**
71
+ * @since 1.2.2
72
+ *
73
+ * @var string
74
+ */
75
+ private $_module_type;
 
 
 
 
 
 
 
76
 
77
+ #endregion Plugin Info
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
+ /**
80
+ * @since 1.0.9
81
+ *
82
+ * @var bool If false, don't turn Freemius on.
83
+ */
84
+ private $_is_on;
 
 
 
 
 
85
 
86
+ /**
87
+ * @since 1.1.3
88
+ *
89
+ * @var bool If false, don't turn Freemius on.
90
+ */
91
+ private $_is_anonymous;
 
 
 
 
 
92
 
93
+ /**
94
+ * @since 1.0.9
95
+ * @var bool If false, issues with connectivity to Freemius API.
96
+ */
97
+ private $_has_api_connection;
98
 
99
+ /**
100
+ * @since 1.0.9
101
+ * @since 2.0.0 Default to true since we need the property during the instance construction, prior to the dynamic_init() execution.
102
+ * @var bool Hints the SDK if plugin can support anonymous mode (if skip connect is visible).
103
+ */
104
+ private $_enable_anonymous = true;
105
 
106
+ /**
107
+ * @since 1.1.7.5
108
+ * @var bool Hints the SDK if plugin should run in anonymous mode (only adds feedback form).
109
+ */
110
+ private $_anonymous_mode;
111
 
112
  /**
113
+ * @since 1.1.9
114
+ * @var bool Hints the SDK if plugin have any free plans.
 
 
115
  */
116
+ private $_is_premium_only;
 
117
 
118
+ /**
119
+ * @since 1.2.1.6
120
+ * @var bool Hints the SDK if plugin have premium code version at all.
121
+ */
122
+ private $_has_premium_version;
123
 
124
+ /**
125
+ * @since 1.2.1.6
126
+ * @var bool Hints the SDK if plugin should ignore pending mode by simulating a skip.
127
+ */
128
+ private $_ignore_pending_mode;
129
 
130
+ /**
131
+ * @since 1.0.8
132
+ * @var bool Hints the SDK if the plugin has any paid plans.
133
+ */
134
+ private $_has_paid_plans;
135
 
136
+ /**
137
+ * @since 1.2.1.5
138
+ * @var int Hints the SDK if the plugin offers a trial period. If negative, no trial, if zero - has a trial but
139
+ * without a specified period, if positive - the number of trial days.
140
+ */
141
+ private $_trial_days = - 1;
142
 
143
+ /**
144
+ * @since 1.2.1.5
145
+ * @var bool Hints the SDK if the trial requires a payment method or not.
146
+ */
147
+ private $_is_trial_require_payment = false;
 
 
 
 
148
 
149
+ /**
150
+ * @since 1.0.7
151
+ * @var bool Hints the SDK if the plugin is WordPress.org compliant.
152
+ */
153
+ private $_is_org_compliant;
154
 
155
+ /**
156
+ * @since 1.0.7
157
+ * @var bool Hints the SDK if the plugin is has add-ons.
158
+ */
159
+ private $_has_addons;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
 
161
+ /**
162
+ * @since 1.1.6
163
+ * @var string[]bool.
164
+ */
165
+ private $_permissions;
166
 
167
+ /**
168
+ * @var FS_Storage
169
+ */
170
+ private $_storage;
171
 
172
+ /**
173
+ * @since 1.2.2.7
174
+ * @var FS_Cache_Manager
175
+ */
176
+ private $_cache;
177
 
178
+ /**
179
+ * @since 1.0.0
180
+ *
181
+ * @var FS_Logger
182
+ */
183
+ private $_logger;
184
+ /**
185
+ * @since 1.0.4
186
+ *
187
+ * @var FS_Plugin
188
+ */
189
+ private $_plugin = false;
190
+ /**
191
+ * @since 1.0.4
192
+ *
193
+ * @var FS_Plugin|false
194
+ */
195
+ private $_parent_plugin = false;
196
+ /**
197
+ * @since 1.1.1
198
+ *
199
+ * @var Freemius
200
+ */
201
+ private $_parent = false;
202
+ /**
203
+ * @since 1.0.1
204
+ *
205
+ * @var FS_User
206
+ */
207
+ private $_user = false;
208
+ /**
209
+ * @since 1.0.1
210
+ *
211
+ * @var FS_Site
212
+ */
213
+ private $_site = false;
214
+ /**
215
+ * @since 1.0.1
216
+ *
217
+ * @var FS_Plugin_License
218
+ */
219
+ private $_license;
220
+ /**
221
+ * @since 1.0.2
222
+ *
223
+ * @var FS_Plugin_Plan[]
224
+ */
225
+ private $_plans = false;
226
+ /**
227
+ * @var FS_Plugin_License[]
228
+ * @since 1.0.5
229
+ */
230
+ private $_licenses = false;
231
 
232
+ /**
233
+ * @since 1.0.1
234
+ *
235
+ * @var FS_Admin_Menu_Manager
236
+ */
237
+ private $_menu;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
 
239
+ /**
240
+ * @var FS_Admin_Notices
241
+ */
242
+ private $_admin_notices;
243
 
244
  /**
245
+ * @since 1.1.6
 
246
  *
247
+ * @var FS_Admin_Notices
248
+ */
249
+ private static $_global_admin_notices;
250
+
251
+ /**
252
+ * @var FS_Logger
253
+ * @since 1.0.0
254
+ */
255
+ private static $_static_logger;
256
+
257
+ /**
258
+ * @var FS_Options
259
+ * @since 1.0.2
260
+ */
261
+ private static $_accounts;
262
+
263
+ /**
264
+ * @since 1.2.2
265
  *
266
+ * @var number
267
  */
268
+ private $_module_id;
 
 
 
 
269
 
270
+ /**
271
+ * @var Freemius[]
272
+ */
273
+ private static $_instances = array();
274
 
275
  /**
276
+ * @since 1.2.3
 
277
  *
278
+ * @var FS_Affiliate
279
+ */
280
+ private $affiliate = null;
281
+
282
+ /**
283
+ * @since 1.2.3
284
  *
285
+ * @var FS_AffiliateTerms
286
  */
287
+ private $plugin_affiliate_terms = null;
 
 
 
 
288
 
289
+ /**
290
+ * @since 1.2.3
291
+ *
292
+ * @var FS_AffiliateTerms
293
+ */
294
+ private $custom_affiliate_terms = null;
295
 
296
  /**
297
+ * @since 2.0.0
 
298
  *
299
+ * @var bool
300
+ */
301
+ private $_is_multisite_integrated;
302
+
303
+ /**
304
+ * @since 2.0.0
305
  *
306
+ * @var bool True if the current request is for a network admin screen and the plugin is network active.
307
  */
308
+ private $_is_network_active;
 
 
 
309
 
310
+ /**
311
+ * @since 2.0.0
312
+ *
313
+ * @var int|null The original blog ID the plugin was loaded with.
314
+ */
315
+ private $_blog_id = null;
316
 
317
+ /**
318
+ * @since 2.0.0
319
+ *
320
+ * @var int|null The current execution context. When true, run on network context. When int, run on the specified blog context.
321
+ */
322
+ private $_context_is_network_or_blog_id = null;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
323
 
324
+ /**
325
+ * @since 2.0.0
326
+ *
327
+ * @var string
328
+ */
329
+ private $_dynamically_added_top_level_page_hook_name = '';
330
+
331
+ #region Uninstall Reasons IDs
332
+
333
+ const REASON_NO_LONGER_NEEDED = 1;
334
+ const REASON_FOUND_A_BETTER_PLUGIN = 2;
335
+ const REASON_NEEDED_FOR_A_SHORT_PERIOD = 3;
336
+ const REASON_BROKE_MY_SITE = 4;
337
+ const REASON_SUDDENLY_STOPPED_WORKING = 5;
338
+ const REASON_CANT_PAY_ANYMORE = 6;
339
+ const REASON_OTHER = 7;
340
+ const REASON_DIDNT_WORK = 8;
341
+ const REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION = 9;
342
+ const REASON_COULDNT_MAKE_IT_WORK = 10;
343
+ const REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE = 11;
344
+ const REASON_NOT_WORKING = 12;
345
+ const REASON_NOT_WHAT_I_WAS_LOOKING_FOR = 13;
346
+ const REASON_DIDNT_WORK_AS_EXPECTED = 14;
347
+ const REASON_TEMPORARY_DEACTIVATION = 15;
348
+
349
+ #endregion
350
+
351
+ /* Ctor
352
+ ------------------------------------------------------------------------------------------------------------------*/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
353
 
354
+ /**
355
+ * Main singleton instance.
356
+ *
357
+ * @author Vova Feldman (@svovaf)
358
+ * @since 1.0.0
359
+ *
360
+ * @param number $module_id
361
+ * @param string|bool $slug
362
+ * @param bool $is_init Since 1.2.1 Is initiation sequence.
363
+ */
364
+ private function __construct( $module_id, $slug = false, $is_init = false ) {
365
+ if ( $is_init && is_numeric( $module_id ) && is_string( $slug ) ) {
366
+ $this->store_id_slug_type_path_map( $module_id, $slug );
367
+ }
368
 
369
+ $this->_module_id = $module_id;
370
+ $this->_slug = $this->get_slug();
371
+ $this->_module_type = $this->get_module_type();
372
 
373
+ $this->_blog_id = is_multisite() ? get_current_blog_id() : null;
 
374
 
375
+ $this->_storage = FS_Storage::instance( $this->_module_type, $this->_slug );
 
 
376
 
377
+ $this->_cache = FS_Cache_Manager::get_manager( WP_FS___OPTION_PREFIX . "cache_{$module_id}" );
378
 
379
+ $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $this->get_unique_affix(), WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
 
 
 
 
 
 
 
 
380
 
381
+ $this->_plugin_main_file_path = $this->_find_caller_plugin_file( $is_init );
382
+ $this->_plugin_dir_path = plugin_dir_path( $this->_plugin_main_file_path );
383
+ $this->_plugin_basename = $this->get_plugin_basename();
384
+ $this->_free_plugin_basename = str_replace( '-premium/', '/', $this->_plugin_basename );
385
 
386
+ $this->_is_multisite_integrated = (
387
+ defined( "WP_FS__PRODUCT_{$module_id}_MULTISITE" ) &&
388
+ ( true === constant( "WP_FS__PRODUCT_{$module_id}_MULTISITE" ) )
389
+ );
 
390
 
391
+ $this->_is_network_active = (
392
+ is_multisite() &&
393
+ $this->_is_multisite_integrated &&
394
+ // Themes are always network activated, but the ACTUAL activation is per site.
395
+ $this->is_plugin() &&
396
+ ( is_plugin_active_for_network( $this->_plugin_basename ) ||
397
+ // Plugin network level activation or uninstall.
398
+ is_plugin_inactive( $this->_plugin_basename ) )
399
+ );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
400
 
401
+ $this->_storage->set_network_active(
402
+ $this->_is_network_active,
403
+ $this->is_delegated_connection()
404
+ );
405
 
406
+ #region Migration
 
 
 
 
 
 
407
 
408
+ if ( is_multisite() ) {
409
  /**
410
+ * If the install_timestamp exists on the site level but doesn't exist on the
411
+ * network level storage, it means that we need to process the storage with migration.
412
+ *
413
+ * 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.
414
  *
415
  * @author Vova Feldman (@svovaf)
416
+ * @since 2.0.0
417
  */
418
+ if ( false === $this->_storage->get( 'install_timestamp', false, true ) &&
419
+ false !== $this->_storage->get( 'install_timestamp', false, false )
420
+ ) {
421
+ // Initiate storage migration.
422
+ $this->_storage->migrate_to_network();
423
 
424
+ // Migrate module cache to network level storage.
425
+ $this->_cache->migrate_to_network();
426
+ }
427
  }
428
 
429
+ #endregion
430
 
431
+ $base_name_split = explode( '/', $this->_plugin_basename );
432
+ $this->_plugin_dir_name = $base_name_split[0];
433
 
434
+ if ( $this->_logger->is_on() ) {
435
+ $this->_logger->info( 'plugin_main_file_path = ' . $this->_plugin_main_file_path );
436
+ $this->_logger->info( 'plugin_dir_path = ' . $this->_plugin_dir_path );
437
+ $this->_logger->info( 'plugin_basename = ' . $this->_plugin_basename );
438
+ $this->_logger->info( 'free_plugin_basename = ' . $this->_free_plugin_basename );
439
+ $this->_logger->info( 'plugin_dir_name = ' . $this->_plugin_dir_name );
440
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
441
 
442
+ // Remember link between file to slug.
443
+ $this->store_file_slug_map();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
444
 
445
+ // Store plugin's initial install timestamp.
446
+ if ( ! isset( $this->_storage->install_timestamp ) ) {
447
+ $this->_storage->install_timestamp = WP_FS__SCRIPT_START_TIME;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
448
  }
449
 
450
+ if ( ! is_object( $this->_plugin ) ) {
451
+ $this->_plugin = FS_Plugin_Manager::instance( $this->_module_id )->get();
 
 
452
  }
453
 
454
+ $this->_admin_notices = FS_Admin_Notices::instance(
455
+ $this->_slug . ( $this->is_theme() ? ':theme' : '' ),
456
+ /**
457
+ * Ensure that the admin notice will always have a title by using the stored plugin title if available and
458
+ * retrieving the title via the "get_plugin_name" method if there is no stored plugin title available.
459
+ *
460
+ * @author Leo Fajardo (@leorw)
461
+ * @since 1.2.2
462
+ */
463
+ ( is_object( $this->_plugin ) ? $this->_plugin->title : $this->get_plugin_name() ),
464
+ $this->get_unique_affix()
465
+ );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
466
 
467
+ if ( 'true' === fs_request_get( 'fs_clear_api_cache' ) ||
468
+ 'true' === fs_request_is_action( 'restart_freemius' )
469
+ ) {
470
+ FS_Api::clear_cache();
471
+ $this->_cache->clear();
472
+ }
 
 
473
 
474
+ $this->_register_hooks();
 
 
475
 
476
+ /**
477
+ * Starting from version 2.0.0, `FS_Site` entities no longer have the `plan` property and have `plan_id`
478
+ * instead. This should be called before calling `_load_account()`, otherwise, `$this->_site` will not be
479
+ * loaded in `_load_account` for versions of SDK starting from 2.0.0.
480
+ *
481
+ * @author Leo Fajardo (@leorw)
482
+ */
483
+ self::migrate_install_plan_to_plan_id( $this->_storage );
484
 
485
+ $this->_load_account();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486
 
487
+ $this->_version_updates_handler();
488
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
489
 
490
+ /**
491
+ * Checks whether this module has a settings menu.
492
+ *
493
+ * @author Leo Fajardo (@leorw)
494
+ * @since 1.2.2
495
+ *
496
+ * @return bool
497
+ */
498
+ function has_settings_menu() {
499
+ return ( $this->_is_network_active && fs_is_network_admin() ) ?
500
+ $this->_menu->has_network_menu() :
501
+ $this->_menu->has_menu();
502
+ }
503
 
504
+ /**
505
+ * Check if the context module is free wp.org theme.
506
+ *
507
+ * This method is helpful because:
508
+ * 1. wp.org themes are limited to a single submenu item,
509
+ * and sub-submenu items are most likely not allowed (never verified).
510
+ * 2. wp.org themes are not allowed to redirect the user
511
+ * after the theme activation, therefore, the agreed UX
512
+ * is showing the opt-in as a modal dialog box after
513
+ * activation (approved by @otto42, @emiluzelac, @greenshady, @grapplerulrich).
514
+ *
515
+ * @author Vova Feldman (@svovaf)
516
+ * @since 1.2.2.7
517
+ *
518
+ * @return bool
519
+ */
520
+ function is_free_wp_org_theme() {
521
+ return (
522
+ $this->is_theme() &&
523
+ $this->is_org_repo_compliant() &&
524
+ ! $this->is_premium()
525
+ );
526
+ }
527
 
528
+ /**
529
+ * Checks whether this a submenu item is visible.
530
+ *
531
+ * @author Vova Feldman (@svovaf)
532
+ * @since 1.2.2.6
533
+ * @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.
534
+ *
535
+ * @param string $slug
536
+ *
537
+ * @return bool
538
+ */
539
+ function is_submenu_item_visible( $slug ) {
540
+ if ( $this->is_admin_page( $slug ) ) {
541
+ /**
542
+ * It is the current context page, so show the submenu item
543
+ * so the user will have the right context page, even if it
544
+ * was set to hidden.
545
+ */
546
+ return true;
547
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
548
 
549
+ if ( ! $this->has_settings_menu() ) {
550
+ // No menu settings at all.
551
+ return false;
552
+ }
553
 
554
+ if ( $this->is_free_wp_org_theme() ) {
555
+ /**
556
+ * wp.org themes are limited to a single submenu item, and
557
+ * sub-submenu items are most likely not allowed (never verified).
558
+ */
559
+ return false;
560
+ }
561
 
562
+ return $this->_menu->is_submenu_item_visible( $slug );
563
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
564
 
565
+ /**
566
+ * Check if a Freemius page should be accessible via the UI.
567
+ *
568
+ * @author Vova Feldman (@svovaf)
569
+ * @since 1.2.2.7
570
+ *
571
+ * @param string $slug
572
+ *
573
+ * @return bool
574
+ */
575
+ function is_page_visible( $slug ) {
576
+ if ( $this->is_admin_page( $slug ) ) {
577
+ return true;
578
+ }
579
 
580
+ return $this->_menu->is_submenu_item_visible( $slug, true, true );
581
+ }
 
582
 
583
+ /**
584
+ * @author Vova Feldman (@svovaf)
585
+ * @since 1.0.9
586
+ */
587
+ private function _version_updates_handler() {
588
+ if ( ! isset( $this->_storage->sdk_version ) || $this->_storage->sdk_version != $this->version ) {
589
+ // Freemius version upgrade mode.
590
+ $this->_storage->sdk_last_version = $this->_storage->sdk_version;
591
+ $this->_storage->sdk_version = $this->version;
592
+
593
+ if ( empty( $this->_storage->sdk_last_version ) ||
594
+ version_compare( $this->_storage->sdk_last_version, $this->version, '<' )
595
+ ) {
596
+ $this->_storage->sdk_upgrade_mode = true;
597
+ $this->_storage->sdk_downgrade_mode = false;
598
+ } else {
599
+ $this->_storage->sdk_downgrade_mode = true;
600
+ $this->_storage->sdk_upgrade_mode = false;
601
 
602
+ }
 
603
 
604
+ $this->do_action( 'sdk_version_update', $this->_storage->sdk_last_version, $this->version );
605
+ }
606
 
607
+ $plugin_version = $this->get_plugin_version();
608
+ if ( ! isset( $this->_storage->plugin_version ) || $this->_storage->plugin_version != $plugin_version ) {
609
+ // Plugin version upgrade mode.
610
+ $this->_storage->plugin_last_version = $this->_storage->plugin_version;
611
+ $this->_storage->plugin_version = $plugin_version;
612
 
613
+ if ( empty( $this->_storage->plugin_last_version ) ||
614
+ version_compare( $this->_storage->plugin_last_version, $plugin_version, '<' )
615
+ ) {
616
+ $this->_storage->plugin_upgrade_mode = true;
617
+ $this->_storage->plugin_downgrade_mode = false;
618
+ } else {
619
+ $this->_storage->plugin_downgrade_mode = true;
620
+ $this->_storage->plugin_upgrade_mode = false;
621
+ }
622
 
623
+ if ( ! empty( $this->_storage->plugin_last_version ) ) {
624
+ // Different version of the plugin was installed before, therefore it's an update.
625
+ $this->_storage->is_plugin_new_install = false;
626
+ }
627
 
628
+ $this->do_action( 'plugin_version_update', $this->_storage->plugin_last_version, $plugin_version );
629
+ }
630
+ }
631
 
632
+ #--------------------------------------------------------------------------------
633
+ #region Data Migration on SDK Update
634
+ #--------------------------------------------------------------------------------
635
 
636
+ /**
637
+ * @author Vova Feldman (@svovaf)
638
+ * @since 1.1.5
639
+ *
640
+ * @param string $sdk_prev_version
641
+ * @param string $sdk_version
642
+ */
643
+ function _data_migration( $sdk_prev_version, $sdk_version ) {
644
+ /**
645
+ * @since 1.1.7.3 Fixed unwanted connectivity test cleanup.
646
+ */
647
+ if ( empty( $sdk_prev_version ) ) {
648
+ return;
649
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
650
 
651
+ if ( version_compare( $sdk_prev_version, '2.0.0', '<' ) &&
652
+ version_compare( $sdk_version, '2.0.0', '>=' )
653
+ ) {
654
+ $this->migrate_to_subscriptions_collection();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
655
 
656
+ $this->consolidate_licenses();
 
 
657
 
658
+ // Clear trial_plan since it's now loaded from the plans collection when needed.
659
+ $this->_storage->remove( 'trial_plan', true, false );
660
+ }
661
+
662
+ if ( version_compare( $sdk_prev_version, '1.2.3', '<' ) &&
663
+ version_compare( $sdk_version, '1.2.3', '>=' )
664
+ ) {
665
+ /**
666
+ * Starting from version 1.2.3, paths are stored as relative paths and not absolute paths; so when upgrading to 1.2.3, make paths relative.
667
+ *
668
+ * @author Leo Fajardo (@leorw)
669
+ */
670
+ $this->make_paths_relative();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
671
  }
672
 
673
+ if ( version_compare( $sdk_prev_version, '1.1.5', '<' ) &&
674
+ version_compare( $sdk_version, '1.1.5', '>=' )
675
+ ) {
676
+ // On version 1.1.5 merged connectivity and is_on data.
677
+ if ( isset( $this->_storage->connectivity_test ) ) {
678
+ if ( ! isset( $this->_storage->is_on ) ) {
679
+ unset( $this->_storage->connectivity_test );
680
+ } else {
681
+ $connectivity_data = $this->_storage->connectivity_test;
682
+ $connectivity_data['is_active'] = $this->_storage->is_on['is_active'];
683
+ $connectivity_data['timestamp'] = $this->_storage->is_on['timestamp'];
684
+
685
+ // Override.
686
+ $this->_storage->connectivity_test = $connectivity_data;
687
+
688
+ // Remove previous structure.
689
+ unset( $this->_storage->is_on );
690
+ }
691
+
692
+ }
693
+ }
694
  }
695
 
696
  /**
697
  * @author Leo Fajardo (@leorw)
698
+ * @since 2.0.0
699
+ *
700
+ * @param \FS_Storage $storage
701
+ * @param bool|int|null $blog_id
702
  */
703
+ private static function migrate_install_plan_to_plan_id( FS_Storage $storage, $blog_id = null ) {
704
+ if ( empty( $storage->sdk_version ) ) {
705
+ // New installation of the plugin, no need to upgrade.
706
+ return;
707
+ }
708
 
709
+ if ( ! version_compare( $storage->sdk_version, '2.0.0', '<' ) ) {
710
+ // Previous version is >= 2.0.0, so no need to migrate.
711
+ return;
712
+ }
713
 
714
+ // Alias.
715
+ $module_type = $storage->get_module_type();
716
+ $module_slug = $storage->get_module_slug();
717
 
718
+ $installs = self::get_all_sites( $module_type, $blog_id );
719
+ $install = isset( $installs[ $module_slug ] ) ? $installs[ $module_slug ] : null;
720
+
721
+ if ( ! is_object( $install ) ) {
722
+ return;
723
  }
724
 
725
+ if ( isset( $install->plan ) && is_object( $install->plan ) ) {
726
+ if ( isset( $install->plan->id ) && ! empty( $install->plan->id ) ) {
727
+ $install->plan_id = self::_decrypt( $install->plan->id );
728
+ }
 
 
729
 
730
+ unset( $install->plan );
 
 
731
 
732
+ $installs[ $module_slug ] = clone $install;
 
 
 
 
 
733
 
734
+ self::set_account_option_by_module(
735
+ $module_type,
736
+ 'sites',
737
+ $installs,
738
+ true,
739
+ $blog_id
740
+ );
741
  }
742
  }
743
 
744
  /**
745
+ * @author Leo Fajardo (@leorw)
746
+ * @since 2.0.0
 
 
747
  */
748
+ private function migrate_to_subscriptions_collection() {
749
+ if ( ! is_object( $this->_site ) ) {
750
+ return;
751
+ }
752
 
753
+ if ( isset( $this->_storage->subscription ) && is_object( $this->_storage->subscription ) ) {
754
+ $this->_storage->subscriptions = array( $this->_storage->subscription );
755
+ }
 
 
 
 
 
 
 
756
  }
757
 
758
  /**
759
+ * @author Leo Fajardo (@leorw)
760
+ * @since 2.0.0
 
 
761
  */
762
+ private function consolidate_licenses() {
763
+ $plugin_licenses = self::get_account_option( 'licenses', WP_FS__MODULE_TYPE_PLUGIN );
764
+ if ( isset( $plugin_licenses[ $this->_slug ] ) ) {
765
+ $plugin_licenses = $plugin_licenses[ $this->_slug ];
766
+ } else {
767
+ $plugin_licenses = array();
768
+ }
769
 
770
+ $theme_licenses = self::get_account_option( 'licenses', WP_FS__MODULE_TYPE_THEME );
771
+ if ( isset( $theme_licenses[ $this->_slug ] ) ) {
772
+ $theme_licenses = $theme_licenses[ $this->_slug ];
773
+ } else {
774
+ $theme_licenses = array();
775
+ }
776
 
777
+ if ( empty( $plugin_licenses ) && empty( $theme_licenses ) ) {
778
+ return;
 
779
  }
780
 
781
+ $all_licenses = array();
782
+ $user_id_license_ids_map = array();
783
 
784
+ foreach ( $plugin_licenses as $user_id => $user_licenses ) {
785
+ if ( is_array( $user_licenses ) ) {
786
+ if ( ! isset( $user_license_ids[ $user_id ] ) ) {
787
+ $user_id_license_ids_map[ $user_id ] = array();
788
+ }
789
 
790
+ foreach ( $user_licenses as $user_license ) {
791
+ $all_licenses[] = $user_license;
792
+ $user_id_license_ids_map[ $user_id ][] = $user_license->id;
793
+ }
794
+ }
795
  }
796
 
797
+ foreach ( $theme_licenses as $user_id => $user_licenses ) {
798
+ if ( is_array( $user_licenses ) ) {
799
+ if ( ! isset( $user_license_ids[ $user_id ] ) ) {
800
+ $user_id_license_ids_map[ $user_id ] = array();
801
+ }
 
 
 
 
 
 
802
 
803
+ foreach ( $user_licenses as $user_license ) {
804
+ $all_licenses[] = $user_license;
805
+ $user_id_license_ids_map[ $user_id ][] = $user_license->id;
806
+ }
 
 
 
 
807
  }
808
  }
809
 
810
+ self::store_user_id_license_ids_map(
811
+ $user_id_license_ids_map,
812
+ $this->_module_id
 
 
813
  );
814
 
815
+ $this->_store_licenses( true, $this->_module_id, $all_licenses );
816
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
817
 
818
+ /**
819
+ * Makes paths relative.
820
+ *
821
+ * @author Leo Fajardo (@leorw)
822
+ * @since 1.2.3
823
+ */
824
+ private function make_paths_relative() {
825
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
826
 
827
+ if ( isset( $id_slug_type_path_map[ $this->_module_id ]['path'] ) ) {
828
+ $id_slug_type_path_map[ $this->_module_id ]['path'] = $this->get_relative_path( $id_slug_type_path_map[ $this->_module_id ]['path'] );
 
829
 
830
+ self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
831
+ }
 
832
 
833
+ if ( isset( $this->_storage->plugin_main_file ) ) {
834
+ $plugin_main_file = $this->_storage->plugin_main_file;
835
+
836
+ if ( isset( $plugin_main_file->path ) ) {
837
+ $this->_storage->plugin_main_file->path = $this->get_relative_path( $this->_storage->plugin_main_file->path );
838
+ } else if ( isset( $plugin_main_file->prev_path ) ) {
839
+ $this->_storage->plugin_main_file->prev_path = $this->get_relative_path( $this->_storage->plugin_main_file->prev_path );
840
+ }
841
  }
842
 
843
+ // Remove invalid path that is still associated with the current slug if there's any.
844
+ $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
845
+ foreach ( $file_slug_map as $plugin_basename => $slug ) {
846
+ if ( $slug === $this->_slug &&
847
+ $plugin_basename !== $this->_plugin_basename &&
848
+ ! file_exists( $this->get_absolute_path( $plugin_basename ) )
849
+ ) {
850
+ unset( $file_slug_map[ $plugin_basename ] );
851
+ self::$_accounts->set_option( 'file_slug_map', $file_slug_map, true );
852
 
853
+ break;
854
+ }
855
+ }
856
  }
857
 
858
  /**
859
+ * @author Vova Feldman (@svovaf)
860
+ * @since 1.2.2.7
861
  *
862
+ * @param string $plugin_prev_version
863
+ * @param string $plugin_version
864
  */
865
+ function _after_version_update( $plugin_prev_version, $plugin_version ) {
866
+ if ( $this->is_theme() ) {
867
+ // Expire the cache of the previous tabs since the theme may
868
+ // have setting updates.
869
+ $this->_cache->expire( 'tabs' );
870
+ $this->_cache->expire( 'tabs_stylesheets' );
871
  }
 
 
872
  }
873
 
874
+ /**
875
+ * A special migration logic for the $_accounts, executed for all the plugins in the system:
876
+ * - Moves some data to the network level storage.
877
+ * - If the plugin's connection was skipped for all sites, set the plugin as if it was network skipped.
878
+ * - If the plugin's connection was ignored for all sites, don't do anything in terms of the network connection.
879
+ * - 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.
880
+ * - 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.
881
+ * - 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.
882
+ *
883
+ * @author Vova Feldman (@svovaf)
884
+ * @since 2.0.0
885
+ */
886
+ private static function migrate_accounts_to_network() {
887
+ $sites = self::get_sites();
888
+ $sites_count = count( $sites );
889
+ $connection_status = array();
890
+ $plugin_slugs = array();
891
+ foreach ( $sites as $site ) {
892
+ $blog_id = self::get_site_blog_id( $site );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
893
 
894
+ self::$_accounts->migrate_to_network( $blog_id );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
895
 
896
  /**
897
+ * Build a list of all Freemius powered plugins slugs.
 
 
898
  */
899
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array(), $blog_id );
900
+ foreach ( $id_slug_type_path_map as $module_id => $data ) {
901
+ if ( WP_FS__MODULE_TYPE_PLUGIN === $data['type'] ) {
902
+ $plugin_slugs[ $data['slug'] ] = true;
903
+ }
904
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
905
 
906
+ $installs = self::get_account_option( 'sites', WP_FS__MODULE_TYPE_PLUGIN, $blog_id );
 
 
 
 
 
 
 
 
 
 
 
 
907
 
908
+ if ( is_array( $installs ) ) {
909
+ foreach ( $installs as $slug => $install ) {
910
+ if ( ! isset( $connection_status[ $slug ] ) ) {
911
+ $connection_status[ $slug ] = array();
912
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
913
 
914
+ if ( is_object( $install ) &&
915
+ FS_Site::is_valid_id( $install->id ) &&
916
+ FS_User::is_valid_id( $install->user_id )
917
+ ) {
918
+ $connection_status[ $slug ][ $blog_id ] = $install->user_id;
919
+ }
920
+ }
921
+ }
922
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
923
 
924
+ foreach ( $plugin_slugs as $slug => $true ) {
925
+ if ( ! isset( $connection_status[ $slug ] ) ) {
926
+ $connection_status[ $slug ] = array();
927
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
928
 
929
+ foreach ( $sites as $site ) {
930
+ $blog_id = self::get_site_blog_id( $site );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
931
 
932
+ if ( isset( $connection_status[ $slug ][ $blog_id ] ) ) {
933
+ continue;
934
+ }
935
 
936
+ $storage = FS_Storage::instance( WP_FS__MODULE_TYPE_PLUGIN, $slug );
 
937
 
938
+ $is_anonymous = $storage->get( 'is_anonymous', null, $blog_id );
 
939
 
940
+ if ( ! is_null( $is_anonymous ) ) {
941
+ // Since 1.1.3 is_anonymous is an array.
942
+ if ( is_array( $is_anonymous ) && isset( $is_anonymous['is'] ) ) {
943
+ $is_anonymous = $is_anonymous['is'];
944
+ }
945
 
946
+ if ( is_bool( $is_anonymous ) && true === $is_anonymous ) {
947
+ $connection_status[ $slug ][ $blog_id ] = 'skipped';
948
+ }
949
+ }
950
 
951
+ if ( ! isset( $connection_status[ $slug ][ $blog_id ] ) ) {
952
+ $connection_status[ $slug ][ $blog_id ] = 'ignored';
953
+ }
954
+ }
955
+ }
956
 
957
+ $super_admins = array();
 
958
 
959
+ foreach ( $connection_status as $slug => $blogs_status ) {
960
+ $skips = 0;
961
+ $ignores = 0;
962
+ $connections = 0;
963
+ $opted_in_users = array();
964
+ $opted_in_super_admins = array();
965
 
966
+ $storage = FS_Storage::instance( WP_FS__MODULE_TYPE_PLUGIN, $slug );
967
 
968
+ foreach ( $blogs_status as $blog_id => $status_or_user_id ) {
969
+ if ( 'skipped' === $status_or_user_id ) {
970
+ $skips ++;
971
+ } else if ( 'ignored' === $status_or_user_id ) {
972
+ $ignores ++;
973
+ } else if ( FS_User::is_valid_id( $status_or_user_id ) ) {
974
+ $connections ++;
975
 
976
+ if ( ! isset( $opted_in_users[ $status_or_user_id ] ) ) {
977
+ $opted_in_users[ $status_or_user_id ] = array();
978
+ }
979
 
980
+ $opted_in_users[ $status_or_user_id ][] = $blog_id;
 
 
 
 
 
 
 
 
981
 
982
+ if ( isset( $super_admins[ $status_or_user_id ] ) ||
983
+ self::is_super_admin( $status_or_user_id )
984
+ ) {
985
+ // Cache super-admin data.
986
+ $super_admins[ $status_or_user_id ] = true;
 
 
 
 
987
 
988
+ // Remember opted-in super-admins for the plugin.
989
+ $opted_in_super_admins[ $status_or_user_id ] = true;
990
+ }
991
+ }
992
+ }
 
 
 
993
 
994
+ $main_super_admin_user_id = null;
995
+ $all_migrated = false;
996
+ if ( $sites_count == $skips ) {
997
+ // All sites were skipped -> network skip by copying the anonymous mode from any of the sites.
998
+ $storage->is_anonymous_ms = $storage->is_anonymous;
999
+
1000
+ $all_migrated = true;
1001
+ } else if ( $sites_count == $ignores ) {
1002
+ // Don't do anything, still in activation mode.
1003
+
1004
+ $all_migrated = true;
1005
+ } else if ( 0 < count( $opted_in_super_admins ) ) {
1006
+ // Find the super-admin with the majority of installs.
1007
+ $max_installs_by_super_admin = 0;
1008
+ foreach ( $opted_in_super_admins as $user_id => $true ) {
1009
+ $installs_count = count( $opted_in_users[ $user_id ] );
1010
+
1011
+ if ( $installs_count > $max_installs_by_super_admin ) {
1012
+ $max_installs_by_super_admin = $installs_count;
1013
+ $main_super_admin_user_id = $user_id;
1014
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1015
  }
1016
 
1017
+ if ( $sites_count == $connections && 1 == count( $opted_in_super_admins ) ) {
1018
+ // Super-admin opted-in for all sites in the network.
1019
+ $storage->is_network_connected = true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1020
 
1021
+ $all_migrated = true;
1022
+ }
1023
 
1024
+ // Store network user.
1025
+ $storage->network_user_id = $main_super_admin_user_id;
1026
 
1027
+ $storage->network_install_blog_id = ( $sites_count == $connections ) ?
1028
+ // Since all sites are opted-in, associating with the main site.
1029
+ get_current_blog_id() :
1030
+ // Associating with the 1st found opted-in site.
1031
+ $opted_in_users[ $main_super_admin_user_id ][0];
1032
 
1033
+ /**
1034
+ * Make sure we migrate the plan ID of the network install, otherwise, if after the migration
1035
+ * the 1st page that will be loaded is the network level WP Admin and $storage->network_install_blog_id
1036
+ * is different than the main site of the network, the $this->_site will not be set since the plan_id
1037
+ * will be empty.
1038
+ */
1039
+ $storage->migrate_to_network();
1040
+ self::migrate_install_plan_to_plan_id( $storage, $storage->network_install_blog_id );
1041
+ } else {
1042
+ // At least one opt-in. All the opt-in were created by a non-super-admin.
1043
+ if ( 0 == $ignores ) {
1044
+ // All sites were opted-in or skipped, all by non-super-admin. So delegate all.
1045
+ $storage->store( 'is_delegated_connection', true, true );
1046
 
1047
+ $all_migrated = true;
1048
+ }
1049
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1050
 
1051
+ if ( ! $all_migrated ) {
1052
+ /**
1053
+ * Delegate all sites that were:
1054
+ * 1) Opted-in by a user that is NOT the main-super-admin.
1055
+ * 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.
1056
+ */
1057
+ foreach ( $blogs_status as $blog_id => $status_or_user_id ) {
1058
+ if ( $status_or_user_id == $main_super_admin_user_id ) {
1059
+ continue;
1060
+ }
1061
 
1062
+ if ( FS_User::is_valid_id( $status_or_user_id ) ||
1063
+ ( 'skipped' === $status_or_user_id && is_null( $main_super_admin_user_id ) )
1064
+ ) {
1065
+ $storage->store( 'is_delegated_connection', true, $blog_id );
1066
+ }
1067
+ }
1068
  }
1069
 
1070
+
1071
+ if ( ( $connections + $skips > 0 ) ) {
1072
+ if ( $ignores > 0 ) {
1073
+ /**
1074
+ * If admin already opted-in or skipped in any of the network sites, and also
1075
+ * have sites which the connection decision was not yet taken, set this plugin
1076
+ * into network activation mode so the super-admin can choose what to do with
1077
+ * the rest of the sites.
1078
+ */
1079
+ self::set_network_upgrade_mode( $storage );
1080
+ }
1081
+ }
1082
  }
1083
+ }
1084
 
1085
+ /**
1086
+ * Set a module into network upgrade mode.
1087
+ *
1088
+ * @author Vova Feldman (@svovaf)
1089
+ * @since 2.0.0
1090
+ *
1091
+ * @param \FS_Storage $storage
1092
+ *
1093
+ * @return bool
1094
+ */
1095
+ private static function set_network_upgrade_mode( FS_Storage $storage ) {
1096
+ return $storage->is_network_activation = true;
1097
  }
1098
 
1099
+ /**
1100
+ * Will return true after upgrading to the SDK with the network level integration,
1101
+ * when the super-admin involvement is required regarding the rest of the sites.
1102
+ *
1103
+ * @author Vova Feldman (@svovaf)
1104
+ * @since 2.0.0
1105
+ *
1106
+ * @return bool
1107
+ */
1108
+ function is_network_upgrade_mode() {
1109
+ return $this->_storage->get( 'is_network_activation' );
1110
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1111
 
1112
+ /**
1113
+ * Clear flag after the upgrade mode completion.
1114
+ *
1115
+ * @author Vova Feldman (@svovaf)
1116
+ * @since 2.0.0
1117
+ *
1118
+ * @return bool True if network activation was on and now completed.
1119
+ */
1120
+ private function network_upgrade_mode_completed() {
1121
+ if ( fs_is_network_admin() && $this->is_network_upgrade_mode() ) {
1122
+ $this->_storage->remove( 'is_network_activation' );
1123
 
1124
+ return true;
1125
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1126
 
1127
+ return false;
1128
+ }
1129
 
1130
+ #endregion
1131
 
1132
+ /**
1133
+ * This action is connected to the 'plugins_loaded' hook and helps to determine
1134
+ * if this is a new plugin installation or a plugin update.
1135
+ *
1136
+ * There are 3 different use-cases:
1137
+ * 1) New plugin installation right with Freemius:
1138
+ * 1.1 _activate_plugin_event_hook() will be executed first
1139
+ * 1.2 Since $this->_storage->is_plugin_new_install is not set,
1140
+ * and $this->_storage->plugin_last_version is not set,
1141
+ * $this->_storage->is_plugin_new_install will be set to TRUE.
1142
+ * 1.3 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will
1143
+ * be already set to TRUE.
1144
+ *
1145
+ * 2) Plugin update, didn't have Freemius before, and now have the SDK:
1146
+ * 2.1 _activate_plugin_event_hook() will not be executed, because
1147
+ * the activation hook do NOT fires on updates since WP 3.1.
1148
+ * 2.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will
1149
+ * be empty, therefore, it will be set to FALSE.
1150
+ *
1151
+ * 3) Plugin update, had Freemius in prev version as well:
1152
+ * 3.1 _version_updates_handler() will be executed 1st, since FS was installed
1153
+ * before, $this->_storage->plugin_last_version will NOT be empty,
1154
+ * therefore, $this->_storage->is_plugin_new_install will be set to FALSE.
1155
+ * 3.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install is
1156
+ * already set, therefore, it will not be modified.
1157
+ *
1158
+ * Use-case #3 is backward compatible, #3.1 will be executed since 1.0.9.
1159
+ *
1160
+ * NOTE:
1161
+ * The only fallback of this mechanism is if an admin updates a plugin based on use-case #2,
1162
+ * and then, the next immediate PageView is the plugin's main settings page, it will not
1163
+ * show the opt-in right away. The reason it will happen is because Freemius execution
1164
+ * will be turned off till the plugin is fully loaded at least once
1165
+ * (till $this->_storage->was_plugin_loaded is TRUE).
1166
+ *
1167
+ * @author Vova Feldman (@svovaf)
1168
+ * @since 1.1.9
1169
+ *
1170
+ */
1171
+ function _plugins_loaded() {
1172
+ // Update flag that plugin was loaded with Freemius at least once.
1173
+ $this->_storage->was_plugin_loaded = true;
1174
 
1175
+ /**
1176
+ * Bug fix - only set to false when it's a plugin, due to the
1177
+ * execution sequence of the theme hooks and our methods, if
1178
+ * this will be set for themes, Freemius will always assume
1179
+ * it's a theme update.
1180
+ *
1181
+ * @author Vova Feldman (@svovaf)
1182
+ * @since 1.2.2.2
1183
+ */
1184
+ if ( $this->is_plugin() &&
1185
+ ! isset( $this->_storage->is_plugin_new_install )
1186
+ ) {
1187
+ $this->_storage->is_plugin_new_install = false;
1188
+ }
1189
+ }
1190
 
1191
+ /**
1192
+ * Add special parameter to WP admin AJAX calls so when we
1193
+ * process AJAX calls we can identify its source properly.
1194
+ *
1195
+ * @author Leo Fajardo (@leorw)
1196
+ * @since 2.0.0
1197
+ */
1198
+ static function _enrich_ajax_url() {
1199
+ $admin_param = is_network_admin() ?
1200
+ '_fs_network_admin' :
1201
+ '_fs_blog_admin';
1202
+ ?>
1203
+ <script type="text/javascript">
1204
+ (function ($) {
1205
+ $(document).ajaxSend(function (event, jqxhr, settings) {
1206
+ if (settings.url &&
1207
+ -1 < settings.url.indexOf('admin-ajax.php') &&
1208
+ ! ( settings.url.indexOf( '<?php echo $admin_param ?>' ) > 0 )
1209
+ ) {
1210
+ if (settings.url.indexOf('?') > 0) {
1211
+ settings.url += '&';
1212
+ } else {
1213
+ settings.url += '?';
1214
+ }
1215
 
1216
+ settings.url += '<?php echo $admin_param ?>=true';
 
 
 
 
 
 
 
 
 
 
 
 
1217
 
1218
+ }
1219
+ });
1220
+ })(jQuery);
1221
+ </script>
1222
+ <?php
1223
+ }
1224
 
1225
+ /**
1226
+ * @author Vova Feldman (@svovaf)
1227
+ * @since 1.0.9
1228
+ */
1229
+ private function _register_hooks() {
1230
+ $this->_logger->entrance();
1231
 
1232
+ if ( is_admin() ) {
1233
+ add_action( 'plugins_loaded', array( &$this, '_hook_action_links_and_register_account_hooks' ) );
 
 
 
 
 
 
1234
 
1235
+ if ( $this->is_plugin() ) {
1236
+ $plugin_dir = dirname( $this->_plugin_dir_path ) . '/';
1237
 
1238
+ /**
1239
+ * @since 1.2.2
1240
+ *
1241
+ * Hook to both free and premium version activations to support
1242
+ * auto deactivation on the other version activation.
1243
+ */
1244
+ register_activation_hook(
1245
+ $plugin_dir . $this->_free_plugin_basename,
1246
+ array( &$this, '_activate_plugin_event_hook' )
1247
+ );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1248
 
1249
+ register_activation_hook(
1250
+ $plugin_dir . $this->premium_plugin_basename(),
1251
+ array( &$this, '_activate_plugin_event_hook' )
1252
+ );
1253
+ } else {
1254
+ add_action( 'after_switch_theme', array( &$this, '_activate_theme_event_hook' ), 10, 2 );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1255
 
1256
+ /**
1257
+ * Include the required hooks to capture the theme settings' page tabs
1258
+ * and cache them.
1259
+ *
1260
+ * @author Vova Feldman (@svovaf)
1261
+ * @since 1.2.2.7
1262
+ */
1263
+ if ( ! $this->_cache->has_valid( 'tabs' ) ) {
1264
+ add_action( 'admin_footer', array( &$this, '_tabs_capture' ) );
1265
+ // Add license activation AJAX callback.
1266
+ $this->add_ajax_action( 'store_tabs', array( &$this, '_store_tabs_ajax_action' ) );
1267
 
1268
+ add_action( 'admin_enqueue_scripts', array( &$this, '_store_tabs_styles' ), 9999999 );
1269
+ }
1270
 
1271
+ add_action(
1272
+ 'admin_footer',
1273
+ array( &$this, '_add_freemius_tabs' ),
1274
+ /**
1275
+ * The tabs JS code must be executed after the tabs capture logic (_tabs_capture()).
1276
+ * That's why the priority is 11 while the tabs capture logic is added
1277
+ * with priority 10.
1278
+ *
1279
+ * @author Vova Feldman (@svovaf)
1280
+ */
1281
+ 11
1282
+ );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1283
 
1284
+ add_action( 'admin_footer', array( &$this, '_style_premium_theme' ) );
1285
+ }
1286
+
1287
+ /**
1288
+ * Part of the mechanism to identify new plugin install vs. plugin update.
1289
+ *
1290
+ * @author Vova Feldman (@svovaf)
1291
+ * @since 1.1.9
1292
+ */
1293
+ if ( empty( $this->_storage->was_plugin_loaded ) ) {
1294
+ /**
1295
+ * During the plugin activation (not theme), 'plugins_loaded' will be already executed
1296
+ * when the logic gets here since the activation logic first add the activate plugins,
1297
+ * then triggers 'plugins_loaded', and only then include the code of the plugin that
1298
+ * is activated. Which means that _plugins_loaded() will NOT be executed during the
1299
+ * plugin activation, and that IS intentional.
1300
+ *
1301
+ * @author Vova Feldman (@svovaf)
1302
+ */
1303
+ if ( $this->is_plugin() && $this->is_activation_mode( false ) ) {
1304
+ add_action( 'plugins_loaded', array( &$this, '_plugins_loaded' ) );
1305
+ } else {
1306
+ // If was activated before, then it was already loaded before.
1307
+ $this->_plugins_loaded();
1308
+ }
1309
+ }
1310
+
1311
+ if ( ! self::is_ajax() ) {
1312
+ if ( ! $this->is_addon() ) {
1313
+ add_action( 'init', array( &$this, '_add_default_submenu_items' ), WP_FS__LOWEST_PRIORITY );
1314
+ }
1315
+ }
1316
+ }
1317
+
1318
+ if ( $this->is_plugin() ) {
1319
+ if ( $this->_is_network_active ) {
1320
+ add_action( 'wpmu_new_blog', array( $this, '_after_new_blog_callback' ), 10, 6 );
1321
+ }
1322
+
1323
+ register_deactivation_hook( $this->_plugin_main_file_path, array( &$this, '_deactivate_plugin_hook' ) );
1324
+ }
1325
+
1326
+ if ( is_multisite() ) {
1327
+ add_action( 'deactivate_blog', array( &$this, '_after_site_deactivated_callback' ) );
1328
+ add_action( 'archive_blog', array( &$this, '_after_site_deactivated_callback' ) );
1329
+ add_action( 'make_spam_blog', array( &$this, '_after_site_deactivated_callback' ) );
1330
+ add_action( 'deleted_blog', array( &$this, '_after_site_deleted_callback' ), 10, 2 );
1331
+
1332
+ add_action( 'activate_blog', array( &$this, '_after_site_reactivated_callback' ) );
1333
+ add_action( 'unarchive_blog', array( &$this, '_after_site_reactivated_callback' ) );
1334
+ add_action( 'make_ham_blog', array( &$this, '_after_site_reactivated_callback' ) );
1335
+ }
1336
+
1337
+ if ( $this->is_theme() && self::is_customizer() ) {
1338
+ // Register customizer upsell.
1339
+ add_action( 'customize_register', array( &$this, '_customizer_register' ) );
1340
+ }
1341
+
1342
+ add_action( 'init', array( &$this, '_redirect_on_clicked_menu_link' ), WP_FS__LOWEST_PRIORITY );
1343
+
1344
+ if ( $this->is_theme() ) {
1345
+ add_action( 'admin_init', array( &$this, '_add_tracking_links' ) );
1346
+ }
1347
+
1348
+ add_action( 'admin_init', array( &$this, '_add_license_activation' ) );
1349
+ $this->add_ajax_action( 'update_billing', array( &$this, '_update_billing_ajax_action' ) );
1350
+ $this->add_ajax_action( 'start_trial', array( &$this, '_start_trial_ajax_action' ) );
1351
+
1352
+ if ( $this->_is_network_active && fs_is_network_admin() ) {
1353
+ $this->add_ajax_action( 'network_activate', array( &$this, '_network_activate_ajax_action' ) );
1354
+ }
1355
+
1356
+ $this->add_ajax_action( 'install_premium_version', array(
1357
+ &$this,
1358
+ '_install_premium_version_ajax_action'
1359
+ ) );
1360
+
1361
+ $this->add_ajax_action( 'submit_affiliate_application', array( &$this, '_submit_affiliate_application' ) );
1362
+
1363
+ $this->add_action( 'after_plans_sync', array( &$this, '_check_for_trial_plans' ) );
1364
+
1365
+ $this->add_action( 'sdk_version_update', array( &$this, '_data_migration' ), WP_FS__DEFAULT_PRIORITY, 2 );
1366
+ $this->add_action(
1367
+ 'plugin_version_update',
1368
+ array( &$this, '_after_version_update' ),
1369
+ WP_FS__DEFAULT_PRIORITY,
1370
+ 2
1371
+ );
1372
+ $this->add_filter( 'after_code_type_change', array( &$this, '_after_code_type_change' ) );
1373
+
1374
+ add_action( 'admin_init', array( &$this, '_add_trial_notice' ) );
1375
+ add_action( 'admin_init', array( &$this, '_add_affiliate_program_notice' ) );
1376
+ add_action( 'admin_init', array( &$this, '_enqueue_common_css' ) );
1377
+
1378
+ /**
1379
+ * Handle request to reset anonymous mode for `get_reconnect_url()`.
1380
+ *
1381
+ * @author Vova Feldman (@svovaf)
1382
+ * @since 1.2.1.5
1383
+ */
1384
+ if ( fs_request_is_action( 'reset_anonymous_mode' ) &&
1385
+ $this->get_unique_affix() === fs_request_get( 'fs_unique_affix' )
1386
+ ) {
1387
+ add_action( 'admin_init', array( &$this, 'connect_again' ) );
1388
+ }
1389
+ }
1390
+
1391
+ /**
1392
+ * Keeping the uninstall hook registered for free or premium plugin version may result to a fatal error that
1393
+ * could happen when a user tries to uninstall either version while one of them is still active. Uninstalling a
1394
+ * plugin will trigger inclusion of the free or premium version and if one of them is active during the
1395
+ * uninstallation, a fatal error may occur in case the plugin's class or functions are already defined.
1396
+ *
1397
+ * @author Leo Fajardo (@leorw)
1398
+ *
1399
+ * @since 1.2.0
1400
+ */
1401
+ private function unregister_uninstall_hook() {
1402
+ $uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
1403
+ unset( $uninstallable_plugins[ $this->_free_plugin_basename ] );
1404
+ unset( $uninstallable_plugins[ $this->premium_plugin_basename() ] );
1405
+
1406
+ update_option( 'uninstall_plugins', $uninstallable_plugins );
1407
+ }
1408
+
1409
+ /**
1410
+ * @since 1.2.0 Invalidate module's main file cache, otherwise, FS_Plugin_Updater will not fetch updates.
1411
+ */
1412
+ private function clear_module_main_file_cache() {
1413
+ if ( ! isset( $this->_storage->plugin_main_file ) ||
1414
+ empty( $this->_storage->plugin_main_file->path )
1415
+ ) {
1416
+ return;
1417
+ }
1418
+
1419
+ $plugin_main_file = clone $this->_storage->plugin_main_file;
1420
+
1421
+ // Store cached path (2nd layer cache).
1422
+ $plugin_main_file->prev_path = $plugin_main_file->path;
1423
+
1424
+ // Clear cached path.
1425
+ unset( $plugin_main_file->path );
1426
+
1427
+ $this->_storage->plugin_main_file = $plugin_main_file;
1428
+
1429
+ /**
1430
+ * Clear global cached path.
1431
+ *
1432
+ * @author Leo Fajardo (@leorw)
1433
+ * @since 1.2.2
1434
+ */
1435
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map' );
1436
+ unset( $id_slug_type_path_map[ $this->_module_id ]['path'] );
1437
+ self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
1438
+ }
1439
+
1440
+ /**
1441
+ * @author Leo Fajardo (@leorw)
1442
+ * @since 2.0.0
1443
+ */
1444
+ function _hook_action_links_and_register_account_hooks() {
1445
+ add_action( 'admin_init', array( &$this, '_add_tracking_links' ) );
1446
+
1447
+ if ( self::is_plugins_page() && $this->is_plugin() ) {
1448
+ $this->hook_plugin_action_links();
1449
+ }
1450
+
1451
+ $this->_register_account_hooks();
1452
+ }
1453
+
1454
+ /**
1455
+ * @author Vova Feldman (@svovaf)
1456
+ * @since 1.0.9
1457
+ */
1458
+ private function _register_account_hooks() {
1459
+ if ( ! is_admin() ) {
1460
+ return;
1461
+ }
1462
+
1463
+ /**
1464
+ * Always show the deactivation feedback form since we added
1465
+ * automatic free version deactivation upon premium code activation.
1466
+ *
1467
+ * @since 1.2.1.6
1468
+ */
1469
+ $this->add_ajax_action(
1470
+ 'submit_uninstall_reason',
1471
+ array( &$this, '_submit_uninstall_reason_action' )
1472
+ );
1473
+
1474
+ if ( ! $this->is_addon() || $this->is_parent_plugin_installed() ) {
1475
+ if ( ( $this->is_plugin() && self::is_plugins_page() ) ||
1476
+ ( $this->is_theme() && self::is_themes_page() )
1477
+ ) {
1478
+ add_action( 'admin_footer', array( &$this, '_add_deactivation_feedback_dialog_box' ) );
1479
+ }
1480
+ }
1481
+ }
1482
+
1483
+ /**
1484
+ * Leverage backtrace to find caller plugin file path.
1485
+ *
1486
+ * @author Vova Feldman (@svovaf)
1487
+ * @since 1.0.6
1488
+ *
1489
+ * @param bool $is_init Is initiation sequence.
1490
+ *
1491
+ * @return string
1492
+ */
1493
+ private function _find_caller_plugin_file( $is_init = false ) {
1494
+ // Try to load the cached value of the file path.
1495
+ if ( isset( $this->_storage->plugin_main_file ) ) {
1496
+ $plugin_main_file = $this->_storage->plugin_main_file;
1497
+ if ( isset( $plugin_main_file->path ) ) {
1498
+ $absolute_path = $this->get_absolute_path( $plugin_main_file->path );
1499
+ if ( file_exists( $absolute_path ) ) {
1500
+ return $absolute_path;
1501
+ }
1502
+ }
1503
+ }
1504
+
1505
+ /**
1506
+ * @since 1.2.1
1507
+ *
1508
+ * `clear_module_main_file_cache()` is clearing the plugin's cached path on
1509
+ * deactivation. Therefore, if any plugin/theme was initiating `Freemius`
1510
+ * with that plugin's slug, it was overriding the empty plugin path with a wrong path.
1511
+ *
1512
+ * So, we've added a special mechanism with a 2nd layer of cache that uses `prev_path`
1513
+ * when the class instantiator isn't the module.
1514
+ */
1515
+ if ( ! $is_init ) {
1516
+ // Fetch prev path cache.
1517
+ if ( isset( $this->_storage->plugin_main_file ) &&
1518
+ isset( $this->_storage->plugin_main_file->prev_path )
1519
+ ) {
1520
+ $absolute_path = $this->get_absolute_path( $this->_storage->plugin_main_file->prev_path );
1521
+ if ( file_exists( $absolute_path ) ) {
1522
+ return $absolute_path;
1523
+ }
1524
+ }
1525
+
1526
+ wp_die(
1527
+ $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' ) .
1528
+ " Module: {$this->_slug}; SDK: " . WP_FS__SDK_VERSION . ";",
1529
+ $this->get_text_inline( 'Error', 'error' ),
1530
+ array( 'back_link' => true )
1531
+ );
1532
+ }
1533
+
1534
+ /**
1535
+ * @since 1.2.1
1536
+ *
1537
+ * Only the original instantiator that calls dynamic_init can modify the module's path.
1538
+ */
1539
+ // Find caller module.
1540
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
1541
+ $this->_storage->plugin_main_file = (object) array(
1542
+ 'path' => $id_slug_type_path_map[ $this->_module_id ]['path'],
1543
+ );
1544
+
1545
+ return $this->get_absolute_path( $id_slug_type_path_map[ $this->_module_id ]['path'] );
1546
+ }
1547
+
1548
+ /**
1549
+ * @author Leo Fajardo (@leorw)
1550
+ * @since 1.2.3
1551
+ *
1552
+ * @param string $path
1553
+ *
1554
+ * @return string
1555
+ */
1556
+ private function get_relative_path( $path ) {
1557
+ $module_root_dir = $this->get_module_root_dir_path();
1558
+ if ( 0 === strpos( $path, $module_root_dir ) ) {
1559
+ $path = substr( $path, strlen( $module_root_dir ) );
1560
+ }
1561
+
1562
+ return $path;
1563
+ }
1564
+
1565
+ /**
1566
+ * @author Leo Fajardo (@leorw)
1567
+ * @since 1.2.3
1568
+ *
1569
+ * @param string $path
1570
+ * @param string|bool $module_type
1571
+ *
1572
+ * @return string
1573
+ */
1574
+ private function get_absolute_path( $path, $module_type = false ) {
1575
+ $module_root_dir = $this->get_module_root_dir_path( $module_type );
1576
+ if ( 0 !== strpos( $path, $module_root_dir ) ) {
1577
+ $path = fs_normalize_path( $module_root_dir . $path );
1578
+ }
1579
+
1580
+ return $path;
1581
+ }
1582
+
1583
+ /**
1584
+ * @author Leo Fajardo (@leorw)
1585
+ * @since 1.2.3
1586
+ *
1587
+ * @param string|bool $module_type
1588
+ *
1589
+ * @return string
1590
+ */
1591
+ private function get_module_root_dir_path( $module_type = false ) {
1592
+ $is_plugin = empty( $module_type ) ?
1593
+ $this->is_plugin() :
1594
+ ( WP_FS__MODULE_TYPE_PLUGIN === $module_type );
1595
+
1596
+ return fs_normalize_path( trailingslashit( $is_plugin ?
1597
+ WP_PLUGIN_DIR :
1598
+ get_theme_root() ) );
1599
+ }
1600
+
1601
+ /**
1602
+ * @author Leo Fajardo (@leorw)
1603
+ *
1604
+ * @param number $module_id
1605
+ * @param string $slug
1606
+ *
1607
+ * @since 1.2.2
1608
+ */
1609
+ private function store_id_slug_type_path_map( $module_id, $slug ) {
1610
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
1611
+
1612
+ $store_option = false;
1613
+
1614
+ if ( ! isset( $id_slug_type_path_map[ $module_id ] ) ) {
1615
+ $id_slug_type_path_map[ $module_id ] = array(
1616
+ 'slug' => $slug
1617
+ );
1618
+
1619
+ $store_option = true;
1620
+ }
1621
+
1622
+ if ( ! isset( $id_slug_type_path_map[ $module_id ]['path'] ) ||
1623
+ /**
1624
+ * This verification is for cases when suddenly the same module
1625
+ * is installed but with a different folder name.
1626
+ *
1627
+ * @author Vova Feldman (@svovaf)
1628
+ * @since 1.2.3
1629
+ */
1630
+ ! file_exists( $this->get_absolute_path(
1631
+ $id_slug_type_path_map[ $module_id ]['path'],
1632
+ $id_slug_type_path_map[ $module_id ]['type']
1633
+ ) )
1634
+ ) {
1635
+ $caller_main_file_and_type = $this->get_caller_main_file_and_type();
1636
+
1637
+ $id_slug_type_path_map[ $module_id ]['type'] = $caller_main_file_and_type->module_type;
1638
+ $id_slug_type_path_map[ $module_id ]['path'] = $caller_main_file_and_type->path;
1639
+
1640
+ $store_option = true;
1641
+ }
1642
+
1643
+ if ( $store_option ) {
1644
+ self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
1645
+ }
1646
+ }
1647
+
1648
+ /**
1649
+ * Identifies the caller type: plugin or theme.
1650
+ *
1651
+ * @author Leo Fajardo (@leorw)
1652
+ * @since 1.2.2
1653
+ *
1654
+ * @author Vova Feldman (@svovaf)
1655
+ * @since 1.2.2.3 Find the earliest module in the call stack that calls to the SDK. This fix is for cases when
1656
+ * add-ons are relying on loading the SDK from the parent module, and also allows themes including the
1657
+ * SDK an internal file instead of directly from functions.php.
1658
+ * @since 1.2.1.7 Knows how to handle cases when an add-on includes the parent module logic.
1659
+ */
1660
+ private function get_caller_main_file_and_type() {
1661
+ self::require_plugin_essentials();
1662
+
1663
+ $all_plugins = get_plugins();
1664
+ $all_plugins_paths = array();
1665
+
1666
+ // Get active plugin's main files real full names (might be symlinks).
1667
+ foreach ( $all_plugins as $relative_path => &$data ) {
1668
+ if ( false === strpos( fs_normalize_path( $relative_path ), '/' ) ) {
1669
+ /**
1670
+ * Ignore plugins that don't have a folder (e.g. Hello Dolly) since they
1671
+ * can't really include the SDK.
1672
+ *
1673
+ * @author Vova Feldman
1674
+ * @since 1.2.1.7
1675
+ */
1676
+ continue;
1677
+ }
1678
+
1679
+ $all_plugins_paths[] = fs_normalize_path( realpath( WP_PLUGIN_DIR . '/' . $relative_path ) );
1680
+ }
1681
+
1682
+ $caller_file_candidate = false;
1683
+ $caller_map = array();
1684
+ $module_type = WP_FS__MODULE_TYPE_PLUGIN;
1685
+ $themes_dir = fs_normalize_path( get_theme_root() );
1686
+
1687
+ for ( $i = 1, $bt = debug_backtrace(), $len = count( $bt ); $i < $len; $i ++ ) {
1688
+ if ( empty( $bt[ $i ]['file'] ) ) {
1689
+ continue;
1690
+ }
1691
+
1692
+ if ( $i > 1 && ! empty( $bt[ $i - 1 ]['file'] ) && $bt[ $i ]['file'] === $bt[ $i - 1 ]['file'] ) {
1693
+ // If file same as the prev file in the stack, skip it.
1694
+ continue;
1695
+ }
1696
+
1697
+ if ( ! empty( $bt[ $i ]['function'] ) && in_array( $bt[ $i ]['function'], array(
1698
+ 'do_action',
1699
+ 'apply_filter',
1700
+ // The string split is stupid, but otherwise, theme check
1701
+ // throws info notices.
1702
+ 'requir' . 'e_once',
1703
+ 'requir' . 'e',
1704
+ 'includ' . 'e_once',
1705
+ 'includ' . 'e'
1706
+ ) )
1707
+ ) {
1708
+ // Ignore call stack hooks and files inclusion.
1709
+ continue;
1710
+ }
1711
+
1712
+ $caller_file_path = fs_normalize_path( $bt[ $i ]['file'] );
1713
+
1714
+ if ( 'functions.php' === basename( $caller_file_path ) ) {
1715
+ /**
1716
+ * 1. Assumes that theme's starting execution file is functions.php.
1717
+ * 2. This complex logic fixes symlink issues (e.g. with Vargant).
1718
+ *
1719
+ * @author Vova Feldman (@svovaf)
1720
+ * @since 1.2.2.5
1721
+ */
1722
+
1723
+ if ( $caller_file_path == fs_normalize_path( realpath( trailingslashit( $themes_dir ) . basename( dirname( $caller_file_path ) ) . '/' . basename( $caller_file_path ) ) ) ) {
1724
+ $module_type = WP_FS__MODULE_TYPE_THEME;
1725
+
1726
+ /**
1727
+ * Relative path of the theme, e.g.:
1728
+ * `my-theme/functions.php`
1729
+ *
1730
+ * @author Leo Fajardo (@leorw)
1731
+ */
1732
+ $caller_file_candidate = basename( dirname( $caller_file_path ) ) .
1733
+ '/' .
1734
+ basename( $caller_file_path );
1735
+
1736
+ continue;
1737
+ }
1738
+ }
1739
+
1740
+ $caller_file_hash = md5( $caller_file_path );
1741
+
1742
+ if ( ! isset( $caller_map[ $caller_file_hash ] ) ) {
1743
+ foreach ( $all_plugins_paths as $plugin_path ) {
1744
+ if ( false !== strpos( $caller_file_path, fs_normalize_path( dirname( $plugin_path ) . '/' ) ) ) {
1745
+ $caller_map[ $caller_file_hash ] = fs_normalize_path( $plugin_path );
1746
+ break;
1747
+ }
1748
+ }
1749
+ }
1750
+
1751
+ if ( isset( $caller_map[ $caller_file_hash ] ) ) {
1752
+ $module_type = WP_FS__MODULE_TYPE_PLUGIN;
1753
+ $caller_file_candidate = plugin_basename( $caller_map[ $caller_file_hash ] );
1754
+ }
1755
+ }
1756
+
1757
+ return (object) array(
1758
+ 'module_type' => $module_type,
1759
+ 'path' => $caller_file_candidate
1760
+ );
1761
+ }
1762
+
1763
+ #----------------------------------------------------------------------------------
1764
+ #region Deactivation Feedback Form
1765
+ #----------------------------------------------------------------------------------
1766
+
1767
+ /**
1768
+ * Displays a confirmation and feedback dialog box when the user clicks on the "Deactivate" link on the plugins
1769
+ * page.
1770
+ *
1771
+ * @author Vova Feldman (@svovaf)
1772
+ * @author Leo Fajardo (@leorw)
1773
+ * @since 1.1.2
1774
+ */
1775
+ function _add_deactivation_feedback_dialog_box() {
1776
+ /* Check the type of user:
1777
+ * 1. Long-term (long-term)
1778
+ * 2. Non-registered and non-anonymous short-term (non-registered-and-non-anonymous-short-term).
1779
+ * 3. Short-term (short-term)
1780
+ */
1781
+ $is_long_term_user = true;
1782
+
1783
+ // Check if the site is at least 2 days old.
1784
+ $time_installed = $this->_storage->install_timestamp;
1785
+
1786
+ // Difference in seconds.
1787
+ $date_diff = time() - $time_installed;
1788
+
1789
+ // Convert seconds to days.
1790
+ $date_diff_days = floor( $date_diff / ( 60 * 60 * 24 ) );
1791
+
1792
+ if ( $date_diff_days < 2 ) {
1793
+ $is_long_term_user = false;
1794
+ }
1795
+
1796
+ $is_long_term_user = $this->apply_filters( 'is_long_term_user', $is_long_term_user );
1797
+
1798
+ if ( $is_long_term_user ) {
1799
+ $user_type = 'long-term';
1800
+ } else {
1801
+ if ( ! $this->is_registered() && ! $this->is_anonymous() ) {
1802
+ $user_type = 'non-registered-and-non-anonymous-short-term';
1803
+ } else {
1804
+ $user_type = 'short-term';
1805
+ }
1806
+ }
1807
+
1808
+ $uninstall_reasons = $this->_get_uninstall_reasons( $user_type );
1809
+
1810
+ // Load the HTML template for the deactivation feedback dialog box.
1811
+ $vars = array(
1812
+ 'reasons' => $uninstall_reasons,
1813
+ 'id' => $this->_module_id
1814
+ );
1815
+
1816
+ /**
1817
+ * @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.
1818
+ */
1819
+ fs_require_template( 'forms/deactivation/form.php', $vars );
1820
+ }
1821
+
1822
+ /**
1823
+ * @author Leo Fajardo (@leorw)
1824
+ * @since 1.1.2
1825
+ *
1826
+ * @param string $user_type
1827
+ *
1828
+ * @return array The uninstall reasons for the specified user type.
1829
+ */
1830
+ function _get_uninstall_reasons( $user_type = 'long-term' ) {
1831
+ $module_type = $this->_module_type;
1832
+
1833
+ $internal_message_template_var = array(
1834
+ 'id' => $this->_module_id
1835
+ );
1836
+
1837
+ $plan = $this->get_plan();
1838
+
1839
+ if ( $this->is_registered() && is_object( $plan ) && $plan->has_technical_support() ) {
1840
+ $contact_support_template = fs_get_template( 'forms/deactivation/contact.php', $internal_message_template_var );
1841
+ } else {
1842
+ $contact_support_template = '';
1843
+ }
1844
+
1845
+ $reason_found_better_plugin = array(
1846
+ 'id' => self::REASON_FOUND_A_BETTER_PLUGIN,
1847
+ 'text' => sprintf( $this->get_text_inline( 'I found a better %s', 'reason-found-a-better-plugin' ), $module_type ),
1848
+ 'input_type' => 'textfield',
1849
+ 'input_placeholder' => sprintf( $this->get_text_inline( "What's the %s's name?", 'placeholder-plugin-name' ), $module_type ),
1850
+ );
1851
+
1852
+ $reason_temporary_deactivation = array(
1853
+ 'id' => self::REASON_TEMPORARY_DEACTIVATION,
1854
+ 'text' => sprintf(
1855
+ $this->get_text_inline( "It's a temporary %s. I'm just debugging an issue.", 'reason-temporary-x' ),
1856
+ strtolower( $this->is_plugin() ?
1857
+ $this->get_text_inline( 'Deactivation', 'deactivation' ) :
1858
+ $this->get_text_inline( 'Theme Switch', 'theme-switch' )
1859
+ )
1860
+ ),
1861
+ 'input_type' => '',
1862
+ 'input_placeholder' => ''
1863
+ );
1864
+
1865
+ $reason_other = array(
1866
+ 'id' => self::REASON_OTHER,
1867
+ 'text' => $this->get_text_inline( 'Other', 'reason-other' ),
1868
+ 'input_type' => 'textfield',
1869
+ 'input_placeholder' => ''
1870
+ );
1871
+
1872
+ $long_term_user_reasons = array(
1873
+ array(
1874
+ 'id' => self::REASON_NO_LONGER_NEEDED,
1875
+ 'text' => sprintf( $this->get_text_inline( 'I no longer need the %s', 'reason-no-longer-needed' ), $module_type ),
1876
+ 'input_type' => '',
1877
+ 'input_placeholder' => ''
1878
+ ),
1879
+ $reason_found_better_plugin,
1880
+ array(
1881
+ 'id' => self::REASON_NEEDED_FOR_A_SHORT_PERIOD,
1882
+ 'text' => sprintf( $this->get_text_inline( 'I only needed the %s for a short period', 'reason-needed-for-a-short-period' ), $module_type ),
1883
+ 'input_type' => '',
1884
+ 'input_placeholder' => ''
1885
+ ),
1886
+ array(
1887
+ 'id' => self::REASON_BROKE_MY_SITE,
1888
+ 'text' => sprintf( $this->get_text_inline( 'The %s broke my site', 'reason-broke-my-site' ), $module_type ),
1889
+ 'input_type' => '',
1890
+ 'input_placeholder' => '',
1891
+ 'internal_message' => $contact_support_template
1892
+ ),
1893
+ array(
1894
+ 'id' => self::REASON_SUDDENLY_STOPPED_WORKING,
1895
+ 'text' => sprintf( $this->get_text_inline( 'The %s suddenly stopped working', 'reason-suddenly-stopped-working' ), $module_type ),
1896
+ 'input_type' => '',
1897
+ 'input_placeholder' => '',
1898
+ 'internal_message' => $contact_support_template
1899
+ )
1900
+ );
1901
+
1902
+ if ( $this->is_paying() ) {
1903
+ $long_term_user_reasons[] = array(
1904
+ 'id' => self::REASON_CANT_PAY_ANYMORE,
1905
+ 'text' => $this->get_text_inline( "I can't pay for it anymore", 'reason-cant-pay-anymore' ),
1906
+ 'input_type' => 'textfield',
1907
+ 'input_placeholder' => $this->get_text_inline( 'What price would you feel comfortable paying?', 'placeholder-comfortable-price' )
1908
+ );
1909
+ }
1910
+
1911
+ $reason_dont_share_info = array(
1912
+ 'id' => self::REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION,
1913
+ 'text' => $this->get_text_inline( "I don't like to share my information with you", 'reason-dont-like-to-share-my-information' ),
1914
+ 'input_type' => '',
1915
+ 'input_placeholder' => ''
1916
+ );
1917
+
1918
+ /**
1919
+ * If the current user has selected the "don't share data" reason in the deactivation feedback modal, inform the
1920
+ * user by showing additional message that he doesn't have to share data and can just choose to skip the opt-in
1921
+ * (the Skip button is included in the message to show). This message will only be shown if anonymous mode is
1922
+ * enabled and the user's account is currently not in pending activation state (similar to the way the Skip
1923
+ * button in the opt-in form is shown/hidden).
1924
+ */
1925
+ if ( $this->is_enable_anonymous() && ! $this->is_pending_activation() ) {
1926
+ $reason_dont_share_info['internal_message'] = fs_get_template( 'forms/deactivation/retry-skip.php', $internal_message_template_var );
1927
+ }
1928
+
1929
+ $uninstall_reasons = array(
1930
+ 'long-term' => $long_term_user_reasons,
1931
+ 'non-registered-and-non-anonymous-short-term' => array(
1932
+ array(
1933
+ 'id' => self::REASON_DIDNT_WORK,
1934
+ 'text' => sprintf( $this->get_text_inline( "The %s didn't work", 'reason-didnt-work' ), $module_type ),
1935
+ 'input_type' => '',
1936
+ 'input_placeholder' => ''
1937
+ ),
1938
+ $reason_dont_share_info,
1939
+ $reason_found_better_plugin
1940
+ ),
1941
+ 'short-term' => array(
1942
+ array(
1943
+ 'id' => self::REASON_COULDNT_MAKE_IT_WORK,
1944
+ 'text' => $this->get_text_inline( "I couldn't understand how to make it work", 'reason-couldnt-make-it-work' ),
1945
+ 'input_type' => '',
1946
+ 'input_placeholder' => '',
1947
+ 'internal_message' => $contact_support_template
1948
+ ),
1949
+ $reason_found_better_plugin,
1950
+ array(
1951
+ 'id' => self::REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE,
1952
+ '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 ),
1953
+ 'input_type' => 'textarea',
1954
+ 'input_placeholder' => $this->get_text_inline( 'What feature?', 'placeholder-feature' )
1955
+ ),
1956
+ array(
1957
+ 'id' => self::REASON_NOT_WORKING,
1958
+ 'text' => sprintf( $this->get_text_inline( 'The %s is not working', 'reason-not-working' ), $module_type ),
1959
+ 'input_type' => 'textarea',
1960
+ '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' )
1961
+ ),
1962
+ array(
1963
+ 'id' => self::REASON_NOT_WHAT_I_WAS_LOOKING_FOR,
1964
+ 'text' => $this->get_text_inline( "It's not what I was looking for", 'reason-not-what-i-was-looking-for' ),
1965
+ 'input_type' => 'textarea',
1966
+ 'input_placeholder' => $this->get_text_inline( "What you've been looking for?", 'placeholder-what-youve-been-looking-for' )
1967
+ ),
1968
+ array(
1969
+ 'id' => self::REASON_DIDNT_WORK_AS_EXPECTED,
1970
+ 'text' => sprintf( $this->get_text_inline( "The %s didn't work as expected", 'reason-didnt-work-as-expected' ), $module_type ),
1971
+ 'input_type' => 'textarea',
1972
+ 'input_placeholder' => $this->get_text_inline( 'What did you expect?', 'placeholder-what-did-you-expect' )
1973
+ )
1974
+ )
1975
+ );
1976
+
1977
+ // Randomize the reasons for the current user type.
1978
+ shuffle( $uninstall_reasons[ $user_type ] );
1979
+
1980
+ // Keep the following reasons as the last items in the list.
1981
+ $uninstall_reasons[ $user_type ][] = $reason_temporary_deactivation;
1982
+ $uninstall_reasons[ $user_type ][] = $reason_other;
1983
+
1984
+ $uninstall_reasons = $this->apply_filters( 'uninstall_reasons', $uninstall_reasons );
1985
+
1986
+ return $uninstall_reasons[ $user_type ];
1987
+ }
1988
+
1989
+ /**
1990
+ * Called after the user has submitted his reason for deactivating the plugin.
1991
+ *
1992
+ * @author Leo Fajardo (@leorw)
1993
+ * @since 1.1.2
1994
+ */
1995
+ function _submit_uninstall_reason_action() {
1996
+ $this->_logger->entrance();
1997
+
1998
+ $this->check_ajax_referer( 'submit_uninstall_reason' );
1999
+
2000
+ $reason_id = fs_request_get( 'reason_id' );
2001
+
2002
+ // Check if the given reason ID is an unsigned integer.
2003
+ if ( ! ctype_digit( $reason_id ) ) {
2004
+ exit;
2005
+ }
2006
+
2007
+ $reason_info = trim( fs_request_get( 'reason_info', '' ) );
2008
+ if ( ! empty( $reason_info ) ) {
2009
+ $reason_info = substr( $reason_info, 0, 128 );
2010
+ }
2011
+
2012
+ $reason = (object) array(
2013
+ 'id' => $reason_id,
2014
+ 'info' => $reason_info,
2015
+ 'is_anonymous' => fs_request_get_bool( 'is_anonymous' )
2016
+ );
2017
+
2018
+ $this->_storage->store( 'uninstall_reason', $reason );
2019
+
2020
+ /**
2021
+ * If the module type is "theme", trigger the uninstall event here (on theme deactivation) since themes do
2022
+ * not support uninstall hook.
2023
+ *
2024
+ * @author Leo Fajardo (@leorw)
2025
+ * @since 1.2.2
2026
+ */
2027
+ if ( $this->is_theme() ) {
2028
+ $this->_uninstall_plugin_event( false );
2029
+ $this->remove_sdk_reference();
2030
+ }
2031
+
2032
+ // Print '1' for successful operation.
2033
+ echo 1;
2034
+ exit;
2035
+ }
2036
+
2037
+ #endregion
2038
+
2039
+ #----------------------------------------------------------------------------------
2040
+ #region Instance
2041
+ #----------------------------------------------------------------------------------
2042
+
2043
+ /**
2044
+ * Main singleton instance.
2045
+ *
2046
+ * @author Vova Feldman (@svovaf)
2047
+ * @since 1.0.0
2048
+ *
2049
+ * @param number $module_id
2050
+ * @param string|bool $slug
2051
+ * @param bool $is_init Is initiation sequence.
2052
+ *
2053
+ * @return Freemius|false
2054
+ */
2055
+ static function instance( $module_id, $slug = false, $is_init = false ) {
2056
+ if ( empty( $module_id ) ) {
2057
+ return false;
2058
+ }
2059
+
2060
+ /**
2061
+ * 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.
2062
+ */
2063
+ self::_load_required_static();
2064
+
2065
+ if ( ! is_numeric( $module_id ) ) {
2066
+ if ( ! $is_init && true === $slug ) {
2067
+ $is_init = true;
2068
+ }
2069
+
2070
+ $slug = $module_id;
2071
+
2072
+ $module = FS_Plugin_Manager::instance( $slug )->get();
2073
+
2074
+ if ( is_object( $module ) ) {
2075
+ $module_id = $module->id;
2076
+ }
2077
+ }
2078
+
2079
+ $key = 'm_' . $module_id;
2080
+
2081
+ if ( ! isset( self::$_instances[ $key ] ) ) {
2082
+ self::$_instances[ $key ] = new Freemius( $module_id, $slug, $is_init );
2083
+ }
2084
+
2085
+ return self::$_instances[ $key ];
2086
+ }
2087
+
2088
+ /**
2089
+ * @author Vova Feldman (@svovaf)
2090
+ * @since 1.0.6
2091
+ *
2092
+ * @param number $addon_id
2093
+ *
2094
+ * @return bool
2095
+ */
2096
+ private static function has_instance( $addon_id ) {
2097
+ return isset( self::$_instances[ 'm_' . $addon_id ] );
2098
+ }
2099
+
2100
+ /**
2101
+ * @author Leo Fajardo (@leorw)
2102
+ * @since 1.2.2
2103
+ *
2104
+ * @param string|number $id_or_slug
2105
+ *
2106
+ * @return number|false
2107
+ */
2108
+ private static function get_module_id( $id_or_slug ) {
2109
+ if ( is_numeric( $id_or_slug ) ) {
2110
+ return $id_or_slug;
2111
+ }
2112
+
2113
+ foreach ( self::$_instances as $instance ) {
2114
+ if ( $instance->is_plugin() && ( $id_or_slug === $instance->get_slug() ) ) {
2115
+ return $instance->get_id();
2116
+ }
2117
+ }
2118
+
2119
+ return false;
2120
+ }
2121
+
2122
+ /**
2123
+ * @author Vova Feldman (@svovaf)
2124
+ * @since 1.0.6
2125
+ *
2126
+ * @param number $id
2127
+ *
2128
+ * @return false|Freemius
2129
+ */
2130
+ static function get_instance_by_id( $id ) {
2131
+ return isset ( self::$_instances[ 'm_' . $id ] ) ?
2132
+ self::$_instances[ 'm_' . $id ] :
2133
+ false;
2134
+ }
2135
+
2136
+ /**
2137
+ *
2138
+ * @author Vova Feldman (@svovaf)
2139
+ * @since 1.0.1
2140
+ *
2141
+ * @param $plugin_file
2142
+ *
2143
+ * @return false|Freemius
2144
+ */
2145
+ static function get_instance_by_file( $plugin_file ) {
2146
+ $slug = self::find_slug_by_basename( $plugin_file );
2147
+
2148
+ return ( false !== $slug ) ?
2149
+ self::instance( self::get_module_id( $slug ) ) :
2150
+ false;
2151
+ }
2152
+
2153
+ /**
2154
+ * @author Vova Feldman (@svovaf)
2155
+ * @since 1.0.6
2156
+ *
2157
+ * @return false|Freemius
2158
+ */
2159
+ function get_parent_instance() {
2160
+ return self::get_instance_by_id( $this->_plugin->parent_plugin_id );
2161
+ }
2162
+
2163
+ /**
2164
+ * @author Vova Feldman (@svovaf)
2165
+ * @since 1.0.6
2166
+ *
2167
+ * @param string|number $id_or_slug
2168
+ *
2169
+ * @return false|Freemius
2170
+ */
2171
+ function get_addon_instance( $id_or_slug ) {
2172
+ $addon_id = self::get_module_id( $id_or_slug );
2173
+
2174
+ return self::instance( $addon_id );
2175
+ }
2176
+
2177
+ #endregion ------------------------------------------------------------------
2178
+
2179
+ /**
2180
+ * @author Vova Feldman (@svovaf)
2181
+ * @since 1.0.6
2182
+ *
2183
+ * @return bool
2184
+ */
2185
+ function is_parent_plugin_installed() {
2186
+ $is_active = self::has_instance( $this->_plugin->parent_plugin_id );
2187
+
2188
+ if ( $is_active ) {
2189
+ return true;
2190
+ }
2191
+
2192
+ /**
2193
+ * Parent module might be a theme. If that's the case, the add-on's FS
2194
+ * instance will be loaded prior to the theme's FS instance, therefore,
2195
+ * we need to check if it's active with a "look ahead".
2196
+ *
2197
+ * @author Vova Feldman
2198
+ * @since 1.2.2.3
2199
+ */
2200
+ global $fs_active_plugins;
2201
+ if ( is_object( $fs_active_plugins ) && is_array( $fs_active_plugins->plugins ) ) {
2202
+ $active_theme = wp_get_theme();
2203
+
2204
+ foreach ( $fs_active_plugins->plugins as $sdk => $module ) {
2205
+ if ( WP_FS__MODULE_TYPE_THEME === $module->type ) {
2206
+ if ( $module->plugin_path == $active_theme->get_stylesheet() ) {
2207
+ // Parent module is a theme and it's currently active.
2208
+ return true;
2209
+ }
2210
+ }
2211
+ }
2212
+ }
2213
+
2214
+ return false;
2215
+ }
2216
+
2217
+ /**
2218
+ * Check if add-on parent plugin in activation mode.
2219
+ *
2220
+ * @author Vova Feldman (@svovaf)
2221
+ * @since 1.0.7
2222
+ *
2223
+ * @return bool
2224
+ */
2225
+ function is_parent_in_activation() {
2226
+ $parent_fs = $this->get_parent_instance();
2227
+ if ( ! is_object( $parent_fs ) ) {
2228
+ return false;
2229
+ }
2230
+
2231
+ return ( $parent_fs->is_activation_mode() );
2232
+ }
2233
+
2234
+ /**
2235
+ * Is plugin in activation mode.
2236
+ *
2237
+ * @author Vova Feldman (@svovaf)
2238
+ * @since 1.0.7
2239
+ *
2240
+ * @param bool $and_on
2241
+ *
2242
+ * @return bool
2243
+ */
2244
+ function is_activation_mode( $and_on = true ) {
2245
+ return fs_is_network_admin() ?
2246
+ $this->is_network_activation_mode( $and_on ) :
2247
+ $this->is_site_activation_mode( $and_on );
2248
+ }
2249
+
2250
+ /**
2251
+ * Is plugin in activation mode.
2252
+ *
2253
+ * @author Vova Feldman (@svovaf)
2254
+ * @since 1.0.7
2255
+ *
2256
+ * @param bool $and_on
2257
+ *
2258
+ * @return bool
2259
+ */
2260
+ function is_site_activation_mode( $and_on = true ) {
2261
+ return (
2262
+ ( $this->is_on() || ! $and_on ) &&
2263
+ ( ! $this->is_registered() ||
2264
+ ( $this->is_only_premium() && ! $this->has_features_enabled_license() ) ) &&
2265
+ ( ! $this->is_enable_anonymous() ||
2266
+ ( ! $this->is_anonymous() && ! $this->is_pending_activation() ) )
2267
+ );
2268
+ }
2269
+
2270
+ /**
2271
+ * Checks if the SDK in network activation mode.
2272
+ *
2273
+ * @author Leo Fajardo (@leorw)
2274
+ * @since 2.0.0
2275
+ *
2276
+ * @param bool $and_on
2277
+ *
2278
+ * @return bool
2279
+ */
2280
+ private function is_network_activation_mode( $and_on = true ) {
2281
+ if ( ! $this->_is_network_active ) {
2282
+ // Not network activated.
2283
+ return false;
2284
+ }
2285
+
2286
+ if ( $this->is_network_upgrade_mode() ) {
2287
+ // Special flag to enforce network activation mode to decide what to do with the sites that are not yet opted-in nor skipped.
2288
+ return true;
2289
+ }
2290
+
2291
+ if ( ! $this->is_site_activation_mode( $and_on ) ) {
2292
+ // 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.
2293
+ return false;
2294
+ }
2295
+
2296
+ if ( $this->is_network_delegated_connection() ) {
2297
+ // Super-admin delegated the connection to the site admins -> not activation mode.
2298
+ return false;
2299
+ }
2300
+
2301
+ if ( $this->is_network_anonymous() ) {
2302
+ // Super-admin skipped the connection network wide -> not activation mode.
2303
+ return false;
2304
+ }
2305
+
2306
+ if ( $this->is_network_registered() ) {
2307
+ // Super-admin connected at least one site -> not activation mode.
2308
+ return false;
2309
+ }
2310
+
2311
+ return true;
2312
+ }
2313
+
2314
+ /**
2315
+ * Check if current page is the opt-in/pending-activation page.
2316
+ *
2317
+ * @author Vova Feldman (@svovaf)
2318
+ * @since 1.2.1.7
2319
+ *
2320
+ * @return bool
2321
+ */
2322
+ function is_activation_page() {
2323
+ if ( $this->_menu->is_main_settings_page() ) {
2324
+ return true;
2325
+ }
2326
+
2327
+ if ( ! $this->is_activation_mode() ) {
2328
+ return false;
2329
+ }
2330
+
2331
+ // Check if current page is matching the activation page.
2332
+ return $this->is_matching_url( $this->get_activation_url() );
2333
+ }
2334
+
2335
+ /**
2336
+ * Check if URL path's are matching and that all querystring
2337
+ * arguments of the $sub_url exist in the $url with the same values.
2338
+ *
2339
+ * WARNING:
2340
+ * 1. This method doesn't check if the sub/domain are matching.
2341
+ * 2. Ignore case sensitivity.
2342
+ *
2343
+ * @author Vova Feldman (@svovaf)
2344
+ * @since 1.2.1.7
2345
+ *
2346
+ * @param string $sub_url
2347
+ * @param string $url If argument is not set, check if the sub_url matching the current's page URL.
2348
+ *
2349
+ * @return bool
2350
+ */
2351
+ private function is_matching_url( $sub_url, $url = '' ) {
2352
+ if ( empty( $url ) ) {
2353
+ $url = $_SERVER['REQUEST_URI'];
2354
+ }
2355
+
2356
+ $url = strtolower( $url );
2357
+ $sub_url = strtolower( $sub_url );
2358
+
2359
+ if ( parse_url( $sub_url, PHP_URL_PATH ) !== parse_url( $url, PHP_URL_PATH ) ) {
2360
+ // Different path - DO NOT OVERRIDE PAGE.
2361
+ return false;
2362
+ }
2363
+
2364
+ $url_params = array();
2365
+ parse_str( parse_url( $url, PHP_URL_QUERY ), $url_params );
2366
+
2367
+ $sub_url_params = array();
2368
+ parse_str( parse_url( $sub_url, PHP_URL_QUERY ), $sub_url_params );
2369
+
2370
+ foreach ( $sub_url_params as $key => $val ) {
2371
+ if ( ! isset( $url_params[ $key ] ) || $val != $url_params[ $key ] ) {
2372
+ // Not matching query string - DO NOT OVERRIDE PAGE.
2373
+ return false;
2374
+ }
2375
+ }
2376
+
2377
+ return true;
2378
+ }
2379
+
2380
+ /**
2381
+ * Get the basenames of all active plugins for specific blog. Including network activated plugins.
2382
+ *
2383
+ * @author Vova Feldman (@svovaf)
2384
+ * @since 2.0.0
2385
+ *
2386
+ * @param int $blog_id
2387
+ *
2388
+ * @return string[]
2389
+ */
2390
+ private static function get_active_plugins_basenames( $blog_id = 0 ) {
2391
+ if ( is_multisite() && $blog_id > 0 ) {
2392
+ $active_basenames = get_blog_option( $blog_id, 'active_plugins' );
2393
+ } else {
2394
+ $active_basenames = get_option( 'active_plugins' );
2395
+ }
2396
+
2397
+ if ( is_multisite() ) {
2398
+ $network_active_basenames = get_site_option( 'active_sitewide_plugins' );
2399
+
2400
+ if ( is_array( $network_active_basenames ) && ! empty( $network_active_basenames ) ) {
2401
+ $active_basenames = array_merge( $active_basenames, $network_active_basenames );
2402
+ }
2403
+ }
2404
+
2405
+ return $active_basenames;
2406
+ }
2407
+
2408
+ /**
2409
+ * Get collection of all active plugins. Including network activated plugins.
2410
+ *
2411
+ * @author Vova Feldman (@svovaf)
2412
+ * @since 1.0.9
2413
+ *
2414
+ * @param int $blog_id Since 2.0.0
2415
+ *
2416
+ * @return array[string]array
2417
+ */
2418
+ private static function get_active_plugins( $blog_id = 0 ) {
2419
+ self::require_plugin_essentials();
2420
+
2421
+ $active_plugin = array();
2422
+ $all_plugins = get_plugins();
2423
+ $active_plugins_basenames = self::get_active_plugins_basenames( $blog_id );
2424
+
2425
+ foreach ( $active_plugins_basenames as $plugin_basename ) {
2426
+ $active_plugin[ $plugin_basename ] = $all_plugins[ $plugin_basename ];
2427
+ }
2428
+
2429
+ return $active_plugin;
2430
+ }
2431
+
2432
+ /**
2433
+ * Get collection of all site active plugins for a specified blog.
2434
+ *
2435
+ * @author Vova Feldman (@svovaf)
2436
+ * @since 2.0.0
2437
+ *
2438
+ * @param int $blog_id
2439
+ *
2440
+ * @return array[string]array
2441
+ */
2442
+ private static function get_site_active_plugins( $blog_id = 0 ) {
2443
+ $active_basenames = ( is_multisite() && $blog_id > 0 ) ?
2444
+ get_blog_option( $blog_id, 'active_plugins' ) :
2445
+ get_option( 'active_plugins' );
2446
+
2447
+ $active = array();
2448
+ foreach ( $active_basenames as $basename ) {
2449
+ $active[ $basename ] = array(
2450
+ 'is_active' => true,
2451
+ 'Version' => '1.0', // Dummy version.
2452
+ 'slug' => self::get_plugin_slug( $basename ),
2453
+ );
2454
+ }
2455
+
2456
+ return $active;
2457
+ }
2458
+
2459
+ /**
2460
+ * Get collection of all plugins with their activation status for a specified blog.
2461
+ *
2462
+ * @author Vova Feldman (@svovaf)
2463
+ * @since 1.1.8
2464
+ *
2465
+ * @param int $blog_id Since 2.0.0
2466
+ *
2467
+ * @return array Key is the plugin file path and the value is an array of the plugin data.
2468
+ */
2469
+ private static function get_all_plugins( $blog_id = 0 ) {
2470
+ self::require_plugin_essentials();
2471
+
2472
+ $all_plugins = get_plugins();
2473
+
2474
+ $active_plugins_basenames = self::get_active_plugins_basenames( $blog_id );
2475
+
2476
+ foreach ( $all_plugins as $basename => &$data ) {
2477
+ // By default set to inactive (next foreach update the active plugins).
2478
+ $data['is_active'] = false;
2479
+ // Enrich with plugin slug.
2480
+ $data['slug'] = self::get_plugin_slug( $basename );
2481
+ }
2482
+
2483
+ // Flag active plugins.
2484
+ foreach ( $active_plugins_basenames as $basename ) {
2485
+ if ( isset( $all_plugins[ $basename ] ) ) {
2486
+ $all_plugins[ $basename ]['is_active'] = true;
2487
+ }
2488
+ }
2489
+
2490
+ return $all_plugins;
2491
+ }
2492
+
2493
+ /**
2494
+ * Get collection of all plugins and if they are network level activated.
2495
+ *
2496
+ * @author Vova Feldman (@svovaf)
2497
+ * @since 2.0.0
2498
+ *
2499
+ * @return array Key is the plugin basename and the value is an array of the plugin data.
2500
+ */
2501
+ private static function get_network_plugins() {
2502
+ self::require_plugin_essentials();
2503
+
2504
+ $all_plugins = get_plugins();
2505
+
2506
+ $network_active_basenames = is_multisite() ?
2507
+ get_site_option( 'active_sitewide_plugins' ) :
2508
+ array();
2509
+
2510
+ foreach ( $all_plugins as $basename => &$data ) {
2511
+ // By default set to inactive (next foreach update the active plugins).
2512
+ $data['is_active'] = false;
2513
+ // Enrich with plugin slug.
2514
+ $data['slug'] = self::get_plugin_slug( $basename );
2515
+ }
2516
+
2517
+ // Flag active plugins.
2518
+ foreach ( $network_active_basenames as $basename ) {
2519
+ if ( isset( $all_plugins[ $basename ] ) ) {
2520
+ $all_plugins[ $basename ]['is_active'] = true;
2521
+ }
2522
+ }
2523
+
2524
+ return $all_plugins;
2525
+ }
2526
+
2527
+ /**
2528
+ * Cached result of get_site_transient( 'update_plugins' )
2529
+ *
2530
+ * @author Vova Feldman (@svovaf)
2531
+ * @since 1.1.8
2532
+ *
2533
+ * @var object
2534
+ */
2535
+ private static $_plugins_info;
2536
+
2537
+ /**
2538
+ * Helper function to get specified plugin's slug.
2539
+ *
2540
+ * @author Vova Feldman (@svovaf)
2541
+ * @since 1.1.8
2542
+ *
2543
+ * @param $basename
2544
+ *
2545
+ * @return string
2546
+ */
2547
+ private static function get_plugin_slug( $basename ) {
2548
+ if ( ! isset( self::$_plugins_info ) ) {
2549
+ self::$_plugins_info = get_site_transient( 'update_plugins' );
2550
+ }
2551
+
2552
+ $slug = '';
2553
+
2554
+ if ( is_object( self::$_plugins_info ) ) {
2555
+ if ( isset( self::$_plugins_info->no_update ) &&
2556
+ isset( self::$_plugins_info->no_update[ $basename ] ) &&
2557
+ ! empty( self::$_plugins_info->no_update[ $basename ]->slug )
2558
+ ) {
2559
+ $slug = self::$_plugins_info->no_update[ $basename ]->slug;
2560
+ } else if ( isset( self::$_plugins_info->response ) &&
2561
+ isset( self::$_plugins_info->response[ $basename ] ) &&
2562
+ ! empty( self::$_plugins_info->response[ $basename ]->slug )
2563
+ ) {
2564
+ $slug = self::$_plugins_info->response[ $basename ]->slug;
2565
+ }
2566
+ }
2567
+
2568
+ if ( empty( $slug ) ) {
2569
+ // Try to find slug from FS data.
2570
+ $slug = self::find_slug_by_basename( $basename );
2571
+ }
2572
+
2573
+ if ( empty( $slug ) ) {
2574
+ // Fallback to plugin's folder name.
2575
+ $slug = dirname( $basename );
2576
+ }
2577
+
2578
+ return $slug;
2579
+ }
2580
+
2581
+ private static $_statics_loaded = false;
2582
+
2583
+ /**
2584
+ * Load static resources.
2585
+ *
2586
+ * @author Vova Feldman (@svovaf)
2587
+ * @since 1.0.1
2588
+ */
2589
+ private static function _load_required_static() {
2590
+ if ( self::$_statics_loaded ) {
2591
+ return;
2592
+ }
2593
+
2594
+ self::$_static_logger = FS_Logger::get_logger( WP_FS__SLUG, WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
2595
+
2596
+ self::$_static_logger->entrance();
2597
+
2598
+ self::$_accounts = FS_Options::instance( WP_FS__ACCOUNTS_OPTION_NAME, true );
2599
+
2600
+ if ( is_multisite() ) {
2601
+ /**
2602
+ * If the id_slug_type_path_map exists on the site level but doesn't exist on the
2603
+ * network level storage, it means that we need to process the storage with migration.
2604
+ *
2605
+ * 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, id_slug_type_path_map will be already set in the network level storage.
2606
+ *
2607
+ * @author Vova Feldman (@svovaf)
2608
+ * @since 2.0.0
2609
+ */
2610
+ if ( null === self::$_accounts->get_option( 'id_slug_type_path_map', null, true ) &&
2611
+ null !== self::$_accounts->get_option( 'id_slug_type_path_map', null, false )
2612
+ ) {
2613
+ self::migrate_accounts_to_network();
2614
+
2615
+ // Migrate API options from site level to network level.
2616
+ $api_network_options = FS_Option_Manager::get_manager( WP_FS__OPTIONS_OPTION_NAME, true, true );
2617
+ $api_network_options->migrate_to_network();
2618
+
2619
+ // Migrate API cache to network level storage.
2620
+ FS_Cache_Manager::get_manager( WP_FS__API_CACHE_OPTION_NAME )->migrate_to_network();
2621
+ }
2622
+ }
2623
+
2624
+ self::$_global_admin_notices = FS_Admin_Notices::instance( 'global' );
2625
+
2626
+ add_action( ( fs_is_network_admin() ? 'network_' : '' ) . 'admin_menu', array(
2627
+ 'Freemius',
2628
+ '_add_debug_section'
2629
+ ) );
2630
+
2631
+ add_action( "wp_ajax_fs_toggle_debug_mode", array( 'Freemius', '_toggle_debug_mode' ) );
2632
+
2633
+ self::add_ajax_action_static( 'get_debug_log', array( 'Freemius', '_get_debug_log' ) );
2634
+
2635
+ self::add_ajax_action_static( 'get_db_option', array( 'Freemius', '_get_db_option' ) );
2636
+
2637
+ self::add_ajax_action_static( 'set_db_option', array( 'Freemius', '_set_db_option' ) );
2638
+
2639
+ if ( 0 == did_action( 'plugins_loaded' ) ) {
2640
+ add_action( 'plugins_loaded', array( 'Freemius', '_load_textdomain' ), 1 );
2641
+ }
2642
+
2643
+ add_action( 'admin_footer', array( 'Freemius', '_enrich_ajax_url' ) );
2644
+
2645
+ self::$_statics_loaded = true;
2646
+ }
2647
+
2648
+ #----------------------------------------------------------------------------------
2649
+ #region Localization
2650
+ #----------------------------------------------------------------------------------
2651
+
2652
+ /**
2653
+ * Load framework's text domain.
2654
+ *
2655
+ * @author Vova Feldman (@svovaf)
2656
+ * @since 1.2.1
2657
+ */
2658
+ static function _load_textdomain() {
2659
+ if ( ! is_admin() ) {
2660
+ return;
2661
+ }
2662
+
2663
+ global $fs_active_plugins;
2664
+
2665
+ // Works both for plugins and themes.
2666
+ load_plugin_textdomain(
2667
+ 'freemius',
2668
+ false,
2669
+ $fs_active_plugins->newest->sdk_path . '/languages/'
2670
+ );
2671
+ }
2672
+
2673
+ #endregion
2674
+
2675
+ #----------------------------------------------------------------------------------
2676
+ #region Debugging
2677
+ #----------------------------------------------------------------------------------
2678
+
2679
+ /**
2680
+ * @author Vova Feldman (@svovaf)
2681
+ * @since 1.0.8
2682
+ */
2683
+ static function _add_debug_section() {
2684
+ if ( ! is_super_admin() ) {
2685
+ // Add debug page only for super-admins.
2686
+ return;
2687
+ }
2688
+
2689
+ self::$_static_logger->entrance();
2690
+
2691
+ $title = sprintf( '%s [v.%s]', fs_text_inline( 'Freemius Debug' ), WP_FS__SDK_VERSION );
2692
+
2693
+ if ( WP_FS__DEV_MODE ) {
2694
+ // Add top-level debug menu item.
2695
+ $hook = FS_Admin_Menu_Manager::add_page(
2696
+ $title,
2697
+ $title,
2698
+ 'manage_options',
2699
+ 'freemius',
2700
+ array( 'Freemius', '_debug_page_render' )
2701
+ );
2702
+ } else {
2703
+ // Add hidden debug page.
2704
+ $hook = FS_Admin_Menu_Manager::add_subpage(
2705
+ null,
2706
+ $title,
2707
+ $title,
2708
+ 'manage_options',
2709
+ 'freemius',
2710
+ array( 'Freemius', '_debug_page_render' )
2711
+ );
2712
+ }
2713
+
2714
+ if ( ! empty( $hook ) ) {
2715
+ add_action( "load-$hook", array( 'Freemius', '_debug_page_actions' ) );
2716
+ }
2717
+ }
2718
+
2719
+ /**
2720
+ * @author Vova Feldman (@svovaf)
2721
+ * @since 1.1.7.3
2722
+ */
2723
+ static function _toggle_debug_mode() {
2724
+ $is_on = fs_request_get( 'is_on', false, 'post' );
2725
+
2726
+ if ( fs_request_is_post() && in_array( $is_on, array( 0, 1 ) ) ) {
2727
+ update_option( 'fs_debug_mode', $is_on );
2728
+
2729
+ // Turn on/off storage logging.
2730
+ FS_Logger::_set_storage_logging( ( 1 == $is_on ) );
2731
+ }
2732
+
2733
+ exit;
2734
+ }
2735
+
2736
+ /**
2737
+ * @author Vova Feldman (@svovaf)
2738
+ * @since 1.2.1.6
2739
+ */
2740
+ static function _get_debug_log() {
2741
+ $logs = FS_Logger::load_db_logs(
2742
+ fs_request_get( 'filters', false, 'post' ),
2743
+ ! empty( $_POST['limit'] ) && is_numeric( $_POST['limit'] ) ? $_POST['limit'] : 200,
2744
+ ! empty( $_POST['offset'] ) && is_numeric( $_POST['offset'] ) ? $_POST['offset'] : 0
2745
+ );
2746
+
2747
+ self::shoot_ajax_success( $logs );
2748
+ }
2749
+
2750
+ /**
2751
+ * @author Vova Feldman (@svovaf)
2752
+ * @since 1.2.1.7
2753
+ */
2754
+ static function _get_db_option() {
2755
+ $option_name = fs_request_get( 'option_name' );
2756
+
2757
+ $value = get_option( $option_name );
2758
+
2759
+ $result = array(
2760
+ 'name' => $option_name,
2761
+ );
2762
+
2763
+ if ( false !== $value ) {
2764
+ if ( ! is_string( $value ) ) {
2765
+ $value = json_encode( $value );
2766
+ }
2767
+
2768
+ $result['value'] = $value;
2769
+ }
2770
+
2771
+ self::shoot_ajax_success( $result );
2772
+ }
2773
+
2774
+ /**
2775
+ * @author Vova Feldman (@svovaf)
2776
+ * @since 1.2.1.7
2777
+ */
2778
+ static function _set_db_option() {
2779
+ $option_name = fs_request_get( 'option_name' );
2780
+ $option_value = fs_request_get( 'option_value' );
2781
+
2782
+ if ( ! empty( $option_value ) ) {
2783
+ update_option( $option_name, $option_value );
2784
+ }
2785
+
2786
+ self::shoot_ajax_success();
2787
+ }
2788
+
2789
+ /**
2790
+ * @author Vova Feldman (@svovaf)
2791
+ * @since 1.0.8
2792
+ */
2793
+ static function _debug_page_actions() {
2794
+ self::_clean_admin_content_section();
2795
+
2796
+ if ( fs_request_is_action( 'restart_freemius' ) ) {
2797
+ check_admin_referer( 'restart_freemius' );
2798
+
2799
+ if ( ! is_multisite() ) {
2800
+ // Clear accounts data.
2801
+ self::$_accounts->clear( null, true );
2802
+ } else {
2803
+ $sites = self::get_sites();
2804
+ foreach ( $sites as $site ) {
2805
+ $blog_id = self::get_site_blog_id( $site );
2806
+ self::$_accounts->clear( $blog_id, true );
2807
+ }
2808
+
2809
+ // Clear network level storage.
2810
+ self::$_accounts->clear( true, true );
2811
+ }
2812
+
2813
+ // Clear SDK reference cache.
2814
+ delete_option( 'fs_active_plugins' );
2815
+ } else if ( fs_request_is_action( 'clear_updates_data' ) ) {
2816
+ check_admin_referer( 'clear_updates_data' );
2817
+
2818
+ if ( ! is_multisite() ) {
2819
+ set_site_transient( 'update_plugins', null );
2820
+ set_site_transient( 'update_themes', null );
2821
+ } else {
2822
+ $current_blog_id = get_current_blog_id();
2823
+
2824
+ $sites = self::get_sites();
2825
+ foreach ( $sites as $site ) {
2826
+ switch_to_blog( self::get_site_blog_id( $site ) );
2827
+
2828
+ set_site_transient( 'update_plugins', null );
2829
+ set_site_transient( 'update_themes', null );
2830
+ }
2831
+
2832
+ switch_to_blog( $current_blog_id );
2833
+ }
2834
+ } else if ( fs_request_is_action( 'simulate_trial' ) ) {
2835
+ check_admin_referer( 'simulate_trial' );
2836
+
2837
+ $fs = freemius( fs_request_get( 'module_id' ) );
2838
+
2839
+ // Update SDK install to at least 24 hours before.
2840
+ $fs->_storage->install_timestamp = ( time() - WP_FS__TIME_24_HOURS_IN_SEC );
2841
+ // Unset the trial shown timestamp.
2842
+ unset( $fs->_storage->trial_promotion_shown );
2843
+ } else if ( fs_request_is_action( 'simulate_network_upgrade' ) ) {
2844
+ check_admin_referer( 'simulate_network_upgrade' );
2845
+
2846
+ $fs = freemius( fs_request_get( 'module_id' ) );
2847
+
2848
+ self::set_network_upgrade_mode( $fs->_storage );
2849
+ } else if ( fs_request_is_action( 'delete_install' ) ) {
2850
+ check_admin_referer( 'delete_install' );
2851
+
2852
+ self::_delete_site_by_slug(
2853
+ fs_request_get( 'slug' ),
2854
+ fs_request_get( 'module_type' ),
2855
+ true,
2856
+ fs_request_get( 'blog_id', null )
2857
+ );
2858
+ } else if ( fs_request_is_action( 'delete_user' ) ) {
2859
+ check_admin_referer( 'delete_user' );
2860
+
2861
+ self::delete_user( fs_request_get( 'user_id' ) );
2862
+ } else if ( fs_request_is_action( 'download_logs' ) ) {
2863
+ check_admin_referer( 'download_logs' );
2864
+
2865
+ $download_url = FS_Logger::download_db_logs(
2866
+ fs_request_get( 'filters', false, 'post' )
2867
+ );
2868
+
2869
+ if ( false === $download_url ) {
2870
+ 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.' );
2871
+ }
2872
+
2873
+ fs_redirect( $download_url );
2874
+ }
2875
+ }
2876
+
2877
+ /**
2878
+ * @author Vova Feldman (@svovaf)
2879
+ * @since 1.0.8
2880
+ */
2881
+ static function _debug_page_render() {
2882
+ self::$_static_logger->entrance();
2883
+
2884
+ if ( ! is_multisite() ) {
2885
+ $all_plugins_installs = self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN );
2886
+ $all_themes_installs = self::get_all_sites( WP_FS__MODULE_TYPE_THEME );
2887
+ } else {
2888
+ $sites = self::get_sites();
2889
+
2890
+ $all_plugins_installs = array();
2891
+ $all_themes_installs = array();
2892
+
2893
+ foreach ( $sites as $site ) {
2894
+ $blog_id = self::get_site_blog_id( $site );
2895
+
2896
+ $plugins_installs = self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN, $blog_id );
2897
+
2898
+ foreach ( $plugins_installs as $slug => $install ) {
2899
+ if ( ! isset( $all_plugins_installs[ $slug ] ) ) {
2900
+ $all_plugins_installs[ $slug ] = array();
2901
+ }
2902
+
2903
+ $install->blog_id = $blog_id;
2904
+
2905
+ $all_plugins_installs[ $slug ][] = $install;
2906
+ }
2907
+
2908
+ $themes_installs = self::get_all_sites( WP_FS__MODULE_TYPE_THEME, $blog_id );
2909
+
2910
+ foreach ( $themes_installs as $slug => $install ) {
2911
+ if ( ! isset( $all_themes_installs[ $slug ] ) ) {
2912
+ $all_themes_installs[ $slug ] = array();
2913
+ }
2914
+
2915
+ $install->blog_id = $blog_id;
2916
+
2917
+ $all_themes_installs[ $slug ][] = $install;
2918
+ }
2919
+ }
2920
+ }
2921
+
2922
+ $vars = array(
2923
+ 'plugin_sites' => $all_plugins_installs,
2924
+ 'theme_sites' => $all_themes_installs,
2925
+ 'users' => self::get_all_users(),
2926
+ 'addons' => self::get_all_addons(),
2927
+ 'account_addons' => self::get_all_account_addons(),
2928
+ 'plugin_licenses' => self::get_all_licenses( WP_FS__MODULE_TYPE_PLUGIN ),
2929
+ 'theme_licenses' => self::get_all_licenses( WP_FS__MODULE_TYPE_THEME )
2930
+ );
2931
+
2932
+ fs_enqueue_local_style( 'fs_debug', '/admin/debug.css' );
2933
+ fs_require_once_template( 'debug.php', $vars );
2934
+ }
2935
+
2936
+ #endregion
2937
+
2938
+ #----------------------------------------------------------------------------------
2939
+ #region Connectivity Issues
2940
+ #----------------------------------------------------------------------------------
2941
+
2942
+ /**
2943
+ * Check if Freemius should be turned on for the current plugin install.
2944
+ *
2945
+ * Note:
2946
+ * $this->_is_on is updated in has_api_connectivity()
2947
+ *
2948
+ * @author Vova Feldman (@svovaf)
2949
+ * @since 1.0.9
2950
+ *
2951
+ * @return bool
2952
+ */
2953
+ function is_on() {
2954
+ self::$_static_logger->entrance();
2955
+
2956
+ if ( isset( $this->_is_on ) ) {
2957
+ return $this->_is_on;
2958
+ }
2959
+
2960
+ // If already installed or pending then sure it's on :)
2961
+ if ( $this->is_registered() || $this->is_pending_activation() ) {
2962
+ $this->_is_on = true;
2963
+
2964
+ return true;
2965
+ }
2966
+
2967
+ return false;
2968
+ }
2969
+
2970
+ /**
2971
+ * @author Vova Feldman (@svovaf)
2972
+ * @since 1.1.7.3
2973
+ *
2974
+ * @param bool $flush_if_no_connectivity
2975
+ *
2976
+ * @return bool
2977
+ */
2978
+ private function should_run_connectivity_test( $flush_if_no_connectivity = false ) {
2979
+ if ( ! isset( $this->_storage->connectivity_test ) ) {
2980
+ // Connectivity test was never executed, or cache was cleared.
2981
+ return true;
2982
+ }
2983
+
2984
+ if ( WP_FS__PING_API_ON_IP_OR_HOST_CHANGES ) {
2985
+ if ( WP_FS__IS_HTTP_REQUEST ) {
2986
+ if ( $_SERVER['HTTP_HOST'] != $this->_storage->connectivity_test['host'] ) {
2987
+ // Domain changed.
2988
+ return true;
2989
+ }
2990
+
2991
+ if ( WP_FS__REMOTE_ADDR != $this->_storage->connectivity_test['server_ip'] ) {
2992
+ // Server IP changed.
2993
+ return true;
2994
+ }
2995
+ }
2996
+ }
2997
+
2998
+ if ( $this->_storage->connectivity_test['is_connected'] &&
2999
+ $this->_storage->connectivity_test['is_active']
3000
+ ) {
3001
+ // API connected and Freemius is active - no need to run connectivity check.
3002
+ return false;
3003
+ }
3004
+
3005
+ if ( $flush_if_no_connectivity ) {
3006
+ /**
3007
+ * If explicitly asked to flush when no connectivity - do it only
3008
+ * if at least 10 sec passed from the last API connectivity test.
3009
+ */
3010
+ return ( isset( $this->_storage->connectivity_test['timestamp'] ) &&
3011
+ ( WP_FS__SCRIPT_START_TIME - $this->_storage->connectivity_test['timestamp'] ) > 10 );
3012
+ }
3013
+
3014
+ /**
3015
+ * @since 1.1.7 Don't check for connectivity on plugin downgrade.
3016
+ */
3017
+ $version = $this->get_plugin_version();
3018
+ if ( version_compare( $version, $this->_storage->connectivity_test['version'], '>' ) ) {
3019
+ // If it's a plugin version upgrade and Freemius is off or no connectivity, run connectivity test.
3020
+ return true;
3021
+ }
3022
+
3023
+ return false;
3024
+ }
3025
+
3026
+ /**
3027
+ * @author Vova Feldman (@svovaf)
3028
+ * @since 1.1.7.4
3029
+ *
3030
+ * @param int|null $blog_id Since 2.0.0.
3031
+ *
3032
+ * @return object|false
3033
+ */
3034
+ private function ping( $blog_id = null ) {
3035
+ if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY ) {
3036
+ return false;
3037
+ }
3038
+
3039
+ $version = $this->get_plugin_version();
3040
+
3041
+ $is_update = $this->apply_filters( 'is_plugin_update', $this->is_plugin_update() );
3042
+
3043
+ return $this->get_api_plugin_scope()->ping(
3044
+ $this->get_anonymous_id( $blog_id ),
3045
+ array(
3046
+ 'is_update' => json_encode( $is_update ),
3047
+ 'version' => $version,
3048
+ 'sdk' => $this->version,
3049
+ 'is_admin' => json_encode( is_admin() ),
3050
+ 'is_ajax' => json_encode( self::is_ajax() ),
3051
+ 'is_cron' => json_encode( self::is_cron() ),
3052
+ 'is_http' => json_encode( WP_FS__IS_HTTP_REQUEST ),
3053
+ )
3054
+ );
3055
+ }
3056
+
3057
+ /**
3058
+ * Check if there's any connectivity issue to Freemius API.
3059
+ *
3060
+ * @author Vova Feldman (@svovaf)
3061
+ * @since 1.0.9
3062
+ *
3063
+ * @param bool $flush_if_no_connectivity
3064
+ *
3065
+ * @return bool
3066
+ */
3067
+ function has_api_connectivity( $flush_if_no_connectivity = false ) {
3068
+ $this->_logger->entrance();
3069
+
3070
+ if ( isset( $this->_has_api_connection ) && ( $this->_has_api_connection || ! $flush_if_no_connectivity ) ) {
3071
+ return $this->_has_api_connection;
3072
+ }
3073
+
3074
+ if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY &&
3075
+ isset( $this->_storage->connectivity_test ) &&
3076
+ true === $this->_storage->connectivity_test['is_connected']
3077
+ ) {
3078
+ unset( $this->_storage->connectivity_test );
3079
+ }
3080
+
3081
+ if ( ! $this->should_run_connectivity_test( $flush_if_no_connectivity ) ) {
3082
+ $this->_has_api_connection = $this->_storage->connectivity_test['is_connected'];
3083
+ /**
3084
+ * @since 1.1.6 During dev mode, if there's connectivity - turn Freemius on regardless the configuration.
3085
+ *
3086
+ * @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.
3087
+ */
3088
+ $this->_is_on = $this->_storage->connectivity_test['is_active'] ||
3089
+ $this->is_premium() ||
3090
+ ( WP_FS__DEV_MODE && $this->_has_api_connection && ! WP_FS__SIMULATE_FREEMIUS_OFF );
3091
+
3092
+ return $this->_has_api_connection;
3093
+ }
3094
+
3095
+ $pong = $this->ping();
3096
+ $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
3097
+
3098
+ if ( ! $is_connected ) {
3099
+ // API failure.
3100
+ $this->_add_connectivity_issue_message( $pong );
3101
+ }
3102
+
3103
+ $this->store_connectivity_info( $pong, $is_connected );
3104
+
3105
+ return $this->_has_api_connection;
3106
+ }
3107
+
3108
+ /**
3109
+ * @author Vova Feldman (@svovaf)
3110
+ * @since 1.1.7.4
3111
+ *
3112
+ * @param object $pong
3113
+ * @param bool $is_connected
3114
+ */
3115
+ private function store_connectivity_info( $pong, $is_connected ) {
3116
+ $this->_logger->entrance();
3117
+
3118
+ $version = $this->get_plugin_version();
3119
+
3120
+ if ( ! $is_connected || WP_FS__SIMULATE_FREEMIUS_OFF ) {
3121
+ $is_active = false;
3122
+ } else {
3123
+ $is_active = ( isset( $pong->is_active ) && true == $pong->is_active );
3124
+ }
3125
+
3126
+ $is_active = $this->apply_filters(
3127
+ 'is_on',
3128
+ $is_active,
3129
+ $this->is_plugin_update(),
3130
+ $version
3131
+ );
3132
+
3133
+ $this->_storage->connectivity_test = array(
3134
+ 'is_connected' => $is_connected,
3135
+ 'host' => $_SERVER['HTTP_HOST'],
3136
+ 'server_ip' => WP_FS__REMOTE_ADDR,
3137
+ 'is_active' => $is_active,
3138
+ 'timestamp' => WP_FS__SCRIPT_START_TIME,
3139
+ // Last version with connectivity attempt.
3140
+ 'version' => $version,
3141
+ );
3142
+
3143
+ $this->_has_api_connection = $is_connected;
3144
+ $this->_is_on = $is_active || ( WP_FS__DEV_MODE && $is_connected && ! WP_FS__SIMULATE_FREEMIUS_OFF );
3145
+ }
3146
+
3147
+ /**
3148
+ * Force turning Freemius on.
3149
+ *
3150
+ * @author Vova Feldman (@svovaf)
3151
+ * @since 1.1.8.1
3152
+ *
3153
+ * @return bool TRUE if successfully turned on.
3154
+ */
3155
+ private function turn_on() {
3156
+ $this->_logger->entrance();
3157
+
3158
+ if ( $this->is_on() || ! isset( $this->_storage->connectivity_test['is_active'] ) ) {
3159
+ return false;
3160
+ }
3161
+
3162
+ $updated_connectivity = $this->_storage->connectivity_test;
3163
+ $updated_connectivity['is_active'] = true;
3164
+ $updated_connectivity['timestamp'] = WP_FS__SCRIPT_START_TIME;
3165
+ $this->_storage->connectivity_test = $updated_connectivity;
3166
+
3167
+ $this->_is_on = true;
3168
+
3169
+ return true;
3170
+ }
3171
+
3172
+ /**
3173
+ * Anonymous and unique site identifier (Hash).
3174
+ *
3175
+ * @author Vova Feldman (@svovaf)
3176
+ * @since 1.1.0
3177
+ *
3178
+ * @param null|int $blog_id Since 2.0.0
3179
+ *
3180
+ * @return string
3181
+ */
3182
+ function get_anonymous_id( $blog_id = null ) {
3183
+ $unique_id = self::$_accounts->get_option( 'unique_id', null, $blog_id );
3184
+
3185
+ if ( empty( $unique_id ) || ! is_string( $unique_id ) ) {
3186
+ $key = fs_strip_url_protocol( get_site_url( $blog_id ) );
3187
+
3188
+ $secure_auth = SECURE_AUTH_KEY;
3189
+ if ( empty( $secure_auth ) || false !== strpos( $secure_auth, ' ' ) ) {
3190
+ // Protect against default auth key.
3191
+ $secure_auth = md5( microtime() );
3192
+ }
3193
+
3194
+ /**
3195
+ * Base the unique identifier on the WP secure authentication key. Which
3196
+ * turns the key into a secret anonymous identifier. This will help us
3197
+ * to avoid duplicate installs generation on the backend upon opt-in.
3198
+ *
3199
+ * @author Vova Feldman (@svovaf)
3200
+ * @since 1.2.3
3201
+ */
3202
+ $unique_id = md5( $key . $secure_auth );
3203
+
3204
+ self::$_accounts->set_option( 'unique_id', $unique_id, true, $blog_id );
3205
+ }
3206
+
3207
+ $this->_logger->departure( $unique_id );
3208
+
3209
+ return $unique_id;
3210
+ }
3211
+
3212
+ /**
3213
+ * @author Vova Feldman (@svovaf)
3214
+ * @since 1.1.7.4
3215
+ *
3216
+ * @return \WP_User
3217
+ */
3218
+ static function _get_current_wp_user() {
3219
+ self::require_pluggable_essentials();
3220
+
3221
+ return wp_get_current_user();
3222
+ }
3223
+
3224
+ /**
3225
+ * @author Vova Feldman (@svovaf)
3226
+ * @since 1.2.1.7
3227
+ *
3228
+ * @param string $email
3229
+ *
3230
+ * @return bool
3231
+ */
3232
+ static function is_valid_email( $email ) {
3233
+ if ( false === filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
3234
+ return false;
3235
+ }
3236
+
3237
+ $parts = explode( '@', $email );
3238
+
3239
+ if ( 2 !== count( $parts ) || empty( $parts[1] ) ) {
3240
+ return false;
3241
+ }
3242
+
3243
+ $blacklist = array(
3244
+ 'admin.',
3245
+ 'webmaster.',
3246
+ 'localhost.',
3247
+ 'dev.',
3248
+ 'development.',
3249
+ 'test.',
3250
+ 'stage.',
3251
+ 'staging.',
3252
+ );
3253
+
3254
+ // Make sure domain is not one of the blacklisted.
3255
+ foreach ( $blacklist as $invalid ) {
3256
+ if ( 0 === strpos( $parts[1], $invalid ) ) {
3257
+ return false;
3258
+ }
3259
+ }
3260
+
3261
+ // Get the UTF encoded domain name.
3262
+ $domain = idn_to_ascii( $parts[1] ) . '.';
3263
+
3264
+ return ( checkdnsrr( $domain, 'MX' ) || checkdnsrr( $domain, 'A' ) );
3265
+ }
3266
+
3267
+ /**
3268
+ * Generate API connectivity issue message.
3269
+ *
3270
+ * @author Vova Feldman (@svovaf)
3271
+ * @since 1.0.9
3272
+ *
3273
+ * @param mixed $api_result
3274
+ * @param bool $is_first_failure
3275
+ */
3276
+ function _add_connectivity_issue_message( $api_result, $is_first_failure = true ) {
3277
+ if ( ! $this->is_premium() && $this->_enable_anonymous ) {
3278
+ // Don't add message if it's the free version and can run anonymously.
3279
+ return;
3280
+ }
3281
+
3282
+ if ( ! function_exists( 'wp_nonce_url' ) ) {
3283
+ require_once ABSPATH . 'wp-includes/functions.php';
3284
+ }
3285
+
3286
+ $current_user = self::_get_current_wp_user();
3287
+ // $admin_email = get_option( 'admin_email' );
3288
+ $admin_email = $current_user->user_email;
3289
+
3290
+ // Aliases.
3291
+ $deactivate_plugin_title = $this->esc_html_inline( 'That\'s exhausting, please deactivate', 'deactivate-plugin-title' );
3292
+ $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' );
3293
+ $install_previous_title = $this->esc_html_inline( 'Let\'s try your previous version', 'install-previous-title' );
3294
+ $install_previous_desc = $this->esc_html_inline( 'Uninstall this version and install the previous one.', 'install-previous-desc' );
3295
+ $fix_issue_title = $this->esc_html_inline( 'Yes - I\'m giving you a chance to fix it', 'fix-issue-title' );
3296
+ $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' );
3297
+ /* translators: %s: product title (e.g. "Awesome Plugin" requires an access to...) */
3298
+ $x_requires_access_to_api = $this->esc_html_inline( '%s requires an access to our API.', 'x-requires-access-to-api' );
3299
+ $sysadmin_title = $this->esc_html_inline( 'I\'m a system administrator', 'sysadmin-title' );
3300
+ $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' );
3301
+
3302
+ $message = false;
3303
+ if ( is_object( $api_result ) &&
3304
+ isset( $api_result->error ) &&
3305
+ isset( $api_result->error->code )
3306
+ ) {
3307
+ switch ( $api_result->error->code ) {
3308
+ case 'curl_missing':
3309
+ $missing_methods = '';
3310
+ if ( is_array( $api_result->missing_methods ) &&
3311
+ ! empty( $api_result->missing_methods )
3312
+ ) {
3313
+ foreach ( $api_result->missing_methods as $m ) {
3314
+ if ( 'curl_version' === $m ) {
3315
+ continue;
3316
+ }
3317
+
3318
+ if ( ! empty( $missing_methods ) ) {
3319
+ $missing_methods .= ', ';
3320
+ }
3321
+
3322
+ $missing_methods .= sprintf( '<code>%s</code>', $m );
3323
+ }
3324
+
3325
+ if ( ! empty( $missing_methods ) ) {
3326
+ $missing_methods = sprintf(
3327
+ '<br><br><b>%s</b> %s',
3328
+ $this->esc_html_inline( 'Disabled method(s):', 'curl-disabled-methods' ),
3329
+ $missing_methods
3330
+ );
3331
+ }
3332
+ }
3333
+
3334
+ $message = sprintf(
3335
+ $x_requires_access_to_api . ' ' .
3336
+ $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' ) . ' ' .
3337
+ $missing_methods .
3338
+ ' %s',
3339
+ '<b>' . $this->get_plugin_name() . '</b>',
3340
+ sprintf(
3341
+ '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
3342
+ sprintf(
3343
+ '<a class="fs-resolve" data-type="curl" href="#"><b>%s</b></a>%s',
3344
+ $this->get_text_inline( 'I don\'t know what is cURL or how to install it, help me!', 'curl-missing-no-clue-title' ),
3345
+ ' - ' . sprintf(
3346
+ $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' ),
3347
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
3348
+ )
3349
+ ),
3350
+ sprintf(
3351
+ '<b>%s</b> - %s',
3352
+ $sysadmin_title,
3353
+ 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 ) ) )
3354
+ ),
3355
+ sprintf(
3356
+ '<a href="%s"><b>%s</b></a> - %s',
3357
+ 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 ),
3358
+ $deactivate_plugin_title,
3359
+ $deactivate_plugin_desc
3360
+ )
3361
+ )
3362
+ );
3363
+ break;
3364
+ case 'cloudflare_ddos_protection':
3365
+ $message = sprintf(
3366
+ $x_requires_access_to_api . ' ' .
3367
+ $this->esc_html_inline( 'From unknown reason, CloudFlare, the firewall we use, blocks the connection.', 'cloudflare-blocks-connection-message' ) . ' ' .
3368
+ $happy_to_resolve_issue_asap .
3369
+ ' %s',
3370
+ '<b>' . $this->get_plugin_name() . '</b>',
3371
+ sprintf(
3372
+ '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
3373
+ sprintf(
3374
+ '<a class="fs-resolve" data-type="cloudflare" href="#"><b>%s</b></a>%s',
3375
+ $fix_issue_title,
3376
+ ' - ' . sprintf(
3377
+ $fix_issue_desc,
3378
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
3379
+ )
3380
+ ),
3381
+ sprintf(
3382
+ '<a href="%s" target="_blank"><b>%s</b></a> - %s',
3383
+ sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),
3384
+ $install_previous_title,
3385
+ $install_previous_desc
3386
+ ),
3387
+ sprintf(
3388
+ '<a href="%s"><b>%s</b></a> - %s',
3389
+ 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 ),
3390
+ $deactivate_plugin_title,
3391
+ $deactivate_plugin_desc
3392
+ )
3393
+ )
3394
+ );
3395
+ break;
3396
+ case 'squid_cache_block':
3397
+ $message = sprintf(
3398
+ $x_requires_access_to_api . ' ' .
3399
+ $this->esc_html_inline( 'It looks like your server is using Squid ACL (access control lists), which blocks the connection.', 'squid-blocks-connection-message' ) .
3400
+ ' %s',
3401
+ '<b>' . $this->get_plugin_name() . '</b>',
3402
+ sprintf(
3403
+ '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
3404
+ sprintf(
3405
+ '<a class="fs-resolve" data-type="squid" href="#"><b>%s</b></a> - %s',
3406
+ $this->esc_html_inline( 'I don\'t know what is Squid or ACL, help me!', 'squid-no-clue-title' ),
3407
+ sprintf(
3408
+ $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' ),
3409
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
3410
+ )
3411
+ ),
3412
+ sprintf(
3413
+ '<b>%s</b> - %s',
3414
+ $sysadmin_title,
3415
+ sprintf(
3416
+ $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' ),
3417
+ // We use a filter since the plugin might require additional API connectivity.
3418
+ '<b>' . implode( ', ', $this->apply_filters( 'api_domains', array(
3419
+ 'api.freemius.com',
3420
+ 'wp.freemius.com'
3421
+ ) ) ) . '</b>',
3422
+ $this->_module_type
3423
+ )
3424
+ ),
3425
+ sprintf(
3426
+ '<a href="%s"><b>%s</b></a> - %s',
3427
+ 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 ),
3428
+ $deactivate_plugin_title,
3429
+ $deactivate_plugin_desc
3430
+ )
3431
+ )
3432
+ );
3433
+ break;
3434
+ // default:
3435
+ // $message = $this->get_text_inline( 'connectivity-test-fails-message' );
3436
+ // break;
3437
+ }
3438
+ }
3439
+
3440
+ $message_id = 'failed_connect_api';
3441
+ $type = 'error';
3442
+
3443
+ $connectivity_test_fails_message = $this->esc_html_inline( 'From unknown reason, the API connectivity test failed.', 'connectivity-test-fails-message' );
3444
+
3445
+ if ( false === $message ) {
3446
+ if ( $is_first_failure ) {
3447
+ // First attempt failed.
3448
+ $message = sprintf(
3449
+ $x_requires_access_to_api . ' ' .
3450
+ $connectivity_test_fails_message . ' ' .
3451
+ $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>' .
3452
+ '%s',
3453
+ '<b>' . $this->get_plugin_name() . '</b>',
3454
+ sprintf(
3455
+ '<div id="fs_firewall_issue_options">%s %s</div>',
3456
+ sprintf(
3457
+ '<a class="button button-primary fs-resolve" data-type="retry_ping" href="#">%s</a>',
3458
+ $this->get_text_inline( 'Yes - do your thing', 'yes-do-your-thing' )
3459
+ ),
3460
+ sprintf(
3461
+ '<a href="%s" class="button">%s</a>',
3462
+ 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 ),
3463
+ $this->get_text_inline( 'No - just deactivate', 'no-deactivate' )
3464
+ )
3465
+ )
3466
+ );
3467
+
3468
+ $message_id = 'failed_connect_api_first';
3469
+ $type = 'promotion';
3470
+ } else {
3471
+ // Second connectivity attempt failed.
3472
+ $message = sprintf(
3473
+ $x_requires_access_to_api . ' ' .
3474
+ $connectivity_test_fails_message . ' ' .
3475
+ $happy_to_resolve_issue_asap .
3476
+ ' %s',
3477
+ '<b>' . $this->get_plugin_name() . '</b>',
3478
+ sprintf(
3479
+ '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
3480
+ sprintf(
3481
+ '<a class="fs-resolve" data-type="general" href="#"><b>%s</b></a>%s',
3482
+ $fix_issue_title,
3483
+ ' - ' . sprintf(
3484
+ $fix_issue_desc,
3485
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
3486
+ )
3487
+ ),
3488
+ sprintf(
3489
+ '<a href="%s" target="_blank"><b>%s</b></a> - %s',
3490
+ sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),
3491
+ $install_previous_title,
3492
+ $install_previous_desc
3493
+ ),
3494
+ sprintf(
3495
+ '<a href="%s"><b>%s</b></a> - %s',
3496
+ 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 ),
3497
+ $deactivate_plugin_title,
3498
+ $deactivate_plugin_desc
3499
+ )
3500
+ )
3501
+ );
3502
+ }
3503
+ }
3504
+
3505
+ $this->_admin_notices->add_sticky(
3506
+ $message,
3507
+ $message_id,
3508
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
3509
+ $type
3510
+ );
3511
+ }
3512
+
3513
+ /**
3514
+ * Handle user request to resolve connectivity issue.
3515
+ * This method will send an email to Freemius API technical staff for resolution.
3516
+ * The email will contain server's info and installed plugins (might be caching issue).
3517
+ *
3518
+ * @author Vova Feldman (@svovaf)
3519
+ * @since 1.0.9
3520
+ */
3521
+ function _email_about_firewall_issue() {
3522
+ $this->_admin_notices->remove_sticky( 'failed_connect_api' );
3523
+
3524
+ $pong = $this->ping();
3525
+
3526
+ $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
3527
+
3528
+ if ( $is_connected ) {
3529
+ $this->store_connectivity_info( $pong, $is_connected );
3530
+
3531
+ echo $this->get_after_plugin_activation_redirect_url();
3532
+ exit;
3533
+ }
3534
+
3535
+ $current_user = self::_get_current_wp_user();
3536
+ $admin_email = $current_user->user_email;
3537
+
3538
+ $error_type = fs_request_get( 'error_type', 'general' );
3539
+
3540
+ switch ( $error_type ) {
3541
+ case 'squid':
3542
+ $title = 'Squid ACL Blocking Issue';
3543
+ break;
3544
+ case 'cloudflare':
3545
+ $title = 'CloudFlare Blocking Issue';
3546
+ break;
3547
+ default:
3548
+ $title = 'API Connectivity Issue';
3549
+ break;
3550
+ }
3551
+
3552
+ $custom_email_sections = array();
3553
+
3554
+ // Add 'API Error' custom email section.
3555
+ $custom_email_sections['api_error'] = array(
3556
+ 'title' => 'API Error',
3557
+ 'rows' => array(
3558
+ 'ping' => array(
3559
+ 'API Error',
3560
+ is_string( $pong ) ? htmlentities( $pong ) : json_encode( $pong )
3561
+ ),
3562
+ )
3563
+ );
3564
+
3565
+ // Send email with technical details to resolve API connectivity issues.
3566
+ $this->send_email(
3567
+ 'api@freemius.com', // recipient
3568
+ $title . ' [' . $this->get_plugin_name() . ']', // subject
3569
+ $custom_email_sections,
3570
+ array( "Reply-To: $admin_email <$admin_email>" ) // headers
3571
+ );
3572
+
3573
+ $this->_admin_notices->add_sticky(
3574
+ sprintf(
3575
+ $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' ),
3576
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
3577
+ ),
3578
+ 'server_details_sent'
3579
+ );
3580
+
3581
+ // Action was taken, tell that API connectivity troubleshooting should be off now.
3582
+
3583
+ echo "1";
3584
+ exit;
3585
+ }
3586
+
3587
+ /**
3588
+ * Handle connectivity test retry approved by the user.
3589
+ *
3590
+ * @author Vova Feldman (@svovaf)
3591
+ * @since 1.1.7.4
3592
+ */
3593
+ function _retry_connectivity_test() {
3594
+ $this->_admin_notices->remove_sticky( 'failed_connect_api_first' );
3595
+
3596
+ $pong = $this->ping();
3597
+
3598
+ $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
3599
+
3600
+ if ( $is_connected ) {
3601
+ $this->store_connectivity_info( $pong, $is_connected );
3602
+
3603
+ echo $this->get_after_plugin_activation_redirect_url();
3604
+ } else {
3605
+ // Add connectivity issue message after 2nd failed attempt.
3606
+ $this->_add_connectivity_issue_message( $pong, false );
3607
+
3608
+ echo "1";
3609
+ }
3610
+
3611
+ exit;
3612
+ }
3613
+
3614
+ static function _add_firewall_issues_javascript() {
3615
+ $params = array();
3616
+ fs_require_once_template( 'firewall-issues-js.php', $params );
3617
+ }
3618
+
3619
+ #endregion
3620
+
3621
+ #----------------------------------------------------------------------------------
3622
+ #region Email
3623
+ #----------------------------------------------------------------------------------
3624
+
3625
+ /**
3626
+ * Generates and sends an HTML email with customizable sections.
3627
+ *
3628
+ * @author Leo Fajardo (@leorw)
3629
+ * @since 1.1.2
3630
+ *
3631
+ * @param string $to_address
3632
+ * @param string $subject
3633
+ * @param array $sections
3634
+ * @param array $headers
3635
+ *
3636
+ * @return bool Whether the email contents were sent successfully.
3637
+ */
3638
+ private function send_email(
3639
+ $to_address,
3640
+ $subject,
3641
+ $sections = array(),
3642
+ $headers = array()
3643
+ ) {
3644
+ $default_sections = $this->get_email_sections();
3645
+
3646
+ // Insert new sections or replace the default email sections.
3647
+ if ( is_array( $sections ) && ! empty( $sections ) ) {
3648
+ foreach ( $sections as $section_id => $custom_section ) {
3649
+ if ( ! isset( $default_sections[ $section_id ] ) ) {
3650
+ // If the section does not exist, add it.
3651
+ $default_sections[ $section_id ] = $custom_section;
3652
+ } else {
3653
+ // If the section already exists, override it.
3654
+ $current_section = $default_sections[ $section_id ];
3655
+
3656
+ // Replace the current section's title if a custom section title exists.
3657
+ if ( isset( $custom_section['title'] ) ) {
3658
+ $current_section['title'] = $custom_section['title'];
3659
+ }
3660
+
3661
+ // Insert new rows under the current section or replace the default rows.
3662
+ if ( isset( $custom_section['rows'] ) && is_array( $custom_section['rows'] ) && ! empty( $custom_section['rows'] ) ) {
3663
+ foreach ( $custom_section['rows'] as $row_id => $row ) {
3664
+ $current_section['rows'][ $row_id ] = $row;
3665
+ }
3666
+ }
3667
+
3668
+ $default_sections[ $section_id ] = $current_section;
3669
+ }
3670
+ }
3671
+ }
3672
+
3673
+ $vars = array( 'sections' => $default_sections );
3674
+ $message = fs_get_template( 'email.php', $vars );
3675
+
3676
+ // Set the type of email to HTML.
3677
+ $headers[] = 'Content-type: text/html; charset=UTF-8';
3678
+
3679
+ $header_string = implode( "\r\n", $headers );
3680
+
3681
+ return wp_mail(
3682
+ $to_address,
3683
+ $subject,
3684
+ $message,
3685
+ $header_string
3686
+ );
3687
+ }
3688
+
3689
+ /**
3690
+ * Generates the data for the sections of the email content.
3691
+ *
3692
+ * @author Leo Fajardo (@leorw)
3693
+ * @since 1.1.2
3694
+ *
3695
+ * @return array
3696
+ */
3697
+ private function get_email_sections() {
3698
+ // Retrieve the current user's information so that we can get the user's email, first name, and last name below.
3699
+ $current_user = self::_get_current_wp_user();
3700
+
3701
+ // Retrieve the cURL version information so that we can get the version number below.
3702
+ $curl_version_information = curl_version();
3703
+
3704
+ $active_plugin = self::get_active_plugins();
3705
+
3706
+ // Generate the list of active plugins separated by new line.
3707
+ $active_plugin_string = '';
3708
+ foreach ( $active_plugin as $plugin ) {
3709
+ $active_plugin_string .= sprintf(
3710
+ '<a href="%s">%s</a> [v%s]<br>',
3711
+ $plugin['PluginURI'],
3712
+ $plugin['Name'],
3713
+ $plugin['Version']
3714
+ );
3715
+ }
3716
+
3717
+ $server_ip = WP_FS__REMOTE_ADDR;
3718
+
3719
+ // Add PHP info for deeper investigation.
3720
+ ob_start();
3721
+ phpinfo();
3722
+ $php_info = ob_get_clean();
3723
+
3724
+ $api_domain = substr( FS_API__ADDRESS, strpos( FS_API__ADDRESS, ':' ) + 3 );
3725
+
3726
+ // Generate the default email sections.
3727
+ $sections = array(
3728
+ 'sdk' => array(
3729
+ 'title' => 'SDK',
3730
+ 'rows' => array(
3731
+ 'fs_version' => array( 'FS Version', $this->version ),
3732
+ 'curl_version' => array( 'cURL Version', $curl_version_information['version'] )
3733
+ )
3734
+ ),
3735
+ 'plugin' => array(
3736
+ 'title' => ucfirst( $this->get_module_type() ),
3737
+ 'rows' => array(
3738
+ 'name' => array( 'Name', $this->get_plugin_name() ),
3739
+ 'version' => array( 'Version', $this->get_plugin_version() )
3740
+ )
3741
+ ),
3742
+ 'api' => array(
3743
+ 'title' => 'API Subdomain',
3744
+ 'rows' => array(
3745
+ 'dns' => array(
3746
+ 'DNS_CNAME',
3747
+ function_exists( 'dns_get_record' ) ?
3748
+ var_export( dns_get_record( $api_domain, DNS_CNAME ), true ) :
3749
+ 'dns_get_record() disabled/blocked'
3750
+ ),
3751
+ 'ip' => array(
3752
+ 'IP',
3753
+ function_exists( 'gethostbyname' ) ?
3754
+ gethostbyname( $api_domain ) :
3755
+ 'gethostbyname() disabled/blocked'
3756
+ ),
3757
+ ),
3758
+ ),
3759
+ 'site' => array(
3760
+ 'title' => 'Site',
3761
+ 'rows' => array(
3762
+ 'unique_id' => array( 'Unique ID', $this->get_anonymous_id() ),
3763
+ 'address' => array( 'Address', site_url() ),
3764
+ 'host' => array(
3765
+ 'HTTP_HOST',
3766
+ ( ! empty( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : '' )
3767
+ ),
3768
+ 'hosting' => array(
3769
+ 'Hosting Company' => fs_request_has( 'hosting_company' ) ?
3770
+ fs_request_get( 'hosting_company' ) :
3771
+ 'Unknown',
3772
+ ),
3773
+ 'server_addr' => array(
3774
+ 'SERVER_ADDR',
3775
+ '<a href="http://www.projecthoneypot.org/ip_' . $server_ip . '">' . $server_ip . '</a>'
3776
+ )
3777
+ )
3778
+ ),
3779
+ 'user' => array(
3780
+ 'title' => 'User',
3781
+ 'rows' => array(
3782
+ 'email' => array( 'Email', $current_user->user_email ),
3783
+ 'first' => array( 'First', $current_user->user_firstname ),
3784
+ 'last' => array( 'Last', $current_user->user_lastname )
3785
+ )
3786
+ ),
3787
+ 'plugins' => array(
3788
+ 'title' => 'Plugins',
3789
+ 'rows' => array(
3790
+ 'active_plugins' => array( 'Active Plugins', $active_plugin_string )
3791
+ )
3792
+ ),
3793
+ 'php_info' => array(
3794
+ 'title' => 'PHP Info',
3795
+ 'rows' => array(
3796
+ 'info' => array( $php_info )
3797
+ ),
3798
+ )
3799
+ );
3800
+
3801
+ // Allow the sections to be modified by other code.
3802
+ $sections = $this->apply_filters( 'email_template_sections', $sections );
3803
+
3804
+ return $sections;
3805
+ }
3806
+
3807
+ #endregion
3808
+
3809
+ #----------------------------------------------------------------------------------
3810
+ #region Initialization
3811
+ #----------------------------------------------------------------------------------
3812
+
3813
+ /**
3814
+ * Init plugin's Freemius instance.
3815
+ *
3816
+ * @author Vova Feldman (@svovaf)
3817
+ * @since 1.0.1
3818
+ *
3819
+ * @param number $id
3820
+ * @param string $public_key
3821
+ * @param bool $is_live
3822
+ * @param bool $is_premium
3823
+ */
3824
+ function init( $id, $public_key, $is_live = true, $is_premium = true ) {
3825
+ $this->_logger->entrance();
3826
+
3827
+ $this->dynamic_init( array(
3828
+ 'id' => $id,
3829
+ 'public_key' => $public_key,
3830
+ 'is_live' => $is_live,
3831
+ 'is_premium' => $is_premium,
3832
+ ) );
3833
+ }
3834
+
3835
+ /**
3836
+ * Dynamic initiator, originally created to support initiation
3837
+ * with parent_id for add-ons.
3838
+ *
3839
+ * @author Vova Feldman (@svovaf)
3840
+ * @since 1.0.6
3841
+ *
3842
+ * @param array $plugin_info
3843
+ *
3844
+ * @throws Freemius_Exception
3845
+ */
3846
+ function dynamic_init( array $plugin_info ) {
3847
+ $this->_logger->entrance();
3848
+
3849
+ $this->parse_settings( $plugin_info );
3850
+
3851
+ if ( ! self::is_ajax() ) {
3852
+ if ( ! $this->is_addon() || $this->is_only_premium() ) {
3853
+ add_action(
3854
+ ( $this->_is_network_active && fs_is_network_admin() ? 'network_' : '' ) . 'admin_menu',
3855
+ array( &$this, '_prepare_admin_menu' ),
3856
+ WP_FS__LOWEST_PRIORITY
3857
+ );
3858
+ }
3859
+ }
3860
+
3861
+ if ( $this->should_stop_execution() ) {
3862
+ return;
3863
+ }
3864
+
3865
+ if ( ! $this->is_registered() ) {
3866
+ if ( $this->is_anonymous() ) {
3867
+ // If user skipped, no need to test connectivity.
3868
+ $this->_has_api_connection = true;
3869
+ $this->_is_on = true;
3870
+ } else {
3871
+ if ( ! $this->has_api_connectivity() ) {
3872
+ if ( $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) ||
3873
+ $this->_admin_notices->has_sticky( 'failed_connect_api' )
3874
+ ) {
3875
+ if ( ! $this->_enable_anonymous || $this->is_premium() ) {
3876
+ // If anonymous mode is disabled, add firewall admin-notice message.
3877
+ add_action( 'admin_footer', array( 'Freemius', '_add_firewall_issues_javascript' ) );
3878
+
3879
+ $ajax_action_suffix = $this->_slug . ( $this->is_theme() ? ':theme' : '' );
3880
+ add_action( "wp_ajax_fs_resolve_firewall_issues_{$ajax_action_suffix}", array(
3881
+ &$this,
3882
+ '_email_about_firewall_issue'
3883
+ ) );
3884
+
3885
+ add_action( "wp_ajax_fs_retry_connectivity_test_{$ajax_action_suffix}", array(
3886
+ &$this,
3887
+ '_retry_connectivity_test'
3888
+ ) );
3889
+
3890
+ /**
3891
+ * 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.
3892
+ *
3893
+ * @author Vova Feldman (@svovaf)
3894
+ * @since 2.0.0
3895
+ */
3896
+ /*$this->add_ajax_action( 'resolve_firewall_issues', array(
3897
+ &$this,
3898
+ '_email_about_firewall_issue'
3899
+ ) );
3900
+
3901
+ $this->add_ajax_action( 'retry_connectivity_test', array(
3902
+ &$this,
3903
+ '_retry_connectivity_test'
3904
+ ) );*/
3905
+ }
3906
+ }
3907
+
3908
+ return;
3909
+ } else {
3910
+ $this->_admin_notices->remove_sticky( array(
3911
+ 'failed_connect_api_first',
3912
+ 'failed_connect_api',
3913
+ ) );
3914
+
3915
+ if ( $this->_anonymous_mode ) {
3916
+ // Simulate anonymous mode.
3917
+ $this->_is_anonymous = true;
3918
+ }
3919
+ }
3920
+ }
3921
+
3922
+ // Check if Freemius is on for the current plugin.
3923
+ // This MUST be executed after all the plugin variables has been loaded.
3924
+ if ( ! $this->is_on() ) {
3925
+ return;
3926
+ }
3927
+ }
3928
+
3929
+ if ( $this->has_api_connectivity() ) {
3930
+ if ( self::is_cron() ) {
3931
+ $this->hook_callback_to_sync_cron();
3932
+ } else if ( $this->is_user_in_admin() ) {
3933
+ /**
3934
+ * Schedule daily data sync cron if:
3935
+ *
3936
+ * 1. User opted-in (for tracking).
3937
+ * 2. If skipped, but later upgraded (opted-in via upgrade).
3938
+ *
3939
+ * @author Vova Feldman (@svovaf)
3940
+ * @since 1.1.7.3
3941
+ *
3942
+ */
3943
+ if ( $this->is_registered() ) {
3944
+ if ( ! $this->is_sync_cron_on() && $this->is_tracking_allowed() ) {
3945
+ $this->schedule_sync_cron();
3946
+ }
3947
+ }
3948
+
3949
+ /**
3950
+ * Check if requested for manual blocking background sync.
3951
+ */
3952
+ if ( fs_request_has( 'background_sync' ) ) {
3953
+ $this->run_manual_sync();
3954
+ }
3955
+ }
3956
+ }
3957
+
3958
+ if ( $this->is_registered() ) {
3959
+ $this->hook_callback_to_install_sync();
3960
+ }
3961
+
3962
+ if ( $this->is_addon() ) {
3963
+ if ( $this->is_parent_plugin_installed() ) {
3964
+ // Link to parent FS.
3965
+ $this->_parent = self::get_instance_by_id( $this->_plugin->parent_plugin_id );
3966
+
3967
+ // Get parent plugin reference.
3968
+ $this->_parent_plugin = $this->_parent->get_plugin();
3969
+ }
3970
+ }
3971
+
3972
+ if ( $this->is_user_in_admin() ) {
3973
+ if ( $this->is_addon() ) {
3974
+ if ( ! $this->is_parent_plugin_installed() ) {
3975
+ $parent_name = $this->get_option( $plugin_info, 'parent_name', null );
3976
+
3977
+ if ( isset( $plugin_info['parent'] ) ) {
3978
+ $parent_name = $this->get_option( $plugin_info['parent'], 'name', null );
3979
+ }
3980
+
3981
+ $this->_admin_notices->add(
3982
+ ( ! empty( $parent_name ) ?
3983
+ 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 ) :
3984
+ 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() )
3985
+ ),
3986
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
3987
+ 'error'
3988
+ );
3989
+
3990
+ return;
3991
+ } else {
3992
+ if ( $this->_parent->is_registered() && ! $this->is_registered() ) {
3993
+ // If parent plugin activated, automatically install add-on for the user.
3994
+ $this->_activate_addon_account( $this->_parent );
3995
+ } else if ( ! $this->_parent->is_registered() && $this->is_registered() ) {
3996
+ // If add-on activated and parent not, automatically install parent for the user.
3997
+ $this->activate_parent_account( $this->_parent );
3998
+ }
3999
+
4000
+ // @todo This should be only executed on activation. It should be migrated to register_activation_hook() together with other activation related logic.
4001
+ if ( $this->is_premium() ) {
4002
+ // Remove add-on download admin-notice.
4003
+ $this->_parent->_admin_notices->remove_sticky( array(
4004
+ 'addon_plan_upgraded_' . $this->_slug,
4005
+ 'no_addon_license_' . $this->_slug,
4006
+ ) );
4007
+ }
4008
+
4009
+ // $this->deactivate_premium_only_addon_without_license();
4010
+ }
4011
+ } else {
4012
+ if ( $this->has_addons() &&
4013
+ 'plugin-information' === fs_request_get( 'tab', false ) &&
4014
+ $this->get_id() == fs_request_get( 'parent_plugin_id', false )
4015
+ ) {
4016
+ require_once WP_FS__DIR_INCLUDES . '/fs-plugin-info-dialog.php';
4017
+
4018
+ new FS_Plugin_Info_Dialog( $this );
4019
+ }
4020
+ }
4021
+
4022
+ add_action( 'admin_init', array( &$this, '_admin_init_action' ) );
4023
+
4024
+ // if ( $this->is_registered() ||
4025
+ // $this->is_anonymous() ||
4026
+ // $this->is_pending_activation()
4027
+ // ) {
4028
+ // $this->_init_admin();
4029
+ // }
4030
+ }
4031
+
4032
+ /**
4033
+ * Should be called outside `$this->is_user_in_admin()` scope
4034
+ * because the updater has some logic that needs to be executed
4035
+ * during AJAX calls.
4036
+ *
4037
+ * Currently we need to hook to the `http_request_host_is_external` filter.
4038
+ * In the future, there might be additional logic added.
4039
+ *
4040
+ * @author Vova Feldman
4041
+ * @since 1.2.1.6
4042
+ */
4043
+ if ( $this->is_premium() && $this->has_release_on_freemius() ) {
4044
+ FS_Plugin_Updater::instance( $this );
4045
+ }
4046
+
4047
+ $this->do_action( 'initiated' );
4048
+
4049
+ if ( $this->_storage->prev_is_premium !== $this->_plugin->is_premium ) {
4050
+ if ( isset( $this->_storage->prev_is_premium ) ) {
4051
+ $this->apply_filters(
4052
+ 'after_code_type_change',
4053
+ // New code type.
4054
+ $this->_plugin->is_premium
4055
+ );
4056
+ } else {
4057
+ // Set for code type for the first time.
4058
+ $this->_storage->prev_is_premium = $this->_plugin->is_premium;
4059
+ }
4060
+ }
4061
+
4062
+ if ( ! $this->is_addon() ) {
4063
+ if ( $this->is_registered() ) {
4064
+ // Fix for upgrade from versions < 1.0.9.
4065
+ if ( ! isset( $this->_storage->activation_timestamp ) ) {
4066
+ $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME;
4067
+ }
4068
+
4069
+ $this->do_action( 'after_init_plugin_registered' );
4070
+ } else if ( $this->is_anonymous() ) {
4071
+ $this->do_action( 'after_init_plugin_anonymous' );
4072
+ } else if ( $this->is_pending_activation() ) {
4073
+ $this->do_action( 'after_init_plugin_pending_activations' );
4074
+ }
4075
+ } else {
4076
+ if ( $this->is_registered() ) {
4077
+ $this->do_action( 'after_init_addon_registered' );
4078
+ } else if ( $this->is_anonymous() ) {
4079
+ $this->do_action( 'after_init_addon_anonymous' );
4080
+ } else if ( $this->is_pending_activation() ) {
4081
+ $this->do_action( 'after_init_addon_pending_activations' );
4082
+ }
4083
+ }
4084
+ }
4085
+
4086
+ /**
4087
+ * @author Leo Fajardo (@leorw)
4088
+ *
4089
+ * @since 1.2.1.5
4090
+ */
4091
+ function _stop_tracking_callback() {
4092
+ $this->_logger->entrance();
4093
+
4094
+ $this->check_ajax_referer( 'stop_tracking' );
4095
+
4096
+ $result = $this->stop_tracking( fs_is_network_admin() );
4097
+
4098
+ if ( true === $result ) {
4099
+ self::shoot_ajax_success();
4100
+ }
4101
+
4102
+ $this->_logger->api_error( $result );
4103
+
4104
+ self::shoot_ajax_failure(
4105
+ sprintf( $this->get_text_inline( 'Unexpected API error. Please contact the %s\'s author with the following error.', 'unexpected-api-error' ), $this->_module_type ) .
4106
+ ( $this->is_api_error( $result ) && isset( $result->error ) ?
4107
+ $result->error->message :
4108
+ var_export( $result, true ) )
4109
+ );
4110
+ }
4111
+
4112
+ /**
4113
+ * @author Leo Fajardo (@leorw)
4114
+ * @since 1.2.1.5
4115
+ */
4116
+ function _allow_tracking_callback() {
4117
+ $this->_logger->entrance();
4118
+
4119
+ $this->check_ajax_referer( 'allow_tracking' );
4120
+
4121
+ $result = $this->allow_tracking( fs_is_network_admin() );
4122
+
4123
+ if ( true === $result ) {
4124
+ self::shoot_ajax_success();
4125
+ }
4126
+
4127
+ $this->_logger->api_error( $result );
4128
+
4129
+ self::shoot_ajax_failure(
4130
+ sprintf( $this->get_text_inline( 'Unexpected API error. Please contact the %s\'s author with the following error.', 'unexpected-api-error' ), $this->_module_type ) .
4131
+ ( $this->is_api_error( $result ) && isset( $result->error ) ?
4132
+ $result->error->message :
4133
+ var_export( $result, true ) )
4134
+ );
4135
+ }
4136
+
4137
+ /**
4138
+ * Opt-out from usage tracking.
4139
+ *
4140
+ * Note: This will not delete the account information but will stop all tracking.
4141
+ *
4142
+ * Returns:
4143
+ * 1. FALSE - If the user never opted-in.
4144
+ * 2. TRUE - If successfully opted-out.
4145
+ * 3. object - API result on failure.
4146
+ *
4147
+ * @author Leo Fajardo (@leorw)
4148
+ * @since 1.2.1.5
4149
+ *
4150
+ * @return bool|object
4151
+ */
4152
+ function stop_site_tracking() {
4153
+ $this->_logger->entrance();
4154
+
4155
+ if ( ! $this->is_registered() ) {
4156
+ // User never opted-in.
4157
+ return false;
4158
+ }
4159
+
4160
+ if ( $this->is_tracking_prohibited() ) {
4161
+ // Already disconnected.
4162
+ return true;
4163
+ }
4164
+
4165
+ // Send update to FS.
4166
+ $result = $this->get_api_site_scope()->call( '/?fields=is_disconnected', 'put', array(
4167
+ 'is_disconnected' => true
4168
+ ) );
4169
+
4170
+ if ( ! $this->is_api_result_entity( $result ) ||
4171
+ ! isset( $result->is_disconnected ) ||
4172
+ ! $result->is_disconnected
4173
+ ) {
4174
+ $this->_logger->api_error( $result );
4175
+
4176
+ return $result;
4177
+ }
4178
+
4179
+ $this->_site->is_disconnected = $result->is_disconnected;
4180
+ $this->_store_site();
4181
+
4182
+ $this->clear_sync_cron();
4183
+
4184
+ // Successfully disconnected.
4185
+ return true;
4186
+ }
4187
+
4188
+ /**
4189
+ * Opt-out network from usage tracking.
4190
+ *
4191
+ * Note: This will not delete the account information but will stop all tracking.
4192
+ *
4193
+ * Returns:
4194
+ * 1. FALSE - If the user never opted-in.
4195
+ * 2. TRUE - If successfully opted-out.
4196
+ * 3. object - API result on failure.
4197
+ *
4198
+ * @author Leo Fajardo (@leorw)
4199
+ * @since 1.2.1.5
4200
+ *
4201
+ * @return bool|object
4202
+ */
4203
+ function stop_network_tracking() {
4204
+ $this->_logger->entrance();
4205
+
4206
+ if ( ! $this->is_registered() ) {
4207
+ // User never opted-in.
4208
+ return false;
4209
+ }
4210
+
4211
+ $install_id_2_blog_id = array();
4212
+ $installs_map = $this->get_blog_install_map();
4213
+
4214
+ $opt_out_all = true;
4215
+
4216
+ $params = array();
4217
+ foreach ( $installs_map as $blog_id => $install ) {
4218
+ if ( $install->is_tracking_prohibited() ) {
4219
+ // Already opted-out.
4220
+ continue;
4221
+ }
4222
+
4223
+ if ( $this->is_site_delegated_connection( $blog_id ) ) {
4224
+ // Opt-out only from non-delegated installs.
4225
+ $opt_out_all = false;
4226
+ continue;
4227
+ }
4228
+
4229
+ $params[] = array( 'id' => $install->id );
4230
+
4231
+ $install_id_2_blog_id[ $install->id ] = $blog_id;
4232
+ }
4233
+
4234
+ if ( empty( $install_id_2_blog_id ) ) {
4235
+ return true;
4236
+ }
4237
+
4238
+ $params[] = array( 'is_disconnected' => true );
4239
+
4240
+ // Send update to FS.
4241
+ $result = $this->get_current_or_network_user_api_scope()->call( "/plugins/{$this->_module_id}/installs.json", 'put', $params );
4242
+
4243
+ if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
4244
+ $this->_logger->api_error( $result );
4245
+
4246
+ return $result;
4247
+ }
4248
+
4249
+ foreach ( $result->installs as $r_install ) {
4250
+ $blog_id = $install_id_2_blog_id[ $r_install->id ];
4251
+ $install = $installs_map[ $blog_id ];
4252
+ $install->is_disconnected = $r_install->is_disconnected;
4253
+ $this->_store_site( true, $blog_id, $install );
4254
+ }
4255
+
4256
+ $this->clear_sync_cron( $opt_out_all );
4257
+
4258
+ // Successfully disconnected.
4259
+ return true;
4260
+ }
4261
+
4262
+ /**
4263
+ * Opt-out from usage tracking.
4264
+ *
4265
+ * Note: This will not delete the account information but will stop all tracking.
4266
+ *
4267
+ * Returns:
4268
+ * 1. FALSE - If the user never opted-in.
4269
+ * 2. TRUE - If successfully opted-out.
4270
+ * 3. object - API result on failure.
4271
+ *
4272
+ * @author Leo Fajardo (@leorw)
4273
+ * @since 1.2.1.5
4274
+ *
4275
+ * @param bool $is_network_action
4276
+ *
4277
+ * @return bool|object
4278
+ */
4279
+ function stop_tracking( $is_network_action = false ) {
4280
+ $this->_logger->entrance();
4281
+
4282
+ return $is_network_action ?
4283
+ $this->stop_network_tracking() :
4284
+ $this->stop_site_tracking();
4285
+ }
4286
+
4287
+ /**
4288
+ * Opt-in back into usage tracking.
4289
+ *
4290
+ * Note: This will only work if the user opted-in previously.
4291
+ *
4292
+ * Returns:
4293
+ * 1. FALSE - If the user never opted-in.
4294
+ * 2. TRUE - If successfully opted-in back to usage tracking.
4295
+ * 3. object - API result on failure.
4296
+ *
4297
+ * @author Leo Fajardo (@leorw)
4298
+ * @since 1.2.1.5
4299
+ *
4300
+ * @return bool|object
4301
+ */
4302
+ function allow_site_tracking() {
4303
+ $this->_logger->entrance();
4304
+
4305
+ if ( ! $this->is_registered() ) {
4306
+ // User never opted-in.
4307
+ return false;
4308
+ }
4309
+
4310
+ if ( $this->is_tracking_allowed() ) {
4311
+ // Tracking already allowed.
4312
+ return true;
4313
+ }
4314
+
4315
+ $result = $this->get_api_site_scope()->call( '/?is_disconnected', 'put', array(
4316
+ 'is_disconnected' => false
4317
+ ) );
4318
+
4319
+ if ( ! $this->is_api_result_entity( $result ) ||
4320
+ ! isset( $result->is_disconnected ) ||
4321
+ $result->is_disconnected
4322
+ ) {
4323
+ $this->_logger->api_error( $result );
4324
+
4325
+ return $result;
4326
+ }
4327
+
4328
+ $this->_site->is_disconnected = $result->is_disconnected;
4329
+ $this->_store_site();
4330
+
4331
+ $this->schedule_sync_cron();
4332
+
4333
+ // Successfully reconnected.
4334
+ return true;
4335
+ }
4336
+
4337
+ /**
4338
+ * Opt-in network back into usage tracking.
4339
+ *
4340
+ * Note: This will only work if the user opted-in previously.
4341
+ *
4342
+ * Returns:
4343
+ * 1. FALSE - If the user never opted-in.
4344
+ * 2. TRUE - If successfully opted-in back to usage tracking.
4345
+ * 3. object - API result on failure.
4346
+ *
4347
+ * @author Leo Fajardo (@leorw)
4348
+ * @since 1.2.1.5
4349
+ *
4350
+ * @return bool|object
4351
+ */
4352
+ function allow_network_tracking() {
4353
+ $this->_logger->entrance();
4354
+
4355
+ if ( ! $this->is_registered() ) {
4356
+ // User never opted-in.
4357
+ return false;
4358
+ }
4359
+
4360
+ $install_id_2_blog_id = array();
4361
+ $installs_map = $this->get_blog_install_map();
4362
+
4363
+ $params = array();
4364
+ foreach ( $installs_map as $blog_id => $install ) {
4365
+ if ( $install->is_tracking_allowed() ) {
4366
+ continue;
4367
+ }
4368
+
4369
+ $params[] = array( 'id' => $install->id );
4370
+
4371
+ $install_id_2_blog_id[ $install->id ] = $blog_id;
4372
+ }
4373
+
4374
+ if ( empty( $install_id_2_blog_id ) ) {
4375
+ return true;
4376
+ }
4377
+
4378
+ $params[] = array( 'is_disconnected' => false );
4379
+
4380
+ // Send update to FS.
4381
+ $result = $this->get_current_or_network_user_api_scope()->call( "/plugins/{$this->_module_id}/installs.json", 'put', $params );
4382
+
4383
+
4384
+ if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
4385
+ $this->_logger->api_error( $result );
4386
+
4387
+ return $result;
4388
+ }
4389
+
4390
+ foreach ( $result->installs as $r_install ) {
4391
+ $blog_id = $install_id_2_blog_id[ $r_install->id ];
4392
+ $install = $installs_map[ $blog_id ];
4393
+ $install->is_disconnected = $r_install->is_disconnected;
4394
+ $this->_store_site( true, $blog_id, $install );
4395
+ }
4396
+
4397
+ $this->schedule_sync_cron();
4398
+
4399
+ // Successfully reconnected.
4400
+ return true;
4401
+ }
4402
+
4403
+ /**
4404
+ * Opt-in back into usage tracking.
4405
+ *
4406
+ * Note: This will only work if the user opted-in previously.
4407
+ *
4408
+ * Returns:
4409
+ * 1. FALSE - If the user never opted-in.
4410
+ * 2. TRUE - If successfully opted-in back to usage tracking.
4411
+ * 3. object - API result on failure.
4412
+ *
4413
+ * @author Leo Fajardo (@leorw)
4414
+ * @since 1.2.1.5
4415
+ *
4416
+ * @param bool $is_network_action
4417
+ *
4418
+ * @return bool|object
4419
+ */
4420
+ function allow_tracking( $is_network_action = false ) {
4421
+ $this->_logger->entrance();
4422
+
4423
+ return $is_network_action ?
4424
+ $this->allow_network_tracking() :
4425
+ $this->allow_site_tracking();
4426
+ }
4427
+
4428
+ /**
4429
+ * If user opted-in and later disabled usage-tracking,
4430
+ * re-allow tracking for licensing and updates.
4431
+ *
4432
+ * @author Leo Fajardo (@leorw)
4433
+ * @since 1.2.1.5
4434
+ *
4435
+ * @param bool $is_context_single_site
4436
+ */
4437
+ private function reconnect_locally( $is_context_single_site = false ) {
4438
+ $this->_logger->entrance();
4439
+
4440
+ if ( ! $this->is_registered() ) {
4441
+ return;
4442
+ }
4443
+
4444
+ if ( ! fs_is_network_admin() || $is_context_single_site ) {
4445
+ if ( $this->is_tracking_prohibited() ) {
4446
+ $this->_site->is_disconnected = false;
4447
+ $this->_store_site();
4448
+ }
4449
+ } else {
4450
+ $installs_map = $this->get_blog_install_map();
4451
+ foreach ( $installs_map as $blog_id => $install ) {
4452
+ /**
4453
+ * @var FS_Site $install
4454
+ */
4455
+ if ( $install->is_tracking_prohibited() ) {
4456
+ $install->is_disconnected = false;
4457
+ $this->_store_site( true, $blog_id, $install );
4458
+ }
4459
+ }
4460
+ }
4461
+ }
4462
+
4463
+ /**
4464
+ * Parse plugin's settings (as defined by the plugin dev).
4465
+ *
4466
+ * @author Vova Feldman (@svovaf)
4467
+ * @since 1.1.7.3
4468
+ *
4469
+ * @param array $plugin_info
4470
+ *
4471
+ * @throws \Freemius_Exception
4472
+ */
4473
+ private function parse_settings( &$plugin_info ) {
4474
+ $this->_logger->entrance();
4475
+
4476
+ $id = $this->get_numeric_option( $plugin_info, 'id', false );
4477
+ $public_key = $this->get_option( $plugin_info, 'public_key', false );
4478
+ $secret_key = $this->get_option( $plugin_info, 'secret_key', null );
4479
+ $parent_id = $this->get_numeric_option( $plugin_info, 'parent_id', null );
4480
+ $parent_name = $this->get_option( $plugin_info, 'parent_name', null );
4481
+
4482
+ /**
4483
+ * @author Vova Feldman (@svovaf)
4484
+ * @since 1.1.9 Try to pull secret key from external config.
4485
+ */
4486
+ if ( is_null( $secret_key ) && defined( "WP_FS__{$this->_slug}_SECRET_KEY" ) ) {
4487
+ $secret_key = constant( "WP_FS__{$this->_slug}_SECRET_KEY" );
4488
+ }
4489
+
4490
+ if ( isset( $plugin_info['parent'] ) ) {
4491
+ $parent_id = $this->get_numeric_option( $plugin_info['parent'], 'id', null );
4492
+ // $parent_slug = $this->get_option( $plugin_info['parent'], 'slug', null );
4493
+ // $parent_public_key = $this->get_option( $plugin_info['parent'], 'public_key', null );
4494
+ // $parent_name = $this->get_option( $plugin_info['parent'], 'name', null );
4495
+ }
4496
+
4497
+ if ( false === $id ) {
4498
+ throw new Freemius_Exception( array(
4499
+ 'error' => array(
4500
+ 'type' => 'ParameterNotSet',
4501
+ 'message' => 'Plugin id parameter is not set.',
4502
+ 'code' => 'plugin_id_not_set',
4503
+ 'http' => 500,
4504
+ )
4505
+ ) );
4506
+ }
4507
+ if ( false === $public_key ) {
4508
+ throw new Freemius_Exception( array(
4509
+ 'error' => array(
4510
+ 'type' => 'ParameterNotSet',
4511
+ 'message' => 'Plugin public_key parameter is not set.',
4512
+ 'code' => 'plugin_public_key_not_set',
4513
+ 'http' => 500,
4514
+ )
4515
+ ) );
4516
+ }
4517
+
4518
+ $plugin = ( $this->_plugin instanceof FS_Plugin ) ?
4519
+ $this->_plugin :
4520
+ new FS_Plugin();
4521
+
4522
+ $plugin->update( array(
4523
+ 'id' => $id,
4524
+ 'type' => $this->get_option( $plugin_info, 'type', $this->_module_type ),
4525
+ 'public_key' => $public_key,
4526
+ 'slug' => $this->_slug,
4527
+ 'parent_plugin_id' => $parent_id,
4528
+ 'version' => $this->get_plugin_version(),
4529
+ 'title' => $this->get_plugin_name(),
4530
+ 'file' => $this->_plugin_basename,
4531
+ 'is_premium' => $this->get_bool_option( $plugin_info, 'is_premium', true ),
4532
+ 'is_live' => $this->get_bool_option( $plugin_info, 'is_live', true ),
4533
+ 'affiliate_moderation' => $this->get_option( $plugin_info, 'has_affiliation' ),
4534
+ ) );
4535
+
4536
+ if ( $plugin->is_updated() ) {
4537
+ // Update plugin details.
4538
+ $this->_plugin = FS_Plugin_Manager::instance( $this->_module_id )->store( $plugin );
4539
+ }
4540
+ // Set the secret key after storing the plugin, we don't want to store the key in the storage.
4541
+ $this->_plugin->secret_key = $secret_key;
4542
+
4543
+ if ( ! isset( $plugin_info['menu'] ) ) {
4544
+ $plugin_info['menu'] = array();
4545
+
4546
+ if ( ! empty( $this->_storage->sdk_last_version ) &&
4547
+ version_compare( $this->_storage->sdk_last_version, '1.1.2', '<=' )
4548
+ ) {
4549
+ // Backward compatibility to 1.1.2
4550
+ $plugin_info['menu']['slug'] = isset( $plugin_info['menu_slug'] ) ?
4551
+ $plugin_info['menu_slug'] :
4552
+ $this->_slug;
4553
+ }
4554
+ }
4555
+
4556
+ $this->_menu = FS_Admin_Menu_Manager::instance(
4557
+ $this->_module_id,
4558
+ $this->_module_type,
4559
+ $this->get_unique_affix()
4560
+ );
4561
+
4562
+ $this->_menu->init( $plugin_info['menu'], $this->is_addon() );
4563
+
4564
+ $this->_has_addons = $this->get_bool_option( $plugin_info, 'has_addons', false );
4565
+ $this->_has_paid_plans = $this->get_bool_option( $plugin_info, 'has_paid_plans', true );
4566
+ $this->_has_premium_version = $this->get_bool_option( $plugin_info, 'has_premium_version', $this->_has_paid_plans );
4567
+ $this->_ignore_pending_mode = $this->get_bool_option( $plugin_info, 'ignore_pending_mode', false );
4568
+ $this->_is_org_compliant = $this->get_bool_option( $plugin_info, 'is_org_compliant', true );
4569
+ $this->_is_premium_only = $this->get_bool_option( $plugin_info, 'is_premium_only', false );
4570
+ if ( $this->_is_premium_only ) {
4571
+ // If premium only plugin, disable anonymous mode.
4572
+ $this->_enable_anonymous = false;
4573
+ $this->_anonymous_mode = false;
4574
+ } else {
4575
+ $this->_enable_anonymous = $this->get_bool_option( $plugin_info, 'enable_anonymous', true );
4576
+ $this->_anonymous_mode = $this->get_bool_option( $plugin_info, 'anonymous_mode', false );
4577
+ }
4578
+ $this->_permissions = $this->get_option( $plugin_info, 'permissions', array() );
4579
+
4580
+ if ( ! empty( $plugin_info['trial'] ) ) {
4581
+ $this->_trial_days = $this->get_numeric_option(
4582
+ $plugin_info['trial'],
4583
+ 'days',
4584
+ // Default to 0 - trial without days specification.
4585
+ 0
4586
+ );
4587
+
4588
+ $this->_is_trial_require_payment = $this->get_bool_option( $plugin_info['trial'], 'is_require_payment', false );
4589
+ }
4590
+ }
4591
+
4592
+ /**
4593
+ * @param string[] $options
4594
+ * @param string $key
4595
+ * @param mixed $default
4596
+ *
4597
+ * @return bool
4598
+ */
4599
+ private function get_option( &$options, $key, $default = false ) {
4600
+ return ! empty( $options[ $key ] ) ? $options[ $key ] : $default;
4601
+ }
4602
+
4603
+ private function get_bool_option( &$options, $key, $default = false ) {
4604
+ return isset( $options[ $key ] ) && is_bool( $options[ $key ] ) ? $options[ $key ] : $default;
4605
+ }
4606
+
4607
+ private function get_numeric_option( &$options, $key, $default = false ) {
4608
+ return isset( $options[ $key ] ) && is_numeric( $options[ $key ] ) ? $options[ $key ] : $default;
4609
+ }
4610
+
4611
+ /**
4612
+ * Gate keeper.
4613
+ *
4614
+ * @author Vova Feldman (@svovaf)
4615
+ * @since 1.1.7.3
4616
+ *
4617
+ * @return bool
4618
+ */
4619
+ private function should_stop_execution() {
4620
+ if ( empty( $this->_storage->was_plugin_loaded ) ) {
4621
+ /**
4622
+ * Don't execute Freemius until plugin was fully loaded at least once,
4623
+ * to give the opportunity for the activation hook to run before pinging
4624
+ * the API for connectivity test. This logic is relevant for the
4625
+ * identification of new plugin install vs. plugin update.
4626
+ *
4627
+ * @author Vova Feldman (@svovaf)
4628
+ * @since 1.1.9
4629
+ */
4630
+ return true;
4631
+ }
4632
+
4633
+ if ( $this->is_activation_mode() ) {
4634
+ if ( ! is_admin() ) {
4635
+ /**
4636
+ * If in activation mode, don't execute Freemius outside of the
4637
+ * admin dashboard.
4638
+ *
4639
+ * @author Vova Feldman (@svovaf)
4640
+ * @since 1.1.7.3
4641
+ */
4642
+ return true;
4643
+ }
4644
+
4645
+ if ( ! WP_FS__IS_HTTP_REQUEST ) {
4646
+ /**
4647
+ * If in activation and executed without HTTP context (e.g. CLI, Cronjob),
4648
+ * then don't start Freemius.
4649
+ *
4650
+ * @author Vova Feldman (@svovaf)
4651
+ * @since 1.1.6.3
4652
+ *
4653
+ * @link https://wordpress.org/support/topic/errors-in-the-freemius-class-when-running-in-wordpress-in-cli
4654
+ */
4655
+ return true;
4656
+ }
4657
+
4658
+ if ( self::is_cron() ) {
4659
+ /**
4660
+ * If in activation mode, don't execute Freemius during wp crons
4661
+ * (wp crons have HTTP context - called as HTTP request).
4662
+ *
4663
+ * @author Vova Feldman (@svovaf)
4664
+ * @since 1.1.7.3
4665
+ */
4666
+ return true;
4667
+ }
4668
+
4669
+ if ( self::is_ajax() &&
4670
+ ! $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) &&
4671
+ ! $this->_admin_notices->has_sticky( 'failed_connect_api' )
4672
+ ) {
4673
+ /**
4674
+ * During activation, if running in AJAX mode, unless there's a sticky
4675
+ * connectivity issue notice, don't run Freemius.
4676
+ *
4677
+ * @author Vova Feldman (@svovaf)
4678
+ * @since 1.1.7.3
4679
+ */
4680
+ return true;
4681
+ }
4682
+ }
4683
+
4684
+ return false;
4685
+ }
4686
+
4687
+ /**
4688
+ * Triggered after code type has changed.
4689
+ *
4690
+ * @author Vova Feldman (@svovaf)
4691
+ * @since 1.1.9.1
4692
+ */
4693
+ function _after_code_type_change() {
4694
+ $this->_logger->entrance();
4695
+
4696
+ if ( $this->is_theme() ) {
4697
+ // Expire the cache of the previous tabs since the theme may
4698
+ // have setting updates after code type has changed.
4699
+ $this->_cache->expire( 'tabs' );
4700
+ $this->_cache->expire( 'tabs_stylesheets' );
4701
+ }
4702
+
4703
+ if ( $this->is_registered() ) {
4704
+ if ( ! $this->is_addon() ) {
4705
+ add_action(
4706
+ is_admin() ? 'admin_init' : 'init',
4707
+ array( &$this, '_plugin_code_type_changed' )
4708
+ );
4709
+ }
4710
+
4711
+ if ( $this->is_premium() ) {
4712
+ // Purge cached payments after switching to the premium version.
4713
+ // @todo This logic doesn't handle purging the cache for serviceware module upgrade.
4714
+ $this->get_api_user_scope()->purge_cache( "/plugins/{$this->_module_id}/payments.json?include_addons=true" );
4715
+ }
4716
+ }
4717
+ }
4718
+
4719
+ /**
4720
+ * Handles plugin's code type change (free <--> premium).
4721
+ *
4722
+ * @author Vova Feldman (@svovaf)
4723
+ * @since 1.0.9
4724
+ */
4725
+ function _plugin_code_type_changed() {
4726
+ $this->_logger->entrance();
4727
+
4728
+ if ( $this->is_premium() ) {
4729
+ $this->reconnect_locally();
4730
+
4731
+ // Activated premium code.
4732
+ $this->do_action( 'after_premium_version_activation' );
4733
+
4734
+ // Remove all sticky messages related to download of the premium version.
4735
+ $this->_admin_notices->remove_sticky( array(
4736
+ 'trial_started',
4737
+ 'plan_upgraded',
4738
+ 'plan_changed',
4739
+ 'license_activated',
4740
+ ) );
4741
+
4742
+ $notice = '';
4743
+ if ( ! $this->is_only_premium() ) {
4744
+ $notice = sprintf( $this->get_text_inline( 'Premium %s version was successfully activated.', 'premium-activated-message' ), $this->_module_type );
4745
+ }
4746
+
4747
+ $license_notice = $this->get_license_network_activation_notice();
4748
+ if ( ! empty( $license_notice ) ) {
4749
+ $notice .= ' ' . $license_notice;
4750
+ }
4751
+
4752
+ if ( ! empty( $notice ) ) {
4753
+ $this->_admin_notices->add_sticky(
4754
+ trim( $notice ),
4755
+ 'premium_activated',
4756
+ $this->get_text_x_inline( 'W00t',
4757
+ 'Used to express elation, enthusiasm, or triumph (especially in electronic communication).', 'woot' ) . '!'
4758
+ );
4759
+ }
4760
+ } else {
4761
+ // Remove sticky message related to premium code activation.
4762
+ $this->_admin_notices->remove_sticky( 'premium_activated' );
4763
+
4764
+ // Activated free code (after had the premium before).
4765
+ $this->do_action( 'after_free_version_reactivation' );
4766
+
4767
+ if ( $this->is_paying() && ! $this->is_premium() ) {
4768
+ $this->_admin_notices->add_sticky(
4769
+ sprintf(
4770
+ /* translators: %s: License type (e.g. you have a professional license) */
4771
+ $this->get_text_inline( 'You have a %s license.', 'you-have-x-license' ),
4772
+ $this->get_plan_title()
4773
+ ) . $this->get_complete_upgrade_instructions(),
4774
+ 'plan_upgraded',
4775
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
4776
+ );
4777
+ }
4778
+ }
4779
+
4780
+ // Schedule code type changes event.
4781
+ $this->schedule_install_sync();
4782
+
4783
+ /**
4784
+ * Unregister the uninstall hook for the other version of the plugin (with different code type) to avoid
4785
+ * triggering a fatal error when uninstalling that plugin. For example, after deactivating the "free" version
4786
+ * of a specific plugin, its uninstall hook should be unregistered after the "premium" version has been
4787
+ * activated. If we don't do that, a fatal error will occur when we try to uninstall the "free" version since
4788
+ * the main file of the "free" version will be loaded first before calling the hooked callback. Since the
4789
+ * free and premium versions are almost identical (same class or have same functions), a fatal error like
4790
+ * "Cannot redeclare class MyClass" or "Cannot redeclare my_function()" will occur.
4791
+ */
4792
+ $this->unregister_uninstall_hook();
4793
+
4794
+ $this->clear_module_main_file_cache();
4795
+
4796
+ // Update is_premium of latest version.
4797
+ $this->_storage->prev_is_premium = $this->_plugin->is_premium;
4798
+ }
4799
+
4800
+ #endregion
4801
+
4802
+ #----------------------------------------------------------------------------------
4803
+ #region Add-ons
4804
+ #----------------------------------------------------------------------------------
4805
+
4806
+ /**
4807
+ * Check if add-on installed and activated on site.
4808
+ *
4809
+ * @author Vova Feldman (@svovaf)
4810
+ * @since 1.0.6
4811
+ *
4812
+ * @param string|number $id_or_slug
4813
+ * @param bool|null $is_premium Since 1.2.1.7 can check for specified add-on version.
4814
+ *
4815
+ * @return bool
4816
+ */
4817
+ function is_addon_activated( $id_or_slug, $is_premium = null ) {
4818
+ $this->_logger->entrance();
4819
+
4820
+ $addon_id = self::get_module_id( $id_or_slug );
4821
+ $is_activated = self::has_instance( $addon_id );
4822
+
4823
+ if ( ! $is_activated ) {
4824
+ return false;
4825
+ }
4826
+
4827
+ if ( is_bool( $is_premium ) ) {
4828
+ // Check if the specified code version is activate.
4829
+ $addon = $this->get_addon_instance( $addon_id );
4830
+ $is_activated = ( $is_premium === $addon->is_premium() );
4831
+ }
4832
+
4833
+ return $is_activated;
4834
+ }
4835
+
4836
+ /**
4837
+ * Check if add-on was connected to install
4838
+ *
4839
+ * @author Vova Feldman (@svovaf)
4840
+ * @since 1.1.7
4841
+ *
4842
+ * @param string|number $id_or_slug
4843
+ *
4844
+ * @return bool
4845
+ */
4846
+ function is_addon_connected( $id_or_slug ) {
4847
+ $this->_logger->entrance();
4848
+
4849
+ $sites = self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN );
4850
+
4851
+ $addon_id = self::get_module_id( $id_or_slug );
4852
+ $addon = $this->get_addon( $addon_id );
4853
+ $slug = $addon->slug;
4854
+ if ( ! isset( $sites[ $slug ] ) ) {
4855
+ return false;
4856
+ }
4857
+
4858
+ $site = $sites[ $slug ];
4859
+
4860
+ $plugin = FS_Plugin_Manager::instance( $addon_id )->get();
4861
+
4862
+ if ( $plugin->parent_plugin_id != $this->_plugin->id ) {
4863
+ // The given slug do NOT belong to any of the plugin's add-ons.
4864
+ return false;
4865
+ }
4866
+
4867
+ return ( is_object( $site ) &&
4868
+ is_numeric( $site->id ) &&
4869
+ is_numeric( $site->user_id ) &&
4870
+ FS_Plugin_Plan::is_valid_id( $site->plan_id )
4871
+ );
4872
+ }
4873
+
4874
+ /**
4875
+ * Determines if add-on installed.
4876
+ *
4877
+ * NOTE: This is a heuristic and only works if the folder/file named as the slug.
4878
+ *
4879
+ * @author Vova Feldman (@svovaf)
4880
+ * @since 1.0.6
4881
+ *
4882
+ * @param string|number $id_or_slug
4883
+ *
4884
+ * @return bool
4885
+ */
4886
+ function is_addon_installed( $id_or_slug ) {
4887
+ $this->_logger->entrance();
4888
+
4889
+ $addon_id = self::get_module_id( $id_or_slug );
4890
+
4891
+ return file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $this->get_addon_basename( $addon_id ) ) );
4892
+ }
4893
+
4894
+ /**
4895
+ * Get add-on basename.
4896
+ *
4897
+ * @author Vova Feldman (@svovaf)
4898
+ * @since 1.0.6
4899
+ *
4900
+ * @param string|number $id_or_slug
4901
+ *
4902
+ * @return string
4903
+ */
4904
+ function get_addon_basename( $id_or_slug ) {
4905
+ $addon_id = self::get_module_id( $id_or_slug );
4906
+
4907
+ if ( $this->is_addon_activated( $addon_id ) ) {
4908
+ return self::instance( $addon_id )->get_plugin_basename();
4909
+ }
4910
+
4911
+ $addon = $this->get_addon( $addon_id );
4912
+ $premium_basename = "{$addon->slug}-premium/{$addon->slug}.php";
4913
+
4914
+ if ( file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $premium_basename ) ) ) {
4915
+ return $premium_basename;
4916
+ }
4917
+
4918
+ $all_plugins = $this->get_all_plugins();
4919
+
4920
+ foreach ( $all_plugins as $basename => &$data ) {
4921
+ if ( $addon->slug === $data['slug'] ||
4922
+ $addon->slug . '-premium' === $data['slug']
4923
+ ) {
4924
+ return $basename;
4925
+ }
4926
+ }
4927
+
4928
+ $free_basename = "{$addon->slug}/{$addon->slug}.php";
4929
+
4930
+ return $free_basename;
4931
+ }
4932
+
4933
+ /**
4934
+ * Get installed add-ons instances.
4935
+ *
4936
+ * @author Vova Feldman (@svovaf)
4937
+ * @since 1.0.6
4938
+ *
4939
+ * @return Freemius[]
4940
+ */
4941
+ function get_installed_addons() {
4942
+ $installed_addons = array();
4943
+ foreach ( self::$_instances as $instance ) {
4944
+ if ( $instance->is_addon() && is_object( $instance->_parent_plugin ) ) {
4945
+ if ( $this->_plugin->id == $instance->_parent_plugin->id ) {
4946
+ $installed_addons[] = $instance;
4947
+ }
4948
+ }
4949
+ }
4950
+
4951
+ return $installed_addons;
4952
+ }
4953
+
4954
+ /**
4955
+ * Check if any add-ons of the plugin are installed.
4956
+ *
4957
+ * @author Leo Fajardo (@leorw)
4958
+ * @since 1.1.1
4959
+ *
4960
+ * @return bool
4961
+ */
4962
+ function has_installed_addons() {
4963
+ if ( ! $this->has_addons() ) {
4964
+ return false;
4965
+ }
4966
+
4967
+ foreach ( self::$_instances as $instance ) {
4968
+ if ( $instance->is_addon() && is_object( $instance->_parent_plugin ) ) {
4969
+ if ( $this->_plugin->id == $instance->_parent_plugin->id ) {
4970
+ return true;
4971
+ }
4972
+ }
4973
+ }
4974
+
4975
+ return false;
4976
+ }
4977
+
4978
+ /**
4979
+ * Tell Freemius that the current plugin is an add-on.
4980
+ *
4981
+ * @author Vova Feldman (@svovaf)
4982
+ * @since 1.0.6
4983
+ *
4984
+ * @param number $parent_plugin_id The parent plugin ID
4985
+ */
4986
+ function init_addon( $parent_plugin_id ) {
4987
+ $this->_plugin->parent_plugin_id = $parent_plugin_id;
4988
+ }
4989
+
4990
+ /**
4991
+ * @author Vova Feldman (@svovaf)
4992
+ * @since 1.0.6
4993
+ *
4994
+ * @return bool
4995
+ */
4996
+ function is_addon() {
4997
+ return isset( $this->_plugin->parent_plugin_id ) && is_numeric( $this->_plugin->parent_plugin_id );
4998
+ }
4999
+
5000
+ /**
5001
+ * Deactivate add-on if it's premium only and the user does't have a valid license.
5002
+ *
5003
+ * @param bool $is_after_trial_cancel
5004
+ *
5005
+ * @return bool If add-on was deactivated.
5006
+ */
5007
+ private function deactivate_premium_only_addon_without_license( $is_after_trial_cancel = false ) {
5008
+ if ( ! $this->has_free_plan() &&
5009
+ ! $this->has_features_enabled_license() &&
5010
+ ! $this->_has_premium_license()
5011
+ ) {
5012
+ if ( $this->is_registered() ) {
5013
+ // IF wrapper is turned off because activation_timestamp is currently only stored for plugins (not addons).
5014
+ // if (empty($this->_storage->activation_timestamp) ||
5015
+ // (WP_FS__SCRIPT_START_TIME - $this->_storage->activation_timestamp) > 30
5016
+ // ) {
5017
+ /**
5018
+ * @todo When it's first fail, there's no reason to try and re-sync because the licenses were just synced after initial activation.
5019
+ *
5020
+ * Retry syncing the user add-on licenses.
5021
+ */
5022
+ // Sync licenses.
5023
+ $this->_sync_licenses();
5024
+ // }
5025
+
5026
+ // Try to activate premium license.
5027
+ $this->_activate_license( true );
5028
+ }
5029
+
5030
+ if ( ! $this->has_free_plan() &&
5031
+ ! $this->has_features_enabled_license() &&
5032
+ ! $this->_has_premium_license()
5033
+ ) {
5034
+ // @todo Check if deactivate plugins also call the deactivation hook.
5035
+
5036
+ $this->_parent->_admin_notices->add_sticky(
5037
+ sprintf(
5038
+ ( $is_after_trial_cancel ?
5039
+ $this->_parent->get_text_inline(
5040
+ '%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.',
5041
+ 'addon-trial-cancelled-message'
5042
+ ) :
5043
+ $this->_parent->get_text_inline(
5044
+ '%s is a premium only add-on. You have to purchase a license first before activating the plugin.',
5045
+ 'addon-no-license-message'
5046
+ )
5047
+ ),
5048
+ '<b>' . $this->_plugin->title . '</b>'
5049
+ ) . ' ' . sprintf(
5050
+ '<a href="%s" aria-label="%s" class="button button-primary" style="margin-left: 10px; vertical-align: middle;">%s &nbsp;&#10140;</a>',
5051
+ $this->_parent->addon_url( $this->_slug ),
5052
+ esc_attr( sprintf( $this->_parent->get_text_inline( 'More information about %s', 'more-information-about-x' ), $this->_plugin->title ) ),
5053
+ $this->_parent->get_text_inline( 'Purchase License', 'purchase-license' )
5054
+ ),
5055
+ 'no_addon_license_' . $this->_slug,
5056
+ ( $is_after_trial_cancel ? '' : $this->_parent->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...' ),
5057
+ ( $is_after_trial_cancel ? 'success' : 'error' )
5058
+ );
5059
+
5060
+ deactivate_plugins( array( $this->_plugin_basename ), true );
5061
+
5062
+ return true;
5063
+ }
5064
+ }
5065
+
5066
+ return false;
5067
+ }
5068
+
5069
+ #endregion
5070
+
5071
+ #----------------------------------------------------------------------------------
5072
+ #region Sandbox
5073
+ #----------------------------------------------------------------------------------
5074
+
5075
+ /**
5076
+ * Set Freemius into sandbox mode for debugging.
5077
+ *
5078
+ * @author Vova Feldman (@svovaf)
5079
+ * @since 1.0.4
5080
+ *
5081
+ * @param string $secret_key
5082
+ */
5083
+ function init_sandbox( $secret_key ) {
5084
+ $this->_plugin->secret_key = $secret_key;
5085
+
5086
+ // Update plugin details.
5087
+ FS_Plugin_Manager::instance( $this->_module_id )->update( $this->_plugin, true );
5088
+ }
5089
+
5090
+ /**
5091
+ * Check if running payments in sandbox mode.
5092
+ *
5093
+ * @author Vova Feldman (@svovaf)
5094
+ * @since 1.0.4
5095
+ *
5096
+ * @return bool
5097
+ */
5098
+ function is_payments_sandbox() {
5099
+ return ( ! $this->is_live() ) || isset( $this->_plugin->secret_key );
5100
+ }
5101
+
5102
+ #endregion
5103
+
5104
+ /**
5105
+ * Check if running test vs. live plugin.
5106
+ *
5107
+ * @author Vova Feldman (@svovaf)
5108
+ * @since 1.0.5
5109
+ *
5110
+ * @return bool
5111
+ */
5112
+ function is_live() {
5113
+ return $this->_plugin->is_live;
5114
+ }
5115
+
5116
+ /**
5117
+ * Check if super-admin skipped connection for all sites in the network.
5118
+ *
5119
+ * @author Vova Feldman (@svovaf)
5120
+ * @since 2.0.0
5121
+ */
5122
+ function is_network_anonymous() {
5123
+ if ( ! $this->_is_network_active ) {
5124
+ return false;
5125
+ }
5126
+
5127
+ $is_anonymous_ms = $this->_storage->get( 'is_anonymous_ms' );
5128
+
5129
+ if ( empty( $is_anonymous_ms ) ) {
5130
+ return false;
5131
+ }
5132
+
5133
+ return $is_anonymous_ms['is'];
5134
+ }
5135
+
5136
+ /**
5137
+ * Check if super-admin opted-in for all sites in the network.
5138
+ *
5139
+ * @author Vova Feldman (@svovaf)
5140
+ * @since 2.0.0
5141
+ */
5142
+ function is_network_connected() {
5143
+ if ( ! $this->_is_network_active ) {
5144
+ return false;
5145
+ }
5146
+
5147
+ return $this->_storage->get( 'is_network_connected' );
5148
+ }
5149
+
5150
+ /**
5151
+ * Check if the user skipped connecting the account with Freemius.
5152
+ *
5153
+ * @author Vova Feldman (@svovaf)
5154
+ * @since 1.0.7
5155
+ *
5156
+ * @return bool
5157
+ */
5158
+ function is_anonymous() {
5159
+ if ( ! isset( $this->_is_anonymous ) ) {
5160
+ if ( $this->is_network_anonymous() ) {
5161
+ $this->_is_anonymous = true;
5162
+ } else {
5163
+ if ( ! isset( $this->_storage->is_anonymous ) ) {
5164
+ // Not skipped.
5165
+ $this->_is_anonymous = false;
5166
+ } else if ( is_bool( $this->_storage->is_anonymous ) ) {
5167
+ // For back compatibility, since the variable was boolean before.
5168
+ $this->_is_anonymous = $this->_storage->is_anonymous;
5169
+
5170
+ // Upgrade stored data format to 1.1.3 format.
5171
+ $this->set_anonymous_mode( $this->_storage->is_anonymous );
5172
+ } else {
5173
+ // Version 1.1.3 and later.
5174
+ $this->_is_anonymous = $this->_storage->is_anonymous['is'];
5175
+ }
5176
+ }
5177
+ }
5178
+
5179
+ return $this->_is_anonymous;
5180
+ }
5181
+
5182
+ /**
5183
+ * Check if the user skipped the connection of a specified site.
5184
+ *
5185
+ * @author Vova Feldman (@svovaf)
5186
+ * @since 2.0.0
5187
+ *
5188
+ * @param int $blog_id
5189
+ *
5190
+ * @return bool
5191
+ */
5192
+ function is_anonymous_site( $blog_id = 0 ) {
5193
+ if ( $this->is_network_anonymous() ) {
5194
+ return true;
5195
+ }
5196
+
5197
+ $is_anonymous = $this->_storage->get( 'is_anonymous', false, $blog_id );
5198
+
5199
+ if ( empty( $is_anonymous ) ) {
5200
+ return false;
5201
+ }
5202
+
5203
+ return $is_anonymous['is'];
5204
+ }
5205
+
5206
+ /**
5207
+ * Check if user connected his account and install pending email activation.
5208
+ *
5209
+ * @author Vova Feldman (@svovaf)
5210
+ * @since 1.0.7
5211
+ *
5212
+ * @return bool
5213
+ */
5214
+ function is_pending_activation() {
5215
+ return $this->_storage->get( 'is_pending_activation', false );
5216
+ }
5217
+
5218
+ /**
5219
+ * Check if plugin must be WordPress.org compliant.
5220
+ *
5221
+ * @since 1.0.7
5222
+ *
5223
+ * @return bool
5224
+ */
5225
+ function is_org_repo_compliant() {
5226
+ return $this->_is_org_compliant;
5227
+ }
5228
+
5229
+ #--------------------------------------------------------------------------------
5230
+ #region WP Cron Common
5231
+ #--------------------------------------------------------------------------------
5232
+
5233
+ /**
5234
+ * @author Vova Feldman (@svovaf)
5235
+ * @since 2.0.0
5236
+ *
5237
+ * @param string $name Cron name.
5238
+ *
5239
+ * @return object
5240
+ */
5241
+ private function get_cron_data( $name ) {
5242
+ $this->_logger->entrance( $name );
5243
+
5244
+ /**
5245
+ * @var object $cron_data
5246
+ */
5247
+ return $this->_storage->get( "{$name}_cron", null );
5248
+ }
5249
+
5250
+ /**
5251
+ * @author Vova Feldman (@svovaf)
5252
+ * @since 2.0.0
5253
+ *
5254
+ * @param string $name Cron name.
5255
+ */
5256
+ private function clear_cron_data( $name ) {
5257
+ $this->_logger->entrance( $name );
5258
+
5259
+ $this->_storage->remove( "{$name}_cron" );
5260
+ }
5261
+
5262
+ /**
5263
+ * @author Vova Feldman (@svovaf)
5264
+ * @since 2.0.0
5265
+ *
5266
+ * @param string $name Cron name.
5267
+ * @param int $cron_blog_id The cron executing blog ID.
5268
+ */
5269
+ private function set_cron_data( $name, $cron_blog_id = 0 ) {
5270
+ $this->_logger->entrance( $name );
5271
+
5272
+ $this->_storage->store( "{$name}_cron", (object) array(
5273
+ 'version' => $this->get_plugin_version(),
5274
+ 'blog_id' => $cron_blog_id,
5275
+ 'sdk_version' => $this->version,
5276
+ 'timestamp' => WP_FS__SCRIPT_START_TIME,
5277
+ 'on' => true,
5278
+ ) );
5279
+ }
5280
+
5281
+ /**
5282
+ * Get the cron's executing blog ID.
5283
+ *
5284
+ * @author Vova Feldman (@svovaf)
5285
+ * @since 2.0.0
5286
+ *
5287
+ * @param string $name Cron name.
5288
+ *
5289
+ * @return int
5290
+ */
5291
+ private function get_cron_blog_id( $name ) {
5292
+ $this->_logger->entrance( $name );
5293
+
5294
+ /**
5295
+ * @var object $cron_data
5296
+ */
5297
+ $cron_data = $this->get_cron_data( $name );
5298
+
5299
+ return ( is_object( $cron_data ) && is_numeric( $cron_data->blog_id ) ) ?
5300
+ $cron_data->blog_id :
5301
+ 0;
5302
+ }
5303
+
5304
+ /**
5305
+ * @author Vova Feldman (@svovaf)
5306
+ * @since 2.0.0
5307
+ *
5308
+ * @param string $name Cron name.
5309
+ *
5310
+ * @return bool
5311
+ */
5312
+ private function is_cron_on( $name ) {
5313
+ $this->_logger->entrance( $name );
5314
+
5315
+ /**
5316
+ * @var object $cron_data
5317
+ */
5318
+ $cron_data = $this->get_cron_data( $name );
5319
+
5320
+ return ( ! is_null( $cron_data ) && true === $cron_data->on );
5321
+ }
5322
+
5323
+ /**
5324
+ * Unix timestamp for previous cron execution or false if never executed.
5325
+ *
5326
+ * @author Vova Feldman (@svovaf)
5327
+ * @since 2.0.0
5328
+ *
5329
+ * @param string $name Cron name.
5330
+ *
5331
+ * @return int|false
5332
+ */
5333
+ private function cron_last_execution( $name ) {
5334
+ $this->_logger->entrance( $name );
5335
+
5336
+ return $this->_storage->get( "{$name}_timestamp" );
5337
+ }
5338
+
5339
+ /**
5340
+ * Set cron execution time to now.
5341
+ *
5342
+ * @author Vova Feldman (@svovaf)
5343
+ * @since 2.0.0
5344
+ *
5345
+ * @param string $name Cron name.
5346
+ */
5347
+ private function set_cron_execution_timestamp( $name ) {
5348
+ $this->_logger->entrance( $name );
5349
+
5350
+ $this->_storage->store( "{$name}_timestamp", time() );
5351
+ }
5352
+
5353
+ /**
5354
+ * Check if cron was executed in the last $period of seconds.
5355
+ *
5356
+ * @author Vova Feldman (@svovaf)
5357
+ * @since 2.0.0
5358
+ *
5359
+ * @param string $name Cron name.
5360
+ * @param int $period In seconds
5361
+ *
5362
+ * @return bool
5363
+ */
5364
+ private function is_cron_executed( $name, $period = WP_FS__TIME_24_HOURS_IN_SEC ) {
5365
+ $this->_logger->entrance( $name );
5366
+
5367
+ $last_execution = $this->set_cron_execution_timestamp( $name );
5368
+
5369
+ if ( ! is_numeric( $last_execution ) ) {
5370
+ return false;
5371
+ }
5372
+
5373
+ return ( $last_execution > ( WP_FS__SCRIPT_START_TIME - $period ) );
5374
+ }
5375
+
5376
+ /**
5377
+ * WP Cron is executed on a site level. When running in a multisite network environment
5378
+ * with the network integration activated, for optimization reasons, we are consolidating
5379
+ * the installs data sync cron to be executed only from a single site.
5380
+ *
5381
+ * @author Vova Feldman (@svovaf)
5382
+ * @since 2.0.0
5383
+ *
5384
+ * @param int $except_blog_id Target any except the excluded blog ID.
5385
+ *
5386
+ * @return int
5387
+ */
5388
+ private function get_cron_target_blog_id( $except_blog_id = 0 ) {
5389
+ if ( ! is_multisite() ) {
5390
+ return 0;
5391
+ }
5392
+
5393
+ if ( $this->_is_network_active &&
5394
+ is_numeric( $this->_storage->network_install_blog_id ) &&
5395
+ $except_blog_id != $this->_storage->network_install_blog_id &&
5396
+ self::is_site_active( $this->_storage->network_install_blog_id )
5397
+ ) {
5398
+ // Try to run cron from the main network blog.
5399
+ $install = $this->get_install_by_blog_id( $this->_storage->network_install_blog_id );
5400
+
5401
+ if ( is_object( $install ) &&
5402
+ ( $this->is_premium() || $install->is_tracking_allowed() )
5403
+ ) {
5404
+ return $this->_storage->network_install_blog_id;
5405
+ }
5406
+ }
5407
+
5408
+ // Get first opted-in blog ID with active tracking.
5409
+ $installs = $this->get_blog_install_map();
5410
+ foreach ( $installs as $blog_id => $install ) {
5411
+ if ( $except_blog_id != $blog_id &&
5412
+ self::is_site_active( $blog_id ) &&
5413
+ ( $this->is_premium() || $install->is_tracking_allowed() )
5414
+ ) {
5415
+ return $blog_id;
5416
+ }
5417
+ }
5418
+
5419
+ return 0;
5420
+ }
5421
+
5422
+ /**
5423
+ * @author Vova Feldman (@svovaf)
5424
+ * @since 2.0.0
5425
+ *
5426
+ * @param string $name Cron name.
5427
+ * @param string $action_tag Callback action tag.
5428
+ * @param bool $is_network_clear If set to TRUE, clear sync cron even if there are installs that are still connected.
5429
+ */
5430
+ private function clear_cron( $name, $action_tag = '', $is_network_clear = false ) {
5431
+ $this->_logger->entrance( $name );
5432
+
5433
+ if ( ! $this->is_cron_on( $name ) ) {
5434
+ return;
5435
+ }
5436
+
5437
+ $clear_cron = true;
5438
+ if ( ! $is_network_clear && $this->_is_network_active ) {
5439
+ $installs = $this->get_blog_install_map();
5440
+
5441
+ foreach ( $installs as $blog_id => $install ) {
5442
+ /**
5443
+ * @var FS_Site $install
5444
+ */
5445
+ if ( $install->is_tracking_allowed() ) {
5446
+ $clear_cron = false;
5447
+ break;
5448
+ }
5449
+ }
5450
+ }
5451
+
5452
+ if ( ! $clear_cron ) {
5453
+ return;
5454
+ }
5455
+
5456
+ /**
5457
+ * @var object $cron_data
5458
+ */
5459
+ $cron_data = $this->get_cron_data( $name );
5460
+
5461
+ $cron_blog_id = is_object( $cron_data ) && isset( $cron_data->blog_id ) ?
5462
+ $cron_data->blog_id :
5463
+ 0;
5464
+
5465
+ $this->clear_cron_data( $name );
5466
+
5467
+ if ( 0 < $cron_blog_id ) {
5468
+ switch_to_blog( $cron_blog_id );
5469
+ }
5470
+
5471
+ if ( empty( $action_tag ) ) {
5472
+ $action_tag = $name;
5473
+ }
5474
+
5475
+ wp_clear_scheduled_hook( $this->get_action_tag( $action_tag ) );
5476
+
5477
+ if ( 0 < $cron_blog_id ) {
5478
+ restore_current_blog();
5479
+ }
5480
+ }
5481
+
5482
+ /**
5483
+ * Unix timestamp for next cron execution or false if not scheduled.
5484
+ *
5485
+ * @author Vova Feldman (@svovaf)
5486
+ * @since 2.0.0
5487
+ *
5488
+ * @param string $name Cron name.
5489
+ * @param string $action_tag Callback action tag.
5490
+ *
5491
+ * @return int|false
5492
+ */
5493
+ private function get_next_scheduled_cron( $name, $action_tag = '' ) {
5494
+ $this->_logger->entrance( $name );
5495
+
5496
+ if ( ! $this->is_cron_on( $name ) ) {
5497
+ return false;
5498
+ }
5499
+
5500
+ /**
5501
+ * @var object $cron_data
5502
+ */
5503
+ $cron_data = $this->get_cron_data( $name );
5504
+
5505
+ $cron_blog_id = is_object( $cron_data ) && isset( $cron_data->blog_id ) ?
5506
+ $cron_data->blog_id :
5507
+ 0;
5508
+
5509
+ if ( 0 < $cron_blog_id ) {
5510
+ switch_to_blog( $cron_blog_id );
5511
+ }
5512
+
5513
+ if ( empty( $action_tag ) ) {
5514
+ $action_tag = $name;
5515
+ }
5516
+
5517
+ $next_scheduled = wp_next_scheduled( $this->get_action_tag( $action_tag ) );
5518
+
5519
+ if ( 0 < $cron_blog_id ) {
5520
+ restore_current_blog();
5521
+ }
5522
+
5523
+ return $next_scheduled;
5524
+ }
5525
+
5526
+ /**
5527
+ * @author Vova Feldman (@svovaf)
5528
+ * @since 2.0.0
5529
+ *
5530
+ * @param string $name Cron name.
5531
+ * @param string $action_tag Callback action tag.
5532
+ * @param string $recurrence 'single' or 'daily'.
5533
+ * @param int $start_at Defaults to now.
5534
+ * @param bool $randomize_start If true, schedule first job randomly during the next 12 hours. Otherwise, schedule job to start right away.
5535
+ * @param int $except_blog_id Target any except the excluded blog ID.
5536
+ */
5537
+ private function schedule_cron(
5538
+ $name,
5539
+ $action_tag = '',
5540
+ $recurrence = 'single',
5541
+ $start_at = WP_FS__SCRIPT_START_TIME,
5542
+ $randomize_start = true,
5543
+ $except_blog_id = 0
5544
+ ) {
5545
+ $this->_logger->entrance( $name );
5546
+
5547
+ $this->clear_cron( $name, $action_tag, true );
5548
+
5549
+ $cron_blog_id = $this->get_cron_target_blog_id( $except_blog_id );
5550
+
5551
+ if ( is_multisite() && 0 == $cron_blog_id ) {
5552
+ // Don't schedule cron since couldn't find a target blog.
5553
+ return;
5554
+ }
5555
+
5556
+ if ( 0 < $cron_blog_id ) {
5557
+ switch_to_blog( $cron_blog_id );
5558
+ }
5559
+
5560
+ if ( 'daily' === $recurrence ) {
5561
+ if ( $randomize_start ) {
5562
+ // Schedule first sync with a random 12 hour time range from now.
5563
+ $start_at += rand( 0, ( WP_FS__TIME_24_HOURS_IN_SEC / 2 ) );
5564
+ }
5565
+
5566
+ // Schedule daily WP cron.
5567
+ wp_schedule_event(
5568
+ $start_at,
5569
+ 'daily',
5570
+ $this->get_action_tag( $action_tag )
5571
+ );
5572
+ } else if ( 'single' === $recurrence ) {
5573
+ // Schedule single cron.
5574
+ wp_schedule_single_event(
5575
+ $start_at,
5576
+ $this->get_action_tag( $action_tag )
5577
+ );
5578
+ }
5579
+
5580
+ $this->set_cron_data( $name, $cron_blog_id );
5581
+
5582
+ if ( 0 < $cron_blog_id ) {
5583
+ restore_current_blog();
5584
+ }
5585
+ }
5586
+
5587
+ /**
5588
+ * Consolidated cron execution for performance optimization. The max number of API requests is based on the number of unique opted-in users.
5589
+ * that doesn't halt page loading.
5590
+ *
5591
+ * @author Vova Feldman (@svovaf)
5592
+ * @since 2.0.0
5593
+ *
5594
+ * @param string $name Cron name.
5595
+ * @param callable $callable The function that should be executed.
5596
+ */
5597
+ private function execute_cron( $name, $callable ) {
5598
+ $this->_logger->entrance( $name );
5599
+
5600
+ // Store the last time data sync was executed.
5601
+ $this->set_cron_execution_timestamp( $name );
5602
+
5603
+ // Check if API is temporary down.
5604
+ if ( FS_Api::is_temporary_down() ) {
5605
+ return;
5606
+ }
5607
+
5608
+ // @todo Add logic that identifies API latency, and reschedule the next background sync randomly between 8-16 hours.
5609
+
5610
+ $users_2_blog_ids = array();
5611
+
5612
+ if ( ! is_multisite() ) {
5613
+ // Add dummy blog.
5614
+ $users_2_blog_ids[0] = array( 0 );
5615
+ } else {
5616
+ $installs = $this->get_blog_install_map();
5617
+ foreach ( $installs as $blog_id => $install ) {
5618
+ if ( $this->is_premium() || $install->is_tracking_allowed() ) {
5619
+ if ( ! isset( $users_2_blog_ids[ $install->user_id ] ) ) {
5620
+ $users_2_blog_ids[ $install->user_id ] = array();
5621
+ }
5622
+
5623
+ $users_2_blog_ids[ $install->user_id ][] = $blog_id;
5624
+ }
5625
+ }
5626
+ }
5627
+
5628
+ foreach ( $users_2_blog_ids as $user_id => $blog_ids ) {
5629
+ if ( 0 < $blog_ids[0] ) {
5630
+ $this->switch_to_blog( $blog_ids[0] );
5631
+ }
5632
+
5633
+ call_user_func_array( $callable, array( $blog_ids ) );
5634
+
5635
+ foreach ( $blog_ids as $blog_id ) {
5636
+ $this->do_action( "after_{$name}_cron", $blog_id );
5637
+ }
5638
+ }
5639
+
5640
+ if ( is_multisite() ) {
5641
+ $this->do_action( "after_{$name}_cron_multisite" );
5642
+ }
5643
+ }
5644
+
5645
+ #endregion
5646
+
5647
+ #----------------------------------------------------------------------------------
5648
+ #region Daily Sync Cron
5649
+ #----------------------------------------------------------------------------------
5650
+
5651
+
5652
+ /**
5653
+ * @author Vova Feldman (@svovaf)
5654
+ * @since 2.0.0
5655
+ *
5656
+ * @return bool
5657
+ */
5658
+ private function is_sync_cron_scheduled() {
5659
+ return $this->is_cron_on( 'sync' );
5660
+ }
5661
+
5662
+ /**
5663
+ * Get the sync cron's executing blog ID.
5664
+ *
5665
+ * @author Vova Feldman (@svovaf)
5666
+ * @since 2.0.0
5667
+ *
5668
+ * @return int
5669
+ */
5670
+ private function get_sync_cron_blog_id() {
5671
+ return $this->get_cron_blog_id( 'sync' );
5672
+ }
5673
+
5674
+ /**
5675
+ * @author Vova Feldman (@svovaf)
5676
+ * @since 1.1.7.3
5677
+ */
5678
+ private function run_manual_sync() {
5679
+ self::require_pluggable_essentials();
5680
+
5681
+ if ( ! $this->is_user_admin() ) {
5682
+ return;
5683
+ }
5684
+
5685
+ // Run manual sync.
5686
+ $this->_sync_cron();
5687
+
5688
+ // Reschedule next cron to run 24 hours from now (performance optimization).
5689
+ $this->schedule_sync_cron( time() + WP_FS__TIME_24_HOURS_IN_SEC, false );
5690
+ }
5691
+
5692
+ /**
5693
+ * Data sync cron job. Replaces the background sync non blocking HTTP request
5694
+ * that doesn't halt page loading.
5695
+ *
5696
+ * @author Vova Feldman (@svovaf)
5697
+ * @since 1.1.7.3
5698
+ * @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.
5699
+ */
5700
+ function _sync_cron() {
5701
+ $this->_logger->entrance();
5702
+
5703
+ $this->execute_cron( 'sync', array( &$this, '_sync_cron_method' ) );
5704
+ }
5705
+
5706
+ /**
5707
+ * The actual data sync cron logic.
5708
+ *
5709
+ * @author Vova Feldman (@svovaf)
5710
+ * @since 2.0.0
5711
+ *
5712
+ * @param int[] $blog_ids
5713
+ */
5714
+ function _sync_cron_method( array $blog_ids ) {
5715
+ if ( $this->is_registered() ) {
5716
+ if ( $this->has_paid_plan() ) {
5717
+ // Initiate background plan sync.
5718
+ $this->_sync_license( true );
5719
+
5720
+ if ( $this->is_paying() ) {
5721
+ // Check for premium plugin updates.
5722
+ $this->check_updates( true );
5723
+ }
5724
+ } else {
5725
+ // Sync install(s) (only if something changed locally).
5726
+ if ( 1 < count( $blog_ids ) ) {
5727
+ $this->sync_installs();
5728
+ } else {
5729
+ $this->sync_install();
5730
+ }
5731
+ }
5732
+ }
5733
+ }
5734
+
5735
+ /**
5736
+ * Check if sync was executed in the last $period of seconds.
5737
+ *
5738
+ * @author Vova Feldman (@svovaf)
5739
+ * @since 1.1.7.3
5740
+ *
5741
+ * @param int $period In seconds
5742
+ *
5743
+ * @return bool
5744
+ */
5745
+ private function is_sync_executed( $period = WP_FS__TIME_24_HOURS_IN_SEC ) {
5746
+ return $this->is_cron_executed( 'sync', $period );
5747
+ }
5748
+
5749
+ /**
5750
+ * @author Vova Feldman (@svovaf)
5751
+ * @since 1.1.7.3
5752
+ *
5753
+ * @return bool
5754
+ */
5755
+ private function is_sync_cron_on() {
5756
+ return $this->is_cron_on( 'sync' );
5757
+ }
5758
+
5759
+ /**
5760
+ * @author Vova Feldman (@svovaf)
5761
+ * @since 1.1.7.3
5762
+ *
5763
+ * @param int $start_at Defaults to now.
5764
+ * @param bool $randomize_start If true, schedule first job randomly during the next 12 hours. Otherwise, schedule job to start right away.
5765
+ * @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.
5766
+ */
5767
+ private function schedule_sync_cron(
5768
+ $start_at = WP_FS__SCRIPT_START_TIME,
5769
+ $randomize_start = true,
5770
+ $except_blog_id = 0
5771
+ ) {
5772
+ $this->schedule_cron(
5773
+ 'sync',
5774
+ 'data_sync',
5775
+ 'daily',
5776
+ $start_at,
5777
+ $randomize_start,
5778
+ $except_blog_id
5779
+ );
5780
+ }
5781
+
5782
+ /**
5783
+ * Add the actual sync function to the cron job hook.
5784
+ *
5785
+ * @author Vova Feldman (@svovaf)
5786
+ * @since 1.1.7.3
5787
+ */
5788
+ private function hook_callback_to_sync_cron() {
5789
+ $this->add_action( 'data_sync', array( &$this, '_sync_cron' ) );
5790
+ }
5791
+
5792
+ /**
5793
+ * @author Vova Feldman (@svovaf)
5794
+ * @since 1.1.7.3
5795
+ *
5796
+ * @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.
5797
+ */
5798
+ private function clear_sync_cron( $is_network_clear = false ) {
5799
+ $this->_logger->entrance();
5800
+
5801
+ $this->clear_cron( 'sync', 'data_sync', $is_network_clear );
5802
+ }
5803
+
5804
+ /**
5805
+ * Unix timestamp for next sync cron execution or false if not scheduled.
5806
+ *
5807
+ * @author Vova Feldman (@svovaf)
5808
+ * @since 1.1.7.3
5809
+ *
5810
+ * @return int|false
5811
+ */
5812
+ function next_sync_cron() {
5813
+ return $this->get_next_scheduled_cron( 'sync', 'data_sync' );
5814
+ }
5815
+
5816
+ /**
5817
+ * Unix timestamp for previous sync cron execution or false if never executed.
5818
+ *
5819
+ * @author Vova Feldman (@svovaf)
5820
+ * @since 1.1.7.3
5821
+ *
5822
+ * @return int|false
5823
+ */
5824
+ function last_sync_cron() {
5825
+ return $this->cron_last_execution( 'sync' );
5826
+ }
5827
+
5828
+ #endregion Daily Sync Cron ------------------------------------------------------------------
5829
+
5830
+ #----------------------------------------------------------------------------------
5831
+ #region Async Install Sync
5832
+ #----------------------------------------------------------------------------------
5833
+
5834
+ /**
5835
+ * @author Vova Feldman (@svovaf)
5836
+ * @since 1.1.7.3
5837
+ *
5838
+ * @return bool
5839
+ */
5840
+ private function is_install_sync_scheduled() {
5841
+ return $this->is_cron_on( 'install_sync' );
5842
+ }
5843
+
5844
+ /**
5845
+ * Get the sync cron's executing blog ID.
5846
+ *
5847
+ * @author Vova Feldman (@svovaf)
5848
+ * @since 2.0.0
5849
+ *
5850
+ * @return int
5851
+ */
5852
+ private function get_install_sync_cron_blog_id() {
5853
+ return $this->get_cron_blog_id( 'install_sync' );
5854
+ }
5855
+
5856
+ /**
5857
+ * Instead of running blocking install sync event, execute non blocking scheduled wp-cron.
5858
+ *
5859
+ * @author Vova Feldman (@svovaf)
5860
+ * @since 1.1.7.3
5861
+ *
5862
+ * @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.
5863
+ */
5864
+ private function schedule_install_sync( $except_blog_id = 0 ) {
5865
+ $this->schedule_cron( 'install_sync', 'install_sync', 'single', 0, false, $except_blog_id );
5866
+ }
5867
+
5868
+ /**
5869
+ * Unix timestamp for previous install sync cron execution or false if never executed.
5870
+ *
5871
+ * @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.
5872
+ *
5873
+ * @author Vova Feldman (@svovaf)
5874
+ * @since 1.1.7.3
5875
+ *
5876
+ * @return int|false
5877
+ */
5878
+ function last_install_sync() {
5879
+ return $this->cron_last_execution( 'install_sync' );
5880
+ }
5881
+
5882
+ /**
5883
+ * Unix timestamp for next install sync cron execution or false if not scheduled.
5884
+ *
5885
+ * @author Vova Feldman (@svovaf)
5886
+ * @since 1.1.7.3
5887
+ *
5888
+ * @return int|false
5889
+ */
5890
+ function next_install_sync() {
5891
+ return $this->get_next_scheduled_cron( 'install_sync', 'install_sync' );
5892
+ }
5893
+
5894
+ /**
5895
+ * Add the actual install sync function to the cron job hook.
5896
+ *
5897
+ * @author Vova Feldman (@svovaf)
5898
+ * @since 1.1.7.3
5899
+ */
5900
+ private function hook_callback_to_install_sync() {
5901
+ $this->add_action( 'install_sync', array( &$this, '_run_sync_install' ) );
5902
+ }
5903
+
5904
+ /**
5905
+ * @author Vova Feldman (@svovaf)
5906
+ * @since 1.1.7.3
5907
+ *
5908
+ * @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.
5909
+ */
5910
+ private function clear_install_sync_cron( $is_network_clear = false ) {
5911
+ $this->_logger->entrance();
5912
+
5913
+ $this->clear_cron( 'install_sync', 'install_sync', $is_network_clear );
5914
+ }
5915
+
5916
+ /**
5917
+ * @author Vova Feldman (@svovaf)
5918
+ * @since 1.1.7.3
5919
+ * @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.
5920
+ */
5921
+ public function _run_sync_install() {
5922
+ $this->_logger->entrance();
5923
+
5924
+ $this->execute_cron( 'sync', array( &$this, '_sync_install_cron_method' ) );
5925
+ }
5926
+
5927
+ /**
5928
+ * The actual install(s) sync cron logic.
5929
+ *
5930
+ * @author Vova Feldman (@svovaf)
5931
+ * @since 2.0.0
5932
+ *
5933
+ * @param int[] $blog_ids
5934
+ */
5935
+ function _sync_install_cron_method( array $blog_ids ) {
5936
+ if ( $this->is_registered() ) {
5937
+ if ( 1 < count( $blog_ids ) ) {
5938
+ $this->sync_installs( array(), true );
5939
+ } else {
5940
+ $this->sync_install( array(), true );
5941
+ }
5942
+ }
5943
+ }
5944
+
5945
+ #endregion Async Install Sync ------------------------------------------------------------------
5946
+
5947
+ /**
5948
+ * Show a notice that activation is currently pending.
5949
+ *
5950
+ * @author Vova Feldman (@svovaf)
5951
+ * @since 1.0.7
5952
+ *
5953
+ * @param bool|string $email
5954
+ * @param bool $is_pending_trial Since 1.2.1.5
5955
+ */
5956
+ function _add_pending_activation_notice( $email = false, $is_pending_trial = false ) {
5957
+ if ( ! is_string( $email ) ) {
5958
+ $current_user = self::_get_current_wp_user();
5959
+ $email = $current_user->user_email;
5960
+ }
5961
+
5962
+ $this->_admin_notices->add_sticky(
5963
+ sprintf(
5964
+ $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' ),
5965
+ '<b>' . $this->get_plugin_name() . '</b>',
5966
+ '<b>' . $email . '</b>',
5967
+ ( $is_pending_trial ?
5968
+ $this->get_text_inline( 'start the trial', 'start-the-trial' ) :
5969
+ $this->get_text_inline( 'complete the install', 'complete-the-install' ) )
5970
+ ),
5971
+ 'activation_pending',
5972
+ 'Thanks!'
5973
+ );
5974
+ }
5975
+
5976
+ /**
5977
+ * Check if currently in plugin activation.
5978
+ *
5979
+ * @author Vova Feldman (@svovaf)
5980
+ * @since 1.1.4
5981
+ *
5982
+ * @return bool
5983
+ */
5984
+ function is_plugin_activation() {
5985
+ return get_option( 'fs_'
5986
+ . ( $this->is_plugin() ? '' : $this->_module_type . '_' )
5987
+ . "{$this->_slug}_activated", false );
5988
+ }
5989
+
5990
+ /**
5991
+ *
5992
+ * NOTE: admin_menu action executed before admin_init.
5993
+ *
5994
+ * @author Vova Feldman (@svovaf)
5995
+ * @since 1.0.7
5996
+ */
5997
+ function _admin_init_action() {
5998
+ /**
5999
+ * Automatically redirect to connect/activation page after plugin activation.
6000
+ *
6001
+ * @since 1.1.7 Do NOT redirect to opt-in when running in network admin mode.
6002
+ */
6003
+ if ( $this->is_plugin_activation() ) {
6004
+ delete_option( 'fs_'
6005
+ . ( $this->is_plugin() ? '' : $this->_module_type . '_' )
6006
+ . "{$this->_slug}_activated" );
6007
+
6008
+ $this->_redirect_on_activation_hook();
6009
+
6010
+ return;
6011
+ }
6012
+
6013
+ if ( fs_request_is_action( $this->get_unique_affix() . '_skip_activation' ) ) {
6014
+ check_admin_referer( $this->get_unique_affix() . '_skip_activation' );
6015
+
6016
+ $this->skip_connection( null, fs_is_network_admin() );
6017
+
6018
+ fs_redirect( $this->get_after_activation_url( 'after_skip_url' ) );
6019
+ }
6020
+
6021
+ if ( $this->is_network_activation_mode() &&
6022
+ fs_request_is_action( $this->get_unique_affix() . '_delegate_activation' )
6023
+ ) {
6024
+ check_admin_referer( $this->get_unique_affix() . '_delegate_activation' );
6025
+
6026
+ $this->delegate_connection();
6027
+
6028
+ fs_redirect( $this->get_after_activation_url( 'after_delegation_url' ) );
6029
+ }
6030
+
6031
+ if ( ! $this->is_addon() &&
6032
+ (
6033
+ // Not registered nor anonymous.
6034
+ ( ! $this->is_registered() && ! $this->is_anonymous() ) ||
6035
+ // OR, network level and in network upgrade mode.
6036
+ ( fs_is_network_admin() && $this->_is_network_active && $this->is_network_upgrade_mode() )
6037
+ )
6038
+ ) {
6039
+ if ( ! $this->is_pending_activation() ) {
6040
+ if ( ! $this->_menu->is_main_settings_page() ) {
6041
+ /**
6042
+ * If a user visits any other admin page before activating the premium-only theme with a valid
6043
+ * license, reactivate the previous theme.
6044
+ *
6045
+ * @author Leo Fajardo (@leorw)
6046
+ * @since 1.2.2
6047
+ */
6048
+ if ( $this->is_theme()
6049
+ && $this->is_only_premium()
6050
+ && ! $this->has_settings_menu()
6051
+ && ! isset( $_REQUEST['fs_action'] )
6052
+ && $this->can_activate_previous_theme()
6053
+ ) {
6054
+ $this->activate_previous_theme();
6055
+
6056
+ return;
6057
+ }
6058
+
6059
+ if ( ! fs_is_network_admin() &&
6060
+ $this->is_network_activation_mode() &&
6061
+ ! $this->is_delegated_connection()
6062
+ ) {
6063
+ return;
6064
+ }
6065
+
6066
+ if ( $this->is_plugin_new_install() || $this->is_only_premium() ) {
6067
+ // Show notice for new plugin installations.
6068
+ $this->_admin_notices->add(
6069
+ sprintf(
6070
+ $this->get_text_inline( 'You are just one step away - %s', 'you-are-step-away' ),
6071
+ sprintf( '<b><a href="%s">%s</a></b>',
6072
+ $this->get_activation_url( array(), ! $this->is_delegated_connection() ),
6073
+ sprintf( $this->get_text_x_inline( 'Complete "%s" Activation Now',
6074
+ '%s - plugin name. As complete "PluginX" activation now', 'activate-x-now' ), $this->get_plugin_name() )
6075
+ )
6076
+ ),
6077
+ '',
6078
+ 'update-nag'
6079
+ );
6080
+ } else {
6081
+ if ( $this->should_add_sticky_optin_notice() ) {
6082
+ $this->add_sticky_optin_admin_notice();
6083
+ }
6084
+
6085
+ if ( $this->has_filter( 'optin_pointer_element' ) ) {
6086
+ // Don't show admin nag if plugin update.
6087
+ wp_enqueue_script( 'wp-pointer' );
6088
+ wp_enqueue_style( 'wp-pointer' );
6089
+
6090
+ $this->_enqueue_connect_essentials();
6091
+
6092
+ add_action( 'admin_print_footer_scripts', array(
6093
+ $this,
6094
+ '_add_connect_pointer_script'
6095
+ ) );
6096
+ }
6097
+ }
6098
+ }
6099
+ }
6100
+
6101
+ if ( $this->is_theme() &&
6102
+ $this->_menu->is_main_settings_page()
6103
+ ) {
6104
+ $this->_show_theme_activation_optin_dialog();
6105
+ }
6106
+ }
6107
+
6108
+ $this->_add_upgrade_action_link();
6109
+ }
6110
+
6111
+ /**
6112
+ * @author Vova Feldman (@svovaf)
6113
+ * @since 2.0.0
6114
+ *
6115
+ * @return bool
6116
+ */
6117
+ private function should_add_sticky_optin_notice() {
6118
+ if ( fs_is_network_admin() ) {
6119
+ if ( ! $this->_is_network_active ) {
6120
+ return false;
6121
+ }
6122
+
6123
+ if ( ! $this->is_network_activation_mode() ) {
6124
+ return false;
6125
+ }
6126
+
6127
+ return ! isset( $this->_storage->sticky_optin_added_ms );
6128
+ }
6129
+
6130
+ if ( ! $this->is_activation_mode() ) {
6131
+ return false;
6132
+ }
6133
+
6134
+ // If running from a blog admin and delegated the connection.
6135
+ return ! isset( $this->_storage->sticky_optin_added );
6136
+ }
6137
+
6138
+ /**
6139
+ * @author Leo Fajardo (@leorw)
6140
+ * @since 2.0.0
6141
+ */
6142
+ private function add_sticky_optin_admin_notice() {
6143
+ if ( ! $this->_is_network_active || ! fs_is_network_admin() ) {
6144
+ $this->_storage->sticky_optin_added = true;
6145
+ } else {
6146
+ $this->_storage->sticky_optin_added_ms = true;
6147
+ }
6148
+
6149
+ // Show notice for new plugin installations.
6150
+ $this->_admin_notices->add_sticky(
6151
+ sprintf(
6152
+ $this->get_text_inline( 'We made a few tweaks to the %s, %s', 'few-plugin-tweaks' ),
6153
+ $this->_module_type,
6154
+ sprintf( '<b><a href="%s">%s</a></b>',
6155
+ $this->get_activation_url(),
6156
+ sprintf( $this->get_text_inline( 'Opt in to make "%s" Better!', 'optin-x-now' ), $this->get_plugin_name() )
6157
+ )
6158
+ ),
6159
+ 'connect_account',
6160
+ '',
6161
+ 'update-nag'
6162
+ );
6163
+ }
6164
+
6165
+ /**
6166
+ * Enqueue connect requires scripts and styles.
6167
+ *
6168
+ * @author Vova Feldman (@svovaf)
6169
+ * @since 1.1.4
6170
+ */
6171
+ function _enqueue_connect_essentials() {
6172
+ wp_enqueue_script( 'jquery' );
6173
+ wp_enqueue_script( 'json2' );
6174
+
6175
+ fs_enqueue_local_script( 'postmessage', 'nojquery.ba-postmessage.min.js' );
6176
+ fs_enqueue_local_script( 'fs-postmessage', 'postmessage.js' );
6177
+
6178
+ fs_enqueue_local_style( 'fs_connect', '/admin/connect.css' );
6179
+ }
6180
+
6181
+ /**
6182
+ * Add connect / opt-in pointer.
6183
+ *
6184
+ * @author Vova Feldman (@svovaf)
6185
+ * @since 1.1.4
6186
+ */
6187
+ function _add_connect_pointer_script() {
6188
+ $vars = array( 'id' => $this->_module_id );
6189
+ $pointer_content = fs_get_template( 'connect.php', $vars );
6190
+ ?>
6191
+ <script type="text/javascript">// <![CDATA[
6192
+ jQuery(document).ready(function ($) {
6193
+ if ('undefined' !== typeof(jQuery().pointer)) {
6194
+
6195
+ var element = <?php echo $this->apply_filters( 'optin_pointer_element', '$("#non_existing_element");' ) ?>;
6196
+
6197
+ if (element.length > 0) {
6198
+ var optin = $(element).pointer($.extend(true, {}, {
6199
+ content : <?php echo json_encode( $pointer_content ) ?>,
6200
+ position : {
6201
+ edge : 'left',
6202
+ align: 'center'
6203
+ },
6204
+ buttons : function () {
6205
+ // Don't show pointer buttons.
6206
+ return '';
6207
+ },
6208
+ pointerWidth: 482
6209
+ }, <?php echo $this->apply_filters( 'optin_pointer_options_json', '{}' ) ?>));
6210
+
6211
+ <?php
6212
+ echo $this->apply_filters( 'optin_pointer_execute', "
6213
+
6214
+ optin.pointer('open');
6215
+
6216
+ // Tag the opt-in pointer with custom class.
6217
+ $('.wp-pointer #fs_connect')
6218
+ .parents('.wp-pointer.wp-pointer-top')
6219
+ .addClass('fs-opt-in-pointer');
6220
+
6221
+ ", 'element', 'optin' ) ?>
6222
+ }
6223
+ }
6224
+ });
6225
+ // ]]></script>
6226
+ <?php
6227
+ }
6228
+
6229
+ /**
6230
+ * Return current page's URL.
6231
+ *
6232
+ * @author Vova Feldman (@svovaf)
6233
+ * @since 1.0.7
6234
+ *
6235
+ * @return string
6236
+ */
6237
+ function current_page_url() {
6238
+ $url = 'http';
6239
+
6240
+ if ( isset( $_SERVER["HTTPS"] ) ) {
6241
+ if ( $_SERVER["HTTPS"] == "on" ) {
6242
+ $url .= "s";
6243
+ }
6244
+ }
6245
+ $url .= "://";
6246
+ if ( $_SERVER["SERVER_PORT"] != "80" ) {
6247
+ $url .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
6248
+ } else {
6249
+ $url .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
6250
+ }
6251
+
6252
+ return esc_url( $url );
6253
+ }
6254
+
6255
+ /**
6256
+ * Check if the current page is the plugin's main admin settings page.
6257
+ *
6258
+ * @author Vova Feldman (@svovaf)
6259
+ * @since 1.0.7
6260
+ *
6261
+ * @return bool
6262
+ */
6263
+ function _is_plugin_page() {
6264
+ return fs_is_plugin_page( $this->_menu->get_raw_slug() ) ||
6265
+ fs_is_plugin_page( $this->_slug );
6266
+ }
6267
+
6268
+ /* Events
6269
+ ------------------------------------------------------------------------------------------------------------------*/
6270
+ /**
6271
+ * Delete site install from Database.
6272
+ *
6273
+ * @author Vova Feldman (@svovaf)
6274
+ * @since 1.0.1
6275
+ *
6276
+ * @param bool $store
6277
+ * @param int|null $blog_id Since 2.0.0
6278
+ *
6279
+ * @return false|int The install ID if deleted. Otherwise, FALSE (when install not exist).
6280
+ */
6281
+ function _delete_site( $store = true, $blog_id = null ) {
6282
+ return self::_delete_site_by_slug( $this->_slug, $this->_module_type, $store, $blog_id );
6283
+ }
6284
+
6285
+ /**
6286
+ * Delete site install from Database.
6287
+ *
6288
+ * @author Vova Feldman (@svovaf)
6289
+ * @since 1.2.2.7
6290
+ *
6291
+ * @param string $slug
6292
+ * @param string $module_type
6293
+ * @param bool $store
6294
+ * @param int|null $blog_id Since 2.0.0
6295
+ *
6296
+ * @return false|int The install ID if deleted. Otherwise, FALSE (when install not exist).
6297
+ */
6298
+ static function _delete_site_by_slug( $slug, $module_type, $store = true, $blog_id = null ) {
6299
+ $sites = self::get_all_sites( $module_type, $blog_id );
6300
+
6301
+ $install_id = false;
6302
+
6303
+ if ( isset( $sites[ $slug ] ) ) {
6304
+ if ( is_object( $sites[ $slug ] ) ) {
6305
+ $install_id = $sites[ $slug ]->id;
6306
+ }
6307
+
6308
+ unset( $sites[ $slug ] );
6309
+
6310
+ self::set_account_option_by_module( $module_type, 'sites', $sites, $store, $blog_id );
6311
+ }
6312
+
6313
+ return $install_id;
6314
+ }
6315
+
6316
+ /**
6317
+ * Delete user.
6318
+ *
6319
+ * @author Vova Feldman (@svovaf)
6320
+ * @since 2.0.0
6321
+ *
6322
+ * @param number $user_id
6323
+ * @param bool $store
6324
+ *
6325
+ * @return false|int The user ID if deleted. Otherwise, FALSE (when install not exist).
6326
+ */
6327
+ private static function delete_user( $user_id, $store = true ) {
6328
+ $users = self::get_all_users();
6329
+
6330
+ if ( ! is_array( $users ) || ! isset( $users[ $user_id ] ) ) {
6331
+ return false;
6332
+ }
6333
+
6334
+ unset( $users[ $user_id ] );
6335
+
6336
+ self::$_accounts->set_option( 'users', $users, $store );
6337
+
6338
+ return $user_id;
6339
+ }
6340
+
6341
+ /**
6342
+ * Delete plugin's plans information.
6343
+ *
6344
+ * @param bool $store Flush to Database if true.
6345
+ * @param bool $keep_associated_plans If set to false, delete all plans, even if a plan is associated with an install.
6346
+ *
6347
+ * @author Vova Feldman (@svovaf)
6348
+ * @since 1.0.9
6349
+ */
6350
+ private function _delete_plans( $store = true, $keep_associated_plans = true ) {
6351
+ $this->_logger->entrance();
6352
+
6353
+ $plans = self::get_all_plans( $this->_module_type );
6354
+
6355
+ $plans_to_keep = array();
6356
+
6357
+ if ( $keep_associated_plans ) {
6358
+ $plans_ids_to_keep = $this->get_plans_ids_associated_with_installs();
6359
+ foreach ( $plans_ids_to_keep as $plan_id ) {
6360
+ $plan = self::_get_plan_by_id( $plan_id );
6361
+ if ( is_object( $plan ) ) {
6362
+ $plans_to_keep[] = $plan;
6363
+ }
6364
+ }
6365
+ }
6366
+
6367
+ if ( ! empty( $plans_to_keep ) ) {
6368
+ $plans[ $this->_slug ] = $plans_to_keep;
6369
+ } else {
6370
+ unset( $plans[ $this->_slug ] );
6371
+ }
6372
+
6373
+ $this->set_account_option( 'plans', $plans, $store );
6374
+ }
6375
+
6376
+ /**
6377
+ * Delete all plugin licenses.
6378
+ *
6379
+ * @author Vova Feldman (@svovaf)
6380
+ * @since 1.0.9
6381
+ *
6382
+ * @param bool $store
6383
+ */
6384
+ private function _delete_licenses( $store = true ) {
6385
+ $this->_logger->entrance();
6386
+
6387
+ $all_licenses = self::get_all_licenses();
6388
+
6389
+ unset( $all_licenses[ $this->_module_id ] );
6390
+
6391
+ self::$_accounts->set_option( 'all_licenses', $all_licenses, $store );
6392
+ }
6393
+
6394
+ /**
6395
+ * Check if Freemius was added on new plugin installation.
6396
+ *
6397
+ * @author Vova Feldman (@svovaf)
6398
+ * @since 1.1.5
6399
+ *
6400
+ * @return bool
6401
+ */
6402
+ function is_plugin_new_install() {
6403
+ return isset( $this->_storage->is_plugin_new_install ) &&
6404
+ $this->_storage->is_plugin_new_install;
6405
+ }
6406
+
6407
+ /**
6408
+ * Check if it's the first plugin release that is running Freemius.
6409
+ *
6410
+ * @author Vova Feldman (@svovaf)
6411
+ * @since 1.2.1.5
6412
+ *
6413
+ * @return bool
6414
+ */
6415
+ function is_first_freemius_powered_version() {
6416
+ return empty( $this->_storage->plugin_last_version );
6417
+ }
6418
+
6419
+ /**
6420
+ * @author Leo Fajardo (@leorw)
6421
+ * @since 1.2.2
6422
+ *
6423
+ * @return bool|string
6424
+ */
6425
+ private function get_previous_theme_slug() {
6426
+ return isset( $this->_storage->previous_theme ) ?
6427
+ $this->_storage->previous_theme :
6428
+ false;
6429
+ }
6430
+
6431
+ /**
6432
+ * @author Leo Fajardo (@leorw)
6433
+ * @since 1.2.2
6434
+ *
6435
+ * @return string
6436
+ */
6437
+ private function can_activate_previous_theme() {
6438
+ $slug = $this->get_previous_theme_slug();
6439
+ if ( false !== $slug && current_user_can( 'switch_themes' ) ) {
6440
+ $theme_instance = wp_get_theme( $slug );
6441
+
6442
+ return $theme_instance->exists();
6443
+ }
6444
+
6445
+ return false;
6446
+ }
6447
+
6448
+ /**
6449
+ * @author Leo Fajardo (@leorw)
6450
+ * @since 1.2.2
6451
+ *
6452
+ * @return string
6453
+ */
6454
+ private function activate_previous_theme() {
6455
+ switch_theme( $this->get_previous_theme_slug() );
6456
+ unset( $this->_storage->previous_theme );
6457
+
6458
+ global $pagenow;
6459
+ if ( 'themes.php' === $pagenow ) {
6460
+ /**
6461
+ * Refresh the active theme information.
6462
+ *
6463
+ * @author Leo Fajardo (@leorw)
6464
+ * @since 1.2.2
6465
+ */
6466
+ fs_redirect( $this->admin_url( $pagenow ) );
6467
+ }
6468
+ }
6469
+
6470
+ /**
6471
+ * @author Leo Fajardo (@leorw)
6472
+ * @since 1.2.2
6473
+ *
6474
+ * @return string
6475
+ */
6476
+ function get_previous_theme_activation_url() {
6477
+ if ( ! $this->can_activate_previous_theme() ) {
6478
+ return '';
6479
+ }
6480
+
6481
+ /**
6482
+ * Activation URL
6483
+ *
6484
+ * @author Leo Fajardo (@leorw)
6485
+ * @since 1.2.2
6486
+ */
6487
+ return wp_nonce_url(
6488
+ $this->admin_url( 'themes.php?action=activate&stylesheet=' . urlencode( $this->get_previous_theme_slug() ) ),
6489
+ 'switch-theme_' . $this->get_previous_theme_slug()
6490
+ );
6491
+ }
6492
+
6493
+ /**
6494
+ * Saves the slug of the previous theme if it still exists so that it can be used by the logic in the opt-in
6495
+ * form that decides whether to add a close button to the opt-in dialog or not. So after a premium-only theme is
6496
+ * activated, the close button will appear and will reactivate the previous theme if clicked. If the previous
6497
+ * theme doesn't exist, then there will be no close button.
6498
+ *
6499
+ * @author Leo Fajardo (@leorw)
6500
+ * @since 1.2.2
6501
+ *
6502
+ * @param string $slug_or_name Old theme's slug or name.
6503
+ * @param bool|WP_Theme $old_theme WP_Theme instance of the old theme if it still exists.
6504
+ */
6505
+ function _activate_theme_event_hook( $slug_or_name, $old_theme = false ) {
6506
+ $this->_storage->previous_theme = ( false !== $old_theme ) ?
6507
+ $old_theme->get_stylesheet() :
6508
+ $slug_or_name;
6509
+
6510
+ $this->_activate_plugin_event_hook();
6511
+ }
6512
+
6513
+ /**
6514
+ * Plugin activated hook.
6515
+ *
6516
+ * @author Vova Feldman (@svovaf)
6517
+ * @since 1.0.1
6518
+ *
6519
+ * @uses FS_Api
6520
+ */
6521
+ function _activate_plugin_event_hook() {
6522
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
6523
+
6524
+ if ( ! $this->is_user_admin() ) {
6525
+ return;
6526
+ }
6527
+
6528
+ $this->unregister_uninstall_hook();
6529
+
6530
+ // Clear API cache on activation.
6531
+ FS_Api::clear_cache();
6532
+
6533
+ $is_premium_version_activation = ( current_filter() !== ( 'activate_' . $this->_free_plugin_basename ) );
6534
+
6535
+ $this->_logger->info( 'Activating ' . ( $is_premium_version_activation ? 'premium' : 'free' ) . ' plugin version.' );
6536
+
6537
+ // 1. If running in the activation of the FREE module, get the basename of the PREMIUM.
6538
+ // 2. If running in the activation of the PREMIUM module, get the basename of the FREE.
6539
+ $other_version_basename = $is_premium_version_activation ?
6540
+ $this->_free_plugin_basename :
6541
+ $this->premium_plugin_basename();
6542
+
6543
+ if ( ! $this->_is_network_active ) {
6544
+ /**
6545
+ * During the activation, the plugin isn't yet active, therefore,
6546
+ * _is_network_active will be set to false even if it's a network level
6547
+ * activation. So we need to fix that by looking at the is_network_admin() value.
6548
+ *
6549
+ * @author Vova Feldman
6550
+ */
6551
+ $this->_is_network_active = (
6552
+ $this->_is_multisite_integrated &&
6553
+ // Themes are always network activated, but the ACTUAL activation is per site.
6554
+ $this->is_plugin() &&
6555
+ fs_is_network_admin()
6556
+ );
6557
+ }
6558
+
6559
+ /**
6560
+ * If the other module version is activate, deactivate it.
6561
+ *
6562
+ * is_plugin_active() checks if the plugin active on the site or the network level
6563
+ * and deactivate_plugins() deactivates the plugin whether its activated on the site
6564
+ * or network level.
6565
+ *
6566
+ * @author Leo Fajardo (@leorw)
6567
+ * @since 1.2.2
6568
+ */
6569
+ if ( is_plugin_active( $other_version_basename ) ) {
6570
+ deactivate_plugins( $other_version_basename );
6571
+ }
6572
+
6573
+ if ( $this->is_registered() ) {
6574
+ if ( $is_premium_version_activation ) {
6575
+ $this->reconnect_locally();
6576
+ }
6577
+
6578
+
6579
+ // Schedule re-activation event and sync.
6580
+ // $this->sync_install( array(), true );
6581
+ $this->schedule_install_sync();
6582
+
6583
+ // If activating the premium module version, add an admin notice to congratulate for an upgrade completion.
6584
+ if ( $is_premium_version_activation ) {
6585
+ $this->_admin_notices->add(
6586
+ sprintf( $this->get_text_inline( 'The upgrade of %s was successfully completed.', 'successful-version-upgrade-message' ), sprintf( '<b>%s</b>', $this->_plugin->title ) ),
6587
+ $this->get_text_x_inline( 'W00t',
6588
+ 'Used to express elation, enthusiasm, or triumph (especially in electronic communication).', 'woot' ) . '!'
6589
+ );
6590
+ }
6591
+ } else if ( $this->is_anonymous() ) {
6592
+ if ( isset( $this->_storage->is_anonymous_ms ) && $this->_storage->is_anonymous_ms['is'] ) {
6593
+ $plugin_version = $this->_storage->is_anonymous_ms['version'];
6594
+ $network = true;
6595
+ } else {
6596
+ $plugin_version = $this->_storage->is_anonymous['version'];
6597
+ $network = false;
6598
+ }
6599
+
6600
+ /**
6601
+ * Reset "skipped" click cache on the following:
6602
+ * 1. Freemius DEV mode.
6603
+ * 2. WordPress DEBUG mode.
6604
+ * 3. If a plugin and the user skipped the exact same version before.
6605
+ *
6606
+ * @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).
6607
+ *
6608
+ * @todo 4. If explicitly asked to retry after every activation.
6609
+ */
6610
+ if ( WP_FS__DEV_MODE ||
6611
+ (
6612
+ ( $this->is_plugin() || ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ) &&
6613
+ $this->get_plugin_version() == $plugin_version
6614
+ )
6615
+ ) {
6616
+ $this->reset_anonymous_mode( $network );
6617
+ }
6618
+ }
6619
+
6620
+ if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
6621
+ /**
6622
+ * If no previous version of plugin's version exist, it means that it's either
6623
+ * the first time that the plugin installed on the site, or the plugin was installed
6624
+ * before but didn't have Freemius integrated.
6625
+ *
6626
+ * Since register_activation_hook() do NOT fires on updates since 3.1, and only fires
6627
+ * on manual activation via the dashboard, is_plugin_activation() is TRUE
6628
+ * only after immediate activation.
6629
+ *
6630
+ * @since 1.1.4
6631
+ * @link https://make.wordpress.org/core/2010/10/27/plugin-activation-hooks-no-longer-fire-for-updates/
6632
+ */
6633
+ $this->_storage->is_plugin_new_install = empty( $this->_storage->plugin_last_version );
6634
+ }
6635
+
6636
+ if ( ! $this->_anonymous_mode &&
6637
+ $this->has_api_connectivity( WP_FS__DEV_MODE ) &&
6638
+ ! $this->_isAutoInstall
6639
+ ) {
6640
+ // Store hint that the plugin was just activated to enable auto-redirection to settings.
6641
+ add_option( 'fs_'
6642
+ . ( $this->is_plugin() ? '' : $this->_module_type . '_' )
6643
+ . "{$this->_slug}_activated", true );
6644
+ }
6645
+
6646
+ /**
6647
+ * Activation hook is executed after the plugin's main file is loaded, therefore,
6648
+ * after the plugin was loaded. The logic is located at activate_plugin()
6649
+ * ./wp-admin/includes/plugin.php.
6650
+ *
6651
+ * @author Vova Feldman (@svovaf)
6652
+ * @since 1.1.9
6653
+ */
6654
+ $this->_storage->was_plugin_loaded = true;
6655
+ }
6656
+
6657
+ /**
6658
+ * Delete account.
6659
+ *
6660
+ * @author Vova Feldman (@svovaf)
6661
+ * @since 1.0.3
6662
+ *
6663
+ * @param bool $check_user Enforce checking if user have plugins activation privileges.
6664
+ */
6665
+ function delete_account_event( $check_user = true ) {
6666
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
6667
+
6668
+ if ( $check_user && ! $this->is_user_admin() ) {
6669
+ return;
6670
+ }
6671
+
6672
+ $this->do_action( 'before_account_delete' );
6673
+
6674
+ // Clear all admin notices.
6675
+ $this->_admin_notices->clear_all_sticky( false );
6676
+
6677
+ $this->_delete_site( false );
6678
+
6679
+ $delete_network_common_data = true;
6680
+
6681
+ if ( $this->_is_network_active ) {
6682
+ $installs = $this->get_blog_install_map();
6683
+
6684
+ // Don't delete common network data unless no other installs left.
6685
+ $delete_network_common_data = empty( $installs );
6686
+ }
6687
+
6688
+ if ( $delete_network_common_data ) {
6689
+ $this->_delete_plans( false );
6690
+
6691
+ $this->_delete_licenses( false );
6692
+
6693
+ // Delete add-ons related to plugin's account.
6694
+ $this->_delete_account_addons( false );
6695
+ }
6696
+
6697
+ // @todo Delete plans and licenses of add-ons.
6698
+
6699
+ self::$_accounts->store();
6700
+
6701
+ /**
6702
+ * IMPORTANT:
6703
+ * Clear crons must be executed before clearing all storage.
6704
+ * Otherwise, the cron will not be cleared.
6705
+ */
6706
+ if ( $delete_network_common_data ) {
6707
+ $this->clear_sync_cron();
6708
+ }
6709
+
6710
+ $this->clear_install_sync_cron();
6711
+
6712
+ // Clear all storage data.
6713
+ $this->_storage->clear_all( true, array(
6714
+ 'connectivity_test',
6715
+ 'is_on',
6716
+ ), false );
6717
+
6718
+ // Send delete event.
6719
+ $this->get_api_site_scope()->call( '/', 'delete' );
6720
+
6721
+ $this->do_action( 'after_account_delete' );
6722
+ }
6723
+
6724
+ /**
6725
+ * Delete network level account.
6726
+ *
6727
+ * @author Vova Feldman (@svovaf)
6728
+ * @since 2.0.0
6729
+ *
6730
+ * @param bool $check_user Enforce checking if user have plugins activation privileges.
6731
+ */
6732
+ function delete_network_account_event( $check_user = true ) {
6733
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
6734
+
6735
+ if ( $check_user && ! $this->is_user_admin() ) {
6736
+ return;
6737
+ }
6738
+
6739
+ $this->do_action( 'before_network_account_delete' );
6740
+
6741
+ // Clear all admin notices.
6742
+ $this->_admin_notices->clear_all_sticky();
6743
+
6744
+ $this->_delete_plans( false, false );
6745
+
6746
+ $this->_delete_licenses( false );
6747
+
6748
+ // Delete add-ons related to plugin's account.
6749
+ $this->_delete_account_addons( false );
6750
+
6751
+ // @todo Delete plans and licenses of add-ons.
6752
+
6753
+ self::$_accounts->store( true );
6754
+
6755
+ /**
6756
+ * IMPORTANT:
6757
+ * Clear crons must be executed before clearing all storage.
6758
+ * Otherwise, the cron will not be cleared.
6759
+ */
6760
+ $this->clear_sync_cron( true );
6761
+ $this->clear_install_sync_cron( true );
6762
+
6763
+ $sites = self::get_sites();
6764
+
6765
+ $install_ids = array();
6766
+ foreach ( $sites as $site ) {
6767
+ $blog_id = self::get_site_blog_id( $site );
6768
+
6769
+ $install_id = $this->_delete_site( true, $blog_id );
6770
+
6771
+ // Clear all storage data.
6772
+ $this->_storage->clear_all( true, array( 'connectivity_test' ), $blog_id );
6773
+
6774
+ if ( FS_Site::is_valid_id( $install_id ) ) {
6775
+ $install_ids[] = $install_id;
6776
+ }
6777
+
6778
+ switch_to_blog( $blog_id );
6779
+
6780
+ $this->do_action( 'after_account_delete' );
6781
+
6782
+ restore_current_blog();
6783
+ }
6784
+
6785
+ $this->_storage->clear_all( true, array(
6786
+ 'connectivity_test',
6787
+ 'is_on',
6788
+ ), true );
6789
+
6790
+ // Send delete event.
6791
+ if ( ! empty( $install_ids ) ) {
6792
+ $result = $this->get_current_or_network_user_api_scope()->call( "/plugins/{$this->_module_id}/installs.json?ids=" . implode( ',', $install_ids ), 'delete' );
6793
+ }
6794
+
6795
+ $this->do_action( 'after_network_account_delete' );
6796
+ }
6797
+
6798
+ /**
6799
+ * Plugin deactivation hook.
6800
+ *
6801
+ * @author Vova Feldman (@svovaf)
6802
+ * @since 1.0.1
6803
+ */
6804
+ function _deactivate_plugin_hook() {
6805
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
6806
+
6807
+ if ( ! $this->is_user_admin() ) {
6808
+ return;
6809
+ }
6810
+
6811
+ $is_network_deactivation = fs_is_network_admin();
6812
+ $storage_keys_for_removal = array();
6813
+
6814
+ $this->_admin_notices->clear_all_sticky();
6815
+
6816
+ $storage_keys_for_removal[] = 'sticky_optin_added';
6817
+ if ( isset( $this->_storage->sticky_optin_added ) ) {
6818
+ unset( $this->_storage->sticky_optin_added );
6819
+ }
6820
+
6821
+ if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
6822
+ // Remember that plugin was already installed.
6823
+ $this->_storage->is_plugin_new_install = false;
6824
+ }
6825
+
6826
+ // Hook to plugin uninstall.
6827
+ register_uninstall_hook( $this->_plugin_main_file_path, array( 'Freemius', '_uninstall_plugin_hook' ) );
6828
+
6829
+ $this->clear_module_main_file_cache();
6830
+ $this->clear_sync_cron( $this->_is_network_active );
6831
+ $this->clear_install_sync_cron();
6832
+
6833
+ if ( $this->is_registered() ) {
6834
+ if ( $is_network_deactivation ) {
6835
+ // Send deactivation event.
6836
+ $this->sync_installs( array(
6837
+ 'is_active' => false,
6838
+ ) );
6839
+ } else {
6840
+ // Send deactivation event.
6841
+ $this->sync_install( array(
6842
+ 'is_active' => false,
6843
+ ) );
6844
+ }
6845
+ } else {
6846
+ if ( ! $this->has_api_connectivity() ) {
6847
+ // Reset connectivity test cache.
6848
+ unset( $this->_storage->connectivity_test );
6849
+
6850
+ $storage_keys_for_removal[] = 'connectivity_test';
6851
+ }
6852
+ }
6853
+
6854
+ if ( $is_network_deactivation ) {
6855
+ if ( isset( $this->_storage->sticky_optin_added_ms ) ) {
6856
+ unset( $this->_storage->sticky_optin_added_ms );
6857
+ }
6858
+
6859
+ if ( ! empty( $storage_keys_for_removal ) ) {
6860
+ $sites = self::get_sites();
6861
+
6862
+ foreach ( $sites as $site ) {
6863
+ $blog_id = self::get_site_blog_id( $site );
6864
+
6865
+ foreach ( $storage_keys_for_removal as $key ) {
6866
+ $this->_storage->remove( $key, false, $blog_id );
6867
+ }
6868
+
6869
+ $this->_storage->save( $blog_id );
6870
+ }
6871
+ }
6872
+ }
6873
+
6874
+ // Clear API cache on deactivation.
6875
+ FS_Api::clear_cache();
6876
+
6877
+ $this->remove_sdk_reference();
6878
+ }
6879
+
6880
+ /**
6881
+ * @author Vova Feldman (@svovaf)
6882
+ * @since 1.1.6
6883
+ */
6884
+ private function remove_sdk_reference() {
6885
+ global $fs_active_plugins;
6886
+
6887
+ foreach ( $fs_active_plugins->plugins as $sdk_path => &$data ) {
6888
+ if ( $this->_plugin_basename == $data->plugin_path ) {
6889
+ unset( $fs_active_plugins->plugins[ $sdk_path ] );
6890
+ break;
6891
+ }
6892
+ }
6893
+
6894
+ fs_fallback_to_newest_active_sdk();
6895
+ }
6896
+
6897
+ /**
6898
+ * @author Vova Feldman (@svovaf)
6899
+ * @since 1.1.3
6900
+ *
6901
+ * @param bool $is_anonymous
6902
+ * @param bool|int $network_or_blog_id Since 2.0.0
6903
+ */
6904
+ private function set_anonymous_mode( $is_anonymous = true, $network_or_blog_id = 0 ) {
6905
+ // Store information regarding skip to try and opt-in the user
6906
+ // again in the future.
6907
+ $skip_info = array(
6908
+ 'is' => $is_anonymous,
6909
+ 'timestamp' => WP_FS__SCRIPT_START_TIME,
6910
+ 'version' => $this->get_plugin_version(),
6911
+ );
6912
+
6913
+ if ( true === $network_or_blog_id ) {
6914
+ $this->_storage->is_anonymous_ms = $skip_info;
6915
+ } else {
6916
+ $this->_storage->store( 'is_anonymous', $skip_info, $network_or_blog_id );
6917
+ }
6918
+
6919
+ $this->network_upgrade_mode_completed();
6920
+
6921
+ // Update anonymous mode cache.
6922
+ $this->_is_anonymous = $is_anonymous;
6923
+ }
6924
+
6925
+ /**
6926
+ * @author Vova Feldman (@svovaf)
6927
+ * @since 2.0.0
6928
+ *
6929
+ * @param int $blog_id Site ID.
6930
+ * @param int $user_id User ID.
6931
+ * @param string $domain Site domain.
6932
+ * @param string $path Site path.
6933
+ * @param int $network_id Network ID. Only relevant on multi-network installations.
6934
+ * @param array $meta Metadata. Used to set initial site options.
6935
+ *
6936
+ * @uses Freemius::is_license_network_active() to check if the context license was network activated by the super-admin.
6937
+ * @uses Freemius::is_network_connected() to check if the super-admin network opted-in.
6938
+ * @uses Freemius::is_network_anonymous() to check if the super-admin network skipped.
6939
+ * @uses Freemius::is_network_delegated_connection() to check if the super-admin network delegated the connection to the site admins.
6940
+ */
6941
+ function _after_new_blog_callback( $blog_id, $user_id, $domain, $path, $network_id, $meta ) {
6942
+ $this->_logger->entrance();
6943
+
6944
+ if ( $this->is_premium() &&
6945
+ $this->is_network_connected() &&
6946
+ is_object( $this->_license ) &&
6947
+ $this->_license->can_activate( FS_Site::is_localhost_by_address( $domain ) ) &&
6948
+ $this->is_license_network_active( $blog_id )
6949
+ ) {
6950
+ /**
6951
+ * 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.
6952
+ */
6953
+ $current_blog_id = get_current_blog_id();
6954
+ $license = clone $this->_license;
6955
+
6956
+ $this->switch_to_blog( $blog_id );
6957
+
6958
+ // Opt-in with network user.
6959
+ $this->install_with_user(
6960
+ $this->get_network_user(),
6961
+ $license->secret_key,
6962
+ false,
6963
+ false,
6964
+ false
6965
+ );
6966
+
6967
+ if ( is_object( $this->_site ) ) {
6968
+ if ( $this->_site->license_id == $license->id ) {
6969
+ /**
6970
+ * If the license was activated successfully, sync the license data from the remote server.
6971
+ */
6972
+ $this->_license = $license;
6973
+ $this->sync_site_license();
6974
+ }
6975
+ }
6976
+
6977
+ $this->switch_to_blog( $current_blog_id );
6978
+
6979
+ if ( is_object( $this->_site ) ) {
6980
+ // Already connected (with or without a license), so no need to continue.
6981
+ return;
6982
+ }
6983
+ }
6984
+
6985
+ if ( $this->is_network_anonymous() ) {
6986
+ /**
6987
+ * Opt-in was network skipped so automatically skip the opt-in for the new site.
6988
+ */
6989
+ $this->skip_site_connection( $blog_id );
6990
+ } else if ( $this->is_network_delegated_connection() ) {
6991
+ /**
6992
+ * Opt-in was network delegated so automatically delegate the opt-in for the new site's admin.
6993
+ */
6994
+ $this->delegate_site_connection( $blog_id );
6995
+ } else if ( $this->is_network_connected() ) {
6996
+ /**
6997
+ * Opt-in was network activated so automatically opt-in with the network user and new site admin.
6998
+ */
6999
+ $current_blog_id = get_current_blog_id();
7000
+
7001
+ $this->switch_to_blog( $blog_id );
7002
+
7003
+ // Opt-in with network user.
7004
+ $this->install_with_user(
7005
+ $this->get_network_user(),
7006
+ false,
7007
+ false,
7008
+ false,
7009
+ false
7010
+ );
7011
+
7012
+ $this->switch_to_blog( $current_blog_id );
7013
+ } else {
7014
+ /**
7015
+ * If the super-admin mixed different options (connect, skip, delegated):
7016
+ * a) If at least one site connection was delegated, then automatically delegate connection.
7017
+ * 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.
7018
+ */
7019
+ $has_delegated_site = false;
7020
+
7021
+ $sites = self::get_sites();
7022
+ foreach ( $sites as $site ) {
7023
+ $blog_id = self::get_site_blog_id( $site );
7024
+
7025
+ if ( $this->is_site_delegated_connection( $blog_id ) ) {
7026
+ $has_delegated_site = true;
7027
+ break;
7028
+ }
7029
+ }
7030
+
7031
+ if ( $has_delegated_site ) {
7032
+ $this->delegate_site_connection( $blog_id );
7033
+ } else {
7034
+ $this->skip_site_connection( $blog_id );
7035
+ }
7036
+ }
7037
+ }
7038
+
7039
+ /**
7040
+ * @author Vova Feldman (@svovaf)
7041
+ * @since 1.1.3
7042
+ *
7043
+ * @param bool|int $network_or_blog_id Since 2.0.0.
7044
+ */
7045
+ private function reset_anonymous_mode( $network_or_blog_id = 0 ) {
7046
+ if ( true === $network_or_blog_id ) {
7047
+ unset( $this->_storage->is_anonymous_ms );
7048
+ } else {
7049
+ $this->_storage->remove( 'is_anonymous', true, $network_or_blog_id );
7050
+ }
7051
+
7052
+ /**
7053
+ * Ensure that this field is also "false", otherwise, if the current module's type is "theme" and the module
7054
+ * has no menus, the opt-in popup will not be shown immediately (in this case, the user will have to click
7055
+ * on the admin notice that contains the opt-in link in order to trigger the opt-in popup).
7056
+ *
7057
+ * @author Leo Fajardo (@leorw)
7058
+ * @since 1.2.2
7059
+ */
7060
+ if ( ! $this->_is_network_active ||
7061
+ 0 === $network_or_blog_id ||
7062
+ get_current_blog_id() == $network_or_blog_id ||
7063
+ ( true === $network_or_blog_id && fs_is_network_admin() )
7064
+ ) {
7065
+ unset( $this->_is_anonymous );
7066
+ }
7067
+ }
7068
+
7069
+ /**
7070
+ * Clears the anonymous mode and redirects to the opt-in screen.
7071
+ *
7072
+ * @author Vova Feldman (@svovaf)
7073
+ * @since 1.1.7
7074
+ */
7075
+ function connect_again() {
7076
+ if ( ! $this->is_anonymous() ) {
7077
+ return;
7078
+ }
7079
+
7080
+ $this->reset_anonymous_mode( fs_is_network_admin() );
7081
+
7082
+ fs_redirect( $this->get_activation_url() );
7083
+ }
7084
+
7085
+ /**
7086
+ * Skip account connect, and set anonymous mode.
7087
+ *
7088
+ * @author Vova Feldman (@svovaf)
7089
+ * @since 1.1.1
7090
+ *
7091
+ * @param array|null $sites Since 2.0.0. Specific sites.
7092
+ * @param bool $skip_all_network Since 2.0.0. If true, skip connection for all sites.
7093
+ */
7094
+ private function skip_connection( $sites = null, $skip_all_network = false ) {
7095
+ $this->_logger->entrance();
7096
+
7097
+ $this->_admin_notices->remove_sticky( 'connect_account' );
7098
+
7099
+ if ( $skip_all_network ) {
7100
+ $this->set_anonymous_mode( true, true );
7101
+ }
7102
+
7103
+ if ( ! $skip_all_network && empty( $sites ) ) {
7104
+ $this->skip_site_connection();
7105
+ } else {
7106
+ $uids = array();
7107
+
7108
+ if ( $skip_all_network ) {
7109
+ $this->set_anonymous_mode( true, true );
7110
+
7111
+ $sites = self::get_sites();
7112
+ foreach ( $sites as $site ) {
7113
+ $blog_id = self::get_site_blog_id( $site );
7114
+ $this->skip_site_connection( $blog_id, false );
7115
+ $uids[] = $this->get_anonymous_id( $blog_id );
7116
+ }
7117
+ } else if ( ! empty( $sites ) ) {
7118
+ foreach ( $sites as $site ) {
7119
+ $uids[] = $site['uid'];
7120
+ $this->skip_site_connection( $site['blog_id'], false );
7121
+ }
7122
+ }
7123
+
7124
+ // Send anonymous skip event.
7125
+ // No user identified info nor any tracking will be sent after the user skips the opt-in.
7126
+ $this->get_api_plugin_scope()->call( 'skip.json', 'put', array(
7127
+ 'uids' => $uids,
7128
+ ) );
7129
+ }
7130
+
7131
+ $this->network_upgrade_mode_completed();
7132
+ }
7133
+
7134
+ /**
7135
+ * Skip connection for specific site in the network.
7136
+ *
7137
+ * @author Vova Feldman (@svovaf)
7138
+ * @since 2.0.0
7139
+ *
7140
+ * @param int|null $blog_id
7141
+ * @param bool $send_skip
7142
+ */
7143
+ private function skip_site_connection( $blog_id = null, $send_skip = true ) {
7144
+ $this->_logger->entrance();
7145
+
7146
+ $this->_admin_notices->remove_sticky( 'connect_account', $blog_id );
7147
+
7148
+ $this->set_anonymous_mode( true, $blog_id );
7149
+
7150
+ if ( $send_skip ) {
7151
+ $this->get_api_plugin_scope()->call( 'skip.json', 'put', array(
7152
+ 'uids' => array( $this->get_anonymous_id( $blog_id ) ),
7153
+ ) );
7154
+ }
7155
+ }
7156
+
7157
+ /**
7158
+ * Plugin version update hook.
7159
+ *
7160
+ * @author Vova Feldman (@svovaf)
7161
+ * @since 1.0.4
7162
+ */
7163
+ private function update_plugin_version_event() {
7164
+ $this->_logger->entrance();
7165
+
7166
+ if ( ! $this->is_registered() ) {
7167
+ return;
7168
+ }
7169
+
7170
+ $this->schedule_install_sync();
7171
+ // $this->sync_install( array(), true );
7172
+ }
7173
+
7174
+ /**
7175
+ * Generate an MD5 signature of a plugins collection.
7176
+ * This helper methods used to identify changes in a plugins collection.
7177
+ *
7178
+ * @author Vova Feldman (@svovaf)
7179
+ * @since 2.0.0
7180
+ *
7181
+ * @param array [string]array $plugins
7182
+ *
7183
+ * @return string
7184
+ */
7185
+ private function get_plugins_thumbprint( $plugins ) {
7186
+ ksort( $plugins );
7187
+
7188
+ $thumbprint = '';
7189
+ foreach ( $plugins as $basename => $data ) {
7190
+ $thumbprint .= $data['slug'] . ',' .
7191
+ $data['Version'] . ',' .
7192
+ ( $data['is_active'] ? '1' : '0' ) . ';';
7193
+ }
7194
+
7195
+ return md5( $thumbprint );
7196
+ }
7197
+
7198
+ /**
7199
+ * Return a list of modified plugins since the last sync.
7200
+ *
7201
+ * Note:
7202
+ * There's no point to store a plugins counter since even if the number of
7203
+ * plugins didn't change, we still need to check if the versions are all the
7204
+ * same and the activity state is similar.
7205
+ *
7206
+ * @author Vova Feldman (@svovaf)
7207
+ * @since 1.1.8
7208
+ *
7209
+ * @return array|false
7210
+ */
7211
+ private function get_plugins_data_for_api() {
7212
+ // Alias.
7213
+ $site_active_plugins_option_name = 'active_plugins';
7214
+ $network_plugins_option_name = 'all_plugins';
7215
+
7216
+ /**
7217
+ * Collection of all site level active plugins.
7218
+ */
7219
+ $site_active_plugins_cache = self::$_accounts->get_option( $site_active_plugins_option_name );
7220
+
7221
+ if ( ! is_object( $site_active_plugins_cache ) ) {
7222
+ $site_active_plugins_cache = (object) array(
7223
+ 'timestamp' => '',
7224
+ 'md5' => '',
7225
+ 'plugins' => array(),
7226
+ );
7227
+ }
7228
+
7229
+ $time = time();
7230
+
7231
+ if ( ! empty( $site_active_plugins_cache->timestamp ) &&
7232
+ ( $time - $site_active_plugins_cache->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC
7233
+ ) {
7234
+ // Don't send plugin updates if last update was in the past 5 min.
7235
+ return false;
7236
+ }
7237
+
7238
+ // Write timestamp to lock the logic.
7239
+ $site_active_plugins_cache->timestamp = $time;
7240
+ self::$_accounts->set_option( $site_active_plugins_option_name, $site_active_plugins_cache, true );
7241
+
7242
+ // Reload options from DB.
7243
+ self::$_accounts->load( true );
7244
+ $site_active_plugins_cache = self::$_accounts->get_option( $site_active_plugins_option_name );
7245
+
7246
+ if ( $time != $site_active_plugins_cache->timestamp ) {
7247
+ // If timestamp is different, then another thread captured the lock.
7248
+ return false;
7249
+ }
7250
+
7251
+ /**
7252
+ * Collection of all plugins (network level).
7253
+ */
7254
+ $network_plugins_cache = self::$_accounts->get_option( $network_plugins_option_name );
7255
+
7256
+ if ( ! is_object( $network_plugins_cache ) ) {
7257
+ $network_plugins_cache = (object) array(
7258
+ 'timestamp' => '',
7259
+ 'md5' => '',
7260
+ 'plugins' => array(),
7261
+ );
7262
+ }
7263
+
7264
+ // Check if there's a change in plugins.
7265
+ $network_plugins = self::get_network_plugins();
7266
+ $site_active_plugins = self::get_site_active_plugins();
7267
+
7268
+ $network_plugins_thumbprint = $this->get_plugins_thumbprint( $network_plugins );
7269
+ $site_active_plugins_thumbprint = $this->get_plugins_thumbprint( $site_active_plugins );
7270
+
7271
+ // Check if plugins status changed (version or active/inactive).
7272
+ $network_plugins_changed = ( $network_plugins_cache->md5 !== $network_plugins_thumbprint );
7273
+ $site_active_plugins_changed = ( $site_active_plugins_cache->md5 !== $site_active_plugins_thumbprint );
7274
+
7275
+ if ( ! $network_plugins_changed &&
7276
+ ! $site_active_plugins_changed
7277
+ ) {
7278
+ // No changes.
7279
+ return array();
7280
+ }
7281
+
7282
+ $plugins_update_data = array();
7283
+
7284
+ foreach ( $network_plugins_cache->plugins as $basename => $data ) {
7285
+ if ( ! isset( $network_plugins[ $basename ] ) ) {
7286
+ // Plugin uninstalled.
7287
+ $uninstalled_plugin_data = $data;
7288
+ $uninstalled_plugin_data['is_active'] = false;
7289
+ $uninstalled_plugin_data['is_uninstalled'] = true;
7290
+ $plugins_update_data[] = $uninstalled_plugin_data;
7291
+
7292
+ unset( $network_plugins[ $basename ] );
7293
+
7294
+ unset( $network_plugins_cache->plugins[ $basename ] );
7295
+ unset( $site_active_plugins_cache->plugins[ $basename ] );
7296
+
7297
+ continue;
7298
+ }
7299
+
7300
+ $was_active = $data['is_active'] ||
7301
+ ( isset( $site_active_plugins_cache->plugins[ $basename ] ) &&
7302
+ true === $site_active_plugins_cache->plugins[ $basename ]['is_active'] );
7303
+ $is_active = $network_plugins[ $basename ]['is_active'] ||
7304
+ ( isset( $site_active_plugins[ $basename ] ) &&
7305
+ $site_active_plugins[ $basename ]['is_active'] );
7306
+
7307
+ if ( ! isset( $site_active_plugins_cache->plugins[ $basename ] ) &&
7308
+ isset( $site_active_plugins[ $basename ] )
7309
+ ) {
7310
+ // Plugin was site level activated.
7311
+ $site_active_plugins_cache->plugins[ $basename ] = $network_plugins[ $basename ];
7312
+ $site_active_plugins_cache->plugins[ $basename ]['is_active'] = true;
7313
+ } else if ( isset( $site_active_plugins_cache->plugins[ $basename ] ) &&
7314
+ ! isset( $site_active_plugins[ $basename ] )
7315
+ ) {
7316
+ // Plugin was site level deactivated.
7317
+ unset( $site_active_plugins_cache->plugins[ $basename ] );
7318
+ }
7319
+
7320
+ $prev_version = $data['version'];
7321
+ $current_version = $network_plugins[ $basename ]['Version'];
7322
+
7323
+ if ( $was_active !== $is_active || $prev_version !== $current_version ) {
7324
+ // Plugin activated or deactivated, or version changed.
7325
+
7326
+ if ( $was_active !== $is_active ) {
7327
+ if ( $data['is_active'] != $network_plugins[ $basename ]['is_active'] ) {
7328
+ $network_plugins_cache->plugins[ $basename ]['is_active'] = $data['is_active'];
7329
+ }
7330
+ }
7331
+
7332
+ if ( $prev_version !== $current_version ) {
7333
+ $network_plugins_cache->plugins[ $basename ]['Version'] = $current_version;
7334
+ }
7335
+
7336
+ $updated_plugin_data = $data;
7337
+ $updated_plugin_data['is_active'] = $is_active;
7338
+ $updated_plugin_data['version'] = $current_version;
7339
+ $updated_plugin_data['title'] = $network_plugins[ $basename ]['Name'];
7340
+ $plugins_update_data[] = $updated_plugin_data;
7341
+ }
7342
+ }
7343
+
7344
+ // Find new plugins that weren't yet seen before.
7345
+ foreach ( $network_plugins as $basename => $data ) {
7346
+ if ( ! isset( $network_plugins_cache->plugins[ $basename ] ) ) {
7347
+ // New plugin.
7348
+ $new_plugin = array(
7349
+ 'slug' => $data['slug'],
7350
+ 'version' => $data['Version'],
7351
+ 'title' => $data['Name'],
7352
+ 'is_active' => $data['is_active'],
7353
+ 'is_uninstalled' => false,
7354
+ );
7355
+
7356
+ $plugins_update_data[] = $new_plugin;
7357
+ $network_plugins_cache->plugins[ $basename ] = $new_plugin;
7358
+
7359
+ if ( isset( $site_active_plugins[ $basename ] ) ) {
7360
+ $site_active_plugins_cache->plugins[ $basename ] = $new_plugin;
7361
+ $site_active_plugins_cache->plugins[ $basename ]['is_active'] = true;
7362
+ }
7363
+ }
7364
+ }
7365
+
7366
+ $site_active_plugins_cache->md5 = $site_active_plugins_thumbprint;
7367
+ $site_active_plugins_cache->timestamp = $time;
7368
+ self::$_accounts->set_option( $site_active_plugins_option_name, $site_active_plugins_cache, true );
7369
+
7370
+ $network_plugins_cache->md5 = $network_plugins_thumbprint;
7371
+ $network_plugins_cache->timestamp = $time;
7372
+ self::$_accounts->set_option( $network_plugins_option_name, $network_plugins_cache, true );
7373
+
7374
+ return $plugins_update_data;
7375
+ }
7376
+
7377
+ /**
7378
+ * Return a list of modified themes since the last sync.
7379
+ *
7380
+ * Note:
7381
+ * There's no point to store a themes counter since even if the number of
7382
+ * themes didn't change, we still need to check if the versions are all the
7383
+ * same and the activity state is similar.
7384
+ *
7385
+ * @author Vova Feldman (@svovaf)
7386
+ * @since 1.1.8
7387
+ *
7388
+ * @return array|false
7389
+ */
7390
+ private function get_themes_data_for_api() {
7391
+ // Alias.
7392
+ $option_name = 'all_themes';
7393
+
7394
+ $all_cached_themes = self::$_accounts->get_option( $option_name );
7395
+
7396
+ if ( ! is_object( $all_cached_themes ) ) {
7397
+ $all_cached_themes = (object) array(
7398
+ 'timestamp' => '',
7399
+ 'md5' => '',
7400
+ 'themes' => array(),
7401
+ );
7402
+ }
7403
+
7404
+ $time = time();
7405
+
7406
+ if ( ! empty( $all_cached_themes->timestamp ) &&
7407
+ ( $time - $all_cached_themes->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC
7408
+ ) {
7409
+ // Don't send theme updates if last update was in the past 5 min.
7410
+ return false;
7411
+ }
7412
+
7413
+ // Write timestamp to lock the logic.
7414
+ $all_cached_themes->timestamp = $time;
7415
+ self::$_accounts->set_option( $option_name, $all_cached_themes, true );
7416
+
7417
+ // Reload options from DB.
7418
+ self::$_accounts->load( true );
7419
+ $all_cached_themes = self::$_accounts->get_option( $option_name );
7420
+
7421
+ if ( $time != $all_cached_themes->timestamp ) {
7422
+ // If timestamp is different, then another thread captured the lock.
7423
+ return false;
7424
+ }
7425
+
7426
+ // Get active theme.
7427
+ $active_theme = wp_get_theme();
7428
+ $active_theme_stylesheet = $active_theme->get_stylesheet();
7429
+
7430
+ // Check if there's a change in themes.
7431
+ $all_themes = wp_get_themes();
7432
+
7433
+ // Check if themes changed.
7434
+ ksort( $all_themes );
7435
+
7436
+ $themes_signature = '';
7437
+ foreach ( $all_themes as $slug => $data ) {
7438
+ $is_active = ( $slug === $active_theme_stylesheet );
7439
+ $themes_signature .= $slug . ',' .
7440
+ $data->version . ',' .
7441
+ ( $is_active ? '1' : '0' ) . ';';
7442
+ }
7443
+
7444
+ // Check if themes status changed (version or active/inactive).
7445
+ $themes_changed = ( $all_cached_themes->md5 !== md5( $themes_signature ) );
7446
+
7447
+ $themes_update_data = array();
7448
+
7449
+ if ( $themes_changed ) {
7450
+ // Change in themes, report changes.
7451
+
7452
+ // Update existing themes info.
7453
+ foreach ( $all_cached_themes->themes as $slug => $data ) {
7454
+ $is_active = ( $slug === $active_theme_stylesheet );
7455
+
7456
+ if ( ! isset( $all_themes[ $slug ] ) ) {
7457
+ // Plugin uninstalled.
7458
+ $uninstalled_theme_data = $data;
7459
+ $uninstalled_theme_data['is_active'] = false;
7460
+ $uninstalled_theme_data['is_uninstalled'] = true;
7461
+ $themes_update_data[] = $uninstalled_theme_data;
7462
+
7463
+ unset( $all_themes[ $slug ] );
7464
+ unset( $all_cached_themes->themes[ $slug ] );
7465
+ } else if ( $data['is_active'] !== $is_active ||
7466
+ $data['version'] !== $all_themes[ $slug ]->version
7467
+ ) {
7468
+ // Plugin activated or deactivated, or version changed.
7469
+
7470
+ $all_cached_themes->themes[ $slug ]['is_active'] = $is_active;
7471
+ $all_cached_themes->themes[ $slug ]['version'] = $all_themes[ $slug ]->version;
7472
+
7473
+ $themes_update_data[] = $all_cached_themes->themes[ $slug ];
7474
+ }
7475
+ }
7476
+
7477
+ // Find new themes that weren't yet seen before.
7478
+ foreach ( $all_themes as $slug => $data ) {
7479
+ if ( ! isset( $all_cached_themes->themes[ $slug ] ) ) {
7480
+ $is_active = ( $slug === $active_theme_stylesheet );
7481
+
7482
+ // New plugin.
7483
+ $new_plugin = array(
7484
+ 'slug' => $slug,
7485
+ 'version' => $data->version,
7486
+ 'title' => $data->name,
7487
+ 'is_active' => $is_active,
7488
+ 'is_uninstalled' => false,
7489
+ );
7490
+
7491
+ $themes_update_data[] = $new_plugin;
7492
+ $all_cached_themes->themes[ $slug ] = $new_plugin;
7493
+ }
7494
+ }
7495
+
7496
+ $all_cached_themes->md5 = md5( $themes_signature );
7497
+ $all_cached_themes->timestamp = time();
7498
+ self::$_accounts->set_option( $option_name, $all_cached_themes, true );
7499
+ }
7500
+
7501
+ return $themes_update_data;
7502
+ }
7503
+
7504
+ /**
7505
+ * Get site data for API install request.
7506
+ *
7507
+ * @author Vova Feldman (@svovaf)
7508
+ * @since 1.1.2
7509
+ *
7510
+ * @param string[] string $override
7511
+ * @param bool $include_plugins Since 1.1.8 by default include plugin changes.
7512
+ * @param bool $include_themes Since 1.1.8 by default include plugin changes.
7513
+ *
7514
+ * @return array
7515
+ */
7516
+ private function get_install_data_for_api(
7517
+ array $override,
7518
+ $include_plugins = true,
7519
+ $include_themes = true
7520
+ ) {
7521
+ /**
7522
+ * @since 1.1.8 Also send plugin updates.
7523
+ */
7524
+ if ( $include_plugins && ! isset( $override['plugins'] ) ) {
7525
+ $plugins = $this->get_plugins_data_for_api();
7526
+ if ( ! empty( $plugins ) ) {
7527
+ $override['plugins'] = $plugins;
7528
+ }
7529
+ }
7530
+ /**
7531
+ * @since 1.1.8 Also send themes updates.
7532
+ */
7533
+ if ( $include_themes && ! isset( $override['themes'] ) ) {
7534
+ $themes = $this->get_themes_data_for_api();
7535
+ if ( ! empty( $themes ) ) {
7536
+ $override['themes'] = $themes;
7537
+ }
7538
+ }
7539
+
7540
+ return array_merge( array(
7541
+ 'version' => $this->get_plugin_version(),
7542
+ 'is_premium' => $this->is_premium(),
7543
+ 'language' => get_bloginfo( 'language' ),
7544
+ 'charset' => get_bloginfo( 'charset' ),
7545
+ 'platform_version' => get_bloginfo( 'version' ),
7546
+ 'sdk_version' => $this->version,
7547
+ 'programming_language_version' => phpversion(),
7548
+ 'title' => get_bloginfo( 'name' ),
7549
+ 'url' => get_site_url(),
7550
+ // Special params.
7551
+ 'is_active' => true,
7552
+ 'is_disconnected' => $this->is_tracking_prohibited(),
7553
+ 'is_uninstalled' => false,
7554
+ ), $override );
7555
+ }
7556
+
7557
+ /**
7558
+ * Update installs details.
7559
+ *
7560
+ * @todo V1 of multiste network support doesn't support plugin and theme data sending.
7561
+ *
7562
+ * @author Vova Feldman (@svovaf)
7563
+ * @since 2.0.0
7564
+ *
7565
+ * @param string[] string $override
7566
+ * @param bool $only_diff
7567
+ * @param bool $include_plugins Since 1.1.8 by default include plugin changes.
7568
+ * @param bool $include_themes Since 1.1.8 by default include plugin changes.
7569
+ *
7570
+ * @return array
7571
+ */
7572
+ private function get_installs_data_for_api(
7573
+ array $override,
7574
+ $only_diff = false,
7575
+ $include_plugins = true,
7576
+ $include_themes = true
7577
+ ) {
7578
+ /**
7579
+ * @since 1.1.8 Also send plugin updates.
7580
+ */
7581
+ // if ( $include_plugins && ! isset( $override['plugins'] ) ) {
7582
+ // $plugins = $this->get_plugins_data_for_api();
7583
+ // if ( ! empty( $plugins ) ) {
7584
+ // $override['plugins'] = $plugins;
7585
+ // }
7586
+ // }
7587
+ /**
7588
+ * @since 1.1.8 Also send themes updates.
7589
+ */
7590
+ // if ( $include_themes && ! isset( $override['themes'] ) ) {
7591
+ // $themes = $this->get_themes_data_for_api();
7592
+ // if ( ! empty( $themes ) ) {
7593
+ // $override['themes'] = $themes;
7594
+ // }
7595
+ // }
7596
+
7597
+ // Common properties.
7598
+ $common = array_merge( array(
7599
+ 'version' => $this->get_plugin_version(),
7600
+ 'is_premium' => $this->is_premium(),
7601
+ 'sdk_version' => $this->version,
7602
+ 'programming_language_version' => phpversion(),
7603
+ 'platform_version' => get_bloginfo( 'version' ),
7604
+ ), $override );
7605
+
7606
+
7607
+ $is_common_diff_for_any_site = false;
7608
+ $common_diff_union = array();
7609
+
7610
+ $installs_data = array();
7611
+
7612
+ $sites = self::get_sites();
7613
+
7614
+ foreach ( $sites as $site ) {
7615
+ $blog_id = self::get_site_blog_id( $site );
7616
+
7617
+ $install = $this->get_install_by_blog_id( $blog_id );
7618
+
7619
+ if ( is_object( $install ) ) {
7620
+ if ( $install->user_id != $this->_user->id ) {
7621
+ // Install belongs to a different owner.
7622
+ continue;
7623
+ }
7624
+
7625
+ if ( ! $this->is_premium() && $install->is_tracking_prohibited() ) {
7626
+ // Don't send updates regarding opted-out installs.
7627
+ continue;
7628
+ }
7629
+
7630
+ $install_data = $this->get_site_info( $site );
7631
+
7632
+ $uid = $install_data['uid'];
7633
+
7634
+ unset( $install_data['blog_id'] );
7635
+ unset( $install_data['uid'] );
7636
+
7637
+ $install_data['is_disconnected'] = $install->is_disconnected;
7638
+ $install_data['is_active'] = $this->is_active_for_site( $blog_id );
7639
+ $install_data['is_uninstalled'] = $install->is_uninstalled;
7640
+
7641
+ $common_diff = null;
7642
+ $is_common_diff = false;
7643
+ if ( $only_diff ) {
7644
+ $install_data = $this->get_install_diff_for_api( $install_data, $install, $override );
7645
+ $common_diff = $this->get_install_diff_for_api( $common, $install, $override );
7646
+
7647
+ $is_common_diff = ! empty( $common_diff );
7648
+
7649
+ if ( $is_common_diff ) {
7650
+ foreach ( $common_diff as $k => $v ) {
7651
+ if ( ! isset( $common_diff_union[ $k ] ) ) {
7652
+ $common_diff_union[ $k ] = $v;
7653
+ }
7654
+ }
7655
+ }
7656
+
7657
+ $is_common_diff_for_any_site = $is_common_diff_for_any_site || $is_common_diff;
7658
+ }
7659
+
7660
+ if ( ! empty( $install_data ) || $is_common_diff ) {
7661
+ // Add install ID and site unique ID.
7662
+ $install_data['id'] = $install->id;
7663
+ $install_data['uid'] = $uid;
7664
+
7665
+ $installs_data[] = $install_data;
7666
+ }
7667
+ }
7668
+ }
7669
+
7670
+ restore_current_blog();
7671
+
7672
+ if ( 0 < count( $installs_data ) && ( $is_common_diff_for_any_site || ! $only_diff ) ) {
7673
+ if ( ! $only_diff ) {
7674
+ $installs_data[] = $common;
7675
+ } else if ( ! empty( $common_diff_union ) ) {
7676
+ $installs_data[] = $common_diff_union;
7677
+ }
7678
+ }
7679
+
7680
+ foreach ( $installs_data as &$data ) {
7681
+ $data = (object) $data;
7682
+ }
7683
+
7684
+ return $installs_data;
7685
+ }
7686
+
7687
+ /**
7688
+ * Compare site actual data to the stored install data and return the differences for an API data sync.
7689
+ *
7690
+ * @author Vova Feldman (@svovaf)
7691
+ * @since 2.0.0
7692
+ *
7693
+ * @param array $site
7694
+ * @param FS_Site $install
7695
+ * @param string[] string $override
7696
+ *
7697
+ * @return array
7698
+ */
7699
+ private function get_install_diff_for_api( $site, $install, $override = array() ) {
7700
+ $diff = array();
7701
+ $special = array();
7702
+ $special_override = false;
7703
+
7704
+ foreach ( $site as $p => $v ) {
7705
+ if ( property_exists( $install, $p ) ) {
7706
+ if ( ( is_bool( $install->{$p} ) || ! empty( $install->{$p} ) ) &&
7707
+ $install->{$p} != $v
7708
+ ) {
7709
+ $install->{$p} = $v;
7710
+ $diff[ $p ] = $v;
7711
+ }
7712
+ } else {
7713
+ $special[ $p ] = $v;
7714
+
7715
+ if ( isset( $override[ $p ] ) ||
7716
+ 'plugins' === $p ||
7717
+ 'themes' === $p
7718
+ ) {
7719
+ $special_override = true;
7720
+ }
7721
+ }
7722
+ }
7723
+
7724
+ if ( $special_override || 0 < count( $diff ) ) {
7725
+ // Add special params only if has at least one
7726
+ // standard param, or if explicitly requested to
7727
+ // override a special param or a param which is not exist
7728
+ // in the install object.
7729
+ $diff = array_merge( $diff, $special );
7730
+ }
7731
+
7732
+ return $diff;
7733
+ }
7734
+
7735
+ /**
7736
+ * Update install only if changed.
7737
+ *
7738
+ * @author Vova Feldman (@svovaf)
7739
+ * @since 1.0.9
7740
+ *
7741
+ * @param string[] string $override
7742
+ * @param bool $flush
7743
+ *
7744
+ * @return false|object|string
7745
+ */
7746
+ private function send_install_update( $override = array(), $flush = false ) {
7747
+ $this->_logger->entrance();
7748
+
7749
+ $check_properties = $this->get_install_data_for_api( $override );
7750
+
7751
+ if ( $flush ) {
7752
+ $params = $check_properties;
7753
+ } else {
7754
+ $params = $this->get_install_diff_for_api( $check_properties, $this->_site, $override );
7755
+ }
7756
+
7757
+ if ( 0 < count( $params ) ) {
7758
+ if ( ! is_multisite() ) {
7759
+ // Update last install sync timestamp.
7760
+ $this->set_cron_execution_timestamp( 'install_sync' );
7761
+ }
7762
+
7763
+ $params['uid'] = $this->get_anonymous_id();
7764
+
7765
+ // Send updated values to FS.
7766
+ $site = $this->get_api_site_scope()->call( '/', 'put', $params );
7767
+
7768
+ if ( $this->is_api_result_entity( $site ) ) {
7769
+ if ( ! is_multisite() ) {
7770
+ // I successfully sent install update, clear scheduled sync if exist.
7771
+ $this->clear_install_sync_cron();
7772
+ }
7773
+ }
7774
+
7775
+ return $site;
7776
+ }
7777
+
7778
+ return false;
7779
+ }
7780
+
7781
+ /**
7782
+ * Update installs only if changed.
7783
+ *
7784
+ * @author Vova Feldman (@svovaf)
7785
+ * @since 2.0.0
7786
+ *
7787
+ * @param string[] string $override
7788
+ * @param bool $flush
7789
+ *
7790
+ * @return false|object|string
7791
+ */
7792
+ private function send_installs_update( $override = array(), $flush = false ) {
7793
+ $this->_logger->entrance();
7794
+
7795
+ $installs_data = $this->get_installs_data_for_api( $override, ! $flush );
7796
+
7797
+ if ( empty( $installs_data ) ) {
7798
+ return false;
7799
+ }
7800
+
7801
+ // Update last install sync timestamp.
7802
+ $this->set_cron_execution_timestamp( 'install_sync' );
7803
+
7804
+ // Send updated values to FS.
7805
+ $result = $this->get_api_user_scope()->call( "/plugins/{$this->_plugin->id}/installs.json", 'put', $installs_data );
7806
+
7807
+ if ( $this->is_api_result_object( $result, 'installs' ) ) {
7808
+ // I successfully sent installs update, clear scheduled sync if exist.
7809
+ $this->clear_install_sync_cron();
7810
+ }
7811
+
7812
+ return $result;
7813
+ }
7814
+
7815
+ /**
7816
+ * Update install only if changed.
7817
+ *
7818
+ * @author Vova Feldman (@svovaf)
7819
+ * @since 1.0.9
7820
+ *
7821
+ * @param string[] string $override
7822
+ * @param bool $flush
7823
+ */
7824
+ private function sync_install( $override = array(), $flush = false ) {
7825
+ $this->_logger->entrance();
7826
+
7827
+ $site = $this->send_install_update( $override, $flush );
7828
+
7829
+ if ( false === $site ) {
7830
+ // No sync required.
7831
+ return;
7832
+ }
7833
+
7834
+ if ( ! $this->is_api_result_entity( $site ) ) {
7835
+ // Failed to sync, don't update locally.
7836
+ return;
7837
+ }
7838
+
7839
+ $this->_site = new FS_Site( $site );
7840
+
7841
+ $this->_store_site( true );
7842
+ }
7843
+
7844
+ /**
7845
+ * Update install only if changed.
7846
+ *
7847
+ * @author Vova Feldman (@svovaf)
7848
+ * @since 1.0.9
7849
+ *
7850
+ * @param string[] string $override
7851
+ * @param bool $flush
7852
+ */
7853
+ private function sync_installs( $override = array(), $flush = false ) {
7854
+ $this->_logger->entrance();
7855
+
7856
+ $result = $this->send_installs_update( $override, $flush );
7857
+
7858
+ if ( false === $result ) {
7859
+ // No sync required.
7860
+ return;
7861
+ }
7862
+
7863
+ if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
7864
+ // Failed to sync, don't update locally.
7865
+ return;
7866
+ }
7867
+
7868
+ $address_to_blog_map = $this->get_address_to_blog_map();
7869
+
7870
+ foreach ( $result->installs as $install ) {
7871
+ $this->_site = new FS_Site( $install );
7872
+
7873
+ $address = trailingslashit( fs_strip_url_protocol( $install->url ) );
7874
+ $blog_id = $address_to_blog_map[ $address ];
7875
+
7876
+ $this->_store_site( true, $blog_id );
7877
+ }
7878
+ }
7879
+
7880
+ /**
7881
+ * Track install's custom event.
7882
+ *
7883
+ * IMPORTANT:
7884
+ * Custom event tracking is currently only supported for specific clients.
7885
+ * If you are not one of them, please don't use this method. If you will,
7886
+ * the API will simply ignore your request based on the plugin ID.
7887
+ *
7888
+ * Need custom tracking for your plugin or theme?
7889
+ * If you are interested in custom event tracking please contact yo@freemius.com
7890
+ * for further details.
7891
+ *
7892
+ * @author Vova Feldman (@svovaf)
7893
+ * @since 1.2.1
7894
+ *
7895
+ * @param string $name Event name.
7896
+ * @param array $properties Associative key/value array with primitive values only
7897
+ * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s.
7898
+ * @param bool $once If true, event will be tracked only once. IMPORTANT: Still trigger the API call.
7899
+ *
7900
+ * @return object|false Event data or FALSE on failure.
7901
+ *
7902
+ * @throws \Freemius_InvalidArgumentException
7903
+ */
7904
+ public function track_event( $name, $properties = array(), $process_at = false, $once = false ) {
7905
+ $this->_logger->entrance( http_build_query( array( 'name' => $name, 'once' => $once ) ) );
7906
+
7907
+ if ( ! $this->is_registered() ) {
7908
+ return false;
7909
+ }
7910
+
7911
+ $event = array( 'type' => $name );
7912
+
7913
+ if ( is_numeric( $process_at ) && $process_at > time() ) {
7914
+ $event['process_at'] = $process_at;
7915
+ }
7916
+
7917
+ if ( $once ) {
7918
+ $event['once'] = true;
7919
+ }
7920
+
7921
+ if ( ! empty( $properties ) ) {
7922
+ // Verify associative array values are primitive.
7923
+ foreach ( $properties as $k => $v ) {
7924
+ if ( ! is_scalar( $v ) ) {
7925
+ throw new Freemius_InvalidArgumentException( 'The $properties argument must be an associative key/value array with primitive values only.' );
7926
+ }
7927
+ }
7928
+
7929
+ $event['properties'] = $properties;
7930
+ }
7931
+
7932
+ $result = $this->get_api_site_scope()->call( 'events.json', 'post', $event );
7933
+
7934
+ return $this->is_api_error( $result ) ?
7935
+ false :
7936
+ $result;
7937
+ }
7938
+
7939
+ /**
7940
+ * Track install's custom event only once, but it still triggers the API call.
7941
+ *
7942
+ * IMPORTANT:
7943
+ * Custom event tracking is currently only supported for specific clients.
7944
+ * If you are not one of them, please don't use this method. If you will,
7945
+ * the API will simply ignore your request based on the plugin ID.
7946
+ *
7947
+ * Need custom tracking for your plugin or theme?
7948
+ * If you are interested in custom event tracking please contact yo@freemius.com
7949
+ * for further details.
7950
+ *
7951
+ * @author Vova Feldman (@svovaf)
7952
+ * @since 1.2.1
7953
+ *
7954
+ * @param string $name Event name.
7955
+ * @param array $properties Associative key/value array with primitive values only
7956
+ * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s.
7957
+ *
7958
+ * @return object|false Event data or FALSE on failure.
7959
+ *
7960
+ * @throws \Freemius_InvalidArgumentException
7961
+ *
7962
+ * @user Freemius::track_event()
7963
+ */
7964
+ public function track_event_once( $name, $properties = array(), $process_at = false ) {
7965
+ return $this->track_event( $name, $properties, $process_at, true );
7966
+ }
7967
+
7968
+ /**
7969
+ * Plugin uninstall hook.
7970
+ *
7971
+ * @author Vova Feldman (@svovaf)
7972
+ * @since 1.0.1
7973
+ *
7974
+ * @param bool $check_user Enforce checking if user have plugins activation privileges.
7975
+ */
7976
+ function _uninstall_plugin_event( $check_user = true ) {
7977
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
7978
+
7979
+ if ( $check_user && ! current_user_can( 'activate_plugins' ) ) {
7980
+ return;
7981
+ }
7982
+
7983
+ $params = array();
7984
+ $uninstall_reason = null;
7985
+ if ( isset( $this->_storage->uninstall_reason ) ) {
7986
+ $uninstall_reason = $this->_storage->uninstall_reason;
7987
+ $params['reason_id'] = $uninstall_reason->id;
7988
+ $params['reason_info'] = $uninstall_reason->info;
7989
+ }
7990
+
7991
+ if ( ! $this->is_registered() ) {
7992
+ // Send anonymous uninstall event only if user submitted a feedback.
7993
+ if ( isset( $uninstall_reason ) ) {
7994
+ if ( isset( $uninstall_reason->is_anonymous ) && ! $uninstall_reason->is_anonymous ) {
7995
+ $this->opt_in( false, false, false, false, true );
7996
+ } else {
7997
+ $params['uid'] = $this->get_anonymous_id();
7998
+ $this->get_api_plugin_scope()->call( 'uninstall.json', 'put', $params );
7999
+ }
8000
+ }
8001
+ } else {
8002
+ $params = array_merge( $params, array(
8003
+ 'is_active' => false,
8004
+ 'is_uninstalled' => true,
8005
+ ) );
8006
+
8007
+ if ( $this->_is_network_active ) {
8008
+ // Send uninstall event.
8009
+ $this->send_installs_update( $params );
8010
+ } else {
8011
+ // Send uninstall event.
8012
+ $this->send_install_update( $params );
8013
+ }
8014
+ }
8015
+
8016
+ // @todo Decide if we want to delete plugin information from db.
8017
+ }
8018
+
8019
+ /**
8020
+ * @author Vova Feldman (@svovaf)
8021
+ * @since 1.1.1
8022
+ *
8023
+ * @return string
8024
+ */
8025
+ function premium_plugin_basename() {
8026
+ return "{$this->_slug}-premium/" . basename( $this->_free_plugin_basename );
8027
+ }
8028
+
8029
+ /**
8030
+ * Uninstall plugin hook. Called only when connected his account with Freemius for active sites tracking.
8031
+ *
8032
+ * @author Vova Feldman (@svovaf)
8033
+ * @since 1.0.2
8034
+ */
8035
+ public static function _uninstall_plugin_hook() {
8036
+ self::_load_required_static();
8037
+
8038
+ self::$_static_logger->entrance();
8039
+
8040
+ if ( ! current_user_can( 'activate_plugins' ) ) {
8041
+ return;
8042
+ }
8043
+
8044
+ $plugin_file = substr( current_filter(), strlen( 'uninstall_' ) );
8045
+
8046
+ self::$_static_logger->info( 'plugin = ' . $plugin_file );
8047
+
8048
+ define( 'WP_FS__UNINSTALL_MODE', true );
8049
+
8050
+ $fs = self::get_instance_by_file( $plugin_file );
8051
+
8052
+ if ( is_object( $fs ) ) {
8053
+ self::require_plugin_essentials();
8054
+
8055
+ if ( is_plugin_active( $fs->_free_plugin_basename ) ||
8056
+ is_plugin_active( $fs->premium_plugin_basename() )
8057
+ ) {
8058
+ // Deleting Free or Premium plugin version while the other version still installed.
8059
+ return;
8060
+ }
8061
+
8062
+ $fs->_uninstall_plugin_event();
8063
+
8064
+ $fs->do_action( 'after_uninstall' );
8065
+ }
8066
+ }
8067
+
8068
+ #----------------------------------------------------------------------------------
8069
+ #region Plugin Information
8070
+ #----------------------------------------------------------------------------------
8071
+
8072
+ /**
8073
+ * Load WordPress core plugin.php essential module.
8074
+ *
8075
+ * @author Vova Feldman (@svovaf)
8076
+ * @since 1.1.1
8077
+ */
8078
+ private static function require_plugin_essentials() {
8079
+ if ( ! function_exists( 'get_plugins' ) ) {
8080
+ self::$_static_logger->log( 'Including wp-admin/includes/plugin.php...' );
8081
+
8082
+ require_once ABSPATH . 'wp-admin/includes/plugin.php';
8083
+ }
8084
+ }
8085
+
8086
+ /**
8087
+ * Load WordPress core pluggable.php module.
8088
+ *
8089
+ * @author Vova Feldman (@svovaf)
8090
+ * @since 1.1.2
8091
+ */
8092
+ private static function require_pluggable_essentials() {
8093
+ if ( ! function_exists( 'wp_get_current_user' ) ) {
8094
+ require_once ABSPATH . 'wp-includes/pluggable.php';
8095
+ }
8096
+ }
8097
+
8098
+ /**
8099
+ * Return plugin data.
8100
+ *
8101
+ * @author Vova Feldman (@svovaf)
8102
+ * @since 1.0.1
8103
+ *
8104
+ * @return array
8105
+ */
8106
+ function get_plugin_data() {
8107
+ if ( ! isset( $this->_plugin_data ) ) {
8108
+ self::require_plugin_essentials();
8109
+
8110
+ if ( $this->is_plugin() ) {
8111
+ /**
8112
+ * @author Vova Feldman (@svovaf)
8113
+ * @since 1.2.0 When using get_plugin_data() do NOT translate plugin data.
8114
+ *
8115
+ * @link https://github.com/Freemius/wordpress-sdk/issues/77
8116
+ */
8117
+ $plugin_data = get_plugin_data(
8118
+ $this->_plugin_main_file_path,
8119
+ false,
8120
+ false
8121
+ );
8122
+ } else {
8123
+ $theme_data = wp_get_theme();
8124
+
8125
+ $plugin_data = array(
8126
+ 'Name' => $theme_data->get( 'Name' ),
8127
+ 'Version' => $theme_data->get( 'Version' ),
8128
+ 'Author' => $theme_data->get( 'Author' ),
8129
+ 'Description' => $theme_data->get( 'Description' ),
8130
+ 'PluginURI' => $theme_data->get( 'ThemeURI' ),
8131
+ );
8132
+ }
8133
+
8134
+ $this->_plugin_data = $plugin_data;
8135
+ }
8136
+
8137
+ return $this->_plugin_data;
8138
+ }
8139
+
8140
+ /**
8141
+ * @author Vova Feldman (@svovaf)
8142
+ * @since 1.0.1
8143
+ * @since 1.2.2.5 If slug not set load slug by module ID.
8144
+ *
8145
+ * @return string Plugin slug.
8146
+ */
8147
+ function get_slug() {
8148
+ if ( ! isset( $this->_slug ) ) {
8149
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
8150
+ $this->_slug = $id_slug_type_path_map[ $this->_module_id ]['slug'];
8151
+ }
8152
+
8153
+ return $this->_slug;
8154
+ }
8155
+
8156
+ /**
8157
+ * @author Vova Feldman (@svovaf)
8158
+ * @since 1.2.1.7
8159
+ *
8160
+ * @return string Plugin slug.
8161
+ */
8162
+ function get_target_folder_name() {
8163
+ return $this->_slug . ( $this->can_use_premium_code() ? '-premium' : '' );
8164
+ }
8165
+
8166
+ /**
8167
+ * @author Vova Feldman (@svovaf)
8168
+ * @since 1.0.1
8169
+ *
8170
+ * @return number Plugin ID.
8171
+ */
8172
+ function get_id() {
8173
+ return $this->_plugin->id;
8174
+ }
8175
+
8176
+ /**
8177
+ * @author Vova Feldman (@svovaf)
8178
+ * @since 1.2.1.5
8179
+ *
8180
+ * @return string Freemius SDK version
8181
+ */
8182
+ function get_sdk_version() {
8183
+ return $this->version;
8184
+ }
8185
+
8186
+ /**
8187
+ * @author Vova Feldman (@svovaf)
8188
+ * @since 1.2.1.5
8189
+ *
8190
+ * @return number Parent plugin ID (if parent exist).
8191
+ */
8192
+ function get_parent_id() {
8193
+ return $this->is_addon() ?
8194
+ $this->get_parent_instance()->get_id() :
8195
+ $this->_plugin->id;
8196
+ }
8197
+
8198
+ /**
8199
+ * @author Vova Feldman (@svovaf)
8200
+ * @since 1.0.1
8201
+ *
8202
+ * @return string Plugin public key.
8203
+ */
8204
+ function get_public_key() {
8205
+ return $this->_plugin->public_key;
8206
+ }
8207
+
8208
+ /**
8209
+ * Will be available only on sandbox mode.
8210
+ *
8211
+ * @author Vova Feldman (@svovaf)
8212
+ * @since 1.0.4
8213
+ *
8214
+ * @return mixed Plugin secret key.
8215
+ */
8216
+ function get_secret_key() {
8217
+ return $this->_plugin->secret_key;
8218
+ }
8219
+
8220
+ /**
8221
+ * @author Vova Feldman (@svovaf)
8222
+ * @since 1.1.1
8223
+ *
8224
+ * @return bool
8225
+ */
8226
+ function has_secret_key() {
8227
+ return ! empty( $this->_plugin->secret_key );
8228
+ }
8229
+
8230
+ /**
8231
+ * @author Vova Feldman (@svovaf)
8232
+ * @since 1.0.9
8233
+ *
8234
+ * @return string
8235
+ */
8236
+ function get_plugin_name() {
8237
+ $this->_logger->entrance();
8238
+
8239
+ if ( ! isset( $this->_plugin_name ) ) {
8240
+ $plugin_data = $this->get_plugin_data();
8241
+
8242
+ // Get name.
8243
+ $this->_plugin_name = $plugin_data['Name'];
8244
+
8245
+ // Check if plugin name contains "(Premium)" suffix and remove it.
8246
+ $suffix = ' (premium)';
8247
+ $suffix_len = strlen( $suffix );
8248
+
8249
+ if ( strlen( $plugin_data['Name'] ) > $suffix_len &&
8250
+ $suffix === substr( strtolower( $plugin_data['Name'] ), - $suffix_len )
8251
+ ) {
8252
+ $this->_plugin_name = substr( $plugin_data['Name'], 0, - $suffix_len );
8253
+ }
8254
+
8255
+ $this->_logger->departure( 'Name = ' . $this->_plugin_name );
8256
+ }
8257
+
8258
+ return $this->_plugin_name;
8259
+ }
8260
+
8261
+ /**
8262
+ * @author Vova Feldman (@svovaf)
8263
+ * @since 1.0.0
8264
+ *
8265
+ * @return string
8266
+ */
8267
+ function get_plugin_version() {
8268
+ $this->_logger->entrance();
8269
+
8270
+ $plugin_data = $this->get_plugin_data();
8271
+
8272
+ $this->_logger->departure( 'Version = ' . $plugin_data['Version'] );
8273
+
8274
+ return $this->apply_filters( 'plugin_version', $plugin_data['Version'] );
8275
+ }
8276
+
8277
+ /**
8278
+ * @author Vova Feldman (@svovaf)
8279
+ * @since 1.2.1.7
8280
+ *
8281
+ * @return string
8282
+ */
8283
+ function get_plugin_title() {
8284
+ $this->_logger->entrance();
8285
+
8286
+ $title = $this->_plugin->title;
8287
+
8288
+ return $this->apply_filters( 'plugin_title', $title );
8289
+ }
8290
+
8291
+ /**
8292
+ * @author Vova Feldman (@svovaf)
8293
+ * @since 1.2.2.7
8294
+ *
8295
+ * @param bool $lowercase
8296
+ *
8297
+ * @return string
8298
+ */
8299
+ function get_module_label( $lowercase = false ) {
8300
+ $label = $this->is_addon() ?
8301
+ $this->get_text_inline( 'Add-On', 'addon' ) :
8302
+ ( $this->is_plugin() ?
8303
+ $this->get_text_inline( 'Plugin', 'plugin' ) :
8304
+ $this->get_text_inline( 'Theme', 'theme' ) );
8305
+
8306
+ if ( $lowercase ) {
8307
+ $label = strtolower( $label );
8308
+ }
8309
+
8310
+ return $label;
8311
+ }
8312
+
8313
+ /**
8314
+ * @author Vova Feldman (@svovaf)
8315
+ * @since 1.0.4
8316
+ *
8317
+ * @return string
8318
+ */
8319
+ function get_plugin_basename() {
8320
+ if ( ! isset( $this->_plugin_basename ) ) {
8321
+ if ( $this->is_plugin() ) {
8322
+ $this->_plugin_basename = plugin_basename( $this->_plugin_main_file_path );
8323
+ } else {
8324
+ $this->_plugin_basename = basename( dirname( $this->_plugin_main_file_path ) );
8325
+ }
8326
+ }
8327
+
8328
+ return $this->_plugin_basename;
8329
+ }
8330
+
8331
+ function get_plugin_folder_name() {
8332
+ $this->_logger->entrance();
8333
+
8334
+ $plugin_folder = $this->_plugin_basename;
8335
+
8336
+ while ( '.' !== dirname( $plugin_folder ) ) {
8337
+ $plugin_folder = dirname( $plugin_folder );
8338
+ }
8339
+
8340
+ $this->_logger->departure( 'Folder Name = ' . $plugin_folder );
8341
+
8342
+ return $plugin_folder;
8343
+ }
8344
+
8345
+ #endregion ------------------------------------------------------------------
8346
+
8347
+ /* Account
8348
+ ------------------------------------------------------------------------------------------------------------------*/
8349
+
8350
+ /**
8351
+ * Find plugin's slug by plugin's basename.
8352
+ *
8353
+ * @author Vova Feldman (@svovaf)
8354
+ * @since 1.0.9
8355
+ *
8356
+ * @param string $plugin_base_name
8357
+ *
8358
+ * @return false|string
8359
+ */
8360
+ private static function find_slug_by_basename( $plugin_base_name ) {
8361
+ $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
8362
+
8363
+ if ( ! array( $file_slug_map ) || ! isset( $file_slug_map[ $plugin_base_name ] ) ) {
8364
+ return false;
8365
+ }
8366
+
8367
+ return $file_slug_map[ $plugin_base_name ];
8368
+ }
8369
+
8370
+ /**
8371
+ * Store the map between the plugin's basename to the slug.
8372
+ *
8373
+ * @author Vova Feldman (@svovaf)
8374
+ * @since 1.0.9
8375
+ */
8376
+ private function store_file_slug_map() {
8377
+ $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
8378
+
8379
+ if ( ! array( $file_slug_map ) ) {
8380
+ $file_slug_map = array();
8381
+ }
8382
+
8383
+ if ( ! isset( $file_slug_map[ $this->_plugin_basename ] ) ||
8384
+ $file_slug_map[ $this->_plugin_basename ] !== $this->_slug
8385
+ ) {
8386
+ $file_slug_map[ $this->_plugin_basename ] = $this->_slug;
8387
+ self::$_accounts->set_option( 'file_slug_map', $file_slug_map, true );
8388
+ }
8389
+ }
8390
+
8391
+ /**
8392
+ * @return array[number]FS_User
8393
+ */
8394
+ static function get_all_users() {
8395
+ $users = self::$_accounts->get_option( 'users', array() );
8396
+
8397
+ if ( ! is_array( $users ) ) {
8398
+ $users = array();
8399
+ }
8400
+
8401
+ return $users;
8402
+ }
8403
+
8404
+ /**
8405
+ * @param string $module_type
8406
+ * @param null|int $blog_id Since 2.0.0
8407
+ *
8408
+ * @return array[string]FS_Site
8409
+ */
8410
+ private static function get_all_sites(
8411
+ $module_type = WP_FS__MODULE_TYPE_PLUGIN,
8412
+ $blog_id = null
8413
+ ) {
8414
+ $sites = self::get_account_option( 'sites', $module_type, $blog_id );
8415
+
8416
+ if ( ! is_array( $sites ) ) {
8417
+ $sites = array();
8418
+ }
8419
+
8420
+ return $sites;
8421
+ }
8422
+
8423
+ /**
8424
+ * @author Leo Fajardo (@leorw)
8425
+ *
8426
+ * @since 1.2.2
8427
+ *
8428
+ * @param string $option_name
8429
+ * @param string $module_type
8430
+ * @param null|int $network_level_or_blog_id Since 2.0.0
8431
+ *
8432
+ * @return mixed
8433
+ */
8434
+ private static function get_account_option( $option_name, $module_type = null, $network_level_or_blog_id = null ) {
8435
+ if ( ! is_null( $module_type ) && WP_FS__MODULE_TYPE_PLUGIN !== $module_type ) {
8436
+ $option_name = $module_type . '_' . $option_name;
8437
+ }
8438
+
8439
+ return self::$_accounts->get_option( $option_name, array(), $network_level_or_blog_id );
8440
+ }
8441
+
8442
+ /**
8443
+ * @author Leo Fajardo (@leorw)
8444
+ *
8445
+ * @since 1.2.2
8446
+ *
8447
+ * @param string $option_name
8448
+ * @param mixed $option_value
8449
+ * @param bool $store
8450
+ * @param null|int $network_level_or_blog_id Since 2.0.0
8451
+ */
8452
+ private function set_account_option( $option_name, $option_value, $store, $network_level_or_blog_id = null ) {
8453
+ self::set_account_option_by_module(
8454
+ $this->_module_type,
8455
+ $option_name,
8456
+ $option_value,
8457
+ $store,
8458
+ $network_level_or_blog_id
8459
+ );
8460
+ }
8461
+
8462
+ /**
8463
+ * @author Vova Feldman (@svovaf)
8464
+ *
8465
+ * @since 1.2.2.7
8466
+ *
8467
+ * @param string $module_type
8468
+ * @param string $option_name
8469
+ * @param mixed $option_value
8470
+ * @param bool $store
8471
+ * @param null|int $network_level_or_blog_id Since 2.0.0
8472
+ */
8473
+ private static function set_account_option_by_module(
8474
+ $module_type,
8475
+ $option_name,
8476
+ $option_value,
8477
+ $store,
8478
+ $network_level_or_blog_id = null
8479
+ ) {
8480
+ if ( WP_FS__MODULE_TYPE_PLUGIN != $module_type ) {
8481
+ $option_name = $module_type . '_' . $option_name;
8482
+ }
8483
+
8484
+ self::$_accounts->set_option( $option_name, $option_value, $store, $network_level_or_blog_id );
8485
+ }
8486
+
8487
+ /**
8488
+ * @author Vova Feldman (@svovaf)
8489
+ * @since 1.0.6
8490
+ *
8491
+ * @param number|null $module_id
8492
+ *
8493
+ * @return FS_Plugin_License[]
8494
+ */
8495
+ private static function get_all_licenses( $module_id = null ) {
8496
+ $licenses = self::get_account_option( 'all_licenses' );
8497
+
8498
+ if ( ! is_array( $licenses ) ) {
8499
+ $licenses = array();
8500
+ }
8501
+
8502
+ if ( is_null( $module_id ) ) {
8503
+ return $licenses;
8504
+ }
8505
+
8506
+ $licenses = isset( $licenses[ $module_id ] ) ?
8507
+ $licenses[ $module_id ] :
8508
+ array();
8509
+
8510
+ return $licenses;
8511
+ }
8512
+
8513
+ /**
8514
+ * @author Leo Fajardo (@leorw)
8515
+ * @since 2.0.0
8516
+ *
8517
+ * @param number $module_id
8518
+ * @param number|null $user_id
8519
+ *
8520
+ * @return array
8521
+ */
8522
+ private static function get_user_id_license_ids_map( $module_id, $user_id = null ) {
8523
+ $all_modules_user_id_license_ids_map = self::get_account_option( 'user_id_license_ids_map' );
8524
+
8525
+ if ( ! is_array( $all_modules_user_id_license_ids_map ) ) {
8526
+ $all_modules_user_id_license_ids_map = array();
8527
+ }
8528
+
8529
+ $user_id_license_ids_map = isset( $all_modules_user_id_license_ids_map[ $module_id ] ) ?
8530
+ $all_modules_user_id_license_ids_map[ $module_id ] :
8531
+ array();
8532
+
8533
+ if ( FS_User::is_valid_id( $user_id ) ) {
8534
+ $user_id_license_ids_map = isset( $user_id_license_ids_map[ $user_id ] ) ?
8535
+ $user_id_license_ids_map[ $user_id ] :
8536
+ array();
8537
+ }
8538
+
8539
+ return $user_id_license_ids_map;
8540
+ }
8541
+
8542
+ /**
8543
+ * @author Leo Fajardo (@leorw)
8544
+ * @since 2.0.0
8545
+ *
8546
+ * @param array $new_user_id_license_ids_map
8547
+ * @param number $module_id
8548
+ * @param number|null $user_id
8549
+ */
8550
+ private static function store_user_id_license_ids_map( $new_user_id_license_ids_map, $module_id, $user_id = null ) {
8551
+ $all_modules_user_id_license_ids_map = self::get_account_option( 'user_id_license_ids_map' );
8552
+ if ( ! is_array( $all_modules_user_id_license_ids_map ) ) {
8553
+ $all_modules_user_id_license_ids_map = array();
8554
+ }
8555
+
8556
+ if ( ! isset( $all_modules_user_id_license_ids_map[ $module_id ] ) ) {
8557
+ $all_modules_user_id_license_ids_map[ $module_id ] = array();
8558
+ }
8559
+
8560
+ if ( FS_User::is_valid_id( $user_id ) ) {
8561
+ $all_modules_user_id_license_ids_map[ $module_id ][ $user_id ] = $new_user_id_license_ids_map;
8562
+ } else {
8563
+ $all_modules_user_id_license_ids_map[ $module_id ] = $new_user_id_license_ids_map;
8564
+ }
8565
+
8566
+ self::$_accounts->set_option( 'user_id_license_ids_map', $all_modules_user_id_license_ids_map, true );
8567
+ }
8568
+
8569
+ /**
8570
+ * Get a collection of the user's linked license IDs.
8571
+ *
8572
+ * @author Vova Feldman (@svovaf)
8573
+ * @since 2.0.0
8574
+ *
8575
+ * @param number $user_id
8576
+ *
8577
+ * @return number[]
8578
+ */
8579
+ private function get_user_linked_license_ids( $user_id ) {
8580
+ return self::get_user_id_license_ids_map( $this->_module_id, $user_id );
8581
+ }
8582
+
8583
+ /**
8584
+ * Override the user's linked license IDs with a new IDs collection.
8585
+ *
8586
+ * @author Vova Feldman (@svovaf)
8587
+ * @since 2.0.0
8588
+ *
8589
+ * @param number $user_id
8590
+ * @param number[] $license_ids
8591
+ */
8592
+ private function set_user_linked_license_ids( $user_id, array $license_ids ) {
8593
+ self::store_user_id_license_ids_map( $license_ids, $this->_module_id, $user_id );
8594
+ }
8595
+
8596
+ /**
8597
+ * Link a specified license ID to a given user.
8598
+ *
8599
+ * @author Vova Feldman (@svovaf)
8600
+ * @since 2.0.0
8601
+ *
8602
+ * @param number $license_id
8603
+ * @param number $user_id
8604
+ */
8605
+ private function link_license_2_user( $license_id, $user_id ) {
8606
+ $license_ids = $this->get_user_linked_license_ids( $user_id );
8607
+
8608
+ if ( in_array( $license_id, $license_ids ) ) {
8609
+ // License already linked.
8610
+ return;
8611
+ }
8612
+
8613
+ $license_ids[] = $license_id;
8614
+
8615
+ $this->set_user_linked_license_ids( $user_id, $license_ids );
8616
+ }
8617
+
8618
+ /**
8619
+ * @param string|bool $module_type
8620
+ *
8621
+ * @return FS_Plugin_Plan[]
8622
+ */
8623
+ private static function get_all_plans( $module_type = false ) {
8624
+ $plans = self::get_account_option( 'plans', $module_type );
8625
+
8626
+ if ( ! is_array( $plans ) ) {
8627
+ $plans = array();
8628
+ }
8629
+
8630
+ return $plans;
8631
+ }
8632
+
8633
+ /**
8634
+ * @author Vova Feldman (@svovaf)
8635
+ * @since 1.0.4
8636
+ *
8637
+ * @return FS_Plugin_Tag[]
8638
+ */
8639
+ private static function get_all_updates() {
8640
+ $updates = self::$_accounts->get_option( 'updates', array() );
8641
+
8642
+ if ( ! is_array( $updates ) ) {
8643
+ $updates = array();
8644
+ }
8645
+
8646
+ return $updates;
8647
+ }
8648
+
8649
+ /**
8650
+ * @author Vova Feldman (@svovaf)
8651
+ * @since 1.0.6
8652
+ *
8653
+ * @return array<number,FS_Plugin[]>|false
8654
+ */
8655
+ private static function get_all_addons() {
8656
+ $addons = self::$_accounts->get_option( 'addons', array() );
8657
+
8658
+ if ( ! is_array( $addons ) ) {
8659
+ $addons = array();
8660
+ }
8661
+
8662
+ return $addons;
8663
+ }
8664
+
8665
+ /**
8666
+ * @author Vova Feldman (@svovaf)
8667
+ * @since 1.0.6
8668
+ *
8669
+ * @return FS_Plugin[]|false
8670
+ */
8671
+ private static function get_all_account_addons() {
8672
+ $addons = self::$_accounts->get_option( 'account_addons', array() );
8673
+
8674
+ if ( ! is_array( $addons ) ) {
8675
+ $addons = array();
8676
+ }
8677
+
8678
+ return $addons;
8679
+ }
8680
+
8681
+ /**
8682
+ * Check if user has connected his account (opted-in).
8683
+ *
8684
+ * Note:
8685
+ * If the user opted-in and opted-out on a later stage,
8686
+ * this will still return true. If you want to check if the
8687
+ * user is currently opted-in, use:
8688
+ * `$fs->is_registered() && $fs->is_tracking_allowed()`
8689
+ *
8690
+ * @author Vova Feldman (@svovaf)
8691
+ * @since 1.0.1
8692
+ * @return bool
8693
+ */
8694
+ function is_registered() {
8695
+ return is_object( $this->_user );
8696
+ }
8697
+
8698
+ /**
8699
+ * Returns TRUE if the user opted-in and didn't disconnect (opt-out).
8700
+ *
8701
+ * @author Leo Fajardo (@leorw)
8702
+ * @since 1.2.1.5
8703
+ *
8704
+ * @return bool
8705
+ */
8706
+ function is_tracking_allowed() {
8707
+ return ( is_object( $this->_site ) && $this->_site->is_tracking_allowed() );
8708
+ }
8709
+
8710
+ /**
8711
+ * @author Vova Feldman (@svovaf)
8712
+ * @since 1.0.4
8713
+ *
8714
+ * @return FS_Plugin
8715
+ */
8716
+ function get_plugin() {
8717
+ return $this->_plugin;
8718
+ }
8719
+
8720
+ /**
8721
+ * @author Vova Feldman (@svovaf)
8722
+ * @since 1.0.3
8723
+ *
8724
+ * @return FS_User
8725
+ */
8726
+ function get_user() {
8727
+ return $this->_user;
8728
+ }
8729
+
8730
+ /**
8731
+ * @author Vova Feldman (@svovaf)
8732
+ * @since 1.0.3
8733
+ *
8734
+ * @return FS_Site
8735
+ */
8736
+ function get_site() {
8737
+ return $this->_site;
8738
+ }
8739
+
8740
+ /**
8741
+ * Get plugin add-ons.
8742
+ *
8743
+ * @author Vova Feldman (@svovaf)
8744
+ * @since 1.0.6
8745
+ *
8746
+ * @since 1.1.7.3 If not yet loaded, fetch data from the API.
8747
+ *
8748
+ * @param bool $flush
8749
+ *
8750
+ * @return FS_Plugin[]|false
8751
+ */
8752
+ function get_addons( $flush = false ) {
8753
+ $this->_logger->entrance();
8754
+
8755
+ if ( ! $this->_has_addons ) {
8756
+ return false;
8757
+ }
8758
+
8759
+ $addons = $this->sync_addons( $flush );
8760
+
8761
+ return ( ! is_array( $addons ) || empty( $addons ) ) ?
8762
+ false :
8763
+ $addons;
8764
+ }
8765
+
8766
+ /**
8767
+ * @author Vova Feldman (@svovaf)
8768
+ * @since 1.0.6
8769
+ *
8770
+ * @return FS_Plugin[]|false
8771
+ */
8772
+ function get_account_addons() {
8773
+ $this->_logger->entrance();
8774
+
8775
+ $addons = self::get_all_account_addons();
8776
+
8777
+ if ( ! is_array( $addons ) ||
8778
+ ! isset( $addons[ $this->_plugin->id ] ) ||
8779
+ ! is_array( $addons[ $this->_plugin->id ] ) ||
8780
+ 0 === count( $addons[ $this->_plugin->id ] )
8781
+ ) {
8782
+ return false;
8783
+ }
8784
+
8785
+ return $addons[ $this->_plugin->id ];
8786
+ }
8787
+
8788
+ /**
8789
+ * Check if user has any
8790
+ *
8791
+ * @author Vova Feldman (@svovaf)
8792
+ * @since 1.1.6
8793
+ *
8794
+ * @return bool
8795
+ */
8796
+ function has_account_addons() {
8797
+ $addons = $this->get_account_addons();
8798
+
8799
+ return is_array( $addons ) && ( 0 < count( $addons ) );
8800
+ }
8801
+
8802
+
8803
+ /**
8804
+ * Get add-on by ID (from local data).
8805
+ *
8806
+ * @author Vova Feldman (@svovaf)
8807
+ * @since 1.0.6
8808
+ *
8809
+ * @param number $id
8810
+ *
8811
+ * @return FS_Plugin|false
8812
+ */
8813
+ function get_addon( $id ) {
8814
+ $this->_logger->entrance();
8815
+
8816
+ $addons = $this->get_addons();
8817
+
8818
+ if ( is_array( $addons ) ) {
8819
+ foreach ( $addons as $addon ) {
8820
+ if ( $id == $addon->id ) {
8821
+ return $addon;
8822
+ }
8823
+ }
8824
+ }
8825
+
8826
+ return false;
8827
+ }
8828
+
8829
+ /**
8830
+ * Get add-on by slug (from local data).
8831
+ *
8832
+ * @author Vova Feldman (@svovaf)
8833
+ * @since 1.0.6
8834
+ *
8835
+ * @param string $slug
8836
+ *
8837
+ * @param bool $flush
8838
+ *
8839
+ * @return FS_Plugin|false
8840
+ */
8841
+ function get_addon_by_slug( $slug, $flush = false ) {
8842
+ $this->_logger->entrance();
8843
+
8844
+ $addons = $this->get_addons( $flush );
8845
+
8846
+ if ( is_array( $addons ) ) {
8847
+ foreach ( $addons as $addon ) {
8848
+ if ( $slug === $addon->slug ) {
8849
+ return $addon;
8850
+ }
8851
+ }
8852
+ }
8853
+
8854
+ return false;
8855
+ }
8856
+
8857
+ /**
8858
+ * @author Vova Feldman (@svovaf)
8859
+ * @since 2.0.0
8860
+ *
8861
+ * @param number $user_id
8862
+ *
8863
+ * @return FS_User
8864
+ */
8865
+ static function _get_user_by_id( $user_id ) {
8866
+ self::$_static_logger->entrance( "user_id = {$user_id}" );
8867
+
8868
+ $users = self::get_all_users();
8869
+
8870
+ if ( is_array( $users ) ) {
8871
+ if ( isset( $users[ $user_id ] ) &&
8872
+ $users[ $user_id ] instanceof FS_User &&
8873
+ $user_id == $users[ $user_id ]->id
8874
+ ) {
8875
+ return $users[ $user_id ];
8876
+ }
8877
+
8878
+ // If user wasn't found by the key, iterate over all the users collection.
8879
+ foreach ( $users as $user ) {
8880
+ /**
8881
+ * @var FS_User $user
8882
+ */
8883
+ if ( $user_id == $user->id ) {
8884
+ return $user;
8885
+ }
8886
+ }
8887
+ }
8888
+
8889
+ return null;
8890
+ }
8891
+
8892
+ /**
8893
+ * Checks if a Freemius user_id is associated with a super-admin.
8894
+ *
8895
+ * @author Vova Feldman (@svovaf)
8896
+ * @since 2.0.0
8897
+ *
8898
+ * @param number $user_id
8899
+ *
8900
+ * @return bool
8901
+ */
8902
+ private static function is_super_admin( $user_id ) {
8903
+ $is_super_admin = false;
8904
+
8905
+ $user = self::_get_user_by_id( $user_id );
8906
+
8907
+ if ( $user instanceof FS_User && ! empty( $user->email ) ) {
8908
+ self::require_pluggable_essentials();
8909
+
8910
+ $wp_user = get_user_by( 'email', $user->email );
8911
+
8912
+ if ( $wp_user instanceof WP_User ) {
8913
+ $super_admins = get_super_admins();
8914
+ $is_super_admin = ( is_array( $super_admins ) && in_array( $wp_user->user_login, $super_admins ) );
8915
+ }
8916
+ }
8917
+
8918
+ return $is_super_admin;
8919
+ }
8920
+
8921
+ #----------------------------------------------------------------------------------
8922
+ #region Plans & Licensing
8923
+ #----------------------------------------------------------------------------------
8924
+
8925
+ /**
8926
+ * Check if running premium plugin code.
8927
+ *
8928
+ * @author Vova Feldman (@svovaf)
8929
+ * @since 1.0.5
8930
+ *
8931
+ * @return bool
8932
+ */
8933
+ function is_premium() {
8934
+ return $this->_plugin->is_premium;
8935
+ }
8936
+
8937
+ /**
8938
+ * Get site's plan ID.
8939
+ *
8940
+ * @author Vova Feldman (@svovaf)
8941
+ * @since 1.0.2
8942
+ *
8943
+ * @return number
8944
+ */
8945
+ function get_plan_id() {
8946
+ return $this->_site->plan_id;
8947
+ }
8948
+
8949
+ /**
8950
+ * Get site's plan title.
8951
+ *
8952
+ * @author Vova Feldman (@svovaf)
8953
+ * @since 1.0.2
8954
+ *
8955
+ * @return string
8956
+ */
8957
+ function get_plan_title() {
8958
+ $plan = $this->get_plan();
8959
+
8960
+ return is_object( $plan ) ? $plan->title : 'PLAN_TITLE';
8961
+ }
8962
+
8963
+ /**
8964
+ * Get site's plan name.
8965
+ *
8966
+ * @author Vova Feldman (@svovaf)
8967
+ * @since 2.0.0
8968
+ *
8969
+ * @return string
8970
+ */
8971
+ function get_plan_name() {
8972
+ $plan = $this->get_plan();
8973
+
8974
+ return is_object( $plan ) ? $plan->name : 'PLAN_NAME';
8975
+ }
8976
+
8977
+ /**
8978
+ * @author Vova Feldman (@svovaf)
8979
+ * @since 1.0.9
8980
+ *
8981
+ * @return FS_Plugin_Plan|false
8982
+ */
8983
+ function get_plan() {
8984
+ if ( ! is_object( $this->_site ) ) {
8985
+ return false;
8986
+ }
8987
+
8988
+ return FS_Plugin_Plan::is_valid_id( $this->_site->plan_id ) ?
8989
+ $this->_get_plan_by_id( $this->_site->plan_id ) :
8990
+ false;
8991
+ }
8992
+
8993
+ /**
8994
+ * @author Vova Feldman (@svovaf)
8995
+ * @since 1.0.3
8996
+ *
8997
+ * @return bool
8998
+ */
8999
+ function is_trial() {
9000
+ $this->_logger->entrance();
9001
+
9002
+ if ( ! $this->is_registered() ) {
9003
+ return false;
9004
+ }
9005
+
9006
+ return $this->_site->is_trial();
9007
+ }
9008
+
9009
+ /**
9010
+ * Check if currently in a trial with payment method (credit card or paypal).
9011
+ *
9012
+ * @author Vova Feldman (@svovaf)
9013
+ * @since 1.1.7
9014
+ *
9015
+ * @return bool
9016
+ */
9017
+ function is_paid_trial() {
9018
+ $this->_logger->entrance();
9019
+
9020
+ if ( ! $this->is_trial() ) {
9021
+ return false;
9022
+ }
9023
+
9024
+ return $this->has_active_valid_license() && ( $this->_site->trial_plan_id == $this->_license->plan_id );
9025
+ }
9026
+
9027
+ /**
9028
+ * Check if trial already utilized.
9029
+ *
9030
+ * @since 1.0.9
9031
+ *
9032
+ * @return bool
9033
+ */
9034
+ function is_trial_utilized() {
9035
+ $this->_logger->entrance();
9036
+
9037
+ if ( ! $this->is_registered() ) {
9038
+ return false;
9039
+ }
9040
+
9041
+ return $this->_site->is_trial_utilized();
9042
+ }
9043
+
9044
+ /**
9045
+ * Get trial plan information (if in trial).
9046
+ *
9047
+ * @author Vova Feldman (@svovaf)
9048
+ * @since 1.0.9
9049
+ *
9050
+ * @return bool|FS_Plugin_Plan
9051
+ */
9052
+ function get_trial_plan() {
9053
+ $this->_logger->entrance();
9054
+
9055
+ if ( ! $this->is_trial() ) {
9056
+ return false;
9057
+ }
9058
+
9059
+ // Try to load plan from local cache.
9060
+ $trial_plan = $this->_get_plan_by_id( $this->_site->trial_plan_id );
9061
+
9062
+ if ( ! is_object( $trial_plan ) ) {
9063
+ $trial_plan = $this->_fetch_site_plan( $this->_site->trial_plan_id );
9064
+
9065
+ /**
9066
+ * If managed to fetch the plan, add it to the plans collection.
9067
+ */
9068
+ if ( $trial_plan instanceof FS_Plugin_Plan ) {
9069
+ if ( ! is_array( $this->_plans ) ) {
9070
+ $this->_plans = array();
9071
+ }
9072
+
9073
+ $this->_plans[] = $trial_plan;
9074
+ $this->_store_plans();
9075
+ }
9076
+ }
9077
+
9078
+ if ( $trial_plan instanceof FS_Plugin_Plan ) {
9079
+ return $trial_plan;
9080
+ }
9081
+
9082
+ /**
9083
+ * If for some reason failed to get the trial plan, fallback to a dummy name and title.
9084
+ */
9085
+ $trial_plan = new FS_Plugin_Plan();
9086
+ $trial_plan->id = $this->_site->trial_plan_id;
9087
+ $trial_plan->name = 'pro';
9088
+ $trial_plan->title = 'Pro';
9089
+
9090
+ return $trial_plan;
9091
+ }
9092
+
9093
+ /**
9094
+ * Check if the user has an activate, non-expired license on current plugin's install.
9095
+ *
9096
+ * @since 1.0.9
9097
+ *
9098
+ * @return bool
9099
+ */
9100
+ function is_paying() {
9101
+ $this->_logger->entrance();
9102
+
9103
+ if ( ! $this->is_registered() ) {
9104
+ return false;
9105
+ }
9106
+
9107
+ if ( ! $this->has_paid_plan() ) {
9108
+ return false;
9109
+ }
9110
+
9111
+ return (
9112
+ ! $this->is_trial() &&
9113
+ 'free' !== $this->get_plan_name() &&
9114
+ $this->has_active_valid_license()
9115
+ );
9116
+ }
9117
+
9118
+ /**
9119
+ * @author Vova Feldman (@svovaf)
9120
+ * @since 1.0.4
9121
+ *
9122
+ * @return bool
9123
+ */
9124
+ function is_free_plan() {
9125
+ if ( ! $this->is_registered() ) {
9126
+ return true;
9127
+ }
9128
+
9129
+ if ( ! $this->has_paid_plan() ) {
9130
+ return true;
9131
+ }
9132
+
9133
+ return (
9134
+ 'free' === $this->get_plan_name() ||
9135
+ ! $this->has_features_enabled_license()
9136
+ );
9137
+ }
9138
+
9139
+ /**
9140
+ * @author Vova Feldman (@svovaf)
9141
+ * @since 1.0.5
9142
+ *
9143
+ * @return bool
9144
+ */
9145
+ function _has_premium_license() {
9146
+ $this->_logger->entrance();
9147
+
9148
+ $premium_license = $this->_get_available_premium_license();
9149
+
9150
+ return ( false !== $premium_license );
9151
+ }
9152
+
9153
+ /**
9154
+ * Check if user has any licenses associated with the plugin (including expired or blocking).
9155
+ *
9156
+ * @author Vova Feldman (@svovaf)
9157
+ * @since 1.1.7.3
9158
+ *
9159
+ * @return bool
9160
+ */
9161
+ private function has_any_license() {
9162
+ return is_array( $this->_licenses ) && ( 0 < count( $this->_licenses ) );
9163
+ }
9164
+
9165
+ /**
9166
+ * @author Vova Feldman (@svovaf)
9167
+ * @since 1.0.5
9168
+ *
9169
+ * @param bool|null $is_localhost
9170
+ *
9171
+ * @return FS_Plugin_License|false
9172
+ */
9173
+ function _get_available_premium_license( $is_localhost = null ) {
9174
+ $this->_logger->entrance();
9175
+
9176
+ $licenses = $this->get_available_premium_licenses( $is_localhost );
9177
+ if ( ! empty( $licenses ) ) {
9178
+ return $licenses[0];
9179
+ }
9180
+
9181
+ return false;
9182
+ }
9183
+
9184
+ /**
9185
+ * @author Vova Feldman (@svovaf)
9186
+ * @since 1.0.5
9187
+ *
9188
+ * @param bool|null $is_localhost
9189
+ *
9190
+ * @return FS_Plugin_License[]
9191
+ */
9192
+ function get_available_premium_licenses( $is_localhost = null ) {
9193
+ $this->_logger->entrance();
9194
+
9195
+ $licenses = array();
9196
+ if ( ! $this->has_paid_plan() ) {
9197
+ return $licenses;
9198
+ }
9199
+
9200
+ if ( is_array( $this->_licenses ) ) {
9201
+ foreach ( $this->_licenses as $license ) {
9202
+ if ( ! $license->can_activate( $is_localhost ) ) {
9203
+ continue;
9204
+ }
9205
+
9206
+ $licenses[] = $license;
9207
+ }
9208
+ }
9209
+
9210
+ return $licenses;
9211
+ }
9212
+
9213
+ /**
9214
+ * Sync local plugin plans with remote server.
9215
+ *
9216
+ * 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).
9217
+ *
9218
+ * @author Vova Feldman (@svovaf)
9219
+ * @since 1.0.5
9220
+ *
9221
+ * @return FS_Plugin_Plan[]|object
9222
+ */
9223
+ function _sync_plans() {
9224
+ $plans = $this->_fetch_plugin_plans();
9225
+
9226
+ if ( $this->is_array_instanceof( $plans, 'FS_Plugin_Plan' ) ) {
9227
+ $plans_map = array();
9228
+ foreach ( $plans as $plan ) {
9229
+ $plans_map[ $plan->id ] = true;
9230
+ }
9231
+
9232
+ $plans_ids_to_keep = $this->get_plans_ids_associated_with_installs();
9233
+
9234
+ foreach ( $plans_ids_to_keep as $plan_id ) {
9235
+ if ( isset( $plans_map[ $plan_id ] ) ) {
9236
+ continue;
9237
+ }
9238
+
9239
+ $missing_plan = self::_get_plan_by_id( $plan_id );
9240
+
9241
+ if ( is_object( $missing_plan ) ) {
9242
+ $plans[] = $missing_plan;
9243
+ }
9244
+ }
9245
+
9246
+ $this->_plans = $plans;
9247
+ $this->_store_plans();
9248
+ }
9249
+
9250
+ $this->do_action( 'after_plans_sync', $plans );
9251
+
9252
+ return $this->_plans;
9253
+ }
9254
+
9255
+ /**
9256
+ * Check if specified plan exists locally. If not, fetch it and store it.
9257
+ *
9258
+ * @author Vova Feldman (@svovaf)
9259
+ * @since 2.0.0
9260
+ *
9261
+ * @param number $plan_id
9262
+ *
9263
+ * @return \FS_Plugin_Plan|object The plan entity or the API error object on failure.
9264
+ */
9265
+ private function sync_plan_if_not_exist( $plan_id ) {
9266
+ $plan = self::_get_plan_by_id( $plan_id );
9267
+
9268
+ if ( is_object( $plan ) ) {
9269
+ // Plan already exists.
9270
+ return $plan;
9271
+ }
9272
+
9273
+ $plan = $this->fetch_plan_by_id( $plan_id );
9274
+
9275
+ if ( $plan instanceof FS_Plugin_Plan ) {
9276
+ $this->_plans[] = $plan;
9277
+ $this->_store_plans();
9278
+
9279
+ return $plan;
9280
+ }
9281
+
9282
+ return $plan;
9283
+ }
9284
+
9285
+ /**
9286
+ * Check if specified license exists locally. If not, fetch it and store it.
9287
+ *
9288
+ * @author Vova Feldman (@svovaf)
9289
+ * @since 2.0.0
9290
+ *
9291
+ * @param number $license_id
9292
+ * @param string $license_key
9293
+ *
9294
+ * @return \FS_Plugin_Plan|object The plan entity or the API error object on failure.
9295
+ */
9296
+ private function sync_license_if_not_exist( $license_id, $license_key ) {
9297
+ $license = $this->_get_license_by_id( $license_id );
9298
+
9299
+ if ( is_object( $license ) ) {
9300
+ // License already exists.
9301
+ return $license;
9302
+ }
9303
+
9304
+ $license = $this->fetch_license_by_key( $license_id, $license_key );
9305
+
9306
+ if ( $license instanceof FS_Plugin_License ) {
9307
+ $this->_licenses[] = $license;
9308
+ $this->_license = $license;
9309
+ $this->_store_licenses();
9310
+
9311
+ return $license;
9312
+ }
9313
+
9314
+ return $license;
9315
+ }
9316
+
9317
+ /**
9318
+ * Get a collection of unique plan IDs that are associated with any installs in the network.
9319
+ *
9320
+ * @author Leo Fajardo (@leorw)
9321
+ * @since 2.0.0
9322
+ *
9323
+ * @return number[]
9324
+ */
9325
+ private function get_plans_ids_associated_with_installs() {
9326
+ if ( ! $this->_is_network_active ) {
9327
+ if ( ! is_object( $this->_site ) ||
9328
+ ! FS_Plugin_Plan::is_valid_id( $this->_site->plan_id )
9329
+ ) {
9330
+ return array();
9331
+ }
9332
+
9333
+ return array( $this->_site->plan_id );
9334
+ }
9335
+
9336
+ $plan_ids = array();
9337
+ $sites = self::get_sites();
9338
+ foreach ( $sites as $site ) {
9339
+ $blog_id = self::get_site_blog_id( $site );
9340
+ $install = $this->get_install_by_blog_id( $blog_id );
9341
+
9342
+ if ( ! is_object( $install ) ||
9343
+ ! FS_Plugin_Plan::is_valid_id( $install->plan_id )
9344
+ ) {
9345
+ continue;
9346
+ }
9347
+
9348
+ $plan_ids[ $install->plan_id ] = true;
9349
+ }
9350
+
9351
+ return array_keys( $plan_ids );
9352
+ }
9353
+
9354
+ /**
9355
+ * Get a collection of unique license IDs that are associated with any installs in the network.
9356
+ *
9357
+ * @author Leo Fajardo (@leorw)
9358
+ * @since 2.0.0
9359
+ *
9360
+ * @return number[]
9361
+ */
9362
+ private function get_license_ids_associated_with_installs() {
9363
+ if ( ! $this->_is_network_active ) {
9364
+ if ( ! is_object( $this->_site ) ||
9365
+ ! FS_Plugin_License::is_valid_id( $this->_site->license_id )
9366
+ ) {
9367
+ return array();
9368
+ }
9369
+
9370
+ return array( $this->_site->license_id );
9371
+ }
9372
+
9373
+ $license_ids = array();
9374
+ $sites = self::get_sites();
9375
+ foreach ( $sites as $site ) {
9376
+ $blog_id = self::get_site_blog_id( $site );
9377
+ $install = $this->get_install_by_blog_id( $blog_id );
9378
+
9379
+ if ( ! is_object( $install ) ||
9380
+ ! FS_Plugin_License::is_valid_id( $install->license_id )
9381
+ ) {
9382
+ continue;
9383
+ }
9384
+
9385
+ $license_ids[ $install->license_id ] = true;
9386
+ }
9387
+
9388
+ return array_keys( $license_ids );
9389
+ }
9390
+
9391
+ /**
9392
+ * @author Vova Feldman (@svovaf)
9393
+ * @since 1.0.5
9394
+ *
9395
+ * @param number $id
9396
+ *
9397
+ * @return FS_Plugin_Plan|false
9398
+ */
9399
+ function _get_plan_by_id( $id ) {
9400
+ $this->_logger->entrance();
9401
+
9402
+ if ( ! is_array( $this->_plans ) || 0 === count( $this->_plans ) ) {
9403
+ $this->_sync_plans();
9404
+ }
9405
+
9406
+ foreach ( $this->_plans as $plan ) {
9407
+ if ( $id == $plan->id ) {
9408
+ return $plan;
9409
+ }
9410
+ }
9411
+
9412
+ return false;
9413
+ }
9414
+
9415
+ /**
9416
+ * @author Vova Feldman (@svovaf)
9417
+ * @since 1.1.8.1
9418
+ *
9419
+ * @param string $name
9420
+ *
9421
+ * @return FS_Plugin_Plan|false
9422
+ */
9423
+ private function get_plan_by_name( $name ) {
9424
+ $this->_logger->entrance();
9425
+
9426
+ if ( ! is_array( $this->_plans ) || 0 === count( $this->_plans ) ) {
9427
+ $this->_sync_plans();
9428
+ }
9429
+
9430
+ foreach ( $this->_plans as $plan ) {
9431
+ if ( $name == $plan->name ) {
9432
+ return $plan;
9433
+ }
9434
+ }
9435
+
9436
+ return false;
9437
+ }
9438
+
9439
+ /**
9440
+ * Sync local licenses with remote server.
9441
+ *
9442
+ * @author Vova Feldman (@svovaf)
9443
+ * @since 1.0.6
9444
+ *
9445
+ * @param number|bool $site_license_id
9446
+ * @param number|null $blog_id
9447
+ *
9448
+ * @return FS_Plugin_License[]|object
9449
+ */
9450
+ function _sync_licenses( $site_license_id = false, $blog_id = null ) {
9451
+ $this->_logger->entrance();
9452
+
9453
+ $is_network_admin = fs_is_network_admin();
9454
+
9455
+ if ( $is_network_admin && is_null( $blog_id ) ) {
9456
+ $all_licenses = self::get_all_licenses( $this->_module_id );
9457
+ } else {
9458
+ $all_licenses = $this->get_user_licenses( $this->_user->id );
9459
+ }
9460
+
9461
+ $foreign_licenses = array(
9462
+ 'ids' => array(),
9463
+ 'license_keys' => array()
9464
+ );
9465
+
9466
+ $all_licenses_map = array();
9467
+ foreach ( $all_licenses as $license ) {
9468
+ $all_licenses_map[ $license->id ] = true;
9469
+ if ( $license->user_id == $this->_user->id || $license->id == $site_license_id ) {
9470
+ continue;
9471
+ }
9472
+
9473
+ $foreign_licenses['ids'][] = $license->id;
9474
+ $foreign_licenses['license_keys'][] = $license->secret_key;
9475
+ }
9476
+
9477
+ if ( empty( $foreign_licenses['ids'] ) ) {
9478
+ $foreign_licenses = array();
9479
+ }
9480
+
9481
+ $licenses = $this->_fetch_licenses( false, $site_license_id, $foreign_licenses, $blog_id );
9482
+
9483
+ if ( $this->is_array_instanceof( $licenses, 'FS_Plugin_License' ) ) {
9484
+ $licenses_map = array();
9485
+ foreach ( $licenses as $license ) {
9486
+ $licenses_map[ $license->id ] = true;
9487
+ }
9488
+
9489
+ // $license_ids_to_keep = $this->get_license_ids_associated_with_installs();
9490
+ // foreach ( $license_ids_to_keep as $license_id ) {
9491
+ // if ( isset( $licenses_map[ $license_id ] ) ) {
9492
+ // continue;
9493
+ // }
9494
+ //
9495
+ // $missing_license = self::_get_license_by_id( $license_id, false );
9496
+ // if ( is_object( $missing_license ) ) {
9497
+ // $licenses[] = $missing_license;
9498
+ // $licenses_map[ $missing_license->id ] = true;
9499
+ // }
9500
+ // }
9501
+
9502
+ $user_license_ids = $this->get_user_linked_license_ids( $this->_user->id );
9503
+
9504
+ foreach ( $user_license_ids as $key => $license_id ) {
9505
+ if ( ! isset( $licenses_map[ $license_id ] ) ) {
9506
+ // Remove access to licenses that no longer exist.
9507
+ unset( $user_license_ids[ $key ] );
9508
+ }
9509
+ }
9510
+
9511
+ if ( ! empty( $user_license_ids ) ) {
9512
+ foreach ( $licenses_map as $license_id => $value ) {
9513
+ if ( ! isset( $all_licenses_map[ $license_id ] ) ) {
9514
+ // Associate new licenses with the user who triggered the license syncing.
9515
+ $user_license_ids[] = $license_id;
9516
+ }
9517
+ }
9518
+
9519
+ $user_license_ids = array_unique( $user_license_ids );
9520
+ } else {
9521
+ $user_license_ids = array_keys( $licenses_map );
9522
+ }
9523
+
9524
+ if ( ! $is_network_admin || ! is_null( $blog_id ) ) {
9525
+ $user_licenses = array();
9526
+ foreach ( $licenses as $license ) {
9527
+ if ( ! in_array( $license->id, $user_license_ids ) ) {
9528
+ continue;
9529
+ }
9530
+
9531
+ $user_licenses[] = $license;
9532
+ }
9533
+
9534
+ $this->_licenses = $user_licenses;
9535
+ } else {
9536
+ $this->_licenses = $licenses;
9537
+ }
9538
+
9539
+ $this->set_user_linked_license_ids( $this->_user->id, $user_license_ids );
9540
+
9541
+ $this->_store_licenses( true, $this->_module_id, $licenses );
9542
+ }
9543
+
9544
+ // Update current license.
9545
+ if ( is_object( $this->_license ) ) {
9546
+ $this->_license = $this->_get_license_by_id( $this->_license->id );
9547
+ }
9548
+
9549
+ return $this->_licenses;
9550
+ }
9551
+
9552
+ /**
9553
+ * @author Vova Feldman (@svovaf)
9554
+ * @since 1.0.5
9555
+ *
9556
+ * @param number $id
9557
+ * @param bool $sync_licenses
9558
+ *
9559
+ * @return FS_Plugin_License|false
9560
+ */
9561
+ function _get_license_by_id( $id, $sync_licenses = true ) {
9562
+ $this->_logger->entrance();
9563
+
9564
+ if ( ! FS_Plugin_License::is_valid_id( $id ) ) {
9565
+ return false;
9566
+ }
9567
+
9568
+ /**
9569
+ * When running from the network level admin and opted-in from the network,
9570
+ * check if the license exists in the network user licenses collection.
9571
+ *
9572
+ * @author Vova Feldman (@svovaf)
9573
+ * @since 2.0.0
9574
+ */
9575
+ if ( fs_is_network_admin() &&
9576
+ $this->is_network_registered() &&
9577
+ ( ! is_object( $this->_user ) || $this->_storage->network_user_id != $this->_user->id )
9578
+ ) {
9579
+ $licenses = $this->get_user_licenses( $this->_storage->network_user_id );
9580
+
9581
+ foreach ( $licenses as $license ) {
9582
+ if ( $id == $license->id ) {
9583
+ return $license;
9584
+ }
9585
+ }
9586
+ }
9587
+
9588
+ if ( ! $this->has_any_license() && $sync_licenses ) {
9589
+ $this->_sync_licenses( $id );
9590
+ }
9591
+
9592
+ if ( is_array( $this->_licenses ) ) {
9593
+ foreach ( $this->_licenses as $license ) {
9594
+ if ( $id == $license->id ) {
9595
+ return $license;
9596
+ }
9597
+ }
9598
+ }
9599
+
9600
+ return false;
9601
+ }
9602
+
9603
+ /**
9604
+ * 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.
9605
+ *
9606
+ * @author Vova Feldman (@svovaf)
9607
+ * @since 2.0.0
9608
+ *
9609
+ * @param number $id
9610
+ *
9611
+ * @return FS_Plugin_License
9612
+ */
9613
+ private function get_license_by_id( $id ) {
9614
+ $licenses = self::get_all_licenses( $this->_module_id );
9615
+
9616
+ if ( is_array( $licenses ) && ! empty( $licenses ) ) {
9617
+ foreach ( $licenses as $license ) {
9618
+ if ( $id == $license->id ) {
9619
+ return $license;
9620
+ }
9621
+ }
9622
+ }
9623
+
9624
+ return null;
9625
+ }
9626
+
9627
+ /**
9628
+ * Synchronize the site's context license by fetching the license form the API and updating the local data with it.
9629
+ *
9630
+ * @author Vova Feldman (@svovaf)
9631
+ * @since 2.0.0
9632
+ *
9633
+ * @return \FS_Plugin_License|mixed
9634
+ */
9635
+ private function sync_site_license() {
9636
+ $api = $this->get_api_user_scope();
9637
+
9638
+ $result = $api->get( "/licenses/{$this->_license->id}.json?license_key=" . urlencode( $this->_license->secret_key ), true );
9639
+
9640
+ if ( ! $this->is_api_result_entity( $result ) ) {
9641
+ return $result;
9642
+ }
9643
+
9644
+ $license = $this->_update_site_license( new FS_Plugin_License( $result ) );
9645
+ $this->_store_licenses();
9646
+
9647
+ return $license;
9648
+ }
9649
+
9650
+ /**
9651
+ * Get all user's available licenses for the current module.
9652
+ *
9653
+ * @author Vova Feldman (@svovaf)
9654
+ * @since 2.0.0
9655
+ *
9656
+ * @param number $user_id
9657
+ *
9658
+ * @return FS_Plugin_License[]
9659
+ */
9660
+ private function get_user_licenses( $user_id ) {
9661
+ $all_licenses = self::get_all_licenses( $this->_module_id );
9662
+ if ( empty( $all_licenses ) ) {
9663
+ return array();
9664
+ }
9665
+
9666
+ $user_license_ids = $this->get_user_linked_license_ids( $user_id );
9667
+ if ( empty( $user_license_ids ) ) {
9668
+ return array();
9669
+ }
9670
+
9671
+ $licenses = array();
9672
+ foreach ( $all_licenses as $license ) {
9673
+ if ( in_array( $license->id, $user_license_ids ) ) {
9674
+ $licenses[] = $license;
9675
+ }
9676
+ }
9677
+
9678
+ return $licenses;
9679
+ }
9680
+
9681
+ /**
9682
+ * Checks if the context license is network activated except on the given blog ID.
9683
+ *
9684
+ * @author Vova Feldman (@svovaf)
9685
+ * @since 2.0.0
9686
+ *
9687
+ * @param int $except_blog_id
9688
+ *
9689
+ * @return bool
9690
+ */
9691
+ private function is_license_network_active( $except_blog_id = 0 ) {
9692
+ $this->_logger->entrance();
9693
+
9694
+ if ( ! is_object( $this->_license ) ) {
9695
+ return false;
9696
+ }
9697
+
9698
+ $sites = self::get_sites();
9699
+
9700
+ if ( $this->_license->total_activations() < ( count( $sites ) - 1 ) ) {
9701
+ // There are more sites than the number of activations, so license cannot be network activated.
9702
+ return false;
9703
+ }
9704
+
9705
+ foreach ( $sites as $site ) {
9706
+ $blog_id = self::get_site_blog_id( $site );
9707
+
9708
+ if ( $except_blog_id == $blog_id ) {
9709
+ // Skip excluded blog.
9710
+ continue;
9711
+ }
9712
+
9713
+ $install = $this->get_install_by_blog_id( $blog_id );
9714
+
9715
+ if ( is_object( $install ) && $install->license_id != $this->_license->id ) {
9716
+ return false;
9717
+ }
9718
+ }
9719
+
9720
+ return true;
9721
+ }
9722
+
9723
+ /**
9724
+ * 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.
9725
+ *
9726
+ * Notice: On success, this method will also update the license activations counters (without updating the license in the storage).
9727
+ *
9728
+ * @author Vova Feldman (@svovaf)
9729
+ * @since 2.0.0
9730
+ *
9731
+ * @param \FS_User $user
9732
+ * @param \FS_Plugin_License $license
9733
+ *
9734
+ * @return bool
9735
+ */
9736
+ private function try_activate_license_on_network( FS_User $user, FS_Plugin_License $license ) {
9737
+ $this->_logger->entrance();
9738
+
9739
+ $result = $this->can_activate_license_on_network( $license );
9740
+
9741
+ if ( false === $result ) {
9742
+ return false;
9743
+ }
9744
+
9745
+ $installs_without_license = $result['installs'];
9746
+ if ( ! empty( $installs_without_license ) ) {
9747
+ $this->activate_license_on_many_installs( $user, $license->secret_key, $installs_without_license );
9748
+ }
9749
+
9750
+ $disconnected_site_ids = $result['sites'];
9751
+ if ( ! empty( $disconnected_site_ids ) ) {
9752
+ $this->activate_license_on_many_sites( $user, $license->secret_key, $disconnected_site_ids );
9753
+ }
9754
+
9755
+ $this->link_license_2_user( $license->id, $user->id );
9756
+
9757
+ // Sync license after activations.
9758
+ $license->activated += $result['production_count'];
9759
+ $license->activated_local += $result['localhost_count'];
9760
+
9761
+ // $this->_store_licenses()
9762
+
9763
+ return true;
9764
+ }
9765
+
9766
+ /**
9767
+ * Checks if the given license can be activated on the whole network.
9768
+ *
9769
+ * @author Vova Feldman (@svovaf)
9770
+ * @since 2.0.0
9771
+ *
9772
+ * @param \FS_Plugin_License $license
9773
+ *
9774
+ * @return false|array {
9775
+ * @type array[int]FS_Site $installs Blog ID to install map.
9776
+ * @type int[] $sites Non-connected blog IDs.
9777
+ * @type int $production_count Production sites count.
9778
+ * @type int $localhost_count Production sites count.
9779
+ * }
9780
+ */
9781
+ private function can_activate_license_on_network( FS_Plugin_License $license ) {
9782
+ $sites = self::get_sites();
9783
+
9784
+ $production_count = 0;
9785
+ $localhost_count = 0;
9786
+
9787
+ $installs_without_license = array();
9788
+ $disconnected_site_ids = array();
9789
+
9790
+ foreach ( $sites as $site ) {
9791
+ $blog_id = self::get_site_blog_id( $site );
9792
+ $install = $this->get_install_by_blog_id( $blog_id );
9793
+
9794
+ if ( is_object( $install ) ) {
9795
+ if ( FS_Plugin_License::is_valid_id( $install->license_id ) ) {
9796
+ // License already activated on the install.
9797
+ continue;
9798
+ }
9799
+
9800
+ $url = $install->url;
9801
+
9802
+ $installs_without_license[ $blog_id ] = $install;
9803
+ } else {
9804
+ $url = is_object( $site ) ?
9805
+ $site->siteurl :
9806
+ get_site_url( $blog_id );
9807
+
9808
+ $disconnected_site_ids[] = $blog_id;
9809
+ }
9810
+
9811
+ if ( FS_Site::is_localhost_by_address( $url ) ) {
9812
+ $localhost_count ++;
9813
+ } else {
9814
+ $production_count ++;
9815
+ }
9816
+ }
9817
+
9818
+ if ( ! $license->can_activate_bulk( $production_count, $localhost_count ) ) {
9819
+ return false;
9820
+ }
9821
+
9822
+ return array(
9823
+ 'installs' => $installs_without_license,
9824
+ 'sites' => $disconnected_site_ids,
9825
+ 'production_count' => $production_count,
9826
+ 'localhost_count' => $localhost_count,
9827
+ );
9828
+ }
9829
+
9830
+ /**
9831
+ * Activate a given license on a collection of installs.
9832
+ *
9833
+ * @author Vova Feldman (@svovaf)
9834
+ * @since 2.0.0
9835
+ *
9836
+ * @param \FS_User $user
9837
+ * @param string $license_key
9838
+ * @param array $blog_2_install_map {
9839
+ * @key int Blog ID.
9840
+ * @value FS_Site Blog's associated install.
9841
+ * }
9842
+ *
9843
+ * @return mixed|true
9844
+ */
9845
+ private function activate_license_on_many_installs(
9846
+ FS_User $user,
9847
+ $license_key,
9848
+ array $blog_2_install_map
9849
+ ) {
9850
+ $params = array(
9851
+ array( 'license_key' => $this->apply_filters( 'license_key', $license_key ) )
9852
+ );
9853
+
9854
+ $install_2_blog_map = array();
9855
+ foreach ( $blog_2_install_map as $blog_id => $install ) {
9856
+ $params[] = array( 'id' => $install->id );
9857
+
9858
+ $install_2_blog_map[ $install->id ] = $blog_id;
9859
+ }
9860
+
9861
+ $result = $this->get_api_user_scope_by_user( $user )->call(
9862
+ "plugins/{$this->_plugin->id}/installs.json",
9863
+ 'PUT',
9864
+ $params
9865
+ );
9866
+
9867
+ if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
9868
+ return $result;
9869
+ }
9870
+
9871
+ foreach ( $result->installs as $r_install ) {
9872
+ $install = new FS_Site( $r_install );
9873
+ $install->is_disconnected = false;
9874
+
9875
+ // Update install.
9876
+ $this->_store_site(
9877
+ true,
9878
+ $install_2_blog_map[ $r_install->id ],
9879
+ $install
9880
+ );
9881
+ }
9882
+
9883
+ return true;
9884
+ }
9885
+
9886
+ /**
9887
+ * Activate a given license on a collection of blogs/sites that are not yet opted-in.
9888
+ *
9889
+ * @author Vova Feldman (@svovaf)
9890
+ * @since 2.0.0
9891
+ *
9892
+ * @param \FS_User $user
9893
+ * @param string $license_key
9894
+ * @param int[] $site_ids
9895
+ *
9896
+ * @return true|mixed True if successful, otherwise, the API result.
9897
+ */
9898
+ private function activate_license_on_many_sites(
9899
+ FS_User $user,
9900
+ $license_key,
9901
+ array $site_ids
9902
+ ) {
9903
+ $sites = array();
9904
+ foreach ( $site_ids as $site_id ) {
9905
+ $sites[] = $this->get_site_info( array( 'blog_id' => $site_id ) );
9906
+ }
9907
+
9908
+ // Install the plugin.
9909
+ $result = $this->create_installs_with_user(
9910
+ $user,
9911
+ $license_key,
9912
+ false,
9913
+ $sites,
9914
+ false,
9915
+ true
9916
+ );
9917
+
9918
+ if ( ! $this->is_api_result_entity( $result ) &&
9919
+ ! $this->is_api_result_object( $result, 'installs' )
9920
+ ) {
9921
+ return $result;
9922
+ }
9923
+
9924
+ $installs = array();
9925
+ foreach ( $result->installs as $install ) {
9926
+ $installs[] = new FS_Site( $install );
9927
+ }
9928
+
9929
+ // Map site addresses to their blog IDs.
9930
+ $address_to_blog_map = $this->get_address_to_blog_map();
9931
+
9932
+ $first_blog_id = null;
9933
+
9934
+ foreach ( $installs as $install ) {
9935
+ $address = trailingslashit( fs_strip_url_protocol( $install->url ) );
9936
+ $blog_id = $address_to_blog_map[ $address ];
9937
+
9938
+ $this->_store_site( true, $blog_id, $install );
9939
+
9940
+ $this->reset_anonymous_mode( $blog_id );
9941
+
9942
+ if ( is_null( $first_blog_id ) ) {
9943
+ $first_blog_id = $blog_id;
9944
+ }
9945
+ }
9946
+
9947
+ if ( ! FS_Site::is_valid_id( $this->_storage->network_install_blog_id ) ) {
9948
+ $this->_storage->network_install_blog_id = $first_blog_id;
9949
+ }
9950
+
9951
+ return true;
9952
+ }
9953
+
9954
+ /**
9955
+ * Sync site's license with user licenses.
9956
+ *
9957
+ * @author Vova Feldman (@svovaf)
9958
+ * @since 1.0.6
9959
+ *
9960
+ * @param FS_Plugin_License|null $new_license
9961
+ *
9962
+ * @return FS_Plugin_License|null
9963
+ */
9964
+ function _update_site_license( $new_license ) {
9965
+ $this->_logger->entrance();
9966
+
9967
+ $this->_license = $new_license;
9968
+
9969
+ if ( ! is_object( $new_license ) ) {
9970
+ $this->_site->license_id = null;
9971
+ $this->_sync_site_subscription( null );
9972
+
9973
+ return $this->_license;
9974
+ }
9975
+
9976
+ $this->_site->license_id = $this->_license->id;
9977
+
9978
+ if ( ! is_array( $this->_licenses ) ) {
9979
+ $this->_licenses = array();
9980
+ }
9981
+
9982
+ $is_license_found = false;
9983
+ for ( $i = 0, $len = count( $this->_licenses ); $i < $len; $i ++ ) {
9984
+ if ( $new_license->id == $this->_licenses[ $i ]->id ) {
9985
+ $this->_licenses[ $i ] = $new_license;
9986
+
9987
+ $is_license_found = true;
9988
+ break;
9989
+ }
9990
+ }
9991
+
9992
+ // If new license just append.
9993
+ if ( ! $is_license_found ) {
9994
+ $this->_licenses[] = $new_license;
9995
+ }
9996
+
9997
+ $this->_sync_site_subscription( $new_license );
9998
+
9999
+ return $this->_license;
10000
+ }
10001
+
10002
+ /**
10003
+ * Sync site's subscription.
10004
+ *
10005
+ * @author Vova Feldman (@svovaf)
10006
+ * @since 1.0.9
10007
+ *
10008
+ * @param FS_Plugin_License|null $license
10009
+ *
10010
+ * @return bool|\FS_Subscription
10011
+ */
10012
+ private function _sync_site_subscription( $license ) {
10013
+ if ( ! is_object( $license ) ) {
10014
+ $this->delete_unused_subscriptions();
10015
+
10016
+ return false;
10017
+ }
10018
+
10019
+ // Load subscription details if not lifetime.
10020
+ $subscription = $license->is_lifetime() ?
10021
+ false :
10022
+ $this->_fetch_site_license_subscription();
10023
+
10024
+ if ( is_object( $subscription ) && ! isset( $subscription->error ) ) {
10025
+ $this->store_subscription( $subscription );
10026
+ } else {
10027
+ $this->delete_unused_subscriptions();
10028
+ }
10029
+
10030
+ return $subscription;
10031
+ }
10032
+
10033
+ /**
10034
+ * @author Vova Feldman (@svovaf)
10035
+ * @since 1.0.6
10036
+ *
10037
+ * @return bool|\FS_Plugin_License
10038
+ */
10039
+ function _get_license() {
10040
+ if ( ! fs_is_network_admin() || is_object( $this->_license ) ) {
10041
+ return $this->_license;
10042
+ }
10043
+
10044
+ return $this->_get_available_premium_license();
10045
+ }
10046
+
10047
+ /**
10048
+ * @param number $license_id
10049
+ *
10050
+ * @return null|\FS_Subscription
10051
+ */
10052
+ function _get_subscription( $license_id ) {
10053
+ if ( ! isset( $this->_storage->subscriptions ) ||
10054
+ empty( $this->_storage->subscriptions )
10055
+ ) {
10056
+ return null;
10057
+ }
10058
+
10059
+ foreach ( $this->_storage->subscriptions as $subscription ) {
10060
+ if ( $subscription->license_id == $license_id ) {
10061
+ return $subscription;
10062
+ }
10063
+ }
10064
+
10065
+ return null;
10066
+ }
10067
+
10068
+ /**
10069
+ * @author Leo Fajardo (@leorw)
10070
+ * @since 2.0.0
10071
+ *
10072
+ * @param FS_Subscription $subscription
10073
+ */
10074
+ function store_subscription( FS_Subscription $subscription ) {
10075
+ if ( ! isset( $this->_storage->subscriptions ) ) {
10076
+ $this->_storage->subscriptions = array();
10077
+ }
10078
+
10079
+ if ( empty( $this->_storage->subscriptions ) || ! is_multisite() ) {
10080
+ $this->_storage->subscriptions = array( $subscription );
10081
+
10082
+ return;
10083
+ }
10084
+
10085
+ $subscriptions = $this->_storage->subscriptions;
10086
+
10087
+ $updated_subscription = false;
10088
+ foreach ( $subscriptions as $key => $existing_subscription ) {
10089
+ if ( $existing_subscription->id == $subscription->id ) {
10090
+ $subscriptions[ $key ] = $subscription;
10091
+ $updated_subscription = true;
10092
+ break;
10093
+ }
10094
+ }
10095
+
10096
+ if ( ! $updated_subscription ) {
10097
+ $subscriptions[] = $subscription;
10098
+ }
10099
+
10100
+ $this->_storage->subscriptions = $subscriptions;
10101
+ }
10102
+
10103
+ /**
10104
+ * @author Leo Fajardo (@leorw)
10105
+ * @since 2.0.0
10106
+ */
10107
+ function delete_unused_subscriptions() {
10108
+ if ( ! isset( $this->_storage->subscriptions ) ||
10109
+ empty( $this->_storage->subscriptions ) ||
10110
+ // Clean up only if there are already at least 3 subscriptions.
10111
+ ( count( $this->_storage->subscriptions ) < 3 )
10112
+ ) {
10113
+ return;
10114
+ }
10115
+
10116
+ if ( ! is_multisite() ) {
10117
+ // If not multisite, there should only be 1 subscription, so just clear the array.
10118
+ $this->_storage->subscriptions = array();
10119
+
10120
+ return;
10121
+ }
10122
+
10123
+ $subscriptions_to_keep_by_license_id_map = array();
10124
+ $sites = self::get_sites();
10125
+ foreach ( $sites as $site ) {
10126
+ $blog_id = self::get_site_blog_id( $site );
10127
+ $install = $this->get_install_by_blog_id( $blog_id );
10128
+
10129
+ if ( ! is_object( $install ) ||
10130
+ ! FS_Plugin_License::is_valid_id( $install->license_id )
10131
+ ) {
10132
+ continue;
10133
+ }
10134
+
10135
+ $subscriptions_to_keep_by_license_id_map[ $install->license_id ] = true;
10136
+ }
10137
+
10138
+ if ( empty( $subscriptions_to_keep_by_license_id_map ) ) {
10139
+ $this->_storage->subscriptions = array();
10140
+
10141
+ return;
10142
+ }
10143
+
10144
+ foreach ( $this->_storage->subscriptions as $key => $subscription ) {
10145
+ if ( ! isset( $subscriptions_to_keep_by_license_id_map[ $subscription->license_id ] ) ) {
10146
+ unset( $this->_storage->subscriptions[ $key ] );
10147
+ }
10148
+ }
10149
+ }
10150
+
10151
+ /**
10152
+ * @author Vova Feldman (@svovaf)
10153
+ * @since 1.0.2
10154
+ *
10155
+ * @param string $plan Plan name
10156
+ * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
10157
+ *
10158
+ * @return bool
10159
+ */
10160
+ function is_plan( $plan, $exact = false ) {
10161
+ $this->_logger->entrance();
10162
+
10163
+ if ( ! $this->is_registered() ) {
10164
+ return false;
10165
+ }
10166
+
10167
+ $plan = strtolower( $plan );
10168
+
10169
+ $current_plan_name = $this->get_plan_name();
10170
+
10171
+ if ( $current_plan_name === $plan ) {
10172
+ // Exact plan.
10173
+ return true;
10174
+ } else if ( $exact ) {
10175
+ // Required exact, but plans are different.
10176
+ return false;
10177
+ }
10178
+
10179
+ $current_plan_order = - 1;
10180
+ $required_plan_order = - 1;
10181
+ for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
10182
+ if ( $plan === $this->_plans[ $i ]->name ) {
10183
+ $required_plan_order = $i;
10184
+ } else if ( $current_plan_name === $this->_plans[ $i ]->name ) {
10185
+ $current_plan_order = $i;
10186
+ }
10187
+ }
10188
+
10189
+ return ( $current_plan_order > $required_plan_order );
10190
+ }
10191
+
10192
+ /**
10193
+ * Check if module has only one plan.
10194
+ *
10195
+ * @author Vova Feldman (@svovaf)
10196
+ * @since 1.2.1.7
10197
+ *
10198
+ * @return bool
10199
+ */
10200
+ function is_single_plan() {
10201
+ $this->_logger->entrance();
10202
+
10203
+ if ( ! $this->is_registered() ||
10204
+ ! is_array( $this->_plans ) ||
10205
+ 0 === count( $this->_plans )
10206
+ ) {
10207
+ return true;
10208
+ }
10209
+
10210
+ return ( 1 === count( $this->_plans ) );
10211
+ }
10212
+
10213
+ /**
10214
+ * Check if plan based on trial. If not in trial mode, should return false.
10215
+ *
10216
+ * @since 1.0.9
10217
+ *
10218
+ * @param string $plan Plan name
10219
+ * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
10220
+ *
10221
+ * @return bool
10222
+ */
10223
+ function is_trial_plan( $plan, $exact = false ) {
10224
+ $this->_logger->entrance();
10225
+
10226
+ if ( ! $this->is_registered() ) {
10227
+ return false;
10228
+ }
10229
+
10230
+ if ( ! $this->is_trial() ) {
10231
+ return false;
10232
+ }
10233
+
10234
+ $trial_plan = $this->get_trial_plan();
10235
+
10236
+ if ( $trial_plan->name === $plan ) {
10237
+ // Exact plan.
10238
+ return true;
10239
+ } else if ( $exact ) {
10240
+ // Required exact, but plans are different.
10241
+ return false;
10242
+ }
10243
+
10244
+ $current_plan_order = - 1;
10245
+ $required_plan_order = - 1;
10246
+ for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
10247
+ if ( $plan === $this->_plans[ $i ]->name ) {
10248
+ $required_plan_order = $i;
10249
+ } else if ( $trial_plan->name === $this->_plans[ $i ]->name ) {
10250
+ $current_plan_order = $i;
10251
+ }
10252
+ }
10253
+
10254
+ return ( $current_plan_order > $required_plan_order );
10255
+ }
10256
+
10257
+ /**
10258
+ * Check if plugin has any paid plans.
10259
+ *
10260
+ * @author Vova Feldman (@svovaf)
10261
+ * @since 1.0.7
10262
+ *
10263
+ * @return bool
10264
+ */
10265
+ function has_paid_plan() {
10266
+ return $this->_has_paid_plans ||
10267
+ FS_Plan_Manager::instance()->has_paid_plan( $this->_plans );
10268
+ }
10269
+
10270
+ /**
10271
+ * Check if plugin has any plan with a trail.
10272
+ *
10273
+ * @author Vova Feldman (@svovaf)
10274
+ * @since 1.0.9
10275
+ *
10276
+ * @return bool
10277
+ */
10278
+ function has_trial_plan() {
10279
+ /**
10280
+ * @author Vova Feldman(@svovaf)
10281
+ * @since 1.2.1.5
10282
+ *
10283
+ * Allow setting a trial from the SDK without calling the API.
10284
+ * But, if the user did opt-in, continue using the real data from the API.
10285
+ */
10286
+ if ( $this->_trial_days >= 0 ) {
10287
+ return true;
10288
+ }
10289
+
10290
+ return $this->_storage->get( 'has_trial_plan', false );
10291
+ }
10292
+
10293
+ /**
10294
+ * Check if plugin has any free plan, or is it premium only.
10295
+ *
10296
+ * Note: If no plans configured, assume plugin is free.
10297
+ *
10298
+ * @author Vova Feldman (@svovaf)
10299
+ * @since 1.0.7
10300
+ *
10301
+ * @return bool
10302
+ */
10303
+ function has_free_plan() {
10304
+ return ! $this->is_only_premium();
10305
+ }
10306
+
10307
+ /**
10308
+ * Displays a license activation dialog box when the user clicks on the "Activate License"
10309
+ * or "Change License" link on the plugins
10310
+ * page.
10311
+ *
10312
+ * @author Leo Fajardo (@leorw)
10313
+ * @since 1.1.9
10314
+ */
10315
+ function _add_license_activation_dialog_box() {
10316
+ $vars = array(
10317
+ 'id' => $this->_module_id,
10318
+ );
10319
+
10320
+ fs_require_template( 'forms/license-activation.php', $vars );
10321
+ fs_require_template( 'forms/resend-key.php', $vars );
10322
+ }
10323
+
10324
+ /**
10325
+ * Displays the opt-out dialog box when the user clicks on the "Opt Out" link on the "Plugins"
10326
+ * page.
10327
+ *
10328
+ * @author Leo Fajardo (@leorw)
10329
+ * @since 1.2.1.5
10330
+ */
10331
+ function _add_optout_dialog() {
10332
+ if ( $this->is_theme() ) {
10333
+ $vars = null;
10334
+ fs_require_once_template( '/js/jquery.content-change.php', $vars );
10335
+ }
10336
+
10337
+ $vars = array( 'id' => $this->_module_id );
10338
+ fs_require_template( 'forms/optout.php', $vars );
10339
+ }
10340
+
10341
+ /**
10342
+ * Prepare page to include all required UI and logic for the license activation dialog.
10343
+ *
10344
+ * @author Vova Feldman (@svovaf)
10345
+ * @since 1.2.0
10346
+ */
10347
+ function _add_license_activation() {
10348
+ if ( ! $this->is_user_admin() ) {
10349
+ // Only admins can activate a license.
10350
+ return;
10351
+ }
10352
+
10353
+ if ( ! $this->has_paid_plan() ) {
10354
+ // Module doesn't have any paid plans.
10355
+ return;
10356
+ }
10357
+
10358
+ if ( ! $this->is_premium() ) {
10359
+ // Only add license activation logic to the premium version.
10360
+ return;
10361
+ }
10362
+
10363
+ // Add license activation link and AJAX request handler.
10364
+ if ( self::is_plugins_page() ) {
10365
+ /**
10366
+ * @since 1.2.0 Add license action link only on plugins page.
10367
+ */
10368
+ $this->_add_license_action_link();
10369
+ }
10370
+
10371
+ // Add license activation AJAX callback.
10372
+ $this->add_ajax_action( 'activate_license', array( &$this, '_activate_license_ajax_action' ) );
10373
+
10374
+ // Add resend license AJAX callback.
10375
+ $this->add_ajax_action( 'resend_license_key', array( &$this, '_resend_license_key_ajax_action' ) );
10376
+ }
10377
+
10378
+ /**
10379
+ * @author Leo Fajardo (@leorw)
10380
+ *
10381
+ * @since 1.1.9
10382
+ * @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).
10383
+ */
10384
+ function _activate_license_ajax_action() {
10385
+ $this->_logger->entrance();
10386
+
10387
+ $this->check_ajax_referer( 'activate_license' );
10388
+
10389
+ $license_key = trim( fs_request_get( 'license_key' ) );
10390
+
10391
+ if ( empty( $license_key ) ) {
10392
+ exit;
10393
+ }
10394
+
10395
+ $plugin_id = fs_request_get( 'module_id', '', 'post' );
10396
+ $fs = ( $plugin_id == $this->_module_id ) ?
10397
+ $this :
10398
+ $this->get_addon_instance( $plugin_id );
10399
+
10400
+ $error = false;
10401
+ $next_page = false;
10402
+
10403
+ $sites = fs_is_network_admin() ?
10404
+ fs_request_get( 'sites', array(), 'post' ) :
10405
+ array();
10406
+
10407
+ $blog_id = fs_request_get( 'blog_id' );
10408
+ $has_valid_blog_id = is_numeric( $blog_id );
10409
+
10410
+ if ( $fs->is_registered() ) {
10411
+ if ( fs_is_network_admin() && ! $has_valid_blog_id ) {
10412
+ // If no specific blog ID was provided, activate the license for all sites in the network.
10413
+ $blog_2_install_map = array();
10414
+ $site_ids = array();
10415
+
10416
+ foreach ( $sites as $site ) {
10417
+ if ( ! isset( $site['blog_id'] ) || ! is_numeric( $site['blog_id'] ) ) {
10418
+ continue;
10419
+ }
10420
+
10421
+ $install = $this->get_install_by_blog_id( $site['blog_id'] );
10422
+
10423
+ if ( is_object( $install ) ) {
10424
+ $blog_2_install_map[ $site['blog_id'] ] = $install;
10425
+ } else {
10426
+ $site_ids[] = $site['blog_id'];
10427
+ }
10428
+ }
10429
+
10430
+ $user = $this->get_current_or_network_user();
10431
+
10432
+ if ( ! empty( $blog_2_install_map ) ) {
10433
+ $result = $this->activate_license_on_many_installs( $user, $license_key, $blog_2_install_map );
10434
+
10435
+ if ( true !== $result ) {
10436
+ $error = FS_Api::is_api_error_object( $result ) ?
10437
+ $result->error->message :
10438
+ var_export( $result, true );
10439
+ }
10440
+ }
10441
+
10442
+ if ( empty( $error ) && ! empty( $site_ids ) ) {
10443
+ $result = $this->activate_license_on_many_sites( $user, $license_key, $site_ids );
10444
+
10445
+ if ( true !== $result ) {
10446
+ $error = FS_Api::is_api_error_object( $result ) ?
10447
+ $result->error->message :
10448
+ var_export( $result, true );
10449
+ }
10450
+ }
10451
+ } else {
10452
+ if ( $has_valid_blog_id ) {
10453
+ /**
10454
+ * If a specific blog ID was provided, activate the license only for the install that is
10455
+ * associated with the given blog ID.
10456
+ *
10457
+ * @author Leo Fajardo (@leorw)
10458
+ */
10459
+ $this->switch_to_blog( $blog_id );
10460
+ }
10461
+
10462
+ $api = $fs->get_api_site_scope();
10463
+
10464
+ $params = array(
10465
+ 'license_key' => $fs->apply_filters( 'license_key', $license_key )
10466
+ );
10467
+
10468
+ $install = $api->call( '/', 'put', $params );
10469
+
10470
+ if ( FS_Api::is_api_error( $install ) ) {
10471
+ $error = FS_Api::is_api_error_object( $install ) ?
10472
+ $install->error->message :
10473
+ var_export( $install->error, true );
10474
+ } else {
10475
+ $fs->reconnect_locally( $has_valid_blog_id );
10476
+ }
10477
+ }
10478
+
10479
+ if ( empty( $error ) ) {
10480
+ $this->network_upgrade_mode_completed();
10481
+
10482
+ $fs->_sync_license( true, $has_valid_blog_id );
10483
+
10484
+ $next_page = $fs->is_addon() ?
10485
+ $fs->get_parent_instance()->get_account_url() :
10486
+ $fs->get_account_url();
10487
+ }
10488
+ } else {
10489
+ $next_page = $fs->opt_in(
10490
+ false,
10491
+ false,
10492
+ false,
10493
+ $license_key,
10494
+ false,
10495
+ false,
10496
+ false,
10497
+ $sites
10498
+ );
10499
+
10500
+ if ( isset( $next_page->error ) ) {
10501
+ $error = $next_page->error;
10502
+ } else {
10503
+ if ( fs_is_network_admin() ) {
10504
+ /**
10505
+ * Get the list of sites that were just opted-in (and license activated).
10506
+ * This is an optimization for the next part below saving some DB queries.
10507
+ */
10508
+ $connected_sites = array();
10509
+ foreach ( $sites as $site ) {
10510
+ if ( isset( $site['blog_id'] ) && is_numeric( $site['blog_id'] ) ) {
10511
+ $connected_sites[ $site['blog_id'] ] = true;
10512
+ }
10513
+ }
10514
+
10515
+ $all_sites = self::get_sites();
10516
+ $pending_sites = array();
10517
+
10518
+ /**
10519
+ * 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.
10520
+ *
10521
+ * @author Vova Feldman (@svovaf)
10522
+ */
10523
+ foreach ( $all_sites as $site ) {
10524
+ $blog_id = self::get_site_blog_id( $site );
10525
+
10526
+ if ( isset( $connected_sites[ $blog_id ] ) ) {
10527
+ // Site was just connected.
10528
+ continue;
10529
+ }
10530
+
10531
+ if ( $this->is_installed_on_site( $blog_id ) ) {
10532
+ // Site was already connected before.
10533
+ continue;
10534
+ }
10535
+
10536
+ if ( $this->is_site_delegated_connection( $blog_id ) ) {
10537
+ // Site's connection was delegated.
10538
+ continue;
10539
+ }
10540
+
10541
+ if ( $this->is_anonymous_site( $blog_id ) ) {
10542
+ // Site connection was already skipped.
10543
+ continue;
10544
+ }
10545
+
10546
+ $pending_sites[] = self::get_site_info( $site );
10547
+ }
10548
+
10549
+ if ( ! empty( $pending_sites ) ) {
10550
+ if ( $this->is_freemium() ) {
10551
+ $this->skip_connection( $pending_sites );
10552
+ } else {
10553
+ $this->delegate_connection( $pending_sites );
10554
+ }
10555
+ }
10556
+ }
10557
+ }
10558
+ }
10559
+
10560
+ $result = array(
10561
+ 'success' => ( false === $error )
10562
+ );
10563
+
10564
+ if ( false !== $error ) {
10565
+ $result['error'] = $error;
10566
+ } else {
10567
+ $result['next_page'] = $next_page;
10568
+ }
10569
+
10570
+ echo json_encode( $result );
10571
+
10572
+ exit;
10573
+ }
10574
+
10575
+ /**
10576
+ * @author Leo Fajardo (@leorw)
10577
+ * @since 1.2.3.1
10578
+ */
10579
+ function _network_activate_ajax_action() {
10580
+ $this->_logger->entrance();
10581
+
10582
+ $this->check_ajax_referer( 'network_activate' );
10583
+
10584
+ $plugin_id = fs_request_get( 'module_id', '', 'post' );
10585
+ $fs = ( $plugin_id == $this->_module_id ) ?
10586
+ $this :
10587
+ $this->get_addon_instance( $plugin_id );
10588
+
10589
+ $error = false;
10590
+
10591
+ $sites = fs_request_get( 'sites', array(), 'post' );
10592
+ if ( is_array( $sites ) && ! empty( $sites ) ) {
10593
+ $sites_by_action = array(
10594
+ 'allow' => array(),
10595
+ 'delegate' => array(),
10596
+ 'skip' => array()
10597
+ );
10598
+
10599
+ foreach ( $sites as $site ) {
10600
+ $sites_by_action[ $site['action'] ][] = $site;
10601
+ }
10602
+
10603
+ $total_sites = count( $sites );
10604
+ $total_sites_to_delegate = count( $sites_by_action['delegate'] );
10605
+
10606
+ $next_page = '';
10607
+ if ( $total_sites === $total_sites_to_delegate &&
10608
+ ! $this->is_network_upgrade_mode()
10609
+ ) {
10610
+ $this->delegate_connection();
10611
+ } else {
10612
+ if ( ! empty( $sites_by_action['delegate'] ) ) {
10613
+ $this->delegate_connection( $sites_by_action['delegate'] );
10614
+ }
10615
+
10616
+ if ( ! empty( $sites_by_action['skip'] ) ) {
10617
+ $this->skip_connection( $sites_by_action['skip'] );
10618
+ }
10619
+
10620
+ if ( ! empty( $sites_by_action['allow'] ) ) {
10621
+ if ( ! $fs->is_registered() || ! $this->_is_network_active ) {
10622
+ $next_page = $fs->opt_in(
10623
+ false,
10624
+ false,
10625
+ false,
10626
+ false,
10627
+ false,
10628
+ false,
10629
+ false,
10630
+ $sites_by_action['allow']
10631
+ );
10632
+ } else {
10633
+ $next_page = $fs->install_with_user(
10634
+ $this->get_network_user(),
10635
+ false,
10636
+ false,
10637
+ false,
10638
+ true,
10639
+ $sites_by_action['allow']
10640
+ );
10641
+ }
10642
+
10643
+ if ( is_object( $next_page ) && isset( $next_page->error ) ) {
10644
+ $error = $next_page->error;
10645
+ }
10646
+ }
10647
+ }
10648
+
10649
+ if ( empty( $next_page ) ) {
10650
+ $next_page = $this->get_after_activation_url( 'after_network_activation_url' );
10651
+ }
10652
+ } else {
10653
+ $error = $this->get_text_inline( 'invalid_site_details_collection', 'Invalid site details collection.' );
10654
+ }
10655
+
10656
+ $result = array(
10657
+ 'success' => ( false === $error )
10658
+ );
10659
+
10660
+ if ( false !== $error ) {
10661
+ $result['error'] = $error;
10662
+ } else {
10663
+ $result['next_page'] = $next_page;
10664
+ }
10665
+
10666
+ echo json_encode( $result );
10667
+
10668
+ exit;
10669
+ }
10670
+
10671
+ /**
10672
+ * Billing update AJAX callback.
10673
+ *
10674
+ * @author Vova Feldman (@svovaf)
10675
+ * @since 1.2.1.5
10676
+ */
10677
+ function _update_billing_ajax_action() {
10678
+ $this->_logger->entrance();
10679
+
10680
+ $this->check_ajax_referer( 'update_billing' );
10681
+
10682
+ if ( ! $this->is_user_admin() ) {
10683
+ // Only for admins.
10684
+ self::shoot_ajax_failure();
10685
+ }
10686
+
10687
+ $billing = fs_request_get( 'billing' );
10688
+
10689
+ $api = $this->get_api_user_scope();
10690
+ $result = $api->call( '/billing.json', 'put', array_merge( $billing, array(
10691
+ 'plugin_id' => $this->get_parent_id(),
10692
+ ) ) );
10693
+
10694
+ if ( ! $this->is_api_result_entity( $result ) ) {
10695
+ self::shoot_ajax_failure();
10696
+ }
10697
+
10698
+ // Purge cached billing.
10699
+ $this->get_api_user_scope()->purge_cache( 'billing.json' );
10700
+
10701
+ self::shoot_ajax_success();
10702
+ }
10703
+
10704
+ /**
10705
+ * Trial start for anonymous users (AJAX callback).
10706
+ *
10707
+ * @author Vova Feldman (@svovaf)
10708
+ * @since 1.2.1.5
10709
+ */
10710
+ function _start_trial_ajax_action() {
10711
+ $this->_logger->entrance();
10712
+
10713
+ $this->check_ajax_referer( 'start_trial' );
10714
+
10715
+ if ( ! $this->is_user_admin() ) {
10716
+ // Only for admins.
10717
+ self::shoot_ajax_failure();
10718
+ }
10719
+
10720
+ $trial_data = fs_request_get( 'trial' );
10721
+
10722
+ $next_page = $this->opt_in(
10723
+ false,
10724
+ false,
10725
+ false,
10726
+ false,
10727
+ false,
10728
+ $trial_data['plan_id']
10729
+ );
10730
+
10731
+ if ( is_object( $next_page ) && $this->is_api_error( $next_page ) ) {
10732
+ self::shoot_ajax_failure(
10733
+ isset( $next_page->error ) ?
10734
+ $next_page->error->message :
10735
+ var_export( $next_page, true )
10736
+ );
10737
+ }
10738
+
10739
+ $this->shoot_ajax_success( array(
10740
+ 'next_page' => $next_page,
10741
+ ) );
10742
+ }
10743
+
10744
+ /**
10745
+ * @author Leo Fajardo (@leorw)
10746
+ * @since 1.2.0
10747
+ */
10748
+ function _resend_license_key_ajax_action() {
10749
+ $this->_logger->entrance();
10750
+
10751
+ $this->check_ajax_referer( 'resend_license_key' );
10752
+
10753
+ $email_address = sanitize_email( trim( fs_request_get( 'email', '', 'post' ) ) );
10754
+
10755
+ if ( empty( $email_address ) ) {
10756
+ exit;
10757
+ }
10758
+
10759
+ $error = false;
10760
+
10761
+ $api = $this->get_api_plugin_scope();
10762
+ $result = $api->call( '/licenses/resend.json', 'post',
10763
+ array(
10764
+ 'email' => $email_address,
10765
+ 'url' => home_url(),
10766
+ )
10767
+ );
10768
+
10769
+ if ( is_object( $result ) && isset( $result->error ) ) {
10770
+ $error = $result->error;
10771
+
10772
+ if ( in_array( $error->code, array( 'invalid_email', 'no_user' ) ) ) {
10773
+ $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' );
10774
+ } else if ( 'no_license' === $error->code ) {
10775
+ $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' );
10776
+ } else {
10777
+ $error = $error->message;
10778
+ }
10779
+ }
10780
+
10781
+ $licenses = array(
10782
+ 'success' => ( false === $error )
10783
+ );
10784
+
10785
+ if ( false !== $error ) {
10786
+ $licenses['error'] = sprintf( '%s... %s', $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ), strtolower( $error ) );
10787
+ }
10788
+
10789
+ echo json_encode( $licenses );
10790
+
10791
+ exit;
10792
+ }
10793
+
10794
+ /**
10795
+ * @author Vova Feldman (@svovaf)
10796
+ * @since 1.2.1.8
10797
+ *
10798
+ * @var string
10799
+ */
10800
+ private static $_pagenow;
10801
+
10802
+ /**
10803
+ * Get current page or the referer if executing a WP AJAX request.
10804
+ *
10805
+ * @author Vova Feldman (@svovaf)
10806
+ * @since 1.2.1.8
10807
+ *
10808
+ * @return string
10809
+ */
10810
+ static function get_current_page() {
10811
+ if ( ! isset( self::$_pagenow ) ) {
10812
+ global $pagenow;
10813
+
10814
+ self::$_pagenow = $pagenow;
10815
+
10816
+ if ( self::is_ajax() &&
10817
+ 'admin-ajax.php' === $pagenow
10818
+ ) {
10819
+ $referer = fs_get_raw_referer();
10820
+
10821
+ if ( is_string( $referer ) ) {
10822
+ $parts = explode( '?', $referer );
10823
+
10824
+ self::$_pagenow = basename( $parts[0] );
10825
+ }
10826
+ }
10827
+ }
10828
+
10829
+ return self::$_pagenow;
10830
+ }
10831
+
10832
+ /**
10833
+ * Helper method to check if user in the plugins page.
10834
+ *
10835
+ * @author Vova Feldman (@svovaf)
10836
+ * @since 1.2.1.5
10837
+ *
10838
+ * @return bool
10839
+ */
10840
+ static function is_plugins_page() {
10841
+ return ( 'plugins.php' === self::get_current_page() );
10842
+ }
10843
+
10844
+ /**
10845
+ * Helper method to check if user in the themes page.
10846
+ *
10847
+ * @author Vova Feldman (@svovaf)
10848
+ * @since 1.2.2.6
10849
+ *
10850
+ * @return bool
10851
+ */
10852
+ static function is_themes_page() {
10853
+ return ( 'themes.php' === self::get_current_page() );
10854
+ }
10855
+
10856
+ #----------------------------------------------------------------------------------
10857
+ #region Affiliation
10858
+ #----------------------------------------------------------------------------------
10859
+
10860
+ /**
10861
+ * @author Leo Fajardo (@leorw)
10862
+ * @since 1.2.3
10863
+ *
10864
+ * @return bool
10865
+ */
10866
+ function has_affiliate_program() {
10867
+ if ( ! is_object( $this->_plugin ) ) {
10868
+ return false;
10869
+ }
10870
+
10871
+ return $this->_plugin->has_affiliate_program();
10872
+ }
10873
+
10874
+ /**
10875
+ * @author Leo Fajardo (@leorw)
10876
+ * @since 1.2.4
10877
+ */
10878
+ private function fetch_affiliate_terms() {
10879
+ if ( ! is_object( $this->plugin_affiliate_terms ) ) {
10880
+ $plugins_api = $this->get_api_plugin_scope();
10881
+ $affiliate_terms = $plugins_api->get( '/aff.json?type=affiliation', false );
10882
+
10883
+ if ( ! $this->is_api_result_entity( $affiliate_terms ) ) {
10884
+ return;
10885
+ }
10886
+
10887
+ $this->plugin_affiliate_terms = new FS_AffiliateTerms( $affiliate_terms );
10888
+ }
10889
+ }
10890
+
10891
+ /**
10892
+ * @author Leo Fajardo (@leorw)
10893
+ * @since 1.2.4
10894
+ */
10895
+ private function fetch_affiliate_and_custom_terms() {
10896
+ if ( ! empty( $this->_storage->affiliate_application_data ) ) {
10897
+ $application_data = $this->_storage->affiliate_application_data;
10898
+ $flush = ( ! isset( $application_data['status'] ) || 'pending' === $application_data['status'] );
10899
+
10900
+ $users_api = $this->get_api_user_scope();
10901
+ $result = $users_api->get( "/plugins/{$this->_plugin->id}/aff/{$this->plugin_affiliate_terms->id}/affiliates.json", $flush );
10902
+ if ( $this->is_api_result_object( $result, 'affiliates' ) ) {
10903
+ if ( ! empty( $result->affiliates ) ) {
10904
+ $affiliate = new FS_Affiliate( $result->affiliates[0] );
10905
+
10906
+ if ( ! isset( $application_data['status'] ) || $application_data['status'] !== $affiliate->status ) {
10907
+ $application_data['status'] = $affiliate->status;
10908
+ $this->_storage->affiliate_application_data = $application_data;
10909
+ }
10910
+
10911
+ if ( $affiliate->is_using_custom_terms ) {
10912
+ $affiliate_terms = $users_api->get( "/plugins/{$this->_plugin->id}/affiliates/{$affiliate->id}/aff/{$affiliate->custom_affiliate_terms_id}.json", $flush );
10913
+ if ( $this->is_api_result_entity( $affiliate_terms ) ) {
10914
+ $this->custom_affiliate_terms = new FS_AffiliateTerms( $affiliate_terms );
10915
+ }
10916
+ }
10917
+
10918
+ $this->affiliate = $affiliate;
10919
+ }
10920
+ }
10921
+ }
10922
+ }
10923
+
10924
+ /**
10925
+ * @author Leo Fajardo (@leorw)
10926
+ * @since 1.2.3
10927
+ */
10928
+ private function fetch_affiliate_and_terms() {
10929
+ $this->_logger->entrance();
10930
+
10931
+ $this->fetch_affiliate_terms();
10932
+ $this->fetch_affiliate_and_custom_terms();
10933
+ }
10934
+
10935
+ /**
10936
+ * @author Leo Fajardo (@leorw)
10937
+ * @since 1.2.3
10938
+ *
10939
+ * @return FS_Affiliate
10940
+ */
10941
+ function get_affiliate() {
10942
+ return $this->affiliate;
10943
+ }
10944
+
10945
+
10946
+ /**
10947
+ * @author Leo Fajardo (@leorw)
10948
+ * @since 1.2.3
10949
+ *
10950
+ * @return FS_AffiliateTerms
10951
+ */
10952
+ function get_affiliate_terms() {
10953
+ return is_object( $this->custom_affiliate_terms ) ?
10954
+ $this->custom_affiliate_terms :
10955
+ $this->plugin_affiliate_terms;
10956
+ }
10957
+
10958
+ /**
10959
+ * @author Leo Fajardo (@leorw)
10960
+ * @since 1.2.3
10961
+ */
10962
+ function _submit_affiliate_application() {
10963
+ $this->_logger->entrance();
10964
+
10965
+ $this->check_ajax_referer( 'submit_affiliate_application' );
10966
+
10967
+ if ( ! $this->is_user_admin() ) {
10968
+ // Only for admins.
10969
+ self::shoot_ajax_failure();
10970
+ }
10971
+
10972
+ $affiliate = fs_request_get( 'affiliate' );
10973
+
10974
+ if ( empty( $affiliate['promotion_methods'] ) ) {
10975
+ unset( $affiliate['promotion_methods'] );
10976
+ }
10977
+
10978
+ if ( ! empty( $affiliate['additional_domains'] ) ) {
10979
+ $affiliate['additional_domains'] = array_unique( $affiliate['additional_domains'] );
10980
+ }
10981
+
10982
+ if ( ! $this->is_registered() ) {
10983
+ // Opt in but don't track usage.
10984
+ $next_page = $this->opt_in(
10985
+ false,
10986
+ false,
10987
+ false,
10988
+ false,
10989
+ false,
10990
+ false,
10991
+ true
10992
+ );
10993
+
10994
+ if ( is_object( $next_page ) && $this->is_api_error( $next_page ) ) {
10995
+ self::shoot_ajax_failure(
10996
+ isset( $next_page->error ) ?
10997
+ $next_page->error->message :
10998
+ var_export( $next_page, true )
10999
+ );
11000
+ } else if ( $this->is_pending_activation() ) {
11001
+ self::shoot_ajax_failure( $this->get_text_inline( 'Account is pending activation.', 'account-is-pending-activation' ) );
11002
+ }
11003
+ }
11004
+
11005
+ $this->fetch_affiliate_terms();
11006
+
11007
+ $api = $this->get_api_user_scope();
11008
+ $result = $api->call(
11009
+ ( "/plugins/{$this->_plugin->id}/aff/{$this->plugin_affiliate_terms->id}/affiliates.json" ),
11010
+ 'post',
11011
+ $affiliate
11012
+ );
11013
+
11014
+ if ( $this->is_api_error( $result ) ) {
11015
+ self::shoot_ajax_failure(
11016
+ isset( $result->error ) ?
11017
+ $result->error->message :
11018
+ var_export( $result, true )
11019
+ );
11020
+ } else {
11021
+ if ( $this->_admin_notices->has_sticky( 'affiliate_program' ) ) {
11022
+ $this->_admin_notices->remove_sticky( 'affiliate_program' );
11023
+ }
11024
+
11025
+ $affiliate_application_data = array(
11026
+ 'status' => 'pending',
11027
+ 'stats_description' => $affiliate['stats_description'],
11028
+ 'promotion_method_description' => $affiliate['promotion_method_description'],
11029
+ );
11030
+
11031
+ if ( ! empty( $affiliate['promotion_methods'] ) ) {
11032
+ $affiliate_application_data['promotion_methods'] = $affiliate['promotion_methods'];
11033
+ }
11034
+
11035
+ if ( ! empty( $affiliate['domain'] ) ) {
11036
+ $affiliate_application_data['domain'] = $affiliate['domain'];
11037
+ }
11038
+
11039
+ if ( ! empty( $affiliate['additional_domains'] ) ) {
11040
+ $affiliate_application_data['additional_domains'] = $affiliate['additional_domains'];
11041
+ }
11042
+
11043
+ $this->_storage->affiliate_application_data = $affiliate_application_data;
11044
+ }
11045
+
11046
+ // Purge cached affiliate.
11047
+ $api->purge_cache( 'affiliate.json' );
11048
+
11049
+ self::shoot_ajax_success( $result );
11050
+ }
11051
+
11052
+ /**
11053
+ * @author Leo Fajardo (@leorw)
11054
+ * @since 1.2.3
11055
+ *
11056
+ * @return array|null
11057
+ */
11058
+ function get_affiliate_application_data() {
11059
+ if ( empty( $this->_storage->affiliate_application_data ) ) {
11060
+ return null;
11061
+ }
11062
+
11063
+ return $this->_storage->affiliate_application_data;
11064
+ }
11065
+
11066
+ #endregion Affiliation ------------------------------------------------------------
11067
+
11068
+ #----------------------------------------------------------------------------------
11069
+ #region URL Generators
11070
+ #----------------------------------------------------------------------------------
11071
+
11072
+ /**
11073
+ * Alias to pricing_url().
11074
+ *
11075
+ * @author Vova Feldman (@svovaf)
11076
+ * @since 1.0.2
11077
+ *
11078
+ * @uses pricing_url()
11079
+ *
11080
+ * @param string $period Billing cycle
11081
+ * @param bool $is_trial
11082
+ *
11083
+ * @return string
11084
+ */
11085
+ function get_upgrade_url( $period = WP_FS__PERIOD_ANNUALLY, $is_trial = false ) {
11086
+ return $this->pricing_url( $period, $is_trial );
11087
+ }
11088
+
11089
+ /**
11090
+ * @author Vova Feldman (@svovaf)
11091
+ * @since 1.0.9
11092
+ *
11093
+ * @uses get_upgrade_url()
11094
+ *
11095
+ * @return string
11096
+ */
11097
+ function get_trial_url() {
11098
+ return $this->get_upgrade_url( WP_FS__PERIOD_ANNUALLY, true );
11099
+ }
11100
+
11101
+ /**
11102
+ * Plugin's pricing URL.
11103
+ *
11104
+ * @author Vova Feldman (@svovaf)
11105
+ * @since 1.0.4
11106
+ *
11107
+ * @param string $billing_cycle Billing cycle
11108
+ *
11109
+ * @param bool $is_trial
11110
+ *
11111
+ * @return string
11112
+ */
11113
+ function pricing_url( $billing_cycle = WP_FS__PERIOD_ANNUALLY, $is_trial = false ) {
11114
+ $this->_logger->entrance();
11115
+
11116
+ $params = array(
11117
+ 'billing_cycle' => $billing_cycle
11118
+ );
11119
+
11120
+ if ( $is_trial ) {
11121
+ $params['trial'] = 'true';
11122
+ }
11123
+
11124
+ return $this->_get_admin_page_url( 'pricing', $params );
11125
+ }
11126
+
11127
+ /**
11128
+ * Checkout page URL.
11129
+ *
11130
+ * @author Vova Feldman (@svovaf)
11131
+ * @since 1.0.6
11132
+ *
11133
+ * @param string $billing_cycle Billing cycle
11134
+ * @param bool $is_trial
11135
+ * @param array $extra (optional) Extra parameters, override other query params.
11136
+ *
11137
+ * @return string
11138
+ */
11139
+ function checkout_url(
11140
+ $billing_cycle = WP_FS__PERIOD_ANNUALLY,
11141
+ $is_trial = false,
11142
+ $extra = array()
11143
+ ) {
11144
+ $this->_logger->entrance();
11145
+
11146
+ $params = array(
11147
+ 'checkout' => 'true',
11148
+ 'billing_cycle' => $billing_cycle,
11149
+ );
11150
+
11151
+ if ( $is_trial ) {
11152
+ $params['trial'] = 'true';
11153
+ }
11154
+
11155
+ /**
11156
+ * Params in extra override other params.
11157
+ */
11158
+ $params = array_merge( $params, $extra );
11159
+
11160
+ return $this->_get_admin_page_url( 'pricing', $params );
11161
+ }
11162
+
11163
+ /**
11164
+ * Add-on checkout URL.
11165
+ *
11166
+ * @author Vova Feldman (@svovaf)
11167
+ * @since 1.1.7
11168
+ *
11169
+ * @param number $addon_id
11170
+ * @param number $pricing_id
11171
+ * @param string $billing_cycle
11172
+ * @param bool $is_trial
11173
+ *
11174
+ * @return string
11175
+ */
11176
+ function addon_checkout_url(
11177
+ $addon_id,
11178
+ $pricing_id,
11179
+ $billing_cycle = WP_FS__PERIOD_ANNUALLY,
11180
+ $is_trial = false
11181
+ ) {
11182
+ return $this->checkout_url( $billing_cycle, $is_trial, array(
11183
+ 'plugin_id' => $addon_id,
11184
+ 'pricing_id' => $pricing_id,
11185
+ ) );
11186
+ }
11187
+
11188
+ #endregion
11189
+
11190
+ #endregion ------------------------------------------------------------------
11191
+
11192
+ /**
11193
+ * Check if plugin has any add-ons.
11194
+ *
11195
+ * @author Vova Feldman (@svovaf)
11196
+ * @since 1.0.5
11197
+ *
11198
+ * @since 1.1.7.3 Base logic only on the parameter provided by the developer in the init function.
11199
+ *
11200
+ * @return bool
11201
+ */
11202
+ function has_addons() {
11203
+ $this->_logger->entrance();
11204
+
11205
+ return $this->_has_addons;
11206
+ }
11207
+
11208
+ /**
11209
+ * Check if plugin can work in anonymous mode.
11210
+ *
11211
+ * @author Vova Feldman (@svovaf)
11212
+ * @since 1.0.9
11213
+ *
11214
+ * @return bool
11215
+ *
11216
+ * @deprecated Please use is_enable_anonymous() instead.
11217
+ */
11218
+ function enable_anonymous() {
11219
+ return $this->_enable_anonymous;
11220
+ }
11221
+
11222
+ /**
11223
+ * Check if plugin can work in anonymous mode.
11224
+ *
11225
+ * @author Vova Feldman (@svovaf)
11226
+ * @since 1.1.9
11227
+ *
11228
+ * @return bool
11229
+ */
11230
+ function is_enable_anonymous() {
11231
+ return $this->_enable_anonymous;
11232
+ }
11233
+
11234
+ /**
11235
+ * Check if plugin is premium only (no free plans).
11236
+ *
11237
+ * @author Vova Feldman (@svovaf)
11238
+ * @since 1.1.9
11239
+ *
11240
+ * @return bool
11241
+ */
11242
+ function is_only_premium() {
11243
+ return $this->_is_premium_only;
11244
+ }
11245
+
11246
+ /**
11247
+ * Checks if the plugin's type is "plugin". The other type is "theme".
11248
+ *
11249
+ * @author Leo Fajardo (@leorw)
11250
+ * @since 1.2.2
11251
+ *
11252
+ * @return bool
11253
+ */
11254
+ function is_plugin() {
11255
+ return ( WP_FS__MODULE_TYPE_PLUGIN === $this->_module_type );
11256
+ }
11257
+
11258
+ /**
11259
+ * @author Leo Fajardo (@leorw)
11260
+ * @since 1.2.2
11261
+ *
11262
+ * @return string
11263
+ */
11264
+ function get_module_type() {
11265
+ if ( ! isset( $this->_module_type ) ) {
11266
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
11267
+ $this->_module_type = $id_slug_type_path_map[ $this->_module_id ]['type'];
11268
+ }
11269
+
11270
+ return $this->_module_type;
11271
+ }
11272
+
11273
+ /**
11274
+ * @author Leo Fajardo (@leorw)
11275
+ * @since 1.2.2
11276
+ *
11277
+ * @return string
11278
+ */
11279
+ function get_plugin_main_file_path() {
11280
+ return $this->_plugin_main_file_path;
11281
+ }
11282
+
11283
+ /**
11284
+ * Check if module has a premium code version.
11285
+ *
11286
+ * Serviceware module might be freemium without any
11287
+ * premium code version, where the paid features
11288
+ * are all part of the service.
11289
+ *
11290
+ * @author Vova Feldman (@svovaf)
11291
+ * @since 1.2.1.6
11292
+ *
11293
+ * @return bool
11294
+ */
11295
+ function has_premium_version() {
11296
+ return $this->_has_premium_version;
11297
+ }
11298
+
11299
+ /**
11300
+ * Check if feature supported with current site's plan.
11301
+ *
11302
+ * @author Vova Feldman (@svovaf)
11303
+ * @since 1.0.1
11304
+ *
11305
+ * @todo IMPLEMENT
11306
+ *
11307
+ * @param number $feature_id
11308
+ *
11309
+ * @throws Exception
11310
+ */
11311
+ function is_feature_supported( $feature_id ) {
11312
+ throw new Exception( 'not implemented' );
11313
+ }
11314
+
11315
+ /**
11316
+ * @author Vova Feldman (@svovaf)
11317
+ * @since 1.0.1
11318
+ *
11319
+ * @return bool Is running in SSL/HTTPS
11320
+ */
11321
+ function is_ssl() {
11322
+ return WP_FS__IS_HTTPS;
11323
+ }
11324
+
11325
+ /**
11326
+ * @author Vova Feldman (@svovaf)
11327
+ * @since 1.0.9
11328
+ *
11329
+ * @return bool Is running in AJAX call.
11330
+ *
11331
+ * @link http://wordpress.stackexchange.com/questions/70676/how-to-check-if-i-am-in-admin-ajax
11332
+ */
11333
+ static function is_ajax() {
11334
+ return ( defined( 'DOING_AJAX' ) && DOING_AJAX );
11335
+ }
11336
+
11337
+ /**
11338
+ * Check if it's an AJAX call targeted for the current module.
11339
+ *
11340
+ * @author Vova Feldman (@svovaf)
11341
+ * @since 1.2.0
11342
+ *
11343
+ * @param array|string $actions Collection of AJAX actions.
11344
+ *
11345
+ * @return bool
11346
+ */
11347
+ function is_ajax_action( $actions ) {
11348
+ // Verify it's an ajax call.
11349
+ if ( ! self::is_ajax() ) {
11350
+ return false;
11351
+ }
11352
+
11353
+ // Verify the call is relevant for the plugin.
11354
+ if ( $this->_module_id != fs_request_get( 'module_id' ) ) {
11355
+ return false;
11356
+ }
11357
+
11358
+ // Verify it's one of the specified actions.
11359
+ if ( is_string( $actions ) ) {
11360
+ $actions = explode( ',', $actions );
11361
+ }
11362
+
11363
+ if ( is_array( $actions ) && 0 < count( $actions ) ) {
11364
+ $ajax_action = fs_request_get( 'action' );
11365
+
11366
+ foreach ( $actions as $action ) {
11367
+ if ( $ajax_action === $this->get_action_tag( $action ) ) {
11368
+ return true;
11369
+ }
11370
+ }
11371
+ }
11372
+
11373
+ return false;
11374
+ }
11375
+
11376
+ /**
11377
+ * Check if it's an AJAX call targeted for current request.
11378
+ *
11379
+ * @author Vova Feldman (@svovaf)
11380
+ * @since 1.2.0
11381
+ *
11382
+ * @param array|string $actions Collection of AJAX actions.
11383
+ * @param number|null $module_id
11384
+ *
11385
+ * @return bool
11386
+ */
11387
+ static function is_ajax_action_static( $actions, $module_id = null ) {
11388
+ // Verify it's an ajax call.
11389
+ if ( ! self::is_ajax() ) {
11390
+ return false;
11391
+ }
11392
+
11393
+
11394
+ if ( ! empty( $module_id ) ) {
11395
+ // Verify the call is relevant for the plugin.
11396
+ if ( $module_id != fs_request_get( 'module_id' ) ) {
11397
+ return false;
11398
+ }
11399
+ }
11400
+
11401
+ // Verify it's one of the specified actions.
11402
+ if ( is_string( $actions ) ) {
11403
+ $actions = explode( ',', $actions );
11404
+ }
11405
+
11406
+ if ( is_array( $actions ) && 0 < count( $actions ) ) {
11407
+ $ajax_action = fs_request_get( 'action' );
11408
+
11409
+ foreach ( $actions as $action ) {
11410
+ if ( $ajax_action === self::get_ajax_action_static( $action, $module_id ) ) {
11411
+ return true;
11412
+ }
11413
+ }
11414
+ }
11415
+
11416
+ return false;
11417
+ }
11418
+
11419
+ /**
11420
+ * @author Vova Feldman (@svovaf)
11421
+ * @since 1.1.7
11422
+ *
11423
+ * @return bool
11424
+ */
11425
+ static function is_cron() {
11426
+ return ( defined( 'DOING_CRON' ) && DOING_CRON );
11427
+ }
11428
+
11429
+ /**
11430
+ * Check if a real user is visiting the admin dashboard.
11431
+ *
11432
+ * @author Vova Feldman (@svovaf)
11433
+ * @since 1.1.7
11434
+ *
11435
+ * @return bool
11436
+ */
11437
+ function is_user_in_admin() {
11438
+ return is_admin() && ! self::is_ajax() && ! self::is_cron();
11439
+ }
11440
+
11441
+ /**
11442
+ * Check if a real user is in the customizer view.
11443
+ *
11444
+ * @author Vova Feldman (@svovaf)
11445
+ * @since 1.2.2.7
11446
+ *
11447
+ * @return bool
11448
+ */
11449
+ static function is_customizer() {
11450
+ return is_customize_preview();
11451
+ }
11452
+
11453
+ /**
11454
+ * Check if running in HTTPS and if site's plan matching the specified plan.
11455
+ *
11456
+ * @param string $plan
11457
+ * @param bool $exact
11458
+ *
11459
+ * @return bool
11460
+ */
11461
+ function is_ssl_and_plan( $plan, $exact = false ) {
11462
+ return ( $this->is_ssl() && $this->is_plan( $plan, $exact ) );
11463
+ }
11464
+
11465
+ /**
11466
+ * Construct plugin's settings page URL.
11467
+ *
11468
+ * @author Vova Feldman (@svovaf)
11469
+ * @since 1.0.4
11470
+ *
11471
+ * @param string $page
11472
+ * @param array $params
11473
+ * @param bool|null $network
11474
+ *
11475
+ * @return string
11476
+ */
11477
+ function _get_admin_page_url( $page = '', $params = array(), $network = null ) {
11478
+ if ( is_null( $network ) ) {
11479
+ $network = (
11480
+ $this->_is_network_active &&
11481
+ ( fs_is_network_admin() || ! $this->is_delegated_connection() )
11482
+ );
11483
+ }
11484
+
11485
+ if ( 0 < count( $params ) ) {
11486
+ foreach ( $params as $k => $v ) {
11487
+ $params[ $k ] = urlencode( $v );
11488
+ }
11489
+ }
11490
+
11491
+ $page_param = $this->_menu->get_slug( $page );
11492
+
11493
+ if ( empty( $page ) &&
11494
+ $this->is_theme() &&
11495
+ // Show the opt-in as an overlay for free wp.org themes or themes without any settings page.
11496
+ ( $this->is_free_wp_org_theme() || ! $this->has_settings_menu() )
11497
+ ) {
11498
+ $params[ $this->get_unique_affix() . '_show_optin' ] = 'true';
11499
+
11500
+ return add_query_arg(
11501
+ $params,
11502
+ $this->admin_url( 'themes.php', 'admin', $network )
11503
+ );
11504
+ }
11505
+
11506
+ if ( ! $this->has_settings_menu() ) {
11507
+ if ( ! empty( $page ) ) {
11508
+ // Module doesn't have a setting page, but since the request is for
11509
+ // a specific Freemius page, use the admin.php path.
11510
+ return add_query_arg( array_merge( $params, array(
11511
+ 'page' => $page_param,
11512
+ ) ), $this->admin_url( 'admin.php', 'admin', $network ) );
11513
+ } else {
11514
+ if ( $this->is_activation_mode() ) {
11515
+ /**
11516
+ * @author Vova Feldman
11517
+ * @since 1.2.1.6
11518
+ *
11519
+ * If plugin doesn't have a settings page, create one for the opt-in screen.
11520
+ */
11521
+ return add_query_arg( array_merge( $params, array(
11522
+ 'page' => $this->_slug,
11523
+ ) ), $this->admin_url( 'admin.php', 'admin', $network ) );
11524
+ } else {
11525
+ // Plugin without a settings page.
11526
+ return add_query_arg(
11527
+ $params,
11528
+ $this->admin_url( 'plugins.php', 'admin', $network )
11529
+ );
11530
+ }
11531
+ }
11532
+ }
11533
+
11534
+ // Module has a submenu settings page.
11535
+ if ( ! $this->_menu->is_top_level() ) {
11536
+ $parent_slug = $this->_menu->get_parent_slug();
11537
+ $menu_file = ( false !== strpos( $parent_slug, '.php' ) ) ?
11538
+ $parent_slug :
11539
+ 'admin.php';
11540
+
11541
+ return add_query_arg( array_merge( $params, array(
11542
+ 'page' => $page_param,
11543
+ ) ), $this->admin_url( $menu_file, 'admin', $network ) );
11544
+ }
11545
+
11546
+ // Module has a top level CPT settings page.
11547
+ if ( $this->_menu->is_cpt() ) {
11548
+ if ( empty( $page ) && $this->is_activation_mode() ) {
11549
+ return add_query_arg( array_merge( $params, array(
11550
+ 'page' => $page_param
11551
+ ) ), $this->admin_url( 'admin.php', 'admin', $network ) );
11552
+ } else {
11553
+ if ( ! empty( $page ) ) {
11554
+ $params['page'] = $page_param;
11555
+ }
11556
+
11557
+ return add_query_arg(
11558
+ $params,
11559
+ $this->admin_url( $this->_menu->get_raw_slug(), 'admin', $network )
11560
+ );
11561
+ }
11562
+ }
11563
+
11564
+ // Module has a custom top level settings page.
11565
+ return add_query_arg( array_merge( $params, array(
11566
+ 'page' => $page_param,
11567
+ ) ), $this->admin_url( 'admin.php', 'admin', $network ) );
11568
+ }
11569
+
11570
+ #--------------------------------------------------------------------------------
11571
+ #region Multisite
11572
+ #--------------------------------------------------------------------------------
11573
+
11574
+ /**
11575
+ * @author Leo Fajardo (@leorw)
11576
+ * @since 2.0.0
11577
+ *
11578
+ * @return bool
11579
+ */
11580
+ function is_network_active() {
11581
+ return $this->_is_network_active;
11582
+ }
11583
+
11584
+ /**
11585
+ * Delegate activation for the given sites in the network (or all sites if `null`) to site admins.
11586
+ *
11587
+ * @author Leo Fajardo (@leorw)
11588
+ * @since 2.0.0
11589
+ *
11590
+ * @param array|null $sites
11591
+ */
11592
+ private function delegate_connection( $sites = null ) {
11593
+ $this->_logger->entrance();
11594
+
11595
+ $this->_admin_notices->remove_sticky( 'connect_account' );
11596
+
11597
+ if ( is_null( $sites ) ) {
11598
+ // All sites delegation.
11599
+ $this->_storage->store( 'is_delegated_connection', true, true, true );
11600
+ } else {
11601
+ // Specified sites delegation.
11602
+ foreach ( $sites as $site ) {
11603
+ $this->delegate_site_connection( $site['blog_id'] );
11604
+ }
11605
+ }
11606
+
11607
+ $this->network_upgrade_mode_completed();
11608
+ }
11609
+
11610
+ /**
11611
+ * Delegate specific network site conncetion to the site admin.
11612
+ *
11613
+ * @author Vova Feldman (@svovaf)
11614
+ * @since 2.0.0
11615
+ *
11616
+ * @param int $blog_id
11617
+ */
11618
+ private function delegate_site_connection( $blog_id ) {
11619
+ $this->_storage->store( 'is_delegated_connection', true, $blog_id, true );
11620
+ }
11621
+
11622
+ /**
11623
+ * Check if super-admin delegated the connection of ALL sites to the site admins.
11624
+ *
11625
+ * @author Vova Feldman (@svovaf)
11626
+ * @since 2.0.0
11627
+ *
11628
+ * @return bool
11629
+ */
11630
+ function is_network_delegated_connection() {
11631
+ if ( ! $this->_is_network_active ) {
11632
+ return false;
11633
+ }
11634
+
11635
+ return $this->_storage->get( 'is_delegated_connection', false, true );
11636
+ }
11637
+
11638
+ /**
11639
+ * @author Leo Fajardo (@leorw)
11640
+ * @since 2.0.0
11641
+ *
11642
+ * @param int $blog_id
11643
+ *
11644
+ * @return bool
11645
+ */
11646
+ function is_site_delegated_connection( $blog_id = 0 ) {
11647
+ if ( ! $this->_is_network_active ) {
11648
+ return false;
11649
+ }
11650
+
11651
+ if ( 0 == $blog_id ) {
11652
+ $blog_id = get_current_blog_id();
11653
+ }
11654
+
11655
+ return $this->_storage->get( 'is_delegated_connection', false, $blog_id );
11656
+ }
11657
+
11658
+ /**
11659
+ * Check if delegated the connection. When running within the the network admin,
11660
+ * and haven't specified the blog ID, checks if network level delegated. If running
11661
+ * within a site admin or specified a blog ID, check if delegated the connection for
11662
+ * the current context site.
11663
+ *
11664
+ * If executed outside the the admin, check if delegated the connection
11665
+ * for the current context site OR the whole network.
11666
+ *
11667
+ * @author Vova Feldman (@svovaf)
11668
+ * @since 2.0.0
11669
+ *
11670
+ * @param int $blog_id If set, checks if network delegated or blog specific delegated.
11671
+ *
11672
+ * @return bool
11673
+ */
11674
+ function is_delegated_connection( $blog_id = 0 ) {
11675
+ if ( ! $this->_is_network_active ) {
11676
+ return false;
11677
+ }
11678
+
11679
+ if ( fs_is_network_admin() && 0 == $blog_id ) {
11680
+ return $this->is_network_delegated_connection();
11681
+ }
11682
+
11683
+ return (
11684
+ $this->is_network_delegated_connection() ||
11685
+ $this->is_site_delegated_connection( $blog_id )
11686
+ );
11687
+ }
11688
+
11689
+ /**
11690
+ * Check if the current module is active for the site.
11691
+ *
11692
+ * @author Vova Feldman (@svovaf)
11693
+ * @since 2.0.0
11694
+ *
11695
+ * @param int $blog_id
11696
+ *
11697
+ * @return bool
11698
+ */
11699
+ function is_active_for_site( $blog_id ) {
11700
+ if ( ! is_multisite() ) {
11701
+ // Not a multisite and this code is executed, means that the plugin is active.
11702
+ return true;
11703
+ }
11704
+
11705
+ if ( $this->is_theme() ) {
11706
+ // All themes are site level activated.
11707
+ return true;
11708
+ }
11709
+
11710
+ if ( $this->_is_network_active ) {
11711
+ // Plugin was network activated so it's active.
11712
+ return true;
11713
+ }
11714
+
11715
+ return in_array( $this->_plugin_basename, (array) get_blog_option( $blog_id, 'active_plugins', array() ) );
11716
+ }
11717
+
11718
+ /**
11719
+ * @author Leo Fajardo (@leorw)
11720
+ * @since 2.0.0
11721
+ *
11722
+ * @return array Active & public sites collection.
11723
+ */
11724
+ static function get_sites() {
11725
+ /**
11726
+ * For consistency with get_blog_list() which only return active public sites.
11727
+ *
11728
+ * @author Vova Feldman (@svovaf)
11729
+ */
11730
+ $args = array(
11731
+ 'public' => 1,
11732
+ 'archived' => 0,
11733
+ 'mature' => 0,
11734
+ 'spam' => 0,
11735
+ 'deleted' => 0,
11736
+ );
11737
+
11738
+ if ( function_exists( 'get_sites' ) ) {
11739
+ // For WP 4.6 and above.
11740
+ return get_sites( $args );
11741
+ } else if ( function_exists( 'wp_get_sites' ) ) {
11742
+ // For WP 3.7 to WP 4.5.
11743
+ return wp_get_sites( $args );
11744
+ } else {
11745
+ // For WP 3.6 and below.
11746
+ return get_blog_list( 0, 'all' );
11747
+ }
11748
+ }
11749
+
11750
+ /**
11751
+ * Checks if a given blog is active.
11752
+ *
11753
+ * @author Vova Feldman (@svovaf)
11754
+ * @since 2.0.0
11755
+ *
11756
+ * @param $blog_id
11757
+ *
11758
+ * @return bool
11759
+ */
11760
+ private static function is_site_active( $blog_id ) {
11761
+ global $wpdb;
11762
+
11763
+ $blog_info = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d", $blog_id ) );
11764
+
11765
+ if ( ! is_object( $blog_info ) ) {
11766
+ return false;
11767
+ }
11768
+
11769
+ return (
11770
+ true == $blog_info->public &&
11771
+ false == $blog_info->archived &&
11772
+ false == $blog_info->mature &&
11773
+ false == $blog_info->spam &&
11774
+ false == $blog_info->deleted
11775
+ );
11776
+ }
11777
+
11778
+ /**
11779
+ * Get a mapping between the site addresses to their blog IDs.
11780
+ *
11781
+ * @author Vova Feldman (@svovaf)
11782
+ * @since 2.0.0
11783
+ *
11784
+ * @return array {
11785
+ * @key string Site address without protocol with a trailing slash.
11786
+ * @value int Site's blog ID.
11787
+ * }
11788
+ */
11789
+ private function get_address_to_blog_map() {
11790
+ $sites = self::get_sites();
11791
+
11792
+ // Map site addresses to their blog IDs.
11793
+ $address_to_blog_map = array();
11794
+ foreach ( $sites as $site ) {
11795
+ $blog_id = self::get_site_blog_id( $site );
11796
+ $address = trailingslashit( fs_strip_url_protocol( get_site_url( $blog_id ) ) );
11797
+ $address_to_blog_map[ $address ] = $blog_id;
11798
+ }
11799
+
11800
+ return $address_to_blog_map;
11801
+ }
11802
+
11803
+ /**
11804
+ * Get a mapping between the site addresses to their blog IDs.
11805
+ *
11806
+ * @author Vova Feldman (@svovaf)
11807
+ * @since 2.0.0
11808
+ *
11809
+ * @return array {
11810
+ * @key int Site's blog ID.
11811
+ * @value FS_Site Associated install.
11812
+ * }
11813
+ */
11814
+ function get_blog_install_map() {
11815
+ $sites = self::get_sites();
11816
+
11817
+ // Map site blog ID to its install.
11818
+ $install_map = array();
11819
+
11820
+ foreach ( $sites as $site ) {
11821
+ $blog_id = self::get_site_blog_id( $site );
11822
+ $install = $this->get_install_by_blog_id( $blog_id );
11823
+
11824
+ if ( is_object( $install ) ) {
11825
+ $install_map[ $blog_id ] = $install;
11826
+ }
11827
+ }
11828
+
11829
+ return $install_map;
11830
+ }
11831
+
11832
+ /**
11833
+ * @author Leo Fajardo (@leorw)
11834
+ *
11835
+ * @return null|array {
11836
+ * 'install' => FS_Site Module's install,
11837
+ * 'blog_id' => string The associated blog ID.
11838
+ * }
11839
+ */
11840
+ private function find_first_install() {
11841
+ $sites = self::get_sites();
11842
+
11843
+ foreach ( $sites as $site ) {
11844
+ $blog_id = self::get_site_blog_id( $site );
11845
+ $install = $this->get_install_by_blog_id( $blog_id );
11846
+
11847
+ if ( is_object( $install ) ) {
11848
+ return array(
11849
+ 'install' => $install,
11850
+ 'blog_id' => $blog_id
11851
+ );
11852
+ }
11853
+ }
11854
+
11855
+ return null;
11856
+ }
11857
+
11858
+ /**
11859
+ * Switches the Freemius site level context to a specified blog.
11860
+ *
11861
+ * @author Vova Feldman (@svovaf)
11862
+ * @since 2.0.0
11863
+ *
11864
+ * @param int $blog_id
11865
+ * @param FS_Site $install
11866
+ */
11867
+ function switch_to_blog( $blog_id, FS_Site $install = null ) {
11868
+ if ( $blog_id == $this->_context_is_network_or_blog_id ) {
11869
+ return;
11870
+ }
11871
+
11872
+ switch_to_blog( $blog_id );
11873
+ $this->_context_is_network_or_blog_id = $blog_id;
11874
+
11875
+ self::$_accounts->set_site_blog_context( $blog_id );
11876
+ $this->_storage->set_site_blog_context( $blog_id );
11877
+ $this->_storage->set_network_active( true, $this->is_delegated_connection( $blog_id ) );
11878
+
11879
+ $this->_site = is_object( $install ) ?
11880
+ $install :
11881
+ $this->get_install_by_blog_id( $blog_id );
11882
+
11883
+ $this->_user = false;
11884
+ $this->_licenses = false;
11885
+ $this->_license = null;
11886
+
11887
+ if ( is_object( $this->_site ) ) {
11888
+ // Try to fetch user from install.
11889
+ $this->_user = self::_get_user_by_id( $this->_site->user_id );
11890
+
11891
+ if ( ! is_object( $this->_user ) &&
11892
+ FS_User::is_valid_id( $this->_storage->prev_user_id )
11893
+ ) {
11894
+ // Try to fetch previously saved user.
11895
+ $this->_user = self::_get_user_by_id( $this->_storage->prev_user_id );
11896
+
11897
+ if ( ! is_object( $this->_user ) ) {
11898
+ // Fallback to network's user.
11899
+ $this->_user = $this->get_network_user();
11900
+ }
11901
+ }
11902
+
11903
+ $all_plugin_licenses = self::get_all_licenses( $this->_module_id );
11904
+
11905
+ if ( ! empty( $all_plugin_licenses ) ) {
11906
+ if ( ! FS_Plugin_License::is_valid_id( $this->_site->license_id ) ) {
11907
+ $this->_license = null;
11908
+ } else {
11909
+ $license_found = false;
11910
+ foreach ( $all_plugin_licenses as $license ) {
11911
+ if ( $license->id == $this->_site->license_id ) {
11912
+ // License found.
11913
+ $this->_license = $license;
11914
+ $license_found = true;
11915
+ break;
11916
+ }
11917
+ }
11918
+
11919
+ if ( $license_found ) {
11920
+ $this->link_license_2_user( $this->_license->id, $this->_user->id );
11921
+ }
11922
+ }
11923
+
11924
+ $this->_licenses = $this->get_user_licenses( $this->_user->id );
11925
+ }
11926
+ }
11927
+
11928
+ unset( $this->_site_api );
11929
+ unset( $this->_user_api );
11930
+ }
11931
+
11932
+ /**
11933
+ * Restore the blog context to the blog that originally loaded the module.
11934
+ *
11935
+ * @author Vova Feldman (@svovaf)
11936
+ * @since 2.0.0
11937
+ */
11938
+ function restore_current_blog() {
11939
+ $this->switch_to_blog( $this->_blog_id );
11940
+ }
11941
+
11942
+ /**
11943
+ * @author Vova Feldman (@svovaf)
11944
+ * @since 2.0.0
11945
+ *
11946
+ * @param array|WP_Site $site
11947
+ *
11948
+ * @return int
11949
+ */
11950
+ static function get_site_blog_id( &$site ) {
11951
+ return ( $site instanceof WP_Site ) ?
11952
+ $site->blog_id :
11953
+ $site['blog_id'];
11954
+ }
11955
+
11956
+ /**
11957
+ * @author Leo Fajardo (@leorw)
11958
+ * @since 2.0.0
11959
+ *
11960
+ * @param array|WP_Site|null $site
11961
+ *
11962
+ * @return array
11963
+ */
11964
+ function get_site_info( $site = null ) {
11965
+ $this->_logger->entrance();
11966
+
11967
+ $switched = false;
11968
+
11969
+ if ( is_null( $site ) ) {
11970
+ $url = get_site_url();
11971
+ $name = get_bloginfo( 'name' );
11972
+ $blog_id = null;
11973
+ } else {
11974
+ $blog_id = self::get_site_blog_id( $site );
11975
+
11976
+ if ( get_current_blog_id() != $blog_id ) {
11977
+ switch_to_blog( $blog_id );
11978
+ $switched = true;
11979
+ }
11980
+
11981
+ if ( $site instanceof WP_Site ) {
11982
+ $url = $site->siteurl;
11983
+ $name = $site->blogname;
11984
+ } else {
11985
+ $url = get_site_url( $blog_id );
11986
+ $name = get_bloginfo( 'name' );
11987
+ }
11988
+ }
11989
+
11990
+ $info = array(
11991
+ 'uid' => $this->get_anonymous_id( $blog_id ),
11992
+ 'url' => $url,
11993
+ 'title' => $name,
11994
+ 'language' => get_bloginfo( 'language' ),
11995
+ 'charset' => get_bloginfo( 'charset' ),
11996
+ );
11997
+
11998
+ if ( is_numeric( $blog_id ) ) {
11999
+ $info['blog_id'] = $blog_id;
12000
+ }
12001
+
12002
+ if ( $switched ) {
12003
+ restore_current_blog();
12004
+ }
12005
+
12006
+ return $info;
12007
+ }
12008
+
12009
+ /**
12010
+ * Load the module's install based on the blog ID.
12011
+ *
12012
+ * @author Vova Feldman (@svovaf)
12013
+ * @since 2.0.0
12014
+ *
12015
+ * @param int|null $blog_id
12016
+ *
12017
+ * @return FS_Site
12018
+ */
12019
+ function get_install_by_blog_id( $blog_id = null ) {
12020
+ $installs = self::get_all_sites( $this->_module_type, $blog_id );
12021
+ $install = isset( $installs[ $this->_slug ] ) ? $installs[ $this->_slug ] : null;
12022
+
12023
+ if ( is_object( $install ) &&
12024
+ is_numeric( $install->id ) &&
12025
+ is_numeric( $install->user_id ) &&
12026
+ FS_Plugin_Plan::is_valid_id( $install->plan_id )
12027
+ ) {
12028
+ // Load site.
12029
+ $install = clone $install;
12030
+ }
12031
+
12032
+ return $install;
12033
+ }
12034
+
12035
+ /**
12036
+ * Check if module is installed on a specified site.
12037
+ *
12038
+ * @author Vova Feldman (@svovaf)
12039
+ * @since 2.0.0
12040
+ *
12041
+ * @param int|null $blog_id
12042
+ *
12043
+ * @return bool
12044
+ */
12045
+ function is_installed_on_site( $blog_id = null ) {
12046
+ $installs = self::get_all_sites( $this->_module_type, $blog_id );
12047
+ $install = isset( $installs[ $this->_slug ] ) ? $installs[ $this->_slug ] : null;
12048
+
12049
+ return (
12050
+ is_object( $install ) &&
12051
+ is_numeric( $install->id ) &&
12052
+ is_numeric( $install->user_id ) &&
12053
+ FS_Plugin_Plan::is_valid_id( $install->plan_id )
12054
+ );
12055
+ }
12056
+
12057
+ /**
12058
+ * Check if super-admin connected at least one site via the network opt-in.
12059
+ *
12060
+ * @author Vova Feldman (@svovaf)
12061
+ * @since 2.0.0
12062
+ *
12063
+ * @return bool
12064
+ */
12065
+ function is_network_registered() {
12066
+ if ( ! $this->_is_network_active ) {
12067
+ return false;
12068
+ }
12069
+
12070
+ return FS_User::is_valid_id( $this->_storage->network_user_id );
12071
+ }
12072
+
12073
+ /**
12074
+ * Returns the main user associated with the network.
12075
+ *
12076
+ * @author Vova Feldman (@svovaf)
12077
+ * @since 2.0.0
12078
+ *
12079
+ * @return FS_User
12080
+ */
12081
+ function get_network_user() {
12082
+ if ( ! $this->_is_network_active ) {
12083
+ return null;
12084
+ }
12085
+
12086
+ return FS_User::is_valid_id( $this->_storage->network_user_id ) ?
12087
+ self::_get_user_by_id( $this->_storage->network_user_id ) :
12088
+ null;
12089
+ }
12090
+
12091
+ /**
12092
+ * Returns the current context user or the network's main user.
12093
+ *
12094
+ * @author Vova Feldman (@svovaf)
12095
+ * @since 2.0.0
12096
+ *
12097
+ * @return FS_User
12098
+ */
12099
+ function get_current_or_network_user() {
12100
+ return ( $this->_user instanceof FS_User ) ?
12101
+ $this->_user :
12102
+ $this->get_network_user();
12103
+ }
12104
+
12105
+ /**
12106
+ * Returns the main install associated with the network.
12107
+ *
12108
+ * @author Vova Feldman (@svovaf)
12109
+ * @since 2.0.0
12110
+ *
12111
+ * @return FS_Site
12112
+ */
12113
+ function get_network_install() {
12114
+ if ( ! $this->_is_network_active ) {
12115
+ return null;
12116
+ }
12117
+
12118
+ return FS_Site::is_valid_id( $this->_storage->network_install_blog_id ) ?
12119
+ $this->get_install_by_blog_id( $this->_storage->network_install_blog_id ) :
12120
+ null;
12121
+ }
12122
+
12123
+ /**
12124
+ * Returns the blog ID that is associated with the main install.
12125
+ *
12126
+ * @author Leo Fajardo (@leorw)
12127
+ * @since 2.0.0
12128
+ *
12129
+ * @return int|null
12130
+ */
12131
+ function get_network_install_blog_id() {
12132
+ if ( ! $this->_is_network_active ) {
12133
+ return null;
12134
+ }
12135
+
12136
+ return FS_Site::is_valid_id( $this->_storage->network_install_blog_id ) ?
12137
+ $this->_storage->network_install_blog_id :
12138
+ null;
12139
+ }
12140
+
12141
+ /**
12142
+ * Returns the current context install or the network's main install.
12143
+ *
12144
+ * @author Vova Feldman (@svovaf)
12145
+ * @since 2.0.0
12146
+ *
12147
+ * @return FS_Site
12148
+ */
12149
+ function get_current_or_network_install() {
12150
+ return ( $this->_site instanceof FS_Site ) ?
12151
+ $this->_site :
12152
+ $this->get_network_install();
12153
+ }
12154
+
12155
+ /**
12156
+ * Check if executing a site level action from the network level admin.
12157
+ *
12158
+ * @author Vova Feldman (@svovaf)
12159
+ * @since 2.0.0
12160
+ *
12161
+ * @return false|int If yes, return the requested blog ID.
12162
+ */
12163
+ private function is_network_level_site_specific_action() {
12164
+ if ( ! $this->_is_network_active ) {
12165
+ return false;
12166
+ }
12167
+
12168
+ if ( ! fs_is_network_admin() ) {
12169
+ return false;
12170
+ }
12171
+
12172
+ $blog_id = fs_request_get( 'blog_id', '' );
12173
+
12174
+ return is_numeric( $blog_id ) ? $blog_id : false;
12175
+ }
12176
+
12177
+ /**
12178
+ * Check if executing an action from the network level admin.
12179
+ *
12180
+ * @author Vova Feldman (@svovaf)
12181
+ * @since 2.0.0
12182
+ *
12183
+ * @return bool
12184
+ */
12185
+ private function is_network_level_action() {
12186
+ return ( $this->_is_network_active && fs_is_network_admin() );
12187
+ }
12188
+
12189
+ /**
12190
+ * Needs to be executed after site deactivation, archive, deletion, or flag as spam.
12191
+ * 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.
12192
+ *
12193
+ * @author Vova Feldman (@svovaf)
12194
+ * @since 2.0.0
12195
+ *
12196
+ * @param int $context_blog_id
12197
+ */
12198
+ private function update_multisite_data_after_site_deactivation( $context_blog_id = 0 ) {
12199
+ $this->_logger->entrance();
12200
+
12201
+ if ( $this->_is_network_active ) {
12202
+ if ( $context_blog_id == $this->_storage->network_install_blog_id ) {
12203
+ $installs_map = $this->get_blog_install_map();
12204
+
12205
+ foreach ( $installs_map as $blog_id => $install ) {
12206
+ /**
12207
+ * @var FS_Site $install
12208
+ */
12209
+ if ( $context_blog_id == $blog_id ) {
12210
+ continue;
12211
+ }
12212
+
12213
+ if ( $install->user_id != $this->_storage->network_user_id ) {
12214
+ continue;
12215
+ }
12216
+
12217
+ // Switch reference to a blog that is opted-in and belong to the same super-admin.
12218
+ $this->_storage->network_install_blog_id = $blog_id;
12219
+ break;
12220
+ }
12221
+ }
12222
+ }
12223
+
12224
+ if ( $this->is_sync_cron_scheduled() &&
12225
+ $context_blog_id == $this->get_sync_cron_blog_id()
12226
+ ) {
12227
+ $this->schedule_sync_cron( WP_FS__SCRIPT_START_TIME, true, $context_blog_id );
12228
+ }
12229
+
12230
+ if ( $this->is_install_sync_scheduled() &&
12231
+ $context_blog_id == $this->get_install_sync_cron_blog_id()
12232
+ ) {
12233
+ $this->schedule_install_sync( $context_blog_id );
12234
+ }
12235
+ }
12236
+
12237
+ /**
12238
+ * Executed after site deactivation, archive, or flag as spam.
12239
+ *
12240
+ * @author Vova Feldman (@svovaf)
12241
+ * @since 2.0.0
12242
+ *
12243
+ * @param int $context_blog_id
12244
+ */
12245
+ public function _after_site_deactivated_callback( $context_blog_id = 0 ) {
12246
+ $this->_logger->entrance();
12247
+
12248
+ $install = $this->get_install_by_blog_id( $context_blog_id );
12249
+
12250
+ if ( ! is_object( $install ) ) {
12251
+ // Site not connected.
12252
+ return;
12253
+ }
12254
+
12255
+ $this->update_multisite_data_after_site_deactivation( $context_blog_id );
12256
+
12257
+ $current_blog_id = get_current_blog_id();
12258
+
12259
+ $this->switch_to_blog( $context_blog_id );
12260
+
12261
+ // Send deactivation event.
12262
+ $this->sync_install( array(
12263
+ 'is_active' => false,
12264
+ ) );
12265
+
12266
+ $this->switch_to_blog( $current_blog_id );
12267
+ }
12268
+
12269
+ /**
12270
+ * Executed after site deletion.
12271
+ *
12272
+ * @author Vova Feldman (@svovaf)
12273
+ * @since 2.0.0
12274
+ *
12275
+ * @param int $context_blog_id
12276
+ * @param bool $drop True if site's database tables should be dropped. Default is false.
12277
+ */
12278
+ public function _after_site_deleted_callback( $context_blog_id = 0, $drop = false ) {
12279
+ $this->_logger->entrance();
12280
+
12281
+ $install = $this->get_install_by_blog_id( $context_blog_id );
12282
+
12283
+ if ( ! is_object( $install ) ) {
12284
+ // Site not connected.
12285
+ return;
12286
+ }
12287
+
12288
+ $this->update_multisite_data_after_site_deactivation( $context_blog_id );
12289
+
12290
+ $current_blog_id = get_current_blog_id();
12291
+
12292
+ $this->switch_to_blog( $context_blog_id );
12293
+
12294
+ if ( $drop ) {
12295
+ // Delete install if dropping site DB.
12296
+ $this->delete_account_event();
12297
+ } else {
12298
+ // Send deactivation event.
12299
+ $this->sync_install( array(
12300
+ 'is_active' => false,
12301
+ ) );
12302
+ }
12303
+
12304
+ $this->switch_to_blog( $current_blog_id );
12305
+ }
12306
+
12307
+ /**
12308
+ * Executed after site re-activation.
12309
+ *
12310
+ * @author Vova Feldman (@svovaf)
12311
+ * @since 2.0.0
12312
+ *
12313
+ * @param int $context_blog_id
12314
+ */
12315
+ public function _after_site_reactivated_callback( $context_blog_id = 0 ) {
12316
+ $this->_logger->entrance();
12317
+
12318
+ $install = $this->get_install_by_blog_id( $context_blog_id );
12319
+
12320
+ if ( ! is_object( $install ) ) {
12321
+ // Site not connected.
12322
+ return;
12323
+ }
12324
+
12325
+ if ( ! self::is_site_active( $context_blog_id ) ) {
12326
+ // Site not yet active (can be in spam mode, archived, deleted...).
12327
+ return;
12328
+ }
12329
+
12330
+ $current_blog_id = get_current_blog_id();
12331
+
12332
+ $this->switch_to_blog( $context_blog_id );
12333
+
12334
+ // Send re-activation event.
12335
+ $this->sync_install( array(
12336
+ 'is_active' => true,
12337
+ ) );
12338
+
12339
+ $this->switch_to_blog( $current_blog_id );
12340
+ }
12341
+
12342
+ #endregion Multisite
12343
+
12344
+ /**
12345
+ * @author Leo Fajardo (@leorw)
12346
+ *
12347
+ * @param string $path
12348
+ * @param string $scheme
12349
+ * @param bool $network
12350
+ *
12351
+ * @return string
12352
+ */
12353
+ private function admin_url( $path = '', $scheme = 'admin', $network = true ) {
12354
+ return ( $this->_is_network_active && $network ) ?
12355
+ network_admin_url( $path, $scheme ) :
12356
+ admin_url( $path, $scheme );
12357
+ }
12358
+
12359
+ /**
12360
+ * Check if currently in a specified admin page.
12361
+ *
12362
+ * @author Vova Feldman (@svovaf)
12363
+ * @since 1.2.2.7
12364
+ *
12365
+ * @param string $page
12366
+ *
12367
+ * @return bool
12368
+ */
12369
+ function is_admin_page( $page ) {
12370
+ return ( $this->_menu->get_slug( $page ) === fs_request_get( 'page', '', 'get' ) );
12371
+ }
12372
+
12373
+ /**
12374
+ * Get module's main admin setting page URL.
12375
+ *
12376
+ * @author Vova Feldman (@svovaf)
12377
+ * @since 1.2.2.7
12378
+ *
12379
+ * @return string
12380
+ */
12381
+ function main_menu_url() {
12382
+ return $this->_menu->main_menu_url();
12383
+ }
12384
+
12385
+ /**
12386
+ * Check if currently on the theme's setting page or
12387
+ * on any of the Freemius added pages (via tabs).
12388
+ *
12389
+ * @author Vova Feldman (@svovaf)
12390
+ * @since 1.2.2.7
12391
+ *
12392
+ * @return bool
12393
+ */
12394
+ function is_theme_settings_page() {
12395
+ return fs_starts_with(
12396
+ fs_request_get( 'page', '', 'get' ),
12397
+ $this->_menu->get_slug()
12398
+ );
12399
+ }
12400
+
12401
+ /**
12402
+ * Plugin's account page + sync license URL.
12403
+ *
12404
+ * @author Vova Feldman (@svovaf)
12405
+ * @since 1.1.9.1
12406
+ *
12407
+ * @param bool|number $plugin_id
12408
+ * @param bool $add_action_nonce
12409
+ * @param array $params
12410
+ *
12411
+ * @return string
12412
+ */
12413
+ function _get_sync_license_url( $plugin_id = false, $add_action_nonce = true, $params = array() ) {
12414
+ if ( is_numeric( $plugin_id ) ) {
12415
+ $params['plugin_id'] = $plugin_id;
12416
+ }
12417
+
12418
+ return $this->get_account_url(
12419
+ $this->get_unique_affix() . '_sync_license',
12420
+ $params,
12421
+ $add_action_nonce
12422
+ );
12423
+ }
12424
+
12425
+ /**
12426
+ * Plugin's account URL.
12427
+ *
12428
+ * @author Vova Feldman (@svovaf)
12429
+ * @since 1.0.4
12430
+ *
12431
+ * @param bool|string $action
12432
+ * @param array $params
12433
+ *
12434
+ * @param bool $add_action_nonce
12435
+ *
12436
+ * @return string
12437
+ */
12438
+ function get_account_url( $action = false, $params = array(), $add_action_nonce = true ) {
12439
+ if ( is_string( $action ) ) {
12440
+ $params['fs_action'] = $action;
12441
+ }
12442
+
12443
+ self::require_pluggable_essentials();
12444
+
12445
+ return ( $add_action_nonce && is_string( $action ) ) ?
12446
+ fs_nonce_url( $this->_get_admin_page_url( 'account', $params ), $action ) :
12447
+ $this->_get_admin_page_url( 'account', $params );
12448
+ }
12449
+
12450
+ /**
12451
+ * @author Vova Feldman (@svovaf)
12452
+ * @since 1.2.0
12453
+ *
12454
+ * @param string $tab
12455
+ * @param bool $action
12456
+ * @param array $params
12457
+ * @param bool $add_action_nonce
12458
+ *
12459
+ * @return string
12460
+ *
12461
+ * @uses get_account_url()
12462
+ */
12463
+ function get_account_tab_url( $tab, $action = false, $params = array(), $add_action_nonce = true ) {
12464
+ $params['tab'] = $tab;
12465
+
12466
+ return $this->get_account_url( $action, $params, $add_action_nonce );
12467
+ }
12468
+
12469
+ /**
12470
+ * Plugin's account URL.
12471
+ *
12472
+ * @author Vova Feldman (@svovaf)
12473
+ * @since 1.0.4
12474
+ *
12475
+ * @param bool|string $topic
12476
+ * @param bool|string $message
12477
+ *
12478
+ * @return string
12479
+ */
12480
+ function contact_url( $topic = false, $message = false ) {
12481
+ $params = array();
12482
+ if ( is_string( $topic ) ) {
12483
+ $params['topic'] = $topic;
12484
+ }
12485
+ if ( is_string( $message ) ) {
12486
+ $params['message'] = $message;
12487
+ }
12488
+
12489
+ if ( $this->is_addon() ) {
12490
+ $params['addon_id'] = $this->get_id();
12491
+
12492
+ return $this->get_parent_instance()->_get_admin_page_url( 'contact', $params );
12493
+ } else {
12494
+ return $this->_get_admin_page_url( 'contact', $params );
12495
+ }
12496
+ }
12497
+
12498
+ /**
12499
+ * Add-on direct info URL.
12500
+ *
12501
+ * @author Vova Feldman (@svovaf)
12502
+ * @since 1.1.0
12503
+ *
12504
+ * @param string $slug
12505
+ *
12506
+ * @return string
12507
+ */
12508
+ function addon_url( $slug ) {
12509
+ return $this->_get_admin_page_url( 'addons', array(
12510
+ 'slug' => $slug
12511
+ ) );
12512
+ }
12513
+
12514
+ /* Logger
12515
+ ------------------------------------------------------------------------------------------------------------------*/
12516
+ /**
12517
+ * @param string $id
12518
+ * @param bool $prefix_slug
12519
+ *
12520
+ * @return FS_Logger
12521
+ */
12522
+ function get_logger( $id = '', $prefix_slug = true ) {
12523
+ return FS_Logger::get_logger( ( $prefix_slug ? $this->_slug : '' ) . ( ( ! $prefix_slug || empty( $id ) ) ? '' : '_' ) . $id );
12524
+ }
12525
+
12526
+ /**
12527
+ * Note: This method is used externally so don't delete it.
12528
+ *
12529
+ * @param $id
12530
+ * @param bool $load_options
12531
+ * @param bool $prefix_slug
12532
+ *
12533
+ * @return FS_Option_Manager
12534
+ */
12535
+ function get_options_manager( $id, $load_options = false, $prefix_slug = true ) {
12536
+ return FS_Option_Manager::get_manager( ( $prefix_slug ? $this->_slug : '' ) . ( ( ! $prefix_slug || empty( $id ) ) ? '' : '_' ) . $id, $load_options );
12537
+ }
12538
+
12539
+ /* Security
12540
+ ------------------------------------------------------------------------------------------------------------------*/
12541
+ private static function _encrypt( $str ) {
12542
+ if ( is_null( $str ) ) {
12543
+ return null;
12544
+ }
12545
+
12546
+ /**
12547
+ * The encrypt/decrypt functions are used to protect
12548
+ * the user from messing up with some of the sensitive
12549
+ * data stored for the module as a JSON in the database.
12550
+ *
12551
+ * I used the same suggested hack by the theme review team.
12552
+ * For more details, look at the function `Base64UrlDecode()`
12553
+ * in `./sdk/FreemiusBase.php`.
12554
+ *
12555
+ * @todo Remove this hack once the base64 error is removed from the Theme Check.
12556
+ *
12557
+ * @author Vova Feldman (@svovaf)
12558
+ * @since 1.2.2
12559
+ */
12560
+ $fn = 'base64' . '_encode';
12561
+
12562
+ return $fn( $str );
12563
+ }
12564
+
12565
+ static function _decrypt( $str ) {
12566
+ if ( is_null( $str ) ) {
12567
+ return null;
12568
+ }
12569
+
12570
+ /**
12571
+ * The encrypt/decrypt functions are used to protect
12572
+ * the user from messing up with some of the sensitive
12573
+ * data stored for the module as a JSON in the database.
12574
+ *
12575
+ * I used the same suggested hack by the theme review team.
12576
+ * For more details, look at the function `Base64UrlDecode()`
12577
+ * in `./sdk/FreemiusBase.php`.
12578
+ *
12579
+ * @todo Remove this hack once the base64 error is removed from the Theme Check.
12580
+ *
12581
+ * @author Vova Feldman (@svovaf)
12582
+ * @since 1.2.2
12583
+ */
12584
+ $fn = 'base64' . '_decode';
12585
+
12586
+ return $fn( $str );
12587
+ }
12588
+
12589
+ /**
12590
+ * @author Vova Feldman (@svovaf)
12591
+ * @since 1.0.5
12592
+ *
12593
+ * @param FS_Entity $entity
12594
+ *
12595
+ * @return FS_Entity Return an encrypted clone entity.
12596
+ */
12597
+ private static function _encrypt_entity( FS_Entity $entity ) {
12598
+ $clone = clone $entity;
12599
+ $props = get_object_vars( $entity );
12600
+
12601
+ foreach ( $props as $key => $val ) {
12602
+ $clone->{$key} = self::_encrypt( $val );
12603
+ }
12604
+
12605
+ return $clone;
12606
+ }
12607
+
12608
+ /**
12609
+ * @author Vova Feldman (@svovaf)
12610
+ * @since 1.0.5
12611
+ *
12612
+ * @param FS_Entity $entity
12613
+ *
12614
+ * @return FS_Entity Return an decrypted clone entity.
12615
+ */
12616
+ private static function decrypt_entity( FS_Entity $entity ) {
12617
+ $clone = clone $entity;
12618
+ $props = get_object_vars( $entity );
12619
+
12620
+ foreach ( $props as $key => $val ) {
12621
+ $clone->{$key} = self::_decrypt( $val );
12622
+ }
12623
+
12624
+ return $clone;
12625
+ }
12626
+
12627
+ /**
12628
+ * Tries to activate account based on POST params.
12629
+ *
12630
+ * @author Vova Feldman (@svovaf)
12631
+ * @since 1.0.2
12632
+ *
12633
+ * @deprecated Not in use, outdated.
12634
+ */
12635
+ function _activate_account() {
12636
+ if ( $this->is_registered() ) {
12637
+ // Already activated.
12638
+ return;
12639
+ }
12640
+
12641
+ self::_clean_admin_content_section();
12642
+
12643
+ if ( fs_request_is_action( 'activate' ) && fs_request_is_post() ) {
12644
+ // check_admin_referer( 'activate_' . $this->_plugin->public_key );
12645
+
12646
+ // Verify matching plugin details.
12647
+ if ( $this->_plugin->id != fs_request_get( 'plugin_id' ) || $this->_slug != fs_request_get( 'plugin_slug' ) ) {
12648
+ return;
12649
+ }
12650
+
12651
+ $user = new FS_User();
12652
+ $user->id = fs_request_get( 'user_id' );
12653
+ $user->public_key = fs_request_get( 'user_public_key' );
12654
+ $user->secret_key = fs_request_get( 'user_secret_key' );
12655
+ $user->email = fs_request_get( 'user_email' );
12656
+ $user->first = fs_request_get( 'user_first' );
12657
+ $user->last = fs_request_get( 'user_last' );
12658
+ $user->is_verified = fs_request_get_bool( 'user_is_verified' );
12659
+
12660
+ $site = new FS_Site();
12661
+ $site->id = fs_request_get( 'install_id' );
12662
+ $site->public_key = fs_request_get( 'install_public_key' );
12663
+ $site->secret_key = fs_request_get( 'install_secret_key' );
12664
+ $site->plan_id = fs_request_get( 'plan_id' );
12665
+
12666
+ $plans = array();
12667
+ $plans_data = json_decode( urldecode( fs_request_get( 'plans' ) ) );
12668
+ foreach ( $plans_data as $p ) {
12669
+ $plan = new FS_Plugin_Plan( $p );
12670
+ if ( $site->plan_id == $plan->id ) {
12671
+ $plan->title = fs_request_get( 'plan_title' );
12672
+ $plan->name = fs_request_get( 'plan_name' );
12673
+ }
12674
+
12675
+ $plans[] = $plan;
12676
+ }
12677
+
12678
+ $this->_set_account( $user, $site, $plans );
12679
+
12680
+ // Reload the page with the keys.
12681
+ fs_redirect( $this->_get_admin_page_url() );
12682
+ }
12683
+ }
12684
+
12685
+ /**
12686
+ * @author Vova Feldman (@svovaf)
12687
+ * @since 1.0.7
12688
+ *
12689
+ * @param string $email
12690
+ *
12691
+ * @return FS_User|false
12692
+ */
12693
+ static function _get_user_by_email( $email ) {
12694
+ self::$_static_logger->entrance();
12695
+
12696
+ $email = trim( strtolower( $email ) );
12697
+
12698
+ $users = self::get_all_users();
12699
+
12700
+ if ( is_array( $users ) ) {
12701
+ foreach ( $users as $user ) {
12702
+ if ( $email === trim( strtolower( $user->email ) ) ) {
12703
+ return $user;
12704
+ }
12705
+ }
12706
+ }
12707
+
12708
+ return false;
12709
+ }
12710
+
12711
+ #----------------------------------------------------------------------------------
12712
+ #region Account (Loading, Updates & Activation)
12713
+ #----------------------------------------------------------------------------------
12714
+
12715
+ /***
12716
+ * Load account information (user + site).
12717
+ *
12718
+ * @author Vova Feldman (@svovaf)
12719
+ * @since 1.0.1
12720
+ */
12721
+ private function _load_account() {
12722
+ $this->_logger->entrance();
12723
+
12724
+ $this->do_action( 'before_account_load' );
12725
+
12726
+ $users = self::get_all_users();
12727
+ $plans = self::get_all_plans( $this->_module_type );
12728
+
12729
+ if ( $this->_logger->is_on() && is_admin() ) {
12730
+ $this->_logger->log( 'users = ' . var_export( $users, true ) );
12731
+ $this->_logger->log( 'plans = ' . var_export( $plans, true ) );
12732
+ }
12733
+
12734
+ $site = fs_is_network_admin() ?
12735
+ $this->get_network_install() :
12736
+ $this->get_install_by_blog_id();
12737
+
12738
+ if ( fs_is_network_admin() &&
12739
+ ! is_object( $site ) &&
12740
+ FS_Site::is_valid_id( $this->_storage->network_install_blog_id )
12741
+ ) {
12742
+ $first_install = $this->find_first_install();
12743
+
12744
+ if ( is_null( $first_install ) ) {
12745
+ unset( $this->_storage->network_install_blog_id );
12746
+ } else {
12747
+ $site = $first_install['install'];
12748
+ $this->_storage->network_install_blog_id = $first_install['blog_id'];
12749
+ }
12750
+ }
12751
+
12752
+ if ( is_object( $site ) &&
12753
+ is_numeric( $site->id ) &&
12754
+ is_numeric( $site->user_id ) &&
12755
+ FS_Plugin_Plan::is_valid_id( $site->plan_id )
12756
+ ) {
12757
+ // Load site.
12758
+ $this->_site = $site;
12759
+
12760
+ // Load plans.
12761
+ $this->_plans = $plans[ $this->_slug ];
12762
+ if ( ! is_array( $this->_plans ) || empty( $this->_plans ) ) {
12763
+ $this->_sync_plans();
12764
+ } else {
12765
+ for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
12766
+ if ( $this->_plans[ $i ] instanceof FS_Plugin_Plan ) {
12767
+ $this->_plans[ $i ] = self::decrypt_entity( $this->_plans[ $i ] );
12768
+ } else {
12769
+ unset( $this->_plans[ $i ] );
12770
+ }
12771
+ }
12772
+ }
12773
+ }
12774
+
12775
+ $user = null;
12776
+ if ( fs_is_network_admin() && $this->_is_network_active ) {
12777
+ $user = $this->get_network_user();
12778
+ }
12779
+
12780
+ if ( is_object( $user ) ) {
12781
+ $this->_user = clone $user;
12782
+ } else if ( $this->_site ) {
12783
+ $user = self::_get_user_by_id( $this->_site->user_id );
12784
+
12785
+ if ( ! is_object( $user ) && FS_User::is_valid_id( $this->_storage->prev_user_id ) ) {
12786
+ /**
12787
+ * Try to load the previous owner. This recovery is used for the following use-case:
12788
+ * 1. Opt-in
12789
+ * 2. Cloning site1 to site2
12790
+ * 3. Ownership switch in site1 (same applies for site2)
12791
+ * 4. Install data sync on site2
12792
+ * 5. Now site2's install is associated with the new owner which does not exists locally.
12793
+ */
12794
+ $user = self::_get_user_by_id( $this->_storage->prev_user_id );
12795
+ }
12796
+
12797
+ if ( ! is_object( $user ) ) {
12798
+ /**
12799
+ * This is a special fault tolerance mechanism to handle a scenario that the user data is missing.
12800
+ */
12801
+ $user = $this->fetch_user_by_install();
12802
+ }
12803
+
12804
+ $this->_user = ( $user instanceof FS_User ) ?
12805
+ clone $user :
12806
+ null;
12807
+ }
12808
+
12809
+ if ( is_object( $this->_user ) ) {
12810
+ // Load licenses.
12811
+ $this->_licenses = $this->get_user_licenses( $this->_user->id );
12812
+ }
12813
+
12814
+ if ( is_object( $this->_site ) ) {
12815
+ $this->_license = $this->_get_license_by_id( $this->_site->license_id );
12816
+
12817
+ if ( $this->_site->version != $this->get_plugin_version() ) {
12818
+ // If stored install version is different than current installed plugin version,
12819
+ // then update plugin version event.
12820
+ $this->update_plugin_version_event();
12821
+ }
12822
+ }
12823
+
12824
+ if ( $this->is_theme() ) {
12825
+ $this->_register_account_hooks();
12826
+ }
12827
+ }
12828
+
12829
+ /**
12830
+ * Special user recovery mechanism.
12831
+ *
12832
+ * @author Vova Feldman (@svovaf)
12833
+ * @since 2.0.0
12834
+ *
12835
+ * @return \FS_User|mixed
12836
+ */
12837
+ private function fetch_user_by_install() {
12838
+ $api = $this->get_api_site_scope();
12839
+
12840
+ $uid = $this->get_anonymous_id();
12841
+ $request_path = "/users/{$this->_site->user_id}.json?uid={$uid}";
12842
+
12843
+ $result = $api->get( $request_path, false, WP_FS__TIME_10_MIN_IN_SEC );
12844
+
12845
+ if ( $this->is_api_result_entity( $result ) ) {
12846
+ $user = new FS_User( $result );
12847
+ $this->_user = $user;
12848
+ $this->_store_user();
12849
+
12850
+ return $user;
12851
+ }
12852
+
12853
+ $error_code = FS_Api::get_error_code( $result );
12854
+
12855
+ if ( in_array( $error_code, array( 'invalid_unique_id', 'user_cannot_be_recovered' ) ) ) {
12856
+ /**
12857
+ * Those API errors will continue coming and are not recoverable with the
12858
+ * current site's data. Therefore, extend the API call's cached result to 7 days.
12859
+ */
12860
+ $api->update_cache_expiration( $request_path, WP_FS__TIME_WEEK_IN_SEC );
12861
+ }
12862
+
12863
+ return $result;
12864
+ }
12865
+
12866
+ /**
12867
+ * @author Vova Feldman (@svovaf)
12868
+ * @since 1.0.1
12869
+ *
12870
+ * @param FS_User $user
12871
+ * @param FS_Site $site
12872
+ * @param bool|array $plans
12873
+ */
12874
+ private function _set_account( FS_User $user, FS_Site $site, $plans = false ) {
12875
+ $site->user_id = $user->id;
12876
+
12877
+ $this->_site = $site;
12878
+ $this->_user = $user;
12879
+ if ( false !== $plans ) {
12880
+ $this->_plans = $plans;
12881
+ }
12882
+
12883
+ $this->send_install_update();
12884
+
12885
+ $this->_store_account();
12886
+
12887
+ }
12888
+
12889
+ /**
12890
+ * @author Vova Feldman (@svovaf)
12891
+ * @since 1.1.7.4
12892
+ *
12893
+ * @param array $override_with
12894
+ * @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.
12895
+ *
12896
+ * @return array
12897
+ */
12898
+ function get_opt_in_params( $override_with = array(), $network_level_or_blog_id = null ) {
12899
+ $this->_logger->entrance();
12900
+
12901
+ $current_user = self::_get_current_wp_user();
12902
+
12903
+ $activation_action = $this->get_unique_affix() . '_activate_new';
12904
+ $return_url = $this->is_anonymous() ?
12905
+ // If skipped already, then return to the account page.
12906
+ $this->get_account_url( $activation_action, array(), false ) :
12907
+ // Return to the module's main page.
12908
+ $this->get_after_activation_url( 'after_connect_url', array( 'fs_action' => $activation_action ) );
12909
+
12910
+ $params = array(
12911
+ 'user_firstname' => $current_user->user_firstname,
12912
+ 'user_lastname' => $current_user->user_lastname,
12913
+ 'user_nickname' => $current_user->user_nicename,
12914
+ 'user_email' => $current_user->user_email,
12915
+ 'user_ip' => WP_FS__REMOTE_ADDR,
12916
+ 'plugin_slug' => $this->_slug,
12917
+ 'plugin_id' => $this->get_id(),
12918
+ 'plugin_public_key' => $this->get_public_key(),
12919
+ 'plugin_version' => $this->get_plugin_version(),
12920
+ 'return_url' => fs_nonce_url( $return_url, $activation_action ),
12921
+ 'account_url' => fs_nonce_url( $this->_get_admin_page_url(
12922
+ 'account',
12923
+ array( 'fs_action' => 'sync_user' )
12924
+ ), 'sync_user' ),
12925
+ 'platform_version' => get_bloginfo( 'version' ),
12926
+ 'sdk_version' => $this->version,
12927
+ 'programming_language_version' => phpversion(),
12928
+ 'is_premium' => $this->is_premium(),
12929
+ 'is_active' => true,
12930
+ 'is_uninstalled' => false,
12931
+ );
12932
+
12933
+ if ( true === $network_level_or_blog_id ) {
12934
+ if ( ! isset( $override_with['sites'] ) ) {
12935
+ $params['sites'] = array();
12936
+
12937
+ $sites = self::get_sites();
12938
+
12939
+ foreach ( $sites as $site ) {
12940
+ $blog_id = self::get_site_blog_id( $site );
12941
+ if ( ! $this->is_site_delegated_connection( $blog_id ) &&
12942
+ ! $this->is_installed_on_site( $blog_id )
12943
+ ) {
12944
+ $params['sites'][] = $this->get_site_info( $site );
12945
+ }
12946
+ }
12947
+ }
12948
+ } else {
12949
+ $site = is_numeric( $network_level_or_blog_id ) ?
12950
+ array( 'blog_id' => $network_level_or_blog_id ) :
12951
+ null;
12952
+
12953
+ $site = $this->get_site_info( $site );
12954
+
12955
+ $params = array_merge( $params, array(
12956
+ 'site_uid' => $site['uid'],
12957
+ 'site_url' => $site['url'],
12958
+ 'site_name' => $site['title'],
12959
+ 'language' => $site['language'],
12960
+ 'charset' => $site['charset'],
12961
+ ) );
12962
+ }
12963
+
12964
+ if ( $this->is_pending_activation() &&
12965
+ ! empty( $this->_storage->pending_license_key )
12966
+ ) {
12967
+ $params['license_key'] = $this->_storage->pending_license_key;
12968
+ }
12969
+
12970
+ if ( WP_FS__SKIP_EMAIL_ACTIVATION && $this->has_secret_key() ) {
12971
+ // Even though rand() is known for its security issues,
12972
+ // the timestamp adds another layer of protection.
12973
+ // It would be very hard for an attacker to get the secret key form here.
12974
+ // Plus, this should never run in production since the secret should never
12975
+ // be included in the production version.
12976
+ $params['ts'] = WP_FS__SCRIPT_START_TIME;
12977
+ $params['salt'] = md5( uniqid( rand() ) );
12978
+ $params['secure'] = md5(
12979
+ $params['ts'] .
12980
+ $params['salt'] .
12981
+ $this->get_secret_key()
12982
+ );
12983
+ }
12984
+
12985
+ return array_merge( $params, $override_with );
12986
+ }
12987
+
12988
+ /**
12989
+ * 1. If successful opt-in or pending activation returns the next page that the user should be redirected to.
12990
+ * 2. If there was an API error, return the API result.
12991
+ *
12992
+ * @author Vova Feldman (@svovaf)
12993
+ * @since 1.1.7.4
12994
+ *
12995
+ * @param string|bool $email
12996
+ * @param string|bool $first
12997
+ * @param string|bool $last
12998
+ * @param string|bool $license_key
12999
+ * @param bool $is_uninstall If "true", this means that the module is currently being uninstalled.
13000
+ * In this case, the user and site info will be sent to the server but no
13001
+ * data will be saved to the WP installation's database.
13002
+ * @param number|bool $trial_plan_id
13003
+ * @param bool $is_disconnected Whether or not to opt in without tracking.
13004
+ * @param array $sites If network-level opt-in, an array of containing details of sites.
13005
+ *
13006
+ * @return string|object
13007
+ * @use WP_Error
13008
+ */
13009
+ function opt_in(
13010
+ $email = false,
13011
+ $first = false,
13012
+ $last = false,
13013
+ $license_key = false,
13014
+ $is_uninstall = false,
13015
+ $trial_plan_id = false,
13016
+ $is_disconnected = false,
13017
+ $sites = array()
13018
+ ) {
13019
+ $this->_logger->entrance();
13020
+
13021
+ if ( false === $email ) {
13022
+ $current_user = self::_get_current_wp_user();
13023
+ $email = $current_user->user_email;
13024
+ }
13025
+
13026
+ /**
13027
+ * @since 1.2.1 If activating with license key, ignore the context-user
13028
+ * since the user will be automatically loaded from the license.
13029
+ */
13030
+ if ( empty( $license_key ) ) {
13031
+ // Clean up pending license if opt-ing in again.
13032
+ $this->_storage->remove( 'pending_license_key' );
13033
+
13034
+ if ( ! $is_uninstall ) {
13035
+ $fs_user = Freemius::_get_user_by_email( $email );
13036
+ if ( is_object( $fs_user ) && ! $this->is_pending_activation() ) {
13037
+ return $this->install_with_current_user(
13038
+ false,
13039
+ $trial_plan_id,
13040
+ $sites
13041
+ );
13042
+ }
13043
+ }
13044
+ }
13045
+
13046
+ $user_info = array();
13047
+ if ( ! empty( $email ) ) {
13048
+ $user_info['user_email'] = $email;
13049
+ }
13050
+ if ( ! empty( $first ) ) {
13051
+ $user_info['user_firstname'] = $first;
13052
+ }
13053
+ if ( ! empty( $last ) ) {
13054
+ $user_info['user_lastname'] = $last;
13055
+ }
13056
+
13057
+ if ( ! empty( $sites ) ) {
13058
+ $is_network = true;
13059
+
13060
+ $user_info['sites'] = $sites;
13061
+ } else {
13062
+ $is_network = false;
13063
+ }
13064
+
13065
+ $params = $this->get_opt_in_params( $user_info, $is_network );
13066
+
13067
+ $filtered_license_key = false;
13068
+ if ( is_string( $license_key ) ) {
13069
+ $filtered_license_key = $this->apply_filters( 'license_key', $license_key );
13070
+ $params['license_key'] = $filtered_license_key;
13071
+ } else if ( FS_Plugin_Plan::is_valid_id( $trial_plan_id ) ) {
13072
+ $params['trial_plan_id'] = $trial_plan_id;
13073
+ }
13074
+
13075
+ if ( $is_uninstall ) {
13076
+ $params['uninstall_params'] = array(
13077
+ 'reason_id' => $this->_storage->uninstall_reason->id,
13078
+ 'reason_info' => $this->_storage->uninstall_reason->info
13079
+ );
13080
+ }
13081
+
13082
+ if ( isset( $params['license_key'] ) ) {
13083
+ $fs_user = Freemius::_get_user_by_email( $email );
13084
+
13085
+ if ( is_object( $fs_user ) ) {
13086
+ /**
13087
+ * If opting in with a context license and the context WP Admin user already opted in
13088
+ * before from the current site, add the user context security params to avoid the
13089
+ * unnecessary email activation when the context license is owned by the same context user.
13090
+ *
13091
+ * @author Leo Fajardo (@leorw)
13092
+ * @since 1.2.3
13093
+ */
13094
+ $params = array_merge( $params, FS_Security::instance()->get_context_params(
13095
+ $fs_user,
13096
+ false,
13097
+ 'install_with_existing_user'
13098
+ ) );
13099
+ }
13100
+ }
13101
+
13102
+ $params['is_disconnected'] = $is_disconnected;
13103
+ $params['format'] = 'json';
13104
+
13105
+ $request = array(
13106
+ 'method' => 'POST',
13107
+ 'body' => $params,
13108
+ 'timeout' => WP_FS__DEBUG_SDK ? 60 : 30,
13109
+ );
13110
+
13111
+ $url = WP_FS__ADDRESS . '/action/service/user/install/';
13112
+
13113
+ if ( WP_FS__DEBUG_SDK || isset( $_COOKIE['XDEBUG_SESSION'] ) ) {
13114
+ $url = add_query_arg( 'XDEBUG_SESSION_START', rand( 0, 9999999 ), $url );
13115
+ $url = add_query_arg( 'XDEBUG_SESSION', 'PHPSTORM', $url );
13116
+
13117
+ $request['cookies'] = array(
13118
+ new WP_Http_Cookie( array(
13119
+ 'name' => 'XDEBUG_SESSION',
13120
+ 'value' => 'PHPSTORM',
13121
+ ) )
13122
+ );
13123
+ }
13124
+
13125
+ $response = wp_remote_post( $url, $request );
13126
+
13127
+ if ( $response instanceof WP_Error ) {
13128
+ if ( 'https://' === substr( $url, 0, 8 ) &&
13129
+ isset( $response->errors ) &&
13130
+ isset( $response->errors['http_request_failed'] )
13131
+ ) {
13132
+ $http_error = strtolower( $response->errors['http_request_failed'][0] );
13133
+
13134
+ if ( false !== strpos( $http_error, 'ssl' ) ||
13135
+ false !== strpos( $http_error, 'curl error 35' )
13136
+ ) {
13137
+ // Failed due to old version of cURL or Open SSL (SSLv3 is not supported by CloudFlare).
13138
+ $url = 'http://' . substr( $url, 8 );
13139
+
13140
+ $response = wp_remote_post( $url, array(
13141
+ 'method' => 'POST',
13142
+ 'body' => $params,
13143
+ 'timeout' => 15,
13144
+ ) );
13145
+ }
13146
+ }
13147
+ }
13148
+
13149
+ if ( is_wp_error( $response ) ) {
13150
+ /**
13151
+ * @var WP_Error $response
13152
+ */
13153
+ $result = new stdClass();
13154
+
13155
+ $error_code = $response->get_error_code();
13156
+ $error_type = str_replace( ' ', '', ucwords( str_replace( '_', ' ', $error_code ) ) );
13157
+
13158
+ $result->error = (object) array(
13159
+ 'type' => $error_type,
13160
+ 'message' => $response->get_error_message(),
13161
+ 'code' => $error_code,
13162
+ 'http' => 402
13163
+ );
13164
+
13165
+ return $result;
13166
+ }
13167
+
13168
+ // Module is being uninstalled, don't handle the returned data.
13169
+ if ( $is_uninstall ) {
13170
+ return true;
13171
+ }
13172
+
13173
+ /**
13174
+ * 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.
13175
+ *
13176
+ * @author Vova Feldman (@svovaf)
13177
+ * @since 1.2.3
13178
+ * @link https://themes.trac.wordpress.org/ticket/46134#comment:5
13179
+ * @link https://themes.trac.wordpress.org/ticket/46134#comment:9
13180
+ * @link https://themes.trac.wordpress.org/ticket/46134#comment:12
13181
+ * @link https://themes.trac.wordpress.org/ticket/46134#comment:14
13182
+ */
13183
+ $decoded = is_string( $response['body'] ) ?
13184
+ json_decode( $response['body'] ) :
13185
+ null;
13186
+
13187
+ if ( empty( $decoded ) ) {
13188
+ return false;
13189
+ }
13190
+
13191
+ if ( ! $this->is_api_result_object( $decoded ) ) {
13192
+ if ( ! empty( $params['license_key'] ) ) {
13193
+ // Pass the fully entered license key to the failure handler.
13194
+ $params['license_key'] = $license_key;
13195
+ }
13196
+
13197
+ return $is_uninstall ?
13198
+ $decoded :
13199
+ $this->apply_filters( 'after_install_failure', $decoded, $params );
13200
+ } else if ( isset( $decoded->pending_activation ) && $decoded->pending_activation ) {
13201
+ if ( $is_network ) {
13202
+ $site_ids = array();
13203
+ foreach ( $sites as $site ) {
13204
+ $site_ids[] = $site['blog_id'];
13205
+ }
13206
+
13207
+ /**
13208
+ * Store the sites so that they can be installed once the user has clicked on the activation link
13209
+ * in the email.
13210
+ *
13211
+ * @author Leo Fajardo (@leorw)
13212
+ */
13213
+ $this->_storage->pending_sites_info = array(
13214
+ 'blog_ids' => $site_ids,
13215
+ 'license_key' => $license_key,
13216
+ 'trial_plan_id' => $trial_plan_id
13217
+ );
13218
+ }
13219
+
13220
+ // Pending activation, add message.
13221
+ return $this->set_pending_confirmation(
13222
+ ( isset( $decoded->email ) ?
13223
+ $decoded->email :
13224
+ true ),
13225
+ false,
13226
+ $filtered_license_key,
13227
+ ! empty( $params['trial_plan_id'] )
13228
+ );
13229
+ } else if ( isset( $decoded->install_secret_key ) ) {
13230
+ return $this->install_with_new_user(
13231
+ $decoded->user_id,
13232
+ $decoded->user_public_key,
13233
+ $decoded->user_secret_key,
13234
+ $decoded->install_id,
13235
+ $decoded->install_public_key,
13236
+ $decoded->install_secret_key,
13237
+ false
13238
+ );
13239
+ } else if ( is_array( $decoded->installs ) ) {
13240
+ return $this->install_many_with_new_user(
13241
+ $decoded->user_id,
13242
+ $decoded->user_public_key,
13243
+ $decoded->user_secret_key,
13244
+ $decoded->installs,
13245
+ false
13246
+ );
13247
+ }
13248
+
13249
+ return $decoded;
13250
+ }
13251
+
13252
+ /**
13253
+ * Set user and site identities.
13254
+ *
13255
+ * @author Vova Feldman (@svovaf)
13256
+ * @since 1.0.9
13257
+ *
13258
+ * @param FS_User $user
13259
+ * @param FS_Site $site
13260
+ * @param bool $redirect
13261
+ * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will
13262
+ * redirect (or return a URL) to the account page with a special parameter to
13263
+ * trigger the auto installation processes.
13264
+ *
13265
+ * @return string If redirect is `false`, returns the next page the user should be redirected to.
13266
+ */
13267
+ function setup_account(
13268
+ FS_User $user,
13269
+ FS_Site $site,
13270
+ $redirect = true,
13271
+ $auto_install = false
13272
+ ) {
13273
+ return $this->setup_network_account(
13274
+ $user,
13275
+ array( $site ),
13276
+ $redirect,
13277
+ $auto_install,
13278
+ false
13279
+ );
13280
+ }
13281
+
13282
+ /**
13283
+ * Set user and site identities.
13284
+ *
13285
+ * @author Vova Feldman (@svovaf)
13286
+ * @since 2.0.0
13287
+ *
13288
+ * @param FS_User $user
13289
+ * @param FS_Site[] $installs
13290
+ * @param bool $redirect
13291
+ * @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.
13292
+ * @param bool $is_network_level_opt_in
13293
+ *
13294
+ * @return string If redirect is `false`, returns the next page the user should be redirected to.
13295
+ */
13296
+ function setup_network_account(
13297
+ FS_User $user,
13298
+ array $installs,
13299
+ $redirect = true,
13300
+ $auto_install = false,
13301
+ $is_network_level_opt_in = true
13302
+ ) {
13303
+ $first_install = $installs[0];
13304
+
13305
+ $this->_user = $user;
13306
+ $this->_site = $first_install;
13307
+
13308
+ $this->_sync_plans();
13309
+
13310
+ if ( 1 < count( $installs ) ) {
13311
+ // Only network level opt-in can have more than one install.
13312
+ $is_network_level_opt_in = true;
13313
+ }
13314
+ // $is_network_level_opt_in = self::is_ajax_action_static( 'network_activate', $this->_module_id );
13315
+ // If Freemius was OFF before, turn it on.
13316
+ $this->turn_on();
13317
+
13318
+ if ( ! $this->_is_network_active || ! $is_network_level_opt_in ) {
13319
+ $this->_set_account( $user, $first_install );
13320
+
13321
+ $this->do_action( 'after_account_connection', $user, $first_install );
13322
+ } else {
13323
+ // Map site addresses to their blog IDs.
13324
+ $address_to_blog_map = $this->get_address_to_blog_map();
13325
+
13326
+ $first_blog_id = null;
13327
+ $blog_2_install_map = array();
13328
+ foreach ( $installs as $install ) {
13329
+ $address = trailingslashit( fs_strip_url_protocol( $install->url ) );
13330
+ $blog_id = $address_to_blog_map[ $address ];
13331
+
13332
+ $this->_store_site( true, $blog_id, $install );
13333
+
13334
+ if ( is_null( $first_blog_id ) ) {
13335
+ $first_blog_id = $blog_id;
13336
+ }
13337
+
13338
+ $blog_2_install_map[ $blog_id ] = $install;
13339
+ }
13340
+
13341
+ if ( ! FS_User::is_valid_id( $this->_storage->network_user_id ) ||
13342
+ ! is_object( self::_get_user_by_id( $this->_storage->network_user_id ) )
13343
+ ) {
13344
+ // Store network user.
13345
+ $this->_storage->network_user_id = $this->_user->id;
13346
+ }
13347
+
13348
+ if ( ! FS_Site::is_valid_id( $this->_storage->network_install_blog_id ) ) {
13349
+ $this->_storage->network_install_blog_id = $first_blog_id;
13350
+ }
13351
+
13352
+ if ( count( $installs ) === count( $address_to_blog_map ) ) {
13353
+ // Super-admin opted-in for all sites in the network.
13354
+ $this->_storage->is_network_connected = true;
13355
+ }
13356
+
13357
+ $this->_store_licenses( false );
13358
+
13359
+ self::$_accounts->store();
13360
+
13361
+ // Don't sync the installs data on network upgrade
13362
+ if ( ! $this->network_upgrade_mode_completed() ) {
13363
+ $this->send_installs_update();
13364
+ }
13365
+
13366
+ // Switch install context back to the first install.
13367
+ $this->_site = $first_install;
13368
+
13369
+ $current_blog = get_current_blog_id();
13370
+
13371
+ foreach ( $blog_2_install_map as $blog_id => $install ) {
13372
+ $this->switch_to_blog( $blog_id );
13373
+
13374
+ $this->do_action( 'after_account_connection', $user, $install );
13375
+ }
13376
+
13377
+ $this->switch_to_blog( $current_blog );
13378
+
13379
+ $this->do_action( 'after_network_account_connection', $user, $blog_2_install_map );
13380
+ }
13381
+
13382
+ if ( is_numeric( $first_install->license_id ) ) {
13383
+ $this->_license = $this->_get_license_by_id( $first_install->license_id );
13384
+ }
13385
+
13386
+ $this->_admin_notices->remove_sticky( 'connect_account' );
13387
+
13388
+ if ( $this->is_pending_activation() || ! $this->has_settings_menu() ) {
13389
+ // Remove pending activation sticky notice (if still exist).
13390
+ $this->_admin_notices->remove_sticky( 'activation_pending' );
13391
+
13392
+ // Remove plugin from pending activation mode.
13393
+ unset( $this->_storage->is_pending_activation );
13394
+
13395
+ if ( ! $this->is_paying_or_trial() ) {
13396
+ $this->_admin_notices->add_sticky(
13397
+ sprintf( $this->get_text_inline( '%s activation was successfully completed.', 'plugin-x-activation-message' ), '<b>' . $this->get_plugin_name() . '</b>' ),
13398
+ 'activation_complete'
13399
+ );
13400
+ }
13401
+ }
13402
+
13403
+ if ( $this->is_paying_or_trial() ) {
13404
+ if ( ! $this->is_premium() ||
13405
+ ! $this->has_premium_version() ||
13406
+ ! $this->has_settings_menu()
13407
+ ) {
13408
+ if ( $this->is_paying() ) {
13409
+ $this->_admin_notices->add_sticky(
13410
+ sprintf(
13411
+ $this->get_text_inline( 'Your account was successfully activated with the %s plan.', 'activation-with-plan-x-message' ),
13412
+ $this->get_plan_title()
13413
+ ) . $this->get_complete_upgrade_instructions(),
13414
+ 'plan_upgraded',
13415
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
13416
+ );
13417
+ } else {
13418
+ $trial_plan = $this->get_trial_plan();
13419
+
13420
+ $this->_admin_notices->add_sticky(
13421
+ sprintf(
13422
+ $this->get_text_inline( 'Your trial has been successfully started.', 'trial-started-message' ),
13423
+ '<i>' . $this->get_plugin_name() . '</i>'
13424
+ ) . $this->get_complete_upgrade_instructions( $trial_plan->title ),
13425
+ 'trial_started',
13426
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
13427
+ );
13428
+ }
13429
+ }
13430
+
13431
+ $this->_admin_notices->remove_sticky( array(
13432
+ 'trial_promotion',
13433
+ ) );
13434
+ }
13435
+
13436
+ $plugin_id = fs_request_get( 'plugin_id', false );
13437
+
13438
+ // Store activation time ONLY for plugins & themes (not add-ons).
13439
+ if ( ! is_numeric( $plugin_id ) || ( $plugin_id == $this->_plugin->id ) ) {
13440
+ if ( empty( $this->_storage->activation_timestamp ) ) {
13441
+ $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME;
13442
+ }
13443
+ }
13444
+
13445
+ $next_page = '';
13446
+
13447
+ $extra = array();
13448
+ if ( $auto_install ) {
13449
+ $extra['auto_install'] = 'true';
13450
+ }
13451
+
13452
+ if ( is_numeric( $plugin_id ) ) {
13453
+ /**
13454
+ * @author Leo Fajardo (@leorw)
13455
+ * @since 1.2.1.6
13456
+ *
13457
+ * Also sync the license after an anonymous user subscribes.
13458
+ */
13459
+ if ( $this->is_anonymous() || $plugin_id != $this->_plugin->id ) {
13460
+ // Add-on was installed - sync license right after install.
13461
+ $next_page = $this->_get_sync_license_url( $plugin_id, true, $extra );
13462
+ }
13463
+ } else {
13464
+ /**
13465
+ * @author Vova Feldman (@svovaf)
13466
+ * @since 1.1.9 If site installed with a valid license, sync license.
13467
+ */
13468
+ if ( $this->is_paying() ) {
13469
+ $this->_sync_plugin_license(
13470
+ true,
13471
+ // Installs data is already synced in the beginning of this method directly or via _set_account().
13472
+ false
13473
+ );
13474
+ }
13475
+
13476
+ // Reload the page with the keys.
13477
+ $next_page = $this->is_anonymous() ?
13478
+ // If user previously skipped, redirect to account page.
13479
+ $this->get_account_url( false, $extra ) :
13480
+ $this->get_after_activation_url( 'after_connect_url', array(), $is_network_level_opt_in );
13481
+ }
13482
+
13483
+ if ( ! empty( $next_page ) && $redirect ) {
13484
+ fs_redirect( $next_page );
13485
+ }
13486
+
13487
+ return $next_page;
13488
+ }
13489
+
13490
+ /**
13491
+ * Install plugin with new user information after approval.
13492
+ *
13493
+ * @author Vova Feldman (@svovaf)
13494
+ * @since 1.0.7
13495
+ */
13496
+ function _install_with_new_user() {
13497
+ $this->_logger->entrance();
13498
+
13499
+ if ( $this->is_registered() ) {
13500
+ return;
13501
+ }
13502
+
13503
+ if ( ( $this->is_plugin() && fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) ||
13504
+ // @todo This logic should be improved because it's executed on every load of a theme.
13505
+ $this->is_theme()
13506
+ ) {
13507
+ // check_admin_referer( $this->_slug . '_activate_new' );
13508
+
13509
+ if ( fs_request_has( 'user_secret_key' ) ) {
13510
+ if ( fs_is_network_admin() && isset( $this->_storage->pending_sites_info ) ) {
13511
+ $pending_sites_info = $this->_storage->pending_sites_info;
13512
+
13513
+ $this->install_many_pending_with_user(
13514
+ fs_request_get( 'user_id' ),
13515
+ fs_request_get( 'user_public_key' ),
13516
+ fs_request_get( 'user_secret_key' ),
13517
+ $pending_sites_info['blog_ids'],
13518
+ $pending_sites_info['license_key'],
13519
+ $pending_sites_info['trial_plan_id']
13520
+ );
13521
+ } else {
13522
+ $this->install_with_new_user(
13523
+ fs_request_get( 'user_id' ),
13524
+ fs_request_get( 'user_public_key' ),
13525
+ fs_request_get( 'user_secret_key' ),
13526
+ fs_request_get( 'install_id' ),
13527
+ fs_request_get( 'install_public_key' ),
13528
+ fs_request_get( 'install_secret_key' ),
13529
+ true,
13530
+ fs_request_get_bool( 'auto_install' )
13531
+ );
13532
+ }
13533
+ } else if ( fs_request_has( 'pending_activation' ) ) {
13534
+ $this->set_pending_confirmation( fs_request_get( 'user_email' ), true );
13535
+ }
13536
+ }
13537
+ }
13538
+
13539
+ /**
13540
+ * @author Vova Feldman (@svovaf)
13541
+ * @since 2.0.0
13542
+ *
13543
+ * @param number $id
13544
+ * @param string $public_key
13545
+ * @param string $secret_key
13546
+ *
13547
+ * @return \FS_User
13548
+ */
13549
+ private function setup_user( $id, $public_key, $secret_key ) {
13550
+ $user = self::_get_user_by_id( $id );
13551
+
13552
+ if ( is_object( $user ) ) {
13553
+ $this->_user = $user;
13554
+ } else {
13555
+ $user = new FS_User();
13556
+ $user->id = $id;
13557
+ $user->public_key = $public_key;
13558
+ $user->secret_key = $secret_key;
13559
+
13560
+ $this->_user = $user;
13561
+ $user_result = $this->get_api_user_scope()->get();
13562
+ $user = new FS_User( $user_result );
13563
+
13564
+ $this->_user = $user;
13565
+ $this->_store_user();
13566
+ }
13567
+
13568
+ return $user;
13569
+ }
13570
+
13571
+ /**
13572
+ * Install plugin with new user.
13573
+ *
13574
+ * @author Vova Feldman (@svovaf)
13575
+ * @since 1.1.7.4
13576
+ *
13577
+ * @param number $user_id
13578
+ * @param string $user_public_key
13579
+ * @param string $user_secret_key
13580
+ * @param number $install_id
13581
+ * @param string $install_public_key
13582
+ * @param string $install_secret_key
13583
+ * @param bool $redirect
13584
+ * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will
13585
+ * redirect (or return a URL) to the account page with a special parameter to
13586
+ * trigger the auto installation processes.
13587
+ *
13588
+ * @return string If redirect is `false`, returns the next page the user should be redirected to.
13589
+ */
13590
+ private function install_with_new_user(
13591
+ $user_id,
13592
+ $user_public_key,
13593
+ $user_secret_key,
13594
+ $install_id,
13595
+ $install_public_key,
13596
+ $install_secret_key,
13597
+ $redirect = true,
13598
+ $auto_install = false
13599
+ ) {
13600
+ /**
13601
+ * This method is also executed after opting in with a license key since the
13602
+ * license can be potentially associated with a different owner.
13603
+ *
13604
+ * @since 2.0.0
13605
+ */
13606
+ $user = self::_get_user_by_id( $user_id );
13607
+
13608
+ if ( ! is_object( $user ) ) {
13609
+ $user = new FS_User();
13610
+ $user->id = $user_id;
13611
+ $user->public_key = $user_public_key;
13612
+ $user->secret_key = $user_secret_key;
13613
+
13614
+ $this->_user = $user;
13615
+ $user_result = $this->get_api_user_scope()->get();
13616
+ $user = new FS_User( $user_result );
13617
+ }
13618
+
13619
+ $this->_user = $user;
13620
+
13621
+ $site = new FS_Site();
13622
+ $site->id = $install_id;
13623
+ $site->public_key = $install_public_key;
13624
+ $site->secret_key = $install_secret_key;
13625
+
13626
+ $this->_site = $site;
13627
+ $site_result = $this->get_api_site_scope()->get();
13628
+ $site = new FS_Site( $site_result );
13629
+ $this->_site = $site;
13630
+
13631
+ return $this->setup_account(
13632
+ $this->_user,
13633
+ $this->_site,