Contact Form 7 Multi-Step Forms - Version 3.0.7

Version Description

updated freemius

Download this release

Release Info

Developer webheadllc
Plugin Icon 128x128 Contact Form 7 Multi-Step Forms
Version 3.0.7
Comparing to
See all releases

Code changes from version 3.0.4 to 3.0.7

cf7msm.php CHANGED
@@ -123,7 +123,6 @@ function cf7msm_scripts()
123
  wp_enqueue_script(
124
  'cf7msm',
125
  plugins_url( '/resources/cf7msm.min.js', CF7MSM_PLUGIN ),
126
- // plugins_url('/js_src/cf7msm.js', CF7MSM_PLUGIN),
127
  array( 'jquery' ),
128
  CF7MSM_VERSION,
129
  true
123
  wp_enqueue_script(
124
  'cf7msm',
125
  plugins_url( '/resources/cf7msm.min.js', CF7MSM_PLUGIN ),
 
126
  array( 'jquery' ),
127
  CF7MSM_VERSION,
128
  true
contact-form-7-multi-step-module.php CHANGED
@@ -6,7 +6,7 @@ Plugin URI: http://www.mymonkeydo.com/contact-form-7-multi-step-module/
6
  Description: Enables the Contact Form 7 plugin to create multi-page, multi-step forms.
7
  Author: Webhead LLC.
8
  Author URI: http://webheadcoder.com
9
- Version: 3.0.4
10
  Text Domain: contact-form-7-multi-step-module
11
  */
12
  /* Copyright 2018 Webhead LLC (email: info at webheadcoder.com)
@@ -62,7 +62,7 @@ if ( !function_exists( 'cf7msm_fs' ) ) {
62
  cf7msm_fs();
63
  // Signal that SDK was initiated.
64
  do_action( 'cf7msm_fs_loaded' );
65
- define( 'CF7MSM_VERSION', '3.0.4' );
66
  define( 'CF7MSM_PLUGIN', __FILE__ );
67
  define( 'CF7MSM_FREE_TEXT_PREFIX_RADIO', '_wpcf7_radio_free_text_' );
68
  define( 'CF7MSM_FREE_TEXT_PREFIX_CHECKBOX', '_wpcf7_checkbox_free_text_' );
6
  Description: Enables the Contact Form 7 plugin to create multi-page, multi-step forms.
7
  Author: Webhead LLC.
8
  Author URI: http://webheadcoder.com
9
+ Version: 3.0.7
10
  Text Domain: contact-form-7-multi-step-module
11
  */
12
  /* Copyright 2018 Webhead LLC (email: info at webheadcoder.com)
62
  cf7msm_fs();
63
  // Signal that SDK was initiated.
64
  do_action( 'cf7msm_fs_loaded' );
65
+ define( 'CF7MSM_VERSION', '3.0.7' );
66
  define( 'CF7MSM_PLUGIN', __FILE__ );
67
  define( 'CF7MSM_FREE_TEXT_PREFIX_RADIO', '_wpcf7_radio_free_text_' );
68
  define( 'CF7MSM_FREE_TEXT_PREFIX_CHECKBOX', '_wpcf7_checkbox_free_text_' );
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%}
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}
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}#fs_marketing_optin{display:none;margin-top:10px;border:1px solid #ccc;padding:10px;line-height:1.5em}#fs_marketing_optin .fs-message{display:block;margin-bottom:5px;font-size:1.05em;font-weight:600}#fs_marketing_optin.error{border:1px solid #d3135a;background:#fee}#fs_marketing_optin.error .fs-message{color:#d3135a}#fs_marketing_optin .fs-input-container{margin-top:5px}#fs_marketing_optin .fs-input-container label{margin-top:5px;display:block}#fs_marketing_optin .fs-input-container label input{float:left;margin:1px 0 0 0}#fs_marketing_optin .fs-input-container label:first-child{display:block;margin-bottom:2px}#fs_marketing_optin .fs-input-label{display:block;margin-left:20px}#fs_marketing_optin .fs-input-label .underlined{text-decoration:underline}.rtl #fs_marketing_optin .fs-input-container label input{float:right}.rtl #fs_marketing_optin .fs-input-label{margin-left:0;margin-right:20px}.rtl #fs_connect .fs-actions{padding:10px 20px;background:#C0C7CA}.rtl #fs_connect .fs-actions .button .dashicons{font-size:37px;margin-left:-8px;margin-right:12px}.rtl #fs_connect .fs-actions .button.button-primary:after{content:' \000bb'}.rtl #fs_connect .fs-actions .button.button-primary.fs-loading:after{content:''}.rtl #fs_connect .fs-actions .button.button-secondary{float:left}.rtl #fs_connect .fs-permissions ul li div{margin-right:55px;margin-left:0}.rtl #fs_connect .fs-permissions ul li i.dashicons{float:right}.rtl #fs_connect .fs-visual .fs-site-icon{right:20px;left:auto}.rtl #fs_connect .fs-visual .fs-connect-logo{right:auto;left:20px}#fs_theme_connect_wrapper{position:fixed;top:0;height:100%;width:100%;z-index:99990;background:rgba(0,0,0,0.75);text-align:center;overflow-y:auto}#fs_theme_connect_wrapper:before{content:"";display:inline-block;vertical-align:middle;height:100%}#fs_theme_connect_wrapper>button.close{color:white;cursor:pointer;height:40px;width:40px;position:absolute;right:0;border:0;background-color:transparent;top:32px}#fs_theme_connect_wrapper #fs_connect{top:0;text-align:left;display:inline-block;vertical-align:middle;margin-top:52px;margin-bottom:20px}#fs_theme_connect_wrapper #fs_connect .fs-terms{background:rgba(140,140,140,0.64)}#fs_theme_connect_wrapper #fs_connect .fs-terms,#fs_theme_connect_wrapper #fs_connect .fs-terms a{color:#c5c5c5}.wp-pointer-content #fs_connect{margin:0;-moz-box-shadow:none;-webkit-box-shadow:none;box-shadow:none}.fs-opt-in-pointer .wp-pointer-content{padding:0}.fs-opt-in-pointer.wp-pointer-top .wp-pointer-arrow{border-bottom-color:#dfdfdf}.fs-opt-in-pointer.wp-pointer-top .wp-pointer-arrow-inner{border-bottom-color:#fafafa}.fs-opt-in-pointer.wp-pointer-bottom .wp-pointer-arrow{border-top-color:#dfdfdf}.fs-opt-in-pointer.wp-pointer-bottom .wp-pointer-arrow-inner{border-top-color:#fafafa}.fs-opt-in-pointer.wp-pointer-left .wp-pointer-arrow{border-right-color:#dfdfdf}.fs-opt-in-pointer.wp-pointer-left .wp-pointer-arrow-inner{border-right-color:#fafafa}.fs-opt-in-pointer.wp-pointer-right .wp-pointer-arrow{border-left-color:#dfdfdf}.fs-opt-in-pointer.wp-pointer-right .wp-pointer-arrow-inner{border-left-color:#fafafa}
freemius/assets/css/admin/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}
freemius/assets/css/admin/gdpr-optin-notice.css ADDED
@@ -0,0 +1 @@
 
1
+ .fs-notice[data-id^="gdpr_optin_actions"] .underlined{text-decoration:underline}.fs-notice[data-id^="gdpr_optin_actions"] ul .button,.fs-notice[data-id^="gdpr_optin_actions"] ul .action-description{vertical-align:middle}.fs-notice[data-id^="gdpr_optin_actions"] ul .action-description{display:inline-block;margin-left:3px}
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);
freemius/assets/scss/admin/_gdpr-consent.scss ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #fs_marketing_optin
2
+ {
3
+ display: none;
4
+ margin-top: 10px;
5
+ border: 1px solid #ccc;
6
+ padding: 10px;
7
+ line-height: 1.5em;
8
+
9
+ .fs-message
10
+ {
11
+ display: block;
12
+ margin-bottom: 5px;
13
+ font-size: 1.05em;
14
+ font-weight: 600;
15
+ }
16
+
17
+ &.error
18
+ {
19
+ border: 1px solid $fs-logo-magenta-color;
20
+ background: #fee;
21
+
22
+ .fs-message
23
+ {
24
+ color: $fs-logo-magenta-color;
25
+ }
26
+ }
27
+
28
+ .fs-input-container
29
+ {
30
+ margin-top: 5px;
31
+
32
+ label
33
+ {
34
+ margin-top: 5px;
35
+ display: block;
36
+
37
+ input
38
+ {
39
+ float: left;
40
+ margin: 1px 0 0 0;
41
+ }
42
+
43
+ &:first-child
44
+ {
45
+ display: block;
46
+ margin-bottom: 2px;
47
+ }
48
+ }
49
+ }
50
+
51
+ .fs-input-label
52
+ {
53
+ display: block;
54
+ margin-left: 20px;
55
+
56
+ .underlined
57
+ {
58
+ text-decoration: underline;
59
+ }
60
+ }
61
+ }
62
+
63
+ .rtl
64
+ {
65
+ #fs_marketing_optin
66
+ {
67
+ .fs-input-container
68
+ {
69
+ label input
70
+ {
71
+ float: right;
72
+ }
73
+ }
74
+
75
+ .fs-input-label
76
+ {
77
+ margin-left: 0;
78
+ margin-right: 20px;
79
+ }
80
+ }
81
+ }
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
  }
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
 
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
+ }
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
+ }
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
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,10 @@ $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
+ @import "gdpr-consent";
356
+
357
  .rtl
358
  {
359
  #fs_connect
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";
freemius/assets/scss/admin/gdpr-optin-notice.scss ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .fs-notice[data-id^="gdpr_optin_actions"]
2
+ {
3
+ .underlined {
4
+ text-decoration: underline;
5
+ }
6
+
7
+ ul {
8
+ .button, .action-description {
9
+ vertical-align: middle;
10
+ }
11
+
12
+ .action-description {
13
+ display: inline-block;
14
+ margin-left: 3px;
15
+ }
16
+ }
17
+ }
freemius/config.php CHANGED
@@ -1,328 +1,388 @@
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
+ if ( ! defined( 'WP_FS__GDPR_OPTION_NAME' ) ) {
252
+ define( 'WP_FS__GDPR_OPTION_NAME', WP_FS___OPTION_PREFIX . 'gdpr' );
253
+ }
254
+ define( 'WP_FS__OPTIONS_OPTION_NAME', WP_FS___OPTION_PREFIX . 'options' );
255
+
256
+ /**
257
+ * Module types
258
+ *
259
+ * @since 1.2.2
260
+ */
261
+ define( 'WP_FS__MODULE_TYPE_PLUGIN', 'plugin' );
262
+ define( 'WP_FS__MODULE_TYPE_THEME', 'theme' );
263
+
264
+ /**
265
+ * Billing Frequencies
266
+ */
267
+ define( 'WP_FS__PERIOD_ANNUALLY', 'annual' );
268
+ define( 'WP_FS__PERIOD_MONTHLY', 'monthly' );
269
+ define( 'WP_FS__PERIOD_LIFETIME', 'lifetime' );
270
+
271
+ /**
272
+ * Plans
273
+ */
274
+ define( 'WP_FS__PLAN_DEFAULT_PAID', false );
275
+ define( 'WP_FS__PLAN_FREE', 'free' );
276
+ define( 'WP_FS__PLAN_TRIAL', 'trial' );
277
+
278
+ /**
279
+ * Times in seconds
280
+ */
281
+ if ( ! defined( 'WP_FS__TIME_5_MIN_IN_SEC' ) ) {
282
+ define( 'WP_FS__TIME_5_MIN_IN_SEC', 300 );
283
+ }
284
+ if ( ! defined( 'WP_FS__TIME_10_MIN_IN_SEC' ) ) {
285
+ define( 'WP_FS__TIME_10_MIN_IN_SEC', 600 );
286
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287
  // define( 'WP_FS__TIME_15_MIN_IN_SEC', 900 );
288
+ if ( ! defined( 'WP_FS__TIME_12_HOURS_IN_SEC' ) ) {
289
+ define( 'WP_FS__TIME_12_HOURS_IN_SEC', 43200 );
290
+ }
291
+ if ( ! defined( 'WP_FS__TIME_24_HOURS_IN_SEC' ) ) {
292
+ define( 'WP_FS__TIME_24_HOURS_IN_SEC', WP_FS__TIME_12_HOURS_IN_SEC * 2 );
293
+ }
294
+ if ( ! defined( 'WP_FS__TIME_WEEK_IN_SEC' ) ) {
295
+ define( 'WP_FS__TIME_WEEK_IN_SEC', 7 * WP_FS__TIME_24_HOURS_IN_SEC );
296
+ }
297
+
298
+ #--------------------------------------------------------------------------------
299
+ #region Debugging
300
+ #--------------------------------------------------------------------------------
301
+
302
+ if ( ! defined( 'WP_FS__DEBUG_SDK' ) ) {
303
+ $debug_mode = get_option( 'fs_debug_mode', null );
304
+
305
+ if ( $debug_mode === null ) {
306
+ $debug_mode = false;
307
+ add_option( 'fs_debug_mode', $debug_mode );
308
+ }
309
+
310
+ define( 'WP_FS__DEBUG_SDK', is_numeric( $debug_mode ) ? ( 0 < $debug_mode ) : WP_FS__DEV_MODE );
311
+ }
312
+
313
+ if ( ! defined( 'WP_FS__ECHO_DEBUG_SDK' ) ) {
314
+ define( 'WP_FS__ECHO_DEBUG_SDK', WP_FS__DEV_MODE && ! empty( $_GET['fs_dbg_echo'] ) );
315
+ }
316
+ if ( ! defined( 'WP_FS__LOG_DATETIME_FORMAT' ) ) {
317
+ define( 'WP_FS__LOG_DATETIME_FORMAT', 'Y-m-d H:i:s' );
318
+ }
319
+ if ( ! defined( 'FS_API__LOGGER_ON' ) ) {
320
+ define( 'FS_API__LOGGER_ON', WP_FS__DEBUG_SDK );
321
+ }
322
+
323
+ if ( WP_FS__ECHO_DEBUG_SDK ) {
324
+ error_reporting( E_ALL );
325
+ }
326
+
327
+ #endregion
328
+
329
+ if ( ! defined( 'WP_FS__SCRIPT_START_TIME' ) ) {
330
+ define( 'WP_FS__SCRIPT_START_TIME', time() );
331
+ }
332
+ if ( ! defined( 'WP_FS__DEFAULT_PRIORITY' ) ) {
333
+ define( 'WP_FS__DEFAULT_PRIORITY', 10 );
334
+ }
335
+ if ( ! defined( 'WP_FS__LOWEST_PRIORITY' ) ) {
336
+ define( 'WP_FS__LOWEST_PRIORITY', 999999999 );
337
+ }
338
+
339
+ #--------------------------------------------------------------------------------
340
+ #region Multisite Network
341
+ #--------------------------------------------------------------------------------
342
+
343
+ /**
344
+ * Do not use this define directly, it will have the wrong value
345
+ * during plugin uninstall/deletion when the inclusion of the plugin
346
+ * is triggered due to registration with register_uninstall_hook().
347
+ *
348
+ * Instead, use fs_is_network_admin().
349
+ *
350
+ * @author Vova Feldman (@svovaf)
351
+ */
352
+ if ( ! defined( 'WP_FS__IS_NETWORK_ADMIN' ) ) {
353
+ define( 'WP_FS__IS_NETWORK_ADMIN',
354
+ is_network_admin() ||
355
+ ( is_multisite() &&
356
+ ( ( defined( 'DOING_AJAX' ) && DOING_AJAX &&
357
+ ( isset( $_REQUEST['_fs_network_admin'] ) /*||
358
+ ( ! empty( $_REQUEST['action'] ) && 'delete-plugin' === $_REQUEST['action'] )*/ )
359
+ ) ||
360
+ // Plugin uninstall.
361
+ defined( 'WP_UNINSTALL_PLUGIN' ) )
362
+ )
363
+ );
364
+ }
365
+
366
+ /**
367
+ * Do not use this define directly, it will have the wrong value
368
+ * during plugin uninstall/deletion when the inclusion of the plugin
369
+ * is triggered due to registration with register_uninstall_hook().
370
+ *
371
+ * Instead, use fs_is_blog_admin().
372
+ *
373
+ * @author Vova Feldman (@svovaf)
374
+ */
375
+ if ( ! defined( 'WP_FS__IS_BLOG_ADMIN' ) ) {
376
+ define( 'WP_FS__IS_BLOG_ADMIN', is_blog_admin() || ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_REQUEST['_fs_blog_admin'] ) ) );
377
+ }
378
+
379
+ if ( ! defined( 'WP_FS__SHOW_NETWORK_EVEN_WHEN_DELEGATED' ) ) {
380
+ // Set to true to show network level settings even if delegated to site admins.
381
+ define( 'WP_FS__SHOW_NETWORK_EVEN_WHEN_DELEGATED', false );
382
+ }
383
+
384
+ #endregion
385
+
386
+ if ( ! defined( 'WP_FS__DEMO_MODE' ) ) {
387
+ define( 'WP_FS__DEMO_MODE', false );
388
+ }
freemius/includes/class-freemius-abstract.php CHANGED
@@ -359,7 +359,7 @@
359
  #----------------------------------------------------------------------------------
360
 
361
  /**
362
- * Check if plugin using the free plan.
363
  *
364
  * @since 1.0.4
365
  *
359
  #----------------------------------------------------------------------------------
360
 
361
  /**
362
+ * Check if the user is on the free plan of the product.
363
  *
364
  * @since 1.0.4
365
  *
freemius/includes/class-freemius.php CHANGED
@@ -1,14305 +1,20822 @@
1
  <?php
2
- /**
3
- * @package Freemius
4
- * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
6
- * @since 1.0.3
7
- */
8
- if ( ! defined( 'ABSPATH' ) ) {
9
- exit;
10
- }
11
-
12
- // "final class"
13
- class Freemius extends Freemius_Abstract {
14
- /**
15
- * SDK Version
16
- *
17
- * @var string
18
- */
19
- public $version = WP_FS__SDK_VERSION;
20
-
21
- #region Plugin Info
22
-
23
- /**
24
- * @since 1.0.1
25
- *
26
- * @var string
27
- */
28
- private $_slug;
29
-
30
- /**
31
- * @since 1.0.0
32
- *
33
- * @var string
34
- */
35
- private $_plugin_basename;
36
- /**
37
- * @since 1.0.0
38
- *
39
- * @var string
40
- */
41
- private $_free_plugin_basename;
42
- /**
43
- * @since 1.0.0
44
- *
45
- * @var string
46
- */
47
- private $_plugin_dir_path;
48
- /**
49
- * @since 1.0.0
50
- *
51
- * @var string
52
- */
53
- private $_plugin_dir_name;
54
- /**
55
- * @since 1.0.0
56
- *
57
- * @var string
58
- */
59
- private $_plugin_main_file_path;
60
- /**
61
- * @var string[]
62
- */
63
- private $_plugin_data;
64
- /**
65
- * @since 1.0.9
66
- *
67
- * @var string
68
- */
69
- private $_plugin_name;
70
- /**
71
- * @since 1.2.2
72
- *
73
- * @var string
74
- */
75
- private $_module_type;
76
-
77
- #endregion Plugin Info
78
-
79
- /**
80
- * @since 1.0.9
81
- *
82
- * @var bool If false, don't turn Freemius on.
83
- */
84
- private $_is_on;
85
-
86
- /**
87
- * @since 1.1.3
88
- *
89
- * @var bool If false, don't turn Freemius on.
90
- */
91
- private $_is_anonymous;
92
-
93
- /**
94
- * @since 1.0.9
95
- * @var bool If false, issues with connectivity to Freemius API.
96
- */
97
- private $_has_api_connection;
98
-
99
- /**
100
- * @since 1.0.9
101
- * @var bool Hints the SDK if plugin can support anonymous mode (if skip connect is visible).
102
- */
103
- private $_enable_anonymous;
104
-
105
- /**
106
- * @since 1.1.7.5
107
- * @var bool Hints the SDK if plugin should run in anonymous mode (only adds feedback form).
108
- */
109
- private $_anonymous_mode;
110
-
111
- /**
112
- * @since 1.1.9
113
- * @var bool Hints the SDK if plugin have any free plans.
114
- */
115
- private $_is_premium_only;
116
-
117
- /**
118
- * @since 1.2.1.6
119
- * @var bool Hints the SDK if plugin have premium code version at all.
120
- */
121
- private $_has_premium_version;
122
-
123
- /**
124
- * @since 1.2.1.6
125
- * @var bool Hints the SDK if plugin should ignore pending mode by simulating a skip.
126
- */
127
- private $_ignore_pending_mode;
128
-
129
- /**
130
- * @since 1.0.8
131
- * @var bool Hints the SDK if the plugin has any paid plans.
132
- */
133
- private $_has_paid_plans;
134
-
135
- /**
136
- * @since 1.2.1.5
137
- * @var int Hints the SDK if the plugin offers a trial period. If negative, no trial, if zero - has a trial but
138
- * without a specified period, if positive - the number of trial days.
139
- */
140
- private $_trial_days = - 1;
141
-
142
- /**
143
- * @since 1.2.1.5
144
- * @var bool Hints the SDK if the trial requires a payment method or not.
145
- */
146
- private $_is_trial_require_payment = false;
147
-
148
- /**
149
- * @since 1.0.7
150
- * @var bool Hints the SDK if the plugin is WordPress.org compliant.
151
- */
152
- private $_is_org_compliant;
153
-
154
- /**
155
- * @since 1.0.7
156
- * @var bool Hints the SDK if the plugin is has add-ons.
157
- */
158
- private $_has_addons;
159
-
160
- /**
161
- * @since 1.1.6
162
- * @var string[]bool.
163
- */
164
- private $_permissions;
165
-
166
- /**
167
- * @var FS_Key_Value_Storage
168
- */
169
- private $_storage;
170
-
171
- /**
172
- * @since 1.2.2.7
173
- * @var FS_Cache_Manager
174
- */
175
- private $_cache;
176
-
177
- /**
178
- * @since 1.0.0
179
- *
180
- * @var FS_Logger
181
- */
182
- private $_logger;
183
- /**
184
- * @since 1.0.4
185
- *
186
- * @var FS_Plugin
187
- */
188
- private $_plugin = false;
189
- /**
190
- * @since 1.0.4
191
- *
192
- * @var FS_Plugin|false
193
- */
194
- private $_parent_plugin = false;
195
- /**
196
- * @since 1.1.1
197
- *
198
- * @var Freemius
199
- */
200
- private $_parent = false;
201
- /**
202
- * @since 1.0.1
203
- *
204
- * @var FS_User
205
- */
206
- private $_user = false;
207
- /**
208
- * @since 1.0.1
209
- *
210
- * @var FS_Site
211
- */
212
- private $_site = false;
213
- /**
214
- * @since 1.0.1
215
- *
216
- * @var FS_Plugin_License
217
- */
218
- private $_license;
219
- /**
220
- * @since 1.0.2
221
- *
222
- * @var FS_Plugin_Plan[]
223
- */
224
- private $_plans = false;
225
- /**
226
- * @var FS_Plugin_License[]
227
- * @since 1.0.5
228
- */
229
- private $_licenses = false;
230
-
231
- /**
232
- * @since 1.0.1
233
- *
234
- * @var FS_Admin_Menu_Manager
235
- */
236
- private $_menu;
237
-
238
- /**
239
- * @var FS_Admin_Notice_Manager
240
- */
241
- private $_admin_notices;
242
-
243
- /**
244
- * @since 1.1.6
245
- *
246
- * @var FS_Admin_Notice_Manager
247
- */
248
- private static $_global_admin_notices;
249
-
250
- /**
251
- * @var FS_Logger
252
- * @since 1.0.0
253
- */
254
- private static $_static_logger;
255
-
256
- /**
257
- * @var FS_Option_Manager
258
- * @since 1.0.2
259
- */
260
- private static $_accounts;
261
-
262
- /**
263
- * @since 1.2.2
264
- *
265
- * @var number
266
- */
267
- private $_module_id;
268
-
269
- /**
270
- * @var Freemius[]
271
- */
272
- private static $_instances = array();
273
-
274
  /**
275
- * @author Leo Fajardo (@leorw)
276
  *
277
- * @since 1.2.3
 
 
 
 
 
 
 
278
  *
279
- * @var FS_Affiliate
280
  */
281
- private $affiliate = null;
282
 
283
  /**
284
- * @author Leo Fajardo (@leorw)
285
  *
286
- * @since 1.2.3
 
 
 
 
287
  *
288
- * @var FS_AffiliateTerms
289
  */
290
- private $plugin_affiliate_terms = null;
291
-
292
  /**
293
- * @author Leo Fajardo (@leorw)
 
 
 
 
 
 
294
  *
295
- * @since 1.2.3
 
 
 
 
296
  *
297
- * @var FS_AffiliateTerms
298
  */
299
- private $custom_affiliate_terms = null;
300
-
301
- #region Uninstall Reasons IDs
302
-
303
- const REASON_NO_LONGER_NEEDED = 1;
304
- const REASON_FOUND_A_BETTER_PLUGIN = 2;
305
- const REASON_NEEDED_FOR_A_SHORT_PERIOD = 3;
306
- const REASON_BROKE_MY_SITE = 4;
307
- const REASON_SUDDENLY_STOPPED_WORKING = 5;
308
- const REASON_CANT_PAY_ANYMORE = 6;
309
- const REASON_OTHER = 7;
310
- const REASON_DIDNT_WORK = 8;
311
- const REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION = 9;
312
- const REASON_COULDNT_MAKE_IT_WORK = 10;
313
- const REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE = 11;
314
- const REASON_NOT_WORKING = 12;
315
- const REASON_NOT_WHAT_I_WAS_LOOKING_FOR = 13;
316
- const REASON_DIDNT_WORK_AS_EXPECTED = 14;
317
- const REASON_TEMPORARY_DEACTIVATION = 15;
318
-
319
- #endregion
320
-
321
- /* Ctor
322
- ------------------------------------------------------------------------------------------------------------------*/
323
 
324
- /**
325
- * Main singleton instance.
326
- *
327
- * @author Vova Feldman (@svovaf)
328
- * @since 1.0.0
329
- *
330
- * @param number $module_id
331
- * @param string|bool $slug
332
- * @param bool $is_init Since 1.2.1 Is initiation sequence.
333
- */
334
- private function __construct( $module_id, $slug = false, $is_init = false ) {
335
- if ( $is_init && is_numeric( $module_id ) && is_string( $slug ) ) {
336
- $this->store_id_slug_type_path_map( $module_id, $slug );
337
- }
338
-
339
- $this->_module_id = $module_id;
340
- $this->_slug = $this->get_slug();
341
- $this->_module_type = $this->get_module_type();
342
-
343
- $this->_storage = FS_Key_Value_Storage::instance( $this->_module_type . '_data', $this->_slug );
344
- $this->_cache = FS_Cache_Manager::get_manager( WP_FS___OPTION_PREFIX . "cache_{$module_id}" );
345
-
346
- $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $this->get_unique_affix(), WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
347
-
348
- $this->_plugin_main_file_path = $this->_find_caller_plugin_file( $is_init );
349
- $this->_plugin_dir_path = plugin_dir_path( $this->_plugin_main_file_path );
350
- $this->_plugin_basename = $this->get_plugin_basename();
351
- $this->_free_plugin_basename = str_replace( '-premium/', '/', $this->_plugin_basename );
352
-
353
- $base_name_split = explode( '/', $this->_plugin_basename );
354
- $this->_plugin_dir_name = $base_name_split[0];
355
-
356
- if ( $this->_logger->is_on() ) {
357
- $this->_logger->info( 'plugin_main_file_path = ' . $this->_plugin_main_file_path );
358
- $this->_logger->info( 'plugin_dir_path = ' . $this->_plugin_dir_path );
359
- $this->_logger->info( 'plugin_basename = ' . $this->_plugin_basename );
360
- $this->_logger->info( 'free_plugin_basename = ' . $this->_free_plugin_basename );
361
- $this->_logger->info( 'plugin_dir_name = ' . $this->_plugin_dir_name );
362
- }
363
-
364
- // Remember link between file to slug.
365
- $this->store_file_slug_map();
366
-
367
- // Store plugin's initial install timestamp.
368
- if ( ! isset( $this->_storage->install_timestamp ) ) {
369
- $this->_storage->install_timestamp = WP_FS__SCRIPT_START_TIME;
370
- }
371
-
372
- if ( ! is_object( $this->_plugin ) ) {
373
- $this->_plugin = FS_Plugin_Manager::instance( $this->_module_id )->get();
374
- }
375
-
376
- $this->_admin_notices = FS_Admin_Notice_Manager::instance(
377
- $this->_slug . ( $this->is_theme() ? ':theme' : '' ),
378
- /**
379
- * Ensure that the admin notice will always have a title by using the stored plugin title if available and
380
- * retrieving the title via the "get_plugin_name" method if there is no stored plugin title available.
381
- *
382
- * @author Leo Fajardo (@leorw)
383
- * @since 1.2.2
384
- */
385
- ( is_object( $this->_plugin ) ? $this->_plugin->title : $this->get_plugin_name() ),
386
- $this->get_unique_affix()
387
- );
388
-
389
- if ( 'true' === fs_request_get( 'fs_clear_api_cache' ) ||
390
- 'true' === fs_request_is_action( 'restart_freemius' )
391
- ) {
392
- FS_Api::clear_cache();
393
- $this->_cache->clear();
394
- }
395
-
396
- $this->_register_hooks();
397
-
398
- $this->_load_account();
399
-
400
- $this->_version_updates_handler();
401
- }
402
-
403
- /**
404
- * Checks whether this module has a settings menu.
405
- *
406
- * @author Leo Fajardo (@leorw)
407
- * @since 1.2.2
408
- *
409
- * @return bool
410
- */
411
- function has_settings_menu() {
412
- return $this->_menu->has_menu();
413
- }
414
-
415
- /**
416
- * Check if the context module is free wp.org theme.
417
- *
418
- * This method is helpful because:
419
- * 1. wp.org themes are limited to a single submenu item,
420
- * and sub-submenu items are most likely not allowed (never verified).
421
- * 2. wp.org themes are not allowed to redirect the user
422
- * after the theme activation, therefore, the agreed UX
423
- * is showing the opt-in as a modal dialog box after
424
- * activation (approved by @otto42, @emiluzelac, @greenshady, @grapplerulrich).
425
- *
426
- * @author Vova Feldman (@svovaf)
427
- * @since 1.2.2.7
428
- *
429
- * @return bool
430
- */
431
- function is_free_wp_org_theme() {
432
- return (
433
- $this->is_theme() &&
434
- $this->is_org_repo_compliant() &&
435
- ! $this->is_premium()
436
- );
437
- }
438
-
439
- /**
440
- * Checks whether this a submenu item is visible.
441
- *
442
- * @author Vova Feldman (@svovaf)
443
- * @since 1.2.2.6
444
- * @since 1.2.2.7 Even if the menu item was specified to be hidden, when it is the context page, then show the submenu item so the user will have the right context page.
445
- *
446
- * @param string $slug
447
- *
448
- * @return bool
449
- */
450
- function is_submenu_item_visible( $slug ) {
451
- if ( $this->is_admin_page( $slug ) ) {
452
- /**
453
- * It is the current context page, so show the submenu item
454
- * so the user will have the right context page, even if it
455
- * was set to hidden.
456
- */
457
- return true;
458
- }
459
-
460
- if ( ! $this->has_settings_menu() ) {
461
- // No menu settings at all.
462
- return false;
463
- }
464
-
465
- if ( $this->is_free_wp_org_theme() ) {
466
- /**
467
- * wp.org themes are limited to a single submenu item, and
468
- * sub-submenu items are most likely not allowed (never verified).
469
- */
470
- return false;
471
- }
472
-
473
- return $this->_menu->is_submenu_item_visible( $slug );
474
- }
475
-
476
- /**
477
- * Check if a Freemius page should be accessible via the UI.
478
- *
479
- * @author Vova Feldman (@svovaf)
480
- * @since 1.2.2.7
481
- *
482
- * @param string $slug
483
- *
484
- * @return bool
485
- */
486
- function is_page_visible( $slug ) {
487
- if ( $this->is_admin_page( $slug ) ) {
488
- return true;
489
- }
490
-
491
- return $this->_menu->is_submenu_item_visible( $slug, true, true );
492
- }
493
-
494
- /**
495
- * @author Vova Feldman (@svovaf)
496
- * @since 1.0.9
497
- */
498
- private function _version_updates_handler() {
499
- if ( ! isset( $this->_storage->sdk_version ) || $this->_storage->sdk_version != $this->version ) {
500
- // Freemius version upgrade mode.
501
- $this->_storage->sdk_last_version = $this->_storage->sdk_version;
502
- $this->_storage->sdk_version = $this->version;
503
-
504
- if ( empty( $this->_storage->sdk_last_version ) ||
505
- version_compare( $this->_storage->sdk_last_version, $this->version, '<' )
506
- ) {
507
- $this->_storage->sdk_upgrade_mode = true;
508
- $this->_storage->sdk_downgrade_mode = false;
509
- } else {
510
- $this->_storage->sdk_downgrade_mode = true;
511
- $this->_storage->sdk_upgrade_mode = false;
512
-
513
- }
514
-
515
- $this->do_action( 'sdk_version_update', $this->_storage->sdk_last_version, $this->version );
516
- }
517
-
518
- $plugin_version = $this->get_plugin_version();
519
- if ( ! isset( $this->_storage->plugin_version ) || $this->_storage->plugin_version != $plugin_version ) {
520
- // Plugin version upgrade mode.
521
- $this->_storage->plugin_last_version = $this->_storage->plugin_version;
522
- $this->_storage->plugin_version = $plugin_version;
523
-
524
- if ( empty( $this->_storage->plugin_last_version ) ||
525
- version_compare( $this->_storage->plugin_last_version, $plugin_version, '<' )
526
- ) {
527
- $this->_storage->plugin_upgrade_mode = true;
528
- $this->_storage->plugin_downgrade_mode = false;
529
- } else {
530
- $this->_storage->plugin_downgrade_mode = true;
531
- $this->_storage->plugin_upgrade_mode = false;
532
- }
533
-
534
- if ( ! empty( $this->_storage->plugin_last_version ) ) {
535
- // Different version of the plugin was installed before, therefore it's an update.
536
- $this->_storage->is_plugin_new_install = false;
537
- }
538
-
539
- $this->do_action( 'plugin_version_update', $this->_storage->plugin_last_version, $plugin_version );
540
- }
541
- }
542
-
543
- /**
544
- * @author Vova Feldman (@svovaf)
545
- * @since 1.1.5
546
- *
547
- * @param string $sdk_prev_version
548
- * @param string $sdk_version
549
- */
550
- function _data_migration( $sdk_prev_version, $sdk_version ) {
551
- /**
552
- * @since 1.1.7.3 Fixed unwanted connectivity test cleanup.
553
- */
554
- if ( empty( $sdk_prev_version ) ) {
555
- return;
556
- }
557
 
558
- if ( version_compare( $sdk_prev_version, '1.2.3', '<' ) &&
559
- version_compare( $sdk_version, '1.2.3', '>=' )
560
- ) {
561
- /**
562
- * Starting from version 1.2.3, paths are stored as relative paths and not absolute paths; so when
563
- * upgrading to 1.2.3, make paths relative.
564
- *
565
- * @author Leo Fajardo (@leorw)
566
- */
567
- $this->make_paths_relative();
568
- }
569
 
570
- if ( version_compare( $sdk_prev_version, '1.1.5', '<' ) &&
571
- version_compare( $sdk_version, '1.1.5', '>=' )
572
- ) {
573
- // On version 1.1.5 merged connectivity and is_on data.
574
- if ( isset( $this->_storage->connectivity_test ) ) {
575
- if ( ! isset( $this->_storage->is_on ) ) {
576
- unset( $this->_storage->connectivity_test );
577
- } else {
578
- $connectivity_data = $this->_storage->connectivity_test;
579
- $connectivity_data['is_active'] = $this->_storage->is_on['is_active'];
580
- $connectivity_data['timestamp'] = $this->_storage->is_on['timestamp'];
581
 
582
- // Override.
583
- $this->_storage->connectivity_test = $connectivity_data;
 
 
 
584
 
585
- // Remove previous structure.
586
- unset( $this->_storage->is_on );
587
- }
 
 
 
588
 
589
- }
590
- }
591
- }
 
 
592
 
593
  /**
594
- * Makes paths relative.
595
- *
596
- * @author Leo Fajardo
597
- * @since 1.2.3
598
  */
599
- private function make_paths_relative() {
600
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
601
 
602
- if ( isset( $id_slug_type_path_map[ $this->_module_id ]['path'] ) ) {
603
- $id_slug_type_path_map[ $this->_module_id ]['path'] = $this->get_relative_path( $id_slug_type_path_map[ $this->_module_id ]['path'] );
 
 
 
604
 
605
- self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
606
- }
 
 
 
607
 
608
- if ( isset( $this->_storage->plugin_main_file ) ) {
609
- $plugin_main_file = $this->_storage->plugin_main_file;
 
 
 
610
 
611
- if ( isset( $plugin_main_file->path ) ) {
612
- $this->_storage->plugin_main_file->path = $this->get_relative_path( $this->_storage->plugin_main_file->path );
613
- } else if ( isset( $plugin_main_file->prev_path ) ) {
614
- $this->_storage->plugin_main_file->prev_path = $this->get_relative_path( $this->_storage->plugin_main_file->prev_path );
615
- }
616
- }
617
 
618
- // Remove invalid path that is still associated with the current slug if there's any.
619
- $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
620
- foreach ( $file_slug_map as $plugin_basename => $slug ) {
621
- if ( $slug === $this->_slug &&
622
- $plugin_basename !== $this->_plugin_basename &&
623
- ! file_exists( $this->get_absolute_path( $plugin_basename ) )
624
- ) {
625
- unset( $file_slug_map[ $plugin_basename ] );
626
- self::$_accounts->set_option( 'file_slug_map', $file_slug_map, true );
627
 
628
- break;
629
- }
630
- }
631
- }
 
632
 
633
- /**
634
- * @author Vova Feldman (@svovaf)
635
- * @since 1.2.2.7
636
- *
637
- * @param string $plugin_prev_version
638
- * @param string $plugin_version
639
- */
640
- function _after_version_update( $plugin_prev_version, $plugin_version ) {
641
- if ( $this->is_theme() ) {
642
- // Expire the cache of the previous tabs since the theme may
643
- // have setting updates.
644
- $this->_cache->expire( 'tabs' );
645
- $this->_cache->expire( 'tabs_stylesheets' );
646
- }
647
- }
648
-
649
- /**
650
- * This action is connected to the 'plugins_loaded' hook and helps to determine
651
- * if this is a new plugin installation or a plugin update.
652
- *
653
- * There are 3 different use-cases:
654
- * 1) New plugin installation right with Freemius:
655
- * 1.1 _activate_plugin_event_hook() will be executed first
656
- * 1.2 Since $this->_storage->is_plugin_new_install is not set,
657
- * and $this->_storage->plugin_last_version is not set,
658
- * $this->_storage->is_plugin_new_install will be set to TRUE.
659
- * 1.3 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will
660
- * be already set to TRUE.
661
- *
662
- * 2) Plugin update, didn't have Freemius before, and now have the SDK:
663
- * 2.1 _activate_plugin_event_hook() will not be executed, because
664
- * the activation hook do NOT fires on updates since WP 3.1.
665
- * 2.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will
666
- * be empty, therefore, it will be set to FALSE.
667
- *
668
- * 3) Plugin update, had Freemius in prev version as well:
669
- * 3.1 _version_updates_handler() will be executed 1st, since FS was installed
670
- * before, $this->_storage->plugin_last_version will NOT be empty,
671
- * therefore, $this->_storage->is_plugin_new_install will be set to FALSE.
672
- * 3.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install is
673
- * already set, therefore, it will not be modified.
674
- *
675
- * Use-case #3 is backward compatible, #3.1 will be executed since 1.0.9.
676
- *
677
- * NOTE:
678
- * The only fallback of this mechanism is if an admin updates a plugin based on use-case #2,
679
- * and then, the next immediate PageView is the plugin's main settings page, it will not
680
- * show the opt-in right away. The reason it will happen is because Freemius execution
681
- * will be turned off till the plugin is fully loaded at least once
682
- * (till $this->_storage->was_plugin_loaded is TRUE).
683
- *
684
- * @author Vova Feldman (@svovaf)
685
- * @since 1.1.9
686
- *
687
- */
688
- function _plugins_loaded() {
689
- // Update flag that plugin was loaded with Freemius at least once.
690
- $this->_storage->was_plugin_loaded = true;
691
-
692
- /**
693
- * Bug fix - only set to false when it's a plugin, due to the
694
- * execution sequence of the theme hooks and our methods, if
695
- * this will be set for themes, Freemius will always assume
696
- * it's a theme update.
697
- *
698
- * @author Vova Feldman (@svovaf)
699
- * @since 1.2.2.2
700
- */
701
- if ( $this->is_plugin() &&
702
- ! isset( $this->_storage->is_plugin_new_install )
703
- ) {
704
- $this->_storage->is_plugin_new_install = false;
705
- }
706
- }
707
-
708
- /**
709
- * @author Vova Feldman (@svovaf)
710
- * @since 1.0.9
711
- */
712
- private function _register_hooks() {
713
- $this->_logger->entrance();
714
-
715
- if ( is_admin() ) {
716
- if ( $this->is_plugin() ) {
717
- $plugin_dir = dirname( $this->_plugin_dir_path ) . '/';
718
-
719
- /**
720
- * @since 1.2.2
721
- *
722
- * Hook to both free and premium version activations to support
723
- * auto deactivation on the other version activation.
724
- */
725
- register_activation_hook(
726
- $plugin_dir . $this->_free_plugin_basename,
727
- array( &$this, '_activate_plugin_event_hook' )
728
- );
729
-
730
- register_activation_hook(
731
- $plugin_dir . $this->premium_plugin_basename(),
732
- array( &$this, '_activate_plugin_event_hook' )
733
- );
734
- } else {
735
- add_action( 'after_switch_theme', array( &$this, '_activate_theme_event_hook' ), 10, 2 );
736
-
737
- /**
738
- * Include the required hooks to capture the theme settings' page tabs
739
- * and cache them.
740
- *
741
- * @author Vova Feldman (@svovaf)
742
- * @since 1.2.2.7
743
- */
744
- if ( ! $this->_cache->has_valid( 'tabs' ) ) {
745
- add_action( 'admin_footer', array( &$this, '_tabs_capture' ) );
746
- // Add license activation AJAX callback.
747
- $this->add_ajax_action( 'store_tabs', array( &$this, '_store_tabs_ajax_action' ) );
748
-
749
- add_action( 'admin_enqueue_scripts', array( &$this, '_store_tabs_styles' ), 9999999 );
750
- }
751
-
752
- add_action(
753
- 'admin_footer',
754
- array( &$this, '_add_freemius_tabs' ),
755
- /**
756
- * The tabs JS code must be executed after the tabs capture logic (_tabs_capture()).
757
- * That's why the priority is 11 while the tabs capture logic is added
758
- * with priority 10.
759
- *
760
- * @author Vova Feldman (@svovaf)
761
- */
762
- 11
763
- );
764
-
765
- add_action( 'admin_footer', array( &$this, '_style_premium_theme' ) );
766
- }
767
-
768
- /**
769
- * Part of the mechanism to identify new plugin install vs. plugin update.
770
- *
771
- * @author Vova Feldman (@svovaf)
772
- * @since 1.1.9
773
- */
774
- if ( empty( $this->_storage->was_plugin_loaded ) ) {
775
- if ( $this->is_plugin() &&
776
- $this->is_activation_mode( false ) &&
777
- 0 == did_action( 'plugins_loaded' )
778
- ) {
779
- add_action( 'plugins_loaded', array( &$this, '_plugins_loaded' ) );
780
- } else {
781
- // If was activated before, then it was already loaded before.
782
- $this->_plugins_loaded();
783
- }
784
- }
785
-
786
- if ( ! self::is_ajax() ) {
787
- if ( ! $this->is_addon() ) {
788
- add_action( 'init', array( &$this, '_add_default_submenu_items' ), WP_FS__LOWEST_PRIORITY );
789
- }
790
- }
791
- }
792
-
793
- if ( $this->is_plugin() ) {
794
- register_deactivation_hook( $this->_plugin_main_file_path, array( &$this, '_deactivate_plugin_hook' ) );
795
- }
796
-
797
- if ( $this->is_theme() && self::is_customizer() ) {
798
- // Register customizer upsell.
799
- add_action( 'customize_register', array( &$this, '_customizer_register' ) );
800
- }
801
-
802
- add_action( 'init', array( &$this, '_redirect_on_clicked_menu_link' ), WP_FS__LOWEST_PRIORITY );
803
-
804
- add_action( 'admin_init', array( &$this, '_add_tracking_links' ) );
805
- add_action( 'admin_init', array( &$this, '_add_license_activation' ) );
806
- $this->add_ajax_action( 'update_billing', array( &$this, '_update_billing_ajax_action' ) );
807
- $this->add_ajax_action( 'start_trial', array( &$this, '_start_trial_ajax_action' ) );
808
-
809
- $this->add_ajax_action( 'install_premium_version', array(
810
- &$this,
811
- '_install_premium_version_ajax_action'
812
- ) );
813
 
814
- $this->add_ajax_action( 'submit_affiliate_application', array( &$this, '_submit_affiliate_application' ) );
 
 
 
 
815
 
816
- $this->add_action( 'after_plans_sync', array( &$this, '_check_for_trial_plans' ) );
 
 
 
817
 
818
- $this->add_action( 'sdk_version_update', array( &$this, '_data_migration' ), WP_FS__DEFAULT_PRIORITY, 2 );
819
- $this->add_action( 'plugin_version_update', array( &$this, '_after_version_update' ), WP_FS__DEFAULT_PRIORITY, 2 );
820
- $this->add_filter( 'after_code_type_change', array( &$this, '_after_code_type_change' ) );
 
 
821
 
822
- add_action( 'admin_init', array( &$this, '_add_trial_notice' ) );
823
- add_action( 'admin_init', array( &$this, '_add_affiliate_program_notice' ) );
824
- add_action( 'admin_init', array( &$this, '_enqueue_common_css' ) );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
825
 
826
- /**
827
- * Handle request to reset anonymous mode for `get_reconnect_url()`.
828
- *
829
- * @author Vova Feldman (@svovaf)
830
- * @since 1.2.1.5
831
- */
832
- if ( fs_request_is_action( 'reset_anonymous_mode' ) &&
833
- $this->get_unique_affix() === fs_request_get( 'fs_unique_affix' )
834
- ) {
835
- add_action( 'admin_init', array( &$this, 'connect_again' ) );
836
- }
837
- }
838
-
839
- /**
840
- * Keeping the uninstall hook registered for free or premium plugin version may result to a fatal error that
841
- * could happen when a user tries to uninstall either version while one of them is still active. Uninstalling a
842
- * plugin will trigger inclusion of the free or premium version and if one of them is active during the
843
- * uninstallation, a fatal error may occur in case the plugin's class or functions are already defined.
844
- *
845
- * @author Leo Fajardo (leorw)
846
- *
847
- * @since 1.2.0
848
- */
849
- private function unregister_uninstall_hook() {
850
- $uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
851
- unset( $uninstallable_plugins[ $this->_free_plugin_basename ] );
852
- unset( $uninstallable_plugins[ $this->premium_plugin_basename() ] );
853
-
854
- update_option( 'uninstall_plugins', $uninstallable_plugins );
855
- }
856
-
857
- /**
858
- * @since 1.2.0 Invalidate module's main file cache, otherwise, FS_Plugin_Updater will not fetch updates.
859
- */
860
- private function clear_module_main_file_cache() {
861
- if ( ! isset( $this->_storage->plugin_main_file ) ||
862
- empty( $this->_storage->plugin_main_file->path )
863
- ) {
864
- return;
865
- }
866
-
867
- $plugin_main_file = clone $this->_storage->plugin_main_file;
868
-
869
- // Store cached path (2nd layer cache).
870
- $plugin_main_file->prev_path = $plugin_main_file->path;
871
-
872
- // Clear cached path.
873
- unset( $plugin_main_file->path );
874
-
875
- $this->_storage->plugin_main_file = $plugin_main_file;
876
-
877
- /**
878
- * Clear global cached path.
879
- *
880
- * @author Leo Fajardo (@leorw)
881
- * @since 1.2.2
882
- */
883
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map' );
884
- unset( $id_slug_type_path_map[ $this->_module_id ]['path'] );
885
- self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
886
- }
887
-
888
- /**
889
- * @author Vova Feldman (@svovaf)
890
- * @since 1.0.9
891
- */
892
- private function _register_account_hooks() {
893
- if ( ! is_admin() ) {
894
- return;
895
- }
896
-
897
- /**
898
- * Always show the deactivation feedback form since we added
899
- * automatic free version deactivation upon premium code activation.
900
- *
901
- * @since 1.2.1.6
902
- */
903
- $this->add_ajax_action(
904
- 'submit_uninstall_reason',
905
- array( &$this, '_submit_uninstall_reason_action' )
906
- );
907
-
908
- if ( ( $this->is_plugin() && self::is_plugins_page() ) ||
909
- ( $this->is_theme() && self::is_themes_page() )
910
- ) {
911
- add_action( 'admin_footer', array( &$this, '_add_deactivation_feedback_dialog_box' ) );
912
- }
913
- }
914
-
915
- /**
916
- * Leverage backtrace to find caller plugin file path.
917
- *
918
- * @author Vova Feldman (@svovaf)
919
- * @since 1.0.6
920
- *
921
- * @param bool $is_init Is initiation sequence.
922
- *
923
- * @return string
924
- */
925
- private function _find_caller_plugin_file( $is_init = false ) {
926
- // Try to load the cached value of the file path.
927
- if ( isset( $this->_storage->plugin_main_file ) ) {
928
- $plugin_main_file = $this->_storage->plugin_main_file;
929
- if ( isset( $plugin_main_file->path ) ) {
930
- $absolute_path = $this->get_absolute_path( $plugin_main_file->path );
931
- if ( file_exists( $absolute_path ) ) {
932
- return $absolute_path;
933
- }
934
- }
935
- }
936
-
937
- /**
938
- * @since 1.2.1
939
- *
940
- * `clear_module_main_file_cache()` is clearing the plugin's cached path on
941
- * deactivation. Therefore, if any plugin/theme was initiating `Freemius`
942
- * with that plugin's slug, it was overriding the empty plugin path with a wrong path.
943
- *
944
- * So, we've added a special mechanism with a 2nd layer of cache that uses `prev_path`
945
- * when the class instantiator isn't the module.
946
- */
947
- if ( ! $is_init ) {
948
- // Fetch prev path cache.
949
- if ( isset( $this->_storage->plugin_main_file ) &&
950
- isset( $this->_storage->plugin_main_file->prev_path )
951
- ) {
952
- $absolute_path = $this->get_absolute_path( $this->_storage->plugin_main_file->prev_path );
953
- if ( file_exists( $absolute_path ) ) {
954
- return $absolute_path;
955
- }
956
- }
957
-
958
- wp_die(
959
- $this->get_text_inline( 'Freemius SDK couldn\'t find the plugin\'s main file. Please contact sdk@freemius.com with the current error.', 'failed-finding-main-path' ) .
960
- " Module: {$this->_slug}; SDK: " . WP_FS__SDK_VERSION . ";",
961
- $this->get_text_inline( 'Error', 'error' ),
962
- array( 'back_link' => true )
963
- );
964
- }
965
-
966
- /**
967
- * @since 1.2.1
968
- *
969
- * Only the original instantiator that calls dynamic_init can modify the module's path.
970
- */
971
- // Find caller module.
972
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
973
- $this->_storage->plugin_main_file = (object) array(
974
- 'path' => $id_slug_type_path_map[ $this->_module_id ]['path'],
975
- );
976
 
977
- return $this->get_absolute_path( $id_slug_type_path_map[ $this->_module_id ]['path'] );
978
- }
 
 
979
 
980
  /**
981
- * @author Leo Fajardo (@leorw)
982
- * @since 1.2.3
983
  *
984
- * @param string $path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
985
  *
986
- * @return string
987
  */
988
- private function get_relative_path( $path ) {
989
- $module_root_dir = $this->get_module_root_dir_path();
990
- if ( 0 === strpos( $path, $module_root_dir ) ) {
991
- $path = substr( $path, strlen( $module_root_dir ) );
992
- }
993
 
994
- return $path;
995
- }
 
 
996
 
997
  /**
998
- * @author Leo Fajardo (@leorw)
999
- * @since 1.2.3
1000
  *
1001
- * @param string $path
1002
- * @param string|bool $module_type
 
 
 
 
1003
  *
1004
- * @return string
1005
  */
1006
- private function get_absolute_path( $path, $module_type = false ) {
1007
- $module_root_dir = $this->get_module_root_dir_path( $module_type );
1008
- if ( 0 !== strpos( $path, $module_root_dir ) ) {
1009
- $path = fs_normalize_path( $module_root_dir . $path );
1010
- }
1011
 
1012
- return $path;
1013
- }
 
 
 
 
1014
 
1015
  /**
1016
- * @author Leo Fajardo (@leorw)
1017
- * @since 1.2.3
1018
  *
1019
- * @param string|bool $module_type
 
 
 
 
 
1020
  *
1021
- * @return string
1022
  */
1023
- private function get_module_root_dir_path( $module_type = false ) {
1024
- $is_plugin = empty( $module_type ) ?
1025
- $this->is_plugin() :
1026
- ( WP_FS__MODULE_TYPE_PLUGIN === $module_type );
1027
 
1028
- return fs_normalize_path( trailingslashit( $is_plugin ?
1029
- WP_PLUGIN_DIR :
1030
- get_theme_root() ) );
1031
- }
 
 
1032
 
1033
- /**
1034
- * @author Leo Fajardo (@leorw)
1035
- *
1036
- * @param number $module_id
1037
- * @param string $slug
1038
- *
1039
- * @since 1.2.2
1040
- */
1041
- private function store_id_slug_type_path_map( $module_id, $slug ) {
1042
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
1043
-
1044
- $store_option = false;
1045
-
1046
- if ( ! isset( $id_slug_type_path_map[ $module_id ] ) ) {
1047
- $id_slug_type_path_map[ $module_id ] = array(
1048
- 'slug' => $slug
1049
- );
1050
-
1051
- $store_option = true;
1052
- }
1053
-
1054
- if ( ! isset( $id_slug_type_path_map[ $module_id ]['path'] ) ||
1055
- /**
1056
- * This verification is for cases when suddenly the same module
1057
- * is installed but with a different folder name.
1058
- *
1059
- * @author Vova Feldman (@svovaf)
1060
- * @since 1.2.3
1061
- */
1062
- ! file_exists( $this->get_absolute_path(
1063
- $id_slug_type_path_map[ $module_id ]['path'],
1064
- $id_slug_type_path_map[ $module_id ]['type']
1065
- ) )
1066
- ) {
1067
- $caller_main_file_and_type = $this->get_caller_main_file_and_type();
1068
-
1069
- $id_slug_type_path_map[ $module_id ]['type'] = $caller_main_file_and_type->module_type;
1070
- $id_slug_type_path_map[ $module_id ]['path'] = $caller_main_file_and_type->path;
1071
-
1072
- $store_option = true;
1073
- }
1074
-
1075
- if ( $store_option ) {
1076
- self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
1077
- }
1078
- }
1079
-
1080
- /**
1081
- * Identifies the caller type: plugin or theme.
1082
- *
1083
- * @author Leo Fajardo (@leorw)
1084
- * @since 1.2.2
1085
- *
1086
- * @author Vova Feldman (@svovaf)
1087
- * @since 1.2.2.3 Find the earliest module in the call stack that calls to the SDK. This fix is for cases when
1088
- * add-ons are relying on loading the SDK from the parent module, and also allows themes including the
1089
- * SDK an internal file instead of directly from functions.php.
1090
- * @since 1.2.1.7 Knows how to handle cases when an add-on includes the parent module logic.
1091
- */
1092
- private function get_caller_main_file_and_type() {
1093
- self::require_plugin_essentials();
1094
-
1095
- $all_plugins = get_plugins();
1096
- $all_plugins_paths = array();
1097
-
1098
- // Get active plugin's main files real full names (might be symlinks).
1099
- foreach ( $all_plugins as $relative_path => &$data ) {
1100
- if ( false === strpos( fs_normalize_path( $relative_path ), '/' ) ) {
1101
- /**
1102
- * Ignore plugins that don't have a folder (e.g. Hello Dolly) since they
1103
- * can't really include the SDK.
1104
- *
1105
- * @author Vova Feldman
1106
- * @since 1.2.1.7
1107
- */
1108
- continue;
1109
- }
1110
-
1111
- $all_plugins_paths[] = fs_normalize_path( realpath( WP_PLUGIN_DIR . '/' . $relative_path ) );
1112
- }
1113
-
1114
- $caller_file_candidate = false;
1115
- $caller_map = array();
1116
- $module_type = WP_FS__MODULE_TYPE_PLUGIN;
1117
- $themes_dir = fs_normalize_path( get_theme_root() );
1118
-
1119
- for ( $i = 1, $bt = debug_backtrace(), $len = count( $bt ); $i < $len; $i ++ ) {
1120
- if ( empty( $bt[ $i ]['file'] ) ) {
1121
- continue;
1122
- }
1123
-
1124
- if ( $i > 1 && ! empty( $bt[ $i - 1 ]['file'] ) && $bt[ $i ]['file'] === $bt[ $i - 1 ]['file'] ) {
1125
- // If file same as the prev file in the stack, skip it.
1126
- continue;
1127
- }
1128
-
1129
- if ( ! empty( $bt[ $i ]['function'] ) && in_array( $bt[ $i ]['function'], array(
1130
- 'do_action',
1131
- 'apply_filter',
1132
- // The string split is stupid, but otherwise, theme check
1133
- // throws info notices.
1134
- 'requir' . 'e_once',
1135
- 'requir' . 'e',
1136
- 'includ' . 'e_once',
1137
- 'includ' . 'e'
1138
- ) )
1139
- ) {
1140
- // Ignore call stack hooks and files inclusion.
1141
- continue;
1142
- }
1143
-
1144
- $caller_file_path = fs_normalize_path( $bt[ $i ]['file'] );
1145
-
1146
- if ( 'functions.php' === basename( $caller_file_path ) ) {
1147
- /**
1148
- * 1. Assumes that theme's starting execution file is functions.php.
1149
- * 2. This complex logic fixes symlink issues (e.g. with Vargant).
1150
- *
1151
- * @author Vova Feldman (@svovaf)
1152
- * @since 1.2.2.5
1153
- */
1154
-
1155
- if ( $caller_file_path == fs_normalize_path( realpath( trailingslashit( $themes_dir ) . basename( dirname( $caller_file_path ) ) . '/' . basename( $caller_file_path ) ) ) ) {
1156
- $module_type = WP_FS__MODULE_TYPE_THEME;
1157
 
1158
- /**
1159
- * Relative path of the theme, e.g.:
1160
- * `my-theme/functions.php`
1161
- *
1162
- * @author Leo Fajardo (@leorw)
1163
- */
1164
- $caller_file_candidate = basename( dirname( $caller_file_path ) ) .
1165
- '/' .
1166
- basename( $caller_file_path );
1167
-
1168
- continue;
1169
- }
1170
- }
1171
-
1172
- $caller_file_hash = md5( $caller_file_path );
1173
-
1174
- if ( ! isset( $caller_map[ $caller_file_hash ] ) ) {
1175
- foreach ( $all_plugins_paths as $plugin_path ) {
1176
- if ( false !== strpos( $caller_file_path, fs_normalize_path( dirname( $plugin_path ) . '/' ) ) ) {
1177
- $caller_map[ $caller_file_hash ] = fs_normalize_path( $plugin_path );
1178
- break;
1179
- }
1180
- }
1181
- }
1182
-
1183
- if ( isset( $caller_map[ $caller_file_hash ] ) ) {
1184
- $module_type = WP_FS__MODULE_TYPE_PLUGIN;
1185
- $caller_file_candidate = plugin_basename( $caller_map[ $caller_file_hash ] );
1186
- }
1187
- }
1188
-
1189
- return (object) array(
1190
- 'module_type' => $module_type,
1191
- 'path' => $caller_file_candidate
1192
- );
1193
- }
1194
-
1195
- #----------------------------------------------------------------------------------
1196
- #region Deactivation Feedback Form
1197
- #----------------------------------------------------------------------------------
1198
-
1199
- /**
1200
- * Displays a confirmation and feedback dialog box when the user clicks on the "Deactivate" link on the plugins
1201
- * page.
1202
- *
1203
- * @author Vova Feldman (@svovaf)
1204
- * @author Leo Fajardo (@leorw)
1205
- * @since 1.1.2
1206
- */
1207
- function _add_deactivation_feedback_dialog_box() {
1208
- /* Check the type of user:
1209
- * 1. Long-term (long-term)
1210
- * 2. Non-registered and non-anonymous short-term (non-registered-and-non-anonymous-short-term).
1211
- * 3. Short-term (short-term)
1212
- */
1213
- $is_long_term_user = true;
1214
 
1215
- // Check if the site is at least 2 days old.
1216
- $time_installed = $this->_storage->install_timestamp;
 
 
 
 
 
 
 
 
 
 
 
 
1217
 
1218
- // Difference in seconds.
1219
- $date_diff = time() - $time_installed;
 
1220
 
1221
- // Convert seconds to days.
1222
- $date_diff_days = floor( $date_diff / ( 60 * 60 * 24 ) );
1223
 
1224
- if ( $date_diff_days < 2 ) {
1225
- $is_long_term_user = false;
1226
- }
1227
 
1228
- $is_long_term_user = $this->apply_filters( 'is_long_term_user', $is_long_term_user );
1229
 
1230
- if ( $is_long_term_user ) {
1231
- $user_type = 'long-term';
1232
- } else {
1233
- if ( ! $this->is_registered() && ! $this->is_anonymous() ) {
1234
- $user_type = 'non-registered-and-non-anonymous-short-term';
1235
- } else {
1236
- $user_type = 'short-term';
1237
- }
1238
- }
1239
 
1240
- $uninstall_reasons = $this->_get_uninstall_reasons( $user_type );
 
 
 
1241
 
1242
- // Load the HTML template for the deactivation feedback dialog box.
1243
- $vars = array(
1244
- 'reasons' => $uninstall_reasons,
1245
- 'id' => $this->_module_id
1246
- );
1247
 
1248
- /**
1249
- * @todo Deactivation form core functions should be loaded only once! Otherwise, when there are multiple Freemius powered plugins the same code is loaded multiple times. The only thing that should be loaded differently is the various deactivation reasons object based on the state of the plugin.
1250
- */
1251
- fs_require_template( 'forms/deactivation/form.php', $vars );
1252
- }
1253
-
1254
- /**
1255
- * @author Leo Fajardo (leorw)
1256
- * @since 1.1.2
1257
- *
1258
- * @param string $user_type
1259
- *
1260
- * @return array The uninstall reasons for the specified user type.
1261
- */
1262
- function _get_uninstall_reasons( $user_type = 'long-term' ) {
1263
- $module_type = $this->_module_type;
1264
-
1265
- $internal_message_template_var = array(
1266
- 'id' => $this->_module_id
1267
- );
1268
-
1269
- if ( $this->is_registered() && false !== $this->get_plan() && $this->get_plan()->has_technical_support() ) {
1270
- $contact_support_template = fs_get_template( 'forms/deactivation/contact.php', $internal_message_template_var );
1271
- } else {
1272
- $contact_support_template = '';
1273
- }
1274
-
1275
- $reason_found_better_plugin = array(
1276
- 'id' => self::REASON_FOUND_A_BETTER_PLUGIN,
1277
- 'text' => sprintf( $this->get_text_inline( 'I found a better %s', 'reason-found-a-better-plugin' ), $module_type ),
1278
- 'input_type' => 'textfield',
1279
- 'input_placeholder' => sprintf( $this->get_text_inline( "What's the %s's name?", 'placeholder-plugin-name' ), $module_type ),
1280
- );
1281
-
1282
- $reason_temporary_deactivation = array(
1283
- 'id' => self::REASON_TEMPORARY_DEACTIVATION,
1284
- 'text' => sprintf(
1285
- $this->get_text_inline( "It's a temporary %s. I'm just debugging an issue.", 'reason-temporary-x' ),
1286
- strtolower( $this->is_plugin() ?
1287
- $this->get_text_inline( 'Deactivation', 'deactivation' ) :
1288
- $this->get_text_inline( 'Theme Switch', 'theme-switch' )
1289
- )
1290
- ),
1291
- 'input_type' => '',
1292
- 'input_placeholder' => ''
1293
- );
1294
-
1295
- $reason_other = array(
1296
- 'id' => self::REASON_OTHER,
1297
- 'text' => $this->get_text_inline( 'Other', 'reason-other' ),
1298
- 'input_type' => 'textfield',
1299
- 'input_placeholder' => ''
1300
- );
1301
-
1302
- $long_term_user_reasons = array(
1303
- array(
1304
- 'id' => self::REASON_NO_LONGER_NEEDED,
1305
- 'text' => sprintf( $this->get_text_inline( 'I no longer need the %s', 'reason-no-longer-needed' ), $module_type ),
1306
- 'input_type' => '',
1307
- 'input_placeholder' => ''
1308
- ),
1309
- $reason_found_better_plugin,
1310
- array(
1311
- 'id' => self::REASON_NEEDED_FOR_A_SHORT_PERIOD,
1312
- 'text' => sprintf( $this->get_text_inline( 'I only needed the %s for a short period', 'reason-needed-for-a-short-period' ), $module_type ),
1313
- 'input_type' => '',
1314
- 'input_placeholder' => ''
1315
- ),
1316
- array(
1317
- 'id' => self::REASON_BROKE_MY_SITE,
1318
- 'text' => sprintf( $this->get_text_inline( 'The %s broke my site', 'reason-broke-my-site' ), $module_type ),
1319
- 'input_type' => '',
1320
- 'input_placeholder' => '',
1321
- 'internal_message' => $contact_support_template
1322
- ),
1323
- array(
1324
- 'id' => self::REASON_SUDDENLY_STOPPED_WORKING,
1325
- 'text' => sprintf( $this->get_text_inline( 'The %s suddenly stopped working', 'reason-suddenly-stopped-working' ), $module_type ),
1326
- 'input_type' => '',
1327
- 'input_placeholder' => '',
1328
- 'internal_message' => $contact_support_template
1329
- )
1330
- );
1331
-
1332
- if ( $this->is_paying() ) {
1333
- $long_term_user_reasons[] = array(
1334
- 'id' => self::REASON_CANT_PAY_ANYMORE,
1335
- 'text' => $this->get_text_inline( "I can't pay for it anymore", 'reason-cant-pay-anymore' ),
1336
- 'input_type' => 'textfield',
1337
- 'input_placeholder' => $this->get_text_inline( 'What price would you feel comfortable paying?', 'placeholder-comfortable-price' )
1338
- );
1339
- }
1340
-
1341
- $reason_dont_share_info = array(
1342
- 'id' => self::REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION,
1343
- 'text' => $this->get_text_inline( "I don't like to share my information with you", 'reason-dont-like-to-share-my-information' ),
1344
- 'input_type' => '',
1345
- 'input_placeholder' => ''
1346
- );
1347
-
1348
- /**
1349
- * If the current user has selected the "don't share data" reason in the deactivation feedback modal, inform the
1350
- * user by showing additional message that he doesn't have to share data and can just choose to skip the opt-in
1351
- * (the Skip button is included in the message to show). This message will only be shown if anonymous mode is
1352
- * enabled and the user's account is currently not in pending activation state (similar to the way the Skip
1353
- * button in the opt-in form is shown/hidden).
1354
- */
1355
- if ( $this->is_enable_anonymous() && ! $this->is_pending_activation() ) {
1356
- $reason_dont_share_info['internal_message'] = fs_get_template( 'forms/deactivation/retry-skip.php', $internal_message_template_var );
1357
- }
1358
-
1359
- $uninstall_reasons = array(
1360
- 'long-term' => $long_term_user_reasons,
1361
- 'non-registered-and-non-anonymous-short-term' => array(
1362
- array(
1363
- 'id' => self::REASON_DIDNT_WORK,
1364
- 'text' => sprintf( $this->get_text_inline( "The %s didn't work", 'reason-didnt-work' ), $module_type ),
1365
- 'input_type' => '',
1366
- 'input_placeholder' => ''
1367
- ),
1368
- $reason_dont_share_info,
1369
- $reason_found_better_plugin
1370
- ),
1371
- 'short-term' => array(
1372
- array(
1373
- 'id' => self::REASON_COULDNT_MAKE_IT_WORK,
1374
- 'text' => $this->get_text_inline( "I couldn't understand how to make it work", 'reason-couldnt-make-it-work' ),
1375
- 'input_type' => '',
1376
- 'input_placeholder' => '',
1377
- 'internal_message' => $contact_support_template
1378
- ),
1379
- $reason_found_better_plugin,
1380
- array(
1381
- 'id' => self::REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE,
1382
- 'text' => sprintf( $this->get_text_inline( "The %s is great, but I need specific feature that you don't support", 'reason-great-but-need-specific-feature' ), $module_type ),
1383
- 'input_type' => 'textarea',
1384
- 'input_placeholder' => $this->get_text_inline( 'What feature?', 'placeholder-feature' )
1385
- ),
1386
- array(
1387
- 'id' => self::REASON_NOT_WORKING,
1388
- 'text' => sprintf( $this->get_text_inline( 'The %s is not working', 'reason-not-working' ), $module_type ),
1389
- 'input_type' => 'textarea',
1390
- 'input_placeholder' => $this->get_text_inline( "Kindly share what didn't work so we can fix it for future users...", 'placeholder-share-what-didnt-work' )
1391
- ),
1392
- array(
1393
- 'id' => self::REASON_NOT_WHAT_I_WAS_LOOKING_FOR,
1394
- 'text' => $this->get_text_inline( "It's not what I was looking for", 'reason-not-what-i-was-looking-for' ),
1395
- 'input_type' => 'textarea',
1396
- 'input_placeholder' => $this->get_text_inline( "What you've been looking for?", 'placeholder-what-youve-been-looking-for' )
1397
- ),
1398
- array(
1399
- 'id' => self::REASON_DIDNT_WORK_AS_EXPECTED,
1400
- 'text' => sprintf( $this->get_text_inline( "The %s didn't work as expected", 'reason-didnt-work-as-expected' ), $module_type ),
1401
- 'input_type' => 'textarea',
1402
- 'input_placeholder' => $this->get_text_inline( 'What did you expect?', 'placeholder-what-did-you-expect' )
1403
- )
1404
- )
1405
- );
1406
-
1407
- // Randomize the reasons for the current user type.
1408
- shuffle( $uninstall_reasons[ $user_type ] );
1409
-
1410
- // Keep the following reasons as the last items in the list.
1411
- $uninstall_reasons[ $user_type ][] = $reason_temporary_deactivation;
1412
- $uninstall_reasons[ $user_type ][] = $reason_other;
1413
-
1414
- $uninstall_reasons = $this->apply_filters( 'uninstall_reasons', $uninstall_reasons );
1415
-
1416
- return $uninstall_reasons[ $user_type ];
1417
- }
1418
-
1419
- /**
1420
- * Called after the user has submitted his reason for deactivating the plugin.
1421
- *
1422
- * @author Leo Fajardo (@leorw)
1423
- * @since 1.1.2
1424
- */
1425
- function _submit_uninstall_reason_action() {
1426
- $this->_logger->entrance();
1427
-
1428
- $this->check_ajax_referer( 'submit_uninstall_reason' );
1429
-
1430
- $reason_id = fs_request_get( 'reason_id' );
1431
-
1432
- // Check if the given reason ID is an unsigned integer.
1433
- if ( ! ctype_digit( $reason_id ) ) {
1434
- exit;
1435
- }
1436
-
1437
- $reason_info = trim( fs_request_get( 'reason_info', '' ) );
1438
- if ( ! empty( $reason_info ) ) {
1439
- $reason_info = substr( $reason_info, 0, 128 );
1440
- }
1441
-
1442
- $reason = (object) array(
1443
- 'id' => $reason_id,
1444
- 'info' => $reason_info,
1445
- 'is_anonymous' => fs_request_get_bool( 'is_anonymous' )
1446
- );
1447
-
1448
- $this->_storage->store( 'uninstall_reason', $reason );
1449
-
1450
- /**
1451
- * If the module type is "theme", trigger the uninstall event here (on theme deactivation) since themes do
1452
- * not support uninstall hook.
1453
- *
1454
- * @author Leo Fajardo (@leorw)
1455
- * @since 1.2.2
1456
- */
1457
- if ( $this->is_theme() ) {
1458
- $this->_uninstall_plugin_event( false );
1459
- $this->remove_sdk_reference();
1460
- }
1461
-
1462
- // Print '1' for successful operation.
1463
- echo 1;
1464
- exit;
1465
- }
1466
-
1467
- #endregion
1468
-
1469
- #----------------------------------------------------------------------------------
1470
- #region Instance
1471
- #----------------------------------------------------------------------------------
1472
-
1473
- /**
1474
- * Main singleton instance.
1475
- *
1476
- * @author Vova Feldman (@svovaf)
1477
- * @since 1.0.0
1478
- *
1479
- * @param number $module_id
1480
- * @param string|bool $slug
1481
- * @param bool $is_init Is initiation sequence.
1482
- *
1483
- * @return Freemius|false
1484
- */
1485
- static function instance( $module_id, $slug = false, $is_init = false ) {
1486
- if ( empty( $module_id ) ) {
1487
- return false;
1488
- }
1489
-
1490
- if ( ! is_numeric( $module_id ) ) {
1491
- if ( ! $is_init && true === $slug ) {
1492
- $is_init = true;
1493
- }
1494
-
1495
- $slug = $module_id;
1496
-
1497
- $module = FS_Plugin_Manager::instance( $slug )->get();
1498
-
1499
- if ( is_object( $module ) ) {
1500
- $module_id = $module->id;
1501
- }
1502
- }
1503
-
1504
- $key = 'm_' . $module_id;
1505
-
1506
- if ( ! isset( self::$_instances[ $key ] ) ) {
1507
- if ( 0 === count( self::$_instances ) ) {
1508
- self::_load_required_static();
1509
- }
1510
-
1511
- self::$_instances[ $key ] = new Freemius( $module_id, $slug, $is_init );
1512
- }
1513
-
1514
- return self::$_instances[ $key ];
1515
- }
1516
-
1517
- /**
1518
- * @author Vova Feldman (@svovaf)
1519
- * @since 1.0.6
1520
- *
1521
- * @param number $addon_id
1522
- *
1523
- * @return bool
1524
- */
1525
- private static function has_instance( $addon_id ) {
1526
- return isset( self::$_instances[ 'm_' . $addon_id ] );
1527
- }
1528
-
1529
- /**
1530
- * @author Leo Fajardo (@leorw)
1531
- * @since 1.2.2
1532
- *
1533
- * @param string|number $id_or_slug
1534
- *
1535
- * @return number|false
1536
- */
1537
- private static function get_module_id( $id_or_slug ) {
1538
- if ( is_numeric( $id_or_slug ) ) {
1539
- return $id_or_slug;
1540
- }
1541
-
1542
- foreach ( self::$_instances as $instance ) {
1543
- if ( $instance->is_plugin() && ( $id_or_slug === $instance->get_slug() ) ) {
1544
- return $instance->get_id();
1545
- }
1546
- }
1547
-
1548
- return false;
1549
- }
1550
-
1551
- /**
1552
- * @author Vova Feldman (@svovaf)
1553
- * @since 1.0.6
1554
- *
1555
- * @param number $id
1556
- *
1557
- * @return false|Freemius
1558
- */
1559
- static function get_instance_by_id( $id ) {
1560
- return isset ( self::$_instances[ 'm_' . $id ] ) ?
1561
- self::$_instances[ 'm_' . $id ] :
1562
- false;
1563
- }
1564
-
1565
- /**
1566
- *
1567
- * @author Vova Feldman (@svovaf)
1568
- * @since 1.0.1
1569
- *
1570
- * @param $plugin_file
1571
- *
1572
- * @return false|Freemius
1573
- */
1574
- static function get_instance_by_file( $plugin_file ) {
1575
- $slug = self::find_slug_by_basename( $plugin_file );
1576
-
1577
- return ( false !== $slug ) ?
1578
- self::instance( self::get_module_id( $slug ) ) :
1579
- false;
1580
- }
1581
-
1582
- /**
1583
- * @author Vova Feldman (@svovaf)
1584
- * @since 1.0.6
1585
- *
1586
- * @return false|Freemius
1587
- */
1588
- function get_parent_instance() {
1589
- return self::get_instance_by_id( $this->_plugin->parent_plugin_id );
1590
- }
1591
-
1592
- /**
1593
- * @author Vova Feldman (@svovaf)
1594
- * @since 1.0.6
1595
- *
1596
- * @param string|number $id_or_slug
1597
- *
1598
- * @return false|Freemius
1599
- */
1600
- function get_addon_instance( $id_or_slug ) {
1601
- $addon_id = self::get_module_id( $id_or_slug );
1602
-
1603
- return self::instance( $addon_id );
1604
- }
1605
-
1606
- #endregion ------------------------------------------------------------------
1607
-
1608
- /**
1609
- * @author Vova Feldman (@svovaf)
1610
- * @since 1.0.6
1611
- *
1612
- * @return bool
1613
- */
1614
- function is_parent_plugin_installed() {
1615
- $is_active = self::has_instance( $this->_plugin->parent_plugin_id );
1616
-
1617
- if ( $is_active ) {
1618
- return true;
1619
- }
1620
-
1621
- /**
1622
- * Parent module might be a theme. If that's the case, the add-on's FS
1623
- * instance will be loaded prior to the theme's FS instance, therefore,
1624
- * we need to check if it's active with a "look ahead".
1625
- *
1626
- * @author Vova Feldman
1627
- * @since 1.2.2.3
1628
- */
1629
- global $fs_active_plugins;
1630
- if ( is_object( $fs_active_plugins ) && is_array( $fs_active_plugins->plugins ) ) {
1631
- $active_theme = wp_get_theme();
1632
-
1633
- foreach ( $fs_active_plugins->plugins as $sdk => $module ) {
1634
- if ( WP_FS__MODULE_TYPE_THEME === $module->type ) {
1635
- if ( $module->plugin_path == $active_theme->get_stylesheet() ) {
1636
- // Parent module is a theme and it's currently active.
1637
- return true;
1638
- }
1639
- }
1640
- }
1641
- }
1642
-
1643
- return false;
1644
- }
1645
-
1646
- /**
1647
- * Check if add-on parent plugin in activation mode.
1648
- *
1649
- * @author Vova Feldman (@svovaf)
1650
- * @since 1.0.7
1651
- *
1652
- * @return bool
1653
- */
1654
- function is_parent_in_activation() {
1655
- $parent_fs = $this->get_parent_instance();
1656
- if ( ! is_object( $parent_fs ) ) {
1657
- return false;
1658
- }
1659
-
1660
- return ( $parent_fs->is_activation_mode() );
1661
- }
1662
-
1663
- /**
1664
- * Is plugin in activation mode.
1665
- *
1666
- * @author Vova Feldman (@svovaf)
1667
- * @since 1.0.7
1668
- *
1669
- * @param bool $and_on
1670
- *
1671
- * @return bool
1672
- */
1673
- function is_activation_mode( $and_on = true ) {
1674
- return (
1675
- ( $this->is_on() || ! $and_on ) &&
1676
- ( ! $this->is_registered() || ( $this->is_only_premium() && ! $this->has_features_enabled_license() ) ) &&
1677
- ( ! $this->is_enable_anonymous() ||
1678
- ( ! $this->is_anonymous() && ! $this->is_pending_activation() ) )
1679
- );
1680
- }
1681
-
1682
- /**
1683
- * Check if current page is the opt-in/pending-activation page.
1684
- *
1685
- * @author Vova Feldman (@svovaf)
1686
- * @since 1.2.1.7
1687
- *
1688
- * @return bool
1689
- */
1690
- function is_activation_page() {
1691
- if ( $this->_menu->is_main_settings_page() ) {
1692
- return true;
1693
- }
1694
-
1695
- if ( ! $this->is_activation_mode() ) {
1696
- return false;
1697
- }
1698
-
1699
- // Check if current page is matching the activation page.
1700
- return $this->is_matching_url( $this->get_activation_url() );
1701
- }
1702
-
1703
- /**
1704
- * Check if URL path's are matching and that all querystring
1705
- * arguments of the $sub_url exist in the $url with the same values.
1706
- *
1707
- * WARNING:
1708
- * 1. This method doesn't check if the sub/domain are matching.
1709
- * 2. Ignore case sensitivity.
1710
- *
1711
- * @author Vova Feldman (@svovaf)
1712
- * @since 1.2.1.7
1713
- *
1714
- * @param string $sub_url
1715
- * @param string $url If argument is not set, check if the sub_url matching the current's page URL.
1716
- *
1717
- * @return bool
1718
- */
1719
- private function is_matching_url( $sub_url, $url = '' ) {
1720
- if ( empty( $url ) ) {
1721
- $url = $_SERVER['REQUEST_URI'];
1722
- }
1723
-
1724
- $url = strtolower( $url );
1725
- $sub_url = strtolower( $sub_url );
1726
-
1727
- if ( parse_url( $sub_url, PHP_URL_PATH ) !== parse_url( $url, PHP_URL_PATH ) ) {
1728
- // Different path - DO NOT OVERRIDE PAGE.
1729
- return false;
1730
- }
1731
-
1732
- $url_params = array();
1733
- parse_str( parse_url( $url, PHP_URL_QUERY ), $url_params );
1734
-
1735
- $sub_url_params = array();
1736
- parse_str( parse_url( $sub_url, PHP_URL_QUERY ), $sub_url_params );
1737
-
1738
- foreach ( $sub_url_params as $key => $val ) {
1739
- if ( ! isset( $url_params[ $key ] ) || $val != $url_params[ $key ] ) {
1740
- // Not matching query string - DO NOT OVERRIDE PAGE.
1741
- return false;
1742
- }
1743
- }
1744
-
1745
- return true;
1746
- }
1747
-
1748
- /**
1749
- * Get collection of all active plugins.
1750
- *
1751
- * @author Vova Feldman (@svovaf)
1752
- * @since 1.0.9
1753
- *
1754
- * @return array[string]array
1755
- */
1756
- private static function get_active_plugins() {
1757
- self::require_plugin_essentials();
1758
-
1759
- $active_plugin = array();
1760
- $all_plugins = get_plugins();
1761
- $active_plugins_basenames = get_option( 'active_plugins' );
1762
-
1763
- foreach ( $active_plugins_basenames as $plugin_basename ) {
1764
- $active_plugin[ $plugin_basename ] = $all_plugins[ $plugin_basename ];
1765
- }
1766
-
1767
- return $active_plugin;
1768
- }
1769
-
1770
- /**
1771
- * Get collection of all plugins.
1772
- *
1773
- * @author Vova Feldman (@svovaf)
1774
- * @since 1.1.8
1775
- *
1776
- * @return array Key is the plugin file path and the value is an array of the plugin data.
1777
- */
1778
- private static function get_all_plugins() {
1779
- self::require_plugin_essentials();
1780
-
1781
- $all_plugins = get_plugins();
1782
- $active_plugins_basenames = get_option( 'active_plugins' );
1783
-
1784
- foreach ( $all_plugins as $basename => &$data ) {
1785
- // By default set to inactive (next foreach update the active plugins).
1786
- $data['is_active'] = false;
1787
- // Enrich with plugin slug.
1788
- $data['slug'] = self::get_plugin_slug( $basename );
1789
- }
1790
-
1791
- // Flag active plugins.
1792
- foreach ( $active_plugins_basenames as $basename ) {
1793
- if ( isset( $all_plugins[ $basename ] ) ) {
1794
- $all_plugins[ $basename ]['is_active'] = true;
1795
- }
1796
- }
1797
-
1798
- return $all_plugins;
1799
- }
1800
-
1801
-
1802
- /**
1803
- * Cached result of get_site_transient( 'update_plugins' )
1804
- *
1805
- * @author Vova Feldman (@svovaf)
1806
- * @since 1.1.8
1807
- *
1808
- * @var object
1809
- */
1810
- private static $_plugins_info;
1811
-
1812
- /**
1813
- * Helper function to get specified plugin's slug.
1814
- *
1815
- * @author Vova Feldman (@svovaf)
1816
- * @since 1.1.8
1817
- *
1818
- * @param $basename
1819
- *
1820
- * @return string
1821
- */
1822
- private static function get_plugin_slug( $basename ) {
1823
- if ( ! isset( self::$_plugins_info ) ) {
1824
- self::$_plugins_info = get_site_transient( 'update_plugins' );
1825
- }
1826
-
1827
- $slug = '';
1828
-
1829
- if ( is_object( self::$_plugins_info ) ) {
1830
- if ( isset( self::$_plugins_info->no_update ) &&
1831
- isset( self::$_plugins_info->no_update[ $basename ] ) &&
1832
- ! empty( self::$_plugins_info->no_update[ $basename ]->slug )
1833
- ) {
1834
- $slug = self::$_plugins_info->no_update[ $basename ]->slug;
1835
- } else if ( isset( self::$_plugins_info->response ) &&
1836
- isset( self::$_plugins_info->response[ $basename ] ) &&
1837
- ! empty( self::$_plugins_info->response[ $basename ]->slug )
1838
- ) {
1839
- $slug = self::$_plugins_info->response[ $basename ]->slug;
1840
- }
1841
- }
1842
-
1843
- if ( empty( $slug ) ) {
1844
- // Try to find slug from FS data.
1845
- $slug = self::find_slug_by_basename( $basename );
1846
- }
1847
-
1848
- if ( empty( $slug ) ) {
1849
- // Fallback to plugin's folder name.
1850
- $slug = dirname( $basename );
1851
- }
1852
-
1853
- return $slug;
1854
- }
1855
-
1856
- private static $_statics_loaded = false;
1857
-
1858
- /**
1859
- * Load static resources.
1860
- *
1861
- * @author Vova Feldman (@svovaf)
1862
- * @since 1.0.1
1863
- */
1864
- private static function _load_required_static() {
1865
- if ( self::$_statics_loaded ) {
1866
- return;
1867
- }
1868
-
1869
- self::$_static_logger = FS_Logger::get_logger( WP_FS__SLUG, WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
1870
-
1871
- self::$_static_logger->entrance();
1872
-
1873
- self::$_accounts = FS_Option_Manager::get_manager( WP_FS__ACCOUNTS_OPTION_NAME, true );
1874
-
1875
- self::$_global_admin_notices = FS_Admin_Notice_Manager::instance( 'global' );
1876
-
1877
- add_action( 'admin_menu', array( 'Freemius', '_add_debug_section' ) );
1878
-
1879
- add_action( "wp_ajax_fs_toggle_debug_mode", array( 'Freemius', '_toggle_debug_mode' ) );
1880
-
1881
- self::add_ajax_action_static( 'get_debug_log', array( 'Freemius', '_get_debug_log' ) );
1882
-
1883
- self::add_ajax_action_static( 'get_db_option', array( 'Freemius', '_get_db_option' ) );
1884
-
1885
- self::add_ajax_action_static( 'set_db_option', array( 'Freemius', '_set_db_option' ) );
1886
-
1887
- if ( 0 == did_action( 'plugins_loaded' ) ) {
1888
- add_action( 'plugins_loaded', array( 'Freemius', '_load_textdomain' ), 1 );
1889
- }
1890
-
1891
- self::$_statics_loaded = true;
1892
- }
1893
-
1894
- #----------------------------------------------------------------------------------
1895
- #region Localization
1896
- #----------------------------------------------------------------------------------
1897
-
1898
- /**
1899
- * Load framework's text domain.
1900
- *
1901
- * @author Vova Feldman (@svovaf)
1902
- * @since 1.2.1
1903
- */
1904
- static function _load_textdomain() {
1905
- if ( ! is_admin() ) {
1906
- return;
1907
- }
1908
-
1909
- global $fs_active_plugins;
1910
-
1911
- // Works both for plugins and themes.
1912
- load_plugin_textdomain(
1913
- 'freemius',
1914
- false,
1915
- $fs_active_plugins->newest->sdk_path . '/languages/'
1916
- );
1917
- }
1918
-
1919
- #endregion
1920
-
1921
- #----------------------------------------------------------------------------------
1922
- #region Debugging
1923
- #----------------------------------------------------------------------------------
1924
-
1925
- /**
1926
- * @author Vova Feldman (@svovaf)
1927
- * @since 1.0.8
1928
- */
1929
- static function _add_debug_section() {
1930
- if ( ! current_user_can( 'activate_plugins' )
1931
- && ! current_user_can( 'switch_themes' )
1932
- ) {
1933
- return;
1934
- }
1935
-
1936
- self::$_static_logger->entrance();
1937
-
1938
- $title = sprintf( '%s [v.%s]', fs_text_inline( 'Freemius Debug' ), WP_FS__SDK_VERSION );
1939
-
1940
- if ( WP_FS__DEV_MODE ) {
1941
- // Add top-level debug menu item.
1942
- $hook = FS_Admin_Menu_Manager::add_page(
1943
- $title,
1944
- $title,
1945
- 'manage_options',
1946
- 'freemius',
1947
- array( 'Freemius', '_debug_page_render' )
1948
- );
1949
- } else {
1950
- // Add hidden debug page.
1951
- $hook = FS_Admin_Menu_Manager::add_subpage(
1952
- null,
1953
- $title,
1954
- $title,
1955
- 'manage_options',
1956
- 'freemius',
1957
- array( 'Freemius', '_debug_page_render' )
1958
- );
1959
- }
1960
-
1961
- if ( ! empty( $hook ) ) {
1962
- add_action( "load-$hook", array( 'Freemius', '_debug_page_actions' ) );
1963
- }
1964
- }
1965
-
1966
- /**
1967
- * @author Vova Feldman (@svovaf)
1968
- * @since 1.1.7.3
1969
- */
1970
- static function _toggle_debug_mode() {
1971
- $is_on = fs_request_get( 'is_on', false, 'post' );
1972
-
1973
- if ( fs_request_is_post() && in_array( $is_on, array( 0, 1 ) ) ) {
1974
- update_option( 'fs_debug_mode', $is_on );
1975
-
1976
- // Turn on/off storage logging.
1977
- FS_Logger::_set_storage_logging( ( 1 == $is_on ) );
1978
- }
1979
-
1980
- exit;
1981
- }
1982
-
1983
- /**
1984
- * @author Vova Feldman (@svovaf)
1985
- * @since 1.2.1.6
1986
- */
1987
- static function _get_debug_log() {
1988
- $logs = FS_Logger::load_db_logs(
1989
- fs_request_get( 'filters', false, 'post' ),
1990
- ! empty( $_POST['limit'] ) && is_numeric( $_POST['limit'] ) ? $_POST['limit'] : 200,
1991
- ! empty( $_POST['offset'] ) && is_numeric( $_POST['offset'] ) ? $_POST['offset'] : 0
1992
- );
1993
-
1994
- self::shoot_ajax_success( $logs );
1995
- }
1996
-
1997
- /**
1998
- * @author Vova Feldman (@svovaf)
1999
- * @since 1.2.1.7
2000
- */
2001
- static function _get_db_option() {
2002
- $option_name = fs_request_get( 'option_name' );
2003
-
2004
- $value = get_option( $option_name );
2005
-
2006
- $result = array(
2007
- 'name' => $option_name,
2008
- );
2009
-
2010
- if ( false !== $value ) {
2011
- if ( ! is_string( $value ) ) {
2012
- $value = json_encode( $value );
2013
- }
2014
-
2015
- $result['value'] = $value;
2016
- }
2017
-
2018
- self::shoot_ajax_success( $result );
2019
- }
2020
-
2021
- /**
2022
- * @author Vova Feldman (@svovaf)
2023
- * @since 1.2.1.7
2024
- */
2025
- static function _set_db_option() {
2026
- $option_name = fs_request_get( 'option_name' );
2027
- $option_value = fs_request_get( 'option_value' );
2028
-
2029
- if ( ! empty( $option_value ) ) {
2030
- update_option( $option_name, $option_value );
2031
- }
2032
-
2033
- self::shoot_ajax_success();
2034
- }
2035
-
2036
- /**
2037
- * @author Vova Feldman (@svovaf)
2038
- * @since 1.0.8
2039
- */
2040
- static function _debug_page_actions() {
2041
- self::_clean_admin_content_section();
2042
-
2043
- if ( fs_request_is_action( 'restart_freemius' ) ) {
2044
- check_admin_referer( 'restart_freemius' );
2045
-
2046
- // Clear accounts data.
2047
- self::$_accounts->clear( true );
2048
-
2049
- // Clear SDK reference cache.
2050
- delete_option( 'fs_active_plugins' );
2051
- } else if ( fs_request_is_action( 'simulate_trial' ) ) {
2052
- check_admin_referer( 'simulate_trial' );
2053
-
2054
- $fs = freemius( fs_request_get( 'module_id' ) );
2055
-
2056
- // Update SDK install to at least 24 hours before.
2057
- $fs->_storage->install_timestamp = ( time() - WP_FS__TIME_24_HOURS_IN_SEC );
2058
- // Unset the trial shown timestamp.
2059
- unset( $fs->_storage->trial_promotion_shown );
2060
- } else if ( fs_request_is_action( 'delete_install' ) ) {
2061
- check_admin_referer( 'delete_install' );
2062
-
2063
- self::_delete_site_by_slug(
2064
- fs_request_get( 'slug' ),
2065
- fs_request_get( 'module_type' )
2066
- );
2067
- } else if ( fs_request_is_action( 'download_logs' ) ) {
2068
- check_admin_referer( 'download_logs' );
2069
-
2070
- $download_url = FS_Logger::download_db_logs(
2071
- fs_request_get( 'filters', false, 'post' )
2072
- );
2073
-
2074
- if ( false === $download_url ) {
2075
- wp_die( 'Oops... there was an error while generating the logs download file. Please try again and if it doesn\'t work contact support@freemius.com.' );
2076
- }
2077
-
2078
- fs_redirect( $download_url );
2079
- }
2080
- }
2081
-
2082
- /**
2083
- * @author Vova Feldman (@svovaf)
2084
- * @since 1.0.8
2085
- */
2086
- static function _debug_page_render() {
2087
- self::$_static_logger->entrance();
2088
-
2089
- $vars = array(
2090
- 'plugin_sites' => self::get_all_sites(),
2091
- 'theme_sites' => self::get_all_sites( WP_FS__MODULE_TYPE_THEME ),
2092
- 'users' => self::get_all_users(),
2093
- 'addons' => self::get_all_addons(),
2094
- 'account_addons' => self::get_all_account_addons(),
2095
- 'plugin_licenses' => self::get_all_licenses(),
2096
- 'theme_licenses' => self::get_all_licenses( WP_FS__MODULE_TYPE_THEME )
2097
- );
2098
-
2099
- fs_enqueue_local_style( 'fs_debug', '/admin/debug.css' );
2100
- fs_require_once_template( 'debug.php', $vars );
2101
- }
2102
-
2103
- #endregion
2104
-
2105
- #----------------------------------------------------------------------------------
2106
- #region Connectivity Issues
2107
- #----------------------------------------------------------------------------------
2108
-
2109
- /**
2110
- * Check if Freemius should be turned on for the current plugin install.
2111
- *
2112
- * Note:
2113
- * $this->_is_on is updated in has_api_connectivity()
2114
- *
2115
- * @author Vova Feldman (@svovaf)
2116
- * @since 1.0.9
2117
- *
2118
- * @return bool
2119
- */
2120
- function is_on() {
2121
- self::$_static_logger->entrance();
2122
-
2123
- if ( isset( $this->_is_on ) ) {
2124
- return $this->_is_on;
2125
- }
2126
-
2127
- // If already installed or pending then sure it's on :)
2128
- if ( $this->is_registered() || $this->is_pending_activation() ) {
2129
- $this->_is_on = true;
2130
-
2131
- return true;
2132
- }
2133
-
2134
- return false;
2135
- }
2136
-
2137
- /**
2138
- * @author Vova Feldman (@svovaf)
2139
- * @since 1.1.7.3
2140
- *
2141
- * @param bool $flush_if_no_connectivity
2142
- *
2143
- * @return bool
2144
- */
2145
- private function should_run_connectivity_test( $flush_if_no_connectivity = false ) {
2146
- if ( ! isset( $this->_storage->connectivity_test ) ) {
2147
- // Connectivity test was never executed, or cache was cleared.
2148
- return true;
2149
- }
2150
-
2151
- if ( WP_FS__PING_API_ON_IP_OR_HOST_CHANGES ) {
2152
- if ( WP_FS__IS_HTTP_REQUEST ) {
2153
- if ( $_SERVER['HTTP_HOST'] != $this->_storage->connectivity_test['host'] ) {
2154
- // Domain changed.
2155
- return true;
2156
- }
2157
-
2158
- if ( WP_FS__REMOTE_ADDR != $this->_storage->connectivity_test['server_ip'] ) {
2159
- // Server IP changed.
2160
- return true;
2161
- }
2162
- }
2163
- }
2164
-
2165
- if ( $this->_storage->connectivity_test['is_connected'] &&
2166
- $this->_storage->connectivity_test['is_active']
2167
- ) {
2168
- // API connected and Freemius is active - no need to run connectivity check.
2169
- return false;
2170
- }
2171
-
2172
- if ( $flush_if_no_connectivity ) {
2173
- /**
2174
- * If explicitly asked to flush when no connectivity - do it only
2175
- * if at least 10 sec passed from the last API connectivity test.
2176
- */
2177
- return ( isset( $this->_storage->connectivity_test['timestamp'] ) &&
2178
- ( WP_FS__SCRIPT_START_TIME - $this->_storage->connectivity_test['timestamp'] ) > 10 );
2179
- }
2180
-
2181
- /**
2182
- * @since 1.1.7 Don't check for connectivity on plugin downgrade.
2183
- */
2184
- $version = $this->get_plugin_version();
2185
- if ( version_compare( $version, $this->_storage->connectivity_test['version'], '>' ) ) {
2186
- // If it's a plugin version upgrade and Freemius is off or no connectivity, run connectivity test.
2187
- return true;
2188
- }
2189
-
2190
- return false;
2191
- }
2192
-
2193
- /**
2194
- * @author Vova Feldman (@svovaf)
2195
- * @since 1.1.7.4
2196
- *
2197
- * @return object|false
2198
- */
2199
- private function ping() {
2200
- if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY ) {
2201
- return false;
2202
- }
2203
-
2204
- $version = $this->get_plugin_version();
2205
-
2206
- $is_update = $this->apply_filters( 'is_plugin_update', $this->is_plugin_update() );
2207
-
2208
- return $this->get_api_plugin_scope()->ping(
2209
- $this->get_anonymous_id(),
2210
- array(
2211
- 'is_update' => json_encode( $is_update ),
2212
- 'version' => $version,
2213
- 'sdk' => $this->version,
2214
- 'is_admin' => json_encode( is_admin() ),
2215
- 'is_ajax' => json_encode( self::is_ajax() ),
2216
- 'is_cron' => json_encode( self::is_cron() ),
2217
- 'is_http' => json_encode( WP_FS__IS_HTTP_REQUEST ),
2218
- )
2219
- );
2220
- }
2221
-
2222
- /**
2223
- * Check if there's any connectivity issue to Freemius API.
2224
- *
2225
- * @author Vova Feldman (@svovaf)
2226
- * @since 1.0.9
2227
- *
2228
- * @param bool $flush_if_no_connectivity
2229
- *
2230
- * @return bool
2231
- */
2232
- function has_api_connectivity( $flush_if_no_connectivity = false ) {
2233
- $this->_logger->entrance();
2234
-
2235
- if ( isset( $this->_has_api_connection ) && ( $this->_has_api_connection || ! $flush_if_no_connectivity ) ) {
2236
- return $this->_has_api_connection;
2237
- }
2238
-
2239
- if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY &&
2240
- isset( $this->_storage->connectivity_test ) &&
2241
- true === $this->_storage->connectivity_test['is_connected']
2242
- ) {
2243
- unset( $this->_storage->connectivity_test );
2244
- }
2245
-
2246
- if ( ! $this->should_run_connectivity_test( $flush_if_no_connectivity ) ) {
2247
- $this->_has_api_connection = $this->_storage->connectivity_test['is_connected'];
2248
- /**
2249
- * @since 1.1.6 During dev mode, if there's connectivity - turn Freemius on regardless the configuration.
2250
- *
2251
- * @since 1.2.1.5 If the user running the premium version then ignore the 'is_active' flag and turn Freemius on to enable license key activation.
2252
- */
2253
- $this->_is_on = $this->_storage->connectivity_test['is_active'] ||
2254
- $this->is_premium() ||
2255
- ( WP_FS__DEV_MODE && $this->_has_api_connection && ! WP_FS__SIMULATE_FREEMIUS_OFF );
2256
-
2257
- return $this->_has_api_connection;
2258
- }
2259
-
2260
- $pong = $this->ping();
2261
- $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
2262
-
2263
- if ( ! $is_connected ) {
2264
- // API failure.
2265
- $this->_add_connectivity_issue_message( $pong );
2266
- }
2267
-
2268
- $this->store_connectivity_info( $pong, $is_connected );
2269
-
2270
- return $this->_has_api_connection;
2271
- }
2272
-
2273
- /**
2274
- * @author Vova Feldman (@svovaf)
2275
- * @since 1.1.7.4
2276
- *
2277
- * @param object $pong
2278
- * @param bool $is_connected
2279
- */
2280
- private function store_connectivity_info( $pong, $is_connected ) {
2281
- $this->_logger->entrance();
2282
-
2283
- $version = $this->get_plugin_version();
2284
-
2285
- if ( ! $is_connected || WP_FS__SIMULATE_FREEMIUS_OFF ) {
2286
- $is_active = false;
2287
- } else {
2288
- $is_active = ( isset( $pong->is_active ) && true == $pong->is_active );
2289
- }
2290
-
2291
- $is_active = $this->apply_filters(
2292
- 'is_on',
2293
- $is_active,
2294
- $this->is_plugin_update(),
2295
- $version
2296
- );
2297
-
2298
- $this->_storage->connectivity_test = array(
2299
- 'is_connected' => $is_connected,
2300
- 'host' => $_SERVER['HTTP_HOST'],
2301
- 'server_ip' => WP_FS__REMOTE_ADDR,
2302
- 'is_active' => $is_active,
2303
- 'timestamp' => WP_FS__SCRIPT_START_TIME,
2304
- // Last version with connectivity attempt.
2305
- 'version' => $version,
2306
- );
2307
-
2308
- $this->_has_api_connection = $is_connected;
2309
- $this->_is_on = $is_active || ( WP_FS__DEV_MODE && $is_connected && ! WP_FS__SIMULATE_FREEMIUS_OFF );
2310
- }
2311
-
2312
- /**
2313
- * Force turning Freemius on.
2314
- *
2315
- * @author Vova Feldman (@svovaf)
2316
- * @since 1.1.8.1
2317
- *
2318
- * @return bool TRUE if successfully turned on.
2319
- */
2320
- private function turn_on() {
2321
- $this->_logger->entrance();
2322
-
2323
- if ( $this->is_on() || ! isset( $this->_storage->connectivity_test['is_active'] ) ) {
2324
- return false;
2325
- }
2326
-
2327
- $updated_connectivity = $this->_storage->connectivity_test;
2328
- $updated_connectivity['is_active'] = true;
2329
- $updated_connectivity['timestamp'] = WP_FS__SCRIPT_START_TIME;
2330
- $this->_storage->connectivity_test = $updated_connectivity;
2331
-
2332
- $this->_is_on = true;
2333
-
2334
- return true;
2335
- }
2336
-
2337
- /**
2338
- * Anonymous and unique site identifier (Hash).
2339
- *
2340
- * @author Vova Feldman (@svovaf)
2341
- * @since 1.1.0
2342
- *
2343
- * @return string
2344
- */
2345
- function get_anonymous_id() {
2346
- $unique_id = self::$_accounts->get_option( 'unique_id' );
2347
 
2348
- if ( empty( $unique_id ) || ! is_string( $unique_id ) ) {
2349
- $key = get_site_url();
 
 
2350
 
2351
- // If localhost, assign microtime instead of domain.
2352
- if ( WP_FS__IS_LOCALHOST ||
2353
- false !== strpos( $key, 'localhost' ) ||
2354
- false === strpos( $key, '.' )
2355
- ) {
2356
- $key = microtime();
2357
- }
2358
 
 
2359
  /**
2360
- * Base the unique identifier on the WP secure authentication key. Which
2361
- * turns the key into a secret anonymous identifier.
 
 
2362
  *
2363
  * @author Vova Feldman (@svovaf)
2364
- * @since 1.2.3
2365
  */
2366
- $unique_id = md5( $key . SECURE_AUTH_KEY );
 
 
 
 
2367
 
2368
- self::$_accounts->set_option( 'unique_id', $unique_id, true );
 
 
2369
  }
2370
 
2371
- $this->_logger->departure( $unique_id );
2372
 
2373
- return $unique_id;
2374
- }
2375
 
2376
- /**
2377
- * @author Vova Feldman (@svovaf)
2378
- * @since 1.1.7.4
2379
- *
2380
- * @return \WP_User
2381
- */
2382
- static function _get_current_wp_user() {
2383
- self::require_pluggable_essentials();
2384
-
2385
- return wp_get_current_user();
2386
- }
2387
-
2388
- /**
2389
- * @author Vova Feldman (@svovaf)
2390
- * @since 1.2.1.7
2391
- *
2392
- * @param string $email
2393
- *
2394
- * @return bool
2395
- */
2396
- static function is_valid_email( $email ) {
2397
- if ( false === filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
2398
- return false;
2399
- }
2400
-
2401
- $parts = explode( '@', $email );
2402
-
2403
- if ( 2 !== count( $parts ) || empty( $parts[1] ) ) {
2404
- return false;
2405
- }
2406
-
2407
- $blacklist = array(
2408
- 'admin.',
2409
- 'webmaster.',
2410
- 'localhost.',
2411
- 'dev.',
2412
- 'development.',
2413
- 'test.',
2414
- 'stage.',
2415
- 'staging.',
2416
- );
2417
-
2418
- // Make sure domain is not one of the blacklisted.
2419
- foreach ( $blacklist as $invalid ) {
2420
- if ( 0 === strpos( $parts[1], $invalid ) ) {
2421
- return false;
2422
- }
2423
- }
2424
-
2425
- // Get the UTF encoded domain name.
2426
- $domain = idn_to_ascii( $parts[1] ) . '.';
2427
-
2428
- return ( checkdnsrr( $domain, 'MX' ) || checkdnsrr( $domain, 'A' ) );
2429
- }
2430
-
2431
- /**
2432
- * Generate API connectivity issue message.
2433
- *
2434
- * @author Vova Feldman (@svovaf)
2435
- * @since 1.0.9
2436
- *
2437
- * @param mixed $api_result
2438
- * @param bool $is_first_failure
2439
- */
2440
- function _add_connectivity_issue_message( $api_result, $is_first_failure = true ) {
2441
- if ( ! $this->is_premium() && $this->_enable_anonymous ) {
2442
- // Don't add message if it's the free version and can run anonymously.
2443
- return;
2444
- }
2445
-
2446
- if ( ! function_exists( 'wp_nonce_url' ) ) {
2447
- require_once ABSPATH . 'wp-includes/functions.php';
2448
- }
2449
-
2450
- $current_user = self::_get_current_wp_user();
2451
- // $admin_email = get_option( 'admin_email' );
2452
- $admin_email = $current_user->user_email;
2453
-
2454
- // Aliases.
2455
- $deactivate_plugin_title = $this->esc_html_inline( 'That\'s exhausting, please deactivate', 'deactivate-plugin-title' );
2456
- $deactivate_plugin_desc = $this->esc_html_inline( 'We feel your frustration and sincerely apologize for the inconvenience. Hope to see you again in the future.', 'deactivate-plugin-desc' );
2457
- $install_previous_title = $this->esc_html_inline( 'Let\'s try your previous version', 'install-previous-title' );
2458
- $install_previous_desc = $this->esc_html_inline( 'Uninstall this version and install the previous one.', 'install-previous-desc' );
2459
- $fix_issue_title = $this->esc_html_inline( 'Yes - I\'m giving you a chance to fix it', 'fix-issue-title' );
2460
- $fix_issue_desc = $this->esc_html_inline( 'We will do our best to whitelist your server and resolve this issue ASAP. You will get a follow-up email to %s once we have an update.', 'fix-issue-desc' );
2461
- /* translators: %s: product title (e.g. "Awesome Plugin" requires an access to...) */
2462
- $x_requires_access_to_api = $this->esc_html_inline( '%s requires an access to our API.', 'x-requires-access-to-api' );
2463
- $sysadmin_title = $this->esc_html_inline( 'I\'m a system administrator', 'sysadmin-title' );
2464
- $happy_to_resolve_issue_asap = $this->esc_html_inline( 'We are sure it\'s an issue on our side and more than happy to resolve it for you ASAP if you give us a chance.', 'happy-to-resolve-issue-asap' );
2465
 
2466
- $message = false;
2467
- if ( is_object( $api_result ) &&
2468
- isset( $api_result->error ) &&
2469
- isset( $api_result->error->code )
2470
- ) {
2471
- switch ( $api_result->error->code ) {
2472
- case 'curl_missing':
2473
- $missing_methods = '';
2474
- if ( is_array( $api_result->missing_methods ) &&
2475
- ! empty( $api_result->missing_methods )
2476
- ) {
2477
- foreach ( $api_result->missing_methods as $m ) {
2478
- if ( 'curl_version' === $m ) {
2479
- continue;
2480
- }
2481
-
2482
- if ( ! empty( $missing_methods ) ) {
2483
- $missing_methods .= ', ';
2484
- }
2485
-
2486
- $missing_methods .= sprintf( '<code>%s</code>', $m );
2487
- }
2488
-
2489
- if ( ! empty( $missing_methods ) ) {
2490
- $missing_methods = sprintf(
2491
- '<br><br><b>%s</b> %s',
2492
- $this->esc_html_inline( 'Disabled method(s):', 'curl-disabled-methods' ),
2493
- $missing_methods
2494
- );
2495
- }
2496
- }
2497
-
2498
- $message = sprintf(
2499
- $x_requires_access_to_api . ' ' .
2500
- $this->esc_html_inline( 'We use PHP cURL library for the API calls, which is a very common library and usually installed and activated out of the box. Unfortunately, cURL is not activated (or disabled) on your server.', 'curl-missing-message' ) . ' ' .
2501
- $missing_methods .
2502
- ' %s',
2503
- '<b>' . $this->get_plugin_name() . '</b>',
2504
- sprintf(
2505
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2506
- sprintf(
2507
- '<a class="fs-resolve" data-type="curl" href="#"><b>%s</b></a>%s',
2508
- $this->get_text_inline( 'I don\'t know what is cURL or how to install it, help me!', 'curl-missing-no-clue-title' ),
2509
- ' - ' . sprintf(
2510
- $this->get_text_inline( 'We\'ll make sure to contact your hosting company and resolve the issue. You will get a follow-up email to %s once we have an update.', 'curl-missing-no-clue-desc' ),
2511
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2512
- )
2513
- ),
2514
- sprintf(
2515
- '<b>%s</b> - %s',
2516
- $sysadmin_title,
2517
- esc_html( sprintf( $this->get_text_inline( 'Great, please install cURL and enable it in your php.ini file. In addition, search for the \'disable_functions\' directive in your php.ini file and remove any disabled methods starting with \'curl_\'. To make sure it was successfully activated, use \'phpinfo()\'. Once activated, deactivate the %s and reactivate it back again.', 'curl-missing-sysadmin-desc' ), $this->get_module_label( true ) ) )
2518
- ),
2519
- sprintf(
2520
- '<a href="%s"><b>%s</b></a> - %s',
2521
- wp_nonce_url( 'plugins.php?action=deactivate&amp;plugin=' . $this->_plugin_basename . '&amp;plugin_status=all&amp;paged=1&amp;s=', 'deactivate-plugin_' . $this->_plugin_basename ),
2522
- $deactivate_plugin_title,
2523
- $deactivate_plugin_desc
2524
- )
2525
- )
2526
- );
2527
- break;
2528
- case 'cloudflare_ddos_protection':
2529
- $message = sprintf(
2530
- $x_requires_access_to_api . ' ' .
2531
- $this->esc_html_inline( 'From unknown reason, CloudFlare, the firewall we use, blocks the connection.', 'cloudflare-blocks-connection-message' ) . ' ' .
2532
- $happy_to_resolve_issue_asap .
2533
- ' %s',
2534
- '<b>' . $this->get_plugin_name() . '</b>',
2535
- sprintf(
2536
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2537
- sprintf(
2538
- '<a class="fs-resolve" data-type="cloudflare" href="#"><b>%s</b></a>%s',
2539
- $fix_issue_title,
2540
- ' - ' . sprintf(
2541
- $fix_issue_desc,
2542
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2543
- )
2544
- ),
2545
- sprintf(
2546
- '<a href="%s" target="_blank"><b>%s</b></a> - %s',
2547
- sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),
2548
- $install_previous_title,
2549
- $install_previous_desc
2550
- ),
2551
- sprintf(
2552
- '<a href="%s"><b>%s</b></a> - %s',
2553
- wp_nonce_url( 'plugins.php?action=deactivate&amp;plugin=' . $this->_plugin_basename . '&amp;plugin_status=all&amp;paged=1&amp;s=' . '', 'deactivate-plugin_' . $this->_plugin_basename ),
2554
- $deactivate_plugin_title,
2555
- $deactivate_plugin_desc
2556
- )
2557
- )
2558
- );
2559
- break;
2560
- case 'squid_cache_block':
2561
- $message = sprintf(
2562
- $x_requires_access_to_api . ' ' .
2563
- $this->esc_html_inline( 'It looks like your server is using Squid ACL (access control lists), which blocks the connection.', 'squid-blocks-connection-message' ) .
2564
- ' %s',
2565
- '<b>' . $this->get_plugin_name() . '</b>',
2566
- sprintf(
2567
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2568
- sprintf(
2569
- '<a class="fs-resolve" data-type="squid" href="#"><b>%s</b></a> - %s',
2570
- $this->esc_html_inline( 'I don\'t know what is Squid or ACL, help me!', 'squid-no-clue-title' ),
2571
- sprintf(
2572
- $this->esc_html_inline( 'We\'ll make sure to contact your hosting company and resolve the issue. You will get a follow-up email to %s once we have an update.', 'squid-no-clue-desc' ),
2573
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2574
- )
2575
- ),
2576
- sprintf(
2577
- '<b>%s</b> - %s',
2578
- $sysadmin_title,
2579
- sprintf(
2580
- $this->esc_html_inline( 'Great, please whitelist the following domains: %s. Once you are done, deactivate the %s and activate it again.', 'squid-sysadmin-desc' ),
2581
- // We use a filter since the plugin might require additional API connectivity.
2582
- '<b>' . implode( ', ', $this->apply_filters( 'api_domains', array( 'api.freemius.com', 'wp.freemius.com' ) ) ) . '</b>',
2583
- $this->_module_type
2584
- )
2585
- ),
2586
- sprintf(
2587
- '<a href="%s"><b>%s</b></a> - %s',
2588
- wp_nonce_url( 'plugins.php?action=deactivate&amp;plugin=' . $this->_plugin_basename . '&amp;plugin_status=all&amp;paged=1&amp;s=', 'deactivate-plugin_' . $this->_plugin_basename ),
2589
- $deactivate_plugin_title,
2590
- $deactivate_plugin_desc
2591
- )
2592
- )
2593
- );
2594
- break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2595
  // default:
2596
  // $message = $this->get_text_inline( 'connectivity-test-fails-message' );
2597
  // break;
2598
- }
2599
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2600
 
2601
- $message_id = 'failed_connect_api';
2602
- $type = 'error';
 
 
2603
 
2604
- $connectivity_test_fails_message = $this->esc_html_inline( 'From unknown reason, the API connectivity test failed.', 'connectivity-test-fails-message' );
 
 
 
2605
 
2606
- if ( false === $message ) {
2607
- if ( $is_first_failure ) {
2608
- // First attempt failed.
2609
- $message = sprintf(
2610
- $x_requires_access_to_api . ' ' .
2611
- $connectivity_test_fails_message . ' ' .
2612
- $this->esc_html_inline( 'It\'s probably a temporary issue on our end. Just to be sure, with your permission, would it be o.k to run another connectivity test?', 'connectivity-test-maybe-temporary' ) . '<br><br>' .
2613
- '%s',
2614
- '<b>' . $this->get_plugin_name() . '</b>',
2615
- sprintf(
2616
- '<div id="fs_firewall_issue_options">%s %s</div>',
2617
- sprintf(
2618
- '<a class="button button-primary fs-resolve" data-type="retry_ping" href="#">%s</a>',
2619
- $this->get_text_inline( 'Yes - do your thing', 'yes-do-your-thing' )
2620
- ),
2621
- sprintf(
2622
- '<a href="%s" class="button">%s</a>',
2623
- wp_nonce_url( 'plugins.php?action=deactivate&amp;plugin=' . $this->_plugin_basename . '&amp;plugin_status=all&amp;paged=1&amp;s=', 'deactivate-plugin_' . $this->_plugin_basename ),
2624
- $this->get_text_inline( 'No - just deactivate', 'no-deactivate' )
2625
- )
2626
- )
2627
- );
2628
-
2629
- $message_id = 'failed_connect_api_first';
2630
- $type = 'promotion';
2631
- } else {
2632
- // Second connectivity attempt failed.
2633
- $message = sprintf(
2634
- $x_requires_access_to_api . ' ' .
2635
- $connectivity_test_fails_message . ' ' .
2636
- $happy_to_resolve_issue_asap .
2637
- ' %s',
2638
- '<b>' . $this->get_plugin_name() . '</b>',
2639
- sprintf(
2640
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2641
- sprintf(
2642
- '<a class="fs-resolve" data-type="general" href="#"><b>%s</b></a>%s',
2643
- $fix_issue_title,
2644
- ' - ' . sprintf(
2645
- $fix_issue_desc,
2646
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2647
- )
2648
- ),
2649
- sprintf(
2650
- '<a href="%s" target="_blank"><b>%s</b></a> - %s',
2651
- sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),
2652
- $install_previous_title,
2653
- $install_previous_desc
2654
- ),
2655
- sprintf(
2656
- '<a href="%s"><b>%s</b></a> - %s',
2657
- wp_nonce_url( 'plugins.php?action=deactivate&amp;plugin=' . $this->_plugin_basename . '&amp;plugin_status=all&amp;paged=1&amp;s=', 'deactivate-plugin_' . $this->_plugin_basename ),
2658
- $deactivate_plugin_title,
2659
- $deactivate_plugin_desc
2660
- )
2661
- )
2662
- );
2663
- }
2664
- }
2665
-
2666
- $this->_admin_notices->add_sticky(
2667
- $message,
2668
- $message_id,
2669
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
2670
- $type
2671
- );
2672
- }
2673
-
2674
- /**
2675
- * Handle user request to resolve connectivity issue.
2676
- * This method will send an email to Freemius API technical staff for resolution.
2677
- * The email will contain server's info and installed plugins (might be caching issue).
2678
- *
2679
- * @author Vova Feldman (@svovaf)
2680
- * @since 1.0.9
2681
- */
2682
- function _email_about_firewall_issue() {
2683
- $this->_admin_notices->remove_sticky( 'failed_connect_api' );
2684
-
2685
- $pong = $this->ping();
2686
-
2687
- $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
2688
-
2689
- if ( $is_connected ) {
2690
- $this->store_connectivity_info( $pong, $is_connected );
2691
-
2692
- echo $this->get_after_plugin_activation_redirect_url();
2693
- exit;
2694
- }
2695
-
2696
- $current_user = self::_get_current_wp_user();
2697
- $admin_email = $current_user->user_email;
2698
-
2699
- $error_type = fs_request_get( 'error_type', 'general' );
2700
-
2701
- switch ( $error_type ) {
2702
- case 'squid':
2703
- $title = 'Squid ACL Blocking Issue';
2704
- break;
2705
- case 'cloudflare':
2706
- $title = 'CloudFlare Blocking Issue';
2707
- break;
2708
- default:
2709
- $title = 'API Connectivity Issue';
2710
- break;
2711
- }
2712
-
2713
- $custom_email_sections = array();
2714
-
2715
- // Add 'API Error' custom email section.
2716
- $custom_email_sections['api_error'] = array(
2717
- 'title' => 'API Error',
2718
- 'rows' => array(
2719
- 'ping' => array(
2720
- 'API Error',
2721
- is_string( $pong ) ? htmlentities( $pong ) : json_encode( $pong )
2722
- ),
2723
- )
2724
- );
2725
-
2726
- // Send email with technical details to resolve API connectivity issues.
2727
- $this->send_email(
2728
- 'api@freemius.com', // recipient
2729
- $title . ' [' . $this->get_plugin_name() . ']', // subject
2730
- $custom_email_sections,
2731
- array( "Reply-To: $admin_email <$admin_email>" ) // headers
2732
- );
2733
-
2734
- $this->_admin_notices->add_sticky(
2735
- sprintf(
2736
- $this->get_text_inline( 'Thank for giving us the chance to fix it! A message was just sent to our technical staff. We will get back to you as soon as we have an update to %s. Appreciate your patience.', 'fix-request-sent-message' ),
2737
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2738
- ),
2739
- 'server_details_sent'
2740
- );
2741
-
2742
- // Action was taken, tell that API connectivity troubleshooting should be off now.
2743
-
2744
- echo "1";
2745
- exit;
2746
- }
2747
-
2748
- /**
2749
- * Handle connectivity test retry approved by the user.
2750
- *
2751
- * @author Vova Feldman (@svovaf)
2752
- * @since 1.1.7.4
2753
- */
2754
- function _retry_connectivity_test() {
2755
- $this->_admin_notices->remove_sticky( 'failed_connect_api_first' );
2756
-
2757
- $pong = $this->ping();
2758
-
2759
- $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
2760
-
2761
- if ( $is_connected ) {
2762
- $this->store_connectivity_info( $pong, $is_connected );
2763
-
2764
- echo $this->get_after_plugin_activation_redirect_url();
2765
- } else {
2766
- // Add connectivity issue message after 2nd failed attempt.
2767
- $this->_add_connectivity_issue_message( $pong, false );
2768
-
2769
- echo "1";
2770
- }
2771
-
2772
- exit;
2773
- }
2774
-
2775
- static function _add_firewall_issues_javascript() {
2776
- $params = array();
2777
- fs_require_once_template( 'firewall-issues-js.php', $params );
2778
- }
2779
-
2780
- #endregion
2781
-
2782
- #----------------------------------------------------------------------------------
2783
- #region Email
2784
- #----------------------------------------------------------------------------------
2785
-
2786
- /**
2787
- * Generates and sends an HTML email with customizable sections.
2788
- *
2789
- * @author Leo Fajardo (@leorw)
2790
- * @since 1.1.2
2791
- *
2792
- * @param string $to_address
2793
- * @param string $subject
2794
- * @param array $sections
2795
- * @param array $headers
2796
- *
2797
- * @return bool Whether the email contents were sent successfully.
2798
- */
2799
- private function send_email(
2800
- $to_address,
2801
- $subject,
2802
- $sections = array(),
2803
- $headers = array()
2804
- ) {
2805
- $default_sections = $this->get_email_sections();
2806
-
2807
- // Insert new sections or replace the default email sections.
2808
- if ( is_array( $sections ) && ! empty( $sections ) ) {
2809
- foreach ( $sections as $section_id => $custom_section ) {
2810
- if ( ! isset( $default_sections[ $section_id ] ) ) {
2811
- // If the section does not exist, add it.
2812
- $default_sections[ $section_id ] = $custom_section;
2813
- } else {
2814
- // If the section already exists, override it.
2815
- $current_section = $default_sections[ $section_id ];
2816
-
2817
- // Replace the current section's title if a custom section title exists.
2818
- if ( isset( $custom_section['title'] ) ) {
2819
- $current_section['title'] = $custom_section['title'];
2820
- }
2821
-
2822
- // Insert new rows under the current section or replace the default rows.
2823
- if ( isset( $custom_section['rows'] ) && is_array( $custom_section['rows'] ) && ! empty( $custom_section['rows'] ) ) {
2824
- foreach ( $custom_section['rows'] as $row_id => $row ) {
2825
- $current_section['rows'][ $row_id ] = $row;
2826
- }
2827
- }
2828
-
2829
- $default_sections[ $section_id ] = $current_section;
2830
- }
2831
- }
2832
- }
2833
-
2834
- $vars = array( 'sections' => $default_sections );
2835
- $message = fs_get_template( 'email.php', $vars );
2836
-
2837
- // Set the type of email to HTML.
2838
- $headers[] = 'Content-type: text/html; charset=UTF-8';
2839
-
2840
- $header_string = implode( "\r\n", $headers );
2841
-
2842
- return wp_mail(
2843
- $to_address,
2844
- $subject,
2845
- $message,
2846
- $header_string
2847
- );
2848
- }
2849
-
2850
- /**
2851
- * Generates the data for the sections of the email content.
2852
- *
2853
- * @author Leo Fajardo (@leorw)
2854
- * @since 1.1.2
2855
- *
2856
- * @return array
2857
- */
2858
- private function get_email_sections() {
2859
- // Retrieve the current user's information so that we can get the user's email, first name, and last name below.
2860
- $current_user = self::_get_current_wp_user();
2861
-
2862
- // Retrieve the cURL version information so that we can get the version number below.
2863
- $curl_version_information = curl_version();
2864
-
2865
- $active_plugin = self::get_active_plugins();
2866
-
2867
- // Generate the list of active plugins separated by new line.
2868
- $active_plugin_string = '';
2869
- foreach ( $active_plugin as $plugin ) {
2870
- $active_plugin_string .= sprintf(
2871
- '<a href="%s">%s</a> [v%s]<br>',
2872
- $plugin['PluginURI'],
2873
- $plugin['Name'],
2874
- $plugin['Version']
2875
- );
2876
- }
2877
-
2878
- $server_ip = WP_FS__REMOTE_ADDR;
2879
-
2880
- // Add PHP info for deeper investigation.
2881
- ob_start();
2882
- phpinfo();
2883
- $php_info = ob_get_clean();
2884
-
2885
- $api_domain = substr( FS_API__ADDRESS, strpos( FS_API__ADDRESS, ':' ) + 3 );
2886
-
2887
- // Generate the default email sections.
2888
- $sections = array(
2889
- 'sdk' => array(
2890
- 'title' => 'SDK',
2891
- 'rows' => array(
2892
- 'fs_version' => array( 'FS Version', $this->version ),
2893
- 'curl_version' => array( 'cURL Version', $curl_version_information['version'] )
2894
- )
2895
- ),
2896
- 'plugin' => array(
2897
- 'title' => ucfirst( $this->get_module_type() ),
2898
- 'rows' => array(
2899
- 'name' => array( 'Name', $this->get_plugin_name() ),
2900
- 'version' => array( 'Version', $this->get_plugin_version() )
2901
- )
2902
- ),
2903
- 'api' => array(
2904
- 'title' => 'API Subdomain',
2905
- 'rows' => array(
2906
- 'dns' => array(
2907
- 'DNS_CNAME',
2908
- function_exists( 'dns_get_record' ) ?
2909
- var_export( dns_get_record( $api_domain, DNS_CNAME ), true ) :
2910
- 'dns_get_record() disabled/blocked'
2911
- ),
2912
- 'ip' => array(
2913
- 'IP',
2914
- function_exists( 'gethostbyname' ) ?
2915
- gethostbyname( $api_domain ) :
2916
- 'gethostbyname() disabled/blocked'
2917
- ),
2918
- ),
2919
- ),
2920
- 'site' => array(
2921
- 'title' => 'Site',
2922
- 'rows' => array(
2923
- 'unique_id' => array( 'Unique ID', $this->get_anonymous_id() ),
2924
- 'address' => array( 'Address', site_url() ),
2925
- 'host' => array(
2926
- 'HTTP_HOST',
2927
- ( ! empty( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : '' )
2928
- ),
2929
- 'hosting' => array(
2930
- 'Hosting Company' => fs_request_has( 'hosting_company' ) ?
2931
- fs_request_get( 'hosting_company' ) :
2932
- 'Unknown',
2933
- ),
2934
- 'server_addr' => array(
2935
- 'SERVER_ADDR',
2936
- '<a href="http://www.projecthoneypot.org/ip_' . $server_ip . '">' . $server_ip . '</a>'
2937
- )
2938
- )
2939
- ),
2940
- 'user' => array(
2941
- 'title' => 'User',
2942
- 'rows' => array(
2943
- 'email' => array( 'Email', $current_user->user_email ),
2944
- 'first' => array( 'First', $current_user->user_firstname ),
2945
- 'last' => array( 'Last', $current_user->user_lastname )
2946
- )
2947
- ),
2948
- 'plugins' => array(
2949
- 'title' => 'Plugins',
2950
- 'rows' => array(
2951
- 'active_plugins' => array( 'Active Plugins', $active_plugin_string )
2952
- )
2953
- ),
2954
- 'php_info' => array(
2955
- 'title' => 'PHP Info',
2956
- 'rows' => array(
2957
- 'info' => array( $php_info )
2958
- ),
2959
- )
2960
- );
2961
-
2962
- // Allow the sections to be modified by other code.
2963
- $sections = $this->apply_filters( 'email_template_sections', $sections );
2964
-
2965
- return $sections;
2966
- }
2967
-
2968
- #endregion
2969
-
2970
- #----------------------------------------------------------------------------------
2971
- #region Initialization
2972
- #----------------------------------------------------------------------------------
2973
-
2974
- /**
2975
- * Init plugin's Freemius instance.
2976
- *
2977
- * @author Vova Feldman (@svovaf)
2978
- * @since 1.0.1
2979
- *
2980
- * @param number $id
2981
- * @param string $public_key
2982
- * @param bool $is_live
2983
- * @param bool $is_premium
2984
- */
2985
- function init( $id, $public_key, $is_live = true, $is_premium = true ) {
2986
- $this->_logger->entrance();
2987
-
2988
- $this->dynamic_init( array(
2989
- 'id' => $id,
2990
- 'public_key' => $public_key,
2991
- 'is_live' => $is_live,
2992
- 'is_premium' => $is_premium,
2993
- ) );
2994
- }
2995
-
2996
- /**
2997
- * Dynamic initiator, originally created to support initiation
2998
- * with parent_id for add-ons.
2999
- *
3000
- * @author Vova Feldman (@svovaf)
3001
- * @since 1.0.6
3002
- *
3003
- * @param array $plugin_info
3004
- *
3005
- * @throws Freemius_Exception
3006
- */
3007
- function dynamic_init( array $plugin_info ) {
3008
- $this->_logger->entrance();
3009
-
3010
- $this->parse_settings( $plugin_info );
3011
-
3012
- if ( $this->has_affiliate_program() ) {
3013
- $this->fetch_affiliate_and_terms();
3014
  }
3015
 
3016
- if ( ! self::is_ajax() ) {
3017
- if ( ! $this->is_addon() || $this->is_only_premium() ) {
3018
- add_action( 'admin_menu', array( &$this, '_prepare_admin_menu' ), WP_FS__LOWEST_PRIORITY );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3019
  }
3020
  }
3021
 
3022
- if ( $this->should_stop_execution() ) {
3023
- return;
3024
- }
3025
-
3026
- if ( ! $this->is_registered() ) {
3027
- if ( $this->is_anonymous() ) {
3028
- // If user skipped, no need to test connectivity.
3029
- $this->_has_api_connection = true;
3030
- $this->_is_on = true;
3031
- } else {
3032
- if ( ! $this->has_api_connectivity() ) {
3033
- if ( $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) ||
3034
- $this->_admin_notices->has_sticky( 'failed_connect_api' )
3035
- ) {
3036
- if ( ! $this->_enable_anonymous || $this->is_premium() ) {
3037
- // If anonymous mode is disabled, add firewall admin-notice message.
3038
- add_action( 'admin_footer', array( 'Freemius', '_add_firewall_issues_javascript' ) );
3039
-
3040
- $this->add_ajax_action( 'resolve_firewall_issues', array(
3041
- &$this,
3042
- '_email_about_firewall_issue'
3043
- ) );
3044
-
3045
- $this->add_ajax_action( 'retry_connectivity_test', array(
3046
- &$this,
3047
- '_retry_connectivity_test'
3048
- ) );
3049
- }
3050
- }
3051
-
3052
- return;
3053
- } else {
3054
- $this->_admin_notices->remove_sticky( array(
3055
- 'failed_connect_api_first',
3056
- 'failed_connect_api',
3057
- ) );
3058
-
3059
- if ( $this->_anonymous_mode ) {
3060
- // Simulate anonymous mode.
3061
- $this->_is_anonymous = true;
3062
- }
3063
- }
3064
- }
3065
-
3066
- // Check if Freemius is on for the current plugin.
3067
- // This MUST be executed after all the plugin variables has been loaded.
3068
- if ( ! $this->is_on() ) {
3069
- return;
3070
- }
3071
- }
3072
-
3073
- if ( $this->has_api_connectivity() ) {
3074
- if ( self::is_cron() ) {
3075
- $this->hook_callback_to_sync_cron();
3076
- } else if ( $this->is_user_in_admin() ) {
3077
- /**
3078
- * Schedule daily data sync cron if:
3079
- *
3080
- * 1. User opted-in (for tracking).
3081
- * 2. If skipped, but later upgraded (opted-in via upgrade).
3082
- *
3083
- * @author Vova Feldman (@svovaf)
3084
- * @since 1.1.7.3
3085
- *
3086
- */
3087
- if ( $this->is_registered() ) {
3088
- if ( ! $this->is_sync_cron_on() && $this->is_tracking_allowed() ) {
3089
- $this->schedule_sync_cron();
3090
- }
3091
- }
3092
-
3093
- /**
3094
- * Check if requested for manual blocking background sync.
3095
- */
3096
- if ( fs_request_has( 'background_sync' ) ) {
3097
- $this->run_manual_sync();
3098
- }
3099
- }
3100
- }
3101
-
3102
- if ( $this->is_registered() ) {
3103
- $this->hook_callback_to_install_sync();
3104
- }
3105
-
3106
- if ( $this->is_addon() ) {
3107
- if ( $this->is_parent_plugin_installed() ) {
3108
- // Link to parent FS.
3109
- $this->_parent = self::get_instance_by_id( $this->_plugin->parent_plugin_id );
3110
-
3111
- // Get parent plugin reference.
3112
- $this->_parent_plugin = $this->_parent->get_plugin();
3113
- }
3114
- }
3115
-
3116
- if ( $this->is_user_in_admin() ) {
3117
- if ( self::is_plugins_page() && $this->is_plugin() ) {
3118
- $this->hook_plugin_action_links();
3119
- }
3120
-
3121
- if ( $this->is_addon() ) {
3122
- if ( ! $this->is_parent_plugin_installed() ) {
3123
- $parent_name = $this->get_option( $plugin_info, 'parent_name', null );
3124
-
3125
- if ( isset( $plugin_info['parent'] ) ) {
3126
- $parent_name = $this->get_option( $plugin_info['parent'], 'name', null );
3127
- }
3128
-
3129
- $this->_admin_notices->add(
3130
- ( ! empty( $parent_name ) ?
3131
- 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 ) :
3132
- 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() )
3133
- ),
3134
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
3135
- 'error'
3136
- );
3137
-
3138
- return;
3139
- } else {
3140
- if ( $this->_parent->is_registered() && ! $this->is_registered() ) {
3141
- // If parent plugin activated, automatically install add-on for the user.
3142
- $this->_activate_addon_account( $this->_parent );
3143
- } else if ( ! $this->_parent->is_registered() && $this->is_registered() ) {
3144
- // If add-on activated and parent not, automatically install parent for the user.
3145
- $this->activate_parent_account( $this->_parent );
3146
- }
3147
-
3148
- // @todo This should be only executed on activation. It should be migrated to register_activation_hook() together with other activation related logic.
3149
- if ( $this->is_premium() ) {
3150
- // Remove add-on download admin-notice.
3151
- $this->_parent->_admin_notices->remove_sticky( array(
3152
- 'addon_plan_upgraded_' . $this->_slug,
3153
- 'no_addon_license_' . $this->_slug,
3154
- ) );
3155
- }
3156
-
3157
- // $this->deactivate_premium_only_addon_without_license();
3158
- }
3159
- } else {
3160
- if ( $this->has_addons() &&
3161
- 'plugin-information' === fs_request_get( 'tab', false ) &&
3162
- $this->get_id() == fs_request_get( 'parent_plugin_id', false )
3163
- ) {
3164
- require_once WP_FS__DIR_INCLUDES . '/fs-plugin-info-dialog.php';
3165
-
3166
- new FS_Plugin_Info_Dialog( $this );
3167
- }
3168
- }
3169
 
3170
- add_action( 'admin_init', array( &$this, '_admin_init_action' ) );
 
3171
 
3172
- // if ( $this->is_registered() ||
3173
- // $this->is_anonymous() ||
3174
- // $this->is_pending_activation()
3175
- // ) {
3176
- // $this->_init_admin();
3177
- // }
3178
- }
3179
-
3180
- /**
3181
- * Should be called outside `$this->is_user_in_admin()` scope
3182
- * because the updater has some logic that needs to be executed
3183
- * during AJAX calls.
3184
- *
3185
- * Currently we need to hook to the `http_request_host_is_external` filter.
3186
- * In the future, there might be additional logic added.
3187
- *
3188
- * @author Vova Feldman
3189
- * @since 1.2.1.6
3190
- */
3191
- if ( $this->is_premium() && $this->has_release_on_freemius() ) {
3192
- new FS_Plugin_Updater( $this );
3193
- }
3194
-
3195
- $this->do_action( 'initiated' );
3196
-
3197
- if ( $this->_storage->prev_is_premium !== $this->_plugin->is_premium ) {
3198
- if ( isset( $this->_storage->prev_is_premium ) ) {
3199
- $this->apply_filters(
3200
- 'after_code_type_change',
3201
- // New code type.
3202
- $this->_plugin->is_premium
3203
- );
3204
- } else {
3205
- // Set for code type for the first time.
3206
- $this->_storage->prev_is_premium = $this->_plugin->is_premium;
3207
- }
3208
- }
3209
-
3210
- if ( ! $this->is_addon() ) {
3211
- if ( $this->is_registered() ) {
3212
- // Fix for upgrade from versions < 1.0.9.
3213
- if ( ! isset( $this->_storage->activation_timestamp ) ) {
3214
- $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME;
3215
- }
3216
-
3217
- $this->do_action( 'after_init_plugin_registered' );
3218
- } else if ( $this->is_anonymous() ) {
3219
- $this->do_action( 'after_init_plugin_anonymous' );
3220
- } else if ( $this->is_pending_activation() ) {
3221
- $this->do_action( 'after_init_plugin_pending_activations' );
3222
- }
3223
- } else {
3224
- if ( $this->is_registered() ) {
3225
- $this->do_action( 'after_init_addon_registered' );
3226
- } else if ( $this->is_anonymous() ) {
3227
- $this->do_action( 'after_init_addon_anonymous' );
3228
- } else if ( $this->is_pending_activation() ) {
3229
- $this->do_action( 'after_init_addon_pending_activations' );
3230
- }
3231
- }
3232
- }
3233
-
3234
- /**
3235
- * @author Leo Fajardo (@leorw)
3236
- *
3237
- * @since 1.2.1.5
3238
- */
3239
- function _stop_tracking_callback() {
3240
- $this->_logger->entrance();
3241
-
3242
- $this->check_ajax_referer( 'stop_tracking' );
3243
-
3244
- $result = $this->stop_tracking();
3245
-
3246
- if ( true === $result ) {
3247
- self::shoot_ajax_success();
3248
- }
3249
-
3250
- $this->_logger->api_error( $result );
3251
-
3252
- self::shoot_ajax_failure(
3253
- sprintf( $this->get_text_inline( 'Unexpected API error. Please contact the %s\'s author with the following error.', 'unexpected-api-error' ), $this->_module_type ) .
3254
- ( $this->is_api_error( $result ) && isset( $result->error ) ?
3255
- $result->error->message :
3256
- var_export( $result, true ) )
3257
- );
3258
- }
3259
-
3260
- /**
3261
- * @author Leo Fajardo (@leorw)
3262
- * @since 1.2.1.5
3263
- */
3264
- function _allow_tracking_callback() {
3265
- $this->_logger->entrance();
3266
-
3267
- $this->check_ajax_referer( 'allow_tracking' );
3268
-
3269
- $result = $this->allow_tracking();
3270
-
3271
- if ( true === $result ) {
3272
- self::shoot_ajax_success();
3273
- }
3274
-
3275
- $this->_logger->api_error( $result );
3276
-
3277
- self::shoot_ajax_failure(
3278
- sprintf( $this->get_text_inline( 'Unexpected API error. Please contact the %s\'s author with the following error.', 'unexpected-api-error' ), $this->_module_type ) .
3279
- ( $this->is_api_error( $result ) && isset( $result->error ) ?
3280
- $result->error->message :
3281
- var_export( $result, true ) )
3282
- );
3283
- }
3284
-
3285
- /**
3286
- * Opt-out from usage tracking.
3287
- *
3288
- * Note: This will not delete the account information but will stop all tracking.
3289
- *
3290
- * Returns:
3291
- * 1. FALSE - If the user never opted-in.
3292
- * 2. TRUE - If successfully opted-out.
3293
- * 3. object - API result on failure.
3294
- *
3295
- * @author Leo Fajardo (@leorw)
3296
- * @since 1.2.1.5
3297
- *
3298
- * @return bool|object
3299
- */
3300
- function stop_tracking() {
3301
- $this->_logger->entrance();
3302
-
3303
- if ( ! $this->is_registered() ) {
3304
- // User never opted-in.
3305
- return false;
3306
- }
3307
-
3308
- if ( $this->is_tracking_prohibited() ) {
3309
- // Already disconnected.
3310
- return true;
3311
- }
3312
-
3313
- // Send update to FS.
3314
- $result = $this->get_api_site_scope()->call( '/?fields=is_disconnected', 'put', array(
3315
- 'is_disconnected' => true
3316
- ) );
3317
-
3318
- if ( ! $this->is_api_result_entity( $result ) ||
3319
- ! isset( $result->is_disconnected ) ||
3320
- ! $result->is_disconnected
3321
- ) {
3322
- $this->_logger->api_error( $result );
3323
-
3324
- return $result;
3325
- }
3326
-
3327
- $this->_site->is_disconnected = $result->is_disconnected;
3328
- $this->_store_site();
3329
-
3330
- $this->clear_sync_cron();
3331
-
3332
- // Successfully disconnected.
3333
- return true;
3334
- }
3335
-
3336
- /**
3337
- * Opt-in back into usage tracking.
3338
- *
3339
- * Note: This will only work if the user opted-in previously.
3340
- *
3341
- * Returns:
3342
- * 1. FALSE - If the user never opted-in.
3343
- * 2. TRUE - If successfully opted-in back to usage tracking.
3344
- * 3. object - API result on failure.
3345
- *
3346
- * @author Leo Fajardo (@leorw)
3347
- * @since 1.2.1.5
3348
- *
3349
- * @return bool|object
3350
- */
3351
- function allow_tracking() {
3352
- $this->_logger->entrance();
3353
-
3354
- if ( ! $this->is_registered() ) {
3355
- // User never opted-in.
3356
- return false;
3357
- }
3358
-
3359
- if ( $this->is_tracking_allowed() ) {
3360
- // Tracking already allowed.
3361
- return true;
3362
- }
3363
-
3364
- $result = $this->get_api_site_scope()->call( '/?is_disconnected', 'put', array(
3365
- 'is_disconnected' => false
3366
- ) );
3367
-
3368
- if ( ! $this->is_api_result_entity( $result ) ||
3369
- ! isset( $result->is_disconnected ) ||
3370
- $result->is_disconnected
3371
- ) {
3372
- $this->_logger->api_error( $result );
3373
-
3374
- return $result;
3375
- }
3376
-
3377
- $this->_site->is_disconnected = $result->is_disconnected;
3378
- $this->_store_site();
3379
-
3380
- $this->schedule_sync_cron();
3381
-
3382
- // Successfully reconnected.
3383
- return true;
3384
- }
3385
-
3386
- /**
3387
- * If user opted-in and later disabled usage-tracking,
3388
- * re-allow tracking for licensing and updates.
3389
- *
3390
- * @author Leo Fajardo (@leorw)
3391
- *
3392
- * @since 1.2.1.5
3393
- */
3394
- private function reconnect_locally() {
3395
- $this->_logger->entrance();
3396
-
3397
- if ( $this->is_tracking_prohibited() &&
3398
- $this->is_registered()
3399
- ) {
3400
- $this->_site->is_disconnected = false;
3401
- $this->_store_site();
3402
- }
3403
- }
3404
-
3405
- /**
3406
- * Parse plugin's settings (as defined by the plugin dev).
3407
- *
3408
- * @author Vova Feldman (@svovaf)
3409
- * @since 1.1.7.3
3410
- *
3411
- * @param array $plugin_info
3412
- *
3413
- * @throws \Freemius_Exception
3414
- */
3415
- private function parse_settings( &$plugin_info ) {
3416
- $this->_logger->entrance();
3417
-
3418
- $id = $this->get_numeric_option( $plugin_info, 'id', false );
3419
- $public_key = $this->get_option( $plugin_info, 'public_key', false );
3420
- $secret_key = $this->get_option( $plugin_info, 'secret_key', null );
3421
- $parent_id = $this->get_numeric_option( $plugin_info, 'parent_id', null );
3422
- $parent_name = $this->get_option( $plugin_info, 'parent_name', null );
3423
-
3424
- /**
3425
- * @author Vova Feldman (@svovaf)
3426
- * @since 1.1.9 Try to pull secret key from external config.
3427
- */
3428
- if ( is_null( $secret_key ) && defined( "WP_FS__{$this->_slug}_SECRET_KEY" ) ) {
3429
- $secret_key = constant( "WP_FS__{$this->_slug}_SECRET_KEY" );
3430
- }
3431
 
3432
- if ( isset( $plugin_info['parent'] ) ) {
3433
- $parent_id = $this->get_numeric_option( $plugin_info['parent'], 'id', null );
3434
- // $parent_slug = $this->get_option( $plugin_info['parent'], 'slug', null );
3435
- // $parent_public_key = $this->get_option( $plugin_info['parent'], 'public_key', null );
3436
- // $parent_name = $this->get_option( $plugin_info['parent'], 'name', null );
3437
- }
3438
-
3439
- if ( false === $id ) {
3440
- throw new Freemius_Exception( array(
3441
- 'error' => array(
3442
- 'type' => 'ParameterNotSet',
3443
- 'message' => 'Plugin id parameter is not set.',
3444
- 'code' => 'plugin_id_not_set',
3445
- 'http' => 500,
3446
- )
3447
- ) );
3448
- }
3449
- if ( false === $public_key ) {
3450
- throw new Freemius_Exception( array(
3451
- 'error' => array(
3452
- 'type' => 'ParameterNotSet',
3453
- 'message' => 'Plugin public_key parameter is not set.',
3454
- 'code' => 'plugin_public_key_not_set',
3455
- 'http' => 500,
3456
- )
3457
- ) );
3458
- }
3459
-
3460
- $plugin = ( $this->_plugin instanceof FS_Plugin ) ?
3461
- $this->_plugin :
3462
- new FS_Plugin();
3463
-
3464
- $plugin->update( array(
3465
- 'id' => $id,
3466
- 'type' => $this->get_option( $plugin_info, 'type', $this->_module_type),
3467
- 'public_key' => $public_key,
3468
- 'slug' => $this->_slug,
3469
- 'parent_plugin_id' => $parent_id,
3470
- 'version' => $this->get_plugin_version(),
3471
- 'title' => $this->get_plugin_name(),
3472
- 'file' => $this->_plugin_basename,
3473
- 'is_premium' => $this->get_bool_option( $plugin_info, 'is_premium', true ),
3474
- 'is_live' => $this->get_bool_option( $plugin_info, 'is_live', true ),
3475
- 'affiliate_moderation' => $this->get_option( $plugin_info, 'has_affiliation' ),
3476
- ) );
3477
-
3478
- if ( $plugin->is_updated() ) {
3479
- // Update plugin details.
3480
- $this->_plugin = FS_Plugin_Manager::instance( $this->_module_id )->store( $plugin );
3481
- }
3482
- // Set the secret key after storing the plugin, we don't want to store the key in the storage.
3483
- $this->_plugin->secret_key = $secret_key;
3484
-
3485
- if ( ! isset( $plugin_info['menu'] ) ) {
3486
- $plugin_info['menu'] = array();
3487
-
3488
- if ( ! empty( $this->_storage->sdk_last_version ) &&
3489
- version_compare( $this->_storage->sdk_last_version, '1.1.2', '<=' )
3490
- ) {
3491
- // Backward compatibility to 1.1.2
3492
- $plugin_info['menu']['slug'] = isset( $plugin_info['menu_slug'] ) ?
3493
- $plugin_info['menu_slug'] :
3494
- $this->_slug;
3495
- }
3496
- }
3497
-
3498
- $this->_menu = FS_Admin_Menu_Manager::instance(
3499
- $this->_module_id,
3500
- $this->_module_type,
3501
- $this->get_unique_affix()
3502
- );
3503
-
3504
- $this->_menu->init( $plugin_info['menu'], $this->is_addon() );
3505
-
3506
- $this->_has_addons = $this->get_bool_option( $plugin_info, 'has_addons', false );
3507
- $this->_has_paid_plans = $this->get_bool_option( $plugin_info, 'has_paid_plans', true );
3508
- $this->_has_premium_version = $this->get_bool_option( $plugin_info, 'has_premium_version', $this->_has_paid_plans );
3509
- $this->_ignore_pending_mode = $this->get_bool_option( $plugin_info, 'ignore_pending_mode', false );
3510
- $this->_is_org_compliant = $this->get_bool_option( $plugin_info, 'is_org_compliant', true );
3511
- $this->_is_premium_only = $this->get_bool_option( $plugin_info, 'is_premium_only', false );
3512
- if ( $this->_is_premium_only ) {
3513
- // If premium only plugin, disable anonymous mode.
3514
- $this->_enable_anonymous = false;
3515
- $this->_anonymous_mode = false;
3516
- } else {
3517
- $this->_enable_anonymous = $this->get_bool_option( $plugin_info, 'enable_anonymous', true );
3518
- $this->_anonymous_mode = $this->get_bool_option( $plugin_info, 'anonymous_mode', false );
3519
- }
3520
- $this->_permissions = $this->get_option( $plugin_info, 'permissions', array() );
3521
-
3522
- if ( ! empty( $plugin_info['trial'] ) ) {
3523
- $this->_trial_days = $this->get_numeric_option(
3524
- $plugin_info['trial'],
3525
- 'days',
3526
- // Default to 0 - trial without days specification.
3527
- 0
3528
- );
3529
-
3530
- $this->_is_trial_require_payment = $this->get_bool_option( $plugin_info['trial'], 'is_require_payment', false );
3531
- }
3532
- }
3533
-
3534
- /**
3535
- * @param string[] $options
3536
- * @param string $key
3537
- * @param mixed $default
3538
- *
3539
- * @return bool
3540
- */
3541
- private function get_option( &$options, $key, $default = false ) {
3542
- return ! empty( $options[ $key ] ) ? $options[ $key ] : $default;
3543
- }
3544
-
3545
- private function get_bool_option( &$options, $key, $default = false ) {
3546
- return isset( $options[ $key ] ) && is_bool( $options[ $key ] ) ? $options[ $key ] : $default;
3547
- }
3548
-
3549
- private function get_numeric_option( &$options, $key, $default = false ) {
3550
- return isset( $options[ $key ] ) && is_numeric( $options[ $key ] ) ? $options[ $key ] : $default;
3551
- }
3552
-
3553
- /**
3554
- * Gate keeper.
3555
- *
3556
- * @author Vova Feldman (@svovaf)
3557
- * @since 1.1.7.3
3558
- *
3559
- * @return bool
3560
- */
3561
- private function should_stop_execution() {
3562
- if ( empty( $this->_storage->was_plugin_loaded ) ) {
3563
- /**
3564
- * Don't execute Freemius until plugin was fully loaded at least once,
3565
- * to give the opportunity for the activation hook to run before pinging
3566
- * the API for connectivity test. This logic is relevant for the
3567
- * identification of new plugin install vs. plugin update.
3568
- *
3569
- * @author Vova Feldman (@svovaf)
3570
- * @since 1.1.9
3571
- */
3572
- return true;
3573
- }
3574
-
3575
- if ( $this->is_activation_mode() ) {
3576
- if ( ! is_admin() ) {
3577
- /**
3578
- * If in activation mode, don't execute Freemius outside of the
3579
- * admin dashboard.
3580
- *
3581
- * @author Vova Feldman (@svovaf)
3582
- * @since 1.1.7.3
3583
- */
3584
- return true;
3585
- }
3586
-
3587
- if ( ! WP_FS__IS_HTTP_REQUEST ) {
3588
- /**
3589
- * If in activation and executed without HTTP context (e.g. CLI, Cronjob),
3590
- * then don't start Freemius.
3591
- *
3592
- * @author Vova Feldman (@svovaf)
3593
- * @since 1.1.6.3
3594
- *
3595
- * @link https://wordpress.org/support/topic/errors-in-the-freemius-class-when-running-in-wordpress-in-cli
3596
- */
3597
- return true;
3598
- }
3599
-
3600
- if ( self::is_cron() ) {
3601
- /**
3602
- * If in activation mode, don't execute Freemius during wp crons
3603
- * (wp crons have HTTP context - called as HTTP request).
3604
- *
3605
- * @author Vova Feldman (@svovaf)
3606
- * @since 1.1.7.3
3607
- */
3608
- return true;
3609
- }
3610
-
3611
- if ( self::is_ajax() &&
3612
- ! $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) &&
3613
- ! $this->_admin_notices->has_sticky( 'failed_connect_api' )
3614
- ) {
3615
- /**
3616
- * During activation, if running in AJAX mode, unless there's a sticky
3617
- * connectivity issue notice, don't run Freemius.
3618
- *
3619
- * @author Vova Feldman (@svovaf)
3620
- * @since 1.1.7.3
3621
- */
3622
- return true;
3623
- }
3624
- }
3625
-
3626
- return false;
3627
- }
3628
-
3629
- /**
3630
- * Triggered after code type has changed.
3631
- *
3632
- * @author Vova Feldman (@svovaf)
3633
- * @since 1.1.9.1
3634
- */
3635
- function _after_code_type_change() {
3636
- $this->_logger->entrance();
3637
-
3638
- if ( $this->is_theme() ) {
3639
- // Expire the cache of the previous tabs since the theme may
3640
- // have setting updates after code type has changed.
3641
- $this->_cache->expire( 'tabs' );
3642
- $this->_cache->expire( 'tabs_stylesheets' );
3643
- }
3644
-
3645
- if ( $this->is_registered() ) {
3646
- if ( ! $this->is_addon() ) {
3647
- add_action(
3648
- is_admin() ? 'admin_init' : 'init',
3649
- array( &$this, '_plugin_code_type_changed' )
3650
- );
3651
- }
3652
-
3653
- if ( $this->is_premium() ) {
3654
- // Purge cached payments after switching to the premium version.
3655
- // @todo This logic doesn't handle purging the cache for serviceware module upgrade.
3656
- $this->get_api_user_scope()->purge_cache( "/plugins/{$this->_module_id}/payments.json?include_addons=true" );
3657
- }
3658
- }
3659
- }
3660
-
3661
- /**
3662
- * Handles plugin's code type change (free <--> premium).
3663
- *
3664
- * @author Vova Feldman (@svovaf)
3665
- * @since 1.0.9
3666
- */
3667
- function _plugin_code_type_changed() {
3668
- $this->_logger->entrance();
3669
-
3670
- if ( $this->is_premium() ) {
3671
- $this->reconnect_locally();
3672
-
3673
- // Activated premium code.
3674
- $this->do_action( 'after_premium_version_activation' );
3675
-
3676
- // Remove all sticky messages related to download of the premium version.
3677
- $this->_admin_notices->remove_sticky( array(
3678
- 'trial_started',
3679
- 'plan_upgraded',
3680
- 'plan_changed',
3681
- 'license_activated',
3682
- ) );
3683
-
3684
- if ( ! $this->is_only_premium() ) {
3685
- $this->_admin_notices->add_sticky(
3686
- sprintf( $this->get_text_inline( 'Premium %s version was successfully activated.', 'premium-activated-message' ), $this->_module_type ),
3687
- 'premium_activated',
3688
- $this->get_text_x_inline( 'W00t',
3689
- 'Used to express elation, enthusiasm, or triumph (especially in electronic communication).', 'woot' ) . '!'
3690
- );
3691
- }
3692
- } else {
3693
- // Remove sticky message related to premium code activation.
3694
- $this->_admin_notices->remove_sticky( 'premium_activated' );
3695
-
3696
- // Activated free code (after had the premium before).
3697
- $this->do_action( 'after_free_version_reactivation' );
3698
-
3699
- if ( $this->is_paying() && ! $this->is_premium() ) {
3700
- $this->_admin_notices->add_sticky(
3701
- sprintf(
3702
- /* translators: %s: License type (e.g. you have a professional license) */
3703
- $this->get_text_inline( 'You have a %s license.', 'you-have-x-license' ),
3704
- $this->_site->plan->title
3705
- ) . $this->get_complete_upgrade_instructions(),
3706
- 'plan_upgraded',
3707
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
3708
- );
3709
- }
3710
- }
3711
-
3712
- // Schedule code type changes event.
3713
- $this->schedule_install_sync();
3714
-
3715
- /**
3716
- * Unregister the uninstall hook for the other version of the plugin (with different code type) to avoid
3717
- * triggering a fatal error when uninstalling that plugin. For example, after deactivating the "free" version
3718
- * of a specific plugin, its uninstall hook should be unregistered after the "premium" version has been
3719
- * activated. If we don't do that, a fatal error will occur when we try to uninstall the "free" version since
3720
- * the main file of the "free" version will be loaded first before calling the hooked callback. Since the
3721
- * free and premium versions are almost identical (same class or have same functions), a fatal error like
3722
- * "Cannot redeclare class MyClass" or "Cannot redeclare my_function()" will occur.
3723
- */
3724
- $this->unregister_uninstall_hook();
3725
-
3726
- $this->clear_module_main_file_cache();
3727
-
3728
- // Update is_premium of latest version.
3729
- $this->_storage->prev_is_premium = $this->_plugin->is_premium;
3730
- }
3731
-
3732
- #endregion
3733
-
3734
- #----------------------------------------------------------------------------------
3735
- #region Add-ons
3736
- #----------------------------------------------------------------------------------
3737
-
3738
- /**
3739
- * Check if add-on installed and activated on site.
3740
- *
3741
- * @author Vova Feldman (@svovaf)
3742
- * @since 1.0.6
3743
- *
3744
- * @param string|number $id_or_slug
3745
- * @param bool|null $is_premium Since 1.2.1.7 can check for specified add-on version.
3746
- *
3747
- * @return bool
3748
- */
3749
- function is_addon_activated( $id_or_slug, $is_premium = null ) {
3750
- $this->_logger->entrance();
3751
-
3752
- $addon_id = self::get_module_id( $id_or_slug );
3753
- $is_activated = self::has_instance( $addon_id );
3754
-
3755
- if ( ! $is_activated ) {
3756
- return false;
3757
- }
3758
-
3759
- if ( is_bool( $is_premium ) ) {
3760
- // Check if the specified code version is activate.
3761
- $addon = $this->get_addon_instance( $addon_id );
3762
- $is_activated = ( $is_premium === $addon->is_premium() );
3763
- }
3764
-
3765
- return $is_activated;
3766
- }
3767
-
3768
- /**
3769
- * Check if add-on was connected to install
3770
- *
3771
- * @author Vova Feldman (@svovaf)
3772
- * @since 1.1.7
3773
- *
3774
- * @param string|number $id_or_slug
3775
- *
3776
- * @return bool
3777
- */
3778
- function is_addon_connected( $id_or_slug ) {
3779
- $this->_logger->entrance();
3780
-
3781
- $sites = self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN );
3782
-
3783
- $addon_id = self::get_module_id( $id_or_slug );
3784
- $addon = $this->get_addon( $addon_id );
3785
- $slug = $addon->slug;
3786
- if ( ! isset( $sites[ $slug ] ) ) {
3787
- return false;
3788
- }
3789
-
3790
- $site = $sites[ $slug ];
3791
-
3792
- $plugin = FS_Plugin_Manager::instance( $addon_id )->get();
3793
-
3794
- if ( $plugin->parent_plugin_id != $this->_plugin->id ) {
3795
- // The given slug do NOT belong to any of the plugin's add-ons.
3796
- return false;
3797
- }
3798
-
3799
- return ( is_object( $site ) &&
3800
- is_numeric( $site->id ) &&
3801
- is_numeric( $site->user_id ) &&
3802
- is_object( $site->plan )
3803
- );
3804
- }
3805
-
3806
- /**
3807
- * Determines if add-on installed.
3808
- *
3809
- * NOTE: This is a heuristic and only works if the folder/file named as the slug.
3810
- *
3811
- * @author Vova Feldman (@svovaf)
3812
- * @since 1.0.6
3813
- *
3814
- * @param string|number $id_or_slug
3815
- *
3816
- * @return bool
3817
- */
3818
- function is_addon_installed( $id_or_slug ) {
3819
- $this->_logger->entrance();
3820
-
3821
- $addon_id = self::get_module_id( $id_or_slug );
3822
-
3823
- return file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $this->get_addon_basename( $addon_id ) ) );
3824
- }
3825
-
3826
- /**
3827
- * Get add-on basename.
3828
- *
3829
- * @author Vova Feldman (@svovaf)
3830
- * @since 1.0.6
3831
- *
3832
- * @param string|number $id_or_slug
3833
- *
3834
- * @return string
3835
- */
3836
- function get_addon_basename( $id_or_slug ) {
3837
- $addon_id = self::get_module_id( $id_or_slug );
3838
-
3839
- if ( $this->is_addon_activated( $addon_id ) ) {
3840
- return self::instance( $addon_id )->get_plugin_basename();
3841
- }
3842
-
3843
- $addon = $this->get_addon( $addon_id );
3844
- $premium_basename = "{$addon->slug}-premium/{$addon->slug}.php";
3845
-
3846
- if ( file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $premium_basename ) ) ) {
3847
- return $premium_basename;
3848
- }
3849
-
3850
- $all_plugins = $this->get_all_plugins();
3851
-
3852
- foreach ( $all_plugins as $basename => &$data ) {
3853
- if ( $addon->slug === $data['slug'] ||
3854
- $addon->slug . '-premium' === $data['slug']
3855
- ) {
3856
- return $basename;
3857
- }
3858
- }
3859
-
3860
- $free_basename = "{$addon->slug}/{$addon->slug}.php";
3861
-
3862
- return $free_basename;
3863
- }
3864
-
3865
- /**
3866
- * Get installed add-ons instances.
3867
- *
3868
- * @author Vova Feldman (@svovaf)
3869
- * @since 1.0.6
3870
- *
3871
- * @return Freemius[]
3872
- */
3873
- function get_installed_addons() {
3874
- $installed_addons = array();
3875
- foreach ( self::$_instances as $instance ) {
3876
- if ( $instance->is_addon() && is_object( $instance->_parent_plugin ) ) {
3877
- if ( $this->_plugin->id == $instance->_parent_plugin->id ) {
3878
- $installed_addons[] = $instance;
3879
- }
3880
- }
3881
- }
3882
-
3883
- return $installed_addons;
3884
- }
3885
-
3886
- /**
3887
- * Check if any add-ons of the plugin are installed.
3888
- *
3889
- * @author Leo Fajardo (@leorw)
3890
- * @since 1.1.1
3891
- *
3892
- * @return bool
3893
- */
3894
- function has_installed_addons() {
3895
- if ( ! $this->has_addons() ) {
3896
- return false;
3897
- }
3898
-
3899
- foreach ( self::$_instances as $instance ) {
3900
- if ( $instance->is_addon() && is_object( $instance->_parent_plugin ) ) {
3901
- if ( $this->_plugin->id == $instance->_parent_plugin->id ) {
3902
- return true;
3903
- }
3904
- }
3905
- }
3906
-
3907
- return false;
3908
- }
3909
-
3910
- /**
3911
- * Tell Freemius that the current plugin is an add-on.
3912
- *
3913
- * @author Vova Feldman (@svovaf)
3914
- * @since 1.0.6
3915
- *
3916
- * @param number $parent_plugin_id The parent plugin ID
3917
- */
3918
- function init_addon( $parent_plugin_id ) {
3919
- $this->_plugin->parent_plugin_id = $parent_plugin_id;
3920
- }
3921
-
3922
- /**
3923
- * @author Vova Feldman (@svovaf)
3924
- * @since 1.0.6
3925
- *
3926
- * @return bool
3927
- */
3928
- function is_addon() {
3929
- return isset( $this->_plugin->parent_plugin_id ) && is_numeric( $this->_plugin->parent_plugin_id );
3930
- }
3931
-
3932
- /**
3933
- * Deactivate add-on if it's premium only and the user does't have a valid license.
3934
- *
3935
- * @param bool $is_after_trial_cancel
3936
- *
3937
- * @return bool If add-on was deactivated.
3938
- */
3939
- private function deactivate_premium_only_addon_without_license( $is_after_trial_cancel = false ) {
3940
- if ( ! $this->has_free_plan() &&
3941
- ! $this->has_features_enabled_license() &&
3942
- ! $this->_has_premium_license()
3943
- ) {
3944
- if ( $this->is_registered() ) {
3945
- // IF wrapper is turned off because activation_timestamp is currently only stored for plugins (not addons).
3946
- // if (empty($this->_storage->activation_timestamp) ||
3947
- // (WP_FS__SCRIPT_START_TIME - $this->_storage->activation_timestamp) > 30
3948
- // ) {
3949
- /**
3950
- * @todo When it's first fail, there's no reason to try and re-sync because the licenses were just synced after initial activation.
3951
- *
3952
- * Retry syncing the user add-on licenses.
3953
- */
3954
- // Sync licenses.
3955
- $this->_sync_licenses();
3956
- // }
3957
-
3958
- // Try to activate premium license.
3959
- $this->_activate_license( true );
3960
- }
3961
-
3962
- if ( ! $this->has_free_plan() &&
3963
- ! $this->has_features_enabled_license() &&
3964
- ! $this->_has_premium_license()
3965
- ) {
3966
- // @todo Check if deactivate plugins also call the deactivation hook.
3967
-
3968
- $this->_parent->_admin_notices->add_sticky(
3969
- sprintf(
3970
- ($is_after_trial_cancel ?
3971
- $this->_parent->get_text_inline(
3972
- '%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.',
3973
- 'addon-trial-cancelled-message'
3974
- ) :
3975
- $this->_parent->get_text_inline(
3976
- '%s is a premium only add-on. You have to purchase a license first before activating the plugin.',
3977
- 'addon-no-license-message'
3978
- )
3979
- ),
3980
- '<b>' . $this->_plugin->title . '</b>'
3981
- ) . ' ' . sprintf(
3982
- '<a href="%s" aria-label="%s" class="button button-primary" style="margin-left: 10px; vertical-align: middle;">%s &nbsp;&#10140;</a>',
3983
- $this->_parent->addon_url( $this->_slug ),
3984
- esc_attr( sprintf( $this->_parent->get_text_inline( 'More information about %s', 'more-information-about-x' ), $this->_plugin->title ) ),
3985
- $this->_parent->get_text_inline( 'Purchase License', 'purchase-license' )
3986
- ),
3987
- 'no_addon_license_' . $this->_slug,
3988
- ( $is_after_trial_cancel ? '' : $this->_parent->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...' ),
3989
- ( $is_after_trial_cancel ? 'success' : 'error' )
3990
- );
3991
-
3992
- deactivate_plugins( array( $this->_plugin_basename ), true );
3993
-
3994
- return true;
3995
- }
3996
- }
3997
-
3998
- return false;
3999
- }
4000
-
4001
- #endregion
4002
-
4003
- #----------------------------------------------------------------------------------
4004
- #region Sandbox
4005
- #----------------------------------------------------------------------------------
4006
-
4007
- /**
4008
- * Set Freemius into sandbox mode for debugging.
4009
- *
4010
- * @author Vova Feldman (@svovaf)
4011
- * @since 1.0.4
4012
- *
4013
- * @param string $secret_key
4014
- */
4015
- function init_sandbox( $secret_key ) {
4016
- $this->_plugin->secret_key = $secret_key;
4017
-
4018
- // Update plugin details.
4019
- FS_Plugin_Manager::instance( $this->_module_id )->update( $this->_plugin, true );
4020
- }
4021
-
4022
- /**
4023
- * Check if running payments in sandbox mode.
4024
- *
4025
- * @author Vova Feldman (@svovaf)
4026
- * @since 1.0.4
4027
- *
4028
- * @return bool
4029
- */
4030
- function is_payments_sandbox() {
4031
- return ( ! $this->is_live() ) || isset( $this->_plugin->secret_key );
4032
- }
4033
-
4034
- #endregion
4035
-
4036
- /**
4037
- * Check if running test vs. live plugin.
4038
- *
4039
- * @author Vova Feldman (@svovaf)
4040
- * @since 1.0.5
4041
- *
4042
- * @return bool
4043
- */
4044
- function is_live() {
4045
- return $this->_plugin->is_live;
4046
- }
4047
-
4048
- /**
4049
- * Check if the user skipped connecting the account with Freemius.
4050
- *
4051
- * @author Vova Feldman (@svovaf)
4052
- * @since 1.0.7
4053
- *
4054
- * @return bool
4055
- */
4056
- function is_anonymous() {
4057
- if ( ! isset( $this->_is_anonymous ) ) {
4058
- if ( ! isset( $this->_storage->is_anonymous ) ) {
4059
- // Not skipped.
4060
- $this->_is_anonymous = false;
4061
- } else if ( is_bool( $this->_storage->is_anonymous ) ) {
4062
- // For back compatibility, since the variable was boolean before.
4063
- $this->_is_anonymous = $this->_storage->is_anonymous;
4064
-
4065
- // Upgrade stored data format to 1.1.3 format.
4066
- $this->set_anonymous_mode( $this->_storage->is_anonymous );
4067
- } else {
4068
- // Version 1.1.3 and later.
4069
- $this->_is_anonymous = $this->_storage->is_anonymous['is'];
4070
- }
4071
- }
4072
-
4073
- return $this->_is_anonymous;
4074
- }
4075
-
4076
- /**
4077
- * Check if user connected his account and install pending email activation.
4078
- *
4079
- * @author Vova Feldman (@svovaf)
4080
- * @since 1.0.7
4081
- *
4082
- * @return bool
4083
- */
4084
- function is_pending_activation() {
4085
- return $this->_storage->get( 'is_pending_activation', false );
4086
- }
4087
-
4088
- /**
4089
- * Check if plugin must be WordPress.org compliant.
4090
- *
4091
- * @since 1.0.7
4092
- *
4093
- * @return bool
4094
- */
4095
- function is_org_repo_compliant() {
4096
- return $this->_is_org_compliant;
4097
- }
4098
-
4099
- #----------------------------------------------------------------------------------
4100
- #region Daily Sync Cron
4101
- #----------------------------------------------------------------------------------
4102
-
4103
- /**
4104
- * @author Vova Feldman (@svovaf)
4105
- * @since 1.1.7.3
4106
- */
4107
- private function run_manual_sync() {
4108
- self::require_pluggable_essentials();
4109
-
4110
- if ( ! $this->is_user_admin() ) {
4111
- return;
4112
- }
4113
-
4114
- // Run manual sync.
4115
- $this->_sync_cron();
4116
-
4117
- // Reschedule next cron to run 24 hours from now (performance optimization).
4118
- $this->clear_sync_cron();
4119
-
4120
- $this->schedule_sync_cron( time() + WP_FS__TIME_24_HOURS_IN_SEC, false );
4121
- }
4122
-
4123
- /**
4124
- * Data sync cron job. Replaces the background sync non blocking HTTP request
4125
- * that doesn't halt page loading.
4126
- *
4127
- * @author Vova Feldman (@svovaf)
4128
- * @since 1.1.7.3
4129
- */
4130
- function _sync_cron() {
4131
- $this->_logger->entrance();
4132
-
4133
- // Store the last time data sync was executed.
4134
- $this->_storage->sync_timestamp = time();
4135
-
4136
- // Check if API is temporary down.
4137
- if ( FS_Api::is_temporary_down() ) {
4138
- return;
4139
- }
4140
-
4141
- // @todo Add logic that identifies API latency, and reschedule the next background sync randomly between 8-16 hours.
4142
-
4143
- if ( $this->is_registered() ) {
4144
- if ( $this->has_paid_plan() ) {
4145
- // Initiate background plan sync.
4146
- $this->_sync_license( true );
4147
-
4148
- if ( $this->is_paying() ) {
4149
- // Check for premium plugin updates.
4150
- $this->check_updates( true );
4151
- }
4152
- } else {
4153
- // Sync install (only if something changed locally).
4154
- $this->sync_install();
4155
- }
4156
- }
4157
-
4158
- $this->do_action( 'after_sync_cron' );
4159
- }
4160
-
4161
- /**
4162
- * Check if sync was executed in the last $period of seconds.
4163
- *
4164
- * @author Vova Feldman (@svovaf)
4165
- * @since 1.1.7.3
4166
- *
4167
- * @param int $period In seconds
4168
- *
4169
- * @return bool
4170
- */
4171
- private function is_sync_executed( $period = WP_FS__TIME_24_HOURS_IN_SEC ) {
4172
- if ( ! isset( $this->_storage->sync_timestamp ) ) {
4173
- return false;
4174
- }
4175
-
4176
- return ( $this->_storage->sync_timestamp > ( WP_FS__SCRIPT_START_TIME - $period ) );
4177
- }
4178
-
4179
- /**
4180
- * @author Vova Feldman (@svovaf)
4181
- * @since 1.1.7.3
4182
- *
4183
- * @return bool
4184
- */
4185
- private function is_sync_cron_on() {
4186
- /**
4187
- * @var object $sync_cron_data
4188
- */
4189
- $sync_cron_data = $this->_storage->get( 'sync_cron', null );
4190
-
4191
- return ( ! is_null( $sync_cron_data ) && true === $sync_cron_data->on );
4192
- }
4193
-
4194
- /**
4195
- * @author Vova Feldman (@svovaf)
4196
- * @since 1.1.7.3
4197
- *
4198
- * @param int $start_at Defaults to now.
4199
- * @param bool $randomize_start If true, schedule first job randomly during the next 12 hours. Otherwise,
4200
- * schedule job to start right away.
4201
- */
4202
- private function schedule_sync_cron( $start_at = WP_FS__SCRIPT_START_TIME, $randomize_start = true ) {
4203
- $this->_logger->entrance();
4204
-
4205
- if ( $randomize_start ) {
4206
- // Schedule first sync with a random 12 hour time range from now.
4207
- $start_at += rand( 0, ( WP_FS__TIME_24_HOURS_IN_SEC / 2 ) );
4208
- }
4209
-
4210
- // Schedule daily WP cron.
4211
- wp_schedule_event(
4212
- $start_at,
4213
- 'daily',
4214
- $this->get_action_tag( 'data_sync' )
4215
- );
4216
-
4217
- $this->_storage->store( 'sync_cron', (object) array(
4218
- 'version' => $this->get_plugin_version(),
4219
- 'sdk_version' => $this->version,
4220
- 'timestamp' => WP_FS__SCRIPT_START_TIME,
4221
- 'on' => true,
4222
- ) );
4223
- }
4224
-
4225
- /**
4226
- * Add the actual sync function to the cron job hook.
4227
- *
4228
- * @author Vova Feldman (@svovaf)
4229
- * @since 1.1.7.3
4230
- */
4231
- private function hook_callback_to_sync_cron() {
4232
- $this->add_action( 'data_sync', array( &$this, '_sync_cron' ) );
4233
- }
4234
-
4235
- /**
4236
- * @author Vova Feldman (@svovaf)
4237
- * @since 1.1.7.3
4238
- */
4239
- private function clear_sync_cron() {
4240
- $this->_logger->entrance();
4241
-
4242
- if ( ! $this->is_sync_cron_on() ) {
4243
- return;
4244
- }
4245
-
4246
- $this->_storage->remove( 'sync_cron' );
4247
-
4248
- wp_clear_scheduled_hook( $this->get_action_tag( 'data_sync' ) );
4249
- }
4250
-
4251
- /**
4252
- * Unix timestamp for next sync cron execution or false if not scheduled.
4253
- *
4254
- * @author Vova Feldman (@svovaf)
4255
- * @since 1.1.7.3
4256
- *
4257
- * @return int|false
4258
- */
4259
- function next_sync_cron() {
4260
- $this->_logger->entrance();
4261
-
4262
- if ( ! $this->is_sync_cron_on() ) {
4263
- return false;
4264
- }
4265
-
4266
- return wp_next_scheduled( $this->get_action_tag( 'data_sync' ) );
4267
- }
4268
-
4269
- /**
4270
- * Unix timestamp for previous sync cron execution or false if never executed.
4271
- *
4272
- * @author Vova Feldman (@svovaf)
4273
- * @since 1.1.7.3
4274
- *
4275
- * @return int|false
4276
- */
4277
- function last_sync_cron() {
4278
- $this->_logger->entrance();
4279
-
4280
- return $this->_storage->get( 'sync_timestamp' );
4281
- }
4282
-
4283
- #endregion Daily Sync Cron ------------------------------------------------------------------
4284
-
4285
- #----------------------------------------------------------------------------------
4286
- #region Async Install Sync
4287
- #----------------------------------------------------------------------------------
4288
-
4289
- /**
4290
- * @author Vova Feldman (@svovaf)
4291
- * @since 1.1.7.3
4292
- *
4293
- * @return bool
4294
- */
4295
- private function is_install_sync_scheduled() {
4296
- /**
4297
- * @var object $cron_data
4298
- */
4299
- $cron_data = $this->_storage->get( 'install_sync_cron', null );
4300
-
4301
- return ( ! is_null( $cron_data ) && true === $cron_data->on );
4302
- }
4303
-
4304
- /**
4305
- * Instead of running blocking install sync event, execute non blocking scheduled wp-cron.
4306
- *
4307
- * @author Vova Feldman (@svovaf)
4308
- * @since 1.1.7.3
4309
- */
4310
- private function schedule_install_sync() {
4311
- $this->_logger->entrance();
4312
-
4313
- $this->clear_install_sync_cron();
4314
-
4315
- // Schedule immediate install sync.
4316
- wp_schedule_single_event(
4317
- WP_FS__SCRIPT_START_TIME,
4318
- $this->get_action_tag( 'install_sync' )
4319
- );
4320
-
4321
- $this->_storage->store( 'install_sync_cron', (object) array(
4322
- 'version' => $this->get_plugin_version(),
4323
- 'sdk_version' => $this->version,
4324
- 'timestamp' => WP_FS__SCRIPT_START_TIME,
4325
- 'on' => true,
4326
- ) );
4327
- }
4328
-
4329
- /**
4330
- * Unix timestamp for previous install sync cron execution or false if never executed.
4331
- *
4332
- * @todo There's some very strange bug that $this->_storage->install_sync_timestamp value is not being
4333
- * updated. But for sure the sync event is working.
4334
- *
4335
- * @author Vova Feldman (@svovaf)
4336
- * @since 1.1.7.3
4337
- *
4338
- * @return int|false
4339
- */
4340
- function last_install_sync() {
4341
- $this->_logger->entrance();
4342
-
4343
- return $this->_storage->get( 'install_sync_timestamp' );
4344
- }
4345
-
4346
- /**
4347
- * Unix timestamp for next install sync cron execution or false if not scheduled.
4348
- *
4349
- * @author Vova Feldman (@svovaf)
4350
- * @since 1.1.7.3
4351
- *
4352
- * @return int|false
4353
- */
4354
- function next_install_sync() {
4355
- $this->_logger->entrance();
4356
-
4357
- if ( ! $this->is_install_sync_scheduled() ) {
4358
- return false;
4359
- }
4360
-
4361
- return wp_next_scheduled( $this->get_action_tag( 'install_sync' ) );
4362
- }
4363
-
4364
- /**
4365
- * Add the actual install sync function to the cron job hook.
4366
- *
4367
- * @author Vova Feldman (@svovaf)
4368
- * @since 1.1.7.3
4369
- */
4370
- private function hook_callback_to_install_sync() {
4371
- $this->add_action( 'install_sync', array( &$this, '_run_sync_install' ) );
4372
- }
4373
-
4374
- /**
4375
- * @author Vova Feldman (@svovaf)
4376
- * @since 1.1.7.3
4377
- */
4378
- private function clear_install_sync_cron() {
4379
- $this->_logger->entrance();
4380
-
4381
- if ( ! $this->is_install_sync_scheduled() ) {
4382
- return;
4383
- }
4384
-
4385
- $this->_storage->remove( 'install_sync_cron' );
4386
-
4387
- wp_clear_scheduled_hook( $this->get_action_tag( 'install_sync' ) );
4388
- }
4389
-
4390
- /**
4391
- * @author Vova Feldman (@svovaf)
4392
- * @since 1.1.7.3
4393
- */
4394
- public function _run_sync_install() {
4395
- $this->_logger->entrance();
4396
-
4397
- // Update last install sync timestamp.
4398
- $this->_storage->install_sync_timestamp = time();
4399
-
4400
- $this->sync_install( array(), true );
4401
- }
4402
-
4403
- #endregion Async Install Sync ------------------------------------------------------------------
4404
-
4405
- /**
4406
- * Show a notice that activation is currently pending.
4407
- *
4408
- * @author Vova Feldman (@svovaf)
4409
- * @since 1.0.7
4410
- *
4411
- * @param bool|string $email
4412
- * @param bool $is_pending_trial Since 1.2.1.5
4413
- */
4414
- function _add_pending_activation_notice( $email = false, $is_pending_trial = false ) {
4415
- if ( ! is_string( $email ) ) {
4416
- $current_user = self::_get_current_wp_user();
4417
- $email = $current_user->user_email;
4418
- }
4419
-
4420
- $this->_admin_notices->add_sticky(
4421
- sprintf(
4422
- $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' ),
4423
- '<b>' . $this->get_plugin_name() . '</b>',
4424
- '<b>' . $email . '</b>',
4425
- ( $is_pending_trial ?
4426
- $this->get_text_inline( 'start the trial', 'start-the-trial' ) :
4427
- $this->get_text_inline( 'complete the install', 'complete-the-install' ) )
4428
- ),
4429
- 'activation_pending',
4430
- 'Thanks!'
4431
- );
4432
- }
4433
-
4434
- /**
4435
- * Check if currently in plugin activation.
4436
- *
4437
- * @author Vova Feldman (@svovaf)
4438
- * @since 1.1.4
4439
- *
4440
- * @return bool
4441
- */
4442
- function is_plugin_activation() {
4443
- return get_option( 'fs_'
4444
- . ( $this->is_plugin() ? '' : $this->_module_type . '_' )
4445
- . "{$this->_slug}_activated", false );
4446
- }
4447
-
4448
- /**
4449
- *
4450
- * NOTE: admin_menu action executed before admin_init.
4451
- *
4452
- * @author Vova Feldman (@svovaf)
4453
- * @since 1.0.7
4454
- */
4455
- function _admin_init_action() {
4456
- /**
4457
- * Automatically redirect to connect/activation page after plugin activation.
4458
- *
4459
- * @since 1.1.7 Do NOT redirect to opt-in when running in network admin mode.
4460
- */
4461
- if ( $this->is_plugin_activation() ) {
4462
- delete_option( 'fs_'
4463
- . ( $this->is_plugin() ? '' : $this->_module_type . '_' )
4464
- . "{$this->_slug}_activated" );
4465
-
4466
- if ( ! function_exists( 'is_network_admin' ) || ! is_network_admin() ) {
4467
- $this->_redirect_on_activation_hook();
4468
-
4469
- return;
4470
- }
4471
- }
4472
-
4473
- if ( fs_request_is_action( $this->get_unique_affix() . '_skip_activation' ) ) {
4474
- check_admin_referer( $this->get_unique_affix() . '_skip_activation' );
4475
-
4476
- $this->skip_connection();
4477
-
4478
- fs_redirect( $this->get_after_activation_url( 'after_skip_url' ) );
4479
- }
4480
-
4481
- if ( ! $this->is_addon() && ! $this->is_registered() && ! $this->is_anonymous() ) {
4482
- if ( ! $this->is_pending_activation() ) {
4483
- if ( ! $this->_menu->is_main_settings_page() ) {
4484
- /**
4485
- * If a user visits any other admin page before activating the premium-only theme with a valid
4486
- * license, reactivate the previous theme.
4487
- *
4488
- * @author Leo Fajardo (@leorw)
4489
- * @since 1.2.2
4490
- */
4491
- if ( $this->is_theme()
4492
- && $this->is_only_premium()
4493
- && ! $this->has_settings_menu()
4494
- && ! isset( $_REQUEST['fs_action'] )
4495
- && $this->can_activate_previous_theme()
4496
- ) {
4497
- $this->activate_previous_theme();
4498
-
4499
- return;
4500
- }
4501
-
4502
- if ( $this->is_plugin_new_install() || $this->is_only_premium() ) {
4503
- // Show notice for new plugin installations.
4504
- $this->_admin_notices->add(
4505
- sprintf(
4506
- $this->get_text_inline( 'You are just one step away - %s', 'you-are-step-away' ),
4507
- sprintf( '<b><a href="%s">%s</a></b>',
4508
- $this->get_activation_url(),
4509
- sprintf( $this->get_text_x_inline( 'Complete "%s" Activation Now',
4510
- '%s - plugin name. As complete "PluginX" activation now', 'activate-x-now' ), $this->get_plugin_name() )
4511
- )
4512
- ),
4513
- '',
4514
- 'update-nag'
4515
- );
4516
- } else {
4517
- if ( ! isset( $this->_storage->sticky_optin_added ) ) {
4518
- $this->_storage->sticky_optin_added = true;
4519
-
4520
- // Show notice for new plugin installations.
4521
- $this->_admin_notices->add_sticky(
4522
- sprintf(
4523
- $this->get_text_inline( 'We made a few tweaks to the %s, %s', 'few-plugin-tweaks' ),
4524
- $this->_module_type,
4525
- sprintf( '<b><a href="%s">%s</a></b>',
4526
- $this->get_activation_url(),
4527
- sprintf( $this->get_text_inline( 'Opt in to make "%s" Better!', 'optin-x-now' ), $this->get_plugin_name() )
4528
- )
4529
- ),
4530
- 'connect_account',
4531
- '',
4532
- 'update-nag'
4533
- );
4534
- }
4535
-
4536
- if ( $this->has_filter( 'optin_pointer_element' ) ) {
4537
- // Don't show admin nag if plugin update.
4538
- wp_enqueue_script( 'wp-pointer' );
4539
- wp_enqueue_style( 'wp-pointer' );
4540
-
4541
- $this->_enqueue_connect_essentials();
4542
-
4543
- add_action( 'admin_print_footer_scripts', array(
4544
- $this,
4545
- '_add_connect_pointer_script'
4546
- ) );
4547
- }
4548
- }
4549
- }
4550
- }
4551
-
4552
- if ( $this->is_theme() &&
4553
- $this->_menu->is_main_settings_page()
4554
- ) {
4555
- $this->_show_theme_activation_optin_dialog();
4556
- }
4557
- }
4558
-
4559
- $this->_add_upgrade_action_link();
4560
- }
4561
-
4562
- /**
4563
- * Enqueue connect requires scripts and styles.
4564
- *
4565
- * @author Vova Feldman (@svovaf)
4566
- * @since 1.1.4
4567
- */
4568
- function _enqueue_connect_essentials() {
4569
- wp_enqueue_script( 'jquery' );
4570
- wp_enqueue_script( 'json2' );
4571
-
4572
- fs_enqueue_local_script( 'postmessage', 'nojquery.ba-postmessage.min.js' );
4573
- fs_enqueue_local_script( 'fs-postmessage', 'postmessage.js' );
4574
-
4575
- fs_enqueue_local_style( 'fs_connect', '/admin/connect.css' );
4576
- }
4577
-
4578
- /**
4579
- * Add connect / opt-in pointer.
4580
- *
4581
- * @author Vova Feldman (@svovaf)
4582
- * @since 1.1.4
4583
- */
4584
- function _add_connect_pointer_script() {
4585
- $vars = array( 'id' => $this->_module_id );
4586
- $pointer_content = fs_get_template( 'connect.php', $vars );
4587
- ?>
4588
- <script type="text/javascript">// <![CDATA[
4589
- jQuery(document).ready(function ($) {
4590
- if ('undefined' !== typeof(jQuery().pointer)) {
4591
-
4592
- var element = <?php echo $this->apply_filters( 'optin_pointer_element', '$("#non_existing_element");' ) ?>;
4593
-
4594
- if (element.length > 0) {
4595
- var optin = $(element).pointer($.extend(true, {}, {
4596
- content : <?php echo json_encode( $pointer_content ) ?>,
4597
- position : {
4598
- edge : 'left',
4599
- align: 'center'
4600
- },
4601
- buttons : function () {
4602
- // Don't show pointer buttons.
4603
- return '';
4604
- },
4605
- pointerWidth: 482
4606
- }, <?php echo $this->apply_filters( 'optin_pointer_options_json', '{}' ) ?>));
4607
-
4608
- <?php
4609
- echo $this->apply_filters( 'optin_pointer_execute', "
4610
 
4611
- optin.pointer('open');
4612
 
4613
- // Tag the opt-in pointer with custom class.
4614
- $('.wp-pointer #fs_connect')
4615
- .parents('.wp-pointer.wp-pointer-top')
4616
- .addClass('fs-opt-in-pointer');
4617
 
4618
- ", 'element', 'optin' ) ?>
4619
- }
4620
- }
4621
- });
4622
- // ]]></script>
4623
- <?php
4624
- }
4625
-
4626
- /**
4627
- * Return current page's URL.
4628
- *
4629
- * @author Vova Feldman (@svovaf)
4630
- * @since 1.0.7
4631
- *
4632
- * @return string
4633
- */
4634
- function current_page_url() {
4635
- $url = 'http';
4636
-
4637
- if ( isset( $_SERVER["HTTPS"] ) ) {
4638
- if ( $_SERVER["HTTPS"] == "on" ) {
4639
- $url .= "s";
4640
- }
4641
- }
4642
- $url .= "://";
4643
- if ( $_SERVER["SERVER_PORT"] != "80" ) {
4644
- $url .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
4645
- } else {
4646
- $url .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
4647
- }
4648
-
4649
- return esc_url( $url );
4650
- }
4651
-
4652
- /**
4653
- * Check if the current page is the plugin's main admin settings page.
4654
- *
4655
- * @author Vova Feldman (@svovaf)
4656
- * @since 1.0.7
4657
- *
4658
- * @return bool
4659
- */
4660
- function _is_plugin_page() {
4661
- return fs_is_plugin_page( $this->_menu->get_raw_slug() ) ||
4662
- fs_is_plugin_page( $this->_slug );
4663
- }
4664
-
4665
- /* Events
4666
- ------------------------------------------------------------------------------------------------------------------*/
4667
- /**
4668
- * Delete site install from Database.
4669
- *
4670
- * @author Vova Feldman (@svovaf)
4671
- * @since 1.0.1
4672
- *
4673
- * @param bool $store
4674
- */
4675
- function _delete_site( $store = true ) {
4676
- self::_delete_site_by_slug( $this->_slug, $this->_module_type, $store );
4677
- }
4678
-
4679
- /**
4680
- * Delete site install from Database.
4681
- *
4682
- * @author Vova Feldman (@svovaf)
4683
- * @since 1.2.2.7
4684
- *
4685
- * @param string $slug
4686
- * @param string $module_type
4687
- * @param bool $store
4688
- */
4689
- static function _delete_site_by_slug($slug, $module_type, $store = true ) {
4690
- $sites = self::get_all_sites( $module_type );
4691
-
4692
- if ( isset( $sites[ $slug ] ) ) {
4693
- unset( $sites[ $slug ] );
4694
- }
4695
-
4696
- self::set_account_option_by_module( $module_type, 'sites', $sites, $store );
4697
- }
4698
-
4699
- /**
4700
- * Delete plugin's plans information.
4701
- *
4702
- * @param bool $store Flush to Database if true.
4703
- *
4704
- * @author Vova Feldman (@svovaf)
4705
- * @since 1.0.9
4706
- */
4707
- private function _delete_plans( $store = true ) {
4708
- $this->_logger->entrance();
4709
-
4710
- $plans = self::get_all_plans( $this->_module_type );
4711
-
4712
- unset( $plans[ $this->_slug ] );
4713
-
4714
- $this->set_account_option( 'plans', $plans, $store );
4715
- }
4716
-
4717
- /**
4718
- * Delete all plugin licenses.
4719
- *
4720
- * @author Vova Feldman (@svovaf)
4721
- * @since 1.0.9
4722
- *
4723
- * @param bool $store
4724
- * @param string|bool $plugin_slug
4725
- */
4726
- private function _delete_licenses( $store = true, $plugin_slug = false ) {
4727
- $this->_logger->entrance();
4728
-
4729
- $all_licenses = self::get_all_licenses( $this->_module_type );
4730
-
4731
- if ( ! is_string( $plugin_slug ) ) {
4732
- $plugin_slug = $this->_slug;
4733
- }
4734
-
4735
- unset( $all_licenses[ $plugin_slug ] );
4736
-
4737
- $this->set_account_option( 'licenses', $all_licenses, $store );
4738
- }
4739
-
4740
- /**
4741
- * Check if Freemius was added on new plugin installation.
4742
- *
4743
- * @author Vova Feldman (@svovaf)
4744
- * @since 1.1.5
4745
- *
4746
- * @return bool
4747
- */
4748
- function is_plugin_new_install() {
4749
- return isset( $this->_storage->is_plugin_new_install ) &&
4750
- $this->_storage->is_plugin_new_install;
4751
- }
4752
-
4753
- /**
4754
- * Check if it's the first plugin release that is running Freemius.
4755
- *
4756
- * @author Vova Feldman (@svovaf)
4757
- * @since 1.2.1.5
4758
- *
4759
- * @return bool
4760
- */
4761
- function is_first_freemius_powered_version() {
4762
- return empty( $this->_storage->plugin_last_version );
4763
- }
4764
-
4765
- /**
4766
- * @author Leo Fajardo (@leorw)
4767
- * @since 1.2.2
4768
- *
4769
- * @return bool|string
4770
- */
4771
- private function get_previous_theme_slug() {
4772
- return isset( $this->_storage->previous_theme ) ?
4773
- $this->_storage->previous_theme :
4774
- false;
4775
- }
4776
-
4777
- /**
4778
- * @author Leo Fajardo (@leorw)
4779
- * @since 1.2.2
4780
- *
4781
- * @return string
4782
- */
4783
- private function can_activate_previous_theme() {
4784
- $slug = $this->get_previous_theme_slug();
4785
- if ( false !== $slug && current_user_can( 'switch_themes' ) ) {
4786
- $theme_instance = wp_get_theme( $slug );
4787
-
4788
- return $theme_instance->exists();
4789
- }
4790
-
4791
- return false;
4792
- }
4793
-
4794
- /**
4795
- * @author Leo Fajardo (@leorw)
4796
- * @since 1.2.2
4797
- *
4798
- * @return string
4799
- */
4800
- private function activate_previous_theme() {
4801
- switch_theme( $this->get_previous_theme_slug() );
4802
- unset( $this->_storage->previous_theme );
4803
-
4804
- global $pagenow;
4805
- if ( 'themes.php' === $pagenow ) {
4806
- /**
4807
- * Refresh the active theme information.
4808
- *
4809
- * @author Leo Fajardo (@leorw)
4810
- * @since 1.2.2
4811
- */
4812
- fs_redirect( admin_url( $pagenow ) );
4813
- }
4814
- }
4815
-
4816
- /**
4817
- * @author Leo Fajardo (@leorw)
4818
- * @since 1.2.2
4819
- *
4820
- * @return string
4821
- */
4822
- function get_previous_theme_activation_url() {
4823
- if ( ! $this->can_activate_previous_theme() ) {
4824
- return '';
4825
- }
4826
-
4827
- /**
4828
- * Activation URL
4829
- *
4830
- * @author Leo Fajardo (@leorw)
4831
- * @since 1.2.2
4832
- */
4833
- return wp_nonce_url(
4834
- admin_url( 'themes.php?action=activate&stylesheet=' . urlencode( $this->get_previous_theme_slug() ) ),
4835
- 'switch-theme_' . $this->get_previous_theme_slug()
4836
- );
4837
- }
4838
-
4839
- /**
4840
- * Saves the slug of the previous theme if it still exists so that it can be used by the logic in the opt-in
4841
- * form that decides whether to add a close button to the opt-in dialog or not. So after a premium-only theme is
4842
- * activated, the close button will appear and will reactivate the previous theme if clicked. If the previous
4843
- * theme doesn't exist, then there will be no close button.
4844
- *
4845
- * @author Leo Fajardo (@leorw)
4846
- * @since 1.2.2
4847
- *
4848
- * @param string $slug_or_name Old theme's slug or name.
4849
- * @param bool|WP_Theme $old_theme WP_Theme instance of the old theme if it still exists.
4850
- */
4851
- function _activate_theme_event_hook( $slug_or_name, $old_theme = false ) {
4852
- $this->_storage->previous_theme = ( false !== $old_theme ) ?
4853
- $old_theme->get_stylesheet() :
4854
- $slug_or_name;
4855
-
4856
- $this->_activate_plugin_event_hook();
4857
- }
4858
-
4859
- /**
4860
- * Plugin activated hook.
4861
- *
4862
- * @author Vova Feldman (@svovaf)
4863
- * @since 1.0.1
4864
- *
4865
- * @uses FS_Api
4866
- */
4867
- function _activate_plugin_event_hook() {
4868
- $this->_logger->entrance( 'slug = ' . $this->_slug );
4869
-
4870
- if ( ! $this->is_user_admin() ) {
4871
- return;
4872
- }
4873
-
4874
- $this->unregister_uninstall_hook();
4875
-
4876
- // Clear API cache on activation.
4877
- FS_Api::clear_cache();
4878
-
4879
- $is_premium_version_activation = ( current_filter() !== ( 'activate_' . $this->_free_plugin_basename ) );
4880
-
4881
- $this->_logger->info( 'Activating ' . ( $is_premium_version_activation ? 'premium' : 'free' ) . ' plugin version.' );
4882
-
4883
- // 1. If running in the activation of the FREE module, get the basename of the PREMIUM.
4884
- // 2. If running in the activation of the PREMIUM module, get the basename of the FREE.
4885
- $other_version_basename = $is_premium_version_activation ?
4886
- $this->_free_plugin_basename :
4887
- $this->premium_plugin_basename();
4888
-
4889
- /**
4890
- * If the other module version is activate, deactivate it.
4891
- *
4892
- * @author Leo Fajardo (@leorw)
4893
- * @since 1.2.2
4894
- */
4895
- if ( is_plugin_active( $other_version_basename ) ) {
4896
- deactivate_plugins( $other_version_basename );
4897
- }
4898
 
4899
- if ( $this->is_registered() ) {
4900
- if ( $is_premium_version_activation ) {
4901
- $this->reconnect_locally();
4902
- }
 
 
 
 
 
4903
 
 
 
 
4904
 
4905
- // Schedule re-activation event and sync.
4906
- // $this->sync_install( array(), true );
4907
- $this->schedule_install_sync();
4908
-
4909
- // If activating the premium module version, add an admin notice to congratulate for an upgrade completion.
4910
- if ( $is_premium_version_activation ) {
4911
- $this->_admin_notices->add(
4912
- sprintf( $this->get_text_inline( 'The upgrade of %s was successfully completed.', 'successful-version-upgrade-message' ), sprintf( '<b>%s</b>', $this->_plugin->title ) ),
4913
- $this->get_text_x_inline( 'W00t',
4914
- 'Used to express elation, enthusiasm, or triumph (especially in electronic communication).', 'woot' ) . '!'
4915
- );
4916
- }
4917
- } else if ( $this->is_anonymous() ) {
4918
- /**
4919
- * Reset "skipped" click cache on the following:
4920
- * 1. Freemius DEV mode.
4921
- * 2. WordPress DEBUG mode.
4922
- * 3. If a plugin and the user skipped the exact same version before.
4923
- *
4924
- * @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).
4925
- *
4926
- * @todo 4. If explicitly asked to retry after every activation.
4927
- */
4928
- if ( WP_FS__DEV_MODE ||
4929
- (
4930
- ( $this->is_plugin() || ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ) &&
4931
- $this->get_plugin_version() == $this->_storage->is_anonymous['version']
4932
- )
4933
- ) {
4934
- $this->reset_anonymous_mode();
4935
- }
4936
- }
4937
-
4938
- if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
4939
- /**
4940
- * If no previous version of plugin's version exist, it means that it's either
4941
- * the first time that the plugin installed on the site, or the plugin was installed
4942
- * before but didn't have Freemius integrated.
4943
- *
4944
- * Since register_activation_hook() do NOT fires on updates since 3.1, and only fires
4945
- * on manual activation via the dashboard, is_plugin_activation() is TRUE
4946
- * only after immediate activation.
4947
- *
4948
- * @since 1.1.4
4949
- * @link https://make.wordpress.org/core/2010/10/27/plugin-activation-hooks-no-longer-fire-for-updates/
4950
- */
4951
- $this->_storage->is_plugin_new_install = empty( $this->_storage->plugin_last_version );
4952
- }
4953
-
4954
- if ( ! $this->_anonymous_mode &&
4955
- $this->has_api_connectivity( WP_FS__DEV_MODE ) &&
4956
- ! $this->_isAutoInstall
4957
- ) {
4958
- // Store hint that the plugin was just activated to enable auto-redirection to settings.
4959
- add_option( 'fs_'
4960
- . ( $this->is_plugin() ? '' : $this->_module_type . '_' )
4961
- . "{$this->_slug}_activated", true );
4962
- }
4963
-
4964
- /**
4965
- * Activation hook is executed after the plugin's main file is loaded, therefore,
4966
- * after the plugin was loaded. The logic is located at activate_plugin()
4967
- * ./wp-admin/includes/plugin.php.
4968
- *
4969
- * @author Vova Feldman (@svovaf)
4970
- * @since 1.1.9
4971
- */
4972
- $this->_storage->was_plugin_loaded = true;
4973
- }
4974
 
4975
- /**
4976
- * Delete account.
4977
- *
4978
- * @author Vova Feldman (@svovaf)
4979
- * @since 1.0.3
4980
- *
4981
- * @param bool $check_user Enforce checking if user have plugins activation privileges.
4982
- */
4983
- function delete_account_event( $check_user = true ) {
4984
- $this->_logger->entrance( 'slug = ' . $this->_slug );
4985
 
4986
- if ( $check_user && ! $this->is_user_admin() ) {
4987
- return;
4988
- }
4989
 
4990
- $this->do_action( 'before_account_delete' );
 
 
 
 
 
 
 
4991
 
4992
- // Clear all admin notices.
4993
- $this->_admin_notices->clear_all_sticky();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4994
 
4995
- $this->_delete_site( false );
 
4996
 
4997
- $this->_delete_plans( false );
 
 
 
 
 
 
4998
 
4999
- $this->_delete_licenses( false );
5000
 
5001
- // Delete add-ons related to plugin's account.
5002
- $this->_delete_account_addons( false );
 
 
 
 
 
 
 
 
 
 
5003
 
5004
- // @todo Delete plans and licenses of add-ons.
 
 
 
5005
 
5006
- self::$_accounts->store();
 
 
 
5007
 
5008
- /**
5009
- * IMPORTANT:
5010
- * Clear crons must be executed before clearing all storage.
5011
- * Otherwise, the cron will not be cleared.
5012
- */
5013
- $this->clear_sync_cron();
5014
- $this->clear_install_sync_cron();
5015
-
5016
- // Clear all storage data.
5017
- $this->_storage->clear_all( true, array(
5018
- 'connectivity_test',
5019
- 'is_on',
5020
- ) );
5021
-
5022
- // Send delete event.
5023
- $this->get_api_site_scope()->call( '/', 'delete' );
5024
-
5025
- $this->do_action( 'after_account_delete' );
5026
- }
5027
-
5028
- /**
5029
- * Plugin deactivation hook.
5030
- *
5031
- * @author Vova Feldman (@svovaf)
5032
- * @since 1.0.1
5033
- */
5034
- function _deactivate_plugin_hook() {
5035
- $this->_logger->entrance( 'slug = ' . $this->_slug );
5036
-
5037
- if ( ! current_user_can( 'activate_plugins' ) ) {
5038
- return;
5039
- }
5040
-
5041
- $this->_admin_notices->clear_all_sticky();
5042
- if ( isset( $this->_storage->sticky_optin_added ) ) {
5043
- unset( $this->_storage->sticky_optin_added );
5044
- }
5045
-
5046
- if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
5047
- // Remember that plugin was already installed.
5048
- $this->_storage->is_plugin_new_install = false;
5049
- }
5050
-
5051
- // Hook to plugin uninstall.
5052
- register_uninstall_hook( $this->_plugin_main_file_path, array( 'Freemius', '_uninstall_plugin_hook' ) );
5053
-
5054
- $this->clear_module_main_file_cache();
5055
- $this->clear_sync_cron();
5056
- $this->clear_install_sync_cron();
5057
-
5058
- if ( $this->is_registered() ) {
5059
- // Send deactivation event.
5060
- $this->sync_install( array(
5061
- 'is_active' => false,
5062
- ) );
5063
- } else {
5064
- if ( ! $this->has_api_connectivity() ) {
5065
- // Reset connectivity test cache.
5066
- unset( $this->_storage->connectivity_test );
5067
- }
5068
- }
5069
-
5070
- // Clear API cache on deactivation.
5071
- FS_Api::clear_cache();
5072
-
5073
- $this->remove_sdk_reference();
5074
- }
5075
-
5076
- /**
5077
- * @author Vova Feldman (@svovaf)
5078
- * @since 1.1.6
5079
- */
5080
- private function remove_sdk_reference() {
5081
- global $fs_active_plugins;
5082
-
5083
- foreach ( $fs_active_plugins->plugins as $sdk_path => &$data ) {
5084
- if ( $this->_plugin_basename == $data->plugin_path ) {
5085
- unset( $fs_active_plugins->plugins[ $sdk_path ] );
5086
- break;
5087
- }
5088
- }
5089
-
5090
- fs_fallback_to_newest_active_sdk();
5091
- }
5092
-
5093
- /**
5094
- * @author Vova Feldman (@svovaf)
5095
- * @since 1.1.3
5096
- *
5097
- * @param bool $is_anonymous
5098
- */
5099
- private function set_anonymous_mode( $is_anonymous = true ) {
5100
- // Store information regarding skip to try and opt-in the user
5101
- // again in the future.
5102
- $this->_storage->is_anonymous = array(
5103
- 'is' => $is_anonymous,
5104
- 'timestamp' => WP_FS__SCRIPT_START_TIME,
5105
- 'version' => $this->get_plugin_version(),
5106
- );
5107
-
5108
- // Update anonymous mode cache.
5109
- $this->_is_anonymous = $is_anonymous;
5110
- }
5111
-
5112
- /**
5113
- * @author Vova Feldman (@svovaf)
5114
- * @since 1.1.3
5115
- */
5116
- private function reset_anonymous_mode() {
5117
- unset( $this->_storage->is_anonymous );
5118
-
5119
- /**
5120
- * Ensure that this field is also "false", otherwise, if the current module's type is "theme" and the module
5121
- * has no menus, the opt-in popup will not be shown immediately (in this case, the user will have to click
5122
- * on the admin notice that contains the opt-in link in order to trigger the opt-in popup).
5123
- *
5124
- * @author Leo Fajardo (@leorw)
5125
- * @since 1.2.2
5126
- */
5127
- unset( $this->_is_anonymous );
5128
- }
5129
-
5130
- /**
5131
- * Clears the anonymous mode and redirects to the opt-in screen.
5132
- *
5133
- * @author Vova Feldman (@svovaf)
5134
- * @since 1.1.7
5135
- */
5136
- function connect_again() {
5137
- if ( ! $this->is_anonymous() ) {
5138
- return;
5139
- }
5140
-
5141
- $this->reset_anonymous_mode();
5142
-
5143
- fs_redirect( $this->get_activation_url() );
5144
- }
5145
-
5146
- /**
5147
- * Skip account connect, and set anonymous mode.
5148
- *
5149
- * @author Vova Feldman (@svovaf)
5150
- * @since 1.1.1
5151
- */
5152
- private function skip_connection() {
5153
- $this->_logger->entrance();
5154
-
5155
- $this->_admin_notices->remove_sticky( 'connect_account' );
5156
-
5157
- $this->set_anonymous_mode();
5158
-
5159
- // Send anonymous skip event.
5160
- // No user identified info nor any tracking will be sent after the user skips the opt-in.
5161
- $this->get_api_plugin_scope()->call( 'skip.json', 'put', array(
5162
- 'uid' => $this->get_anonymous_id(),
5163
- ) );
5164
- }
5165
-
5166
- /**
5167
- * Plugin version update hook.
5168
- *
5169
- * @author Vova Feldman (@svovaf)
5170
- * @since 1.0.4
5171
- */
5172
- private function update_plugin_version_event() {
5173
- $this->_logger->entrance();
5174
-
5175
- if ( ! $this->is_registered() ) {
5176
- return;
5177
- }
5178
-
5179
- $this->schedule_install_sync();
5180
- // $this->sync_install( array(), true );
5181
- }
5182
-
5183
- /**
5184
- * Return a list of modified plugins since the last sync.
5185
- *
5186
- * Note:
5187
- * There's no point to store a plugins counter since even if the number of
5188
- * plugins didn't change, we still need to check if the versions are all the
5189
- * same and the activity state is similar.
5190
- *
5191
- * @author Vova Feldman (@svovaf)
5192
- * @since 1.1.8
5193
- *
5194
- * @return array|false
5195
- */
5196
- private function get_plugins_data_for_api() {
5197
- // Alias.
5198
- $option_name = 'all_plugins';
5199
-
5200
- $all_cached_plugins = self::$_accounts->get_option( $option_name );
5201
-
5202
- if ( ! is_object( $all_cached_plugins ) ) {
5203
- $all_cached_plugins = (object) array(
5204
- 'timestamp' => '',
5205
- 'md5' => '',
5206
- 'plugins' => array(),
5207
- );
5208
- }
5209
-
5210
- $time = time();
5211
-
5212
- if ( ! empty( $all_cached_plugins->timestamp ) &&
5213
- ( $time - $all_cached_plugins->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC
5214
- ) {
5215
- // Don't send plugin updates if last update was in the past 5 min.
5216
- return false;
5217
- }
5218
-
5219
- // Write timestamp to lock the logic.
5220
- $all_cached_plugins->timestamp = $time;
5221
- self::$_accounts->set_option( $option_name, $all_cached_plugins, true );
5222
-
5223
- // Reload options from DB.
5224
- self::$_accounts->load( true );
5225
- $all_cached_plugins = self::$_accounts->get_option( $option_name );
5226
-
5227
- if ( $time != $all_cached_plugins->timestamp ) {
5228
- // If timestamp is different, then another thread captured the lock.
5229
- return false;
5230
- }
5231
-
5232
- // Check if there's a change in plugins.
5233
- $all_plugins = self::get_all_plugins();
5234
-
5235
- // Check if plugins changed.
5236
- ksort( $all_plugins );
5237
-
5238
- $plugins_signature = '';
5239
- foreach ( $all_plugins as $basename => $data ) {
5240
- $plugins_signature .= $data['slug'] . ',' .
5241
- $data['Version'] . ',' .
5242
- ( $data['is_active'] ? '1' : '0' ) . ';';
5243
- }
5244
-
5245
- // Check if plugins status changed (version or active/inactive).
5246
- $plugins_changed = ( $all_cached_plugins->md5 !== md5( $plugins_signature ) );
5247
-
5248
- $plugins_update_data = array();
5249
-
5250
- if ( $plugins_changed ) {
5251
- // Change in plugins, report changes.
5252
-
5253
- // Update existing plugins info.
5254
- foreach ( $all_cached_plugins->plugins as $basename => $data ) {
5255
- if ( ! isset( $all_plugins[ $basename ] ) ) {
5256
- // Plugin uninstalled.
5257
- $uninstalled_plugin_data = $data;
5258
- $uninstalled_plugin_data['is_active'] = false;
5259
- $uninstalled_plugin_data['is_uninstalled'] = true;
5260
- $plugins_update_data[] = $uninstalled_plugin_data;
5261
-
5262
- unset( $all_plugins[ $basename ] );
5263
- unset( $all_cached_plugins->plugins[ $basename ] );
5264
- } else if ( $data['is_active'] !== $all_plugins[ $basename ]['is_active'] ||
5265
- $data['version'] !== $all_plugins[ $basename ]['Version']
5266
- ) {
5267
- // Plugin activated or deactivated, or version changed.
5268
- $all_cached_plugins->plugins[ $basename ]['is_active'] = $all_plugins[ $basename ]['is_active'];
5269
- $all_cached_plugins->plugins[ $basename ]['version'] = $all_plugins[ $basename ]['Version'];
5270
-
5271
- $plugins_update_data[] = $all_cached_plugins->plugins[ $basename ];
5272
- }
5273
- }
5274
-
5275
- // Find new plugins that weren't yet seen before.
5276
- foreach ( $all_plugins as $basename => $data ) {
5277
- if ( ! isset( $all_cached_plugins->plugins[ $basename ] ) ) {
5278
- // New plugin.
5279
- $new_plugin = array(
5280
- 'slug' => $data['slug'],
5281
- 'version' => $data['Version'],
5282
- 'title' => $data['Name'],
5283
- 'is_active' => $data['is_active'],
5284
- 'is_uninstalled' => false,
5285
- );
5286
-
5287
- $plugins_update_data[] = $new_plugin;
5288
- $all_cached_plugins->plugins[ $basename ] = $new_plugin;
5289
- }
5290
- }
5291
-
5292
- $all_cached_plugins->md5 = md5( $plugins_signature );
5293
- $all_cached_plugins->timestamp = $time;
5294
- self::$_accounts->set_option( $option_name, $all_cached_plugins, true );
5295
- }
5296
-
5297
- return $plugins_update_data;
5298
- }
5299
-
5300
- /**
5301
- * Return a list of modified themes since the last sync.
5302
- *
5303
- * Note:
5304
- * There's no point to store a themes counter since even if the number of
5305
- * themes didn't change, we still need to check if the versions are all the
5306
- * same and the activity state is similar.
5307
- *
5308
- * @author Vova Feldman (@svovaf)
5309
- * @since 1.1.8
5310
- *
5311
- * @return array|false
5312
- */
5313
- private function get_themes_data_for_api() {
5314
- // Alias.
5315
- $option_name = 'all_themes';
5316
-
5317
- $all_cached_themes = self::$_accounts->get_option( $option_name );
5318
-
5319
- if ( ! is_object( $all_cached_themes ) ) {
5320
- $all_cached_themes = (object) array(
5321
- 'timestamp' => '',
5322
- 'md5' => '',
5323
- 'themes' => array(),
5324
- );
5325
- }
5326
-
5327
- $time = time();
5328
-
5329
- if ( ! empty( $all_cached_themes->timestamp ) &&
5330
- ( $time - $all_cached_themes->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC
5331
- ) {
5332
- // Don't send theme updates if last update was in the past 5 min.
5333
- return false;
5334
- }
5335
-
5336
- // Write timestamp to lock the logic.
5337
- $all_cached_themes->timestamp = $time;
5338
- self::$_accounts->set_option( $option_name, $all_cached_themes, true );
5339
-
5340
- // Reload options from DB.
5341
- self::$_accounts->load( true );
5342
- $all_cached_themes = self::$_accounts->get_option( $option_name );
5343
-
5344
- if ( $time != $all_cached_themes->timestamp ) {
5345
- // If timestamp is different, then another thread captured the lock.
5346
- return false;
5347
- }
5348
-
5349
- // Get active theme.
5350
- $active_theme = wp_get_theme();
5351
- $active_theme_stylesheet = $active_theme->get_stylesheet();
5352
-
5353
- // Check if there's a change in themes.
5354
- $all_themes = wp_get_themes();
5355
-
5356
- // Check if themes changed.
5357
- ksort( $all_themes );
5358
-
5359
- $themes_signature = '';
5360
- foreach ( $all_themes as $slug => $data ) {
5361
- $is_active = ( $slug === $active_theme_stylesheet );
5362
- $themes_signature .= $slug . ',' .
5363
- $data->version . ',' .
5364
- ( $is_active ? '1' : '0' ) . ';';
5365
- }
5366
-
5367
- // Check if themes status changed (version or active/inactive).
5368
- $themes_changed = ( $all_cached_themes->md5 !== md5( $themes_signature ) );
5369
-
5370
- $themes_update_data = array();
5371
-
5372
- if ( $themes_changed ) {
5373
- // Change in themes, report changes.
5374
-
5375
- // Update existing themes info.
5376
- foreach ( $all_cached_themes->themes as $slug => $data ) {
5377
- $is_active = ( $slug === $active_theme_stylesheet );
5378
-
5379
- if ( ! isset( $all_themes[ $slug ] ) ) {
5380
- // Plugin uninstalled.
5381
- $uninstalled_theme_data = $data;
5382
- $uninstalled_theme_data['is_active'] = false;
5383
- $uninstalled_theme_data['is_uninstalled'] = true;
5384
- $themes_update_data[] = $uninstalled_theme_data;
5385
-
5386
- unset( $all_themes[ $slug ] );
5387
- unset( $all_cached_themes->themes[ $slug ] );
5388
- } else if ( $data['is_active'] !== $is_active ||
5389
- $data['version'] !== $all_themes[ $slug ]->version
5390
- ) {
5391
- // Plugin activated or deactivated, or version changed.
5392
-
5393
- $all_cached_themes->themes[ $slug ]['is_active'] = $is_active;
5394
- $all_cached_themes->themes[ $slug ]['version'] = $all_themes[ $slug ]->version;
5395
-
5396
- $themes_update_data[] = $all_cached_themes->themes[ $slug ];
5397
- }
5398
- }
5399
-
5400
- // Find new themes that weren't yet seen before.
5401
- foreach ( $all_themes as $slug => $data ) {
5402
- if ( ! isset( $all_cached_themes->themes[ $slug ] ) ) {
5403
- $is_active = ( $slug === $active_theme_stylesheet );
5404
-
5405
- // New plugin.
5406
- $new_plugin = array(
5407
- 'slug' => $slug,
5408
- 'version' => $data->version,
5409
- 'title' => $data->name,
5410
- 'is_active' => $is_active,
5411
- 'is_uninstalled' => false,
5412
- );
5413
-
5414
- $themes_update_data[] = $new_plugin;
5415
- $all_cached_themes->themes[ $slug ] = $new_plugin;
5416
- }
5417
- }
5418
-
5419
- $all_cached_themes->md5 = md5( $themes_signature );
5420
- $all_cached_themes->timestamp = time();
5421
- self::$_accounts->set_option( $option_name, $all_cached_themes, true );
5422
- }
5423
-
5424
- return $themes_update_data;
5425
- }
5426
-
5427
- /**
5428
- * Update install details.
5429
- *
5430
- * @author Vova Feldman (@svovaf)
5431
- * @since 1.1.2
5432
- *
5433
- * @param string[] string $override
5434
- * @param bool $include_plugins Since 1.1.8 by default include plugin changes.
5435
- * @param bool $include_themes Since 1.1.8 by default include plugin changes.
5436
- *
5437
- * @return array
5438
- */
5439
- private function get_install_data_for_api(
5440
- array $override,
5441
- $include_plugins = true,
5442
- $include_themes = true
5443
- ) {
5444
- /**
5445
- * @since 1.1.8 Also send plugin updates.
5446
- */
5447
- if ( $include_plugins && ! isset( $override['plugins'] ) ) {
5448
- $plugins = $this->get_plugins_data_for_api();
5449
- if ( ! empty( $plugins ) ) {
5450
- $override['plugins'] = $plugins;
5451
- }
5452
- }
5453
- /**
5454
- * @since 1.1.8 Also send themes updates.
5455
- */
5456
- if ( $include_themes && ! isset( $override['themes'] ) ) {
5457
- $themes = $this->get_themes_data_for_api();
5458
- if ( ! empty( $themes ) ) {
5459
- $override['themes'] = $themes;
5460
- }
5461
- }
5462
-
5463
- return array_merge( array(
5464
- 'version' => $this->get_plugin_version(),
5465
- 'is_premium' => $this->is_premium(),
5466
- 'language' => get_bloginfo( 'language' ),
5467
- 'charset' => get_bloginfo( 'charset' ),
5468
- 'platform_version' => get_bloginfo( 'version' ),
5469
- 'sdk_version' => $this->version,
5470
- 'programming_language_version' => phpversion(),
5471
- 'title' => get_bloginfo( 'name' ),
5472
- 'url' => get_site_url(),
5473
- // Special params.
5474
- 'is_active' => true,
5475
- 'is_disconnected' => $this->is_tracking_prohibited(),
5476
- 'is_uninstalled' => false,
5477
- ), $override );
5478
- }
5479
-
5480
- /**
5481
- * Update install only if changed.
5482
- *
5483
- * @author Vova Feldman (@svovaf)
5484
- * @since 1.0.9
5485
- *
5486
- * @param string[] string $override
5487
- * @param bool $flush
5488
- *
5489
- * @return false|object|string
5490
- */
5491
- private function send_install_update( $override = array(), $flush = false ) {
5492
- $this->_logger->entrance();
5493
-
5494
- $check_properties = $this->get_install_data_for_api( $override );
5495
-
5496
- if ( $flush ) {
5497
- $params = $check_properties;
5498
- } else {
5499
- $params = array();
5500
- $special = array();
5501
- $special_override = false;
5502
-
5503
- foreach ( $check_properties as $p => $v ) {
5504
- if ( property_exists( $this->_site, $p ) ) {
5505
- if ( ( is_bool( $this->_site->{$p} ) || ! empty( $this->_site->{$p} ) ) &&
5506
- $this->_site->{$p} != $v
5507
- ) {
5508
- $this->_site->{$p} = $v;
5509
- $params[ $p ] = $v;
5510
- }
5511
- } else {
5512
- $special[ $p ] = $v;
5513
-
5514
- if ( isset( $override[ $p ] ) ||
5515
- 'plugins' === $p ||
5516
- 'themes' === $p
5517
- ) {
5518
- $special_override = true;
5519
- }
5520
- }
5521
- }
5522
-
5523
- if ( $special_override || 0 < count( $params ) ) {
5524
- // Add special params only if has at least one
5525
- // standard param, or if explicitly requested to
5526
- // override a special param or a param which is not exist
5527
- // in the install object.
5528
- $params = array_merge( $params, $special );
5529
- }
5530
- }
5531
-
5532
- if ( 0 < count( $params ) ) {
5533
- // Update last install sync timestamp.
5534
- $this->_storage->install_sync_timestamp = time();
5535
-
5536
- $params['uid'] = $this->get_anonymous_id();
5537
-
5538
- // Send updated values to FS.
5539
- $site = $this->get_api_site_scope()->call( '/', 'put', $params );
5540
-
5541
- if ( $this->is_api_result_entity( $site ) ) {
5542
- // I successfully sent install update, clear scheduled sync if exist.
5543
- $this->clear_install_sync_cron();
5544
- }
5545
-
5546
- return $site;
5547
- }
5548
-
5549
- return false;
5550
- }
5551
-
5552
- /**
5553
- * Update install only if changed.
5554
- *
5555
- * @author Vova Feldman (@svovaf)
5556
- * @since 1.0.9
5557
- *
5558
- * @param string[] string $override
5559
- * @param bool $flush
5560
- */
5561
- private function sync_install( $override = array(), $flush = false ) {
5562
- $this->_logger->entrance();
5563
-
5564
- $site = $this->send_install_update( $override, $flush );
5565
-
5566
- if ( false === $site ) {
5567
- // No sync required.
5568
- return;
5569
- }
5570
-
5571
- if ( ! $this->is_api_result_entity( $site ) ) {
5572
- // Failed to sync, don't update locally.
5573
- return;
5574
- }
5575
-
5576
- $plan = $this->get_plan();
5577
- $this->_site = new FS_Site( $site );
5578
- $this->_site->plan = $plan;
5579
-
5580
- $this->_store_site( true );
5581
- }
5582
-
5583
- /**
5584
- * Track install's custom event.
5585
- *
5586
- * IMPORTANT:
5587
- * Custom event tracking is currently only supported for specific clients.
5588
- * If you are not one of them, please don't use this method. If you will,
5589
- * the API will simply ignore your request based on the plugin ID.
5590
- *
5591
- * Need custom tracking for your plugin or theme?
5592
- * If you are interested in custom event tracking please contact yo@freemius.com
5593
- * for further details.
5594
- *
5595
- * @author Vova Feldman (@svovaf)
5596
- * @since 1.2.1
5597
- *
5598
- * @param string $name Event name.
5599
- * @param array $properties Associative key/value array with primitive values only
5600
- * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s.
5601
- * @param bool $once If true, event will be tracked only once. IMPORTANT: Still trigger the API call.
5602
- *
5603
- * @return object|false Event data or FALSE on failure.
5604
- *
5605
- * @throws \Freemius_InvalidArgumentException
5606
- */
5607
- public function track_event( $name, $properties = array(), $process_at = false, $once = false ) {
5608
- $this->_logger->entrance( http_build_query( array( 'name' => $name, 'once' => $once ) ) );
5609
-
5610
- if ( ! $this->is_registered() ) {
5611
- return false;
5612
- }
5613
-
5614
- $event = array( 'type' => $name );
5615
-
5616
- if ( is_numeric( $process_at ) && $process_at > time() ) {
5617
- $event['process_at'] = $process_at;
5618
- }
5619
-
5620
- if ( $once ) {
5621
- $event['once'] = true;
5622
- }
5623
-
5624
- if ( ! empty( $properties ) ) {
5625
- // Verify associative array values are primitive.
5626
- foreach ( $properties as $k => $v ) {
5627
- if ( ! is_scalar( $v ) ) {
5628
- throw new Freemius_InvalidArgumentException( 'The $properties argument must be an associative key/value array with primitive values only.' );
5629
- }
5630
- }
5631
-
5632
- $event['properties'] = $properties;
5633
- }
5634
-
5635
- $result = $this->get_api_site_scope()->call( 'events.json', 'post', $event );
5636
-
5637
- return $this->is_api_error( $result ) ?
5638
- false :
5639
- $result;
5640
- }
5641
-
5642
- /**
5643
- * Track install's custom event only once, but it still triggers the API call.
5644
- *
5645
- * IMPORTANT:
5646
- * Custom event tracking is currently only supported for specific clients.
5647
- * If you are not one of them, please don't use this method. If you will,
5648
- * the API will simply ignore your request based on the plugin ID.
5649
- *
5650
- * Need custom tracking for your plugin or theme?
5651
- * If you are interested in custom event tracking please contact yo@freemius.com
5652
- * for further details.
5653
- *
5654
- * @author Vova Feldman (@svovaf)
5655
- * @since 1.2.1
5656
- *
5657
- * @param string $name Event name.
5658
- * @param array $properties Associative key/value array with primitive values only
5659
- * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s.
5660
- *
5661
- * @return object|false Event data or FALSE on failure.
5662
- *
5663
- * @throws \Freemius_InvalidArgumentException
5664
- *
5665
- * @user Freemius::track_event()
5666
- */
5667
- public function track_event_once( $name, $properties = array(), $process_at = false ) {
5668
- return $this->track_event( $name, $properties, $process_at, true );
5669
- }
5670
-
5671
- /**
5672
- * Plugin uninstall hook.
5673
- *
5674
- * @author Vova Feldman (@svovaf)
5675
- * @since 1.0.1
5676
- *
5677
- * @param bool $check_user Enforce checking if user have plugins activation privileges.
5678
- */
5679
- function _uninstall_plugin_event( $check_user = true ) {
5680
- $this->_logger->entrance( 'slug = ' . $this->_slug );
5681
-
5682
- if ( $check_user && ! current_user_can( 'activate_plugins' ) ) {
5683
- return;
5684
- }
5685
-
5686
- $params = array();
5687
- $uninstall_reason = null;
5688
- if ( isset( $this->_storage->uninstall_reason ) ) {
5689
- $uninstall_reason = $this->_storage->uninstall_reason;
5690
- $params['reason_id'] = $uninstall_reason->id;
5691
- $params['reason_info'] = $uninstall_reason->info;
5692
- }
5693
-
5694
- if ( ! $this->is_registered() ) {
5695
- // Send anonymous uninstall event only if user submitted a feedback.
5696
- if ( isset( $uninstall_reason ) ) {
5697
- if ( isset( $uninstall_reason->is_anonymous ) && ! $uninstall_reason->is_anonymous ) {
5698
- $this->opt_in( false, false, false, false, true );
5699
- } else {
5700
- $params['uid'] = $this->get_anonymous_id();
5701
- $this->get_api_plugin_scope()->call( 'uninstall.json', 'put', $params );
5702
- }
5703
- }
5704
- } else {
5705
- // Send uninstall event.
5706
- $this->send_install_update( array_merge( $params, array(
5707
- 'is_active' => false,
5708
- 'is_uninstalled' => true,
5709
- ) ) );
5710
- }
5711
-
5712
- // @todo Decide if we want to delete plugin information from db.
5713
- }
5714
-
5715
- /**
5716
- * @author Vova Feldman (@svovaf)
5717
- * @since 1.1.1
5718
- *
5719
- * @return string
5720
- */
5721
- function premium_plugin_basename() {
5722
- return "{$this->_slug}-premium/" . basename( $this->_free_plugin_basename );
5723
- }
5724
-
5725
- /**
5726
- * Uninstall plugin hook. Called only when connected his account with Freemius for active sites tracking.
5727
- *
5728
- * @author Vova Feldman (@svovaf)
5729
- * @since 1.0.2
5730
- */
5731
- public static function _uninstall_plugin_hook() {
5732
- self::_load_required_static();
5733
-
5734
- self::$_static_logger->entrance();
5735
-
5736
- if ( ! current_user_can( 'activate_plugins' ) ) {
5737
- return;
5738
- }
5739
-
5740
- $plugin_file = substr( current_filter(), strlen( 'uninstall_' ) );
5741
-
5742
- self::$_static_logger->info( 'plugin = ' . $plugin_file );
5743
-
5744
- define( 'WP_FS__UNINSTALL_MODE', true );
5745
-
5746
- $fs = self::get_instance_by_file( $plugin_file );
5747
-
5748
- if ( is_object( $fs ) ) {
5749
- self::require_plugin_essentials();
5750
-
5751
- if ( is_plugin_active( $fs->_free_plugin_basename ) ||
5752
- is_plugin_active( $fs->premium_plugin_basename() )
5753
- ) {
5754
- // Deleting Free or Premium plugin version while the other version still installed.
5755
- return;
5756
- }
5757
-
5758
- $fs->_uninstall_plugin_event();
5759
-
5760
- $fs->do_action( 'after_uninstall' );
5761
- }
5762
- }
5763
-
5764
- #----------------------------------------------------------------------------------
5765
- #region Plugin Information
5766
- #----------------------------------------------------------------------------------
5767
-
5768
- /**
5769
- * Load WordPress core plugin.php essential module.
5770
- *
5771
- * @author Vova Feldman (@svovaf)
5772
- * @since 1.1.1
5773
- */
5774
- private static function require_plugin_essentials() {
5775
- if ( ! function_exists( 'get_plugins' ) ) {
5776
- self::$_static_logger->log( 'Including wp-admin/includes/plugin.php...' );
5777
-
5778
- require_once ABSPATH . 'wp-admin/includes/plugin.php';
5779
- }
5780
- }
5781
-
5782
- /**
5783
- * Load WordPress core pluggable.php module.
5784
- *
5785
- * @author Vova Feldman (@svovaf)
5786
- * @since 1.1.2
5787
- */
5788
- private static function require_pluggable_essentials() {
5789
- if ( ! function_exists( 'wp_get_current_user' ) ) {
5790
- require_once ABSPATH . 'wp-includes/pluggable.php';
5791
- }
5792
- }
5793
-
5794
- /**
5795
- * Return plugin data.
5796
- *
5797
- * @author Vova Feldman (@svovaf)
5798
- * @since 1.0.1
5799
- *
5800
- * @return array
5801
- */
5802
- function get_plugin_data() {
5803
- if ( ! isset( $this->_plugin_data ) ) {
5804
- self::require_plugin_essentials();
5805
-
5806
- if ( $this->is_plugin() ) {
5807
- /**
5808
- * @author Vova Feldman (@svovaf)
5809
- * @since 1.2.0 When using get_plugin_data() do NOT translate plugin data.
5810
- *
5811
- * @link https://github.com/Freemius/wordpress-sdk/issues/77
5812
- */
5813
- $plugin_data = get_plugin_data(
5814
- $this->_plugin_main_file_path,
5815
- false,
5816
- false
5817
- );
5818
- } else {
5819
- $theme_data = wp_get_theme();
5820
-
5821
- $plugin_data = array(
5822
- 'Name' => $theme_data->get( 'Name' ),
5823
- 'Version' => $theme_data->get( 'Version' ),
5824
- 'Author' => $theme_data->get( 'Author' ),
5825
- 'Description' => $theme_data->get( 'Description' ),
5826
- 'PluginURI' => $theme_data->get( 'ThemeURI' ),
5827
- );
5828
- }
5829
-
5830
- $this->_plugin_data = $plugin_data;
5831
- }
5832
-
5833
- return $this->_plugin_data;
5834
- }
5835
-
5836
- /**
5837
- * @author Vova Feldman (@svovaf)
5838
- * @since 1.0.1
5839
- * @since 1.2.2.5 If slug not set load slug by module ID.
5840
- *
5841
- * @return string Plugin slug.
5842
- */
5843
- function get_slug() {
5844
- if ( ! isset( $this->_slug ) ) {
5845
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
5846
- $this->_slug = $id_slug_type_path_map[ $this->_module_id ]['slug'];
5847
- }
5848
-
5849
- return $this->_slug;
5850
- }
5851
-
5852
- /**
5853
- * @author Vova Feldman (@svovaf)
5854
- * @since 1.2.1.7
5855
- *
5856
- * @return string Plugin slug.
5857
- */
5858
- function get_target_folder_name() {
5859
- return $this->_slug . ( $this->can_use_premium_code() ? '-premium' : '' );
5860
- }
5861
-
5862
- /**
5863
- * @author Vova Feldman (@svovaf)
5864
- * @since 1.0.1
5865
- *
5866
- * @return number Plugin ID.
5867
- */
5868
- function get_id() {
5869
- return $this->_plugin->id;
5870
- }
5871
-
5872
- /**
5873
- * @author Vova Feldman (@svovaf)
5874
- * @since 1.2.1.5
5875
- *
5876
- * @return string Freemius SDK version
5877
- */
5878
- function get_sdk_version() {
5879
- return $this->version;
5880
- }
5881
-
5882
- /**
5883
- * @author Vova Feldman (@svovaf)
5884
- * @since 1.2.1.5
5885
- *
5886
- * @return number Parent plugin ID (if parent exist).
5887
- */
5888
- function get_parent_id() {
5889
- return $this->is_addon() ?
5890
- $this->get_parent_instance()->get_id() :
5891
- $this->_plugin->id;
5892
- }
5893
-
5894
- /**
5895
- * @author Vova Feldman (@svovaf)
5896
- * @since 1.0.1
5897
- *
5898
- * @return string Plugin public key.
5899
- */
5900
- function get_public_key() {
5901
- return $this->_plugin->public_key;
5902
- }
5903
-
5904
- /**
5905
- * Will be available only on sandbox mode.
5906
- *
5907
- * @author Vova Feldman (@svovaf)
5908
- * @since 1.0.4
5909
- *
5910
- * @return mixed Plugin secret key.
5911
- */
5912
- function get_secret_key() {
5913
- return $this->_plugin->secret_key;
5914
- }
5915
-
5916
- /**
5917
- * @author Vova Feldman (@svovaf)
5918
- * @since 1.1.1
5919
- *
5920
- * @return bool
5921
- */
5922
- function has_secret_key() {
5923
- return ! empty( $this->_plugin->secret_key );
5924
- }
5925
-
5926
- /**
5927
- * @author Vova Feldman (@svovaf)
5928
- * @since 1.0.9
5929
- *
5930
- * @return string
5931
- */
5932
- function get_plugin_name() {
5933
- $this->_logger->entrance();
5934
-
5935
- if ( ! isset( $this->_plugin_name ) ) {
5936
- $plugin_data = $this->get_plugin_data();
5937
-
5938
- // Get name.
5939
- $this->_plugin_name = $plugin_data['Name'];
5940
-
5941
- // Check if plugin name contains "(Premium)" suffix and remove it.
5942
- $suffix = ' (premium)';
5943
- $suffix_len = strlen( $suffix );
5944
-
5945
- if ( strlen( $plugin_data['Name'] ) > $suffix_len &&
5946
- $suffix === substr( strtolower( $plugin_data['Name'] ), - $suffix_len )
5947
- ) {
5948
- $this->_plugin_name = substr( $plugin_data['Name'], 0, - $suffix_len );
5949
- }
5950
-
5951
- $this->_logger->departure( 'Name =
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
+ * @param bool $ignore_free_wp_org_theme_context This is used to decide if the associated tab should be shown
537
+ * or hidden.
538
+ *
539
+ * @return bool
540
+ */
541
+ function is_submenu_item_visible( $slug, $ignore_free_wp_org_theme_context = false ) {
542
+ if ( $this->is_admin_page( $slug ) ) {
543
+ /**
544
+ * It is the current context page, so show the submenu item
545
+ * so the user will have the right context page, even if it
546
+ * was set to hidden.
547
+ */
548
+ return true;
549
+ }
550
+
551
+ if ( ! $this->has_settings_menu() ) {
552
+ // No menu settings at all.
553
+ return false;
554
+ }
555
+
556
+ if ( ! $ignore_free_wp_org_theme_context && $this->is_free_wp_org_theme() ) {
557
+ /**
558
+ * wp.org themes are limited to a single submenu item, and
559
+ * sub-submenu items are most likely not allowed (never verified).
560
+ */
561
+ return false;
562
+ }
563
+
564
+ return $this->_menu->is_submenu_item_visible( $slug );
565
+ }
566
+
567
+ /**
568
+ * Check if a Freemius page should be accessible via the UI.
569
+ *
570
+ * @author Vova Feldman (@svovaf)
571
+ * @since 1.2.2.7
572
+ *
573
+ * @param string $slug
574
+ *
575
+ * @return bool
576
+ */
577
+ function is_page_visible( $slug ) {
578
+ if ( $this->is_admin_page( $slug ) ) {
579
+ return true;
580
+ }
581
+
582
+ return $this->_menu->is_submenu_item_visible( $slug, true, true );
583
+ }
584
+
585
+ /**
586
+ * @author Vova Feldman (@svovaf)
587
+ * @since 1.0.9
588
+ */
589
+ private function _version_updates_handler() {
590
+ if ( ! isset( $this->_storage->sdk_version ) || $this->_storage->sdk_version != $this->version ) {
591
+ // Freemius version upgrade mode.
592
+ $this->_storage->sdk_last_version = $this->_storage->sdk_version;
593
+ $this->_storage->sdk_version = $this->version;
594
+
595
+ if ( empty( $this->_storage->sdk_last_version ) ||
596
+ version_compare( $this->_storage->sdk_last_version, $this->version, '<' )
597
+ ) {
598
+ $this->_storage->sdk_upgrade_mode = true;
599
+ $this->_storage->sdk_downgrade_mode = false;
600
+ } else {
601
+ $this->_storage->sdk_downgrade_mode = true;
602
+ $this->_storage->sdk_upgrade_mode = false;
603
+
604
+ }
605
+
606
+ $this->do_action( 'sdk_version_update', $this->_storage->sdk_last_version, $this->version );
607
+ }
608
+
609
+ $plugin_version = $this->get_plugin_version();
610
+ if ( ! isset( $this->_storage->plugin_version ) || $this->_storage->plugin_version != $plugin_version ) {
611
+ // Plugin version upgrade mode.
612
+ $this->_storage->plugin_last_version = $this->_storage->plugin_version;
613
+ $this->_storage->plugin_version = $plugin_version;
614
+
615
+ if ( empty( $this->_storage->plugin_last_version ) ||
616
+ version_compare( $this->_storage->plugin_last_version, $plugin_version, '<' )
617
+ ) {
618
+ $this->_storage->plugin_upgrade_mode = true;
619
+ $this->_storage->plugin_downgrade_mode = false;
620
+ } else {
621
+ $this->_storage->plugin_downgrade_mode = true;
622
+ $this->_storage->plugin_upgrade_mode = false;
623
+ }
624
+
625
+ if ( ! empty( $this->_storage->plugin_last_version ) ) {
626
+ // Different version of the plugin was installed before, therefore it's an update.
627
+ $this->_storage->is_plugin_new_install = false;
628
+ }
629
+
630
+ $this->do_action( 'plugin_version_update', $this->_storage->plugin_last_version, $plugin_version );
631
+ }
632
+ }
633
+
634
+ #--------------------------------------------------------------------------------
635
+ #region Data Migration on SDK Update
636
+ #--------------------------------------------------------------------------------
637
+
638
+ /**
639
+ * @author Vova Feldman (@svovaf)
640
+ * @since 1.1.5
641
+ *
642
+ * @param string $sdk_prev_version
643
+ * @param string $sdk_version
644
+ */
645
+ function _sdk_version_update( $sdk_prev_version, $sdk_version ) {
646
+ /**
647
+ * @since 1.1.7.3 Fixed unwanted connectivity test cleanup.
648
+ */
649
+ if ( empty( $sdk_prev_version ) ) {
650
+ return;
651
+ }
652
+
653
+ if ( version_compare( $sdk_prev_version, '2.1.0', '<' ) &&
654
+ version_compare( $sdk_version, '2.1.0', '>=' )
655
+ ) {
656
+ $this->_storage->handle_gdpr_admin_notice = true;
657
+ }
658
+
659
+ if ( version_compare( $sdk_prev_version, '2.0.0', '<' ) &&
660
+ version_compare( $sdk_version, '2.0.0', '>=' )
661
+ ) {
662
+ $this->migrate_to_subscriptions_collection();
663
+
664
+ $this->consolidate_licenses();
665
+
666
+ // Clear trial_plan since it's now loaded from the plans collection when needed.
667
+ $this->_storage->remove( 'trial_plan', true, false );
668
+ }
669
+
670
+ if ( version_compare( $sdk_prev_version, '1.2.3', '<' ) &&
671
+ version_compare( $sdk_version, '1.2.3', '>=' )
672
+ ) {
673
+ /**
674
+ * 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.
675
+ *
676
+ * @author Leo Fajardo (@leorw)
677
+ */
678
+ $this->make_paths_relative();
679
+ }
680
+
681
+ if ( version_compare( $sdk_prev_version, '1.1.5', '<' ) &&
682
+ version_compare( $sdk_version, '1.1.5', '>=' )
683
+ ) {
684
+ // On version 1.1.5 merged connectivity and is_on data.
685
+ if ( isset( $this->_storage->connectivity_test ) ) {
686
+ if ( ! isset( $this->_storage->is_on ) ) {
687
+ unset( $this->_storage->connectivity_test );
688
+ } else {
689
+ $connectivity_data = $this->_storage->connectivity_test;
690
+ $connectivity_data['is_active'] = $this->_storage->is_on['is_active'];
691
+ $connectivity_data['timestamp'] = $this->_storage->is_on['timestamp'];
692
+
693
+ // Override.
694
+ $this->_storage->connectivity_test = $connectivity_data;
695
+
696
+ // Remove previous structure.
697
+ unset( $this->_storage->is_on );
698
+ }
699
+
700
+ }
701
+ }
702
+ }
703
+
704
+ /**
705
+ * @author Leo Fajardo (@leorw)
706
+ * @since 2.0.0
707
+ *
708
+ * @param \FS_Storage $storage
709
+ * @param bool|int|null $blog_id
710
+ */
711
+ private static function migrate_install_plan_to_plan_id( FS_Storage $storage, $blog_id = null ) {
712
+ if ( empty( $storage->sdk_version ) ) {
713
+ // New installation of the plugin, no need to upgrade.
714
+ return;
715
+ }
716
+
717
+ if ( ! version_compare( $storage->sdk_version, '2.0.0', '<' ) ) {
718
+ // Previous version is >= 2.0.0, so no need to migrate.
719
+ return;
720
+ }
721
+
722
+ // Alias.
723
+ $module_type = $storage->get_module_type();
724
+ $module_slug = $storage->get_module_slug();
725
+
726
+ $installs = self::get_all_sites( $module_type, $blog_id );
727
+ $install = isset( $installs[ $module_slug ] ) ? $installs[ $module_slug ] : null;
728
+
729
+ if ( ! is_object( $install ) ) {
730
+ return;
731
+ }
732
+
733
+ if ( isset( $install->plan ) && is_object( $install->plan ) ) {
734
+ if ( isset( $install->plan->id ) && ! empty( $install->plan->id ) ) {
735
+ $install->plan_id = self::_decrypt( $install->plan->id );
736
+ }
737
+
738
+ unset( $install->plan );
739
+
740
+ $installs[ $module_slug ] = clone $install;
741
+
742
+ self::set_account_option_by_module(
743
+ $module_type,
744
+ 'sites',
745
+ $installs,
746
+ true,
747
+ $blog_id
748
+ );
749
+ }
750
+ }
751
+
752
+ /**
753
+ * @author Leo Fajardo (@leorw)
754
+ * @since 2.0.0
755
+ */
756
+ private function migrate_to_subscriptions_collection() {
757
+ if ( ! is_object( $this->_site ) ) {
758
+ return;
759
+ }
760
+
761
+ if ( isset( $this->_storage->subscription ) && is_object( $this->_storage->subscription ) ) {
762
+ $this->_storage->subscriptions = array( $this->_storage->subscription );
763
+ }
764
+ }
765
+
766
+ /**
767
+ * @author Leo Fajardo (@leorw)
768
+ * @since 2.0.0
769
+ */
770
+ private function consolidate_licenses() {
771
+ $plugin_licenses = self::get_account_option( 'licenses', WP_FS__MODULE_TYPE_PLUGIN );
772
+ if ( isset( $plugin_licenses[ $this->_slug ] ) ) {
773
+ $plugin_licenses = $plugin_licenses[ $this->_slug ];
774
+ } else {
775
+ $plugin_licenses = array();
776
+ }
777
+
778
+ $theme_licenses = self::get_account_option( 'licenses', WP_FS__MODULE_TYPE_THEME );
779
+ if ( isset( $theme_licenses[ $this->_slug ] ) ) {
780
+ $theme_licenses = $theme_licenses[ $this->_slug ];
781
+ } else {
782
+ $theme_licenses = array();
783
+ }
784
+
785
+ if ( empty( $plugin_licenses ) && empty( $theme_licenses ) ) {
786
+ return;
787
+ }
788
+
789
+ $all_licenses = array();
790
+ $user_id_license_ids_map = array();
791
+
792
+ foreach ( $plugin_licenses as $user_id => $user_licenses ) {
793
+ if ( is_array( $user_licenses ) ) {
794
+ if ( ! isset( $user_license_ids[ $user_id ] ) ) {
795
+ $user_id_license_ids_map[ $user_id ] = array();
796
+ }
797
+
798
+ foreach ( $user_licenses as $user_license ) {
799
+ $all_licenses[] = $user_license;
800
+ $user_id_license_ids_map[ $user_id ][] = $user_license->id;
801
+ }
802
+ }
803
+ }
804
+
805
+ foreach ( $theme_licenses as $user_id => $user_licenses ) {
806
+ if ( is_array( $user_licenses ) ) {
807
+ if ( ! isset( $user_license_ids[ $user_id ] ) ) {
808
+ $user_id_license_ids_map[ $user_id ] = array();
809
+ }
810
+
811
+ foreach ( $user_licenses as $user_license ) {
812
+ $all_licenses[] = $user_license;
813
+ $user_id_license_ids_map[ $user_id ][] = $user_license->id;
814
+ }
815
+ }
816
+ }
817
+
818
+ self::store_user_id_license_ids_map(
819
+ $user_id_license_ids_map,
820
+ $this->_module_id
821
+ );
822
+
823
+ $this->_store_licenses( true, $this->_module_id, $all_licenses );
824
+ }
825
+
826
+ /**
827
+ * Makes paths relative.
828
+ *
829
+ * @author Leo Fajardo (@leorw)
830
+ * @since 1.2.3
831
+ */
832
+ private function make_paths_relative() {
833
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
834
+
835
+ if ( isset( $id_slug_type_path_map[ $this->_module_id ]['path'] ) ) {
836
+ $id_slug_type_path_map[ $this->_module_id ]['path'] = $this->get_relative_path( $id_slug_type_path_map[ $this->_module_id ]['path'] );
837
+
838
+ self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
839
+ }
840
+
841
+ if ( isset( $this->_storage->plugin_main_file ) ) {
842
+ $plugin_main_file = $this->_storage->plugin_main_file;
843
+
844
+ if ( isset( $plugin_main_file->path ) ) {
845
+ $this->_storage->plugin_main_file->path = $this->get_relative_path( $this->_storage->plugin_main_file->path );
846
+ } else if ( isset( $plugin_main_file->prev_path ) ) {
847
+ $this->_storage->plugin_main_file->prev_path = $this->get_relative_path( $this->_storage->plugin_main_file->prev_path );
848
+ }
849
+ }
850
+
851
+ // Remove invalid path that is still associated with the current slug if there's any.
852
+ $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
853
+ foreach ( $file_slug_map as $plugin_basename => $slug ) {
854
+ if ( $slug === $this->_slug &&
855
+ $plugin_basename !== $this->_plugin_basename &&
856
+ ! file_exists( $this->get_absolute_path( $plugin_basename ) )
857
+ ) {
858
+ unset( $file_slug_map[ $plugin_basename ] );
859
+ self::$_accounts->set_option( 'file_slug_map', $file_slug_map, true );
860
+
861
+ break;
862
+ }
863
+ }
864
+ }
865
+
866
+ /**
867
+ * @author Vova Feldman (@svovaf)
868
+ * @since 1.2.2.7
869
+ *
870
+ * @param string $plugin_prev_version
871
+ * @param string $plugin_version
872
+ */
873
+ function _after_version_update( $plugin_prev_version, $plugin_version ) {
874
+ if ( $this->is_theme() ) {
875
+ // Expire the cache of the previous tabs since the theme may
876
+ // have setting updates.
877
+ $this->_cache->expire( 'tabs' );
878
+ $this->_cache->expire( 'tabs_stylesheets' );
879
+ }
880
+ }
881
+
882
+ /**
883
+ * A special migration logic for the $_accounts, executed for all the plugins in the system:
884
+ * - Moves some data to the network level storage.
885
+ * - If the plugin's connection was skipped for all sites, set the plugin as if it was network skipped.
886
+ * - If the plugin's connection was ignored for all sites, don't do anything in terms of the network connection.
887
+ * - 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.
888
+ * - 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.
889
+ * - 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.
890
+ *
891
+ * @author Vova Feldman (@svovaf)
892
+ * @since 2.0.0
893
+ */
894
+ private static function migrate_accounts_to_network() {
895
+ $sites = self::get_sites();
896
+ $sites_count = count( $sites );
897
+ $connection_status = array();
898
+ $plugin_slugs = array();
899
+ foreach ( $sites as $site ) {
900
+ $blog_id = self::get_site_blog_id( $site );
901
+
902
+ self::$_accounts->migrate_to_network( $blog_id );
903
+
904
+ /**
905
+ * Build a list of all Freemius powered plugins slugs.
906
+ */
907
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array(), $blog_id );
908
+ foreach ( $id_slug_type_path_map as $module_id => $data ) {
909
+ if ( WP_FS__MODULE_TYPE_PLUGIN === $data['type'] ) {
910
+ $plugin_slugs[ $data['slug'] ] = true;
911
+ }
912
+ }
913
+
914
+ $installs = self::get_account_option( 'sites', WP_FS__MODULE_TYPE_PLUGIN, $blog_id );
915
+
916
+ if ( is_array( $installs ) ) {
917
+ foreach ( $installs as $slug => $install ) {
918
+ if ( ! isset( $connection_status[ $slug ] ) ) {
919
+ $connection_status[ $slug ] = array();
920
+ }
921
+
922
+ if ( is_object( $install ) &&
923
+ FS_Site::is_valid_id( $install->id ) &&
924
+ FS_User::is_valid_id( $install->user_id )
925
+ ) {
926
+ $connection_status[ $slug ][ $blog_id ] = $install->user_id;
927
+ }
928
+ }
929
+ }
930
+ }
931
+
932
+ foreach ( $plugin_slugs as $slug => $true ) {
933
+ if ( ! isset( $connection_status[ $slug ] ) ) {
934
+ $connection_status[ $slug ] = array();
935
+ }
936
+
937
+ foreach ( $sites as $site ) {
938
+ $blog_id = self::get_site_blog_id( $site );
939
+
940
+ if ( isset( $connection_status[ $slug ][ $blog_id ] ) ) {
941
+ continue;
942
+ }
943
+
944
+ $storage = FS_Storage::instance( WP_FS__MODULE_TYPE_PLUGIN, $slug );
945
+
946
+ $is_anonymous = $storage->get( 'is_anonymous', null, $blog_id );
947
+
948
+ if ( ! is_null( $is_anonymous ) ) {
949
+ // Since 1.1.3 is_anonymous is an array.
950
+ if ( is_array( $is_anonymous ) && isset( $is_anonymous['is'] ) ) {
951
+ $is_anonymous = $is_anonymous['is'];
952
+ }
953
+
954
+ if ( is_bool( $is_anonymous ) && true === $is_anonymous ) {
955
+ $connection_status[ $slug ][ $blog_id ] = 'skipped';
956
+ }
957
+ }
958
+
959
+ if ( ! isset( $connection_status[ $slug ][ $blog_id ] ) ) {
960
+ $connection_status[ $slug ][ $blog_id ] = 'ignored';
961
+ }
962
+ }
963
+ }
964
+
965
+ $super_admins = array();
966
+
967
+ foreach ( $connection_status as $slug => $blogs_status ) {
968
+ $skips = 0;
969
+ $ignores = 0;
970
+ $connections = 0;
971
+ $opted_in_users = array();
972
+ $opted_in_super_admins = array();
973
+
974
+ $storage = FS_Storage::instance( WP_FS__MODULE_TYPE_PLUGIN, $slug );
975
+
976
+ foreach ( $blogs_status as $blog_id => $status_or_user_id ) {
977
+ if ( 'skipped' === $status_or_user_id ) {
978
+ $skips ++;
979
+ } else if ( 'ignored' === $status_or_user_id ) {
980
+ $ignores ++;
981
+ } else if ( FS_User::is_valid_id( $status_or_user_id ) ) {
982
+ $connections ++;
983
+
984
+ if ( ! isset( $opted_in_users[ $status_or_user_id ] ) ) {
985
+ $opted_in_users[ $status_or_user_id ] = array();
986
+ }
987
+
988
+ $opted_in_users[ $status_or_user_id ][] = $blog_id;
989
+
990
+ if ( isset( $super_admins[ $status_or_user_id ] ) ||
991
+ self::is_super_admin( $status_or_user_id )
992
+ ) {
993
+ // Cache super-admin data.
994
+ $super_admins[ $status_or_user_id ] = true;
995
+
996
+ // Remember opted-in super-admins for the plugin.
997
+ $opted_in_super_admins[ $status_or_user_id ] = true;
998
+ }
999
+ }
1000
+ }
1001
+
1002
+ $main_super_admin_user_id = null;
1003
+ $all_migrated = false;
1004
+ if ( $sites_count == $skips ) {
1005
+ // All sites were skipped -> network skip by copying the anonymous mode from any of the sites.
1006
+ $storage->is_anonymous_ms = $storage->is_anonymous;
1007
+
1008
+ $all_migrated = true;
1009
+ } else if ( $sites_count == $ignores ) {
1010
+ // Don't do anything, still in activation mode.
1011
+
1012
+ $all_migrated = true;
1013
+ } else if ( 0 < count( $opted_in_super_admins ) ) {
1014
+ // Find the super-admin with the majority of installs.
1015
+ $max_installs_by_super_admin = 0;
1016
+ foreach ( $opted_in_super_admins as $user_id => $true ) {
1017
+ $installs_count = count( $opted_in_users[ $user_id ] );
1018
+
1019
+ if ( $installs_count > $max_installs_by_super_admin ) {
1020
+ $max_installs_by_super_admin = $installs_count;
1021
+ $main_super_admin_user_id = $user_id;
1022
+ }
1023
+ }
1024
+
1025
+ if ( $sites_count == $connections && 1 == count( $opted_in_super_admins ) ) {
1026
+ // Super-admin opted-in for all sites in the network.
1027
+ $storage->is_network_connected = true;
1028
+
1029
+ $all_migrated = true;
1030
+ }
1031
+
1032
+ // Store network user.
1033
+ $storage->network_user_id = $main_super_admin_user_id;
1034
+
1035
+ $storage->network_install_blog_id = ( $sites_count == $connections ) ?
1036
+ // Since all sites are opted-in, associating with the main site.
1037
+ get_current_blog_id() :
1038
+ // Associating with the 1st found opted-in site.
1039
+ $opted_in_users[ $main_super_admin_user_id ][0];
1040
+
1041
+ /**
1042
+ * Make sure we migrate the plan ID of the network install, otherwise, if after the migration
1043
+ * the 1st page that will be loaded is the network level WP Admin and $storage->network_install_blog_id
1044
+ * is different than the main site of the network, the $this->_site will not be set since the plan_id
1045
+ * will be empty.
1046
+ */
1047
+ $storage->migrate_to_network();
1048
+ self::migrate_install_plan_to_plan_id( $storage, $storage->network_install_blog_id );
1049
+ } else {
1050
+ // At least one opt-in. All the opt-in were created by a non-super-admin.
1051
+ if ( 0 == $ignores ) {
1052
+ // All sites were opted-in or skipped, all by non-super-admin. So delegate all.
1053
+ $storage->store( 'is_delegated_connection', true, true );
1054
+
1055
+ $all_migrated = true;
1056
+ }
1057
+ }
1058
+
1059
+ if ( ! $all_migrated ) {
1060
+ /**
1061
+ * Delegate all sites that were:
1062
+ * 1) Opted-in by a user that is NOT the main-super-admin.
1063
+ * 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.
1064
+ */
1065
+ foreach ( $blogs_status as $blog_id => $status_or_user_id ) {
1066
+ if ( $status_or_user_id == $main_super_admin_user_id ) {
1067
+ continue;
1068
+ }
1069
+
1070
+ if ( FS_User::is_valid_id( $status_or_user_id ) ||
1071
+ ( 'skipped' === $status_or_user_id && is_null( $main_super_admin_user_id ) )
1072
+ ) {
1073
+ $storage->store( 'is_delegated_connection', true, $blog_id );
1074
+ }
1075
+ }
1076
+ }
1077
+
1078
+
1079
+ if ( ( $connections + $skips > 0 ) ) {
1080
+ if ( $ignores > 0 ) {
1081
+ /**
1082
+ * If admin already opted-in or skipped in any of the network sites, and also
1083
+ * have sites which the connection decision was not yet taken, set this plugin
1084
+ * into network activation mode so the super-admin can choose what to do with
1085
+ * the rest of the sites.
1086
+ */
1087
+ self::set_network_upgrade_mode( $storage );
1088
+ }
1089
+ }
1090
+ }
1091
+ }
1092
+
1093
+ /**
1094
+ * Set a module into network upgrade mode.
1095
+ *
1096
+ * @author Vova Feldman (@svovaf)
1097
+ * @since 2.0.0
1098
+ *
1099
+ * @param \FS_Storage $storage
1100
+ *
1101
+ * @return bool
1102
+ */
1103
+ private static function set_network_upgrade_mode( FS_Storage $storage ) {
1104
+ return $storage->is_network_activation = true;
1105
+ }
1106
+
1107
+ /**
1108
+ * Will return true after upgrading to the SDK with the network level integration,
1109
+ * when the super-admin involvement is required regarding the rest of the sites.
1110
+ *
1111
+ * @author Vova Feldman (@svovaf)
1112
+ * @since 2.0.0
1113
+ *
1114
+ * @return bool
1115
+ */
1116
+ function is_network_upgrade_mode() {
1117
+ return $this->_storage->get( 'is_network_activation' );
1118
+ }
1119
+
1120
+ /**
1121
+ * Clear flag after the upgrade mode completion.
1122
+ *
1123
+ * @author Vova Feldman (@svovaf)
1124
+ * @since 2.0.0
1125
+ *
1126
+ * @return bool True if network activation was on and now completed.
1127
+ */
1128
+ private function network_upgrade_mode_completed() {
1129
+ if ( fs_is_network_admin() && $this->is_network_upgrade_mode() ) {
1130
+ $this->_storage->remove( 'is_network_activation' );
1131
+
1132
+ return true;
1133
+ }
1134
+
1135
+ return false;
1136
+ }
1137
+
1138
+ #endregion
1139
+
1140
+ /**
1141
+ * This action is connected to the 'plugins_loaded' hook and helps to determine
1142
+ * if this is a new plugin installation or a plugin update.
1143
+ *
1144
+ * There are 3 different use-cases:
1145
+ * 1) New plugin installation right with Freemius:
1146
+ * 1.1 _activate_plugin_event_hook() will be executed first
1147
+ * 1.2 Since $this->_storage->is_plugin_new_install is not set,
1148
+ * and $this->_storage->plugin_last_version is not set,
1149
+ * $this->_storage->is_plugin_new_install will be set to TRUE.
1150
+ * 1.3 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will
1151
+ * be already set to TRUE.
1152
+ *
1153
+ * 2) Plugin update, didn't have Freemius before, and now have the SDK:
1154
+ * 2.1 _activate_plugin_event_hook() will not be executed, because
1155
+ * the activation hook do NOT fires on updates since WP 3.1.
1156
+ * 2.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will
1157
+ * be empty, therefore, it will be set to FALSE.
1158
+ *
1159
+ * 3) Plugin update, had Freemius in prev version as well:
1160
+ * 3.1 _version_updates_handler() will be executed 1st, since FS was installed
1161
+ * before, $this->_storage->plugin_last_version will NOT be empty,
1162
+ * therefore, $this->_storage->is_plugin_new_install will be set to FALSE.
1163
+ * 3.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install is
1164
+ * already set, therefore, it will not be modified.
1165
+ *
1166
+ * Use-case #3 is backward compatible, #3.1 will be executed since 1.0.9.
1167
+ *
1168
+ * NOTE:
1169
+ * The only fallback of this mechanism is if an admin updates a plugin based on use-case #2,
1170
+ * and then, the next immediate PageView is the plugin's main settings page, it will not
1171
+ * show the opt-in right away. The reason it will happen is because Freemius execution
1172
+ * will be turned off till the plugin is fully loaded at least once
1173
+ * (till $this->_storage->was_plugin_loaded is TRUE).
1174
+ *
1175
+ * @author Vova Feldman (@svovaf)
1176
+ * @since 1.1.9
1177
+ *
1178
+ */
1179
+ function _plugins_loaded() {
1180
+ // Update flag that plugin was loaded with Freemius at least once.
1181
+ $this->_storage->was_plugin_loaded = true;
1182
+
1183
+ /**
1184
+ * Bug fix - only set to false when it's a plugin, due to the
1185
+ * execution sequence of the theme hooks and our methods, if
1186
+ * this will be set for themes, Freemius will always assume
1187
+ * it's a theme update.
1188
+ *
1189
+ * @author Vova Feldman (@svovaf)
1190
+ * @since 1.2.2.2
1191
+ */
1192
+ if ( $this->is_plugin() &&
1193
+ ! isset( $this->_storage->is_plugin_new_install )
1194
+ ) {
1195
+ $this->_storage->is_plugin_new_install = false;
1196
+ }
1197
+ }
1198
+
1199
+ /**
1200
+ * Add special parameter to WP admin AJAX calls so when we
1201
+ * process AJAX calls we can identify its source properly.
1202
+ *
1203
+ * @author Leo Fajardo (@leorw)
1204
+ * @since 2.0.0
1205
+ */
1206
+ static function _enrich_ajax_url() {
1207
+ $admin_param = is_network_admin() ?
1208
+ '_fs_network_admin' :
1209
+ '_fs_blog_admin';
1210
+ ?>
1211
+ <script type="text/javascript">
1212
+ (function ($) {
1213
+ $(document).ajaxSend(function (event, jqxhr, settings) {
1214
+ if (settings.url &&
1215
+ -1 < settings.url.indexOf('admin-ajax.php') &&
1216
+ ! ( settings.url.indexOf( '<?php echo $admin_param ?>' ) > 0 )
1217
+ ) {
1218
+ if (settings.url.indexOf('?') > 0) {
1219
+ settings.url += '&';
1220
+ } else {
1221
+ settings.url += '?';
1222
+ }
1223
+
1224
+ settings.url += '<?php echo $admin_param ?>=true';
1225
+
1226
+ }
1227
+ });
1228
+ })(jQuery);
1229
+ </script>
1230
+ <?php
1231
+ }
1232
+
1233
+ /**
1234
+ * @author Vova Feldman (@svovaf)
1235
+ * @since 1.0.9
1236
+ */
1237
+ private function _register_hooks() {
1238
+ $this->_logger->entrance();
1239
+
1240
+ if ( is_admin() ) {
1241
+ add_action( 'plugins_loaded', array( &$this, '_hook_action_links_and_register_account_hooks' ) );
1242
+
1243
+ if ( $this->is_plugin() ) {
1244
+ $plugin_dir = dirname( $this->_plugin_dir_path ) . '/';
1245
+
1246
+ /**
1247
+ * @since 1.2.2
1248
+ *
1249
+ * Hook to both free and premium version activations to support
1250
+ * auto deactivation on the other version activation.
1251
+ */
1252
+ register_activation_hook(
1253
+ $plugin_dir . $this->_free_plugin_basename,
1254
+ array( &$this, '_activate_plugin_event_hook' )
1255
+ );
1256
+
1257
+ register_activation_hook(
1258
+ $plugin_dir . $this->premium_plugin_basename(),
1259
+ array( &$this, '_activate_plugin_event_hook' )
1260
+ );
1261
+ } else {
1262
+ add_action( 'after_switch_theme', array( &$this, '_activate_theme_event_hook' ), 10, 2 );
1263
+
1264
+ /**
1265
+ * Include the required hooks to capture the theme settings' page tabs
1266
+ * and cache them.
1267
+ *
1268
+ * @author Vova Feldman (@svovaf)
1269
+ * @since 1.2.2.7
1270
+ */
1271
+ if ( ! $this->_cache->has_valid( 'tabs' ) ) {
1272
+ add_action( 'admin_footer', array( &$this, '_tabs_capture' ) );
1273
+ // Add license activation AJAX callback.
1274
+ $this->add_ajax_action( 'store_tabs', array( &$this, '_store_tabs_ajax_action' ) );
1275
+
1276
+ add_action( 'admin_enqueue_scripts', array( &$this, '_store_tabs_styles' ), 9999999 );
1277
+ }
1278
+
1279
+ add_action(
1280
+ 'admin_footer',
1281
+ array( &$this, '_add_freemius_tabs' ),
1282
+ /**
1283
+ * The tabs JS code must be executed after the tabs capture logic (_tabs_capture()).
1284
+ * That's why the priority is 11 while the tabs capture logic is added
1285
+ * with priority 10.
1286
+ *
1287
+ * @author Vova Feldman (@svovaf)
1288
+ */
1289
+ 11
1290
+ );
1291
+
1292
+ add_action( 'admin_footer', array( &$this, '_style_premium_theme' ) );
1293
+ }
1294
+
1295
+ /**
1296
+ * Part of the mechanism to identify new plugin install vs. plugin update.
1297
+ *
1298
+ * @author Vova Feldman (@svovaf)
1299
+ * @since 1.1.9
1300
+ */
1301
+ if ( empty( $this->_storage->was_plugin_loaded ) ) {
1302
+ /**
1303
+ * During the plugin activation (not theme), 'plugins_loaded' will be already executed
1304
+ * when the logic gets here since the activation logic first add the activate plugins,
1305
+ * then triggers 'plugins_loaded', and only then include the code of the plugin that
1306
+ * is activated. Which means that _plugins_loaded() will NOT be executed during the
1307
+ * plugin activation, and that IS intentional.
1308
+ *
1309
+ * @author Vova Feldman (@svovaf)
1310
+ */
1311
+ if ( $this->is_plugin() && $this->is_activation_mode( false ) ) {
1312
+ add_action( 'plugins_loaded', array( &$this, '_plugins_loaded' ) );
1313
+ } else {
1314
+ // If was activated before, then it was already loaded before.
1315
+ $this->_plugins_loaded();
1316
+ }
1317
+ }
1318
+
1319
+ if ( ! self::is_ajax() ) {
1320
+ if ( ! $this->is_addon() ) {
1321
+ add_action( 'init', array( &$this, '_add_default_submenu_items' ), WP_FS__LOWEST_PRIORITY );
1322
+ }
1323
+ }
1324
+
1325
+ if ( $this->_storage->handle_gdpr_admin_notice ) {
1326
+ add_action( 'init', array( &$this, '_maybe_show_gdpr_admin_notice' ) );
1327
+ }
1328
+
1329
+ add_action( 'init', array( &$this, '_maybe_add_gdpr_optin_ajax_handler') );
1330
+ }
1331
+
1332
+ if ( $this->is_plugin() ) {
1333
+ if ( $this->_is_network_active ) {
1334
+ add_action( 'wpmu_new_blog', array( $this, '_after_new_blog_callback' ), 10, 6 );
1335
+ }
1336
+
1337
+ register_deactivation_hook( $this->_plugin_main_file_path, array( &$this, '_deactivate_plugin_hook' ) );
1338
+ }
1339
+
1340
+ if ( is_multisite() ) {
1341
+ add_action( 'deactivate_blog', array( &$this, '_after_site_deactivated_callback' ) );
1342
+ add_action( 'archive_blog', array( &$this, '_after_site_deactivated_callback' ) );
1343
+ add_action( 'make_spam_blog', array( &$this, '_after_site_deactivated_callback' ) );
1344
+ add_action( 'deleted_blog', array( &$this, '_after_site_deleted_callback' ), 10, 2 );
1345
+
1346
+ add_action( 'activate_blog', array( &$this, '_after_site_reactivated_callback' ) );
1347
+ add_action( 'unarchive_blog', array( &$this, '_after_site_reactivated_callback' ) );
1348
+ add_action( 'make_ham_blog', array( &$this, '_after_site_reactivated_callback' ) );
1349
+ }
1350
+
1351
+ if ( $this->is_theme() && self::is_customizer() ) {
1352
+ // Register customizer upsell.
1353
+ add_action( 'customize_register', array( &$this, '_customizer_register' ) );
1354
+ }
1355
+
1356
+ add_action( 'admin_init', array( &$this, '_redirect_on_clicked_menu_link' ), WP_FS__LOWEST_PRIORITY );
1357
+
1358
+ if ( $this->is_theme() ) {
1359
+ add_action( 'admin_init', array( &$this, '_add_tracking_links' ) );
1360
+ }
1361
+
1362
+ add_action( 'admin_init', array( &$this, '_add_license_activation' ) );
1363
+ add_action( 'admin_init', array( &$this, '_add_premium_version_upgrade_selection' ) );
1364
+
1365
+ $this->add_ajax_action( 'update_billing', array( &$this, '_update_billing_ajax_action' ) );
1366
+ $this->add_ajax_action( 'start_trial', array( &$this, '_start_trial_ajax_action' ) );
1367
+
1368
+ if ( $this->_is_network_active && fs_is_network_admin() ) {
1369
+ $this->add_ajax_action( 'network_activate', array( &$this, '_network_activate_ajax_action' ) );
1370
+ }
1371
+
1372
+ $this->add_ajax_action( 'install_premium_version', array(
1373
+ &$this,
1374
+ '_install_premium_version_ajax_action'
1375
+ ) );
1376
+
1377
+ $this->add_ajax_action( 'submit_affiliate_application', array( &$this, '_submit_affiliate_application' ) );
1378
+
1379
+ $this->add_action( 'after_plans_sync', array( &$this, '_check_for_trial_plans' ) );
1380
+
1381
+ $this->add_action( 'sdk_version_update', array( &$this, '_sdk_version_update' ), WP_FS__DEFAULT_PRIORITY, 2 );
1382
+
1383
+ $this->add_action(
1384
+ 'plugin_version_update',
1385
+ array( &$this, '_after_version_update' ),
1386
+ WP_FS__DEFAULT_PRIORITY,
1387
+ 2
1388
+ );
1389
+ $this->add_filter( 'after_code_type_change', array( &$this, '_after_code_type_change' ) );
1390
+
1391
+ add_action( 'admin_init', array( &$this, '_add_trial_notice' ) );
1392
+ add_action( 'admin_init', array( &$this, '_add_affiliate_program_notice' ) );
1393
+ add_action( 'admin_enqueue_scripts', array( &$this, '_enqueue_common_css' ) );
1394
+
1395
+ /**
1396
+ * Handle request to reset anonymous mode for `get_reconnect_url()`.
1397
+ *
1398
+ * @author Vova Feldman (@svovaf)
1399
+ * @since 1.2.1.5
1400
+ */
1401
+ if ( fs_request_is_action( 'reset_anonymous_mode' ) &&
1402
+ $this->get_unique_affix() === fs_request_get( 'fs_unique_affix' )
1403
+ ) {
1404
+ add_action( 'admin_init', array( &$this, 'connect_again' ) );
1405
+ }
1406
+ }
1407
+
1408
+ /**
1409
+ * Keeping the uninstall hook registered for free or premium plugin version may result to a fatal error that
1410
+ * could happen when a user tries to uninstall either version while one of them is still active. Uninstalling a
1411
+ * plugin will trigger inclusion of the free or premium version and if one of them is active during the
1412
+ * uninstallation, a fatal error may occur in case the plugin's class or functions are already defined.
1413
+ *
1414
+ * @author Leo Fajardo (@leorw)
1415
+ *
1416
+ * @since 1.2.0
1417
+ */
1418
+ private function unregister_uninstall_hook() {
1419
+ $uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
1420
+ unset( $uninstallable_plugins[ $this->_free_plugin_basename ] );
1421
+ unset( $uninstallable_plugins[ $this->premium_plugin_basename() ] );
1422
+
1423
+ update_option( 'uninstall_plugins', $uninstallable_plugins );
1424
+ }
1425
+
1426
+ /**
1427
+ * @since 1.2.0 Invalidate module's main file cache, otherwise, FS_Plugin_Updater will not fetch updates.
1428
+ */
1429
+ private function clear_module_main_file_cache() {
1430
+ if ( ! isset( $this->_storage->plugin_main_file ) ||
1431
+ empty( $this->_storage->plugin_main_file->path )
1432
+ ) {
1433
+ return;
1434
+ }
1435
+
1436
+ $plugin_main_file = clone $this->_storage->plugin_main_file;
1437
+
1438
+ // Store cached path (2nd layer cache).
1439
+ $plugin_main_file->prev_path = $plugin_main_file->path;
1440
+
1441
+ // Clear cached path.
1442
+ unset( $plugin_main_file->path );
1443
+
1444
+ $this->_storage->plugin_main_file = $plugin_main_file;
1445
+
1446
+ /**
1447
+ * Clear global cached path.
1448
+ *
1449
+ * @author Leo Fajardo (@leorw)
1450
+ * @since 1.2.2
1451
+ */
1452
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map' );
1453
+ unset( $id_slug_type_path_map[ $this->_module_id ]['path'] );
1454
+ self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
1455
+ }
1456
+
1457
+ /**
1458
+ * @author Leo Fajardo (@leorw)
1459
+ * @since 2.0.0
1460
+ */
1461
+ function _hook_action_links_and_register_account_hooks() {
1462
+ add_action( 'admin_init', array( &$this, '_add_tracking_links' ) );
1463
+
1464
+ if ( self::is_plugins_page() && $this->is_plugin() ) {
1465
+ $this->hook_plugin_action_links();
1466
+ }
1467
+
1468
+ $this->_register_account_hooks();
1469
+ }
1470
+
1471
+ /**
1472
+ * @author Vova Feldman (@svovaf)
1473
+ * @since 1.0.9
1474
+ */
1475
+ private function _register_account_hooks() {
1476
+ if ( ! is_admin() ) {
1477
+ return;
1478
+ }
1479
+
1480
+ /**
1481
+ * Always show the deactivation feedback form since we added
1482
+ * automatic free version deactivation upon premium code activation.
1483
+ *
1484
+ * @since 1.2.1.6
1485
+ */
1486
+ $this->add_ajax_action(
1487
+ 'submit_uninstall_reason',
1488
+ array( &$this, '_submit_uninstall_reason_action' )
1489
+ );
1490
+
1491
+ if ( $this->is_theme() && $this->is_premium() && ! $this->has_active_valid_license() ) {
1492
+ $this->add_ajax_action(
1493
+ 'delete_theme_update_data',
1494
+ array( &$this, '_delete_theme_update_data_action' )
1495
+ );
1496
+ }
1497
+
1498
+ if ( ! $this->is_addon() || $this->is_parent_plugin_installed() ) {
1499
+ if ( ( $this->is_plugin() && self::is_plugins_page() ) ||
1500
+ ( $this->is_theme() && self::is_themes_page() )
1501
+ ) {
1502
+ add_action( 'admin_footer', array( &$this, '_add_deactivation_feedback_dialog_box' ) );
1503
+ }
1504
+ }
1505
+ }
1506
+
1507
+ /**
1508
+ * Leverage backtrace to find caller plugin file path.
1509
+ *
1510
+ * @author Vova Feldman (@svovaf)
1511
+ * @since 1.0.6
1512
+ *
1513
+ * @param bool $is_init Is initiation sequence.
1514
+ *
1515
+ * @return string
1516
+ */
1517
+ private function _find_caller_plugin_file( $is_init = false ) {
1518
+ // Try to load the cached value of the file path.
1519
+ if ( isset( $this->_storage->plugin_main_file ) ) {
1520
+ $plugin_main_file = $this->_storage->plugin_main_file;
1521
+ if ( isset( $plugin_main_file->path ) ) {
1522
+ $absolute_path = $this->get_absolute_path( $plugin_main_file->path );
1523
+ if ( file_exists( $absolute_path ) ) {
1524
+ return $absolute_path;
1525
+ }
1526
+ }
1527
+ }
1528
+
1529
+ /**
1530
+ * @since 1.2.1
1531
+ *
1532
+ * `clear_module_main_file_cache()` is clearing the plugin's cached path on
1533
+ * deactivation. Therefore, if any plugin/theme was initiating `Freemius`
1534
+ * with that plugin's slug, it was overriding the empty plugin path with a wrong path.
1535
+ *
1536
+ * So, we've added a special mechanism with a 2nd layer of cache that uses `prev_path`
1537
+ * when the class instantiator isn't the module.
1538
+ */
1539
+ if ( ! $is_init ) {
1540
+ // Fetch prev path cache.
1541
+ if ( isset( $this->_storage->plugin_main_file ) &&
1542
+ isset( $this->_storage->plugin_main_file->prev_path )
1543
+ ) {
1544
+ $absolute_path = $this->get_absolute_path( $this->_storage->plugin_main_file->prev_path );
1545
+ if ( file_exists( $absolute_path ) ) {
1546
+ return $absolute_path;
1547
+ }
1548
+ }
1549
+
1550
+ wp_die(
1551
+ $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' ) .
1552
+ " Module: {$this->_slug}; SDK: " . WP_FS__SDK_VERSION . ";",
1553
+ $this->get_text_inline( 'Error', 'error' ),
1554
+ array( 'back_link' => true )
1555
+ );
1556
+ }
1557
+
1558
+ /**
1559
+ * @since 1.2.1
1560
+ *
1561
+ * Only the original instantiator that calls dynamic_init can modify the module's path.
1562
+ */
1563
+ // Find caller module.
1564
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
1565
+ $this->_storage->plugin_main_file = (object) array(
1566
+ 'path' => $id_slug_type_path_map[ $this->_module_id ]['path'],
1567
+ );
1568
+
1569
+ return $this->get_absolute_path( $id_slug_type_path_map[ $this->_module_id ]['path'] );
1570
+ }
1571
+
1572
+ /**
1573
+ * @author Leo Fajardo (@leorw)
1574
+ * @since 1.2.3
1575
+ *
1576
+ * @param string $path
1577
+ *
1578
+ * @return string
1579
+ */
1580
+ private function get_relative_path( $path ) {
1581
+ $module_root_dir = $this->get_module_root_dir_path();
1582
+ if ( 0 === strpos( $path, $module_root_dir ) ) {
1583
+ $path = substr( $path, strlen( $module_root_dir ) );
1584
+ }
1585
+
1586
+ return $path;
1587
+ }
1588
+
1589
+ /**
1590
+ * @author Leo Fajardo (@leorw)
1591
+ * @since 1.2.3
1592
+ *
1593
+ * @param string $path
1594
+ * @param string|bool $module_type
1595
+ *
1596
+ * @return string
1597
+ */
1598
+ private function get_absolute_path( $path, $module_type = false ) {
1599
+ $module_root_dir = $this->get_module_root_dir_path( $module_type );
1600
+ if ( 0 !== strpos( $path, $module_root_dir ) ) {
1601
+ $path = fs_normalize_path( $module_root_dir . $path );
1602
+ }
1603
+
1604
+ return $path;
1605
+ }
1606
+
1607
+ /**
1608
+ * @author Leo Fajardo (@leorw)
1609
+ * @since 1.2.3
1610
+ *
1611
+ * @param string|bool $module_type
1612
+ *
1613
+ * @return string
1614
+ */
1615
+ private function get_module_root_dir_path( $module_type = false ) {
1616
+ $is_plugin = empty( $module_type ) ?
1617
+ $this->is_plugin() :
1618
+ ( WP_FS__MODULE_TYPE_PLUGIN === $module_type );
1619
+
1620
+ return fs_normalize_path( trailingslashit( $is_plugin ?
1621
+ WP_PLUGIN_DIR :
1622
+ get_theme_root() ) );
1623
+ }
1624
+
1625
+ /**
1626
+ * @author Leo Fajardo (@leorw)
1627
+ *
1628
+ * @param number $module_id
1629
+ * @param string $slug
1630
+ *
1631
+ * @since 1.2.2
1632
+ */
1633
+ private function store_id_slug_type_path_map( $module_id, $slug ) {
1634
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
1635
+
1636
+ $store_option = false;
1637
+
1638
+ if ( ! isset( $id_slug_type_path_map[ $module_id ] ) ) {
1639
+ $id_slug_type_path_map[ $module_id ] = array(
1640
+ 'slug' => $slug
1641
+ );
1642
+
1643
+ $store_option = true;
1644
+ }
1645
+
1646
+ if ( ! isset( $id_slug_type_path_map[ $module_id ]['path'] ) ||
1647
+ /**
1648
+ * This verification is for cases when suddenly the same module
1649
+ * is installed but with a different folder name.
1650
+ *
1651
+ * @author Vova Feldman (@svovaf)
1652
+ * @since 1.2.3
1653
+ */
1654
+ ! file_exists( $this->get_absolute_path(
1655
+ $id_slug_type_path_map[ $module_id ]['path'],
1656
+ $id_slug_type_path_map[ $module_id ]['type']
1657
+ ) )
1658
+ ) {
1659
+ $caller_main_file_and_type = $this->get_caller_main_file_and_type();
1660
+
1661
+ $id_slug_type_path_map[ $module_id ]['type'] = $caller_main_file_and_type->module_type;
1662
+ $id_slug_type_path_map[ $module_id ]['path'] = $caller_main_file_and_type->path;
1663
+
1664
+ $store_option = true;
1665
+ }
1666
+
1667
+ if ( $store_option ) {
1668
+ self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
1669
+ }
1670
+ }
1671
+
1672
+ /**
1673
+ * Identifies the caller type: plugin or theme.
1674
+ *
1675
+ * @author Leo Fajardo (@leorw)
1676
+ * @since 1.2.2
1677
+ *
1678
+ * @author Vova Feldman (@svovaf)
1679
+ * @since 1.2.2.3 Find the earliest module in the call stack that calls to the SDK. This fix is for cases when
1680
+ * add-ons are relying on loading the SDK from the parent module, and also allows themes including the
1681
+ * SDK an internal file instead of directly from functions.php.
1682
+ * @since 1.2.1.7 Knows how to handle cases when an add-on includes the parent module logic.
1683
+ */
1684
+ private function get_caller_main_file_and_type() {
1685
+ self::require_plugin_essentials();
1686
+
1687
+ $all_plugins = get_plugins();
1688
+ $all_plugins_paths = array();
1689
+
1690
+ // Get active plugin's main files real full names (might be symlinks).
1691
+ foreach ( $all_plugins as $relative_path => &$data ) {
1692
+ if ( false === strpos( fs_normalize_path( $relative_path ), '/' ) ) {
1693
+ /**
1694
+ * Ignore plugins that don't have a folder (e.g. Hello Dolly) since they
1695
+ * can't really include the SDK.
1696
+ *
1697
+ * @author Vova Feldman
1698
+ * @since 1.2.1.7
1699
+ */
1700
+ continue;
1701
+ }
1702
+
1703
+ $all_plugins_paths[] = fs_normalize_path( realpath( WP_PLUGIN_DIR . '/' . $relative_path ) );
1704
+ }
1705
+
1706
+ $caller_file_candidate = false;
1707
+ $caller_map = array();
1708
+ $module_type = WP_FS__MODULE_TYPE_PLUGIN;
1709
+ $themes_dir = fs_normalize_path( get_theme_root() );
1710
+
1711
+ for ( $i = 1, $bt = debug_backtrace(), $len = count( $bt ); $i < $len; $i ++ ) {
1712
+ if ( empty( $bt[ $i ]['file'] ) ) {
1713
+ continue;
1714
+ }
1715
+
1716
+ if ( $i > 1 && ! empty( $bt[ $i - 1 ]['file'] ) && $bt[ $i ]['file'] === $bt[ $i - 1 ]['file'] ) {
1717
+ // If file same as the prev file in the stack, skip it.
1718
+ continue;
1719
+ }
1720
+
1721
+ if ( ! empty( $bt[ $i ]['function'] ) && in_array( $bt[ $i ]['function'], array(
1722
+ 'do_action',
1723
+ 'apply_filter',
1724
+ // The string split is stupid, but otherwise, theme check
1725
+ // throws info notices.
1726
+ 'requir' . 'e_once',
1727
+ 'requir' . 'e',
1728
+ 'includ' . 'e_once',
1729
+ 'includ' . 'e'
1730
+ ) )
1731
+ ) {
1732
+ // Ignore call stack hooks and files inclusion.
1733
+ continue;
1734
+ }
1735
+
1736
+ $caller_file_path = fs_normalize_path( $bt[ $i ]['file'] );
1737
+
1738
+ if ( 'functions.php' === basename( $caller_file_path ) ) {
1739
+ /**
1740
+ * 1. Assumes that theme's starting execution file is functions.php.
1741
+ * 2. This complex logic fixes symlink issues (e.g. with Vargant).
1742
+ *
1743
+ * @author Vova Feldman (@svovaf)
1744
+ * @since 1.2.2.5
1745
+ */
1746
+
1747
+ if ( $caller_file_path == fs_normalize_path( realpath( trailingslashit( $themes_dir ) . basename( dirname( $caller_file_path ) ) . '/' . basename( $caller_file_path ) ) ) ) {
1748
+ $module_type = WP_FS__MODULE_TYPE_THEME;
1749
+
1750
+ /**
1751
+ * Relative path of the theme, e.g.:
1752
+ * `my-theme/functions.php`
1753
+ *
1754
+ * @author Leo Fajardo (@leorw)
1755
+ */
1756
+ $caller_file_candidate = basename( dirname( $caller_file_path ) ) .
1757
+ '/' .
1758
+ basename( $caller_file_path );
1759
+
1760
+ continue;
1761
+ }
1762
+ }
1763
+
1764
+ $caller_file_hash = md5( $caller_file_path );
1765
+
1766
+ if ( ! isset( $caller_map[ $caller_file_hash ] ) ) {
1767
+ foreach ( $all_plugins_paths as $plugin_path ) {
1768
+ if ( false !== strpos( $caller_file_path, fs_normalize_path( dirname( $plugin_path ) . '/' ) ) ) {
1769
+ $caller_map[ $caller_file_hash ] = fs_normalize_path( $plugin_path );
1770
+ break;
1771
+ }
1772
+ }
1773
+ }
1774
+
1775
+ if ( isset( $caller_map[ $caller_file_hash ] ) ) {
1776
+ $module_type = WP_FS__MODULE_TYPE_PLUGIN;
1777
+ $caller_file_candidate = plugin_basename( $caller_map[ $caller_file_hash ] );
1778
+ }
1779
+ }
1780
+
1781
+ return (object) array(
1782
+ 'module_type' => $module_type,
1783
+ 'path' => $caller_file_candidate
1784
+ );
1785
+ }
1786
+
1787
+ #----------------------------------------------------------------------------------
1788
+ #region Deactivation Feedback Form
1789
+ #----------------------------------------------------------------------------------
1790
+
1791
+ /**
1792
+ * Displays a confirmation and feedback dialog box when the user clicks on the "Deactivate" link on the plugins
1793
+ * page.
1794
+ *
1795
+ * @author Vova Feldman (@svovaf)
1796
+ * @author Leo Fajardo (@leorw)
1797
+ * @since 1.1.2
1798
+ */
1799
+ function _add_deactivation_feedback_dialog_box() {
1800
+ /* Check the type of user:
1801
+ * 1. Long-term (long-term)
1802
+ * 2. Non-registered and non-anonymous short-term (non-registered-and-non-anonymous-short-term).
1803
+ * 3. Short-term (short-term)
1804
+ */
1805
+ $is_long_term_user = true;
1806
+
1807
+ // Check if the site is at least 2 days old.
1808
+ $time_installed = $this->_storage->install_timestamp;
1809
+
1810
+ // Difference in seconds.
1811
+ $date_diff = time() - $time_installed;
1812
+
1813
+ // Convert seconds to days.
1814
+ $date_diff_days = floor( $date_diff / ( 60 * 60 * 24 ) );
1815
+
1816
+ if ( $date_diff_days < 2 ) {
1817
+ $is_long_term_user = false;
1818
+ }
1819
+
1820
+ $is_long_term_user = $this->apply_filters( 'is_long_term_user', $is_long_term_user );
1821
+
1822
+ if ( $is_long_term_user ) {
1823
+ $user_type = 'long-term';
1824
+ } else {
1825
+ if ( ! $this->is_registered() && ! $this->is_anonymous() ) {
1826
+ $user_type = 'non-registered-and-non-anonymous-short-term';
1827
+ } else {
1828
+ $user_type = 'short-term';
1829
+ }
1830
+ }
1831
+
1832
+ $uninstall_reasons = $this->_get_uninstall_reasons( $user_type );
1833
+
1834
+ // Load the HTML template for the deactivation feedback dialog box.
1835
+ $vars = array(
1836
+ 'reasons' => $uninstall_reasons,
1837
+ 'id' => $this->_module_id
1838
+ );
1839
+
1840
+ /**
1841
+ * @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.
1842
+ */
1843
+ fs_require_template( 'forms/deactivation/form.php', $vars );
1844
+ }
1845
+
1846
+ /**
1847
+ * @author Leo Fajardo (@leorw)
1848
+ * @since 1.1.2
1849
+ *
1850
+ * @param string $user_type
1851
+ *
1852
+ * @return array The uninstall reasons for the specified user type.
1853
+ */
1854
+ function _get_uninstall_reasons( $user_type = 'long-term' ) {
1855
+ $module_type = $this->_module_type;
1856
+
1857
+ $internal_message_template_var = array(
1858
+ 'id' => $this->_module_id
1859
+ );
1860
+
1861
+ $plan = $this->get_plan();
1862
+
1863
+ if ( $this->is_registered() && is_object( $plan ) && $plan->has_technical_support() ) {
1864
+ $contact_support_template = fs_get_template( 'forms/deactivation/contact.php', $internal_message_template_var );
1865
+ } else {
1866
+ $contact_support_template = '';
1867
+ }
1868
+
1869
+ $reason_found_better_plugin = array(
1870
+ 'id' => self::REASON_FOUND_A_BETTER_PLUGIN,
1871
+ 'text' => sprintf( $this->get_text_inline( 'I found a better %s', 'reason-found-a-better-plugin' ), $module_type ),
1872
+ 'input_type' => 'textfield',
1873
+ 'input_placeholder' => sprintf( $this->get_text_inline( "What's the %s's name?", 'placeholder-plugin-name' ), $module_type ),
1874
+ );
1875
+
1876
+ $reason_temporary_deactivation = array(
1877
+ 'id' => self::REASON_TEMPORARY_DEACTIVATION,
1878
+ 'text' => sprintf(
1879
+ $this->get_text_inline( "It's a temporary %s. I'm just debugging an issue.", 'reason-temporary-x' ),
1880
+ strtolower( $this->is_plugin() ?
1881
+ $this->get_text_inline( 'Deactivation', 'deactivation' ) :
1882
+ $this->get_text_inline( 'Theme Switch', 'theme-switch' )
1883
+ )
1884
+ ),
1885
+ 'input_type' => '',
1886
+ 'input_placeholder' => ''
1887
+ );
1888
+
1889
+ $reason_other = array(
1890
+ 'id' => self::REASON_OTHER,
1891
+ 'text' => $this->get_text_inline( 'Other', 'reason-other' ),
1892
+ 'input_type' => 'textfield',
1893
+ 'input_placeholder' => ''
1894
+ );
1895
+
1896
+ $long_term_user_reasons = array(
1897
+ array(
1898
+ 'id' => self::REASON_NO_LONGER_NEEDED,
1899
+ 'text' => sprintf( $this->get_text_inline( 'I no longer need the %s', 'reason-no-longer-needed' ), $module_type ),
1900
+ 'input_type' => '',
1901
+ 'input_placeholder' => ''
1902
+ ),
1903
+ $reason_found_better_plugin,
1904
+ array(
1905
+ 'id' => self::REASON_NEEDED_FOR_A_SHORT_PERIOD,
1906
+ 'text' => sprintf( $this->get_text_inline( 'I only needed the %s for a short period', 'reason-needed-for-a-short-period' ), $module_type ),
1907
+ 'input_type' => '',
1908
+ 'input_placeholder' => ''
1909
+ ),
1910
+ array(
1911
+ 'id' => self::REASON_BROKE_MY_SITE,
1912
+ 'text' => sprintf( $this->get_text_inline( 'The %s broke my site', 'reason-broke-my-site' ), $module_type ),
1913
+ 'input_type' => '',
1914
+ 'input_placeholder' => '',
1915
+ 'internal_message' => $contact_support_template
1916
+ ),
1917
+ array(
1918
+ 'id' => self::REASON_SUDDENLY_STOPPED_WORKING,
1919
+ 'text' => sprintf( $this->get_text_inline( 'The %s suddenly stopped working', 'reason-suddenly-stopped-working' ), $module_type ),
1920
+ 'input_type' => '',
1921
+ 'input_placeholder' => '',
1922
+ 'internal_message' => $contact_support_template
1923
+ )
1924
+ );
1925
+
1926
+ if ( $this->is_paying() ) {
1927
+ $long_term_user_reasons[] = array(
1928
+ 'id' => self::REASON_CANT_PAY_ANYMORE,
1929
+ 'text' => $this->get_text_inline( "I can't pay for it anymore", 'reason-cant-pay-anymore' ),
1930
+ 'input_type' => 'textfield',
1931
+ 'input_placeholder' => $this->get_text_inline( 'What price would you feel comfortable paying?', 'placeholder-comfortable-price' )
1932
+ );
1933
+ }
1934
+
1935
+ $reason_dont_share_info = array(
1936
+ 'id' => self::REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION,
1937
+ 'text' => $this->get_text_inline( "I don't like to share my information with you", 'reason-dont-like-to-share-my-information' ),
1938
+ 'input_type' => '',
1939
+ 'input_placeholder' => ''
1940
+ );
1941
+
1942
+ /**
1943
+ * If the current user has selected the "don't share data" reason in the deactivation feedback modal, inform the
1944
+ * user by showing additional message that he doesn't have to share data and can just choose to skip the opt-in
1945
+ * (the Skip button is included in the message to show). This message will only be shown if anonymous mode is
1946
+ * enabled and the user's account is currently not in pending activation state (similar to the way the Skip
1947
+ * button in the opt-in form is shown/hidden).
1948
+ */
1949
+ if ( $this->is_enable_anonymous() && ! $this->is_pending_activation() ) {
1950
+ $reason_dont_share_info['internal_message'] = fs_get_template( 'forms/deactivation/retry-skip.php', $internal_message_template_var );
1951
+ }
1952
+
1953
+ $uninstall_reasons = array(
1954
+ 'long-term' => $long_term_user_reasons,
1955
+ 'non-registered-and-non-anonymous-short-term' => array(
1956
+ array(
1957
+ 'id' => self::REASON_DIDNT_WORK,
1958
+ 'text' => sprintf( $this->get_text_inline( "The %s didn't work", 'reason-didnt-work' ), $module_type ),
1959
+ 'input_type' => '',
1960
+ 'input_placeholder' => ''
1961
+ ),
1962
+ $reason_dont_share_info,
1963
+ $reason_found_better_plugin
1964
+ ),
1965
+ 'short-term' => array(
1966
+ array(
1967
+ 'id' => self::REASON_COULDNT_MAKE_IT_WORK,
1968
+ 'text' => $this->get_text_inline( "I couldn't understand how to make it work", 'reason-couldnt-make-it-work' ),
1969
+ 'input_type' => '',
1970
+ 'input_placeholder' => '',
1971
+ 'internal_message' => $contact_support_template
1972
+ ),
1973
+ $reason_found_better_plugin,
1974
+ array(
1975
+ 'id' => self::REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE,
1976
+ '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 ),
1977
+ 'input_type' => 'textarea',
1978
+ 'input_placeholder' => $this->get_text_inline( 'What feature?', 'placeholder-feature' )
1979
+ ),
1980
+ array(
1981
+ 'id' => self::REASON_NOT_WORKING,
1982
+ 'text' => sprintf( $this->get_text_inline( 'The %s is not working', 'reason-not-working' ), $module_type ),
1983
+ 'input_type' => 'textarea',
1984
+ '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' )
1985
+ ),
1986
+ array(
1987
+ 'id' => self::REASON_NOT_WHAT_I_WAS_LOOKING_FOR,
1988
+ 'text' => $this->get_text_inline( "It's not what I was looking for", 'reason-not-what-i-was-looking-for' ),
1989
+ 'input_type' => 'textarea',
1990
+ 'input_placeholder' => $this->get_text_inline( "What you've been looking for?", 'placeholder-what-youve-been-looking-for' )
1991
+ ),
1992
+ array(
1993
+ 'id' => self::REASON_DIDNT_WORK_AS_EXPECTED,
1994
+ 'text' => sprintf( $this->get_text_inline( "The %s didn't work as expected", 'reason-didnt-work-as-expected' ), $module_type ),
1995
+ 'input_type' => 'textarea',
1996
+ 'input_placeholder' => $this->get_text_inline( 'What did you expect?', 'placeholder-what-did-you-expect' )
1997
+ )
1998
+ )
1999
+ );
2000
+
2001
+ // Randomize the reasons for the current user type.
2002
+ shuffle( $uninstall_reasons[ $user_type ] );
2003
+
2004
+ // Keep the following reasons as the last items in the list.
2005
+ $uninstall_reasons[ $user_type ][] = $reason_temporary_deactivation;
2006
+ $uninstall_reasons[ $user_type ][] = $reason_other;
2007
+
2008
+ $uninstall_reasons = $this->apply_filters( 'uninstall_reasons', $uninstall_reasons );
2009
+
2010
+ return $uninstall_reasons[ $user_type ];
2011
+ }
2012
+
2013
+ /**
2014
+ * Called after the user has submitted his reason for deactivating the plugin.
2015
+ *
2016
+ * @author Leo Fajardo (@leorw)
2017
+ * @since 1.1.2
2018
+ */
2019
+ function _submit_uninstall_reason_action() {
2020
+ $this->_logger->entrance();
2021
+
2022
+ $this->check_ajax_referer( 'submit_uninstall_reason' );
2023
+
2024
+ $reason_id = fs_request_get( 'reason_id' );
2025
+
2026
+ // Check if the given reason ID is an unsigned integer.
2027
+ if ( ! ctype_digit( $reason_id ) ) {
2028
+ exit;
2029
+ }
2030
+
2031
+ $reason_info = trim( fs_request_get( 'reason_info', '' ) );
2032
+ if ( ! empty( $reason_info ) ) {
2033
+ $reason_info = substr( $reason_info, 0, 128 );
2034
+ }
2035
+
2036
+ $reason = (object) array(
2037
+ 'id' => $reason_id,
2038
+ 'info' => $reason_info,
2039
+ 'is_anonymous' => fs_request_get_bool( 'is_anonymous' )
2040
+ );
2041
+
2042
+ $this->_storage->store( 'uninstall_reason', $reason );
2043
+
2044
+ /**
2045
+ * If the module type is "theme", trigger the uninstall event here (on theme deactivation) since themes do
2046
+ * not support uninstall hook.
2047
+ *
2048
+ * @author Leo Fajardo (@leorw)
2049
+ * @since 1.2.2
2050
+ */
2051
+ if ( $this->is_theme() ) {
2052
+ if ( $this->is_premium() && ! $this->has_active_valid_license() ) {
2053
+ FS_Plugin_Updater::instance( $this )->delete_update_data();
2054
+ }
2055
+
2056
+ $this->_uninstall_plugin_event( false );
2057
+ $this->remove_sdk_reference();
2058
+ }
2059
+
2060
+ // Print '1' for successful operation.
2061
+ echo 1;
2062
+ exit;
2063
+ }
2064
+
2065
+ /**
2066
+ * @author Leo Fajardo (@leorw)
2067
+ * @since 2.0.2
2068
+ */
2069
+ function _delete_theme_update_data_action() {
2070
+ FS_Plugin_Updater::instance( $this )->delete_update_data();
2071
+ }
2072
+
2073
+ #endregion
2074
+
2075
+ #----------------------------------------------------------------------------------
2076
+ #region Instance
2077
+ #----------------------------------------------------------------------------------
2078
+
2079
+ /**
2080
+ * Main singleton instance.
2081
+ *
2082
+ * @author Vova Feldman (@svovaf)
2083
+ * @since 1.0.0
2084
+ *
2085
+ * @param number $module_id
2086
+ * @param string|bool $slug
2087
+ * @param bool $is_init Is initiation sequence.
2088
+ *
2089
+ * @return Freemius|false
2090
+ */
2091
+ static function instance( $module_id, $slug = false, $is_init = false ) {
2092
+ if ( empty( $module_id ) ) {
2093
+ return false;
2094
+ }
2095
+
2096
+ /**
2097
+ * 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.
2098
+ */
2099
+ self::_load_required_static();
2100
+
2101
+ if ( ! is_numeric( $module_id ) ) {
2102
+ if ( ! $is_init && true === $slug ) {
2103
+ $is_init = true;
2104
+ }
2105
+
2106
+ $slug = $module_id;
2107
+
2108
+ $module = FS_Plugin_Manager::instance( $slug )->get();
2109
+
2110
+ if ( is_object( $module ) ) {
2111
+ $module_id = $module->id;
2112
+ }
2113
+ }
2114
+
2115
+ $key = 'm_' . $module_id;
2116
+
2117
+ if ( ! isset( self::$_instances[ $key ] ) ) {
2118
+ self::$_instances[ $key ] = new Freemius( $module_id, $slug, $is_init );
2119
+ }
2120
+
2121
+ return self::$_instances[ $key ];
2122
+ }
2123
+
2124
+ /**
2125
+ * @author Vova Feldman (@svovaf)
2126
+ * @since 1.0.6
2127
+ *
2128
+ * @param number $addon_id
2129
+ *
2130
+ * @return bool
2131
+ */
2132
+ private static function has_instance( $addon_id ) {
2133
+ return isset( self::$_instances[ 'm_' . $addon_id ] );
2134
+ }
2135
+
2136
+ /**
2137
+ * @author Leo Fajardo (@leorw)
2138
+ * @since 1.2.2
2139
+ *
2140
+ * @param string|number $id_or_slug
2141
+ *
2142
+ * @return number|false
2143
+ */
2144
+ private static function get_module_id( $id_or_slug ) {
2145
+ if ( is_numeric( $id_or_slug ) ) {
2146
+ return $id_or_slug;
2147
+ }
2148
+
2149
+ foreach ( self::$_instances as $instance ) {
2150
+ if ( $instance->is_plugin() && ( $id_or_slug === $instance->get_slug() ) ) {
2151
+ return $instance->get_id();
2152
+ }
2153
+ }
2154
+
2155
+ return false;
2156
+ }
2157
+
2158
+ /**
2159
+ * @author Vova Feldman (@svovaf)
2160
+ * @since 1.0.6
2161
+ *
2162
+ * @param number $id
2163
+ *
2164
+ * @return false|Freemius
2165
+ */
2166
+ static function get_instance_by_id( $id ) {
2167
+ return isset ( self::$_instances[ 'm_' . $id ] ) ?
2168
+ self::$_instances[ 'm_' . $id ] :
2169
+ false;
2170
+ }
2171
+
2172
+ /**
2173
+ *
2174
+ * @author Vova Feldman (@svovaf)
2175
+ * @since 1.0.1
2176
+ *
2177
+ * @param $plugin_file
2178
+ *
2179
+ * @return false|Freemius
2180
+ */
2181
+ static function get_instance_by_file( $plugin_file ) {
2182
+ $slug = self::find_slug_by_basename( $plugin_file );
2183
+
2184
+ return ( false !== $slug ) ?
2185
+ self::instance( self::get_module_id( $slug ) ) :
2186
+ false;
2187
+ }
2188
+
2189
+ /**
2190
+ * @author Vova Feldman (@svovaf)
2191
+ * @since 1.0.6
2192
+ *
2193
+ * @return false|Freemius
2194
+ */
2195
+ function get_parent_instance() {
2196
+ return self::get_instance_by_id( $this->_plugin->parent_plugin_id );
2197
+ }
2198
+
2199
+ /**
2200
+ * @author Vova Feldman (@svovaf)
2201
+ * @since 1.0.6
2202
+ *
2203
+ * @param string|number $id_or_slug
2204
+ *
2205
+ * @return false|Freemius
2206
+ */
2207
+ function get_addon_instance( $id_or_slug ) {
2208
+ $addon_id = self::get_module_id( $id_or_slug );
2209
+
2210
+ return self::instance( $addon_id );
2211
+ }
2212
+
2213
+ #endregion ------------------------------------------------------------------
2214
+
2215
+ /**
2216
+ * @author Vova Feldman (@svovaf)
2217
+ * @since 1.0.6
2218
+ *
2219
+ * @return bool
2220
+ */
2221
+ function is_parent_plugin_installed() {
2222
+ $is_active = self::has_instance( $this->_plugin->parent_plugin_id );
2223
+
2224
+ if ( $is_active ) {
2225
+ return true;
2226
+ }
2227
+
2228
+ /**
2229
+ * Parent module might be a theme. If that's the case, the add-on's FS
2230
+ * instance will be loaded prior to the theme's FS instance, therefore,
2231
+ * we need to check if it's active with a "look ahead".
2232
+ *
2233
+ * @author Vova Feldman
2234
+ * @since 1.2.2.3
2235
+ */
2236
+ global $fs_active_plugins;
2237
+ if ( is_object( $fs_active_plugins ) && is_array( $fs_active_plugins->plugins ) ) {
2238
+ $active_theme = wp_get_theme();
2239
+
2240
+ foreach ( $fs_active_plugins->plugins as $sdk => $module ) {
2241
+ if ( WP_FS__MODULE_TYPE_THEME === $module->type ) {
2242
+ if ( $module->plugin_path == $active_theme->get_stylesheet() ) {
2243
+ // Parent module is a theme and it's currently active.
2244
+ return true;
2245
+ }
2246
+ }
2247
+ }
2248
+ }
2249
+
2250
+ return false;
2251
+ }
2252
+
2253
+ /**
2254
+ * Check if add-on parent plugin in activation mode.
2255
+ *
2256
+ * @author Vova Feldman (@svovaf)
2257
+ * @since 1.0.7
2258
+ *
2259
+ * @return bool
2260
+ */
2261
+ function is_parent_in_activation() {
2262
+ $parent_fs = $this->get_parent_instance();
2263
+ if ( ! is_object( $parent_fs ) ) {
2264
+ return false;
2265
+ }
2266
+
2267
+ return ( $parent_fs->is_activation_mode() );
2268
+ }
2269
+
2270
+ /**
2271
+ * Is plugin in activation mode.
2272
+ *
2273
+ * @author Vova Feldman (@svovaf)
2274
+ * @since 1.0.7
2275
+ *
2276
+ * @param bool $and_on
2277
+ *
2278
+ * @return bool
2279
+ */
2280
+ function is_activation_mode( $and_on = true ) {
2281
+ return fs_is_network_admin() ?
2282
+ $this->is_network_activation_mode( $and_on ) :
2283
+ $this->is_site_activation_mode( $and_on );
2284
+ }
2285
+
2286
+ /**
2287
+ * Is plugin in activation mode.
2288
+ *
2289
+ * @author Vova Feldman (@svovaf)
2290
+ * @since 1.0.7
2291
+ *
2292
+ * @param bool $and_on
2293
+ *
2294
+ * @return bool
2295
+ */
2296
+ function is_site_activation_mode( $and_on = true ) {
2297
+ return (
2298
+ ( $this->is_on() || ! $and_on ) &&
2299
+ ( ! $this->is_registered() ||
2300
+ ( $this->is_only_premium() && ! $this->has_features_enabled_license() ) ) &&
2301
+ ( ! $this->is_enable_anonymous() ||
2302
+ ( ! $this->is_anonymous() && ! $this->is_pending_activation() ) )
2303
+ );
2304
+ }
2305
+
2306
+ /**
2307
+ * Checks if the SDK in network activation mode.
2308
+ *
2309
+ * @author Leo Fajardo (@leorw)
2310
+ * @since 2.0.0
2311
+ *
2312
+ * @param bool $and_on
2313
+ *
2314
+ * @return bool
2315
+ */
2316
+ private function is_network_activation_mode( $and_on = true ) {
2317
+ if ( ! $this->_is_network_active ) {
2318
+ // Not network activated.
2319
+ return false;
2320
+ }
2321
+
2322
+ if ( $this->is_network_upgrade_mode() ) {
2323
+ // Special flag to enforce network activation mode to decide what to do with the sites that are not yet opted-in nor skipped.
2324
+ return true;
2325
+ }
2326
+
2327
+ if ( ! $this->is_site_activation_mode( $and_on ) ) {
2328
+ // 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.
2329
+ return false;
2330
+ }
2331
+
2332
+ if ( $this->is_network_delegated_connection() ) {
2333
+ // Super-admin delegated the connection to the site admins -> not activation mode.
2334
+ return false;
2335
+ }
2336
+
2337
+ if ( $this->is_network_anonymous() ) {
2338
+ // Super-admin skipped the connection network wide -> not activation mode.
2339
+ return false;
2340
+ }
2341
+
2342
+ if ( $this->is_network_registered() ) {
2343
+ // Super-admin connected at least one site -> not activation mode.
2344
+ return false;
2345
+ }
2346
+
2347
+ return true;
2348
+ }
2349
+
2350
+ /**
2351
+ * Check if current page is the opt-in/pending-activation page.
2352
+ *
2353
+ * @author Vova Feldman (@svovaf)
2354
+ * @since 1.2.1.7
2355
+ *
2356
+ * @return bool
2357
+ */
2358
+ function is_activation_page() {
2359
+ if ( $this->_menu->is_main_settings_page() ) {
2360
+ return true;
2361
+ }
2362
+
2363
+ if ( ! $this->is_activation_mode() ) {
2364
+ return false;
2365
+ }
2366
+
2367
+ // Check if current page is matching the activation page.
2368
+ return $this->is_matching_url( $this->get_activation_url() );
2369
+ }
2370
+
2371
+ /**
2372
+ * Check if URL path's are matching and that all querystring
2373
+ * arguments of the $sub_url exist in the $url with the same values.
2374
+ *
2375
+ * WARNING:
2376
+ * 1. This method doesn't check if the sub/domain are matching.
2377
+ * 2. Ignore case sensitivity.
2378
+ *
2379
+ * @author Vova Feldman (@svovaf)
2380
+ * @since 1.2.1.7
2381
+ *
2382
+ * @param string $sub_url
2383
+ * @param string $url If argument is not set, check if the sub_url matching the current's page URL.
2384
+ *
2385
+ * @return bool
2386
+ */
2387
+ private function is_matching_url( $sub_url, $url = '' ) {
2388
+ if ( empty( $url ) ) {
2389
+ $url = $_SERVER['REQUEST_URI'];
2390
+ }
2391
+
2392
+ $url = strtolower( $url );
2393
+ $sub_url = strtolower( $sub_url );
2394
+
2395
+ if ( parse_url( $sub_url, PHP_URL_PATH ) !== parse_url( $url, PHP_URL_PATH ) ) {
2396
+ // Different path - DO NOT OVERRIDE PAGE.
2397
+ return false;
2398
+ }
2399
+
2400
+ $url_params = array();
2401
+ parse_str( parse_url( $url, PHP_URL_QUERY ), $url_params );
2402
+
2403
+ $sub_url_params = array();
2404
+ parse_str( parse_url( $sub_url, PHP_URL_QUERY ), $sub_url_params );
2405
+
2406
+ foreach ( $sub_url_params as $key => $val ) {
2407
+ if ( ! isset( $url_params[ $key ] ) || $val != $url_params[ $key ] ) {
2408
+ // Not matching query string - DO NOT OVERRIDE PAGE.
2409
+ return false;
2410
+ }
2411
+ }
2412
+
2413
+ return true;
2414
+ }
2415
+
2416
+ /**
2417
+ * Get the basenames of all active plugins for specific blog. Including network activated plugins.
2418
+ *
2419
+ * @author Vova Feldman (@svovaf)
2420
+ * @since 2.0.0
2421
+ *
2422
+ * @param int $blog_id
2423
+ *
2424
+ * @return string[]
2425
+ */
2426
+ private static function get_active_plugins_basenames( $blog_id = 0 ) {
2427
+ if ( is_multisite() && $blog_id > 0 ) {
2428
+ $active_basenames = get_blog_option( $blog_id, 'active_plugins' );
2429
+ } else {
2430
+ $active_basenames = get_option( 'active_plugins' );
2431
+ }
2432
+
2433
+ if ( is_multisite() ) {
2434
+ $network_active_basenames = get_site_option( 'active_sitewide_plugins' );
2435
+
2436
+ if ( is_array( $network_active_basenames ) && ! empty( $network_active_basenames ) ) {
2437
+ $active_basenames = array_merge( $active_basenames, $network_active_basenames );
2438
+ }
2439
+ }
2440
+
2441
+ return $active_basenames;
2442
+ }
2443
+
2444
+ /**
2445
+ * Get collection of all active plugins. Including network activated plugins.
2446
+ *
2447
+ * @author Vova Feldman (@svovaf)
2448
+ * @since 1.0.9
2449
+ *
2450
+ * @param int $blog_id Since 2.0.0
2451
+ *
2452
+ * @return array[string]array
2453
+ */
2454
+ private static function get_active_plugins( $blog_id = 0 ) {
2455
+ self::require_plugin_essentials();
2456
+
2457
+ $active_plugin = array();
2458
+ $all_plugins = get_plugins();
2459
+ $active_plugins_basenames = self::get_active_plugins_basenames( $blog_id );
2460
+
2461
+ foreach ( $active_plugins_basenames as $plugin_basename ) {
2462
+ $active_plugin[ $plugin_basename ] = $all_plugins[ $plugin_basename ];
2463
+ }
2464
+
2465
+ return $active_plugin;
2466
+ }
2467
+
2468
+ /**
2469
+ * Get collection of all site active plugins for a specified blog.
2470
+ *
2471
+ * @author Vova Feldman (@svovaf)
2472
+ * @since 2.0.0
2473
+ *
2474
+ * @param int $blog_id
2475
+ *
2476
+ * @return array[string]array
2477
+ */
2478
+ private static function get_site_active_plugins( $blog_id = 0 ) {
2479
+ $active_basenames = ( is_multisite() && $blog_id > 0 ) ?
2480
+ get_blog_option( $blog_id, 'active_plugins' ) :
2481
+ get_option( 'active_plugins' );
2482
+
2483
+ $active = array();
2484
+ foreach ( $active_basenames as $basename ) {
2485
+ $active[ $basename ] = array(
2486
+ 'is_active' => true,
2487
+ 'Version' => '1.0', // Dummy version.
2488
+ 'slug' => self::get_plugin_slug( $basename ),
2489
+ );
2490
+ }
2491
+
2492
+ return $active;
2493
+ }
2494
+
2495
+ /**
2496
+ * Get collection of all plugins with their activation status for a specified blog.
2497
+ *
2498
+ * @author Vova Feldman (@svovaf)
2499
+ * @since 1.1.8
2500
+ *
2501
+ * @param int $blog_id Since 2.0.0
2502
+ *
2503
+ * @return array Key is the plugin file path and the value is an array of the plugin data.
2504
+ */
2505
+ private static function get_all_plugins( $blog_id = 0 ) {
2506
+ self::require_plugin_essentials();
2507
+
2508
+ $all_plugins = get_plugins();
2509
+
2510
+ $active_plugins_basenames = self::get_active_plugins_basenames( $blog_id );
2511
+
2512
+ foreach ( $all_plugins as $basename => &$data ) {
2513
+ // By default set to inactive (next foreach update the active plugins).
2514
+ $data['is_active'] = false;
2515
+ // Enrich with plugin slug.
2516
+ $data['slug'] = self::get_plugin_slug( $basename );
2517
+ }
2518
+
2519
+ // Flag active plugins.
2520
+ foreach ( $active_plugins_basenames as $basename ) {
2521
+ if ( isset( $all_plugins[ $basename ] ) ) {
2522
+ $all_plugins[ $basename ]['is_active'] = true;
2523
+ }
2524
+ }
2525
+
2526
+ return $all_plugins;
2527
+ }
2528
+
2529
+ /**
2530
+ * Get collection of all plugins and if they are network level activated.
2531
+ *
2532
+ * @author Vova Feldman (@svovaf)
2533
+ * @since 2.0.0
2534
+ *
2535
+ * @return array Key is the plugin basename and the value is an array of the plugin data.
2536
+ */
2537
+ private static function get_network_plugins() {
2538
+ self::require_plugin_essentials();
2539
+
2540
+ $all_plugins = get_plugins();
2541
+
2542
+ $network_active_basenames = is_multisite() ?
2543
+ get_site_option( 'active_sitewide_plugins' ) :
2544
+ array();
2545
+
2546
+ foreach ( $all_plugins as $basename => &$data ) {
2547
+ // By default set to inactive (next foreach update the active plugins).
2548
+ $data['is_active'] = false;
2549
+ // Enrich with plugin slug.
2550
+ $data['slug'] = self::get_plugin_slug( $basename );
2551
+ }
2552
+
2553
+ // Flag active plugins.
2554
+ foreach ( $network_active_basenames as $basename ) {
2555
+ if ( isset( $all_plugins[ $basename ] ) ) {
2556
+ $all_plugins[ $basename ]['is_active'] = true;
2557
+ }
2558
+ }
2559
+
2560
+ return $all_plugins;
2561
+ }
2562
+
2563
+ /**
2564
+ * Cached result of get_site_transient( 'update_plugins' )
2565
+ *
2566
+ * @author Vova Feldman (@svovaf)
2567
+ * @since 1.1.8
2568
+ *
2569
+ * @var object
2570
+ */
2571
+ private static $_plugins_info;
2572
+
2573
+ /**
2574
+ * Helper function to get specified plugin's slug.
2575
+ *
2576
+ * @author Vova Feldman (@svovaf)
2577
+ * @since 1.1.8
2578
+ *
2579
+ * @param $basename
2580
+ *
2581
+ * @return string
2582
+ */
2583
+ private static function get_plugin_slug( $basename ) {
2584
+ if ( ! isset( self::$_plugins_info ) ) {
2585
+ self::$_plugins_info = get_site_transient( 'update_plugins' );
2586
+ }
2587
+
2588
+ $slug = '';
2589
+
2590
+ if ( is_object( self::$_plugins_info ) ) {
2591
+ if ( isset( self::$_plugins_info->no_update ) &&
2592
+ isset( self::$_plugins_info->no_update[ $basename ] ) &&
2593
+ ! empty( self::$_plugins_info->no_update[ $basename ]->slug )
2594
+ ) {
2595
+ $slug = self::$_plugins_info->no_update[ $basename ]->slug;
2596
+ } else if ( isset( self::$_plugins_info->response ) &&
2597
+ isset( self::$_plugins_info->response[ $basename ] ) &&
2598
+ ! empty( self::$_plugins_info->response[ $basename ]->slug )
2599
+ ) {
2600
+ $slug = self::$_plugins_info->response[ $basename ]->slug;
2601
+ }
2602
+ }
2603
+
2604
+ if ( empty( $slug ) ) {
2605
+ // Try to find slug from FS data.
2606
+ $slug = self::find_slug_by_basename( $basename );
2607
+ }
2608
+
2609
+ if ( empty( $slug ) ) {
2610
+ // Fallback to plugin's folder name.
2611
+ $slug = dirname( $basename );
2612
+ }
2613
+
2614
+ return $slug;
2615
+ }
2616
+
2617
+ private static $_statics_loaded = false;
2618
+
2619
+ /**
2620
+ * Load static resources.
2621
+ *
2622
+ * @author Vova Feldman (@svovaf)
2623
+ * @since 1.0.1
2624
+ */
2625
+ private static function _load_required_static() {
2626
+ if ( self::$_statics_loaded ) {
2627
+ return;
2628
+ }
2629
+
2630
+ self::$_static_logger = FS_Logger::get_logger( WP_FS__SLUG, WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
2631
+
2632
+ self::$_static_logger->entrance();
2633
+
2634
+ self::$_accounts = FS_Options::instance( WP_FS__ACCOUNTS_OPTION_NAME, true );
2635
+
2636
+ if ( is_multisite() ) {
2637
+ /**
2638
+ * If the id_slug_type_path_map exists on the site level but doesn't exist on the
2639
+ * network level storage, it means that we need to process the storage with migration.
2640
+ *
2641
+ * 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.
2642
+ *
2643
+ * @author Vova Feldman (@svovaf)
2644
+ * @since 2.0.0
2645
+ */
2646
+ if ( null === self::$_accounts->get_option( 'id_slug_type_path_map', null, true ) &&
2647
+ null !== self::$_accounts->get_option( 'id_slug_type_path_map', null, false )
2648
+ ) {
2649
+ self::migrate_accounts_to_network();
2650
+
2651
+ // Migrate API options from site level to network level.
2652
+ $api_network_options = FS_Option_Manager::get_manager( WP_FS__OPTIONS_OPTION_NAME, true, true );
2653
+ $api_network_options->migrate_to_network();
2654
+
2655
+ // Migrate API cache to network level storage.
2656
+ FS_Cache_Manager::get_manager( WP_FS__API_CACHE_OPTION_NAME )->migrate_to_network();
2657
+ }
2658
+ }
2659
+
2660
+ self::$_global_admin_notices = FS_Admin_Notices::instance( 'global' );
2661
+
2662
+ if ( ! WP_FS__DEMO_MODE ) {
2663
+ add_action( ( fs_is_network_admin() ? 'network_' : '' ) . 'admin_menu', array(
2664
+ 'Freemius',
2665
+ '_add_debug_section'
2666
+ ) );
2667
+ }
2668
+
2669
+ add_action( "wp_ajax_fs_toggle_debug_mode", array( 'Freemius', '_toggle_debug_mode' ) );
2670
+
2671
+ self::add_ajax_action_static( 'get_debug_log', array( 'Freemius', '_get_debug_log' ) );
2672
+
2673
+ self::add_ajax_action_static( 'get_db_option', array( 'Freemius', '_get_db_option' ) );
2674
+
2675
+ self::add_ajax_action_static( 'set_db_option', array( 'Freemius', '_set_db_option' ) );
2676
+
2677
+ if ( 0 == did_action( 'plugins_loaded' ) ) {
2678
+ add_action( 'plugins_loaded', array( 'Freemius', '_load_textdomain' ), 1 );
2679
+ }
2680
+
2681
+ add_action( 'admin_footer', array( 'Freemius', '_enrich_ajax_url' ) );
2682
+
2683
+ self::$_statics_loaded = true;
2684
+ }
2685
+
2686
+ #----------------------------------------------------------------------------------
2687
+ #region Localization
2688
+ #----------------------------------------------------------------------------------
2689
+
2690
+ /**
2691
+ * Load framework's text domain.
2692
+ *
2693
+ * @author Vova Feldman (@svovaf)
2694
+ * @since 1.2.1
2695
+ */
2696
+ static function _load_textdomain() {
2697
+ if ( ! is_admin() ) {
2698
+ return;
2699
+ }
2700
+
2701
+ global $fs_active_plugins;
2702
+
2703
+ // Works both for plugins and themes.
2704
+ load_plugin_textdomain(
2705
+ 'freemius',
2706
+ false,
2707
+ $fs_active_plugins->newest->sdk_path . '/languages/'
2708
+ );
2709
+ }
2710
+
2711
+ #endregion
2712
+
2713
+ #----------------------------------------------------------------------------------
2714
+ #region Debugging
2715
+ #----------------------------------------------------------------------------------
2716
+
2717
+ /**
2718
+ * @author Vova Feldman (@svovaf)
2719
+ * @since 1.0.8
2720
+ */
2721
+ static function _add_debug_section() {
2722
+ if ( ! is_super_admin() ) {
2723
+ // Add debug page only for super-admins.
2724
+ return;
2725
+ }
2726
+
2727
+ self::$_static_logger->entrance();
2728
+
2729
+ $title = sprintf( '%s [v.%s]', fs_text_inline( 'Freemius Debug' ), WP_FS__SDK_VERSION );
2730
+
2731
+ if ( WP_FS__DEV_MODE ) {
2732
+ // Add top-level debug menu item.
2733
+ $hook = FS_Admin_Menu_Manager::add_page(
2734
+ $title,
2735
+ $title,
2736
+ 'manage_options',
2737
+ 'freemius',
2738
+ array( 'Freemius', '_debug_page_render' )
2739
+ );
2740
+ } else {
2741
+ // Add hidden debug page.
2742
+ $hook = FS_Admin_Menu_Manager::add_subpage(
2743
+ null,
2744
+ $title,
2745
+ $title,
2746
+ 'manage_options',
2747
+ 'freemius',
2748
+ array( 'Freemius', '_debug_page_render' )
2749
+ );
2750
+ }
2751
+
2752
+ if ( ! empty( $hook ) ) {
2753
+ add_action( "load-$hook", array( 'Freemius', '_debug_page_actions' ) );
2754
+ }
2755
+ }
2756
+
2757
+ /**
2758
+ * @author Vova Feldman (@svovaf)
2759
+ * @since 1.1.7.3
2760
+ */
2761
+ static function _toggle_debug_mode() {
2762
+ $is_on = fs_request_get( 'is_on', false, 'post' );
2763
+
2764
+ if ( fs_request_is_post() && in_array( $is_on, array( 0, 1 ) ) ) {
2765
+ update_option( 'fs_debug_mode', $is_on );
2766
+
2767
+ // Turn on/off storage logging.
2768
+ FS_Logger::_set_storage_logging( ( 1 == $is_on ) );
2769
+ }
2770
+
2771
+ exit;
2772
+ }
2773
+
2774
+ /**
2775
+ * @author Vova Feldman (@svovaf)
2776
+ * @since 1.2.1.6
2777
+ */
2778
+ static function _get_debug_log() {
2779
+ $logs = FS_Logger::load_db_logs(
2780
+ fs_request_get( 'filters', false, 'post' ),
2781
+ ! empty( $_POST['limit'] ) && is_numeric( $_POST['limit'] ) ? $_POST['limit'] : 200,
2782
+ ! empty( $_POST['offset'] ) && is_numeric( $_POST['offset'] ) ? $_POST['offset'] : 0
2783
+ );
2784
+
2785
+ self::shoot_ajax_success( $logs );
2786
+ }
2787
+
2788
+ /**
2789
+ * @author Vova Feldman (@svovaf)
2790
+ * @since 1.2.1.7
2791
+ */
2792
+ static function _get_db_option() {
2793
+ $option_name = fs_request_get( 'option_name' );
2794
+
2795
+ $value = get_option( $option_name );
2796
+
2797
+ $result = array(
2798
+ 'name' => $option_name,
2799
+ );
2800
+
2801
+ if ( false !== $value ) {
2802
+ if ( ! is_string( $value ) ) {
2803
+ $value = json_encode( $value );
2804
+ }
2805
+
2806
+ $result['value'] = $value;
2807
+ }
2808
+
2809
+ self::shoot_ajax_success( $result );
2810
+ }
2811
+
2812
+ /**
2813
+ * @author Vova Feldman (@svovaf)
2814
+ * @since 1.2.1.7
2815
+ */
2816
+ static function _set_db_option() {
2817
+ $option_name = fs_request_get( 'option_name' );
2818
+ $option_value = fs_request_get( 'option_value' );
2819
+
2820
+ if ( ! empty( $option_value ) ) {
2821
+ update_option( $option_name, $option_value );
2822
+ }
2823
+
2824
+ self::shoot_ajax_success();
2825
+ }
2826
+
2827
+ /**
2828
+ * @author Vova Feldman (@svovaf)
2829
+ * @since 1.0.8
2830
+ */
2831
+ static function _debug_page_actions() {
2832
+ self::_clean_admin_content_section();
2833
+
2834
+ if ( fs_request_is_action( 'restart_freemius' ) ) {
2835
+ check_admin_referer( 'restart_freemius' );
2836
+
2837
+ if ( ! is_multisite() ) {
2838
+ // Clear accounts data.
2839
+ self::$_accounts->clear( null, true );
2840
+ } else {
2841
+ $sites = self::get_sites();
2842
+ foreach ( $sites as $site ) {
2843
+ $blog_id = self::get_site_blog_id( $site );
2844
+ self::$_accounts->clear( $blog_id, true );
2845
+ }
2846
+
2847
+ // Clear network level storage.
2848
+ self::$_accounts->clear( true, true );
2849
+ }
2850
+
2851
+ // Clear SDK reference cache.
2852
+ delete_option( 'fs_active_plugins' );
2853
+ } else if ( fs_request_is_action( 'clear_updates_data' ) ) {
2854
+ check_admin_referer( 'clear_updates_data' );
2855
+
2856
+ if ( ! is_multisite() ) {
2857
+ set_site_transient( 'update_plugins', null );
2858
+ set_site_transient( 'update_themes', null );
2859
+ } else {
2860
+ $current_blog_id = get_current_blog_id();
2861
+
2862
+ $sites = self::get_sites();
2863
+ foreach ( $sites as $site ) {
2864
+ switch_to_blog( self::get_site_blog_id( $site ) );
2865
+
2866
+ set_site_transient( 'update_plugins', null );
2867
+ set_site_transient( 'update_themes', null );
2868
+ }
2869
+
2870
+ switch_to_blog( $current_blog_id );
2871
+ }
2872
+ } else if ( fs_request_is_action( 'simulate_trial' ) ) {
2873
+ check_admin_referer( 'simulate_trial' );
2874
+
2875
+ $fs = freemius( fs_request_get( 'module_id' ) );
2876
+
2877
+ // Update SDK install to at least 24 hours before.
2878
+ $fs->_storage->install_timestamp = ( time() - WP_FS__TIME_24_HOURS_IN_SEC );
2879
+ // Unset the trial shown timestamp.
2880
+ unset( $fs->_storage->trial_promotion_shown );
2881
+ } else if ( fs_request_is_action( 'simulate_network_upgrade' ) ) {
2882
+ check_admin_referer( 'simulate_network_upgrade' );
2883
+
2884
+ $fs = freemius( fs_request_get( 'module_id' ) );
2885
+
2886
+ self::set_network_upgrade_mode( $fs->_storage );
2887
+ } else if ( fs_request_is_action( 'delete_install' ) ) {
2888
+ check_admin_referer( 'delete_install' );
2889
+
2890
+ self::_delete_site_by_slug(
2891
+ fs_request_get( 'slug' ),
2892
+ fs_request_get( 'module_type' ),
2893
+ true,
2894
+ fs_request_get( 'blog_id', null )
2895
+ );
2896
+ } else if ( fs_request_is_action( 'delete_user' ) ) {
2897
+ check_admin_referer( 'delete_user' );
2898
+
2899
+ self::delete_user( fs_request_get( 'user_id' ) );
2900
+ } else if ( fs_request_is_action( 'download_logs' ) ) {
2901
+ check_admin_referer( 'download_logs' );
2902
+
2903
+ $download_url = FS_Logger::download_db_logs(
2904
+ fs_request_get( 'filters', false, 'post' )
2905
+ );
2906
+
2907
+ if ( false === $download_url ) {
2908
+ 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.' );
2909
+ }
2910
+
2911
+ fs_redirect( $download_url );
2912
+ }
2913
+ }
2914
+
2915
+ /**
2916
+ * @author Vova Feldman (@svovaf)
2917
+ * @since 1.0.8
2918
+ */
2919
+ static function _debug_page_render() {
2920
+ self::$_static_logger->entrance();
2921
+
2922
+ if ( ! is_multisite() ) {
2923
+ $all_plugins_installs = self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN );
2924
+ $all_themes_installs = self::get_all_sites( WP_FS__MODULE_TYPE_THEME );
2925
+ } else {
2926
+ $sites = self::get_sites();
2927
+
2928
+ $all_plugins_installs = array();
2929
+ $all_themes_installs = array();
2930
+
2931
+ foreach ( $sites as $site ) {
2932
+ $blog_id = self::get_site_blog_id( $site );
2933
+
2934
+ $plugins_installs = self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN, $blog_id );
2935
+
2936
+ foreach ( $plugins_installs as $slug => $install ) {
2937
+ if ( ! isset( $all_plugins_installs[ $slug ] ) ) {
2938
+ $all_plugins_installs[ $slug ] = array();
2939
+ }
2940
+
2941
+ $install->blog_id = $blog_id;
2942
+
2943
+ $all_plugins_installs[ $slug ][] = $install;
2944
+ }
2945
+
2946
+ $themes_installs = self::get_all_sites( WP_FS__MODULE_TYPE_THEME, $blog_id );
2947
+
2948
+ foreach ( $themes_installs as $slug => $install ) {
2949
+ if ( ! isset( $all_themes_installs[ $slug ] ) ) {
2950
+ $all_themes_installs[ $slug ] = array();
2951
+ }
2952
+
2953
+ $install->blog_id = $blog_id;
2954
+
2955
+ $all_themes_installs[ $slug ][] = $install;
2956
+ }
2957
+ }
2958
+ }
2959
+
2960
+ $licenses_by_module_type = self::get_all_licenses_by_module_type();
2961
+
2962
+ $vars = array(
2963
+ 'plugin_sites' => $all_plugins_installs,
2964
+ 'theme_sites' => $all_themes_installs,
2965
+ 'users' => self::get_all_users(),
2966
+ 'addons' => self::get_all_addons(),
2967
+ 'account_addons' => self::get_all_account_addons(),
2968
+ 'plugin_licenses' => $licenses_by_module_type[ WP_FS__MODULE_TYPE_PLUGIN ],
2969
+ 'theme_licenses' => $licenses_by_module_type[ WP_FS__MODULE_TYPE_THEME ]
2970
+ );
2971
+
2972
+ fs_enqueue_local_style( 'fs_debug', '/admin/debug.css' );
2973
+ fs_require_once_template( 'debug.php', $vars );
2974
+ }
2975
+
2976
+ #endregion
2977
+
2978
+ #----------------------------------------------------------------------------------
2979
+ #region Connectivity Issues
2980
+ #----------------------------------------------------------------------------------
2981
+
2982
+ /**
2983
+ * Check if Freemius should be turned on for the current plugin install.
2984
+ *
2985
+ * Note:
2986
+ * $this->_is_on is updated in has_api_connectivity()
2987
+ *
2988
+ * @author Vova Feldman (@svovaf)
2989
+ * @since 1.0.9
2990
+ *
2991
+ * @return bool
2992
+ */
2993
+ function is_on() {
2994
+ self::$_static_logger->entrance();
2995
+
2996
+ if ( isset( $this->_is_on ) ) {
2997
+ return $this->_is_on;
2998
+ }
2999
+
3000
+ // If already installed or pending then sure it's on :)
3001
+ if ( $this->is_registered() || $this->is_pending_activation() ) {
3002
+ $this->_is_on = true;
3003
+
3004
+ return true;
3005
+ }
3006
+
3007
+ return false;
3008
+ }
3009
+
3010
+ /**
3011
+ * @author Vova Feldman (@svovaf)
3012
+ * @since 1.1.7.3
3013
+ *
3014
+ * @param bool $flush_if_no_connectivity
3015
+ *
3016
+ * @return bool
3017
+ */
3018
+ private function should_run_connectivity_test( $flush_if_no_connectivity = false ) {
3019
+ if ( ! isset( $this->_storage->connectivity_test ) ) {
3020
+ // Connectivity test was never executed, or cache was cleared.
3021
+ return true;
3022
+ }
3023
+
3024
+ if ( WP_FS__PING_API_ON_IP_OR_HOST_CHANGES ) {
3025
+ if ( WP_FS__IS_HTTP_REQUEST ) {
3026
+ if ( $_SERVER['HTTP_HOST'] != $this->_storage->connectivity_test['host'] ) {
3027
+ // Domain changed.
3028
+ return true;
3029
+ }
3030
+
3031
+ if ( WP_FS__REMOTE_ADDR != $this->_storage->connectivity_test['server_ip'] ) {
3032
+ // Server IP changed.
3033
+ return true;
3034
+ }
3035
+ }
3036
+ }
3037
+
3038
+ if ( $this->_storage->connectivity_test['is_connected'] &&
3039
+ $this->_storage->connectivity_test['is_active']
3040
+ ) {
3041
+ // API connected and Freemius is active - no need to run connectivity check.
3042
+ return false;
3043
+ }
3044
+
3045
+ if ( $flush_if_no_connectivity ) {
3046
+ /**
3047
+ * If explicitly asked to flush when no connectivity - do it only
3048
+ * if at least 10 sec passed from the last API connectivity test.
3049
+ */
3050
+ return ( isset( $this->_storage->connectivity_test['timestamp'] ) &&
3051
+ ( WP_FS__SCRIPT_START_TIME - $this->_storage->connectivity_test['timestamp'] ) > 10 );
3052
+ }
3053
+
3054
+ /**
3055
+ * @since 1.1.7 Don't check for connectivity on plugin downgrade.
3056
+ */
3057
+ $version = $this->get_plugin_version();
3058
+ if ( version_compare( $version, $this->_storage->connectivity_test['version'], '>' ) ) {
3059
+ // If it's a plugin version upgrade and Freemius is off or no connectivity, run connectivity test.
3060
+ return true;
3061
+ }
3062
+
3063
+ return false;
3064
+ }
3065
+
3066
+ /**
3067
+ * @author Vova Feldman (@svovaf)
3068
+ * @since 1.1.7.4
3069
+ *
3070
+ * @param int|null $blog_id Since 2.0.0.
3071
+ * @param bool $is_gdpr_test Since 2.0.2. Perform only the GDPR test.
3072
+ *
3073
+ * @return object|false
3074
+ */
3075
+ private function ping( $blog_id = null, $is_gdpr_test = false ) {
3076
+ if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY ) {
3077
+ return false;
3078
+ }
3079
+
3080
+ $version = $this->get_plugin_version();
3081
+
3082
+ $is_update = $this->apply_filters( 'is_plugin_update', $this->is_plugin_update() );
3083
+
3084
+ return $this->get_api_plugin_scope()->ping(
3085
+ $this->get_anonymous_id( $blog_id ),
3086
+ array(
3087
+ 'is_update' => json_encode( $is_update ),
3088
+ 'version' => $version,
3089
+ 'sdk' => $this->version,
3090
+ 'is_admin' => json_encode( is_admin() ),
3091
+ 'is_ajax' => json_encode( self::is_ajax() ),
3092
+ 'is_cron' => json_encode( self::is_cron() ),
3093
+ 'is_gdpr_test' => $is_gdpr_test,
3094
+ 'is_http' => json_encode( WP_FS__IS_HTTP_REQUEST ),
3095
+ )
3096
+ );
3097
+ }
3098
+
3099
+ /**
3100
+ * Check if there's any connectivity issue to Freemius API.
3101
+ *
3102
+ * @author Vova Feldman (@svovaf)
3103
+ * @since 1.0.9
3104
+ *
3105
+ * @param bool $flush_if_no_connectivity
3106
+ *
3107
+ * @return bool
3108
+ */
3109
+ function has_api_connectivity( $flush_if_no_connectivity = false ) {
3110
+ $this->_logger->entrance();
3111
+
3112
+ if ( isset( $this->_has_api_connection ) && ( $this->_has_api_connection || ! $flush_if_no_connectivity ) ) {
3113
+ return $this->_has_api_connection;
3114
+ }
3115
+
3116
+ if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY &&
3117
+ isset( $this->_storage->connectivity_test ) &&
3118
+ true === $this->_storage->connectivity_test['is_connected']
3119
+ ) {
3120
+ unset( $this->_storage->connectivity_test );
3121
+ }
3122
+
3123
+ if ( ! $this->should_run_connectivity_test( $flush_if_no_connectivity ) ) {
3124
+ $this->_has_api_connection = $this->_storage->connectivity_test['is_connected'];
3125
+ /**
3126
+ * @since 1.1.6 During dev mode, if there's connectivity - turn Freemius on regardless the configuration.
3127
+ *
3128
+ * @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.
3129
+ */
3130
+ $this->_is_on = $this->_storage->connectivity_test['is_active'] ||
3131
+ $this->is_premium() ||
3132
+ ( WP_FS__DEV_MODE && $this->_has_api_connection && ! WP_FS__SIMULATE_FREEMIUS_OFF );
3133
+
3134
+ return $this->_has_api_connection;
3135
+ }
3136
+
3137
+ $pong = $this->ping();
3138
+ $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
3139
+
3140
+ if ( ! $is_connected ) {
3141
+ // API failure.
3142
+ $this->_add_connectivity_issue_message( $pong );
3143
+ }
3144
+
3145
+ if ( $is_connected ) {
3146
+ FS_GDPR_Manager::instance()->store_is_required( $pong->is_gdpr_required );
3147
+ }
3148
+
3149
+ $this->store_connectivity_info( $pong, $is_connected );
3150
+
3151
+ return $this->_has_api_connection;
3152
+ }
3153
+
3154
+ /**
3155
+ * @author Vova Feldman (@svovaf)
3156
+ * @since 1.1.7.4
3157
+ *
3158
+ * @param object $pong
3159
+ * @param bool $is_connected
3160
+ */
3161
+ private function store_connectivity_info( $pong, $is_connected ) {
3162
+ $this->_logger->entrance();
3163
+
3164
+ $version = $this->get_plugin_version();
3165
+
3166
+ if ( ! $is_connected || WP_FS__SIMULATE_FREEMIUS_OFF ) {
3167
+ $is_active = false;
3168
+ } else {
3169
+ $is_active = ( isset( $pong->is_active ) && true == $pong->is_active );
3170
+ }
3171
+
3172
+ $is_active = $this->apply_filters(
3173
+ 'is_on',
3174
+ $is_active,
3175
+ $this->is_plugin_update(),
3176
+ $version
3177
+ );
3178
+
3179
+ $this->_storage->connectivity_test = array(
3180
+ 'is_connected' => $is_connected,
3181
+ 'host' => $_SERVER['HTTP_HOST'],
3182
+ 'server_ip' => WP_FS__REMOTE_ADDR,
3183
+ 'is_active' => $is_active,
3184
+ 'timestamp' => WP_FS__SCRIPT_START_TIME,
3185
+ // Last version with connectivity attempt.
3186
+ 'version' => $version,
3187
+ );
3188
+
3189
+ $this->_has_api_connection = $is_connected;
3190
+ $this->_is_on = $is_active || ( WP_FS__DEV_MODE && $is_connected && ! WP_FS__SIMULATE_FREEMIUS_OFF );
3191
+ }
3192
+
3193
+ /**
3194
+ * Force turning Freemius on.
3195
+ *
3196
+ * @author Vova Feldman (@svovaf)
3197
+ * @since 1.1.8.1
3198
+ *
3199
+ * @return bool TRUE if successfully turned on.
3200
+ */
3201
+ private function turn_on() {
3202
+ $this->_logger->entrance();
3203
+
3204
+ if ( $this->is_on() || ! isset( $this->_storage->connectivity_test['is_active'] ) ) {
3205
+ return false;
3206
+ }
3207
+
3208
+ $updated_connectivity = $this->_storage->connectivity_test;
3209
+ $updated_connectivity['is_active'] = true;
3210
+ $updated_connectivity['timestamp'] = WP_FS__SCRIPT_START_TIME;
3211
+ $this->_storage->connectivity_test = $updated_connectivity;
3212
+
3213
+ $this->_is_on = true;
3214
+
3215
+ return true;
3216
+ }
3217
+
3218
+ /**
3219
+ * Anonymous and unique site identifier (Hash).
3220
+ *
3221
+ * @author Vova Feldman (@svovaf)
3222
+ * @since 1.1.0
3223
+ *
3224
+ * @param null|int $blog_id Since 2.0.0
3225
+ *
3226
+ * @return string
3227
+ */
3228
+ function get_anonymous_id( $blog_id = null ) {
3229
+ $unique_id = self::$_accounts->get_option( 'unique_id', null, $blog_id );
3230
+
3231
+ if ( empty( $unique_id ) || ! is_string( $unique_id ) ) {
3232
+ $key = fs_strip_url_protocol( get_site_url( $blog_id ) );
3233
+
3234
+ $secure_auth = SECURE_AUTH_KEY;
3235
+ if ( empty( $secure_auth ) || false !== strpos( $secure_auth, ' ' ) ) {
3236
+ // Protect against default auth key.
3237
+ $secure_auth = md5( microtime() );
3238
+ }
3239
+
3240
+ /**
3241
+ * Base the unique identifier on the WP secure authentication key. Which
3242
+ * turns the key into a secret anonymous identifier. This will help us
3243
+ * to avoid duplicate installs generation on the backend upon opt-in.
3244
+ *
3245
+ * @author Vova Feldman (@svovaf)
3246
+ * @since 1.2.3
3247
+ */
3248
+ $unique_id = md5( $key . $secure_auth );
3249
+
3250
+ self::$_accounts->set_option( 'unique_id', $unique_id, true, $blog_id );
3251
+ }
3252
+
3253
+ $this->_logger->departure( $unique_id );
3254
+
3255
+ return $unique_id;
3256
+ }
3257
+
3258
+ /**
3259
+ * @author Vova Feldman (@svovaf)
3260
+ * @since 1.1.7.4
3261
+ *
3262
+ * @return \WP_User
3263
+ */
3264
+ static function _get_current_wp_user() {
3265
+ self::require_pluggable_essentials();
3266
+ self::wp_cookie_constants();
3267
+
3268
+ return wp_get_current_user();
3269
+ }
3270
+
3271
+ /**
3272
+ * Define cookie constants which are required by Freemius::_get_current_wp_user() since
3273
+ * it uses wp_get_current_user() which needs the cookie constants set. When a plugin
3274
+ * is network activated the cookie constants are only configured after the network
3275
+ * plugins activation, therefore, if we don't define those constants WP will throw
3276
+ * PHP warnings/notices.
3277
+ *
3278
+ * @author Vova Feldman (@svovaf)
3279
+ * @since 2.1.1
3280
+ */
3281
+ private static function wp_cookie_constants() {
3282
+ if ( defined( 'LOGGED_IN_COOKIE' ) &&
3283
+ ( defined( 'AUTH_COOKIE' ) || defined( 'SECURE_AUTH_COOKIE' ) )
3284
+ ) {
3285
+ return;
3286
+ }
3287
+
3288
+ /**
3289
+ * Used to guarantee unique hash cookies
3290
+ *
3291
+ * @since 1.5.0
3292
+ */
3293
+ if ( ! defined( 'COOKIEHASH' ) ) {
3294
+ $siteurl = get_site_option( 'siteurl' );
3295
+ if ( $siteurl ) {
3296
+ define( 'COOKIEHASH', md5( $siteurl ) );
3297
+ } else {
3298
+ define( 'COOKIEHASH', '' );
3299
+ }
3300
+ }
3301
+
3302
+ if ( ! defined( 'LOGGED_IN_COOKIE' ) ) {
3303
+ define( 'LOGGED_IN_COOKIE', 'wordpress_logged_in_' . COOKIEHASH );
3304
+ }
3305
+
3306
+ /**
3307
+ * @since 2.5.0
3308
+ */
3309
+ if ( ! defined( 'AUTH_COOKIE' ) ) {
3310
+ define( 'AUTH_COOKIE', 'wordpress_' . COOKIEHASH );
3311
+ }
3312
+
3313
+ /**
3314
+ * @since 2.6.0
3315
+ */
3316
+ if ( ! defined( 'SECURE_AUTH_COOKIE' ) ) {
3317
+ define( 'SECURE_AUTH_COOKIE', 'wordpress_sec_' . COOKIEHASH );
3318
+ }
3319
+ }
3320
+
3321
+ /**
3322
+ * @author Vova Feldman (@svovaf)
3323
+ * @since 2.1.0
3324
+ *
3325
+ * @return int
3326
+ */
3327
+ static function get_current_wp_user_id() {
3328
+ $wp_user = self::_get_current_wp_user();
3329
+
3330
+ return $wp_user->ID;
3331
+ }
3332
+
3333
+ /**
3334
+ * @author Vova Feldman (@svovaf)
3335
+ * @since 1.2.1.7
3336
+ *
3337
+ * @param string $email
3338
+ *
3339
+ * @return bool
3340
+ */
3341
+ static function is_valid_email( $email ) {
3342
+ if ( false === filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
3343
+ return false;
3344
+ }
3345
+
3346
+ $parts = explode( '@', $email );
3347
+
3348
+ if ( 2 !== count( $parts ) || empty( $parts[1] ) ) {
3349
+ return false;
3350
+ }
3351
+
3352
+ $blacklist = array(
3353
+ 'admin.',
3354
+ 'webmaster.',
3355
+ 'localhost.',
3356
+ 'dev.',
3357
+ 'development.',
3358
+ 'test.',
3359
+ 'stage.',
3360
+ 'staging.',
3361
+ );
3362
+
3363
+ // Make sure domain is not one of the blacklisted.
3364
+ foreach ( $blacklist as $invalid ) {
3365
+ if ( 0 === strpos( $parts[1], $invalid ) ) {
3366
+ return false;
3367
+ }
3368
+ }
3369
+
3370
+ // Get the UTF encoded domain name.
3371
+ $domain = idn_to_ascii( $parts[1] ) . '.';
3372
+
3373
+ return ( checkdnsrr( $domain, 'MX' ) || checkdnsrr( $domain, 'A' ) );
3374
+ }
3375
+
3376
+ /**
3377
+ * Generate API connectivity issue message.
3378
+ *
3379
+ * @author Vova Feldman (@svovaf)
3380
+ * @since 1.0.9
3381
+ *
3382
+ * @param mixed $api_result
3383
+ * @param bool $is_first_failure
3384
+ */
3385
+ function _add_connectivity_issue_message( $api_result, $is_first_failure = true ) {
3386
+ if ( ! $this->is_premium() && $this->_enable_anonymous ) {
3387
+ // Don't add message if it's the free version and can run anonymously.
3388
+ return;
3389
+ }
3390
+
3391
+ if ( ! function_exists( 'wp_nonce_url' ) ) {
3392
+ require_once ABSPATH . 'wp-includes/functions.php';
3393
+ }
3394
+
3395
+ $current_user = self::_get_current_wp_user();
3396
+ // $admin_email = get_option( 'admin_email' );
3397
+ $admin_email = $current_user->user_email;
3398
+
3399
+ // Aliases.
3400
+ $deactivate_plugin_title = $this->esc_html_inline( 'That\'s exhausting, please deactivate', 'deactivate-plugin-title' );
3401
+ $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' );
3402
+ $install_previous_title = $this->esc_html_inline( 'Let\'s try your previous version', 'install-previous-title' );
3403
+ $install_previous_desc = $this->esc_html_inline( 'Uninstall this version and install the previous one.', 'install-previous-desc' );
3404
+ $fix_issue_title = $this->esc_html_inline( 'Yes - I\'m giving you a chance to fix it', 'fix-issue-title' );
3405
+ $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' );
3406
+ /* translators: %s: product title (e.g. "Awesome Plugin" requires an access to...) */
3407
+ $x_requires_access_to_api = $this->esc_html_inline( '%s requires an access to our API.', 'x-requires-access-to-api' );
3408
+ $sysadmin_title = $this->esc_html_inline( 'I\'m a system administrator', 'sysadmin-title' );
3409
+ $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' );
3410
+
3411
+ $message = false;
3412
+ if ( is_object( $api_result ) &&
3413
+ isset( $api_result->error ) &&
3414
+ isset( $api_result->error->code )
3415
+ ) {
3416
+ switch ( $api_result->error->code ) {
3417
+ case 'curl_missing':
3418
+ $missing_methods = '';
3419
+ if ( is_array( $api_result->missing_methods ) &&
3420
+ ! empty( $api_result->missing_methods )
3421
+ ) {
3422
+ foreach ( $api_result->missing_methods as $m ) {
3423
+ if ( 'curl_version' === $m ) {
3424
+ continue;
3425
+ }
3426
+
3427
+ if ( ! empty( $missing_methods ) ) {
3428
+ $missing_methods .= ', ';
3429
+ }
3430
+
3431
+ $missing_methods .= sprintf( '<code>%s</code>', $m );
3432
+ }
3433
+
3434
+ if ( ! empty( $missing_methods ) ) {
3435
+ $missing_methods = sprintf(
3436
+ '<br><br><b>%s</b> %s',
3437
+ $this->esc_html_inline( 'Disabled method(s):', 'curl-disabled-methods' ),
3438
+ $missing_methods
3439
+ );
3440
+ }
3441
+ }
3442
+
3443
+ $message = sprintf(
3444
+ $x_requires_access_to_api . ' ' .
3445
+ $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' ) . ' ' .
3446
+ $missing_methods .
3447
+ ' %s',
3448
+ '<b>' . $this->get_plugin_name() . '</b>',
3449
+ sprintf(
3450
+ '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
3451
+ sprintf(
3452
+ '<a class="fs-resolve" data-type="curl" href="#"><b>%s</b></a>%s',
3453
+ $this->get_text_inline( 'I don\'t know what is cURL or how to install it, help me!', 'curl-missing-no-clue-title' ),
3454
+ ' - ' . sprintf(
3455
+ $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' ),
3456
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
3457
+ )
3458
+ ),
3459
+ sprintf(
3460
+ '<b>%s</b> - %s',
3461
+ $sysadmin_title,
3462
+ 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 ) ) )
3463
+ ),
3464
+ sprintf(
3465
+ '<a href="%s"><b>%s</b></a> - %s',
3466
+ 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 ),
3467
+ $deactivate_plugin_title,
3468
+ $deactivate_plugin_desc
3469
+ )
3470
+ )
3471
+ );
3472
+ break;
3473
+ case 'cloudflare_ddos_protection':
3474
+ $message = sprintf(
3475
+ $x_requires_access_to_api . ' ' .
3476
+ $this->esc_html_inline( 'From unknown reason, CloudFlare, the firewall we use, blocks the connection.', 'cloudflare-blocks-connection-message' ) . ' ' .
3477
+ $happy_to_resolve_issue_asap .
3478
+ ' %s',
3479
+ '<b>' . $this->get_plugin_name() . '</b>',
3480
+ sprintf(
3481
+ '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
3482
+ sprintf(
3483
+ '<a class="fs-resolve" data-type="cloudflare" href="#"><b>%s</b></a>%s',
3484
+ $fix_issue_title,
3485
+ ' - ' . sprintf(
3486
+ $fix_issue_desc,
3487
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
3488
+ )
3489
+ ),
3490
+ sprintf(
3491
+ '<a href="%s" target="_blank"><b>%s</b></a> - %s',
3492
+ sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),
3493
+ $install_previous_title,
3494
+ $install_previous_desc
3495
+ ),
3496
+ sprintf(
3497
+ '<a href="%s"><b>%s</b></a> - %s',
3498
+ 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 ),
3499
+ $deactivate_plugin_title,
3500
+ $deactivate_plugin_desc
3501
+ )
3502
+ )
3503
+ );
3504
+ break;
3505
+ case 'squid_cache_block':
3506
+ $message = sprintf(
3507
+ $x_requires_access_to_api . ' ' .
3508
+ $this->esc_html_inline( 'It looks like your server is using Squid ACL (access control lists), which blocks the connection.', 'squid-blocks-connection-message' ) .
3509
+ ' %s',
3510
+ '<b>' . $this->get_plugin_name() . '</b>',
3511
+ sprintf(
3512
+ '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
3513
+ sprintf(
3514
+ '<a class="fs-resolve" data-type="squid" href="#"><b>%s</b></a> - %s',
3515
+ $this->esc_html_inline( 'I don\'t know what is Squid or ACL, help me!', 'squid-no-clue-title' ),
3516
+ sprintf(
3517
+ $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' ),
3518
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
3519
+ )
3520
+ ),
3521
+ sprintf(
3522
+ '<b>%s</b> - %s',
3523
+ $sysadmin_title,
3524
+ sprintf(
3525
+ $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' ),
3526
+ // We use a filter since the plugin might require additional API connectivity.
3527
+ '<b>' . implode( ', ', $this->apply_filters( 'api_domains', array(
3528
+ 'api.freemius.com',
3529
+ 'wp.freemius.com'
3530
+ ) ) ) . '</b>',
3531
+ $this->_module_type
3532
+ )
3533
+ ),
3534
+ sprintf(
3535
+ '<a href="%s"><b>%s</b></a> - %s',
3536
+ 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 ),
3537
+ $deactivate_plugin_title,
3538
+ $deactivate_plugin_desc
3539
+ )
3540
+ )
3541
+ );
3542
+ break;
3543
  // default:
3544
  // $message = $this->get_text_inline( 'connectivity-test-fails-message' );
3545
  // break;
3546
+ }
3547
+ }
3548
+
3549
+ $message_id = 'failed_connect_api';
3550
+ $type = 'error';
3551
+
3552
+ $connectivity_test_fails_message = $this->esc_html_inline( 'From unknown reason, the API connectivity test failed.', 'connectivity-test-fails-message' );
3553
+
3554
+ if ( false === $message ) {
3555
+ if ( $is_first_failure ) {
3556
+ // First attempt failed.
3557
+ $message = sprintf(
3558
+ $x_requires_access_to_api . ' ' .
3559
+ $connectivity_test_fails_message . ' ' .
3560
+ $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>' .
3561
+ '%s',
3562
+ '<b>' . $this->get_plugin_name() . '</b>',
3563
+ sprintf(
3564
+ '<div id="fs_firewall_issue_options">%s %s</div>',
3565
+ sprintf(
3566
+ '<a class="button button-primary fs-resolve" data-type="retry_ping" href="#">%s</a>',
3567
+ $this->get_text_inline( 'Yes - do your thing', 'yes-do-your-thing' )
3568
+ ),
3569
+ sprintf(
3570
+ '<a href="%s" class="button">%s</a>',
3571
+ 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 ),
3572
+ $this->get_text_inline( 'No - just deactivate', 'no-deactivate' )
3573
+ )
3574
+ )
3575
+ );
3576
+
3577
+ $message_id = 'failed_connect_api_first';
3578
+ $type = 'promotion';
3579
+ } else {
3580
+ // Second connectivity attempt failed.
3581
+ $message = sprintf(
3582
+ $x_requires_access_to_api . ' ' .
3583
+ $connectivity_test_fails_message . ' ' .
3584
+ $happy_to_resolve_issue_asap .
3585
+ ' %s',
3586
+ '<b>' . $this->get_plugin_name() . '</b>',
3587
+ sprintf(
3588
+ '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
3589
+ sprintf(
3590
+ '<a class="fs-resolve" data-type="general" href="#"><b>%s</b></a>%s',
3591
+ $fix_issue_title,
3592
+ ' - ' . sprintf(
3593
+ $fix_issue_desc,
3594
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
3595
+ )
3596
+ ),
3597
+ sprintf(
3598
+ '<a href="%s" target="_blank"><b>%s</b></a> - %s',
3599
+ sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),
3600
+ $install_previous_title,
3601
+ $install_previous_desc
3602
+ ),
3603
+ sprintf(
3604
+ '<a href="%s"><b>%s</b></a> - %s',
3605
+ 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 ),
3606
+ $deactivate_plugin_title,
3607
+ $deactivate_plugin_desc
3608
+ )
3609
+ )
3610
+ );
3611
+ }
3612
+ }
3613
+
3614
+ $this->_admin_notices->add_sticky(
3615
+ $message,
3616
+ $message_id,
3617
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
3618
+ $type
3619
+ );
3620
+ }
3621
+
3622
+ /**
3623
+ * Handle user request to resolve connectivity issue.
3624
+ * This method will send an email to Freemius API technical staff for resolution.
3625
+ * The email will contain server's info and installed plugins (might be caching issue).
3626
+ *
3627
+ * @author Vova Feldman (@svovaf)
3628
+ * @since 1.0.9
3629
+ */
3630
+ function _email_about_firewall_issue() {
3631
+ $this->_admin_notices->remove_sticky( 'failed_connect_api' );
3632
+
3633
+ $pong = $this->ping();
3634
+
3635
+ $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
3636
+
3637
+ if ( $is_connected ) {
3638
+ FS_GDPR_Manager::instance()->store_is_required( $pong->is_gdpr_required );
3639
+
3640
+ $this->store_connectivity_info( $pong, $is_connected );
3641
+
3642
+ echo $this->get_after_plugin_activation_redirect_url();
3643
+ exit;
3644
+ }
3645
+
3646
+ $current_user = self::_get_current_wp_user();
3647
+ $admin_email = $current_user->user_email;
3648
+
3649
+ $error_type = fs_request_get( 'error_type', 'general' );
3650
+
3651
+ switch ( $error_type ) {
3652
+ case 'squid':
3653
+ $title = 'Squid ACL Blocking Issue';
3654
+ break;
3655
+ case 'cloudflare':
3656
+ $title = 'CloudFlare Blocking Issue';
3657
+ break;
3658
+ default:
3659
+ $title = 'API Connectivity Issue';
3660
+ break;
3661
+ }
3662
+
3663
+ $custom_email_sections = array();
3664
+
3665
+ // Add 'API Error' custom email section.
3666
+ $custom_email_sections['api_error'] = array(
3667
+ 'title' => 'API Error',
3668
+ 'rows' => array(
3669
+ 'ping' => array(
3670
+ 'API Error',
3671
+ is_string( $pong ) ? htmlentities( $pong ) : json_encode( $pong )
3672
+ ),
3673
+ )
3674
+ );
3675
+
3676
+ // Send email with technical details to resolve API connectivity issues.
3677
+ $this->send_email(
3678
+ 'api@freemius.com', // recipient
3679
+ $title . ' [' . $this->get_plugin_name() . ']', // subject
3680
+ $custom_email_sections,
3681
+ array( "Reply-To: $admin_email <$admin_email>" ) // headers
3682
+ );
3683
+
3684
+ $this->_admin_notices->add_sticky(
3685
+ sprintf(
3686
+ $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' ),
3687
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
3688
+ ),
3689
+ 'server_details_sent'
3690
+ );
3691
+
3692
+ // Action was taken, tell that API connectivity troubleshooting should be off now.
3693
+
3694
+ echo "1";
3695
+ exit;
3696
+ }
3697
+
3698
+ /**
3699
+ * Handle connectivity test retry approved by the user.
3700
+ *
3701
+ * @author Vova Feldman (@svovaf)
3702
+ * @since 1.1.7.4
3703
+ */
3704
+ function _retry_connectivity_test() {
3705
+ $this->_admin_notices->remove_sticky( 'failed_connect_api_first' );
3706
+
3707
+ $pong = $this->ping();
3708
+
3709
+ $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
3710
+
3711
+ if ( $is_connected ) {
3712
+ FS_GDPR_Manager::instance()->store_is_required( $pong->is_gdpr_required );
3713
+
3714
+ $this->store_connectivity_info( $pong, $is_connected );
3715
+
3716
+ echo $this->get_after_plugin_activation_redirect_url();
3717
+ } else {
3718
+ // Add connectivity issue message after 2nd failed attempt.
3719
+ $this->_add_connectivity_issue_message( $pong, false );
3720
+
3721
+ echo "1";
3722
+ }
3723
+
3724
+ exit;
3725
+ }
3726
+
3727
+ static function _add_firewall_issues_javascript() {
3728
+ $params = array();
3729
+ fs_require_once_template( 'firewall-issues-js.php', $params );
3730
+ }
3731
+
3732
+ #endregion
3733
+
3734
+ #----------------------------------------------------------------------------------
3735
+ #region Email
3736
+ #----------------------------------------------------------------------------------
3737
+
3738
+ /**
3739
+ * Generates and sends an HTML email with customizable sections.
3740
+ *
3741
+ * @author Leo Fajardo (@leorw)
3742
+ * @since 1.1.2
3743
+ *
3744
+ * @param string $to_address
3745
+ * @param string $subject
3746
+ * @param array $sections
3747
+ * @param array $headers
3748
+ *
3749
+ * @return bool Whether the email contents were sent successfully.
3750
+ */
3751
+ private function send_email(
3752
+ $to_address,
3753
+ $subject,
3754
+ $sections = array(),
3755
+ $headers = array()
3756
+ ) {
3757
+ $default_sections = $this->get_email_sections();
3758
+
3759
+ // Insert new sections or replace the default email sections.
3760
+ if ( is_array( $sections ) && ! empty( $sections ) ) {
3761
+ foreach ( $sections as $section_id => $custom_section ) {
3762
+ if ( ! isset( $default_sections[ $section_id ] ) ) {
3763
+ // If the section does not exist, add it.
3764
+ $default_sections[ $section_id ] = $custom_section;
3765
+ } else {
3766
+ // If the section already exists, override it.
3767
+ $current_section = $default_sections[ $section_id ];
3768
+
3769
+ // Replace the current section's title if a custom section title exists.
3770
+ if ( isset( $custom_section['title'] ) ) {
3771
+ $current_section['title'] = $custom_section['title'];
3772
+ }
3773
+
3774
+ // Insert new rows under the current section or replace the default rows.
3775
+ if ( isset( $custom_section['rows'] ) && is_array( $custom_section['rows'] ) && ! empty( $custom_section['rows'] ) ) {
3776
+ foreach ( $custom_section['rows'] as $row_id => $row ) {
3777
+ $current_section['rows'][ $row_id ] = $row;
3778
+ }
3779
+ }
3780
+
3781
+ $default_sections[ $section_id ] = $current_section;
3782
+ }
3783
+ }
3784
+ }
3785
+
3786
+ $vars = array( 'sections' => $default_sections );
3787
+ $message = fs_get_template( 'email.php', $vars );
3788
+
3789
+ // Set the type of email to HTML.
3790
+ $headers[] = 'Content-type: text/html; charset=UTF-8';
3791
+
3792
+ $header_string = implode( "\r\n", $headers );
3793
+
3794
+ return wp_mail(
3795
+ $to_address,
3796
+ $subject,
3797
+ $message,
3798
+ $header_string
3799
+ );
3800
+ }
3801
+
3802
+ /**
3803
+ * Generates the data for the sections of the email content.
3804
+ *
3805
+ * @author Leo Fajardo (@leorw)
3806
+ * @since 1.1.2
3807
+ *
3808
+ * @return array
3809
+ */
3810
+ private function get_email_sections() {
3811
+ // Retrieve the current user's information so that we can get the user's email, first name, and last name below.
3812
+ $current_user = self::_get_current_wp_user();
3813
+
3814
+ // Retrieve the cURL version information so that we can get the version number below.
3815
+ $curl_version_information = curl_version();
3816
+
3817
+ $active_plugin = self::get_active_plugins();
3818
+
3819
+ // Generate the list of active plugins separated by new line.
3820
+ $active_plugin_string = '';
3821
+ foreach ( $active_plugin as $plugin ) {
3822
+ $active_plugin_string .= sprintf(
3823
+ '<a href="%s">%s</a> [v%s]<br>',
3824
+ $plugin['PluginURI'],
3825
+ $plugin['Name'],
3826
+ $plugin['Version']
3827
+ );
3828
+ }
3829
+
3830
+ $server_ip = WP_FS__REMOTE_ADDR;
3831
+
3832
+ // Add PHP info for deeper investigation.
3833
+ ob_start();
3834
+ phpinfo();
3835
+ $php_info = ob_get_clean();
3836
+
3837
+ $api_domain = substr( FS_API__ADDRESS, strpos( FS_API__ADDRESS, ':' ) + 3 );
3838
+
3839
+ // Generate the default email sections.
3840
+ $sections = array(
3841
+ 'sdk' => array(
3842
+ 'title' => 'SDK',
3843
+ 'rows' => array(
3844
+ 'fs_version' => array( 'FS Version', $this->version ),
3845
+ 'curl_version' => array( 'cURL Version', $curl_version_information['version'] )
3846
+ )
3847
+ ),
3848
+ 'plugin' => array(
3849
+ 'title' => ucfirst( $this->get_module_type() ),
3850
+ 'rows' => array(
3851
+ 'name' => array( 'Name', $this->get_plugin_name() ),
3852
+ 'version' => array( 'Version', $this->get_plugin_version() )
3853
+ )
3854
+ ),
3855
+ 'api' => array(
3856
+ 'title' => 'API Subdomain',
3857
+ 'rows' => array(
3858
+ 'dns' => array(
3859
+ 'DNS_CNAME',
3860
+ function_exists( 'dns_get_record' ) ?
3861
+ var_export( dns_get_record( $api_domain, DNS_CNAME ), true ) :
3862
+ 'dns_get_record() disabled/blocked'
3863
+ ),
3864
+ 'ip' => array(
3865
+ 'IP',
3866
+ function_exists( 'gethostbyname' ) ?
3867
+ gethostbyname( $api_domain ) :
3868
+ 'gethostbyname() disabled/blocked'
3869
+ ),
3870
+ ),
3871
+ ),
3872
+ 'site' => array(
3873
+ 'title' => 'Site',
3874
+ 'rows' => array(
3875
+ 'unique_id' => array( 'Unique ID', $this->get_anonymous_id() ),
3876
+ 'address' => array( 'Address', site_url() ),
3877
+ 'host' => array(
3878
+ 'HTTP_HOST',
3879
+ ( ! empty( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : '' )
3880
+ ),
3881
+ 'hosting' => array(
3882
+ 'Hosting Company' => fs_request_has( 'hosting_company' ) ?
3883
+ fs_request_get( 'hosting_company' ) :
3884
+ 'Unknown',
3885
+ ),
3886
+ 'server_addr' => array(
3887
+ 'SERVER_ADDR',
3888
+ '<a href="http://www.projecthoneypot.org/ip_' . $server_ip . '">' . $server_ip . '</a>'
3889
+ )
3890
+ )
3891
+ ),
3892
+ 'user' => array(
3893
+ 'title' => 'User',
3894
+ 'rows' => array(
3895
+ 'email' => array( 'Email', $current_user->user_email ),
3896
+ 'first' => array( 'First', $current_user->user_firstname ),
3897
+ 'last' => array( 'Last', $current_user->user_lastname )
3898
+ )
3899
+ ),
3900
+ 'plugins' => array(
3901
+ 'title' => 'Plugins',
3902
+ 'rows' => array(
3903
+ 'active_plugins' => array( 'Active Plugins', $active_plugin_string )
3904
+ )
3905
+ ),
3906
+ 'php_info' => array(
3907
+ 'title' => 'PHP Info',
3908
+ 'rows' => array(
3909
+ 'info' => array( $php_info )
3910
+ ),
3911
+ )
3912
+ );
3913
+
3914
+ // Allow the sections to be modified by other code.
3915
+ $sections = $this->apply_filters( 'email_template_sections', $sections );
3916
+
3917
+ return $sections;
3918
+ }
3919
+
3920
+ #endregion
3921
+
3922
+ #----------------------------------------------------------------------------------
3923
+ #region Initialization
3924
+ #----------------------------------------------------------------------------------
3925
+
3926
+ /**
3927
+ * Init plugin's Freemius instance.
3928
+ *
3929
+ * @author Vova Feldman (@svovaf)
3930
+ * @since 1.0.1
3931
+ *
3932
+ * @param number $id
3933
+ * @param string $public_key
3934
+ * @param bool $is_live
3935
+ * @param bool $is_premium
3936
+ */
3937
+ function init( $id, $public_key, $is_live = true, $is_premium = true ) {
3938
+ $this->_logger->entrance();
3939
+
3940
+ $this->dynamic_init( array(
3941
+ 'id' => $id,
3942
+ 'public_key' => $public_key,
3943
+ 'is_live' => $is_live,
3944
+ 'is_premium' => $is_premium,
3945
+ ) );
3946
+ }
3947
+
3948
+ /**
3949
+ * Dynamic initiator, originally created to support initiation
3950
+ * with parent_id for add-ons.
3951
+ *
3952
+ * @author Vova Feldman (@svovaf)
3953
+ * @since 1.0.6
3954
+ *
3955
+ * @param array $plugin_info
3956
+ *
3957
+ * @throws Freemius_Exception
3958
+ */
3959
+ function dynamic_init( array $plugin_info ) {
3960
+ $this->_logger->entrance();
3961
+
3962
+ $this->parse_settings( $plugin_info );
3963
+
3964
+ if ( ! self::is_ajax() ) {
3965
+ if ( ! $this->is_addon() || $this->is_only_premium() ) {
3966
+ add_action(
3967
+ ( $this->_is_network_active && fs_is_network_admin() ? 'network_' : '' ) . 'admin_menu',
3968
+ array( &$this, '_prepare_admin_menu' ),
3969
+ WP_FS__LOWEST_PRIORITY
3970
+ );
3971
+ }
3972
+ }
3973
+
3974
+ if ( $this->should_stop_execution() ) {
3975
+ return;
3976
+ }
3977
+
3978
+ if ( ! $this->is_registered() ) {
3979
+ if ( $this->is_anonymous() ) {
3980
+ // If user skipped, no need to test connectivity.
3981
+ $this->_has_api_connection = true;
3982
+ $this->_is_on = true;
3983
+ } else {
3984
+ if ( ! $this->has_api_connectivity() ) {
3985
+ if ( $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) ||
3986
+ $this->_admin_notices->has_sticky( 'failed_connect_api' )
3987
+ ) {
3988
+ if ( ! $this->_enable_anonymous || $this->is_premium() ) {
3989
+ // If anonymous mode is disabled, add firewall admin-notice message.
3990
+ add_action( 'admin_footer', array( 'Freemius', '_add_firewall_issues_javascript' ) );
3991
+
3992
+ $ajax_action_suffix = $this->_slug . ( $this->is_theme() ? ':theme' : '' );
3993
+ add_action( "wp_ajax_fs_resolve_firewall_issues_{$ajax_action_suffix}", array(
3994
+ &$this,
3995
+ '_email_about_firewall_issue'
3996
+ ) );
3997
+
3998
+ add_action( "wp_ajax_fs_retry_connectivity_test_{$ajax_action_suffix}", array(
3999
+ &$this,
4000
+ '_retry_connectivity_test'
4001
+ ) );
4002
+
4003
+ /**
4004
+ * 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.
4005
+ *
4006
+ * @author Vova Feldman (@svovaf)
4007
+ * @since 2.0.0
4008
+ */
4009
+ /*$this->add_ajax_action( 'resolve_firewall_issues', array(
4010
+ &$this,
4011
+ '_email_about_firewall_issue'
4012
+ ) );
4013
+
4014
+ $this->add_ajax_action( 'retry_connectivity_test', array(
4015
+ &$this,
4016
+ '_retry_connectivity_test'
4017
+ ) );*/
4018
+ }
4019
+ }
4020
+
4021
+ return;
4022
+ } else {
4023
+ $this->_admin_notices->remove_sticky( array(
4024
+ 'failed_connect_api_first',
4025
+ 'failed_connect_api',
4026
+ ) );
4027
+
4028
+ if ( $this->_anonymous_mode ) {
4029
+ // Simulate anonymous mode.
4030
+ $this->_is_anonymous = true;
4031
+ }
4032
+ }
4033
+ }
4034
+ }
4035
+
4036
+ /**
4037
+ * This should be executed even if Freemius is off for the core module,
4038
+ * otherwise, the add-ons dialogbox won't work properly. This is esepcially
4039
+ * relevant when the developer decided to turn FS off for existing users.
4040
+ *
4041
+ * @author Vova Feldman (@svovaf)
4042
+ */
4043
+ if ( $this->is_user_in_admin() &&
4044
+ ! $this->is_addon() &&
4045
+ $this->has_addons() &&
4046
+ 'plugin-information' === fs_request_get( 'tab', false ) &&
4047
+ $this->get_id() == fs_request_get( 'parent_plugin_id', false )
4048
+ ) {
4049
+ require_once WP_FS__DIR_INCLUDES . '/fs-plugin-info-dialog.php';
4050
+
4051
+ new FS_Plugin_Info_Dialog( $this );
4052
+ }
4053
+
4054
+ // Check if Freemius is on for the current plugin.
4055
+ // This MUST be executed after all the plugin variables has been loaded.
4056
+ if ( ! $this->is_registered() && ! $this->is_on() ) {
4057
+ return;
4058
+ }
4059
+
4060
+ if ( $this->has_api_connectivity() ) {
4061
+ if ( self::is_cron() ) {
4062
+ $this->hook_callback_to_sync_cron();
4063
+ } else if ( $this->is_user_in_admin() ) {
4064
+ /**
4065
+ * Schedule daily data sync cron if:
4066
+ *
4067
+ * 1. User opted-in (for tracking).
4068
+ * 2. If skipped, but later upgraded (opted-in via upgrade).
4069
+ *
4070
+ * @author Vova Feldman (@svovaf)
4071
+ * @since 1.1.7.3
4072
+ *
4073
+ */
4074
+ if ( $this->is_registered() ) {
4075
+ if ( ! $this->is_sync_cron_on() && $this->is_tracking_allowed() ) {
4076
+ $this->schedule_sync_cron();
4077
+ }
4078
+ }
4079
+
4080
+ /**
4081
+ * Check if requested for manual blocking background sync.
4082
+ */
4083
+ if ( fs_request_has( 'background_sync' ) ) {
4084
+ $this->run_manual_sync();
4085
+ }
4086
+ }
4087
+ }
4088
+
4089
+ if ( $this->is_registered() ) {
4090
+ $this->hook_callback_to_install_sync();
4091
+ }
4092
+
4093
+ if ( $this->is_addon() ) {
4094
+ if ( $this->is_parent_plugin_installed() ) {
4095
+ // Link to parent FS.
4096
+ $this->_parent = self::get_instance_by_id( $this->_plugin->parent_plugin_id );
4097
+
4098
+ // Get parent plugin reference.
4099
+ $this->_parent_plugin = $this->_parent->get_plugin();
4100
+ }
4101
+ }
4102
+
4103
+ if ( $this->is_user_in_admin() ) {
4104
+ if ( $this->is_addon() ) {
4105
+ if ( ! $this->is_parent_plugin_installed() ) {
4106
+ $parent_name = $this->get_option( $plugin_info, 'parent_name', null );
4107
+
4108
+ if ( isset( $plugin_info['parent'] ) ) {
4109
+ $parent_name = $this->get_option( $plugin_info['parent'], 'name', null );
4110
+ }
4111
+
4112
+ $this->_admin_notices->add(
4113
+ ( ! empty( $parent_name ) ?
4114
+ 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 ) :
4115
+ 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() )
4116
+ ),
4117
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
4118
+ 'error'
4119
+ );
4120
+
4121
+ return;
4122
+ } else {
4123
+ if ( $this->_parent->is_registered() && ! $this->is_registered() ) {
4124
+ // If parent plugin activated, automatically install add-on for the user.
4125
+ $this->_activate_addon_account( $this->_parent );
4126
+ } else if ( ! $this->_parent->is_registered() && $this->is_registered() ) {
4127
+ // If add-on activated and parent not, automatically install parent for the user.
4128
+ $this->activate_parent_account( $this->_parent );
4129
+ }
4130
+
4131
+ // @todo This should be only executed on activation. It should be migrated to register_activation_hook() together with other activation related logic.
4132
+ if ( $this->is_premium() ) {
4133
+ // Remove add-on download admin-notice.
4134
+ $this->_parent->_admin_notices->remove_sticky( array(
4135
+ 'addon_plan_upgraded_' . $this->_slug,
4136
+ 'no_addon_license_' . $this->_slug,
4137
+ ) );
4138
+ }
4139
+
4140
+ // $this->deactivate_premium_only_addon_without_license();
4141
+ }
4142
+ }
4143
+
4144
+ add_action( 'admin_init', array( &$this, '_admin_init_action' ) );
4145
+
4146
+ // if ( $this->is_registered() ||
4147
+ // $this->is_anonymous() ||
4148
+ // $this->is_pending_activation()
4149
+ // ) {
4150
+ // $this->_init_admin();
4151
+ // }
4152
+ }
4153
+
4154
+ /**
4155
+ * Should be called outside `$this->is_user_in_admin()` scope
4156
+ * because the updater has some logic that needs to be executed
4157
+ * during AJAX calls.
4158
+ *
4159
+ * Currently we need to hook to the `http_request_host_is_external` filter.
4160
+ * In the future, there might be additional logic added.
4161
+ *
4162
+ * @author Vova Feldman
4163
+ * @since 1.2.1.6
4164
+ */
4165
+ if ( $this->is_premium() && $this->has_release_on_freemius() ) {
4166
+ FS_Plugin_Updater::instance( $this );
4167
+ }
4168
+
4169
+ $this->do_action( 'initiated' );
4170
+
4171
+ if ( $this->_storage->prev_is_premium !== $this->_plugin->is_premium ) {
4172
+ if ( isset( $this->_storage->prev_is_premium ) ) {
4173
+ $this->apply_filters(
4174
+ 'after_code_type_change',
4175
+ // New code type.
4176
+ $this->_plugin->is_premium
4177
+ );
4178
+ } else {
4179
+ // Set for code type for the first time.
4180
+ $this->_storage->prev_is_premium = $this->_plugin->is_premium;
4181
+ }
4182
+ }
4183
+
4184
+ if ( ! $this->is_addon() ) {
4185
+ if ( $this->is_registered() ) {
4186
+ // Fix for upgrade from versions < 1.0.9.
4187
+ if ( ! isset( $this->_storage->activation_timestamp ) ) {
4188
+ $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME;
4189
+ }
4190
+
4191
+ $this->do_action( 'after_init_plugin_registered' );
4192
+ } else if ( $this->is_anonymous() ) {
4193
+ $this->do_action( 'after_init_plugin_anonymous' );
4194
+ } else if ( $this->is_pending_activation() ) {
4195
+ $this->do_action( 'after_init_plugin_pending_activations' );
4196
+ }
4197
+ } else {
4198
+ if ( $this->is_registered() ) {
4199
+ $this->do_action( 'after_init_addon_registered' );
4200
+ } else if ( $this->is_anonymous() ) {
4201
+ $this->do_action( 'after_init_addon_anonymous' );
4202
+ } else if ( $this->is_pending_activation() ) {
4203
+ $this->do_action( 'after_init_addon_pending_activations' );
4204
+ }
4205
+ }
4206
+ }
4207
+
4208
+ /**
4209
+ * @author Leo Fajardo (@leorw)
4210
+ *
4211
+ * @since 1.2.1.5
4212
+ */
4213
+ function _stop_tracking_callback() {
4214
+ $this->_logger->entrance();
4215
+
4216
+ $this->check_ajax_referer( 'stop_tracking' );
4217
+
4218
+ $result = $this->stop_tracking( fs_is_network_admin() );
4219
+
4220
+ if ( true === $result ) {
4221
+ self::shoot_ajax_success();
4222
+ }
4223
+
4224
+ $this->_logger->api_error( $result );
4225
+
4226
+ self::shoot_ajax_failure(
4227
+ sprintf( $this->get_text_inline( 'Unexpected API error. Please contact the %s\'s author with the following error.', 'unexpected-api-error' ), $this->_module_type ) .
4228
+ ( $this->is_api_error( $result ) && isset( $result->error ) ?
4229
+ $result->error->message :
4230
+ var_export( $result, true ) )
4231
+ );
4232
+ }
4233
+
4234
+ /**
4235
+ * @author Leo Fajardo (@leorw)
4236
+ * @since 1.2.1.5
4237
+ */
4238
+ function _allow_tracking_callback() {
4239
+ $this->_logger->entrance();
4240
+
4241
+ $this->check_ajax_referer( 'allow_tracking' );
4242
+
4243
+ $result = $this->allow_tracking( fs_is_network_admin() );
4244
+
4245
+ if ( true === $result ) {
4246
+ self::shoot_ajax_success();
4247
+ }
4248
+
4249
+ $this->_logger->api_error( $result );
4250
+
4251
+ self::shoot_ajax_failure(
4252
+ sprintf( $this->get_text_inline( 'Unexpected API error. Please contact the %s\'s author with the following error.', 'unexpected-api-error' ), $this->_module_type ) .
4253
+ ( $this->is_api_error( $result ) && isset( $result->error ) ?
4254
+ $result->error->message :
4255
+ var_export( $result, true ) )
4256
+ );
4257
+ }
4258
+
4259
+ /**
4260
+ * Opt-out from usage tracking.
4261
+ *
4262
+ * Note: This will not delete the account information but will stop all tracking.
4263
+ *
4264
+ * Returns:
4265
+ * 1. FALSE - If the user never opted-in.
4266
+ * 2. TRUE - If successfully opted-out.
4267
+ * 3. object - API result on failure.
4268
+ *
4269
+ * @author Leo Fajardo (@leorw)
4270
+ * @since 1.2.1.5
4271
+ *
4272
+ * @return bool|object
4273
+ */
4274
+ function stop_site_tracking() {
4275
+ $this->_logger->entrance();
4276
+
4277
+ if ( ! $this->is_registered() ) {
4278
+ // User never opted-in.
4279
+ return false;
4280
+ }
4281
+
4282
+ if ( $this->is_tracking_prohibited() ) {
4283
+ // Already disconnected.
4284
+ return true;
4285
+ }
4286
+
4287
+ // Send update to FS.
4288
+ $result = $this->get_api_site_scope()->call( '/?fields=is_disconnected', 'put', array(
4289
+ 'is_disconnected' => true
4290
+ ) );
4291
+
4292
+ if ( ! $this->is_api_result_entity( $result ) ||
4293
+ ! isset( $result->is_disconnected ) ||
4294
+ ! $result->is_disconnected
4295
+ ) {
4296
+ $this->_logger->api_error( $result );
4297
+
4298
+ return $result;
4299
+ }
4300
+
4301
+ $this->_site->is_disconnected = $result->is_disconnected;
4302
+ $this->_store_site();
4303
+
4304
+ $this->clear_sync_cron();
4305
+
4306
+ // Successfully disconnected.
4307
+ return true;
4308
+ }
4309
+
4310
+ /**
4311
+ * Opt-out network from usage tracking.
4312
+ *
4313
+ * Note: This will not delete the account information but will stop all tracking.
4314
+ *
4315
+ * Returns:
4316
+ * 1. FALSE - If the user never opted-in.
4317
+ * 2. TRUE - If successfully opted-out.
4318
+ * 3. object - API result on failure.
4319
+ *
4320
+ * @author Leo Fajardo (@leorw)
4321
+ * @since 1.2.1.5
4322
+ *
4323
+ * @return bool|object
4324
+ */
4325
+ function stop_network_tracking() {
4326
+ $this->_logger->entrance();
4327
+
4328
+ if ( ! $this->is_registered() ) {
4329
+ // User never opted-in.
4330
+ return false;
4331
+ }
4332
+
4333
+ $install_id_2_blog_id = array();
4334
+ $installs_map = $this->get_blog_install_map();
4335
+
4336
+ $opt_out_all = true;
4337
+
4338
+ $params = array();
4339
+ foreach ( $installs_map as $blog_id => $install ) {
4340
+ if ( $install->is_tracking_prohibited() ) {
4341
+ // Already opted-out.
4342
+ continue;
4343
+ }
4344
+
4345
+ if ( $this->is_site_delegated_connection( $blog_id ) ) {
4346
+ // Opt-out only from non-delegated installs.
4347
+ $opt_out_all = false;
4348
+ continue;
4349
+ }
4350
+
4351
+ $params[] = array( 'id' => $install->id );
4352
+
4353
+ $install_id_2_blog_id[ $install->id ] = $blog_id;
4354
+ }
4355
+
4356
+ if ( empty( $install_id_2_blog_id ) ) {
4357
+ return true;
4358
+ }
4359
+
4360
+ $params[] = array( 'is_disconnected' => true );
4361
+
4362
+ // Send update to FS.
4363
+ $result = $this->get_current_or_network_user_api_scope()->call( "/plugins/{$this->_module_id}/installs.json", 'put', $params );
4364
+
4365
+ if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
4366
+ $this->_logger->api_error( $result );
4367
+
4368
+ return $result;
4369
+ }
4370
+
4371
+ foreach ( $result->installs as $r_install ) {
4372
+ $blog_id = $install_id_2_blog_id[ $r_install->id ];
4373
+ $install = $installs_map[ $blog_id ];
4374
+ $install->is_disconnected = $r_install->is_disconnected;
4375
+ $this->_store_site( true, $blog_id, $install );
4376
+ }
4377
+
4378
+ $this->clear_sync_cron( $opt_out_all );
4379
+
4380
+ // Successfully disconnected.
4381
+ return true;
4382
+ }
4383
+
4384
+ /**
4385
+ * Opt-out from usage tracking.
4386
+ *
4387
+ * Note: This will not delete the account information but will stop all tracking.
4388
+ *
4389
+ * Returns:
4390
+ * 1. FALSE - If the user never opted-in.
4391
+ * 2. TRUE - If successfully opted-out.
4392
+ * 3. object - API result on failure.
4393
+ *
4394
+ * @author Leo Fajardo (@leorw)
4395
+ * @since 1.2.1.5
4396
+ *
4397
+ * @param bool $is_network_action
4398
+ *
4399
+ * @return bool|object
4400
+ */
4401
+ function stop_tracking( $is_network_action = false ) {
4402
+ $this->_logger->entrance();
4403
+
4404
+ return $is_network_action ?
4405
+ $this->stop_network_tracking() :
4406
+ $this->stop_site_tracking();
4407
+ }
4408
+
4409
+ /**
4410
+ * Opt-in back into usage tracking.
4411
+ *
4412
+ * Note: This will only work if the user opted-in previously.
4413
+ *
4414
+ * Returns:
4415
+ * 1. FALSE - If the user never opted-in.
4416
+ * 2. TRUE - If successfully opted-in back to usage tracking.
4417
+ * 3. object - API result on failure.
4418
+ *
4419
+ * @author Leo Fajardo (@leorw)
4420
+ * @since 1.2.1.5
4421
+ *
4422
+ * @return bool|object
4423
+ */
4424
+ function allow_site_tracking() {
4425
+ $this->_logger->entrance();
4426
+
4427
+ if ( ! $this->is_registered() ) {
4428
+ // User never opted-in.
4429
+ return false;
4430
+ }
4431
+
4432
+ if ( $this->is_tracking_allowed() ) {
4433
+ // Tracking already allowed.
4434
+ return true;
4435
+ }
4436
+
4437
+ $result = $this->get_api_site_scope()->call( '/?is_disconnected', 'put', array(
4438
+ 'is_disconnected' => false
4439
+ ) );
4440
+
4441
+ if ( ! $this->is_api_result_entity( $result ) ||
4442
+ ! isset( $result->is_disconnected ) ||
4443
+ $result->is_disconnected
4444
+ ) {
4445
+ $this->_logger->api_error( $result );
4446
+
4447
+ return $result;
4448
+ }
4449
+
4450
+ $this->_site->is_disconnected = $result->is_disconnected;
4451
+ $this->_store_site();
4452
+
4453
+ $this->schedule_sync_cron();
4454
+
4455
+ // Successfully reconnected.
4456
+ return true;
4457
+ }
4458
+
4459
+ /**
4460
+ * Opt-in network back into usage tracking.
4461
+ *
4462
+ * Note: This will only work if the user opted-in previously.
4463
+ *
4464
+ * Returns:
4465
+ * 1. FALSE - If the user never opted-in.
4466
+ * 2. TRUE - If successfully opted-in back to usage tracking.
4467
+ * 3. object - API result on failure.
4468
+ *
4469
+ * @author Leo Fajardo (@leorw)
4470
+ * @since 1.2.1.5
4471
+ *
4472
+ * @return bool|object
4473
+ */
4474
+ function allow_network_tracking() {
4475
+ $this->_logger->entrance();
4476
+
4477
+ if ( ! $this->is_registered() ) {
4478
+ // User never opted-in.
4479
+ return false;
4480
+ }
4481
+
4482
+ $install_id_2_blog_id = array();
4483
+ $installs_map = $this->get_blog_install_map();
4484
+
4485
+ $params = array();
4486
+ foreach ( $installs_map as $blog_id => $install ) {
4487
+ if ( $install->is_tracking_allowed() ) {
4488
+ continue;
4489
+ }
4490
+
4491
+ $params[] = array( 'id' => $install->id );
4492
+
4493
+ $install_id_2_blog_id[ $install->id ] = $blog_id;
4494
+ }
4495
+
4496
+ if ( empty( $install_id_2_blog_id ) ) {
4497
+ return true;
4498
+ }
4499
+
4500
+ $params[] = array( 'is_disconnected' => false );
4501
+
4502
+ // Send update to FS.
4503
+ $result = $this->get_current_or_network_user_api_scope()->call( "/plugins/{$this->_module_id}/installs.json", 'put', $params );
4504
+
4505
+
4506
+ if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
4507
+ $this->_logger->api_error( $result );
4508
+
4509
+ return $result;
4510
+ }
4511
+
4512
+ foreach ( $result->installs as $r_install ) {
4513
+ $blog_id = $install_id_2_blog_id[ $r_install->id ];
4514
+ $install = $installs_map[ $blog_id ];
4515
+ $install->is_disconnected = $r_install->is_disconnected;
4516
+ $this->_store_site( true, $blog_id, $install );
4517
+ }
4518
+
4519
+ $this->schedule_sync_cron();
4520
+
4521
+ // Successfully reconnected.
4522
+ return true;
4523
+ }
4524
+
4525
+ /**
4526
+ * Opt-in back into usage tracking.
4527
+ *
4528
+ * Note: This will only work if the user opted-in previously.
4529
+ *
4530
+ * Returns:
4531
+ * 1. FALSE - If the user never opted-in.
4532
+ * 2. TRUE - If successfully opted-in back to usage tracking.
4533
+ * 3. object - API result on failure.
4534
+ *
4535
+ * @author Leo Fajardo (@leorw)
4536
+ * @since 1.2.1.5
4537
+ *
4538
+ * @param bool $is_network_action
4539
+ *
4540
+ * @return bool|object
4541
+ */
4542
+ function allow_tracking( $is_network_action = false ) {
4543
+ $this->_logger->entrance();
4544
+
4545
+ return $is_network_action ?
4546
+ $this->allow_network_tracking() :
4547
+ $this->allow_site_tracking();
4548
+ }
4549
+
4550
+ /**
4551
+ * If user opted-in and later disabled usage-tracking,
4552
+ * re-allow tracking for licensing and updates.
4553
+ *
4554
+ * @author Leo Fajardo (@leorw)
4555
+ * @since 1.2.1.5
4556
+ *
4557
+ * @param bool $is_context_single_site
4558
+ */
4559
+ private function reconnect_locally( $is_context_single_site = false ) {
4560
+ $this->_logger->entrance();
4561
+
4562
+ if ( ! $this->is_registered() ) {
4563
+ return;
4564
+ }
4565
+
4566
+ if ( ! fs_is_network_admin() || $is_context_single_site ) {
4567
+ if ( $this->is_tracking_prohibited() ) {
4568
+ $this->_site->is_disconnected = false;
4569
+ $this->_store_site();
4570
+ }
4571
+ } else {
4572
+ $installs_map = $this->get_blog_install_map();
4573
+ foreach ( $installs_map as $blog_id => $install ) {
4574
+ /**
4575
+ * @var FS_Site $install
4576
+ */
4577
+ if ( $install->is_tracking_prohibited() ) {
4578
+ $install->is_disconnected = false;
4579
+ $this->_store_site( true, $blog_id, $install );
4580
+ }
4581
+ }
4582
+ }
4583
+ }
4584
+
4585
+ /**
4586
+ * Parse plugin's settings (as defined by the plugin dev).
4587
+ *
4588
+ * @author Vova Feldman (@svovaf)
4589
+ * @since 1.1.7.3
4590
+ *
4591
+ * @param array $plugin_info
4592
+ *
4593
+ * @throws \Freemius_Exception
4594
+ */
4595
+ private function parse_settings( &$plugin_info ) {
4596
+ $this->_logger->entrance();
4597
+
4598
+ $id = $this->get_numeric_option( $plugin_info, 'id', false );
4599
+ $public_key = $this->get_option( $plugin_info, 'public_key', false );
4600
+ $secret_key = $this->get_option( $plugin_info, 'secret_key', null );
4601
+ $parent_id = $this->get_numeric_option( $plugin_info, 'parent_id', null );
4602
+ $parent_name = $this->get_option( $plugin_info, 'parent_name', null );
4603
+
4604
+ /**
4605
+ * @author Vova Feldman (@svovaf)
4606
+ * @since 1.1.9 Try to pull secret key from external config.
4607
+ */
4608
+ if ( is_null( $secret_key ) && defined( "WP_FS__{$this->_slug}_SECRET_KEY" ) ) {
4609
+ $secret_key = constant( "WP_FS__{$this->_slug}_SECRET_KEY" );
4610
+ }
4611
+
4612
+ if ( isset( $plugin_info['parent'] ) ) {
4613
+ $parent_id = $this->get_numeric_option( $plugin_info['parent'], 'id', null );
4614
+ // $parent_slug = $this->get_option( $plugin_info['parent'], 'slug', null );
4615
+ // $parent_public_key = $this->get_option( $plugin_info['parent'], 'public_key', null );
4616
+ // $parent_name = $this->get_option( $plugin_info['parent'], 'name', null );
4617
+ }
4618
+
4619
+ if ( false === $id ) {
4620
+ throw new Freemius_Exception( array(
4621
+ 'error' => array(
4622
+ 'type' => 'ParameterNotSet',
4623
+ 'message' => 'Plugin id parameter is not set.',
4624
+ 'code' => 'plugin_id_not_set',
4625
+ 'http' => 500,
4626
+ )
4627
+ ) );
4628
+ }
4629
+ if ( false === $public_key ) {
4630
+ throw new Freemius_Exception( array(
4631
+ 'error' => array(
4632
+ 'type' => 'ParameterNotSet',
4633
+ 'message' => 'Plugin public_key parameter is not set.',
4634
+ 'code' => 'plugin_public_key_not_set',
4635
+ 'http' => 500,
4636
+ )
4637
+ ) );
4638
+ }
4639
+
4640
+ $plugin = ( $this->_plugin instanceof FS_Plugin ) ?
4641
+ $this->_plugin :
4642
+ new FS_Plugin();
4643
+
4644
+ $plugin->update( array(
4645
+ 'id' => $id,
4646
+ 'type' => $this->get_option( $plugin_info, 'type', $this->_module_type ),
4647
+ 'public_key' => $public_key,
4648
+ 'slug' => $this->_slug,
4649
+ 'parent_plugin_id' => $parent_id,
4650
+ 'version' => $this->get_plugin_version(),
4651
+ 'title' => $this->get_plugin_name(),
4652
+ 'file' => $this->_plugin_basename,
4653
+ 'is_premium' => $this->get_bool_option( $plugin_info, 'is_premium', true ),
4654
+ 'is_live' => $this->get_bool_option( $plugin_info, 'is_live', true ),
4655
+ 'affiliate_moderation' => $this->get_option( $plugin_info, 'has_affiliation' ),
4656
+ ) );
4657
+
4658
+ if ( $plugin->is_updated() ) {
4659
+ // Update plugin details.
4660
+ $this->_plugin = FS_Plugin_Manager::instance( $this->_module_id )->store( $plugin );
4661
+ }
4662
+ // Set the secret key after storing the plugin, we don't want to store the key in the storage.
4663
+ $this->_plugin->secret_key = $secret_key;
4664
+
4665
+ if ( ! isset( $plugin_info['menu'] ) ) {
4666
+ $plugin_info['menu'] = array();
4667
+
4668
+ if ( ! empty( $this->_storage->sdk_last_version ) &&
4669
+ version_compare( $this->_storage->sdk_last_version, '1.1.2', '<=' )
4670
+ ) {
4671
+ // Backward compatibility to 1.1.2
4672
+ $plugin_info['menu']['slug'] = isset( $plugin_info['menu_slug'] ) ?
4673
+ $plugin_info['menu_slug'] :
4674
+ $this->_slug;
4675
+ }
4676
+ }
4677
+
4678
+ $this->_menu = FS_Admin_Menu_Manager::instance(
4679
+ $this->_module_id,
4680
+ $this->_module_type,
4681
+ $this->get_unique_affix()
4682
+ );
4683
+
4684
+ $this->_menu->init( $plugin_info['menu'], $this->is_addon() );
4685
+
4686
+ $this->_has_addons = $this->get_bool_option( $plugin_info, 'has_addons', false );
4687
+ $this->_has_paid_plans = $this->get_bool_option( $plugin_info, 'has_paid_plans', true );
4688
+ $this->_has_premium_version = $this->get_bool_option( $plugin_info, 'has_premium_version', $this->_has_paid_plans );
4689
+ $this->_ignore_pending_mode = $this->get_bool_option( $plugin_info, 'ignore_pending_mode', false );
4690
+ $this->_is_org_compliant = $this->get_bool_option( $plugin_info, 'is_org_compliant', true );
4691
+ $this->_is_premium_only = $this->get_bool_option( $plugin_info, 'is_premium_only', false );
4692
+ if ( $this->_is_premium_only ) {
4693
+ // If premium only plugin, disable anonymous mode.
4694
+ $this->_enable_anonymous = false;
4695
+ $this->_anonymous_mode = false;
4696
+ } else {
4697
+ $this->_enable_anonymous = $this->get_bool_option( $plugin_info, 'enable_anonymous', true );
4698
+ $this->_anonymous_mode = $this->get_bool_option( $plugin_info, 'anonymous_mode', false );
4699
+ }
4700
+ $this->_permissions = $this->get_option( $plugin_info, 'permissions', array() );
4701
+
4702
+ if ( ! empty( $plugin_info['trial'] ) ) {
4703
+ $this->_trial_days = $this->get_numeric_option(
4704
+ $plugin_info['trial'],
4705
+ 'days',
4706
+ // Default to 0 - trial without days specification.
4707
+ 0
4708
+ );
4709
+
4710
+ $this->_is_trial_require_payment = $this->get_bool_option( $plugin_info['trial'], 'is_require_payment', false );
4711
+ }
4712
+ }
4713
+
4714
+ /**
4715
+ * @param string[] $options
4716
+ * @param string $key
4717
+ * @param mixed $default
4718
+ *
4719
+ * @return bool
4720
+ */
4721
+ private function get_option( &$options, $key, $default = false ) {
4722
+ return ! empty( $options[ $key ] ) ? $options[ $key ] : $default;
4723
+ }
4724
+
4725
+ private function get_bool_option( &$options, $key, $default = false ) {
4726
+ return isset( $options[ $key ] ) && is_bool( $options[ $key ] ) ? $options[ $key ] : $default;
4727
+ }
4728
+
4729
+ private function get_numeric_option( &$options, $key, $default = false ) {
4730
+ return isset( $options[ $key ] ) && is_numeric( $options[ $key ] ) ? $options[ $key ] : $default;
4731
+ }
4732
+
4733
+ /**
4734
+ * Gate keeper.
4735
+ *
4736
+ * @author Vova Feldman (@svovaf)
4737
+ * @since 1.1.7.3
4738
+ *
4739
+ * @return bool
4740
+ */
4741
+ private function should_stop_execution() {
4742
+ if ( empty( $this->_storage->was_plugin_loaded ) ) {
4743
+ /**
4744
+ * Don't execute Freemius until plugin was fully loaded at least once,
4745
+ * to give the opportunity for the activation hook to run before pinging
4746
+ * the API for connectivity test. This logic is relevant for the
4747
+ * identification of new plugin install vs. plugin update.
4748
+ *
4749
+ * @author Vova Feldman (@svovaf)
4750
+ * @since 1.1.9
4751
+ */
4752
+ return true;
4753
+ }
4754
+
4755
+ if ( $this->is_activation_mode() ) {
4756
+ if ( ! is_admin() ) {
4757
+ /**
4758
+ * If in activation mode, don't execute Freemius outside of the
4759
+ * admin dashboard.
4760
+ *
4761
+ * @author Vova Feldman (@svovaf)
4762
+ * @since 1.1.7.3
4763
+ */
4764
+ return true;
4765
+ }
4766
+
4767
+ if ( ! WP_FS__IS_HTTP_REQUEST ) {
4768
+ /**
4769
+ * If in activation and executed without HTTP context (e.g. CLI, Cronjob),
4770
+ * then don't start Freemius.
4771
+ *
4772
+ * @author Vova Feldman (@svovaf)
4773
+ * @since 1.1.6.3
4774
+ *
4775
+ * @link https://wordpress.org/support/topic/errors-in-the-freemius-class-when-running-in-wordpress-in-cli
4776
+ */
4777
+ return true;
4778
+ }
4779
+
4780
+ if ( self::is_cron() ) {
4781
+ /**
4782
+ * If in activation mode, don't execute Freemius during wp crons
4783
+ * (wp crons have HTTP context - called as HTTP request).
4784
+ *
4785
+ * @author Vova Feldman (@svovaf)
4786
+ * @since 1.1.7.3
4787
+ */
4788
+ return true;
4789
+ }
4790
+
4791
+ if ( self::is_ajax() &&
4792
+ ! $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) &&
4793
+ ! $this->_admin_notices->has_sticky( 'failed_connect_api' )
4794
+ ) {
4795
+ /**
4796
+ * During activation, if running in AJAX mode, unless there's a sticky
4797
+ * connectivity issue notice, don't run Freemius.
4798
+ *
4799
+ * @author Vova Feldman (@svovaf)
4800
+ * @since 1.1.7.3
4801
+ */
4802
+ return true;
4803
+ }
4804
+ }
4805
+
4806
+ return false;
4807
+ }
4808
+
4809
+ /**
4810
+ * Triggered after code type has changed.
4811
+ *
4812
+ * @author Vova Feldman (@svovaf)
4813
+ * @since 1.1.9.1
4814
+ */
4815
+ function _after_code_type_change() {
4816
+ $this->_logger->entrance();
4817
+
4818
+ if ( $this->is_theme() ) {
4819
+ // Expire the cache of the previous tabs since the theme may
4820
+ // have setting updates after code type has changed.
4821
+ $this->_cache->expire( 'tabs' );
4822
+ $this->_cache->expire( 'tabs_stylesheets' );
4823
+ }
4824
+
4825
+ if ( $this->is_registered() ) {
4826
+ if ( ! $this->is_addon() ) {
4827
+ add_action(
4828
+ is_admin() ? 'admin_init' : 'init',
4829
+ array( &$this, '_plugin_code_type_changed' )
4830
+ );
4831
+ }
4832
+
4833
+ if ( $this->is_premium() ) {
4834
+ // Purge cached payments after switching to the premium version.
4835
+ // @todo This logic doesn't handle purging the cache for serviceware module upgrade.
4836
+ $this->get_api_user_scope()->purge_cache( "/plugins/{$this->_module_id}/payments.json?include_addons=true" );
4837
+ }
4838
+ }
4839
+ }
4840
+
4841
+ /**
4842
+ * Handles plugin's code type change (free <--> premium).
4843
+ *
4844
+ * @author Vova Feldman (@svovaf)
4845
+ * @since 1.0.9
4846
+ */
4847
+ function _plugin_code_type_changed() {
4848
+ $this->_logger->entrance();
4849
+
4850
+ if ( $this->is_premium() ) {
4851
+ $this->reconnect_locally();
4852
+
4853
+ // Activated premium code.
4854
+ $this->do_action( 'after_premium_version_activation' );
4855
+
4856
+ // Remove all sticky messages related to download of the premium version.
4857
+ $this->_admin_notices->remove_sticky( array(
4858
+ 'trial_started',
4859
+ 'plan_upgraded',
4860
+ 'plan_changed',
4861
+ 'license_activated',
4862
+ ) );
4863
+
4864
+ $notice = '';
4865
+ if ( ! $this->is_only_premium() ) {
4866
+ $notice = sprintf( $this->get_text_inline( 'Premium %s version was successfully activated.', 'premium-activated-message' ), $this->_module_type );
4867
+ }
4868
+
4869
+ $license_notice = $this->get_license_network_activation_notice();
4870
+ if ( ! empty( $license_notice ) ) {
4871
+ $notice .= ' ' . $license_notice;
4872
+ }
4873
+
4874
+ if ( ! empty( $notice ) ) {
4875
+ $this->_admin_notices->add_sticky(
4876
+ trim( $notice ),
4877
+ 'premium_activated',
4878
+ $this->get_text_x_inline( 'W00t',
4879
+ 'Used to express elation, enthusiasm, or triumph (especially in electronic communication).', 'woot' ) . '!'
4880
+ );
4881
+ }
4882
+ } else {
4883
+ // Remove sticky message related to premium code activation.
4884
+ $this->_admin_notices->remove_sticky( 'premium_activated' );
4885
+
4886
+ // Activated free code (after had the premium before).
4887
+ $this->do_action( 'after_free_version_reactivation' );
4888
+
4889
+ if ( $this->is_paying() && ! $this->is_premium() ) {
4890
+ $this->_admin_notices->add_sticky(
4891
+ sprintf(
4892
+ /* translators: %s: License type (e.g. you have a professional license) */
4893
+ $this->get_text_inline( 'You have a %s license.', 'you-have-x-license' ),
4894
+ $this->get_plan_title()
4895
+ ) . $this->get_complete_upgrade_instructions(),
4896
+ 'plan_upgraded',
4897
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
4898
+ );
4899
+ }
4900
+ }
4901
+
4902
+ // Schedule code type changes event.
4903
+ $this->schedule_install_sync();
4904
+
4905
+ /**
4906
+ * Unregister the uninstall hook for the other version of the plugin (with different code type) to avoid
4907
+ * triggering a fatal error when uninstalling that plugin. For example, after deactivating the "free" version
4908
+ * of a specific plugin, its uninstall hook should be unregistered after the "premium" version has been
4909
+ * activated. If we don't do that, a fatal error will occur when we try to uninstall the "free" version since
4910
+ * the main file of the "free" version will be loaded first before calling the hooked callback. Since the
4911
+ * free and premium versions are almost identical (same class or have same functions), a fatal error like
4912
+ * "Cannot redeclare class MyClass" or "Cannot redeclare my_function()" will occur.
4913
+ */
4914
+ $this->unregister_uninstall_hook();
4915
+
4916
+ $this->clear_module_main_file_cache();
4917
+
4918
+ // Update is_premium of latest version.
4919
+ $this->_storage->prev_is_premium = $this->_plugin->is_premium;
4920
+ }
4921
+
4922
+ #endregion
4923
+
4924
+ #----------------------------------------------------------------------------------
4925
+ #region Add-ons
4926
+ #----------------------------------------------------------------------------------
4927
+
4928
+ /**
4929
+ * Check if add-on installed and activated on site.
4930
+ *
4931
+ * @author Vova Feldman (@svovaf)
4932
+ * @since 1.0.6
4933
+ *
4934
+ * @param string|number $id_or_slug
4935
+ * @param bool|null $is_premium Since 1.2.1.7 can check for specified add-on version.
4936
+ *
4937
+ * @return bool
4938
+ */
4939
+ function is_addon_activated( $id_or_slug, $is_premium = null ) {
4940
+ $this->_logger->entrance();
4941
+
4942
+ $addon_id = self::get_module_id( $id_or_slug );
4943
+ $is_activated = self::has_instance( $addon_id );
4944
+
4945
+ if ( ! $is_activated ) {
4946
+ return false;
4947
+ }
4948
+
4949
+ if ( is_bool( $is_premium ) ) {
4950
+ // Check if the specified code version is activate.
4951
+ $addon = $this->get_addon_instance( $addon_id );
4952
+ $is_activated = ( $is_premium === $addon->is_premium() );
4953
+ }
4954
+
4955
+ return $is_activated;
4956
+ }
4957
+
4958
+ /**
4959
+ * Check if add-on was connected to install
4960
+ *
4961
+ * @author Vova Feldman (@svovaf)
4962
+ * @since 1.1.7
4963
+ *
4964
+ * @param string|number $id_or_slug
4965
+ *
4966
+ * @return bool
4967
+ */
4968
+ function is_addon_connected( $id_or_slug ) {
4969
+ $this->_logger->entrance();
4970
+
4971
+ $sites = self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN );
4972
+
4973
+ $addon_id = self::get_module_id( $id_or_slug );
4974
+ $addon = $this->get_addon( $addon_id );
4975
+ $slug = $addon->slug;
4976
+ if ( ! isset( $sites[ $slug ] ) ) {
4977
+ return false;
4978
+ }
4979
+
4980
+ $site = $sites[ $slug ];
4981
+
4982
+ $plugin = FS_Plugin_Manager::instance( $addon_id )->get();
4983
+
4984
+ if ( $plugin->parent_plugin_id != $this->_plugin->id ) {
4985
+ // The given slug do NOT belong to any of the plugin's add-ons.
4986
+ return false;
4987
+ }
4988
+
4989
+ return ( is_object( $site ) &&
4990
+ is_numeric( $site->id ) &&
4991
+ is_numeric( $site->user_id ) &&
4992
+ FS_Plugin_Plan::is_valid_id( $site->plan_id )
4993
+ );
4994
+ }
4995
+
4996
+ /**
4997
+ * Determines if add-on installed.
4998
+ *
4999
+ * NOTE: This is a heuristic and only works if the folder/file named as the slug.
5000
+ *
5001
+ * @author Vova Feldman (@svovaf)
5002
+ * @since 1.0.6
5003
+ *
5004
+ * @param string|number $id_or_slug
5005
+ *
5006
+ * @return bool
5007
+ */
5008
+ function is_addon_installed( $id_or_slug ) {
5009
+ $this->_logger->entrance();
5010
+
5011
+ $addon_id = self::get_module_id( $id_or_slug );
5012
+
5013
+ return file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $this->get_addon_basename( $addon_id ) ) );
5014
+ }
5015
+
5016
+ /**
5017
+ * Get add-on basename.
5018
+ *
5019
+ * @author Vova Feldman (@svovaf)
5020
+ * @since 1.0.6
5021
+ *
5022
+ * @param string|number $id_or_slug
5023
+ *
5024
+ * @return string
5025
+ */
5026
+ function get_addon_basename( $id_or_slug ) {
5027
+ $addon_id = self::get_module_id( $id_or_slug );
5028
+
5029
+ if ( $this->is_addon_activated( $addon_id ) ) {
5030
+ return self::instance( $addon_id )->get_plugin_basename();
5031
+ }
5032
+
5033
+ $addon = $this->get_addon( $addon_id );
5034
+ $premium_basename = "{$addon->slug}-premium/{$addon->slug}.php";
5035
+
5036
+ if ( file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $premium_basename ) ) ) {
5037
+ return $premium_basename;
5038
+ }
5039
+
5040
+ $all_plugins = $this->get_all_plugins();
5041
+
5042
+ foreach ( $all_plugins as $basename => &$data ) {
5043
+ if ( $addon->slug === $data['slug'] ||
5044
+ $addon->slug . '-premium' === $data['slug']
5045
+ ) {
5046
+ return $basename;
5047
+ }
5048
+ }
5049
+
5050
+ $free_basename = "{$addon->slug}/{$addon->slug}.php";
5051
+
5052
+ return $free_basename;
5053
+ }
5054
+
5055
+ /**
5056
+ * Get installed add-ons instances.
5057
+ *
5058
+ * @author Vova Feldman (@svovaf)
5059
+ * @since 1.0.6
5060
+ *
5061
+ * @return Freemius[]
5062
+ */
5063
+ function get_installed_addons() {
5064
+ $installed_addons = array();
5065
+ foreach ( self::$_instances as $instance ) {
5066
+ if ( $instance->is_addon() && is_object( $instance->_parent_plugin ) ) {
5067
+ if ( $this->_plugin->id == $instance->_parent_plugin->id ) {
5068
+ $installed_addons[] = $instance;
5069
+ }
5070
+ }
5071
+ }
5072
+
5073
+ return $installed_addons;
5074
+ }
5075
+
5076
+ /**
5077
+ * Check if any add-ons of the plugin are installed.
5078
+ *
5079
+ * @author Leo Fajardo (@leorw)
5080
+ * @since 1.1.1
5081
+ *
5082
+ * @return bool
5083
+ */
5084
+ function has_installed_addons() {
5085
+ if ( ! $this->has_addons() ) {
5086
+ return false;
5087
+ }
5088
+
5089
+ foreach ( self::$_instances as $instance ) {
5090
+ if ( $instance->is_addon() && is_object( $instance->_parent_plugin ) ) {
5091
+ if ( $this->_plugin->id == $instance->_parent_plugin->id ) {
5092
+ return true;
5093
+ }
5094
+ }
5095
+ }
5096
+
5097
+ return false;
5098
+ }
5099
+
5100
+ /**
5101
+ * Tell Freemius that the current plugin is an add-on.
5102
+ *
5103
+ * @author Vova Feldman (@svovaf)
5104
+ * @since 1.0.6
5105
+ *
5106
+ * @param number $parent_plugin_id The parent plugin ID
5107
+ */
5108
+ function init_addon( $parent_plugin_id ) {
5109
+ $this->_plugin->parent_plugin_id = $parent_plugin_id;
5110
+ }
5111
+
5112
+ /**
5113
+ * @author Vova Feldman (@svovaf)
5114
+ * @since 1.0.6
5115
+ *
5116
+ * @return bool
5117
+ */
5118
+ function is_addon() {
5119
+ return isset( $this->_plugin->parent_plugin_id ) && is_numeric( $this->_plugin->parent_plugin_id );
5120
+ }
5121
+
5122
+ /**
5123
+ * Deactivate add-on if it's premium only and the user does't have a valid license.
5124
+ *
5125
+ * @param bool $is_after_trial_cancel
5126
+ *
5127
+ * @return bool If add-on was deactivated.
5128
+ */
5129
+ private function deactivate_premium_only_addon_without_license( $is_after_trial_cancel = false ) {
5130
+ if ( ! $this->has_free_plan() &&
5131
+ ! $this->has_features_enabled_license() &&
5132
+ ! $this->_has_premium_license()
5133
+ ) {
5134
+ if ( $this->is_registered() ) {
5135
+ // IF wrapper is turned off because activation_timestamp is currently only stored for plugins (not addons).
5136
+ // if (empty($this->_storage->activation_timestamp) ||
5137
+ // (WP_FS__SCRIPT_START_TIME - $this->_storage->activation_timestamp) > 30
5138
+ // ) {
5139
+ /**
5140
+ * @todo When it's first fail, there's no reason to try and re-sync because the licenses were just synced after initial activation.
5141
+ *
5142
+ * Retry syncing the user add-on licenses.
5143
+ */
5144
+ // Sync licenses.
5145
+ $this->_sync_licenses();
5146
+ // }
5147
+
5148
+ // Try to activate premium license.
5149
+ $this->_activate_license( true );
5150
+ }
5151
+
5152
+ if ( ! $this->has_free_plan() &&
5153
+ ! $this->has_features_enabled_license() &&
5154
+ ! $this->_has_premium_license()
5155
+ ) {
5156
+ // @todo Check if deactivate plugins also call the deactivation hook.
5157
+
5158
+ $this->_parent->_admin_notices->add_sticky(
5159
+ sprintf(
5160
+ ( $is_after_trial_cancel ?
5161
+ $this->_parent->get_text_inline(
5162
+ '%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.',
5163
+ 'addon-trial-cancelled-message'
5164
+ ) :
5165
+ $this->_parent->get_text_inline(
5166
+ '%s is a premium only add-on. You have to purchase a license first before activating the plugin.',
5167
+ 'addon-no-license-message'
5168
+ )
5169
+ ),
5170
+ '<b>' . $this->_plugin->title . '</b>'
5171
+ ) . ' ' . sprintf(
5172
+ '<a href="%s" aria-label="%s" class="button button-primary" style="margin-left: 10px; vertical-align: middle;">%s &nbsp;&#10140;</a>',
5173
+ $this->_parent->addon_url( $this->_slug ),
5174
+ esc_attr( sprintf( $this->_parent->get_text_inline( 'More information about %s', 'more-information-about-x' ), $this->_plugin->title ) ),
5175
+ $this->_parent->get_text_inline( 'Purchase License', 'purchase-license' )
5176
+ ),
5177
+ 'no_addon_license_' . $this->_slug,
5178
+ ( $is_after_trial_cancel ? '' : $this->_parent->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...' ),
5179
+ ( $is_after_trial_cancel ? 'success' : 'error' )
5180
+ );
5181
+
5182
+ deactivate_plugins( array( $this->_plugin_basename ), true );
5183
+
5184
+ return true;
5185
+ }
5186
+ }
5187
+
5188
+ return false;
5189
+ }
5190
+
5191
+ #endregion
5192
+
5193
+ #----------------------------------------------------------------------------------
5194
+ #region Sandbox
5195
+ #----------------------------------------------------------------------------------
5196
+
5197
+ /**
5198
+ * Set Freemius into sandbox mode for debugging.
5199
+ *
5200
+ * @author Vova Feldman (@svovaf)
5201
+ * @since 1.0.4
5202
+ *
5203
+ * @param string $secret_key
5204
+ */
5205
+ function init_sandbox( $secret_key ) {
5206
+ $this->_plugin->secret_key = $secret_key;
5207
+
5208
+ // Update plugin details.
5209
+ FS_Plugin_Manager::instance( $this->_module_id )->update( $this->_plugin, true );
5210
+ }
5211
+
5212
+ /**
5213
+ * Check if running payments in sandbox mode.
5214
+ *
5215
+ * @author Vova Feldman (@svovaf)
5216
+ * @since 1.0.4
5217
+ *
5218
+ * @return bool
5219
+ */
5220
+ function is_payments_sandbox() {
5221
+ return ( ! $this->is_live() ) || isset( $this->_plugin->secret_key );
5222
+ }
5223
+
5224
+ #endregion
5225
+
5226
+ /**
5227
+ * Check if running test vs. live plugin.
5228
+ *
5229
+ * @author Vova Feldman (@svovaf)
5230
+ * @since 1.0.5
5231
+ *
5232
+ * @return bool
5233
+ */
5234
+ function is_live() {
5235
+ return $this->_plugin->is_live;
5236
+ }
5237
+
5238
+ /**
5239
+ * Check if super-admin skipped connection for all sites in the network.
5240
+ *
5241
+ * @author Vova Feldman (@svovaf)
5242
+ * @since 2.0.0
5243
+ */
5244
+ function is_network_anonymous() {
5245
+ if ( ! $this->_is_network_active ) {
5246
+ return false;
5247
+ }
5248
+
5249
+ $is_anonymous_ms = $this->_storage->get( 'is_anonymous_ms' );
5250
+
5251
+ if ( empty( $is_anonymous_ms ) ) {
5252
+ return false;
5253
+ }
5254
+
5255
+ return $is_anonymous_ms['is'];
5256
+ }
5257
+
5258
+ /**
5259
+ * Check if super-admin opted-in for all sites in the network.
5260
+ *
5261
+ * @author Vova Feldman (@svovaf)
5262
+ * @since 2.0.0
5263
+ */
5264
+ function is_network_connected() {
5265
+ if ( ! $this->_is_network_active ) {
5266
+ return false;
5267
+ }
5268
+
5269
+ return $this->_storage->get( 'is_network_connected' );
5270
+ }
5271
+
5272
+ /**
5273
+ * Check if the user skipped connecting the account with Freemius.
5274
+ *
5275
+ * @author Vova Feldman (@svovaf)
5276
+ * @since 1.0.7
5277
+ *
5278
+ * @return bool
5279
+ */
5280
+ function is_anonymous() {
5281
+ if ( ! isset( $this->_is_anonymous ) ) {
5282
+ if ( $this->is_network_anonymous() ) {
5283
+ $this->_is_anonymous = true;
5284
+ } else {
5285
+ if ( ! isset( $this->_storage->is_anonymous ) ) {
5286
+ // Not skipped.
5287
+ $this->_is_anonymous = false;
5288
+ } else if ( is_bool( $this->_storage->is_anonymous ) ) {
5289
+ // For back compatibility, since the variable was boolean before.
5290
+ $this->_is_anonymous = $this->_storage->is_anonymous;
5291
+
5292
+ // Upgrade stored data format to 1.1.3 format.
5293
+ $this->set_anonymous_mode( $this->_storage->is_anonymous );
5294
+ } else {
5295
+ // Version 1.1.3 and later.
5296
+ $this->_is_anonymous = $this->_storage->is_anonymous['is'];
5297
+ }
5298
+ }
5299
+ }
5300
+
5301
+ return $this->_is_anonymous;
5302
+ }
5303
+
5304
+ /**
5305
+ * Check if the user skipped the connection of a specified site.
5306
+ *
5307
+ * @author Vova Feldman (@svovaf)
5308
+ * @since 2.0.0
5309
+ *
5310
+ * @param int $blog_id
5311
+ *
5312
+ * @return bool
5313
+ */
5314
+ function is_anonymous_site( $blog_id = 0 ) {
5315
+ if ( $this->is_network_anonymous() ) {
5316
+ return true;
5317
+ }
5318
+
5319
+ $is_anonymous = $this->_storage->get( 'is_anonymous', false, $blog_id );
5320
+
5321
+ if ( empty( $is_anonymous ) ) {
5322
+ return false;
5323
+ }
5324
+
5325
+ return $is_anonymous['is'];
5326
+ }
5327
+
5328
+ /**
5329
+ * Check if user connected his account and install pending email activation.
5330
+ *
5331
+ * @author Vova Feldman (@svovaf)
5332
+ * @since 1.0.7
5333
+ *
5334
+ * @return bool
5335
+ */
5336
+ function is_pending_activation() {
5337
+ return $this->_storage->get( 'is_pending_activation', false );
5338
+ }
5339
+
5340
+ /**
5341
+ * Check if plugin must be WordPress.org compliant.
5342
+ *
5343
+ * @since 1.0.7
5344
+ *
5345
+ * @return bool
5346
+ */
5347
+ function is_org_repo_compliant() {
5348
+ return $this->_is_org_compliant;
5349
+ }
5350
+
5351
+ #--------------------------------------------------------------------------------
5352
+ #region WP Cron Common
5353
+ #--------------------------------------------------------------------------------
5354
+
5355
+ /**
5356
+ * @author Vova Feldman (@svovaf)
5357
+ * @since 2.0.0
5358
+ *
5359
+ * @param string $name Cron name.
5360
+ *
5361
+ * @return object
5362
+ */
5363
+ private function get_cron_data( $name ) {
5364
+ $this->_logger->entrance( $name );
5365
+
5366
+ /**
5367
+ * @var object $cron_data
5368
+ */
5369
+ return $this->_storage->get( "{$name}_cron", null );
5370
+ }
5371
+
5372
+ /**
5373
+ * @author Vova Feldman (@svovaf)
5374
+ * @since 2.0.0
5375
+ *
5376
+ * @param string $name Cron name.
5377
+ */
5378
+ private function clear_cron_data( $name ) {
5379
+ $this->_logger->entrance( $name );
5380
+
5381
+ $this->_storage->remove( "{$name}_cron" );
5382
+ }
5383
+
5384
+ /**
5385
+ * @author Vova Feldman (@svovaf)
5386
+ * @since 2.0.0
5387
+ *
5388
+ * @param string $name Cron name.
5389
+ * @param int $cron_blog_id The cron executing blog ID.
5390
+ */
5391
+ private function set_cron_data( $name, $cron_blog_id = 0 ) {
5392
+ $this->_logger->entrance( $name );
5393
+
5394
+ $this->_storage->store( "{$name}_cron", (object) array(
5395
+ 'version' => $this->get_plugin_version(),
5396
+ 'blog_id' => $cron_blog_id,
5397
+ 'sdk_version' => $this->version,
5398
+ 'timestamp' => WP_FS__SCRIPT_START_TIME,
5399
+ 'on' => true,
5400
+ ) );
5401
+ }
5402
+
5403
+ /**
5404
+ * Get the cron's executing blog ID.
5405
+ *
5406
+ * @author Vova Feldman (@svovaf)
5407
+ * @since 2.0.0
5408
+ *
5409
+ * @param string $name Cron name.
5410
+ *
5411
+ * @return int
5412
+ */
5413
+ private function get_cron_blog_id( $name ) {
5414
+ $this->_logger->entrance( $name );
5415
+
5416
+ /**
5417
+ * @var object $cron_data
5418
+ */
5419
+ $cron_data = $this->get_cron_data( $name );
5420
+
5421
+ return ( is_object( $cron_data ) && is_numeric( $cron_data->blog_id ) ) ?
5422
+ $cron_data->blog_id :
5423
+ 0;
5424
+ }
5425
+
5426
+ /**
5427
+ * @author Vova Feldman (@svovaf)
5428
+ * @since 2.0.0
5429
+ *
5430
+ * @param string $name Cron name.
5431
+ *
5432
+ * @return bool
5433
+ */
5434
+ private function is_cron_on( $name ) {
5435
+ $this->_logger->entrance( $name );
5436
+
5437
+ /**
5438
+ * @var object $cron_data
5439
+ */
5440
+ $cron_data = $this->get_cron_data( $name );
5441
+
5442
+ return ( ! is_null( $cron_data ) && true === $cron_data->on );
5443
+ }
5444
+
5445
+ /**
5446
+ * Unix timestamp for previous cron execution or false if never executed.
5447
+ *
5448
+ * @author Vova Feldman (@svovaf)
5449
+ * @since 2.0.0
5450
+ *
5451
+ * @param string $name Cron name.
5452
+ *
5453
+ * @return int|false
5454
+ */
5455
+ private function cron_last_execution( $name ) {
5456
+ $this->_logger->entrance( $name );
5457
+
5458
+ return $this->_storage->get( "{$name}_timestamp" );
5459
+ }
5460
+
5461
+ /**
5462
+ * Set cron execution time to now.
5463
+ *
5464
+ * @author Vova Feldman (@svovaf)
5465
+ * @since 2.0.0
5466
+ *
5467
+ * @param string $name Cron name.
5468
+ */
5469
+ private function set_cron_execution_timestamp( $name ) {
5470
+ $this->_logger->entrance( $name );
5471
+
5472
+ $this->_storage->store( "{$name}_timestamp", time() );
5473
+ }
5474
+
5475
+ /**
5476
+ * Check if cron was executed in the last $period of seconds.
5477
+ *
5478
+ * @author Vova Feldman (@svovaf)
5479
+ * @since 2.0.0
5480
+ *
5481
+ * @param string $name Cron name.
5482
+ * @param int $period In seconds
5483
+ *
5484
+ * @return bool
5485
+ */
5486
+ private function is_cron_executed( $name, $period = WP_FS__TIME_24_HOURS_IN_SEC ) {
5487
+ $this->_logger->entrance( $name );
5488
+
5489
+ $last_execution = $this->set_cron_execution_timestamp( $name );
5490
+
5491
+ if ( ! is_numeric( $last_execution ) ) {
5492
+ return false;
5493
+ }
5494
+
5495
+ return ( $last_execution > ( WP_FS__SCRIPT_START_TIME - $period ) );
5496
+ }
5497
+
5498
+ /**
5499
+ * WP Cron is executed on a site level. When running in a multisite network environment
5500
+ * with the network integration activated, for optimization reasons, we are consolidating
5501
+ * the installs data sync cron to be executed only from a single site.
5502
+ *
5503
+ * @author Vova Feldman (@svovaf)
5504
+ * @since 2.0.0
5505
+ *
5506
+ * @param int $except_blog_id Target any except the excluded blog ID.
5507
+ *
5508
+ * @return int
5509
+ */
5510
+ private function get_cron_target_blog_id( $except_blog_id = 0 ) {
5511
+ if ( ! is_multisite() ) {
5512
+ return 0;
5513
+ }
5514
+
5515
+ if ( $this->_is_network_active &&
5516
+ is_numeric( $this->_storage->network_install_blog_id ) &&
5517
+ $except_blog_id != $this->_storage->network_install_blog_id &&
5518
+ self::is_site_active( $this->_storage->network_install_blog_id )
5519
+ ) {
5520
+ // Try to run cron from the main network blog.
5521
+ $install = $this->get_install_by_blog_id( $this->_storage->network_install_blog_id );
5522
+
5523
+ if ( is_object( $install ) &&
5524
+ ( $this->is_premium() || $install->is_tracking_allowed() )
5525
+ ) {
5526
+ return $this->_storage->network_install_blog_id;
5527
+ }
5528
+ }
5529
+
5530
+ // Get first opted-in blog ID with active tracking.
5531
+ $installs = $this->get_blog_install_map();
5532
+ foreach ( $installs as $blog_id => $install ) {
5533
+ if ( $except_blog_id != $blog_id &&
5534
+ self::is_site_active( $blog_id ) &&
5535
+ ( $this->is_premium() || $install->is_tracking_allowed() )
5536
+ ) {
5537
+ return $blog_id;
5538
+ }
5539
+ }
5540
+
5541
+ return 0;
5542
+ }
5543
+
5544
+ /**
5545
+ * @author Vova Feldman (@svovaf)
5546
+ * @since 2.0.0
5547
+ *
5548
+ * @param string $name Cron name.
5549
+ * @param string $action_tag Callback action tag.
5550
+ * @param bool $is_network_clear If set to TRUE, clear sync cron even if there are installs that are still connected.
5551
+ */
5552
+ private function clear_cron( $name, $action_tag = '', $is_network_clear = false ) {
5553
+ $this->_logger->entrance( $name );
5554
+
5555
+ if ( ! $this->is_cron_on( $name ) ) {
5556
+ return;
5557
+ }
5558
+
5559
+ $clear_cron = true;
5560
+ if ( ! $is_network_clear && $this->_is_network_active ) {
5561
+ $installs = $this->get_blog_install_map();
5562
+
5563
+ foreach ( $installs as $blog_id => $install ) {
5564
+ /**
5565
+ * @var FS_Site $install
5566
+ */
5567
+ if ( $install->is_tracking_allowed() ) {
5568
+ $clear_cron = false;
5569
+ break;
5570
+ }
5571
+ }
5572
+ }
5573
+
5574
+ if ( ! $clear_cron ) {
5575
+ return;
5576
+ }
5577
+
5578
+ /**
5579
+ * @var object $cron_data
5580
+ */
5581
+ $cron_data = $this->get_cron_data( $name );
5582
+
5583
+ $cron_blog_id = is_object( $cron_data ) && isset( $cron_data->blog_id ) ?
5584
+ $cron_data->blog_id :
5585
+ 0;
5586
+
5587
+ $this->clear_cron_data( $name );
5588
+
5589
+ if ( 0 < $cron_blog_id ) {
5590
+ switch_to_blog( $cron_blog_id );
5591
+ }
5592
+
5593
+ if ( empty( $action_tag ) ) {
5594
+ $action_tag = $name;
5595
+ }
5596
+
5597
+ wp_clear_scheduled_hook( $this->get_action_tag( $action_tag ) );
5598
+
5599
+ if ( 0 < $cron_blog_id ) {
5600
+ restore_current_blog();
5601
+ }
5602
+ }
5603
+
5604
+ /**
5605
+ * Unix timestamp for next cron execution or false if not scheduled.
5606
+ *
5607
+ * @author Vova Feldman (@svovaf)
5608
+ * @since 2.0.0
5609
+ *
5610
+ * @param string $name Cron name.
5611
+ * @param string $action_tag Callback action tag.
5612
+ *
5613
+ * @return int|false
5614
+ */
5615
+ private function get_next_scheduled_cron( $name, $action_tag = '' ) {
5616
+ $this->_logger->entrance( $name );
5617
+
5618
+ if ( ! $this->is_cron_on( $name ) ) {
5619
+ return false;
5620
+ }
5621
+
5622
+ /**
5623
+ * @var object $cron_data
5624
+ */
5625
+ $cron_data = $this->get_cron_data( $name );
5626
+
5627
+ $cron_blog_id = is_object( $cron_data ) && isset( $cron_data->blog_id ) ?
5628
+ $cron_data->blog_id :
5629
+ 0;
5630
+
5631
+ if ( 0 < $cron_blog_id ) {
5632
+ switch_to_blog( $cron_blog_id );
5633
+ }
5634
+
5635
+ if ( empty( $action_tag ) ) {
5636
+ $action_tag = $name;
5637
+ }
5638
+
5639
+ $next_scheduled = wp_next_scheduled( $this->get_action_tag( $action_tag ) );
5640
+
5641
+ if ( 0 < $cron_blog_id ) {
5642
+ restore_current_blog();
5643
+ }
5644
+
5645
+ return $next_scheduled;
5646
+ }
5647
+
5648
+ /**
5649
+ * @author Vova Feldman (@svovaf)
5650
+ * @since 2.0.0
5651
+ *
5652
+ * @param string $name Cron name.
5653
+ * @param string $action_tag Callback action tag.
5654
+ * @param string $recurrence 'single' or 'daily'.
5655
+ * @param int $start_at Defaults to now.
5656
+ * @param bool $randomize_start If true, schedule first job randomly during the next 12 hours. Otherwise, schedule job to start right away.
5657
+ * @param int $except_blog_id Target any except the excluded blog ID.
5658
+ */
5659
+ private function schedule_cron(
5660
+ $name,
5661
+ $action_tag = '',
5662
+ $recurrence = 'single',
5663
+ $start_at = WP_FS__SCRIPT_START_TIME,
5664
+ $randomize_start = true,
5665
+ $except_blog_id = 0
5666
+ ) {
5667
+ $this->_logger->entrance( $name );
5668
+
5669
+ $this->clear_cron( $name, $action_tag, true );
5670
+
5671
+ $cron_blog_id = $this->get_cron_target_blog_id( $except_blog_id );
5672
+
5673
+ if ( is_multisite() && 0 == $cron_blog_id ) {
5674
+ // Don't schedule cron since couldn't find a target blog.
5675
+ return;
5676
+ }
5677
+
5678
+ if ( 0 < $cron_blog_id ) {
5679
+ switch_to_blog( $cron_blog_id );
5680
+ }
5681
+
5682
+ if ( 'daily' === $recurrence ) {
5683
+ if ( $randomize_start ) {
5684
+ // Schedule first sync with a random 12 hour time range from now.
5685
+ $start_at += rand( 0, ( WP_FS__TIME_24_HOURS_IN_SEC / 2 ) );
5686
+ }
5687
+
5688
+ // Schedule daily WP cron.
5689
+ wp_schedule_event(
5690
+ $start_at,
5691
+ 'daily',
5692
+ $this->get_action_tag( $action_tag )
5693
+ );
5694
+ } else if ( 'single' === $recurrence ) {
5695
+ // Schedule single cron.
5696
+ wp_schedule_single_event(
5697
+ $start_at,
5698
+ $this->get_action_tag( $action_tag )
5699
+ );
5700
+ }
5701
+
5702
+ $this->set_cron_data( $name, $cron_blog_id );
5703
+
5704
+ if ( 0 < $cron_blog_id ) {
5705
+ restore_current_blog();
5706
+ }
5707
+ }
5708
+
5709
+ /**
5710
+ * Consolidated cron execution for performance optimization. The max number of API requests is based on the number of unique opted-in users.
5711
+ * that doesn't halt page loading.
5712
+ *
5713
+ * @author Vova Feldman (@svovaf)
5714
+ * @since 2.0.0
5715
+ *
5716
+ * @param string $name Cron name.
5717
+ * @param callable $callable The function that should be executed.
5718
+ */
5719
+ private function execute_cron( $name, $callable ) {
5720
+ $this->_logger->entrance( $name );
5721
+
5722
+ // Store the last time data sync was executed.
5723
+ $this->set_cron_execution_timestamp( $name );
5724
+
5725
+ // Check if API is temporary down.
5726
+ if ( FS_Api::is_temporary_down() ) {
5727
+ return;
5728
+ }
5729
+
5730
+ // @todo Add logic that identifies API latency, and reschedule the next background sync randomly between 8-16 hours.
5731
+
5732
+ $users_2_blog_ids = array();
5733
+
5734
+ if ( ! is_multisite() ) {
5735
+ // Add dummy blog.
5736
+ $users_2_blog_ids[0] = array( 0 );
5737
+ } else {
5738
+ $installs = $this->get_blog_install_map();
5739
+ foreach ( $installs as $blog_id => $install ) {
5740
+ if ( $this->is_premium() || $install->is_tracking_allowed() ) {
5741
+ if ( ! isset( $users_2_blog_ids[ $install->user_id ] ) ) {
5742
+ $users_2_blog_ids[ $install->user_id ] = array();
5743
+ }
5744
+
5745
+ $users_2_blog_ids[ $install->user_id ][] = $blog_id;
5746
+ }
5747
+ }
5748
+ }
5749
+
5750
+ foreach ( $users_2_blog_ids as $user_id => $blog_ids ) {
5751
+ if ( 0 < $blog_ids[0] ) {
5752
+ $this->switch_to_blog( $blog_ids[0] );
5753
+ }
5754
+
5755
+ call_user_func_array( $callable, array( $blog_ids ) );
5756
+
5757
+ foreach ( $blog_ids as $blog_id ) {
5758
+ $this->do_action( "after_{$name}_cron", $blog_id );
5759
+ }
5760
+ }
5761
+
5762
+ if ( is_multisite() ) {
5763
+ $this->do_action( "after_{$name}_cron_multisite" );
5764
+ }
5765
+ }
5766
+
5767
+ #endregion
5768
+
5769
+ #----------------------------------------------------------------------------------
5770
+ #region Daily Sync Cron
5771
+ #----------------------------------------------------------------------------------
5772
+
5773
+
5774
+ /**
5775
+ * @author Vova Feldman (@svovaf)
5776
+ * @since 2.0.0
5777
+ *
5778
+ * @return bool
5779
+ */
5780
+ private function is_sync_cron_scheduled() {
5781
+ return $this->is_cron_on( 'sync' );
5782
+ }
5783
+
5784
+ /**
5785
+ * Get the sync cron's executing blog ID.
5786
+ *
5787
+ * @author Vova Feldman (@svovaf)
5788
+ * @since 2.0.0
5789
+ *
5790
+ * @return int
5791
+ */
5792
+ private function get_sync_cron_blog_id() {
5793
+ return $this->get_cron_blog_id( 'sync' );
5794
+ }
5795
+
5796
+ /**
5797
+ * @author Vova Feldman (@svovaf)
5798
+ * @since 1.1.7.3
5799
+ */
5800
+ private function run_manual_sync() {
5801
+ self::require_pluggable_essentials();
5802
+
5803
+ if ( ! $this->is_user_admin() ) {
5804
+ return;
5805
+ }
5806
+
5807
+ // Run manual sync.
5808
+ $this->_sync_cron();
5809
+
5810
+ // Reschedule next cron to run 24 hours from now (performance optimization).
5811
+ $this->schedule_sync_cron( time() + WP_FS__TIME_24_HOURS_IN_SEC, false );
5812
+ }
5813
+
5814
+ /**
5815
+ * Data sync cron job. Replaces the background sync non blocking HTTP request
5816
+ * that doesn't halt page loading.
5817
+ *
5818
+ * @author Vova Feldman (@svovaf)
5819
+ * @since 1.1.7.3
5820
+ * @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.
5821
+ */
5822
+ function _sync_cron() {
5823
+ $this->_logger->entrance();
5824
+
5825
+ $this->execute_cron( 'sync', array( &$this, '_sync_cron_method' ) );
5826
+ }
5827
+
5828
+ /**
5829
+ * The actual data sync cron logic.
5830
+ *
5831
+ * @author Vova Feldman (@svovaf)
5832
+ * @since 2.0.0
5833
+ *
5834
+ * @param int[] $blog_ids
5835
+ */
5836
+ function _sync_cron_method( array $blog_ids ) {
5837
+ if ( $this->is_registered() ) {
5838
+ if ( $this->has_paid_plan() ) {
5839
+ // Initiate background plan sync.
5840
+ $this->_sync_license( true );
5841
+
5842
+ if ( $this->is_paying() ) {
5843
+ // Check for premium plugin updates.
5844
+ $this->check_updates( true );
5845
+ }
5846
+ } else {
5847
+ // Sync install(s) (only if something changed locally).
5848
+ if ( 1 < count( $blog_ids ) ) {
5849
+ $this->sync_installs();
5850
+ } else {
5851
+ $this->sync_install();
5852
+ }
5853
+ }
5854
+ }
5855
+ }
5856
+
5857
+ /**
5858
+ * Check if sync was executed in the last $period of seconds.
5859
+ *
5860
+ * @author Vova Feldman (@svovaf)
5861
+ * @since 1.1.7.3
5862
+ *
5863
+ * @param int $period In seconds
5864
+ *
5865
+ * @return bool
5866
+ */
5867
+ private function is_sync_executed( $period = WP_FS__TIME_24_HOURS_IN_SEC ) {
5868
+ return $this->is_cron_executed( 'sync', $period );
5869
+ }
5870
+
5871
+ /**
5872
+ * @author Vova Feldman (@svovaf)
5873
+ * @since 1.1.7.3
5874
+ *
5875
+ * @return bool
5876
+ */
5877
+ private function is_sync_cron_on() {
5878
+ return $this->is_cron_on( 'sync' );
5879
+ }
5880
+
5881
+ /**
5882
+ * @author Vova Feldman (@svovaf)
5883
+ * @since 1.1.7.3
5884
+ *
5885
+ * @param int $start_at Defaults to now.
5886
+ * @param bool $randomize_start If true, schedule first job randomly during the next 12 hours. Otherwise, schedule job to start right away.
5887
+ * @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.
5888
+ */
5889
+ private function schedule_sync_cron(
5890
+ $start_at = WP_FS__SCRIPT_START_TIME,
5891
+ $randomize_start = true,
5892
+ $except_blog_id = 0
5893
+ ) {
5894
+ $this->schedule_cron(
5895
+ 'sync',
5896
+ 'data_sync',
5897
+ 'daily',
5898
+ $start_at,
5899
+ $randomize_start,
5900
+ $except_blog_id
5901
+ );
5902
+ }
5903
+
5904
+ /**
5905
+ * Add the actual sync function to the cron job hook.
5906
+ *
5907
+ * @author Vova Feldman (@svovaf)
5908
+ * @since 1.1.7.3
5909
+ */
5910
+ private function hook_callback_to_sync_cron() {
5911
+ $this->add_action( 'data_sync', array( &$this, '_sync_cron' ) );
5912
+ }
5913
+
5914
+ /**
5915
+ * @author Vova Feldman (@svovaf)
5916
+ * @since 1.1.7.3
5917
+ *
5918
+ * @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.
5919
+ */
5920
+ private function clear_sync_cron( $is_network_clear = false ) {
5921
+ $this->_logger->entrance();
5922
+
5923
+ $this->clear_cron( 'sync', 'data_sync', $is_network_clear );
5924
+ }
5925
+
5926
+ /**
5927
+ * Unix timestamp for next sync cron execution or false if not scheduled.
5928
+ *
5929
+ * @author Vova Feldman (@svovaf)
5930
+ * @since 1.1.7.3
5931
+ *
5932
+ * @return int|false
5933
+ */
5934
+ function next_sync_cron() {
5935
+ return $this->get_next_scheduled_cron( 'sync', 'data_sync' );
5936
+ }
5937
+
5938
+ /**
5939
+ * Unix timestamp for previous sync cron execution or false if never executed.
5940
+ *
5941
+ * @author Vova Feldman (@svovaf)
5942
+ * @since 1.1.7.3
5943
+ *
5944
+ * @return int|false
5945
+ */
5946
+ function last_sync_cron() {
5947
+ return $this->cron_last_execution( 'sync' );
5948
+ }
5949
+
5950
+ #endregion Daily Sync Cron ------------------------------------------------------------------
5951
+
5952
+ #----------------------------------------------------------------------------------
5953
+ #region Async Install Sync
5954
+ #----------------------------------------------------------------------------------
5955
+
5956
+ /**
5957
+ * @author Vova Feldman (@svovaf)
5958
+ * @since 1.1.7.3
5959
+ *
5960
+ * @return bool
5961
+ */
5962
+ private function is_install_sync_scheduled() {
5963
+ return $this->is_cron_on( 'install_sync' );
5964
+ }
5965
+
5966
+ /**
5967
+ * Get the sync cron's executing blog ID.
5968
+ *
5969
+ * @author Vova Feldman (@svovaf)
5970
+ * @since 2.0.0
5971
+ *
5972
+ * @return int
5973
+ */
5974
+ private function get_install_sync_cron_blog_id() {
5975
+ return $this->get_cron_blog_id( 'install_sync' );
5976
+ }
5977
+
5978
+ /**
5979
+ * Instead of running blocking install sync event, execute non blocking scheduled wp-cron.
5980
+ *
5981
+ * @author Vova Feldman (@svovaf)
5982
+ * @since 1.1.7.3
5983
+ *
5984
+ * @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.
5985
+ */
5986
+ private function schedule_install_sync( $except_blog_id = 0 ) {
5987
+ $this->schedule_cron( 'install_sync', 'install_sync', 'single', 0, false, $except_blog_id );
5988
+ }
5989
+
5990
+ /**
5991
+ * Unix timestamp for previous install sync cron execution or false if never executed.
5992
+ *
5993
+ * @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.
5994
+ *
5995
+ * @author Vova Feldman (@svovaf)
5996
+ * @since 1.1.7.3
5997
+ *
5998
+ * @return int|false
5999
+ */
6000
+ function last_install_sync() {
6001
+ return $this->cron_last_execution( 'install_sync' );
6002
+ }
6003
+
6004
+ /**
6005
+ * Unix timestamp for next install sync cron execution or false if not scheduled.
6006
+ *
6007
+ * @author Vova Feldman (@svovaf)
6008
+ * @since 1.1.7.3
6009
+ *
6010
+ * @return int|false
6011
+ */
6012
+ function next_install_sync() {
6013
+ return $this->get_next_scheduled_cron( 'install_sync', 'install_sync' );
6014
+ }
6015
+
6016
+ /**
6017
+ * Add the actual install sync function to the cron job hook.
6018
+ *
6019
+ * @author Vova Feldman (@svovaf)
6020
+ * @since 1.1.7.3
6021
+ */
6022
+ private function hook_callback_to_install_sync() {
6023
+ $this->add_action( 'install_sync', array( &$this, '_run_sync_install' ) );
6024
+ }
6025
+
6026
+ /**
6027
+ * @author Vova Feldman (@svovaf)
6028
+ * @since 1.1.7.3
6029
+ *
6030
+ * @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.
6031
+ */
6032
+ private function clear_install_sync_cron( $is_network_clear = false ) {
6033
+ $this->_logger->entrance();
6034
+
6035
+ $this->clear_cron( 'install_sync', 'install_sync', $is_network_clear );
6036
+ }
6037
+
6038
+ /**
6039
+ * @author Vova Feldman (@svovaf)
6040
+ * @since 1.1.7.3
6041
+ * @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.
6042
+ */
6043
+ public function _run_sync_install() {
6044
+ $this->_logger->entrance();
6045
+
6046
+ $this->execute_cron( 'sync', array( &$this, '_sync_install_cron_method' ) );
6047
+ }
6048
+
6049
+ /**
6050
+ * The actual install(s) sync cron logic.
6051
+ *
6052
+ * @author Vova Feldman (@svovaf)
6053
+ * @since 2.0.0
6054
+ *
6055
+ * @param int[] $blog_ids
6056
+ */
6057
+ function _sync_install_cron_method( array $blog_ids ) {
6058
+ if ( $this->is_registered() ) {
6059
+ if ( 1 < count( $blog_ids ) ) {
6060
+ $this->sync_installs( array(), true );
6061
+ } else {
6062
+ $this->sync_install( array(), true );
6063
+ }
6064
+ }
6065
+ }
6066
+
6067
+ #endregion Async Install Sync ------------------------------------------------------------------
6068
+
6069
+ /**
6070
+ * Show a notice that activation is currently pending.
6071
+ *
6072
+ * @author Vova Feldman (@svovaf)
6073
+ * @since 1.0.7
6074
+ *
6075
+ * @param bool|string $email
6076
+ * @param bool $is_pending_trial Since 1.2.1.5
6077
+ */
6078
+ function _add_pending_activation_notice( $email = false, $is_pending_trial = false ) {
6079
+ if ( ! is_string( $email ) ) {
6080
+ $current_user = self::_get_current_wp_user();
6081
+ $email = $current_user->user_email;
6082
+ }
6083
+
6084
+ $this->_admin_notices->add_sticky(
6085
+ sprintf(
6086
+ $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' ),
6087
+ '<b>' . $this->get_plugin_name() . '</b>',
6088
+ '<b>' . $email . '</b>',
6089
+ ( $is_pending_trial ?
6090
+ $this->get_text_inline( 'start the trial', 'start-the-trial' ) :
6091
+ $this->get_text_inline( 'complete the install', 'complete-the-install' ) )
6092
+ ),
6093
+ 'activation_pending',
6094
+ 'Thanks!'
6095
+ );
6096
+ }
6097
+
6098
+ /**
6099
+ * Check if currently in plugin activation.
6100
+ *
6101
+ * @author Vova Feldman (@svovaf)
6102
+ * @since 1.1.4
6103
+ *
6104
+ * @return bool
6105
+ */
6106
+ function is_plugin_activation() {
6107
+ $result = get_transient( "fs_{$this->_module_type}_{$this->_slug}_activated" );
6108
+
6109
+ return !empty($result);
6110
+ }
6111
+
6112
+ /**
6113
+ *
6114
+ * NOTE: admin_menu action executed before admin_init.
6115
+ *
6116
+ * @author Vova Feldman (@svovaf)
6117
+ * @since 1.0.7
6118
+ */
6119
+ function _admin_init_action() {
6120
+ /**
6121
+ * Automatically redirect to connect/activation page after plugin activation.
6122
+ *
6123
+ * @since 1.1.7 Do NOT redirect to opt-in when running in network admin mode.
6124
+ */
6125
+ if ( $this->is_plugin_activation() ) {
6126
+ delete_transient( "fs_{$this->_module_type}_{$this->_slug}_activated" );
6127
+
6128
+ if ( isset( $_GET['activate-multi'] ) ) {
6129
+ /**
6130
+ * Don't redirect if activating multiple plugins at once (bulk activation).
6131
+ */
6132
+ } else {
6133
+ $this->_redirect_on_activation_hook();
6134
+ return;
6135
+ }
6136
+ }
6137
+
6138
+ if ( fs_request_is_action( $this->get_unique_affix() . '_skip_activation' ) ) {
6139
+ check_admin_referer( $this->get_unique_affix() . '_skip_activation' );
6140
+
6141
+ $this->skip_connection( null, fs_is_network_admin() );
6142
+
6143
+ fs_redirect( $this->get_after_activation_url( 'after_skip_url' ) );
6144
+ }
6145
+
6146
+ if ( $this->is_network_activation_mode() &&
6147
+ fs_request_is_action( $this->get_unique_affix() . '_delegate_activation' )
6148
+ ) {
6149
+ check_admin_referer( $this->get_unique_affix() . '_delegate_activation' );
6150
+
6151
+ $this->delegate_connection();
6152
+
6153
+ fs_redirect( $this->get_after_activation_url( 'after_delegation_url' ) );
6154
+ }
6155
+
6156
+ $this->_add_upgrade_action_link();
6157
+
6158
+ if ( ! $this->is_addon() &&
6159
+ ! ( ! $this->_is_network_active && fs_is_network_admin() ) &&
6160
+ (
6161
+ // Not registered nor anonymous.
6162
+ ( ! $this->is_registered() && ! $this->is_anonymous() ) ||
6163
+ // OR, network level and in network upgrade mode.
6164
+ ( fs_is_network_admin() && $this->_is_network_active && $this->is_network_upgrade_mode() )
6165
+ )
6166
+ ) {
6167
+ if ( ! $this->is_pending_activation() ) {
6168
+ if ( ! $this->_menu->is_main_settings_page() ) {
6169
+ /**
6170
+ * If a user visits any other admin page before activating the premium-only theme with a valid
6171
+ * license, reactivate the previous theme.
6172
+ *
6173
+ * @author Leo Fajardo (@leorw)
6174
+ * @since 1.2.2
6175
+ */
6176
+ if ( $this->is_theme()
6177
+ && $this->is_only_premium()
6178
+ && ! $this->has_settings_menu()
6179
+ && ! isset( $_REQUEST['fs_action'] )
6180
+ && $this->can_activate_previous_theme()
6181
+ ) {
6182
+ $this->activate_previous_theme();
6183
+
6184
+ return;
6185
+ }
6186
+
6187
+ if ( ! fs_is_network_admin() &&
6188
+ $this->is_network_activation_mode() &&
6189
+ ! $this->is_delegated_connection()
6190
+ ) {
6191
+ return;
6192
+ }
6193
+
6194
+ if ( $this->is_plugin_new_install() || $this->is_only_premium() ) {
6195
+ // Show notice for new plugin installations.
6196
+ $this->_admin_notices->add(
6197
+ sprintf(
6198
+ $this->get_text_inline( 'You are just one step away - %s', 'you-are-step-away' ),
6199
+ sprintf( '<b><a href="%s">%s</a></b>',
6200
+ $this->get_activation_url( array(), ! $this->is_delegated_connection() ),
6201
+ sprintf( $this->get_text_x_inline( 'Complete "%s" Activation Now',
6202
+ '%s - plugin name. As complete "PluginX" activation now', 'activate-x-now' ), $this->get_plugin_name() )
6203
+ )
6204
+ ),
6205
+ '',
6206
+ 'update-nag'
6207
+ );
6208
+ } else {
6209
+ if ( $this->should_add_sticky_optin_notice() ) {
6210
+ $this->add_sticky_optin_admin_notice();
6211
+ }
6212
+
6213
+ if ( $this->has_filter( 'optin_pointer_element' ) ) {
6214
+ // Don't show admin nag if plugin update.
6215
+ wp_enqueue_script( 'wp-pointer' );
6216
+ wp_enqueue_style( 'wp-pointer' );
6217
+
6218
+ $this->_enqueue_connect_essentials();
6219
+
6220
+ add_action( 'admin_print_footer_scripts', array(
6221
+ $this,
6222
+ '_add_connect_pointer_script'
6223
+ ) );
6224
+ }
6225
+ }
6226
+ }
6227
+ }
6228
+
6229
+ if ( $this->is_theme() &&
6230
+ $this->_menu->is_main_settings_page()
6231
+ ) {
6232
+ $this->_show_theme_activation_optin_dialog();
6233
+ }
6234
+ }
6235
+ }
6236
+
6237
+ /**
6238
+ * @author Vova Feldman (@svovaf)
6239
+ * @since 2.0.0
6240
+ *
6241
+ * @return bool
6242
+ */
6243
+ private function should_add_sticky_optin_notice() {
6244
+ if ( fs_is_network_admin() ) {
6245
+ if ( ! $this->_is_network_active ) {
6246
+ return false;
6247
+ }
6248
+
6249
+ if ( ! $this->is_network_activation_mode() ) {
6250
+ return false;
6251
+ }
6252
+
6253
+ return ! isset( $this->_storage->sticky_optin_added_ms );
6254
+ }
6255
+
6256
+ if ( ! $this->is_activation_mode() ) {
6257
+ return false;
6258
+ }
6259
+
6260
+ // If running from a blog admin and delegated the connection.
6261
+ return ! isset( $this->_storage->sticky_optin_added );
6262
+ }
6263
+
6264
+ /**
6265
+ * @author Leo Fajardo (@leorw)
6266
+ * @since 2.0.0
6267
+ */
6268
+ private function add_sticky_optin_admin_notice() {
6269
+ if ( ! $this->_is_network_active || ! fs_is_network_admin() ) {
6270
+ $this->_storage->sticky_optin_added = true;
6271
+ } else {
6272
+ $this->_storage->sticky_optin_added_ms = true;
6273
+ }
6274
+
6275
+ // Show notice for new plugin installations.
6276
+ $this->_admin_notices->add_sticky(
6277
+ sprintf(
6278
+ $this->get_text_inline( 'We made a few tweaks to the %s, %s', 'few-plugin-tweaks' ),
6279
+ $this->_module_type,
6280
+ sprintf( '<b><a href="%s">%s</a></b>',
6281
+ $this->get_activation_url(),
6282
+ sprintf( $this->get_text_inline( 'Opt in to make "%s" Better!', 'optin-x-now' ), $this->get_plugin_name() )
6283
+ )
6284
+ ),
6285
+ 'connect_account',
6286
+ '',
6287
+ 'update-nag'
6288
+ );
6289
+ }
6290
+
6291
+ /**
6292
+ * Enqueue connect requires scripts and styles.
6293
+ *
6294
+ * @author Vova Feldman (@svovaf)
6295
+ * @since 1.1.4
6296
+ */
6297
+ function _enqueue_connect_essentials() {
6298
+ wp_enqueue_script( 'jquery' );
6299
+ wp_enqueue_script( 'json2' );
6300
+
6301
+ fs_enqueue_local_script( 'postmessage', 'nojquery.ba-postmessage.min.js' );
6302
+ fs_enqueue_local_script( 'fs-postmessage', 'postmessage.js' );
6303
+
6304
+ fs_enqueue_local_style( 'fs_connect', '/admin/connect.css' );
6305
+ }
6306
+
6307
+ /**
6308
+ * Add connect / opt-in pointer.
6309
+ *
6310
+ * @author Vova Feldman (@svovaf)
6311
+ * @since 1.1.4
6312
+ */
6313
+ function _add_connect_pointer_script() {
6314
+ $vars = array( 'id' => $this->_module_id );
6315
+ $pointer_content = fs_get_template( 'connect.php', $vars );
6316
+ ?>
6317
+ <script type="text/javascript">// <![CDATA[
6318
+ jQuery(document).ready(function ($) {
6319
+ if ('undefined' !== typeof(jQuery().pointer)) {
6320
+
6321
+ var element = <?php echo $this->apply_filters( 'optin_pointer_element', '$("#non_existing_element");' ) ?>;
6322
+
6323
+ if (element.length > 0) {
6324
+ var optin = $(element).pointer($.extend(true, {}, {
6325
+ content : <?php echo json_encode( $pointer_content ) ?>,
6326
+ position : {
6327
+ edge : 'left',
6328
+ align: 'center'
6329
+ },
6330
+ buttons : function () {
6331
+ // Don't show pointer buttons.
6332
+ return '';
6333
+ },
6334
+ pointerWidth: 482
6335
+ }, <?php echo $this->apply_filters( 'optin_pointer_options_json', '{}' ) ?>));
6336
+
6337
+ <?php
6338
+ echo $this->apply_filters( 'optin_pointer_execute', "
6339
+
6340
+ optin.pointer('open');
6341
+
6342
+ // Tag the opt-in pointer with custom class.
6343
+ $('.wp-pointer #fs_connect')
6344
+ .parents('.wp-pointer.wp-pointer-top')
6345
+ .addClass('fs-opt-in-pointer');
6346
+
6347
+ ", 'element', 'optin' ) ?>
6348
+ }
6349
+ }
6350
+ });
6351
+ // ]]></script>
6352
+ <?php
6353
+ }
6354
+
6355
+ /**
6356
+ * Return current page's URL.
6357
+ *
6358
+ * @author Vova Feldman (@svovaf)
6359
+ * @since 1.0.7
6360
+ *
6361
+ * @return string
6362
+ */
6363
+ function current_page_url() {
6364
+ $url = 'http';
6365
+
6366
+ if ( isset( $_SERVER["HTTPS"] ) ) {
6367
+ if ( $_SERVER["HTTPS"] == "on" ) {
6368
+ $url .= "s";
6369
+ }
6370
+ }
6371
+ $url .= "://";
6372
+ if ( $_SERVER["SERVER_PORT"] != "80" ) {
6373
+ $url .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
6374
+ } else {
6375
+ $url .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
6376
+ }
6377
+
6378
+ return esc_url( $url );
6379
+ }
6380
+
6381
+ /**
6382
+ * Check if the current page is the plugin's main admin settings page.
6383
+ *
6384
+ * @author Vova Feldman (@svovaf)
6385
+ * @since 1.0.7
6386
+ *
6387
+ * @return bool
6388
+ */
6389
+ function _is_plugin_page() {
6390
+ return fs_is_plugin_page( $this->_menu->get_raw_slug() ) ||
6391
+ fs_is_plugin_page( $this->_slug );
6392
+ }
6393
+
6394
+ /* Events
6395
+ ------------------------------------------------------------------------------------------------------------------*/
6396
+ /**
6397
+ * Delete site install from Database.
6398
+ *
6399
+ * @author Vova Feldman (@svovaf)
6400
+ * @since 1.0.1
6401
+ *
6402
+ * @param bool $store
6403
+ * @param int|null $blog_id Since 2.0.0
6404
+ *
6405
+ * @return false|int The install ID if deleted. Otherwise, FALSE (when install not exist).
6406
+ */
6407
+ function _delete_site( $store = true, $blog_id = null ) {
6408
+ return self::_delete_site_by_slug( $this->_slug, $this->_module_type, $store, $blog_id );
6409
+ }
6410
+
6411
+ /**
6412
+ * Delete site install from Database.
6413
+ *
6414
+ * @author Vova Feldman (@svovaf)
6415
+ * @since 1.2.2.7
6416
+ *
6417
+ * @param string $slug
6418
+ * @param string $module_type
6419
+ * @param bool $store
6420
+ * @param int|null $blog_id Since 2.0.0
6421
+ *
6422
+ * @return false|int The install ID if deleted. Otherwise, FALSE (when install not exist).
6423
+ */
6424
+ static function _delete_site_by_slug( $slug, $module_type, $store = true, $blog_id = null ) {
6425
+ $sites = self::get_all_sites( $module_type, $blog_id );
6426
+
6427
+ $install_id = false;
6428
+
6429
+ if ( isset( $sites[ $slug ] ) ) {
6430
+ if ( is_object( $sites[ $slug ] ) ) {
6431
+ $install_id = $sites[ $slug ]->id;
6432
+ }
6433
+
6434
+ unset( $sites[ $slug ] );
6435
+
6436
+ self::set_account_option_by_module( $module_type, 'sites', $sites, $store, $blog_id );
6437
+ }
6438
+
6439
+ return $install_id;
6440
+ }
6441
+
6442
+ /**
6443
+ * Delete user.
6444
+ *
6445
+ * @author Vova Feldman (@svovaf)
6446
+ * @since 2.0.0
6447
+ *
6448
+ * @param number $user_id
6449
+ * @param bool $store
6450
+ *
6451
+ * @return false|int The user ID if deleted. Otherwise, FALSE (when install not exist).
6452
+ */
6453
+ private static function delete_user( $user_id, $store = true ) {
6454
+ $users = self::get_all_users();
6455
+
6456
+ if ( ! is_array( $users ) || ! isset( $users[ $user_id ] ) ) {
6457
+ return false;
6458
+ }
6459
+
6460
+ unset( $users[ $user_id ] );
6461
+
6462
+ self::$_accounts->set_option( 'users', $users, $store );
6463
+
6464
+ return $user_id;
6465
+ }
6466
+
6467
+ /**
6468
+ * Delete plugin's plans information.
6469
+ *
6470
+ * @param bool $store Flush to Database if true.
6471
+ * @param bool $keep_associated_plans If set to false, delete all plans, even if a plan is associated with an install.
6472
+ *
6473
+ * @author Vova Feldman (@svovaf)
6474
+ * @since 1.0.9
6475
+ */
6476
+ private function _delete_plans( $store = true, $keep_associated_plans = true ) {
6477
+ $this->_logger->entrance();
6478
+
6479
+ $plans = self::get_all_plans( $this->_module_type );
6480
+
6481
+ $plans_to_keep = array();
6482
+
6483
+ if ( $keep_associated_plans ) {
6484
+ $plans_ids_to_keep = $this->get_plans_ids_associated_with_installs();
6485
+ foreach ( $plans_ids_to_keep as $plan_id ) {
6486
+ $plan = self::_get_plan_by_id( $plan_id );
6487
+ if ( is_object( $plan ) ) {
6488
+ $plans_to_keep[] = $plan;
6489
+ }
6490
+ }
6491
+ }
6492
+
6493
+ if ( ! empty( $plans_to_keep ) ) {
6494
+ $plans[ $this->_slug ] = $plans_to_keep;
6495
+ } else {
6496
+ unset( $plans[ $this->_slug ] );
6497
+ }
6498
+
6499
+ $this->set_account_option( 'plans', $plans, $store );
6500
+ }
6501
+
6502
+ /**
6503
+ * Delete all plugin licenses.
6504
+ *
6505
+ * @author Vova Feldman (@svovaf)
6506
+ * @since 1.0.9
6507
+ *
6508
+ * @param bool $store
6509
+ */
6510
+ private function _delete_licenses( $store = true ) {
6511
+ $this->_logger->entrance();
6512
+
6513
+ $all_licenses = self::get_all_licenses();
6514
+
6515
+ unset( $all_licenses[ $this->_module_id ] );
6516
+
6517
+ self::$_accounts->set_option( 'all_licenses', $all_licenses, $store );
6518
+ }
6519
+
6520
+ /**
6521
+ * Check if Freemius was added on new plugin installation.
6522
+ *
6523
+ * @author Vova Feldman (@svovaf)
6524
+ * @since 1.1.5
6525
+ *
6526
+ * @return bool
6527
+ */
6528
+ function is_plugin_new_install() {
6529
+ return isset( $this->_storage->is_plugin_new_install ) &&
6530
+ $this->_storage->is_plugin_new_install;
6531
+ }
6532
+
6533
+ /**
6534
+ * Check if it's the first plugin release that is running Freemius.
6535
+ *
6536
+ * @author Vova Feldman (@svovaf)
6537
+ * @since 1.2.1.5
6538
+ *
6539
+ * @return bool
6540
+ */
6541
+ function is_first_freemius_powered_version() {
6542
+ return empty( $this->_storage->plugin_last_version );
6543
+ }
6544
+
6545
+ /**
6546
+ * @author Leo Fajardo (@leorw)
6547
+ * @since 1.2.2
6548
+ *
6549
+ * @return bool|string
6550
+ */
6551
+ private function get_previous_theme_slug() {
6552
+ return isset( $this->_storage->previous_theme ) ?
6553
+ $this->_storage->previous_theme :
6554
+ false;
6555
+ }
6556
+
6557
+ /**
6558
+ * @author Leo Fajardo (@leorw)
6559
+ * @since 1.2.2
6560
+ *
6561
+ * @return string
6562
+ */
6563
+ private function can_activate_previous_theme() {
6564
+ $slug = $this->get_previous_theme_slug();
6565
+ if ( false !== $slug && current_user_can( 'switch_themes' ) ) {
6566
+ $theme_instance = wp_get_theme( $slug );
6567
+
6568
+ return $theme_instance->exists();
6569
+ }
6570
+
6571
+ return false;
6572
+ }
6573
+
6574
+ /**
6575
+ * @author Leo Fajardo (@leorw)
6576
+ * @since 1.2.2
6577
+ */
6578
+ private function activate_previous_theme() {
6579
+ switch_theme( $this->get_previous_theme_slug() );
6580
+ unset( $this->_storage->previous_theme );
6581
+
6582
+ global $pagenow;
6583
+ if ( 'themes.php' === $pagenow ) {
6584
+ /**
6585
+ * Refresh the active theme information.
6586
+ *
6587
+ * @author Leo Fajardo (@leorw)
6588
+ * @since 1.2.2
6589
+ */
6590
+ fs_redirect( $this->admin_url( $pagenow ) );
6591
+ }
6592
+ }
6593
+
6594
+ /**
6595
+ * @author Leo Fajardo (@leorw)
6596
+ * @since 1.2.2
6597
+ *
6598
+ * @return string
6599
+ */
6600
+ function get_previous_theme_activation_url() {
6601
+ if ( ! $this->can_activate_previous_theme() ) {
6602
+ return '';
6603
+ }
6604
+
6605
+ /**
6606
+ * Activation URL
6607
+ *
6608
+ * @author Leo Fajardo (@leorw)
6609
+ * @since 1.2.2
6610
+ */
6611
+ return wp_nonce_url(
6612
+ $this->admin_url( 'themes.php?action=activate&stylesheet=' . urlencode( $this->get_previous_theme_slug() ) ),
6613
+ 'switch-theme_' . $this->get_previous_theme_slug()
6614
+ );
6615
+ }
6616
+
6617
+ /**
6618
+ * Saves the slug of the previous theme if it still exists so that it can be used by the logic in the opt-in
6619
+ * form that decides whether to add a close button to the opt-in dialog or not. So after a premium-only theme is
6620
+ * activated, the close button will appear and will reactivate the previous theme if clicked. If the previous
6621
+ * theme doesn't exist, then there will be no close button.
6622
+ *
6623
+ * @author Leo Fajardo (@leorw)
6624
+ * @since 1.2.2
6625
+ *
6626
+ * @param string $slug_or_name Old theme's slug or name.
6627
+ * @param bool|WP_Theme $old_theme WP_Theme instance of the old theme if it still exists.
6628
+ */
6629
+ function _activate_theme_event_hook( $slug_or_name, $old_theme = false ) {
6630
+ $this->_storage->previous_theme = ( false !== $old_theme ) ?
6631
+ $old_theme->get_stylesheet() :
6632
+ $slug_or_name;
6633
+
6634
+ $this->_activate_plugin_event_hook();
6635
+ }
6636
+
6637
+ /**
6638
+ * Plugin activated hook.
6639
+ *
6640
+ * @author Vova Feldman (@svovaf)
6641
+ * @since 1.0.1
6642
+ *
6643
+ * @uses FS_Api
6644
+ */
6645
+ function _activate_plugin_event_hook() {
6646
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
6647
+
6648
+ if ( ! $this->is_user_admin() ) {
6649
+ return;
6650
+ }
6651
+
6652
+ $this->unregister_uninstall_hook();
6653
+
6654
+ // Clear API cache on activation.
6655
+ FS_Api::clear_cache();
6656
+
6657
+ $is_premium_version_activation = ( current_filter() !== ( 'activate_' . $this->_free_plugin_basename ) );
6658
+
6659
+ $this->_logger->info( 'Activating ' . ( $is_premium_version_activation ? 'premium' : 'free' ) . ' plugin version.' );
6660
+
6661
+ // 1. If running in the activation of the FREE module, get the basename of the PREMIUM.
6662
+ // 2. If running in the activation of the PREMIUM module, get the basename of the FREE.
6663
+ $other_version_basename = $is_premium_version_activation ?
6664
+ $this->_free_plugin_basename :
6665
+ $this->premium_plugin_basename();
6666
+
6667
+ if ( ! $this->_is_network_active ) {
6668
+ /**
6669
+ * During the activation, the plugin isn't yet active, therefore,
6670
+ * _is_network_active will be set to false even if it's a network level
6671
+ * activation. So we need to fix that by looking at the is_network_admin() value.
6672
+ *
6673
+ * @author Vova Feldman
6674
+ */
6675
+ $this->_is_network_active = (
6676
+ $this->_is_multisite_integrated &&
6677
+ // Themes are always network activated, but the ACTUAL activation is per site.
6678
+ $this->is_plugin() &&
6679
+ fs_is_network_admin()
6680
+ );
6681
+ }
6682
+
6683
+ /**
6684
+ * If the other module version is activate, deactivate it.
6685
+ *
6686
+ * is_plugin_active() checks if the plugin active on the site or the network level
6687
+ * and deactivate_plugins() deactivates the plugin whether its activated on the site
6688
+ * or network level.
6689
+ *
6690
+ * @author Leo Fajardo (@leorw)
6691
+ * @since 1.2.2
6692
+ */
6693
+ if ( is_plugin_active( $other_version_basename ) ) {
6694
+ deactivate_plugins( $other_version_basename );
6695
+ }
6696
+
6697
+ if ( $this->is_registered() ) {
6698
+ if ( $is_premium_version_activation ) {
6699
+ $this->reconnect_locally();
6700
+ }
6701
+
6702
+
6703
+ // Schedule re-activation event and sync.
6704
+ // $this->sync_install( array(), true );
6705
+ $this->schedule_install_sync();
6706
+
6707
+ // If activating the premium module version, add an admin notice to congratulate for an upgrade completion.
6708
+ if ( $is_premium_version_activation ) {
6709
+ $this->_admin_notices->add(
6710
+ sprintf( $this->get_text_inline( 'The upgrade of %s was successfully completed.', 'successful-version-upgrade-message' ), sprintf( '<b>%s</b>', $this->_plugin->title ) ),
6711
+ $this->get_text_x_inline( 'W00t',
6712
+ 'Used to express elation, enthusiasm, or triumph (especially in electronic communication).', 'woot' ) . '!'
6713
+ );
6714
+ }
6715
+ } else if ( $this->is_anonymous() ) {
6716
+ if ( isset( $this->_storage->is_anonymous_ms ) && $this->_storage->is_anonymous_ms['is'] ) {
6717
+ $plugin_version = $this->_storage->is_anonymous_ms['version'];
6718
+ $network = true;
6719
+ } else {
6720
+ $plugin_version = $this->_storage->is_anonymous['version'];
6721
+ $network = false;
6722
+ }
6723
+
6724
+ /**
6725
+ * Reset "skipped" click cache on the following:
6726
+ * 1. Freemius DEV mode.
6727
+ * 2. WordPress DEBUG mode.
6728
+ * 3. If a plugin and the user skipped the exact same version before.
6729
+ *
6730
+ * @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).
6731
+ *
6732
+ * @todo 4. If explicitly asked to retry after every activation.
6733
+ */
6734
+ if ( WP_FS__DEV_MODE ||
6735
+ (
6736
+ ( $this->is_plugin() || ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ) &&
6737
+ $this->get_plugin_version() == $plugin_version
6738
+ )
6739
+ ) {
6740
+ $this->reset_anonymous_mode( $network );
6741
+ }
6742
+ }
6743
+
6744
+ if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
6745
+ /**
6746
+ * If no previous version of plugin's version exist, it means that it's either
6747
+ * the first time that the plugin installed on the site, or the plugin was installed
6748
+ * before but didn't have Freemius integrated.
6749
+ *
6750
+ * Since register_activation_hook() do NOT fires on updates since 3.1, and only fires
6751
+ * on manual activation via the dashboard, is_plugin_activation() is TRUE
6752
+ * only after immediate activation.
6753
+ *
6754
+ * @since 1.1.4
6755
+ * @link https://make.wordpress.org/core/2010/10/27/plugin-activation-hooks-no-longer-fire-for-updates/
6756
+ */
6757
+ $this->_storage->is_plugin_new_install = empty( $this->_storage->plugin_last_version );
6758
+ }
6759
+
6760
+ if ( ! $this->_anonymous_mode &&
6761
+ $this->has_api_connectivity( WP_FS__DEV_MODE ) &&
6762
+ ! $this->_isAutoInstall
6763
+ ) {
6764
+ // Store hint that the plugin was just activated to enable auto-redirection to settings.
6765
+ set_transient( "fs_{$this->_module_type}_{$this->_slug}_activated", true, 60 );
6766
+ }
6767
+
6768
+ /**
6769
+ * Activation hook is executed after the plugin's main file is loaded, therefore,
6770
+ * after the plugin was loaded. The logic is located at activate_plugin()
6771
+ * ./wp-admin/includes/plugin.php.
6772
+ *
6773
+ * @author Vova Feldman (@svovaf)
6774
+ * @since 1.1.9
6775
+ */
6776
+ $this->_storage->was_plugin_loaded = true;
6777
+ }
6778
+
6779
+ /**
6780
+ * Delete account.
6781
+ *
6782
+ * @author Vova Feldman (@svovaf)
6783
+ * @since 1.0.3
6784
+ *
6785
+ * @param bool $check_user Enforce checking if user have plugins activation privileges.
6786
+ */
6787
+ function delete_account_event( $check_user = true ) {
6788
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
6789
+
6790
+ if ( $check_user && ! $this->is_user_admin() ) {
6791
+ return;
6792
+ }
6793
+
6794
+ $this->do_action( 'before_account_delete' );
6795
+
6796
+ // Clear all admin notices.
6797
+ $this->_admin_notices->clear_all_sticky( false );
6798
+
6799
+ $this->_delete_site( false );
6800
+
6801
+ $delete_network_common_data = true;
6802
+
6803
+ if ( $this->_is_network_active ) {
6804
+ $installs = $this->get_blog_install_map();
6805
+
6806
+ // Don't delete common network data unless no other installs left.
6807
+ $delete_network_common_data = empty( $installs );
6808
+ }
6809
+
6810
+ if ( $delete_network_common_data ) {
6811
+ $this->_delete_plans( false );
6812
+
6813
+ $this->_delete_licenses( false );
6814
+
6815
+ // Delete add-ons related to plugin's account.
6816
+ $this->_delete_account_addons( false );
6817
+ }
6818
+
6819
+ // @todo Delete plans and licenses of add-ons.
6820
+
6821
+ self::$_accounts->store();
6822
+
6823
+ /**
6824
+ * IMPORTANT:
6825
+ * Clear crons must be executed before clearing all storage.
6826
+ * Otherwise, the cron will not be cleared.
6827
+ */
6828
+ if ( $delete_network_common_data ) {
6829
+ $this->clear_sync_cron();
6830
+ }
6831
+
6832
+ $this->clear_install_sync_cron();
6833
+
6834
+ // Clear all storage data.
6835
+ $this->_storage->clear_all( true, array(
6836
+ 'connectivity_test',
6837
+ 'is_on',
6838
+ ), false );
6839
+
6840
+ // Send delete event.
6841
+ $this->get_api_site_scope()->call( '/', 'delete' );
6842
+
6843
+ $this->do_action( 'after_account_delete' );
6844
+ }
6845
+
6846
+ /**
6847
+ * Delete network level account.
6848
+ *
6849
+ * @author Vova Feldman (@svovaf)
6850
+ * @since 2.0.0
6851
+ *
6852
+ * @param bool $check_user Enforce checking if user have plugins activation privileges.
6853
+ */
6854
+ function delete_network_account_event( $check_user = true ) {
6855
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
6856
+
6857
+ if ( $check_user && ! $this->is_user_admin() ) {
6858
+ return;
6859
+ }
6860
+
6861
+ $this->do_action( 'before_network_account_delete' );
6862
+
6863
+ // Clear all admin notices.
6864
+ $this->_admin_notices->clear_all_sticky();
6865
+
6866
+ $this->_delete_plans( false, false );
6867
+
6868
+ $this->_delete_licenses( false );
6869
+
6870
+ // Delete add-ons related to plugin's account.
6871
+ $this->_delete_account_addons( false );
6872
+
6873
+ // @todo Delete plans and licenses of add-ons.
6874
+
6875
+ self::$_accounts->store( true );
6876
+
6877
+ /**
6878
+ * IMPORTANT:
6879
+ * Clear crons must be executed before clearing all storage.
6880
+ * Otherwise, the cron will not be cleared.
6881
+ */
6882
+ $this->clear_sync_cron( true );
6883
+ $this->clear_install_sync_cron( true );
6884
+
6885
+ $sites = self::get_sites();
6886
+
6887
+ $install_ids = array();
6888
+ foreach ( $sites as $site ) {
6889
+ $blog_id = self::get_site_blog_id( $site );
6890
+
6891
+ $install_id = $this->_delete_site( true, $blog_id );
6892
+
6893
+ // Clear all storage data.
6894
+ $this->_storage->clear_all( true, array( 'connectivity_test' ), $blog_id );
6895
+
6896
+ if ( FS_Site::is_valid_id( $install_id ) ) {
6897
+ $install_ids[] = $install_id;
6898
+ }
6899
+
6900
+ switch_to_blog( $blog_id );
6901
+
6902
+ $this->do_action( 'after_account_delete' );
6903
+
6904
+ restore_current_blog();
6905
+ }
6906
+
6907
+ $this->_storage->clear_all( true, array(
6908
+ 'connectivity_test',
6909
+ 'is_on',
6910
+ ), true );
6911
+
6912
+ // Send delete event.
6913
+ if ( ! empty( $install_ids ) ) {
6914
+ $result = $this->get_current_or_network_user_api_scope()->call( "/plugins/{$this->_module_id}/installs.json?ids=" . implode( ',', $install_ids ), 'delete' );
6915
+ }
6916
+
6917
+ $this->do_action( 'after_network_account_delete' );
6918
+ }
6919
+
6920
+ /**
6921
+ * Plugin deactivation hook.
6922
+ *
6923
+ * @author Vova Feldman (@svovaf)
6924
+ * @since 1.0.1
6925
+ */
6926
+ function _deactivate_plugin_hook() {
6927
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
6928
+
6929
+ if ( ! $this->is_user_admin() ) {
6930
+ return;
6931
+ }
6932
+
6933
+ $is_network_deactivation = fs_is_network_admin();
6934
+ $storage_keys_for_removal = array();
6935
+
6936
+ $this->_admin_notices->clear_all_sticky();
6937
+
6938
+ $storage_keys_for_removal[] = 'sticky_optin_added';
6939
+ if ( isset( $this->_storage->sticky_optin_added ) ) {
6940
+ unset( $this->_storage->sticky_optin_added );
6941
+ }
6942
+
6943
+ if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
6944
+ // Remember that plugin was already installed.
6945
+ $this->_storage->is_plugin_new_install = false;
6946
+ }
6947
+
6948
+ // Hook to plugin uninstall.
6949
+ register_uninstall_hook( $this->_plugin_main_file_path, array( 'Freemius', '_uninstall_plugin_hook' ) );
6950
+
6951
+ $this->clear_module_main_file_cache();
6952
+ $this->clear_sync_cron( $this->_is_network_active );
6953
+ $this->clear_install_sync_cron();
6954
+
6955
+ if ( $this->is_registered() ) {
6956
+ if ( $this->is_premium() && ! $this->has_active_valid_license() ) {
6957
+ FS_Plugin_Updater::instance( $this )->delete_update_data();
6958
+ }
6959
+
6960
+ if ( $is_network_deactivation ) {
6961
+ // Send deactivation event.
6962
+ $this->sync_installs( array(
6963
+ 'is_active' => false,
6964
+ ) );
6965
+ } else {
6966
+ // Send deactivation event.
6967
+ $this->sync_install( array(
6968
+ 'is_active' => false,
6969
+ ) );
6970
+ }
6971
+ } else {
6972
+ if ( ! $this->has_api_connectivity() ) {
6973
+ // Reset connectivity test cache.
6974
+ unset( $this->_storage->connectivity_test );
6975
+
6976
+ $storage_keys_for_removal[] = 'connectivity_test';
6977
+ }
6978
+ }
6979
+
6980
+ if ( $is_network_deactivation ) {
6981
+ if ( isset( $this->_storage->sticky_optin_added_ms ) ) {
6982
+ unset( $this->_storage->sticky_optin_added_ms );
6983
+ }
6984
+
6985
+ if ( ! empty( $storage_keys_for_removal ) ) {
6986
+ $sites = self::get_sites();
6987
+
6988
+ foreach ( $sites as $site ) {
6989
+ $blog_id = self::get_site_blog_id( $site );
6990
+
6991
+ foreach ( $storage_keys_for_removal as $key ) {
6992
+ $this->_storage->remove( $key, false, $blog_id );
6993
+ }
6994
+
6995
+ $this->_storage->save( $blog_id );
6996
+ }
6997
+ }
6998
+ }
6999
+
7000
+ // Clear API cache on deactivation.
7001
+ FS_Api::clear_cache();
7002
+
7003
+ $this->remove_sdk_reference();
7004
+ }
7005
+
7006
+ /**
7007
+ * @author Vova Feldman (@svovaf)
7008
+ * @since 1.1.6
7009
+ */
7010
+ private function remove_sdk_reference() {
7011
+ global $fs_active_plugins;
7012
+
7013
+ foreach ( $fs_active_plugins->plugins as $sdk_path => &$data ) {
7014
+ if ( $this->_plugin_basename == $data->plugin_path ) {
7015
+ unset( $fs_active_plugins->plugins[ $sdk_path ] );
7016
+ break;
7017
+ }
7018
+ }
7019
+
7020
+ fs_fallback_to_newest_active_sdk();
7021
+ }
7022
+
7023
+ /**
7024
+ * @author Vova Feldman (@svovaf)
7025
+ * @since 1.1.3
7026
+ *
7027
+ * @param bool $is_anonymous
7028
+ * @param bool|int $network_or_blog_id Since 2.0.0
7029
+ */
7030
+ private function set_anonymous_mode( $is_anonymous = true, $network_or_blog_id = 0 ) {
7031
+ // Store information regarding skip to try and opt-in the user
7032
+ // again in the future.
7033
+ $skip_info = array(
7034
+ 'is' => $is_anonymous,
7035
+ 'timestamp' => WP_FS__SCRIPT_START_TIME,
7036
+ 'version' => $this->get_plugin_version(),
7037
+ );
7038
+
7039
+ if ( true === $network_or_blog_id ) {
7040
+ $this->_storage->is_anonymous_ms = $skip_info;
7041
+ } else {
7042
+ $this->_storage->store( 'is_anonymous', $skip_info, $network_or_blog_id );
7043
+ }
7044
+
7045
+ $this->network_upgrade_mode_completed();
7046
+
7047
+ // Update anonymous mode cache.
7048
+ $this->_is_anonymous = $is_anonymous;
7049
+ }
7050
+
7051
+ /**
7052
+ * @author Vova Feldman (@svovaf)
7053
+ * @since 2.0.0
7054
+ *
7055
+ * @param int $blog_id Site ID.
7056
+ * @param int $user_id User ID.
7057
+ * @param string $domain Site domain.
7058
+ * @param string $path Site path.
7059
+ * @param int $network_id Network ID. Only relevant on multi-network installations.
7060
+ * @param array $meta Metadata. Used to set initial site options.
7061
+ *
7062
+ * @uses Freemius::is_license_network_active() to check if the context license was network activated by the super-admin.
7063
+ * @uses Freemius::is_network_connected() to check if the super-admin network opted-in.
7064
+ * @uses Freemius::is_network_anonymous() to check if the super-admin network skipped.
7065
+ * @uses Freemius::is_network_delegated_connection() to check if the super-admin network delegated the connection to the site admins.
7066
+ */
7067
+ function _after_new_blog_callback( $blog_id, $user_id, $domain, $path, $network_id, $meta ) {
7068
+ $this->_logger->entrance();
7069
+
7070
+ if ( $this->is_premium() &&
7071
+ $this->is_network_connected() &&
7072
+ is_object( $this->_license ) &&
7073
+ $this->_license->can_activate( FS_Site::is_localhost_by_address( $domain ) ) &&
7074
+ $this->is_license_network_active( $blog_id )
7075
+ ) {
7076
+ /**
7077
+ * 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.
7078
+ */
7079
+ $current_blog_id = get_current_blog_id();
7080
+ $license = clone $this->_license;
7081
+
7082
+ $this->switch_to_blog( $blog_id );
7083
+
7084
+ // Opt-in with network user.
7085
+ $this->install_with_user(
7086
+ $this->get_network_user(),
7087
+ $license->secret_key,
7088
+ false,
7089
+ false,
7090
+ false
7091
+ );
7092
+
7093
+ if ( is_object( $this->_site ) ) {
7094
+ if ( $this->_site->license_id == $license->id ) {
7095
+ /**
7096
+ * If the license was activated successfully, sync the license data from the remote server.
7097
+ */
7098
+ $this->_license = $license;
7099
+ $this->sync_site_license();
7100
+ }
7101
+ }
7102
+
7103
+ $this->switch_to_blog( $current_blog_id );
7104
+
7105
+ if ( is_object( $this->_site ) ) {
7106
+ // Already connected (with or without a license), so no need to continue.
7107
+ return;
7108
+ }
7109
+ }
7110
+
7111
+ if ( $this->is_network_anonymous() ) {
7112
+ /**
7113
+ * Opt-in was network skipped so automatically skip the opt-in for the new site.
7114
+ */
7115
+ $this->skip_site_connection( $blog_id );
7116
+ } else if ( $this->is_network_delegated_connection() ) {
7117
+ /**
7118
+ * Opt-in was network delegated so automatically delegate the opt-in for the new site's admin.
7119
+ */
7120
+ $this->delegate_site_connection( $blog_id );
7121
+ } else if ( $this->is_network_connected() ) {
7122
+ /**
7123
+ * Opt-in was network activated so automatically opt-in with the network user and new site admin.
7124
+ */
7125
+ $current_blog_id = get_current_blog_id();
7126
+
7127
+ $this->switch_to_blog( $blog_id );
7128
+
7129
+ // Opt-in with network user.
7130
+ $this->install_with_user(
7131
+ $this->get_network_user(),
7132
+ false,
7133
+ false,
7134
+ false,
7135
+ false
7136
+ );
7137
+
7138
+ $this->switch_to_blog( $current_blog_id );
7139
+ } else {
7140
+ /**
7141
+ * If the super-admin mixed different options (connect, skip, delegated):
7142
+ * a) If at least one site connection was delegated, then automatically delegate connection.
7143
+ * 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.
7144
+ */
7145
+ $has_delegated_site = false;
7146
+
7147
+ $sites = self::get_sites();
7148
+ foreach ( $sites as $site ) {
7149
+ $blog_id = self::get_site_blog_id( $site );
7150
+
7151
+ if ( $this->is_site_delegated_connection( $blog_id ) ) {
7152
+ $has_delegated_site = true;
7153
+ break;
7154
+ }
7155
+ }
7156
+
7157
+ if ( $has_delegated_site ) {
7158
+ $this->delegate_site_connection( $blog_id );
7159
+ } else {
7160
+ $this->skip_site_connection( $blog_id );
7161
+ }
7162
+ }
7163
+ }
7164
+
7165
+ /**
7166
+ * @author Vova Feldman (@svovaf)
7167
+ * @since 1.1.3
7168
+ *
7169
+ * @param bool|int $network_or_blog_id Since 2.0.0.
7170
+ */
7171
+ private function reset_anonymous_mode( $network_or_blog_id = 0 ) {
7172
+ if ( true === $network_or_blog_id ) {
7173
+ unset( $this->_storage->is_anonymous_ms );
7174
+ } else {
7175
+ $this->_storage->remove( 'is_anonymous', true, $network_or_blog_id );
7176
+ }
7177
+
7178
+ /**
7179
+ * Ensure that this field is also "false", otherwise, if the current module's type is "theme" and the module
7180
+ * has no menus, the opt-in popup will not be shown immediately (in this case, the user will have to click
7181
+ * on the admin notice that contains the opt-in link in order to trigger the opt-in popup).
7182
+ *
7183
+ * @author Leo Fajardo (@leorw)
7184
+ * @since 1.2.2
7185
+ */
7186
+ if ( ! $this->_is_network_active ||
7187
+ 0 === $network_or_blog_id ||
7188
+ get_current_blog_id() == $network_or_blog_id ||
7189
+ ( true === $network_or_blog_id && fs_is_network_admin() )
7190
+ ) {
7191
+ unset( $this->_is_anonymous );
7192
+ }
7193
+ }
7194
+
7195
+ /**
7196
+ * Clears the anonymous mode and redirects to the opt-in screen.
7197
+ *
7198
+ * @author Vova Feldman (@svovaf)
7199
+ * @since 1.1.7
7200
+ */
7201
+ function connect_again() {
7202
+ if ( ! $this->is_anonymous() ) {
7203
+ return;
7204
+ }
7205
+
7206
+ $this->reset_anonymous_mode( fs_is_network_admin() );
7207
+
7208
+ fs_redirect( $this->get_activation_url() );
7209
+ }
7210
+
7211
+ /**
7212
+ * Skip account connect, and set anonymous mode.
7213
+ *
7214
+ * @author Vova Feldman (@svovaf)
7215
+ * @since 1.1.1
7216
+ *
7217
+ * @param array|null $sites Since 2.0.0. Specific sites.
7218
+ * @param bool $skip_all_network Since 2.0.0. If true, skip connection for all sites.
7219
+ */
7220
+ function skip_connection( $sites = null, $skip_all_network = false ) {
7221
+ $this->_logger->entrance();
7222
+
7223
+ $this->_admin_notices->remove_sticky( 'connect_account' );
7224
+
7225
+ if ( $skip_all_network ) {
7226
+ $this->set_anonymous_mode( true, true );
7227
+ }
7228
+
7229
+ if ( ! $skip_all_network && empty( $sites ) ) {
7230
+ $this->skip_site_connection();
7231
+ } else {
7232
+ $uids = array();
7233
+
7234
+ if ( $skip_all_network ) {
7235
+ $this->set_anonymous_mode( true, true );
7236
+
7237
+ $sites = self::get_sites();
7238
+ foreach ( $sites as $site ) {
7239
+ $blog_id = self::get_site_blog_id( $site );
7240
+ $this->skip_site_connection( $blog_id, false );
7241
+ $uids[] = $this->get_anonymous_id( $blog_id );
7242
+ }
7243
+ } else if ( ! empty( $sites ) ) {
7244
+ foreach ( $sites as $site ) {
7245
+ $uids[] = $site['uid'];
7246
+ $this->skip_site_connection( $site['blog_id'], false );
7247
+ }
7248
+ }
7249
+
7250
+ // Send anonymous skip event.
7251
+ // No user identified info nor any tracking will be sent after the user skips the opt-in.
7252
+ $this->get_api_plugin_scope()->call( 'skip.json', 'put', array(
7253
+ 'uids' => $uids,
7254
+ ) );
7255
+ }
7256
+
7257
+ $this->network_upgrade_mode_completed();
7258
+ }
7259
+
7260
+ /**
7261
+ * Skip connection for specific site in the network.
7262
+ *
7263
+ * @author Vova Feldman (@svovaf)
7264
+ * @since 2.0.0
7265
+ *
7266
+ * @param int|null $blog_id
7267
+ * @param bool $send_skip
7268
+ */
7269
+ private function skip_site_connection( $blog_id = null, $send_skip = true ) {
7270
+ $this->_logger->entrance();
7271
+
7272
+ $this->_admin_notices->remove_sticky( 'connect_account', $blog_id );
7273
+
7274
+ $this->set_anonymous_mode( true, $blog_id );
7275
+
7276
+ if ( $send_skip ) {
7277
+ $this->get_api_plugin_scope()->call( 'skip.json', 'put', array(
7278
+ 'uids' => array( $this->get_anonymous_id( $blog_id ) ),
7279
+ ) );
7280
+ }
7281
+ }
7282
+
7283
+ /**
7284
+ * Plugin version update hook.
7285
+ *
7286
+ * @author Vova Feldman (@svovaf)
7287
+ * @since 1.0.4
7288
+ */
7289
+ private function update_plugin_version_event() {
7290
+ $this->_logger->entrance();
7291
+
7292
+ if ( ! $this->is_registered() ) {
7293
+ return;
7294
+ }
7295
+
7296
+ $this->schedule_install_sync();
7297
+ // $this->sync_install( array(), true );
7298
+ }
7299
+
7300
+ /**
7301
+ * Generate an MD5 signature of a plugins collection.
7302
+ * This helper methods used to identify changes in a plugins collection.
7303
+ *
7304
+ * @author Vova Feldman (@svovaf)
7305
+ * @since 2.0.0
7306
+ *
7307
+ * @param array [string]array $plugins
7308
+ *
7309
+ * @return string
7310
+ */
7311
+ private function get_plugins_thumbprint( $plugins ) {
7312
+ ksort( $plugins );
7313
+
7314
+ $thumbprint = '';
7315
+ foreach ( $plugins as $basename => $data ) {
7316
+ $thumbprint .= $data['slug'] . ',' .
7317
+ $data['Version'] . ',' .
7318
+ ( $data['is_active'] ? '1' : '0' ) . ';';
7319
+ }
7320
+
7321
+ return md5( $thumbprint );
7322
+ }
7323
+
7324
+ /**
7325
+ * Return a list of modified plugins since the last sync.
7326
+ *
7327
+ * Note:
7328
+ * There's no point to store a plugins counter since even if the number of
7329
+ * plugins didn't change, we still need to check if the versions are all the
7330
+ * same and the activity state is similar.
7331
+ *
7332
+ * @author Vova Feldman (@svovaf)
7333
+ * @since 1.1.8
7334
+ *
7335
+ * @return array|false
7336
+ */
7337
+ private function get_plugins_data_for_api() {
7338
+ // Alias.
7339
+ $site_active_plugins_option_name = 'active_plugins';
7340
+ $network_plugins_option_name = 'all_plugins';
7341
+
7342
+ /**
7343
+ * Collection of all site level active plugins.
7344
+ */
7345
+ $site_active_plugins_cache = self::$_accounts->get_option( $site_active_plugins_option_name );
7346
+
7347
+ if ( ! is_object( $site_active_plugins_cache ) ) {
7348
+ $site_active_plugins_cache = (object) array(
7349
+ 'timestamp' => '',
7350
+ 'md5' => '',
7351
+ 'plugins' => array(),
7352
+ );
7353
+ }
7354
+
7355
+ $time = time();
7356
+
7357
+ if ( ! empty( $site_active_plugins_cache->timestamp ) &&
7358
+ ( $time - $site_active_plugins_cache->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC
7359
+ ) {
7360
+ // Don't send plugin updates if last update was in the past 5 min.
7361
+ return false;
7362
+ }
7363
+
7364
+ // Write timestamp to lock the logic.
7365
+ $site_active_plugins_cache->timestamp = $time;
7366
+ self::$_accounts->set_option( $site_active_plugins_option_name, $site_active_plugins_cache, true );
7367
+
7368
+ // Reload options from DB.
7369
+ self::$_accounts->load( true );
7370
+ $site_active_plugins_cache = self::$_accounts->get_option( $site_active_plugins_option_name );
7371
+
7372
+ if ( $time != $site_active_plugins_cache->timestamp ) {
7373
+ // If timestamp is different, then another thread captured the lock.
7374
+ return false;
7375
+ }
7376
+
7377
+ /**
7378
+ * Collection of all plugins (network level).
7379
+ */
7380
+ $network_plugins_cache = self::$_accounts->get_option( $network_plugins_option_name );
7381
+
7382
+ if ( ! is_object( $network_plugins_cache ) ) {
7383
+ $network_plugins_cache = (object) array(
7384
+ 'timestamp' => '',
7385
+ 'md5' => '',
7386
+ 'plugins' => array(),
7387
+ );
7388
+ }
7389
+
7390
+ // Check if there's a change in plugins.
7391
+ $network_plugins = self::get_network_plugins();
7392
+ $site_active_plugins = self::get_site_active_plugins();
7393
+
7394
+ $network_plugins_thumbprint = $this->get_plugins_thumbprint( $network_plugins );
7395
+ $site_active_plugins_thumbprint = $this->get_plugins_thumbprint( $site_active_plugins );
7396
+
7397
+ // Check if plugins status changed (version or active/inactive).
7398
+ $network_plugins_changed = ( $network_plugins_cache->md5 !== $network_plugins_thumbprint );
7399
+ $site_active_plugins_changed = ( $site_active_plugins_cache->md5 !== $site_active_plugins_thumbprint );
7400
+
7401
+ if ( ! $network_plugins_changed &&
7402
+ ! $site_active_plugins_changed
7403
+ ) {
7404
+ // No changes.
7405
+ return array();
7406
+ }
7407
+
7408
+ $plugins_update_data = array();
7409
+
7410
+ foreach ( $network_plugins_cache->plugins as $basename => $data ) {
7411
+ if ( ! isset( $network_plugins[ $basename ] ) ) {
7412
+ // Plugin uninstalled.
7413
+ $uninstalled_plugin_data = $data;
7414
+ $uninstalled_plugin_data['is_active'] = false;
7415
+ $uninstalled_plugin_data['is_uninstalled'] = true;
7416
+ $plugins_update_data[] = $uninstalled_plugin_data;
7417
+
7418
+ unset( $network_plugins[ $basename ] );
7419
+
7420
+ unset( $network_plugins_cache->plugins[ $basename ] );
7421
+ unset( $site_active_plugins_cache->plugins[ $basename ] );
7422
+
7423
+ continue;
7424
+ }
7425
+
7426
+ $was_active = $data['is_active'] ||
7427
+ ( isset( $site_active_plugins_cache->plugins[ $basename ] ) &&
7428
+ true === $site_active_plugins_cache->plugins[ $basename ]['is_active'] );
7429
+ $is_active = $network_plugins[ $basename ]['is_active'] ||
7430
+ ( isset( $site_active_plugins[ $basename ] ) &&
7431
+ $site_active_plugins[ $basename ]['is_active'] );
7432
+
7433
+ if ( ! isset( $site_active_plugins_cache->plugins[ $basename ] ) &&
7434
+ isset( $site_active_plugins[ $basename ] )
7435
+ ) {
7436
+ // Plugin was site level activated.
7437
+ $site_active_plugins_cache->plugins[ $basename ] = $network_plugins[ $basename ];
7438
+ $site_active_plugins_cache->plugins[ $basename ]['is_active'] = true;
7439
+ } else if ( isset( $site_active_plugins_cache->plugins[ $basename ] ) &&
7440
+ ! isset( $site_active_plugins[ $basename ] )
7441
+ ) {
7442
+ // Plugin was site level deactivated.
7443
+ unset( $site_active_plugins_cache->plugins[ $basename ] );
7444
+ }
7445
+
7446
+ $prev_version = $data['version'];
7447
+ $current_version = $network_plugins[ $basename ]['Version'];
7448
+
7449
+ if ( $was_active !== $is_active || $prev_version !== $current_version ) {
7450
+ // Plugin activated or deactivated, or version changed.
7451
+
7452
+ if ( $was_active !== $is_active ) {
7453
+ if ( $data['is_active'] != $network_plugins[ $basename ]['is_active'] ) {
7454
+ $network_plugins_cache->plugins[ $basename ]['is_active'] = $data['is_active'];
7455
+ }
7456
+ }
7457
+
7458
+ if ( $prev_version !== $current_version ) {
7459
+ $network_plugins_cache->plugins[ $basename ]['Version'] = $current_version;
7460
+ }
7461
+
7462
+ $updated_plugin_data = $data;
7463
+ $updated_plugin_data['is_active'] = $is_active;
7464
+ $updated_plugin_data['version'] = $current_version;
7465
+ $updated_plugin_data['title'] = $network_plugins[ $basename ]['Name'];
7466
+ $plugins_update_data[] = $updated_plugin_data;
7467
+ }
7468
+ }
7469
+
7470
+ // Find new plugins that weren't yet seen before.
7471
+ foreach ( $network_plugins as $basename => $data ) {
7472
+ if ( ! isset( $network_plugins_cache->plugins[ $basename ] ) ) {
7473
+ // New plugin.
7474
+ $new_plugin = array(
7475
+ 'slug' => $data['slug'],
7476
+ 'version' => $data['Version'],
7477
+ 'title' => $data['Name'],
7478
+ 'is_active' => $data['is_active'],
7479
+ 'is_uninstalled' => false,
7480
+ );
7481
+
7482
+ $plugins_update_data[] = $new_plugin;
7483
+ $network_plugins_cache->plugins[ $basename ] = $new_plugin;
7484
+
7485
+ if ( isset( $site_active_plugins[ $basename ] ) ) {
7486
+ $site_active_plugins_cache->plugins[ $basename ] = $new_plugin;
7487
+ $site_active_plugins_cache->plugins[ $basename ]['is_active'] = true;
7488
+ }
7489
+ }
7490
+ }
7491
+
7492
+ $site_active_plugins_cache->md5 = $site_active_plugins_thumbprint;
7493
+ $site_active_plugins_cache->timestamp = $time;
7494
+ self::$_accounts->set_option( $site_active_plugins_option_name, $site_active_plugins_cache, true );
7495
+
7496
+ $network_plugins_cache->md5 = $network_plugins_thumbprint;
7497
+ $network_plugins_cache->timestamp = $time;
7498
+ self::$_accounts->set_option( $network_plugins_option_name, $network_plugins_cache, true );
7499
+
7500
+ return $plugins_update_data;
7501
+ }
7502
+
7503
+ /**
7504
+ * Return a list of modified themes since the last sync.
7505
+ *
7506
+ * Note:
7507
+ * There's no point to store a themes counter since even if the number of
7508
+ * themes didn't change, we still need to check if the versions are all the
7509
+ * same and the activity state is similar.
7510
+ *
7511
+ * @author Vova Feldman (@svovaf)
7512
+ * @since 1.1.8
7513
+ *
7514
+ * @return array|false
7515
+ */
7516
+ private function get_themes_data_for_api() {
7517
+ // Alias.
7518
+ $option_name = 'all_themes';
7519
+
7520
+ $all_cached_themes = self::$_accounts->get_option( $option_name );
7521
+
7522
+ if ( ! is_object( $all_cached_themes ) ) {
7523
+ $all_cached_themes = (object) array(
7524
+ 'timestamp' => '',
7525
+ 'md5' => '',
7526
+ 'themes' => array(),
7527
+ );
7528
+ }
7529
+
7530
+ $time = time();
7531
+
7532
+ if ( ! empty( $all_cached_themes->timestamp ) &&
7533
+ ( $time - $all_cached_themes->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC
7534
+ ) {
7535
+ // Don't send theme updates if last update was in the past 5 min.
7536
+ return false;
7537
+ }
7538
+
7539
+ // Write timestamp to lock the logic.
7540
+ $all_cached_themes->timestamp = $time;
7541
+ self::$_accounts->set_option( $option_name, $all_cached_themes, true );
7542
+
7543
+ // Reload options from DB.
7544
+ self::$_accounts->load( true );
7545
+ $all_cached_themes = self::$_accounts->get_option( $option_name );
7546
+
7547
+ if ( $time != $all_cached_themes->timestamp ) {
7548
+ // If timestamp is different, then another thread captured the lock.
7549
+ return false;
7550
+ }
7551
+
7552
+ // Get active theme.
7553
+ $active_theme = wp_get_theme();
7554
+ $active_theme_stylesheet = $active_theme->get_stylesheet();
7555
+
7556
+ // Check if there's a change in themes.
7557
+ $all_themes = wp_get_themes();
7558
+
7559
+ // Check if themes changed.
7560
+ ksort( $all_themes );
7561
+
7562
+ $themes_signature = '';
7563
+ foreach ( $all_themes as $slug => $data ) {
7564
+ $is_active = ( $slug === $active_theme_stylesheet );
7565
+ $themes_signature .= $slug . ',' .
7566
+ $data->version . ',' .
7567
+ ( $is_active ? '1' : '0' ) . ';';
7568
+ }
7569
+
7570
+ // Check if themes status changed (version or active/inactive).
7571
+ $themes_changed = ( $all_cached_themes->md5 !== md5( $themes_signature ) );
7572
+
7573
+ $themes_update_data = array();
7574
+
7575
+ if ( $themes_changed ) {
7576
+ // Change in themes, report changes.
7577
+
7578
+ // Update existing themes info.
7579
+ foreach ( $all_cached_themes->themes as $slug => $data ) {
7580
+ $is_active = ( $slug === $active_theme_stylesheet );
7581
+
7582
+ if ( ! isset( $all_themes[ $slug ] ) ) {
7583
+ // Plugin uninstalled.
7584
+ $uninstalled_theme_data = $data;
7585
+ $uninstalled_theme_data['is_active'] = false;
7586
+ $uninstalled_theme_data['is_uninstalled'] = true;
7587
+ $themes_update_data[] = $uninstalled_theme_data;
7588
+
7589
+ unset( $all_themes[ $slug ] );
7590
+ unset( $all_cached_themes->themes[ $slug ] );
7591
+ } else if ( $data['is_active'] !== $is_active ||
7592
+ $data['version'] !== $all_themes[ $slug ]->version
7593
+ ) {
7594
+ // Plugin activated or deactivated, or version changed.
7595
+
7596
+ $all_cached_themes->themes[ $slug ]['is_active'] = $is_active;
7597
+ $all_cached_themes->themes[ $slug ]['version'] = $all_themes[ $slug ]->version;
7598
+
7599
+ $themes_update_data[] = $all_cached_themes->themes[ $slug ];
7600
+ }
7601
+ }
7602
+
7603
+ // Find new themes that weren't yet seen before.
7604
+ foreach ( $all_themes as $slug => $data ) {
7605
+ if ( ! isset( $all_cached_themes->themes[ $slug ] ) ) {
7606
+ $is_active = ( $slug === $active_theme_stylesheet );
7607
+
7608
+ // New plugin.
7609
+ $new_plugin = array(
7610
+ 'slug' => $slug,
7611
+ 'version' => $data->version,
7612
+ 'title' => $data->name,
7613
+ 'is_active' => $is_active,
7614
+ 'is_uninstalled' => false,
7615
+ );
7616
+
7617
+ $themes_update_data[] = $new_plugin;
7618
+ $all_cached_themes->themes[ $slug ] = $new_plugin;
7619
+ }
7620
+ }
7621
+
7622
+ $all_cached_themes->md5 = md5( $themes_signature );
7623
+ $all_cached_themes->timestamp = time();
7624
+ self::$_accounts->set_option( $option_name, $all_cached_themes, true );
7625
+ }
7626
+
7627
+ return $themes_update_data;
7628
+ }
7629
+
7630
+ /**
7631
+ * Get site data for API install request.
7632
+ *
7633
+ * @author Vova Feldman (@svovaf)
7634
+ * @since 1.1.2
7635
+ *
7636
+ * @param string[] string $override
7637
+ * @param bool $include_plugins Since 1.1.8 by default include plugin changes.
7638
+ * @param bool $include_themes Since 1.1.8 by default include plugin changes.
7639
+ *
7640
+ * @return array
7641
+ */
7642
+ private function get_install_data_for_api(
7643
+ array $override,
7644
+ $include_plugins = true,
7645
+ $include_themes = true
7646
+ ) {
7647
+ /**
7648
+ * @since 1.1.8 Also send plugin updates.
7649
+ */
7650
+ if ( $include_plugins && ! isset( $override['plugins'] ) ) {
7651
+ $plugins = $this->get_plugins_data_for_api();
7652
+ if ( ! empty( $plugins ) ) {
7653
+ $override['plugins'] = $plugins;
7654
+ }
7655
+ }
7656
+ /**
7657
+ * @since 1.1.8 Also send themes updates.
7658
+ */
7659
+ if ( $include_themes && ! isset( $override['themes'] ) ) {
7660
+ $themes = $this->get_themes_data_for_api();
7661
+ if ( ! empty( $themes ) ) {
7662
+ $override['themes'] = $themes;
7663
+ }
7664
+ }
7665
+
7666
+ return array_merge( array(
7667
+ 'version' => $this->get_plugin_version(),
7668
+ 'is_premium' => $this->is_premium(),
7669
+ 'language' => get_bloginfo( 'language' ),
7670
+ 'charset' => get_bloginfo( 'charset' ),
7671
+ 'platform_version' => get_bloginfo( 'version' ),
7672
+ 'sdk_version' => $this->version,
7673
+ 'programming_language_version' => phpversion(),
7674
+ 'title' => get_bloginfo( 'name' ),
7675
+ 'url' => get_site_url(),
7676
+ // Special params.
7677
+ 'is_active' => true,
7678
+ 'is_disconnected' => $this->is_tracking_prohibited(),
7679
+ 'is_uninstalled' => false,
7680
+ ), $override );
7681
+ }
7682
+
7683
+ /**
7684
+ * Update installs details.
7685
+ *
7686
+ * @todo V1 of multiste network support doesn't support plugin and theme data sending.
7687
+ *
7688
+ * @author Vova Feldman (@svovaf)
7689
+ * @since 2.0.0
7690
+ *
7691
+ * @param string[] string $override
7692
+ * @param bool $only_diff
7693
+ * @param bool $include_plugins Since 1.1.8 by default include plugin changes.
7694
+ * @param bool $include_themes Since 1.1.8 by default include plugin changes.
7695
+ *
7696
+ * @return array
7697
+ */
7698
+ private function get_installs_data_for_api(
7699
+ array $override,
7700
+ $only_diff = false,
7701
+ $include_plugins = true,
7702
+ $include_themes = true
7703
+ ) {
7704
+ /**
7705
+ * @since 1.1.8 Also send plugin updates.
7706
+ */
7707
+ // if ( $include_plugins && ! isset( $override['plugins'] ) ) {
7708
+ // $plugins = $this->get_plugins_data_for_api();
7709
+ // if ( ! empty( $plugins ) ) {
7710
+ // $override['plugins'] = $plugins;
7711
+ // }
7712
+ // }
7713
+ /**
7714
+ * @since 1.1.8 Also send themes updates.
7715
+ */
7716
+ // if ( $include_themes && ! isset( $override['themes'] ) ) {
7717
+ // $themes = $this->get_themes_data_for_api();
7718
+ // if ( ! empty( $themes ) ) {
7719
+ // $override['themes'] = $themes;
7720
+ // }
7721
+ // }
7722
+
7723
+ // Common properties.
7724
+ $common = array_merge( array(
7725
+ 'version' => $this->get_plugin_version(),
7726
+ 'is_premium' => $this->is_premium(),
7727
+ 'sdk_version' => $this->version,
7728
+ 'programming_language_version' => phpversion(),
7729
+ 'platform_version' => get_bloginfo( 'version' ),
7730
+ ), $override );
7731
+
7732
+
7733
+ $is_common_diff_for_any_site = false;
7734
+ $common_diff_union = array();
7735
+
7736
+ $installs_data = array();
7737
+
7738
+ $sites = self::get_sites();
7739
+
7740
+ foreach ( $sites as $site ) {
7741
+ $blog_id = self::get_site_blog_id( $site );
7742
+
7743
+ $install = $this->get_install_by_blog_id( $blog_id );
7744
+
7745
+ if ( is_object( $install ) ) {
7746
+ if ( $install->user_id != $this->_user->id ) {
7747
+ // Install belongs to a different owner.
7748
+ continue;
7749
+ }
7750
+
7751
+ if ( ! $this->is_premium() && $install->is_tracking_prohibited() ) {
7752
+ // Don't send updates regarding opted-out installs.
7753
+ continue;
7754
+ }
7755
+
7756
+ $install_data = $this->get_site_info( $site );
7757
+
7758
+ $uid = $install_data['uid'];
7759
+
7760
+ unset( $install_data['blog_id'] );
7761
+ unset( $install_data['uid'] );
7762
+
7763
+ $install_data['is_disconnected'] = $install->is_disconnected;
7764
+ $install_data['is_active'] = $this->is_active_for_site( $blog_id );
7765
+ $install_data['is_uninstalled'] = $install->is_uninstalled;
7766
+
7767
+ $common_diff = null;
7768
+ $is_common_diff = false;
7769
+ if ( $only_diff ) {
7770
+ $install_data = $this->get_install_diff_for_api( $install_data, $install, $override );
7771
+ $common_diff = $this->get_install_diff_for_api( $common, $install, $override );
7772
+
7773
+ $is_common_diff = ! empty( $common_diff );
7774
+
7775
+ if ( $is_common_diff ) {
7776
+ foreach ( $common_diff as $k => $v ) {
7777
+ if ( ! isset( $common_diff_union[ $k ] ) ) {
7778
+ $common_diff_union[ $k ] = $v;
7779
+ }
7780
+ }
7781
+ }
7782
+
7783
+ $is_common_diff_for_any_site = $is_common_diff_for_any_site || $is_common_diff;
7784
+ }
7785
+
7786
+ if ( ! empty( $install_data ) || $is_common_diff ) {
7787
+ // Add install ID and site unique ID.
7788
+ $install_data['id'] = $install->id;
7789
+ $install_data['uid'] = $uid;
7790
+
7791
+ $installs_data[] = $install_data;
7792
+ }
7793
+ }
7794
+ }
7795
+
7796
+ restore_current_blog();
7797
+
7798
+ if ( 0 < count( $installs_data ) && ( $is_common_diff_for_any_site || ! $only_diff ) ) {
7799
+ if ( ! $only_diff ) {
7800
+ $installs_data[] = $common;
7801
+ } else if ( ! empty( $common_diff_union ) ) {
7802
+ $installs_data[] = $common_diff_union;
7803
+ }
7804
+ }
7805
+
7806
+ foreach ( $installs_data as &$data ) {
7807
+ $data = (object) $data;
7808
+ }
7809
+
7810
+ return $installs_data;
7811
+ }
7812
+
7813
+ /**
7814
+ * Compare site actual data to the stored install data and return the differences for an API data sync.
7815
+ *
7816
+ * @author Vova Feldman (@svovaf)
7817
+ * @since 2.0.0
7818
+ *
7819
+ * @param array $site
7820
+ * @param FS_Site $install
7821
+ * @param string[] string $override
7822
+ *
7823
+ * @return array
7824
+ */
7825
+ private function get_install_diff_for_api( $site, $install, $override = array() ) {
7826
+ $diff = array();
7827
+ $special = array();
7828
+ $special_override = false;
7829
+
7830
+ foreach ( $site as $p => $v ) {
7831
+ if ( property_exists( $install, $p ) ) {
7832
+ if ( ( is_bool( $install->{$p} ) || ! empty( $install->{$p} ) ) &&
7833
+ $install->{$p} != $v
7834
+ ) {
7835
+ $install->{$p} = $v;
7836
+ $diff[ $p ] = $v;
7837
+ }
7838
+ } else {
7839
+ $special[ $p ] = $v;
7840
+
7841
+ if ( isset( $override[ $p ] ) ||
7842
+ 'plugins' === $p ||
7843
+ 'themes' === $p
7844
+ ) {
7845
+ $special_override = true;
7846
+ }
7847
+ }
7848
+ }
7849
+
7850
+ if ( $special_override || 0 < count( $diff ) ) {
7851
+ // Add special params only if has at least one
7852
+ // standard param, or if explicitly requested to
7853
+ // override a special param or a param which is not exist
7854
+ // in the install object.
7855
+ $diff = array_merge( $diff, $special );
7856
+ }
7857
+
7858
+ return $diff;
7859
+ }
7860
+
7861
+ /**
7862
+ * Update install only if changed.
7863
+ *
7864
+ * @author Vova Feldman (@svovaf)
7865
+ * @since 1.0.9
7866
+ *
7867
+ * @param string[] string $override
7868
+ * @param bool $flush
7869
+ *
7870
+ * @return false|object|string
7871
+ */
7872
+ private function send_install_update( $override = array(), $flush = false ) {
7873
+ $this->_logger->entrance();
7874
+
7875
+ $check_properties = $this->get_install_data_for_api( $override );
7876
+
7877
+ if ( $flush ) {
7878
+ $params = $check_properties;
7879
+ } else {
7880
+ $params = $this->get_install_diff_for_api( $check_properties, $this->_site, $override );
7881
+ }
7882
+
7883
+ if ( 0 < count( $params ) ) {
7884
+ if ( ! is_multisite() ) {
7885
+ // Update last install sync timestamp.
7886
+ $this->set_cron_execution_timestamp( 'install_sync' );
7887
+ }
7888
+
7889
+ $params['uid'] = $this->get_anonymous_id();
7890
+
7891
+ // Send updated values to FS.
7892
+ $site = $this->get_api_site_scope()->call( '/', 'put', $params );
7893
+
7894
+ if ( $this->is_api_result_entity( $site ) ) {
7895
+ if ( ! is_multisite() ) {
7896
+ // I successfully sent install update, clear scheduled sync if exist.
7897
+ $this->clear_install_sync_cron();
7898
+ }
7899
+ }
7900
+
7901
+ return $site;
7902
+ }
7903
+
7904
+ return false;
7905
+ }
7906
+
7907
+ /**
7908
+ * Update installs only if changed.
7909
+ *
7910
+ * @author Vova Feldman (@svovaf)
7911
+ * @since 2.0.0
7912
+ *
7913
+ * @param string[] string $override
7914
+ * @param bool $flush
7915
+ *
7916
+ * @return false|object|string
7917
+ */
7918
+ private function send_installs_update( $override = array(), $flush = false ) {
7919
+ $this->_logger->entrance();
7920
+
7921
+ $installs_data = $this->get_installs_data_for_api( $override, ! $flush );
7922
+
7923
+ if ( empty( $installs_data ) ) {
7924
+ return false;
7925
+ }
7926
+
7927
+ // Update last install sync timestamp.
7928
+ $this->set_cron_execution_timestamp( 'install_sync' );
7929
+
7930
+ // Send updated values to FS.
7931
+ $result = $this->get_api_user_scope()->call( "/plugins/{$this->_plugin->id}/installs.json", 'put', $installs_data );
7932
+
7933
+ if ( $this->is_api_result_object( $result, 'installs' ) ) {
7934
+ // I successfully sent installs update, clear scheduled sync if exist.
7935
+ $this->clear_install_sync_cron();
7936
+ }
7937
+
7938
+ return $result;
7939
+ }
7940
+
7941
+ /**
7942
+ * Update install only if changed.
7943
+ *
7944
+ * @author Vova Feldman (@svovaf)
7945
+ * @since 1.0.9
7946
+ *
7947
+ * @param string[] string $override
7948
+ * @param bool $flush
7949
+ */
7950
+ private function sync_install( $override = array(), $flush = false ) {
7951
+ $this->_logger->entrance();
7952
+
7953
+ $site = $this->send_install_update( $override, $flush );
7954
+
7955
+ if ( false === $site ) {
7956
+ // No sync required.
7957
+ return;
7958
+ }
7959
+
7960
+ if ( ! $this->is_api_result_entity( $site ) ) {
7961
+ // Failed to sync, don't update locally.
7962
+ return;
7963
+ }
7964
+
7965
+ $this->_site = new FS_Site( $site );
7966
+
7967
+ $this->_store_site( true );
7968
+ }
7969
+
7970
+ /**
7971
+ * Update install only if changed.
7972
+ *
7973
+ * @author Vova Feldman (@svovaf)
7974
+ * @since 1.0.9
7975
+ *
7976
+ * @param string[] string $override
7977
+ * @param bool $flush
7978
+ */
7979
+ private function sync_installs( $override = array(), $flush = false ) {
7980
+ $this->_logger->entrance();
7981
+
7982
+ $result = $this->send_installs_update( $override, $flush );
7983
+
7984
+ if ( false === $result ) {
7985
+ // No sync required.
7986
+ return;
7987
+ }
7988
+
7989
+ if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
7990
+ // Failed to sync, don't update locally.
7991
+ return;
7992
+ }
7993
+
7994
+ $address_to_blog_map = $this->get_address_to_blog_map();
7995
+
7996
+ foreach ( $result->installs as $install ) {
7997
+ $this->_site = new FS_Site( $install );
7998
+
7999
+ $address = trailingslashit( fs_strip_url_protocol( $install->url ) );
8000
+ $blog_id = $address_to_blog_map[ $address ];
8001
+
8002
+ $this->_store_site( true, $blog_id );
8003
+ }
8004
+ }
8005
+
8006
+ /**
8007
+ * Track install's custom event.
8008
+ *
8009
+ * IMPORTANT:
8010
+ * Custom event tracking is currently only supported for specific clients.
8011
+ * If you are not one of them, please don't use this method. If you will,
8012
+ * the API will simply ignore your request based on the plugin ID.
8013
+ *
8014
+ * Need custom tracking for your plugin or theme?
8015
+ * If you are interested in custom event tracking please contact yo@freemius.com
8016
+ * for further details.
8017
+ *
8018
+ * @author Vova Feldman (@svovaf)
8019
+ * @since 1.2.1
8020
+ *
8021
+ * @param string $name Event name.
8022
+ * @param array $properties Associative key/value array with primitive values only
8023
+ * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s.
8024
+ * @param bool $once If true, event will be tracked only once. IMPORTANT: Still trigger the API call.
8025
+ *
8026
+ * @return object|false Event data or FALSE on failure.
8027
+ *
8028
+ * @throws \Freemius_InvalidArgumentException
8029
+ */
8030
+ public function track_event( $name, $properties = array(), $process_at = false, $once = false ) {
8031
+ $this->_logger->entrance( http_build_query( array( 'name' => $name, 'once' => $once ) ) );
8032
+
8033
+ if ( ! $this->is_registered() ) {
8034
+ return false;
8035
+ }
8036
+
8037
+ $event = array( 'type' => $name );
8038
+
8039
+ if ( is_numeric( $process_at ) && $process_at > time() ) {
8040
+ $event['process_at'] = $process_at;
8041
+ }
8042
+
8043
+ if ( $once ) {
8044
+ $event['once'] = true;
8045
+ }
8046
+
8047
+ if ( ! empty( $properties ) ) {
8048
+ // Verify associative array values are primitive.
8049
+ foreach ( $properties as $k => $v ) {
8050
+ if ( ! is_scalar( $v ) ) {
8051
+ throw new Freemius_InvalidArgumentException( 'The $properties argument must be an associative key/value array with primitive values only.' );
8052
+ }
8053
+ }
8054
+
8055
+ $event['properties'] = $properties;
8056
+ }
8057
+
8058
+ $result = $this->get_api_site_scope()->call( 'events.json', 'post', $event );
8059
+
8060
+ return $this->is_api_error( $result ) ?
8061
+ false :
8062
+ $result;
8063
+ }
8064
+
8065
+ /**
8066
+ * Track install's custom event only once, but it still triggers the API call.
8067
+ *
8068
+ * IMPORTANT:
8069
+ * Custom event tracking is currently only supported for specific clients.
8070
+ * If you are not one of them, please don't use this method. If you will,
8071
+ * the API will simply ignore your request based on the plugin ID.
8072
+ *
8073
+ * Need custom tracking for your plugin or theme?
8074
+ * If you are interested in custom event tracking please contact yo@freemius.com
8075
+ * for further details.
8076
+ *
8077
+ * @author Vova Feldman (@svovaf)
8078
+ * @since 1.2.1
8079
+ *
8080
+ * @param string $name Event name.
8081
+ * @param array $properties Associative key/value array with primitive values only
8082
+ * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s.
8083
+ *
8084
+ * @return object|false Event data or FALSE on failure.
8085
+ *
8086
+ * @throws \Freemius_InvalidArgumentException
8087
+ *
8088
+ * @user Freemius::track_event()
8089
+ */
8090
+ public function track_event_once( $name, $properties = array(), $process_at = false ) {
8091
+ return $this->track_event( $name, $properties, $process_at, true );
8092
+ }
8093
+
8094
+ /**
8095
+ * Plugin uninstall hook.
8096
+ *
8097
+ * @author Vova Feldman (@svovaf)
8098
+ * @since 1.0.1
8099
+ *
8100
+ * @param bool $check_user Enforce checking if user have plugins activation privileges.
8101
+ */
8102
+ function _uninstall_plugin_event( $check_user = true ) {
8103
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
8104
+
8105
+ if ( $check_user && ! current_user_can( 'activate_plugins' ) ) {
8106
+ return;
8107
+ }
8108
+
8109
+ $params = array();
8110
+ $uninstall_reason = null;
8111
+ if ( isset( $this->_storage->uninstall_reason ) ) {
8112
+ $uninstall_reason = $this->_storage->uninstall_reason;
8113
+ $params['reason_id'] = $uninstall_reason->id;
8114
+ $params['reason_info'] = $uninstall_reason->info;
8115
+ }
8116
+
8117
+ if ( ! $this->is_registered() ) {
8118
+ // Send anonymous uninstall event only if user submitted a feedback.
8119
+ if ( isset( $uninstall_reason ) ) {
8120
+ if ( isset( $uninstall_reason->is_anonymous ) && ! $uninstall_reason->is_anonymous ) {
8121
+ $this->opt_in( false, false, false, false, true );
8122
+ } else {
8123
+ $params['uid'] = $this->get_anonymous_id();
8124
+ $this->get_api_plugin_scope()->call( 'uninstall.json', 'put', $params );
8125
+ }
8126
+ }
8127
+ } else {
8128
+ $params = array_merge( $params, array(
8129
+ 'is_active' => false,
8130
+ 'is_uninstalled' => true,
8131
+ ) );
8132
+
8133
+ if ( $this->_is_network_active ) {
8134
+ // Send uninstall event.
8135
+ $this->send_installs_update( $params );
8136
+ } else {
8137
+ // Send uninstall event.
8138
+ $this->send_install_update( $params );
8139
+ }
8140
+ }
8141
+
8142
+ // @todo Decide if we want to delete plugin information from db.
8143
+ }
8144
+
8145
+ /**
8146
+ * @author Vova Feldman (@svovaf)
8147
+ * @since 1.1.1
8148
+ *
8149
+ * @return string
8150
+ */
8151
+ function premium_plugin_basename() {
8152
+ return "{$this->_slug}-premium/" . basename( $this->_free_plugin_basename );
8153
+ }
8154
+
8155
+ /**
8156
+ * Uninstall plugin hook. Called only when connected his account with Freemius for active sites tracking.
8157
+ *
8158
+ * @author Vova Feldman (@svovaf)
8159
+ * @since 1.0.2
8160
+ */
8161
+ public static function _uninstall_plugin_hook() {
8162
+ self::_load_required_static();
8163
+
8164
+ self::$_static_logger->entrance();
8165
+
8166
+ if ( ! current_user_can( 'activate_plugins' ) ) {
8167
+ return;
8168
+ }
8169
+
8170
+ $plugin_file = substr( current_filter(), strlen( 'uninstall_' ) );
8171
+
8172
+ self::$_static_logger->info( 'plugin = ' . $plugin_file );
8173
+
8174
+ define( 'WP_FS__UNINSTALL_MODE', true );
8175
+
8176
+ $fs = self::get_instance_by_file( $plugin_file );
8177
+
8178
+ if ( is_object( $fs ) ) {
8179
+ self::require_plugin_essentials();
8180
+
8181
+ if ( is_plugin_active( $fs->_free_plugin_basename ) ||
8182
+ is_plugin_active( $fs->premium_plugin_basename() )
8183
+ ) {
8184
+ // Deleting Free or Premium plugin version while the other version still installed.
8185
+ return;
8186
+ }
8187
+
8188
+ $fs->_uninstall_plugin_event();
8189
+
8190
+ $fs->do_action( 'after_uninstall' );
8191
+ }
8192
+ }
8193
+
8194
+ #----------------------------------------------------------------------------------
8195
+ #region Plugin Information
8196
+ #----------------------------------------------------------------------------------
8197
+
8198
+ /**
8199
+ * Load WordPress core plugin.php essential module.
8200
+ *
8201
+ * @author Vova Feldman (@svovaf)
8202
+ * @since 1.1.1
8203
+ */
8204
+ private static function require_plugin_essentials() {
8205
+ if ( ! function_exists( 'get_plugins' ) ) {
8206
+ self::$_static_logger->log( 'Including wp-admin/includes/plugin.php...' );
8207
+
8208
+ require_once ABSPATH . 'wp-admin/includes/plugin.php';
8209
+ }
8210
+ }
8211
+
8212
+ /**
8213
+ * Load WordPress core pluggable.php module.
8214
+ *
8215
+ * @author Vova Feldman (@svovaf)
8216
+ * @since 1.1.2
8217
+ */
8218
+ private static function require_pluggable_essentials() {
8219
+ if ( ! function_exists( 'wp_get_current_user' ) ) {
8220
+ require_once ABSPATH . 'wp-includes/pluggable.php';
8221
+ }
8222
+ }
8223
+
8224
+ /**
8225
+ * Return plugin data.
8226
+ *
8227
+ * @author Vova Feldman (@svovaf)
8228
+ * @since 1.0.1
8229
+ *
8230
+ * @return array
8231
+ */
8232
+ function get_plugin_data() {
8233
+ if ( ! isset( $this->_plugin_data ) ) {
8234
+ self::require_plugin_essentials();
8235
+
8236
+ if ( $this->is_plugin() ) {
8237
+ /**
8238
+ * @author Vova Feldman (@svovaf)
8239
+ * @since 1.2.0 When using get_plugin_data() do NOT translate plugin data.
8240
+ *
8241
+ * @link https://github.com/Freemius/wordpress-sdk/issues/77
8242
+ */
8243
+ $plugin_data = get_plugin_data(
8244
+ $this->_plugin_main_file_path,
8245
+ false,
8246
+ false
8247
+ );
8248
+ } else {
8249
+ $theme_data = wp_get_theme();
8250
+
8251
+ if ( $this->_plugin_basename !== $theme_data->get_stylesheet() && is_child_theme() ) {
8252
+ $parent_theme = $theme_data->parent();
8253
+
8254
+ if ( ( $parent_theme instanceof WP_Theme ) && $this->_plugin_basename === $parent_theme->get_stylesheet() ) {
8255
+ $theme_data = $parent_theme;
8256
+ }
8257
+ }
8258
+
8259
+ $plugin_data = array(
8260
+ 'Name' => $theme_data->get( 'Name' ),
8261
+ 'Version' => $theme_data->get( 'Version' ),
8262
+ 'Author' => $theme_data->get( 'Author' ),
8263
+ 'Description' => $theme_data->get( 'Description' ),
8264
+ 'PluginURI' => $theme_data->get( 'ThemeURI' ),
8265
+ );
8266
+ }
8267
+
8268
+ $this->_plugin_data = $plugin_data;
8269
+ }
8270
+
8271
+ return $this->_plugin_data;
8272
+ }
8273
+
8274
+ /**
8275
+ * @author Vova Feldman (@svovaf)
8276
+ * @since 1.0.1
8277
+ * @since 1.2.2.5 If slug not set load slug by module ID.
8278
+ *
8279
+ * @return string Plugin slug.
8280
+ */
8281
+ function get_slug() {
8282
+ if ( ! isset( $this->_slug ) ) {
8283
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
8284
+ $this->_slug = $id_slug_type_path_map[ $this->_module_id ]['slug'];
8285
+ }
8286
+
8287
+ return $this->_slug;
8288
+ }
8289
+
8290
+ /**
8291
+ * @author Vova Feldman (@svovaf)
8292
+ * @since 1.2.1.7
8293
+ *
8294
+ * @return string Plugin slug.
8295
+ */
8296
+ function get_target_folder_name() {
8297
+ return $this->_slug . ( $this->can_use_premium_code() ? '-premium' : '' );
8298
+ }
8299
+
8300
+ /**
8301
+ * @author Vova Feldman (@svovaf)
8302
+ * @since 1.0.1
8303
+ *
8304
+ * @return number Plugin ID.
8305
+ */
8306
+ function get_id() {
8307
+ return $this->_plugin->id;
8308
+ }
8309
+
8310
+ /**
8311
+ * @author Vova Feldman (@svovaf)
8312
+ * @since 1.2.1.5
8313
+ *
8314
+ * @return string Freemius SDK version
8315
+ */
8316
+ function get_sdk_version() {
8317
+ return $this->version;
8318
+ }
8319
+
8320
+ /**
8321
+ * @author Vova Feldman (@svovaf)
8322
+ * @since 1.2.1.5
8323
+ *
8324
+ * @return number Parent plugin ID (if parent exist).
8325
+ */
8326
+ function get_parent_id() {
8327
+ return $this->is_addon() ?
8328
+ $this->get_parent_instance()->get_id() :
8329
+ $this->_plugin->id;
8330
+ }
8331
+
8332
+ /**
8333
+ * @author Vova Feldman (@svovaf)
8334
+ * @since 1.0.1
8335
+ *
8336
+ * @return string Plugin public key.
8337
+ */
8338
+ function get_public_key() {
8339
+ return $this->_plugin->public_key;
8340
+ }
8341
+
8342
+ /**
8343
+ * Will be available only on sandbox mode.
8344
+ *
8345
+ * @author Vova Feldman (@svovaf)
8346
+ * @since 1.0.4
8347
+ *
8348
+ * @return mixed Plugin secret key.
8349
+ */
8350
+ function get_secret_key() {
8351
+ return $this->_plugin->secret_key;
8352
+ }
8353
+
8354
+ /**
8355
+ * @author Vova Feldman (@svovaf)
8356
+ * @since 1.1.1
8357
+ *
8358
+ * @return bool
8359
+ */
8360
+ function has_secret_key() {
8361
+ return ! empty( $this->_plugin->secret_key );
8362
+ }
8363
+
8364
+ /**
8365
+ * @author Vova Feldman (@svovaf)
8366
+ * @since 1.0.9
8367
+ *
8368
+ * @return string
8369
+ */
8370
+ function get_plugin_name() {
8371
+ $this->_logger->entrance();
8372
+
8373
+ if ( ! isset( $this->_plugin_name ) ) {
8374
+ $plugin_data = $this->get_plugin_data();
8375
+
8376
+ // Get name.
8377
+ $this->_plugin_name = $plugin_data['Name'];
8378
+
8379
+ // Check if plugin name contains "(Premium)" suffix and remove it.
8380
+ $suffix = ' (premium)';
8381
+ $suffix_len = strlen( $suffix );
8382
+
8383
+ if ( strlen( $plugin_data['Name'] ) > $suffix_len &&
8384
+ $suffix === substr( strtolower( $plugin_data['Name'] ), - $suffix_len )
8385
+ ) {
8386
+ $this->_plugin_name = substr( $plugin_data['Name'], 0, - $suffix_len );
8387
+ }
8388
+
8389
+ $this->_logger->departure( 'Name = ' . $this->_plugin_name );
8390
+ }
8391
+
8392
+ return $this->_plugin_name;
8393
+ }
8394
+
8395
+ /**
8396
+ * @author Vova Feldman (@svovaf)
8397
+ * @since 1.0.0
8398
+ *
8399
+ * @return string
8400
+ */
8401
+ function get_plugin_version() {
8402
+ $this->_logger->entrance();
8403
+
8404
+ $plugin_data = $this->get_plugin_data();
8405
+
8406
+ $this->_logger->departure( 'Version = ' . $plugin_data['Version'] );
8407
+
8408
+ return $this->apply_filters( 'plugin_version', $plugin_data['Version'] );
8409
+ }
8410
+
8411
+ /**
8412
+ * @author Vova Feldman (@svovaf)
8413
+ * @since 1.2.1.7
8414
+ *
8415
+ * @return string
8416
+ */
8417
+ function get_plugin_title() {
8418
+ $this->_logger->entrance();
8419
+
8420
+ $title = $this->_plugin->title;
8421
+
8422
+ return $this->apply_filters( 'plugin_title', $title );
8423
+ }
8424
+
8425
+ /**
8426
+ * @author Vova Feldman (@svovaf)
8427
+ * @since 1.2.2.7
8428
+ *
8429
+ * @param bool $lowercase
8430
+ *
8431
+ * @return string
8432
+ */
8433
+ function get_module_label( $lowercase = false ) {
8434
+ $label = $this->is_addon() ?
8435
+ $this->get_text_inline( 'Add-On', 'addon' ) :
8436
+ ( $this->is_plugin() ?
8437
+ $this->get_text_inline( 'Plugin', 'plugin' ) :
8438
+ $this->get_text_inline( 'Theme', 'theme' ) );
8439
+
8440
+ if ( $lowercase ) {
8441
+ $label = strtolower( $label );
8442
+ }
8443
+
8444
+ return $label;
8445
+ }
8446
+
8447
+ /**
8448
+ * @author Vova Feldman (@svovaf)
8449
+ * @since 1.0.4
8450
+ *
8451
+ * @return string
8452
+ */
8453
+ function get_plugin_basename() {
8454
+ if ( ! isset( $this->_plugin_basename ) ) {
8455
+ if ( $this->is_plugin() ) {
8456
+ $this->_plugin_basename = plugin_basename( $this->_plugin_main_file_path );
8457
+ } else {
8458
+ $this->_plugin_basename = basename( dirname( $this->_plugin_main_file_path ) );
8459
+ }
8460
+ }
8461
+
8462
+ return $this->_plugin_basename;
8463
+ }
8464
+
8465
+ function get_plugin_folder_name() {
8466
+ $this->_logger->entrance();
8467
+
8468
+ $plugin_folder = $this->_plugin_basename;
8469
+
8470
+ while ( '.' !== dirname( $plugin_folder ) ) {
8471
+ $plugin_folder = dirname( $plugin_folder );
8472
+ }
8473
+
8474
+ $this->_logger->departure( 'Folder Name = ' . $plugin_folder );
8475
+
8476
+ return $plugin_folder;
8477
+ }
8478
+
8479
+ #endregion ------------------------------------------------------------------
8480
+
8481
+ /* Account
8482
+ ------------------------------------------------------------------------------------------------------------------*/
8483
+
8484
+ /**
8485
+ * Find plugin's slug by plugin's basename.
8486
+ *
8487
+ * @author Vova Feldman (@svovaf)
8488
+ * @since 1.0.9
8489
+ *
8490
+ * @param string $plugin_base_name
8491
+ *
8492
+ * @return false|string
8493
+ */
8494
+ private static function find_slug_by_basename( $plugin_base_name ) {
8495
+ $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
8496
+
8497
+ if ( ! array( $file_slug_map ) || ! isset( $file_slug_map[ $plugin_base_name ] ) ) {
8498
+ return false;
8499
+ }
8500
+
8501
+ return $file_slug_map[ $plugin_base_name ];
8502
+ }
8503
+
8504
+ /**
8505
+ * Store the map between the plugin's basename to the slug.
8506
+ *
8507
+ * @author Vova Feldman (@svovaf)
8508
+ * @since 1.0.9
8509
+ */
8510
+ private function store_file_slug_map() {
8511
+ $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
8512
+
8513
+ if ( ! array( $file_slug_map ) ) {
8514
+ $file_slug_map = array();
8515
+ }
8516
+
8517
+ if ( ! isset( $file_slug_map[ $this->_plugin_basename ] ) ||
8518
+ $file_slug_map[ $this->_plugin_basename ] !== $this->_slug
8519
+ ) {
8520
+ $file_slug_map[ $this->_plugin_basename ] = $this->_slug;
8521
+ self::$_accounts->set_option( 'file_slug_map', $file_slug_map, true );
8522
+ }
8523
+ }
8524
+
8525
+ /**
8526
+ * @return array[number]FS_User
8527
+ */
8528
+ static function get_all_users() {
8529
+ $users = self::$_accounts->get_option( 'users', array() );
8530
+
8531
+ if ( ! is_array( $users ) ) {
8532
+ $users = array();
8533
+ }
8534
+
8535
+ return $users;
8536
+ }
8537
+
8538
+ /**
8539
+ * @param string $module_type
8540
+ * @param null|int $blog_id Since 2.0.0
8541
+ *
8542
+ * @return array[string]FS_Site
8543
+ */
8544
+ private static function get_all_sites(
8545
+ $module_type = WP_FS__MODULE_TYPE_PLUGIN,
8546
+ $blog_id = null
8547
+ ) {
8548
+ $sites = self::get_account_option( 'sites', $module_type, $blog_id );
8549
+
8550
+ if ( ! is_array( $sites ) ) {
8551
+ $sites = array();
8552
+ }
8553
+
8554
+ return $sites;
8555
+ }
8556
+
8557
+ /**
8558
+ * @author Leo Fajardo (@leorw)
8559
+ *
8560
+ * @since 1.2.2
8561
+ *
8562
+ * @param string $option_name
8563
+ * @param string $module_type
8564
+ * @param null|int $network_level_or_blog_id Since 2.0.0
8565
+ *
8566
+ * @return mixed
8567
+ */
8568
+ private static function get_account_option( $option_name, $module_type = null, $network_level_or_blog_id = null ) {
8569
+ if ( ! is_null( $module_type ) && WP_FS__MODULE_TYPE_PLUGIN !== $module_type ) {
8570
+ $option_name = $module_type . '_' . $option_name;
8571
+ }
8572
+
8573
+ return self::$_accounts->get_option( $option_name, array(), $network_level_or_blog_id );
8574
+ }
8575
+
8576
+ /**
8577
+ * @author Leo Fajardo (@leorw)
8578
+ *
8579
+ * @since 1.2.2
8580
+ *
8581
+ * @param string $option_name
8582
+ * @param mixed $option_value
8583
+ * @param bool $store
8584
+ * @param null|int $network_level_or_blog_id Since 2.0.0
8585
+ */
8586
+ private function set_account_option( $option_name, $option_value, $store, $network_level_or_blog_id = null ) {
8587
+ self::set_account_option_by_module(
8588
+ $this->_module_type,
8589
+ $option_name,
8590
+ $option_value,
8591
+ $store,
8592
+ $network_level_or_blog_id
8593
+ );
8594
+ }
8595
+
8596
+ /**
8597
+ * @author Vova Feldman (@svovaf)
8598
+ *
8599
+ * @since 1.2.2.7
8600
+ *
8601
+ * @param string $module_type
8602
+ * @param string $option_name
8603
+ * @param mixed $option_value
8604
+ * @param bool $store
8605
+ * @param null|int $network_level_or_blog_id Since 2.0.0
8606
+ */
8607
+ private static function set_account_option_by_module(
8608
+ $module_type,
8609
+ $option_name,
8610
+ $option_value,
8611
+ $store,
8612
+ $network_level_or_blog_id = null
8613
+ ) {
8614
+ if ( WP_FS__MODULE_TYPE_PLUGIN != $module_type ) {
8615
+ $option_name = $module_type . '_' . $option_name;
8616
+ }
8617
+
8618
+ self::$_accounts->set_option( $option_name, $option_value, $store, $network_level_or_blog_id );
8619
+ }
8620
+
8621
+ /**
8622
+ * @author Vova Feldman (@svovaf)
8623
+ * @since 1.0.6
8624
+ *
8625
+ * @param number|null $module_id
8626
+ *
8627
+ * @return FS_Plugin_License[]
8628
+ */
8629
+ private static function get_all_licenses( $module_id = null ) {
8630
+ $licenses = self::get_account_option( 'all_licenses' );
8631
+
8632
+ if ( ! is_array( $licenses ) ) {
8633
+ $licenses = array();
8634
+ }
8635
+
8636
+ if ( is_null( $module_id ) ) {
8637
+ return $licenses;
8638
+ }
8639
+
8640
+ $licenses = isset( $licenses[ $module_id ] ) ?
8641
+ $licenses[ $module_id ] :
8642
+ array();
8643
+
8644
+ return $licenses;
8645
+ }
8646
+
8647
+ /**
8648
+ * @author Leo Fajardo (@leorw)
8649
+ * @since 2.0.0
8650
+ *
8651
+ * @return array
8652
+ */
8653
+ private static function get_all_licenses_by_module_type() {
8654
+ $licenses = self::get_account_option( 'all_licenses' );
8655
+
8656
+ $licenses_by_module_type = array(
8657
+ WP_FS__MODULE_TYPE_PLUGIN => array(),
8658
+ WP_FS__MODULE_TYPE_THEME => array()
8659
+ );
8660
+
8661
+ if ( ! is_array( $licenses ) ) {
8662
+ return $licenses_by_module_type;
8663
+ }
8664
+
8665
+ foreach ( $licenses as $module_id => $module_licenses ) {
8666
+ $fs = self::get_instance_by_id( $module_id );
8667
+ if ( false === $fs ) {
8668
+ continue;
8669
+ }
8670
+
8671
+ $licenses_by_module_type[ $fs->_module_type ] = array_merge( $licenses_by_module_type[ $fs->_module_type ], $module_licenses );
8672
+ }
8673
+
8674
+ return $licenses_by_module_type;
8675
+ }
8676
+
8677
+ /**
8678
+ * @author Leo Fajardo (@leorw)
8679
+ * @since 2.0.0
8680
+ *
8681
+ * @param number $module_id
8682
+ * @param number|null $user_id
8683
+ *
8684
+ * @return array
8685
+ */
8686
+ private static function get_user_id_license_ids_map( $module_id, $user_id = null ) {
8687
+ $all_modules_user_id_license_ids_map = self::get_account_option( 'user_id_license_ids_map' );
8688
+
8689
+ if ( ! is_array( $all_modules_user_id_license_ids_map ) ) {
8690
+ $all_modules_user_id_license_ids_map = array();
8691
+ }
8692
+
8693
+ $user_id_license_ids_map = isset( $all_modules_user_id_license_ids_map[ $module_id ] ) ?
8694
+ $all_modules_user_id_license_ids_map[ $module_id ] :
8695
+ array();
8696
+
8697
+ if ( FS_User::is_valid_id( $user_id ) ) {
8698
+ $user_id_license_ids_map = isset( $user_id_license_ids_map[ $user_id ] ) ?
8699
+ $user_id_license_ids_map[ $user_id ] :
8700
+ array();
8701
+ }
8702
+
8703
+ return $user_id_license_ids_map;
8704
+ }
8705
+
8706
+ /**
8707
+ * @author Leo Fajardo (@leorw)
8708
+ * @since 2.0.0
8709
+ *
8710
+ * @param array $new_user_id_license_ids_map
8711
+ * @param number $module_id
8712
+ * @param number|null $user_id
8713
+ */
8714
+ private static function store_user_id_license_ids_map( $new_user_id_license_ids_map, $module_id, $user_id = null ) {
8715
+ $all_modules_user_id_license_ids_map = self::get_account_option( 'user_id_license_ids_map' );
8716
+ if ( ! is_array( $all_modules_user_id_license_ids_map ) ) {
8717
+ $all_modules_user_id_license_ids_map = array();
8718
+ }
8719
+
8720
+ if ( ! isset( $all_modules_user_id_license_ids_map[ $module_id ] ) ) {
8721
+ $all_modules_user_id_license_ids_map[ $module_id ] = array();
8722
+ }
8723
+
8724
+ if ( FS_User::is_valid_id( $user_id ) ) {
8725
+ $all_modules_user_id_license_ids_map[ $module_id ][ $user_id ] = $new_user_id_license_ids_map;
8726
+ } else {
8727
+ $all_modules_user_id_license_ids_map[ $module_id ] = $new_user_id_license_ids_map;
8728
+ }
8729
+
8730
+ self::$_accounts->set_option( 'user_id_license_ids_map', $all_modules_user_id_license_ids_map, true );
8731
+ }
8732
+
8733
+ /**
8734
+ * Get a collection of the user's linked license IDs.
8735
+ *
8736
+ * @author Vova Feldman (@svovaf)
8737
+ * @since 2.0.0
8738
+ *
8739
+ * @param number $user_id
8740
+ *
8741
+ * @return number[]
8742
+ */
8743
+ private function get_user_linked_license_ids( $user_id ) {
8744
+ return self::get_user_id_license_ids_map( $this->_module_id, $user_id );
8745
+ }
8746
+
8747
+ /**
8748
+ * Override the user's linked license IDs with a new IDs collection.
8749
+ *
8750
+ * @author Vova Feldman (@svovaf)
8751
+ * @since 2.0.0
8752
+ *
8753
+ * @param number $user_id
8754
+ * @param number[] $license_ids
8755
+ */
8756
+ private function set_user_linked_license_ids( $user_id, array $license_ids ) {
8757
+ self::store_user_id_license_ids_map( $license_ids, $this->_module_id, $user_id );
8758
+ }
8759
+
8760
+ /**
8761
+ * Link a specified license ID to a given user.
8762
+ *
8763
+ * @author Vova Feldman (@svovaf)
8764
+ * @since 2.0.0
8765
+ *
8766
+ * @param number $license_id
8767
+ * @param number $user_id
8768
+ */
8769
+ private function link_license_2_user( $license_id, $user_id ) {
8770
+ $license_ids = $this->get_user_linked_license_ids( $user_id );
8771
+
8772
+ if ( in_array( $license_id, $license_ids ) ) {
8773
+ // License already linked.
8774
+ return;
8775
+ }
8776
+
8777
+ $license_ids[] = $license_id;
8778
+
8779
+ $this->set_user_linked_license_ids( $user_id, $license_ids );
8780
+ }
8781
+
8782
+ /**
8783
+ * @param string|bool $module_type
8784
+ *
8785
+ * @return FS_Plugin_Plan[]
8786
+ */
8787
+ private static function get_all_plans( $module_type = false ) {
8788
+ $plans = self::get_account_option( 'plans', $module_type );
8789
+
8790
+ if ( ! is_array( $plans ) ) {
8791
+ $plans = array();
8792
+ }
8793
+
8794
+ return $plans;
8795
+ }
8796
+
8797
+ /**
8798
+ * @author Vova Feldman (@svovaf)
8799
+ * @since 1.0.4
8800
+ *
8801
+ * @return FS_Plugin_Tag[]
8802
+ */
8803
+ private static function get_all_updates() {
8804
+ $updates = self::$_accounts->get_option( 'updates', array() );
8805
+
8806
+ if ( ! is_array( $updates ) ) {
8807
+ $updates = array();
8808
+ }
8809
+
8810
+ return $updates;
8811
+ }
8812
+
8813
+ /**
8814
+ * @author Vova Feldman (@svovaf)
8815
+ * @since 1.0.6
8816
+ *
8817
+ * @return array<number,FS_Plugin[]>|false
8818
+ */
8819
+ private static function get_all_addons() {
8820
+ $addons = self::$_accounts->get_option( 'addons', array() );
8821
+
8822
+ if ( ! is_array( $addons ) ) {
8823
+ $addons = array();
8824
+ }
8825
+
8826
+ return $addons;
8827
+ }
8828
+
8829
+ /**
8830
+ * @author Vova Feldman (@svovaf)
8831
+ * @since 1.0.6
8832
+ *
8833
+ * @return FS_Plugin[]|false
8834
+ */
8835
+ private static function get_all_account_addons() {
8836
+ $addons = self::$_accounts->get_option( 'account_addons', array() );
8837
+
8838
+ if ( ! is_array( $addons ) ) {
8839
+ $addons = array();
8840
+ }
8841
+
8842
+ return $addons;
8843
+ }
8844
+
8845
+ /**
8846
+ * Check if user has connected his account (opted-in).
8847
+ *
8848
+ * Note:
8849
+ * If the user opted-in and opted-out on a later stage,
8850
+ * this will still return true. If you want to check if the
8851
+ * user is currently opted-in, use:
8852
+ * `$fs->is_registered() && $fs->is_tracking_allowed()`
8853
+ *
8854
+ * @author Vova Feldman (@svovaf)
8855
+ * @since 1.0.1
8856
+ * @return bool
8857
+ */
8858
+ function is_registered() {
8859
+ return is_object( $this->_user );
8860
+ }
8861
+
8862
+ /**
8863
+ * Returns TRUE if the user opted-in and didn't disconnect (opt-out).
8864
+ *
8865
+ * @author Leo Fajardo (@leorw)
8866
+ * @since 1.2.1.5
8867
+ *
8868
+ * @return bool
8869
+ */
8870
+ function is_tracking_allowed() {
8871
+ return ( is_object( $this->_site ) && $this->_site->is_tracking_allowed() );
8872
+ }
8873
+
8874
+ /**
8875
+ * @author Vova Feldman (@svovaf)
8876
+ * @since 1.0.4
8877
+ *
8878
+ * @return FS_Plugin
8879
+ */
8880
+ function get_plugin() {
8881
+ return $this->_plugin;
8882
+ }
8883
+
8884
+ /**
8885
+ * @author Vova Feldman (@svovaf)
8886
+ * @since 1.0.3
8887
+ *
8888
+ * @return FS_User
8889
+ */
8890
+ function get_user() {
8891
+ return $this->_user;
8892
+ }
8893
+
8894
+ /**
8895
+ * @author Vova Feldman (@svovaf)
8896
+ * @since 1.0.3
8897
+ *
8898
+ * @return FS_Site
8899
+ */
8900
+ function get_site() {
8901
+ return $this->_site;
8902
+ }
8903
+
8904
+ /**
8905
+ * Get plugin add-ons.
8906
+ *
8907
+ * @author Vova Feldman (@svovaf)
8908
+ * @since 1.0.6
8909
+ *
8910
+ * @since 1.1.7.3 If not yet loaded, fetch data from the API.
8911
+ *
8912
+ * @param bool $flush
8913
+ *
8914
+ * @return FS_Plugin[]|false
8915
+ */
8916
+ function get_addons( $flush = false ) {
8917
+ $this->_logger->entrance();
8918
+
8919
+ if ( ! $this->_has_addons ) {
8920
+ return false;
8921
+ }
8922
+
8923
+ $addons = $this->sync_addons( $flush );
8924
+
8925
+ return ( ! is_array( $addons ) || empty( $addons ) ) ?
8926
+ false :
8927
+ $addons;
8928
+ }
8929
+
8930
+ /**
8931
+ * @author Vova Feldman (@svovaf)
8932
+ * @since 1.0.6
8933
+ *
8934
+ * @return FS_Plugin[]|false
8935
+ */
8936
+ function get_account_addons() {
8937
+ $this->_logger->entrance();
8938
+
8939
+ $addons = self::get_all_account_addons();
8940
+
8941
+ if ( ! is_array( $addons ) ||
8942
+ ! isset( $addons[ $this->_plugin->id ] ) ||
8943
+ ! is_array( $addons[ $this->_plugin->id ] ) ||
8944
+ 0 === count( $addons[ $this->_plugin->id ] )
8945
+ ) {
8946
+ return false;
8947
+ }
8948
+
8949
+ return $addons[ $this->_plugin->id ];
8950
+ }
8951
+
8952
+ /**
8953
+ * Check if user has any
8954
+ *
8955
+ * @author Vova Feldman (@svovaf)
8956
+ * @since 1.1.6
8957
+ *
8958
+ * @return bool
8959
+ */
8960
+ function has_account_addons() {
8961
+ $addons = $this->get_account_addons();
8962
+
8963
+ return is_array( $addons ) && ( 0 < count( $addons ) );
8964
+ }
8965
+
8966
+
8967
+ /**
8968
+ * Get add-on by ID (from local data).
8969
+ *
8970
+ * @author Vova Feldman (@svovaf)
8971
+ * @since 1.0.6
8972
+ *
8973
+ * @param number $id
8974
+ *
8975
+ * @return FS_Plugin|false
8976
+ */
8977
+ function get_addon( $id ) {
8978
+ $this->_logger->entrance();
8979
+
8980
+ $addons = $this->get_addons();
8981
+
8982
+ if ( is_array( $addons ) ) {
8983
+ foreach ( $addons as $addon ) {
8984
+ if ( $id == $addon->id ) {
8985
+ return $addon;
8986
+ }
8987
+ }
8988
+ }
8989
+
8990
+ return false;
8991
+ }
8992
+
8993
+ /**
8994
+ * Get add-on by slug (from local data).
8995
+ *
8996
+ * @author Vova Feldman (@svovaf)
8997
+ * @since 1.0.6
8998
+ *
8999
+ * @param string $slug
9000
+ *
9001
+ * @param bool $flush
9002
+ *
9003
+ * @return FS_Plugin|false
9004
+ */
9005
+ function get_addon_by_slug( $slug, $flush = false ) {
9006
+ $this->_logger->entrance();
9007
+
9008
+ $addons = $this->get_addons( $flush );
9009
+
9010
+ if ( is_array( $addons ) ) {
9011
+ foreach ( $addons as $addon ) {
9012
+ if ( $slug === $addon->slug ) {
9013
+ return $addon;
9014
+ }
9015
+ }
9016
+ }
9017
+
9018
+ return false;
9019
+ }
9020
+
9021
+ /**
9022
+ * @author Vova Feldman (@svovaf)
9023
+ * @since 2.0.0
9024
+ *
9025
+ * @param number $user_id
9026
+ *
9027
+ * @return FS_User
9028
+ */
9029
+ static function _get_user_by_id( $user_id ) {
9030
+ self::$_static_logger->entrance( "user_id = {$user_id}" );
9031
+
9032
+ $users = self::get_all_users();
9033
+
9034
+ if ( is_array( $users ) ) {
9035
+ if ( isset( $users[ $user_id ] ) &&
9036
+ $users[ $user_id ] instanceof FS_User &&
9037
+ $user_id == $users[ $user_id ]->id
9038
+ ) {
9039
+ return $users[ $user_id ];
9040
+ }
9041
+
9042
+ // If user wasn't found by the key, iterate over all the users collection.
9043
+ foreach ( $users as $user ) {
9044
+ /**
9045
+ * @var FS_User $user
9046
+ */
9047
+ if ( $user_id == $user->id ) {
9048
+ return $user;
9049
+ }
9050
+ }
9051
+ }
9052
+
9053
+ return null;
9054
+ }
9055
+
9056
+ /**
9057
+ * Checks if a Freemius user_id is associated with a super-admin.
9058
+ *
9059
+ * @author Vova Feldman (@svovaf)
9060
+ * @since 2.0.0
9061
+ *
9062
+ * @param number $user_id
9063
+ *
9064
+ * @return bool
9065
+ */
9066
+ private static function is_super_admin( $user_id ) {
9067
+ $is_super_admin = false;
9068
+
9069
+ $user = self::_get_user_by_id( $user_id );
9070
+
9071
+ if ( $user instanceof FS_User && ! empty( $user->email ) ) {
9072
+ self::require_pluggable_essentials();
9073
+
9074
+ $wp_user = get_user_by( 'email', $user->email );
9075
+
9076
+ if ( $wp_user instanceof WP_User ) {
9077
+ $super_admins = get_super_admins();
9078
+ $is_super_admin = ( is_array( $super_admins ) && in_array( $wp_user->user_login, $super_admins ) );
9079
+ }
9080
+ }
9081
+
9082
+ return $is_super_admin;
9083
+ }
9084
+
9085
+ #----------------------------------------------------------------------------------
9086
+ #region Plans & Licensing
9087
+ #----------------------------------------------------------------------------------
9088
+
9089
+ /**
9090
+ * Check if running premium plugin code.
9091
+ *
9092
+ * @author Vova Feldman (@svovaf)
9093
+ * @since 1.0.5
9094
+ *
9095
+ * @return bool
9096
+ */
9097
+ function is_premium() {
9098
+ return $this->_plugin->is_premium;
9099
+ }
9100
+
9101
+ /**
9102
+ * Get site's plan ID.
9103
+ *
9104
+ * @author Vova Feldman (@svovaf)
9105
+ * @since 1.0.2
9106
+ *
9107
+ * @return number
9108
+ */
9109
+ function get_plan_id() {
9110
+ return $this->_site->plan_id;
9111
+ }
9112
+
9113
+ /**
9114
+ * Get site's plan title.
9115
+ *
9116
+ * @author Vova Feldman (@svovaf)
9117
+ * @since 1.0.2
9118
+ *
9119
+ * @return string
9120
+ */
9121
+ function get_plan_title() {
9122
+ $plan = $this->get_plan();
9123
+
9124
+ return is_object( $plan ) ? $plan->title : 'PLAN_TITLE';
9125
+ }
9126
+
9127
+ /**
9128
+ * Get site's plan name.
9129
+ *
9130
+ * @author Vova Feldman (@svovaf)
9131
+ * @since 2.0.0
9132
+ *
9133
+ * @return string
9134
+ */
9135
+ function get_plan_name() {
9136
+ $plan = $this->get_plan();
9137
+
9138
+ return is_object( $plan ) ? $plan->name : 'PLAN_NAME';
9139
+ }
9140
+
9141
+ /**
9142
+ * @author Vova Feldman (@svovaf)
9143
+ * @since 1.0.9
9144
+ *
9145
+ * @return FS_Plugin_Plan|false
9146
+ */
9147
+ function get_plan() {
9148
+ if ( ! is_object( $this->_site ) ) {
9149
+ return false;
9150
+ }
9151
+
9152
+ return FS_Plugin_Plan::is_valid_id( $this->_site->plan_id ) ?
9153
+ $this->_get_plan_by_id( $this->_site->plan_id ) :
9154
+ false;
9155
+ }
9156
+
9157
+ /**
9158
+ * @author Vova Feldman (@svovaf)
9159
+ * @since 1.0.3
9160
+ *
9161
+ * @return bool
9162
+ */
9163
+ function is_trial() {
9164
+ $this->_logger->entrance();
9165
+
9166
+ if ( ! $this->is_registered() || ! is_object( $this->_site ) ) {
9167
+ return false;
9168
+ }
9169
+
9170
+ return $this->_site->is_trial();
9171
+ }
9172
+
9173
+ /**
9174
+ * Check if currently in a trial with payment method (credit card or paypal).
9175
+ *
9176
+ * @author Vova Feldman (@svovaf)
9177
+ * @since 1.1.7
9178
+ *
9179
+ * @return bool
9180
+ */
9181
+ function is_paid_trial() {
9182
+ $this->_logger->entrance();
9183
+
9184
+ if ( ! $this->is_trial() ) {
9185
+ return false;
9186
+ }
9187
+
9188
+ return $this->has_active_valid_license() && ( $this->_site->trial_plan_id == $this->_license->plan_id );
9189
+ }
9190
+
9191
+ /**
9192
+ * Check if trial already utilized.
9193
+ *
9194
+ * @since 1.0.9
9195
+ *
9196
+ * @return bool
9197
+ */
9198
+ function is_trial_utilized() {
9199
+ $this->_logger->entrance();
9200
+
9201
+ if ( ! $this->is_registered() ) {
9202
+ return false;
9203
+ }
9204
+
9205
+ return $this->_site->is_trial_utilized();
9206
+ }
9207
+
9208
+ /**
9209
+ * Get trial plan information (if in trial).
9210
+ *
9211
+ * @author Vova Feldman (@svovaf)
9212
+ * @since 1.0.9
9213
+ *
9214
+ * @return bool|FS_Plugin_Plan
9215
+ */
9216
+ function get_trial_plan() {
9217
+ $this->_logger->entrance();
9218
+
9219
+ if ( ! $this->is_trial() ) {
9220
+ return false;
9221
+ }
9222
+
9223
+ // Try to load plan from local cache.
9224
+ $trial_plan = $this->_get_plan_by_id( $this->_site->trial_plan_id );
9225
+
9226
+ if ( ! is_object( $trial_plan ) ) {
9227
+ $trial_plan = $this->_fetch_site_plan( $this->_site->trial_plan_id );
9228
+
9229
+ /**
9230
+ * If managed to fetch the plan, add it to the plans collection.
9231
+ */
9232
+ if ( $trial_plan instanceof FS_Plugin_Plan ) {
9233
+ if ( ! is_array( $this->_plans ) ) {
9234
+ $this->_plans = array();
9235
+ }
9236
+
9237
+ $this->_plans[] = $trial_plan;
9238
+ $this->_store_plans();
9239
+ }
9240
+ }
9241
+
9242
+ if ( $trial_plan instanceof FS_Plugin_Plan ) {
9243
+ return $trial_plan;
9244
+ }
9245
+
9246
+ /**
9247
+ * If for some reason failed to get the trial plan, fallback to a dummy name and title.
9248
+ */
9249
+ $trial_plan = new FS_Plugin_Plan();
9250
+ $trial_plan->id = $this->_site->trial_plan_id;
9251
+ $trial_plan->name = 'pro';
9252
+ $trial_plan->title = 'Pro';
9253
+
9254
+ return $trial_plan;
9255
+ }
9256
+
9257
+ /**
9258
+ * Check if the user has an activate, non-expired license on current plugin's install.
9259
+ *
9260
+ * @since 1.0.9
9261
+ *
9262
+ * @return bool
9263
+ */
9264
+ function is_paying() {
9265
+ $this->_logger->entrance();
9266
+
9267
+ if ( ! $this->is_registered() ) {
9268
+ return false;
9269
+ }
9270
+
9271
+ if ( ! $this->has_paid_plan() ) {
9272
+ return false;
9273
+ }
9274
+
9275
+ return (
9276
+ ! $this->is_trial() &&
9277
+ 'free' !== $this->get_plan_name() &&
9278
+ $this->has_active_valid_license()
9279
+ );
9280
+ }
9281
+
9282
+ /**
9283
+ * @author Vova Feldman (@svovaf)
9284
+ * @since 1.0.4
9285
+ *
9286
+ * @return bool
9287
+ */
9288
+ function is_free_plan() {
9289
+ if ( ! $this->is_registered() ) {
9290
+ return true;
9291
+ }
9292
+
9293
+ if ( ! $this->has_paid_plan() ) {
9294
+ return true;
9295
+ }
9296
+
9297
+ return (
9298
+ 'free' === $this->get_plan_name() ||
9299
+ ! $this->has_features_enabled_license()
9300
+ );
9301
+ }
9302
+
9303
+ /**
9304
+ * @author Vova Feldman (@svovaf)
9305
+ * @since 1.0.5
9306
+ *
9307
+ * @return bool
9308
+ */
9309
+ function _has_premium_license() {
9310
+ $this->_logger->entrance();
9311
+
9312
+ $premium_license = $this->_get_available_premium_license();
9313
+
9314
+ return ( false !== $premium_license );
9315
+ }
9316
+
9317
+ /**
9318
+ * Check if user has any licenses associated with the plugin (including expired or blocking).
9319
+ *
9320
+ * @author Vova Feldman (@svovaf)
9321
+ * @since 1.1.7.3
9322
+ *
9323
+ * @return bool
9324
+ */
9325
+ function has_any_license() {
9326
+ return is_array( $this->_licenses ) && ( 0 < count( $this->_licenses ) );
9327
+ }
9328
+
9329
+ /**
9330
+ * @author Vova Feldman (@svovaf)
9331
+ * @since 1.0.5
9332
+ *
9333
+ * @param bool|null $is_localhost
9334
+ *
9335
+ * @return FS_Plugin_License|false
9336
+ */
9337
+ function _get_available_premium_license( $is_localhost = null ) {
9338
+ $this->_logger->entrance();
9339
+
9340
+ $licenses = $this->get_available_premium_licenses( $is_localhost );
9341
+ if ( ! empty( $licenses ) ) {
9342
+ return $licenses[0];
9343
+ }
9344
+
9345
+ return false;
9346
+ }
9347
+
9348
+ /**
9349
+ * @author Vova Feldman (@svovaf)
9350
+ * @since 1.0.5
9351
+ *
9352
+ * @param bool|null $is_localhost
9353
+ *
9354
+ * @return FS_Plugin_License[]
9355
+ */
9356
+ function get_available_premium_licenses( $is_localhost = null ) {
9357
+ $this->_logger->entrance();
9358
+
9359
+ $licenses = array();
9360
+ if ( ! $this->has_paid_plan() ) {
9361
+ return $licenses;
9362
+ }
9363
+
9364
+ if ( is_array( $this->_licenses ) ) {
9365
+ foreach ( $this->_licenses as $license ) {
9366
+ if ( ! $license->can_activate( $is_localhost ) ) {
9367
+ continue;
9368
+ }
9369
+
9370
+ $licenses[] = $license;
9371
+ }
9372
+ }
9373
+
9374
+ return $licenses;
9375
+ }
9376
+
9377
+ /**
9378
+ * Sync local plugin plans with remote server.
9379
+ *
9380
+ * 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).
9381
+ *
9382
+ * @author Vova Feldman (@svovaf)
9383
+ * @since 1.0.5
9384
+ *
9385
+ * @return FS_Plugin_Plan[]|object
9386
+ */
9387
+ function _sync_plans() {
9388
+ $plans = $this->_fetch_plugin_plans();
9389
+
9390
+ if ( $this->is_array_instanceof( $plans, 'FS_Plugin_Plan' ) ) {
9391
+ $plans_map = array();
9392
+ foreach ( $plans as $plan ) {
9393
+ $plans_map[ $plan->id ] = true;
9394
+ }
9395
+
9396
+ $plans_ids_to_keep = $this->get_plans_ids_associated_with_installs();
9397
+
9398
+ foreach ( $plans_ids_to_keep as $plan_id ) {
9399
+ if ( isset( $plans_map[ $plan_id ] ) ) {
9400
+ continue;
9401
+ }
9402
+
9403
+ $missing_plan = self::_get_plan_by_id( $plan_id );
9404
+
9405
+ if ( is_object( $missing_plan ) ) {
9406
+ $plans[] = $missing_plan;
9407
+ }
9408
+ }
9409
+
9410
+ $this->_plans = $plans;
9411
+ $this->_store_plans();
9412
+ }
9413
+
9414
+ $this->do_action( 'after_plans_sync', $plans );
9415
+
9416
+ return $this->_plans;
9417
+ }
9418
+
9419
+ /**
9420
+ * Check if specified plan exists locally. If not, fetch it and store it.
9421
+ *
9422
+ * @author Vova Feldman (@svovaf)
9423
+ * @since 2.0.0
9424
+ *
9425
+ * @param number $plan_id
9426
+ *
9427
+ * @return \FS_Plugin_Plan|object The plan entity or the API error object on failure.
9428
+ */
9429
+ private function sync_plan_if_not_exist( $plan_id ) {
9430
+ $plan = self::_get_plan_by_id( $plan_id );
9431
+
9432
+ if ( is_object( $plan ) ) {
9433
+ // Plan already exists.
9434
+ return $plan;
9435
+ }
9436
+
9437
+ $plan = $this->fetch_plan_by_id( $plan_id );
9438
+
9439
+ if ( $plan instanceof FS_Plugin_Plan ) {
9440
+ $this->_plans[] = $plan;
9441
+ $this->_store_plans();
9442
+
9443
+ return $plan;
9444
+ }
9445
+
9446
+ return $plan;
9447
+ }
9448
+
9449
+ /**
9450
+ * Check if specified license exists locally. If not, fetch it and store it.
9451
+ *
9452
+ * @author Vova Feldman (@svovaf)
9453
+ * @since 2.0.0
9454
+ *
9455
+ * @param number $license_id
9456
+ * @param string $license_key
9457
+ *
9458
+ * @return \FS_Plugin_Plan|object The plan entity or the API error object on failure.
9459
+ */
9460
+ private function sync_license_if_not_exist( $license_id, $license_key ) {
9461
+ $license = $this->_get_license_by_id( $license_id );
9462
+
9463
+ if ( is_object( $license ) ) {
9464
+ // License already exists.
9465
+ return $license;
9466
+ }
9467
+
9468
+ $license = $this->fetch_license_by_key( $license_id, $license_key );
9469
+
9470
+ if ( $license instanceof FS_Plugin_License ) {
9471
+ $this->_licenses[] = $license;
9472
+ $this->_license = $license;
9473
+ $this->_store_licenses();
9474
+
9475
+ return $license;
9476
+ }
9477
+
9478
+ return $license;
9479
+ }
9480
+
9481
+ /**
9482
+ * Get a collection of unique plan IDs that are associated with any installs in the network.
9483
+ *
9484
+ * @author Leo Fajardo (@leorw)
9485
+ * @since 2.0.0
9486
+ *
9487
+ * @return number[]
9488
+ */
9489
+ private function get_plans_ids_associated_with_installs() {
9490
+ if ( ! $this->_is_network_active ) {
9491
+ if ( ! is_object( $this->_site ) ||
9492
+ ! FS_Plugin_Plan::is_valid_id( $this->_site->plan_id )
9493
+ ) {
9494
+ return array();
9495
+ }
9496
+
9497
+ return array( $this->_site->plan_id );
9498
+ }
9499
+
9500
+ $plan_ids = array();
9501
+ $sites = self::get_sites();
9502
+ foreach ( $sites as $site ) {
9503
+ $blog_id = self::get_site_blog_id( $site );
9504
+ $install = $this->get_install_by_blog_id( $blog_id );
9505
+
9506
+ if ( ! is_object( $install ) ||
9507
+ ! FS_Plugin_Plan::is_valid_id( $install->plan_id )
9508
+ ) {
9509
+ continue;
9510
+ }
9511
+
9512
+ $plan_ids[ $install->plan_id ] = true;
9513
+ }
9514
+
9515
+ return array_keys( $plan_ids );
9516
+ }
9517
+
9518
+ /**
9519
+ * Get a collection of unique license IDs that are associated with any installs in the network.
9520
+ *
9521
+ * @author Leo Fajardo (@leorw)
9522
+ * @since 2.0.0
9523
+ *
9524
+ * @return number[]
9525
+ */
9526
+ private function get_license_ids_associated_with_installs() {
9527
+ if ( ! $this->_is_network_active ) {
9528
+ if ( ! is_object( $this->_site ) ||
9529
+ ! FS_Plugin_License::is_valid_id( $this->_site->license_id )
9530
+ ) {
9531
+ return array();
9532
+ }
9533
+
9534
+ return array( $this->_site->license_id );
9535
+ }
9536
+
9537
+ $license_ids = array();
9538
+ $sites = self::get_sites();
9539
+ foreach ( $sites as $site ) {
9540
+ $blog_id = self::get_site_blog_id( $site );
9541
+ $install = $this->get_install_by_blog_id( $blog_id );
9542
+
9543
+ if ( ! is_object( $install ) ||
9544
+ ! FS_Plugin_License::is_valid_id( $install->license_id )
9545
+ ) {
9546
+ continue;
9547
+ }
9548
+
9549
+ $license_ids[ $install->license_id ] = true;
9550
+ }
9551
+
9552
+ return array_keys( $license_ids );
9553
+ }
9554
+
9555
+ /**
9556
+ * @author Vova Feldman (@svovaf)
9557
+ * @since 1.0.5
9558
+ *
9559
+ * @param number $id
9560
+ *
9561
+ * @return FS_Plugin_Plan|false
9562
+ */
9563
+ function _get_plan_by_id( $id ) {
9564
+ $this->_logger->entrance();
9565
+
9566
+ if ( ! is_array( $this->_plans ) || 0 === count( $this->_plans ) ) {
9567
+ $this->_sync_plans();
9568
+ }
9569
+
9570
+ foreach ( $this->_plans as $plan ) {
9571
+ if ( $id == $plan->id ) {
9572
+ return $plan;
9573
+ }
9574
+ }
9575
+
9576
+ return false;
9577
+ }
9578
+
9579
+ /**
9580
+ * @author Vova Feldman (@svovaf)
9581
+ * @since 1.1.8.1
9582
+ *
9583
+ * @param string $name
9584
+ *
9585
+ * @return FS_Plugin_Plan|false
9586
+ */
9587
+ private function get_plan_by_name( $name ) {
9588
+ $this->_logger->entrance();
9589
+
9590
+ if ( ! is_array( $this->_plans ) || 0 === count( $this->_plans ) ) {
9591
+ $this->_sync_plans();
9592
+ }
9593
+
9594
+ foreach ( $this->_plans as $plan ) {
9595
+ if ( $name == $plan->name ) {
9596
+ return $plan;
9597
+ }
9598
+ }
9599
+
9600
+ return false;
9601
+ }
9602
+
9603
+ /**
9604
+ * Sync local licenses with remote server.
9605
+ *
9606
+ * @author Vova Feldman (@svovaf)
9607
+ * @since 1.0.6
9608
+ *
9609
+ * @param number|bool $site_license_id
9610
+ * @param number|null $blog_id
9611
+ *
9612
+ * @return FS_Plugin_License[]|object
9613
+ */
9614
+ function _sync_licenses( $site_license_id = false, $blog_id = null ) {
9615
+ $this->_logger->entrance();
9616
+
9617
+ $is_network_admin = fs_is_network_admin();
9618
+
9619
+ if ( $is_network_admin && is_null( $blog_id ) ) {
9620
+ $all_licenses = self::get_all_licenses( $this->_module_id );
9621
+ } else {
9622
+ $all_licenses = $this->get_user_licenses( $this->_user->id );
9623
+ }
9624
+
9625
+ $foreign_licenses = array(
9626
+ 'ids' => array(),
9627
+ 'license_keys' => array()
9628
+ );
9629
+
9630
+ $all_licenses_map = array();
9631
+ foreach ( $all_licenses as $license ) {
9632
+ $all_licenses_map[ $license->id ] = true;
9633
+ if ( $license->user_id == $this->_user->id || $license->id == $site_license_id ) {
9634
+ continue;
9635
+ }
9636
+
9637
+ $foreign_licenses['ids'][] = $license->id;
9638
+ $foreign_licenses['license_keys'][] = $license->secret_key;
9639
+ }
9640
+
9641
+ if ( empty( $foreign_licenses['ids'] ) ) {
9642
+ $foreign_licenses = array();
9643
+ }
9644
+
9645
+ $licenses = $this->_fetch_licenses( false, $site_license_id, $foreign_licenses, $blog_id );
9646
+
9647
+ if ( $this->is_array_instanceof( $licenses, 'FS_Plugin_License' ) ) {
9648
+ $licenses_map = array();
9649
+ foreach ( $licenses as $license ) {
9650
+ $licenses_map[ $license->id ] = true;
9651
+ }
9652
+
9653
+ // $license_ids_to_keep = $this->get_license_ids_associated_with_installs();
9654
+ // foreach ( $license_ids_to_keep as $license_id ) {
9655
+ // if ( isset( $licenses_map[ $license_id ] ) ) {
9656
+ // continue;
9657
+ // }
9658
+ //
9659
+ // $missing_license = self::_get_license_by_id( $license_id, false );
9660
+ // if ( is_object( $missing_license ) ) {
9661
+ // $licenses[] = $missing_license;
9662
+ // $licenses_map[ $missing_license->id ] = true;
9663
+ // }
9664
+ // }
9665
+
9666
+ $user_license_ids = $this->get_user_linked_license_ids( $this->_user->id );
9667
+
9668
+ foreach ( $user_license_ids as $key => $license_id ) {
9669
+ if ( ! isset( $licenses_map[ $license_id ] ) ) {
9670
+ // Remove access to licenses that no longer exist.
9671
+ unset( $user_license_ids[ $key ] );
9672
+ }
9673
+ }
9674
+
9675
+ if ( ! empty( $user_license_ids ) ) {
9676
+ foreach ( $licenses_map as $license_id => $value ) {
9677
+ if ( ! isset( $all_licenses_map[ $license_id ] ) ) {
9678
+ // Associate new licenses with the user who triggered the license syncing.
9679
+ $user_license_ids[] = $license_id;
9680
+ }
9681
+ }
9682
+
9683
+ $user_license_ids = array_unique( $user_license_ids );
9684
+ } else {
9685
+ $user_license_ids = array_keys( $licenses_map );
9686
+ }
9687
+
9688
+ if ( ! $is_network_admin || ! is_null( $blog_id ) ) {
9689
+ $user_licenses = array();
9690
+ foreach ( $licenses as $license ) {
9691
+ if ( ! in_array( $license->id, $user_license_ids ) ) {
9692
+ continue;
9693
+ }
9694
+
9695
+ $user_licenses[] = $license;
9696
+ }
9697
+
9698
+ $this->_licenses = $user_licenses;
9699
+ } else {
9700
+ $this->_licenses = $licenses;
9701
+ }
9702
+
9703
+ $this->set_user_linked_license_ids( $this->_user->id, $user_license_ids );
9704
+
9705
+ $this->_store_licenses( true, $this->_module_id, $licenses );
9706
+ }
9707
+
9708
+ // Update current license.
9709
+ if ( is_object( $this->_license ) ) {
9710
+ $this->_license = $this->_get_license_by_id( $this->_license->id );
9711
+ }
9712
+
9713
+ return $this->_licenses;
9714
+ }
9715
+
9716
+ /**
9717
+ * @author Vova Feldman (@svovaf)
9718
+ * @since 1.0.5
9719
+ *
9720
+ * @param number $id
9721
+ * @param bool $sync_licenses
9722
+ *
9723
+ * @return FS_Plugin_License|false
9724
+ */
9725
+ function _get_license_by_id( $id, $sync_licenses = true ) {
9726
+ $this->_logger->entrance();
9727
+
9728
+ if ( ! FS_Plugin_License::is_valid_id( $id ) ) {
9729
+ return false;
9730
+ }
9731
+
9732
+ /**
9733
+ * When running from the network level admin and opted-in from the network,
9734
+ * check if the license exists in the network user licenses collection.
9735
+ *
9736
+ * @author Vova Feldman (@svovaf)
9737
+ * @since 2.0.0
9738
+ */
9739
+ if ( fs_is_network_admin() &&
9740
+ $this->is_network_registered() &&
9741
+ ( ! is_object( $this->_user ) || $this->_storage->network_user_id != $this->_user->id )
9742
+ ) {
9743
+ $licenses = $this->get_user_licenses( $this->_storage->network_user_id );
9744
+
9745
+ foreach ( $licenses as $license ) {
9746
+ if ( $id == $license->id ) {
9747
+ return $license;
9748
+ }
9749
+ }
9750
+ }
9751
+
9752
+ if ( ! $this->has_any_license() && $sync_licenses ) {
9753
+ $this->_sync_licenses( $id );
9754
+ }
9755
+
9756
+ if ( is_array( $this->_licenses ) ) {
9757
+ foreach ( $this->_licenses as $license ) {
9758
+ if ( $id == $license->id ) {
9759
+ return $license;
9760
+ }
9761
+ }
9762
+ }
9763
+
9764
+ return false;
9765
+ }
9766
+
9767
+ /**
9768
+ * 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.
9769
+ *
9770
+ * @author Vova Feldman (@svovaf)
9771
+ * @since 2.0.0
9772
+ *
9773
+ * @param number $id
9774
+ *
9775
+ * @return FS_Plugin_License
9776
+ */
9777
+ private function get_license_by_id( $id ) {
9778
+ $licenses = self::get_all_licenses( $this->_module_id );
9779
+
9780
+ if ( is_array( $licenses ) && ! empty( $licenses ) ) {
9781
+ foreach ( $licenses as $license ) {
9782
+ if ( $id == $license->id ) {
9783
+ return $license;
9784
+ }
9785
+ }
9786
+ }
9787
+
9788
+ return null;
9789
+ }
9790
+
9791
+ /**
9792
+ * Synchronize the site's context license by fetching the license form the API and updating the local data with it.
9793
+ *
9794
+ * @author Vova Feldman (@svovaf)
9795
+ * @since 2.0.0
9796
+ *
9797
+ * @return \FS_Plugin_License|mixed
9798
+ */
9799
+ private function sync_site_license() {
9800
+ $api = $this->get_api_user_scope();
9801
+
9802
+ $result = $api->get( "/licenses/{$this->_license->id}.json?license_key=" . urlencode( $this->_license->secret_key ), true );
9803
+
9804
+ if ( ! $this->is_api_result_entity( $result ) ) {
9805
+ return $result;
9806
+ }
9807
+
9808
+ $license = $this->_update_site_license( new FS_Plugin_License( $result ) );
9809
+ $this->_store_licenses();
9810
+
9811
+ return $license;
9812
+ }
9813
+
9814
+ /**
9815
+ * Get all user's available licenses for the current module.
9816
+ *
9817
+ * @author Vova Feldman (@svovaf)
9818
+ * @since 2.0.0
9819
+ *
9820
+ * @param number $user_id
9821
+ *
9822
+ * @return FS_Plugin_License[]
9823
+ */
9824
+ private function get_user_licenses( $user_id ) {
9825
+ $all_licenses = self::get_all_licenses( $this->_module_id );
9826
+ if ( empty( $all_licenses ) ) {
9827
+ return array();
9828
+ }
9829
+
9830
+ $user_license_ids = $this->get_user_linked_license_ids( $user_id );
9831
+ if ( empty( $user_license_ids ) ) {
9832
+ return array();
9833
+ }
9834
+
9835
+ $licenses = array();
9836
+ foreach ( $all_licenses as $license ) {
9837
+ if ( in_array( $license->id, $user_license_ids ) ) {
9838
+ $licenses[] = $license;
9839
+ }
9840
+ }
9841
+
9842
+ return $licenses;
9843
+ }
9844
+
9845
+ /**
9846
+ * Checks if the context license is network activated except on the given blog ID.
9847
+ *
9848
+ * @author Vova Feldman (@svovaf)
9849
+ * @since 2.0.0
9850
+ *
9851
+ * @param int $except_blog_id
9852
+ *
9853
+ * @return bool
9854
+ */
9855
+ private function is_license_network_active( $except_blog_id = 0 ) {
9856
+ $this->_logger->entrance();
9857
+
9858
+ if ( ! is_object( $this->_license ) ) {
9859
+ return false;
9860
+ }
9861
+
9862
+ $sites = self::get_sites();
9863
+
9864
+ if ( $this->_license->total_activations() < ( count( $sites ) - 1 ) ) {
9865
+ // There are more sites than the number of activations, so license cannot be network activated.
9866
+ return false;
9867
+ }
9868
+
9869
+ foreach ( $sites as $site ) {
9870
+ $blog_id = self::get_site_blog_id( $site );
9871
+
9872
+ if ( $except_blog_id == $blog_id ) {
9873
+ // Skip excluded blog.
9874
+ continue;
9875
+ }
9876
+
9877
+ $install = $this->get_install_by_blog_id( $blog_id );
9878
+
9879
+ if ( is_object( $install ) && $install->license_id != $this->_license->id ) {
9880
+ return false;
9881
+ }
9882
+ }
9883
+
9884
+ return true;
9885
+ }
9886
+
9887
+ /**
9888
+ * 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.
9889
+ *
9890
+ * Notice: On success, this method will also update the license activations counters (without updating the license in the storage).
9891
+ *
9892
+ * @author Vova Feldman (@svovaf)
9893
+ * @since 2.0.0
9894
+ *
9895
+ * @param \FS_User $user
9896
+ * @param \FS_Plugin_License $license
9897
+ *
9898
+ * @return bool
9899
+ */
9900
+ private function try_activate_license_on_network( FS_User $user, FS_Plugin_License $license ) {
9901
+ $this->_logger->entrance();
9902
+
9903
+ $result = $this->can_activate_license_on_network( $license );
9904
+
9905
+ if ( false === $result ) {
9906
+ return false;
9907
+ }
9908
+
9909
+ $installs_without_license = $result['installs'];
9910
+ if ( ! empty( $installs_without_license ) ) {
9911
+ $this->activate_license_on_many_installs( $user, $license->secret_key, $installs_without_license );
9912
+ }
9913
+
9914
+ $disconnected_site_ids = $result['sites'];
9915
+ if ( ! empty( $disconnected_site_ids ) ) {
9916
+ $this->activate_license_on_many_sites( $user, $license->secret_key, $disconnected_site_ids );
9917
+ }
9918
+
9919
+ $this->link_license_2_user( $license->id, $user->id );
9920
+
9921
+ // Sync license after activations.
9922
+ $license->activated += $result['production_count'];
9923
+ $license->activated_local += $result['localhost_count'];
9924
+
9925
+ // $this->_store_licenses()
9926
+
9927
+ return true;
9928
+ }
9929
+
9930
+ /**
9931
+ * Checks if the given license can be activated on the whole network.
9932
+ *
9933
+ * @author Vova Feldman (@svovaf)
9934
+ * @since 2.0.0
9935
+ *
9936
+ * @param \FS_Plugin_License $license
9937
+ *
9938
+ * @return false|array {
9939
+ * @type array[int]FS_Site $installs Blog ID to install map.
9940
+ * @type int[] $sites Non-connected blog IDs.
9941
+ * @type int $production_count Production sites count.
9942
+ * @type int $localhost_count Production sites count.
9943
+ * }
9944
+ */
9945
+ private function can_activate_license_on_network( FS_Plugin_License $license ) {
9946
+ $sites = self::get_sites();
9947
+
9948
+ $production_count = 0;
9949
+ $localhost_count = 0;
9950
+
9951
+ $installs_without_license = array();
9952
+ $disconnected_site_ids = array();
9953
+
9954
+ foreach ( $sites as $site ) {
9955
+ $blog_id = self::get_site_blog_id( $site );
9956
+ $install = $this->get_install_by_blog_id( $blog_id );
9957
+
9958
+ if ( is_object( $install ) ) {
9959
+ if ( FS_Plugin_License::is_valid_id( $install->license_id ) ) {
9960
+ // License already activated on the install.
9961
+ continue;
9962
+ }
9963
+
9964
+ $url = $install->url;
9965
+
9966
+ $installs_without_license[ $blog_id ] = $install;
9967
+ } else {
9968
+ $url = is_object( $site ) ?
9969
+ $site->siteurl :
9970
+ get_site_url( $blog_id );
9971
+
9972
+ $disconnected_site_ids[] = $blog_id;
9973
+ }
9974
+
9975
+ if ( FS_Site::is_localhost_by_address( $url ) ) {
9976
+ $localhost_count ++;
9977
+ } else {
9978
+ $production_count ++;
9979
+ }
9980
+ }
9981
+
9982
+ if ( ! $license->can_activate_bulk( $production_count, $localhost_count ) ) {
9983
+ return false;
9984
+ }
9985
+
9986
+ return array(
9987
+ 'installs' => $installs_without_license,
9988
+ 'sites' => $disconnected_site_ids,
9989
+ 'production_count' => $production_count,
9990
+ 'localhost_count' => $localhost_count,
9991
+ );
9992
+ }
9993
+
9994
+ /**
9995
+ * Activate a given license on a collection of installs.
9996
+ *
9997
+ * @author Vova Feldman (@svovaf)
9998
+ * @since 2.0.0
9999
+ *
10000
+ * @param \FS_User $user
10001
+ * @param string $license_key
10002
+ * @param array $blog_2_install_map {
10003
+ * @key int Blog ID.
10004
+ * @value FS_Site Blog's associated install.
10005
+ * }
10006
+ *
10007
+ * @return mixed|true
10008
+ */
10009
+ private function activate_license_on_many_installs(
10010
+ FS_User $user,
10011
+ $license_key,
10012
+ array $blog_2_install_map
10013
+ ) {
10014
+ $params = array(
10015
+ array( 'license_key' => $this->apply_filters( 'license_key', $license_key ) )
10016
+ );
10017
+
10018
+ $install_2_blog_map = array();
10019
+ foreach ( $blog_2_install_map as $blog_id => $install ) {
10020
+ $params[] = array( 'id' => $install->id );
10021
+
10022
+ $install_2_blog_map[ $install->id ] = $blog_id;
10023
+ }
10024
+
10025
+ $result = $this->get_api_user_scope_by_user( $user )->call(
10026
+ "plugins/{$this->_plugin->id}/installs.json",
10027
+ 'PUT',
10028
+ $params
10029
+ );
10030
+
10031
+ if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
10032
+ return $result;
10033
+ }
10034
+
10035
+ foreach ( $result->installs as $r_install ) {
10036
+ $install = new FS_Site( $r_install );
10037
+ $install->is_disconnected = false;
10038
+
10039
+ // Update install.
10040
+ $this->_store_site(
10041
+ true,
10042
+ $install_2_blog_map[ $r_install->id ],
10043
+ $install
10044
+ );
10045
+ }
10046
+
10047
+ return true;
10048
+ }
10049
+
10050
+ /**
10051
+ * Activate a given license on a collection of blogs/sites that are not yet opted-in.
10052
+ *
10053
+ * @author Vova Feldman (@svovaf)
10054
+ * @since 2.0.0
10055
+ *
10056
+ * @param \FS_User $user
10057
+ * @param string $license_key
10058
+ * @param int[] $site_ids
10059
+ *
10060
+ * @return true|mixed True if successful, otherwise, the API result.
10061
+ */
10062
+ private function activate_license_on_many_sites(
10063
+ FS_User $user,
10064
+ $license_key,
10065
+ array $site_ids
10066
+ ) {
10067
+ $sites = array();
10068
+ foreach ( $site_ids as $site_id ) {
10069
+ $sites[] = $this->get_site_info( array( 'blog_id' => $site_id ) );
10070
+ }
10071
+
10072
+ // Install the plugin.
10073
+ $result = $this->create_installs_with_user(
10074
+ $user,
10075
+ $license_key,
10076
+ false,
10077
+ $sites,
10078
+ false,
10079
+ true
10080
+ );
10081
+
10082
+ if ( ! $this->is_api_result_entity( $result ) &&
10083
+ ! $this->is_api_result_object( $result, 'installs' )
10084
+ ) {
10085
+ return $result;
10086
+ }
10087
+
10088
+ $installs = array();
10089
+ foreach ( $result->installs as $install ) {
10090
+ $installs[] = new FS_Site( $install );
10091
+ }
10092
+
10093
+ // Map site addresses to their blog IDs.
10094
+ $address_to_blog_map = $this->get_address_to_blog_map();
10095
+
10096
+ $first_blog_id = null;
10097
+
10098
+ foreach ( $installs as $install ) {
10099
+ $address = trailingslashit( fs_strip_url_protocol( $install->url ) );
10100
+ $blog_id = $address_to_blog_map[ $address ];
10101
+
10102
+ $this->_store_site( true, $blog_id, $install );
10103
+
10104
+ $this->reset_anonymous_mode( $blog_id );
10105
+
10106
+ if ( is_null( $first_blog_id ) ) {
10107
+ $first_blog_id = $blog_id;
10108
+ }
10109
+ }
10110
+
10111
+ if ( ! FS_Site::is_valid_id( $this->_storage->network_install_blog_id ) ) {
10112
+ $this->_storage->network_install_blog_id = $first_blog_id;
10113
+ }
10114
+
10115
+ return true;
10116
+ }
10117
+
10118
+ /**
10119
+ * Sync site's license with user licenses.
10120
+ *
10121
+ * @author Vova Feldman (@svovaf)
10122
+ * @since 1.0.6
10123
+ *
10124
+ * @param FS_Plugin_License|null $new_license
10125
+ *
10126
+ * @return FS_Plugin_License|null
10127
+ */
10128
+ function _update_site_license( $new_license ) {
10129
+ $this->_logger->entrance();
10130
+
10131
+ $this->_license = $new_license;
10132
+
10133
+ if ( ! is_object( $new_license ) ) {
10134
+ $this->_site->license_id = null;
10135
+ $this->_sync_site_subscription( null );
10136
+
10137
+ return $this->_license;
10138
+ }
10139
+
10140
+ $this->_site->license_id = $this->_license->id;
10141
+
10142
+ if ( ! is_array( $this->_licenses ) ) {
10143
+ $this->_licenses = array();
10144
+ }
10145
+
10146
+ $is_license_found = false;
10147
+ for ( $i = 0, $len = count( $this->_licenses ); $i < $len; $i ++ ) {
10148
+ if ( $new_license->id == $this->_licenses[ $i ]->id ) {
10149
+ $this->_licenses[ $i ] = $new_license;
10150
+
10151
+ $is_license_found = true;
10152
+ break;
10153
+ }
10154
+ }
10155
+
10156
+ // If new license just append.
10157
+ if ( ! $is_license_found ) {
10158
+ $this->_licenses[] = $new_license;
10159
+ }
10160
+
10161
+ $this->_sync_site_subscription( $new_license );
10162
+
10163
+ return $this->_license;
10164
+ }
10165
+
10166
+ /**
10167
+ * Sync site's subscription.
10168
+ *
10169
+ * @author Vova Feldman (@svovaf)
10170
+ * @since 1.0.9
10171
+ *
10172
+ * @param FS_Plugin_License|null $license
10173
+ *
10174
+ * @return bool|\FS_Subscription
10175
+ */
10176
+ private function _sync_site_subscription( $license ) {
10177
+ if ( ! is_object( $license ) ) {
10178
+ $this->delete_unused_subscriptions();
10179
+
10180
+ return false;
10181
+ }
10182
+
10183
+ // Load subscription details if not lifetime.
10184
+ $subscription = $license->is_lifetime() ?
10185
+ false :
10186
+ $this->_fetch_site_license_subscription();
10187
+
10188
+ if ( is_object( $subscription ) && ! isset( $subscription->error ) ) {
10189
+ $this->store_subscription( $subscription );
10190
+ } else {
10191
+ $this->delete_unused_subscriptions();
10192
+ }
10193
+
10194
+ return $subscription;
10195
+ }
10196
+
10197
+ /**
10198
+ * @author Vova Feldman (@svovaf)
10199
+ * @since 1.0.6
10200
+ *
10201
+ * @return bool|\FS_Plugin_License
10202
+ */
10203
+ function _get_license() {
10204
+ if ( ! fs_is_network_admin() || is_object( $this->_license ) ) {
10205
+ return $this->_license;
10206
+ }
10207
+
10208
+ return $this->_get_available_premium_license();
10209
+ }
10210
+
10211
+ /**
10212
+ * @param number $license_id
10213
+ *
10214
+ * @return null|\FS_Subscription
10215
+ */
10216
+ function _get_subscription( $license_id ) {
10217
+ if ( ! isset( $this->_storage->subscriptions ) ||
10218
+ empty( $this->_storage->subscriptions )
10219
+ ) {
10220
+ return null;
10221
+ }
10222
+
10223
+ foreach ( $this->_storage->subscriptions as $subscription ) {
10224
+ if ( $subscription->license_id == $license_id ) {
10225
+ return $subscription;
10226
+ }
10227
+ }
10228
+
10229
+ return null;
10230
+ }
10231
+
10232
+ /**
10233
+ * @author Leo Fajardo (@leorw)
10234
+ * @since 2.0.0
10235
+ *
10236
+ * @param FS_Subscription $subscription
10237
+ */
10238
+ function store_subscription( FS_Subscription $subscription ) {
10239
+ if ( ! isset( $this->_storage->subscriptions ) ) {
10240
+ $this->_storage->subscriptions = array();
10241
+ }
10242
+
10243
+ if ( empty( $this->_storage->subscriptions ) || ! is_multisite() ) {
10244
+ $this->_storage->subscriptions = array( $subscription );
10245
+
10246
+ return;
10247
+ }
10248
+
10249
+ $subscriptions = $this->_storage->subscriptions;
10250
+
10251
+ $updated_subscription = false;
10252
+ foreach ( $subscriptions as $key => $existing_subscription ) {
10253
+ if ( $existing_subscription->id == $subscription->id ) {
10254
+ $subscriptions[ $key ] = $subscription;
10255
+ $updated_subscription = true;
10256
+ break;
10257
+ }
10258
+ }
10259
+
10260
+ if ( ! $updated_subscription ) {
10261
+ $subscriptions[] = $subscription;
10262
+ }
10263
+
10264
+ $this->_storage->subscriptions = $subscriptions;
10265
+ }
10266
+
10267
+ /**
10268
+ * @author Leo Fajardo (@leorw)
10269
+ * @since 2.0.0
10270
+ */
10271
+ function delete_unused_subscriptions() {
10272
+ if ( ! isset( $this->_storage->subscriptions ) ||
10273
+ empty( $this->_storage->subscriptions ) ||
10274
+ // Clean up only if there are already at least 3 subscriptions.
10275
+ ( count( $this->_storage->subscriptions ) < 3 )
10276
+ ) {
10277
+ return;
10278
+ }
10279
+
10280
+ if ( ! is_multisite() ) {
10281
+ // If not multisite, there should only be 1 subscription, so just clear the array.
10282
+ $this->_storage->subscriptions = array();
10283
+
10284
+ return;
10285
+ }
10286
+
10287
+ $subscriptions_to_keep_by_license_id_map = array();
10288
+ $sites = self::get_sites();
10289
+ foreach ( $sites as $site ) {
10290
+ $blog_id = self::get_site_blog_id( $site );
10291
+ $install = $this->get_install_by_blog_id( $blog_id );
10292
+
10293
+ if ( ! is_object( $install ) ||
10294
+ ! FS_Plugin_License::is_valid_id( $install->license_id )
10295
+ ) {
10296
+ continue;
10297
+ }
10298
+
10299
+ $subscriptions_to_keep_by_license_id_map[ $install->license_id ] = true;
10300
+ }
10301
+
10302
+ if ( empty( $subscriptions_to_keep_by_license_id_map ) ) {
10303
+ $this->_storage->subscriptions = array();
10304
+
10305
+ return;
10306
+ }
10307
+
10308
+ foreach ( $this->_storage->subscriptions as $key => $subscription ) {
10309
+ if ( ! isset( $subscriptions_to_keep_by_license_id_map[ $subscription->license_id ] ) ) {
10310
+ unset( $this->_storage->subscriptions[ $key ] );
10311
+ }
10312
+ }
10313
+ }
10314
+
10315
+ /**
10316
+ * @author Vova Feldman (@svovaf)
10317
+ * @since 1.0.2
10318
+ *
10319
+ * @param string $plan Plan name
10320
+ * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
10321
+ *
10322
+ * @return bool
10323
+ */
10324
+ function is_plan( $plan, $exact = false ) {
10325
+ $this->_logger->entrance();
10326
+
10327
+ if ( ! $this->is_registered() ) {
10328
+ return false;
10329
+ }
10330
+
10331
+ $plan = strtolower( $plan );
10332
+
10333
+ $current_plan_name = $this->get_plan_name();
10334
+
10335
+ if ( $current_plan_name === $plan ) {
10336
+ // Exact plan.
10337
+ return true;
10338
+ } else if ( $exact ) {
10339
+ // Required exact, but plans are different.
10340
+ return false;
10341
+ }
10342
+
10343
+ $current_plan_order = - 1;
10344
+ $required_plan_order = - 1;
10345
+ for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
10346
+ if ( $plan === $this->_plans[ $i ]->name ) {
10347
+ $required_plan_order = $i;
10348
+ } else if ( $current_plan_name === $this->_plans[ $i ]->name ) {
10349
+ $current_plan_order = $i;
10350
+ }
10351
+ }
10352
+
10353
+ return ( $current_plan_order > $required_plan_order );
10354
+ }
10355
+
10356
+ /**
10357
+ * Check if module has only one plan.
10358
+ *
10359
+ * @author Vova Feldman (@svovaf)
10360
+ * @since 1.2.1.7
10361
+ *
10362
+ * @return bool
10363
+ */
10364
+ function is_single_plan() {
10365
+ $this->_logger->entrance();
10366
+
10367
+ if ( ! $this->is_registered() ||
10368
+ ! is_array( $this->_plans ) ||
10369
+ 0 === count( $this->_plans )
10370
+ ) {
10371
+ return true;
10372
+ }
10373
+
10374
+ return ( 1 === count( $this->_plans ) );
10375
+ }
10376
+
10377
+ /**
10378
+ * Check if plan based on trial. If not in trial mode, should return false.
10379
+ *
10380
+ * @since 1.0.9
10381
+ *
10382
+ * @param string $plan Plan name
10383
+ * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
10384
+ *
10385
+ * @return bool
10386
+ */
10387
+ function is_trial_plan( $plan, $exact = false ) {
10388
+ $this->_logger->entrance();
10389
+
10390
+ if ( ! $this->is_registered() ) {
10391
+ return false;
10392
+ }
10393
+
10394
+ if ( ! $this->is_trial() ) {
10395
+ return false;
10396
+ }
10397
+
10398
+ $trial_plan = $this->get_trial_plan();
10399
+
10400
+ if ( $trial_plan->name === $plan ) {
10401
+ // Exact plan.
10402
+ return true;
10403
+ } else if ( $exact ) {
10404
+ // Required exact, but plans are different.
10405
+ return false;
10406
+ }
10407
+
10408
+ $current_plan_order = - 1;
10409
+ $required_plan_order = - 1;
10410
+ for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
10411
+ if ( $plan === $this->_plans[ $i ]->name ) {
10412
+ $required_plan_order = $i;
10413
+ } else if ( $trial_plan->name === $this->_plans[ $i ]->name ) {
10414
+ $current_plan_order = $i;
10415
+ }
10416
+ }
10417
+
10418
+ return ( $current_plan_order > $required_plan_order );
10419
+ }
10420
+
10421
+ /**
10422
+ * Check if plugin has any paid plans.
10423
+ *
10424
+ * @author Vova Feldman (@svovaf)
10425
+ * @since 1.0.7
10426
+ *
10427
+ * @return bool
10428
+ */
10429
+ function has_paid_plan() {
10430
+ return $this->_has_paid_plans ||
10431
+ FS_Plan_Manager::instance()->has_paid_plan( $this->_plans );
10432
+ }
10433
+
10434
+ /**
10435
+ * Check if plugin has any plan with a trail.
10436
+ *
10437
+ * @author Vova Feldman (@svovaf)
10438
+ * @since 1.0.9
10439
+ *
10440
+ * @return bool
10441
+ */
10442
+ function has_trial_plan() {
10443
+ /**
10444
+ * @author Vova Feldman(@svovaf)
10445
+ * @since 1.2.1.5
10446
+ *
10447
+ * Allow setting a trial from the SDK without calling the API.
10448
+ * But, if the user did opt-in, continue using the real data from the API.
10449
+ */
10450
+ if ( $this->_trial_days >= 0 ) {
10451
+ return true;
10452
+ }
10453
+
10454
+ return $this->_storage->get( 'has_trial_plan', false );
10455
+ }
10456
+
10457
+ /**
10458
+ * Check if plugin has any free plan, or is it premium only.
10459
+ *
10460
+ * Note: If no plans configured, assume plugin is free.
10461
+ *
10462
+ * @author Vova Feldman (@svovaf)
10463
+ * @since 1.0.7
10464
+ *
10465
+ * @return bool
10466
+ */
10467
+ function has_free_plan() {
10468
+ return ! $this->is_only_premium();
10469
+ }
10470
+
10471
+ /**
10472
+ * Displays a license activation dialog box when the user clicks on the "Activate License"
10473
+ * or "Change License" link on the plugins
10474
+ * page.
10475
+ *
10476
+ * @author Leo Fajardo (@leorw)
10477
+ * @since 1.1.9
10478
+ */
10479
+ function _add_license_activation_dialog_box() {
10480
+ $vars = array(
10481
+ 'id' => $this->_module_id,
10482
+ );
10483
+
10484
+ fs_require_template( 'forms/license-activation.php', $vars );
10485
+ fs_require_template( 'forms/resend-key.php', $vars );
10486
+ }
10487
+
10488
+ /**
10489
+ * @author Leo Fajardo (@leorw)
10490
+ * @since 2.0.2
10491
+ */
10492
+ function _add_premium_version_upgrade_selection_dialog_box() {
10493
+ $modules_update = get_site_transient( $this->is_theme() ? 'update_themes' : 'update_plugins' );
10494
+ if ( ! isset( $modules_update->response[ $this->_plugin_basename ] ) ) {
10495
+ return;
10496
+ }
10497
+
10498
+ $vars = array(
10499
+ 'id' => $this->_module_id,
10500
+ 'new_version' => is_object( $modules_update->response[ $this->_plugin_basename ] ) ?
10501
+ $modules_update->response[ $this->_plugin_basename ]->new_version :
10502
+ $modules_update->response[ $this->_plugin_basename ]['new_version']
10503
+ );
10504
+
10505
+ fs_require_template( 'forms/premium-versions-upgrade-metadata.php', $vars );
10506
+ fs_require_once_template( 'forms/premium-versions-upgrade-handler.php', $vars );
10507
+ }
10508
+
10509
+ /**
10510
+ * Displays the opt-out dialog box when the user clicks on the "Opt Out" link on the "Plugins"
10511
+ * page.
10512
+ *
10513
+ * @author Leo Fajardo (@leorw)
10514
+ * @since 1.2.1.5
10515
+ */
10516
+ function _add_optout_dialog() {
10517
+ if ( $this->is_theme() ) {
10518
+ $vars = null;
10519
+ fs_require_once_template( '/js/jquery.content-change.php', $vars );
10520
+ }
10521
+
10522
+ $vars = array( 'id' => $this->_module_id );
10523
+ fs_require_template( 'forms/optout.php', $vars );
10524
+ }
10525
+
10526
+ /**
10527
+ * Prepare page to include all required UI and logic for the license activation dialog.
10528
+ *
10529
+ * @author Vova Feldman (@svovaf)
10530
+ * @since 1.2.0
10531
+ */
10532
+ function _add_license_activation() {
10533
+ if ( ! $this->is_user_admin() ) {
10534
+ // Only admins can activate a license.
10535
+ return;
10536
+ }
10537
+
10538
+ if ( ! $this->has_paid_plan() ) {
10539
+ // Module doesn't have any paid plans.
10540
+ return;
10541
+ }
10542
+
10543
+ if ( ! $this->is_premium() ) {
10544
+ // Only add license activation logic to the premium version.
10545
+ return;
10546
+ }
10547
+
10548
+ // Add license activation link and AJAX request handler.
10549
+ if ( self::is_plugins_page() ) {
10550
+ /**
10551
+ * @since 1.2.0 Add license action link only on plugins page.
10552
+ */
10553
+ $this->_add_license_action_link();
10554
+ }
10555
+
10556
+ // Add license activation AJAX callback.
10557
+ $this->add_ajax_action( 'activate_license', array( &$this, '_activate_license_ajax_action' ) );
10558
+
10559
+ // Add resend license AJAX callback.
10560
+ $this->add_ajax_action( 'resend_license_key', array( &$this, '_resend_license_key_ajax_action' ) );
10561
+ }
10562
+
10563
+ /**
10564
+ * @author Leo Fajardo (@leorw)
10565
+ * @since 2.0.2
10566
+ */
10567
+ function _add_premium_version_upgrade_selection() {
10568
+ if ( ! $this->is_user_admin() ) {
10569
+ return;
10570
+ }
10571
+
10572
+ if ( ! $this->is_premium() || $this->has_active_valid_license() ) {
10573
+ // This is relevant only to the free versions and premium versions without an active license.
10574
+ return;
10575
+ }
10576
+
10577
+ if ( self::is_updates_page() || ( $this->is_plugin() && self::is_plugins_page() ) ) {
10578
+ $this->_add_premium_version_upgrade_selection_action();
10579
+ }
10580
+ }
10581
+
10582
+ /**
10583
+ * @author Leo Fajardo (@leorw)
10584
+ *
10585
+ * @since 1.1.9
10586
+ * @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).
10587
+ */
10588
+ function _activate_license_ajax_action() {
10589
+ $this->_logger->entrance();
10590
+
10591
+ $this->check_ajax_referer( 'activate_license' );
10592
+
10593
+ $license_key = trim( fs_request_get( 'license_key' ) );
10594
+
10595
+ if ( empty( $license_key ) ) {
10596
+ exit;
10597
+ }
10598
+
10599
+ $plugin_id = fs_request_get( 'module_id', '', 'post' );
10600
+ $fs = ( $plugin_id == $this->_module_id ) ?
10601
+ $this :
10602
+ $this->get_addon_instance( $plugin_id );
10603
+
10604
+ $error = false;
10605
+ $next_page = false;
10606
+
10607
+ $sites = fs_is_network_admin() ?
10608
+ fs_request_get( 'sites', array(), 'post' ) :
10609
+ array();
10610
+
10611
+ $blog_id = fs_request_get( 'blog_id' );
10612
+ $has_valid_blog_id = is_numeric( $blog_id );
10613
+
10614
+ if ( $fs->is_registered() ) {
10615
+ if ( fs_is_network_admin() && ! $has_valid_blog_id ) {
10616
+ // If no specific blog ID was provided, activate the license for all sites in the network.
10617
+ $blog_2_install_map = array();
10618
+ $site_ids = array();
10619
+
10620
+ foreach ( $sites as $site ) {
10621
+ if ( ! isset( $site['blog_id'] ) || ! is_numeric( $site['blog_id'] ) ) {
10622
+ continue;
10623
+ }
10624
+
10625
+ $install = $this->get_install_by_blog_id( $site['blog_id'] );
10626
+
10627
+ if ( is_object( $install ) ) {
10628
+ $blog_2_install_map[ $site['blog_id'] ] = $install;
10629
+ } else {
10630
+ $site_ids[] = $site['blog_id'];
10631
+ }
10632
+ }
10633
+
10634
+ $user = $this->get_current_or_network_user();
10635
+
10636
+ if ( ! empty( $blog_2_install_map ) ) {
10637
+ $result = $this->activate_license_on_many_installs( $user, $license_key, $blog_2_install_map );
10638
+
10639
+ if ( true !== $result ) {
10640
+ $error = FS_Api::is_api_error_object( $result ) ?
10641
+ $result->error->message :
10642
+ var_export( $result, true );
10643
+ }
10644
+ }
10645
+
10646
+ if ( empty( $error ) && ! empty( $site_ids ) ) {
10647
+ $result = $this->activate_license_on_many_sites( $user, $license_key, $site_ids );
10648
+
10649
+ if ( true !== $result ) {
10650
+ $error = FS_Api::is_api_error_object( $result ) ?
10651
+ $result->error->message :
10652
+ var_export( $result, true );
10653
+ }
10654
+ }
10655
+ } else {
10656
+ if ( $has_valid_blog_id ) {
10657
+ /**
10658
+ * If a specific blog ID was provided, activate the license only for the install that is
10659
+ * associated with the given blog ID.
10660
+ *
10661
+ * @author Leo Fajardo (@leorw)
10662
+ */
10663
+ $this->switch_to_blog( $blog_id );
10664
+ }
10665
+
10666
+ $api = $fs->get_api_site_scope();
10667
+
10668
+ $params = array(
10669
+ 'license_key' => $fs->apply_filters( 'license_key', $license_key )
10670
+ );
10671
+
10672
+ $install = $api->call( '/', 'put', $params );
10673
+
10674
+ if ( FS_Api::is_api_error( $install ) ) {
10675
+ $error = FS_Api::is_api_error_object( $install ) ?
10676
+ $install->error->message :
10677
+ var_export( $install->error, true );
10678
+ } else {
10679
+ $fs->reconnect_locally( $has_valid_blog_id );
10680
+ }
10681
+ }
10682
+
10683
+ if ( empty( $error ) ) {
10684
+ $this->network_upgrade_mode_completed();
10685
+
10686
+ $fs->_sync_license( true, $has_valid_blog_id );
10687
+
10688
+ $next_page = $fs->is_addon() ?
10689
+ $fs->get_parent_instance()->get_account_url() :
10690
+ $fs->get_account_url();
10691
+ }
10692
+ } else {
10693
+ $next_page = $fs->opt_in(
10694
+ false,
10695
+ false,
10696
+ false,
10697
+ $license_key,
10698
+ false,
10699
+ false,
10700
+ false,
10701
+ fs_request_get_bool( 'is_marketing_allowed', null ),
10702
+ $sites
10703
+ );
10704
+
10705
+ if ( isset( $next_page->error ) ) {
10706
+ $error = $next_page->error;
10707
+ } else {
10708
+ if ( fs_is_network_admin() ) {
10709
+ /**
10710
+ * Get the list of sites that were just opted-in (and license activated).
10711
+ * This is an optimization for the next part below saving some DB queries.
10712
+ */
10713
+ $connected_sites = array();
10714
+ foreach ( $sites as $site ) {
10715
+ if ( isset( $site['blog_id'] ) && is_numeric( $site['blog_id'] ) ) {
10716
+ $connected_sites[ $site['blog_id'] ] = true;
10717
+ }
10718
+ }
10719
+
10720
+ $all_sites = self::get_sites();
10721
+ $pending_sites = array();
10722
+
10723
+ /**
10724
+ * 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.
10725
+ *
10726
+ * @author Vova Feldman (@svovaf)
10727
+ */
10728
+ foreach ( $all_sites as $site ) {
10729
+ $blog_id = self::get_site_blog_id( $site );
10730
+
10731
+ if ( isset( $connected_sites[ $blog_id ] ) ) {
10732
+ // Site was just connected.
10733
+ continue;
10734
+ }
10735
+
10736
+ if ( $this->is_installed_on_site( $blog_id ) ) {
10737
+ // Site was already connected before.
10738
+ continue;
10739
+ }
10740
+
10741
+ if ( $this->is_site_delegated_connection( $blog_id ) ) {
10742
+ // Site's connection was delegated.
10743
+ continue;
10744
+ }
10745
+
10746
+ if ( $this->is_anonymous_site( $blog_id ) ) {
10747
+ // Site connection was already skipped.
10748
+ continue;
10749
+ }
10750
+
10751
+ $pending_sites[] = self::get_site_info( $site );
10752
+ }
10753
+
10754
+ if ( ! empty( $pending_sites ) ) {
10755
+ if ( $this->is_freemium() ) {
10756
+ $this->skip_connection( $pending_sites );
10757
+ } else {
10758
+ $this->delegate_connection( $pending_sites );
10759
+ }
10760
+ }
10761
+ }
10762
+ }
10763
+ }
10764
+
10765
+ $result = array(
10766
+ 'success' => ( false === $error )
10767
+ );
10768
+
10769
+ if ( false !== $error ) {
10770
+ $result['error'] = $error;
10771
+ } else {
10772
+ $result['next_page'] = $next_page;
10773
+ }
10774
+
10775
+ echo json_encode( $result );
10776
+
10777
+ exit;
10778
+ }
10779
+
10780
+ /**
10781
+ * @author Leo Fajardo (@leorw)
10782
+ * @since 1.2.3.1
10783
+ */
10784
+ function _network_activate_ajax_action() {
10785
+ $this->_logger->entrance();
10786
+
10787
+ $this->check_ajax_referer( 'network_activate' );
10788
+
10789
+ $plugin_id = fs_request_get( 'module_id', '', 'post' );
10790
+ $fs = ( $plugin_id == $this->_module_id ) ?
10791
+ $this :
10792
+ $this->get_addon_instance( $plugin_id );
10793
+
10794
+ $error = false;
10795
+
10796
+ $sites = fs_request_get( 'sites', array(), 'post' );
10797
+ if ( is_array( $sites ) && ! empty( $sites ) ) {
10798
+ $sites_by_action = array(
10799
+ 'allow' => array(),
10800
+ 'delegate' => array(),
10801
+ 'skip' => array()
10802
+ );
10803
+
10804
+ foreach ( $sites as $site ) {
10805
+ $sites_by_action[ $site['action'] ][] = $site;
10806
+ }
10807
+
10808
+ $total_sites = count( $sites );
10809
+ $total_sites_to_delegate = count( $sites_by_action['delegate'] );
10810
+
10811
+ $next_page = '';
10812
+ if ( $total_sites === $total_sites_to_delegate &&
10813
+ ! $this->is_network_upgrade_mode()
10814
+ ) {
10815
+ $this->delegate_connection();
10816
+ } else {
10817
+ if ( ! empty( $sites_by_action['delegate'] ) ) {
10818
+ $this->delegate_connection( $sites_by_action['delegate'] );
10819
+ }
10820
+
10821
+ if ( ! empty( $sites_by_action['skip'] ) ) {
10822
+ $this->skip_connection( $sites_by_action['skip'] );
10823
+ }
10824
+
10825
+ if ( ! empty( $sites_by_action['allow'] ) ) {
10826
+ if ( ! $fs->is_registered() || ! $this->_is_network_active ) {
10827
+ $next_page = $fs->opt_in(
10828
+ false,
10829
+ false,
10830
+ false,
10831
+ false,
10832
+ false,
10833
+ false,
10834
+ false,
10835
+ fs_request_get_bool( 'is_marketing_allowed', null ),
10836
+ $sites_by_action['allow']
10837
+ );
10838
+ } else {
10839
+ $next_page = $fs->install_with_user(
10840
+ $this->get_network_user(),
10841
+ false,
10842
+ false,
10843
+ false,
10844
+ true,
10845
+ $sites_by_action['allow']
10846
+ );
10847
+ }
10848
+
10849
+ if ( is_object( $next_page ) && isset( $next_page->error ) ) {
10850
+ $error = $next_page->error;
10851
+ }
10852
+ }
10853
+ }
10854
+
10855
+ if ( empty( $next_page ) ) {
10856
+ $next_page = $this->get_after_activation_url( 'after_network_activation_url' );
10857
+ }
10858
+ } else {
10859
+ $error = $this->get_text_inline( 'Invalid site details collection.', 'invalid_site_details_collection' );
10860
+ }
10861
+
10862
+ $result = array(
10863
+ 'success' => ( false === $error )
10864
+ );
10865
+
10866
+ if ( false !== $error ) {
10867
+ $result['error'] = $error;
10868
+ } else {
10869
+ $result['next_page'] = $next_page;
10870
+ }
10871
+
10872
+ echo json_encode( $result );
10873
+
10874
+ exit;
10875
+ }
10876
+
10877
+ /**
10878
+ * Billing update AJAX callback.
10879
+ *
10880
+ * @author Vova Feldman (@svovaf)
10881
+ * @since 1.2.1.5
10882
+ */
10883
+ function _update_billing_ajax_action() {
10884
+ $this->_logger->entrance();
10885
+
10886
+ $this->check_ajax_referer( 'update_billing' );
10887
+
10888
+ if ( ! $this->is_user_admin() ) {
10889
+ // Only for admins.
10890
+ self::shoot_ajax_failure();
10891
+ }
10892
+
10893
+ $billing = fs_request_get( 'billing' );
10894
+
10895
+ $api = $this->get_api_user_scope();
10896
+ $result = $api->call( '/billing.json', 'put', array_merge( $billing, array(
10897
+ 'plugin_id' => $this->get_parent_id(),
10898
+ ) ) );
10899
+
10900
+ if ( ! $this->is_api_result_entity( $result ) ) {
10901
+ self::shoot_ajax_failure();
10902
+ }
10903
+
10904
+ // Purge cached billing.
10905
+ $this->get_api_user_scope()->purge_cache( 'billing.json' );
10906
+
10907
+ self::shoot_ajax_success();
10908
+ }
10909
+
10910
+ /**
10911
+ * Trial start for anonymous users (AJAX callback).
10912
+ *
10913
+ * @author Vova Feldman (@svovaf)
10914
+ * @since 1.2.1.5
10915
+ */
10916
+ function _start_trial_ajax_action() {
10917
+ $this->_logger->entrance();
10918
+
10919
+ $this->check_ajax_referer( 'start_trial' );
10920
+
10921
+ if ( ! $this->is_user_admin() ) {
10922
+ // Only for admins.
10923
+ self::shoot_ajax_failure();
10924
+ }
10925
+
10926
+ $trial_data = fs_request_get( 'trial' );
10927
+
10928
+ $next_page = $this->opt_in(
10929
+ false,
10930
+ false,
10931
+ false,
10932
+ false,
10933
+ false,
10934
+ $trial_data['plan_id']
10935
+ );
10936
+
10937
+ if ( is_object( $next_page ) && $this->is_api_error( $next_page ) ) {
10938
+ self::shoot_ajax_failure(
10939
+ isset( $next_page->error ) ?
10940
+ $next_page->error->message :
10941
+ var_export( $next_page, true )
10942
+ );
10943
+ }
10944
+
10945
+ $this->shoot_ajax_success( array(
10946
+ 'next_page' => $next_page,
10947
+ ) );
10948
+ }
10949
+
10950
+ /**
10951
+ * @author Leo Fajardo (@leorw)
10952
+ * @since 1.2.0
10953
+ */
10954
+ function _resend_license_key_ajax_action() {
10955
+ $this->_logger->entrance();
10956
+
10957
+ $this->check_ajax_referer( 'resend_license_key' );
10958
+
10959
+ $email_address = sanitize_email( trim( fs_request_get( 'email', '', 'post' ) ) );
10960
+
10961
+ if ( empty( $email_address ) ) {
10962
+ exit;
10963
+ }
10964
+
10965
+ $error = false;
10966
+
10967
+ $api = $this->get_api_plugin_scope();
10968
+ $result = $api->call( '/licenses/resend.json', 'post',
10969
+ array(
10970
+ 'email' => $email_address,
10971
+ 'url' => home_url(),
10972
+ )
10973
+ );
10974
+
10975
+ if ( is_object( $result ) && isset( $result->error ) ) {
10976
+ $error = $result->error;
10977
+
10978
+ if ( in_array( $error->code, array( 'invalid_email', 'no_user' ) ) ) {
10979
+ $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' );
10980
+ } else if ( 'no_license' === $error->code ) {
10981
+ $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' );
10982
+ } else {
10983
+ $error = $error->message;
10984
+ }
10985
+ }
10986
+
10987
+ $licenses = array(
10988
+ 'success' => ( false === $error )
10989
+ );
10990
+
10991
+ if ( false !== $error ) {
10992
+ $licenses['error'] = sprintf( '%s... %s', $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ), strtolower( $error ) );
10993
+ }
10994
+
10995
+ echo json_encode( $licenses );
10996
+
10997
+ exit;
10998
+ }
10999
+
11000
+ /**
11001
+ * @author Vova Feldman (@svovaf)
11002
+ * @since 1.2.1.8
11003
+ *
11004
+ * @var string
11005
+ */
11006
+ private static $_pagenow;
11007
+
11008
+ /**
11009
+ * Get current page or the referer if executing a WP AJAX request.
11010
+ *
11011
+ * @author Vova Feldman (@svovaf)
11012
+ * @since 1.2.1.8
11013
+ *
11014
+ * @return string
11015
+ */
11016
+ static function get_current_page() {
11017
+ if ( ! isset( self::$_pagenow ) ) {
11018
+ global $pagenow;
11019
+
11020
+ self::$_pagenow = $pagenow;
11021
+
11022
+ if ( self::is_ajax() &&
11023
+ 'admin-ajax.php' === $pagenow
11024
+ ) {
11025
+ $referer = fs_get_raw_referer();
11026
+
11027
+ if ( is_string( $referer ) ) {
11028
+ $parts = explode( '?', $referer );
11029
+
11030
+ self::$_pagenow = basename( $parts[0] );
11031
+ }
11032
+ }
11033
+ }
11034
+
11035
+ return self::$_pagenow;
11036
+ }
11037
+
11038
+ /**
11039
+ * Helper method to check if user in the plugins page.
11040
+ *
11041
+ * @author Vova Feldman (@svovaf)
11042
+ * @since 1.2.1.5
11043
+ *
11044
+ * @return bool
11045
+ */
11046
+ static function is_plugins_page() {
11047
+ return ( 'plugins.php' === self::get_current_page() );
11048
+ }
11049
+
11050
+ /**
11051
+ * @author Leo Fajardo (@leorw)
11052
+ * @since 2.0.2
11053
+ *
11054
+ * @return bool
11055
+ */
11056
+ static function is_updates_page() {
11057
+ return ( 'update-core.php' === self::get_current_page() );
11058
+ }
11059
+
11060
+ /**
11061
+ * Helper method to check if user in the themes page.
11062
+ *
11063
+ * @author Vova Feldman (@svovaf)
11064
+ * @since 1.2.2.6
11065
+ *
11066
+ * @return bool
11067
+ */
11068
+ static function is_themes_page() {
11069
+ return ( 'themes.php' === self::get_current_page() );
11070
+ }
11071
+
11072
+ #----------------------------------------------------------------------------------
11073
+ #region Affiliation
11074
+ #----------------------------------------------------------------------------------
11075
+
11076
+ /**
11077
+ * @author Leo Fajardo (@leorw)
11078
+ * @since 1.2.3
11079
+ *
11080
+ * @return bool
11081
+ */
11082
+ function has_affiliate_program() {
11083
+ if ( ! is_object( $this->_plugin ) ) {
11084
+ return false;
11085
+ }
11086
+
11087
+ return $this->_plugin->has_affiliate_program();
11088
+ }
11089
+
11090
+ /**
11091
+ * @author Leo Fajardo (@leorw)
11092
+ * @since 1.2.4
11093
+ */
11094
+ private function fetch_affiliate_terms() {
11095
+ if ( ! is_object( $this->plugin_affiliate_terms ) ) {
11096
+ $plugins_api = $this->get_api_plugin_scope();
11097
+ $affiliate_terms = $plugins_api->get( '/aff.json?type=affiliation', false );
11098
+
11099
+ if ( ! $this->is_api_result_entity( $affiliate_terms ) ) {
11100
+ return;
11101
+ }
11102
+
11103
+ $this->plugin_affiliate_terms = new FS_AffiliateTerms( $affiliate_terms );
11104
+ }
11105
+ }
11106
+
11107
+ /**
11108
+ * @author Leo Fajardo (@leorw)
11109
+ * @since 1.2.4
11110
+ */
11111
+ private function fetch_affiliate_and_custom_terms() {
11112
+ if ( ! empty( $this->_storage->affiliate_application_data ) ) {
11113
+ $application_data = $this->_storage->affiliate_application_data;
11114
+ $flush = ( ! isset( $application_data['status'] ) || 'pending' === $application_data['status'] );
11115
+
11116
+ $users_api = $this->get_api_user_scope();
11117
+ $result = $users_api->get( "/plugins/{$this->_plugin->id}/aff/{$this->plugin_affiliate_terms->id}/affiliates.json", $flush );
11118
+ if ( $this->is_api_result_object( $result, 'affiliates' ) ) {
11119
+ if ( ! empty( $result->affiliates ) ) {
11120
+ $affiliate = new FS_Affiliate( $result->affiliates[0] );
11121
+
11122
+ if ( ! isset( $application_data['status'] ) || $application_data['status'] !== $affiliate->status ) {
11123
+ $application_data['status'] = $affiliate->status;
11124
+ $this->_storage->affiliate_application_data = $application_data;
11125
+ }
11126
+
11127
+ if ( $affiliate->is_using_custom_terms ) {
11128
+ $affiliate_terms = $users_api->get( "/plugins/{$this->_plugin->id}/affiliates/{$affiliate->id}/aff/{$affiliate->custom_affiliate_terms_id}.json", $flush );
11129
+ if ( $this->is_api_result_entity( $affiliate_terms ) ) {
11130
+ $this->custom_affiliate_terms = new FS_AffiliateTerms( $affiliate_terms );
11131
+ }
11132
+ }
11133
+
11134
+ $this->affiliate = $affiliate;
11135
+ }
11136
+ }
11137
+ }
11138
+ }
11139
+
11140
+ /**
11141
+ * @author Leo Fajardo (@leorw)
11142
+ * @since 1.2.3
11143
+ */
11144
+ private function fetch_affiliate_and_terms() {
11145
+ $this->_logger->entrance();
11146
+
11147
+ $this->fetch_affiliate_terms();
11148
+ $this->fetch_affiliate_and_custom_terms();
11149
+ }
11150
+
11151
+ /**
11152
+ * @author Leo Fajardo (@leorw)
11153
+ * @since 1.2.3
11154
+ *
11155
+ * @return FS_Affiliate
11156
+ */
11157
+ function get_affiliate() {
11158
+ return $this->affiliate;
11159
+ }
11160
+
11161
+
11162
+ /**
11163
+ * @author Leo Fajardo (@leorw)
11164
+ * @since 1.2.3
11165
+ *
11166
+ * @return FS_AffiliateTerms
11167
+ */
11168
+ function get_affiliate_terms() {
11169
+ return is_object( $this->custom_affiliate_terms ) ?
11170
+ $this->custom_affiliate_terms :
11171
+ $this->plugin_affiliate_terms;
11172
+ }
11173
+
11174
+ /**
11175
+ * @author Leo Fajardo (@leorw)
11176
+ * @since 1.2.3
11177
+ */
11178
+ function _submit_affiliate_application() {
11179
+ $this->_logger->entrance();
11180
+
11181
+ $this->check_ajax_referer( 'submit_affiliate_application' );
11182
+
11183
+ if ( ! $this->is_user_admin() ) {
11184
+ // Only for admins.
11185
+ self::shoot_ajax_failure();
11186
+ }
11187
+
11188
+ $affiliate = fs_request_get( 'affiliate' );
11189
+
11190
+ if ( empty( $affiliate['promotion_methods'] ) ) {
11191
+ unset( $affiliate['promotion_methods'] );
11192
+ }
11193
+
11194
+ if ( ! empty( $affiliate['additional_domains'] ) ) {
11195
+ $affiliate['additional_domains'] = array_unique( $affiliate['additional_domains'] );
11196
+ }
11197
+
11198
+ if ( ! $this->is_registered() ) {
11199
+ // Opt in but don't track usage.
11200
+ $next_page = $this->opt_in(
11201
+ false,
11202
+ false,
11203
+ false,
11204
+ false,
11205
+ false,
11206
+ false,
11207
+ true
11208
+ );
11209
+
11210
+ if ( is_object( $next_page ) && $this->is_api_error( $next_page ) ) {
11211
+ self::shoot_ajax_failure(
11212
+ isset( $next_page->error ) ?
11213
+ $next_page->error->message :
11214
+ var_export( $next_page, true )
11215
+ );
11216
+ } else if ( $this->is_pending_activation() ) {
11217
+ self::shoot_ajax_failure( $this->get_text_inline( 'Account is pending activation.', 'account-is-pending-activation' ) );
11218
+ }
11219
+ }
11220
+
11221
+ $this->fetch_affiliate_terms();
11222
+
11223
+ $api = $this->get_api_user_scope();
11224
+ $result = $api->call(
11225
+ ( "/plugins/{$this->_plugin->id}/aff/{$this->plugin_affiliate_terms->id}/affiliates.json" ),
11226
+ 'post',
11227
+ $affiliate
11228
+ );
11229
+
11230
+ if ( $this->is_api_error( $result ) ) {
11231
+ self::shoot_ajax_failure(
11232
+ isset( $result->error ) ?
11233
+ $result->error->message :
11234
+ var_export( $result, true )
11235
+ );
11236
+ } else {
11237
+ if ( $this->_admin_notices->has_sticky( 'affiliate_program' ) ) {
11238
+ $this->_admin_notices->remove_sticky( 'affiliate_program' );
11239
+ }
11240
+
11241
+ $affiliate_application_data = array(
11242
+ 'status' => 'pending',
11243
+ 'stats_description' => $affiliate['stats_description'],
11244
+ 'promotion_method_description' => $affiliate['promotion_method_description'],
11245
+ );
11246
+
11247
+ if ( ! empty( $affiliate['promotion_methods'] ) ) {
11248
+ $affiliate_application_data['promotion_methods'] = $affiliate['promotion_methods'];
11249
+ }
11250
+
11251
+ if ( ! empty( $affiliate['domain'] ) ) {
11252
+ $affiliate_application_data['domain'] = $affiliate['domain'];
11253
+ }
11254
+
11255
+ if ( ! empty( $affiliate['additional_domains'] ) ) {
11256
+ $affiliate_application_data['additional_domains'] = $affiliate['additional_domains'];
11257
+ }
11258
+
11259
+ $this->_storage->affiliate_application_data = $affiliate_application_data;
11260
+ }
11261
+
11262
+ // Purge cached affiliate.
11263
+ $api->purge_cache( 'affiliate.json' );
11264
+
11265
+ self::shoot_ajax_success( $result );
11266
+ }
11267
+
11268
+ /**
11269
+ * @author Leo Fajardo (@leorw)
11270
+ * @since 1.2.3
11271
+ *
11272
+ * @return array|null
11273
+ */
11274
+ function get_affiliate_application_data() {
11275
+ if ( empty( $this->_storage->affiliate_application_data ) ) {
11276
+ return null;
11277
+ }
11278
+
11279
+ return $this->_storage->affiliate_application_data;
11280
+ }
11281
+
11282
+ #endregion Affiliation ------------------------------------------------------------
11283
+
11284
+ #----------------------------------------------------------------------------------
11285
+ #region URL Generators
11286
+ #----------------------------------------------------------------------------------
11287
+
11288
+ /**
11289
+ * Alias to pricing_url().
11290
+ *
11291
+ * @author Vova Feldman (@svovaf)
11292
+ * @since 1.0.2
11293
+ *
11294
+ * @uses pricing_url()
11295
+ *
11296
+ * @param string $period Billing cycle
11297
+ * @param bool $is_trial
11298
+ *
11299
+ * @return string
11300
+ */
11301
+ function get_upgrade_url( $period = WP_FS__PERIOD_ANNUALLY, $is_trial = false ) {
11302
+ return $this->pricing_url( $period, $is_trial );
11303
+ }
11304
+
11305
+ /**
11306
+ * @author Vova Feldman (@svovaf)
11307
+ * @since 1.0.9
11308
+ *
11309
+ * @uses get_upgrade_url()
11310
+ *
11311
+ * @return string
11312
+ */
11313
+ function get_trial_url() {
11314
+ return $this->get_upgrade_url( WP_FS__PERIOD_ANNUALLY, true );
11315
+ }
11316
+
11317
+ /**
11318
+ * Plugin's pricing URL.
11319
+ *
11320
+ * @author Vova Feldman (@svovaf)
11321
+ * @since 1.0.4
11322
+ *
11323
+ * @param string $billing_cycle Billing cycle
11324
+ *
11325
+ * @param bool $is_trial
11326
+ *
11327
+ * @return string
11328
+ */
11329
+ function pricing_url( $billing_cycle = WP_FS__PERIOD_ANNUALLY, $is_trial = false ) {
11330
+ $this->_logger->entrance();
11331
+
11332
+ $params = array(
11333
+ 'billing_cycle' => $billing_cycle
11334
+ );
11335
+
11336
+ if ( $is_trial ) {
11337
+ $params['trial'] = 'true';
11338
+ }
11339
+
11340
+ if ( $this->is_addon() ) {
11341
+ return $this->_parent->addon_url( $this->_slug );
11342
+ }
11343
+
11344
+ return $this->_get_admin_page_url( 'pricing', $params );
11345
+ }
11346
+
11347
+ /**
11348
+ * Checkout page URL.
11349
+ *
11350
+ * @author Vova Feldman (@svovaf)
11351
+ * @since 1.0.6
11352
+ *
11353
+ * @param string $billing_cycle Billing cycle
11354
+ * @param bool $is_trial
11355
+ * @param array $extra (optional) Extra parameters, override other query params.
11356
+ *
11357
+ * @return string
11358
+ */
11359
+ function checkout_url(
11360
+ $billing_cycle = WP_FS__PERIOD_ANNUALLY,
11361
+ $is_trial = false,
11362
+ $extra = array()
11363
+ ) {
11364
+ $this->_logger->entrance();
11365
+
11366
+ $params = array(
11367
+ 'checkout' => 'true',
11368
+ 'billing_cycle' => $billing_cycle,
11369
+ );
11370
+
11371
+ if ( $is_trial ) {
11372
+ $params['trial'] = 'true';
11373
+ }
11374
+
11375
+ /**
11376
+ * Params in extra override other params.
11377
+ */
11378
+ $params = array_merge( $params, $extra );
11379
+
11380
+ return $this->_get_admin_page_url( 'pricing', $params );
11381
+ }
11382
+
11383
+ /**
11384
+ * Add-on checkout URL.
11385
+ *
11386
+ * @author Vova Feldman (@svovaf)
11387
+ * @since 1.1.7
11388
+ *
11389
+ * @param number $addon_id
11390
+ * @param number $pricing_id
11391
+ * @param string $billing_cycle
11392
+ * @param bool $is_trial
11393
+ *
11394
+ * @return string
11395
+ */
11396
+ function addon_checkout_url(
11397
+ $addon_id,
11398
+ $pricing_id,
11399
+ $billing_cycle = WP_FS__PERIOD_ANNUALLY,
11400
+ $is_trial = false
11401
+ ) {
11402
+ return $this->checkout_url( $billing_cycle, $is_trial, array(
11403
+ 'plugin_id' => $addon_id,
11404
+ 'pricing_id' => $pricing_id,
11405
+ ) );
11406
+ }
11407
+
11408
+ #endregion
11409
+
11410
+ #endregion ------------------------------------------------------------------
11411
+
11412
+ /**
11413
+ * Check if plugin has any add-ons.
11414
+ *
11415
+ * @author Vova Feldman (@svovaf)
11416
+ * @since 1.0.5
11417
+ *
11418
+ * @since 1.1.7.3 Base logic only on the parameter provided by the developer in the init function.
11419
+ *
11420
+ * @return bool
11421
+ */
11422
+ function has_addons() {
11423
+ $this->_logger->entrance();
11424
+
11425
+ return $this->_has_addons;
11426
+ }
11427
+
11428
+ /**
11429
+ * Check if plugin can work in anonymous mode.
11430
+ *
11431
+ * @author Vova Feldman (@svovaf)
11432
+ * @since 1.0.9
11433
+ *
11434
+ * @return bool
11435
+ *
11436
+ * @deprecated Please use is_enable_anonymous() instead.
11437
+ */
11438
+ function enable_anonymous() {
11439
+ return $this->_enable_anonymous;
11440
+ }
11441
+
11442
+ /**
11443
+ * Check if plugin can work in anonymous mode.
11444
+ *
11445
+ * @author Vova Feldman (@svovaf)
11446
+ * @since 1.1.9
11447
+ *
11448
+ * @return bool
11449
+ */
11450
+ function is_enable_anonymous() {
11451
+ return $this->_enable_anonymous;
11452
+ }
11453
+
11454
+ /**
11455
+ * Check if plugin is premium only (no free plans).
11456
+ *
11457
+ * @author Vova Feldman (@svovaf)
11458
+ * @since 1.1.9
11459
+ *
11460
+ * @return bool
11461
+ */
11462
+ function is_only_premium() {
11463
+ return $this->_is_premium_only;
11464
+ }
11465
+
11466
+ /**
11467
+ * Checks if the plugin's type is "plugin". The other type is "theme".
11468
+ *
11469
+ * @author Leo Fajardo (@leorw)
11470
+ * @since 1.2.2
11471
+ *
11472
+ * @return bool
11473
+ */
11474
+ function is_plugin() {
11475
+ return ( WP_FS__MODULE_TYPE_PLUGIN === $this->_module_type );
11476
+ }
11477
+
11478
+ /**
11479
+ * @author Leo Fajardo (@leorw)
11480
+ * @since 1.2.2
11481
+ *
11482
+ * @return string
11483
+ */
11484
+ function get_module_type() {
11485
+ if ( ! isset( $this->_module_type ) ) {
11486
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
11487
+ $this->_module_type = $id_slug_type_path_map[ $this->_module_id ]['type'];
11488
+ }
11489
+
11490
+ return $this->_module_type;
11491
+ }
11492
+
11493
+ /**
11494
+ * @author Leo Fajardo (@leorw)
11495
+ * @since 1.2.2
11496
+ *
11497
+ * @return string
11498
+ */
11499
+ function get_plugin_main_file_path() {
11500
+ return $this->_plugin_main_file_path;
11501
+ }
11502
+
11503
+ /**
11504
+ * Check if module has a premium code version.
11505
+ *
11506
+ * Serviceware module might be freemium without any
11507
+ * premium code version, where the paid features
11508
+ * are all part of the service.
11509
+ *
11510
+ * @author Vova Feldman (@svovaf)
11511
+ * @since 1.2.1.6
11512
+ *
11513
+ * @return bool
11514
+ */
11515
+ function has_premium_version() {
11516
+ return $this->_has_premium_version;
11517
+ }
11518
+
11519
+ /**
11520
+ * Check if feature supported with current site's plan.
11521
+ *
11522
+ * @author Vova Feldman (@svovaf)
11523
+ * @since 1.0.1
11524
+ *
11525
+ * @todo IMPLEMENT
11526
+ *
11527
+ * @param number $feature_id
11528
+ *
11529
+ * @throws Exception
11530
+ */
11531
+ function is_feature_supported( $feature_id ) {
11532
+ throw new Exception( 'not implemented' );
11533
+ }
11534
+
11535
+ /**
11536
+ * @author Vova Feldman (@svovaf)
11537
+ * @since 1.0.1
11538
+ *
11539
+ * @return bool Is running in SSL/HTTPS
11540
+ */
11541
+ function is_ssl() {
11542
+ return WP_FS__IS_HTTPS;
11543
+ }
11544
+
11545
+ /**
11546
+ * @author Vova Feldman (@svovaf)
11547
+ * @since 1.0.9
11548
+ *
11549
+ * @return bool Is running in AJAX call.
11550
+ *
11551
+ * @link http://wordpress.stackexchange.com/questions/70676/how-to-check-if-i-am-in-admin-ajax
11552
+ */
11553
+ static function is_ajax() {
11554
+ return ( defined( 'DOING_AJAX' ) && DOING_AJAX );
11555
+ }
11556
+
11557
+ /**
11558
+ * Check if it's an AJAX call targeted for the current module.
11559
+ *
11560
+ * @author Vova Feldman (@svovaf)
11561
+ * @since 1.2.0
11562
+ *
11563
+ * @param array|string $actions Collection of AJAX actions.
11564
+ *
11565
+ * @return bool
11566
+ */
11567
+ function is_ajax_action( $actions ) {
11568
+ // Verify it's an ajax call.
11569
+ if ( ! self::is_ajax() ) {
11570
+ return false;
11571
+ }
11572
+
11573
+ // Verify the call is relevant for the plugin.
11574
+ if ( $this->_module_id != fs_request_get( 'module_id' ) ) {
11575
+ return false;
11576
+ }
11577
+
11578
+ // Verify it's one of the specified actions.
11579
+ if ( is_string( $actions ) ) {
11580
+ $actions = explode( ',', $actions );
11581
+ }
11582
+
11583
+ if ( is_array( $actions ) && 0 < count( $actions ) ) {
11584
+ $ajax_action = fs_request_get( 'action' );
11585
+
11586
+ foreach ( $actions as $action ) {
11587
+ if ( $ajax_action === $this->get_action_tag( $action ) ) {
11588
+ return true;
11589
+ }
11590
+ }
11591
+ }
11592
+
11593
+ return false;
11594
+ }
11595
+
11596
+ /**
11597
+ * Check if it's an AJAX call targeted for current request.
11598
+ *
11599
+ * @author Vova Feldman (@svovaf)
11600
+ * @since 1.2.0
11601
+ *
11602
+ * @param array|string $actions Collection of AJAX actions.
11603
+ * @param number|null $module_id
11604
+ *
11605
+ * @return bool
11606
+ */
11607
+ static function is_ajax_action_static( $actions, $module_id = null ) {
11608
+ // Verify it's an ajax call.
11609
+ if ( ! self::is_ajax() ) {
11610
+ return false;
11611
+ }
11612
+
11613
+
11614
+ if ( ! empty( $module_id ) ) {
11615
+ // Verify the call is relevant for the plugin.
11616
+ if ( $module_id != fs_request_get( 'module_id' ) ) {
11617
+ return false;
11618
+ }
11619
+ }
11620
+
11621
+ // Verify it's one of the specified actions.
11622
+ if ( is_string( $actions ) ) {
11623
+ $actions = explode( ',', $actions );
11624
+ }
11625
+
11626
+ if ( is_array( $actions ) && 0 < count( $actions ) ) {
11627
+ $ajax_action = fs_request_get( 'action' );
11628
+
11629
+ foreach ( $actions as $action ) {
11630
+ if ( $ajax_action === self::get_ajax_action_static( $action, $module_id ) ) {
11631
+ return true;
11632
+ }
11633
+ }
11634
+ }
11635
+
11636
+ return false;
11637
+ }
11638
+
11639
+ /**
11640
+ * @author Vova Feldman (@svovaf)
11641
+ * @since 1.1.7
11642
+ *
11643
+ * @return bool
11644
+ */
11645
+ static function is_cron() {
11646
+ return ( defined( 'DOING_CRON' ) && DOING_CRON );
11647
+ }
11648
+
11649
+ /**
11650
+ * Check if a real user is visiting the admin dashboard.
11651
+ *
11652
+ * @author Vova Feldman (@svovaf)
11653
+ * @since 1.1.7
11654
+ *
11655
+ * @return bool
11656
+ */
11657
+ function is_user_in_admin() {
11658
+ return is_admin() && ! self::is_ajax() && ! self::is_cron();
11659
+ }
11660
+
11661
+ /**
11662
+ * Check if a real user is in the customizer view.
11663
+ *
11664
+ * @author Vova Feldman (@svovaf)
11665
+ * @since 1.2.2.7
11666
+ *
11667
+ * @return bool
11668
+ */
11669
+ static function is_customizer() {
11670
+ return is_customize_preview();
11671
+ }
11672
+
11673
+ /**
11674
+ * Check if running in HTTPS and if site's plan matching the specified plan.
11675
+ *
11676
+ * @param string $plan
11677
+ * @param bool $exact
11678
+ *
11679
+ * @return bool
11680
+ */
11681
+ function is_ssl_and_plan( $plan, $exact = false ) {
11682
+ return ( $this->is_ssl() && $this->is_plan( $plan, $exact ) );
11683
+ }
11684
+
11685
+ /**
11686
+ * Construct plugin's settings page URL.
11687
+ *
11688
+ * @author Vova Feldman (@svovaf)
11689
+ * @since 1.0.4
11690
+ *
11691
+ * @param string $page
11692
+ * @param array $params
11693
+ * @param bool|null $network
11694
+ *
11695
+ * @return string
11696
+ */
11697
+ function _get_admin_page_url( $page = '', $params = array(), $network = null ) {
11698
+ if ( is_null( $network ) ) {
11699
+ $network = (
11700
+ $this->_is_network_active &&
11701
+ ( fs_is_network_admin() || ! $this->is_delegated_connection() )
11702
+ );
11703
+ }
11704
+
11705
+ if ( 0 < count( $params ) ) {
11706
+ foreach ( $params as $k => $v ) {
11707
+ $params[ $k ] = urlencode( $v );
11708
+ }
11709
+ }
11710
+
11711
+ $page_param = $this->_menu->get_slug( $page );
11712
+
11713
+ if ( empty( $page ) &&
11714
+ $this->is_theme() &&
11715
+ // Show the opt-in as an overlay for free wp.org themes or themes without any settings page.
11716
+ ( $this->is_free_wp_org_theme() || ! $this->has_settings_menu() )
11717
+ ) {
11718
+ $params[ $this->get_unique_affix() . '_show_optin' ] = 'true';
11719
+
11720
+ return add_query_arg(
11721
+ $params,
11722
+ $this->admin_url( 'themes.php', 'admin', $network )
11723
+ );
11724
+ }
11725
+
11726
+ if ( ! $this->has_settings_menu() ) {
11727
+ if ( ! empty( $page ) ) {
11728
+ // Module doesn't have a setting page, but since the request is for
11729
+ // a specific Freemius page, use the admin.php path.
11730
+ return add_query_arg( array_merge( $params, array(
11731
+ 'page' => $page_param,
11732
+ ) ), $this->admin_url( 'admin.php', 'admin', $network ) );
11733
+ } else {
11734
+ if ( $this->is_activation_mode() ) {
11735
+ /**
11736
+ * @author Vova Feldman
11737
+ * @since 1.2.1.6
11738
+ *
11739
+ * If plugin doesn't have a settings page, create one for the opt-in screen.
11740
+ */
11741
+ return add_query_arg( array_merge( $params, array(
11742
+ 'page' => $this->_slug,
11743
+ ) ), $this->admin_url( 'admin.php', 'admin', $network ) );
11744
+ } else {
11745
+ // Plugin without a settings page.
11746
+ return add_query_arg(
11747
+ $params,
11748
+ $this->admin_url( 'plugins.php', 'admin', $network )
11749
+ );
11750
+ }
11751
+ }
11752
+ }
11753
+
11754
+ // Module has a submenu settings page.
11755
+ if ( ! $this->_menu->is_top_level() ) {
11756
+ $parent_slug = $this->_menu->get_parent_slug();
11757
+ $menu_file = ( false !== strpos( $parent_slug, '.php' ) ) ?
11758
+ $parent_slug :
11759
+ 'admin.php';
11760
+
11761
+ return add_query_arg( array_merge( $params, array(
11762
+ 'page' => $page_param,
11763
+ ) ), $this->admin_url( $menu_file, 'admin', $network ) );
11764
+ }
11765
+
11766
+ // Module has a top level CPT settings page.
11767
+ if ( $this->_menu->is_cpt() ) {
11768
+ if ( empty( $page ) && $this->is_activation_mode() ) {
11769
+ return add_query_arg( array_merge( $params, array(
11770
+ 'page' => $page_param
11771
+ ) ), $this->admin_url( 'admin.php', 'admin', $network ) );
11772
+ } else {
11773
+ if ( ! empty( $page ) ) {
11774
+ $params['page'] = $page_param;
11775
+ }
11776
+
11777
+ return add_query_arg(
11778
+ $params,
11779
+ $this->admin_url( $this->_menu->get_raw_slug(), 'admin', $network )
11780
+ );
11781
+ }
11782
+ }
11783
+
11784
+ // Module has a custom top level settings page.
11785
+ return add_query_arg( array_merge( $params, array(
11786
+ 'page' => $page_param,
11787
+ ) ), $this->admin_url( 'admin.php', 'admin', $network ) );
11788
+ }
11789
+
11790
+ #--------------------------------------------------------------------------------
11791
+ #region Multisite
11792
+ #--------------------------------------------------------------------------------
11793
+
11794
+ /**
11795
+ * @author Leo Fajardo (@leorw)
11796
+ * @since 2.0.0
11797
+ *
11798
+ * @return bool
11799
+ */
11800
+ function is_network_active() {
11801
+ return $this->_is_network_active;
11802
+ }
11803
+
11804
+ /**
11805
+ * Delegate activation for the given sites in the network (or all sites if `null`) to site admins.
11806
+ *
11807
+ * @author Leo Fajardo (@leorw)
11808
+ * @since 2.0.0
11809
+ *
11810
+ * @param array|null $sites
11811
+ */
11812
+ private function delegate_connection( $sites = null ) {
11813
+ $this->_logger->entrance();
11814
+
11815
+ $this->_admin_notices->remove_sticky( 'connect_account' );
11816
+
11817
+ if ( is_null( $sites ) ) {
11818
+ // All sites delegation.
11819
+ $this->_storage->store( 'is_delegated_connection', true, true, true );
11820
+ } else {
11821
+ // Specified sites delegation.
11822
+ foreach ( $sites as $site ) {
11823
+ $this->delegate_site_connection( $site['blog_id'] );
11824
+ }
11825
+ }
11826
+
11827
+ $this->network_upgrade_mode_completed();
11828
+ }
11829
+
11830
+ /**
11831
+ * Delegate specific network site conncetion to the site admin.
11832
+ *
11833
+ * @author Vova Feldman (@svovaf)
11834
+ * @since 2.0.0
11835
+ *
11836
+ * @param int $blog_id
11837
+ */
11838
+ private function delegate_site_connection( $blog_id ) {
11839
+ $this->_storage->store( 'is_delegated_connection', true, $blog_id, true );
11840
+ }
11841
+
11842
+ /**
11843
+ * Check if super-admin delegated the connection of ALL sites to the site admins.
11844
+ *
11845
+ * @author Vova Feldman (@svovaf)
11846
+ * @since 2.0.0
11847
+ *
11848
+ * @return bool
11849
+ */
11850
+ function is_network_delegated_connection() {
11851
+ if ( ! $this->_is_network_active ) {
11852
+ return false;
11853
+ }
11854
+
11855
+ return $this->_storage->get( 'is_delegated_connection', false, true );
11856
+ }
11857
+
11858
+ /**
11859
+ * @author Leo Fajardo (@leorw)
11860
+ * @since 2.0.0
11861
+ *
11862
+ * @param int $blog_id
11863
+ *
11864
+ * @return bool
11865
+ */
11866
+ function is_site_delegated_connection( $blog_id = 0 ) {
11867
+ if ( ! $this->_is_network_active ) {
11868
+ return false;
11869
+ }
11870
+
11871
+ if ( 0 == $blog_id ) {
11872
+ $blog_id = get_current_blog_id();
11873
+ }
11874
+
11875
+ return $this->_storage->get( 'is_delegated_connection', false, $blog_id );
11876
+ }
11877
+
11878
+ /**
11879
+ * Check if delegated the connection. When running within the the network admin,
11880
+ * and haven't specified the blog ID, checks if network level delegated. If running
11881
+ * within a site admin or specified a blog ID, check if delegated the connection for
11882
+ * the current context site.
11883
+ *
11884
+ * If executed outside the the admin, check if delegated the connection
11885
+ * for the current context site OR the whole network.
11886
+ *
11887
+ * @author Vova Feldman (@svovaf)
11888
+ * @since 2.0.0
11889
+ *
11890
+ * @param int $blog_id If set, checks if network delegated or blog specific delegated.
11891
+ *
11892
+ * @return bool
11893
+ */
11894
+ function is_delegated_connection( $blog_id = 0 ) {
11895
+ if ( ! $this->_is_network_active ) {
11896
+ return false;
11897
+ }
11898
+
11899
+ if ( fs_is_network_admin() && 0 == $blog_id ) {
11900
+ return $this->is_network_delegated_connection();
11901
+ }
11902
+
11903
+ return (
11904
+ $this->is_network_delegated_connection() ||
11905
+ $this->is_site_delegated_connection( $blog_id )
11906
+ );
11907
+ }
11908
+
11909
+ /**
11910
+ * Check if the current module is active for the site.
11911
+ *
11912
+ * @author Vova Feldman (@svovaf)
11913
+ * @since 2.0.0
11914
+ *
11915
+ * @param int $blog_id
11916
+ *
11917
+ * @return bool
11918
+ */
11919
+ function is_active_for_site( $blog_id ) {
11920
+ if ( ! is_multisite() ) {
11921
+ // Not a multisite and this code is executed, means that the plugin is active.
11922
+ return true;
11923
+ }
11924
+
11925
+ if ( $this->is_theme() ) {
11926
+ // All themes are site level activated.
11927
+ return true;
11928
+ }
11929
+
11930
+ if ( $this->_is_network_active ) {
11931
+ // Plugin was network activated so it's active.
11932
+ return true;
11933
+ }
11934
+
11935
+ return in_array( $this->_plugin_basename, (array) get_blog_option( $blog_id, 'active_plugins', array() ) );
11936
+ }
11937
+
11938
+ /**
11939
+ * @author Leo Fajardo (@leorw)
11940
+ * @since 2.0.0
11941
+ *
11942
+ * @return array Active & public sites collection.
11943
+ */
11944
+ static function get_sites() {
11945
+ /**
11946
+ * For consistency with get_blog_list() which only return active public sites.
11947
+ *
11948
+ * @author Vova Feldman (@svovaf)
11949
+ */
11950
+ $args = array(
11951
+ 'public' => 1,
11952
+ 'archived' => 0,
11953
+ 'mature' => 0,
11954
+ 'spam' => 0,
11955
+ 'deleted' => 0,
11956
+ );
11957
+
11958
+ if ( function_exists( 'get_sites' ) ) {
11959
+ // For WP 4.6 and above.
11960
+ return get_sites( $args );
11961
+ } else if ( function_exists( 'wp_get_sites' ) ) {
11962
+ // For WP 3.7 to WP 4.5.
11963
+ return wp_get_sites( $args );
11964
+ } else {
11965
+ // For WP 3.6 and below.
11966
+ return get_blog_list( 0, 'all' );
11967
+ }
11968
+ }
11969
+
11970
+ /**
11971
+ * Checks if a given blog is active.
11972
+ *
11973
+ * @author Vova Feldman (@svovaf)
11974
+ * @since 2.0.0
11975
+ *
11976
+ * @param $blog_id
11977
+ *
11978
+ * @return bool
11979
+ */
11980
+ private static function is_site_active( $blog_id ) {
11981
+ global $wpdb;
11982
+
11983
+ $blog_info = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d", $blog_id ) );
11984
+
11985
+ if ( ! is_object( $blog_info ) ) {
11986
+ return false;
11987
+ }
11988
+
11989
+ return (
11990
+ true == $blog_info->public &&
11991
+ false == $blog_info->archived &&
11992
+ false == $blog_info->mature &&
11993
+ false == $blog_info->spam &&
11994
+ false == $blog_info->deleted
11995
+ );
11996
+ }
11997
+
11998
+ /**
11999
+ * Get a mapping between the site addresses to their blog IDs.
12000
+ *
12001
+ * @author Vova Feldman (@svovaf)
12002
+ * @since 2.0.0
12003
+ *
12004
+ * @return array {
12005
+ * @key string Site address without protocol with a trailing slash.
12006
+ * @value int Site's blog ID.
12007
+ * }
12008
+ */
12009
+ private function get_address_to_blog_map() {
12010
+ $sites = self::get_sites();
12011
+
12012
+ // Map site addresses to their blog IDs.
12013
+ $address_to_blog_map = array();
12014
+ foreach ( $sites as $site ) {
12015
+ $blog_id = self::get_site_blog_id( $site );
12016
+ $address = trailingslashit( fs_strip_url_protocol( get_site_url( $blog_id ) ) );
12017
+ $address_to_blog_map[ $address ] = $blog_id;
12018
+ }
12019
+
12020
+ return $address_to_blog_map;
12021
+ }
12022
+
12023
+ /**
12024
+ * Get a mapping between the site addresses to their blog IDs.
12025
+ *
12026
+ * @author Vova Feldman (@svovaf)
12027
+ * @since 2.0.0
12028
+ *
12029
+ * @return array {
12030
+ * @key int Site's blog ID.
12031
+ * @value FS_Site Associated install.
12032
+ * }
12033
+ */
12034
+ function get_blog_install_map() {
12035
+ $sites = self::get_sites();
12036
+
12037
+ // Map site blog ID to its install.
12038
+ $install_map = array();
12039
+
12040
+ foreach ( $sites as $site ) {
12041
+ $blog_id = self::get_site_blog_id( $site );
12042
+ $install = $this->get_install_by_blog_id( $blog_id );
12043
+
12044
+ if ( is_object( $install ) ) {
12045
+ $install_map[ $blog_id ] = $install;
12046
+ }
12047
+ }
12048
+
12049
+ return $install_map;
12050
+ }
12051
+
12052
+ /**
12053
+ * Gets a map of module IDs that the given user has opted-in to.
12054
+ *
12055
+ * @author Leo Fajardo (@leorw)
12056
+ * @since 2.1.0
12057
+ *
12058
+ * @param number $fs_user_id
12059
+ *
12060
+ * @return array {
12061
+ * @key number $plugin_id
12062
+ * @value bool Always true.
12063
+ * }
12064
+ */
12065
+ private static function get_user_opted_in_module_ids_map( $fs_user_id ) {
12066
+ self::$_static_logger->entrance();
12067
+
12068
+ if ( ! is_multisite() ) {
12069
+ $installs = array_merge(
12070
+ self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN ),
12071
+ self::get_all_sites( WP_FS__MODULE_TYPE_THEME )
12072
+ );
12073
+ } else {
12074
+ $sites = self::get_sites();
12075
+
12076
+ $installs = array();
12077
+ foreach ( $sites as $site ) {
12078
+ $blog_id = self::get_site_blog_id( $site );
12079
+
12080
+ $installs = array_merge(
12081
+ $installs,
12082
+ self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN, $blog_id ),
12083
+ self::get_all_sites( WP_FS__MODULE_TYPE_THEME, $blog_id )
12084
+ );
12085
+ }
12086
+ }
12087
+
12088
+ $module_ids_map = array();
12089
+ foreach ( $installs as $install ) {
12090
+ if ( is_object( $install ) &&
12091
+ FS_Site::is_valid_id( $install->id ) &&
12092
+ FS_User::is_valid_id( $install->user_id ) &&
12093
+ ( $install->user_id == $fs_user_id )
12094
+ ) {
12095
+ $module_ids_map[ $install->plugin_id ] = true;
12096
+ }
12097
+ }
12098
+
12099
+ return $module_ids_map;
12100
+ }
12101
+
12102
+ /**
12103
+ * @author Leo Fajardo (@leorw)
12104
+ *
12105
+ * @return null|array {
12106
+ * 'install' => FS_Site Module's install,
12107
+ * 'blog_id' => string The associated blog ID.
12108
+ * }
12109
+ */
12110
+ private function find_first_install() {
12111
+ $sites = self::get_sites();
12112
+
12113
+ foreach ( $sites as $site ) {
12114
+ $blog_id = self::get_site_blog_id( $site );
12115
+ $install = $this->get_install_by_blog_id( $blog_id );
12116
+
12117
+ if ( is_object( $install ) ) {
12118
+ return array(
12119
+ 'install' => $install,
12120
+ 'blog_id' => $blog_id
12121
+ );
12122
+ }
12123
+ }
12124
+
12125
+ return null;
12126
+ }
12127
+
12128
+ /**
12129
+ * Switches the Freemius site level context to a specified blog.
12130
+ *
12131
+ * @author Vova Feldman (@svovaf)
12132
+ * @since 2.0.0
12133
+ *
12134
+ * @param int $blog_id
12135
+ * @param FS_Site $install
12136
+ */
12137
+ function switch_to_blog( $blog_id, FS_Site $install = null ) {
12138
+ if ( $blog_id == $this->_context_is_network_or_blog_id ) {
12139
+ return;
12140
+ }
12141
+
12142
+ switch_to_blog( $blog_id );
12143
+ $this->_context_is_network_or_blog_id = $blog_id;
12144
+
12145
+ self::$_accounts->set_site_blog_context( $blog_id );
12146
+ $this->_storage->set_site_blog_context( $blog_id );
12147
+ $this->_storage->set_network_active( true, $this->is_delegated_connection( $blog_id ) );
12148
+
12149
+ $this->_site = is_object( $install ) ?
12150
+ $install :
12151
+ $this->get_install_by_blog_id( $blog_id );
12152
+
12153
+ $this->_user = false;
12154
+ $this->_licenses = false;
12155
+ $this->_license = null;
12156
+
12157
+ if ( is_object( $this->_site ) ) {
12158
+ // Try to fetch user from install.
12159
+ $this->_user = self::_get_user_by_id( $this->_site->user_id );
12160
+
12161
+ if ( ! is_object( $this->_user ) &&
12162
+ FS_User::is_valid_id( $this->_storage->prev_user_id )
12163
+ ) {
12164
+ // Try to fetch previously saved user.
12165
+ $this->_user = self::_get_user_by_id( $this->_storage->prev_user_id );
12166
+
12167
+ if ( ! is_object( $this->_user ) ) {
12168
+ // Fallback to network's user.
12169
+ $this->_user = $this->get_network_user();
12170
+ }
12171
+ }
12172
+
12173
+ $all_plugin_licenses = self::get_all_licenses( $this->_module_id );
12174
+
12175
+ if ( ! empty( $all_plugin_licenses ) ) {
12176
+ if ( ! FS_Plugin_License::is_valid_id( $this->_site->license_id ) ) {
12177
+ $this->_license = null;
12178
+ } else {
12179
+ $license_found = false;
12180
+ foreach ( $all_plugin_licenses as $license ) {
12181
+ if ( $license->id == $this->_site->license_id ) {
12182
+ // License found.
12183
+ $this->_license = $license;
12184
+ $license_found = true;
12185
+ break;
12186
+ }
12187
+ }
12188
+
12189
+ if ( $license_found ) {
12190
+ $this->link_license_2_user( $this->_license->id, $this->_user->id );
12191
+ }
12192
+ }
12193
+
12194
+ $this->_licenses = $this->get_user_licenses( $this->_user->id );
12195
+ }
12196
+ }
12197
+
12198
+ unset( $this->_site_api );
12199
+ unset( $this->_user_api );
12200
+ }
12201
+
12202
+ /**
12203
+ * Restore the blog context to the blog that originally loaded the module.
12204
+ *
12205
+ * @author Vova Feldman (@svovaf)
12206
+ * @since 2.0.0
12207
+ */
12208
+ function restore_current_blog() {
12209
+ $this->switch_to_blog( $this->_blog_id );
12210
+ }
12211
+
12212
+ /**
12213
+ * @author Vova Feldman (@svovaf)
12214
+ * @since 2.0.0
12215
+ *
12216
+ * @param array|WP_Site $site
12217
+ *
12218
+ * @return int
12219
+ */
12220
+ static function get_site_blog_id( &$site ) {
12221
+ return ( $site instanceof WP_Site ) ?
12222
+ $site->blog_id :
12223
+ ( is_object( $site ) && isset( $site->userblog_id ) ?
12224
+ $site->userblog_id :
12225
+ $site['blog_id'] );
12226
+ }
12227
+
12228
+ /**
12229
+ * @author Leo Fajardo (@leorw)
12230
+ * @since 2.0.0
12231
+ *
12232
+ * @param array|WP_Site|null $site
12233
+ *
12234
+ * @return array
12235
+ */
12236
+ function get_site_info( $site = null ) {
12237
+ $this->_logger->entrance();
12238
+
12239
+ $switched = false;
12240
+
12241
+ if ( is_null( $site ) ) {
12242
+ $url = get_site_url();
12243
+ $name = get_bloginfo( 'name' );
12244
+ $blog_id = null;
12245
+ } else {
12246
+ $blog_id = self::get_site_blog_id( $site );
12247
+
12248
+ if ( get_current_blog_id() != $blog_id ) {
12249
+ switch_to_blog( $blog_id );
12250
+ $switched = true;
12251
+ }
12252
+
12253
+ if ( $site instanceof WP_Site ) {
12254
+ $url = $site->siteurl;
12255
+ $name = $site->blogname;
12256
+ } else {
12257
+ $url = get_site_url( $blog_id );
12258
+ $name = get_bloginfo( 'name' );
12259
+ }
12260
+ }
12261
+
12262
+ $info = array(
12263
+ 'uid' => $this->get_anonymous_id( $blog_id ),
12264
+ 'url' => $url,
12265
+ 'title' => $name,
12266
+ 'language' => get_bloginfo( 'language' ),
12267
+ 'charset' => get_bloginfo( 'charset' ),
12268
+ );
12269
+
12270
+ if ( is_numeric( $blog_id ) ) {
12271
+ $info['blog_id'] = $blog_id;
12272
+ }
12273
+
12274
+ if ( $switched ) {
12275
+ restore_current_blog();
12276
+ }
12277
+
12278
+ return $info;
12279
+ }
12280
+
12281
+ /**
12282
+ * Load the module's install based on the blog ID.
12283
+ *
12284
+ * @author Vova Feldman (@svovaf)
12285
+ * @since 2.0.0
12286
+ *
12287
+ * @param int|null $blog_id
12288
+ *
12289
+ * @return FS_Site
12290
+ */
12291
+ function get_install_by_blog_id( $blog_id = null ) {
12292
+ $installs = self::get_all_sites( $this->_module_type, $blog_id );
12293
+ $install = isset( $installs[ $this->_slug ] ) ? $installs[ $this->_slug ] : null;
12294
+
12295
+ if ( is_object( $install ) &&
12296
+ is_numeric( $install->id ) &&
12297
+ is_numeric( $install->user_id ) &&
12298
+ FS_Plugin_Plan::is_valid_id( $install->plan_id )
12299
+ ) {
12300
+ // Load site.
12301
+ $install = clone $install;
12302
+ }
12303
+
12304
+ return $install;
12305
+ }
12306
+
12307
+ /**
12308
+ * Check if module is installed on a specified site.
12309
+ *
12310
+ * @author Vova Feldman (@svovaf)
12311
+ * @since 2.0.0
12312
+ *
12313
+ * @param int|null $blog_id
12314
+ *
12315
+ * @return bool
12316
+ */
12317
+ function is_installed_on_site( $blog_id = null ) {
12318
+ $installs = self::get_all_sites( $this->_module_type, $blog_id );
12319
+ $install = isset( $installs[ $this->_slug ] ) ? $installs[ $this->_slug ] : null;
12320
+
12321
+ return (
12322
+ is_object( $install ) &&
12323
+ is_numeric( $install->id ) &&
12324
+ is_numeric( $install->user_id ) &&
12325
+ FS_Plugin_Plan::is_valid_id( $install->plan_id )
12326
+ );
12327
+ }
12328
+
12329
+ /**
12330
+ * Check if super-admin connected at least one site via the network opt-in.
12331
+ *
12332
+ * @author Vova Feldman (@svovaf)
12333
+ * @since 2.0.0
12334
+ *
12335
+ * @return bool
12336
+ */
12337
+ function is_network_registered() {
12338
+ if ( ! $this->_is_network_active ) {
12339
+ return false;
12340
+ }
12341
+
12342
+ return FS_User::is_valid_id( $this->_storage->network_user_id );
12343
+ }
12344
+
12345
+ /**
12346
+ * Returns the main user associated with the network.
12347
+ *
12348
+ * @author Vova Feldman (@svovaf)
12349
+ * @since 2.0.0
12350
+ *
12351
+ * @return FS_User
12352
+ */
12353
+ function get_network_user() {
12354
+ if ( ! $this->_is_network_active ) {
12355
+ return null;
12356
+ }
12357
+
12358
+ return FS_User::is_valid_id( $this->_storage->network_user_id ) ?
12359
+ self::_get_user_by_id( $this->_storage->network_user_id ) :
12360
+ null;
12361
+ }
12362
+
12363
+ /**
12364
+ * Returns the current context user or the network's main user.
12365
+ *
12366
+ * @author Vova Feldman (@svovaf)
12367
+ * @since 2.0.0
12368
+ *
12369
+ * @return FS_User
12370
+ */
12371
+ function get_current_or_network_user() {
12372
+ return ( $this->_user instanceof FS_User ) ?
12373
+ $this->_user :
12374
+ $this->get_network_user();
12375
+ }
12376
+
12377
+ /**
12378
+ * Returns the main install associated with the network.
12379
+ *
12380
+ * @author Vova Feldman (@svovaf)
12381
+ * @since 2.0.0
12382
+ *
12383
+ * @return FS_Site
12384
+ */
12385
+ function get_network_install() {
12386
+ if ( ! $this->_is_network_active ) {
12387
+ return null;
12388
+ }
12389
+
12390
+ return FS_Site::is_valid_id( $this->_storage->network_install_blog_id ) ?
12391
+ $this->get_install_by_blog_id( $this->_storage->network_install_blog_id ) :
12392
+ null;
12393
+ }
12394
+
12395
+ /**
12396
+ * Returns the blog ID that is associated with the main install.
12397
+ *
12398
+ * @author Leo Fajardo (@leorw)
12399
+ * @since 2.0.0
12400
+ *
12401
+ * @return int|null
12402
+ */
12403
+ function get_network_install_blog_id() {
12404
+ if ( ! $this->_is_network_active ) {
12405
+ return null;
12406
+ }
12407
+
12408
+ return FS_Site::is_valid_id( $this->_storage->network_install_blog_id ) ?
12409
+ $this->_storage->network_install_blog_id :
12410
+ null;
12411
+ }
12412
+
12413
+ /**
12414
+ * Returns the current context install or the network's main install.
12415
+ *
12416
+ * @author Vova Feldman (@svovaf)
12417
+ * @since 2.0.0
12418
+ *
12419
+ * @return FS_Site
12420
+ */
12421
+ function get_current_or_network_install() {
12422
+ return ( $this->_site instanceof FS_Site ) ?
12423
+ $this->_site :
12424
+ $this->get_network_install();
12425
+ }
12426
+
12427
+ /**
12428
+ * Check if executing a site level action from the network level admin.
12429
+ *
12430
+ * @author Vova Feldman (@svovaf)
12431
+ * @since 2.0.0
12432
+ *
12433
+ * @return false|int If yes, return the requested blog ID.
12434
+ */
12435
+ private function is_network_level_site_specific_action() {
12436
+ if ( ! $this->_is_network_active ) {
12437
+ return false;
12438
+ }
12439
+
12440
+ if ( ! fs_is_network_admin() ) {
12441
+ return false;
12442
+ }
12443
+
12444
+ $blog_id = fs_request_get( 'blog_id', '' );
12445
+
12446
+ return is_numeric( $blog_id ) ? $blog_id : false;
12447
+ }
12448
+
12449
+ /**
12450
+ * Check if executing an action from the network level admin.
12451
+ *
12452
+ * @author Vova Feldman (@svovaf)
12453
+ * @since 2.0.0
12454
+ *
12455
+ * @return bool
12456
+ */
12457
+ private function is_network_level_action() {
12458
+ return ( $this->_is_network_active && fs_is_network_admin() );
12459
+ }
12460
+
12461
+ /**
12462
+ * Needs to be executed after site deactivation, archive, deletion, or flag as spam.
12463
+ * 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.
12464
+ *
12465
+ * @author Vova Feldman (@svovaf)
12466
+ * @since 2.0.0
12467
+ *
12468
+ * @param int $context_blog_id
12469
+ */
12470
+ private function update_multisite_data_after_site_deactivation( $context_blog_id = 0 ) {
12471
+ $this->_logger->entrance();
12472
+
12473
+ if ( $this->_is_network_active ) {
12474
+ if ( $context_blog_id == $this->_storage->network_install_blog_id ) {
12475
+ $installs_map = $this->get_blog_install_map();
12476
+
12477
+ foreach ( $installs_map as $blog_id => $install ) {
12478
+ /**
12479
+ * @var FS_Site $install
12480
+ */
12481
+ if ( $context_blog_id == $blog_id ) {
12482
+ continue;
12483
+ }
12484
+
12485
+ if ( $install->user_id != $this->_storage->network_user_id ) {
12486
+ continue;
12487
+ }
12488
+
12489
+ // Switch reference to a blog that is opted-in and belong to the same super-admin.
12490
+ $this->_storage->network_install_blog_id = $blog_id;
12491
+ break;
12492
+ }
12493
+ }
12494
+ }
12495
+
12496
+ if ( $this->is_sync_cron_scheduled() &&
12497
+ $context_blog_id == $this->get_sync_cron_blog_id()
12498
+ ) {
12499
+ $this->schedule_sync_cron( WP_FS__SCRIPT_START_TIME, true, $context_blog_id );
12500
+ }
12501
+
12502
+ if ( $this->is_install_sync_scheduled() &&
12503
+ $context_blog_id == $this->get_install_sync_cron_blog_id()
12504
+ ) {
12505
+ $this->schedule_install_sync( $context_blog_id );
12506
+ }
12507
+ }
12508
+
12509
+ /**
12510
+ * Executed after site deactivation, archive, or flag as spam.
12511
+ *
12512
+ * @author Vova Feldman (@svovaf)
12513
+ * @since 2.0.0
12514
+ *
12515
+ * @param int $context_blog_id
12516
+ */
12517
+ public function _after_site_deactivated_callback( $context_blog_id = 0 ) {
12518
+ $this->_logger->entrance();
12519
+
12520
+ $install = $this->get_install_by_blog_id( $context_blog_id );
12521
+
12522
+ if ( ! is_object( $install ) ) {
12523
+ // Site not connected.
12524
+ return;
12525
+ }
12526
+
12527
+ $this->update_multisite_data_after_site_deactivation( $context_blog_id );
12528
+
12529
+ $current_blog_id = get_current_blog_id();
12530
+
12531
+ $this->switch_to_blog( $context_blog_id );
12532
+
12533
+ // Send deactivation event.
12534
+ $this->sync_install( array(
12535
+ 'is_active' => false,
12536
+ ) );
12537
+
12538
+ $this->switch_to_blog( $current_blog_id );
12539
+ }
12540
+
12541
+ /**
12542
+ * Executed after site deletion.
12543
+ *
12544
+ * @author Vova Feldman (@svovaf)
12545
+ * @since 2.0.0
12546
+ *
12547
+ * @param int $context_blog_id
12548
+ * @param bool $drop True if site's database tables should be dropped. Default is false.
12549
+ */
12550
+ public function _after_site_deleted_callback( $context_blog_id = 0, $drop = false ) {
12551
+ $this->_logger->entrance();
12552
+
12553
+ $install = $this->get_install_by_blog_id( $context_blog_id );
12554
+
12555
+ if ( ! is_object( $install ) ) {
12556
+ // Site not connected.
12557
+ return;
12558
+ }
12559
+
12560
+ $this->update_multisite_data_after_site_deactivation( $context_blog_id );
12561
+
12562
+ $current_blog_id = get_current_blog_id();
12563
+
12564
+ $this->switch_to_blog( $context_blog_id );
12565
+
12566
+ if ( $drop ) {
12567
+ // Delete install if dropping site DB.
12568
+ $this->delete_account_event();
12569
+ } else {
12570
+ // Send deactivation event.
12571
+ $this->sync_install( array(
12572
+ 'is_active' => false,
12573
+ ) );
12574
+ }
12575
+
12576
+ $this->switch_to_blog( $current_blog_id );
12577
+ }
12578
+
12579
+ /**
12580
+ * Executed after site re-activation.
12581
+ *
12582
+ * @author Vova Feldman (@svovaf)
12583
+ * @since 2.0.0
12584
+ *
12585
+ * @param int $context_blog_id
12586
+ */
12587
+ public function _after_site_reactivated_callback( $context_blog_id = 0 ) {
12588
+ $this->_logger->entrance();
12589
+
12590
+ $install = $this->get_install_by_blog_id( $context_blog_id );
12591
+
12592
+ if ( ! is_object( $install ) ) {
12593
+ // Site not connected.
12594
+ return;
12595
+ }
12596
+
12597
+ if ( ! self::is_site_active( $context_blog_id ) ) {
12598
+ // Site not yet active (can be in spam mode, archived, deleted...).
12599
+ return;
12600
+ }
12601
+
12602
+ $current_blog_id = get_current_blog_id();
12603
+
12604
+ $this->switch_to_blog( $context_blog_id );
12605
+
12606
+ // Send re-activation event.
12607
+ $this->sync_install( array(
12608
+ 'is_active' => true,
12609
+ ) );
12610
+
12611
+ $this->switch_to_blog( $current_blog_id );
12612
+ }
12613
+
12614
+ #endregion Multisite
12615
+
12616
+ /**
12617
+ * @author Leo Fajardo (@leorw)
12618
+ *
12619
+ * @param string $path
12620
+ * @param string $scheme
12621
+ * @param bool $network
12622
+ *
12623
+ * @return string
12624
+ */
12625
+ private function admin_url( $path = '', $scheme = 'admin', $network = true ) {
12626
+ return ( $this->_is_network_active && $network ) ?
12627
+ network_admin_url( $path, $scheme ) :
12628
+ admin_url( $path, $scheme );
12629
+ }
12630
+
12631
+ /**
12632
+ * Check if currently in a specified admin page.
12633
+ *
12634
+ * @author Vova Feldman (@svovaf)
12635
+ * @since 1.2.2.7
12636
+ *
12637
+ * @param string $page
12638
+ *
12639
+ * @return bool
12640
+ */
12641
+ function is_admin_page( $page ) {
12642
+ return ( $this->_menu->get_slug( $page ) === fs_request_get( 'page', '', 'get' ) );
12643
+ }
12644
+
12645
+ /**
12646
+ * Get module's main admin setting page URL.
12647
+ *
12648
+ * @author Vova Feldman (@svovaf)
12649
+ * @since 1.2.2.7
12650
+ *
12651
+ * @return string
12652
+ */
12653
+ function main_menu_url() {
12654
+ return $this->_menu->main_menu_url();
12655
+ }
12656
+
12657
+ /**
12658
+ * Check if currently on the theme's setting page or
12659
+ * on any of the Freemius added pages (via tabs).
12660
+ *
12661
+ * @author Vova Feldman (@svovaf)
12662
+ * @since 1.2.2.7
12663
+ *
12664
+ * @return bool
12665
+ */
12666
+ function is_theme_settings_page() {
12667
+ return fs_starts_with(
12668
+ fs_request_get( 'page', '', 'get' ),
12669
+ $this->_menu->get_slug()
12670
+ );
12671
+ }
12672
+
12673
+ /**
12674
+ * Plugin's account page + sync license URL.
12675
+ *
12676
+ * @author Vova Feldman (@svovaf)
12677
+ * @since 1.1.9.1
12678
+ *
12679
+ * @param bool|number $plugin_id
12680
+ * @param bool $add_action_nonce
12681
+ * @param array $params
12682
+ *
12683
+ * @return string
12684
+ */
12685
+ function _get_sync_license_url( $plugin_id = false, $add_action_nonce = true, $params = array() ) {
12686
+ if ( is_numeric( $plugin_id ) ) {
12687
+ $params['plugin_id'] = $plugin_id;
12688
+ }
12689
+
12690
+ return $this->get_account_url(
12691
+ $this->get_unique_affix() . '_sync_license',
12692
+ $params,
12693
+ $add_action_nonce
12694
+ );
12695
+ }
12696
+
12697
+ /**
12698
+ * Plugin's account URL.
12699
+ *
12700
+ * @author Vova Feldman (@svovaf)
12701
+ * @since 1.0.4
12702
+ *
12703
+ * @param bool|string $action
12704
+ * @param array $params
12705
+ *
12706
+ * @param bool $add_action_nonce
12707
+ *
12708
+ * @return string
12709
+ */
12710
+ function get_account_url( $action = false, $params = array(), $add_action_nonce = true ) {
12711
+ if ( is_string( $action ) ) {
12712
+ $params['fs_action'] = $action;
12713
+ }
12714
+
12715
+ self::require_pluggable_essentials();
12716
+
12717
+ return ( $add_action_nonce && is_string( $action ) ) ?
12718
+ fs_nonce_url( $this->_get_admin_page_url( 'account', $params ), $action ) :
12719
+ $this->_get_admin_page_url( 'account', $params );
12720
+ }
12721
+
12722
+ /**
12723
+ * @author Vova Feldman (@svovaf)
12724
+ * @since 1.2.0
12725
+ *
12726
+ * @param string $tab
12727
+ * @param bool $action
12728
+ * @param array $params
12729
+ * @param bool $add_action_nonce
12730
+ *
12731
+ * @return string
12732
+ *
12733
+ * @uses get_account_url()
12734
+ */
12735
+ function get_account_tab_url( $tab, $action = false, $params = array(), $add_action_nonce = true ) {
12736
+ $params['tab'] = $tab;
12737
+
12738
+ return $this->get_account_url( $action, $params, $add_action_nonce );
12739
+ }
12740
+
12741
+ /**
12742
+ * Plugin's account URL.
12743
+ *
12744
+ * @author Vova Feldman (@svovaf)
12745
+ * @since 1.0.4
12746
+ *
12747
+ * @param bool|string $topic
12748
+ * @param bool|string $message
12749
+ *
12750
+ * @return string
12751
+ */
12752
+ function contact_url( $topic = false, $message = false ) {
12753
+ $params = array();
12754
+ if ( is_string( $topic ) ) {
12755
+ $params['topic'] = $topic;
12756
+ }
12757
+ if ( is_string( $message ) ) {
12758
+ $params['message'] = $message;
12759
+ }
12760
+
12761
+ if ( $this->is_addon() ) {
12762
+ $params['addon_id'] = $this->get_id();
12763
+
12764
+ return $this->get_parent_instance()->_get_admin_page_url( 'contact', $params );
12765
+ } else {
12766
+ return $this->_get_admin_page_url( 'contact', $params );
12767
+ }
12768
+ }
12769
+
12770
+ /**
12771
+ * Add-on direct info URL.
12772
+ *
12773
+ * @author Vova Feldman (@svovaf)
12774
+ * @since 1.1.0
12775
+ *
12776
+ * @param string $slug
12777
+ *
12778
+ * @return string
12779
+ */
12780
+ function addon_url( $slug ) {
12781
+ return $this->_get_admin_page_url( 'addons', array(
12782
+ 'slug' => $slug
12783
+ ) );
12784
+ }
12785
+
12786
+ /* Logger
12787
+ ------------------------------------------------------------------------------------------------------------------*/
12788
+ /**
12789
+ * @param string $id
12790
+ * @param bool $prefix_slug
12791
+ *
12792
+ * @return FS_Logger
12793
+ */
12794
+ function get_logger( $id = '', $prefix_slug = true ) {
12795
+ return FS_Logger::get_logger( ( $prefix_slug ? $this->_slug : '' ) . ( ( ! $prefix_slug || empty( $id ) ) ? '' : '_' ) . $id );
12796
+ }
12797
+
12798
+ /**
12799
+ * Note: This method is used externally so don't delete it.
12800
+ *
12801
+ * @param $id
12802
+ * @param bool $load_options
12803
+ * @param bool $prefix_slug
12804
+ *
12805
+ * @return FS_Option_Manager
12806
+ */
12807
+ function get_options_manager( $id, $load_options = false, $prefix_slug = true ) {
12808
+ return FS_Option_Manager::get_manager( ( $prefix_slug ? $this->_slug : '' ) . ( ( ! $prefix_slug || empty( $id ) ) ? '' : '_' ) . $id, $load_options );
12809
+ }
12810
+
12811
+ /* Security
12812
+ ------------------------------------------------------------------------------------------------------------------*/
12813
+ private static function _encrypt( $str ) {
12814
+ if ( is_null( $str ) ) {
12815
+ return null;
12816
+ }
12817
+
12818
+ /**
12819
+ * The encrypt/decrypt functions are used to protect
12820
+ * the user from messing up with some of the sensitive
12821
+ * data stored for the module as a JSON in the database.
12822
+ *
12823
+ * I used the same suggested hack by the theme review team.
12824
+ * For more details, look at the function `Base64UrlDecode()`
12825
+ * in `./sdk/FreemiusBase.php`.
12826
+ *
12827
+ * @todo Remove this hack once the base64 error is removed from the Theme Check.
12828
+ *
12829
+ * @author Vova Feldman (@svovaf)
12830
+ * @since 1.2.2
12831
+ */
12832
+ $fn = 'base64' . '_encode';
12833
+
12834
+ return $fn( $str );
12835
+ }
12836
+
12837
+ static function _decrypt( $str ) {
12838
+ if ( is_null( $str ) ) {
12839
+ return null;
12840
+ }
12841
+
12842
+ /**
12843
+ * The encrypt/decrypt functions are used to protect
12844
+ * the user from messing up with some of the sensitive
12845
+ * data stored for the module as a JSON in the database.
12846
+ *
12847
+ * I used the same suggested hack by the theme review team.
12848
+ * For more details, look at the function `Base64UrlDecode()`
12849
+ * in `./sdk/FreemiusBase.php`.
12850
+ *
12851
+ * @todo Remove this hack once the base64 error is removed from the Theme Check.
12852
+ *
12853
+ * @author Vova Feldman (@svovaf)
12854
+ * @since 1.2.2
12855
+ */
12856
+ $fn = 'base64' . '_decode';
12857
+
12858
+ return $fn( $str );
12859
+ }
12860
+
12861
+ /**
12862
+ * @author Vova Feldman (@svovaf)
12863
+ * @since 1.0.5
12864
+ *
12865
+ * @param FS_Entity $entity
12866
+ *
12867
+ * @return FS_Entity Return an encrypted clone entity.
12868
+ */
12869
+ private static function _encrypt_entity( FS_Entity $entity ) {
12870
+ $clone = clone $entity;
12871
+ $props = get_object_vars( $entity );
12872
+
12873
+ foreach ( $props as $key => $val ) {
12874
+ $clone->{$key} = self::_encrypt( $val );
12875
+ }
12876
+
12877
+ return $clone;
12878
+ }
12879
+
12880
+ /**
12881
+ * @author Vova Feldman (@svovaf)
12882
+ * @since 1.0.5
12883
+ *
12884
+ * @param FS_Entity $entity
12885
+ *
12886
+ * @return FS_Entity Return an decrypted clone entity.
12887
+ */
12888
+ private static function decrypt_entity( FS_Entity $entity ) {
12889
+ $clone = clone $entity;
12890
+ $props = get_object_vars( $entity );
12891
+
12892
+ foreach ( $props as $key => $val ) {
12893
+ $clone->{$key} = self::_decrypt( $val );
12894
+ }
12895
+
12896
+ return $clone;
12897
+ }
12898
+
12899
+ /**
12900
+ * Tries to activate account based on POST params.
12901
+ *
12902
+ * @author Vova Feldman (@svovaf)
12903
+ * @since 1.0.2
12904
+ *
12905
+ * @deprecated Not in use, outdated.
12906
+ */
12907
+ function _activate_account() {
12908
+ if ( $this->is_registered() ) {
12909
+ // Already activated.
12910
+ return;
12911
+ }
12912
+
12913
+ self::_clean_admin_content_section();
12914
+
12915
+ if ( fs_request_is_action( 'activate' ) && fs_request_is_post() ) {
12916
+ // check_admin_referer( 'activate_' . $this->_plugin->public_key );
12917
+
12918
+ // Verify matching plugin details.
12919
+ if ( $this->_plugin->id != fs_request_get( 'plugin_id' ) || $this->_slug != fs_request_get( 'plugin_slug' ) ) {
12920
+ return;
12921
+ }
12922
+
12923
+ $user = new FS_User();
12924
+ $user->id = fs_request_get( 'user_id' );
12925
+ $user->public_key = fs_request_get( 'user_public_key' );
12926
+ $user->secret_key = fs_request_get( 'user_secret_key' );
12927
+ $user->email = fs_request_get( 'user_email' );
12928
+ $user->first = fs_request_get( 'user_first' );
12929
+ $user->last = fs_request_get( 'user_last' );
12930
+ $user->is_verified = fs_request_get_bool( 'user_is_verified' );
12931
+
12932
+ $site = new FS_Site();
12933
+ $site->id = fs_request_get( 'install_id' );
12934
+ $site->public_key = fs_request_get( 'install_public_key' );
12935
+ $site->secret_key = fs_request_get( 'install_secret_key' );
12936
+ $site->plan_id = fs_request_get( 'plan_id' );
12937
+
12938
+ $plans = array();
12939
+ $plans_data = json_decode( urldecode( fs_request_get( 'plans' ) ) );
12940
+ foreach ( $plans_data as $p ) {
12941
+ $plan = new FS_Plugin_Plan( $p );
12942
+ if ( $site->plan_id == $plan->id ) {
12943
+ $plan->title = fs_request_get( 'plan_title' );
12944
+ $plan->name = fs_request_get( 'plan_name' );
12945
+ }
12946
+
12947
+ $plans[] = $plan;
12948
+ }
12949
+
12950
+ $this->_set_account( $user, $site, $plans );
12951
+
12952
+ // Reload the page with the keys.
12953
+ fs_redirect( $this->_get_admin_page_url() );
12954
+ }
12955
+ }
12956
+
12957
+ /**
12958
+ * @author Vova Feldman (@svovaf)
12959
+ * @since 1.0.7
12960
+ *
12961
+ * @param string $email
12962
+ *
12963
+ * @return FS_User|false
12964
+ */
12965
+ static function _get_user_by_email( $email ) {
12966
+ self::$_static_logger->entrance();
12967
+
12968
+ $email = trim( strtolower( $email ) );
12969
+
12970
+ $users = self::get_all_users();
12971
+
12972
+ if ( is_array( $users ) ) {
12973
+ foreach ( $users as $user ) {
12974
+ if ( $email === trim( strtolower( $user->email ) ) ) {
12975
+ return $user;
12976
+ }
12977
+ }
12978
+ }
12979
+
12980
+ return false;
12981
+ }
12982
+
12983
+ #----------------------------------------------------------------------------------
12984
+ #region Account (Loading, Updates & Activation)
12985
+ #----------------------------------------------------------------------------------
12986
+
12987
+ /***
12988
+ * Load account information (user + site).
12989
+ *
12990
+ * @author Vova Feldman (@svovaf)
12991
+ * @since 1.0.1
12992
+ */
12993
+ private function _load_account() {
12994
+ $this->_logger->entrance();
12995
+
12996
+ $this->do_action( 'before_account_load' );
12997
+
12998
+ $users = self::get_all_users();
12999
+ $plans = self::get_all_plans( $this->_module_type );
13000
+
13001
+ if ( $this->_logger->is_on() && is_admin() ) {
13002
+ $this->_logger->log( 'users = ' . var_export( $users, true ) );
13003
+ $this->_logger->log( 'plans = ' . var_export( $plans, true ) );
13004
+ }
13005
+
13006
+ $site = fs_is_network_admin() ?
13007
+ $this->get_network_install() :
13008
+ $this->get_install_by_blog_id();
13009
+
13010
+ if ( fs_is_network_admin() &&
13011
+ ! is_object( $site ) &&
13012
+ FS_Site::is_valid_id( $this->_storage->network_install_blog_id )
13013
+ ) {
13014
+ $first_install = $this->find_first_install();
13015
+
13016
+ if ( is_null( $first_install ) ) {
13017
+ unset( $this->_storage->network_install_blog_id );
13018
+ } else {
13019
+ $site = $first_install['install'];
13020
+ $this->_storage->network_install_blog_id = $first_install['blog_id'];
13021
+ }
13022
+ }
13023
+
13024
+ if ( is_object( $site ) &&
13025
+ is_numeric( $site->id ) &&
13026
+ is_numeric( $site->user_id ) &&
13027
+ FS_Plugin_Plan::is_valid_id( $site->plan_id )
13028
+ ) {
13029
+ // Load site.
13030
+ $this->_site = $site;
13031
+
13032
+ // Load plans.
13033
+ $this->_plans = $plans[ $this->_slug ];
13034
+ if ( ! is_array( $this->_plans ) || empty( $this->_plans ) ) {
13035
+ $this->_sync_plans();
13036
+ } else {
13037
+ for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
13038
+ if ( $this->_plans[ $i ] instanceof FS_Plugin_Plan ) {
13039
+ $this->_plans[ $i ] = self::decrypt_entity( $this->_plans[ $i ] );
13040
+ } else {
13041
+ unset( $this->_plans[ $i ] );
13042
+ }
13043
+ }
13044
+ }
13045
+ }
13046
+
13047
+ $user = null;
13048
+ if ( fs_is_network_admin() && $this->_is_network_active ) {
13049
+ $user = $this->get_network_user();
13050
+ }
13051
+
13052
+ if ( is_object( $user ) ) {
13053
+ $this->_user = clone $user;
13054
+ } else if ( $this->_site ) {
13055
+ $user = self::_get_user_by_id( $this->_site->user_id );
13056
+
13057
+ if ( ! is_object( $user ) && FS_User::is_valid_id( $this->_storage->prev_user_id ) ) {
13058
+ /**
13059
+ * Try to load the previous owner. This recovery is used for the following use-case:
13060
+ * 1. Opt-in
13061
+ * 2. Cloning site1 to site2
13062
+ * 3. Ownership switch in site1 (same applies for site2)
13063
+ * 4. Install data sync on site2
13064
+ * 5. Now site2's install is associated with the new owner which does not exists locally.
13065
+ */
13066
+ $user = self::_get_user_by_id( $this->_storage->prev_user_id );
13067
+ }
13068
+
13069
+ if ( ! is_object( $user ) ) {
13070
+ /**
13071
+ * This is a special fault tolerance mechanism to handle a scenario that the user data is missing.
13072
+ */
13073
+ $user = $this->fetch_user_by_install();
13074
+ }
13075
+
13076
+ $this->_user = ( $user instanceof FS_User ) ?
13077
+ clone $user :
13078
+ null;
13079
+ }
13080
+
13081
+ if ( is_object( $this->_user ) ) {
13082
+ // Load licenses.
13083
+ $this->_licenses = $this->get_user_licenses( $this->_user->id );
13084
+ }
13085
+
13086
+ if ( is_object( $this->_site ) ) {
13087
+ $this->_license = $this->_get_license_by_id( $this->_site->license_id );
13088
+
13089
+ if ( $this->_site->version != $this->get_plugin_version() ) {
13090
+ // If stored install version is different than current installed plugin version,
13091
+ // then update plugin version event.
13092
+ $this->update_plugin_version_event();
13093
+ }
13094
+ }
13095
+
13096
+ if ( $this->is_theme() ) {
13097
+ $this->_register_account_hooks();
13098
+ }
13099
+ }
13100
+
13101
+ /**
13102
+ * Special user recovery mechanism.
13103
+ *
13104
+ * @author Vova Feldman (@svovaf)
13105
+ * @since 2.0.0
13106
+ *
13107
+ * @return \FS_User|mixed
13108
+ */
13109
+ private function fetch_user_by_install() {
13110
+ $api = $this->get_api_site_scope();
13111
+
13112
+ $uid = $this->get_anonymous_id();
13113
+ $request_path = "/users/{$this->_site->user_id}.json?uid={$uid}";
13114
+
13115
+ $result = $api->get( $request_path, false, WP_FS__TIME_10_MIN_IN_SEC );
13116
+
13117
+ if ( $this->is_api_result_entity( $result ) ) {
13118
+ $user = new FS_User( $result );
13119
+ $this->_user = $user;
13120
+ $this->_store_user();
13121
+
13122
+ return $user;
13123
+ }
13124
+
13125
+ $error_code = FS_Api::get_error_code( $result );
13126
+
13127
+ if ( in_array( $error_code, array( 'invalid_unique_id', 'user_cannot_be_recovered' ) ) ) {
13128
+ /**
13129
+ * Those API errors will continue coming and are not recoverable with the
13130
+ * current site's data. Therefore, extend the API call's cached result to 7 days.
13131
+ */
13132
+ $api->update_cache_expiration( $request_path, WP_FS__TIME_WEEK_IN_SEC );
13133
+ }
13134
+
13135
+ return $result;
13136
+ }
13137
+
13138
+ /**
13139
+ * @author Vova Feldman (@svovaf)
13140
+ * @since 1.0.1
13141
+ *
13142
+ * @param FS_User $user
13143
+ * @param FS_Site $site
13144
+ * @param bool|array $plans
13145
+ */
13146
+ private function _set_account( FS_User $user, FS_Site $site, $plans = false ) {
13147
+ $site->user_id = $user->id;
13148
+
13149
+ $this->_site = $site;
13150
+ $this->_user = $user;
13151
+ if ( false !== $plans ) {
13152
+ $this->_plans = $plans;
13153
+ }
13154
+
13155
+ $this->send_install_update();
13156
+
13157
+ $this->_store_account();
13158
+
13159
+ }
13160
+
13161
+ /**
13162
+ * @author Vova Feldman (@svovaf)
13163
+ * @since 1.1.7.4
13164
+ *
13165
+ * @param array $override_with
13166
+ * @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.
13167
+ *
13168
+ * @return array
13169
+ */
13170
+ function get_opt_in_params( $override_with = array(), $network_level_or_blog_id = null ) {
13171
+ $this->_logger->entrance();
13172
+
13173
+ $current_user = self::_get_current_wp_user();
13174
+
13175
+ $activation_action = $this->get_unique_affix() . '_activate_new';
13176
+ $return_url = $this->is_anonymous() ?
13177
+ // If skipped already, then return to the account page.
13178
+ $this->get_account_url( $activation_action, array(), false ) :
13179
+ // Return to the module's main page.
13180
+ $this->get_after_activation_url( 'after_connect_url', array( 'fs_action' => $activation_action ) );
13181
+
13182
+ $params = array(
13183
+ 'user_firstname' => $current_user->user_firstname,
13184
+ 'user_lastname' => $current_user->user_lastname,
13185
+ 'user_nickname' => $current_user->user_nicename,
13186
+ 'user_email' => $current_user->user_email,
13187
+ 'user_ip' => WP_FS__REMOTE_ADDR,
13188
+ 'plugin_slug' => $this->_slug,
13189
+ 'plugin_id' => $this->get_id(),
13190
+ 'plugin_public_key' => $this->get_public_key(),
13191
+ 'plugin_version' => $this->get_plugin_version(),
13192
+ 'return_url' => fs_nonce_url( $return_url, $activation_action ),
13193
+ 'account_url' => fs_nonce_url( $this->_get_admin_page_url(
13194
+ 'account',
13195
+ array( 'fs_action' => 'sync_user' )
13196
+ ), 'sync_user' ),
13197
+ 'platform_version' => get_bloginfo( 'version' ),
13198
+ 'sdk_version' => $this->version,
13199
+ 'programming_language_version' => phpversion(),
13200
+ 'is_premium' => $this->is_premium(),
13201
+ 'is_active' => true,
13202
+ 'is_uninstalled' => false,
13203
+ );
13204
+
13205
+ if ( true === $network_level_or_blog_id ) {
13206
+ if ( ! isset( $override_with['sites'] ) ) {
13207
+ $params['sites'] = array();
13208
+
13209
+ $sites = self::get_sites();
13210
+
13211
+ foreach ( $sites as $site ) {
13212
+ $blog_id = self::get_site_blog_id( $site );
13213
+ if ( ! $this->is_site_delegated_connection( $blog_id ) &&
13214
+ ! $this->is_installed_on_site( $blog_id )
13215
+ ) {
13216
+ $params['sites'][] = $this->get_site_info( $site );
13217
+ }
13218
+ }
13219
+ }
13220
+ } else {
13221
+ $site = is_numeric( $network_level_or_blog_id ) ?
13222
+ array( 'blog_id' => $network_level_or_blog_id ) :
13223
+ null;
13224
+
13225
+ $site = $this->get_site_info( $site );
13226
+
13227
+ $params = array_merge( $params, array(
13228
+ 'site_uid' => $site['uid'],
13229
+ 'site_url' => $site['url'],
13230
+ 'site_name' => $site['title'],
13231
+ 'language' => $site['language'],
13232
+ 'charset' => $site['charset'],
13233
+ ) );
13234
+ }
13235
+
13236
+ if ( $this->is_pending_activation() &&
13237
+ ! empty( $this->_storage->pending_license_key )
13238
+ ) {
13239
+ $params['license_key'] = $this->_storage->pending_license_key;
13240
+ }
13241
+
13242
+ if ( WP_FS__SKIP_EMAIL_ACTIVATION && $this->has_secret_key() ) {
13243
+ // Even though rand() is known for its security issues,
13244
+ // the timestamp adds another layer of protection.
13245
+ // It would be very hard for an attacker to get the secret key form here.
13246
+ // Plus, this should never run in production since the secret should never
13247
+ // be included in the production version.
13248
+ $params['ts'] = WP_FS__SCRIPT_START_TIME;
13249
+ $params['salt'] = md5( uniqid( rand() ) );
13250
+ $params['secure'] = md5(
13251
+ $params['ts'] .
13252
+ $params['salt'] .
13253
+ $this->get_secret_key()
13254
+ );
13255
+ }
13256
+
13257
+ return array_merge( $params, $override_with );
13258
+ }
13259
+
13260
+ /**
13261
+ * 1. If successful opt-in or pending activation returns the next page that the user should be redirected to.
13262
+ * 2. If there was an API error, return the API result.
13263
+ *
13264
+ * @author Vova Feldman (@svovaf)
13265
+ * @since 1.1.7.4
13266
+ *
13267
+ * @param string|bool $email
13268
+ * @param string|bool $first
13269
+ * @param string|bool $last
13270
+ * @param string|bool $license_key
13271
+ * @param bool $is_uninstall If "true", this means that the module is currently being uninstalled.
13272
+ * In this case, the user and site info will be sent to the server but no
13273
+ * data will be saved to the WP installation's database.
13274
+ * @param number|bool $trial_plan_id
13275
+ * @param bool $is_disconnected Whether or not to opt in without tracking.
13276
+ * @param null|bool $is_marketing_allowed
13277
+ * @param array $sites If network-level opt-in, an array of containing details of sites.
13278
+ *
13279
+ * @return string|object
13280
+ * @use WP_Error
13281
+ */
13282
+ function opt_in(
13283
+ $email = false,
13284
+ $first = false,
13285
+ $last = false,
13286
+ $license_key = false,
13287
+ $is_uninstall = false,
13288
+ $trial_plan_id = false,
13289
+ $is_disconnected = false,
13290
+ $is_marketing_allowed = null,
13291
+ $sites = array()
13292
+ ) {
13293
+ $this->_logger->entrance();
13294
+
13295
+ if ( false === $email ) {
13296
+ $current_user = self::_get_current_wp_user();
13297
+ $email = $current_user->user_email;
13298
+ }
13299
+
13300
+ /**
13301
+ * @since 1.2.1 If activating with license key, ignore the context-user
13302
+ * since the user will be automatically loaded from the license.
13303
+ */
13304
+ if ( empty( $license_key ) ) {
13305
+ // Clean up pending license if opt-ing in again.
13306
+ $this->_storage->remove( 'pending_license_key' );
13307
+
13308
+ if ( ! $is_uninstall ) {
13309
+ $fs_user = Freemius::_get_user_by_email( $email );
13310
+ if ( is_object( $fs_user ) && ! $this->is_pending_activation() ) {
13311
+ return $this->install_with_current_user(
13312
+ false,
13313
+ $trial_plan_id,
13314
+ $sites
13315
+ );
13316
+ }
13317
+ }
13318
+ }
13319
+
13320
+ $user_info = array();
13321
+ if ( ! empty( $email ) ) {
13322
+ $user_info['user_email'] = $email;
13323
+ }
13324
+ if ( ! empty( $first ) ) {
13325
+ $user_info['user_firstname'] = $first;
13326
+ }
13327
+ if ( ! empty( $last ) ) {
13328
+ $user_info['user_lastname'] = $last;
13329
+ }
13330
+
13331
+ if ( ! empty( $sites ) ) {
13332
+ $is_network = true;
13333
+
13334
+ $user_info['sites'] = $sites;
13335
+ } else {
13336
+ $is_network = false;
13337
+ }
13338
+
13339
+ $params = $this->get_opt_in_params( $user_info, $is_network );
13340
+
13341
+ $filtered_license_key = false;
13342
+ if ( is_string( $license_key ) ) {
13343
+ $filtered_license_key = $this->apply_filters( 'license_key', $license_key );
13344
+ $params['license_key'] = $filtered_license_key;
13345
+ } else if ( FS_Plugin_Plan::is_valid_id( $trial_plan_id ) ) {
13346
+ $params['trial_plan_id'] = $trial_plan_id;
13347
+ }
13348
+
13349
+ if ( $is_uninstall ) {
13350
+ $params['uninstall_params'] = array(
13351
+ 'reason_id' => $this->_storage->uninstall_reason->id,
13352
+ 'reason_info' => $this->_storage->uninstall_reason->info
13353
+ );
13354
+ }
13355
+
13356
+ if ( isset( $params['license_key'] ) ) {
13357
+ $fs_user = Freemius::_get_user_by_email( $email );
13358
+
13359
+ if ( is_object( $fs_user ) ) {
13360
+ /**
13361
+ * If opting in with a context license and the context WP Admin user already opted in
13362
+ * before from the current site, add the user context security params to avoid the
13363
+ * unnecessary email activation when the context license is owned by the same context user.
13364
+ *
13365
+ * @author Leo Fajardo (@leorw)
13366
+ * @since 1.2.3
13367
+ */
13368
+ $params = array_merge( $params, FS_Security::instance()->get_context_params(
13369
+ $fs_user,
13370
+ false,
13371
+ 'install_with_existing_user'
13372
+ ) );
13373
+ }
13374
+ }
13375
+
13376
+ if ( is_bool( $is_marketing_allowed ) ) {
13377
+ $params['is_marketing_allowed'] = $is_marketing_allowed;
13378
+ }
13379
+
13380
+ $params['is_disconnected'] = $is_disconnected;
13381
+ $params['format'] = 'json';
13382
+
13383
+ $request = array(
13384
+ 'method' => 'POST',
13385
+ 'body' => $params,
13386
+ 'timeout' => WP_FS__DEBUG_SDK ? 60 : 30,
13387
+ );
13388
+
13389
+ $url = WP_FS__ADDRESS . '/action/service/user/install/';
13390
+ $response = self::safe_remote_post( $url, $request );
13391
+
13392
+ if ( is_wp_error( $response ) ) {
13393
+ /**
13394
+ * @var WP_Error $response
13395
+ */
13396
+ $result = new stdClass();
13397
+
13398
+ $error_code = $response->get_error_code();
13399
+ $error_type = str_replace( ' ', '', ucwords( str_replace( '_', ' ', $error_code ) ) );
13400
+
13401
+ $result->error = (object) array(
13402
+ 'type' => $error_type,
13403
+ 'message' => $response->get_error_message(),
13404
+ 'code' => $error_code,
13405
+ 'http' => 402
13406
+ );
13407
+
13408
+ return $result;
13409
+ }
13410
+
13411
+ // Module is being uninstalled, don't handle the returned data.
13412
+ if ( $is_uninstall ) {
13413
+ return true;
13414
+ }
13415
+
13416
+ /**
13417
+ * 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.
13418
+ *
13419
+ * @author Vova Feldman (@svovaf)
13420
+ * @since 1.2.3
13421
+ * @link https://themes.trac.wordpress.org/ticket/46134#comment:5
13422
+ * @link https://themes.trac.wordpress.org/ticket/46134#comment:9
13423
+ * @link https://themes.trac.wordpress.org/ticket/46134#comment:12
13424
+ * @link https://themes.trac.wordpress.org/ticket/46134#comment:14
13425
+ */
13426
+ $decoded = is_string( $response['body'] ) ?
13427
+ json_decode( $response['body'] ) :
13428
+ null;
13429
+
13430
+ if ( empty( $decoded ) ) {
13431
+ return false;
13432
+ }
13433
+
13434
+ if ( ! $this->is_api_result_object( $decoded ) ) {
13435
+ if ( ! empty( $params['license_key'] ) ) {
13436
+ // Pass the fully entered license key to the failure handler.
13437
+ $params['license_key'] = $license_key;
13438
+ }
13439
+
13440
+ return $is_uninstall ?
13441
+ $decoded :
13442
+ $this->apply_filters( 'after_install_failure', $decoded, $params );
13443
+ } else if ( isset( $decoded->pending_activation ) && $decoded->pending_activation ) {
13444
+ if ( $is_network ) {
13445
+ $site_ids = array();
13446
+ foreach ( $sites as $site ) {
13447
+ $site_ids[] = $site['blog_id'];
13448
+ }
13449
+
13450
+ /**
13451
+ * Store the sites so that they can be installed once the user has clicked on the activation link
13452
+ * in the email.
13453
+ *
13454
+ * @author Leo Fajardo (@leorw)
13455
+ */
13456
+ $this->_storage->pending_sites_info = array(
13457
+ 'blog_ids' => $site_ids,
13458
+ 'license_key' => $license_key,
13459
+ 'trial_plan_id' => $trial_plan_id
13460
+ );
13461
+ }
13462
+
13463
+ // Pending activation, add message.
13464
+ return $this->set_pending_confirmation(
13465
+ ( isset( $decoded->email ) ?
13466
+ $decoded->email :
13467
+ true ),
13468
+ false,
13469
+ $filtered_license_key,
13470
+ ! empty( $params['trial_plan_id'] )
13471
+ );
13472
+ } else if ( isset( $decoded->install_secret_key ) ) {
13473
+ return $this->install_with_new_user(
13474
+ $decoded->user_id,
13475
+ $decoded->user_public_key,
13476
+ $decoded->user_secret_key,
13477
+ ( isset( $decoded->is_marketing_allowed ) && ! is_null( $decoded->is_marketing_allowed ) ?
13478
+ $decoded->is_marketing_allowed :
13479
+ null ),
13480
+ $decoded->install_id,
13481
+ $decoded->install_public_key,
13482
+ $decoded->install_secret_key,
13483
+ false
13484
+ );
13485
+ } else if ( is_array( $decoded->installs ) ) {
13486
+ return $this->install_many_with_new_user(
13487
+ $decoded->user_id,
13488
+ $decoded->user_public_key,
13489
+ $decoded->user_secret_key,
13490
+ ( isset( $decoded->is_marketing_allowed ) && ! is_null( $decoded->is_marketing_allowed ) ?
13491
+ $decoded->is_marketing_allowed :
13492
+ null ),
13493
+ $decoded->installs,
13494
+ false
13495
+ );
13496
+ }
13497
+
13498
+ return $decoded;
13499
+ }
13500
+
13501
+ /**
13502
+ * Set user and site identities.
13503
+ *
13504
+ * @author Vova Feldman (@svovaf)
13505
+ * @since 1.0.9
13506
+ *
13507
+ * @param FS_User $user
13508
+ * @param FS_Site $site
13509
+ * @param bool $redirect
13510
+ * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will
13511
+ * redirect (or return a URL) to the account page with a special parameter to
13512
+ * trigger the auto installation processes.
13513
+ *
13514
+ * @return string If redirect is `false`, returns the next page the user should be redirected to.
13515
+ */
13516
+ function setup_account(
13517
+ FS_User $user,
13518
+ FS_Site $site,
13519
+ $redirect = true,
13520
+ $auto_install = false
13521
+ ) {
13522
+ return $this->setup_network_account(
13523
+ $user,
13524
+ array( $site ),
13525
+ $redirect,
13526
+ $auto_install,
13527
+ false
13528
+ );
13529
+ }
13530
+
13531
+ /**
13532
+ * Set user and site identities.
13533
+ *
13534
+ * @author Vova Feldman (@svovaf)
13535
+ * @since 2.0.0
13536
+ *
13537
+ * @param FS_User $user
13538
+ * @param FS_Site[] $installs
13539
+ * @param bool $redirect
13540
+ * @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.
13541
+ * @param bool $is_network_level_opt_in
13542
+ *
13543
+ * @return string If redirect is `false`, returns the next page the user should be redirected to.
13544
+ */
13545
+ function setup_network_account(
13546
+ FS_User $user,
13547
+ array $installs,
13548
+ $redirect = true,
13549
+ $auto_install = false,
13550
+ $is_network_level_opt_in = true
13551
+ ) {
13552
+ $first_install = $installs[0];
13553
+
13554
+ $this->_user = $user;
13555
+ $this->_site = $first_install;
13556
+
13557
+ $this->_sync_plans();
13558
+
13559
+ if ( $this->_storage->handle_gdpr_admin_notice &&
13560
+ $this->should_handle_gdpr_admin_notice() &&
13561
+ FS_GDPR_Manager::instance()->should_show_opt_in_notice()
13562
+ ) {
13563
+ /**
13564
+ * Clear user lock after an opt-in.
13565
+ */
13566
+ require_once WP_FS__DIR_INCLUDES . '/class-fs-user-lock.php';
13567
+ FS_User_Lock::instance()->unlock();
13568
+ }
13569
+
13570
+ if ( 1 < count( $installs ) ) {
13571
+ // Only network level opt-in can have more than one install.
13572
+ $is_network_level_opt_in = true;
13573
+ }
13574
+ // $is_network_level_opt_in = self::is_ajax_action_static( 'network_activate', $this->_module_id );
13575
+ // If Freemius was OFF before, turn it on.
13576
+ $this->turn_on();
13577
+
13578
+ if ( ! $this->_is_network_active || ! $is_network_level_opt_in ) {
13579
+ $this->_set_account( $user, $first_install );
13580
+
13581
+ $this->do_action( 'after_account_connection', $user, $first_install );
13582
+ } else {
13583
+ $this->_store_user();
13584
+
13585
+ // Map site addresses to their blog IDs.
13586
+ $address_to_blog_map = $this->get_address_to_blog_map();
13587
+
13588
+ $first_blog_id = null;
13589
+ $blog_2_install_map = array();
13590
+ foreach ( $installs as $install ) {
13591
+ $address = trailingslashit( fs_strip_url_protocol( $install->url ) );
13592
+ $blog_id = $address_to_blog_map[ $address ];
13593
+
13594
+ $this->_store_site( true, $blog_id, $install );
13595
+
13596
+ if ( is_null( $first_blog_id ) ) {
13597
+ $first_blog_id = $blog_id;
13598
+ }
13599
+
13600
+ $blog_2_install_map[ $blog_id ] = $install;
13601
+ }
13602
+
13603
+ if ( ! FS_User::is_valid_id( $this->_storage->network_user_id ) ||
13604
+ ! is_object( self::_get_user_by_id( $this->_storage->network_user_id ) )
13605
+ ) {
13606
+ // Store network user.
13607
+ $this->_storage->network_user_id = $this->_user->id;
13608
+ }
13609
+
13610
+ if ( ! FS_Site::is_valid_id( $this->_storage->network_install_blog_id ) ) {
13611
+ $this->_storage->network_install_blog_id = $first_blog_id;
13612
+ }
13613
+
13614
+ if ( count( $installs ) === count( $address_to_blog_map ) ) {
13615
+ // Super-admin opted-in for all sites in the network.
13616
+ $this->_storage->is_network_connected = true;
13617
+ }
13618
+
13619
+ $this->_store_licenses( false );
13620
+
13621
+ self::$_accounts->store();
13622
+
13623
+ // Don't sync the installs data on network upgrade
13624
+ if ( ! $this->network_upgrade_mode_completed() ) {
13625
+ $this->send_installs_update();
13626
+ }
13627
+
13628
+ // Switch install context back to the first install.
13629
+ $this->_site = $first_install;
13630
+
13631
+ $current_blog = get_current_blog_id();
13632
+
13633
+ foreach ( $blog_2_install_map as $blog_id => $install ) {
13634
+ $this->switch_to_blog( $blog_id );
13635
+
13636
+ $this->do_action( 'after_account_connection', $user, $install );
13637
+ }
13638
+
13639
+ $this->switch_to_blog( $current_blog );
13640
+
13641
+ $this->do_action( 'after_network_account_connection', $user, $blog_2_install_map );
13642
+ }
13643
+
13644
+ if ( is_numeric( $first_install->license_id ) ) {
13645
+ $this->_license = $this->_get_license_by_id( $first_install->license_id );
13646
+ }
13647
+
13648
+ $this->_admin_notices->remove_sticky( 'connect_account' );
13649
+
13650
+ if ( $this->is_pending_activation() || ! $this->has_settings_menu() ) {
13651
+ // Remove pending activation sticky notice (if still exist).
13652
+ $this->_admin_notices->remove_sticky( 'activation_pending' );
13653
+
13654
+ // Remove plugin from pending activation mode.
13655
+ unset( $this->_storage->is_pending_activation );
13656
+
13657
+ if ( ! $this->is_paying_or_trial() ) {
13658
+ $this->_admin_notices->add_sticky(
13659
+ sprintf( $this->get_text_inline( '%s activation was successfully completed.', 'plugin-x-activation-message' ), '<b>' . $this->get_plugin_name() . '</b>' ),
13660
+ 'activation_complete'
13661
+ );
13662
+ }
13663
+ }
13664
+
13665
+ if ( $this->is_paying_or_trial() ) {
13666
+ if ( ! $this->is_premium() ||
13667
+ ! $this->has_premium_version() ||
13668
+ ! $this->has_settings_menu()
13669
+ ) {
13670
+ if ( $this->is_paying() ) {
13671
+ $this->_admin_notices->add_sticky(
13672
+ sprintf(
13673
+ $this->get_text_inline( 'Your account was successfully activated with the %s plan.', 'activation-with-plan-x-message' ),
13674
+ $this->get_plan_title()
13675
+ ) . $this->get_complete_upgrade_instructions(),
13676
+ 'plan_upgraded',
13677
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
13678
+ );
13679
+ } else {
13680
+ $trial_plan = $this->get_trial_plan();
13681
+
13682
+ $this->_admin_notices->add_sticky(
13683
+ sprintf(
13684
+ $this->get_text_inline( 'Your trial has been successfully started.', 'trial-started-message' ),
13685
+ '<i>' . $this->get_plugin_name() . '</i>'
13686
+ ) . $this->get_complete_upgrade_instructions( $trial_plan->title ),
13687
+ 'trial_started',
13688
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
13689
+ );
13690
+ }
13691
+ }
13692
+
13693
+ $this->_admin_notices->remove_sticky( array(
13694
+ 'trial_promotion',
13695
+ ) );
13696
+ }
13697
+
13698
+ $plugin_id = fs_request_get( 'plugin_id', false );
13699
+
13700
+ // Store activation time ONLY for plugins & themes (not add-ons).
13701
+ if ( ! is_numeric( $plugin_id ) || ( $plugin_id == $this->_plugin->id ) ) {
13702
+ if ( empty( $this->_storage->activation_timestamp ) ) {
13703
+ $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME;
13704
+ }
13705
+ }
13706
+
13707
+ $next_page = '';
13708
+
13709
+ $extra = array();
13710
+ if ( $auto_install ) {
13711
+ $extra['auto_install'] = 'true';
13712
+ }
13713
+
13714
+ if ( is_numeric( $plugin_id ) ) {
13715
+ /**
13716
+ * @author Leo Fajardo (@leorw)
13717
+ * @since 1.2.1.6
13718
+ *
13719
+ * Also sync the license after an anonymous user subscribes.
13720
+ */
13721
+ if ( $this->is_anonymous() || $plugin_id != $this->_plugin->id ) {
13722
+ // Add-on was installed - sync license right after install.
13723
+ $next_page = $this->_get_sync_license_url( $plugin_id, true, $extra );
13724
+ }
13725
+ } else {
13726
+ /**
13727
+ * @author Vova Feldman (@svovaf)
13728
+ * @since 1.1.9 If site installed with a valid license, sync license.
13729
+ */
13730
+ if ( $this->is_paying() ) {
13731
+ $this->_sync_plugin_license(
13732
+ true,
13733
+ // Installs data is already synced in the beginning of this method directly or via _set_account().
13734
+ false
13735
+ );
13736
+ }
13737
+
13738
+ // Reload the page with the keys.
13739
+ $next_page = $this->is_anonymous() ?
13740
+ // If user previously skipped, redirect to account page.
13741
+ $this->get_account_url( false, $extra ) :
13742
+ $this->get_after_activation_url( 'after_connect_url', array(), $is_network_level_opt_in );
13743
+ }
13744
+
13745
+ if ( ! empty( $next_page ) && $redirect ) {
13746
+ fs_redirect( $next_page );
13747
+ }
13748
+
13749
+ return $next_page;
13750
+ }
13751
+
13752
+ /**
13753
+ * Install plugin with new user information after approval.
13754
+ *
13755
+ * @author Vova Feldman (@svovaf)
13756
+ * @since 1.0.7
13757
+ */
13758
+ function _install_with_new_user() {
13759
+ $this->_logger->entrance();
13760
+
13761
+ if ( $this->is_registered() ) {
13762
+ return;
13763
+ }
13764
+
13765
+ if ( ( $this->is_plugin() && fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) ||
13766
+ // @todo This logic should be improved because it's executed on every load of a theme.
13767
+ $this->is_theme()
13768
+ ) {
13769
+ // check_admin_referer( $this->_slug . '_activate_new' );
13770
+
13771
+ if ( fs_request_has( 'user_secret_key' ) ) {
13772
+ if ( fs_is_network_admin() && isset( $this->_storage->pending_sites_info ) ) {
13773
+ $pending_sites_info = $this->_storage->pending_sites_info;
13774
+
13775
+ $this->install_many_pending_with_user(
13776
+ fs_request_get( 'user_id' ),
13777
+ fs_request_get( 'user_public_key' ),
13778
+ fs_request_get( 'user_secret_key' ),
13779
+ fs_request_get_bool( 'is_marketing_allowed', null ),
13780
+ $pending_sites_info['blog_ids'],
13781
+ $pending_sites_info['license_key'],
13782
+ $pending_sites_info['trial_plan_id']
13783
+ );
13784
+ } else {
13785
+ $this->install_with_new_user(
13786
+ fs_request_get( 'user_id' ),
13787
+ fs_request_get( 'user_public_key' ),
13788
+ fs_request_get( 'user_secret_key' ),
13789
+ fs_request_get_bool( 'is_marketing_allowed', null ),
13790
+ fs_request_get( 'install_id' ),
13791
+ fs_request_get( 'install_public_key' ),
13792
+ fs_request_get( 'install_secret_key' ),
13793
+ true,
13794
+ fs_request_get_bool( 'auto_install' )
13795
+ );
13796
+ }
13797
+ } else if ( fs_request_has( 'pending_activation' ) ) {
13798
+ $this->set_pending_confirmation( fs_request_get( 'user_email' ), true );
13799
+ }
13800
+ }
13801
+ }
13802
+
13803
+ /**
13804
+ * @author Vova Feldman (@svovaf)
13805
+ * @since 2.0.0
13806
+ *
13807
+ * @param number $id
13808
+ * @param string $public_key
13809
+ * @param string $secret_key
13810
+ *
13811
+ * @return \FS_User
13812
+ */
13813
+ private function setup_user( $id, $public_key, $secret_key ) {
13814
+ $user = self::_get_user_by_id( $id );
13815
+
13816
+ if ( is_object( $user ) ) {
13817
+ $this->_user = $user;
13818
+ } else {
13819
+ $user = new FS_User();
13820
+ $user->id = $id;
13821
+ $user->public_key = $public_key;
13822
+ $user->secret_key = $secret_key;
13823
+
13824
+ $this->_user = $user;
13825
+ $user_result = $this->get_api_user_scope()->get();
13826
+ $user = new FS_User( $user_result );
13827
+
13828
+ $this->_user = $user;
13829
+ $this->_store_user();
13830
+ }
13831
+
13832
+ return $user;
13833
+ }
13834
+
13835
+ /**
13836
+ * Install plugin with new user.
13837
+ *
13838
+ * @author Vova Feldman (@svovaf)
13839
+ * @since 1.1.7.4
13840
+ *
13841
+ * @param number $user_id
13842
+ * @param string $user_public_key
13843
+ * @param string $user_secret_key
13844
+ * @param bool|null $is_marketing_allowed
13845
+ * @param number $install_id
13846
+ * @param string $install_public_key
13847
+ * @param string $install_secret_key
13848
+ * @param bool $redirect
13849
+ * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will
13850
+ * redirect (or return a URL) to the account page with a special parameter to
13851
+ * trigger the auto installation processes.
13852
+ *
13853
+ * @return string If redirect is `false`, returns the next page the user should be redirected to.
13854
+ */
13855
+ private function install_with_new_user(
13856
+ $user_id,
13857
+ $user_public_key,
13858
+ $user_secret_key,
13859
+ $is_marketing_allowed,
13860
+ $install_id,
13861
+ $install_public_key,
13862
+ $install_secret_key,
13863
+ $redirect = true,
13864
+ $auto_install = false
13865
+ ) {
13866
+ /**
13867
+ * This method is also executed after opting in with a license key since the
13868
+ * license can be potentially associated with a different owner.
13869
+ *
13870
+ * @since 2.0.0
13871
+ */
13872
+ $user = self::_get_user_by_id( $user_id );
13873
+
13874
+ if ( ! is_object( $user ) ) {
13875
+ $user = new FS_User();
13876
+ $user->id = $user_id;
13877
+ $user->public_key = $user_public_key;
13878
+ $user->secret_key = $user_secret_key;
13879
+
13880
+ $this->_user = $user;
13881
+ $user_result = $this->get_api_user_scope()->get();
13882
+ $user = new FS_User( $user_result );
13883
+ }
13884
+
13885
+ $this->_user = $user;
13886
+
13887
+ $site = new FS_Site();
13888
+ $site->id = $install_id;
13889
+ $site->public_key = $install_public_key;
13890
+ $site->secret_key = $install_secret_key;
13891
+
13892
+ $this->_site = $site;
13893
+ $site_result = $this->get_api_site_scope()->get();
13894
+ $site = new FS_Site( $site_result );
13895
+ $this->_site = $site;
13896
+
13897
+ if ( ! is_null( $is_marketing_allowed ) ) {
13898
+ $this->disable_opt_in_notice_and_lock_user();
13899
+ }
13900
+
13901
+ return $this->setup_account(
13902
+ $this->_user,
13903
+ $this->_site,
13904
+ $redirect,
13905
+ $auto_install
13906
+ );
13907
+ }
13908
+
13909
+ /**
13910
+ * Install plugin with user.
13911
+ *
13912
+ * @author Leo Fajardo (@leorw)
13913
+ * @since 2.0.0
13914
+ *
13915
+ * @param number $user_id
13916
+ * @param string $user_public_key
13917
+ * @param string $user_secret_key
13918
+ * @param bool|null $is_marketing_allowed
13919
+ * @param array $site_ids
13920
+ * @param bool $license_key
13921
+ * @param bool $trial_plan_id
13922
+ * @param bool $redirect
13923
+ *
13924
+ * @return string If redirect is `false`, returns the next page the user should be redirected to.
13925
+ */
13926
+ private function install_many_pending_with_user(
13927
+ $user_id,
13928
+ $user_public_key,
13929
+ $user_secret_key,
13930
+ $is_marketing_allowed,
13931
+ $site_ids,
13932
+ $license_key = false,
13933
+ $trial_plan_id = false,
13934
+ $redirect = true
13935
+ ) {
13936
+ $user = $this->setup_user( $user_id, $user_public_key, $user_secret_key );
13937
+
13938
+ if ( ! is_null( $is_marketing_allowed ) ) {
13939
+ $this->disable_opt_in_notice_and_lock_user();
13940
+ }
13941
+
13942
+ $sites = array();
13943
+ foreach ( $site_ids as $site_id ) {
13944
+ $sites[] = $this->get_site_info( array( 'blog_id' => $site_id ) );
13945
+ }
13946
+
13947
+ $this->install_with_user( $user, $license_key, $trial_plan_id, $redirect, true, $sites );
13948
+ }
13949
+
13950
+ /**
13951
+ * Multi-site install with a new user.
13952
+ *
13953
+ * @author Vova Feldman (@svovaf)
13954
+ * @since 2.0.0
13955
+ *
13956
+ * @param number $user_id
13957
+ * @param string $user_public_key
13958
+ * @param string $user_secret_key
13959
+ * @param bool|null $is_marketing_allowed
13960
+ * @param object[] $installs
13961
+ * @param bool $redirect
13962
+ * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will
13963
+ * redirect (or return a URL) to the account page with a special parameter to
13964
+ * trigger the auto installation processes.
13965
+ *
13966
+ * @return string If redirect is `false`, returns the next page the user should be redirected to.
13967
+ */
13968
+ private function install_many_with_new_user(
13969
+ $user_id,
13970
+ $user_public_key,
13971
+ $user_secret_key,
13972
+ $is_marketing_allowed,
13973
+ array $installs,
13974
+ $redirect = true,
13975
+ $auto_install = false
13976
+ ) {
13977
+ $this->setup_user( $user_id, $user_public_key, $user_secret_key );
13978
+
13979
+ if ( ! is_null( $is_marketing_allowed ) ) {
13980
+ $this->disable_opt_in_notice_and_lock_user();
13981
+ }
13982
+
13983
+ $install_ids = array();
13984
+
13985
+ foreach ( $installs as $install ) {
13986
+ $install_ids[] = $install->id;
13987
+ }
13988
+
13989
+ $left = count( $install_ids );
13990
+ $offset = 0;
13991
+
13992
+ $installs = array();
13993
+ while ( $left > 0 ) {
13994
+ $result = $this->get_api_user_scope()->get( "/plugins/{$this->_module_id}/installs.json?ids=" . implode( ',', array_slice( $install_ids, $offset, 25 ) ) );
13995
+
13996
+ if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
13997
+ // @todo Handle API error.
13998
+ }
13999
+
14000
+ $installs = array_merge( $installs, $result->installs );
14001
+
14002
+ $left -= 25;
14003
+ }
14004
+
14005
+ foreach ( $installs as &$install ) {
14006
+ $install = new FS_Site( $install );
14007
+ }
14008
+
14009
+ return $this->setup_network_account(
14010
+ $this->_user,
14011
+ $installs,
14012
+ $redirect,
14013
+ $auto_install
14014
+ );
14015
+ }
14016
+
14017
+ /**
14018
+ * @author Vova Feldman (@svovaf)
14019
+ * @since 1.1.7.4
14020
+ *
14021
+ * @param string|bool $email
14022
+ * @param bool $redirect
14023
+ * @param string|bool $license_key Since 1.2.1.5
14024
+ * @param bool $is_pending_trial Since 1.2.1.5
14025
+ *
14026
+ * @return string Since 1.2.1.5 if $redirect is `false`, return the pending activation page.
14027
+ */
14028
+ private function set_pending_confirmation(
14029
+ $email = false,
14030
+ $redirect = true,
14031
+ $license_key = false,
14032
+ $is_pending_trial = false
14033
+ ) {
14034
+ if ( $this->_ignore_pending_mode ) {
14035
+ /**
14036
+ * If explicitly asked to ignore pending mode, set to anonymous mode
14037
+ * if require confirmation before finalizing the opt-in.
14038
+ *
14039
+ * @author Vova Feldman
14040
+ * @since 1.2.1.6
14041
+ */
14042
+ $this->skip_connection( null, fs_is_network_admin() );
14043
+ } else {
14044
+ // Install must be activated via email since
14045
+ // user with the same email already exist.
14046
+ $this->_storage->is_pending_activation = true;
14047
+ $this->_add_pending_activation_notice( $email, $is_pending_trial );
14048
+ }
14049
+
14050
+ if ( ! empty( $license_key ) ) {
14051
+ $this->_storage->pending_license_key = $license_key;
14052
+ }
14053
+
14054
+ // Remove the opt-in sticky notice.
14055
+ $this->_admin_notices->remove_sticky( array(
14056
+ 'connect_account',
14057
+ 'trial_promotion',
14058
+ ) );
14059
+
14060
+ $next_page = $this->get_after_activation_url( 'after_pending_connect_url' );
14061
+
14062
+ // Reload the page with with pending activation message.
14063
+ if ( $redirect ) {
14064
+ fs_redirect( $next_page );
14065
+ }
14066
+
14067
+ return $next_page;
14068
+ }
14069
+
14070
+ /**
14071
+ * Install plugin with current logged WP user info.
14072
+ *
14073
+ * @author Vova Feldman (@svovaf)
14074
+ * @since 1.0.7
14075
+ */
14076
+ function _install_with_current_user() {
14077
+ $this->_logger->entrance();
14078
+
14079
+ if ( $this->is_registered() ) {
14080
+ return;
14081
+ }
14082
+
14083
+ if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) && fs_request_is_post() ) {
14084
+ // check_admin_referer( 'activate_existing_' . $this->_plugin->public_key );
14085
+
14086
+ /**
14087
+ * @author Vova Feldman (@svovaf)
14088
+ * @since 1.1.9 Add license key if given.
14089
+ */
14090
+ $license_key = fs_request_get( 'license_secret_key' );
14091
+
14092
+ $this->install_with_current_user( $license_key );
14093
+ }
14094
+ }
14095
+
14096
+
14097
+ /**
14098
+ * @author Vova Feldman (@svovaf)
14099
+ * @since 1.1.7.4
14100
+ *
14101
+ * @param string|bool $license_key
14102
+ * @param number|bool $trial_plan_id
14103
+ * @param array $sites Since 2.0.0
14104
+ * @param bool $redirect
14105
+ *
14106
+ * @return object|string If redirect is `false`, returns the next page the user should be redirected to, or the API error object if failed to install.
14107
+ */
14108
+ private function install_with_current_user(
14109
+ $license_key = false,
14110
+ $trial_plan_id = false,
14111
+ $sites = array(),
14112
+ $redirect = true
14113
+ ) {
14114
+ // Get current logged WP user.
14115
+ $current_user = self::_get_current_wp_user();
14116
+
14117
+ // Find the relevant FS user by the email.
14118
+ $user = self::_get_user_by_email( $current_user->user_email );
14119
+
14120
+ return $this->install_with_user( $user, $license_key, $trial_plan_id, $redirect, true, $sites );
14121
+ }
14122
+
14123
+ /**
14124
+ * @author Vova Feldman (@svovaf)
14125
+ * @since 2.0.0
14126
+ *
14127
+ * @param \FS_User $user
14128
+ * @param string|bool $license_key
14129
+ * @param number|bool $trial_plan_id
14130
+ * @param bool $redirect
14131
+ * @param bool $setup_account Since 2.0.0. When set to FALSE, executes a light installation without setting up the account as if it's the first opt-in.
14132
+ * @param array $sites Since 2.0.0. If not empty, should be a collection of site details for the bulk install API request.
14133
+ *
14134
+ * @return \FS_Site|object|string If redirect is `false`, returns the next page the user should be redirected to, or the API error object if failed to install. If $setup_account is set to `false`, return the newly created install.
14135
+ */
14136
+ private function install_with_user(
14137
+ FS_User $user,
14138
+ $license_key = false,
14139
+ $trial_plan_id = false,
14140
+ $redirect = true,
14141
+ $setup_account = true,
14142
+ $sites = array()
14143
+ ) {
14144
+ // We have to set the user before getting user scope API handler.
14145
+ $this->_user = $user;
14146
+
14147
+ // Install the plugin.
14148
+ $result = $this->create_installs_with_user(
14149
+ $user,
14150
+ $license_key,
14151
+ $trial_plan_id,
14152
+ $sites,
14153
+ $redirect
14154
+ );
14155
+
14156
+ if ( ! $this->is_api_result_entity( $result ) &&
14157
+ ! $this->is_api_result_object( $result, 'installs' )
14158
+ ) {
14159
+ // @todo Handler potential API error of the $result
14160
+ }
14161
+
14162
+ if ( empty( $sites ) ) {
14163
+ $site = new FS_Site( $result );
14164
+ $this->_site = $site;
14165
+
14166
+ if ( ! $setup_account ) {
14167
+ $this->_store_site();
14168
+
14169
+ $this->sync_plan_if_not_exist( $site->plan_id );
14170
+
14171
+ if ( ! empty( $license_key ) && FS_Plugin_License::is_valid_id( $site->license_id ) ) {
14172
+ $this->sync_license_if_not_exist( $site->license_id, $license_key );
14173
+ }
14174
+
14175
+ $this->_admin_notices->remove_sticky( 'connect_account', false );
14176
+
14177
+ return $site;
14178
+ }
14179
+
14180
+ return $this->setup_account( $this->_user, $this->_site, $redirect );
14181
+ } else {
14182
+ $installs = array();
14183
+ foreach ( $result->installs as $install ) {
14184
+ $installs[] = new FS_Site( $install );
14185
+ }
14186
+
14187
+ return $this->setup_network_account(
14188
+ $user,
14189
+ $installs,
14190
+ $redirect
14191
+ );
14192
+ }
14193
+ }
14194
+
14195
+ /**
14196
+ * Initiate an API request to create a collection of installs.
14197
+ *
14198
+ * @author Vova Feldman (@svovaf)
14199
+ * @since 2.0.0
14200
+ *
14201
+ * @param \FS_User $user
14202
+ * @param bool $license_key
14203
+ * @param bool $trial_plan_id
14204
+ * @param array $sites
14205
+ * @param bool $redirect
14206
+ * @param bool $silent
14207
+ *
14208
+ * @return object|mixed
14209
+ */
14210
+ private function create_installs_with_user(
14211
+ FS_User $user,
14212
+ $license_key = false,
14213
+ $trial_plan_id = false,
14214
+ $sites = array(),
14215
+ $redirect = false,
14216
+ $silent = false
14217
+ ) {
14218
+ $extra_install_params = array(
14219
+ 'uid' => $this->get_anonymous_id(),
14220
+ 'is_disconnected' => false,
14221
+ );
14222
+
14223
+ if ( ! empty( $license_key ) ) {
14224
+ $extra_install_params['license_key'] = $this->apply_filters( 'license_key', $license_key );
14225
+ } else if ( FS_Plugin_Plan::is_valid_id( $trial_plan_id ) ) {
14226
+ $extra_install_params['trial_plan_id'] = $trial_plan_id;
14227
+ }
14228
+
14229
+ if ( ! empty( $sites ) ) {
14230
+ $extra_install_params['sites'] = $sites;
14231
+ }
14232
+
14233
+ $args = $this->get_install_data_for_api( $extra_install_params, false, false );
14234
+
14235
+ // Install the plugin.
14236
+ $result = $this->get_api_user_scope_by_user( $user )->call(
14237
+ "/plugins/{$this->get_id()}/installs.json",
14238
+ 'post',
14239
+ $args
14240
+ );
14241
+
14242
+ if ( ! $this->is_api_result_entity( $result ) &&
14243
+ ! $this->is_api_result_object( $result, 'installs' )
14244
+ ) {
14245
+ if ( ! empty( $args['license_key'] ) ) {
14246
+ // Pass full the fully entered license key to the failure handler.
14247
+ $args['license_key'] = $license_key;
14248
+ }
14249
+
14250
+ $result = $this->apply_filters( 'after_install_failure', $result, $args );
14251
+
14252
+ if ( ! $silent ) {
14253
+ $this->_admin_notices->add(
14254
+ sprintf( $this->get_text_inline( 'Couldn\'t activate %s.', 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
14255
+ $this->get_text_inline( 'Please contact us with the following message:', 'contact-us-with-error-message' ) . ' ' . '<b>' . $result->error->message . '</b>',
14256
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
14257
+ 'error'
14258
+ );
14259
+ }
14260
+
14261
+ if ( $redirect ) {
14262
+ /**
14263
+ * We set the user before getting the user scope API handler, so the user became temporarily
14264
+ * registered (`is_registered() = true`). Since the API returned an error and we will redirect,
14265
+ * we have to set the user to `null`, otherwise, the user will be redirected to the wrong
14266
+ * activation page based on the return value of `is_registered()`. In addition, in case the
14267
+ * context plugin doesn't have a settings menu and the default page is the `Plugins` page,
14268
+ * misleading plugin activation errors will be shown on the `Plugins` page.
14269
+ *
14270
+ * @author Leo Fajardo (@leorw)
14271
+ */
14272
+ $this->_user = null;
14273
+
14274
+ fs_redirect( $this->get_activation_url( array( 'error' => $result->error->message ) ) );
14275
+ }
14276
+ }
14277
+
14278
+ return $result;
14279
+ }
14280
+
14281
+ /**
14282
+ * Tries to activate add-on account based on parent plugin info.
14283
+ *
14284
+ * @author Vova Feldman (@svovaf)
14285
+ * @since 1.0.6
14286
+ *
14287
+ * @param Freemius $parent_fs
14288
+ */
14289
+ private function _activate_addon_account( Freemius $parent_fs ) {
14290
+ if ( $this->is_registered() ) {
14291
+ // Already activated.
14292
+ return;
14293
+ }
14294
+
14295
+ // Activate add-on with parent plugin credentials.
14296
+ $addon_install = $parent_fs->get_api_site_scope()->call(
14297
+ "/addons/{$this->_plugin->id}/installs.json",
14298
+ 'post',
14299
+ $this->get_install_data_for_api( array(
14300
+ 'uid' => $this->get_anonymous_id(),
14301
+ ), false, false )
14302
+ );
14303
+
14304
+ if ( isset( $addon_install->error ) ) {
14305
+ $this->_admin_notices->add(
14306
+ sprintf( $this->get_text_inline( 'Couldn\'t activate %s.', 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
14307
+ $this->get_text_inline( 'Please contact us with the following message:', 'contact-us-with-error-message' ) . ' ' . '<b>' . $addon_install->error->message . '</b>',
14308
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
14309
+ 'error'
14310
+ );
14311
+
14312
+ return;
14313
+ }
14314
+
14315
+ // Get user information based on parent's plugin.
14316
+ $user = $parent_fs->get_user();
14317
+
14318
+ // First of all, set site and user info - otherwise we won't
14319
+ // be able to invoke API calls.
14320
+ $this->_site = new FS_Site( $addon_install );
14321
+ $this->_user = $user;
14322
+
14323
+ // Sync add-on plans.
14324
+ $this->_sync_plans();
14325
+
14326
+ // Get site's current plan.
14327
+ //$this->_site->plan = $this->_get_plan_by_id( $this->_site->plan->id );
14328
+
14329
+ $this->_set_account( $user, $this->_site );
14330
+
14331
+ // Sync licenses.
14332
+ $this->_sync_licenses();
14333
+
14334
+ // Try to activate premium license.
14335
+ $this->_activate_license( true );
14336
+ }
14337
+
14338
+ /**
14339
+ * Tries to activate parent account based on add-on's info.
14340
+ *
14341
+ * @author Vova Feldman (@svovaf)
14342
+ * @since 1.2.2.7
14343
+ *
14344
+ * @param Freemius $parent_fs
14345
+ */
14346
+ private function activate_parent_account( Freemius $parent_fs ) {
14347
+ if ( ! $this->is_addon() ) {
14348
+ // This is not an add-on.
14349
+ return;
14350
+ }
14351
+
14352
+ if ( $parent_fs->is_registered() ) {
14353
+ // Already activated.
14354
+ return;
14355
+ }
14356
+
14357
+ // Activate parent with add-on's user credentials.
14358
+ $parent_install = $this->get_api_user_scope()->call(
14359
+ "/plugins/{$parent_fs->_plugin->id}/installs.json",
14360
+ 'post',
14361
+ $parent_fs->get_install_data_for_api( array(
14362
+ 'uid' => $parent_fs->get_anonymous_id(),
14363
+ ), false, false )
14364
+ );
14365
+
14366
+ if ( isset( $parent_install->error ) ) {
14367
+ $this->_admin_notices->add(
14368
+ sprintf( $this->get_text_inline( 'Couldn\'t activate %s.', 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
14369
+ $this->get_text_inline( 'Please contact us with the following message:', 'contact-us-with-error-message' ) . ' ' . '<b>' . $parent_install->error->message . '</b>',
14370
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
14371
+ 'error'
14372
+ );
14373
+
14374
+ return;
14375
+ }
14376
+
14377
+ $parent_fs->_admin_notices->remove_sticky( 'connect_account' );
14378
+
14379
+ if ( $parent_fs->is_pending_activation() ) {
14380
+ $parent_fs->_admin_notices->remove_sticky( 'activation_pending' );
14381
+
14382
+ unset( $parent_fs->_storage->is_pending_activation );
14383
+ }
14384
+
14385
+ // Get user information based on parent's plugin.
14386
+ $user = $this->get_user();
14387
+
14388
+ // First of all, set site info - otherwise we won't
14389
+ // be able to invoke API calls.
14390
+ $parent_fs->_site = new FS_Site( $parent_install );
14391
+ $parent_fs->_user = $user;
14392
+
14393
+ // Sync add-on plans.
14394
+ $parent_fs->_sync_plans();
14395
+
14396
+ $parent_fs->_set_account( $user, $parent_fs->_site );
14397
+ }
14398
+
14399
+ #endregion
14400
+
14401
+ #----------------------------------------------------------------------------------
14402
+ #region Admin Menu Items
14403
+ #----------------------------------------------------------------------------------
14404
+
14405
+ private $_menu_items = array();
14406
+
14407
+ /**
14408
+ * @author Vova Feldman (@svovaf)
14409
+ * @since 1.2.1.8
14410
+ *
14411
+ * @return array
14412
+ */
14413
+ function get_menu_items() {
14414
+ return $this->_menu_items;
14415
+ }
14416
+
14417
+ /**
14418
+ * @author Vova Feldman (@svovaf)
14419
+ * @since 1.0.7
14420
+ *
14421
+ * @return string
14422
+ */
14423
+ function get_menu_slug() {
14424
+ return $this->_menu->get_slug();
14425
+ }
14426
+
14427
+ /**
14428
+ * @author Vova Feldman (@svovaf)
14429
+ * @since 1.0.9
14430
+ */
14431
+ function _prepare_admin_menu() {
14432
+ // if ( ! $this->is_on() ) {
14433
+ // return;
14434
+ // }
14435
+
14436
+ /**
14437
+ * When running from a site admin with a network activated module and the connection
14438
+ * was NOT delegated and the user still haven't skipped or opted-in, then hide the
14439
+ * site level settings.
14440
+ *
14441
+ * @author Vova Feldman (@svovaf)
14442
+ * @since 2.0.0
14443
+ */
14444
+ $should_hide_site_admin_settings = (
14445
+ $this->_is_network_active &&
14446
+ ! fs_is_network_admin() &&
14447
+ ! $this->is_delegated_connection() &&
14448
+ ! $this->is_anonymous() &&
14449
+ ! $this->is_registered()
14450
+ );
14451
+
14452
+ if ( ( ! $this->has_api_connectivity() && ! $this->is_enable_anonymous() ) ||
14453
+ $should_hide_site_admin_settings
14454
+ ) {
14455
+ $this->_menu->remove_menu_item( $should_hide_site_admin_settings );
14456
+ } else {
14457
+ $this->do_action( fs_is_network_admin() ?
14458
+ 'before_network_admin_menu_init' :
14459
+ 'before_admin_menu_init'
14460
+ );
14461
+
14462
+ $this->add_menu_action();
14463
+
14464
+ $this->add_network_menu_when_missing();
14465
+
14466
+ $this->add_submenu_items();
14467
+ }
14468
+ }
14469
+
14470
+ /**
14471
+ * Admin dashboard menu items modifications.
14472
+ *
14473
+ * NOTE: admin_menu action executed before admin_init.
14474
+ *
14475
+ * @author Vova Feldman (@svovaf)
14476
+ * @since 1.0.7
14477
+ *
14478
+ */
14479
+ private function add_menu_action() {
14480
+ if ( $this->is_activation_mode() ) {
14481
+ if ( $this->is_plugin() || ( $this->has_settings_menu() && ! $this->is_free_wp_org_theme() ) ) {
14482
+ $this->override_plugin_menu_with_activation();
14483
+ } else {
14484
+ /**
14485
+ * Handle theme opt-in when the opt-in form shows as a dialog box in the themes page.
14486
+ */
14487
+ if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) ) {
14488
+ add_action( 'load-themes.php', array( &$this, '_install_with_current_user' ) );
14489
+ } else if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ||
14490
+ fs_request_get_bool( 'pending_activation' )
14491
+ ) {
14492
+ add_action( 'load-themes.php', array( &$this, '_install_with_new_user' ) );
14493
+ }
14494
+ }
14495
+ } else {
14496
+ if ( ! $this->is_registered() ) {
14497
+ // If not registered try to install user.
14498
+ if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) {
14499
+ $this->_install_with_new_user();
14500
+ }
14501
+ } else if (
14502
+ fs_request_is_action( 'sync_user' ) &&
14503
+ ( ! $this->has_settings_menu() || $this->is_free_wp_org_theme() )
14504
+ ) {
14505
+ $this->_handle_account_user_sync();
14506
+ }
14507
+ }
14508
+ }
14509
+
14510
+ /**
14511
+ * @author Vova Feldman (@svovaf)
14512
+ * @since 1.0.1
14513
+ */
14514
+ function _redirect_on_clicked_menu_link() {
14515
+ $this->_logger->entrance();
14516
+
14517
+ $page = fs_request_get('page');
14518
+ $page = is_string($page) ? strtolower($page) : '';
14519
+
14520
+ $this->_logger->log( 'page = ' . $page );
14521
+
14522
+ foreach ( $this->_menu_items as $priority => $items ) {
14523
+ foreach ( $items as $item ) {
14524
+ if ( isset( $item['url'] ) ) {
14525
+ if ( $page === $this->_menu->get_slug( strtolower( $item['menu_slug'] ) ) ) {
14526
+ $this->_logger->log( 'Redirecting to ' . $item['url'] );
14527
+
14528
+ fs_redirect( $item['url'] );
14529
+ }
14530
+ }
14531
+ }
14532
+ }
14533
+ }
14534
+
14535
+ /**
14536
+ * Remove plugin's all admin menu items & pages, and replace with activation page.
14537
+ *
14538
+ * @author Vova Feldman (@svovaf)
14539
+ * @since 1.0.1
14540
+ */
14541
+ private function override_plugin_menu_with_activation() {
14542
+ $this->_logger->entrance();
14543
+
14544
+ $hook = false;
14545
+
14546
+ if ( ! $this->_menu->has_menu() ) {
14547
+ // Add the opt-in page without a menu item.
14548
+ $hook = FS_Admin_Menu_Manager::add_subpage(
14549
+ null,
14550
+ $this->get_plugin_name(),
14551
+ $this->get_plugin_name(),
14552
+ 'manage_options',
14553
+ $this->_slug,
14554
+ array( &$this, '_connect_page_render' )
14555
+ );
14556
+ } else if ( $this->_menu->is_top_level() ) {
14557
+ if ( $this->_menu->is_override_exact() ) {
14558
+ // Make sure the current page is matching the activation page.
14559
+ if ( ! $this->is_matching_url( $this->get_activation_url() ) ) {
14560
+ return;
14561
+ }
14562
+ }
14563
+
14564
+ $hook = $this->_menu->override_menu_item( array( &$this, '_connect_page_render' ) );
14565
+
14566
+ if ( false === $hook ) {
14567
+ // Create new menu item just for the opt-in.
14568
+ $hook = FS_Admin_Menu_Manager::add_page(
14569
+ $this->get_plugin_name(),
14570
+ $this->get_plugin_name(),
14571
+ 'manage_options',
14572
+ $this->_menu->get_slug(),
14573
+ array( &$this, '_connect_page_render' )
14574
+ );
14575
+ }
14576
+ } else {
14577
+ $menus = array( $this->_menu->get_parent_slug() );
14578
+
14579
+ if ( $this->_menu->is_override_exact() ) {
14580
+ // Make sure the current page is matching the activation page.
14581
+ if ( ! $this->is_matching_url( $this->get_activation_url() ) ) {
14582
+ return;
14583
+ }
14584
+ }
14585
+
14586
+ foreach ( $menus as $parent_slug ) {
14587
+ $hook = $this->_menu->override_submenu_action(
14588
+ $parent_slug,
14589
+ $this->_menu->get_raw_slug(),
14590
+ array( &$this, '_connect_page_render' )
14591
+ );
14592
+
14593
+ if ( false !== $hook ) {
14594
+ // Found plugin's submenu item.
14595
+ break;
14596
+ }
14597
+ }
14598
+ }
14599
+
14600
+ if ( $this->is_activation_page() ) {
14601
+ // Clean admin page from distracting content.
14602
+ self::_clean_admin_content_section();
14603
+ }
14604
+
14605
+ if ( false !== $hook ) {
14606
+ if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) ) {
14607
+ $this->_install_with_current_user();
14608
+ } else if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) {
14609
+ $this->_install_with_new_user();
14610
+ }
14611
+ }
14612
+ }
14613
+
14614
+ /**
14615
+ * If a plugin was network activated and connected but don't have a network
14616
+ * level settings, then add an artificial menu item for the Account and other
14617
+ * Freemius settings.
14618
+ *
14619
+ * @author Vova Feldman (@svovaf)
14620
+ * @since 2.0.0
14621
+ */
14622
+ private function add_network_menu_when_missing() {
14623
+ $this->_logger->entrance();
14624
+
14625
+ if ( ! $this->_is_network_active ) {
14626
+ // Plugin wasn't activated on the network level.
14627
+ return;
14628
+ }
14629
+
14630
+ if ( ! fs_is_network_admin() ) {
14631
+ // The context is not the network admin.
14632
+ return;
14633
+ }
14634
+
14635
+ if ( $this->_menu->has_network_menu() ) {
14636
+ // Plugin already has a network level menu.
14637
+ return;
14638
+ }
14639
+
14640
+ if ( $this->is_network_activation_mode() ) {
14641
+ /**
14642
+ * Do not add during activation mode, otherwise, there will be duplicate menus while the opt-in
14643
+ * screen is being shown.
14644
+ *
14645
+ * @author Leo Fajardo (@leorw)
14646
+ */
14647
+ return;
14648
+ }
14649
+
14650
+ if ( ! WP_FS__SHOW_NETWORK_EVEN_WHEN_DELEGATED ) {
14651
+ if ( $this->is_network_delegated_connection() ) {
14652
+ // Super-admin delegated the connection to the site admins.
14653
+ return;
14654
+ }
14655
+ }
14656
+
14657
+ if ( ! $this->_menu->has_menu() || $this->_menu->is_top_level() ) {
14658
+ $this->_dynamically_added_top_level_page_hook_name = $this->_menu->add_page_and_update(
14659
+ $this->get_plugin_name(),
14660
+ $this->get_plugin_name(),
14661
+ 'manage_options',
14662
+ $this->_menu->has_menu() ? $this->_menu->get_slug() : $this->_slug
14663
+ );
14664
+ } else {
14665
+ $this->_menu->add_subpage_and_update(
14666
+ $this->_menu->get_parent_slug(),
14667
+ $this->get_plugin_name(),
14668
+ $this->get_plugin_name(),
14669
+ 'manage_options',
14670
+ $this->_menu->get_slug()
14671
+ );
14672
+ }
14673
+ }
14674
+
14675
+ /**
14676
+ * @author Leo Fajardo (@leorw)
14677
+ * @since 1.2.1
14678
+ *
14679
+ * return string
14680
+ */
14681
+ function get_top_level_menu_capability() {
14682
+ global $menu;
14683
+
14684
+ $top_level_menu_slug = $this->get_top_level_menu_slug();
14685
+
14686
+ foreach ( $menu as $menu_info ) {
14687
+ /**
14688
+ * The second element in the menu info array is the capability/role that has access to the menu and the
14689
+ * third element is the menu slug.
14690
+ */
14691
+ if ( $menu_info[2] === $top_level_menu_slug ) {
14692
+ return $menu_info[1];
14693
+ }
14694
+ }
14695
+
14696
+ return 'read';
14697
+ }
14698
+
14699
+ /**
14700
+ * @author Vova Feldman (@svovaf)
14701
+ * @since 1.0.0
14702
+ *
14703
+ * @return string
14704
+ */
14705
+ private function get_top_level_menu_slug() {
14706
+ return ( $this->is_addon() ?
14707
+ $this->get_parent_instance()->_menu->get_top_level_menu_slug() :
14708
+ $this->_menu->get_top_level_menu_slug() );
14709
+ }
14710
+
14711
+ /**
14712
+ * @author Vova Feldman (@svovaf)
14713
+ * @since 1.2.2.7
14714
+ *
14715
+ * @return string
14716
+ */
14717
+ function get_pricing_cta_label() {
14718
+ $label = $this->get_text_inline( 'Upgrade', 'upgrade' );
14719
+
14720
+ if ( $this->is_in_trial_promotion() &&
14721
+ ! $this->is_paying_or_trial()
14722
+ ) {
14723
+ // If running a trial promotion, modify the pricing to load the trial.
14724
+ $label = $this->get_text_inline( 'Start Trial', 'start-trial' );
14725
+ } else if ( $this->is_paying() ) {
14726
+ $label = $this->get_text_inline( 'Pricing', 'pricing' );
14727
+ }
14728
+
14729
+ return $label;
14730
+ }
14731
+
14732
+ /**
14733
+ * @author Vova Feldman (@svovaf)
14734
+ * @since 1.2.2.7
14735
+ *
14736
+ * @return bool
14737
+ */
14738
+ function is_pricing_page_visible() {
14739
+ return (
14740
+ // Has at least one paid plan.
14741
+ $this->has_paid_plan() &&
14742
+ // Didn't ask to hide the pricing page.
14743
+ $this->is_page_visible( 'pricing' ) &&
14744
+ // Don't have a valid active license or has more than one plan.
14745
+ ( ! $this->is_paying() || ! $this->is_single_plan() )
14746
+ );
14747
+ }
14748
+
14749
+ /**
14750
+ * Add default Freemius menu items.
14751
+ *
14752
+ * @author Vova Feldman (@svovaf)
14753
+ * @since 1.0.0
14754
+ * @since 1.2.2.7 Also add submenu items when running in a free .org theme so the tabs will be visible.
14755
+ */
14756
+ private function add_submenu_items() {
14757
+ $this->_logger->entrance();
14758
+
14759
+ if ( $this->is_addon() ) {
14760
+ // No submenu items for add-ons.
14761
+ $add_submenu_items = false;
14762
+ } else if ( $this->is_free_wp_org_theme() && ! fs_is_network_admin() ) {
14763
+ // Also add submenu items when running in a free .org theme so the tabs will be visible.
14764
+ $add_submenu_items = true;
14765
+ } else if ( $this->is_activation_mode() && ! $this->is_free_wp_org_theme() ) {
14766
+ $add_submenu_items = false;
14767
+ } else if ( fs_is_network_admin() ) {
14768
+ /**
14769
+ * Add submenu items to network level when plugin was network
14770
+ * activated and the super-admin did NOT delegated the connection
14771
+ * of all sites to site admins.
14772
+ */
14773
+ $add_submenu_items = (
14774
+ $this->_is_network_active &&
14775
+ ( WP_FS__SHOW_NETWORK_EVEN_WHEN_DELEGATED ||
14776
+ ! $this->is_network_delegated_connection() )
14777
+ );
14778
+ } else {
14779
+ $add_submenu_items = ( ! $this->_is_network_active || $this->is_delegated_connection() );
14780
+ }
14781
+
14782
+ if ( $add_submenu_items ) {
14783
+ if ( $this->has_affiliate_program() ) {
14784
+ // Add affiliation page.
14785
+ $this->add_submenu_item(
14786
+ $this->get_text_inline( 'Affiliation', 'affiliation' ),
14787
+ array( &$this, '_affiliation_page_render' ),
14788
+ $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Affiliation', 'affiliation' ),
14789
+ 'manage_options',
14790
+ 'affiliation',
14791
+ 'Freemius::_clean_admin_content_section',
14792
+ WP_FS__DEFAULT_PRIORITY,
14793
+ $this->is_submenu_item_visible( 'affiliation' )
14794
+ );
14795
+ }
14796
+
14797
+ if ( ! WP_FS__DEMO_MODE && $this->is_registered() ) {
14798
+ $show_account = (
14799
+ $this->is_submenu_item_visible( 'account' ) &&
14800
+ /**
14801
+ * @since 1.2.2.7 Don't show the Account for free WP.org themes without any paid plans.
14802
+ */
14803
+ ( ! $this->is_free_wp_org_theme() || $this->has_paid_plan() )
14804
+ );
14805
+
14806
+ // Add user account page.
14807
+ $this->add_submenu_item(
14808
+ $this->get_text_inline( 'Account', 'account' ),
14809
+ array( &$this, '_account_page_render' ),
14810
+ $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Account', 'account' ),
14811
+ 'manage_options',
14812
+ 'account',
14813
+ array( &$this, '_account_page_load' ),
14814
+ WP_FS__DEFAULT_PRIORITY,
14815
+ $show_account
14816
+ );
14817
+ }
14818
+
14819
+ // Add contact page.
14820
+ $this->add_submenu_item(
14821
+ $this->get_text_inline( 'Contact Us', 'contact-us' ),
14822
+ array( &$this, '_contact_page_render' ),
14823
+ $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Contact Us', 'contact-us' ),
14824
+ 'manage_options',
14825
+ 'contact',
14826
+ 'Freemius::_clean_admin_content_section',
14827
+ WP_FS__DEFAULT_PRIORITY,
14828
+ $this->is_submenu_item_visible( 'contact' )
14829
+ );
14830
+
14831
+ if ( $this->has_addons() ) {
14832
+ $this->add_submenu_item(
14833
+ $this->get_text_inline( 'Add-Ons', 'add-ons' ),
14834
+ array( &$this, '_addons_page_render' ),
14835
+ $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Add-Ons', 'add-ons' ),
14836
+ 'manage_options',
14837
+ 'addons',
14838
+ array( &$this, '_addons_page_load' ),
14839
+ WP_FS__LOWEST_PRIORITY - 1,
14840
+ $this->is_submenu_item_visible( 'addons' )
14841
+ );
14842
+ }
14843
+
14844
+ if ( ! WP_FS__DEMO_MODE ) {
14845
+ $show_pricing = (
14846
+ $this->is_submenu_item_visible( 'pricing' ) &&
14847
+ $this->is_pricing_page_visible()
14848
+ );
14849
+
14850
+ $pricing_cta_text = $this->get_pricing_cta_label();
14851
+ $pricing_class = 'upgrade-mode';
14852
+ if ( $show_pricing ) {
14853
+ if ( $this->is_in_trial_promotion() &&
14854
+ ! $this->is_paying_or_trial()
14855
+ ) {
14856
+ // If running a trial promotion, modify the pricing to load the trial.
14857
+ $pricing_class = 'trial-mode';
14858
+ } else if ( $this->is_paying() ) {
14859
+ $pricing_class = '';
14860
+ }
14861
+ }
14862
+
14863
+ // Add upgrade/pricing page.
14864
+ $this->add_submenu_item(
14865
+ $pricing_cta_text . '&nbsp;&nbsp;' . ( is_rtl() ? '&#x2190;' : '&#x27a4;' ),
14866
+ array( &$this, '_pricing_page_render' ),
14867
+ $this->get_plugin_name() . ' &ndash; ' . $this->get_text_x_inline( 'Pricing', 'noun', 'pricing' ),
14868
+ 'manage_options',
14869
+ 'pricing',
14870
+ 'Freemius::_clean_admin_content_section',
14871
+ WP_FS__LOWEST_PRIORITY,
14872
+ $show_pricing,
14873
+ $pricing_class
14874
+ );
14875
+ }
14876
+ }
14877
+
14878
+ if ( 0 < count( $this->_menu_items ) ) {
14879
+ if ( ! $this->_menu->is_top_level() ) {
14880
+ fs_enqueue_local_style( 'fs_common', '/admin/common.css' );
14881
+
14882
+ // Append submenu items right after the plugin's submenu item.
14883
+ $this->order_sub_submenu_items();
14884
+ } else {
14885
+ // Append submenu items.
14886
+ $this->embed_submenu_items();
14887
+ }
14888
+ }
14889
+ }
14890
+
14891
+ /**
14892
+ * Moved the actual submenu item additions to a separated function,
14893
+ * in order to support sub-submenu items when the plugin's settings
14894
+ * only have a submenu and not top-level menu item.
14895
+ *
14896
+ * @author Vova Feldman (@svovaf)
14897
+ * @since 1.1.4
14898
+ */
14899
+ private function embed_submenu_items() {
14900
+ $item_template = $this->_menu->is_top_level() ?
14901
+ '<span class="fs-submenu-item %s %s %s">%s</span>' :
14902
+ '<span class="fs-submenu-item fs-sub %s %s %s">%s</span>';
14903
+
14904
+ $top_level_menu_capability = $this->get_top_level_menu_capability();
14905
+
14906
+ ksort( $this->_menu_items );
14907
+
14908
+ $is_first_submenu_item = true;
14909
+
14910
+ foreach ( $this->_menu_items as $priority => $items ) {
14911
+ foreach ( $items as $item ) {
14912
+ if ( $item['show_submenu'] && $is_first_submenu_item ) {
14913
+ if ( $this->_is_network_active && ! empty( $this->_dynamically_added_top_level_page_hook_name ) ) {
14914
+ $item['menu_slug'] = '';
14915
+
14916
+ $this->_menu->override_menu_item( $item['render_function'] );
14917
+ }
14918
+
14919
+ $is_first_submenu_item = false;
14920
+ }
14921
+
14922
+ $capability = ( ! empty( $item['capability'] ) ? $item['capability'] : $top_level_menu_capability );
14923
+
14924
+ $menu_item = sprintf(
14925
+ $item_template,
14926
+ $this->get_unique_affix(),
14927
+ $item['menu_slug'],
14928
+ ! empty( $item['class'] ) ? $item['class'] : '',
14929
+ $item['menu_title']
14930
+ );
14931
+
14932
+ $menu_slug = $this->_menu->get_slug( $item['menu_slug'] );
14933
+
14934
+ if ( ! isset( $item['url'] ) ) {
14935
+ $hook = FS_Admin_Menu_Manager::add_subpage(
14936
+ $item['show_submenu'] ?
14937
+ $this->get_top_level_menu_slug() :
14938
+ null,
14939
+ $item['page_title'],
14940
+ $menu_item,
14941
+ $capability,
14942
+ $menu_slug,
14943
+ $item['render_function']
14944
+ );
14945
+
14946
+ if ( false !== $item['before_render_function'] ) {
14947
+ add_action( "load-$hook", $item['before_render_function'] );
14948
+ }
14949
+ } else {
14950
+ FS_Admin_Menu_Manager::add_subpage(
14951
+ $item['show_submenu'] ?
14952
+ $this->get_top_level_menu_slug() :
14953
+ null,
14954
+ $item['page_title'],
14955
+ $menu_item,
14956
+ $capability,
14957
+ $menu_slug,
14958
+ array( $this, '' )
14959
+ );
14960
+ }
14961
+ }
14962
+ }
14963
+ }
14964
+
14965
+ /**
14966
+ * Re-order the submenu items so all Freemius added new submenu items
14967
+ * are added right after the plugin's settings submenu item.
14968
+ *
14969
+ * @author Vova Feldman (@svovaf)
14970
+ * @since 1.1.4
14971
+ */
14972
+ private function order_sub_submenu_items() {
14973
+ global $submenu;
14974
+
14975
+ $menu_slug = $this->_menu->get_top_level_menu_slug();
14976
+
14977
+ /**
14978
+ * Before "admin_menu" fires, WordPress will loop over the default submenus and remove pages for which the user
14979
+ * does not have permissions. So in case a plugin does not have top-level menu but does have submenus under any
14980
+ * of the default menus, only users that have the right role can access its sub-submenus (Account, Contact Us,
14981
+ * Support Forum, etc.) since $submenu[ $menu_slug ] will be empty if the user doesn't have permission.
14982
+ *
14983
+ * In case a plugin does not have submenus under any of the default menus but does have submenus under the menu
14984
+ * of another plugin, only users that have the right role can access its sub-submenus since we will use the
14985
+ * capability needed to access the parent menu as the capability for the submenus that we will add.
14986
+ */
14987
+ if ( empty( $submenu[ $menu_slug ] ) ) {
14988
+ return;
14989
+ }
14990
+
14991
+ $top_level_menu = &$submenu[ $menu_slug ];
14992
+
14993
+ $all_submenu_items_after = array();
14994
+
14995
+ $found_submenu_item = false;
14996
+
14997
+ foreach ( $top_level_menu as $submenu_id => $meta ) {
14998
+ if ( $found_submenu_item ) {
14999
+ // Remove all submenu items after the plugin's submenu item.
15000
+ $all_submenu_items_after[] = $meta;
15001
+ unset( $top_level_menu[ $submenu_id ] );
15002
+ }
15003
+
15004
+ if ( $this->_menu->get_raw_slug() === $meta[2] ) {
15005
+ // Found the submenu item, put all below.
15006
+ $found_submenu_item = true;
15007
+ continue;
15008
+ }
15009
+ }
15010
+
15011
+ // Embed all plugin's new submenu items.
15012
+ $this->embed_submenu_items();
15013
+
15014
+ // Start with specially high number to make sure it's appended.
15015
+ $i = max( 10000, max( array_keys( $top_level_menu ) ) + 1 );
15016
+ foreach ( $all_submenu_items_after as $meta ) {
15017
+ $top_level_menu[ $i ] = $meta;
15018
+ $i ++;
15019
+ }
15020
+
15021
+ // Sort submenu items.
15022
+ ksort( $top_level_menu );
15023
+ }
15024
+
15025
+ /**
15026
+ * Helper method to return the module's support forum URL.
15027
+ *
15028
+ * @author Vova Feldman (@svovaf)
15029
+ * @since 1.2.2.7
15030
+ *
15031
+ * @return string
15032
+ */
15033
+ function get_support_forum_url() {
15034
+ return $this->apply_filters( 'support_forum_url', "https://wordpress.org/support/{$this->_module_type}/{$this->_slug}" );
15035
+ }
15036
+
15037
+ /**
15038
+ * Displays the Support Forum link when enabled.
15039
+ *
15040
+ * Can be filtered like so:
15041
+ *
15042
+ * function _fs_show_support_menu( $is_visible, $menu_id ) {
15043
+ * if ( 'support' === $menu_id ) {
15044
+ * return _fs->is_registered();
15045
+ * }
15046
+ * return $is_visible;
15047
+ * }
15048
+ * _fs()->add_filter('is_submenu_visible', '_fs_show_support_menu', 10, 2);
15049
+ *
15050
+ */
15051
+ function _add_default_submenu_items() {
15052
+ if ( ! $this->is_on() ) {
15053
+ return;
15054
+ }
15055
+
15056
+ if ( ! $this->is_activation_mode() &&
15057
+ ( ( $this->_is_network_active && fs_is_network_admin() ) ||
15058
+ ( ! $this->_is_network_active && is_admin() ) )
15059
+ ) {
15060
+ $this->add_submenu_link_item(
15061
+ $this->apply_filters( 'support_forum_submenu', $this->get_text_inline( 'Support Forum', 'support-forum' ) ),
15062
+ $this->get_support_forum_url(),
15063
+ 'wp-support-forum',
15064
+ null,
15065
+ 50,
15066
+ $this->is_submenu_item_visible( 'support' )
15067
+ );
15068
+ }
15069
+ }
15070
+
15071
+ /**
15072
+ * @author Vova Feldman (@svovaf)
15073
+ * @since 1.0.1
15074
+ *
15075
+ * @param string $menu_title
15076
+ * @param callable $render_function
15077
+ * @param bool|string $page_title
15078
+ * @param string $capability
15079
+ * @param bool|string $menu_slug
15080
+ * @param bool|callable $before_render_function
15081
+ * @param int $priority
15082
+ * @param bool $show_submenu
15083
+ * @param string $class Since 1.2.1.5 can add custom classes to menu items.
15084
+ */
15085
+ function add_submenu_item(
15086
+ $menu_title,
15087
+ $render_function,
15088
+ $page_title = false,
15089
+ $capability = 'manage_options',
15090
+ $menu_slug = false,
15091
+ $before_render_function = false,
15092
+ $priority = WP_FS__DEFAULT_PRIORITY,
15093
+ $show_submenu = true,
15094
+ $class = ''
15095
+ ) {
15096
+ $this->_logger->entrance( 'Title = ' . $menu_title );
15097
+
15098
+ if ( $this->is_addon() ) {
15099
+ $parent_fs = $this->get_parent_instance();
15100
+
15101
+ if ( is_object( $parent_fs ) ) {
15102
+ $parent_fs->add_submenu_item(
15103
+ $menu_title,
15104
+ $render_function,
15105
+ $page_title,
15106
+ $capability,
15107
+ $menu_slug,
15108
+ $before_render_function,
15109
+ $priority,
15110
+ $show_submenu,
15111
+ $class
15112
+ );
15113
+
15114
+ return;
15115
+ }
15116
+ }
15117
+
15118
+ if ( ! isset( $this->_menu_items[ $priority ] ) ) {
15119
+ $this->_menu_items[ $priority ] = array();
15120
+ }
15121
+
15122
+ $this->_menu_items[ $priority ][] = array(
15123
+ 'page_title' => is_string( $page_title ) ? $page_title : $menu_title,
15124
+ 'menu_title' => $menu_title,
15125
+ 'capability' => $capability,
15126
+ 'menu_slug' => is_string( $menu_slug ) ? $menu_slug : strtolower( $menu_title ),
15127
+ 'render_function' => $render_function,
15128
+ 'before_render_function' => $before_render_function,
15129
+ 'show_submenu' => $show_submenu,
15130
+ 'class' => $class,
15131
+ );
15132
+ }
15133
+
15134
+ /**
15135
+ * @author Vova Feldman (@svovaf)
15136
+ * @since 1.0.1
15137
+ *
15138
+ * @param string $menu_title
15139
+ * @param string $url
15140
+ * @param bool $menu_slug
15141
+ * @param string $capability
15142
+ * @param int $priority
15143
+ * @param bool $show_submenu
15144
+ */
15145
+ function add_submenu_link_item(
15146
+ $menu_title,
15147
+ $url,
15148
+ $menu_slug = false,
15149
+ $capability = 'read',
15150
+ $priority = WP_FS__DEFAULT_PRIORITY,
15151
+ $show_submenu = true
15152
+ ) {
15153
+ $this->_logger->entrance( 'Title = ' . $menu_title . '; Url = ' . $url );
15154
+
15155
+ if ( $this->is_addon() ) {
15156
+ $parent_fs = $this->get_parent_instance();
15157
+
15158
+ if ( is_object( $parent_fs ) ) {
15159
+ $parent_fs->add_submenu_link_item(
15160
+ $menu_title,
15161
+ $url,
15162
+ $menu_slug,
15163
+ $capability,
15164
+ $priority,
15165
+ $show_submenu
15166
+ );
15167
+
15168
+ return;
15169
+ }
15170
+ }
15171
+
15172
+ if ( ! isset( $this->_menu_items[ $priority ] ) ) {
15173
+ $this->_menu_items[ $priority ] = array();
15174
+ }
15175
+
15176
+ $this->_menu_items[ $priority ][] = array(
15177
+ 'menu_title' => $menu_title,
15178
+ 'capability' => $capability,
15179
+ 'menu_slug' => is_string( $menu_slug ) ? $menu_slug : strtolower( $menu_title ),
15180
+ 'url' => $url,
15181
+ 'page_title' => $menu_title,
15182
+ 'render_function' => 'fs_dummy',
15183
+ 'before_render_function' => '',
15184
+ 'show_submenu' => $show_submenu,
15185
+ );
15186
+ }
15187
+
15188
+ #endregion ------------------------------------------------------------------
15189
+
15190
+
15191
+ #--------------------------------------------------------------------------------
15192
+ #region Actions / Hooks / Filters
15193
+ #--------------------------------------------------------------------------------
15194
+
15195
+ /**
15196
+ * @author Vova Feldman (@svovaf)
15197
+ * @since 1.1.7
15198
+ *
15199
+ * @param string $tag
15200
+ *
15201
+ * @return string
15202
+ */
15203
+ public function get_action_tag( $tag ) {
15204
+ return self::get_action_tag_static( $tag, $this->_slug, $this->is_plugin() );
15205
+ }
15206
+
15207
+ /**
15208
+ * @author Vova Feldman (@svovaf)
15209
+ * @since 1.2.1.6
15210
+ *
15211
+ * @param string $tag
15212
+ * @param string $slug
15213
+ * @param bool $is_plugin
15214
+ *
15215
+ * @return string
15216
+ */
15217
+ static function get_action_tag_static( $tag, $slug = '', $is_plugin = true ) {
15218
+ $action = "fs_{$tag}";
15219
+
15220
+ if ( ! empty( $slug ) ) {
15221
+ $action .= '_' . self::get_module_unique_affix( $slug, $is_plugin );
15222
+ }
15223
+
15224
+ return $action;
15225
+ }
15226
+
15227
+ /**
15228
+ * Returns a string that can be used to generate a unique action name,
15229
+ * option name, HTML element ID, or HTML element class.
15230
+ *
15231
+ * @author Leo Fajardo (@leorw)
15232
+ * @since 1.2.2
15233
+ *
15234
+ * @return string
15235
+ */
15236
+ public function get_unique_affix() {
15237
+ return self::get_module_unique_affix( $this->_slug, $this->is_plugin() );
15238
+ }
15239
+
15240
+ /**
15241
+ * Returns a string that can be used to generate a unique action name,
15242
+ * option name, HTML element ID, or HTML element class.
15243
+ *
15244
+ * @author Vova Feldman (@svovaf)
15245
+ * @since 1.2.2.5
15246
+ *
15247
+ * @param string $slug
15248
+ * @param bool $is_plugin
15249
+ *
15250
+ * @return string
15251
+ */
15252
+ static function get_module_unique_affix( $slug, $is_plugin = true ) {
15253
+ $affix = $slug;
15254
+
15255
+ if ( ! $is_plugin ) {
15256
+ $affix .= '-' . WP_FS__MODULE_TYPE_THEME;
15257
+ }
15258
+
15259
+ return $affix;
15260
+ }
15261
+
15262
+ /**
15263
+ * @author Vova Feldman (@svovaf)
15264
+ * @since 1.2.1
15265
+ * @since 1.2.2.5 The AJAX action names are based on the module ID, not like the non-AJAX actions that are
15266
+ * based on the slug for backward compatibility.
15267
+ *
15268
+ * @param string $tag
15269
+ *
15270
+ * @return string
15271
+ */
15272
+ function get_ajax_action( $tag ) {
15273
+ return self::get_ajax_action_static( $tag, $this->_module_id );
15274
+ }
15275
+
15276
+ /**
15277
+ * @author Vova Feldman (@svovaf)
15278
+ * @since 1.2.1.7
15279
+ *
15280
+ * @param string $tag
15281
+ *
15282
+ * @return string
15283
+ */
15284
+ function get_ajax_security( $tag ) {
15285
+ return wp_create_nonce( $this->get_ajax_action( $tag ) );
15286
+ }
15287
+
15288
+ /**
15289
+ * @author Vova Feldman (@svovaf)
15290
+ * @since 1.2.1.7
15291
+ *
15292
+ * @param string $tag
15293
+ */
15294
+ function check_ajax_referer( $tag ) {
15295
+ check_ajax_referer( $this->get_ajax_action( $tag ), 'security' );
15296
+ }
15297
+
15298
+ /**
15299
+ * @author Vova Feldman (@svovaf)
15300
+ * @since 1.2.1.6
15301
+ * @since 1.2.2.5 The AJAX action names are based on the module ID, not like the non-AJAX actions that are
15302
+ * based on the slug for backward compatibility.
15303
+ *
15304
+ * @param string $tag
15305
+ * @param number|null $module_id
15306
+ *
15307
+ * @return string
15308
+ */
15309
+ private static function get_ajax_action_static( $tag, $module_id = null ) {
15310
+ $action = "fs_{$tag}";
15311
+
15312
+ if ( ! empty( $module_id ) ) {
15313
+ $action .= "_{$module_id}";
15314
+ }
15315
+
15316
+ return $action;
15317
+ }
15318
+
15319
+ /**
15320
+ * Do action, specific for the current context plugin.
15321
+ *
15322
+ * @author Vova Feldman (@svovaf)
15323
+ * @since 1.0.1
15324
+ *
15325
+ * @param string $tag The name of the action to be executed.
15326
+ * @param mixed $arg,... Optional. Additional arguments which are passed on to the
15327
+ * functions hooked to the action. Default empty.
15328
+ *
15329
+ * @uses do_action()
15330
+ */
15331
+ function do_action( $tag, $arg = '' ) {
15332
+ $this->_logger->entrance( $tag );
15333
+
15334
+ $args = func_get_args();
15335
+
15336
+ call_user_func_array( 'do_action', array_merge(
15337
+ array( $this->get_action_tag( $tag ) ),
15338
+ array_slice( $args, 1 ) )
15339
+ );
15340
+ }
15341
+
15342
+ /**
15343
+ * Add action, specific for the current context plugin.
15344
+ *
15345
+ * @author Vova Feldman (@svovaf)
15346
+ * @since 1.0.1
15347
+ *
15348
+ * @param string $tag
15349
+ * @param callable $function_to_add
15350
+ * @param int $priority
15351
+ * @param int $accepted_args
15352
+ *
15353
+ * @uses add_action()
15354
+ */
15355
+ function add_action(
15356
+ $tag,
15357
+ $function_to_add,
15358
+ $priority = WP_FS__DEFAULT_PRIORITY,
15359
+ $accepted_args = 1
15360
+ ) {
15361
+ $this->_logger->entrance( $tag );
15362
+
15363
+ add_action( $this->get_action_tag( $tag ), $function_to_add, $priority, $accepted_args );
15364
+ }
15365
+
15366
+ /**
15367
+ * Add AJAX action, specific for the current context plugin.
15368
+ *
15369
+ * @author Vova Feldman (@svovaf)
15370
+ * @since 1.2.1
15371
+ *
15372
+ * @param string $tag
15373
+ * @param callable $function_to_add
15374
+ * @param int $priority
15375
+ *
15376
+ * @uses add_action()
15377
+ *
15378
+ * @return bool True if action added, false if no need to add the action since the AJAX call isn't matching.
15379
+ */
15380
+ function add_ajax_action(
15381
+ $tag,
15382
+ $function_to_add,
15383
+ $priority = WP_FS__DEFAULT_PRIORITY
15384
+ ) {
15385
+ $this->_logger->entrance( $tag );
15386
+
15387
+ return self::add_ajax_action_static(
15388
+ $tag,
15389
+ $function_to_add,
15390
+ $priority,
15391
+ $this->_module_id
15392
+ );
15393
+ }
15394
+
15395
+ /**
15396
+ * Add AJAX action.
15397
+ *
15398
+ * @author Vova Feldman (@svovaf)
15399
+ * @since 1.2.1.6
15400
+ *
15401
+ * @param string $tag
15402
+ * @param callable $function_to_add
15403
+ * @param int $priority
15404
+ * @param number|null $module_id
15405
+ *
15406
+ * @return bool True if action added, false if no need to add the action since the AJAX call isn't matching.
15407
+ * @uses add_action()
15408
+ *
15409
+ */
15410
+ static function add_ajax_action_static(
15411
+ $tag,
15412
+ $function_to_add,
15413
+ $priority = WP_FS__DEFAULT_PRIORITY,
15414
+ $module_id = null
15415
+ ) {
15416
+ self::$_static_logger->entrance( $tag );
15417
+
15418
+ if ( ! self::is_ajax_action_static( $tag, $module_id ) ) {
15419
+ return false;
15420
+ }
15421
+
15422
+ add_action(
15423
+ 'wp_ajax_' . self::get_ajax_action_static( $tag, $module_id ),
15424
+ $function_to_add,
15425
+ $priority,
15426
+ 0
15427
+ );
15428
+
15429
+ self::$_static_logger->info( "$tag AJAX callback action added." );
15430
+
15431
+ return true;
15432
+ }
15433
+
15434
+ /**
15435
+ * Send a JSON response back to an Ajax request.
15436
+ *
15437
+ * @author Vova Feldman (@svovaf)
15438
+ * @since 1.2.1.5
15439
+ *
15440
+ * @param mixed $response
15441
+ */
15442
+ static function shoot_ajax_response( $response ) {
15443
+ wp_send_json( $response );
15444
+ }
15445
+
15446
+ /**
15447
+ * Send a JSON response back to an Ajax request, indicating success.
15448
+ *
15449
+ * @author Vova Feldman (@svovaf)
15450
+ * @since 1.2.1.5
15451
+ *
15452
+ * @param mixed $data Data to encode as JSON, then print and exit.
15453
+ */
15454
+ static function shoot_ajax_success( $data = null ) {
15455
+ wp_send_json_success( $data );
15456
+ }
15457
+
15458
+ /**
15459
+ * Send a JSON response back to an Ajax request, indicating failure.
15460
+ *
15461
+ * @author Vova Feldman (@svovaf)
15462
+ * @since 1.2.1.5
15463
+ *
15464
+ * @param mixed $error Optional error message.
15465
+ */
15466
+ static function shoot_ajax_failure( $error = '' ) {
15467
+ $result = array( 'success' => false );
15468
+ if ( ! empty( $error ) ) {
15469
+ $result['error'] = $error;
15470
+ }
15471
+
15472
+ wp_send_json( $result );
15473
+ }
15474
+
15475
+ /**
15476
+ * Apply filter, specific for the current context plugin.
15477
+ *
15478
+ * @author Vova Feldman (@svovaf)
15479
+ * @since 1.0.9
15480
+ *
15481
+ * @param string $tag The name of the filter hook.
15482
+ * @param mixed $value The value on which the filters hooked to `$tag` are applied on.
15483
+ *
15484
+ * @return mixed The filtered value after all hooked functions are applied to it.
15485
+ *
15486
+ * @uses apply_filters()
15487
+ */
15488
+ function apply_filters( $tag, $value ) {
15489
+ $this->_logger->entrance( $tag );
15490
+
15491
+ $args = func_get_args();
15492
+ array_unshift( $args, $this->get_unique_affix() );
15493
+
15494
+ return call_user_func_array( 'fs_apply_filter', $args );
15495
+ }
15496
+
15497
+ /**
15498
+ * Add filter, specific for the current context plugin.
15499
+ *
15500
+ * @author Vova Feldman (@svovaf)
15501
+ * @since 1.0.9
15502
+ *
15503
+ * @param string $tag
15504
+ * @param callable $function_to_add
15505
+ * @param int $priority
15506
+ * @param int $accepted_args
15507
+ *
15508
+ * @uses add_filter()
15509
+ */
15510
+ function add_filter( $tag, $function_to_add, $priority = WP_FS__DEFAULT_PRIORITY, $accepted_args = 1 ) {
15511
+ $this->_logger->entrance( $tag );
15512
+
15513
+ add_filter( $this->get_action_tag( $tag ), $function_to_add, $priority, $accepted_args );
15514
+ }
15515
+
15516
+ /**
15517
+ * Check if has filter.
15518
+ *
15519
+ * @author Vova Feldman (@svovaf)
15520
+ * @since 1.1.4
15521
+ *
15522
+ * @param string $tag
15523
+ * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
15524
+ *
15525
+ * @return false|int
15526
+ *
15527
+ * @uses has_filter()
15528
+ */
15529
+ function has_filter( $tag, $function_to_check = false ) {
15530
+ $this->_logger->entrance( $tag );
15531
+
15532
+ return has_filter( $this->get_action_tag( $tag ), $function_to_check );
15533
+ }
15534
+
15535
+ #endregion
15536
+
15537
+ /**
15538
+ * Override default i18n text phrases.
15539
+ *
15540
+ * @author Vova Feldman (@svovaf)
15541
+ * @since 1.1.6
15542
+ *
15543
+ * @param string[] string $key_value
15544
+ *
15545
+ * @uses fs_override_i18n()
15546
+ */
15547
+ function override_i18n( $key_value ) {
15548
+ fs_override_i18n( $key_value, $this->_slug );
15549
+ }
15550
+
15551
+ /* Account Page
15552
+ ------------------------------------------------------------------------------------------------------------------*/
15553
+ /**
15554
+ * Update site information.
15555
+ *
15556
+ * @author Vova Feldman (@svovaf)
15557
+ * @since 1.0.1
15558
+ *
15559
+ * @param bool $store Flush to Database if true.
15560
+ * @param null|int $network_level_or_blog_id Since 2.0.0
15561
+ * @param \FS_Site $site Since 2.0.0
15562
+ */
15563
+ private function _store_site( $store = true, $network_level_or_blog_id = null, FS_Site $site = null ) {
15564
+ $this->_logger->entrance();
15565
+
15566
+ if ( empty( $this->_site->id ) ) {
15567
+ $this->_logger->error( "Empty install ID, can't store site." );
15568
+
15569
+ return;
15570
+ }
15571
+
15572
+ $encrypted_site = clone ( is_object( $site ) ? $site : $this->_site );
15573
+
15574
+ $sites = self::get_all_sites( $this->_module_type, $network_level_or_blog_id );
15575
+
15576
+ $prev_stored_user_id = $this->_storage->get( 'prev_user_id', false, $network_level_or_blog_id );
15577
+
15578
+ if ( empty( $prev_stored_user_id ) &&
15579
+ $this->_user->id != $this->_site->user_id
15580
+ ) {
15581
+ /**
15582
+ * Store the current user ID as the previous user ID so that the previous user can be used
15583
+ * as the install's owner while the new owner's details are not yet available.
15584
+ *
15585
+ * This will be executed only in the `replica` site. For example, there are 2 sites, namely `original`
15586
+ * and `replica`, then an ownership change was initiated and completed in the `original`, the `replica`
15587
+ * will be using the previous user until it is updated again (e.g.: until the next clone of `original`
15588
+ * into `replica`.
15589
+ *
15590
+ * @author Leo Fajardo (@leorw)
15591
+ */
15592
+ $this->_storage->store( 'prev_user_id', $sites[ $this->_slug ]->user_id, $network_level_or_blog_id );
15593
+ }
15594
+
15595
+ $sites[ $this->_slug ] = $encrypted_site;
15596
+
15597
+ $this->set_account_option( 'sites', $sites, $store, $network_level_or_blog_id );
15598
+ }
15599
+
15600
+ /**
15601
+ * Update plugin's plans information.
15602
+ *
15603
+ * @author Vova Feldman (@svovaf)
15604
+ * @since 1.0.2
15605
+ *
15606
+ * @param bool $store Flush to Database if true.
15607
+ */
15608
+ private function _store_plans( $store = true ) {
15609
+ $this->_logger->entrance();
15610
+
15611
+ $plans = self::get_all_plans( $this->_module_type );
15612
+
15613
+ // Copy plans.
15614
+ $encrypted_plans = array();
15615
+ for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
15616
+ $encrypted_plans[] = self::_encrypt_entity( $this->_plans[ $i ] );
15617
+ }
15618
+
15619
+ $plans[ $this->_slug ] = $encrypted_plans;
15620
+
15621
+ $this->set_account_option( 'plans', $plans, $store );
15622
+ }
15623
+
15624
+ /**
15625
+ * Update user's plugin licenses.
15626
+ *
15627
+ * @author Vova Feldman (@svovaf)
15628
+ * @since 1.0.5
15629
+ *
15630
+ * @param bool $store
15631
+ * @param number|bool $module_id
15632
+ * @param FS_Plugin_License[] $licenses
15633
+ */
15634
+ private function _store_licenses( $store = true, $module_id = false, $licenses = array() ) {
15635
+ $this->_logger->entrance();
15636
+
15637
+ $all_licenses = self::get_all_licenses();
15638
+
15639
+ if ( ! FS_Plugin::is_valid_id( $module_id ) ) {
15640
+ $module_id = $this->_module_id;
15641
+
15642
+ $user_licenses = is_array( $this->_licenses ) ?
15643
+ $this->_licenses :
15644
+ array();
15645
+
15646
+ if ( empty( $user_licenses ) ) {
15647
+ // If the context user doesn't have any license, don't update the licenses collection.
15648
+ return;
15649
+ }
15650
+
15651
+ $new_user_licenses_map = array();
15652
+ foreach ( $user_licenses as $user_license ) {
15653
+ $new_user_licenses_map[ $user_license->id ] = $user_license;
15654
+ }
15655
+
15656
+ self::store_user_id_license_ids_map( array_keys( $new_user_licenses_map ), $this->_module_id, $this->_user->id );
15657
+
15658
+ // Update user licenses.
15659
+ $licenses_to_update_count = count( $new_user_licenses_map );
15660
+ foreach ( $all_licenses[ $module_id ] as $key => $license ) {
15661
+ if ( 0 === $licenses_to_update_count ) {
15662
+ break;
15663
+ }
15664
+
15665
+ if ( isset( $new_user_licenses_map[ $license->id ] ) ) {
15666
+ // Update license.
15667
+ $all_licenses[ $module_id ][ $key ] = $new_user_licenses_map[ $license->id ];
15668
+ unset( $new_user_licenses_map[ $license->id ] );
15669
+
15670
+ $licenses_to_update_count --;
15671
+ }
15672
+ }
15673
+
15674
+ if ( ! empty( $new_user_licenses_map ) ) {
15675
+ // Add new licenses.
15676
+ $all_licenses[ $module_id ] = array_merge( array_values( $new_user_licenses_map ), $all_licenses[ $module_id ] );
15677
+ }
15678
+
15679
+ $licenses = $all_licenses[ $module_id ];
15680
+ }
15681
+
15682
+ if ( ! isset( $all_licenses[ $module_id ] ) ) {
15683
+ $all_licenses[ $module_id ] = array();
15684
+ }
15685
+
15686
+ $all_licenses[ $module_id ] = $licenses;
15687
+
15688
+ self::$_accounts->set_option( 'all_licenses', $all_licenses, $store );
15689
+ }
15690
+
15691
+ /**
15692
+ * Update user information.
15693
+ *
15694
+ * @author Vova Feldman (@svovaf)
15695
+ * @since 1.0.1
15696
+ *
15697
+ * @param bool $store Flush to Database if true.
15698
+ */
15699
+ private function _store_user( $store = true ) {
15700
+ $this->_logger->entrance();
15701
+
15702
+ if ( empty( $this->_user->id ) ) {
15703
+ $this->_logger->error( "Empty user ID, can't store user." );
15704
+
15705
+ return;
15706
+ }
15707
+
15708
+ $users = self::get_all_users();
15709
+ $users[ $this->_user->id ] = $this->_user;
15710
+ self::$_accounts->set_option( 'users', $users, $store );
15711
+ }
15712
+
15713
+ /**
15714
+ * Update new updates information.
15715
+ *
15716
+ * @author Vova Feldman (@svovaf)
15717
+ * @since 1.0.4
15718
+ *
15719
+ * @param FS_Plugin_Tag|null $update
15720
+ * @param bool $store Flush to Database if true.
15721
+ * @param bool|number $plugin_id
15722
+ */
15723
+ private function _store_update( $update, $store = true, $plugin_id = false ) {
15724
+ $this->_logger->entrance();
15725
+
15726
+ if ( $update instanceof FS_Plugin_Tag ) {
15727
+ $update->updated = time();
15728
+ }
15729
+
15730
+ if ( ! is_numeric( $plugin_id ) ) {
15731
+ $plugin_id = $this->_plugin->id;
15732
+ }
15733
+
15734
+ $updates = self::get_all_updates();
15735
+ $updates[ $plugin_id ] = $update;
15736
+ self::$_accounts->set_option( 'updates', $updates, $store );
15737
+ }
15738
+
15739
+ /**
15740
+ * Update new updates information.
15741
+ *
15742
+ * @author Vova Feldman (@svovaf)
15743
+ * @since 1.0.6
15744
+ *
15745
+ * @param FS_Plugin[] $plugin_addons
15746
+ * @param bool $store Flush to Database if true.
15747
+ */
15748
+ private function _store_addons( $plugin_addons, $store = true ) {
15749
+ $this->_logger->entrance();
15750
+
15751
+ $addons = self::get_all_addons();
15752
+ $addons[ $this->_plugin->id ] = $plugin_addons;
15753
+ self::$_accounts->set_option( 'addons', $addons, $store );
15754
+ }
15755
+
15756
+ /**
15757
+ * Delete plugin's associated add-ons.
15758
+ *
15759
+ * @author Vova Feldman (@svovaf)
15760
+ * @since 1.0.8
15761
+ *
15762
+ * @param bool $store
15763
+ *
15764
+ * @return bool
15765
+ */
15766
+ private function _delete_account_addons( $store = true ) {
15767
+ $all_addons = self::get_all_account_addons();
15768
+
15769
+ if ( ! isset( $all_addons[ $this->_plugin->id ] ) ) {
15770
+ return false;
15771
+ }
15772
+
15773
+ unset( $all_addons[ $this->_plugin->id ] );
15774
+
15775
+ self::$_accounts->set_option( 'account_addons', $all_addons, $store );
15776
+
15777
+ return true;
15778
+ }
15779
+
15780
+ /**
15781
+ * Update account add-ons list.
15782
+ *
15783
+ * @author Vova Feldman (@svovaf)
15784
+ * @since 1.0.6
15785
+ *
15786
+ * @param FS_Plugin[] $addons
15787
+ * @param bool $store Flush to Database if true.
15788
+ */
15789
+ private function _store_account_addons( $addons, $store = true ) {
15790
+ $this->_logger->entrance();
15791
+
15792
+ $all_addons = self::get_all_account_addons();
15793
+ $all_addons[ $this->_plugin->id ] = $addons;
15794
+ self::$_accounts->set_option( 'account_addons', $all_addons, $store );
15795
+ }
15796
+
15797
+ /**
15798
+ * Store account params in the Database.
15799
+ *
15800
+ * @author Vova Feldman (@svovaf)
15801
+ * @since 1.0.1
15802
+ *
15803
+ * @param null|int $blog_id Since 2.0.0
15804
+ */
15805
+ private function _store_account( $blog_id = null ) {
15806
+ $this->_logger->entrance();
15807
+
15808
+ $this->_store_site( false, $blog_id );
15809
+ $this->_store_user( false );
15810
+ $this->_store_plans( false );
15811
+ $this->_store_licenses( false );
15812
+
15813
+ self::$_accounts->store( $blog_id );
15814
+ }
15815
+
15816
+ /**
15817
+ * Sync user's information.
15818
+ *
15819
+ * @author Vova Feldman (@svovaf)
15820
+ * @since 1.0.3
15821
+ * @uses FS_Api
15822
+ */
15823
+ private function _handle_account_user_sync() {
15824
+ $this->_logger->entrance();
15825
+
15826
+ $api = $this->get_api_user_scope();
15827
+
15828
+ // Get user's information.
15829
+ $user = $api->get( '/', true );
15830
+
15831
+ if ( isset( $user->id ) ) {
15832
+ $this->_user->first = $user->first;
15833
+ $this->_user->last = $user->last;
15834
+ $this->_user->email = $user->email;
15835
+
15836
+ $is_menu_item_account_visible = $this->is_submenu_item_visible( 'account' );
15837
+
15838
+ if ( $user->is_verified &&
15839
+ ( ! isset( $this->_user->is_verified ) || false === $this->_user->is_verified )
15840
+ ) {
15841
+ $this->_user->is_verified = true;
15842
+
15843
+ $this->do_action( 'account_email_verified', $user->email );
15844
+
15845
+ $this->_admin_notices->add(
15846
+ $this->get_text_inline( 'Your email has been successfully verified - you are AWESOME!', 'email-verified-message' ),
15847
+ $this->get_text_x_inline( 'Right on', 'a positive response', 'right-on' ) . '!',
15848
+ 'success',
15849
+ // Make admin sticky if account menu item is invisible,
15850
+ // since the page will be auto redirected to the plugin's
15851
+ // main settings page, and the non-sticky message
15852
+ // will disappear.
15853
+ ! $is_menu_item_account_visible,
15854
+ 'email_verified'
15855
+ );
15856
+ }
15857
+
15858
+ // Flush user details to DB.
15859
+ $this->_store_user();
15860
+
15861
+ $this->do_action( 'after_account_user_sync', $user );
15862
+
15863
+ /**
15864
+ * If account menu item is hidden, redirect to plugin's main settings page.
15865
+ *
15866
+ * @author Vova Feldman (@svovaf)
15867
+ * @since 1.1.6
15868
+ *
15869
+ * @link https://github.com/Freemius/wordpress-sdk/issues/6
15870
+ */
15871
+ if ( ! $is_menu_item_account_visible ) {
15872
+ fs_redirect( $this->_get_admin_page_url() );
15873
+ }
15874
+ }
15875
+ }
15876
+
15877
+ /**
15878
+ * @author Vova Feldman (@svovaf)
15879
+ * @since 1.0.9
15880
+ * @uses FS_Api
15881
+ *
15882
+ * @param number|bool $license_id
15883
+ *
15884
+ * @return FS_Subscription|object|bool
15885
+ */
15886
+ private function _fetch_site_license_subscription( $license_id = false ) {
15887
+ $this->_logger->entrance();
15888
+ $api = $this->get_api_site_scope();
15889
+
15890
+ if ( ! is_numeric( $license_id ) ) {
15891
+ $license_id = $this->_license->id;
15892
+ }
15893
+
15894
+ $result = $api->get( "/licenses/{$license_id}/subscriptions.json", true );
15895
+
15896
+ return ! isset( $result->error ) ?
15897
+ ( ( is_array( $result->subscriptions ) && 0 < count( $result->subscriptions ) ) ?
15898
+ new FS_Subscription( $result->subscriptions[0] ) :
15899
+ false
15900
+ ) :
15901
+ $result;
15902
+ }
15903
+
15904
+ /**
15905
+ * @author Vova Feldman (@svovaf)
15906
+ * @since 1.0.4
15907
+ * @uses FS_Api
15908
+ *
15909
+ * @param number|bool $plan_id
15910
+ *
15911
+ * @return FS_Plugin_Plan|object
15912
+ */
15913
+ private function _fetch_site_plan( $plan_id = false ) {
15914
+ $this->_logger->entrance();
15915
+ $api = $this->get_api_site_scope();
15916
+
15917
+ if ( ! is_numeric( $plan_id ) ) {
15918
+ $plan_id = $this->_site->plan_id;
15919
+ }
15920
+
15921
+ $plan = $api->get( "/plans/{$plan_id}.json", true );
15922
+
15923
+ return ! isset( $plan->error ) ? new FS_Plugin_Plan( $plan ) : $plan;
15924
+ }
15925
+
15926
+ /**
15927
+ * @author Vova Feldman (@svovaf)
15928
+ * @since 1.0.5
15929
+ * @uses FS_Api
15930
+ *
15931
+ * @return FS_Plugin_Plan[]|object
15932
+ */
15933
+ private function _fetch_plugin_plans() {
15934
+ $this->_logger->entrance();
15935
+ $api = $this->get_current_or_network_user_api_scope();
15936
+
15937
+ /**
15938
+ * @since 1.2.3 When running in DEV mode, retrieve pending plans as well.
15939
+ */
15940
+ $result = $api->get( "/plugins/{$this->_module_id}/plans.json?show_pending=" . ( $this->has_secret_key() ? 'true' : 'false' ), true );
15941
+
15942
+ if ( $this->is_api_result_object( $result, 'plans' ) && is_array( $result->plans ) ) {
15943
+ for ( $i = 0, $len = count( $result->plans ); $i < $len; $i ++ ) {
15944
+ $result->plans[ $i ] = new FS_Plugin_Plan( $result->plans[ $i ] );
15945
+ }
15946
+
15947
+ $result = $result->plans;
15948
+ }
15949
+
15950
+ return $result;
15951
+ }
15952
+
15953
+ /**
15954
+ * @author Vova Feldman (@svovaf)
15955
+ * @since 2.0.0
15956
+ *
15957
+ * @param number $plan_id
15958
+ *
15959
+ * @return \FS_Plugin_Plan|object
15960
+ */
15961
+ private function fetch_plan_by_id( $plan_id ) {
15962
+ $this->_logger->entrance();
15963
+ $api = $this->get_current_or_network_user_api_scope();
15964
+
15965
+ $result = $api->get( "/plugins/{$this->_module_id}/plans/{$plan_id}.json", true );
15966
+
15967
+ return $this->is_api_result_entity( $result ) ?
15968
+ new FS_Plugin_Plan( $result ) :
15969
+ $result;
15970
+ }
15971
+
15972
+ /**
15973
+ * @author Vova Feldman (@svovaf)
15974
+ * @since 1.0.5
15975
+ * @uses FS_Api
15976
+ *
15977
+ * @param number|bool $plugin_id
15978
+ * @param number|bool $site_license_id
15979
+ * @param array $foreign_licenses @since 2.0.0. This is used by network-activated plugins.
15980
+ * @param number|null $blog_id
15981
+ *
15982
+ * @return FS_Plugin_License[]|object
15983
+ */
15984
+ private function _fetch_licenses(
15985
+ $plugin_id = false,
15986
+ $site_license_id = false,
15987
+ $foreign_licenses = array(),
15988
+ $blog_id = null
15989
+ ) {
15990
+ $this->_logger->entrance();
15991
+
15992
+ $api = $this->get_api_user_scope();
15993
+
15994
+ if ( ! is_numeric( $plugin_id ) ) {
15995
+ $plugin_id = $this->_plugin->id;
15996
+ }
15997
+
15998
+ $user_licenses_endpoint = "/plugins/{$plugin_id}/licenses.json";
15999
+ if ( ! empty ( $foreign_licenses ) ) {
16000
+ $foreign_licenses = array(
16001
+ // Prefix with `+` to tell the server to include foreign licenses in the licenses collection.
16002
+ 'ids' => ( urlencode( '+' ) . implode( ',', $foreign_licenses['ids'] ) ),
16003
+ 'license_keys' => implode( ',', array_map( 'urlencode', $foreign_licenses['license_keys'] ) )
16004
+ );
16005
+
16006
+ $user_licenses_endpoint = add_query_arg( $foreign_licenses, $user_licenses_endpoint );
16007
+ }
16008
+
16009
+ $result = $api->get( $user_licenses_endpoint, true );
16010
+
16011
+ $is_site_license_synced = false;
16012
+
16013
+ $api_errors = array();
16014
+
16015
+ if ( $this->is_api_result_object( $result, 'licenses' ) &&
16016
+ is_array( $result->licenses )
16017
+ ) {
16018
+ for ( $i = 0, $len = count( $result->licenses ); $i < $len; $i ++ ) {
16019
+ $result->licenses[ $i ] = new FS_Plugin_License( $result->licenses[ $i ] );
16020
+
16021
+ if ( ( ! $is_site_license_synced ) && is_numeric( $site_license_id ) ) {
16022
+ $is_site_license_synced = ( $site_license_id == $result->licenses[ $i ]->id );
16023
+ }
16024
+ }
16025
+
16026
+ $result = $result->licenses;
16027
+ } else {
16028
+ $api_errors[] = $result;
16029
+ $result = array();
16030
+ }
16031
+
16032
+ if ( ! $is_site_license_synced ) {
16033
+ if ( ! is_null( $blog_id ) ) {
16034
+ /**
16035
+ * If blog ID is not null, the request is for syncing of the license of a single site via the
16036
+ * network-level "Account" page.
16037
+ *
16038
+ * @author Leo Fajardo (@leorw)
16039
+ */
16040
+ $this->switch_to_blog( $blog_id );
16041
+ }
16042
+
16043
+ $api = $this->get_api_site_scope();
16044
+
16045
+ if ( is_numeric( $site_license_id ) ) {
16046
+ // Try to retrieve a foreign license that is linked to the install.
16047
+ $api_result = $api->call( '/licenses.json' );
16048
+
16049
+ if ( $this->is_api_result_object( $api_result, 'licenses' ) &&
16050
+ is_array( $api_result->licenses )
16051
+ ) {
16052
+ $licenses = $api_result->licenses;
16053
+
16054
+ if ( ! empty( $licenses ) ) {
16055
+ $result[] = new FS_Plugin_License( $licenses[0] );
16056
+ }
16057
+ } else {
16058
+ $api_errors[] = $api_result;
16059
+ }
16060
+ } else if ( is_object( $this->_license ) ) {
16061
+ $is_license_in_result = false;
16062
+ if ( ! empty( $result ) ) {
16063
+ foreach ( $result as $license ) {
16064
+ if ( $license->id == $this->_license->id ) {
16065
+ $is_license_in_result = true;
16066
+ break;
16067
+ }
16068
+ }
16069
+ }
16070
+
16071
+ if ( ! $is_license_in_result ) {
16072
+ // Fetch foreign license by ID and license key.
16073
+ $license = $api->get( "/licenses/{$this->_license->id}.json?license_key=" .
16074
+ urlencode( $this->_license->secret_key ) );
16075
+
16076
+ if ( $this->is_api_result_entity( $license ) ) {
16077
+ $result[] = new FS_Plugin_License( $license );
16078
+ } else {
16079
+ $api_errors[] = $license;
16080
+ }
16081
+ }
16082
+ }
16083
+
16084
+ if ( ! is_null( $blog_id ) ) {
16085
+ $this->switch_to_blog( $this->_storage->network_install_blog_id );
16086
+ }
16087
+ }
16088
+
16089
+ if ( is_array( $result ) && 0 < count( $result ) ) {
16090
+ // If found at least one license, return license collection even if there are errors.
16091
+ return $result;
16092
+ }
16093
+
16094
+ if ( ! empty( $api_errors ) ) {
16095
+ // If found any errors and no licenses, return first error.
16096
+ return $api_errors[0];
16097
+ }
16098
+
16099
+ // Fallback to empty licenses list.
16100
+ return $result;
16101
+ }
16102
+
16103
+ /**
16104
+ * @author Vova Feldman (@svovaf)
16105
+ * @since 2.0.0
16106
+ *
16107
+ * @param number $license_id
16108
+ * @param string $license_key
16109
+ *
16110
+ * @return \FS_Plugin_License|object
16111
+ */
16112
+ private function fetch_license_by_key( $license_id, $license_key ) {
16113
+ $this->_logger->entrance();
16114
+
16115
+ $api = $this->get_current_or_network_user_api_scope();
16116
+
16117
+ $result = $api->get( "/licenses/{$license_id}.json?license_key=" . urlencode( $license_key ) );
16118
+
16119
+ return $this->is_api_result_entity( $result ) ?
16120
+ new FS_Plugin_License( $result ) :
16121
+ $result;
16122
+ }
16123
+
16124
+ /**
16125
+ * @author Vova Feldman (@svovaf)
16126
+ * @since 1.2.0
16127
+ * @uses FS_Api
16128
+ *
16129
+ * @param number|bool $plugin_id
16130
+ * @param bool $flush
16131
+ *
16132
+ * @return FS_Payment[]|object
16133
+ */
16134
+ function _fetch_payments( $plugin_id = false, $flush = false ) {
16135
+ $this->_logger->entrance();
16136
+
16137
+ $api = $this->get_api_user_scope();
16138
+
16139
+ if ( ! is_numeric( $plugin_id ) ) {
16140
+ $plugin_id = $this->_plugin->id;
16141
+ }
16142
+
16143
+ $result = $api->get( "/plugins/{$plugin_id}/payments.json?include_addons=true", $flush );
16144
+
16145
+ if ( ! isset( $result->error ) ) {
16146
+ for ( $i = 0, $len = count( $result->payments ); $i < $len; $i ++ ) {
16147
+ $result->payments[ $i ] = new FS_Payment( $result->payments[ $i ] );
16148
+ }
16149
+ $result = $result->payments;
16150
+ }
16151
+
16152
+ return $result;
16153
+ }
16154
+
16155
+ /**
16156
+ * @author Vova Feldman (@svovaf)
16157
+ * @since 1.2.1.5
16158
+ * @uses FS_Api
16159
+ *
16160
+ * @param bool $flush
16161
+ *
16162
+ * @return \FS_Billing|mixed
16163
+ */
16164
+ function _fetch_billing( $flush = false ) {
16165
+ require_once WP_FS__DIR_INCLUDES . '/entities/class-fs-billing.php';
16166
+
16167
+ $billing = $this->get_api_user_scope()->get( 'billing.json', $flush );
16168
+
16169
+ if ( $this->is_api_result_entity( $billing ) ) {
16170
+ $billing = new FS_Billing( $billing );
16171
+ }
16172
+
16173
+ return $billing;
16174
+ }
16175
+
16176
+ /**
16177
+ * @author Vova Feldman (@svovaf)
16178
+ * @since 1.0.5
16179
+ *
16180
+ * @param FS_Plugin_License[] $licenses
16181
+ * @param number $module_id
16182
+ */
16183
+ private function _update_licenses( $licenses, $module_id ) {
16184
+ $this->_logger->entrance();
16185
+
16186
+ if ( is_array( $licenses ) ) {
16187
+ for ( $i = 0, $len = count( $licenses ); $i < $len; $i ++ ) {
16188
+ $licenses[ $i ]->updated = time();
16189
+ }
16190
+ }
16191
+
16192
+ $this->_store_licenses( true, $module_id, $licenses );
16193
+ }
16194
+
16195
+ /**
16196
+ * @author Vova Feldman (@svovaf)
16197
+ * @since 1.0.4
16198
+ *
16199
+ * @param bool|number $plugin_id
16200
+ * @param bool $flush Since 1.1.7.3
16201
+ * @param int $expiration Since 1.2.2.7
16202
+ *
16203
+ * @return object|false New plugin tag info if exist.
16204
+ */
16205
+ private function _fetch_newer_version( $plugin_id = false, $flush = true, $expiration = WP_FS__TIME_24_HOURS_IN_SEC ) {
16206
+ $latest_tag = $this->_fetch_latest_version( $plugin_id, $flush, $expiration );
16207
+
16208
+ if ( ! is_object( $latest_tag ) ) {
16209
+ return false;
16210
+ }
16211
+
16212
+ // Check if version is actually newer.
16213
+ $has_new_version =
16214
+ // If it's an non-installed add-on then always return latest.
16215
+ ( $this->_is_addon_id( $plugin_id ) && ! $this->is_addon_activated( $plugin_id ) ) ||
16216
+ // Compare versions.
16217
+ version_compare( $this->get_plugin_version(), $latest_tag->version, '<' );
16218
+
16219
+ $this->_logger->departure( $has_new_version ? 'Found newer plugin version ' . $latest_tag->version : 'No new version' );
16220
+
16221
+ return $has_new_version ? $latest_tag : false;
16222
+ }
16223
+
16224
+ /**
16225
+ * @author Vova Feldman (@svovaf)
16226
+ * @since 1.0.5
16227
+ *
16228
+ * @param bool|number $plugin_id
16229
+ * @param bool $flush Since 1.1.7.3
16230
+ * @param int $expiration Since 1.2.2.7
16231
+ *
16232
+ * @return bool|FS_Plugin_Tag
16233
+ */
16234
+ function get_update( $plugin_id = false, $flush = true, $expiration = WP_FS__TIME_24_HOURS_IN_SEC ) {
16235
+ $this->_logger->entrance();
16236
+
16237
+ if ( ! is_numeric( $plugin_id ) ) {
16238
+ $plugin_id = $this->_plugin->id;
16239
+ }
16240
+
16241
+ $this->check_updates( true, $plugin_id, $flush, $expiration );
16242
+ $updates = $this->get_all_updates();
16243
+
16244
+ return isset( $updates[ $plugin_id ] ) && is_object( $updates[ $plugin_id ] ) ? $updates[ $plugin_id ] : false;
16245
+ }
16246
+
16247
+ /**
16248
+ * Check if site assigned with active license.
16249
+ *
16250
+ * @author Vova Feldman (@svovaf)
16251
+ * @since 1.0.6
16252
+ *
16253
+ * @deprecated Please use has_active_valid_license() instead because license can be cancelled.
16254
+ */
16255
+ function has_active_license() {
16256
+ return (
16257
+ is_object( $this->_license ) &&
16258
+ is_numeric( $this->_license->id ) &&
16259
+ ! $this->_license->is_expired()
16260
+ );
16261
+ }
16262
+
16263
+ /**
16264
+ * Check if site assigned with active & valid (not expired) license.
16265
+ *
16266
+ * @author Vova Feldman (@svovaf)
16267
+ * @since 1.2.1
16268
+ */
16269
+ function has_active_valid_license() {
16270
+ return (
16271
+ is_object( $this->_license ) &&
16272
+ is_numeric( $this->_license->id ) &&
16273
+ $this->_license->is_active() &&
16274
+ $this->_license->is_valid()
16275
+ );
16276
+ }
16277
+
16278
+ /**
16279
+ * Check if site assigned with license with enabled features.
16280
+ *
16281
+ * @author Vova Feldman (@svovaf)
16282
+ * @since 1.0.6
16283
+ *
16284
+ * @return bool
16285
+ */
16286
+ function has_features_enabled_license() {
16287
+ return (
16288
+ is_object( $this->_license ) &&
16289
+ is_numeric( $this->_license->id ) &&
16290
+ $this->_license->is_features_enabled()
16291
+ );
16292
+ }
16293
+
16294
+ /**
16295
+ * Check if user is a trial or have feature enabled license.
16296
+ *
16297
+ * @author Vova Feldman (@svovaf)
16298
+ * @since 1.1.7
16299
+ *
16300
+ * @return bool
16301
+ */
16302
+ function can_use_premium_code() {
16303
+ return $this->is_trial() || $this->has_features_enabled_license();
16304
+ }
16305
+
16306
+ /**
16307
+ * Checks if the current user can activate plugins or switch themes. Note that this method should only be used
16308
+ * after the `init` action is triggered because it is using `current_user_can()` which is only functional after
16309
+ * the context user is authenticated.
16310
+ *
16311
+ * @author Leo Fajardo (@leorw)
16312
+ * @since 1.2.2
16313
+ *
16314
+ * @return bool
16315
+ */
16316
+ function is_user_admin() {
16317
+ /**
16318
+ * Require a super-admin when network activated, running from the network level OR if
16319
+ * running from the site level but not delegated the opt-in.
16320
+ *
16321
+ * @author Vova Feldman (@svovaf)
16322
+ * @since 2.0.0
16323
+ */
16324
+ if ( $this->_is_network_active &&
16325
+ ( fs_is_network_admin() || ! $this->is_delegated_connection() )
16326
+ ) {
16327
+ return is_super_admin();
16328
+ }
16329
+
16330
+ return ( $this->is_plugin() && current_user_can( is_multisite() ? 'manage_options' : 'activate_plugins' ) )
16331
+ || ( $this->is_theme() && current_user_can( 'switch_themes' ) );
16332
+ }
16333
+
16334
+ /**
16335
+ * Sync site's plan.
16336
+ *
16337
+ * @author Vova Feldman (@svovaf)
16338
+ * @since 1.0.3
16339
+ *
16340
+ * @uses FS_Api
16341
+ *
16342
+ * @param bool $background Hints the method if it's a background sync. If false, it means that was initiated by
16343
+ * the admin.
16344
+ * @param bool $is_context_single_site @since 2.0.0. This is used when syncing a license for a single install from the
16345
+ * network-level "Account" page.
16346
+ */
16347
+ private function _sync_license( $background = false, $is_context_single_site = false ) {
16348
+ $this->_logger->entrance();
16349
+
16350
+ $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
16351
+
16352
+ $is_addon_sync = ( ! $this->_plugin->is_addon() && $plugin_id != $this->get_id() );
16353
+
16354
+ if ( $is_addon_sync ) {
16355
+ $this->_sync_addon_license( $plugin_id, $background );
16356
+ } else {
16357
+ $this->_sync_plugin_license( $background, true, $is_context_single_site );
16358
+ }
16359
+
16360
+ $this->do_action( 'after_account_plan_sync', $this->get_plan_name() );
16361
+ }
16362
+
16363
+ /**
16364
+ * Sync plugin's add-on license.
16365
+ *
16366
+ * @author Vova Feldman (@svovaf)
16367
+ * @since 1.0.6
16368
+ * @uses FS_Api
16369
+ *
16370
+ * @param number $addon_id
16371
+ * @param bool $background
16372
+ */
16373
+ private function _sync_addon_license( $addon_id, $background ) {
16374
+ $this->_logger->entrance();
16375
+
16376
+ if ( $this->is_addon_activated( $addon_id ) ) {
16377
+ // If already installed, use add-on sync.
16378
+ $fs_addon = self::get_instance_by_id( $addon_id );
16379
+ $fs_addon->_sync_license( $background );
16380
+
16381
+ return;
16382
+ }
16383
+
16384
+ // Validate add-on exists.
16385
+ $addon = $this->get_addon( $addon_id );
16386
+
16387
+ if ( ! is_object( $addon ) ) {
16388
+ return;
16389
+ }
16390
+
16391
+ // Add add-on into account add-ons.
16392
+ $account_addons = $this->get_account_addons();
16393
+ if ( ! is_array( $account_addons ) ) {
16394
+ $account_addons = array();
16395
+ }
16396
+ $account_addons[] = $addon->id;
16397
+ $account_addons = array_unique( $account_addons );
16398
+ $this->_store_account_addons( $account_addons );
16399
+
16400
+ // Load add-on licenses.
16401
+ $licenses = $this->_fetch_licenses( $addon->id );
16402
+
16403
+ // Sync add-on licenses.
16404
+ if ( $this->is_array_instanceof( $licenses, 'FS_Plugin_License' ) ) {
16405
+ $this->_update_licenses( $licenses, $addon->id );
16406
+
16407
+ if ( ! $this->is_addon_installed( $addon->id ) && FS_License_Manager::has_premium_license( $licenses ) ) {
16408
+ $plans_result = $this->get_api_site_or_plugin_scope()->get( "/addons/{$addon_id}/plans.json" );
16409
+
16410
+ if ( ! isset( $plans_result->error ) ) {
16411
+ $plans = array();
16412
+ foreach ( $plans_result->plans as $plan ) {
16413
+ $plans[] = new FS_Plugin_Plan( $plan );
16414
+ }
16415
+
16416
+ $this->_admin_notices->add_sticky(
16417
+ sprintf(
16418
+ ( FS_Plan_Manager::instance()->has_free_plan( $plans ) ?
16419
+ $this->get_text_inline( 'Your %s Add-on plan was successfully upgraded.', 'addon-successfully-upgraded-message' ) :
16420
+ /* translators: %s:product name, e.g. Facebook add-on was successfully... */
16421
+ $this->get_text_inline( '%s Add-on was successfully purchased.', 'addon-successfully-purchased-message' ) ),
16422
+ $addon->title
16423
+ ) . ' ' . $this->get_latest_download_link(
16424
+ $this->get_text_inline( 'Download the latest version', 'download-latest-version' ),
16425
+ $addon_id
16426
+ ),
16427
+ 'addon_plan_upgraded_' . $addon->slug,
16428
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
16429
+ );
16430
+ }
16431
+ }
16432
+ }
16433
+ }
16434
+
16435
+ /**
16436
+ * Sync site's plugin plan.
16437
+ *
16438
+ * @author Vova Feldman (@svovaf)
16439
+ * @since 1.0.6
16440
+ * @uses FS_Api
16441
+ *
16442
+ * @param bool $background Hints the method if it's a background sync. If false, it means that was initiated by the admin.
16443
+ * @param bool $send_installs_update Since 2.0.0
16444
+ * @param bool $is_context_single_site Since 2.0.0. This is used when sending an update for a single install and
16445
+ * syncing its license from the network-level "Account" page (e.g.: after
16446
+ * activating a license only for the single install).
16447
+ */
16448
+ private function _sync_plugin_license(
16449
+ $background = false,
16450
+ $send_installs_update = true,
16451
+ $is_context_single_site = false
16452
+ ) {
16453
+ $this->_logger->entrance();
16454
+
16455
+ $plan_change = 'none';
16456
+
16457
+ $is_site_level_sync = ( $is_context_single_site || fs_is_blog_admin() || ! $this->_is_network_active );
16458
+
16459
+ if ( ! $send_installs_update ) {
16460
+ $site = $this->_site;
16461
+ } else {
16462
+ /**
16463
+ * Sync site info.
16464
+ *
16465
+ * @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.
16466
+ */
16467
+ if ( $is_site_level_sync ) {
16468
+ $result = $this->send_install_update( array(), true );
16469
+ $is_valid = $this->is_api_result_entity( $result );
16470
+ } else {
16471
+ $result = $this->send_installs_update( array(), true );
16472
+ $is_valid = $this->is_api_result_object( $result, 'installs' );
16473
+ }
16474
+
16475
+ if ( ! $is_valid ) {
16476
+ if ( $is_context_single_site ) {
16477
+ // Switch back to the main blog so that the following logic will have the right entities.
16478
+ $this->switch_to_blog( $this->_storage->network_install_blog_id );
16479
+ }
16480
+
16481
+ // Show API messages only if not background sync or if paying customer.
16482
+ if ( ! $background || $this->is_paying() ) {
16483
+ // Try to ping API to see if not blocked.
16484
+ if ( ! FS_Api::test() ) {
16485
+ /**
16486
+ * Failed to ping API - blocked!
16487
+ *
16488
+ * @author Vova Feldman (@svovaf)
16489
+ * @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.
16490
+ */
16491
+ $api = $this->get_api_site_scope();
16492
+
16493
+ if ( ! self::$_global_admin_notices->has_sticky( 'api_blocked' ) ) {
16494
+ self::$_global_admin_notices->add(
16495
+ sprintf(
16496
+ $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' ),
16497
+ $this->get_plugin_name(),
16498
+ '<a href="' . $api->get_url() . '" target="_blank">' . $api->get_url() . '</a>'
16499
+ ) . '<br> ' . $this->get_text_inline( 'Error received from the server:', 'server-error-message' ) . var_export( $result->error, true ),
16500
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
16501
+ 'error',
16502
+ $background,
16503
+ 'api_blocked'
16504
+ );
16505
+ }
16506
+ } else {
16507
+ // Authentication params are broken.
16508
+ $this->_admin_notices->add(
16509
+ $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' ),
16510
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
16511
+ 'error'
16512
+ );
16513
+ }
16514
+ }
16515
+
16516
+ // No reason to continue with license sync while there are API issues.
16517
+ return;
16518
+ }
16519
+
16520
+ if ( $is_site_level_sync ) {
16521
+ $site = new FS_Site( $result );
16522
+ } else {
16523
+ // Map site addresses to their blog IDs.
16524
+ $address_to_blog_map = $this->get_address_to_blog_map();
16525
+
16526
+ // Find the current context install.
16527
+ $site = null;
16528
+ foreach ( $result->installs as $install ) {
16529
+ if ( $install->id == $this->_site->id ) {
16530
+ $site = new FS_Site( $install );
16531
+ } else {
16532
+ $address = trailingslashit( fs_strip_url_protocol( $install->url ) );
16533
+ $blog_id = $address_to_blog_map[ $address ];
16534
+
16535
+ $this->_store_site( true, $blog_id, new FS_Site( $install ) );
16536
+ }
16537
+ }
16538
+ }
16539
+
16540
+ // Sync plans.
16541
+ $this->_sync_plans();
16542
+ }
16543
+
16544
+ // Remove sticky API connectivity message.
16545
+ self::$_global_admin_notices->remove_sticky( 'api_blocked' );
16546
+
16547
+ if ( ! $this->has_paid_plan() ) {
16548
+ $this->_site = $site;
16549
+ $this->_store_site(
16550
+ true,
16551
+ $is_site_level_sync ?
16552
+ null :
16553
+ $this->get_network_install_blog_id()
16554
+ );
16555
+ } else {
16556
+ $context_blog_id = 0;
16557
+
16558
+ if ( $is_context_single_site ) {
16559
+ $context_blog_id = get_current_blog_id();
16560
+
16561
+ // Switch back to the main blog in order to properly sync the license.
16562
+ $this->switch_to_blog( $this->_storage->network_install_blog_id );
16563
+ }
16564
+
16565
+ /**
16566
+ * Sync licenses. Pass the site's license ID so that the foreign licenses will be fetched if the license
16567
+ * associated with that ID is not included in the user's licenses collection.
16568
+ */
16569
+ $this->_sync_licenses(
16570
+ $site->license_id,
16571
+ ( $is_context_single_site ?
16572
+ $context_blog_id :
16573
+ null
16574
+ )
16575
+ );
16576
+
16577
+ if ( $is_context_single_site ) {
16578
+ $this->switch_to_blog( $context_blog_id );
16579
+ }
16580
+
16581
+ // Check if plan / license changed.
16582
+ if ( $site->plan_id != $this->_site->plan_id ||
16583
+ // Check if trial started.
16584
+ $site->trial_plan_id != $this->_site->trial_plan_id ||
16585
+ $site->trial_ends != $this->_site->trial_ends ||
16586
+ // Check if license changed.
16587
+ $site->license_id != $this->_site->license_id
16588
+ ) {
16589
+ if ( $site->is_trial() && ( ! $this->_site->is_trial() || $site->trial_ends != $this->_site->trial_ends ) ) {
16590
+ // New trial started.
16591
+ $this->_site = $site;
16592
+ $plan_change = 'trial_started';
16593
+
16594
+ // For trial with subscription use-case.
16595
+ $new_license = is_null( $site->license_id ) ? null : $this->_get_license_by_id( $site->license_id );
16596
+
16597
+ if ( is_object( $new_license ) && $new_license->is_valid() ) {
16598
+ $this->_site = $site;
16599
+ $this->_update_site_license( $new_license );
16600
+ $this->_store_licenses();
16601
+
16602
+ $this->_sync_site_subscription( $this->_license );
16603
+ }
16604
+ } else if ( $this->_site->is_trial() && ! $site->is_trial() && ! is_numeric( $site->license_id ) ) {
16605
+ // Was in trial, but now trial expired and no license ID.
16606
+ // New trial started.
16607
+ $this->_site = $site;
16608
+ $plan_change = 'trial_expired';
16609
+ } else {
16610
+ $is_free = $this->is_free_plan();
16611
+
16612
+ // Make sure license exist and not expired.
16613
+ $new_license = is_null( $site->license_id ) ?
16614
+ null :
16615
+ $this->_get_license_by_id( $site->license_id );
16616
+
16617
+ if ( $is_free && is_null( $new_license ) && $this->has_any_license() && $this->_license->is_cancelled ) {
16618
+ // License cancelled.
16619
+ $this->_site = $site;
16620
+ $this->_update_site_license( $new_license );
16621
+ $this->_store_licenses();
16622
+
16623
+ $plan_change = 'cancelled';
16624
+ } else if ( $is_free && ( ( ! is_object( $new_license ) || $new_license->is_expired() ) ) ) {
16625
+ // The license is expired, so ignore upgrade method.
16626
+ $this->_site = $site;
16627
+ } else {
16628
+ // License changed.
16629
+ $this->_site = $site;
16630
+
16631
+ /**
16632
+ * IMPORTANT:
16633
+ * The line below should be executed before trying to activate the license on the rest of the network, otherwise, the license' activation counters may be out of sync + there's no need to activate the license on the context site since it's already activated on it.
16634
+ *
16635
+ * @author Vova Feldman (@svovaf)
16636
+ * @since 2.0.0
16637
+ */
16638
+ $this->_update_site_license( $new_license );
16639
+
16640
+ if ( ! $is_context_single_site &&
16641
+ fs_is_network_admin() &&
16642
+ $this->_is_network_active &&
16643
+ $new_license->quota > 1 &&
16644
+ get_blog_count() > 1
16645
+ ) {
16646
+ // See if license can activated on all sites.
16647
+ if ( ! $this->try_activate_license_on_network( $this->_user, $new_license ) ) {
16648
+ if ( ! fs_request_get_bool( 'auto_install' ) ) {
16649
+ // Open the license activation dialog box on the account page.
16650
+ add_action( 'admin_footer', array(
16651
+ &$this,
16652
+ '_open_license_activation_dialog_box'
16653
+ ) );
16654
+ }
16655
+ }
16656
+ }
16657
+
16658
+ $this->_store_licenses();
16659
+
16660
+ $plan_change = $is_free ?
16661
+ 'upgraded' :
16662
+ ( is_object( $new_license ) ?
16663
+ 'changed' :
16664
+ 'downgraded' );
16665
+ }
16666
+ }
16667
+
16668
+ // Store updated site info.
16669
+ $this->_store_site(
16670
+ true,
16671
+ $is_site_level_sync ?
16672
+ null :
16673
+ $this->get_network_install_blog_id()
16674
+ );
16675
+ } else {
16676
+ if ( is_object( $this->_license ) && $this->_license->is_expired() ) {
16677
+ if ( ! $this->has_features_enabled_license() ) {
16678
+ $this->_deactivate_license();
16679
+ $plan_change = 'downgraded';
16680
+ } else {
16681
+ $plan_change = 'expired';
16682
+ }
16683
+ }
16684
+
16685
+ if ( is_numeric( $site->license_id ) && is_object( $this->_license ) ) {
16686
+ $this->_sync_site_subscription( $this->_license );
16687
+ }
16688
+ }
16689
+ }
16690
+
16691
+ $hmm_text = $this->get_text_x_inline( 'Hmm', 'something somebody says when they are thinking about what you have just said.', 'hmm' ) . '...';
16692
+
16693
+ if ( $this->has_paid_plan() ) {
16694
+ switch ( $plan_change ) {
16695
+ case 'none':
16696
+ if ( ! $background && is_admin() ) {
16697
+ $plan = $this->is_trial() ?
16698
+ $this->get_trial_plan() :
16699
+ $this->get_plan();
16700
+
16701
+ if ( $plan->is_free() ) {
16702
+ $this->_admin_notices->add(
16703
+ sprintf(
16704
+ $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' ),
16705
+ '<i><b>' . $plan->title . ( $this->is_trial() ? ' ' . $this->get_text_x_inline( 'Trial', 'trial period', 'trial' ) : '' ) . '</b></i>'
16706
+ ) . ' ' . sprintf(
16707
+ '<a href="%s">%s</a>',
16708
+ $this->contact_url(
16709
+ 'bug',
16710
+ 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' ),
16711
+ strtoupper( $plan->name )
16712
+ )
16713
+ ),
16714
+ $this->get_text_inline( 'Please contact us here', 'contact-us-here' )
16715
+ ),
16716
+ $hmm_text
16717
+ );
16718
+ }
16719
+ }
16720
+ break;
16721
+ case 'upgraded':
16722
+ $this->_admin_notices->add_sticky(
16723
+ sprintf(
16724
+ $this->get_text_inline( 'Your plan was successfully upgraded.', 'plan-upgraded-message' ),
16725
+ '<i>' . $this->get_plugin_name() . '</i>'
16726
+ ) . $this->get_complete_upgrade_instructions(),
16727
+ 'plan_upgraded',
16728
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
16729
+ );
16730
+
16731
+ $this->_admin_notices->remove_sticky( array(
16732
+ 'trial_started',
16733
+ 'trial_promotion',
16734
+ 'trial_expired',
16735
+ 'activation_complete',
16736
+ ) );
16737
+ break;
16738
+ case 'changed':
16739
+ $this->_admin_notices->add_sticky(
16740
+ sprintf(
16741
+ $this->get_text_inline( 'Your plan was successfully changed to %s.', 'plan-changed-to-x-message' ),
16742
+ $this->get_plan_title()
16743
+ ),
16744
+ 'plan_changed'
16745
+ );
16746
+
16747
+ $this->_admin_notices->remove_sticky( array(
16748
+ 'trial_started',
16749
+ 'trial_promotion',
16750
+ 'trial_expired',
16751
+ 'activation_complete',
16752
+ ) );
16753
+ break;
16754
+ case 'downgraded':
16755
+ $this->_admin_notices->add_sticky(
16756
+ ($this->has_free_plan() ?
16757
+ 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 ) :
16758
+ /* translators: %1$s: product title; %2$s, %3$s: wrapping HTML anchor element; %4$s: 'plugin', 'theme', or 'add-on'. */
16759
+ sprintf( $this->get_text_inline( 'Your license has expired. %1$sUpgrade now%2$s to continue using the %3$s without interruptions.', 'license-expired-blocking-message_premium-only' ), sprintf('<a href="%s">', $this->pricing_url()), '</a>', $this->get_module_label(true) ) ),
16760
+ 'license_expired',
16761
+ $hmm_text
16762
+ );
16763
+ $this->_admin_notices->remove_sticky( 'plan_upgraded' );
16764
+ break;
16765
+ case 'cancelled':
16766
+ $this->_admin_notices->add(
16767
+ $this->get_text_inline( 'Your license has been cancelled. If you think it\'s a mistake, please contact support.', 'license-cancelled' ) . ' ' .
16768
+ sprintf(
16769
+ '<a href="%s">%s</a>',
16770
+ $this->contact_url( 'bug' ),
16771
+ $this->get_text_inline( 'Please contact us here', 'contact-us-here' )
16772
+ ),
16773
+ $hmm_text,
16774
+ 'error'
16775
+ );
16776
+ $this->_admin_notices->remove_sticky( 'plan_upgraded' );
16777
+ break;
16778
+ case 'expired':
16779
+ $this->_admin_notices->add_sticky(
16780
+ sprintf( $this->get_text_inline( 'Your license has expired. You can still continue using all the %s features, but you\'ll need to renew your license to continue getting updates and support.', 'license-expired-non-blocking-message' ), $this->get_plan()->title ),
16781
+ 'license_expired',
16782
+ $hmm_text
16783
+ );
16784
+ $this->_admin_notices->remove_sticky( 'plan_upgraded' );
16785
+ break;
16786
+ case 'trial_started':
16787
+ $this->_admin_notices->add_sticky(
16788
+ sprintf(
16789
+ $this->get_text_inline( 'Your trial has been successfully started.', 'trial-started-message' ),
16790
+ '<i>' . $this->get_plugin_name() . '</i>'
16791
+ ) . $this->get_complete_upgrade_instructions( $this->get_trial_plan()->title ),
16792
+ 'trial_started',
16793
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
16794
+ );
16795
+
16796
+ $this->_admin_notices->remove_sticky( array(
16797
+ 'trial_promotion',
16798
+ ) );
16799
+ break;
16800
+ case 'trial_expired':
16801
+ $this->_admin_notices->add_sticky(
16802
+ ($this->has_free_plan() ?
16803
+ $this->get_text_inline( 'Your free trial has expired. You can still continue using all our free features.', 'trial-expired-message' ) :
16804
+ /* translators: %1$s: product title; %2$s, %3$s: wrapping HTML anchor element; %4$s: 'plugin', 'theme', or 'add-on'. */
16805
+ sprintf( $this->get_text_inline( 'Your free trial has expired. %1$sUpgrade now%2$s to continue using the %3$s without interruptions.', 'trial-expired-message_premium-only' ), sprintf('<a href="%s">', $this->pricing_url()), '</a>', $this->get_module_label(true))),
16806
+ 'trial_expired',
16807
+ $hmm_text
16808
+ );
16809
+ $this->_admin_notices->remove_sticky( array(
16810
+ 'trial_started',
16811
+ 'trial_promotion',
16812
+ 'plan_upgraded',
16813
+ ) );
16814
+ break;
16815
+ }
16816
+ }
16817
+
16818
+ if ( 'none' !== $plan_change ) {
16819
+ $this->do_action( 'after_license_change', $plan_change, $this->get_plan() );
16820
+ }
16821
+ }
16822
+
16823
+ /**
16824
+ * Include the required JS at the footer of the admin to trigger the license activation dialog box.
16825
+ *
16826
+ * @author Vova Feldman (@svovaf)
16827
+ * @since 2.0.0
16828
+ */
16829
+ public function _open_license_activation_dialog_box() {
16830
+ $vars = array( 'license_id' => $this->_site->license_id );
16831
+ fs_require_once_template( 'js/open-license-activation.php', $vars );
16832
+ }
16833
+
16834
+ /**
16835
+ * @author Vova Feldman (@svovaf)
16836
+ * @since 1.0.5
16837
+ *
16838
+ * @param bool $background
16839
+ */
16840
+ protected function _activate_license( $background = false ) {
16841
+ $this->_logger->entrance();
16842
+
16843
+ $license_id = fs_request_get( 'license_id' );
16844
+
16845
+ if ( is_object( $this->_site ) &&
16846
+ FS_Plugin_License::is_valid_id( $license_id ) &&
16847
+ $license_id == $this->_site->license_id
16848
+ ) {
16849
+ // License is already activated.
16850
+ return;
16851
+ }
16852
+
16853
+ $premium_license = FS_Plugin_License::is_valid_id( $license_id ) ?
16854
+ $this->_get_license_by_id( $license_id ) :
16855
+ $this->_get_available_premium_license();
16856
+
16857
+ if ( ! is_object( $premium_license ) ) {
16858
+ return;
16859
+ }
16860
+
16861
+ if ( ! is_object( $this->_site ) ) {
16862
+ // Not yet opted-in.
16863
+ $user = $this->get_current_or_network_user();
16864
+ if ( ! is_object( $user ) ) {
16865
+ $user = self::_get_user_by_id( $premium_license->user_id );
16866
+ }
16867
+
16868
+ if ( is_object( $user ) ) {
16869
+ $this->install_with_user( $user, $premium_license->secret_key, false, false, false );
16870
+ } else {
16871
+ $this->opt_in(
16872
+ false,
16873
+ false,
16874
+ false,
16875
+ $premium_license->secret_key
16876
+ );
16877
+
16878
+ return;
16879
+ }
16880
+ }
16881
+
16882
+
16883
+ /**
16884
+ * If the premium license is already associated with the install, just
16885
+ * update the license reference (activation is not required).
16886
+ *
16887
+ * @since 1.1.9
16888
+ */
16889
+ if ( $premium_license->id == $this->_site->license_id ) {
16890
+ // License is already activated.
16891
+ $this->_update_site_license( $premium_license );
16892
+ $this->_store_account();
16893
+
16894
+ return;
16895
+ }
16896
+
16897
+ if ( $this->_site->user_id != $premium_license->user_id ) {
16898
+ $api_request_params = array( 'license_key' => $premium_license->secret_key );
16899
+ } else {
16900
+ $api_request_params = array();
16901
+ }
16902
+
16903
+ $api = $this->get_api_site_scope();
16904
+ $license = $api->call( "/licenses/{$premium_license->id}.json", 'put', $api_request_params );
16905
+
16906
+ if ( ! $this->is_api_result_entity( $license ) ) {
16907
+ if ( ! $background ) {
16908
+ $this->_admin_notices->add( sprintf(
16909
+ '%s %s',
16910
+ $this->get_text_inline( 'It looks like the license could not be activated.', 'license-activation-failed-message' ),
16911
+ ( is_object( $license ) && isset( $license->error ) ?
16912
+ $license->error->message :
16913
+ sprintf( '%s<br><code>%s</code>',
16914
+ $this->get_text_inline( 'Error received from the server:', 'server-error-message' ),
16915
+ var_export( $license, true )
16916
+ )
16917
+ )
16918
+ ),
16919
+ $this->get_text_x_inline( 'Hmm', 'something somebody says when they are thinking about what you have just said.', 'hmm' ) . '...',
16920
+ 'error'
16921
+ );
16922
+ }
16923
+
16924
+ return;
16925
+ }
16926
+
16927
+ $premium_license = new FS_Plugin_License( $license );
16928
+
16929
+ // Updated site plan.
16930
+ $site = $this->get_api_site_scope()->get( '/', true );
16931
+ if ( $this->is_api_result_entity( $site ) ) {
16932
+ $this->_site = new FS_Site( $site );
16933
+ }
16934
+ $this->_update_site_license( $premium_license );
16935
+
16936
+ $this->_store_account();
16937
+
16938
+ if ( ! $background ) {
16939
+ $this->_admin_notices->add_sticky(
16940
+ $this->get_text_inline( 'Your license was successfully activated.', 'license-activated-message' ) .
16941
+ $this->get_complete_upgrade_instructions(),
16942
+ 'license_activated',
16943
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
16944
+ );
16945
+ }
16946
+
16947
+ $this->_admin_notices->remove_sticky( array(
16948
+ 'trial_promotion',
16949
+ 'license_expired',
16950
+ ) );
16951
+ }
16952
+
16953
+ /**
16954
+ * @author Vova Feldman (@svovaf)
16955
+ * @since 1.0.5
16956
+ *
16957
+ * @param bool $show_notice
16958
+ */
16959
+ protected function _deactivate_license( $show_notice = true ) {
16960
+ $this->_logger->entrance();
16961
+
16962
+ $hmm_text = $this->get_text_x_inline( 'Hmm', 'something somebody says when they are thinking about what you have just said.', 'hmm' ) . '...';
16963
+
16964
+ if ( ! FS_Plugin_License::is_valid_id( $this->_site->license_id ) ) {
16965
+ $this->_admin_notices->add(
16966
+ sprintf( $this->get_text_inline( 'It looks like your site currently doesn\'t have an active license.', 'no-active-license-message' ), $this->get_plan_title() ),
16967
+ $hmm_text
16968
+ );
16969
+
16970
+ return;
16971
+ }
16972
+
16973
+ $api = $this->get_api_site_scope();
16974
+ $license = $api->call( "/licenses/{$this->_site->license_id}.json", 'delete' );
16975
+
16976
+ if ( isset( $license->error ) ) {
16977
+ $this->_admin_notices->add(
16978
+ $this->get_text_inline( 'It looks like the license deactivation failed.', 'license-deactivation-failed-message' ) . '<br> ' .
16979
+ $this->get_text_inline( 'Error received from the server:', 'server-error-message' ) . ' ' . var_export( $license->error, true ),
16980
+ $hmm_text,
16981
+ 'error'
16982
+ );
16983
+
16984
+ return;
16985
+ }
16986
+
16987
+ // Update license cache.
16988
+ if ( is_array( $this->_licenses ) ) {
16989
+ for ( $i = 0, $len = count( $this->_licenses ); $i < $len; $i ++ ) {
16990
+ if ( $license->id == $this->_licenses[ $i ]->id ) {
16991
+ $this->_licenses[ $i ] = new FS_Plugin_License( $license );
16992
+ }
16993
+ }
16994
+ }
16995
+
16996
+ // Updated site plan to default.
16997
+ $this->_sync_plans();
16998
+ $this->_site->plan_id = $this->_plans[0]->id;
16999
+ // Unlink license from site.
17000
+ $this->_update_site_license( null );
17001
+
17002
+ $this->_store_account();
17003
+
17004
+ if ( $show_notice ) {
17005
+ $this->_admin_notices->add(
17006
+ sprintf( $this->get_text_inline( 'Your license was successfully deactivated, you are back to the %s plan.', 'license-deactivation-message' ), $this->get_plan_title() ),
17007
+ $this->get_text_inline( 'O.K', 'ok' )
17008
+ );
17009
+ }
17010
+
17011
+ $this->_admin_notices->remove_sticky( array(
17012
+ 'plan_upgraded',
17013
+ 'license_activated',
17014
+ ) );
17015
+ }
17016
+
17017
+ /**
17018
+ * Site plan downgrade.
17019
+ *
17020
+ * @author Vova Feldman (@svovaf)
17021
+ * @since 1.0.4
17022
+ *
17023
+ * @uses FS_Api
17024
+ */
17025
+ private function _downgrade_site() {
17026
+ $this->_logger->entrance();
17027
+
17028
+ $api = $this->get_api_site_scope();
17029
+ $site = $api->call( 'downgrade.json', 'put' );
17030
+
17031
+ $plan_downgraded = false;
17032
+ $plan = false;
17033
+ if ( $this->is_api_result_entity( $site ) ) {
17034
+ $prev_plan_id = $this->_site->plan_id;
17035
+
17036
+ // Update new site plan id.
17037
+ $this->_site->plan_id = $site->plan_id;
17038
+
17039
+ $plan = $this->get_plan();
17040
+ $subscription = $this->_sync_site_subscription( $this->_license );
17041
+
17042
+ // Plan downgraded if plan was changed or subscription was cancelled.
17043
+ $plan_downgraded = ( $plan instanceof FS_Plugin_Plan && $prev_plan_id != $plan->id ) ||
17044
+ ( is_object( $subscription ) && ! isset( $subscription->error ) && ! $subscription->is_active() );
17045
+ } else {
17046
+ // handle different error cases.
17047
+
17048
+ }
17049
+
17050
+ if ( $plan_downgraded ) {
17051
+ // Remove previous sticky message about upgrade (if exist).
17052
+ $this->_admin_notices->remove_sticky( 'plan_upgraded' );
17053
+
17054
+ $this->_admin_notices->add(
17055
+ sprintf( $this->get_text_inline( 'Your plan was successfully downgraded. Your %s plan license will expire in %s.', 'plan-x-downgraded-message' ),
17056
+ $plan->title,
17057
+ human_time_diff( time(), strtotime( $this->_license->expiration ) )
17058
+ )
17059
+ );
17060
+
17061
+ // Store site updates.
17062
+ $this->_store_site();
17063
+ } else {
17064
+ $this->_admin_notices->add(
17065
+ $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' ),
17066
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
17067
+ 'error'
17068
+ );
17069
+ }
17070
+ }
17071
+
17072
+ /**
17073
+ * @author Vova Feldman (@svovaf)
17074
+ * @since 1.1.8.1
17075
+ *
17076
+ * @param bool|string $plan_name
17077
+ *
17078
+ * @return bool If trial was successfully started.
17079
+ */
17080
+ function start_trial( $plan_name = false ) {
17081
+ $this->_logger->entrance();
17082
+
17083
+ // Alias.
17084
+ $oops_text = $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...';
17085
+
17086
+ if ( $this->is_trial() ) {
17087
+ // Already in trial mode.
17088
+ $this->_admin_notices->add(
17089
+ sprintf( $this->get_text_inline( 'You are already running the %s in a trial mode.', 'in-trial-mode' ), $this->_module_type ),
17090
+ $oops_text,
17091
+ 'error'
17092
+ );
17093
+
17094
+ return false;
17095
+ }
17096
+
17097
+ if ( $this->_site->is_trial_utilized() ) {
17098
+ // Trial was already utilized.
17099
+ $this->_admin_notices->add(
17100
+ $this->get_text_inline( 'You already utilized a trial before.', 'trial-utilized' ),
17101
+ $oops_text,
17102
+ 'error'
17103
+ );
17104
+
17105
+ return false;
17106
+ }
17107
+
17108
+ if ( false !== $plan_name ) {
17109
+ $plan = $this->get_plan_by_name( $plan_name );
17110
+
17111
+ if ( false === $plan ) {
17112
+ // Plan doesn't exist.
17113
+ $this->_admin_notices->add(
17114
+ sprintf( $this->get_text_inline( 'Plan %s do not exist, therefore, can\'t start a trial.', 'trial-plan-x-not-exist' ), $plan_name ),
17115
+ $oops_text,
17116
+ 'error'
17117
+ );
17118
+
17119
+ return false;
17120
+ }
17121
+
17122
+ if ( ! $plan->has_trial() ) {
17123
+ // Plan doesn't exist.
17124
+ $this->_admin_notices->add(
17125
+ sprintf( $this->get_text_inline( 'Plan %s does not support a trial period.', 'plan-x-no-trial' ), $plan_name ),
17126
+ $oops_text,
17127
+ 'error'
17128
+ );
17129
+
17130
+ return false;
17131
+ }
17132
+ } else {
17133
+ if ( ! $this->has_trial_plan() ) {
17134
+ // None of the plans have a trial.
17135
+ $this->_admin_notices->add(
17136
+ sprintf( $this->get_text_inline( 'None of the %s\'s plans supports a trial period.', 'no-trials' ), $this->_module_type ),
17137
+ $oops_text,
17138
+ 'error'
17139
+ );
17140
+
17141
+ return false;
17142
+ }
17143
+
17144
+ $plans_with_trial = FS_Plan_Manager::instance()->get_trial_plans( $this->_plans );
17145
+
17146
+ $plan = $plans_with_trial[0];
17147
+ }
17148
+
17149
+ $api = $this->get_api_site_scope();
17150
+ $plan = $api->call( "plans/{$plan->id}/trials.json", 'post' );
17151
+
17152
+ if ( ! $this->is_api_result_entity( $plan ) ) {
17153
+ // Some API error while trying to start the trial.
17154
+ $this->_admin_notices->add(
17155
+ sprintf( $this->get_text_inline( 'Unexpected API error. Please contact the %s\'s author with the following error.', 'unexpected-api-error' ), $this->_module_type )
17156
+ . ' ' . var_export( $plan, true ),
17157
+ $oops_text,
17158
+ 'error'
17159
+ );
17160
+
17161
+ return false;
17162
+ }
17163
+
17164
+ // Sync license.
17165
+ $this->_sync_license();
17166
+
17167
+ return $this->is_trial();
17168
+ }
17169
+
17170
+ /**
17171
+ * Cancel site trial.
17172
+ *
17173
+ * @author Vova Feldman (@svovaf)
17174
+ * @since 1.0.9
17175
+ *
17176
+ * @uses FS_Api
17177
+ */
17178
+ private function _cancel_trial() {
17179
+ $this->_logger->entrance();
17180
+
17181
+ // Alias.
17182
+ $oops_text = $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...';
17183
+
17184
+ if ( ! $this->is_trial() ) {
17185
+ $this->_admin_notices->add(
17186
+ $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' ),
17187
+ $oops_text,
17188
+ 'error'
17189
+ );
17190
+
17191
+ return;
17192
+ }
17193
+
17194
+ $trial_plan = $this->get_trial_plan();
17195
+
17196
+ $api = $this->get_api_site_scope();
17197
+ $site = $api->call( 'trials.json', 'delete' );
17198
+
17199
+ $trial_cancelled = false;
17200
+
17201
+ if ( $this->is_api_result_entity( $site ) ) {
17202
+ $prev_trial_ends = $this->_site->trial_ends;
17203
+
17204
+ if ( $this->is_paid_trial() ) {
17205
+ $this->_license->expiration = $site->trial_ends;
17206
+ $this->_license->is_cancelled = true;
17207
+ $this->_update_site_license( $this->_license );
17208
+ $this->_store_licenses();
17209
+
17210
+ // Clear subscription reference.
17211
+ $this->_sync_site_subscription( null );
17212
+ }
17213
+
17214
+ // Update site info.
17215
+ $this->_site = new FS_Site( $site );
17216
+
17217
+ $trial_cancelled = ( $prev_trial_ends != $site->trial_ends );
17218
+ } else {
17219
+ // @todo handle different error cases.
17220
+ }
17221
+
17222
+ if ( $trial_cancelled ) {
17223
+ // Remove previous sticky messages about upgrade or trial (if exist).
17224
+ $this->_admin_notices->remove_sticky( array(
17225
+ 'trial_started',
17226
+ 'trial_promotion',
17227
+ 'plan_upgraded',
17228
+ ) );
17229
+
17230
+ // Store site updates.
17231
+ $this->_store_site();
17232
+
17233
+ if ( ! $this->is_addon() ||
17234
+ ! $this->deactivate_premium_only_addon_without_license( true )
17235
+ ) {
17236
+ $this->_admin_notices->add(
17237
+ sprintf( $this->get_text_inline( 'Your %s free trial was successfully cancelled.', 'trial-cancel-message' ), $trial_plan->title )
17238
+ );
17239
+ }
17240
+ } else {
17241
+ $this->_admin_notices->add(
17242
+ $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' ),
17243
+ $oops_text,
17244
+ 'error'
17245
+ );
17246
+ }
17247
+ }
17248
+
17249
+ /**
17250
+ * @author Vova Feldman (@svovaf)
17251
+ * @since 1.0.6
17252
+ *
17253
+ * @param bool|number $plugin_id
17254
+ *
17255
+ * @return bool
17256
+ */
17257
+ private function _is_addon_id( $plugin_id ) {
17258
+ return is_numeric( $plugin_id ) && ( $this->get_id() != $plugin_id );
17259
+ }
17260
+
17261
+ /**
17262
+ * Check if user eligible to download premium version updates.
17263
+ *
17264
+ * @author Vova Feldman (@svovaf)
17265
+ * @since 1.0.6
17266
+ *
17267
+ * @return bool
17268
+ */
17269
+ private function _can_download_premium() {
17270
+ return $this->has_active_valid_license() ||
17271
+ ( $this->is_trial() && ! $this->get_trial_plan()->is_free() );
17272
+ }
17273
+
17274
+ /**
17275
+ *
17276
+ * @author Vova Feldman (@svovaf)
17277
+ * @since 1.0.6
17278
+ *
17279
+ * @param bool|number $addon_id
17280
+ * @param string $type "json" or "zip"
17281
+ *
17282
+ * @return string
17283
+ */
17284
+ private function _get_latest_version_endpoint( $addon_id = false, $type = 'json' ) {
17285
+
17286
+ $is_addon = $this->_is_addon_id( $addon_id );
17287
+
17288
+ $is_premium = null;
17289
+ if ( ! $is_addon ) {
17290
+ $is_premium = ( $this->is_premium() || $this->_can_download_premium() );
17291
+ } else if ( $this->is_addon_activated( $addon_id ) ) {
17292
+ $fs_addon = self::get_instance_by_id( $addon_id );
17293
+ $is_premium = ( $fs_addon->is_premium() || $fs_addon->_can_download_premium() );
17294
+ }
17295
+
17296
+ // If add-on, then append add-on ID.
17297
+ $endpoint = ( $is_addon ? "/addons/$addon_id" : '' ) .
17298
+ '/updates/latest.' . $type;
17299
+
17300
+ // If add-on and not yet activated, try to fetch based on server licensing.
17301
+ if ( is_bool( $is_premium ) ) {
17302
+ $endpoint = add_query_arg( 'is_premium', json_encode( $is_premium ), $endpoint );
17303
+ }
17304
+
17305
+ if ( $this->has_secret_key() ) {
17306
+ $endpoint = add_query_arg( 'type', 'all', $endpoint );
17307
+ }
17308
+
17309
+ return $endpoint;
17310
+ }
17311
+
17312
+ /**
17313
+ * @author Vova Feldman (@svovaf)
17314
+ * @since 1.0.4
17315
+ *
17316
+ * @param bool|number $addon_id
17317
+ * @param bool $flush Since 1.1.7.3
17318
+ * @param int $expiration Since 1.2.2.7
17319
+ *
17320
+ * @return object|false Plugin latest tag info.
17321
+ */
17322
+ function _fetch_latest_version(
17323
+ $addon_id = false,
17324
+ $flush = true,
17325
+ $expiration = WP_FS__TIME_24_HOURS_IN_SEC
17326
+ ) {
17327
+ $this->_logger->entrance();
17328
+
17329
+ $switch_to_blog_id = null;
17330
+
17331
+ /**
17332
+ * @since 1.1.7.3 Check for plugin updates from Freemius only if opted-in.
17333
+ * @since 1.1.7.4 Also check updates for add-ons.
17334
+ */
17335
+ if ( ! $this->is_registered() &&
17336
+ ! $this->_is_addon_id( $addon_id )
17337
+ ) {
17338
+ if ( ! is_multisite() ) {
17339
+ return false;
17340
+ }
17341
+
17342
+ $installs_map = $this->get_blog_install_map();
17343
+
17344
+ foreach ( $installs_map as $blog_id => $install ) {
17345
+ /**
17346
+ * @var FS_Site $install
17347
+ */
17348
+ if ( $install->is_trial() ) {
17349
+ $switch_to_blog_id = $blog_id;
17350
+ break;
17351
+ }
17352
+
17353
+ if ( FS_Plugin_License::is_valid_id( $install->license_id ) ) {
17354
+ $license = $this->get_license_by_id( $install->license_id );
17355
+
17356
+ if ( is_object( $license ) && $license->is_features_enabled() ) {
17357
+ $switch_to_blog_id = $blog_id;
17358
+ break;
17359
+ }
17360
+ }
17361
+ }
17362
+
17363
+ if ( is_null( $switch_to_blog_id ) ) {
17364
+ return false;
17365
+ }
17366
+ }
17367
+
17368
+ $current_blog_id = is_numeric( $switch_to_blog_id ) ?
17369
+ get_current_blog_id() :
17370
+ 0;
17371
+
17372
+ if ( is_numeric( $switch_to_blog_id ) ) {
17373
+ $this->switch_to_blog( $switch_to_blog_id );
17374
+ }
17375
+
17376
+ $tag = $this->get_api_site_or_plugin_scope()->get(
17377
+ $this->_get_latest_version_endpoint( $addon_id, 'json' ),
17378
+ $flush,
17379
+ $expiration
17380
+ );
17381
+
17382
+ if ( is_numeric( $switch_to_blog_id ) ) {
17383
+ $this->switch_to_blog( $current_blog_id );
17384
+ }
17385
+
17386
+ $latest_version = ( is_object( $tag ) && isset( $tag->version ) ) ? $tag->version : 'couldn\'t get';
17387
+
17388
+ $this->_logger->departure( 'Latest version ' . $latest_version );
17389
+
17390
+ return ( is_object( $tag ) && isset( $tag->version ) ) ? $tag : false;
17391
+ }
17392
+
17393
+ #----------------------------------------------------------------------------------
17394
+ #region Download Plugin
17395
+ #----------------------------------------------------------------------------------
17396
+
17397
+ /**
17398
+ * Download latest plugin version, based on plan.
17399
+ *
17400
+ * Not like _download_latest(), this will redirect the page
17401
+ * to secure download url to prevent dual download (from FS to WP server,
17402
+ * and then from WP server to the client / browser).
17403
+ *
17404
+ * @author Vova Feldman (@svovaf)
17405
+ * @since 1.0.9
17406
+ *
17407
+ * @param bool|number $plugin_id
17408
+ *
17409
+ * @uses FS_Api
17410
+ * @uses wp_redirect()
17411
+ */
17412
+ private function download_latest_directly( $plugin_id = false ) {
17413
+ $this->_logger->entrance();
17414
+
17415
+ wp_redirect( $this->get_latest_download_api_url( $plugin_id ) );
17416
+ }
17417
+
17418
+ /**
17419
+ * Get latest plugin FS API download URL.
17420
+ *
17421
+ * @author Vova Feldman (@svovaf)
17422
+ * @since 1.0.9
17423
+ *
17424
+ * @param bool|number $plugin_id
17425
+ *
17426
+ * @return string
17427
+ */
17428
+ private function get_latest_download_api_url( $plugin_id = false ) {
17429
+ $this->_logger->entrance();
17430
+
17431
+ return $this->get_api_site_scope()->get_signed_url(
17432
+ $this->_get_latest_version_endpoint( $plugin_id, 'zip' )
17433
+ );
17434
+ }
17435
+
17436
+ /**
17437
+ * Get payment invoice URL.
17438
+ *
17439
+ * @author Vova Feldman (@svovaf)
17440
+ * @since 1.2.0
17441
+ *
17442
+ * @param bool|number $payment_id
17443
+ *
17444
+ * @return string
17445
+ */
17446
+ function _get_invoice_api_url( $payment_id = false ) {
17447
+ $this->_logger->entrance();
17448
+
17449
+ return $this->get_api_user_scope()->get_signed_url(
17450
+ "/payments/{$payment_id}/invoice.pdf"
17451
+ );
17452
+ }
17453
+
17454
+ /**
17455
+ * Get latest plugin download link.
17456
+ *
17457
+ * @author Vova Feldman (@svovaf)
17458
+ * @since 1.0.9
17459
+ *
17460
+ * @param string $label
17461
+ * @param bool|number $plugin_id
17462
+ *
17463
+ * @return string
17464
+ */
17465
+ private function get_latest_download_link( $label, $plugin_id = false ) {
17466
+ return sprintf(
17467
+ '<a target="_blank" href="%s">%s</a>',
17468
+ $this->_get_latest_download_local_url( $plugin_id ),
17469
+ $label
17470
+ );
17471
+ }
17472
+
17473
+ /**
17474
+ * Get latest plugin download local URL.
17475
+ *
17476
+ * @author Vova Feldman (@svovaf)
17477
+ * @since 1.0.9
17478
+ *
17479
+ * @param bool|number $plugin_id
17480
+ *
17481
+ * @return string
17482
+ */
17483
+ function _get_latest_download_local_url( $plugin_id = false ) {
17484
+ // Add timestamp to protect from caching.
17485
+ $params = array( 'ts' => WP_FS__SCRIPT_START_TIME );
17486
+
17487
+ if ( ! empty( $plugin_id ) ) {
17488
+ $params['plugin_id'] = $plugin_id;
17489
+ } else if ( $this->is_addon() ) {
17490
+ $params['plugin_id'] = $this->get_id();
17491
+ }
17492
+
17493
+ $fs = $this->is_addon() ?
17494
+ $this->get_parent_instance() :
17495
+ $this;
17496
+
17497
+ return $fs->get_account_url( 'download_latest', $params );
17498
+ }
17499
+
17500
+ #endregion Download Plugin ------------------------------------------------------------------
17501
+
17502
+ /**
17503
+ * @author Vova Feldman (@svovaf)
17504
+ * @since 1.0.4
17505
+ *
17506
+ * @uses FS_Api
17507
+ *
17508
+ * @param bool $background Hints the method if it's a background updates check. If false, it means that
17509
+ * was initiated by the admin.
17510
+ * @param bool|number $plugin_id
17511
+ * @param bool $flush Since 1.1.7.3
17512
+ * @param int $expiration Since 1.2.2.7
17513
+ */
17514
+ private function check_updates(
17515
+ $background = false,
17516
+ $plugin_id = false,
17517
+ $flush = true,
17518
+ $expiration = WP_FS__TIME_24_HOURS_IN_SEC
17519
+ ) {
17520
+ $this->_logger->entrance();
17521
+
17522
+ // Check if there's a newer version for download.
17523
+ $new_version = $this->_fetch_newer_version( $plugin_id, $flush, $expiration );
17524
+
17525
+ $update = null;
17526
+ if ( is_object( $new_version ) ) {
17527
+ $update = new FS_Plugin_Tag( $new_version );
17528
+
17529
+ if ( ! $background ) {
17530
+ $this->_admin_notices->add(
17531
+ sprintf(
17532
+ /* translators: %s: Numeric version number (e.g. '2.1.9' */
17533
+ $this->get_text_inline( 'Version %s was released.', 'version-x-released' ) . ' ' . $this->get_text_inline( 'Please download %s.', 'please-download-x' ),
17534
+ $update->version,
17535
+ sprintf(
17536
+ '<a href="%s" target="_blank">%s</a>',
17537
+ $this->get_account_url( 'download_latest' ),
17538
+ sprintf(
17539
+ /* translators: %s: plan name (e.g. latest "Professional" version) */
17540
+ $this->get_text_inline( 'the latest %s version here', 'latest-x-version' ),
17541
+ $this->get_plan_title()
17542
+ )
17543
+ )
17544
+ ),
17545
+ $this->get_text_inline( 'New', 'new' ) . '!'
17546
+ );
17547
+ }
17548
+ } else if ( false === $new_version && ! $background ) {
17549
+ $this->_admin_notices->add(
17550
+ $this->get_text_inline( 'Seems like you got the latest release.', 'you-have-latest' ),
17551
+ $this->get_text_inline( 'You are all good!', 'you-are-good' )
17552
+ );
17553
+ }
17554
+
17555
+ $this->_store_update( $update, true, $plugin_id );
17556
+ }
17557
+
17558
+ /**
17559
+ * @author Vova Feldman (@svovaf)
17560
+ * @since 1.0.4
17561
+ *
17562
+ * @param bool $flush Since 1.1.7.3 add 24 hour cache by default.
17563
+ *
17564
+ * @return FS_Plugin[]
17565
+ *
17566
+ * @uses FS_Api
17567
+ */
17568
+ private function sync_addons( $flush = false ) {
17569
+ $this->_logger->entrance();
17570
+
17571
+ $api = $this->get_api_site_or_plugin_scope();
17572
+
17573
+ /**
17574
+ * @since 1.2.1
17575
+ *
17576
+ * If there's a cached version of the add-ons and not asking
17577
+ * for a flush, just use the currently stored add-ons.
17578
+ */
17579
+ if ( ! $flush && $api->is_cached( '/addons.json?enriched=true' ) ) {
17580
+ $addons = self::get_all_addons();
17581
+
17582
+ return $addons[ $this->_plugin->id ];
17583
+ }
17584
+
17585
+ $result = $api->get( '/addons.json?enriched=true', $flush );
17586
+
17587
+ $addons = array();
17588
+ if ( $this->is_api_result_object( $result, 'plugins' ) &&
17589
+ is_array( $result->plugins )
17590
+ ) {
17591
+ for ( $i = 0, $len = count( $result->plugins ); $i < $len; $i ++ ) {
17592
+ $addons[ $i ] = new FS_Plugin( $result->plugins[ $i ] );
17593
+ }
17594
+
17595
+ $this->_store_addons( $addons, true );
17596
+ }
17597
+
17598
+ return $addons;
17599
+ }
17600
+
17601
+ /**
17602
+ * Handle user email update.
17603
+ *
17604
+ * @author Vova Feldman (@svovaf)
17605
+ * @since 1.0.3
17606
+ * @uses FS_Api
17607
+ *
17608
+ * @param string $new_email
17609
+ *
17610
+ * @return object
17611
+ */
17612
+ private function update_email( $new_email ) {
17613
+ $this->_logger->entrance();
17614
+
17615
+
17616
+ $api = $this->get_api_user_scope();
17617
+ $user = $api->call( "?plugin_id={$this->_plugin->id}&fields=id,email,is_verified", 'put', array(
17618
+ 'email' => $new_email,
17619
+ 'after_email_confirm_url' => $this->_get_admin_page_url(
17620
+ 'account',
17621
+ array( 'fs_action' => 'sync_user' )
17622
+ ),
17623
+ ) );
17624
+
17625
+ if ( ! isset( $user->error ) ) {
17626
+ $this->_user->email = $user->email;
17627
+ $this->_user->is_verified = $user->is_verified;
17628
+ $this->_store_user();
17629
+ } else {
17630
+ // handle different error cases.
17631
+
17632
+ }
17633
+
17634
+ return $user;
17635
+ }
17636
+
17637
+ #----------------------------------------------------------------------------------
17638
+ #region API Error Handling
17639
+ #----------------------------------------------------------------------------------
17640
+
17641
+ /**
17642
+ * @author Vova Feldman (@svovaf)
17643
+ * @since 1.1.1
17644
+ *
17645
+ * @param mixed $result
17646
+ *
17647
+ * @return bool Is API result contains an error.
17648
+ */
17649
+ private function is_api_error( $result ) {
17650
+ return FS_Api::is_api_error( $result );
17651
+ }
17652
+
17653
+ /**
17654
+ * Checks if given API result is a non-empty and not an error object.
17655
+ *
17656
+ * @author Vova Feldman (@svovaf)
17657
+ * @since 1.2.1.5
17658
+ *
17659
+ * @param mixed $result
17660
+ * @param string|null $required_property Optional property we want to verify that is set.
17661
+ *
17662
+ * @return bool
17663
+ */
17664
+ function is_api_result_object( $result, $required_property = null ) {
17665
+ return FS_Api::is_api_result_object( $result, $required_property );
17666
+ }
17667
+
17668
+ /**
17669
+ * Checks if given API result is a non-empty entity object with non-empty ID.
17670
+ *
17671
+ * @author Vova Feldman (@svovaf)
17672
+ * @since 1.2.1.5
17673
+ *
17674
+ * @param mixed $result
17675
+ *
17676
+ * @return bool
17677
+ */
17678
+ private function is_api_result_entity( $result ) {
17679
+ return FS_Api::is_api_result_entity( $result );
17680
+ }
17681
+
17682
+ #endregion
17683
+
17684
+ /**
17685
+ * Make sure a given argument is an array of a specific type.
17686
+ *
17687
+ * @author Vova Feldman (@svovaf)
17688
+ * @since 1.2.1.5
17689
+ *
17690
+ * @param mixed $array
17691
+ * @param string $class
17692
+ *
17693
+ * @return bool
17694
+ */
17695
+ private function is_array_instanceof( $array, $class ) {
17696
+ return ( is_array( $array ) && ( empty( $array ) || $array[0] instanceof $class ) );
17697
+ }
17698
+
17699
+ /**
17700
+ * Start install ownership change.
17701
+ *
17702
+ * @author Vova Feldman (@svovaf)
17703
+ * @since 1.1.1
17704
+ * @uses FS_Api
17705
+ *
17706
+ * @param string $candidate_email
17707
+ *
17708
+ * @return bool Is ownership change successfully initiated.
17709
+ */
17710
+ private function init_change_owner( $candidate_email ) {
17711
+ $this->_logger->entrance();
17712
+
17713
+ $api = $this->get_api_site_scope();
17714
+ $result = $api->call( "/users/{$this->_user->id}.json", 'put', array(
17715
+ 'email' => $candidate_email,
17716
+ 'after_confirm_url' => $this->_get_admin_page_url(
17717
+ 'account',
17718
+ array( 'fs_action' => 'change_owner' )
17719
+ ),
17720
+ ) );
17721
+
17722
+ return ! $this->is_api_error( $result );
17723
+ }
17724
+
17725
+ /**
17726
+ * Handle install ownership change.
17727
+ *
17728
+ * @author Vova Feldman (@svovaf)
17729
+ * @since 1.1.1
17730
+ * @uses FS_Api
17731
+ *
17732
+ * @return bool Was ownership change successfully complete.
17733
+ */
17734
+ private function complete_change_owner() {
17735
+ $this->_logger->entrance();
17736
+
17737
+ $site_result = $this->get_api_site_scope( true )->get();
17738
+ $site = new FS_Site( $site_result );
17739
+ $this->_site = $site;
17740
+
17741
+ $user = new FS_User();
17742
+ $user->id = fs_request_get( 'user_id' );
17743
+
17744
+ // Validate install's user and given user.
17745
+ if ( $user->id != $this->_site->user_id ) {
17746
+ return false;
17747
+ }
17748
+
17749
+ $user->public_key = fs_request_get( 'user_public_key' );
17750
+ $user->secret_key = fs_request_get( 'user_secret_key' );
17751
+
17752
+ // Fetch new user information.
17753
+ $this->_user = $user;
17754
+ $user_result = $this->get_api_user_scope( true )->get();
17755
+ $user = new FS_User( $user_result );
17756
+ $this->_user = $user;
17757
+
17758
+ $this->_set_account( $user, $site );
17759
+
17760
+ return true;
17761
+ }
17762
+
17763
+ /**
17764
+ * Handle user name update.
17765
+ *
17766
+ * @author Vova Feldman (@svovaf)
17767
+ * @since 1.0.9
17768
+ * @uses FS_Api
17769
+ *
17770
+ * @return object
17771
+ */
17772
+ private function update_user_name() {
17773
+ $this->_logger->entrance();
17774
+ $name = fs_request_get( 'fs_user_name_' . $this->get_unique_affix(), '' );
17775
+
17776
+ $api = $this->get_api_user_scope();
17777
+ $user = $api->call( "?plugin_id={$this->_plugin->id}&fields=id,first,last", 'put', array(
17778
+ 'name' => $name,
17779
+ ) );
17780
+
17781
+ if ( ! isset( $user->error ) ) {
17782
+ $this->_user->first = $user->first;
17783
+ $this->_user->last = $user->last;
17784
+ $this->_store_user();
17785
+ } else {
17786
+ // handle different error cases.
17787
+
17788
+ }
17789
+
17790
+ return $user;
17791
+ }
17792
+
17793
+ /**
17794
+ * Verify user email.
17795
+ *
17796
+ * @author Vova Feldman (@svovaf)
17797
+ * @since 1.0.3
17798
+ * @uses FS_Api
17799
+ */
17800
+ private function verify_email() {
17801
+ $this->_handle_account_user_sync();
17802
+
17803
+ if ( $this->_user->is_verified() ) {
17804
+ return;
17805
+ }
17806
+
17807
+ $api = $this->get_api_site_scope();
17808
+ $result = $api->call( "/users/{$this->_user->id}/verify.json", 'put', array(
17809
+ 'after_email_confirm_url' => $this->_get_admin_page_url(
17810
+ 'account',
17811
+ array( 'fs_action' => 'sync_user' )
17812
+ )
17813
+ ) );
17814
+
17815
+ if ( ! isset( $result->error ) ) {
17816
+ $this->_admin_notices->add( sprintf(
17817
+ $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' ),
17818
+ sprintf( '<a href="mailto:%1s">%2s</a>', esc_url( $this->_user->email ), $this->_user->email )
17819
+ ) );
17820
+ } else {
17821
+ // handle different error cases.
17822
+
17823
+ }
17824
+ }
17825
+
17826
+ /**
17827
+ * @author Vova Feldman (@svovaf)
17828
+ * @since 1.1.2
17829
+ *
17830
+ * @param array $params
17831
+ * @param bool|null $network
17832
+ *
17833
+ * @return string
17834
+ */
17835
+ function get_activation_url( $params = array(), $network = null ) {
17836
+ if ( $this->is_addon() && $this->has_free_plan() ) {
17837
+ /**
17838
+ * @author Vova Feldman (@svovaf)
17839
+ * @since 1.2.1.7 Add-on's activation is the parent's module activation.
17840
+ */
17841
+ return $this->get_parent_instance()->get_activation_url( $params );
17842
+ }
17843
+
17844
+ return $this->apply_filters( 'connect_url', $this->_get_admin_page_url( '', $params, $network ) );
17845
+ }
17846
+
17847
+ /**
17848
+ * @author Vova Feldman (@svovaf)
17849
+ * @since 1.2.1.5
17850
+ *
17851
+ * @param array $params
17852
+ *
17853
+ * @return string
17854
+ */
17855
+ function get_reconnect_url( $params = array() ) {
17856
+ $params['fs_action'] = 'reset_anonymous_mode';
17857
+ $params['fs_unique_affix'] = $this->get_unique_affix();
17858
+
17859
+ return $this->get_activation_url( $params );
17860
+ }
17861
+
17862
+ /**
17863
+ * Get the URL of the page that should be loaded after the user connect
17864
+ * or skip in the opt-in screen.
17865
+ *
17866
+ * @author Vova Feldman (@svovaf)
17867
+ * @since 1.1.3
17868
+ *
17869
+ * @param string $filter Filter name.
17870
+ * @param array $params Since 1.2.2.7
17871
+ * @param bool|null $network
17872
+ *
17873
+ * @return string
17874
+ */
17875
+ function get_after_activation_url( $filter, $params = array(), $network = null ) {
17876
+ if ( $this->is_free_wp_org_theme() &&
17877
+ fs_request_has( 'pending_activation' )
17878
+ ) {
17879
+ $first_time_path = '';
17880
+ } else {
17881
+ $first_time_path = $this->_menu->get_first_time_path();
17882
+ }
17883
+
17884
+ if ( $this->_is_network_active &&
17885
+ fs_is_network_admin() &&
17886
+ ! $this->_menu->has_network_menu() &&
17887
+ $this->is_network_registered()
17888
+ ) {
17889
+ $target_url = $this->get_account_url();
17890
+ } else {
17891
+ // Default plugin's page.
17892
+ $target_url = $this->_get_admin_page_url( '', array(), $network );
17893
+ }
17894
+
17895
+ return add_query_arg( $params, $this->apply_filters(
17896
+ $filter,
17897
+ empty( $first_time_path ) ?
17898
+ $target_url :
17899
+ $first_time_path
17900
+ ) );
17901
+ }
17902
+
17903
+ /**
17904
+ * Handle account page updates / edits / actions.
17905
+ *
17906
+ * @author Vova Feldman (@svovaf)
17907
+ * @since 1.0.2
17908
+ *
17909
+ */
17910
+ private function _handle_account_edits() {
17911
+ if ( ! $this->is_user_admin() ) {
17912
+ return;
17913
+ }
17914
+
17915
+ $action = fs_get_action();
17916
+
17917
+ if ( empty( $action ) ) {
17918
+ return;
17919
+ }
17920
+
17921
+ $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
17922
+ $install_id = fs_request_get( 'install_id', '' );
17923
+
17924
+ // Alias.
17925
+ $oops_text = $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...';
17926
+
17927
+ $is_network_action = $this->is_network_level_action();
17928
+ $blog_id = $this->is_network_level_site_specific_action();
17929
+
17930
+ if ( is_numeric( $blog_id ) ) {
17931
+ $this->switch_to_blog( $blog_id );
17932
+ } else {
17933
+ $blog_id = '';
17934
+ }
17935
+
17936
+ switch ( $action ) {
17937
+ case 'opt_in':
17938
+ check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
17939
+
17940
+ if ( $plugin_id == $this->get_id() ) {
17941
+ if ( $is_network_action && ! empty( $blog_id ) ) {
17942
+ if ( ! $this->is_registered() ) {
17943
+ $this->install_with_user(
17944
+ $this->get_network_user(),
17945
+ false,
17946
+ false,
17947
+ false,
17948
+ false
17949
+ );
17950
+
17951
+ $this->_admin_notices->add(
17952
+ $this->get_text_inline( 'Site successfully opted in.', 'successful-opt-in' ),
17953
+ $this->get_text_inline( 'Awesome', 'awesome' )
17954
+ );
17955
+ }
17956
+ }
17957
+ }
17958
+ break;
17959
+
17960
+ case 'toggle_tracking':
17961
+ check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
17962
+
17963
+ if ( $plugin_id == $this->get_id() ) {
17964
+ if ( $is_network_action && ! empty( $blog_id ) ) {
17965
+ if ( $this->is_registered() ) {
17966
+ if ( $this->is_tracking_prohibited() ) {
17967
+ if ( $this->allow_site_tracking() ) {
17968
+ $this->_admin_notices->add(
17969
+ sprintf( $this->get_text_inline( 'We appreciate your help in making the %s better by letting us track some usage data.', 'opt-out-message-appreciation' ), $this->_module_type ),
17970
+ $this->get_text_inline( 'Thank you!', 'thank-you' )
17971
+ );
17972
+ }
17973
+ } else {
17974
+ if ( $this->stop_site_tracking() ) {
17975
+ $this->_admin_notices->add(
17976
+ sprintf(
17977
+ $this->get_text_inline( 'We will no longer be sending any usage data of %s on %s to %s.', 'opted-out-successfully' ),
17978
+ $this->get_plugin_title(),
17979
+ fs_strip_url_protocol( get_site_url( $blog_id ) ),
17980
+ sprintf(
17981
+ '<a href="%s" target="_blank">%s</a>',
17982
+ 'https://freemius.com',
17983
+ 'freemius.com'
17984
+ )
17985
+ )
17986
+ );
17987
+ }
17988
+ }
17989
+ }
17990
+ }
17991
+ }
17992
+
17993
+ break;
17994
+
17995
+ case 'delete_account':
17996
+ check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
17997
+
17998
+ if ( $plugin_id == $this->get_id() ) {
17999
+ if ( $is_network_action && empty( $blog_id ) ) {
18000
+ $this->delete_network_account_event();
18001
+ } else {
18002
+ $this->delete_account_event();
18003
+ }
18004
+
18005
+ // Clear user and site.
18006
+ $this->_site = null;
18007
+ $this->_user = null;
18008
+
18009
+ fs_redirect( $this->get_activation_url() );
18010
+ } else {
18011
+ if ( $this->is_addon_activated( $plugin_id ) ) {
18012
+ $fs_addon = self::get_instance_by_id( $plugin_id );
18013
+ $fs_addon->delete_account_event();
18014
+
18015
+ fs_redirect( $this->_get_admin_page_url( 'account' ) );
18016
+ }
18017
+ }
18018
+
18019
+ return;
18020
+
18021
+ case 'downgrade_account':
18022
+ if ( is_numeric( $blog_id ) ) {
18023
+ check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
18024
+ } else {
18025
+ check_admin_referer( $action );
18026
+ }
18027
+
18028
+ if ( $plugin_id == $this->get_id() ) {
18029
+ $this->_downgrade_site();
18030
+
18031
+ if ( is_numeric( $blog_id ) ) {
18032
+ $this->switch_to_blog( $this->_storage->network_install_blog_id );
18033
+ }
18034
+ } else if ( $this->is_addon_activated( $plugin_id ) ) {
18035
+ $fs_addon = self::get_instance_by_id( $plugin_id );
18036
+ $fs_addon->_downgrade_site();
18037
+ }
18038
+
18039
+ return;
18040
+
18041
+ case 'activate_license':
18042
+ check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
18043
+
18044
+ $fs = $this;
18045
+ if ( $plugin_id != $this->get_id() ) {
18046
+ $fs = $this->is_addon_activated( $plugin_id ) ?
18047
+ self::get_instance_by_id( $plugin_id ) :
18048
+ null;
18049
+ }
18050
+
18051
+ if ( is_object( $fs ) ) {
18052
+ $fs->_activate_license();
18053
+ }
18054
+
18055
+ return;
18056
+
18057
+ case 'deactivate_license':
18058
+ check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
18059
+
18060
+ if ( $plugin_id == $this->get_id() ) {
18061
+ $this->_deactivate_license();
18062
+
18063
+ if ( $this->is_only_premium() ) {
18064
+ // Clear user and site.
18065
+ $this->_site = null;
18066
+ $this->_user = null;
18067
+
18068
+ fs_redirect( $this->get_activation_url() );
18069
+ }
18070
+ } else {
18071
+ if ( $this->is_addon_activated( $plugin_id ) ) {
18072
+ $fs_addon = self::get_instance_by_id( $plugin_id );
18073
+ $fs_addon->_deactivate_license();
18074
+ }
18075
+ }
18076
+
18077
+ return;
18078
+
18079
+ case 'check_updates':
18080
+ check_admin_referer( $action );
18081
+ $this->check_updates();
18082
+
18083
+ return;
18084
+
18085
+ case 'change_owner':
18086
+ $state = fs_request_get( 'state', 'init' );
18087
+ switch ( $state ) {
18088
+ case 'init':
18089
+ $candidate_email = fs_request_get( 'candidate_email', '' );
18090
+
18091
+ if ( $this->init_change_owner( $candidate_email ) ) {
18092
+ $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>' ) );
18093
+ }
18094
+ break;
18095
+ case 'owner_confirmed':
18096
+ $candidate_email = fs_request_get( 'candidate_email', '' );
18097
+
18098
+ $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>' ) );
18099
+ break;
18100
+ case 'candidate_confirmed':
18101
+ if ( $this->complete_change_owner() ) {
18102
+ $this->_admin_notices->add_sticky(
18103
+ sprintf( $this->get_text_inline( '%s is the new owner of the account.', 'change-owner-request_candidate-confirmed' ), '<b>' . $this->_user->email . '</b>' ),
18104
+ 'ownership_changed',
18105
+ $this->get_text_x_inline( 'Congrats', 'as congratulations', 'congrats' ) . '!'
18106
+ );
18107
+ } else {
18108
+ // @todo Handle failed ownership change message.
18109
+ }
18110
+ break;
18111
+ }
18112
+
18113
+ return;
18114
+
18115
+ case 'update_email':
18116
+ check_admin_referer( 'update_email' );
18117
+
18118
+ $new_email = fs_request_get( 'fs_email_' . $this->get_unique_affix(), '' );
18119
+ $result = $this->update_email( $new_email );
18120
+
18121
+ if ( isset( $result->error ) ) {
18122
+ switch ( $result->error->code ) {
18123
+ case 'user_exist':
18124
+ $this->_admin_notices->add(
18125
+ $this->get_text_inline( 'Sorry, we could not complete the email update. Another user with the same email is already registered.', 'user-exist-message' ) . ' ' .
18126
+ 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>' ) .
18127
+ sprintf(
18128
+ '<a style="margin-left: 10px;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
18129
+ $this->get_account_url( 'change_owner', array(
18130
+ 'state' => 'init',
18131
+ 'candidate_email' => $new_email
18132
+ ) ),
18133
+ $this->get_text_inline( 'Change Ownership', 'change-ownership' )
18134
+ ),
18135
+ $oops_text,
18136
+ 'error'
18137
+ );
18138
+ break;
18139
+ }
18140
+ } else {
18141
+ $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' ) );
18142
+ }
18143
+
18144
+ return;
18145
+
18146
+ case 'update_user_name':
18147
+ check_admin_referer( 'update_user_name' );
18148
+
18149
+ $result = $this->update_user_name();
18150
+
18151
+ if ( isset( $result->error ) ) {
18152
+ $this->_admin_notices->add(
18153
+ $this->get_text_inline( 'Please provide your full name.', 'name-update-failed-message' ),
18154
+ $oops_text,
18155
+ 'error'
18156
+ );
18157
+ } else {
18158
+ $this->_admin_notices->add( $this->get_text_inline( 'Your name was successfully updated.', 'name-updated-message' ) );
18159
+ }
18160
+
18161
+ return;
18162
+
18163
+ #region Actions that might be called from external links (e.g. email)
18164
+
18165
+ case 'cancel_trial':
18166
+ if ( $plugin_id == $this->get_id() ) {
18167
+ $this->_cancel_trial();
18168
+ } else {
18169
+ if ( $this->is_addon_activated( $plugin_id ) ) {
18170
+ $fs_addon = self::get_instance_by_id( $plugin_id );
18171
+ $fs_addon->_cancel_trial();
18172
+ }
18173
+ }
18174
+
18175
+ return;
18176
+
18177
+ case 'verify_email':
18178
+ $this->verify_email();
18179
+
18180
+ return;
18181
+
18182
+ case 'sync_user':
18183
+ $this->_handle_account_user_sync();
18184
+
18185
+ return;
18186
+
18187
+ case $this->get_unique_affix() . '_sync_license':
18188
+ $this->_sync_license();
18189
+
18190
+ return;
18191
+
18192
+ case 'download_latest':
18193
+ $this->download_latest_directly( $plugin_id );
18194
+
18195
+ return;
18196
+
18197
+ #endregion
18198
+ }
18199
+
18200
+ if ( WP_FS__IS_POST_REQUEST ) {
18201
+ $properties = array( 'site_secret_key', 'site_id', 'site_public_key' );
18202
+ foreach ( $properties as $p ) {
18203
+ if ( 'update_' . $p === $action ) {
18204
+ check_admin_referer( $action );
18205
+
18206
+ $this->_logger->log( $action );
18207
+
18208
+ $site_property = substr( $p, strlen( 'site_' ) );
18209
+ $site_property_value = fs_request_get( 'fs_' . $p . '_' . $this->get_unique_affix(), '' );
18210
+ $this->get_site()->{$site_property} = $site_property_value;
18211
+
18212
+ // Store account after modification.
18213
+ $this->_store_site();
18214
+
18215
+ $this->do_action( 'account_property_edit', 'site', $site_property, $site_property_value );
18216
+
18217
+ $this->_admin_notices->add( sprintf(
18218
+ /* translators: %s: User's account property (e.g. email address, name) */
18219
+ $this->get_text_inline( 'You have successfully updated your %s.', 'x-updated' ),
18220
+ '<b>' . str_replace( '_', ' ', $p ) . '</b>'
18221
+ ) );
18222
+
18223
+ return;
18224
+ }
18225
+ }
18226
+ }
18227
+ }
18228
+
18229
+ /**
18230
+ * Account page resources load.
18231
+ *
18232
+ * @author Vova Feldman (@svovaf)
18233
+ * @since 1.0.6
18234
+ */
18235
+ function _account_page_load() {
18236
+ $this->_logger->entrance();
18237
+
18238
+ $this->_logger->info( var_export( $_REQUEST, true ) );
18239
+
18240
+ fs_enqueue_local_style( 'fs_account', '/admin/account.css' );
18241
+
18242
+ if ( $this->has_addons() ) {
18243
+ wp_enqueue_script( 'plugin-install' );
18244
+ add_thickbox();
18245
+
18246
+ function fs_addons_body_class( $classes ) {
18247
+ $classes .= ' plugins-php';
18248
+
18249
+ return $classes;
18250
+ }
18251
+
18252
+ add_filter( 'admin_body_class', 'fs_addons_body_class' );
18253
+ }
18254
+
18255
+ if ( $this->has_paid_plan() &&
18256
+ ! $this->has_any_license() &&
18257
+ ! $this->is_sync_executed() &&
18258
+ $this->is_tracking_allowed()
18259
+ ) {
18260
+ /**
18261
+ * If no licenses found and no sync job was executed during the last 24 hours,
18262
+ * just execute the sync job right away (blocking execution).
18263
+ *
18264
+ * @since 1.1.7.3
18265
+ */
18266
+ $this->run_manual_sync();
18267
+ }
18268
+
18269
+ $this->_handle_account_edits();
18270
+
18271
+ $this->do_action( 'account_page_load_before_departure' );
18272
+ }
18273
+
18274
+ /**
18275
+ * Renders the "Affiliation" page.
18276
+ *
18277
+ * @author Leo Fajardo (@leorw)
18278
+ * @since 1.2.3
18279
+ */
18280
+ function _affiliation_page_render() {
18281
+ $this->_logger->entrance();
18282
+
18283
+ $this->fetch_affiliate_and_terms();
18284
+
18285
+ fs_enqueue_local_style( 'fs_affiliation', '/admin/affiliation.css' );
18286
+
18287
+ $vars = array( 'id' => $this->_module_id );
18288
+ echo $this->apply_filters( "/forms/affiliation.php", fs_get_template( '/forms/affiliation.php', $vars ) );
18289
+ }
18290
+
18291
+
18292
+ /**
18293
+ * Render account page.
18294
+ *
18295
+ * @author Vova Feldman (@svovaf)
18296
+ * @since 1.0.0
18297
+ */
18298
+ function _account_page_render() {
18299
+ $this->_logger->entrance();
18300
+
18301
+ $template = 'account.php';
18302
+ $vars = array( 'id' => $this->_module_id );
18303
+
18304
+ /**
18305
+ * Added filter to the template to allow developers wrapping the template
18306
+ * in custom HTML (e.g. within a wizard/tabs).
18307
+ *
18308
+ * @author Vova Feldman (@svovaf)
18309
+ * @since 1.2.1.6
18310
+ */
18311
+ echo $this->apply_filters( "templates/{$template}", fs_get_template( $template, $vars ) );
18312
+ }
18313
+
18314
+ /**
18315
+ * Render account connect page.
18316
+ *
18317
+ * @author Vova Feldman (@svovaf)
18318
+ * @since 1.0.7
18319
+ */
18320
+ function _connect_page_render() {
18321
+ $this->_logger->entrance();
18322
+
18323
+ $vars = array( 'id' => $this->_module_id );
18324
+
18325
+ /**
18326
+ * Added filter to the template to allow developers wrapping the template
18327
+ * in custom HTML (e.g. within a wizard/tabs).
18328
+ *
18329
+ * @author Vova Feldman (@svovaf)
18330
+ * @since 1.2.1.6
18331
+ */
18332
+ echo $this->apply_filters( 'templates/connect.php', fs_get_template( 'connect.php', $vars ) );
18333
+ }
18334
+
18335
+ /**
18336
+ * Load required resources before add-ons page render.
18337
+ *
18338
+ * @author Vova Feldman (@svovaf)
18339
+ * @since 1.0.6
18340
+ */
18341
+ function _addons_page_load() {
18342
+ $this->_logger->entrance();
18343
+
18344
+ fs_enqueue_local_style( 'fs_addons', '/admin/add-ons.css' );
18345
+
18346
+ wp_enqueue_script( 'plugin-install' );
18347
+ add_thickbox();
18348
+
18349
+ function fs_addons_body_class( $classes ) {
18350
+ $classes .= ' plugins-php';
18351
+
18352
+ return $classes;
18353
+ }
18354
+
18355
+ add_filter( 'admin_body_class', 'fs_addons_body_class' );
18356
+
18357
+ if ( ! $this->is_registered() && $this->is_org_repo_compliant() ) {
18358
+ $this->_admin_notices->add(
18359
+ 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>' ),
18360
+ $this->get_text_x_inline( 'Heads up', 'advance notice of something that will need attention.', 'heads-up' ),
18361
+ 'update-nag'
18362
+ );
18363
+ }
18364
+ }
18365
+
18366
+ /**
18367
+ * Render add-ons page.
18368
+ *
18369
+ * @author Vova Feldman (@svovaf)
18370
+ * @since 1.0.6
18371
+ */
18372
+ function _addons_page_render() {
18373
+ $this->_logger->entrance();
18374
+
18375
+ $vars = array( 'id' => $this->_module_id );
18376
+
18377
+ /**
18378
+ * Added filter to the template to allow developers wrapping the template
18379
+ * in custom HTML (e.g. within a wizard/tabs).
18380
+ *
18381
+ * @author Vova Feldman (@svovaf)
18382
+ * @since 1.2.1.6
18383
+ */
18384
+ echo $this->apply_filters( 'templates/add-ons.php', fs_get_template( 'add-ons.php', $vars ) );
18385
+ }
18386
+
18387
+ /* Pricing & Upgrade
18388
+ ------------------------------------------------------------------------------------------------------------------*/
18389
+ /**
18390
+ * Render pricing page.
18391
+ *
18392
+ * @author Vova Feldman (@svovaf)
18393
+ * @since 1.0.0
18394
+ */
18395
+ function _pricing_page_render() {
18396
+ $this->_logger->entrance();
18397
+
18398
+ $vars = array( 'id' => $this->_module_id );
18399
+
18400
+ if ( 'true' === fs_request_get( 'checkout', false ) ) {
18401
+ fs_require_once_template( 'checkout.php', $vars );
18402
+ } else {
18403
+ fs_require_once_template( 'pricing.php', $vars );
18404
+ }
18405
+ }
18406
+
18407
+ #----------------------------------------------------------------------------------
18408
+ #region Contact Us
18409
+ #----------------------------------------------------------------------------------
18410
+
18411
+ /**
18412
+ * Render contact-us page.
18413
+ *
18414
+ * @author Vova Feldman (@svovaf)
18415
+ * @since 1.0.3
18416
+ */
18417
+ function _contact_page_render() {
18418
+ $this->_logger->entrance();
18419
+
18420
+ $vars = array( 'id' => $this->_module_id );
18421
+ fs_require_once_template( 'contact.php', $vars );
18422
+ }
18423
+
18424
+ #endregion ------------------------------------------------------------------------
18425
+
18426
+ /**
18427
+ * Hide all admin notices to prevent distractions.
18428
+ *
18429
+ * @author Vova Feldman (@svovaf)
18430
+ * @since 1.0.3
18431
+ *
18432
+ * @uses remove_all_actions()
18433
+ */
18434
+ private static function _hide_admin_notices() {
18435
+ remove_all_actions( 'admin_notices' );
18436
+ remove_all_actions( 'network_admin_notices' );
18437
+ remove_all_actions( 'all_admin_notices' );
18438
+ remove_all_actions( 'user_admin_notices' );
18439
+ }
18440
+
18441
+ static function _clean_admin_content_section_hook() {
18442
+ self::_hide_admin_notices();
18443
+
18444
+ // Hide footer.
18445
+ echo '<style>#wpfooter { display: none !important; }</style>';
18446
+ }
18447
+
18448
+ /**
18449
+ * Attach to admin_head hook to hide all admin notices.
18450
+ *
18451
+ * @author Vova Feldman (@svovaf)
18452
+ * @since 1.0.3
18453
+ */
18454
+ static function _clean_admin_content_section() {
18455
+ add_action( 'admin_head', 'Freemius::_clean_admin_content_section_hook' );
18456
+ }
18457
+
18458
+ /* CSS & JavaScript
18459
+ ------------------------------------------------------------------------------------------------------------------*/
18460
+ /* function _enqueue_script($handle, $src) {
18461
+ $url = plugins_url( substr( WP_FS__DIR_JS, strlen( $this->_plugin_dir_path ) ) . '/assets/js/' . $src );
18462
+
18463
+ $this->_logger->entrance( 'script = ' . $url );
18464
+
18465
+ wp_enqueue_script( $handle, $url );
18466
+ }*/
18467
+
18468
+ /* SDK
18469
+ ------------------------------------------------------------------------------------------------------------------*/
18470
+ private $_user_api;
18471
+
18472
+ /**
18473
+ *
18474
+ * @author Vova Feldman (@svovaf)
18475
+ * @since 1.0.2
18476
+ *
18477
+ * @param bool $flush
18478
+ *
18479
+ * @return FS_Api
18480
+ */
18481
+ private function get_api_user_scope( $flush = false ) {
18482
+ if ( ! isset( $this->_user_api ) || $flush ) {
18483
+ $this->_user_api = $this->get_api_user_scope_by_user( $this->_user );
18484
+ }
18485
+
18486
+ return $this->_user_api;
18487
+ }
18488
+
18489
+ /**
18490
+ * @author Vova Feldman (@svovaf)
18491
+ * @since 2.0.0
18492
+ *
18493
+ * @param \FS_User $user
18494
+ *
18495
+ * @return \FS_Api
18496
+ */
18497
+ private function get_api_user_scope_by_user( FS_User $user ) {
18498
+ return FS_Api::instance(
18499
+ $this->_module_id,
18500
+ 'user',
18501
+ $user->id,
18502
+ $user->public_key,
18503
+ ! $this->is_live(),
18504
+ $user->secret_key
18505
+ );
18506
+ }
18507
+
18508
+ /**
18509
+ *
18510
+ * @author Leo Fajardo (@leorw)
18511
+ * @since 2.0.0
18512
+ *
18513
+ * @param bool $flush
18514
+ *
18515
+ * @return FS_Api
18516
+ */
18517
+ private function get_current_or_network_user_api_scope( $flush = false ) {
18518
+ if ( ! $this->_is_network_active || isset( $this->_user ) ) {
18519
+ return $this->get_api_user_scope( $flush );
18520
+ }
18521
+
18522
+ $user = $this->get_current_or_network_user();
18523
+
18524
+ $this->_user_api = FS_Api::instance(
18525
+ $this->_module_id,
18526
+ 'user',
18527
+ $user->id,
18528
+ $user->public_key,
18529
+ ! $this->is_live(),
18530
+ $user->secret_key
18531
+ );
18532
+
18533
+ return $this->_user_api;
18534
+ }
18535
+
18536
+ private $_site_api;
18537
+
18538
+ /**
18539
+ *
18540
+ * @author Vova Feldman (@svovaf)
18541
+ * @since 1.0.2
18542
+ *
18543
+ * @param bool $flush
18544
+ *
18545
+ * @return FS_Api
18546
+ */
18547
+ private function get_api_site_scope( $flush = false ) {
18548
+ if ( ! isset( $this->_site_api ) || $flush ) {
18549
+ $this->_site_api = FS_Api::instance(
18550
+ $this->_module_id,
18551
+ 'install',
18552
+ $this->_site->id,
18553
+ $this->_site->public_key,
18554
+ ! $this->is_live(),
18555
+ $this->_site->secret_key
18556
+ );
18557
+ }
18558
+
18559
+ return $this->_site_api;
18560
+ }
18561
+
18562
+ private $_plugin_api;
18563
+
18564
+ /**
18565
+ * Get plugin public API scope.
18566
+ *
18567
+ * @author Vova Feldman (@svovaf)
18568
+ * @since 1.0.7
18569
+ *
18570
+ * @return FS_Api
18571
+ */
18572
+ function get_api_plugin_scope() {
18573
+ if ( ! isset( $this->_plugin_api ) ) {
18574
+ $this->_plugin_api = FS_Api::instance(
18575
+ $this->_module_id,
18576
+ 'plugin',
18577
+ $this->_plugin->id,
18578
+ $this->_plugin->public_key,
18579
+ ! $this->is_live()
18580
+ );
18581
+ }
18582
+
18583
+ return $this->_plugin_api;
18584
+ }
18585
+
18586
+ /**
18587
+ * Get site API scope object (fallback to public plugin scope when not registered).
18588
+ *
18589
+ * @author Vova Feldman (@svovaf)
18590
+ * @since 1.0.7
18591
+ *
18592
+ * @return FS_Api
18593
+ */
18594
+ function get_api_site_or_plugin_scope() {
18595
+ return $this->is_registered() ?
18596
+ $this->get_api_site_scope() :
18597
+ $this->get_api_plugin_scope();
18598
+ }
18599
+
18600
+ /**
18601
+ * Show trial promotional notice (if any trial exist).
18602
+ *
18603
+ * @author Vova Feldman (@svovaf)
18604
+ * @since 1.0.9
18605
+ *
18606
+ * @param $plans
18607
+ */
18608
+ function _check_for_trial_plans( $plans ) {
18609
+ $this->_storage->has_trial_plan = FS_Plan_Manager::instance()->has_trial_plan( $plans );
18610
+ }
18611
+
18612
+ /**
18613
+ * During trial promotion the "upgrade" submenu item turns to
18614
+ * "start trial" to encourage the trial. Since we want to keep
18615
+ * the same menu item handler and there's no robust way to
18616
+ * add new arguments to the menu item link's querystring,
18617
+ * use JavaScript to find the menu item and update the href of
18618
+ * the link.
18619
+ *
18620
+ * @author Vova Feldman (@svovaf)
18621
+ * @since 1.2.1.5
18622
+ */
18623
+ function _fix_start_trial_menu_item_url() {
18624
+ $template_args = array( 'id' => $this->_module_id );
18625
+ fs_require_template( 'add-trial-to-pricing.php', $template_args );
18626
+ }
18627
+
18628
+ /**
18629
+ * Check if module is currently in a trial promotion mode.
18630
+ *
18631
+ * @author Vova Feldman (@svovaf)
18632
+ * @since 1.2.2.7
18633
+ *
18634
+ * @return bool
18635
+ */
18636
+ function is_in_trial_promotion() {
18637
+ return $this->_admin_notices->has_sticky( 'trial_promotion' );
18638
+ }
18639
+
18640
+ /**
18641
+ * Show trial promotional notice (if any trial exist).
18642
+ *
18643
+ * @author Vova Feldman (@svovaf)
18644
+ * @since 1.0.9
18645
+ *
18646
+ * @return bool If trial notice added.
18647
+ */
18648
+ function _add_trial_notice() {
18649
+ if ( ! $this->is_user_admin() ) {
18650
+ return false;
18651
+ }
18652
+
18653
+ if ( ! $this->is_user_in_admin() ) {
18654
+ return false;
18655
+ }
18656
+
18657
+ if ( $this->_is_network_active ) {
18658
+ if ( fs_is_network_admin() ) {
18659
+ // Network level trial is disabled at the moment.
18660
+ return false;
18661
+ }
18662
+
18663
+ if ( ! $this->is_delegated_connection() ) {
18664
+ // Only delegated sites should support trials.
18665
+ return false;
18666
+ }
18667
+ }
18668
+
18669
+ // Check if trial message is already shown.
18670
+ if ( $this->is_in_trial_promotion() ) {
18671
+ add_action( 'admin_footer', array( &$this, '_fix_start_trial_menu_item_url' ) );
18672
+
18673
+ $this->_menu->add_counter_to_menu_item( 1, 'fs-trial' );
18674
+
18675
+ return false;
18676
+ }
18677
+
18678
+ if ( $this->is_premium() && ! WP_FS__DEV_MODE ) {
18679
+ // Don't show trial if running the premium code, unless running in DEV mode.
18680
+ return false;
18681
+ }
18682
+
18683
+ if ( ! $this->has_trial_plan() ) {
18684
+ // No plans with trial.
18685
+ return false;
18686
+ }
18687
+
18688
+ if ( ! $this->apply_filters( 'show_trial', true ) ) {
18689
+ // Developer explicitly asked not to show the trial promo.
18690
+ return false;
18691
+ }
18692
+
18693
+ if ( $this->is_registered() ) {
18694
+ // Check if trial already utilized.
18695
+ if ( $this->_site->is_trial_utilized() ) {
18696
+ return false;
18697
+ }
18698
+
18699
+ if ( $this->is_paying_or_trial() ) {
18700
+ // Don't show trial if paying or already in trial.
18701
+ return false;
18702
+ }
18703
+ }
18704
+
18705
+ if ( $this->is_activation_mode() || $this->is_pending_activation() ) {
18706
+ // If not yet opted-in/skipped, or pending activation, don't show trial.
18707
+ return false;
18708
+ }
18709
+
18710
+ $last_time_trial_promotion_shown = $this->_storage->get( 'trial_promotion_shown', false );
18711
+ $was_promotion_shown_before = ( false !== $last_time_trial_promotion_shown );
18712
+
18713
+ // Show promotion if never shown before and 24 hours after initial activation with FS.
18714
+ if ( ! $was_promotion_shown_before &&
18715
+ $this->_storage->install_timestamp > ( time() - WP_FS__TIME_24_HOURS_IN_SEC )
18716
+ ) {
18717
+ return false;
18718
+ }
18719
+
18720
+ // OR if promotion was shown before, try showing it every 30 days.
18721
+ if ( $was_promotion_shown_before &&
18722
+ 30 * WP_FS__TIME_24_HOURS_IN_SEC > time() - $last_time_trial_promotion_shown
18723
+ ) {
18724
+ return false;
18725
+ }
18726
+
18727
+ $trial_period = $this->_trial_days;
18728
+ $require_payment = $this->_is_trial_require_payment;
18729
+ $trial_url = $this->get_trial_url();
18730
+ $plans_string = strtolower( $this->get_text_inline( 'Awesome', 'awesome' ) );
18731
+
18732
+ if ( $this->is_registered() ) {
18733
+ // If opted-in, override trial with up to date data from API.
18734
+ $trial_plans = FS_Plan_Manager::instance()->get_trial_plans( $this->_plans );
18735
+ $trial_plans_count = count( $trial_plans );
18736
+
18737
+ if ( 0 === $trial_plans_count ) {
18738
+ // If there's no plans with a trial just exit.
18739
+ return false;
18740
+ }
18741
+
18742
+ /**
18743
+ * @var FS_Plugin_Plan $paid_plan
18744
+ */
18745
+ $paid_plan = $trial_plans[0];
18746
+ $require_payment = $paid_plan->is_require_subscription;
18747
+ $trial_period = $paid_plan->trial_period;
18748
+
18749
+ $total_paid_plans = count( $this->_plans ) - ( FS_Plan_Manager::instance()->has_free_plan( $this->_plans ) ? 1 : 0 );
18750
+
18751
+ if ( $total_paid_plans !== $trial_plans_count ) {
18752
+ // Not all paid plans have a trial - generate a string of those that have it.
18753
+ for ( $i = 0; $i < $trial_plans_count; $i ++ ) {
18754
+ $plans_string .= sprintf(
18755
+ ' <a href="%s">%s</a>',
18756
+ $trial_url,
18757
+ $trial_plans[ $i ]->title
18758
+ );
18759
+
18760
+ if ( $i < $trial_plans_count - 2 ) {
18761
+ $plans_string .= ', ';
18762
+ } else if ( $i == $trial_plans_count - 2 ) {
18763
+ $plans_string .= ' and ';
18764
+ }
18765
+ }
18766
+ }
18767
+ }
18768
+
18769
+ $message = sprintf(
18770
+ $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' ),
18771
+ sprintf( '<b>%s</b>', $this->get_plugin_name() ),
18772
+ $plans_string,
18773
+ $trial_period
18774
+ );
18775
+
18776
+ // "No Credit-Card Required" or "No Commitment for N Days".
18777
+ $cc_string = $require_payment ?
18778
+ sprintf( $this->get_text_inline( 'No commitment for %s days - cancel anytime!', 'no-commitment-for-x-days' ), $trial_period ) :
18779
+ $this->get_text_inline( 'No credit card required', 'no-cc-required' ) . '!';
18780
+
18781
+
18782
+ // Start trial button.
18783
+ $button = ' ' . sprintf(
18784
+ '<a style="margin-left: 10px; vertical-align: super;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
18785
+ $trial_url,
18786
+ $this->get_text_x_inline( 'Start free trial', 'call to action', 'start-free-trial' )
18787
+ );
18788
+
18789
+ $this->_admin_notices->add_sticky(
18790
+ $this->apply_filters( 'trial_promotion_message', "{$message} {$cc_string} {$button}" ),
18791
+ 'trial_promotion',
18792
+ '',
18793
+ 'promotion'
18794
+ );
18795
+
18796
+ $this->_storage->trial_promotion_shown = WP_FS__SCRIPT_START_TIME;
18797
+
18798
+ return true;
18799
+ }
18800
+
18801
+ /**
18802
+ * Lets users/customers know that the product has an affiliate program.
18803
+ *
18804
+ * @author Leo Fajardo (@leorw)
18805
+ * @since 1.2.2.11
18806
+ *
18807
+ * @return bool Returns true if the notice has been added.
18808
+ */
18809
+ function _add_affiliate_program_notice() {
18810
+ if ( ! $this->is_user_admin() ) {
18811
+ return false;
18812
+ }
18813
+
18814
+ if ( ! $this->is_user_in_admin() ) {
18815
+ return false;
18816
+ }
18817
+
18818
+ // Check if the notice is already shown.
18819
+ if ( $this->_admin_notices->has_sticky( 'affiliate_program' ) ) {
18820
+ return false;
18821
+ }
18822
+
18823
+ if (
18824
+ // Product has no affiliate program.
18825
+ ! $this->has_affiliate_program() ||
18826
+ // User has applied for an affiliate account.
18827
+ ! empty( $this->_storage->affiliate_application_data )
18828
+ ) {
18829
+ return false;
18830
+ }
18831
+
18832
+ if ( ! $this->apply_filters( 'show_affiliate_program_notice', true ) ) {
18833
+ // Developer explicitly asked not to show the notice about the affiliate program.
18834
+ return false;
18835
+ }
18836
+
18837
+ if ( $this->is_activation_mode() || $this->is_pending_activation() ) {
18838
+ // If not yet opted in/skipped, or pending activation, don't show the notice.
18839
+ return false;
18840
+ }
18841
+
18842
+ $last_time_notice_was_shown = $this->_storage->get( 'affiliate_program_notice_shown', false );
18843
+ $was_notice_shown_before = ( false !== $last_time_notice_was_shown );
18844
+
18845
+ /**
18846
+ * Do not show the notice if it was already shown before or less than 30 days have passed since the initial
18847
+ * activation with FS.
18848
+ */
18849
+ if ( $was_notice_shown_before ||
18850
+ $this->_storage->install_timestamp > ( time() - ( WP_FS__TIME_24_HOURS_IN_SEC * 30 ) )
18851
+ ) {
18852
+ return false;
18853
+ }
18854
+
18855
+ if ( ! $this->is_paying() &&
18856
+ FS_Plugin::AFFILIATE_MODERATION_CUSTOMERS == $this->_plugin->affiliate_moderation
18857
+ ) {
18858
+ // If the user is not a customer and the affiliate program is only for customers, don't show the notice.
18859
+ return false;
18860
+ }
18861
+
18862
+ $message = sprintf(
18863
+ $this->get_text_inline( 'Hey there, did you know that %s has an affiliate program? If you like the %s you can become our ambassador and earn some cash!', 'become-an-ambassador-admin-notice' ),
18864
+ sprintf( '<strong>%s</strong>', $this->get_plugin_name() ),
18865
+ $this->get_module_label( true )
18866
+ );
18867
+
18868
+ // HTML code for the "Learn more..." button.
18869
+ $button = ' ' . sprintf(
18870
+ '<a style="display: block; margin-top: 10px;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
18871
+ $this->_get_admin_page_url( 'affiliation' ),
18872
+ $this->get_text_inline( 'Learn more', 'learn-more' ) . '...'
18873
+ );
18874
+
18875
+ $this->_admin_notices->add_sticky(
18876
+ $this->apply_filters( 'affiliate_program_notice', "{$message} {$button}" ),
18877
+ 'affiliate_program',
18878
+ '',
18879
+ 'promotion'
18880
+ );
18881
+
18882
+ $this->_storage->affiliate_program_notice_shown = WP_FS__SCRIPT_START_TIME;
18883
+
18884
+ return true;
18885
+ }
18886
+
18887
+ /**
18888
+ * @author Vova Feldman (@svovaf)
18889
+ * @since 1.2.1.5
18890
+ */
18891
+ function _enqueue_common_css() {
18892
+ if ( $this->has_paid_plan() && ! $this->is_paying() ) {
18893
+ // Add basic CSS for admin-notices and menu-item colors.
18894
+ fs_enqueue_local_style( 'fs_common', '/admin/common.css' );
18895
+ }
18896
+ }
18897
+
18898
+ /**
18899
+ * @author Leo Fajardo (@leorw)
18900
+ * @since 1.2.2
18901
+ */
18902
+ function _show_theme_activation_optin_dialog() {
18903
+ fs_enqueue_local_style( 'fs_connect', '/admin/connect.css' );
18904
+
18905
+ add_action( 'admin_footer-themes.php', array( &$this, '_add_fs_theme_activation_dialog' ) );
18906
+ }
18907
+
18908
+ /**
18909
+ * @author Leo Fajardo (@leorw)
18910
+ * @since 1.2.2
18911
+ */
18912
+ function _add_fs_theme_activation_dialog() {
18913
+ $vars = array( 'id' => $this->_module_id );
18914
+ fs_require_once_template( 'connect.php', $vars );
18915
+ }
18916
+
18917
+ /* Action Links
18918
+ ------------------------------------------------------------------------------------------------------------------*/
18919
+ private $_action_links_hooked = false;
18920
+ private $_action_links = array();
18921
+
18922
+ /**
18923
+ * Hook to plugin action links filter.
18924
+ *
18925
+ * @author Vova Feldman (@svovaf)
18926
+ * @since 1.0.0
18927
+ */
18928
+ private function hook_plugin_action_links() {
18929
+ $this->_logger->entrance();
18930
+
18931
+ $this->_action_links_hooked = true;
18932
+
18933
+ $this->_logger->log( 'Adding action links hooks.' );
18934
+
18935
+ // Add action link to settings page.
18936
+ add_filter( 'plugin_action_links_' . $this->_plugin_basename, array(
18937
+ &$this,
18938
+ '_modify_plugin_action_links_hook'
18939
+ ), WP_FS__DEFAULT_PRIORITY, 2 );
18940
+ add_filter( 'network_admin_plugin_action_links_' . $this->_plugin_basename, array(
18941
+ &$this,
18942
+ '_modify_plugin_action_links_hook'
18943
+ ), WP_FS__DEFAULT_PRIORITY, 2 );
18944
+ }
18945
+
18946
+ /**
18947
+ * Add plugin action link.
18948
+ *
18949
+ * @author Vova Feldman (@svovaf)
18950
+ * @since 1.0.0
18951
+ *
18952
+ * @param $label
18953
+ * @param $url
18954
+ * @param bool $external
18955
+ * @param int $priority
18956
+ * @param bool $key
18957
+ */
18958
+ function add_plugin_action_link( $label, $url, $external = false, $priority = WP_FS__DEFAULT_PRIORITY, $key = false ) {
18959
+ $this->_logger->entrance();
18960
+
18961
+ if ( ! isset( $this->_action_links[ $priority ] ) ) {
18962
+ $this->_action_links[ $priority ] = array();
18963
+ }
18964
+
18965
+ if ( false === $key ) {
18966
+ $key = preg_replace( "/[^A-Za-z0-9 ]/", '', strtolower( $label ) );
18967
+ }
18968
+
18969
+ $this->_action_links[ $priority ][] = array(
18970
+ 'label' => $label,
18971
+ 'href' => $url,
18972
+ 'key' => $key,
18973
+ 'external' => $external
18974
+ );
18975
+ }
18976
+
18977
+ /**
18978
+ * Adds Upgrade and Add-Ons links to the main Plugins page link actions collection.
18979
+ *
18980
+ * @author Vova Feldman (@svovaf)
18981
+ * @since 1.0.0
18982
+ */
18983
+ function _add_upgrade_action_link() {
18984
+ $this->_logger->entrance();
18985
+
18986
+ if ( ! $this->is_paying() && $this->has_paid_plan() ) {
18987
+ $this->add_plugin_action_link(
18988
+ $this->get_text_inline( 'Upgrade', 'upgrade' ),
18989
+ $this->get_upgrade_url(),
18990
+ false,
18991
+ 7,
18992
+ 'upgrade'
18993
+ );
18994
+ }
18995
+
18996
+ if ( $this->has_addons() ) {
18997
+ $this->add_plugin_action_link(
18998
+ $this->get_text_inline( 'Add-Ons', 'add-ons' ),
18999
+ $this->_get_admin_page_url( 'addons' ),
19000
+ false,
19001
+ 9,
19002
+ 'addons'
19003
+ );
19004
+ }
19005
+ }
19006
+
19007
+ /**
19008
+ * Adds "Activate License" or "Change License" link to the main Plugins page link actions collection.
19009
+ *
19010
+ * @author Leo Fajardo (@leorw)
19011
+ * @since 1.1.9
19012
+ */
19013
+ function _add_license_action_link() {
19014
+ $this->_logger->entrance();
19015
+
19016
+ if ( ! self::is_ajax() ) {
19017
+ // Inject license activation dialog UI and client side code.
19018
+ add_action( 'admin_footer', array( &$this, '_add_license_activation_dialog_box' ) );
19019
+ }
19020
+
19021
+ $link_text = $this->is_free_plan() ?
19022
+ $this->get_text_inline( 'Activate License', 'activate-license' ) :
19023
+ $this->get_text_inline( 'Change License', 'change-license' );
19024
+
19025
+ $this->add_plugin_action_link(
19026
+ $link_text,
19027
+ '#',
19028
+ false,
19029
+ 11,
19030
+ ( 'activate-license ' . $this->get_unique_affix() )
19031
+ );
19032
+ }
19033
+
19034
+ /**
19035
+ * @author Leo Fajardo (@leorw)
19036
+ * @since 2.0.2
19037
+ */
19038
+ function _add_premium_version_upgrade_selection_action() {
19039
+ $this->_logger->entrance();
19040
+
19041
+ if ( ! self::is_ajax() ) {
19042
+ add_action( 'admin_footer', array( &$this, '_add_premium_version_upgrade_selection_dialog_box' ) );
19043
+ }
19044
+ }
19045
+
19046
+ /**
19047
+ * Adds "Opt in" or "Opt out" link to the main "Plugins" page link actions collection.
19048
+ *
19049
+ * @author Leo Fajardo (@leorw)
19050
+ * @since 1.2.1.5
19051
+ */
19052
+ function _add_tracking_links() {
19053
+ if ( ! current_user_can( 'manage_options' ) ) {
19054
+ return;
19055
+ }
19056
+
19057
+ $this->_logger->entrance();
19058
+
19059
+ /**
19060
+ * If the activation has been delegated to site admins, no tracking-related actions for now.
19061
+ *
19062
+ * @author Leo Fajardo (@leorw)
19063
+ */
19064
+ if ( $this->_is_network_active && $this->is_network_delegated_connection() ) {
19065
+ return;
19066
+ }
19067
+
19068
+ if ( fs_request_is_action_secure( $this->get_unique_affix() . '_reconnect' ) ) {
19069
+ if ( ! $this->is_registered() && $this->is_anonymous() ) {
19070
+ $this->connect_again();
19071
+
19072
+ return;
19073
+ }
19074
+ }
19075
+
19076
+ if ( ( $this->is_plugin() && ! self::is_plugins_page() ) ||
19077
+ ( $this->is_theme() && ! self::is_themes_page() )
19078
+ ) {
19079
+ // Only show tracking links on the plugins and themes pages.
19080
+ return;
19081
+ }
19082
+
19083
+ if ( ! $this->is_enable_anonymous() ) {
19084
+ // Don't allow to opt-out if anonymous mode is disabled.
19085
+ return;
19086
+ }
19087
+
19088
+ if ( ! $this->is_free_plan() ) {
19089
+ // Don't allow to opt-out if running in paid plan.
19090
+ return;
19091
+ }
19092
+
19093
+ if ( $this->add_ajax_action( 'stop_tracking', array( &$this, '_stop_tracking_callback' ) ) ) {
19094
+ return;
19095
+ }
19096
+
19097
+ if ( $this->add_ajax_action( 'allow_tracking', array( &$this, '_allow_tracking_callback' ) ) ) {
19098
+ return;
19099
+ }
19100
+
19101
+ $url = '#';
19102
+
19103
+ if ( $this->is_registered() ) {
19104
+ if ( $this->is_tracking_allowed() ) {
19105
+ $link_text_id = $this->get_text_inline( 'Opt Out', 'opt-out' );
19106
+ } else {
19107
+ $link_text_id = $this->get_text_inline( 'Opt In', 'opt-in' );
19108
+ }
19109
+
19110
+ add_action( 'admin_footer', array( &$this, '_add_optout_dialog' ) );
19111
+ } else {
19112
+ $link_text_id = $this->get_text_inline( 'Opt In', 'opt-in' );
19113
+
19114
+ $params = ! $this->is_anonymous() ?
19115
+ array() :
19116
+ array(
19117
+ 'nonce' => wp_create_nonce( $this->get_unique_affix() . '_reconnect' ),
19118
+ 'fs_action' => ( $this->get_unique_affix() . '_reconnect' ),
19119
+ );
19120
+
19121
+ $url = $this->get_activation_url( $params );
19122
+ }
19123
+
19124
+ if ( $this->is_plugin() && self::is_plugins_page() ) {
19125
+ $this->add_plugin_action_link(
19126
+ $link_text_id,
19127
+ $url,
19128
+ false,
19129
+ 13,
19130
+ "opt-in-or-opt-out {$this->_slug}"
19131
+ );
19132
+ }
19133
+ }
19134
+
19135
+ /**
19136
+ * Get the URL of the page that should be loaded right after the plugin activation.
19137
+ *
19138
+ * @author Vova Feldman (@svovaf)
19139
+ * @since 1.1.7.4
19140
+ *
19141
+ * @return string
19142
+ */
19143
+ function get_after_plugin_activation_redirect_url() {
19144
+ $url = false;
19145
+
19146
+ if ( ! $this->is_addon() || ! $this->has_free_plan() ) {
19147
+ $first_time_path = $this->_menu->get_first_time_path();
19148
+ $url = $this->is_activation_mode() ?
19149
+ $this->get_activation_url() :
19150
+ ( empty( $first_time_path ) ?
19151
+ $this->_get_admin_page_url() :
19152
+ $first_time_path );
19153
+ } else {
19154
+ $plugin_fs = false;
19155
+
19156
+ if ( $this->is_parent_plugin_installed() ) {
19157
+ $plugin_fs = self::get_parent_instance();
19158
+ }
19159
+
19160
+ if ( is_object( $plugin_fs ) ) {
19161
+ if ( ! $plugin_fs->is_registered() ) {
19162
+ // Forward to parent plugin connect when parent not registered.
19163
+ $url = $plugin_fs->get_activation_url();
19164
+ } else {
19165
+ // Forward to account page.
19166
+ $url = $plugin_fs->_get_admin_page_url( 'account' );
19167
+ }
19168
+ }
19169
+ }
19170
+
19171
+ return $url;
19172
+ }
19173
+
19174
+ /**
19175
+ * Forward page to activation page.
19176
+ *
19177
+ * @author Vova Feldman (@svovaf)
19178
+ * @since 1.0.3
19179
+ */
19180
+ function _redirect_on_activation_hook() {
19181
+ $url = $this->get_after_plugin_activation_redirect_url();
19182
+
19183
+ if ( is_string( $url ) ) {
19184
+ fs_redirect( $url );
19185
+ }
19186
+ }
19187
+
19188
+ /**
19189
+ * Modify plugin's page action links collection.
19190
+ *
19191
+ * @author Vova Feldman (@svovaf)
19192
+ * @since 1.0.0
19193
+ *
19194
+ * @param array $links
19195
+ * @param $file
19196
+ *
19197
+ * @return array
19198
+ */
19199
+ function _modify_plugin_action_links_hook( $links, $file ) {
19200
+ $this->_logger->entrance();
19201
+
19202
+ $passed_deactivate = false;
19203
+ $deactivate_link = '';
19204
+ $before_deactivate = array();
19205
+ $after_deactivate = array();
19206
+ foreach ( $links as $key => $link ) {
19207
+ if ( 'deactivate' === $key ) {
19208
+ $deactivate_link = $link;
19209
+ $passed_deactivate = true;
19210
+ continue;
19211
+ }
19212
+
19213
+ if ( ! $passed_deactivate ) {
19214
+ $before_deactivate[ $key ] = $link;
19215
+ } else {
19216
+ $after_deactivate[ $key ] = $link;
19217
+ }
19218
+ }
19219
+
19220
+ ksort( $this->_action_links );
19221
+
19222
+ foreach ( $this->_action_links as $new_links ) {
19223
+ foreach ( $new_links as $link ) {
19224
+ $before_deactivate[ $link['key'] ] = '<a href="' . $link['href'] . '"' . ( $link['external'] ? ' target="_blank"' : '' ) . '>' . $link['label'] . '</a>';
19225
+ }
19226
+ }
19227
+
19228
+ if ( ! empty( $deactivate_link ) ) {
19229
+ /**
19230
+ * This HTML element is used to identify the correct plugin when attaching an event to its Deactivate link.
19231
+ *
19232
+ * @since 1.2.1.6 Always show the deactivation feedback form since we added automatic free version deactivation upon premium code activation.
19233
+ */
19234
+ $deactivate_link .= '<i class="fs-module-id" data-module-id="' . $this->_module_id . '"></i>';
19235
+
19236
+ // Append deactivation link.
19237
+ $before_deactivate['deactivate'] = $deactivate_link;
19238
+ }
19239
+
19240
+ return array_merge( $before_deactivate, $after_deactivate );
19241
+ }
19242
+
19243
+ /**
19244
+ * Adds admin message.
19245
+ *
19246
+ * @author Vova Feldman (@svovaf)
19247
+ * @since 1.0.4
19248
+ *
19249
+ * @param string $message
19250
+ * @param string $title
19251
+ * @param string $type
19252
+ */
19253
+ function add_admin_message( $message, $title = '', $type = 'success' ) {
19254
+ $this->_admin_notices->add( $message, $title, $type );
19255
+ }
19256
+
19257
+ /**
19258
+ * Adds sticky admin message.
19259
+ *
19260
+ * @author Vova Feldman (@svovaf)
19261
+ * @since 1.1.0
19262
+ *
19263
+ * @param string $message
19264
+ * @param string $id
19265
+ * @param string $title
19266
+ * @param string $type
19267
+ */
19268
+ function add_sticky_admin_message( $message, $id, $title = '', $type = 'success' ) {
19269
+ $this->_admin_notices->add_sticky( $message, $id, $title, $type );
19270
+ }
19271
+
19272
+ /**
19273
+ * Helper function that returns the final steps for the upgrade completion.
19274
+ *
19275
+ * If the module is already running the premium code, returns an empty string.
19276
+ *
19277
+ * @author Vova Feldman (@svovaf)
19278
+ * @since 1.2.1
19279
+ *
19280
+ * @param string $plan_title
19281
+ *
19282
+ * @return string
19283
+ */
19284
+ private function get_complete_upgrade_instructions( $plan_title = '' ) {
19285
+ $this->_logger->entrance();
19286
+
19287
+ $activate_license_string = $this->get_license_network_activation_notice();
19288
+
19289
+ if ( ! $this->has_premium_version() || $this->is_premium() ) {
19290
+ return '' . $activate_license_string;
19291
+ }
19292
+
19293
+ if ( empty( $plan_title ) ) {
19294
+ $plan_title = $this->get_plan_title();
19295
+ }
19296
+
19297
+ // @since 1.2.1.5 The free version is auto deactivated.
19298
+ $deactivation_step = version_compare( $this->version, '1.2.1.5', '<' ) ?
19299
+ ( '<li>' . $this->esc_html_inline( 'Deactivate the free version', 'deactivate-free-version' ) . '.</li>' ) :
19300
+ '';
19301
+
19302
+ return sprintf(
19303
+ ' %s: <ol><li>%s.</li>%s<li>%s (<a href="%s" target="_blank">%s</a>).</li></ol>',
19304
+ $this->get_text_inline( 'Please follow these steps to complete the upgrade', 'follow-steps-to-complete-upgrade' ),
19305
+ ( empty( $activate_license_string ) ? '' : $activate_license_string . '</li><li>' ) .
19306
+ $this->get_latest_download_link( sprintf(
19307
+ /* translators: %s: Plan title */
19308
+ $this->get_text_inline( 'Download the latest %s version', 'download-latest-x-version' ),
19309
+ $plan_title
19310
+ ) ),
19311
+ $deactivation_step,
19312
+ $this->get_text_inline( 'Upload and activate the downloaded version', 'upload-and-activate' ),
19313
+ '//bit.ly/upload-wp-' . $this->_module_type . 's',
19314
+ $this->get_text_inline( 'How to upload and activate?', 'howto-upload-activate' )
19315
+ );
19316
+ }
19317
+
19318
+ /**
19319
+ * @author Leo Fajardo (@leorw)
19320
+ * @since 2.1.0
19321
+ *
19322
+ * @param string $url
19323
+ * @param array $request
19324
+ */
19325
+ private static function enrich_request_for_debug( &$url, &$request ) {
19326
+ if ( WP_FS__DEBUG_SDK || isset( $_COOKIE['XDEBUG_SESSION'] ) ) {
19327
+ $url = add_query_arg( 'XDEBUG_SESSION_START', rand( 0, 9999999 ), $url );
19328
+ $url = add_query_arg( 'XDEBUG_SESSION', 'PHPSTORM', $url );
19329
+
19330
+ $request['cookies'] = array(
19331
+ new WP_Http_Cookie( array(
19332
+ 'name' => 'XDEBUG_SESSION',
19333
+ 'value' => 'PHPSTORM',
19334
+ ) )
19335
+ );
19336
+ }
19337
+ }
19338
+
19339
+ /**
19340
+ * @author Leo Fajardo (@leorw)
19341
+ * @since 2.1.0
19342
+ *
19343
+ * @param string $url
19344
+ * @param array $request
19345
+ * @param int $success_cache_expiration
19346
+ * @param int $failure_cache_expiration
19347
+ *
19348
+ * @return WP_Error|array
19349
+ */
19350
+ private static function safe_remote_post(
19351
+ &$url,
19352
+ $request,
19353
+ $success_cache_expiration = 0,
19354
+ $failure_cache_expiration = 0
19355
+ ) {
19356
+ $should_cache = ($success_cache_expiration + $failure_cache_expiration > 0);
19357
+
19358
+ $cache_key = $should_cache ? md5( fs_strip_url_protocol($url) . json_encode( $request ) ) : false;
19359
+
19360
+ $response = (!WP_FS__DEBUG_SDK && ( false !== $cache_key )) ?
19361
+ get_transient( $cache_key ) :
19362
+ false;
19363
+
19364
+ if ( false === $response ) {
19365
+ self::enrich_request_for_debug( $url, $request );
19366
+
19367
+ $response = wp_remote_post( $url, $request );
19368
+
19369
+ if ( $response instanceof WP_Error ) {
19370
+ if ( 'https://' === substr( $url, 0, 8 ) &&
19371
+ isset( $response->errors ) &&
19372
+ isset( $response->errors['http_request_failed'] )
19373
+ ) {
19374
+ $http_error = strtolower( $response->errors['http_request_failed'][0] );
19375
+
19376
+ if ( false !== strpos( $http_error, 'ssl' ) ||
19377
+ false !== strpos( $http_error, 'curl error 35' )
19378
+ ) {
19379
+ // Failed due to old version of cURL or Open SSL (SSLv3 is not supported by CloudFlare).
19380
+ $url = 'http://' . substr( $url, 8 );
19381
+
19382
+ $request['timeout'] = 15;
19383
+ $response = wp_remote_post( $url, $request );
19384
+ }
19385
+ }
19386
+ }
19387
+
19388
+ if ( false !== $cache_key ) {
19389
+ set_transient(
19390
+ $cache_key,
19391
+ $response,
19392
+ ( ( $response instanceof WP_Error ) ?
19393
+ $failure_cache_expiration :
19394
+ $success_cache_expiration )
19395
+ );
19396
+ }
19397
+ }
19398
+
19399
+ return $response;
19400
+ }
19401
+
19402
+ /**
19403
+ * This method is used to enrich the after upgrade notice instructions when the upgraded
19404
+ * license cannot be activated network wide (license quota isn't large enough).
19405
+ *
19406
+ * @author Vova Feldman (@svovaf)
19407
+ * @since 2.0.0
19408
+ *
19409
+ * @return string
19410
+ */
19411
+ private function get_license_network_activation_notice() {
19412
+ if ( ! $this->_is_network_active ) {
19413
+ // Module isn't network level activated.
19414
+ return '';
19415
+ }
19416
 
19417
+ if ( ! fs_is_network_admin() ) {
19418
+ // Not network level admin.
19419
+ return '';
19420
+ }
19421
 
19422
+ if ( get_blog_count() == 1 ) {
19423
+ // There's only a single site in the network so if there's a context license it was already activated.
19424
+ return '';
19425
+ }
19426
 
19427
+ if ( ! is_object( $this->_license ) ) {
19428
+ // No context license.
19429
+ return '';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19430
  }
19431
 
19432
+ if ( $this->_license->is_single_site() && 0 < $this->_license->activated ) {
19433
+ // License was already utilized (this is not 100% the case if all the network is localhost sites and the license can be utilized on unlimited localhost sites).
19434
+ return '';
19435
+ }
19436
+
19437
+ if ( $this->can_activate_license_on_network( $this->_license ) ) {
19438
+ // License can be activated on all the network, so probably, the license is already activate on all the network (that's how the after upgrade sync works).
19439
+ return '';
19440
+ }
19441
+
19442
+ return sprintf(
19443
+ $this->get_text_inline( '%sClick here%s to choose the sites where you\'d like to activate the license on.', 'network-choose-sites-for-license' ),
19444
+ '<a href="' . $this->get_account_url( false, array( 'activate_license' => 'true' ) ) . '">',
19445
+ '</a>'
19446
+ );
19447
+ }
19448
+
19449
+ /**
19450
+ * @author Vova Feldman (@svovaf)
19451
+ * @since 1.2.1.7
19452
+ *
19453
+ * @param string $key
19454
+ *
19455
+ * @return string
19456
+ */
19457
+ function get_text( $key ) {
19458
+ return fs_text( $key, $this->_slug );
19459
+ }
19460
+
19461
+ /**
19462
+ * @author Vova Feldman (@svovaf)
19463
+ * @since 1.2.3
19464
+ *
19465
+ * @param string $text Translatable string.
19466
+ * @param string $key String key for overrides.
19467
+ *
19468
+ * @return string
19469
+ */
19470
+ function get_text_inline( $text, $key = '' ) {
19471
+ return _fs_text_inline( $text, $key, $this->_slug );
19472
+ }
19473
+
19474
+ /**
19475
+ * @author Vova Feldman (@svovaf)
19476
+ * @since 1.2.3
19477
+ *
19478
+ * @param string $text Translatable string.
19479
+ * @param string $context Context information for the translators.
19480
+ * @param string $key String key for overrides.
19481
+ *
19482
+ * @return string
19483
+ */
19484
+ function get_text_x_inline( $text, $context, $key ) {
19485
+ return _fs_text_x_inline( $text, $context, $key, $this->_slug );
19486
+ }
19487
+
19488
+ /**
19489
+ * @author Vova Feldman (@svovaf)
19490
+ * @since 1.2.3
19491
+ *
19492
+ * @param string $text Translatable string.
19493
+ * @param string $key String key for overrides.
19494
+ *
19495
+ * @return string
19496
+ */
19497
+ function esc_html_inline( $text, $key ) {
19498
+ return esc_html( _fs_text_inline( $text, $key, $this->_slug ) );
19499
+ }
19500
+
19501
+ #----------------------------------------------------------------------------------
19502
+ #region Versioning
19503
+ #----------------------------------------------------------------------------------
19504
+
19505
+ /**
19506
+ * Check if Freemius in SDK upgrade mode.
19507
+ *
19508
+ * @author Vova Feldman (@svovaf)
19509
+ * @since 1.0.9
19510
+ *
19511
+ * @return bool
19512
+ */
19513
+ function is_sdk_upgrade_mode() {
19514
+ return isset( $this->_storage->sdk_upgrade_mode ) ?
19515
+ $this->_storage->sdk_upgrade_mode :
19516
+ false;
19517
+ }
19518
+
19519
+ /**
19520
+ * Turn SDK upgrade mode off.
19521
+ *
19522
+ * @author Vova Feldman (@svovaf)
19523
+ * @since 1.0.9
19524
+ */
19525
+ function set_sdk_upgrade_complete() {
19526
+ $this->_storage->sdk_upgrade_mode = false;
19527
+ }
19528
+
19529
+ /**
19530
+ * Check if plugin upgrade mode.
19531
+ *
19532
+ * @author Vova Feldman (@svovaf)
19533
+ * @since 1.0.9
19534
+ *
19535
+ * @return bool
19536
+ */
19537
+ function is_plugin_upgrade_mode() {
19538
+ return isset( $this->_storage->plugin_upgrade_mode ) ?
19539
+ $this->_storage->plugin_upgrade_mode :
19540
+ false;
19541
+ }
19542
+
19543
+ /**
19544
+ * Turn plugin upgrade mode off.
19545
+ *
19546
+ * @author Vova Feldman (@svovaf)
19547
+ * @since 1.0.9
19548
+ */
19549
+ function set_plugin_upgrade_complete() {
19550
+ $this->_storage->plugin_upgrade_mode = false;
19551
+ }
19552
+
19553
+ #endregion
19554
+
19555
+ #----------------------------------------------------------------------------------
19556
+ #region Permissions
19557
+ #----------------------------------------------------------------------------------
19558
+
19559
+ /**
19560
+ * Check if specific permission requested.
19561
+ *
19562
+ * @author Vova Feldman (@svovaf)
19563
+ * @since 1.1.6
19564
+ *
19565
+ * @param string $permission
19566
+ *
19567
+ * @return bool
19568
+ */
19569
+ function is_permission_requested( $permission ) {
19570
+ return isset( $this->_permissions[ $permission ] ) && ( true === $this->_permissions[ $permission ] );
19571
+ }
19572
+
19573
+ #endregion
19574
+
19575
+ #----------------------------------------------------------------------------------
19576
+ #region Auto Activation
19577
+ #----------------------------------------------------------------------------------
19578
+
19579
+ /**
19580
+ * Hints the SDK if running an auto-installation.
19581
+ *
19582
+ * @var bool
19583
+ */
19584
+ private $_isAutoInstall = false;
19585
+
19586
+ /**
19587
+ * After upgrade callback to install and auto activate a plugin.
19588
+ * This code will only be executed on explicit request from the user,
19589
+ * following the practice Jetpack are using with their theme installations.
19590
+ *
19591
+ * @link https://make.wordpress.org/plugins/2017/03/16/clarification-of-guideline-8-executable-code-and-installs/
19592
+ *
19593
+ * @author Vova Feldman (@svovaf)
19594
+ * @since 1.2.1.7
19595
+ */
19596
+ function _install_premium_version_ajax_action() {
19597
+ $this->_logger->entrance();
19598
+
19599
+ $this->check_ajax_referer( 'install_premium_version' );
19600
+
19601
+ if ( ! $this->is_registered() ) {
19602
+ // Not registered.
19603
+ self::shoot_ajax_failure( array(
19604
+ 'message' => $this->get_text_inline( 'Auto installation only works for opted-in users.', 'auto-install-error-not-opted-in' ),
19605
+ 'code' => 'premium_installed',
19606
+ ) );
19607
+ }
19608
+
19609
+ $plugin_id = fs_request_get( 'target_module_id', $this->get_id() );
19610
+
19611
+ if ( ! FS_Plugin::is_valid_id( $plugin_id ) ) {
19612
+ // Invalid ID.
19613
+ self::shoot_ajax_failure( array(
19614
+ 'message' => $this->get_text_inline( 'Invalid module ID.', 'auto-install-error-invalid-id' ),
19615
+ 'code' => 'invalid_module_id',
19616
+ ) );
19617
+ }
19618
+
19619
+ if ( $plugin_id == $this->get_id() ) {
19620
+ if ( $this->is_premium() ) {
19621
+ // Already using the premium code version.
19622
+ self::shoot_ajax_failure( array(
19623
+ 'message' => $this->get_text_inline( 'Premium version already active.', 'auto-install-error-premium-activated' ),
19624
+ 'code' => 'premium_installed',
19625
+ ) );
19626
+ }
19627
+ if ( ! $this->can_use_premium_code() ) {
19628
+ // Don't have access to the premium code.
19629
+ self::shoot_ajax_failure( array(
19630
+ 'message' => $this->get_text_inline( 'You do not have a valid license to access the premium version.', 'auto-install-error-invalid-license' ),
19631
+ 'code' => 'invalid_license',
19632
+ ) );
19633
+ }
19634
+ if ( ! $this->has_release_on_freemius() ) {
19635
+ // Plugin is a serviceware, no premium code version.
19636
+ self::shoot_ajax_failure( array(
19637
+ 'message' => $this->get_text_inline( 'Plugin is a "Serviceware" which means it does not have a premium code version.', 'auto-install-error-serviceware' ),
19638
+ 'code' => 'premium_version_missing',
19639
+ ) );
19640
+ }
19641
+ } else {
19642
+ $addon = $this->get_addon( $plugin_id );
19643
+
19644
+ if ( ! is_object( $addon ) ) {
19645
+ // Invalid add-on ID.
19646
+ self::shoot_ajax_failure( array(
19647
+ 'message' => $this->get_text_inline( 'Invalid module ID.', 'auto-install-error-invalid-id' ),
19648
+ 'code' => 'invalid_module_id',
19649
+ ) );
19650
+ }
19651
+
19652
+ if ( $this->is_addon_activated( $plugin_id, true ) ) {
19653
+ // Premium add-on version is already activated.
19654
+ self::shoot_ajax_failure( array(
19655
+ 'message' => $this->get_text_inline( 'Premium add-on version already installed.', 'auto-install-error-premium-addon-activated' ),
19656
+ 'code' => 'premium_installed',
19657
+ ) );
19658
+ }
19659
+ }
19660
+
19661
+ $this->_isAutoInstall = true;
19662
+
19663
+ // Try to install and activate.
19664
+ $updater = FS_Plugin_Updater::instance( $this );
19665
+ $result = $updater->install_and_activate_plugin( $plugin_id );
19666
+
19667
+ if ( is_array( $result ) && ! empty( $result['message'] ) ) {
19668
+ self::shoot_ajax_failure( array(
19669
+ 'message' => $result['message'],
19670
+ 'code' => $result['code'],
19671
+ ) );
19672
+ }
19673
+
19674
+ self::shoot_ajax_success( $result );
19675
+ }
19676
+
19677
+ /**
19678
+ * Displays module activation dialog box after a successful upgrade
19679
+ * where the user explicitly requested to auto download and install
19680
+ * the premium version.
19681
+ *
19682
+ * @author Vova Feldman (@svovaf)
19683
+ * @since 1.2.1.7
19684
+ */
19685
+ function _add_auto_installation_dialog_box() {
19686
+ $this->_logger->entrance();
19687
+
19688
+ if ( ! $this->is_registered() ) {
19689
+ // Not registered.
19690
+ return;
19691
+ }
19692
+
19693
+ $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
19694
+
19695
+ if ( ! FS_Plugin::is_valid_id( $plugin_id ) ) {
19696
+ // Invalid module ID.
19697
+ return;
19698
+ }
19699
+
19700
+ if ( $plugin_id == $this->get_id() ) {
19701
+ if ( $this->is_premium() ) {
19702
+ // Already using the premium code version.
19703
+ return;
19704
+ }
19705
+ if ( ! $this->can_use_premium_code() ) {
19706
+ // Don't have access to the premium code.
19707
+ return;
19708
+ }
19709
+ if ( ! $this->has_release_on_freemius() ) {
19710
+ // Plugin is a serviceware, no premium code version.
19711
+ return;
19712
+ }
19713
+ } else {
19714
+ $addon = $this->get_addon( $plugin_id );
19715
+
19716
+ if ( ! is_object( $addon ) ) {
19717
+ // Invalid add-on ID.
19718
+ return;
19719
+ }
19720
+
19721
+ if ( $this->is_addon_activated( $plugin_id, true ) ) {
19722
+ // Premium add-on version is already activated.
19723
+ return;
19724
+ }
19725
+ }
19726
+
19727
+ $vars = array(
19728
+ 'id' => $this->_module_id,
19729
+ 'target_module_id' => $plugin_id,
19730
+ 'slug' => $this->_slug,
19731
+ );
19732
+
19733
+ fs_require_template( 'auto-installation.php', $vars );
19734
+ }
19735
+
19736
+ #endregion
19737
+
19738
+ #--------------------------------------------------------------------------------
19739
+ #region Tabs Integration
19740
+ #--------------------------------------------------------------------------------
19741
+
19742
+ #region Module's Original Tabs
19743
+
19744
+ /**
19745
+ * Inject a JavaScript logic to capture the theme tabs HTML.
19746
+ *
19747
+ * @author Vova Feldman (@svovaf)
19748
+ * @since 1.2.2.7
19749
+ */
19750
+ function _tabs_capture() {
19751
+ $this->_logger->entrance();
19752
+
19753
+ if ( ! $this->is_theme_settings_page() ||
19754
+ ! $this->is_matching_url( $this->main_menu_url() )
19755
+ ) {
19756
+ return;
19757
+ }
19758
+
19759
+ $params = array(
19760
+ 'id' => $this->_module_id,
19761
+ );
19762
+
19763
+ fs_require_once_template( 'tabs-capture-js.php', $params );
19764
+ }
19765
+
19766
+ /**
19767
+ * Cache theme's tabs HTML for a week. The cache will also be set as expired
19768
+ * after version and type (free/premium) changes, in addition to the week period.
19769
+ *
19770
+ * @author Vova Feldman (@svovaf)
19771
+ * @since 1.2.2.7
19772
+ */
19773
+ function _store_tabs_ajax_action() {
19774
+ $this->_logger->entrance();
19775
+
19776
+ $this->check_ajax_referer( 'store_tabs' );
19777
+
19778
+ // Init filesystem if not yet initiated.
19779
+ WP_Filesystem();
19780
+
19781
+ // Get POST body HTML data.
19782
+ global $wp_filesystem;
19783
+ $tabs_html = $wp_filesystem->get_contents( "php://input" );
19784
+
19785
+ if ( is_string( $tabs_html ) ) {
19786
+ $tabs_html = trim( $tabs_html );
19787
+ }
19788
+
19789
+ if ( ! is_string( $tabs_html ) || empty( $tabs_html ) ) {
19790
+ self::shoot_ajax_failure();
19791
+ }
19792
+
19793
+ $this->_cache->set( 'tabs', $tabs_html, 7 * WP_FS__TIME_24_HOURS_IN_SEC );
19794
+
19795
+ self::shoot_ajax_success();
19796
+ }
19797
+
19798
+ /**
19799
+ * Cache theme's settings page custom styles. The cache will also be set as expired
19800
+ * after version and type (free/premium) changes, in addition to the week period.
19801
+ *
19802
+ * @author Vova Feldman (@svovaf)
19803
+ * @since 1.2.2.7
19804
+ */
19805
+ function _store_tabs_styles() {
19806
+ $this->_logger->entrance();
19807
+
19808
+ if ( ! $this->is_theme_settings_page() ||
19809
+ ! $this->is_matching_url( $this->main_menu_url() )
19810
+ ) {
19811
+ return;
19812
+ }
19813
+
19814
+ $wp_styles = wp_styles();
19815
+
19816
+ $theme_styles_url = get_template_directory_uri();
19817
+
19818
+ $stylesheets = array();
19819
+ foreach ( $wp_styles->queue as $handler ) {
19820
+ if ( fs_starts_with( $handler, 'fs_' ) ) {
19821
+ // Assume that stylesheets that their handler starts with "fs_" belong to the SDK.
19822
+ continue;
19823
+ }
19824
+
19825
+ /**
19826
+ * @var _WP_Dependency $stylesheet
19827
+ */
19828
+ $stylesheet = $wp_styles->registered[ $handler ];
19829
+
19830
+ if ( fs_starts_with( $stylesheet->src, $theme_styles_url ) ) {
19831
+ $stylesheets[] = $stylesheet->src;
19832
+ }
19833
+ }
19834
+
19835
+ if ( ! empty( $stylesheets ) ) {
19836
+ $this->_cache->set( 'tabs_stylesheets', $stylesheets, 7 * WP_FS__TIME_24_HOURS_IN_SEC );
19837
+ }
19838
+ }
19839
+
19840
+ /**
19841
+ * Check if module's original settings page has any tabs.
19842
+ *
19843
+ * @author Vova Feldman (@svovaf)
19844
+ * @since 1.2.2.7
19845
+ *
19846
+ * @return bool
19847
+ */
19848
+ private function has_tabs() {
19849
+ return $this->_cache->has( 'tabs' );
19850
+ }
19851
+
19852
+ /**
19853
+ * Get module's settings page HTML content, starting
19854
+ * from the beginning of the <div class="wrap"> element,
19855
+ * until the tabs HTML (including).
19856
+ *
19857
+ * @author Vova Feldman (@svovaf)
19858
+ * @since 1.2.2.7
19859
+ *
19860
+ * @return string
19861
+ */
19862
+ private function get_tabs_html() {
19863
+ $this->_logger->entrance();
19864
+
19865
+ return $this->_cache->get( 'tabs' );
19866
+ }
19867
+
19868
+ /**
19869
+ * Check if page should include tabs.
19870
+ *
19871
+ * @author Vova Feldman (@svovaf)
19872
+ * @since 1.2.2.7
19873
+ *
19874
+ * @return bool
19875
+ */
19876
+ private function should_page_include_tabs() {
19877
+ if ( ! $this->has_settings_menu() ) {
19878
+ // Don't add tabs if no settings at all.
19879
+ return false;
19880
+ }
19881
+
19882
+ if ( ! $this->is_theme() ) {
19883
+ // Only add tabs to themes for now.
19884
+ return false;
19885
+ }
19886
+
19887
+ if ( ! $this->has_paid_plan() && ! $this->has_addons() ) {
19888
+ // Only add tabs to monetizing themes.
19889
+ return false;
19890
+ }
19891
+
19892
+ if ( ! $this->is_theme_settings_page() ) {
19893
+ // Only add tabs if browsing one of the theme's setting pages.
19894
+ return false;
19895
+ }
19896
+
19897
+ if ( $this->is_admin_page( 'pricing' ) && fs_request_get_bool( 'checkout' ) ) {
19898
+ // Don't add tabs on checkout page, we want to reduce distractions
19899
+ // as much as possible.
19900
+ return false;
19901
+ }
19902
+
19903
+ return true;
19904
+ }
19905
+
19906
+ /**
19907
+ * Add the tabs HTML before the setting's page content and
19908
+ * enqueue any required stylesheets.
19909
+ *
19910
+ * @author Vova Feldman (@svovaf)
19911
+ * @since 1.2.2.7
19912
+ *
19913
+ * @return bool If tabs were included.
19914
+ */
19915
+ function _add_tabs_before_content() {
19916
+ $this->_logger->entrance();
19917
+
19918
+ if ( ! $this->should_page_include_tabs() ) {
19919
+ return false;
19920
+ }
19921
+
19922
+ /**
19923
+ * Enqueue the original stylesheets that are included in the
19924
+ * theme settings page. That way, if the theme settings has
19925
+ * some custom _styled_ content above the tabs UI, this
19926
+ * will make sure that the styling is preserved.
19927
+ */
19928
+ $stylesheets = $this->_cache->get( 'tabs_stylesheets', array() );
19929
+ if ( is_array( $stylesheets ) ) {
19930
+ for ( $i = 0, $len = count( $stylesheets ); $i < $len; $i ++ ) {
19931
+ wp_enqueue_style( "fs_{$this->_module_id}_tabs_{$i}", $stylesheets[ $i ] );
19932
  }
19933
  }
19934
 
19935
+ // Cut closing </div> tag.
19936
+ echo substr( trim( $this->get_tabs_html() ), 0, - 6 );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19937
 
19938
+ return true;
19939
+ }
19940
 
19941
+ /**
19942
+ * Add the tabs closing HTML after the setting's page content.
19943
+ *
19944
+ * @author Vova Feldman (@svovaf)
19945
+ * @since 1.2.2.7
19946
+ *
19947
+ * @return bool If tabs closing HTML was included.
19948
+ */
19949
+ function _add_tabs_after_content() {
19950
+ $this->_logger->entrance();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19951
 
19952
+ if ( ! $this->should_page_include_tabs() ) {
19953
+ return false;
19954
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19955
 
19956
+ echo '</div>';
19957
 
19958
+ return true;
19959
+ }
 
 
19960
 
19961
+ #endregion
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19962
 
19963
+ /**
19964
+ * Add in-page JavaScript to inject the Freemius tabs into
19965
+ * the module's setting tabs section.
19966
+ *
19967
+ * @author Vova Feldman (@svovaf)
19968
+ * @since 1.2.2.7
19969
+ */
19970
+ function _add_freemius_tabs() {
19971
+ $this->_logger->entrance();
19972
 
19973
+ if ( ! $this->should_page_include_tabs() ) {
19974
+ return;
19975
+ }
19976
 
19977
+ $params = array( 'id' => $this->_module_id );
19978
+ fs_require_once_template( 'tabs.php', $params );
19979
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19980
 
19981
+ #endregion
 
 
 
 
 
 
 
 
 
19982
 
19983
+ #--------------------------------------------------------------------------------
19984
+ #region Customizer Integration for Themes
19985
+ #--------------------------------------------------------------------------------
19986
 
19987
+ /**
19988
+ * @author Vova Feldman (@svovaf)
19989
+ * @since 1.2.2.7
19990
+ *
19991
+ * @param WP_Customize_Manager $customizer
19992
+ */
19993
+ function _customizer_register( $customizer ) {
19994
+ $this->_logger->entrance();
19995
 
19996
+ if ( $this->is_pricing_page_visible() ) {
19997
+ require_once WP_FS__DIR_INCLUDES . '/customizer/class-fs-customizer-upsell-control.php';
19998
+
19999
+ $customizer->add_section( 'freemius_upsell', array(
20000
+ 'title' => '&#9733; ' . $this->get_text_inline( 'View paid features', 'view-paid-features' ),
20001
+ 'priority' => 1,
20002
+ ) );
20003
+ $customizer->add_setting( 'freemius_upsell', array(
20004
+ 'sanitize_callback' => 'esc_html',
20005
+ ) );
20006
+
20007
+ $customizer->add_control( new FS_Customizer_Upsell_Control( $customizer, 'freemius_upsell', array(
20008
+ 'fs' => $this,
20009
+ 'section' => 'freemius_upsell',
20010
+ 'priority' => 100,
20011
+ ) ) );
20012
+ }
20013
 
20014
+ if ( $this->is_page_visible( 'contact' ) || $this->is_page_visible( 'support' ) ) {
20015
+ require_once WP_FS__DIR_INCLUDES . '/customizer/class-fs-customizer-support-section.php';
20016
 
20017
+ // Main Documentation Link In Customizer Root.
20018
+ $customizer->add_section( new FS_Customizer_Support_Section( $customizer, 'freemius_support', array(
20019
+ 'fs' => $this,
20020
+ 'priority' => 1000,
20021
+ ) ) );
20022
+ }
20023
+ }
20024
 
20025
+ #endregion
20026
 
20027
+ /**
20028
+ * If the theme has a paid version, add some custom
20029
+ * styling to the theme's premium version (if exists)
20030
+ * to highlight that it's the premium version of the
20031
+ * same theme, making it easier for identification
20032
+ * after the user upgrades and upload it to the site.
20033
+ *
20034
+ * @author Vova Feldman (@svovaf)
20035
+ * @since 1.2.2.7
20036
+ */
20037
+ function _style_premium_theme() {
20038
+ $this->_logger->entrance();
20039
 
20040
+ if ( ! self::is_themes_page() ) {
20041
+ // Only include in the themes page.
20042
+ return;
20043
+ }
20044
 
20045
+ if ( ! $this->has_paid_plan() ) {
20046
+ // Only include if has any paid plans.
20047
+ return;
20048
+ }
20049