Asset CleanUp: Page Speed Booster - Version 1.2.8.6

Version Description

  • Better support for WordPress 5.0 when updating a post/page within the Dashboard
  • On new plugin installations, "Hide WordPress Core Files From The Assets List?" is enabled by default
  • Added option for users to opt in to security and feature updates notifications, and non-sensitive diagnostic tracking
  • Added "Tools" page which allows you to reset all settings or reset everything
  • Bug Fix: Notice error was printing when there was no source file for specific handles that are loading inline code (e.g. woocommerce-inline)
Download this release

Release Info

Developer gabelivan
Plugin Icon 128x128 Asset CleanUp: Page Speed Booster
Version 1.2.8.6
Comparing to
See all releases

Code changes from version 1.2.8.5 to 1.2.8.6

assets/script.min.js CHANGED
@@ -1 +1 @@
1
- jQuery(document).ready(function(a){setTimeout(function(){jQuery("a[href*='admin.php?page=wpassetcleanup_feature_request']").attr("target","_blank")},1e3),a("#wpacu-mark-license-valid-button").click(function(){return confirm(wpacu_object.mark_license_valid_confirm)});var b={load:function(){b.getExternalFileSize();var c;a(".input-unload-on-this-page").on("click change",function(){a(this).prop("checked")?a(this).closest("tr").addClass("wpacu_not_load"):a(this).closest("tr").removeClass("wpacu_not_load")}),a(".wpacu_global_unload").click(function(){c=a(this).attr("data-handle"),a(this).prop("checked")?a(this).parent("label").addClass("wpacu_global_checked"):a(this).parent("label").removeClass("wpacu_global_checked")}),a(".wpacu_keep_bulk_rule").click(function(){a(this).prop("checked")&&a(this).parents("li").next().removeClass("remove_rule")}),a(".wpacu_remove_bulk_rule").click(function(){a(this).prop("checked")&&a(this).parents("li").addClass("remove_rule")}),a(".wpacu_bulk_unload").click(function(){a(this).prop("checked")?a(this).parent("label").addClass("wpacu_bulk_unload_active"):a(this).parent("label").removeClass("wpacu_bulk_unload_active")}),a(".wpacu_load_it_option").click(function(){var b=a(this).attr("data-handle");if(a(this).prop("checked")){a(this).parent("label").addClass("wpacu_global_unload_exception");var c="";a(this).hasClass("wpacu_style")?c="style":a(this).hasClass("wpacu_script")&&(c="script"),a("#"+c+"_"+b).prop("checked",!1).trigger("change")}else a(this).parent("label").removeClass("wpacu_global_unload_exception")})},getExternalFileSize:function(){a(".wpacu-external-file-size").each(function(){var b=a(this);a.post(wpacu_object.ajax_url,{action:"get_external_file_size",wpacu_remote_file:a(this).attr("data-src")},function(a){b.html(a)})})}};if(a("#wpacu_post_type_select").change(function(){a("#wpacu_post_type_form").submit()}),a("#wpacu_taxonomy_select").change(function(){a("#wpacu_taxonomy_form").submit()}),a("#wpacu_dashboard").click(function(){a(this).prop("checked")?a("#wpacu-settings-assets-retrieval-mode").fadeIn():a("#wpacu-settings-assets-retrieval-mode").fadeOut()}),a("#wpacu_disable_jquery_migrate").on("click",function(){return!a(this).is(":checked")||(!(!a(this).is(":checked")||!confirm(wpacu_object.jquery_migration_disable_confirm_msg))||(a(this).prop("checked",!1),!1))}),a("#wpacu_disable_comment_reply").on("click",function(){return!a(this).is(":checked")||(!(!a(this).is(":checked")||!confirm(wpacu_object.comment_reply_disable_confirm_msg))||(a(this).prop("checked",!1),!1))}),a(".wpacu_remove_rule").click(function(){var b=a(this).parents(".wpacu_global_rule_row");a(this).prop("checked")?b.addClass("selected"):b.removeClass("selected")}),a("#wpacu_wrap_assets").length>0&&b.load(),"undefined"==typeof wpacu_object||a("#wpacu_meta_box_content").length<1)return!1;var c={};"direct"===wpacu_object.dom_get_type?(c[wpacu_object.plugin_name+"_load"]=1,c[wpacu_object.plugin_name+"_time_r"]=(new Date).getTime(),a.ajax({method:"GET",url:wpacu_object.page_url,data:c,cache:!1,complete:function(b,c){if("error"===b.statusText){var d=wpacu_object.ajax_direct_fetch_error;d=d.replace(/\{wpacu_output\}/,b.responseText),d=d.replace(/\{wpacu_status_code_error\}/,b.status),a("#wpacu_meta_box_content").html(d)}}}).done(function(c){var d=c.substring(c.lastIndexOf(wpacu_object.start_del)+wpacu_object.start_del.length,c.lastIndexOf(wpacu_object.end_del)),e={action:wpacu_object.plugin_name+"_get_loaded_assets",wpacu_list:d,post_id:wpacu_object.post_id,page_url:wpacu_object.page_url,tag_id:wpacu_object.tag_id};if("btoa"in window)try{c=c.replace(/[\u0250-\ue007]/g,""),c=c.replace(/[^\x00-\x7F]/g,""),e.contents=window.btoa(c)}catch(a){e.contents=""}a.post(wpacu_object.ajax_url,e,function(c){if(!c)return!1;a("#wpacu_meta_box_content").html(c),a("#wpacu_home_page_form").length>0&&a("#submit").show(),b.load()})})):"wp_remote_post"===wpacu_object.dom_get_type&&(c={action:wpacu_object.plugin_name+"_get_loaded_assets",post_id:wpacu_object.post_id,page_url:wpacu_object.page_url,tag_id:wpacu_object.tag_id},a.post(wpacu_object.ajax_url,c,function(c){if(!c)return!1;a("#wpacu_meta_box_content").html(c),a("#wpacu_home_page_form").length>0&&a("#submit").show(),b.load()}))});
1
+ jQuery(document).ready(function(a){function b(){var b={};"direct"===wpacu_object.dom_get_type?(b[wpacu_object.plugin_name+"_load"]=1,b[wpacu_object.plugin_name+"_time_r"]=(new Date).getTime(),a.ajax({method:"GET",url:wpacu_object.page_url,data:b,cache:!1,complete:function(b,c){if("error"===b.statusText){var d=wpacu_object.ajax_direct_fetch_error;d=d.replace(/\{wpacu_output\}/,b.responseText),d=d.replace(/\{wpacu_status_code_error\}/,b.status),a("#wpacu_meta_box_content").html(d)}}}).done(function(b){var c=b.substring(b.lastIndexOf(wpacu_object.start_del)+wpacu_object.start_del.length,b.lastIndexOf(wpacu_object.end_del)),d={action:wpacu_object.plugin_name+"_get_loaded_assets",wpacu_list:c,post_id:wpacu_object.post_id,page_url:wpacu_object.page_url,tag_id:wpacu_object.tag_id,time_r:(new Date).getTime()};if("btoa"in window)try{b=b.replace(/[\u0250-\ue007]/g,""),b=b.replace(/[^\x00-\x7F]/g,""),d.contents=window.btoa(b)}catch(a){d.contents=""}a.post(wpacu_object.ajax_url,d,function(b){if(!b)return!1;a("#wpacu_meta_box_content").html(b),a("#wpacu_home_page_form").length>0&&a("#submit").show(),e.load()})})):"wp_remote_post"===wpacu_object.dom_get_type&&(b={action:wpacu_object.plugin_name+"_get_loaded_assets",post_id:wpacu_object.post_id,page_url:wpacu_object.page_url,tag_id:wpacu_object.tag_id,time_r:(new Date).getTime()},a.post(wpacu_object.ajax_url,b,function(b){if(!b)return!1;a("#wpacu_meta_box_content").html(b),a("#wpacu_home_page_form").length>0&&a("#submit").show(),e.load()}))}a("#wpacu-mark-license-valid-button").click(function(){return confirm(wpacu_object.mark_license_valid_confirm)});var c,d;a("#wpacu-reset-drop-down").on("change",function(){""===a(this).val()?(a("#wpacu-warning-read").removeClass("wpacu-visible"),a("#wpacu-reset-submit-btn").attr("disabled","disabled")):(a("#wpacu-warning-read").addClass("wpacu-visible"),a("#wpacu-reset-submit-btn").removeAttr("disabled")),a(".wpacu-tools-area .wpacu-warning").hide(),c=a(this).find("option:selected"),a("#"+c.attr("data-id")).fadeIn("fast")}),a("#wpacu-reset-submit-btn").on("click",function(){if("reset_settings"===a("#wpacu-reset-drop-down").val()?d=wpacu_object.reset_settings_confirm_msg:"reset_everything"===a("#wpacu-reset-drop-down").val()&&(d=wpacu_object.reset_everything_confirm_msg),!confirm(d))return!1;a("#wpacu-action-confirmed").val("yes"),setTimeout(function(){"yes"===a("#wpacu-action-confirmed").val()&&a("#wpacu-tools-form").submit()},1e3)});var e={load:function(){e.getExternalFileSize();var b;a(".input-unload-on-this-page").on("click change",function(){a(this).prop("checked")?a(this).closest("tr").addClass("wpacu_not_load"):a(this).closest("tr").removeClass("wpacu_not_load")}),a(".wpacu_global_unload").click(function(){b=a(this).attr("data-handle"),a(this).prop("checked")?a(this).parent("label").addClass("wpacu_global_checked"):a(this).parent("label").removeClass("wpacu_global_checked")}),a(".wpacu_keep_bulk_rule").click(function(){a(this).prop("checked")&&a(this).parents("li").next().removeClass("remove_rule")}),a(".wpacu_remove_bulk_rule").click(function(){a(this).prop("checked")&&a(this).parents("li").addClass("remove_rule")}),a(".wpacu_bulk_unload").click(function(){a(this).prop("checked")?a(this).parent("label").addClass("wpacu_bulk_unload_active"):a(this).parent("label").removeClass("wpacu_bulk_unload_active")}),a(".wpacu_load_it_option").click(function(){var b=a(this).attr("data-handle");if(a(this).prop("checked")){a(this).parent("label").addClass("wpacu_global_unload_exception");var c="";a(this).hasClass("wpacu_style")?c="style":a(this).hasClass("wpacu_script")&&(c="script"),a("#"+c+"_"+b).prop("checked",!1).trigger("change")}else a(this).parent("label").removeClass("wpacu_global_unload_exception")})},getExternalFileSize:function(){a(".wpacu-external-file-size").each(function(){var b=a(this);a.post(wpacu_object.ajax_url,{action:"get_external_file_size",wpacu_remote_file:a(this).attr("data-src")},function(a){b.html(a)})})}};if(a("#wpacu_post_type_select").change(function(){a("#wpacu_post_type_form").submit()}),a("#wpacu_taxonomy_select").change(function(){a("#wpacu_taxonomy_form").submit()}),a("#wpacu_dashboard").click(function(){a(this).prop("checked")?a("#wpacu-settings-assets-retrieval-mode").fadeIn():a("#wpacu-settings-assets-retrieval-mode").fadeOut()}),a("#wpacu_disable_jquery_migrate").on("click",function(){return!a(this).is(":checked")||(!(!a(this).is(":checked")||!confirm(wpacu_object.jquery_migration_disable_confirm_msg))||(a(this).prop("checked",!1),!1))}),a("#wpacu_disable_comment_reply").on("click",function(){return!a(this).is(":checked")||(!(!a(this).is(":checked")||!confirm(wpacu_object.comment_reply_disable_confirm_msg))||(a(this).prop("checked",!1),!1))}),a(".wpacu_remove_rule").click(function(){var b=a(this).parents(".wpacu_global_rule_row");a(this).prop("checked")?b.addClass("selected"):b.removeClass("selected")}),a("#wpacu_wrap_assets").length>0&&e.load(),"undefined"==typeof wpacu_object||a("#wpacu_meta_box_content").length<1)return!1;b(),a(document).on("click","body.branch-5 .edit-post-header__settings button.is-primary",function(){var c=function(){0===a(".edit-post-header__settings .is-saving").length&&(b(),clearInterval(d))},d=setInterval(c,900)})});
assets/style.min.css CHANGED
@@ -1 +1 @@
1
- .wpacu_update_btn,label.wpacu_add_global{cursor:pointer!important}.go-pro-link-no-style:not(.nav-tab),.wpacu-notice p{font-style:italic}#wpacu-pages-info-area{width:98%}#wpacu-logo-area{margin:15px 0;padding:0 10px}#wpacu-logo-area img{max-width:340px;width:100%}.wpacu-tabs{position:relative;overflow:hidden;margin:0 auto 20px;width:100%;font-weight:300;font-size:20px}.wpacu-tabs nav{text-align:center}.wpacu-tabs nav ul{position:relative;display:-ms-flexbox;display:-webkit-flex;display:-moz-flex;display:-ms-flex;display:flex;margin:0 auto;padding:0;max-width:1200px;list-style:none;-ms-box-orient:horizontal;-ms-box-pack:center;-webkit-flex-flow:row wrap;-moz-flex-flow:row wrap;-ms-flex-flow:row wrap;flex-flow:row wrap;-webkit-justify-content:center;-moz-justify-content:center;-ms-justify-content:center;justify-content:center}.wpacu-tabs nav ul li{position:relative;z-index:1;display:block;margin:0;text-align:center;-webkit-flex:1;-moz-flex:1;-ms-flex:1;flex:1}.wpacu-tabs nav ul li span.license-status{position:absolute;font-size:12px;left:0;right:50%;bottom:12px;-webkit-transform:translate(50%,50%);transform:translate(50%,50%)}.wpacu-tabs nav ul li span.license-status.inactive{color:#c00}.wpacu-tabs nav ul li span.license-status.active{color:green}.wpacu-tabs nav a{position:relative;display:block;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;line-height:2.5}.wpacu-tabs nav a span{vertical-align:middle;font-size:14px}.wpacu-tabs nav li.wpacu-tab-current a{color:#74777b}.wpacu-tabs nav a:focus{outline:0}.wpacu-tabs .dashicons{font-size:20px}.no-js .wpacu-content-wrap section{display:block;padding-bottom:2em;border-bottom:1px solid rgba(255,255,255,.6)}.no-flexbox nav ul{display:block}.no-flexbox nav ul li{min-width:15%;display:inline-block}@media screen and (max-width:58em){.wpacu-tabs nav a.icon span{display:none}.wpacu-tabs nav a:before{margin-right:0}}.wpacu-tabs-style-underline nav{background:#fff}.wpacu-tabs-style-underline nav a{color:#74777b;font-size:18px;text-transform:uppercase;text-decoration:none;padding:.25em 0 .5em;border-left:1px solid #e7ecea;-webkit-transition:color .2s;transition:color .2s}.wpacu-tabs-style-underline nav li:last-child a{border-right:1px solid #e7ecea}.wpacu-tabs-style-underline nav li a::after{position:absolute;bottom:0;left:0;width:100%;height:6px;background:#2CC185;content:'';-webkit-transition:-webkit-transform .3s;transition:transform .3s;-webkit-transform:translate3d(0,150%,0);transform:translate3d(0,150%,0)}.wpacu-tabs-style-underline nav li.wpacu-tab-current a::after{-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}.wpacu-tabs-style-underline nav a span{font-weight:700}.wpacu-tabs-style-topline{max-width:1200px;margin:20px auto 30px 0;width:100%;font-weight:300;text-decoration:none}.wpacu-tabs-style-topline nav li{border:1px solid rgba(40,44,42,.1)}.wpacu-tabs-style-topline nav li:not(:last-child){border-right:none}.wpacu-tabs-style-topline nav li.wpacu-tab-current{border-top-color:#0085ba;border-bottom:none}.wpacu-tabs-style-topline nav a{padding:20px 10px;background:rgba(40,44,42,.05);color:#74777b;line-height:1;-webkit-transition:color .2s;transition:color .2s;text-decoration:none}.wpacu-tabs-style-topline nav a:focus,.wpacu-tabs-style-topline nav a:hover{color:#0085ba}.wpacu-tabs-style-topline nav li.wpacu-tab-current a{background:0 0;box-shadow:inset 0 3px 0 #0085ba;color:#0085ba}.wpacu-tabs-style-topline nav li.wpacu-tab-current a span:not(.dashicons){font-weight:600}.wpacu-tabs-style-topline nav a span{text-transform:uppercase;font-weight:400}.wpacu-script-attributes-area{margin:20px 0 0!important}.wpacu-script-attributes-area.wpacu-lite ul.wpacu-script-attributes-settings{padding:5px 7px!important}.wpacu-script-attributes-area.wpacu-lite ul.wpacu-script-attributes-settings.wpacu-first{margin:0 28px 0 0!important}.wpacu-script-attributes-area.wpacu-lite ul.wpacu-script-attributes-settings label{margin:0!important}.wpacu-script-attributes-area.wpacu-lite ul.wpacu-script-attributes-settings li{margin:0 16px 0 0!important}.wpacu-script-attributes-area.wpacu-lite ul.wpacu-script-attributes-settings li:first-child{margin:0 10px 0 0!important}.wpacu-script-attributes-area.wpacu-lite ul.wpacu-script-attributes-settings li:last-child{margin:0 2px 0 0!important}.wpacu-script-attributes-area .wpacu-script-attributes-settings{background:#fff!important;border:1px solid #eee!important;-webkit-border-radius:5px;-moz-border-radius:5px;border-radius:5px;padding:5px 10px!important;list-style:none;display:inline-block;margin:0}.wpacu-script-attributes-area .wpacu-script-attributes-settings input[type=radio]{padding:0;margin:0 5px 0 0}.wpacu-script-attributes-area .wpacu-script-attributes-settings.wpacu-first{margin:0 34px 0 0!important}.wpacu-script-attributes-area .wpacu-script-attributes-settings li{float:left;margin:0 17px 0 0}.wpacu-script-attributes-area .wpacu-script-attributes-settings li:first-child{margin:0 10px 0 0!important}.wpacu-script-attributes-area .wpacu-script-attributes-settings li:last-child{margin:0}.wpacu-script-attributes-area .wpacu-script-attributes-settings:hover{background:#f2faf2}body.toplevel_page_wpassetcleanup_settings .notice{padding:10px!important}tr.wpacu_not_load td,tr.wpacu_not_load th{background:#FFE1E1!important}.dashicons-wpacu{width:17px;height:17px;margin:2px 0 0;font-size:16px;text-decoration:none}.wpacu_widefat .check-column.wpacu_check{padding:10px}body[class*=wpassetcleanup] #footer-left{border-left:2px solid green;padding-left:10px}#wpacu_meta_box_content{width:99%}#wpacu_meta_box_content h3{margin-left:0;padding-left:0}#wpacu_meta_box_content div.wpacu_note{padding:10px;background-color:#f5f5f5}#wpacu_meta_box_content .dashicons.dashicons-warning{color:#c00}#wpacu_meta_box_content div.wpacu_warning,#wpacu_wrap_assets div.wpacu_warning{border:1px solid #c00}#wpacu_meta_box_content div.wpacu_verified{padding:10px;background-color:#f5f5f5;border-left:1px solid #ddd}#wpacu_meta_box_content div.wpacu_verified span{color:#0073aa;font-weight:700}#wpacu_meta_box_content ul.wpacu_asset_options{margin:0;padding:0}#wpacu_meta_box_content ul.wpacu_asset_options li{list-style:none;display:inline-block}#wpacu_wrap_assets img,#wpacu_wrap_assets small,.wpacu_asset_row td label{display:inline-block!important}#wpacu_meta_box_content ul.wpacu_asset_options label{font-weight:400;margin-right:5px;margin-top:5px;margin-bottom:5px}.wpacu-updated-frontend{font-weight:400;background-color:#fff;border-left:4px solid #46b450;border-top:1px solid #46b450;border-right:1px solid #46b450;border-bottom:1px solid #46b450;padding:9px 14px;-webkit-border-radius:5px;-moz-border-radius:5px;border-radius:5px}.wpacu-updated-frontend span.dashicons-yes{color:#46b450;font-size:26px}#wpacu_wrap_assets{color:#000!important;text-align:left!important;clear:both!important;width:90%!important;padding:20px!important;margin:30px 0 0 15px!important;border:1px solid #cdcdcd;z-index:10000!important;position:relative!important;background-color:#fff!important}#wpacu_wrap_assets small{margin:0!important;letter-spacing:normal!important}#wpacu_wrap_assets input[type=checkbox]{margin-right:4px!important}#wpacu_wrap_assets p{margin:10px 0!important;line-height:22px!important;text-align:left!important;font-weight:400!important}#wpacu_wrap_assets p:first-child,.wpacu_verified{margin:0 0 10px!important}#wpacu_wrap_assets h1{margin:0 0 20px!important;font-size:160%!important;text-align:left!important}#wpacu_wrap_assets h3{margin:20px 0;font-size:130%}.wpacu_asset_row td{padding:10px!important;border-left:1px solid #cdcdcd;border-right:1px solid #cdcdcd;border-bottom:1px solid #cdcdcd}.wpacu_asset_row td:first-child{border-top:1px solid #cdcdcd}.wpacu_asset_row td p{padding:0!important;margin-bottom:10px!important;word-wrap:break-word!important;-ms-word-break:break-all;word-break:break-word}.wpacu_asset_row td label{margin:0!important;cursor:pointer!important;font-size:100%!important;font-weight:400!important}.wpacu_asset_row td label span:not(.dashicons){font-size:100%!important}.wpacu_asset_row td.wpacu_check{width:50px!important}.asset-table-extra-info td{padding:5px!important}body.wp-admin .wpacu_asset_row td.wpacu_check{width:25px!important}.wpacu_asset_row .dashicons-warning{color:red!important}label.wpacu_global_checked{font-weight:700;color:#d54e21}label.wpacu_global_unload_exception{font-weight:700!important}label.wpacu_bulk_unload_active{color:#d54e21}tr.wpacu_global_rule_row.selected{background:#e7e7e7}.nav-tab-wrapper{border-bottom:1px solid #ccc;width:95%}ul.wpacu_asset_options{margin:0 10px 0 0;padding:0}ul.wpacu_asset_options li{margin:0 20px 0 0;list-style:none;display:inline-block}ul.wpacu_asset_options li.remove_rule label{font-weight:700}ul.wpacu_asset_options label{margin-right:20px;font-weight:400}ul.wpacu_asset_options label input{margin-right:4px}.wpacu_list_table,.wpacu_list_table div,.wpacu_list_table li,.wpacu_list_table p{font-weight:400}.wpacu_widefat{width:99%;border:1px solid #e5e5e5!important}.wpacu_striped>tbody>:nth-child(even),ul.wpacu_striped>:nth-child(even){background-color:#fff!important}.wpacu_striped>tbody>:nth-child(odd),ul.wpacu_striped>:nth-child(odd){background-color:#f9f9f9!important}.wpacu_update_btn{-moz-box-shadow:inset 0 1px 0 0 #54a3f7!important;-webkit-box-shadow:inset 0 1px 0 0 #54a3f7!important;box-shadow:inset 0 1px 0 0 #54a3f7!important;background:-webkit-gradient(linear,left top,left bottom,color-stop(.05,#007dc1),color-stop(1,#0061a7))!important;background:-moz-linear-gradient(top,#007dc1 5%,#0061a7 100%)!important;background:-webkit-linear-gradient(top,#007dc1 5%,#0061a7 100%)!important;background:-o-linear-gradient(top,#007dc1 5%,#0061a7 100%)!important;background:-ms-linear-gradient(top,#007dc1 5%,#0061a7 100%)!important;background:linear-gradient(to bottom,#007dc1 5%,#0061a7 100%)!important;filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#007dc1', endColorstr='#0061a7', GradientType=0)!important;background-color:#007dc1!important;-moz-border-radius:3px!important;-webkit-border-radius:3px!important;border-radius:3px!important;border:1px solid #124d77!important;display:inline-block!important;color:#fff!important;font-size:15px!important;padding:10px 24px!important;text-decoration:none!important;text-shadow:0 1px 0 #154682!important;height:auto!important;line-height:normal!important}.wpacu-btns .btn:focus,.wpacu-btns .btn:hover,.wpacu-btns a{text-decoration:none}.wpacu_update_btn:hover{background:-webkit-gradient(linear,left top,left bottom,color-stop(.05,#0061a7),color-stop(1,#007dc1))!important;background:-moz-linear-gradient(top,#0061a7 5%,#007dc1 100%)!important;background:-webkit-linear-gradient(top,#0061a7 5%,#007dc1 100%)!important;background:-o-linear-gradient(top,#0061a7 5%,#007dc1 100%)!important;background:-ms-linear-gradient(top,#0061a7 5%,#007dc1 100%)!important;background:linear-gradient(to bottom,#0061a7 5%,#007dc1 100%)!important;filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#0061a7', endColorstr='#007dc1', GradientType=0)!important;background-color:#0061a7!important}.wpacu_update_btn:active{position:relative!important;top:1px!important}ul.hire-reasons{margin-bottom:20px}ul.hire-reasons li{font-size:14px;line-height:22px;margin-bottom:12px}ul.hire-reasons li .dashicons{font-size:23px;line-height:23px}ul.hire-reasons li .dashicons.dashicons-yes{color:green}ol.getting-started li{font-size:14px;line-height:22px;margin-bottom:8px}.wpacu-get-help-wrap p{font-size:14px}.wpacu-btns .btn{display:inline-block;font-weight:400;line-height:1.25;text-align:center;white-space:nowrap;vertical-align:middle;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;border:1px solid transparent;padding:.5rem 1rem;font-size:1rem;-webkit-border-radius:.25rem;-moz-border-radius:.25rem;border-radius:.25rem;-webkit-transition:all .2s ease-in-out;-o-transition:all .2s ease-in-out;transition:all .2s ease-in-out}.wpacu-btns .btn.focus,.wpacu-btns .btn:focus{outline:0;-webkit-box-shadow:0 0 0 2px rgba(2,117,216,.25);box-shadow:0 0 0 2px rgba(2,117,216,.25)}.wpacu-btns .btn.disabled,.wpacu-btns .btn:disabled{cursor:not-allowed;opacity:.65}.wpacu-btns .btn.active,.wpacu-btns .btn:active{background-image:none}.wpacu-btns a.btn.disabled,.wpacu-btns fieldset[disabled] a.btn{pointer-events:none}.wpacu-btns .btn-primary{color:#fff;background-color:#0275d8;border-color:#0275d8}.wpacu-btns .btn-primary:hover{color:#fff;background-color:#025aa5;border-color:#01549b}.wpacu-btns .btn-primary.focus,.wpacu-btns .btn-primary:focus{-webkit-box-shadow:0 0 0 2px rgba(2,117,216,.5);box-shadow:0 0 0 2px rgba(2,117,216,.5)}.wpacu-btns .btn-primary.disabled,.wpacu-btns .btn-primary:disabled{background-color:#0275d8;border-color:#0275d8}.wpacu-btns .btn-primary.active,.wpacu-btns .btn-primary:active,.wpacu-btns .show>.btn-primary.dropdown-toggle{color:#fff;background-color:#025aa5;background-image:none;border-color:#01549b}.wpacu-btns .btn-secondary{color:#292b2c;background-color:#fff;border-color:#ccc}.wpacu-btns .btn-secondary:hover{color:#292b2c;background-color:#e6e6e6;border-color:#adadad}.wpacu-btns .btn-secondary.focus,.wpacu-btns .btn-secondary:focus{-webkit-box-shadow:0 0 0 2px rgba(204,204,204,.5);box-shadow:0 0 0 2px rgba(204,204,204,.5)}.wpacu-btns .btn-secondary.disabled,.wpacu-btns .btn-secondary:disabled{background-color:#fff;border-color:#ccc}.wpacu-btns .btn-secondary.active,.wpacu-btns .btn-secondary:active,.wpacu-btns .show>.btn-secondary.dropdown-toggle{color:#292b2c;background-color:#e6e6e6;background-image:none;border-color:#adadad}.wpacu-btns .btn-info{color:#fff;background-color:#5bc0de;border-color:#5bc0de}.wpacu-btns .btn-info:hover{color:#fff;background-color:#31b0d5;border-color:#2aabd2}.wpacu-btns .btn-info.focus,.wpacu-btns .btn-info:focus{-webkit-box-shadow:0 0 0 2px rgba(91,192,222,.5);box-shadow:0 0 0 2px rgba(91,192,222,.5)}.wpacu-btns .btn-info.disabled,.wpacu-btns .btn-info:disabled{background-color:#5bc0de;border-color:#5bc0de}.wpacu-btns .btn-info.active,.wpacu-btns .btn-info:active,.wpacu-btns .show>.btn-info.dropdown-toggle{color:#fff;background-color:#31b0d5;background-image:none;border-color:#2aabd2}.wpacu-btns .btn-success{color:#fff;background-color:#5cb85c;border-color:#5cb85c}.wpacu-btns .btn-success:hover{color:#fff;background-color:#449d44;border-color:#419641}.wpacu-btns .btn-success.focus,.wpacu-btns .btn-success:focus{-webkit-box-shadow:0 0 0 2px rgba(92,184,92,.5);box-shadow:0 0 0 2px rgba(92,184,92,.5)}.wpacu-btns .btn-success.disabled,.wpacu-btns .btn-success:disabled{background-color:#5cb85c;border-color:#5cb85c}.wpacu-btns .btn-success.active,.wpacu-btns .btn-success:active,.wpacu-btns .show>.btn-success.dropdown-toggle{color:#fff;background-color:#449d44;background-image:none;border-color:#419641}.wpacu-btns .btn-warning{color:#fff;background-color:#f0ad4e;border-color:#f0ad4e}.wpacu-btns .btn-warning:hover{color:#fff;background-color:#ec971f;border-color:#eb9316}.wpacu-btns .btn-warning.focus,.wpacu-btns .btn-warning:focus{-webkit-box-shadow:0 0 0 2px rgba(240,173,78,.5);box-shadow:0 0 0 2px rgba(240,173,78,.5)}.wpacu-btns .btn-warning.disabled,.wpacu-btns .btn-warning:disabled{background-color:#f0ad4e;border-color:#f0ad4e}.wpacu-btns .btn-warning.active,.wpacu-btns .btn-warning:active,.wpacu-btns .show>.btn-warning.dropdown-toggle{color:#fff;background-color:#ec971f;background-image:none;border-color:#eb9316}.wpacu-btns .btn-danger{color:#fff;background-color:#d9534f;border-color:#d9534f}.wpacu-btns .btn-danger:hover{color:#fff;background-color:#c9302c;border-color:#c12e2a}.wpacu-btns .btn-danger.focus,.wpacu-btns .btn-danger:focus{-webkit-box-shadow:0 0 0 2px rgba(217,83,79,.5);box-shadow:0 0 0 2px rgba(217,83,79,.5)}.wpacu-btns .btn-danger.disabled,.wpacu-btns .btn-danger:disabled{background-color:#d9534f;border-color:#d9534f}.wpacu-btns .btn-danger.active,.wpacu-btns .btn-danger:active,.wpacu-btns .show>.btn-danger.dropdown-toggle{color:#fff;background-color:#c9302c;background-image:none;border-color:#c12e2a}.wpacu-btns .btn-outline-primary{color:#0275d8;background-image:none;background-color:transparent;border-color:#0275d8}.wpacu-btns .btn-outline-primary:hover{color:#fff;background-color:#0275d8;border-color:#0275d8}.wpacu-btns .btn-outline-primary.focus,.wpacu-btns .btn-outline-primary:focus{-webkit-box-shadow:0 0 0 2px rgba(2,117,216,.5);box-shadow:0 0 0 2px rgba(2,117,216,.5)}.wpacu-btns .btn-outline-primary.disabled,.wpacu-btns .btn-outline-primary:disabled{color:#0275d8;background-color:transparent}.wpacu-btns .btn-outline-primary.active,.wpacu-btns .btn-outline-primary:active,.wpacu-btns .show>.btn-outline-primary.dropdown-toggle{color:#fff;background-color:#0275d8;border-color:#0275d8}.wpacu-btns .btn-outline-secondary{color:#ccc;background-image:none;background-color:transparent;border-color:#ccc}.wpacu-btns .btn-outline-secondary:hover{color:#fff;background-color:#ccc;border-color:#ccc}.wpacu-btns .btn-outline-secondary.focus,.wpacu-btns .btn-outline-secondary:focus{-webkit-box-shadow:0 0 0 2px rgba(204,204,204,.5);box-shadow:0 0 0 2px rgba(204,204,204,.5)}.wpacu-btns .btn-outline-secondary.disabled,.wpacu-btns .btn-outline-secondary:disabled{color:#ccc;background-color:transparent}.wpacu-btns .btn-outline-secondary.active,.wpacu-btns .btn-outline-secondary:active,.wpacu-btns .show>.btn-outline-secondary.dropdown-toggle{color:#fff;background-color:#ccc;border-color:#ccc}.wpacu-btns .btn-outline-info{color:#5bc0de;background-image:none;background-color:transparent;border-color:#5bc0de}.wpacu-btns .btn-outline-info:hover{color:#fff;background-color:#5bc0de;border-color:#5bc0de}.wpacu-btns .btn-outline-info.focus,.wpacu-btns .btn-outline-info:focus{-webkit-box-shadow:0 0 0 2px rgba(91,192,222,.5);box-shadow:0 0 0 2px rgba(91,192,222,.5)}.wpacu-btns .btn-outline-info.disabled,.wpacu-btns .btn-outline-info:disabled{color:#5bc0de;background-color:transparent}.wpacu-btns .btn-outline-info.active,.wpacu-btns .btn-outline-info:active,.wpacu-btns .show>.btn-outline-info.dropdown-toggle{color:#fff;background-color:#5bc0de;border-color:#5bc0de}.wpacu-btns .btn-outline-success{color:#5cb85c;background-image:none;background-color:transparent;border-color:#5cb85c}.wpacu-btns .btn-outline-success:hover{color:#fff;background-color:#5cb85c;border-color:#5cb85c}.wpacu-btns .btn-outline-success.focus,.wpacu-btns .btn-outline-success:focus{-webkit-box-shadow:0 0 0 2px rgba(92,184,92,.5);box-shadow:0 0 0 2px rgba(92,184,92,.5)}.wpacu-btns .btn-outline-success.disabled,.wpacu-btns .btn-outline-success:disabled{color:#5cb85c;background-color:transparent}.wpacu-btns .btn-outline-success.active,.wpacu-btns .btn-outline-success:active,.wpacu-btns .show>.btn-outline-success.dropdown-toggle{color:#fff;background-color:#5cb85c;border-color:#5cb85c}.wpacu-btns .btn-outline-warning{color:#f0ad4e;background-image:none;background-color:transparent;border-color:#f0ad4e}.wpacu-btns .btn-outline-warning:hover{color:#fff;background-color:#f0ad4e;border-color:#f0ad4e}.wpacu-btns .btn-outline-warning.focus,.wpacu-btns .btn-outline-warning:focus{-webkit-box-shadow:0 0 0 2px rgba(240,173,78,.5);box-shadow:0 0 0 2px rgba(240,173,78,.5)}.wpacu-btns .btn-outline-warning.disabled,.wpacu-btns .btn-outline-warning:disabled{color:#f0ad4e;background-color:transparent}.wpacu-btns .btn-outline-warning.active,.wpacu-btns .btn-outline-warning:active,.wpacu-btns .show>.btn-outline-warning.dropdown-toggle{color:#fff;background-color:#f0ad4e;border-color:#f0ad4e}.wpacu-btns .btn-outline-danger{color:#d9534f;background-image:none;background-color:transparent;border-color:#d9534f}.wpacu-btns .btn-outline-danger:hover{color:#fff;background-color:#d9534f;border-color:#d9534f}.wpacu-btns .btn-outline-danger.focus,.wpacu-btns .btn-outline-danger:focus{-webkit-box-shadow:0 0 0 2px rgba(217,83,79,.5);box-shadow:0 0 0 2px rgba(217,83,79,.5)}.wpacu-btns .btn-outline-danger.disabled,.wpacu-btns .btn-outline-danger:disabled{color:#d9534f;background-color:transparent}.wpacu-btns .btn-outline-danger.active,.wpacu-btns .btn-outline-danger:active,.wpacu-btns .show>.btn-outline-danger.dropdown-toggle{color:#fff;background-color:#d9534f;border-color:#d9534f}.wpacu-btns .btn-link{font-weight:400;color:#0275d8;-webkit-border-radius:0;-moz-border-radius:0;border-radius:0}.wpacu-btns .btn-link,.wpacu-btns .btn-link.active,.wpacu-btns .btn-link:active,.wpacu-btns .btn-link:disabled{background-color:transparent}.wpacu-btns .btn-link,.wpacu-btns .btn-link:active,.wpacu-btns .btn-link:focus,.wpacu-btns .btn-link:hover{border-color:transparent}.wpacu-btns .btn-link:focus,.wpacu-btns .btn-link:hover{color:#014c8c;text-decoration:underline;background-color:transparent}.wpacu-btns .btn-link:disabled{color:#636c72}.wpacu-btns .btn-link:disabled:focus,.wpacu-btns .btn-link:disabled:hover{text-decoration:none}.wpacu-btns .btn-group-lg>.btn,.wpacu-btns .btn-lg{padding:.75rem 1.5rem;font-size:1.25rem;-webkit-border-radius:.3rem;-moz-border-radius:.3rem;border-radius:.3rem}.wpacu-btns .btn-group-sm>.btn,.wpacu-btns .btn-sm{padding:.25rem .5rem;font-size:.875rem;-webkit-border-radius:.2rem;-moz-border-radius:.2rem;border-radius:.2rem}.wpacu-btns .btn-block{display:block;width:100%}.go-pro-button,.go-pro-link-no-style,.wordpress-core-file{display:inline-block;text-decoration:none}.wpacu-btns .btn-block+.btn-block{margin-top:.5rem}.wpacu-btns input[type=submit].btn-block,.wpacu-btns input[type=reset].btn-block,.wpacu-btns input[type=button].btn-block{width:100%}.wpacu-btns .fade{opacity:0;-webkit-transition:opacity .15s linear;-o-transition:opacity .15s linear;transition:opacity .15s linear}.go-pro-button{background-color:#4CAF50;border:2px solid #4CAF50;color:#fff;padding:12px 18px;text-align:center;font-size:16px;margin:4px 2px;cursor:pointer;-webkit-border-radius:10px;-moz-border-radius:10px;border-radius:10px;-webkit-transition-duration:.4s;transition-duration:.4s}.go-pro-button:hover{background-color:#fff;color:#000;border:2px solid #4CAF50}body.wp-admin .wpacu_asset_row .go-pro-link-no-style .tooltip{padding:8px 2px}body.wp-admin .go-pro-link-no-style .tooltip{font-weight:400;font-size:12px;line-height:16px;padding:8px 11px}.go-pro-link-no-style{position:relative;color:inherit}.go-pro-link-no-style.no-transition .tooltip{-webkit-transition:0s all ease;transition:0s all ease;-webkit-transition-delay:0s;transition-delay:0s}.go-pro-link-no-style .tooltip{font-size:14px;line-height:18px;position:absolute;z-index:1;visibility:hidden;width:130px;background-color:#27ae60;color:#fff;text-align:center;padding:8px;-webkit-border-radius:6px;-moz-border-radius:6px;border-radius:6px;bottom:100%;bottom:calc(100% + 15px);left:50%;margin-left:-65px;-webkit-transition:.2s all ease;transition:.2s all ease;-webkit-transition-delay:0s;transition-delay:0s}.go-pro-link-no-style .tooltip:after{content:"";position:absolute;border-width:10px;border-style:solid;top:100%;left:50%;left:calc(50% - 10px);border-color:#27ae60 transparent transparent}.go-pro-link-no-style .tooltip.wpacu-larger{width:240px}.go-pro-link-no-style:hover .tooltip{visibility:visible}.wordpress-core-file{position:relative;font-style:normal;color:inherit}.wordpress-core-file .tooltip{font-size:14px;line-height:18px;position:absolute;z-index:1;visibility:hidden;width:160px;background-color:#c00;color:#fff;text-align:center;padding:6px;-webkit-border-radius:6px;-moz-border-radius:6px;border-radius:6px;bottom:100%;bottom:calc(100% + 15px);left:50%;margin-left:-86px;-webkit-transition:.5s all ease;transition:.5s all ease;-webkit-transition-delay:0s;transition-delay:0s}.wordpress-core-file .tooltip:after{content:"";position:absolute;border-width:10px;border-style:solid;top:100%;left:50%;left:calc(50% - 10px);border-color:#c00 transparent transparent}.wordpress-core-file:hover .tooltip{visibility:visible}.pro-page-unlock-notice{-webkit-border-radius:10px;-moz-border-radius:10px;border-radius:10px;padding:10px;background:#f2faf2;border:1px solid #c0e8c0;width:auto;display:block}.wpacu-get-help-wrap .help-content-wrap{margin:20px 0 25px;display:-webkit-box;display:-moz-box;display:-ms-flexbox;display:-moz-flex;display:-webkit-flex;display:flex;width:100%}@media only screen and (min-width:768px){.wpacu-get-help-wrap .help-content-wrap{-webkit-box-align:start;-ms-flex-align:start;-ms-grid-row-align:flex-start;align-items:flex-start}}@media only screen and (max-width:767px){.wpacu-get-help-wrap .help-content-wrap{-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column}}.wpacu-get-help-wrap .help-content-wrap .help-content{padding:15px;background:#fff;border:1px solid #E0E0E0;-webkit-border-radius:10px;-moz-border-radius:10px;border-radius:10px;box-shadow:2px 3px 8px 0 #231f2038;flex:1 1 50%;margin-bottom:10px}.wpacu-get-help-wrap .help-content-wrap .help-content .content-title{margin:5px 0 15px}.wpacu-get-help-wrap .help-content-wrap .help-content .cols-wrap{width:100%;margin:0 auto;display:table}.wpacu-get-help-wrap .help-content-wrap .help-content .wpacu-image-area{margin:5px 0 0;position:relative}.wpacu-get-help-wrap .help-content-wrap .help-content .wpacu-image-area.col-left{float:left;width:48%}.wpacu-get-help-wrap .help-content-wrap .help-content .wpacu-image-area.col-right{float:right;width:48%}.wpacu-get-help-wrap .help-content-wrap .help-content .wpacu-image-area.activate-plugin img{max-width:280px}.wpacu-get-help-wrap .help-content-wrap .help-content .wpacu-image-area img{width:99%;margin:8px 0;border:1px solid #cdcdcd;-webkit-border-radius:10px;-moz-border-radius:10px;border-radius:10px}.wpacu-get-help-wrap .help-content-wrap .help-content.upgrade-help{order:2}@media only screen and (min-width:768px){.wpacu-get-help-wrap .help-content-wrap .help-content{margin-right:24px}.wpacu-get-help-wrap .help-content-wrap .help-content.upgrade-help{margin-right:0}}.wpacu-get-help-wrap .help-content-wrap .help-content.tech-help{order:1}@media only screen and (max-width:767px){.wpacu-get-help-wrap .help-content-wrap .help-content.tech-help{margin-bottom:30px}}.wpacu-get-help-wrap .help-content-wrap .help-content ul{list-style:none;padding-left:0}.wpacu-get-help-wrap .help-content-wrap .help-content ul li{font-size:14px;line-height:22px;margin-bottom:15px}.wpacu-get-help-wrap .help-content-wrap .help-content p:last-child,.wpacu-get-help-wrap .help-content-wrap .help-content ul li:last-child{margin-bottom:0}.ajax-direct-call-error-area{background:#fff;padding:10px;border:1px solid #c00;-webkit-border-radius:8px;-moz-border-radius:8px;border-radius:8px}.ajax-direct-call-error-area td{padding:6px}.ajax-direct-call-error-area .note{margin-top:0;padding-top:0}.ajax-direct-call-error-area .error-code{color:#c00;font-weight:700}.ajax-wp-remote-post-call-error-area{background:#fff;padding:20px}.ajax-wp-remote-post-call-error-area .table-data{background:0 0;border:1px solid #c66;-webkit-border-radius:10px;-moz-border-radius:10px;border-radius:10px}.ajax-wp-remote-post-call-error-area .table-data td{padding:10px}.wrap-upgrade-info{background:#fff;padding:0 15px;border:1px solid #cdcdcd;-webkit-border-radius:10px;-moz-border-radius:10px;border-radius:10px}.wpacu-notice{margin:20px 20px 0 0!important}.wpacu-notice p{font-weight:600!important}.wpacu-notice .dashicons{font-weight:inherit!important;color:#46b450!important;margin-right:4px;vertical-align:text-bottom;font-size:27px}.wpacu-wrap.wpacu-switch-standard .toplevel_page_wpassetcleanup_settings .wpacu_switch{width:auto;height:auto}.wpacu-wrap.wpacu-switch-standard .toplevel_page_wpassetcleanup_settings .wpacu_switch input{display:block}.wpacu-wrap.wpacu-switch-standard .toplevel_page_wpassetcleanup_settings .wpacu_switch input[type=checkbox]{margin:0 0 3px}.wpacu-wrap.wpacu-switch-standard .toplevel_page_wpassetcleanup_settings .wpacu_switch .wpacu_slider{display:none}.wpacu-wrap.wpacu-switch-enhanced .wpacu_switch{position:relative;display:inline-block;width:52px;height:29px}.wpacu-wrap.wpacu-switch-enhanced .wpacu_switch input{display:none}.wpacu-wrap.wpacu-switch-enhanced .wpacu_switch:not(.wpacu_switch_standard) .wpacu_slider{position:absolute;cursor:pointer;top:0;left:0;right:0;bottom:0;background-color:#ccc;-webkit-transition:.2s;transition:.2s}.wpacu-wrap.wpacu-switch-enhanced .wpacu_switch:not(.wpacu_switch_standard) .wpacu_slider:before{position:absolute;content:'';height:22px;width:22px;left:4px;bottom:4px;background-color:#fff;-webkit-transition:.2s;transition:.2s}.wpacu-wrap.wpacu-switch-enhanced .wpacu_switch input:checked+.wpacu_slider{background-color:#52af00}.wpacu-wrap.wpacu-switch-enhanced .wpacu_switch input:focus+.wpacu_slider{box-shadow:0 0 1px #52af00}.wpacu-wrap.wpacu-switch-enhanced .wpacu_switch input:checked+.wpacu_slider:before{-webkit-transform:translateX(22px);-ms-transform:translateX(22px);transform:translateX(22px)}.wpacu-wrap.wpacu-switch-enhanced .wpacu_switch .wpacu_slider.wpacu_round{border-radius:29px}.wpacu-wrap.wpacu-switch-enhanced .wpacu_switch .wpacu_slider.wpacu_round:before{border-radius:50%}.wpacu-wrap.wpacu-switch-enhanced .wpacu_switch.wpacu_locked_for_pro{opacity:.3}.toplevel_page_wpassetcleanup_settings .setting_title p.wpacu_subtitle{margin-top:2px;line-height:1.2}.toplevel_page_wpassetcleanup_settings .setting_title p.wpacu_subtitle em{font-weight:200}.toplevel_page_wpassetcleanup_settings .setting_title p.wpacu_read_more{margin-top:-10px;font-style:italic;font-size:12px}.toplevel_page_wpassetcleanup_settings .assets_list_inline_code_status_choices,.toplevel_page_wpassetcleanup_settings .assets_list_layout_areas_status_choices,.toplevel_page_wpassetcleanup_settings .input_style_choices{list-style:none;margin:0}.toplevel_page_wpassetcleanup_settings .assets_list_inline_code_status_choices li,.toplevel_page_wpassetcleanup_settings .assets_list_layout_areas_status_choices li,.toplevel_page_wpassetcleanup_settings .input_style_choices li{float:left;margin-right:30px}.toplevel_page_wpassetcleanup_settings .assets_list_inline_code_status_choices li:last-child,.toplevel_page_wpassetcleanup_settings .assets_list_layout_areas_status_choices li:last-child,.toplevel_page_wpassetcleanup_settings .input_style_choices li:last-child{margin-right:0}.wrap_bulk_unload_options{width:auto;display:inline-flex;-webkit-flex-wrap:wrap;flex-wrap:wrap}.wrap_bulk_unload_options .wpacu_asset_options_wrap{flex-grow:1;margin:5px 10px 14px 0!important;padding:8px 10px 2px!important}.wrap_bulk_unload_options .wpacu_asset_options_wrap .wpacu_slider_text{position:relative!important;vertical-align:top;left:inherit!important;top:inherit!important}.wpacu-switch-enhanced .wpacu_asset_row .wpacu_switch{position:relative;display:inline-block;width:52px;height:29px;margin-right:5px!important}.wpacu-switch-enhanced .wpacu_asset_row .wpacu_switch input{display:none}.wpacu-switch-enhanced .wpacu_asset_row .wpacu_switch .wpacu_slider{position:absolute;cursor:pointer;top:0;left:0;right:0;bottom:0;background-color:#ccc;-webkit-transition:.2s;transition:.2s}.wpacu-switch-enhanced .wpacu_asset_row .wpacu_switch .wpacu_slider:before{position:absolute;content:'';height:22px;width:22px;left:4px;bottom:4px;background-color:#fff;-webkit-transition:.2s;transition:.2s}.wpacu-switch-enhanced .wpacu_asset_row .wpacu_switch input:checked+.wpacu_slider{background-color:#C00}.wpacu-switch-enhanced .wpacu_asset_row .wpacu_switch input:focus+.wpacu_slider{box-shadow:0 0 1px #C00}.wpacu-switch-enhanced .wpacu_asset_row .wpacu_switch input:checked+.wpacu_slider:before{-webkit-transform:translateX(22px);-ms-transform:translateX(22px);transform:translateX(22px)}.wpacu-switch-enhanced .wpacu_asset_row .wpacu_switch .wpacu_slider.wpacu_round{border-radius:29px}.wpacu-switch-enhanced .wpacu_asset_row .wpacu_switch .wpacu_slider.wpacu_round:before{border-radius:50%}.wpacu-switch-enhanced .wpacu_asset_row .wpacu_slider_text{position:absolute;left:62px;top:3px}.wpacu_asset_row td{width:100%}.wpacu_asset_row td p:last-child{margin-bottom:0!important}.wpacu_asset_row td .wpacu_asset_options_wrap{padding:8px 10px;margin:15px 0;background:#fff;border:1px solid #eee;-webkit-border-radius:5px;-moz-border-radius:5px;border-radius:5px}.wpacu_asset_row td .wpacu_asset_options_wrap p:first-child{margin-top:0;margin-bottom:8px!important}.wpacu_asset_row p.wpacu_handle{margin-top:0}.wpacu_asset_row li.wpacu_unload_this_page{display:block!important;position:relative}.wpacu-clearfix:after,.wpacu-clearfix:before{content:'';display:table}.wpacu-contract-expand-area{width:100%;margin:20px 0 0}.wpacu-contract-expand-area .col-left{float:left}.wpacu-contract-expand-area .col-left h4{margin-bottom:0}.wpacu-contract-expand-area .col-right{float:right}.wpacu-clearfix:after{clear:both}.wpacu-wp-button{color:#555;border-color:#ccc;background:#f7f7f7;box-shadow:0 1px 0 #ccc;vertical-align:top}body.wp-admin .wpacu-wp-button{font-size:13px;line-height:26px;height:28px}.wpacu-wp-button.wpacu-wp-button-secondary{display:inline-block;text-decoration:none;height:auto;margin:0 5px 0 0;padding:0 10px 1px;cursor:pointer;border-width:1px;border-style:solid;-webkit-appearance:none;border-radius:3px;white-space:nowrap;box-sizing:border-box}.wpacu-assets-collapsible-wrap.wpacu-wrap-all,.wpacu-assets-collapsible-wrap.wpacu-wrap-styles{margin-top:20px}.wpacu-wp-button:hover{background:#fafafa;border-color:#999;color:#23282d}a.wpacu-assets-collapsible.wpacu-assets-collapsible-active,a.wpacu-assets-collapsible:hover{background-color:#f1f3f8}.wpacu-assets-collapsible-wrap{border:1px solid #e7e7e7}.wpacu-assets-collapsible-wrap.wpacu-wrap-all .distinguish-asset-list{list-style:none;margin-left:0;margin-bottom:5px;display:inline-block}.wpacu-assets-collapsible-wrap.wpacu-wrap-all .distinguish-asset-list li{float:left;margin-right:20px}.wpacu-assets-collapsible-wrap.wpacu-wrap-all .distinguish-asset-list li:last-child{margin-right:0}.wpacu-assets-collapsible-wrap.wpacu-wrap-scripts{margin-top:10px}a.wpacu-assets-collapsible{color:#000;font-size:20px;text-decoration:none;display:block;position:relative;background-color:#f1f3f8;cursor:pointer;padding:15px 15px 15px 36px;width:auto;border:none;text-align:left;outline:0}a.wpacu-assets-collapsible>span.dashicons{font-size:25px;line-height:normal;position:absolute;top:15px;top:calc(50% - 21px);left:22px;-webkit-transform:translate(-50%,50%);transform:translate(-50%,50%)}a.wpacu-assets-collapsible:after{position:absolute;font-size:22px;font-family:Dashicons;right:14px;top:16px}a.wpacu-assets-collapsible:not(.wpacu-assets-collapsible-active):after{content:"\f132"}a.wpacu-assets-collapsible.wpacu-assets-collapsible-active:after{content:"\f460"}.wpacu-assets-collapsible-content{padding:0 16px;max-height:0;overflow:hidden;background-color:#fbfbfb8c}.wpacu-assets-collapsible-content>div{padding:16px 0 0}.wpacu-assets-collapsible-content>div>p:first-child{margin-top:0}.wpacu-assets-collapsible-content.wpacu-open{max-height:100%}a.wpacu-assets-inline-code-collapsible{font-size:14px;text-decoration:none;display:inline-block;position:relative;cursor:pointer;width:auto;outline:0;background:#f1f3f8;border:1px solid #cdcdcd;padding:4px 21px 4px 8px;margin:0 0 0 6px;-webkit-border-radius:5px;-moz-border-radius:5px;border-radius:5px}a.wpacu-assets-inline-code-collapsible:hover{border:1px solid #5c5c5c}a.wpacu-assets-inline-code-collapsible:after{position:absolute;font-size:15px;font-family:Dashicons;right:-3px;top:-1px;height:14px;width:14px;-webkit-transform:translate(-50%,50%);transform:translate(-50%,50%)}a.wpacu-assets-inline-code-collapsible:not(.wpacu-assets-inline-code-collapsible-active):after{content:"\f132"}a.wpacu-assets-inline-code-collapsible.wpacu-assets-inline-code-collapsible-active:after{content:"\f460"}.wpacu-assets-inline-code-collapsible-content{padding:0 8px;max-height:0;overflow:hidden;background-color:#fbfbfb8c}.wpacu-assets-inline-code-collapsible-content>div{padding:7px 8px 0}.wpacu-assets-inline-code-collapsible-content>div>p:first-child{margin-top:0}.wpacu-assets-inline-code-collapsible-content.wpacu-open{max-height:100%}.wpacu_table_wrap{margin:20px 0 0}.wpacu_table_wrap>table{border:1px solid #cdcdcd}.wpacu_table_wrap .table{width:100%;max-width:100%;margin-bottom:1rem;-webkit-border-radius:10px;-moz-border-radius:10px;border-radius:10px}.wpacu_table_wrap .table td,.wpacu_table_wrap .table th{padding:.75rem;vertical-align:top;border-top:1px solid #eceeef}.wpacu_table_wrap .table thead th{vertical-align:bottom;border-bottom:2px solid #eceeef}.wpacu_table_wrap .table tbody+tbody{border-top:2px solid #eceeef}.wpacu_table_wrap .table .table{background-color:#fff}.wpacu_table_wrap .table-sm td,.wpacu_table_wrap .table-sm th{padding:.3rem}.wpacu_table_wrap .table-bordered,.wpacu_table_wrap .table-bordered td,.wpacu_table_wrap .table-bordered th{border:1px solid #eceeef}.wpacu_table_wrap .table-bordered thead td,.wpacu_table_wrap .table-bordered thead th{border-bottom-width:2px}.wpacu_table_wrap .table-striped tbody tr:nth-of-type(even){background-color:#f9f9f9}.wpacu_table_wrap .table-striped tbody tr:nth-of-type(odd){background-color:#fff}.wpacu_table_wrap .table-active,.wpacu_table_wrap .table-active>td,.wpacu_table_wrap .table-active>th,.wpacu_table_wrap .table-hover .table-active:hover,.wpacu_table_wrap .table-hover .table-active:hover>td,.wpacu_table_wrap .table-hover .table-active:hover>th,.wpacu_table_wrap .table-hover tbody tr:hover{background-color:rgba(0,0,0,.075)}.wpacu_table_wrap .table-success,.wpacu_table_wrap .table-success>td,.wpacu_table_wrap .table-success>th{background-color:#dff0d8}.wpacu_table_wrap .table-hover .table-success:hover,.wpacu_table_wrap .table-hover .table-success:hover>td,.wpacu_table_wrap .table-hover .table-success:hover>th{background-color:#d0e9c6}.wpacu_table_wrap .table-info,.wpacu_table_wrap .table-info>td,.wpacu_table_wrap .table-info>th{background-color:#d9edf7}.wpacu_table_wrap .table-hover .table-info:hover,.wpacu_table_wrap .table-hover .table-info:hover>td,.wpacu_table_wrap .table-hover .table-info:hover>th{background-color:#c4e3f3}.wpacu_table_wrap .table-warning,.wpacu_table_wrap .table-warning>td,.wpacu_table_wrap .table-warning>th{background-color:#fcf8e3}.wpacu_table_wrap .table-hover .table-warning:hover,.wpacu_table_wrap .table-hover .table-warning:hover>td,.wpacu_table_wrap .table-hover .table-warning:hover>th{background-color:#faf2cc}.wpacu_table_wrap .table-danger,.wpacu_table_wrap .table-danger>td,.wpacu_table_wrap .table-danger>th{background-color:#f2dede}.wpacu_table_wrap .table-hover .table-danger:hover,.wpacu_table_wrap .table-hover .table-danger:hover>td,.wpacu_table_wrap .table-hover .table-danger:hover>th{background-color:#ebcccc}.wpacu_table_wrap .thead-inverse th{color:#fff;background-color:#292b2c}.wpacu_table_wrap .thead-default th{color:#464a4c;background-color:#eceeef}.wpacu_table_wrap .table-inverse{color:#fff;background-color:#292b2c}.wpacu_table_wrap .table-inverse td,.wpacu_table_wrap .table-inverse th,.wpacu_table_wrap .table-inverse thead th{border-color:#fff}.wpacu_table_wrap .table-inverse.table-bordered,.wpacu_table_wrap .table-responsive.table-bordered{border:0}.wpacu_table_wrap .table-responsive{display:block;width:100%;overflow-x:auto;-ms-overflow-style:-ms-autohiding-scrollbar}.wpacu-warning{font-size:15px;padding:10px;background:#fffcec;border:1px solid #fdd5c9;margin:0 0 5px;width:100%}
1
+ .wpacu_update_btn,label.wpacu_add_global{cursor:pointer!important}#wpacu-pages-info-area{width:98%}#wpacu-logo-area{margin:15px 0;padding:0 10px}#wpacu-logo-area img{max-width:340px;width:100%}.wpacu-tabs{position:relative;overflow:hidden;margin:0 auto 20px;width:100%;font-weight:300;font-size:20px}.wpacu-tabs nav{text-align:center}.wpacu-tabs nav ul{position:relative;display:-ms-flexbox;display:-webkit-flex;display:-moz-flex;display:-ms-flex;display:flex;margin:0 auto;padding:0;max-width:1200px;list-style:none;-ms-box-orient:horizontal;-ms-box-pack:center;-webkit-flex-flow:row wrap;-moz-flex-flow:row wrap;-ms-flex-flow:row wrap;flex-flow:row wrap;-webkit-justify-content:center;-moz-justify-content:center;-ms-justify-content:center;justify-content:center}.wpacu-tabs nav ul li{position:relative;z-index:1;display:block;margin:0;text-align:center;-webkit-flex:1;-moz-flex:1;-ms-flex:1;flex:1}.wpacu-tabs nav ul li span.license-status{position:absolute;font-size:12px;left:0;right:50%;bottom:12px;-webkit-transform:translate(50%,50%);transform:translate(50%,50%)}.wpacu-tabs nav ul li span.license-status.inactive{color:#c00}.wpacu-tabs nav ul li span.license-status.active{color:green}.wpacu-tabs nav a{position:relative;display:block;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;line-height:2.5}.wpacu-tabs nav a span{vertical-align:middle;font-size:14px}.wpacu-tabs nav li.wpacu-tab-current a{color:#74777b}.wpacu-tabs nav a:focus{outline:0}.wpacu-tabs .dashicons{font-size:20px}.no-js .wpacu-content-wrap section{display:block;padding-bottom:2em;border-bottom:1px solid rgba(255,255,255,.6)}.no-flexbox nav ul{display:block}.no-flexbox nav ul li{min-width:15%;display:inline-block}@media screen and (max-width:58em){.wpacu-tabs nav a.icon span{display:none}.wpacu-tabs nav a:before{margin-right:0}}.wpacu-tabs-style-underline nav{background:#fff}.wpacu-tabs-style-underline nav a{color:#74777b;font-size:18px;text-transform:uppercase;text-decoration:none;padding:.25em 0 .5em;border-left:1px solid #e7ecea;-webkit-transition:color .2s;transition:color .2s}.wpacu-tabs-style-underline nav li:last-child a{border-right:1px solid #e7ecea}.wpacu-tabs-style-underline nav li a::after{position:absolute;bottom:0;left:0;width:100%;height:6px;background:#2CC185;content:'';-webkit-transition:-webkit-transform .3s;transition:transform .3s;-webkit-transform:translate3d(0,150%,0);transform:translate3d(0,150%,0)}.wpacu-tabs-style-underline nav li.wpacu-tab-current a::after{-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}.wpacu-tabs-style-underline nav a span{font-weight:700}.wpacu-tabs-style-topline{max-width:1200px;margin:20px auto 30px 0;width:100%;font-weight:300;text-decoration:none}.wpacu-tabs-style-topline nav li{border:1px solid rgba(40,44,42,.1)}.wpacu-tabs-style-topline nav li:not(:last-child){border-right:none}.wpacu-tabs-style-topline nav li.wpacu-tab-current{border-top-color:#0085ba;border-bottom:none}.wpacu-tabs-style-topline nav a{padding:20px 10px;background:rgba(40,44,42,.05);color:#74777b;line-height:1;-webkit-transition:color .2s;transition:color .2s;text-decoration:none}.wpacu-tabs-style-topline nav a:focus,.wpacu-tabs-style-topline nav a:hover{color:#0085ba}.wpacu-tabs-style-topline nav li.wpacu-tab-current a{background:0 0;box-shadow:inset 0 3px 0 #0085ba;color:#0085ba}.wpacu-tabs-style-topline nav li.wpacu-tab-current a span:not(.dashicons){font-weight:600}.wpacu-tabs-style-topline nav a span{text-transform:uppercase;font-weight:400}.wpacu-script-attributes-area{margin:20px 0 0!important}.wpacu-script-attributes-area.wpacu-lite ul.wpacu-script-attributes-settings{padding:5px 7px!important}.wpacu-script-attributes-area.wpacu-lite ul.wpacu-script-attributes-settings.wpacu-first{margin:0 28px 0 0!important}.wpacu-script-attributes-area.wpacu-lite ul.wpacu-script-attributes-settings label{margin:0!important}.wpacu-script-attributes-area.wpacu-lite ul.wpacu-script-attributes-settings li{margin:0 16px 0 0!important}.wpacu-script-attributes-area.wpacu-lite ul.wpacu-script-attributes-settings li:first-child{margin:0 10px 0 0!important}.wpacu-script-attributes-area.wpacu-lite ul.wpacu-script-attributes-settings li:last-child{margin:0 2px 0 0!important}.wpacu-script-attributes-area .wpacu-script-attributes-settings{background:#fff!important;border:1px solid #eee!important;-webkit-border-radius:5px;-moz-border-radius:5px;border-radius:5px;padding:5px 10px!important;list-style:none;display:inline-block;margin:0}.wpacu-script-attributes-area .wpacu-script-attributes-settings input[type=radio]{padding:0;margin:0 5px 0 0}.wpacu-script-attributes-area .wpacu-script-attributes-settings.wpacu-first{margin:0 34px 0 0!important}.wpacu-script-attributes-area .wpacu-script-attributes-settings li{float:left;margin:0 17px 0 0}.wpacu-script-attributes-area .wpacu-script-attributes-settings li:first-child{margin:0 10px 0 0!important}.wpacu-script-attributes-area .wpacu-script-attributes-settings li:last-child{margin:0}.wpacu-script-attributes-area .wpacu-script-attributes-settings:hover{background:#f2faf2}.wpacu-license-area .notice,.wpacu-settings-area .notice{margin-left:0;padding:10px!important;width:97%;box-sizing:border-box;font-weight:600;font-style:italic}.wpacu-license-area .notice .dashicons,.wpacu-settings-area .notice .dashicons{color:green}tr.wpacu_not_load td,tr.wpacu_not_load th{background:#FFE1E1!important}.dashicons-wpacu{width:17px;height:17px;margin:2px 0 0;font-size:16px;text-decoration:none}.wpacu_widefat .check-column.wpacu_check{padding:10px}body[class*=wpassetcleanup] #footer-left{border-left:2px solid green;padding-left:10px}#wpacu_meta_box_content{width:99%}#wpacu_meta_box_content h3{margin-left:0;padding-left:0}#wpacu_meta_box_content div.wpacu_note{padding:10px;background-color:#f5f5f5}#wpacu_meta_box_content .dashicons.dashicons-warning{color:#c00}#wpacu_meta_box_content div.wpacu_warning,#wpacu_wrap_assets div.wpacu_warning{border:1px solid #c00}#wpacu_meta_box_content div.wpacu_verified{padding:10px;background-color:#f5f5f5;border-left:1px solid #ddd}#wpacu_meta_box_content div.wpacu_verified span{color:#0073aa;font-weight:700}#wpacu_meta_box_content ul.wpacu_asset_options{margin:0;padding:0}#wpacu_meta_box_content ul.wpacu_asset_options li{list-style:none;display:inline-block}#wpacu_wrap_assets img,#wpacu_wrap_assets small,.wpacu_asset_row td label{display:inline-block!important}#wpacu_meta_box_content ul.wpacu_asset_options label{font-weight:400;margin-right:5px;margin-top:5px;margin-bottom:5px}.wpacu-updated-frontend{font-weight:400;background-color:#fff;border-left:4px solid #46b450;border-top:1px solid #46b450;border-right:1px solid #46b450;border-bottom:1px solid #46b450;padding:9px 14px;-webkit-border-radius:5px;-moz-border-radius:5px;border-radius:5px}.wpacu-updated-frontend span.dashicons-yes{color:#46b450;font-size:26px}#wpacu_wrap_assets{color:#000!important;text-align:left!important;clear:both!important;width:90%!important;padding:20px!important;margin:30px 0 0 15px!important;border:1px solid #cdcdcd;z-index:10000!important;position:relative!important;background-color:#fff!important}#wpacu_wrap_assets small{margin:0!important;letter-spacing:normal!important}#wpacu_wrap_assets input[type=checkbox]{margin-right:4px!important}#wpacu_wrap_assets p{margin:10px 0!important;line-height:22px!important;text-align:left!important;font-weight:400!important}#wpacu_wrap_assets p:first-child,.wpacu_verified{margin:0 0 10px!important}#wpacu_wrap_assets h1{margin:0 0 20px!important;font-size:160%!important;text-align:left!important}#wpacu_wrap_assets h3{margin:20px 0;font-size:130%}.wpacu_asset_row td{padding:10px!important;border-left:1px solid #cdcdcd;border-right:1px solid #cdcdcd;border-bottom:1px solid #cdcdcd}.wpacu_asset_row td:first-child{border-top:1px solid #cdcdcd}.wpacu_asset_row td p{padding:0!important;margin-bottom:10px!important;word-wrap:break-word!important;-ms-word-break:break-all;word-break:break-word}.wpacu_asset_row td label{margin:0!important;cursor:pointer!important;font-size:100%!important;font-weight:400!important}.wpacu_asset_row td label span:not(.dashicons){font-size:100%!important}.wpacu_asset_row td.wpacu_check{width:50px!important}.asset-table-extra-info td{padding:5px!important}body.wp-admin .wpacu_asset_row td.wpacu_check{width:25px!important}.wpacu_asset_row .dashicons-warning{color:red!important}label.wpacu_global_checked{font-weight:700;color:#d54e21}label.wpacu_global_unload_exception{font-weight:700!important}label.wpacu_bulk_unload_active{color:#d54e21}tr.wpacu_global_rule_row.selected{background:#e7e7e7}.nav-tab-wrapper{border-bottom:1px solid #ccc;width:95%}ul.wpacu_asset_options{margin:0 10px 0 0;padding:0}ul.wpacu_asset_options li{margin:0 20px 0 0;list-style:none;display:inline-block}ul.wpacu_asset_options li.remove_rule label{font-weight:700}ul.wpacu_asset_options label{margin-right:20px;font-weight:400}ul.wpacu_asset_options label input{margin-right:4px}.wpacu_list_table,.wpacu_list_table div,.wpacu_list_table li,.wpacu_list_table p{font-weight:400}.wpacu_widefat{width:99%;border:1px solid #e5e5e5!important}.wpacu_striped>tbody>:nth-child(even),ul.wpacu_striped>:nth-child(even){background-color:#fff!important}.wpacu_striped>tbody>:nth-child(odd),ul.wpacu_striped>:nth-child(odd){background-color:#f9f9f9!important}.wpacu_update_btn{-moz-box-shadow:inset 0 1px 0 0 #54a3f7!important;-webkit-box-shadow:inset 0 1px 0 0 #54a3f7!important;box-shadow:inset 0 1px 0 0 #54a3f7!important;background:-webkit-gradient(linear,left top,left bottom,color-stop(.05,#007dc1),color-stop(1,#0061a7))!important;background:-moz-linear-gradient(top,#007dc1 5%,#0061a7 100%)!important;background:-webkit-linear-gradient(top,#007dc1 5%,#0061a7 100%)!important;background:-o-linear-gradient(top,#007dc1 5%,#0061a7 100%)!important;background:-ms-linear-gradient(top,#007dc1 5%,#0061a7 100%)!important;background:linear-gradient(to bottom,#007dc1 5%,#0061a7 100%)!important;filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#007dc1', endColorstr='#0061a7', GradientType=0)!important;background-color:#007dc1!important;-moz-border-radius:3px!important;-webkit-border-radius:3px!important;border-radius:3px!important;border:1px solid #124d77!important;display:inline-block!important;color:#fff!important;font-size:15px!important;padding:10px 24px!important;text-decoration:none!important;text-shadow:0 1px 0 #154682!important;height:auto!important;line-height:normal!important}.wpacu-btns .btn:focus,.wpacu-btns .btn:hover,.wpacu-btns a{text-decoration:none}.wpacu_update_btn:hover{background:-webkit-gradient(linear,left top,left bottom,color-stop(.05,#0061a7),color-stop(1,#007dc1))!important;background:-moz-linear-gradient(top,#0061a7 5%,#007dc1 100%)!important;background:-webkit-linear-gradient(top,#0061a7 5%,#007dc1 100%)!important;background:-o-linear-gradient(top,#0061a7 5%,#007dc1 100%)!important;background:-ms-linear-gradient(top,#0061a7 5%,#007dc1 100%)!important;background:linear-gradient(to bottom,#0061a7 5%,#007dc1 100%)!important;filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#0061a7', endColorstr='#007dc1', GradientType=0)!important;background-color:#0061a7!important}.wpacu_update_btn:active{position:relative!important;top:1px!important}ul.hire-reasons{margin-bottom:20px}ul.hire-reasons li{font-size:14px;line-height:22px;margin-bottom:12px}ul.hire-reasons li .dashicons{font-size:23px;line-height:23px}ul.hire-reasons li .dashicons.dashicons-yes{color:green}ol.getting-started li{font-size:14px;line-height:22px;margin-bottom:8px}.wpacu-get-help-wrap p{font-size:14px}.wpacu-btns .btn{display:inline-block;font-weight:400;line-height:1.25;text-align:center;white-space:nowrap;vertical-align:middle;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;border:1px solid transparent;padding:.5rem 1rem;font-size:1rem;-webkit-border-radius:.25rem;-moz-border-radius:.25rem;border-radius:.25rem;-webkit-transition:all .2s ease-in-out;-o-transition:all .2s ease-in-out;transition:all .2s ease-in-out}.wpacu-btns .btn.focus,.wpacu-btns .btn:focus{outline:0;-webkit-box-shadow:0 0 0 2px rgba(2,117,216,.25);box-shadow:0 0 0 2px rgba(2,117,216,.25)}.wpacu-btns .btn.disabled,.wpacu-btns .btn:disabled{cursor:not-allowed;opacity:.65}.wpacu-btns .btn.active,.wpacu-btns .btn:active{background-image:none}.wpacu-btns a.btn.disabled,.wpacu-btns fieldset[disabled] a.btn{pointer-events:none}.wpacu-btns .btn-primary{color:#fff;background-color:#0275d8;border-color:#0275d8}.wpacu-btns .btn-primary:hover{color:#fff;background-color:#025aa5;border-color:#01549b}.wpacu-btns .btn-primary.focus,.wpacu-btns .btn-primary:focus{-webkit-box-shadow:0 0 0 2px rgba(2,117,216,.5);box-shadow:0 0 0 2px rgba(2,117,216,.5)}.wpacu-btns .btn-primary.disabled,.wpacu-btns .btn-primary:disabled{background-color:#0275d8;border-color:#0275d8}.wpacu-btns .btn-primary.active,.wpacu-btns .btn-primary:active,.wpacu-btns .show>.btn-primary.dropdown-toggle{color:#fff;background-color:#025aa5;background-image:none;border-color:#01549b}.wpacu-btns .btn-secondary{color:#292b2c;background-color:#fff;border-color:#ccc}.wpacu-btns .btn-secondary:hover{color:#292b2c;background-color:#e6e6e6;border-color:#adadad}.wpacu-btns .btn-secondary.focus,.wpacu-btns .btn-secondary:focus{-webkit-box-shadow:0 0 0 2px rgba(204,204,204,.5);box-shadow:0 0 0 2px rgba(204,204,204,.5)}.wpacu-btns .btn-secondary.disabled,.wpacu-btns .btn-secondary:disabled{background-color:#fff;border-color:#ccc}.wpacu-btns .btn-secondary.active,.wpacu-btns .btn-secondary:active,.wpacu-btns .show>.btn-secondary.dropdown-toggle{color:#292b2c;background-color:#e6e6e6;background-image:none;border-color:#adadad}.wpacu-btns .btn-info{color:#fff;background-color:#5bc0de;border-color:#5bc0de}.wpacu-btns .btn-info:hover{color:#fff;background-color:#31b0d5;border-color:#2aabd2}.wpacu-btns .btn-info.focus,.wpacu-btns .btn-info:focus{-webkit-box-shadow:0 0 0 2px rgba(91,192,222,.5);box-shadow:0 0 0 2px rgba(91,192,222,.5)}.wpacu-btns .btn-info.disabled,.wpacu-btns .btn-info:disabled{background-color:#5bc0de;border-color:#5bc0de}.wpacu-btns .btn-info.active,.wpacu-btns .btn-info:active,.wpacu-btns .show>.btn-info.dropdown-toggle{color:#fff;background-color:#31b0d5;background-image:none;border-color:#2aabd2}.wpacu-btns .btn-success{color:#fff;background-color:#5cb85c;border-color:#5cb85c}.wpacu-btns .btn-success:hover{color:#fff;background-color:#449d44;border-color:#419641}.wpacu-btns .btn-success.focus,.wpacu-btns .btn-success:focus{-webkit-box-shadow:0 0 0 2px rgba(92,184,92,.5);box-shadow:0 0 0 2px rgba(92,184,92,.5)}.wpacu-btns .btn-success.disabled,.wpacu-btns .btn-success:disabled{background-color:#5cb85c;border-color:#5cb85c}.wpacu-btns .btn-success.active,.wpacu-btns .btn-success:active,.wpacu-btns .show>.btn-success.dropdown-toggle{color:#fff;background-color:#449d44;background-image:none;border-color:#419641}.wpacu-btns .btn-warning{color:#fff;background-color:#f0ad4e;border-color:#f0ad4e}.wpacu-btns .btn-warning:hover{color:#fff;background-color:#ec971f;border-color:#eb9316}.wpacu-btns .btn-warning.focus,.wpacu-btns .btn-warning:focus{-webkit-box-shadow:0 0 0 2px rgba(240,173,78,.5);box-shadow:0 0 0 2px rgba(240,173,78,.5)}.wpacu-btns .btn-warning.disabled,.wpacu-btns .btn-warning:disabled{background-color:#f0ad4e;border-color:#f0ad4e}.wpacu-btns .btn-warning.active,.wpacu-btns .btn-warning:active,.wpacu-btns .show>.btn-warning.dropdown-toggle{color:#fff;background-color:#ec971f;background-image:none;border-color:#eb9316}.wpacu-btns .btn-danger{color:#fff;background-color:#d9534f;border-color:#d9534f}.wpacu-btns .btn-danger:hover{color:#fff;background-color:#c9302c;border-color:#c12e2a}.wpacu-btns .btn-danger.focus,.wpacu-btns .btn-danger:focus{-webkit-box-shadow:0 0 0 2px rgba(217,83,79,.5);box-shadow:0 0 0 2px rgba(217,83,79,.5)}.wpacu-btns .btn-danger.disabled,.wpacu-btns .btn-danger:disabled{background-color:#d9534f;border-color:#d9534f}.wpacu-btns .btn-danger.active,.wpacu-btns .btn-danger:active,.wpacu-btns .show>.btn-danger.dropdown-toggle{color:#fff;background-color:#c9302c;background-image:none;border-color:#c12e2a}.wpacu-btns .btn-outline-primary{color:#0275d8;background-image:none;background-color:transparent;border-color:#0275d8}.wpacu-btns .btn-outline-primary:hover{color:#fff;background-color:#0275d8;border-color:#0275d8}.wpacu-btns .btn-outline-primary.focus,.wpacu-btns .btn-outline-primary:focus{-webkit-box-shadow:0 0 0 2px rgba(2,117,216,.5);box-shadow:0 0 0 2px rgba(2,117,216,.5)}.wpacu-btns .btn-outline-primary.disabled,.wpacu-btns .btn-outline-primary:disabled{color:#0275d8;background-color:transparent}.wpacu-btns .btn-outline-primary.active,.wpacu-btns .btn-outline-primary:active,.wpacu-btns .show>.btn-outline-primary.dropdown-toggle{color:#fff;background-color:#0275d8;border-color:#0275d8}.wpacu-btns .btn-outline-secondary{color:#ccc;background-image:none;background-color:transparent;border-color:#ccc}.wpacu-btns .btn-outline-secondary:hover{color:#fff;background-color:#ccc;border-color:#ccc}.wpacu-btns .btn-outline-secondary.focus,.wpacu-btns .btn-outline-secondary:focus{-webkit-box-shadow:0 0 0 2px rgba(204,204,204,.5);box-shadow:0 0 0 2px rgba(204,204,204,.5)}.wpacu-btns .btn-outline-secondary.disabled,.wpacu-btns .btn-outline-secondary:disabled{color:#ccc;background-color:transparent}.wpacu-btns .btn-outline-secondary.active,.wpacu-btns .btn-outline-secondary:active,.wpacu-btns .show>.btn-outline-secondary.dropdown-toggle{color:#fff;background-color:#ccc;border-color:#ccc}.wpacu-btns .btn-outline-info{color:#5bc0de;background-image:none;background-color:transparent;border-color:#5bc0de}.wpacu-btns .btn-outline-info:hover{color:#fff;background-color:#5bc0de;border-color:#5bc0de}.wpacu-btns .btn-outline-info.focus,.wpacu-btns .btn-outline-info:focus{-webkit-box-shadow:0 0 0 2px rgba(91,192,222,.5);box-shadow:0 0 0 2px rgba(91,192,222,.5)}.wpacu-btns .btn-outline-info.disabled,.wpacu-btns .btn-outline-info:disabled{color:#5bc0de;background-color:transparent}.wpacu-btns .btn-outline-info.active,.wpacu-btns .btn-outline-info:active,.wpacu-btns .show>.btn-outline-info.dropdown-toggle{color:#fff;background-color:#5bc0de;border-color:#5bc0de}.wpacu-btns .btn-outline-success{color:#5cb85c;background-image:none;background-color:transparent;border-color:#5cb85c}.wpacu-btns .btn-outline-success:hover{color:#fff;background-color:#5cb85c;border-color:#5cb85c}.wpacu-btns .btn-outline-success.focus,.wpacu-btns .btn-outline-success:focus{-webkit-box-shadow:0 0 0 2px rgba(92,184,92,.5);box-shadow:0 0 0 2px rgba(92,184,92,.5)}.wpacu-btns .btn-outline-success.disabled,.wpacu-btns .btn-outline-success:disabled{color:#5cb85c;background-color:transparent}.wpacu-btns .btn-outline-success.active,.wpacu-btns .btn-outline-success:active,.wpacu-btns .show>.btn-outline-success.dropdown-toggle{color:#fff;background-color:#5cb85c;border-color:#5cb85c}.wpacu-btns .btn-outline-warning{color:#f0ad4e;background-image:none;background-color:transparent;border-color:#f0ad4e}.wpacu-btns .btn-outline-warning:hover{color:#fff;background-color:#f0ad4e;border-color:#f0ad4e}.wpacu-btns .btn-outline-warning.focus,.wpacu-btns .btn-outline-warning:focus{-webkit-box-shadow:0 0 0 2px rgba(240,173,78,.5);box-shadow:0 0 0 2px rgba(240,173,78,.5)}.wpacu-btns .btn-outline-warning.disabled,.wpacu-btns .btn-outline-warning:disabled{color:#f0ad4e;background-color:transparent}.wpacu-btns .btn-outline-warning.active,.wpacu-btns .btn-outline-warning:active,.wpacu-btns .show>.btn-outline-warning.dropdown-toggle{color:#fff;background-color:#f0ad4e;border-color:#f0ad4e}.wpacu-btns .btn-outline-danger{color:#d9534f;background-image:none;background-color:transparent;border-color:#d9534f}.wpacu-btns .btn-outline-danger:hover{color:#fff;background-color:#d9534f;border-color:#d9534f}.wpacu-btns .btn-outline-danger.focus,.wpacu-btns .btn-outline-danger:focus{-webkit-box-shadow:0 0 0 2px rgba(217,83,79,.5);box-shadow:0 0 0 2px rgba(217,83,79,.5)}.wpacu-btns .btn-outline-danger.disabled,.wpacu-btns .btn-outline-danger:disabled{color:#d9534f;background-color:transparent}.wpacu-btns .btn-outline-danger.active,.wpacu-btns .btn-outline-danger:active,.wpacu-btns .show>.btn-outline-danger.dropdown-toggle{color:#fff;background-color:#d9534f;border-color:#d9534f}.wpacu-btns .btn-link{font-weight:400;color:#0275d8;-webkit-border-radius:0;-moz-border-radius:0;border-radius:0}.wpacu-btns .btn-link,.wpacu-btns .btn-link.active,.wpacu-btns .btn-link:active,.wpacu-btns .btn-link:disabled{background-color:transparent}.wpacu-btns .btn-link,.wpacu-btns .btn-link:active,.wpacu-btns .btn-link:focus,.wpacu-btns .btn-link:hover{border-color:transparent}.wpacu-btns .btn-link:focus,.wpacu-btns .btn-link:hover{color:#014c8c;text-decoration:underline;background-color:transparent}.wpacu-btns .btn-link:disabled{color:#636c72}.wpacu-btns .btn-link:disabled:focus,.wpacu-btns .btn-link:disabled:hover{text-decoration:none}.wpacu-btns .btn-group-lg>.btn,.wpacu-btns .btn-lg{padding:.75rem 1.5rem;font-size:1.25rem;-webkit-border-radius:.3rem;-moz-border-radius:.3rem;border-radius:.3rem}.wpacu-btns .btn-group-sm>.btn,.wpacu-btns .btn-sm{padding:.25rem .5rem;font-size:.875rem;-webkit-border-radius:.2rem;-moz-border-radius:.2rem;border-radius:.2rem}.wpacu-btns .btn-block{display:block;width:100%}.go-pro-button,.go-pro-link-no-style,.wordpress-core-file{display:inline-block;text-decoration:none}.wpacu-btns .btn-block+.btn-block{margin-top:.5rem}.wpacu-btns input[type=submit].btn-block,.wpacu-btns input[type=reset].btn-block,.wpacu-btns input[type=button].btn-block{width:100%}.wpacu-btns .fade{opacity:0;-webkit-transition:opacity .15s linear;-o-transition:opacity .15s linear;transition:opacity .15s linear}.go-pro-button{background-color:#4CAF50;border:2px solid #4CAF50;color:#fff;padding:12px 18px;text-align:center;font-size:16px;margin:4px 2px;cursor:pointer;-webkit-border-radius:10px;-moz-border-radius:10px;border-radius:10px;-webkit-transition-duration:.4s;transition-duration:.4s}.go-pro-button:hover{background-color:#fff;color:#000;border:2px solid #4CAF50}body.wp-admin .wpacu_asset_row .go-pro-link-no-style .tooltip{padding:8px 2px}body.wp-admin .go-pro-link-no-style .tooltip{font-weight:400;font-size:12px;line-height:16px;padding:8px 11px}.go-pro-link-no-style{position:relative;color:inherit}.go-pro-link-no-style:not(.nav-tab){font-style:italic}.go-pro-link-no-style.no-transition .tooltip{-webkit-transition:0s all ease;transition:0s all ease;-webkit-transition-delay:0s;transition-delay:0s}.go-pro-link-no-style .tooltip{font-size:14px;line-height:18px;position:absolute;z-index:1;visibility:hidden;width:130px;background-color:#27ae60;color:#fff;text-align:center;padding:8px;-webkit-border-radius:6px;-moz-border-radius:6px;border-radius:6px;bottom:100%;bottom:calc(100% + 15px);left:50%;margin-left:-65px;-webkit-transition:.2s all ease;transition:.2s all ease;-webkit-transition-delay:0s;transition-delay:0s}.go-pro-link-no-style .tooltip:after{content:"";position:absolute;border-width:10px;border-style:solid;top:100%;left:50%;left:calc(50% - 10px);border-color:#27ae60 transparent transparent}.go-pro-link-no-style .tooltip.wpacu-larger{width:240px}.go-pro-link-no-style:hover .tooltip{visibility:visible}.wordpress-core-file{position:relative;font-style:normal;color:inherit}.wordpress-core-file .tooltip{font-size:14px;line-height:18px;position:absolute;z-index:1;visibility:hidden;width:160px;background-color:#c00;color:#fff;text-align:center;padding:6px;-webkit-border-radius:6px;-moz-border-radius:6px;border-radius:6px;bottom:100%;bottom:calc(100% + 15px);left:50%;margin-left:-86px;-webkit-transition:.5s all ease;transition:.5s all ease;-webkit-transition-delay:0s;transition-delay:0s}.wordpress-core-file .tooltip:after{content:"";position:absolute;border-width:10px;border-style:solid;top:100%;left:50%;left:calc(50% - 10px);border-color:#c00 transparent transparent}.wordpress-core-file:hover .tooltip{visibility:visible}.pro-page-unlock-notice{-webkit-border-radius:10px;-moz-border-radius:10px;border-radius:10px;padding:10px;background:#f2faf2;border:1px solid #c0e8c0;width:auto;display:block}.wpacu-get-help-wrap .help-content-wrap{margin:20px 0 25px;display:-webkit-box;display:-moz-box;display:-ms-flexbox;display:-moz-flex;display:-webkit-flex;display:flex;width:100%}@media only screen and (min-width:768px){.wpacu-get-help-wrap .help-content-wrap{-webkit-box-align:start;-ms-flex-align:start;-ms-grid-row-align:flex-start;align-items:flex-start}}@media only screen and (max-width:767px){.wpacu-get-help-wrap .help-content-wrap{-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column}}.wpacu-get-help-wrap .help-content-wrap .help-content{padding:15px;background:#fff;border:1px solid #E0E0E0;-webkit-border-radius:10px;-moz-border-radius:10px;border-radius:10px;box-shadow:2px 3px 8px 0 #231f2038;flex:1 1 50%;margin-bottom:10px}.wpacu-get-help-wrap .help-content-wrap .help-content .content-title{margin:5px 0 15px}.wpacu-get-help-wrap .help-content-wrap .help-content .cols-wrap{width:100%;margin:0 auto;display:table}.wpacu-get-help-wrap .help-content-wrap .help-content .wpacu-image-area{margin:5px 0 0;position:relative}.wpacu-get-help-wrap .help-content-wrap .help-content .wpacu-image-area.col-left{float:left;width:48%}.wpacu-get-help-wrap .help-content-wrap .help-content .wpacu-image-area.col-right{float:right;width:48%}.wpacu-get-help-wrap .help-content-wrap .help-content .wpacu-image-area.activate-plugin img{max-width:280px}.wpacu-get-help-wrap .help-content-wrap .help-content .wpacu-image-area img{width:99%;margin:8px 0;border:1px solid #cdcdcd;-webkit-border-radius:10px;-moz-border-radius:10px;border-radius:10px}.wpacu-get-help-wrap .help-content-wrap .help-content.upgrade-help{order:2}@media only screen and (min-width:768px){.wpacu-get-help-wrap .help-content-wrap .help-content{margin-right:24px}.wpacu-get-help-wrap .help-content-wrap .help-content.upgrade-help{margin-right:0}}.wpacu-get-help-wrap .help-content-wrap .help-content.tech-help{order:1}@media only screen and (max-width:767px){.wpacu-get-help-wrap .help-content-wrap .help-content.tech-help{margin-bottom:30px}}.wpacu-get-help-wrap .help-content-wrap .help-content ul{list-style:none;padding-left:0}.wpacu-get-help-wrap .help-content-wrap .help-content ul li{font-size:14px;line-height:22px;margin-bottom:15px}.wpacu-get-help-wrap .help-content-wrap .help-content p:last-child,.wpacu-get-help-wrap .help-content-wrap .help-content ul li:last-child{margin-bottom:0}.ajax-direct-call-error-area{background:#fff;padding:10px;border:1px solid #c00;-webkit-border-radius:8px;-moz-border-radius:8px;border-radius:8px}.ajax-direct-call-error-area td{padding:6px}.ajax-direct-call-error-area .note{margin-top:0;padding-top:0}.ajax-direct-call-error-area .error-code{color:#c00;font-weight:700}.ajax-wp-remote-post-call-error-area{background:#fff;padding:20px}.ajax-wp-remote-post-call-error-area .table-data{background:0 0;border:1px solid #c66;-webkit-border-radius:10px;-moz-border-radius:10px;border-radius:10px}.ajax-wp-remote-post-call-error-area .table-data td{padding:10px}.wrap-upgrade-info{background:#fff;padding:0 15px;border:1px solid #cdcdcd;-webkit-border-radius:10px;-moz-border-radius:10px;border-radius:10px}.wpacu-notice{margin:20px 20px 0 0!important}.wpacu-notice p{font-weight:600!important;font-style:italic}.wpacu-notice .dashicons{font-weight:inherit!important;color:#46b450!important;margin-right:4px;vertical-align:text-bottom;font-size:27px}.wpacu-wrap.wpacu-switch-standard .toplevel_page_wpassetcleanup_settings .wpacu_switch{width:auto;height:auto}.wpacu-wrap.wpacu-switch-standard .toplevel_page_wpassetcleanup_settings .wpacu_switch input{display:block}.wpacu-wrap.wpacu-switch-standard .toplevel_page_wpassetcleanup_settings .wpacu_switch input[type=checkbox]{margin:0 0 3px}.wpacu-wrap.wpacu-switch-standard .toplevel_page_wpassetcleanup_settings .wpacu_switch .wpacu_slider{display:none}.wpacu-wrap.wpacu-switch-enhanced .wpacu_switch{position:relative;display:inline-block;width:52px;height:29px}.wpacu-wrap.wpacu-switch-enhanced .wpacu_switch input{display:none}.wpacu-wrap.wpacu-switch-enhanced .wpacu_switch:not(.wpacu_switch_standard) .wpacu_slider{position:absolute;cursor:pointer;top:0;left:0;right:0;bottom:0;background-color:#ccc;-webkit-transition:.2s;transition:.2s}.wpacu-wrap.wpacu-switch-enhanced .wpacu_switch:not(.wpacu_switch_standard) .wpacu_slider:before{position:absolute;content:'';height:22px;width:22px;left:4px;bottom:4px;background-color:#fff;-webkit-transition:.2s;transition:.2s}.wpacu-wrap.wpacu-switch-enhanced .wpacu_switch input:checked+.wpacu_slider{background-color:#52af00}.wpacu-wrap.wpacu-switch-enhanced .wpacu_switch input:focus+.wpacu_slider{box-shadow:0 0 1px #52af00}.wpacu-wrap.wpacu-switch-enhanced .wpacu_switch input:checked+.wpacu_slider:before{-webkit-transform:translateX(22px);-ms-transform:translateX(22px);transform:translateX(22px)}.wpacu-wrap.wpacu-switch-enhanced .wpacu_switch .wpacu_slider.wpacu_round{border-radius:29px}.wpacu-wrap.wpacu-switch-enhanced .wpacu_switch .wpacu_slider.wpacu_round:before{border-radius:50%}.wpacu-wrap.wpacu-switch-enhanced .wpacu_switch.wpacu_locked_for_pro{opacity:.3}.toplevel_page_wpassetcleanup_settings .setting_title p.wpacu_subtitle{margin-top:2px;line-height:1.2}.toplevel_page_wpassetcleanup_settings .setting_title p.wpacu_subtitle em{font-weight:200}.toplevel_page_wpassetcleanup_settings .setting_title p.wpacu_read_more{margin-top:-10px;font-style:italic;font-size:12px}.toplevel_page_wpassetcleanup_settings .assets_list_inline_code_status_choices,.toplevel_page_wpassetcleanup_settings .assets_list_layout_areas_status_choices,.toplevel_page_wpassetcleanup_settings .input_style_choices{list-style:none;margin:0}.toplevel_page_wpassetcleanup_settings .assets_list_inline_code_status_choices li,.toplevel_page_wpassetcleanup_settings .assets_list_layout_areas_status_choices li,.toplevel_page_wpassetcleanup_settings .input_style_choices li{float:left;margin-right:30px}.toplevel_page_wpassetcleanup_settings .assets_list_inline_code_status_choices li:last-child,.toplevel_page_wpassetcleanup_settings .assets_list_layout_areas_status_choices li:last-child,.toplevel_page_wpassetcleanup_settings .input_style_choices li:last-child{margin-right:0}.wpacu-form-table th{vertical-align:top;text-align:left;padding:5px 20px 20px 0;width:200px;line-height:1.3;font-weight:600}.wpacu-form-table th label{color:#23282d;font-weight:600;font-size:14px;text-shadow:none;vertical-align:middle;cursor:pointer}.wpacu-form-table td{padding-bottom:25px}.wpacu-form-table td:last-child p:last-child{margin:0}.wrap_bulk_unload_options{width:auto;display:inline-flex;-webkit-flex-wrap:wrap;flex-wrap:wrap}.wrap_bulk_unload_options .wpacu_asset_options_wrap{flex-grow:1;margin:5px 10px 14px 0!important;padding:8px 10px 2px!important}.wrap_bulk_unload_options .wpacu_asset_options_wrap .wpacu_slider_text{position:relative!important;vertical-align:top;left:inherit!important;top:inherit!important}.wpacu-switch-enhanced .wpacu_asset_row .wpacu_switch{position:relative;display:inline-block;width:52px;height:29px;margin-right:5px!important}.wpacu-switch-enhanced .wpacu_asset_row .wpacu_switch input{display:none}.wpacu-switch-enhanced .wpacu_asset_row .wpacu_switch .wpacu_slider{position:absolute;cursor:pointer;top:0;left:0;right:0;bottom:0;background-color:#ccc;-webkit-transition:.2s;transition:.2s}.wpacu-switch-enhanced .wpacu_asset_row .wpacu_switch .wpacu_slider:before{position:absolute;content:'';height:22px;width:22px;left:4px;bottom:4px;background-color:#fff;-webkit-transition:.2s;transition:.2s}.wpacu-switch-enhanced .wpacu_asset_row .wpacu_switch input:checked+.wpacu_slider{background-color:#C00}.wpacu-switch-enhanced .wpacu_asset_row .wpacu_switch input:focus+.wpacu_slider{box-shadow:0 0 1px #C00}.wpacu-switch-enhanced .wpacu_asset_row .wpacu_switch input:checked+.wpacu_slider:before{-webkit-transform:translateX(22px);-ms-transform:translateX(22px);transform:translateX(22px)}.wpacu-switch-enhanced .wpacu_asset_row .wpacu_switch .wpacu_slider.wpacu_round{border-radius:29px}.wpacu-switch-enhanced .wpacu_asset_row .wpacu_switch .wpacu_slider.wpacu_round:before{border-radius:50%}.wpacu-switch-enhanced .wpacu_asset_row .wpacu_slider_text{position:absolute;left:62px;top:3px}.wpacu_asset_row td{width:100%}.wpacu_asset_row td p:last-child{margin-bottom:0!important}.wpacu_asset_row td .wpacu_asset_options_wrap{padding:8px 10px;margin:15px 0;background:#fff;border:1px solid #eee;-webkit-border-radius:5px;-moz-border-radius:5px;border-radius:5px}.wpacu_asset_row td .wpacu_asset_options_wrap p:first-child{margin-top:0;margin-bottom:8px!important}.wpacu_asset_row p.wpacu_handle{margin-top:0}.wpacu_asset_row li.wpacu_unload_this_page{display:block!important;position:relative}.wpacu-clearfix:after,.wpacu-clearfix:before{content:'';display:table}.wpacu-contract-expand-area{width:100%;margin:20px 0 0}.wpacu-contract-expand-area .col-left{float:left}.wpacu-contract-expand-area .col-left h4{margin-bottom:0}.wpacu-contract-expand-area .col-right{float:right}.wpacu-clearfix:after{clear:both}.wpacu-wp-button{color:#555;border-color:#ccc;background:#f7f7f7;box-shadow:0 1px 0 #ccc;vertical-align:top}body.wp-admin .wpacu-wp-button{font-size:13px;line-height:26px;height:28px}.wpacu-wp-button.wpacu-wp-button-secondary{display:inline-block;text-decoration:none;height:auto;margin:0 5px 0 0;padding:0 10px 1px;cursor:pointer;border-width:1px;border-style:solid;-webkit-appearance:none;border-radius:3px;white-space:nowrap;box-sizing:border-box}.wpacu-assets-collapsible-wrap.wpacu-wrap-all,.wpacu-assets-collapsible-wrap.wpacu-wrap-styles{margin-top:20px}.wpacu-wp-button:hover{background:#fafafa;border-color:#999;color:#23282d}a.wpacu-assets-collapsible.wpacu-assets-collapsible-active,a.wpacu-assets-collapsible:hover{background-color:#f1f3f8}.wpacu-assets-collapsible-wrap{border:1px solid #e7e7e7}.wpacu-assets-collapsible-wrap.wpacu-wrap-all .distinguish-asset-list{list-style:none;margin-left:0;margin-bottom:5px;display:inline-block}.wpacu-assets-collapsible-wrap.wpacu-wrap-all .distinguish-asset-list li{float:left;margin-right:20px}.wpacu-assets-collapsible-wrap.wpacu-wrap-all .distinguish-asset-list li:last-child{margin-right:0}.wpacu-assets-collapsible-wrap.wpacu-wrap-scripts{margin-top:10px}a.wpacu-assets-collapsible{color:#000;font-size:20px;text-decoration:none;display:block;position:relative;background-color:#f1f3f8;cursor:pointer;padding:15px 15px 15px 36px;width:auto;border:none;text-align:left;outline:0}a.wpacu-assets-collapsible>span.dashicons{font-size:25px;line-height:normal;position:absolute;top:15px;top:calc(50% - 21px);left:22px;-webkit-transform:translate(-50%,50%);transform:translate(-50%,50%)}a.wpacu-assets-collapsible:after{position:absolute;font-size:22px;font-family:Dashicons;right:14px;top:16px}a.wpacu-assets-collapsible:not(.wpacu-assets-collapsible-active):after{content:"\f132"}a.wpacu-assets-collapsible.wpacu-assets-collapsible-active:after{content:"\f460"}.wpacu-assets-collapsible-content{padding:0 16px;max-height:0;overflow:hidden;background-color:#fbfbfb8c}.wpacu-assets-collapsible-content>div{padding:16px 0 0}.wpacu-assets-collapsible-content>div>p:first-child{margin-top:0}.wpacu-assets-collapsible-content.wpacu-open{max-height:100%}a.wpacu-assets-inline-code-collapsible{font-size:14px;text-decoration:none;display:inline-block;position:relative;cursor:pointer;width:auto;outline:0;background:#f1f3f8;border:1px solid #cdcdcd;padding:4px 21px 4px 8px;margin:0 0 0 6px;-webkit-border-radius:5px;-moz-border-radius:5px;border-radius:5px}a.wpacu-assets-inline-code-collapsible:hover{border:1px solid #5c5c5c}a.wpacu-assets-inline-code-collapsible:after{position:absolute;font-size:15px;font-family:Dashicons;right:-3px;top:-1px;height:14px;width:14px;-webkit-transform:translate(-50%,50%);transform:translate(-50%,50%)}a.wpacu-assets-inline-code-collapsible:not(.wpacu-assets-inline-code-collapsible-active):after{content:"\f132"}a.wpacu-assets-inline-code-collapsible.wpacu-assets-inline-code-collapsible-active:after{content:"\f460"}.wpacu-assets-inline-code-collapsible-content{padding:0 8px;max-height:0;overflow:hidden;background-color:#fbfbfb8c}.wpacu-assets-inline-code-collapsible-content>div{padding:7px 8px 0}.wpacu-assets-inline-code-collapsible-content>div>p:first-child{margin-top:0}.wpacu-assets-inline-code-collapsible-content.wpacu-open{max-height:100%}.wpacu_table_wrap{margin:20px 0 0}.wpacu_table_wrap>table{border:1px solid #cdcdcd}.wpacu_table_wrap .table{width:100%;max-width:100%;margin-bottom:1rem;-webkit-border-radius:10px;-moz-border-radius:10px;border-radius:10px}.wpacu_table_wrap .table td,.wpacu_table_wrap .table th{padding:.75rem;vertical-align:top;border-top:1px solid #eceeef}.wpacu_table_wrap .table thead th{vertical-align:bottom;border-bottom:2px solid #eceeef}.wpacu_table_wrap .table tbody+tbody{border-top:2px solid #eceeef}.wpacu_table_wrap .table .table{background-color:#fff}.wpacu_table_wrap .table-sm td,.wpacu_table_wrap .table-sm th{padding:.3rem}.wpacu_table_wrap .table-bordered,.wpacu_table_wrap .table-bordered td,.wpacu_table_wrap .table-bordered th{border:1px solid #eceeef}.wpacu_table_wrap .table-bordered thead td,.wpacu_table_wrap .table-bordered thead th{border-bottom-width:2px}.wpacu_table_wrap .table-striped tbody tr:nth-of-type(even){background-color:#f9f9f9}.wpacu_table_wrap .table-striped tbody tr:nth-of-type(odd){background-color:#fff}.wpacu_table_wrap .table-active,.wpacu_table_wrap .table-active>td,.wpacu_table_wrap .table-active>th,.wpacu_table_wrap .table-hover .table-active:hover,.wpacu_table_wrap .table-hover .table-active:hover>td,.wpacu_table_wrap .table-hover .table-active:hover>th,.wpacu_table_wrap .table-hover tbody tr:hover{background-color:rgba(0,0,0,.075)}.wpacu_table_wrap .table-success,.wpacu_table_wrap .table-success>td,.wpacu_table_wrap .table-success>th{background-color:#dff0d8}.wpacu_table_wrap .table-hover .table-success:hover,.wpacu_table_wrap .table-hover .table-success:hover>td,.wpacu_table_wrap .table-hover .table-success:hover>th{background-color:#d0e9c6}.wpacu_table_wrap .table-info,.wpacu_table_wrap .table-info>td,.wpacu_table_wrap .table-info>th{background-color:#d9edf7}.wpacu_table_wrap .table-hover .table-info:hover,.wpacu_table_wrap .table-hover .table-info:hover>td,.wpacu_table_wrap .table-hover .table-info:hover>th{background-color:#c4e3f3}.wpacu_table_wrap .table-warning,.wpacu_table_wrap .table-warning>td,.wpacu_table_wrap .table-warning>th{background-color:#fcf8e3}.wpacu_table_wrap .table-hover .table-warning:hover,.wpacu_table_wrap .table-hover .table-warning:hover>td,.wpacu_table_wrap .table-hover .table-warning:hover>th{background-color:#faf2cc}.wpacu_table_wrap .table-danger,.wpacu_table_wrap .table-danger>td,.wpacu_table_wrap .table-danger>th{background-color:#f2dede}.wpacu_table_wrap .table-hover .table-danger:hover,.wpacu_table_wrap .table-hover .table-danger:hover>td,.wpacu_table_wrap .table-hover .table-danger:hover>th{background-color:#ebcccc}.wpacu_table_wrap .thead-inverse th{color:#fff;background-color:#292b2c}.wpacu_table_wrap .thead-default th{color:#464a4c;background-color:#eceeef}.wpacu_table_wrap .table-inverse{color:#fff;background-color:#292b2c}.wpacu_table_wrap .table-inverse td,.wpacu_table_wrap .table-inverse th,.wpacu_table_wrap .table-inverse thead th{border-color:#fff}.wpacu_table_wrap .table-inverse.table-bordered,.wpacu_table_wrap .table-responsive.table-bordered{border:0}.wpacu_table_wrap .table-responsive{display:block;width:100%;overflow-x:auto;-ms-overflow-style:-ms-autohiding-scrollbar}#wpacu-warning-read{display:none;margin:10px 0}#wpacu-warning-read span.dashicons-warning{color:#c00}#wpacu-warning-read.wpacu-visible{display:block}#wpacu-reset-submit-area{margin:10px 0;clear:both}#wpacu-reset-submit-btn{margin:10px 0 0}.wpacu-tools-area{margin:10px 20px 0 2px}.wpacu-tools-area .wpacu-tools-container{background:#fff;padding:15px;border:1px solid #cdcdcd;width:96%}.wpacu-tools-area .wpacu-tools-container form>div:first-child{margin:0 0 10px}.wpacu-tools-area .wpacu-tools-container form .wpacu-warning{display:none;box-sizing:border-box;margin-top:10px}.wpacu-tools-area .wpacu-tools-container form .wpacu-warning p{margin:0 0 10px;line-height:22px}.wpacu-tools-area .wpacu-tools-container form .wpacu-warning p:only-child{margin:0}.wpacu-tools-area .wpacu-tools-container form .wpacu-warning ul{font-size:13px;list-style-type:disc;padding-left:30px}.wpacu-tools-area .wpacu-tools-container form .wpacu-warning.wpacu-visible{display:block}.wpacu-warning{font-size:15px;padding:10px;background:#fffcec;border:1px solid #fdd5c9;margin:0 0 5px;width:100%}
classes/AdminBar.php CHANGED
@@ -34,7 +34,7 @@ class AdminBar
34
  $wp_admin_bar->add_menu(array(
35
  'id' => 'wpacu-test-mode',
36
  'title' => WPACU_PLUGIN_TITLE.': <span class="dashicons dashicons-admin-tools"></span> <strong>TEST MODE</strong> is <strong>ENABLED SITE-WIDE</strong>',
37
- 'href' => admin_url('admin.php?page=' . WPACU_PLUGIN_NAME . '_settings')
38
  ));
39
 
40
  $wp_admin_bar->add_menu(array(
34
  $wp_admin_bar->add_menu(array(
35
  'id' => 'wpacu-test-mode',
36
  'title' => WPACU_PLUGIN_TITLE.': <span class="dashicons dashicons-admin-tools"></span> <strong>TEST MODE</strong> is <strong>ENABLED SITE-WIDE</strong>',
37
+ 'href' => admin_url( 'admin.php?page=' . WPACU_PLUGIN_ID . '_settings')
38
  ));
39
 
40
  $wp_admin_bar->add_menu(array(
classes/HomePage.php CHANGED
@@ -17,10 +17,10 @@ class HomePage
17
  */
18
  public function __construct()
19
  {
20
- $this->data['nonce_name'] = WPACU_PLUGIN_NAME.'_home_page_update';
21
  $this->data['show_on_front'] = Misc::getShowOnFront();
22
 
23
- $isHomePageEdit = (Misc::getVar('get', 'page') === WPACU_PLUGIN_NAME.'_home_page');
24
 
25
  // Only continue if we are on the plugin's homepage edit mode
26
  if (! $isHomePageEdit) {
@@ -57,7 +57,7 @@ class HomePage
57
  */
58
  public function page()
59
  {
60
- $wpacuNoLoadAssets = Misc::getVar('post', WPACU_PLUGIN_NAME, array());
61
  $wpacuHomePageUpdate = Misc::getVar('post', 'wpacu_manage_home_page_assets', false);
62
 
63
  // Could Be an Empty Array as Well so just is_array() is enough to use
17
  */
18
  public function __construct()
19
  {
20
+ $this->data['nonce_name'] = WPACU_PLUGIN_ID . '_home_page_update';
21
  $this->data['show_on_front'] = Misc::getShowOnFront();
22
 
23
+ $isHomePageEdit = ( Misc::getVar('get', 'page') === WPACU_PLUGIN_ID . '_home_page');
24
 
25
  // Only continue if we are on the plugin's homepage edit mode
26
  if (! $isHomePageEdit) {
57
  */
58
  public function page()
59
  {
60
+ $wpacuNoLoadAssets = Misc::getVar('post', WPACU_PLUGIN_ID, array());
61
  $wpacuHomePageUpdate = Misc::getVar('post', 'wpacu_manage_home_page_assets', false);
62
 
63
  // Could Be an Empty Array as Well so just is_array() is enough to use
classes/Main.php CHANGED
@@ -205,7 +205,7 @@ class Main
205
  if ( $this->settings['dashboard_show'] == 1 ) {
206
  // Send an AJAX request to get the list of loaded scripts and styles and print it nicely
207
  add_action(
208
- 'wp_ajax_' . WPACU_PLUGIN_NAME . '_get_loaded_assets',
209
  array( $this, 'ajaxGetJsonListCallback' )
210
  );
211
 
@@ -276,8 +276,8 @@ class Main
276
 
277
  if (isset($obj->public) && $obj->public > 0) {
278
  add_meta_box(
279
- WPACU_PLUGIN_NAME.'_asset_list',
280
- __('Asset CleanUp', WPACU_PLUGIN_NAME),
281
  array($this, 'renderMetaBoxContent'),
282
  $postType,
283
  'advanced',
@@ -307,7 +307,7 @@ class Main
307
 
308
  if ($getAssets) {
309
  // Add an nonce field so we can check for it later.
310
- wp_nonce_field(WPACU_PLUGIN_NAME . '_meta_box', WPACU_PLUGIN_NAME . '_nonce');
311
  }
312
 
313
  $data = array();
@@ -585,13 +585,13 @@ class Main
585
  || ($homepageClass->data['show_on_front'] === 'page' && $postId)
586
  ) {
587
  $exceptionsListJson = get_post_meta(
588
- $postId, '_' . WPACU_PLUGIN_NAME . '_load_exceptions',
589
  true
590
  );
591
  } elseif ($type === 'front_page') {
592
  // The home page could also be the list of the latest blog posts
593
  $exceptionsListJson = get_option(
594
- WPACU_PLUGIN_NAME . '_front_page_load_exceptions'
595
  );
596
  } elseif ($type === 'for_pro' && Main::wpacuProEnabled()) {
597
  // [wpacu_pro]
@@ -627,7 +627,7 @@ class Main
627
  public function getGlobalUnload()
628
  {
629
  $existingListEmpty = array('styles' => array(), 'scripts' => array());
630
- $existingListJson = get_option(WPACU_PLUGIN_NAME.'_global_unload');
631
 
632
  $existingListData = $this->existingList($existingListJson, $existingListEmpty);
633
 
@@ -644,7 +644,7 @@ class Main
644
  {
645
  $existingListEmpty = array('styles' => array(), 'scripts' => array());
646
 
647
- $existingListAllJson = get_option(WPACU_PLUGIN_NAME.'_bulk_unload');
648
 
649
  if (! $existingListAllJson) {
650
  return $existingListEmpty;
@@ -816,7 +816,7 @@ class Main
816
  as they are loaded only when you (or other admin) manage the assets, never for your website visitors */
817
  $skipStyles = array(
818
  'admin-bar',
819
- WPACU_PLUGIN_NAME . '-style'
820
  );
821
 
822
  if (is_admin_bar_showing()) {
@@ -874,7 +874,7 @@ class Main
874
  as they are loaded only when you (or other admin) manage the assets, never for your website visitors */
875
  $skipScripts = array(
876
  'admin-bar',
877
- WPACU_PLUGIN_NAME.'-script'
878
  );
879
 
880
  foreach ($manageScripts as $handle) {
@@ -1311,9 +1311,9 @@ class Main
1311
  if (empty($this->assetsRemoved)) {
1312
  // For Home Page (latest blog posts)
1313
  if ($postId < 1 && ($isInAdminPageViaAjax || Misc::isHomePage())) {
1314
- $this->assetsRemoved = get_option(WPACU_PLUGIN_NAME . '_front_page_no_load');
1315
  } elseif ($postId > 0) {
1316
- $this->assetsRemoved = get_post_meta($postId, '_' . WPACU_PLUGIN_NAME . '_no_load', true);
1317
  }
1318
 
1319
  // [wpacu_pro]
@@ -1479,7 +1479,7 @@ class Main
1479
  */
1480
  public static function isSettingsPage()
1481
  {
1482
- return (array_key_exists('page', $_GET) && $_GET['page'] === WPACU_PLUGIN_NAME.'_settings');
1483
  }
1484
 
1485
  /**
205
  if ( $this->settings['dashboard_show'] == 1 ) {
206
  // Send an AJAX request to get the list of loaded scripts and styles and print it nicely
207
  add_action(
208
+ 'wp_ajax_' . WPACU_PLUGIN_ID . '_get_loaded_assets',
209
  array( $this, 'ajaxGetJsonListCallback' )
210
  );
211
 
276
 
277
  if (isset($obj->public) && $obj->public > 0) {
278
  add_meta_box(
279
+ WPACU_PLUGIN_ID . '_asset_list',
280
+ __('Asset CleanUp', WPACU_PLUGIN_TEXT_DOMAIN),
281
  array($this, 'renderMetaBoxContent'),
282
  $postType,
283
  'advanced',
307
 
308
  if ($getAssets) {
309
  // Add an nonce field so we can check for it later.
310
+ wp_nonce_field( WPACU_PLUGIN_ID . '_meta_box', WPACU_PLUGIN_ID . '_nonce');
311
  }
312
 
313
  $data = array();
585
  || ($homepageClass->data['show_on_front'] === 'page' && $postId)
586
  ) {
587
  $exceptionsListJson = get_post_meta(
588
+ $postId, '_' . WPACU_PLUGIN_ID . '_load_exceptions',
589
  true
590
  );
591
  } elseif ($type === 'front_page') {
592
  // The home page could also be the list of the latest blog posts
593
  $exceptionsListJson = get_option(
594
+ WPACU_PLUGIN_ID . '_front_page_load_exceptions'
595
  );
596
  } elseif ($type === 'for_pro' && Main::wpacuProEnabled()) {
597
  // [wpacu_pro]
627
  public function getGlobalUnload()
628
  {
629
  $existingListEmpty = array('styles' => array(), 'scripts' => array());
630
+ $existingListJson = get_option( WPACU_PLUGIN_ID . '_global_unload');
631
 
632
  $existingListData = $this->existingList($existingListJson, $existingListEmpty);
633
 
644
  {
645
  $existingListEmpty = array('styles' => array(), 'scripts' => array());
646
 
647
+ $existingListAllJson = get_option( WPACU_PLUGIN_ID . '_bulk_unload');
648
 
649
  if (! $existingListAllJson) {
650
  return $existingListEmpty;
816
  as they are loaded only when you (or other admin) manage the assets, never for your website visitors */
817
  $skipStyles = array(
818
  'admin-bar',
819
+ WPACU_PLUGIN_ID . '-style'
820
  );
821
 
822
  if (is_admin_bar_showing()) {
874
  as they are loaded only when you (or other admin) manage the assets, never for your website visitors */
875
  $skipScripts = array(
876
  'admin-bar',
877
+ WPACU_PLUGIN_ID . '-script'
878
  );
879
 
880
  foreach ($manageScripts as $handle) {
1311
  if (empty($this->assetsRemoved)) {
1312
  // For Home Page (latest blog posts)
1313
  if ($postId < 1 && ($isInAdminPageViaAjax || Misc::isHomePage())) {
1314
+ $this->assetsRemoved = get_option( WPACU_PLUGIN_ID . '_front_page_no_load');
1315
  } elseif ($postId > 0) {
1316
+ $this->assetsRemoved = get_post_meta($postId, '_' . WPACU_PLUGIN_ID . '_no_load', true);
1317
  }
1318
 
1319
  // [wpacu_pro]
1479
  */
1480
  public static function isSettingsPage()
1481
  {
1482
+ return (array_key_exists('page', $_GET) && $_GET['page'] === WPACU_PLUGIN_ID . '_settings');
1483
  }
1484
 
1485
  /**
classes/Menu.php CHANGED
@@ -22,16 +22,11 @@ class Menu
22
  */
23
  public function __construct()
24
  {
25
- self::$slug = WPACU_PLUGIN_NAME . '_settings';
26
 
27
  add_action('admin_menu', array($this, 'activeMenu'));
28
 
29
- if (isset($_GET['page']) && $_GET['page'] === WPACU_PLUGIN_NAME.'_feature_request') {
30
- header('Location: '.WPACU_PLUGIN_FEATURE_REQUEST_URL.'?utm_source=plugin_feature_request_from_lite');
31
- exit();
32
- }
33
-
34
- if (isset($_GET['page']) && $_GET['page'] === WPACU_PLUGIN_NAME.'_go_pro') {
35
  header('Location: '.WPACU_PLUGIN_GO_PRO_URL.'?utm_source=plugin_go_pro');
36
  exit();
37
  }
@@ -48,8 +43,8 @@ class Menu
48
  }
49
 
50
  add_menu_page(
51
- __('Asset CleanUp', WPACU_PLUGIN_NAME),
52
- __('Asset CleanUp', WPACU_PLUGIN_NAME),
53
  self::$capability,
54
  self::$slug,
55
  array(new Settings, 'settingsPage'),
@@ -58,82 +53,81 @@ class Menu
58
 
59
  add_submenu_page(
60
  self::$slug,
61
- __('Home Page', WPACU_PLUGIN_NAME),
62
- __('Home Page', WPACU_PLUGIN_NAME),
63
  self::$capability,
64
- WPACU_PLUGIN_NAME.'_home_page',
65
  array(new HomePage, 'page')
66
  );
67
 
68
  add_submenu_page(
69
  self::$slug,
70
- __('Pages Info', WPACU_PLUGIN_NAME),
71
- __('Pages Info', WPACU_PLUGIN_NAME),
72
  self::$capability,
73
- WPACU_PLUGIN_NAME.'_pages_info',
74
  array(new Info, 'pagesInfo')
75
  );
76
 
77
  add_submenu_page(
78
  self::$slug,
79
- __('Bulk Unloaded', WPACU_PLUGIN_NAME),
80
- __('Bulk Unloaded', WPACU_PLUGIN_NAME),
81
  self::$capability,
82
- WPACU_PLUGIN_NAME.'_bulk_unloads',
83
  array(new BulkUnloads, 'pageBulkUnloads')
84
  );
85
 
 
 
 
 
 
 
 
 
 
86
  // Get Help | Support Page
87
  add_submenu_page(
88
  self::$slug,
89
- __('License', WPACU_PLUGIN_NAME),
90
- __('License', WPACU_PLUGIN_NAME),
91
  self::$capability,
92
- WPACU_PLUGIN_NAME.'_license',
93
  array(new Info, 'license')
94
  );
95
 
96
  // Get Help | Support Page
97
  add_submenu_page(
98
  self::$slug,
99
- __('Help', WPACU_PLUGIN_NAME),
100
- __('Help', WPACU_PLUGIN_NAME),
101
  self::$capability,
102
- WPACU_PLUGIN_NAME.'_get_help',
103
  array(new Info, 'help')
104
  );
105
 
106
- // Feature Request | Redirects to feature request form
107
- add_submenu_page(
108
- self::$slug,
109
- __('Feature Request', WPACU_PLUGIN_NAME),
110
- __('Feature Request', WPACU_PLUGIN_NAME).' <span style="font-size: 16px; line-height: 22px; margin-left: -3px;" class="dashicons dashicons-plus"></span>',
111
- self::$capability,
112
- WPACU_PLUGIN_NAME.'_feature_request',
113
- function() {}
114
- );
115
-
116
  // Upgrade to "Go Pro" | Redirects to sale page
117
  add_submenu_page(
118
  self::$slug,
119
- __('Go Pro', WPACU_PLUGIN_NAME),
120
- __('Go Pro', WPACU_PLUGIN_NAME).' <span style="font-size: 16px;" class="dashicons dashicons-star-filled"></span>',
121
  self::$capability,
122
- WPACU_PLUGIN_NAME.'_go_pro',
123
  function() {}
124
  );
125
 
126
  // Add "Asset CleanUp Pro" Settings Link to the main "Settings" menu within the Dashboard
127
  // For easier navigation
128
  $GLOBALS['submenu']['options-general.php'][] = array(
129
- __('Asset CleanUp', WPACU_PLUGIN_NAME),
130
  self::$capability,
131
- admin_url('admin.php?page='.WPACU_PLUGIN_NAME.'_settings'),
132
- __('Asset CleanUp', WPACU_PLUGIN_NAME),
133
  );
134
 
135
  // Rename first item from the menu which has the same title as the menu page
136
- $GLOBALS['submenu'][self::$slug][0][0] = esc_attr__('Settings', WPACU_PLUGIN_NAME);
137
  }
138
 
139
  /**
22
  */
23
  public function __construct()
24
  {
25
+ self::$slug = WPACU_PLUGIN_ID . '_settings';
26
 
27
  add_action('admin_menu', array($this, 'activeMenu'));
28
 
29
+ if (isset($_GET['page']) && $_GET['page'] === WPACU_PLUGIN_ID . '_go_pro') {
 
 
 
 
 
30
  header('Location: '.WPACU_PLUGIN_GO_PRO_URL.'?utm_source=plugin_go_pro');
31
  exit();
32
  }
43
  }
44
 
45
  add_menu_page(
46
+ __('Asset CleanUp', WPACU_PLUGIN_TEXT_DOMAIN),
47
+ __('Asset CleanUp', WPACU_PLUGIN_TEXT_DOMAIN),
48
  self::$capability,
49
  self::$slug,
50
  array(new Settings, 'settingsPage'),
53
 
54
  add_submenu_page(
55
  self::$slug,
56
+ __('Home Page', WPACU_PLUGIN_TEXT_DOMAIN),
57
+ __('Home Page', WPACU_PLUGIN_TEXT_DOMAIN),
58
  self::$capability,
59
+ WPACU_PLUGIN_ID . '_home_page',
60
  array(new HomePage, 'page')
61
  );
62
 
63
  add_submenu_page(
64
  self::$slug,
65
+ __('Pages Info', WPACU_PLUGIN_TEXT_DOMAIN),
66
+ __('Pages Info', WPACU_PLUGIN_TEXT_DOMAIN),
67
  self::$capability,
68
+ WPACU_PLUGIN_ID . '_pages_info',
69
  array(new Info, 'pagesInfo')
70
  );
71
 
72
  add_submenu_page(
73
  self::$slug,
74
+ __('Bulk Unloaded', WPACU_PLUGIN_TEXT_DOMAIN),
75
+ __('Bulk Unloaded', WPACU_PLUGIN_TEXT_DOMAIN),
76
  self::$capability,
77
+ WPACU_PLUGIN_ID . '_bulk_unloads',
78
  array(new BulkUnloads, 'pageBulkUnloads')
79
  );
80
 
81
+ add_submenu_page(
82
+ self::$slug,
83
+ __('Tools', WPACU_PLUGIN_TEXT_DOMAIN),
84
+ __('Tools', WPACU_PLUGIN_TEXT_DOMAIN),
85
+ self::$capability,
86
+ WPACU_PLUGIN_ID . '_tools',
87
+ array(new Tools, 'toolsPage')
88
+ );
89
+
90
  // Get Help | Support Page
91
  add_submenu_page(
92
  self::$slug,
93
+ __('License', WPACU_PLUGIN_TEXT_DOMAIN),
94
+ __('License', WPACU_PLUGIN_TEXT_DOMAIN),
95
  self::$capability,
96
+ WPACU_PLUGIN_ID . '_license',
97
  array(new Info, 'license')
98
  );
99
 
100
  // Get Help | Support Page
101
  add_submenu_page(
102
  self::$slug,
103
+ __('Help', WPACU_PLUGIN_TEXT_DOMAIN),
104
+ __('Help', WPACU_PLUGIN_TEXT_DOMAIN),
105
  self::$capability,
106
+ WPACU_PLUGIN_ID . '_get_help',
107
  array(new Info, 'help')
108
  );
109
 
 
 
 
 
 
 
 
 
 
 
110
  // Upgrade to "Go Pro" | Redirects to sale page
111
  add_submenu_page(
112
  self::$slug,
113
+ __('Go Pro', WPACU_PLUGIN_TEXT_DOMAIN),
114
+ __('Go Pro', WPACU_PLUGIN_TEXT_DOMAIN) . ' <span style="font-size: 16px;" class="dashicons dashicons-star-filled"></span>',
115
  self::$capability,
116
+ WPACU_PLUGIN_ID . '_go_pro',
117
  function() {}
118
  );
119
 
120
  // Add "Asset CleanUp Pro" Settings Link to the main "Settings" menu within the Dashboard
121
  // For easier navigation
122
  $GLOBALS['submenu']['options-general.php'][] = array(
123
+ __('Asset CleanUp', WPACU_PLUGIN_TEXT_DOMAIN),
124
  self::$capability,
125
+ admin_url( 'admin.php?page=' . WPACU_PLUGIN_ID . '_settings'),
126
+ __('Asset CleanUp', WPACU_PLUGIN_TEXT_DOMAIN),
127
  );
128
 
129
  // Rename first item from the menu which has the same title as the menu page
130
+ $GLOBALS['submenu'][self::$slug][0][0] = esc_attr__('Settings', WPACU_PLUGIN_TEXT_DOMAIN);
131
  }
132
 
133
  /**
classes/OwnAssets.php CHANGED
@@ -78,7 +78,7 @@ class OwnAssets
78
  $this->loadPluginAssets = true;
79
  }
80
 
81
- if (strpos($page, WPACU_PLUGIN_NAME) === 0) {
82
  $this->loadPluginAssets = true;
83
  }
84
 
@@ -125,7 +125,7 @@ class OwnAssets
125
  private function enqueueAdminStyles()
126
  {
127
  $styleRelPath = '/assets/style.min.css';
128
- wp_enqueue_style(WPACU_PLUGIN_NAME . '-style', plugins_url($styleRelPath, WPACU_PLUGIN_FILE), array(), $this->_assetVer($styleRelPath));
129
  }
130
 
131
  /**
@@ -145,7 +145,7 @@ class OwnAssets
145
  $postId = $getPostId;
146
  }
147
 
148
- if ($page === WPACU_PLUGIN_NAME.'_home_page' || $postId < 1) {
149
  $postId = 0; // for home page
150
  }
151
 
@@ -167,7 +167,7 @@ class OwnAssets
167
  $scriptRelPath = '/assets/script.min.js';
168
 
169
  wp_register_script(
170
- WPACU_PLUGIN_NAME . '-script',
171
  plugins_url($scriptRelPath, WPACU_PLUGIN_FILE),
172
  array('jquery'),
173
  $this->_assetVer($scriptRelPath)
@@ -177,7 +177,7 @@ class OwnAssets
177
  $pageUrl = Misc::getPageUrl($postId);
178
 
179
  $wpacuObjectData = array(
180
- 'plugin_name' => WPACU_PLUGIN_NAME,
181
  'dom_get_type' => Main::$domGetType,
182
  'start_del' => Main::START_DEL,
183
  'end_del' => Main::END_DEL,
@@ -216,20 +216,32 @@ HTML;
216
  'Make sure to properly test your website if you unload the jQuery migration library.'."\n\n".
217
  'In some cases, due to old jQuery code triggered from plugins or the theme, unloading this migration library could cause those scripts not to function anymore and break some of the front-end functionality.'."\n\n".
218
  'If you are not sure about whether activating this option is right or not, it is better to leave it as it is (to be loaded by default) and consult with a developer.'."\n\n".
219
- 'Confirm this action to enable the unloading or cancel to leave it loaded by default.', WPACU_PLUGIN_NAME);
220
 
221
  $wpacuObjectData['comment_reply_disable_confirm_msg'] = __(
222
  'This is worth disabling if you are NOT using the default WordPress comment system (e.g. you are using the website for business purposes, to showcase your products and you are not using it as a blog where people leave comments to your posts).'."\n\n".
223
  'If you are not sure about whether activating this option is right or not, it is better to leave it as it is (to be loaded by default).'."\n\n".
224
- 'Confirm this action to enable the unloading or cancel to leave it loaded by default.', WPACU_PLUGIN_NAME);
 
 
 
 
 
 
 
 
 
 
 
 
225
 
226
  wp_localize_script(
227
- WPACU_PLUGIN_NAME . '-script',
228
  'wpacu_object',
229
  apply_filters('wpacu_object_data', $wpacuObjectData)
230
  );
231
 
232
- wp_enqueue_script(WPACU_PLUGIN_NAME . '-script');
233
  }
234
 
235
  /**
@@ -238,7 +250,7 @@ HTML;
238
  private function enqueuePublicStyles()
239
  {
240
  $styleRelPath = '/assets/style.min.css';
241
- wp_enqueue_style(WPACU_PLUGIN_NAME . '-style', plugins_url($styleRelPath, WPACU_PLUGIN_FILE), array(), $this->_assetVer($styleRelPath));
242
  }
243
 
244
  /**
@@ -247,7 +259,7 @@ HTML;
247
  public function enqueuePublicScripts()
248
  {
249
  $scriptRelPath = '/assets/script.min.js';
250
- wp_enqueue_script(WPACU_PLUGIN_NAME . '-script', plugins_url($scriptRelPath, WPACU_PLUGIN_FILE), array('jquery'), $this->_assetVer($scriptRelPath));
251
  }
252
 
253
  /**
78
  $this->loadPluginAssets = true;
79
  }
80
 
81
+ if ( strpos($page, WPACU_PLUGIN_ID) === 0) {
82
  $this->loadPluginAssets = true;
83
  }
84
 
125
  private function enqueueAdminStyles()
126
  {
127
  $styleRelPath = '/assets/style.min.css';
128
+ wp_enqueue_style( WPACU_PLUGIN_ID . '-style', plugins_url($styleRelPath, WPACU_PLUGIN_FILE), array(), $this->_assetVer($styleRelPath));
129
  }
130
 
131
  /**
145
  $postId = $getPostId;
146
  }
147
 
148
+ if ( $page === WPACU_PLUGIN_ID . '_home_page' || $postId < 1) {
149
  $postId = 0; // for home page
150
  }
151
 
167
  $scriptRelPath = '/assets/script.min.js';
168
 
169
  wp_register_script(
170
+ WPACU_PLUGIN_ID . '-script',
171
  plugins_url($scriptRelPath, WPACU_PLUGIN_FILE),
172
  array('jquery'),
173
  $this->_assetVer($scriptRelPath)
177
  $pageUrl = Misc::getPageUrl($postId);
178
 
179
  $wpacuObjectData = array(
180
+ 'plugin_name' => WPACU_PLUGIN_ID,
181
  'dom_get_type' => Main::$domGetType,
182
  'start_del' => Main::START_DEL,
183
  'end_del' => Main::END_DEL,
216
  'Make sure to properly test your website if you unload the jQuery migration library.'."\n\n".
217
  'In some cases, due to old jQuery code triggered from plugins or the theme, unloading this migration library could cause those scripts not to function anymore and break some of the front-end functionality.'."\n\n".
218
  'If you are not sure about whether activating this option is right or not, it is better to leave it as it is (to be loaded by default) and consult with a developer.'."\n\n".
219
+ 'Confirm this action to enable the unloading or cancel to leave it loaded by default.', WPACU_PLUGIN_TEXT_DOMAIN);
220
 
221
  $wpacuObjectData['comment_reply_disable_confirm_msg'] = __(
222
  'This is worth disabling if you are NOT using the default WordPress comment system (e.g. you are using the website for business purposes, to showcase your products and you are not using it as a blog where people leave comments to your posts).'."\n\n".
223
  'If you are not sure about whether activating this option is right or not, it is better to leave it as it is (to be loaded by default).'."\n\n".
224
+ 'Confirm this action to enable the unloading or cancel to leave it loaded by default.', WPACU_PLUGIN_TEXT_DOMAIN);
225
+
226
+ $wpacuObjectData['reset_settings_confirm_msg'] = __(
227
+ 'Are you sure you want to reset the settings to their default values?'."\n\n".'This is an irreversible action.'."\n\n".'Please confirm to continue or "Cancel" to abort it',
228
+ WPACU_PLUGIN_TEXT_DOMAIN
229
+ );
230
+
231
+ $wpacuObjectData['reset_everything_confirm_msg'] = __(
232
+ 'Are you sure you want to reset everything (settings, unloads, load exceptions etc.) to the same point it was when you first activated the plugin?'."\n\n".
233
+ 'This is an irreversible action.'."\n\n".
234
+ 'Please confirm to continue or "Cancel" to abort it.',
235
+ WPACU_PLUGIN_TEXT_DOMAIN
236
+ );
237
 
238
  wp_localize_script(
239
+ WPACU_PLUGIN_ID . '-script',
240
  'wpacu_object',
241
  apply_filters('wpacu_object_data', $wpacuObjectData)
242
  );
243
 
244
+ wp_enqueue_script( WPACU_PLUGIN_ID . '-script');
245
  }
246
 
247
  /**
250
  private function enqueuePublicStyles()
251
  {
252
  $styleRelPath = '/assets/style.min.css';
253
+ wp_enqueue_style( WPACU_PLUGIN_ID . '-style', plugins_url($styleRelPath, WPACU_PLUGIN_FILE), array(), $this->_assetVer($styleRelPath));
254
  }
255
 
256
  /**
259
  public function enqueuePublicScripts()
260
  {
261
  $scriptRelPath = '/assets/script.min.js';
262
+ wp_enqueue_script( WPACU_PLUGIN_ID . '-script', plugins_url($scriptRelPath, WPACU_PLUGIN_FILE), array('jquery'), $this->_assetVer($scriptRelPath));
263
  }
264
 
265
  /**
classes/Plugin.php CHANGED
@@ -11,8 +11,6 @@ class Plugin
11
  */
12
  public function __construct()
13
  {
14
- register_activation_hook(WPACU_PLUGIN_FILE, array($this, 'whenActivated'));
15
-
16
  // [wpacu_lite]
17
  // Admin footer text: Ask the user to review the plugin
18
  add_filter('admin_footer_text', array($this, 'adminFooter'), 1, 1);
@@ -22,22 +20,6 @@ class Plugin
22
  add_filter('plugin_action_links_'.WPACU_PLUGIN_BASE, array($this, 'actionLinks'));
23
  }
24
 
25
- /**
26
- *
27
- */
28
- public function whenActivated()
29
- {
30
- if (! get_option(WPACU_PLUGIN_NAME.'_settings')) {
31
- $defaultSettings = array(
32
- 'dashboard_show' => 1,
33
- 'dom_get_type' => 'direct'
34
- );
35
-
36
- $settings = new Settings();
37
- $settings->update($defaultSettings);
38
- }
39
- }
40
-
41
  /**
42
  * @param $links
43
  *
@@ -45,7 +27,7 @@ class Plugin
45
  */
46
  public function actionLinks($links)
47
  {
48
- $links['settings'] = '<a href="admin.php?page='.WPACU_PLUGIN_NAME.'_settings">Settings</a>';
49
 
50
  // [wpacu_lite]
51
  $allPlugins = get_plugins();
@@ -67,7 +49,7 @@ class Plugin
67
  */
68
  public function adminFooter($text)
69
  {
70
- if (isset($_GET['page']) && strpos($_GET['page'], WPACU_PLUGIN_NAME) !== false) {
71
  $reviewUrl = 'https://wordpress.org/support/plugin/wp-asset-clean-up/reviews/?filter=5#new-post';
72
  $text = 'Thank you for using '.WPACU_PLUGIN_TITLE.' v'.WPACU_PLUGIN_VERSION.') <span class="dashicons dashicons-smiley"></span> &nbsp;&nbsp; If you like it, please <a target="_blank" href="'.$reviewUrl.'"><strong>rate</strong> '.WPACU_PLUGIN_TITLE.'</a> <a target="_blank" href="'.$reviewUrl.'"><span class="dashicons dashicons-wpacu dashicons-star-filled"></span><span class="dashicons dashicons-wpacu dashicons-star-filled"></span><span class="dashicons dashicons-wpacu dashicons-star-filled"></span><span class="dashicons dashicons-wpacu dashicons-star-filled"></span><span class="dashicons dashicons-wpacu dashicons-star-filled"></span></a> on WordPress.org to help me spread the word to the community.';
73
  }
11
  */
12
  public function __construct()
13
  {
 
 
14
  // [wpacu_lite]
15
  // Admin footer text: Ask the user to review the plugin
16
  add_filter('admin_footer_text', array($this, 'adminFooter'), 1, 1);
20
  add_filter('plugin_action_links_'.WPACU_PLUGIN_BASE, array($this, 'actionLinks'));
21
  }
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  /**
24
  * @param $links
25
  *
27
  */
28
  public function actionLinks($links)
29
  {
30
+ $links['settings'] = '<a href="admin.php?page=' . WPACU_PLUGIN_ID . '_settings">' . __('Settings', WPACU_PLUGIN_TEXT_DOMAIN) . '</a>';
31
 
32
  // [wpacu_lite]
33
  $allPlugins = get_plugins();
49
  */
50
  public function adminFooter($text)
51
  {
52
+ if (isset($_GET['page']) && strpos($_GET['page'], WPACU_PLUGIN_ID) !== false) {
53
  $reviewUrl = 'https://wordpress.org/support/plugin/wp-asset-clean-up/reviews/?filter=5#new-post';
54
  $text = 'Thank you for using '.WPACU_PLUGIN_TITLE.' v'.WPACU_PLUGIN_VERSION.') <span class="dashicons dashicons-smiley"></span> &nbsp;&nbsp; If you like it, please <a target="_blank" href="'.$reviewUrl.'"><strong>rate</strong> '.WPACU_PLUGIN_TITLE.'</a> <a target="_blank" href="'.$reviewUrl.'"><span class="dashicons dashicons-wpacu dashicons-star-filled"></span><span class="dashicons dashicons-wpacu dashicons-star-filled"></span><span class="dashicons dashicons-wpacu dashicons-star-filled"></span><span class="dashicons dashicons-wpacu dashicons-star-filled"></span><span class="dashicons dashicons-wpacu dashicons-star-filled"></span></a> on WordPress.org to help me spread the word to the community.';
55
  }
classes/Settings.php CHANGED
@@ -16,15 +16,12 @@ class Settings
16
  'dom_get_type',
17
  'frontend_show',
18
 
19
- 'input_style',
20
-
21
- // [wpacu_pro]
22
  'assets_list_layout',
23
- // [wpacu_pro]
24
-
25
  'assets_list_layout_areas_status',
26
  'assets_list_inline_code_status',
27
 
 
 
28
  'hide_core_files',
29
  'test_mode',
30
 
@@ -40,6 +37,26 @@ class Settings
40
  */
41
  public $currentSettings = array();
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  /**
44
  * @var array
45
  */
@@ -55,8 +72,8 @@ class Settings
55
  // This is triggered BEFORE "initAfterPluginsLoaded" from 'Main' class
56
  add_action('plugins_loaded', array($this, 'saveSettings'), 9);
57
 
58
- if (array_key_exists('page', $_GET) && $_GET['page'] === 'wpassetcleanup_settings') {
59
- add_action('admin_notices', array($this, 'notices'));
60
  }
61
  }
62
 
@@ -71,7 +88,7 @@ class Settings
71
  if ($settings['dashboard_show'] != 1 && $settings['frontend_show'] != 1) {
72
  ?>
73
  <div class="notice notice-warning">
74
- <p><span style="color: #ffb900;" class="dashicons dashicons-info"></span>&nbsp;<?php _e('It looks like you have both "Manage in the Dashboard?" and "Manage in the Front-end?" inactive. The plugin still works fine and any assets you have selected for unload are not loaded. However, if you want to manage the assets in any page, you need to have at least one of the view options enabled.', WPACU_PLUGIN_NAME); ?></p>
75
  </div>
76
  <?php
77
  }
@@ -79,8 +96,8 @@ class Settings
79
  // After "Save changes" is clicked
80
  if ($this->status['updated']) {
81
  ?>
82
- <div class="notice notice-success">
83
- <?php _e('The settings were successfully updated.', WPACU_PLUGIN_NAME); ?>
84
  </div>
85
  <?php
86
  }
@@ -94,7 +111,7 @@ class Settings
94
  if (! empty($_POST) && array_key_exists('wpacu_settings_page', $_POST)) {
95
  check_admin_referer('wpacu_settings_update');
96
 
97
- $data = Misc::getVar('post', WPACU_PLUGIN_NAME . '_settings', array());
98
  $this->update($data);
99
  }
100
  }
@@ -137,16 +154,18 @@ class Settings
137
  return true;
138
  }
139
 
 
140
  // Prior to 1.2.4.4
141
- if (get_option(WPACU_PLUGIN_NAME.'_frontend_show') == 1) {
142
  // Put it in the main settings option
143
  $settings = $this->getAll();
144
  $settings['frontend_show'] = 1;
145
  $this->update($settings);
146
 
147
- delete_option(WPACU_PLUGIN_NAME.'_frontend_show');
148
  return true;
149
  }
 
150
 
151
  return false;
152
  }
@@ -160,8 +179,9 @@ class Settings
160
  return $this->currentSettings;
161
  }
162
 
163
- $settingsOption = get_option(WPACU_PLUGIN_NAME.'_settings');
164
 
 
165
  if ($settingsOption !== '' && is_string($settingsOption)) {
166
  $settings = (array)json_decode($settingsOption);
167
 
@@ -180,14 +200,20 @@ class Settings
180
  }
181
  }
182
 
183
- // Keep the keys with empty values
184
- $list = array();
 
 
 
185
 
186
  foreach ($this->settingsKeys as $settingsKey) {
187
- $list[$settingsKey] = '';
 
 
 
188
  }
189
 
190
- return $list;
191
  }
192
 
193
  /**
@@ -197,8 +223,24 @@ class Settings
197
  {
198
  $wpacuUpdate = new Update;
199
 
200
- $disableJQueryMigrate = isset($_POST[WPACU_PLUGIN_NAME.'_global_unloads']['disable_jquery_migrate']);
201
- $disableCommentReply = isset($_POST[WPACU_PLUGIN_NAME.'_global_unloads']['disable_comment_reply']);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
 
203
  /*
204
  * Add element(s) to the global unload rules
@@ -238,7 +280,7 @@ class Settings
238
  $wpacuUpdate->removeEverywhereUnloads(array(), $removeFromUnloadList);
239
  }
240
 
241
- update_option(WPACU_PLUGIN_NAME . '_settings', json_encode($settings), 'no');
242
  $this->status['updated'] = true;
243
  }
244
  }
16
  'dom_get_type',
17
  'frontend_show',
18
 
 
 
 
19
  'assets_list_layout',
 
 
20
  'assets_list_layout_areas_status',
21
  'assets_list_inline_code_status',
22
 
23
+ 'input_style',
24
+
25
  'hide_core_files',
26
  'test_mode',
27
 
37
  */
38
  public $currentSettings = array();
39
 
40
+ /**
41
+ * @var array
42
+ */
43
+ public static $defaultSettings = array(
44
+ // Show the assets list within the Dashboard, while they are hidden in the front-end view
45
+ 'dashboard_show' => '1',
46
+
47
+ // Direct AJAX call by default (not via WP Remote Post)
48
+ 'dom_get_type' => 'direct',
49
+
50
+ 'assets_list_layout' => 'default',
51
+ 'assets_list_layout_areas_status' => 'expanded',
52
+ 'assets_list_inline_code_status' => 'expanded',
53
+
54
+ 'input_style' => 'enhanced',
55
+
56
+ // Starting from v1.2.8.6 (lite), WordPress core files are hidden in the assets list as a default setting
57
+ 'hide_core_files' => '1'
58
+ );
59
+
60
  /**
61
  * @var array
62
  */
72
  // This is triggered BEFORE "initAfterPluginsLoaded" from 'Main' class
73
  add_action('plugins_loaded', array($this, 'saveSettings'), 9);
74
 
75
+ if (Misc::getVar('get', 'page') === WPACU_PLUGIN_ID . '_settings') {
76
+ add_action('wpacu_admin_notices', array($this, 'notices'));
77
  }
78
  }
79
 
88
  if ($settings['dashboard_show'] != 1 && $settings['frontend_show'] != 1) {
89
  ?>
90
  <div class="notice notice-warning">
91
+ <p><span style="color: #ffb900;" class="dashicons dashicons-info"></span>&nbsp;<?php _e('It looks like you have both "Manage in the Dashboard?" and "Manage in the Front-end?" inactive. The plugin still works fine and any assets you have selected for unload are not loaded. However, if you want to manage the assets in any page, you need to have at least one of the view options enabled.', WPACU_PLUGIN_TEXT_DOMAIN); ?></p>
92
  </div>
93
  <?php
94
  }
96
  // After "Save changes" is clicked
97
  if ($this->status['updated']) {
98
  ?>
99
+ <div class="notice notice-success is-dismissible">
100
+ <p><span class="dashicons dashicons-yes"></span> <?php _e('The settings were successfully updated.', WPACU_PLUGIN_TEXT_DOMAIN); ?></p>
101
  </div>
102
  <?php
103
  }
111
  if (! empty($_POST) && array_key_exists('wpacu_settings_page', $_POST)) {
112
  check_admin_referer('wpacu_settings_update');
113
 
114
+ $data = Misc::getVar('post', WPACU_PLUGIN_ID . '_settings', array());
115
  $this->update($data);
116
  }
117
  }
154
  return true;
155
  }
156
 
157
+ // [wpacu_lite]
158
  // Prior to 1.2.4.4
159
+ if ( get_option( WPACU_PLUGIN_ID . '_frontend_show') == 1) {
160
  // Put it in the main settings option
161
  $settings = $this->getAll();
162
  $settings['frontend_show'] = 1;
163
  $this->update($settings);
164
 
165
+ delete_option( WPACU_PLUGIN_ID . '_frontend_show');
166
  return true;
167
  }
168
+ // [/wpacu_lite]
169
 
170
  return false;
171
  }
179
  return $this->currentSettings;
180
  }
181
 
182
+ $settingsOption = get_option(WPACU_PLUGIN_ID . '_settings');
183
 
184
+ // If there's already a record in the database
185
  if ($settingsOption !== '' && is_string($settingsOption)) {
186
  $settings = (array)json_decode($settingsOption);
187
 
200
  }
201
  }
202
 
203
+ // No record in the database? Set the default values
204
+ // That could be because no changes were done on the "Settings" page
205
+ // OR a full reset of the plugin (via "Tools") was performed
206
+
207
+ $defaultSettings = self::$defaultSettings;
208
 
209
  foreach ($this->settingsKeys as $settingsKey) {
210
+ if (! array_key_exists($settingsKey, $defaultSettings)) {
211
+ // Keep the keys with empty values to avoid notice errors
212
+ $defaultSettings[$settingsKey] = '';
213
+ }
214
  }
215
 
216
+ return $defaultSettings;
217
  }
218
 
219
  /**
223
  {
224
  $wpacuUpdate = new Update;
225
 
226
+ $settingsNotNull = array();
227
+
228
+ foreach ($settings as $settingKey => $settingValue) {
229
+ if ($settingValue !== '') {
230
+ $settingsNotNull[$settingKey] = $settingValue;
231
+ }
232
+ }
233
+
234
+ if (json_encode(self::$defaultSettings) === json_encode($settingsNotNull)) {
235
+ // Do not keep a record in the database (no point of having an extra entry)
236
+ // if the submitted values are the same as the default ones
237
+ delete_option(WPACU_PLUGIN_ID . '_settings');
238
+ $this->status['updated'] = true;
239
+ return;
240
+ }
241
+
242
+ $disableJQueryMigrate = isset($_POST[ WPACU_PLUGIN_ID . '_global_unloads']['disable_jquery_migrate']);
243
+ $disableCommentReply = isset($_POST[ WPACU_PLUGIN_ID . '_global_unloads']['disable_comment_reply']);
244
 
245
  /*
246
  * Add element(s) to the global unload rules
280
  $wpacuUpdate->removeEverywhereUnloads(array(), $removeFromUnloadList);
281
  }
282
 
283
+ update_option(WPACU_PLUGIN_ID . '_settings', json_encode($settings), 'no');
284
  $this->status['updated'] = true;
285
  }
286
  }
classes/Tools.php ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ namespace WpAssetCleanUp;
3
+
4
+ /**
5
+ * Class Tools
6
+ * @package WpAssetCleanUp
7
+ */
8
+ class Tools
9
+ {
10
+ /**
11
+ * @var
12
+ */
13
+ public $resetChoice;
14
+
15
+ /**
16
+ * Tools constructor.
17
+ */
18
+ public function __construct()
19
+ {
20
+ add_action('plugins_loaded', function() {
21
+ if ( array_key_exists( 'wpacu-tools-reset', $_POST ) && $_POST['wpacu-tools-reset'] ) {
22
+ $this->doReset();
23
+ }
24
+ });
25
+ }
26
+
27
+ /**
28
+ *
29
+ */
30
+ public function toolsPage()
31
+ {
32
+ $data = array();
33
+ Main::instance()->parseTemplate('admin-page-tools', $data, true);
34
+ }
35
+
36
+ /**
37
+ *
38
+ */
39
+ public function doReset()
40
+ {
41
+ // Several security checks before proceeding with the chosen action
42
+ if ( ! (isset($_POST['wpacu-tools-reset']) && $_POST['wpacu-tools-reset']) ) {
43
+ exit();
44
+ }
45
+
46
+ \check_admin_referer('wpacu_tools_reset');
47
+
48
+ $wpacuResetValue = isset($_POST['wpacu-reset']) ? $_POST['wpacu-reset'] : false;
49
+
50
+ if (! $wpacuResetValue) {
51
+ exit('Error: Field not found, the action is not valid!');
52
+ }
53
+
54
+ // Has to be confirmed
55
+ $wpacuConfirmedValue = isset($_POST['wpacu-action-confirmed']) ? $_POST['wpacu-action-confirmed'] : false;
56
+
57
+ if ($wpacuConfirmedValue !== 'yes') {
58
+ exit('Error: Action needs to be confirmed.');
59
+ }
60
+
61
+ if (! Menu::userCanManageAssets()) {
62
+ exit();
63
+ }
64
+
65
+ global $wpdb;
66
+
67
+ $this->resetChoice = $wpacuResetValue;
68
+
69
+ if ($wpacuResetValue === 'reset_everything') {
70
+ // `usermeta` and `termmeta` might have traces from the Pro version (if ever used)
71
+ foreach (array('postmeta', 'usermeta', 'termmeta') as $tableBaseName) {
72
+ $sqlQuery = <<<SQL
73
+ DELETE FROM `{$wpdb->prefix}{$tableBaseName}` WHERE meta_key LIKE '_wpassetcleanup_%'
74
+ SQL;
75
+ $wpdb->query($sqlQuery);
76
+ }
77
+
78
+ $sqlQuery = <<<SQL
79
+ DELETE FROM `{$wpdb->prefix}options` WHERE option_name LIKE 'wpassetcleanup_%'
80
+ SQL;
81
+ $wpdb->query($sqlQuery);
82
+ } elseif ($wpacuResetValue === 'reset_settings') {
83
+ $sqlQuery = <<<SQL
84
+ DELETE FROM `{$wpdb->prefix}options` WHERE option_name='wpassetcleanup_settings'
85
+ SQL;
86
+ $wpdb->query($sqlQuery);
87
+ }
88
+
89
+ add_action('wpacu_admin_notices', array($this, 'resetDone'));
90
+ }
91
+
92
+ /**
93
+ *
94
+ */
95
+ public function resetDone()
96
+ {
97
+ $msg = '';
98
+
99
+ if ($this->resetChoice === 'reset_settings') {
100
+ $msg = __('All the settings were reset to their default values.', WPACU_PLUGIN_TEXT_DOMAIN);
101
+ } elseif ($this->resetChoice === 'reset_everything') {
102
+ $msg = __('Everything was reset (including settings, individual &amp; bulk unloads, load exceptions) to the same point it was when you first activated the plugin', WPACU_PLUGIN_TEXT_DOMAIN);
103
+ }
104
+ ?>
105
+ <div class="updated notice wpacu-notice is-dismissible">
106
+ <p><span class="dashicons dashicons-yes"></span> <?php echo $msg; ?></p>
107
+ </div>
108
+ <?php
109
+ }
110
+ }
classes/Update.php CHANGED
@@ -111,11 +111,11 @@ HTML;
111
  $postUrlAnchor = $_SERVER['REQUEST_URI'].'#wpacu_wrap_assets';
112
  wp_die(
113
  sprintf(
114
- __('The nonce expired or is not correct, thus the request was not processed. %sPlease retry%s.', WPACU_PLUGIN_NAME),
115
  '<a href="'.$postUrlAnchor.'">',
116
  '</a>'
117
  ),
118
- __('Nonce Expired', WPACU_PLUGIN_NAME)
119
  );
120
  }
121
 
@@ -124,8 +124,8 @@ HTML;
124
  // Form submitted from the homepage
125
  // e.g. from a page such as latest blog posts, not a static page that was selected as home page)
126
  if (Misc::isHomePage() && ! ($postId > 0)) {
127
- $wpacuNoLoadAssets = isset($_POST[WPACU_PLUGIN_NAME])
128
- ? $_POST[WPACU_PLUGIN_NAME] : array();
129
 
130
  $this->updateFrontPage($wpacuNoLoadAssets);
131
  return;
@@ -202,7 +202,7 @@ HTML;
202
  public function savePost($postId, $post = array())
203
  {
204
  // This is triggered only if the "Asset CleanUp" meta box was loaded with the list of assets
205
- // Otherwise, $_POST[WPACU_PLUGIN_NAME] will be taken as empty which might be not if there are values in the database
206
  if (! (isset($_POST['wpacu_unload_assets_area_loaded']) && $_POST['wpacu_unload_assets_area_loaded'])) {
207
  return;
208
  }
@@ -227,8 +227,8 @@ HTML;
227
  return;
228
  }
229
 
230
- $wpacuNoLoadAssets = isset($_POST[WPACU_PLUGIN_NAME])
231
- ? $_POST[WPACU_PLUGIN_NAME] : array();
232
 
233
  if (is_array($wpacuNoLoadAssets)) {
234
  global $wpdb;
@@ -240,7 +240,7 @@ HTML;
240
  // Remove any row with no results
241
  $wpdb->delete(
242
  $wpdb->postmeta,
243
- array('post_id' => $postId, 'meta_key' => '_' . WPACU_PLUGIN_NAME . '_no_load')
244
  );
245
  $noUpdate = true;
246
  }
@@ -248,8 +248,8 @@ HTML;
248
  if (! $noUpdate) {
249
  $jsonNoAssetsLoadList = json_encode($wpacuNoLoadAssets);
250
 
251
- if (! add_post_meta($postId, '_' . WPACU_PLUGIN_NAME . '_no_load', $jsonNoAssetsLoadList, true)) {
252
- update_post_meta($postId, '_' . WPACU_PLUGIN_NAME . '_no_load', $jsonNoAssetsLoadList);
253
  }
254
  }
255
  }
@@ -282,8 +282,8 @@ HTML;
282
 
283
  $jsonNoAssetsLoadList = json_encode($wpacuNoLoadAssets);
284
 
285
- if (! update_option(WPACU_PLUGIN_NAME . '_front_page_no_load', $jsonNoAssetsLoadList)) {
286
- add_option(WPACU_PLUGIN_NAME . '_front_page_no_load', $jsonNoAssetsLoadList);
287
  }
288
 
289
  // If globally disabled, make exception to load for submitted assets
@@ -336,9 +336,9 @@ HTML;
336
 
337
  // Clear existing list first
338
  if ($type === 'post') {
339
- delete_post_meta($postId, '_' . WPACU_PLUGIN_NAME . '_load_exceptions');
340
  } elseif ($type === 'front_page') {
341
- delete_option(WPACU_PLUGIN_NAME . '_front_page_load_exceptions');
342
  } /* [wpacu_pro] */ elseif ($type === 'for_pro') {
343
  // Clear existing list for pages like: taxonomy, 404, search, date etc.
344
  do_action( 'wpacu_pro_clear_load_exceptions' );
@@ -399,11 +399,11 @@ HTML;
399
  $jsonLoadExceptions = json_encode($list);
400
 
401
  if ($type === 'post') {
402
- if (! add_post_meta($postId, '_' . WPACU_PLUGIN_NAME . '_load_exceptions', $jsonLoadExceptions, true)) {
403
- update_post_meta($postId, '_' . WPACU_PLUGIN_NAME . '_load_exceptions', $jsonLoadExceptions);
404
  }
405
  } elseif ($type === 'front_page') {
406
- update_option(WPACU_PLUGIN_NAME . '_front_page_load_exceptions', $jsonLoadExceptions);
407
  } /* [wpacu_pro] */ elseif ($type === 'for_pro') {
408
  // Update any load extensions for pages like: taxonomy, 404, search, date etc.
409
  do_action( 'wpacu_pro_update_load_exceptions', $jsonLoadExceptions );
@@ -442,7 +442,7 @@ HTML;
442
  public function saveToEverywhereUnloads($reqStyles = array(), $reqScripts = array())
443
  {
444
  // Is there any entry already in JSON format?
445
- $existingListJson = get_option(WPACU_PLUGIN_NAME.'_global_unload');
446
 
447
  // Default list as array
448
  $existingListEmpty = array('styles' => array(), 'scripts' => array());
@@ -467,7 +467,7 @@ HTML;
467
  $existingList['styles'] = array_unique($existingList['styles']);
468
  $existingList['scripts'] = array_unique($existingList['scripts']);
469
 
470
- update_option(WPACU_PLUGIN_NAME.'_global_unload', json_encode($existingList));
471
  }
472
 
473
  /**
@@ -480,7 +480,7 @@ HTML;
480
  public function removeEverywhereUnloads($stylesList = array(), $scriptsList = array(), $checkType = '')
481
  {
482
  if ($checkType === 'post') {
483
- $stylesList = Misc::getVar('post', 'wpacu_options_styles', array());
484
  $scriptsList = Misc::getVar('post', 'wpacu_options_scripts', array());
485
  }
486
 
@@ -504,7 +504,7 @@ HTML;
504
  }
505
  }
506
 
507
- $existingListJson = get_option(WPACU_PLUGIN_NAME.'_global_unload');
508
 
509
  if (! $existingListJson) {
510
  return false;
@@ -535,7 +535,7 @@ HTML;
535
  }
536
 
537
  if ($isUpdated) {
538
- update_option(WPACU_PLUGIN_NAME . '_global_unload', json_encode($existingList));
539
  }
540
  }
541
 
@@ -563,7 +563,7 @@ HTML;
563
  ? $_POST['wpacu_bulk_unload_scripts'] : array();
564
 
565
  // Is there any entry already in JSON format?
566
- $existingListJson = get_option(WPACU_PLUGIN_NAME.'_bulk_unload');
567
 
568
  // Default list as array
569
  $existingListEmpty = array(
@@ -596,6 +596,10 @@ HTML;
596
 
597
  if ($bulkType === 'post_type') {
598
  foreach ($values as $postType => $handles) {
 
 
 
 
599
  foreach (array_unique($handles) as $handle) {
600
  $existingList[ $assetType ]['post_type'][ $postType ][] = $handle;
601
  }
@@ -606,7 +610,7 @@ HTML;
606
  }
607
  }
608
 
609
- update_option(WPACU_PLUGIN_NAME.'_bulk_unload', json_encode($existingList));
610
  }
611
 
612
  /**
@@ -656,7 +660,7 @@ HTML;
656
  }
657
  }
658
 
659
- $existingListJson = get_option(WPACU_PLUGIN_NAME.'_bulk_unload');
660
 
661
  if (! $existingListJson) {
662
  return false;
@@ -686,7 +690,7 @@ HTML;
686
  }
687
  }
688
 
689
- update_option(WPACU_PLUGIN_NAME.'_bulk_unload', json_encode($existingList));
690
  }
691
 
692
  return $isUpdated;
111
  $postUrlAnchor = $_SERVER['REQUEST_URI'].'#wpacu_wrap_assets';
112
  wp_die(
113
  sprintf(
114
+ __('The nonce expired or is not correct, thus the request was not processed. %sPlease retry%s.', WPACU_PLUGIN_TEXT_DOMAIN),
115
  '<a href="'.$postUrlAnchor.'">',
116
  '</a>'
117
  ),
118
+ __('Nonce Expired', WPACU_PLUGIN_TEXT_DOMAIN)
119
  );
120
  }
121
 
124
  // Form submitted from the homepage
125
  // e.g. from a page such as latest blog posts, not a static page that was selected as home page)
126
  if (Misc::isHomePage() && ! ($postId > 0)) {
127
+ $wpacuNoLoadAssets = isset($_POST[WPACU_PLUGIN_ID])
128
+ ? $_POST[WPACU_PLUGIN_ID] : array();
129
 
130
  $this->updateFrontPage($wpacuNoLoadAssets);
131
  return;
202
  public function savePost($postId, $post = array())
203
  {
204
  // This is triggered only if the "Asset CleanUp" meta box was loaded with the list of assets
205
+ // Otherwise, $_POST[WPACU_PLUGIN_ID] will be taken as empty which might be not if there are values in the database
206
  if (! (isset($_POST['wpacu_unload_assets_area_loaded']) && $_POST['wpacu_unload_assets_area_loaded'])) {
207
  return;
208
  }
227
  return;
228
  }
229
 
230
+ $wpacuNoLoadAssets = isset($_POST[WPACU_PLUGIN_ID])
231
+ ? $_POST[WPACU_PLUGIN_ID] : array();
232
 
233
  if (is_array($wpacuNoLoadAssets)) {
234
  global $wpdb;
240
  // Remove any row with no results
241
  $wpdb->delete(
242
  $wpdb->postmeta,
243
+ array('post_id' => $postId, 'meta_key' => '_' . WPACU_PLUGIN_ID . '_no_load')
244
  );
245
  $noUpdate = true;
246
  }
248
  if (! $noUpdate) {
249
  $jsonNoAssetsLoadList = json_encode($wpacuNoLoadAssets);
250
 
251
+ if (! add_post_meta($postId, '_' . WPACU_PLUGIN_ID . '_no_load', $jsonNoAssetsLoadList, true)) {
252
+ update_post_meta($postId, '_' . WPACU_PLUGIN_ID . '_no_load', $jsonNoAssetsLoadList);
253
  }
254
  }
255
  }
282
 
283
  $jsonNoAssetsLoadList = json_encode($wpacuNoLoadAssets);
284
 
285
+ if (! update_option( WPACU_PLUGIN_ID . '_front_page_no_load', $jsonNoAssetsLoadList)) {
286
+ add_option( WPACU_PLUGIN_ID . '_front_page_no_load', $jsonNoAssetsLoadList);
287
  }
288
 
289
  // If globally disabled, make exception to load for submitted assets
336
 
337
  // Clear existing list first
338
  if ($type === 'post') {
339
+ delete_post_meta($postId, '_' . WPACU_PLUGIN_ID . '_load_exceptions');
340
  } elseif ($type === 'front_page') {
341
+ delete_option( WPACU_PLUGIN_ID . '_front_page_load_exceptions');
342
  } /* [wpacu_pro] */ elseif ($type === 'for_pro') {
343
  // Clear existing list for pages like: taxonomy, 404, search, date etc.
344
  do_action( 'wpacu_pro_clear_load_exceptions' );
399
  $jsonLoadExceptions = json_encode($list);
400
 
401
  if ($type === 'post') {
402
+ if (! add_post_meta($postId, '_' . WPACU_PLUGIN_ID . '_load_exceptions', $jsonLoadExceptions, true)) {
403
+ update_post_meta($postId, '_' . WPACU_PLUGIN_ID . '_load_exceptions', $jsonLoadExceptions);
404
  }
405
  } elseif ($type === 'front_page') {
406
+ update_option( WPACU_PLUGIN_ID . '_front_page_load_exceptions', $jsonLoadExceptions);
407
  } /* [wpacu_pro] */ elseif ($type === 'for_pro') {
408
  // Update any load extensions for pages like: taxonomy, 404, search, date etc.
409
  do_action( 'wpacu_pro_update_load_exceptions', $jsonLoadExceptions );
442
  public function saveToEverywhereUnloads($reqStyles = array(), $reqScripts = array())
443
  {
444
  // Is there any entry already in JSON format?
445
+ $existingListJson = get_option( WPACU_PLUGIN_ID . '_global_unload');
446
 
447
  // Default list as array
448
  $existingListEmpty = array('styles' => array(), 'scripts' => array());
467
  $existingList['styles'] = array_unique($existingList['styles']);
468
  $existingList['scripts'] = array_unique($existingList['scripts']);
469
 
470
+ update_option( WPACU_PLUGIN_ID . '_global_unload', json_encode($existingList));
471
  }
472
 
473
  /**
480
  public function removeEverywhereUnloads($stylesList = array(), $scriptsList = array(), $checkType = '')
481
  {
482
  if ($checkType === 'post') {
483
+ $stylesList = Misc::getVar('post', 'wpacu_options_styles', array());
484
  $scriptsList = Misc::getVar('post', 'wpacu_options_scripts', array());
485
  }
486
 
504
  }
505
  }
506
 
507
+ $existingListJson = get_option( WPACU_PLUGIN_ID . '_global_unload');
508
 
509
  if (! $existingListJson) {
510
  return false;
535
  }
536
 
537
  if ($isUpdated) {
538
+ update_option( WPACU_PLUGIN_ID . '_global_unload', json_encode($existingList));
539
  }
540
  }
541
 
563
  ? $_POST['wpacu_bulk_unload_scripts'] : array();
564
 
565
  // Is there any entry already in JSON format?
566
+ $existingListJson = get_option( WPACU_PLUGIN_ID . '_bulk_unload');
567
 
568
  // Default list as array
569
  $existingListEmpty = array(
596
 
597
  if ($bulkType === 'post_type') {
598
  foreach ($values as $postType => $handles) {
599
+ if (empty($handles)) {
600
+ continue;
601
+ }
602
+
603
  foreach (array_unique($handles) as $handle) {
604
  $existingList[ $assetType ]['post_type'][ $postType ][] = $handle;
605
  }
610
  }
611
  }
612
 
613
+ update_option( WPACU_PLUGIN_ID . '_bulk_unload', json_encode($existingList));
614
  }
615
 
616
  /**
660
  }
661
  }
662
 
663
+ $existingListJson = get_option( WPACU_PLUGIN_ID . '_bulk_unload');
664
 
665
  if (! $existingListJson) {
666
  return false;
690
  }
691
  }
692
 
693
+ update_option( WPACU_PLUGIN_ID . '_bulk_unload', json_encode($existingList));
694
  }
695
 
696
  return $isUpdated;
freemius-load.php ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if ( ! function_exists( 'wpassetcleanup_fs' )
3
+ && ! class_exists( 'Freemius' )
4
+ && is_admin() ) {
5
+
6
+ // Create a helper function for easy SDK access.
7
+ function wpassetcleanup_fs() {
8
+ global $wpassetcleanup_fs;
9
+
10
+ if ( ! isset( $wpassetcleanup_fs ) ) {
11
+ // Include Freemius SDK.
12
+ require_once __DIR__ . '/freemius/start.php';
13
+
14
+ $wpassetcleanup_fs = fs_dynamic_init( array (
15
+ 'id' => '2951',
16
+ 'slug' => WPACU_PLUGIN_TEXT_DOMAIN,
17
+ 'type' => 'plugin',
18
+ 'public_key' => 'pk_70ecc6600cb03b5168150b4c99257',
19
+ 'is_premium' => false,
20
+ 'has_addons' => false,
21
+ 'has_paid_plans' => false,
22
+ 'menu' => array(
23
+ 'slug' => WPACU_PLUGIN_ID . '_settings',
24
+ 'override_exact' => true,
25
+ 'account' => false,
26
+ 'contact' => false,
27
+ 'support' => true,
28
+ ),
29
+ ) );
30
+ }
31
+
32
+ return $wpassetcleanup_fs;
33
+ }
34
+
35
+ // Init Freemius.
36
+ wpassetcleanup_fs();
37
+
38
+ // Signal that SDK was initiated.
39
+ do_action('wpassetcleanup_fs_loaded');
40
+
41
+ function wpassetcleanup_fs_settings_url() {
42
+ return admin_url('admin.php?page='.WPACU_PLUGIN_ID.'_settings');
43
+ }
44
+
45
+ wpassetcleanup_fs()->add_filter('connect_url', WPACU_PLUGIN_ID.'_fs_settings_url');
46
+ wpassetcleanup_fs()->add_filter('after_skip_url', WPACU_PLUGIN_ID.'_fs_settings_url');
47
+ wpassetcleanup_fs()->add_filter('after_connect_url', WPACU_PLUGIN_ID.'_fs_settings_url');
48
+ wpassetcleanup_fs()->add_filter('after_pending_connect_url', WPACU_PLUGIN_ID.'_fs_settings_url');
49
+ }
freemius/assets/css/admin/account.css ADDED
@@ -0,0 +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 .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/add-ons.css ADDED
@@ -0,0 +1,2 @@
 
 
1
+ #fs_addons .fs-cards-list{list-style:none}#fs_addons .fs-cards-list .fs-card{float:left;height:152px;width:310px;padding:0;margin:0 0 30px 30px;font-size:14px;list-style:none;border:1px solid #ddd;cursor:pointer;position:relative}#fs_addons .fs-cards-list .fs-card .fs-overlay{position:absolute;left:0;right:0;bottom:0;top:0;z-index:9}#fs_addons .fs-cards-list .fs-card .fs-inner{background-color:#fff;overflow:hidden;height:100%;position:relative}#fs_addons .fs-cards-list .fs-card .fs-inner ul{-moz-transition:all,0.15s;-o-transition:all,0.15s;-ms-transition:all,0.15s;-webkit-transition:all,0.15s;transition:all,0.15s;left:0;right:0;top:0;position:absolute}#fs_addons .fs-cards-list .fs-card .fs-inner li{list-style:none;line-height:18px;padding:0 15px;width:100%;display:block;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box}#fs_addons .fs-cards-list .fs-card .fs-inner .fs-card-banner{padding:0;margin:0;line-height:0;display:block;height:100px;background-repeat:repeat-x;background-size:100% 100%;-moz-transition:all,0.15s;-o-transition:all,0.15s;-ms-transition:all,0.15s;-webkit-transition:all,0.15s;transition:all,0.15s}#fs_addons .fs-cards-list .fs-card .fs-inner .fs-title{margin:10px 0 0 0;height:18px;overflow:hidden;color:#000;white-space:nowrap;text-overflow:ellipsis;font-weight:bold}#fs_addons .fs-cards-list .fs-card .fs-inner .fs-offer{font-size:0.9em}#fs_addons .fs-cards-list .fs-card .fs-inner .fs-description{background-color:#f9f9f9;padding:10px 15px 100px 15px;border-top:1px solid #eee;margin:0 0 10px 0;color:#777}#fs_addons .fs-cards-list .fs-card .fs-inner .fs-tag{position:absolute;top:10px;right:0px;background:greenyellow;display:block;padding:2px 10px;-moz-box-shadow:1px 1px 1px rgba(0,0,0,0.3);-webkit-box-shadow:1px 1px 1px rgba(0,0,0,0.3);box-shadow:1px 1px 1px rgba(0,0,0,0.3);text-transform:uppercase;font-size:0.9em;font-weight:bold}#fs_addons .fs-cards-list .fs-card .fs-inner .fs-cta .button{position:absolute;top:112px;right:10px}@media screen and (min-width: 960px){#fs_addons .fs-cards-list .fs-card:hover .fs-overlay{border:2px solid #29abe1;margin-left:-1px;margin-top:-1px}#fs_addons .fs-cards-list .fs-card:hover .fs-inner ul{top:-100px}#fs_addons .fs-cards-list .fs-card:hover .fs-inner .fs-title,#fs_addons .fs-cards-list .fs-card:hover .fs-inner .fs-offer{color:#29abe1}}
2
+ #TB_window,#TB_window iframe{width:772px !important}#plugin-information #section-description h2,#plugin-information #section-description h3,#plugin-information #section-description p,#plugin-information #section-description b,#plugin-information #section-description i,#plugin-information #section-description blockquote,#plugin-information #section-description li,#plugin-information #section-description ul,#plugin-information #section-description ol{clear:none}#plugin-information #section-description .fs-selling-points{padding-bottom:10px;border-bottom:1px solid #ddd}#plugin-information #section-description .fs-selling-points ul{margin:0}#plugin-information #section-description .fs-selling-points ul li{padding:0;list-style:none outside none}#plugin-information #section-description .fs-selling-points ul li i.dashicons{color:#71ae00;font-size:3em;vertical-align:middle;line-height:30px;float:left;margin:0 0 0 -15px}#plugin-information #section-description .fs-selling-points ul li h3{margin:1em 30px !important}#plugin-information #section-description .fs-screenshots:after{content:"";display:table;clear:both}#plugin-information #section-description .fs-screenshots ul{list-style:none;margin:0}#plugin-information #section-description .fs-screenshots ul li{width:225px;height:225px;float:left;margin-bottom:20px;-moz-box-sizing:content-box;-webkit-box-sizing:content-box;box-sizing:content-box}#plugin-information #section-description .fs-screenshots ul li a{display:block;width:100%;height:100%;border:1px solid;-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);background-size:cover}#plugin-information #section-description .fs-screenshots ul li.odd{margin-right:20px}#plugin-information .plugin-information-pricing{margin:-16px;border-bottom:1px solid #ddd}#plugin-information .plugin-information-pricing .fs-plan h3{margin-top:0;padding:20px;font-size:16px}#plugin-information .plugin-information-pricing .fs-plan .nav-tab-wrapper{border-bottom:1px solid #ddd}#plugin-information .plugin-information-pricing .fs-plan .nav-tab-wrapper .nav-tab{cursor:pointer;position:relative;padding:0 10px;font-size:0.9em}#plugin-information .plugin-information-pricing .fs-plan .nav-tab-wrapper .nav-tab label{text-transform:uppercase;color:green;background:greenyellow;position:absolute;left:-1px;right:-1px;bottom:100%;border:1px solid darkgreen;padding:2px;text-align:center;font-size:0.9em;line-height:1em}#plugin-information .plugin-information-pricing .fs-plan .nav-tab-wrapper .nav-tab.nav-tab-active{cursor:default;background:#fffeec;border-bottom-color:#fffeec}#plugin-information .plugin-information-pricing .fs-plan.fs-single-cycle h3{background:#fffeec;margin:0;padding-bottom:0;color:#0073aa}#plugin-information .plugin-information-pricing .fs-plan.fs-single-cycle .nav-tab-wrapper,#plugin-information .plugin-information-pricing .fs-plan.fs-single-cycle .fs-billing-frequency{display:none}#plugin-information .plugin-information-pricing .fs-plan .fs-pricing-body{background:#fffeec;padding:20px}#plugin-information .plugin-information-pricing .fs-plan .button{width:100%;text-align:center;font-weight:bold;text-transform:uppercase;font-size:1.1em}#plugin-information .plugin-information-pricing .fs-plan label{white-space:nowrap}#plugin-information .plugin-information-pricing .fs-plan var{font-style:normal}#plugin-information .plugin-information-pricing .fs-plan .fs-billing-frequency,#plugin-information .plugin-information-pricing .fs-plan .fs-annual-discount{text-align:center;display:block;font-weight:bold;margin-bottom:10px;text-transform:uppercase;background:#F3F3F3;padding:2px;border:1px solid #ccc}#plugin-information .plugin-information-pricing .fs-plan .fs-annual-discount{text-transform:none;color:green;background:greenyellow}#plugin-information .plugin-information-pricing .fs-plan ul.fs-trial-terms{font-size:0.9em}#plugin-information .plugin-information-pricing .fs-plan ul.fs-trial-terms i{float:left;margin:0 0 0 -15px}#plugin-information .plugin-information-pricing .fs-plan ul.fs-trial-terms li{margin:10px 0 0 0}#plugin-information #section-features .fs-features{margin:-20px -26px}#plugin-information #section-features table{width:100%;border-spacing:0;border-collapse:separate}#plugin-information #section-features table thead th{padding:10px 0}#plugin-information #section-features table thead .fs-price{color:#71ae00;font-weight:normal;display:block;text-align:center}#plugin-information #section-features table tbody td{border-top:1px solid #ccc;padding:10px 0;text-align:center;width:100px;color:#71ae00}#plugin-information #section-features table tbody td:first-child{text-align:left;width:auto;color:inherit;padding-left:26px}#plugin-information #section-features table tbody tr.fs-odd td{background:#fefefe}#plugin-information #section-features .dashicons-yes{width:30px;height:30px;font-size:30px}@media screen and (max-width: 961px){#fs_addons .fs-cards-list .fs-card{height:265px}}
freemius/assets/css/admin/affiliation.css ADDED
@@ -0,0 +1 @@
 
1
+ @charset "UTF-8";#fs_affiliation_content_wrapper #messages{margin-top:25px}#fs_affiliation_content_wrapper h3{font-size:24px;padding:0;margin-left:0}#fs_affiliation_content_wrapper ul li{-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/checkout.css ADDED
@@ -0,0 +1 @@
 
1
+ @media screen and (max-width: 782px){#wpbody-content{padding-bottom:0 !important}}
freemius/assets/css/admin/common.css ADDED
@@ -0,0 +1,2 @@
 
 
1
+ .theme-browser .theme .fs-premium-theme-badge{position:absolute;top:10px;right:0;background:#71ae00;color:#fff;text-transform:uppercase;padding:5px 10px;-moz-border-radius:3px 0 0 3px;-webkit-border-radius:3px 0 0 3px;border-radius:3px 0 0 3px;font-weight:bold;border-right:0;-moz-box-shadow:0 2px 1px -1px rgba(0,0,0,0.3);-webkit-box-shadow:0 2px 1px -1px rgba(0,0,0,0.3);box-shadow:0 2px 1px -1px rgba(0,0,0,0.3);font-size:1.1em}#iframe{line-height:0;font-size:0}.fs-full-size-wrapper{margin:40px 0 -65px -20px}@media (max-width: 600px){.fs-full-size-wrapper{margin:0 0 -65px -10px}}
2
+ .fs-notice{position:relative}.fs-notice.fs-has-title{margin-bottom:30px !important}.fs-notice.success{color:green}.fs-notice.promotion{border-color:#00a0d2 !important;background-color:#f2fcff !important}.fs-notice .fs-notice-body{margin:.5em 0;padding:2px}.fs-notice .fs-close{cursor:pointer;color:#aaa;float:right}.fs-notice .fs-close:hover{color:#666}.fs-notice .fs-close>*{margin-top:7px;display:inline-block}.fs-notice label.fs-plugin-title{background:rgba(0,0,0,0.3);color:#fff;padding:2px 10px;position:absolute;top:100%;bottom:auto;right:auto;-moz-border-radius:0 0 3px 3px;-webkit-border-radius:0 0 3px 3px;border-radius:0 0 3px 3px;left:10px;font-size:12px;font-weight:bold;cursor:auto}div.fs-notice.updated,div.fs-notice.success,div.fs-notice.promotion{display:block !important}.rtl .fs-notice .fs-close{float:left}.fs-secure-notice{position:fixed;top:32px;left:160px;right:0;background:#ebfdeb;padding:10px 20px;color:green;z-index:9999;-moz-box-shadow:0 2px 2px rgba(6,113,6,0.3);-webkit-box-shadow:0 2px 2px rgba(6,113,6,0.3);box-shadow:0 2px 2px rgba(6,113,6,0.3);opacity:0.95;filter:alpha(opacity=95)}.fs-secure-notice:hover{opacity:1;filter:alpha(opacity=100)}.fs-secure-notice a.fs-security-proof{color:green;text-decoration:none}@media screen and (max-width: 960px){.fs-secure-notice{left:36px}}@media screen and (max-width: 600px){.fs-secure-notice{display:none}}@media screen and (max-width: 500px){#fs_promo_tab{display:none}}@media screen and (max-width: 782px){.fs-secure-notice{left:0;top:46px;text-align:center}}span.fs-submenu-item.fs-sub:before{content:'\21B3';padding:0 5px}.rtl span.fs-submenu-item.fs-sub:before{content:'\21B2'}.fs-submenu-item.pricing.upgrade-mode{color:greenyellow}.fs-submenu-item.pricing.trial-mode{color:#83e2ff}#adminmenu .update-plugins.fs-trial{background-color:#00b9eb}.fs-ajax-spinner{border:0;width:20px;height:20px;margin-right:5px;vertical-align:sub;display:inline-block;background:url("/wp-admin/images/wpspin_light-2x.gif");background-size:contain}.wrap.fs-section h2{text-align:left}
freemius/assets/css/admin/connect.css ADDED
@@ -0,0 +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.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/debug.css ADDED
@@ -0,0 +1 @@
 
1
+ .switch{position:relative;display:inline-block;font-size:1.6em;font-weight:bold;color:#ccc;text-shadow:0px 1px 1px rgba(255,255,255,0.8);height:18px;padding:6px 6px 5px 6px;border:1px solid #ccc;border:1px solid rgba(0,0,0,0.2);border-radius:4px;background:#ececec;box-shadow:0px 0px 4px rgba(0,0,0,0.1),inset 0px 1px 3px 0px rgba(0,0,0,0.1);cursor:pointer}.switch span{display:inline-block;width:35px;text-transform:uppercase}.switch span.on{color:#6bc406}.switch .toggle{position:absolute;top:1px;width:37px;height:25px;border:1px solid #ccc;border:1px solid rgba(0,0,0,0.3);border-radius:4px;background:#fff;background:-moz-linear-gradient(top, #ececec 0%, #fff 100%);background:-webkit-gradient(linear, left top, left bottom, color-stop(0%, #ececec), color-stop(100%, #fff));background:-webkit-linear-gradient(top, #ececec 0%, #fff 100%);background:-o-linear-gradient(top, #ececec 0%, #fff 100%);background:-ms-linear-gradient(top, #ececec 0%, #fff 100%);background:linear-gradient(top, #ececec 0%, #fff 100%);box-shadow:inset 0px 1px 0px 0px rgba(255,255,255,0.5);z-index:999;-moz-transition:all 0.15s ease-in-out;-o-transition:all 0.15s ease-in-out;-ms-transition:all 0.15s ease-in-out;-webkit-transition:all 0.15s ease-in-out;transition:all 0.15s ease-in-out}.switch.on .toggle{left:2%}.switch.off .toggle{left:54%}.switch.round{padding:0px 20px;border-radius:40px}.switch.round .toggle{border-radius:40px;width:14px;height:14px}.switch.round.on .toggle{left:3%;background:#6bc406}.switch.round.off .toggle{left:58%}.switch-label{font-size:20px;line-height:31px;margin:0 5px}#fs_log_book table{font-family:Consolas,Monaco,monospace;font-size:12px}#fs_log_book table th{color:#ccc}#fs_log_book table tr{background:#232525}#fs_log_book table tr.alternate{background:#2b2b2b}#fs_log_book table tr td.fs-col--logger{color:#5a7435}#fs_log_book table tr td.fs-col--type{color:#ffc861}#fs_log_book table tr td.fs-col--function{color:#a7b7b1;font-weight:bold}#fs_log_book table tr td.fs-col--message,#fs_log_book table tr td.fs-col--message a{color:#9a73ac !important}#fs_log_book table tr td.fs-col--file{color:#d07922}#fs_log_book table tr td.fs-col--timestamp{color:#6596be}
freemius/assets/css/admin/dialog-boxes.css ADDED
@@ -0,0 +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-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-subscription-cancellation .fs-price-increase-warning{color:red;font-weight:bold;padding:0 25px;margin-bottom:0}.fs-modal.fs-modal-subscription-cancellation ul.subscription-actions label input{float:left;top:5px;position:relative}.rtl .fs-modal.fs-modal-subscription-cancellation ul.subscription-actions label input{float:right}.fs-modal.fs-modal-subscription-cancellation ul.subscription-actions label span{display:block;margin-left:24px}.rtl .fs-modal.fs-modal-subscription-cancellation ul.subscription-actions label span{margin-left:0;margin-right:24px}.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}.button-primary.warn{box-shadow:0 1px 0 #d2593c;text-shadow:0 -1px 1px #d2593c,1px 0 1px #d2593c,0 1px 1px #d2593c,-1px 0 1px #d2593c;background:#f56a48;border-color:#ec6544 #d2593c #d2593c}.button-primary.warn:hover{background:#fd6d4a;border-color:#d2593c}.button-primary.warn:focus{box-shadow:0 1px 0 #dd6041,0 0 2px 1px #e4a796}.button-primary.warn:active{background:#dd6041;border-color:#d2593c;box-shadow:inset 0 2px 0 #d2593c}.button-primary.warn.disabled{color:#f5b3a1 !important;background:#e76444 !important;border-color:#d85e40 !important;text-shadow:0 -1px 0 rgba(0,0,0,0.1) !important}
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/css/admin/index.php ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ <?php
2
+ // Silence is golden.
3
+ // Hide file structure from users on unprotected servers.
freemius/assets/css/customizer.css ADDED
@@ -0,0 +1 @@
 
1
+ #fs_customizer_upsell .fs-customizer-plan{padding:10px 20px 20px 20px;border-radius:3px;background:#fff}#fs_customizer_upsell .fs-customizer-plan h2{position:relative;margin:0;line-height:2em;text-transform:uppercase}#fs_customizer_upsell .fs-customizer-plan h2 .button-link{top:-2px}#fs_customizer_upsell .fs-feature{position:relative}#fs_customizer_upsell .dashicons-yes{color:#0085ba;font-size:2em;vertical-align:bottom;margin-left:-7px;margin-right:10px}.rtl #fs_customizer_upsell .dashicons-yes{margin-left:10px;margin-right:-7px}#fs_customizer_upsell .dashicons-editor-help{color:#bbb;cursor:help}#fs_customizer_upsell .dashicons-editor-help .fs-feature-desc{opacity:0;visibility:hidden;-moz-transition:opacity 0.3s ease-in-out;-o-transition:opacity 0.3s ease-in-out;-ms-transition:opacity 0.3s ease-in-out;-webkit-transition:opacity 0.3s ease-in-out;transition:opacity 0.3s ease-in-out;position:absolute;background:#000;color:#fff;font-family:'arial', serif;font-size:12px;padding:10px;z-index:999999;bottom:100%;margin-bottom:5px;left:0;right:0;-moz-border-radius:5px;-webkit-border-radius:5px;border-radius:5px;-moz-box-shadow:1px 1px 1px rgba(0,0,0,0.2);-webkit-box-shadow:1px 1px 1px rgba(0,0,0,0.2);box-shadow:1px 1px 1px rgba(0,0,0,0.2);line-height:1.3em;font-weight:bold;text-align:left}.rtl #fs_customizer_upsell .dashicons-editor-help .fs-feature-desc{text-align:right}#fs_customizer_upsell .dashicons-editor-help .fs-feature-desc::after{content:' ';display:block;width:0;height:0;border-style:solid;border-width:5px 5px 0 5px;border-color:#000 transparent transparent transparent;position:absolute;top:100%;left:21px}.rtl #fs_customizer_upsell .dashicons-editor-help .fs-feature-desc::after{right:21px;left:auto}#fs_customizer_upsell .dashicons-editor-help:hover .fs-feature-desc{visibility:visible;opacity:1}#fs_customizer_upsell .button-primary{display:block;text-align:center;margin-top:10px}#fs_customizer_support{display:block !important}#fs_customizer_support .button{float:right}#fs_customizer_support .button-group{width:100%;display:block;margin-top:10px}#fs_customizer_support .button-group .button{float:none;width:50%;text-align:center}
freemius/assets/css/index.php ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ <?php
2
+ // Silence is golden.
3
+ // Hide file structure from users on unprotected servers.
freemius/assets/img/index.php ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ <?php
2
+ // Silence is golden.
3
+ // Hide file structure from users on unprotected servers.
freemius/assets/img/plugin-icon.png ADDED
Binary file
freemius/assets/img/theme-icon.png ADDED
Binary file
freemius/assets/img/wp-asset-clean-up.png ADDED
Binary file
freemius/assets/index.php ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ <?php
2
+ // Silence is golden.
3
+ // Hide file structure from users on unprotected servers.
freemius/assets/js/index.php ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ <?php
2
+ // Silence is golden.
3
+ // Hide file structure from users on unprotected servers.
freemius/assets/js/nojquery.ba-postmessage.js ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*!
2
+ * jQuery postMessage - v0.5 - 9/11/2009
3
+ * http://benalman.com/projects/jquery-postmessage-plugin/
4
+ *
5
+ * Copyright (c) 2009 "Cowboy" Ben Alman
6
+ * Dual licensed under the MIT and GPL licenses.
7
+ * http://benalman.com/about/license/
8
+ *
9
+ * Non-jQuery fork by Jeff Lee
10
+ *
11
+ * This fork consists of the following changes:
12
+ * 1. Basic code cleanup and restructuring, for legibility.
13
+ * 2. The `postMessage` and `receiveMessage` functions can be bound arbitrarily,
14
+ * in terms of both function names and object scope. Scope is specified by
15
+ * the the "this" context of NoJQueryPostMessageMixin();
16
+ * 3. I've removed the check for Opera 9.64, which used `$.browser`. There were
17
+ * at least three different GitHub users requesting the removal of this
18
+ * "Opera sniff" on the original project's Issues page, so I figured this
19
+ * would be a relatively safe change.
20
+ * 4. `postMessage` no longer uses `$.param` to serialize messages that are not
21
+ * strings. I actually prefer this structure anyway. `receiveMessage` does
22
+ * not implement a corresponding deserialization step, and as such it seems
23
+ * cleaner and more symmetric to leave both data serialization and
24
+ * deserialization to the client.
25
+ * 5. The use of `$.isFunction` is replaced by a functionally-identical check.
26
+ * 6. The `$:nomunge` YUI option is no longer necessary.
27
+ */
28
+
29
+ function NoJQueryPostMessageMixin(postBinding, receiveBinding) {
30
+
31
+ var setMessageCallback, unsetMessageCallback, currentMsgCallback,
32
+ intervalId, lastHash, cacheBust = 1;
33
+
34
+ if (window.postMessage) {
35
+
36
+ if (window.addEventListener) {
37
+ setMessageCallback = function(callback) {
38
+ window.addEventListener('message', callback, false);
39
+ }
40
+
41
+ unsetMessageCallback = function(callback) {
42
+ window.removeEventListener('message', callback, false);
43
+ }
44
+ } else {
45
+ setMessageCallback = function(callback) {
46
+ window.attachEvent('onmessage', callback);
47
+ }
48
+
49
+ unsetMessageCallback = function(callback) {
50
+ window.detachEvent('onmessage', callback);
51
+ }
52
+ }
53
+
54
+ this[postBinding] = function(message, targetUrl, target) {
55
+ if (!targetUrl) {
56
+ return;
57
+ }
58
+
59
+ // The browser supports window.postMessage, so call it with a targetOrigin
60
+ // set appropriately, based on the targetUrl parameter.
61
+ target.postMessage( message, targetUrl.replace( /([^:]+:\/\/[^\/]+).*/, '$1' ) );
62
+ }
63
+
64
+ // Since the browser supports window.postMessage, the callback will be
65
+ // bound to the actual event associated with window.postMessage.
66
+ this[receiveBinding] = function(callback, sourceOrigin, delay) {
67
+ // Unbind an existing callback if it exists.
68
+ if (currentMsgCallback) {
69
+ unsetMessageCallback(currentMsgCallback);
70
+ currentMsgCallback = null;
71
+ }
72
+
73
+ if (!callback) {
74
+ return false;
75
+ }
76
+
77
+ // Bind the callback. A reference to the callback is stored for ease of
78
+ // unbinding.
79
+ currentMsgCallback = setMessageCallback(function(e) {
80
+ switch(Object.prototype.toString.call(sourceOrigin)) {
81
+ case '[object String]':
82
+ if (sourceOrigin !== e.origin) {
83
+ return false;
84
+ }
85
+ break;
86
+ case '[object Function]':
87
+ if (sourceOrigin(e.origin)) {
88
+ return false;
89
+ }
90
+ break;
91
+ }
92
+
93
+ callback(e);
94
+ });
95
+ };
96
+
97
+ } else {
98
+
99
+ this[postBinding] = function(message, targetUrl, target) {
100
+ if (!targetUrl) {
101
+ return;
102
+ }
103
+
104
+ // The browser does not support window.postMessage, so set the location
105
+ // of the target to targetUrl#message. A bit ugly, but it works! A cache
106
+ // bust parameter is added to ensure that repeat messages trigger the
107
+ // callback.
108
+ target.location = targetUrl.replace( /#.*$/, '' ) + '#' + (+new Date) + (cacheBust++) + '&' + message;
109
+ }
110
+
111
+ // Since the browser sucks, a polling loop will be started, and the
112
+ // callback will be called whenever the location.hash changes.
113
+ this[receiveBinding] = function(callback, sourceOrigin, delay) {
114
+ if (intervalId) {
115
+ clearInterval(intervalId);
116
+ intervalId = null;
117
+ }
118
+
119
+ if (callback) {
120
+ delay = typeof sourceOrigin === 'number'
121
+ ? sourceOrigin
122
+ : typeof delay === 'number'
123
+ ? delay
124
+ : 100;
125
+
126
+ intervalId = setInterval(function(){
127
+ var hash = document.location.hash,
128
+ re = /^#?\d+&/;
129
+ if ( hash !== lastHash && re.test( hash ) ) {
130
+ lastHash = hash;
131
+ callback({ data: hash.replace( re, '' ) });
132
+ }
133
+ }, delay );
134
+ }
135
+ };
136
+
137
+ }
138
+
139
+ return this;
140
+ }
freemius/assets/js/nojquery.ba-postmessage.min.js ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * nojquery-postmessage by Jeff Lee
3
+ * a non-jQuery fork of:
4
+ *
5
+ * jQuery postMessage - v0.5 - 9/11/2009
6
+ * http://benalman.com/projects/jquery-postmessage-plugin/
7
+ *
8
+ * Copyright (c) 2009 "Cowboy" Ben Alman
9
+ * Dual licensed under the MIT and GPL licenses.
10
+ * http://benalman.com/about/license/
11
+ */
12
+ function NoJQueryPostMessageMixin(g,a){var b,h,e,d,f,c=1;if(window.postMessage){if(window.addEventListener){b=function(i){window.addEventListener("message",i,false)};h=function(i){window.removeEventListener("message",i,false)}}else{b=function(i){window.attachEvent("onmessage",i)};h=function(i){window.detachEvent("onmessage",i)}}this[g]=function(i,k,j){if(!k){return}j.postMessage(i,k.replace(/([^:]+:\/\/[^\/]+).*/,"$1"))};this[a]=function(k,j,i){if(e){h(e);e=null}if(!k){return false}e=b(function(l){switch(Object.prototype.toString.call(j)){case"[object String]":if(j!==l.origin){return false}break;case"[object Function]":if(j(l.origin)){return false}break}k(l)})}}else{this[g]=function(i,k,j){if(!k){return}j.location=k.replace(/#.*$/,"")+"#"+(+new Date)+(c++)+"&"+i};this[a]=function(k,j,i){if(d){clearInterval(d);d=null}if(k){i=typeof j==="number"?j:typeof i==="number"?i:100;d=setInterval(function(){var m=document.location.hash,l=/^#?\d+&/;if(m!==f&&l.test(m)){f=m;k({data:m.replace(l,"")})}},i)}}}return this};
freemius/assets/js/postmessage.js ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (function ($, undef) {
2
+ var global = this;
3
+
4
+ // Namespace.
5
+ global.FS = global.FS || {};
6
+
7
+ global.FS.PostMessage = function ()
8
+ {
9
+ var
10
+ _is_child = false,
11
+ _postman = new NoJQueryPostMessageMixin('postMessage', 'receiveMessage'),
12
+ _callbacks = {},
13
+ _base_url,
14
+ _parent_url = decodeURIComponent(document.location.hash.replace(/^#/, '')),
15
+ _parent_subdomain = _parent_url.substring(0, _parent_url.indexOf('/', ('https://' === _parent_url.substring(0, ('https://').length)) ? 8 : 7)),
16
+ _init = function () {
17
+ _postman.receiveMessage(function (e) {
18
+ var data = JSON.parse(e.data);
19
+
20
+ if (_callbacks[data.type]) {
21
+ for (var i = 0; i < _callbacks[data.type].length; i++) {
22
+ // Execute type callbacks.
23
+ _callbacks[data.type][i](data.data);
24
+ }
25
+ }
26
+ }, _base_url);
27
+ },
28
+ _hasParent = ('' !== _parent_url),
29
+ $window = $(window),
30
+ $html = $('html');
31
+
32
+ return {
33
+ init : function (url, iframes)
34
+ {
35
+ _base_url = url;
36
+ _init();
37
+
38
+ // Automatically receive forward messages.
39
+ FS.PostMessage.receiveOnce('forward', function (data){
40
+ window.location = data.url;
41
+ });
42
+
43
+ iframes = iframes || [];
44
+
45
+ if (iframes.length > 0) {
46
+ $window.on('scroll', function () {
47
+ for (var i = 0; i < iframes.length; i++) {
48
+ FS.PostMessage.postScroll(iframes[i]);
49
+ }
50
+ });
51
+ }
52
+ },
53
+ init_child : function ()
54
+ {
55
+ this.init(_parent_subdomain);
56
+
57
+ _is_child = true;
58
+
59
+ // Post height of a child right after window is loaded.
60
+ $(window).bind('load', function () {
61
+ FS.PostMessage.postHeight();
62
+
63
+ // Post message that window was loaded.
64
+ FS.PostMessage.post('loaded');
65
+ });
66
+ },
67
+ hasParent : function ()
68
+ {
69
+ return _hasParent;
70
+ },
71
+ postHeight : function (diff, wrapper) {
72
+ diff = diff || 0;
73
+ wrapper = wrapper || '#wrap_section';
74
+ this.post('height', {
75
+ height: diff + $(wrapper).outerHeight(true)
76
+ });
77
+ },
78
+ postScroll : function (iframe) {
79
+ this.post('scroll', {
80
+ top: $window.scrollTop(),
81
+ height: ($window.height() - parseFloat($html.css('paddingTop')) - parseFloat($html.css('marginTop')))
82
+ }, iframe);
83
+ },
84
+ post : function (type, data, iframe)
85
+ {
86
+ console.debug('PostMessage.post', type);
87
+
88
+ if (iframe)
89
+ {
90
+ // Post to iframe.
91
+ _postman.postMessage(JSON.stringify({
92
+ type: type,
93
+ data: data
94
+ }), iframe.src, iframe.contentWindow);
95
+ }
96
+ else {
97
+ // Post to parent.
98
+ _postman.postMessage(JSON.stringify({
99
+ type: type,
100
+ data: data
101
+ }), _parent_url, window.parent);
102
+ }
103
+ },
104
+ receive: function (type, callback)
105
+ {
106
+ console.debug('PostMessage.receive', type);
107
+
108
+ if (undef === _callbacks[type])
109
+ _callbacks[type] = [];
110
+
111
+ _callbacks[type].push(callback);
112
+ },
113
+ receiveOnce: function (type, callback)
114
+ {
115
+ if (this.is_set(type))
116
+ return;
117
+
118
+ this.receive(type, callback);
119
+ },
120
+ // Check if any callbacks assigned to a specified message type.
121
+ is_set: function (type)
122
+ {
123
+ return (undef != _callbacks[type]);
124
+ },
125
+ parent_url: function ()
126
+ {
127
+ return _parent_url;
128
+ },
129
+ parent_subdomain: function ()
130
+ {
131
+ return _parent_subdomain;
132
+ }
133
+ };
134
+ }();
135
+ })(jQuery);
freemius/config.php ADDED
@@ -0,0 +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
+ 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 ADDED
@@ -0,0 +1,597 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.7
7
+ */
8
+
9
+ if ( ! defined( 'ABSPATH' ) ) {
10
+ exit;
11
+ }
12
+
13
+
14
+ /**
15
+ * - Each instance of Freemius class represents a single plugin
16
+ * install by a single user (the installer of the plugin).
17
+ *
18
+ * - Each website can only have one install of the same plugin.
19
+ *
20
+ * - Install entity is only created after a user connects his account with Freemius.
21
+ *
22
+ * Class Freemius_Abstract
23
+ */
24
+ abstract class Freemius_Abstract {
25
+
26
+ #----------------------------------------------------------------------------------
27
+ #region Identity
28
+ #----------------------------------------------------------------------------------
29
+
30
+ /**
31
+ * Check if user has connected his account (opted-in).
32
+ *
33
+ * Note:
34
+ * If the user opted-in and opted-out on a later stage,
35
+ * this will still return true. If you want to check if the
36
+ * user is currently opted-in, use:
37
+ * `$fs->is_registered() && $fs->is_tracking_allowed()`
38
+ *
39
+ * @since 1.0.1
40
+ * @return bool
41
+ */
42
+ abstract function is_registered();
43
+
44
+ /**
45
+ * Check if the user skipped connecting the account with Freemius.
46
+ *
47
+ * @since 1.0.7
48
+ *
49
+ * @return bool
50
+ */
51
+ abstract function is_anonymous();
52
+
53
+ /**
54
+ * Check if the user currently in activation mode.
55
+ *
56
+ * @since 1.0.7
57
+ *
58
+ * @return bool
59
+ */
60
+ abstract function is_activation_mode();
61
+
62
+ #endregion
63
+
64
+ #----------------------------------------------------------------------------------
65
+ #region Usage Tracking
66
+ #----------------------------------------------------------------------------------
67
+
68
+ /**
69
+ * Returns TRUE if the user opted-in and didn't disconnect (opt-out).
70
+ *
71
+ * @author Leo Fajardo (@leorw)
72
+ * @since 1.2.1.5
73
+ *
74
+ * @return bool
75
+ */
76
+ abstract function is_tracking_allowed();
77
+
78
+ /**
79
+ * Returns TRUE if the user never opted-in or manually opted-out.
80
+ *
81
+ * @author Vova Feldman (@svovaf)
82
+ * @since 1.2.1.5
83
+ *
84
+ * @return bool
85
+ */
86
+ function is_tracking_prohibited() {
87
+ return ! $this->is_registered() || ! $this->is_tracking_allowed();
88
+ }
89
+
90
+ /**
91
+ * Opt-out from usage tracking.
92
+ *
93
+ * Note: This will not delete the account information but will stop all tracking.
94
+ *
95
+ * Returns:
96
+ * 1. FALSE - If the user never opted-in.
97
+ * 2. TRUE - If successfully opted-out.
98
+ * 3. object - API Result on failure.
99
+ *
100
+ * @author Leo Fajardo (@leorw)
101
+ * @since 1.2.1.5
102
+ *
103
+ * @return bool|object
104
+ */
105
+ abstract function stop_tracking();
106
+
107
+ /**
108
+ * Opt-in back into usage tracking.
109
+ *
110
+ * Note: This will only work if the user opted-in previously.
111
+ *
112
+ * Returns:
113
+ * 1. FALSE - If the user never opted-in.
114
+ * 2. TRUE - If successfully opted-in back to usage tracking.
115
+ * 3. object - API result on failure.
116
+ *
117
+ * @author Leo Fajardo (@leorw)
118
+ * @since 1.2.1.5
119
+ *
120
+ * @return bool|object
121
+ */
122
+ abstract function allow_tracking();
123
+
124
+ #endregion
125
+
126
+ #----------------------------------------------------------------------------------
127
+ #region Module Type
128
+ #----------------------------------------------------------------------------------
129
+
130
+ /**
131
+ * Checks if the plugin's type is "plugin". The other type is "theme".
132
+ *
133
+ * @author Leo Fajardo (@leorw)
134
+ * @since 1.2.2
135
+ *
136
+ * @return bool
137
+ */
138
+ abstract function is_plugin();
139
+
140
+ /**
141
+ * Checks if the module type is "theme". The other type is "plugin".
142
+ *
143
+ * @author Leo Fajardo (@leorw)
144
+ * @since 1.2.2
145
+ *
146
+ * @return bool
147
+ */
148
+ function is_theme() {
149
+ return ( ! $this->is_plugin() );
150
+ }
151
+
152
+ #endregion
153
+
154
+ #----------------------------------------------------------------------------------
155
+ #region Permissions
156
+ #----------------------------------------------------------------------------------
157
+
158
+ /**
159
+ * Check if plugin must be WordPress.org compliant.
160
+ *
161
+ * @since 1.0.7
162
+ *
163
+ * @return bool
164
+ */
165
+ abstract function is_org_repo_compliant();
166
+
167
+ /**
168
+ * Check if plugin is allowed to install executable files.
169
+ *
170
+ * @author Vova Feldman (@svovaf)
171
+ * @since 1.0.5
172
+ *
173
+ * @return bool
174
+ */
175
+ function is_allowed_to_install() {
176
+ return ( $this->is_premium() || ! $this->is_org_repo_compliant() );
177
+ }
178
+
179
+ #endregion
180
+
181
+ /**
182
+ * Check if user in trial or in free plan (not paying).
183
+ *
184
+ * @author Vova Feldman (@svovaf)
185
+ * @since 1.0.4
186
+ *
187
+ * @return bool
188
+ */
189
+ function is_not_paying() {
190
+ return ( $this->is_trial() || $this->is_free_plan() );
191
+ }
192
+
193
+ /**
194
+ * Check if the user has an activated and valid paid license on current plugin's install.
195
+ *
196
+ * @since 1.0.9
197
+ *
198
+ * @return bool
199
+ */
200
+ abstract function is_paying();
201
+
202
+ /**
203
+ * Check if the user is paying or in trial.
204
+ *
205
+ * @since 1.0.9
206
+ *
207
+ * @return bool
208
+ */
209
+ function is_paying_or_trial() {
210
+ return ( $this->is_paying() || $this->is_trial() );
211
+ }
212
+
213
+ /**
214
+ * Check if user in a trial or have feature enabled license.
215
+ *
216
+ * @author Vova Feldman (@svovaf)
217
+ * @since 1.1.7
218
+ *
219
+ * @return bool
220
+ */
221
+ abstract function can_use_premium_code();
222
+
223
+ #----------------------------------------------------------------------------------
224
+ #region Premium Only
225
+ #----------------------------------------------------------------------------------
226
+
227
+ /**
228
+ * All logic wrapped in methods with "__premium_only()" suffix will be only
229
+ * included in the premium code.
230
+ *
231
+ * Example:
232
+ * if ( freemius()->is__premium_only() ) {
233
+ * ...
234
+ * }
235
+ */
236
+
237
+ /**
238
+ * Returns true when running premium plugin code.
239
+ *
240
+ * @since 1.0.9
241
+ *
242
+ * @return bool
243
+ */
244
+ function is__premium_only() {
245
+ return $this->is_premium();
246
+ }
247
+
248
+ /**
249
+ * Check if the user has an activated and valid paid license on current plugin's install.
250
+ *
251
+ * @since 1.0.9
252
+ *
253
+ * @return bool
254
+ *
255
+ */
256
+ function is_paying__premium_only() {
257
+ return ( $this->is__premium_only() && $this->is_paying() );
258
+ }
259
+
260
+ /**
261
+ * All code wrapped in this statement will be only included in the premium code.
262
+ *
263
+ * @since 1.0.9
264
+ *
265
+ * @param string $plan Plan name.
266
+ * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
267
+ *
268
+ * @return bool
269
+ */
270
+ function is_plan__premium_only( $plan, $exact = false ) {
271
+ return ( $this->is_premium() && $this->is_plan( $plan, $exact ) );
272
+ }
273
+
274
+ /**
275
+ * Check if plan matches active license' plan or active trial license' plan.
276
+ *
277
+ * All code wrapped in this statement will be only included in the premium code.
278
+ *
279
+ * @since 1.0.9
280
+ *
281
+ * @param string $plan Plan name.
282
+ * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
283
+ *
284
+ * @return bool
285
+ */
286
+ function is_plan_or_trial__premium_only( $plan, $exact = false ) {
287
+ return ( $this->is_premium() && $this->is_plan_or_trial( $plan, $exact ) );
288
+ }
289
+
290
+ /**
291
+ * Check if the user is paying or in trial.
292
+ *
293
+ * All code wrapped in this statement will be only included in the premium code.
294
+ *
295
+ * @since 1.0.9
296
+ *
297
+ * @return bool
298
+ */
299
+ function is_paying_or_trial__premium_only() {
300
+ return $this->is_premium() && $this->is_paying_or_trial();
301
+ }
302
+
303
+ /**
304
+ * Check if the user has an activated and valid paid license on current plugin's install.
305
+ *
306
+ * @since 1.0.4
307
+ *
308
+ * @return bool
309
+ *
310
+ * @deprecated Method name is confusing since it's not clear from the name the code will be removed.
311
+ * @using Alias to is_paying__premium_only()
312
+ */
313
+ function is_paying__fs__() {
314
+ return $this->is_paying__premium_only();
315
+ }
316
+
317
+ /**
318
+ * Check if user in a trial or have feature enabled license.
319
+ *
320
+ * All code wrapped in this statement will be only included in the premium code.
321
+ *
322
+ * @author Vova Feldman (@svovaf)
323
+ * @since 1.1.9
324
+ *
325
+ * @return bool
326
+ */
327
+ function can_use_premium_code__premium_only() {
328
+ return $this->is_premium() && $this->can_use_premium_code();
329
+ }
330
+
331
+ #endregion
332
+
333
+ #----------------------------------------------------------------------------------
334
+ #region Trial
335
+ #----------------------------------------------------------------------------------
336
+
337
+ /**
338
+ * Check if the user in a trial.
339
+ *
340
+ * @since 1.0.3
341
+ *
342
+ * @return bool
343
+ */
344
+ abstract function is_trial();
345
+
346
+ /**
347
+ * Check if trial already utilized.
348
+ *
349
+ * @since 1.0.9
350
+ *
351
+ * @return bool
352
+ */
353
+ abstract function is_trial_utilized();
354
+
355
+ #endregion
356
+
357
+ #----------------------------------------------------------------------------------
358
+ #region Plans
359
+ #----------------------------------------------------------------------------------
360
+
361
+ /**
362
+ * Check if the user is on the free plan of the product.
363
+ *
364
+ * @since 1.0.4
365
+ *
366
+ * @return bool
367
+ */
368
+ abstract function is_free_plan();
369
+
370
+ /**
371
+ * @since 1.0.2
372
+ *
373
+ * @param string $plan Plan name.
374
+ * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
375
+ *
376
+ * @return bool
377
+ */
378
+ abstract function is_plan( $plan, $exact = false );
379
+
380
+ /**
381
+ * Check if plan based on trial. If not in trial mode, should return false.
382
+ *
383
+ * @since 1.0.9
384
+ *
385
+ * @param string $plan Plan name.
386
+ * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
387
+ *
388
+ * @return bool
389
+ */
390
+ abstract function is_trial_plan( $plan, $exact = false );
391
+
392
+ /**
393
+ * Check if plan matches active license' plan or active trial license' plan.
394
+ *
395
+ * @since 1.0.9
396
+ *
397
+ * @param string $plan Plan name.
398
+ * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
399
+ *
400
+ * @return bool
401
+ */
402
+ function is_plan_or_trial( $plan, $exact = false ) {
403
+ return $this->is_plan( $plan, $exact ) ||
404
+ $this->is_trial_plan( $plan, $exact );
405
+ }
406
+
407
+ /**
408
+ * Check if plugin has any paid plans.
409
+ *
410
+ * @author Vova Feldman (@svovaf)
411
+ * @since 1.0.7
412
+ *
413
+ * @return bool
414
+ */
415
+ abstract function has_paid_plan();
416
+
417
+ /**
418
+ * Check if plugin has any free plan, or is it premium only.
419
+ *
420
+ * Note: If no plans configured, assume plugin is free.
421
+ *
422
+ * @author Vova Feldman (@svovaf)
423
+ * @since 1.0.7
424
+ *
425
+ * @return bool
426
+ */
427
+ abstract function has_free_plan();
428
+
429
+ /**
430
+ * Check if plugin is premium only (no free plans).
431
+ *
432
+ * NOTE: is__premium_only() is very different method, don't get confused.
433
+ *
434
+ * @author Vova Feldman (@svovaf)
435
+ * @since 1.1.9
436
+ *
437
+ * @return bool
438
+ */
439
+ abstract function is_only_premium();
440
+
441
+ /**
442
+ * Check if module has a premium code version.
443
+ *
444
+ * Serviceware module might be freemium without any
445
+ * premium code version, where the paid features
446
+ * are all part of the service.
447
+ *
448
+ * @author Vova Feldman (@svovaf)
449
+ * @since 1.2.1.6
450
+ *
451
+ * @return bool
452
+ */
453
+ abstract function has_premium_version();
454
+
455
+ /**
456
+ * Check if module has any release on Freemius,
457
+ * or all plugin's code is on WordPress.org (Serviceware).
458
+ *
459
+ * @return bool
460
+ */
461
+ function has_release_on_freemius() {
462
+ return ! $this->is_org_repo_compliant() ||
463
+ $this->has_premium_version();
464
+ }
465
+
466
+ /**
467
+ * Checks if it's a freemium plugin.
468
+ *
469
+ * @author Vova Feldman (@svovaf)
470
+ * @since 1.1.9
471
+ *
472
+ * @return bool
473
+ */
474
+ function is_freemium() {
475
+ return $this->has_paid_plan() &&
476
+ $this->has_free_plan();
477
+ }
478
+
479
+ /**
480
+ * Check if module has only one plan.
481
+ *
482
+ * @author Vova Feldman (@svovaf)
483
+ * @since 1.2.1.7
484
+ *
485
+ * @return bool
486
+ */
487
+ abstract function is_single_plan();
488
+
489
+ #endregion
490
+
491
+ /**
492
+ * Check if running payments in sandbox mode.
493
+ *
494
+ * @since 1.0.4
495
+ *
496
+ * @return bool
497
+ */
498
+ abstract function is_payments_sandbox();
499
+
500
+ /**
501
+ * Check if running test vs. live plugin.
502
+ *
503
+ * @since 1.0.5
504
+ *
505
+ * @return bool
506
+ */
507
+ abstract function is_live();
508
+
509
+ /**
510
+ * Check if running premium plugin code.
511
+ *
512
+ * @since 1.0.5
513
+ *
514
+ * @return bool
515
+ */
516
+ abstract function is_premium();
517
+
518
+ /**
519
+ * Get upgrade URL.
520
+ *
521
+ * @author Vova Feldman (@svovaf)
522
+ * @since 1.0.2
523
+ *
524
+ * @param string $period Billing cycle.
525
+ *
526
+ * @return string
527
+ */
528
+ abstract function get_upgrade_url( $period = WP_FS__PERIOD_ANNUALLY );
529
+
530
+ /**
531
+ * Check if Freemius was first added in a plugin update.
532
+ *
533
+ * @author Vova Feldman (@svovaf)
534
+ * @since 1.1.5
535
+ *
536
+ * @return bool
537
+ */
538
+ function is_plugin_update() {
539
+ return ! $this->is_plugin_new_install();
540
+ }
541
+
542
+ /**
543
+ * Check if Freemius was part of the plugin when the user installed it first.
544
+ *
545
+ * @author Vova Feldman (@svovaf)
546
+ * @since 1.1.5
547
+ *
548
+ * @return bool
549
+ */
550
+ abstract function is_plugin_new_install();
551
+
552
+ #----------------------------------------------------------------------------------
553
+ #region Marketing
554
+ #----------------------------------------------------------------------------------
555
+
556
+ /**
557
+ * Check if current user purchased any other plugins before.
558
+ *
559
+ * @author Vova Feldman (@svovaf)
560
+ * @since 1.0.9
561
+ *
562
+ * @return bool
563
+ */
564
+ abstract function has_purchased_before();
565
+
566
+ /**
567
+ * Check if current user classified as an agency.
568
+ *
569
+ * @author Vova Feldman (@svovaf)
570
+ * @since 1.0.9
571
+ *
572
+ * @return bool
573
+ */
574
+ abstract function is_agency();
575
+
576
+ /**
577
+ * Check if current user classified as a developer.
578
+ *
579
+ * @author Vova Feldman (@svovaf)
580
+ * @since 1.0.9
581
+ *
582
+ * @return bool
583
+ */
584
+ abstract function is_developer();
585
+
586
+ /**
587
+ * Check if current user classified as a business.
588
+ *
589
+ * @author Vova Feldman (@svovaf)
590
+ * @since 1.0.9
591
+ *
592
+ * @return bool
593
+ */
594
+ abstract function is_business();
595
+
596
+ #endregion
597
+ }
freemius/includes/class-freemius.php ADDED
@@ -0,0 +1,21482 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @package Freemius
4
+ * @copyright Copyright (c) 2015, Freemius, Inc.
5
+ * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
6
+ * @since 1.0.3
7
+ */
8
+ if ( ! defined( 'ABSPATH' ) ) {
9
+ exit;
10
+ }
11
+
12
+ // "final class"
13
+ class Freemius extends Freemius_Abstract {
14
+ /**
15
+ * SDK Version
16
+ *
17
+ * @var string
18
+ */
19
+ public $version = WP_FS__SDK_VERSION;
20
+
21
+ #region Plugin Info
22
+
23
+ /**
24
+ * @since 1.0.1
25
+ *
26
+ * @var string
27
+ */
28
+ private $_slug;
29
+
30
+ /**
31
+ * @since 1.0.0
32
+ *
33
+ * @var string
34
+ */
35
+ private $_plugin_basename;
36
+ /**
37
+ * @since 2.2.1
38
+ *
39
+ * @var string
40
+ */
41
+ private $_premium_plugin_basename;
42
+ /**
43
+ * @since 1.0.0
44
+ *
45
+ * @var string
46
+ */
47
+ private $_free_plugin_basename;
48
+ /**
49
+ * @since 1.0.0
50
+ *
51
+ * @var string
52
+ */
53
+ private $_plugin_dir_path;
54
+ /**
55
+ * @since 1.0.0
56
+ *
57
+ * @var string
58
+ */
59
+ private $_plugin_dir_name;
60
+ /**
61
+ * @since 1.0.0
62
+ *
63
+ * @var string
64
+ */
65
+ private $_plugin_main_file_path;
66
+ /**
67
+ * @var string[]
68
+ */
69
+ private $_plugin_data;
70
+ /**
71
+ * @since 1.0.9
72
+ *
73
+ * @var string
74
+ */
75
+ private $_plugin_name;
76
+ /**
77
+ * @since 1.2.2
78
+ *
79
+ * @var string
80
+ */
81
+ private $_module_type;
82
+
83
+ #endregion Plugin Info
84
+
85
+ /**
86
+ * @since 1.0.9
87
+ *
88
+ * @var bool If false, don't turn Freemius on.
89
+ */
90
+ private $_is_on;
91
+
92
+ /**
93
+ * @since 1.1.3
94
+ *
95
+ * @var bool If false, don't turn Freemius on.
96
+ */
97
+ private $_is_anonymous;
98
+
99
+ /**
100
+ * @since 1.0.9
101
+ * @var bool If false, issues with connectivity to Freemius API.
102
+ */
103
+ private $_has_api_connection;
104
+
105
+ /**
106
+ * @since 1.0.9
107
+ * @since 2.0.0 Default to true since we need the property during the instance construction, prior to the dynamic_init() execution.
108
+ * @var bool Hints the SDK if plugin can support anonymous mode (if skip connect is visible).
109
+ */
110
+ private $_enable_anonymous = true;
111
+
112
+ /**
113
+ * @since 1.1.7.5
114
+ * @var bool Hints the SDK if plugin should run in anonymous mode (only adds feedback form).
115
+ */
116
+ private $_anonymous_mode;
117
+
118
+ /**
119
+ * @since 1.1.9
120
+ * @var bool Hints the SDK if plugin have any free plans.
121
+ */
122
+ private $_is_premium_only;
123
+
124
+ /**
125
+ * @since 1.2.1.6
126
+ * @var bool Hints the SDK if plugin have premium code version at all.
127
+ */
128
+ private $_has_premium_version;
129
+
130
+ /**
131
+ * @since 1.2.1.6
132
+ * @var bool Hints the SDK if plugin should ignore pending mode by simulating a skip.
133
+ */
134
+ private $_ignore_pending_mode;
135
+
136
+ /**
137
+ * @since 1.0.8
138
+ * @var bool Hints the SDK if the plugin has any paid plans.
139
+ */
140
+ private $_has_paid_plans;
141
+
142
+ /**
143
+ * @since 1.2.1.5
144
+ * @var int Hints the SDK if the plugin offers a trial period. If negative, no trial, if zero - has a trial but
145
+ * without a specified period, if positive - the number of trial days.
146
+ */
147
+ private $_trial_days = - 1;
148
+
149
+ /**
150
+ * @since 1.2.1.5
151
+ * @var bool Hints the SDK if the trial requires a payment method or not.
152
+ */
153
+ private $_is_trial_require_payment = false;
154
+
155
+ /**
156
+ * @since 1.0.7
157
+ * @var bool Hints the SDK if the plugin is WordPress.org compliant.
158
+ */
159
+ private $_is_org_compliant;
160
+
161
+ /**
162
+ * @since 1.0.7
163
+ * @var bool Hints the SDK if the plugin is has add-ons.
164
+ */
165
+ private $_has_addons;
166
+
167
+ /**
168
+ * @since 1.1.6
169
+ * @var string[]bool.
170
+ */
171
+ private $_permissions;
172
+
173
+ /**
174
+ * @var FS_Storage
175
+ */
176
+ private $_storage;
177
+
178
+ /**
179
+ * @since 1.2.2.7
180
+ * @var FS_Cache_Manager
181
+ */
182
+ private $_cache;
183
+
184
+ /**
185
+ * @since 1.0.0
186
+ *
187
+ * @var FS_Logger
188
+ */
189
+ private $_logger;
190
+ /**
191
+ * @since 1.0.4
192
+ *
193
+ * @var FS_Plugin
194
+ */
195
+ private $_plugin = false;
196
+ /**
197
+ * @since 1.0.4
198
+ *
199
+ * @var FS_Plugin|false
200
+ */
201
+ private $_parent_plugin = false;
202
+ /**
203
+ * @since 1.1.1
204
+ *
205
+ * @var Freemius
206
+ */
207
+ private $_parent = false;
208
+ /**
209
+ * @since 1.0.1
210
+ *
211
+ * @var FS_User
212
+ */
213
+ private $_user = false;
214
+ /**
215
+ * @since 1.0.1
216
+ *
217
+ * @var FS_Site
218
+ */
219
+ private $_site = false;
220
+ /**
221
+ * @since 1.0.1
222
+ *
223
+ * @var FS_Plugin_License
224
+ */
225
+ private $_license;
226
+ /**
227
+ * @since 1.0.2
228
+ *
229
+ * @var FS_Plugin_Plan[]
230
+ */
231
+ private $_plans = false;
232
+ /**
233
+ * @var FS_Plugin_License[]
234
+ * @since 1.0.5
235
+ */
236
+ private $_licenses = false;
237
+
238
+ /**
239
+ * @since 1.0.1
240
+ *
241
+ * @var FS_Admin_Menu_Manager
242
+ */
243
+ private $_menu;
244
+
245
+ /**
246
+ * @var FS_Admin_Notices
247
+ */
248
+ private $_admin_notices;
249
+
250
+ /**
251
+ * @since 1.1.6
252
+ *
253
+ * @var FS_Admin_Notices
254
+ */
255
+ private static $_global_admin_notices;
256
+
257
+ /**
258
+ * @var FS_Logger
259
+ * @since 1.0.0
260
+ */
261
+ private static $_static_logger;
262
+
263
+ /**
264
+ * @var FS_Options
265
+ * @since 1.0.2
266
+ */
267
+ private static $_accounts;
268
+
269
+ /**
270
+ * @since 1.2.2
271
+ *
272
+ * @var number
273
+ */
274
+ private $_module_id;
275
+
276
+ /**
277
+ * @var Freemius[]
278
+ */
279
+ private static $_instances = array();
280
+
281
+ /**
282
+ * @since 1.2.3
283
+ *
284
+ * @var FS_Affiliate
285
+ */
286
+ private $affiliate = null;
287
+
288
+ /**
289
+ * @since 1.2.3
290
+ *
291
+ * @var FS_AffiliateTerms
292
+ */
293
+ private $plugin_affiliate_terms = null;
294
+
295
+ /**
296
+ * @since 1.2.3
297
+ *
298
+ * @var FS_AffiliateTerms
299
+ */
300
+ private $custom_affiliate_terms = null;
301
+
302
+ /**
303
+ * @since 2.0.0
304
+ *
305
+ * @var bool
306
+ */
307
+ private $_is_multisite_integrated;
308
+
309
+ /**
310
+ * @since 2.0.0
311
+ *
312
+ * @var bool True if the current request is for a network admin screen and the plugin is network active.
313
+ */
314
+ private $_is_network_active;
315
+
316
+ /**
317
+ * @since 2.0.0
318
+ *
319
+ * @var int|null The original blog ID the plugin was loaded with.
320
+ */
321
+ private $_blog_id = null;
322
+
323
+ /**
324
+ * @since 2.0.0
325
+ *
326
+ * @var int|null The current execution context. When true, run on network context. When int, run on the specified blog context.
327
+ */
328
+ private $_context_is_network_or_blog_id = null;
329
+
330
+ /**
331
+ * @since 2.0.0
332
+ *
333
+ * @var string
334
+ */
335
+ private $_dynamically_added_top_level_page_hook_name = '';
336
+
337
+ #region Uninstall Reasons IDs
338
+
339
+ const REASON_NO_LONGER_NEEDED = 1;
340
+ const REASON_FOUND_A_BETTER_PLUGIN = 2;
341
+ const REASON_NEEDED_FOR_A_SHORT_PERIOD = 3;
342
+ const REASON_BROKE_MY_SITE = 4;
343
+ const REASON_SUDDENLY_STOPPED_WORKING = 5;
344
+ const REASON_CANT_PAY_ANYMORE = 6;
345
+ const REASON_OTHER = 7;
346
+ const REASON_DIDNT_WORK = 8;
347
+ const REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION = 9;
348
+ const REASON_COULDNT_MAKE_IT_WORK = 10;
349
+ const REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE = 11;
350
+ const REASON_NOT_WORKING = 12;
351
+ const REASON_NOT_WHAT_I_WAS_LOOKING_FOR = 13;
352
+ const REASON_DIDNT_WORK_AS_EXPECTED = 14;
353
+ const REASON_TEMPORARY_DEACTIVATION = 15;
354
+
355
+ #endregion
356
+
357
+ /* Ctor
358
+ ------------------------------------------------------------------------------------------------------------------*/
359
+
360
+ /**
361
+ * Main singleton instance.
362
+ *
363
+ * @author Vova Feldman (@svovaf)
364
+ * @since 1.0.0
365
+ *
366
+ * @param number $module_id
367
+ * @param string|bool $slug
368
+ * @param bool $is_init Since 1.2.1 Is initiation sequence.
369
+ */
370
+ private function __construct( $module_id, $slug = false, $is_init = false ) {
371
+ if ( $is_init && is_numeric( $module_id ) && is_string( $slug ) ) {
372
+ $this->store_id_slug_type_path_map( $module_id, $slug );
373
+ }
374
+
375
+ $this->_module_id = $module_id;
376
+ $this->_slug = $this->get_slug();
377
+ $this->_module_type = $this->get_module_type();
378
+
379
+ $this->_blog_id = is_multisite() ? get_current_blog_id() : null;
380
+
381
+ $this->_storage = FS_Storage::instance( $this->_module_type, $this->_slug );
382
+
383
+ $this->_cache = FS_Cache_Manager::get_manager( WP_FS___OPTION_PREFIX . "cache_{$module_id}" );
384
+
385
+ $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $this->get_unique_affix(), WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
386
+
387
+ $this->_plugin_main_file_path = $this->_find_caller_plugin_file( $is_init );
388
+ $this->_plugin_dir_path = plugin_dir_path( $this->_plugin_main_file_path );
389
+ $this->_plugin_basename = $this->get_plugin_basename();
390
+ $this->_free_plugin_basename = str_replace( '-premium/', '/', $this->_plugin_basename );
391
+
392
+ $this->_is_multisite_integrated = (
393
+ defined( "WP_FS__PRODUCT_{$module_id}_MULTISITE" ) &&
394
+ ( true === constant( "WP_FS__PRODUCT_{$module_id}_MULTISITE" ) )
395
+ );
396
+
397
+ $this->_is_network_active = (
398
+ is_multisite() &&
399
+ $this->_is_multisite_integrated &&
400
+ // Themes are always network activated, but the ACTUAL activation is per site.
401
+ $this->is_plugin() &&
402
+ ( is_plugin_active_for_network( $this->_plugin_basename ) ||
403
+ // Plugin network level activation or uninstall.
404
+ is_plugin_inactive( $this->_plugin_basename ) )
405
+ );
406
+
407
+ $this->_storage->set_network_active(
408
+ $this->_is_network_active,
409
+ $this->is_delegated_connection()
410
+ );
411
+
412
+ #region Migration
413
+
414
+ if ( is_multisite() ) {
415
+ /**
416
+ * If the install_timestamp exists on the site level but doesn't exist on the
417
+ * network level storage, it means that we need to process the storage with migration.
418
+ *
419
+ * 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.
420
+ *
421
+ * @author Vova Feldman (@svovaf)
422
+ * @since 2.0.0
423
+ */
424
+ if ( false === $this->_storage->get( 'install_timestamp', false, true ) &&
425
+ false !== $this->_storage->get( 'install_timestamp', false, false )
426
+ ) {
427
+ // Initiate storage migration.
428
+ $this->_storage->migrate_to_network();
429
+
430
+ // Migrate module cache to network level storage.
431
+ $this->_cache->migrate_to_network();
432
+ }
433
+ }
434
+
435
+ #endregion
436
+
437
+ $base_name_split = explode( '/', $this->_plugin_basename );
438
+ $this->_plugin_dir_name = $base_name_split[0];
439
+
440
+ if ( $this->_logger->is_on() ) {
441
+ $this->_logger->info( 'plugin_main_file_path = ' . $this->_plugin_main_file_path );
442
+ $this->_logger->info( 'plugin_dir_path = ' . $this->_plugin_dir_path );
443
+ $this->_logger->info( 'plugin_basename = ' . $this->_plugin_basename );
444
+ $this->_logger->info( 'free_plugin_basename = ' . $this->_free_plugin_basename );
445
+ $this->_logger->info( 'plugin_dir_name = ' . $this->_plugin_dir_name );
446
+ }
447
+
448
+ // Remember link between file to slug.
449
+ $this->store_file_slug_map();
450
+
451
+ // Store plugin's initial install timestamp.
452
+ if ( ! isset( $this->_storage->install_timestamp ) ) {
453
+ $this->_storage->install_timestamp = WP_FS__SCRIPT_START_TIME;
454
+ }
455
+
456
+ if ( ! is_object( $this->_plugin ) ) {
457
+ $this->_plugin = FS_Plugin_Manager::instance( $this->_module_id )->get();
458
+ }
459
+
460
+ $this->_admin_notices = FS_Admin_Notices::instance(
461
+ $this->_slug . ( $this->is_theme() ? ':theme' : '' ),
462
+ /**
463
+ * Ensure that the admin notice will always have a title by using the stored plugin title if available and
464
+ * retrieving the title via the "get_plugin_name" method if there is no stored plugin title available.
465
+ *
466
+ * @author Leo Fajardo (@leorw)
467
+ * @since 1.2.2
468
+ */
469
+ ( is_object( $this->_plugin ) ? $this->_plugin->title : $this->get_plugin_name() ),
470
+ $this->get_unique_affix()
471
+ );
472
+
473
+ if ( 'true' === fs_request_get( 'fs_clear_api_cache' ) ||
474
+ 'true' === fs_request_is_action( 'restart_freemius' )
475
+ ) {
476
+ FS_Api::clear_cache();
477
+ $this->_cache->clear();
478
+ }
479
+
480
+ $this->_register_hooks();
481
+
482
+ /**
483
+ * Starting from version 2.0.0, `FS_Site` entities no longer have the `plan` property and have `plan_id`
484
+ * instead. This should be called before calling `_load_account()`, otherwise, `$this->_site` will not be
485
+ * loaded in `_load_account` for versions of SDK starting from 2.0.0.
486
+ *
487
+ * @author Leo Fajardo (@leorw)
488
+ */
489
+ self::migrate_install_plan_to_plan_id( $this->_storage );
490
+
491
+ $this->_load_account();
492
+
493
+ $this->_version_updates_handler();
494
+ }
495
+
496
+ /**
497
+ * Checks whether this module has a settings menu.
498
+ *
499
+ * @author Leo Fajardo (@leorw)
500
+ * @since 1.2.2
501
+ *
502
+ * @return bool
503
+ */
504
+ function has_settings_menu() {
505
+ return ( $this->_is_network_active && fs_is_network_admin() ) ?
506
+ $this->_menu->has_network_menu() :
507
+ $this->_menu->has_menu();
508
+ }
509
+
510
+ /**
511
+ * Check if the context module is free wp.org theme.
512
+ *
513
+ * This method is helpful because:
514
+ * 1. wp.org themes are limited to a single submenu item,
515
+ * and sub-submenu items are most likely not allowed (never verified).
516
+ * 2. wp.org themes are not allowed to redirect the user
517
+ * after the theme activation, therefore, the agreed UX
518
+ * is showing the opt-in as a modal dialog box after
519
+ * activation (approved by @otto42, @emiluzelac, @greenshady, @grapplerulrich).
520
+ *
521
+ * @author Vova Feldman (@svovaf)
522
+ * @since 1.2.2.7
523
+ *
524
+ * @return bool
525
+ */
526
+ function is_free_wp_org_theme() {
527
+ return (
528
+ $this->is_theme() &&
529
+ $this->is_org_repo_compliant() &&
530
+ ! $this->is_premium()
531
+ );
532
+ }
533
+
534
+ /**
535
+ * Checks whether this a submenu item is visible.
536
+ *
537
+ * @author Vova Feldman (@svovaf)
538
+ * @since 1.2.2.6
539
+ * @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.
540
+ *
541
+ * @param string $slug
542
+ * @param bool $ignore_free_wp_org_theme_context This is used to decide if the associated tab should be shown
543
+ * or hidden.
544
+ *
545
+ * @return bool
546
+ */
547
+ function is_submenu_item_visible( $slug, $ignore_free_wp_org_theme_context = false ) {
548
+ if ( $this->is_admin_page( $slug ) ) {
549
+ /**
550
+ * It is the current context page, so show the submenu item
551
+ * so the user will have the right context page, even if it
552
+ * was set to hidden.
553
+ */
554
+ return true;
555
+ }
556
+
557
+ if ( ! $this->has_settings_menu() ) {
558
+ // No menu settings at all.
559
+ return false;
560
+ }
561
+
562
+ if ( ! $ignore_free_wp_org_theme_context && $this->is_free_wp_org_theme() ) {
563
+ /**
564
+ * wp.org themes are limited to a single submenu item, and
565
+ * sub-submenu items are most likely not allowed (never verified).
566
+ */
567
+ return false;
568
+ }
569
+
570
+ return $this->_menu->is_submenu_item_visible( $slug );
571
+ }
572
+
573
+ /**
574
+ * Check if a Freemius page should be accessible via the UI.
575
+ *
576
+ * @author Vova Feldman (@svovaf)
577
+ * @since 1.2.2.7
578
+ *
579
+ * @param string $slug
580
+ *
581
+ * @return bool
582
+ */
583
+ function is_page_visible( $slug ) {
584
+ if ( $this->is_admin_page( $slug ) ) {
585
+ return true;
586
+ }
587
+
588
+ return $this->_menu->is_submenu_item_visible( $slug, true, true );
589
+ }
590
+
591
+ /**
592
+ * @author Vova Feldman (@svovaf)
593
+ * @since 1.0.9
594
+ */
595
+ private function _version_updates_handler() {
596
+ if ( ! isset( $this->_storage->sdk_version ) || $this->_storage->sdk_version != $this->version ) {
597
+ // Freemius version upgrade mode.
598
+ $this->_storage->sdk_last_version = $this->_storage->sdk_version;
599
+ $this->_storage->sdk_version = $this->version;
600
+
601
+ if ( empty( $this->_storage->sdk_last_version ) ||
602
+ version_compare( $this->_storage->sdk_last_version, $this->version, '<' )
603
+ ) {
604
+ $this->_storage->sdk_upgrade_mode = true;
605
+ $this->_storage->sdk_downgrade_mode = false;
606
+ } else {
607
+ $this->_storage->sdk_downgrade_mode = true;
608
+ $this->_storage->sdk_upgrade_mode = false;
609
+
610
+ }
611
+
612
+ $this->do_action( 'sdk_version_update', $this->_storage->sdk_last_version, $this->version );
613
+ }
614
+
615
+ $plugin_version = $this->get_plugin_version();
616
+ if ( ! isset( $this->_storage->plugin_version ) || $this->_storage->plugin_version != $plugin_version ) {
617
+ // Plugin version upgrade mode.
618
+ $this->_storage->plugin_last_version = $this->_storage->plugin_version;
619
+ $this->_storage->plugin_version = $plugin_version;
620
+
621
+ if ( empty( $this->_storage->plugin_last_version ) ||
622
+ version_compare( $this->_storage->plugin_last_version, $plugin_version, '<' )
623
+ ) {
624
+ $this->_storage->plugin_upgrade_mode = true;
625
+ $this->_storage->plugin_downgrade_mode = false;
626
+ } else {
627
+ $this->_storage->plugin_downgrade_mode = true;
628
+ $this->_storage->plugin_upgrade_mode = false;
629
+ }
630
+
631
+ if ( ! empty( $this->_storage->plugin_last_version ) ) {
632
+ // Different version of the plugin was installed before, therefore it's an update.
633
+ $this->_storage->is_plugin_new_install = false;
634
+ }
635
+
636
+ $this->do_action( 'plugin_version_update', $this->_storage->plugin_last_version, $plugin_version );
637
+ }
638
+ }
639
+
640
+ #--------------------------------------------------------------------------------
641
+ #region Data Migration on SDK Update
642
+ #--------------------------------------------------------------------------------
643
+
644
+ /**
645
+ * @author Vova Feldman (@svovaf)
646
+ * @since 1.1.5
647
+ *
648
+ * @param string $sdk_prev_version
649
+ * @param string $sdk_version
650
+ */
651
+ function _sdk_version_update( $sdk_prev_version, $sdk_version ) {
652
+ /**
653
+ * @since 1.1.7.3 Fixed unwanted connectivity test cleanup.
654
+ */
655
+ if ( empty( $sdk_prev_version ) ) {
656
+ return;
657
+ }
658
+
659
+ if ( version_compare( $sdk_prev_version, '2.1.0', '<' ) &&
660
+ version_compare( $sdk_version, '2.1.0', '>=' )
661
+ ) {
662
+ $this->_storage->handle_gdpr_admin_notice = true;
663
+ }
664
+
665
+ if ( version_compare( $sdk_prev_version, '2.0.0', '<' ) &&
666
+ version_compare( $sdk_version, '2.0.0', '>=' )
667
+ ) {
668
+ $this->migrate_to_subscriptions_collection();
669
+
670
+ $this->consolidate_licenses();
671
+
672
+ // Clear trial_plan since it's now loaded from the plans collection when needed.
673
+ $this->_storage->remove( 'trial_plan', true, false );
674
+ }
675
+
676
+ if ( version_compare( $sdk_prev_version, '1.2.3', '<' ) &&
677
+ version_compare( $sdk_version, '1.2.3', '>=' )
678
+ ) {
679
+ /**
680
+ * Starting from version 1.2.3, paths are stored as relative instead of absolute and some of them can be
681
+ * invalid.
682
+ *
683
+ * @author Leo Fajardo (@leorw)
684
+ */
685
+ $this->remove_invalid_paths();
686
+ }
687
+
688
+ if ( version_compare( $sdk_prev_version, '1.1.5', '<' ) &&
689
+ version_compare( $sdk_version, '1.1.5', '>=' )
690
+ ) {
691
+ // On version 1.1.5 merged connectivity and is_on data.
692
+ if ( isset( $this->_storage->connectivity_test ) ) {
693
+ if ( ! isset( $this->_storage->is_on ) ) {
694
+ unset( $this->_storage->connectivity_test );
695
+ } else {
696
+ $connectivity_data = $this->_storage->connectivity_test;
697
+ $connectivity_data['is_active'] = $this->_storage->is_on['is_active'];
698
+ $connectivity_data['timestamp'] = $this->_storage->is_on['timestamp'];
699
+
700
+ // Override.
701
+ $this->_storage->connectivity_test = $connectivity_data;
702
+
703
+ // Remove previous structure.
704
+ unset( $this->_storage->is_on );
705
+ }
706
+
707
+ }
708
+ }
709
+
710
+ if (
711
+ version_compare( $sdk_prev_version, '2.2.1', '<' ) &&
712
+ version_compare( $sdk_version, '2.2.1', '>=' )
713
+ ) {
714
+ /**
715
+ * Clear the file cache without storing the previous path since it could be a wrong path. For example,
716
+ * in the versions of the SDK lower than 2.2.1, it's possible for the path of an add-on to be the same
717
+ * as the parent plugin's when the add-on was auto-installed since the relevant method names were not
718
+ * skipped in the logic that determines the right path in the `get_caller_main_file_and_type` method
719
+ * (e.g. `try_activate_plugin`). Since it was an auto-installation, the caller was the parent plugin
720
+ * and so its path was used. In case the stored path is wrong, clearing the cache will resolve issues
721
+ * related to data mix-up between plugins (e.g. titles and versions of an add-on and its parent plugin).
722
+ *
723
+ * @author Leo Fajardo (@leorw)
724
+ * @since 2.2.1
725
+ */
726
+ $this->clear_module_main_file_cache( false );
727
+ }
728
+ }
729
+
730
+ /**
731
+ * @author Leo Fajardo (@leorw)
732
+ * @since 2.0.0
733
+ *
734
+ * @param \FS_Storage $storage
735
+ * @param bool|int|null $blog_id
736
+ */
737
+ private static function migrate_install_plan_to_plan_id( FS_Storage $storage, $blog_id = null ) {
738
+ if ( empty( $storage->sdk_version ) ) {
739
+ // New installation of the plugin, no need to upgrade.
740
+ return;
741
+ }
742
+
743
+ if ( ! version_compare( $storage->sdk_version, '2.0.0', '<' ) ) {
744
+ // Previous version is >= 2.0.0, so no need to migrate.
745
+ return;
746
+ }
747
+
748
+ // Alias.
749
+ $module_type = $storage->get_module_type();
750
+ $module_slug = $storage->get_module_slug();
751
+
752
+ $installs = self::get_all_sites( $module_type, $blog_id );
753
+ $install = isset( $installs[ $module_slug ] ) ? $installs[ $module_slug ] : null;
754
+
755
+ if ( ! is_object( $install ) ) {
756
+ return;
757
+ }
758
+
759
+ if ( isset( $install->plan ) && is_object( $install->plan ) ) {
760
+ if ( isset( $install->plan->id ) && ! empty( $install->plan->id ) ) {
761
+ $install->plan_id = self::_decrypt( $install->plan->id );
762
+ }
763
+
764
+ unset( $install->plan );
765
+
766
+ $installs[ $module_slug ] = clone $install;
767
+
768
+ self::set_account_option_by_module(
769
+ $module_type,
770
+ 'sites',
771
+ $installs,
772
+ true,
773
+ $blog_id
774
+ );
775
+ }
776
+ }
777
+
778
+ /**
779
+ * @author Leo Fajardo (@leorw)
780
+ * @since 2.0.0
781
+ */
782
+ private function migrate_to_subscriptions_collection() {
783
+ if ( ! is_object( $this->_site ) ) {
784
+ return;
785
+ }
786
+
787
+ if ( isset( $this->_storage->subscription ) && is_object( $this->_storage->subscription ) ) {
788
+ $this->_storage->subscriptions = array( $this->_storage->subscription );
789
+ }
790
+ }
791
+
792
+ /**
793
+ * @author Leo Fajardo (@leorw)
794
+ * @since 2.0.0
795
+ */
796
+ private function consolidate_licenses() {
797
+ $plugin_licenses = self::get_account_option( 'licenses', WP_FS__MODULE_TYPE_PLUGIN );
798
+ if ( isset( $plugin_licenses[ $this->_slug ] ) ) {
799
+ $plugin_licenses = $plugin_licenses[ $this->_slug ];
800
+ } else {
801
+ $plugin_licenses = array();
802
+ }
803
+
804
+ $theme_licenses = self::get_account_option( 'licenses', WP_FS__MODULE_TYPE_THEME );
805
+ if ( isset( $theme_licenses[ $this->_slug ] ) ) {
806
+ $theme_licenses = $theme_licenses[ $this->_slug ];
807
+ } else {
808
+ $theme_licenses = array();
809
+ }
810
+
811
+ if ( empty( $plugin_licenses ) && empty( $theme_licenses ) ) {
812
+ return;
813
+ }
814
+
815
+ $all_licenses = array();
816
+ $user_id_license_ids_map = array();
817
+
818
+ foreach ( $plugin_licenses as $user_id => $user_licenses ) {
819
+ if ( is_array( $user_licenses ) ) {
820
+ if ( ! isset( $user_license_ids[ $user_id ] ) ) {
821
+ $user_id_license_ids_map[ $user_id ] = array();
822
+ }
823
+
824
+ foreach ( $user_licenses as $user_license ) {
825
+ $all_licenses[] = $user_license;
826
+ $user_id_license_ids_map[ $user_id ][] = $user_license->id;
827
+ }
828
+ }
829
+ }
830
+
831
+ foreach ( $theme_licenses as $user_id => $user_licenses ) {
832
+ if ( is_array( $user_licenses ) ) {
833
+ if ( ! isset( $user_license_ids[ $user_id ] ) ) {
834
+ $user_id_license_ids_map[ $user_id ] = array();
835
+ }
836
+
837
+ foreach ( $user_licenses as $user_license ) {
838
+ $all_licenses[] = $user_license;
839
+ $user_id_license_ids_map[ $user_id ][] = $user_license->id;
840
+ }
841
+ }
842
+ }
843
+
844
+ self::store_user_id_license_ids_map(
845
+ $user_id_license_ids_map,
846
+ $this->_module_id
847
+ );
848
+
849
+ $this->_store_licenses( true, $this->_module_id, $all_licenses );
850
+ }
851
+
852
+ /**
853
+ * Remove invalid paths.
854
+ *
855
+ * @author Leo Fajardo (@leorw)
856
+ * @since 1.2.3
857
+ */
858
+ private function remove_invalid_paths() {
859
+ // Remove invalid path that is still associated with the current slug if there's any.
860
+ $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
861
+ foreach ( $file_slug_map as $plugin_basename => $slug ) {
862
+ if ( $slug === $this->_slug &&
863
+ $plugin_basename !== $this->_plugin_basename &&
864
+ ! file_exists( $this->get_absolute_path( $plugin_basename ) )
865
+ ) {
866
+ unset( $file_slug_map[ $plugin_basename ] );
867
+ self::$_accounts->set_option( 'file_slug_map', $file_slug_map, true );
868
+
869
+ break;
870
+ }
871
+ }
872
+ }
873
+
874
+ /**
875
+ * @author Vova Feldman (@svovaf)
876
+ * @since 1.2.2.7
877
+ *
878
+ * @param string $plugin_prev_version
879
+ * @param string $plugin_version
880
+ */
881
+ function _after_version_update( $plugin_prev_version, $plugin_version ) {
882
+ if ( $this->is_theme() ) {
883
+ // Expire the cache of the previous tabs since the theme may
884
+ // have setting updates.
885
+ $this->_cache->expire( 'tabs' );
886
+ $this->_cache->expire( 'tabs_stylesheets' );
887
+ }
888
+ }
889
+
890
+ /**
891
+ * A special migration logic for the $_accounts, executed for all the plugins in the system:
892
+ * - Moves some data to the network level storage.
893
+ * - If the plugin's connection was skipped for all sites, set the plugin as if it was network skipped.
894
+ * - If the plugin's connection was ignored for all sites, don't do anything in terms of the network connection.
895
+ * - 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.
896
+ * - 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.
897
+ * - 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.
898
+ *
899
+ * @author Vova Feldman (@svovaf)
900
+ * @since 2.0.0
901
+ */
902
+ private static function migrate_accounts_to_network() {
903
+ $sites = self::get_sites();
904
+ $sites_count = count( $sites );
905
+ $connection_status = array();
906
+ $plugin_slugs = array();
907
+ foreach ( $sites as $site ) {
908
+ $blog_id = self::get_site_blog_id( $site );
909
+
910
+ self::$_accounts->migrate_to_network( $blog_id );
911
+
912
+ /**
913
+ * Build a list of all Freemius powered plugins slugs.
914
+ */
915
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array(), $blog_id );
916
+ foreach ( $id_slug_type_path_map as $module_id => $data ) {
917
+ if ( WP_FS__MODULE_TYPE_PLUGIN === $data['type'] ) {
918
+ $plugin_slugs[ $data['slug'] ] = true;
919
+ }
920
+ }
921
+
922
+ $installs = self::get_account_option( 'sites', WP_FS__MODULE_TYPE_PLUGIN, $blog_id );
923
+
924
+ if ( is_array( $installs ) ) {
925
+ foreach ( $installs as $slug => $install ) {
926
+ if ( ! isset( $connection_status[ $slug ] ) ) {
927
+ $connection_status[ $slug ] = array();
928
+ }
929
+
930
+ if ( is_object( $install ) &&
931
+ FS_Site::is_valid_id( $install->id ) &&
932
+ FS_User::is_valid_id( $install->user_id )
933
+ ) {
934
+ $connection_status[ $slug ][ $blog_id ] = $install->user_id;
935
+ }
936
+ }
937
+ }
938
+ }
939
+
940
+ foreach ( $plugin_slugs as $slug => $true ) {
941
+ if ( ! isset( $connection_status[ $slug ] ) ) {
942
+ $connection_status[ $slug ] = array();
943
+ }
944
+
945
+ foreach ( $sites as $site ) {
946
+ $blog_id = self::get_site_blog_id( $site );
947
+
948
+ if ( isset( $connection_status[ $slug ][ $blog_id ] ) ) {
949
+ continue;
950
+ }
951
+
952
+ $storage = FS_Storage::instance( WP_FS__MODULE_TYPE_PLUGIN, $slug );
953
+
954
+ $is_anonymous = $storage->get( 'is_anonymous', null, $blog_id );
955
+
956
+ if ( ! is_null( $is_anonymous ) ) {
957
+ // Since 1.1.3 is_anonymous is an array.
958
+ if ( is_array( $is_anonymous ) && isset( $is_anonymous['is'] ) ) {
959
+ $is_anonymous = $is_anonymous['is'];
960
+ }
961
+
962
+ if ( is_bool( $is_anonymous ) && true === $is_anonymous ) {
963
+ $connection_status[ $slug ][ $blog_id ] = 'skipped';
964
+ }
965
+ }
966
+
967
+ if ( ! isset( $connection_status[ $slug ][ $blog_id ] ) ) {
968
+ $connection_status[ $slug ][ $blog_id ] = 'ignored';
969
+ }
970
+ }
971
+ }
972
+
973
+ $super_admins = array();
974
+
975
+ foreach ( $connection_status as $slug => $blogs_status ) {
976
+ $skips = 0;
977
+ $ignores = 0;
978
+ $connections = 0;
979
+ $opted_in_users = array();
980
+ $opted_in_super_admins = array();
981
+
982
+ $storage = FS_Storage::instance( WP_FS__MODULE_TYPE_PLUGIN, $slug );
983
+
984
+ foreach ( $blogs_status as $blog_id => $status_or_user_id ) {
985
+ if ( 'skipped' === $status_or_user_id ) {
986
+ $skips ++;
987
+ } else if ( 'ignored' === $status_or_user_id ) {
988
+ $ignores ++;
989
+ } else if ( FS_User::is_valid_id( $status_or_user_id ) ) {
990
+ $connections ++;
991
+
992
+ if ( ! isset( $opted_in_users[ $status_or_user_id ] ) ) {
993
+ $opted_in_users[ $status_or_user_id ] = array();
994
+ }
995
+
996
+ $opted_in_users[ $status_or_user_id ][] = $blog_id;
997
+
998
+ if ( isset( $super_admins[ $status_or_user_id ] ) ||
999
+ self::is_super_admin( $status_or_user_id )
1000
+ ) {
1001
+ // Cache super-admin data.
1002
+ $super_admins[ $status_or_user_id ] = true;
1003
+
1004
+ // Remember opted-in super-admins for the plugin.
1005
+ $opted_in_super_admins[ $status_or_user_id ] = true;
1006
+ }
1007
+ }
1008
+ }
1009
+
1010
+ $main_super_admin_user_id = null;
1011
+ $all_migrated = false;
1012
+ if ( $sites_count == $skips ) {
1013
+ // All sites were skipped -> network skip by copying the anonymous mode from any of the sites.
1014
+ $storage->is_anonymous_ms = $storage->is_anonymous;
1015
+
1016
+ $all_migrated = true;
1017
+ } else if ( $sites_count == $ignores ) {
1018
+ // Don't do anything, still in activation mode.
1019
+
1020
+ $all_migrated = true;
1021
+ } else if ( 0 < count( $opted_in_super_admins ) ) {
1022
+ // Find the super-admin with the majority of installs.
1023
+ $max_installs_by_super_admin = 0;
1024
+ foreach ( $opted_in_super_admins as $user_id => $true ) {
1025
+ $installs_count = count( $opted_in_users[ $user_id ] );
1026
+
1027
+ if ( $installs_count > $max_installs_by_super_admin ) {
1028
+ $max_installs_by_super_admin = $installs_count;
1029
+ $main_super_admin_user_id = $user_id;
1030
+ }
1031
+ }
1032
+
1033
+ if ( $sites_count == $connections && 1 == count( $opted_in_super_admins ) ) {
1034
+ // Super-admin opted-in for all sites in the network.
1035
+ $storage->is_network_connected = true;
1036
+
1037
+ $all_migrated = true;
1038
+ }
1039
+
1040
+ // Store network user.
1041
+ $storage->network_user_id = $main_super_admin_user_id;
1042
+
1043
+ $storage->network_install_blog_id = ( $sites_count == $connections ) ?
1044
+ // Since all sites are opted-in, associating with the main site.
1045
+ get_current_blog_id() :
1046
+ // Associating with the 1st found opted-in site.
1047
+ $opted_in_users[ $main_super_admin_user_id ][0];
1048
+
1049
+ /**
1050
+ * Make sure we migrate the plan ID of the network install, otherwise, if after the migration
1051
+ * the 1st page that will be loaded is the network level WP Admin and $storage->network_install_blog_id
1052
+ * is different than the main site of the network, the $this->_site will not be set since the plan_id
1053
+ * will be empty.
1054
+ */
1055
+ $storage->migrate_to_network();
1056
+ self::migrate_install_plan_to_plan_id( $storage, $storage->network_install_blog_id );
1057
+ } else {
1058
+ // At least one opt-in. All the opt-in were created by a non-super-admin.
1059
+ if ( 0 == $ignores ) {
1060
+ // All sites were opted-in or skipped, all by non-super-admin. So delegate all.
1061
+ $storage->store( 'is_delegated_connection', true, true );
1062
+
1063
+ $all_migrated = true;
1064
+ }
1065
+ }
1066
+
1067
+ if ( ! $all_migrated ) {
1068
+ /**
1069
+ * Delegate all sites that were:
1070
+ * 1) Opted-in by a user that is NOT the main-super-admin.
1071
+ * 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.
1072
+ */
1073
+ foreach ( $blogs_status as $blog_id => $status_or_user_id ) {
1074
+ if ( $status_or_user_id == $main_super_admin_user_id ) {
1075
+ continue;
1076
+ }
1077
+
1078
+ if ( FS_User::is_valid_id( $status_or_user_id ) ||
1079
+ ( 'skipped' === $status_or_user_id && is_null( $main_super_admin_user_id ) )
1080
+ ) {
1081
+ $storage->store( 'is_delegated_connection', true, $blog_id );
1082
+ }
1083
+ }
1084
+ }
1085
+
1086
+
1087
+ if ( ( $connections + $skips > 0 ) ) {
1088
+ if ( $ignores > 0 ) {
1089
+ /**
1090
+ * If admin already opted-in or skipped in any of the network sites, and also
1091
+ * have sites which the connection decision was not yet taken, set this plugin
1092
+ * into network activation mode so the super-admin can choose what to do with
1093
+ * the rest of the sites.
1094
+ */
1095
+ self::set_network_upgrade_mode( $storage );
1096
+ }
1097
+ }
1098
+ }
1099
+ }
1100
+
1101
+ /**
1102
+ * Set a module into network upgrade mode.
1103
+ *
1104
+ * @author Vova Feldman (@svovaf)
1105
+ * @since 2.0.0
1106
+ *
1107
+ * @param \FS_Storage $storage
1108
+ *
1109
+ * @return bool
1110
+ */
1111
+ private static function set_network_upgrade_mode( FS_Storage $storage ) {
1112
+ return $storage->is_network_activation = true;
1113
+ }
1114
+
1115
+ /**
1116
+ * Will return true after upgrading to the SDK with the network level integration,
1117
+ * when the super-admin involvement is required regarding the rest of the sites.
1118
+ *
1119
+ * @author Vova Feldman (@svovaf)
1120
+ * @since 2.0.0
1121
+ *
1122
+ * @return bool
1123
+ */
1124
+ function is_network_upgrade_mode() {
1125
+ return $this->_storage->get( 'is_network_activation' );
1126
+ }
1127
+
1128
+ /**
1129
+ * Clear flag after the upgrade mode completion.
1130
+ *
1131
+ * @author Vova Feldman (@svovaf)
1132
+ * @since 2.0.0
1133
+ *
1134
+ * @return bool True if network activation was on and now completed.
1135
+ */
1136
+ private function network_upgrade_mode_completed() {
1137
+ if ( fs_is_network_admin() && $this->is_network_upgrade_mode() ) {
1138
+ $this->_storage->remove( 'is_network_activation' );
1139
+
1140
+ return true;
1141
+ }
1142
+
1143
+ return false;
1144
+ }
1145
+
1146
+ #endregion
1147
+
1148
+ /**
1149
+ * This action is connected to the 'plugins_loaded' hook and helps to determine
1150
+ * if this is a new plugin installation or a plugin update.
1151
+ *
1152
+ * There are 3 different use-cases:
1153
+ * 1) New plugin installation right with Freemius:
1154
+ * 1.1 _activate_plugin_event_hook() will be executed first
1155
+ * 1.2 Since $this->_storage->is_plugin_new_install is not set,
1156
+ * and $this->_storage->plugin_last_version is not set,
1157
+ * $this->_storage->is_plugin_new_install will be set to TRUE.
1158
+ * 1.3 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will
1159
+ * be already set to TRUE.
1160
+ *
1161
+ * 2) Plugin update, didn't have Freemius before, and now have the SDK:
1162
+ * 2.1 _activate_plugin_event_hook() will not be executed, because
1163
+ * the activation hook do NOT fires on updates since WP 3.1.
1164
+ * 2.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will
1165
+ * be empty, therefore, it will be set to FALSE.
1166
+ *
1167
+ * 3) Plugin update, had Freemius in prev version as well:
1168
+ * 3.1 _version_updates_handler() will be executed 1st, since FS was installed
1169
+ * before, $this->_storage->plugin_last_version will NOT be empty,
1170
+ * therefore, $this->_storage->is_plugin_new_install will be set to FALSE.
1171
+ * 3.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install is
1172
+ * already set, therefore, it will not be modified.
1173
+ *
1174
+ * Use-case #3 is backward compatible, #3.1 will be executed since 1.0.9.
1175
+ *
1176
+ * NOTE:
1177
+ * The only fallback of this mechanism is if an admin updates a plugin based on use-case #2,
1178
+ * and then, the next immediate PageView is the plugin's main settings page, it will not
1179
+ * show the opt-in right away. The reason it will happen is because Freemius execution
1180
+ * will be turned off till the plugin is fully loaded at least once
1181
+ * (till $this->_storage->was_plugin_loaded is TRUE).
1182
+ *
1183
+ * @author Vova Feldman (@svovaf)
1184
+ * @since 1.1.9
1185
+ *
1186
+ */
1187
+ function _plugins_loaded() {
1188
+ // Update flag that plugin was loaded with Freemius at least once.
1189
+ $this->_storage->was_plugin_loaded = true;
1190
+
1191
+ /**
1192
+ * Bug fix - only set to false when it's a plugin, due to the
1193
+ * execution sequence of the theme hooks and our methods, if
1194
+ * this will be set for themes, Freemius will always assume
1195
+ * it's a theme update.
1196
+ *
1197
+ * @author Vova Feldman (@svovaf)
1198
+ * @since 1.2.2.2
1199
+ */
1200
+ if ( $this->is_plugin() &&
1201
+ ! isset( $this->_storage->is_plugin_new_install )
1202
+ ) {
1203
+ $this->_storage->is_plugin_new_install = false;
1204
+ }
1205
+ }
1206
+
1207
+ /**
1208
+ * Add special parameter to WP admin AJAX calls so when we
1209
+ * process AJAX calls we can identify its source properly.
1210
+ *
1211
+ * @author Leo Fajardo (@leorw)
1212
+ * @since 2.0.0
1213
+ */
1214
+ static function _enrich_ajax_url() {
1215
+ $admin_param = is_network_admin() ?
1216
+ '_fs_network_admin' :
1217
+ '_fs_blog_admin';
1218
+ ?>
1219
+ <script type="text/javascript">
1220
+ (function ($) {
1221
+ $(document).ajaxSend(function (event, jqxhr, settings) {
1222
+ if (settings.url &&
1223
+ -1 < settings.url.indexOf('admin-ajax.php') &&
1224
+ ! ( settings.url.indexOf( '<?php echo $admin_param ?>' ) > 0 )
1225
+ ) {
1226
+ if (settings.url.indexOf('?') > 0) {
1227
+ settings.url += '&';
1228
+ } else {
1229
+ settings.url += '?';
1230
+ }
1231
+
1232
+ settings.url += '<?php echo $admin_param ?>=true';
1233
+
1234
+ }
1235
+ });
1236
+ })(jQuery);
1237
+ </script>
1238
+ <?php
1239
+ }
1240
+
1241
+ /**
1242
+ * Opens the support forum subemenu item in a new browser page.
1243
+ *
1244
+ * @author Vova Feldman (@svovaf)
1245
+ * @since 2.1.4
1246
+ */
1247
+ static function _open_support_forum_in_new_page() {
1248
+ ?>
1249
+ <script type="text/javascript">
1250
+ (function ($) {
1251
+ $('.fs-submenu-item.wp-support-forum').parent().attr('target', '_blank');
1252
+ })(jQuery);
1253
+ </script>
1254
+ <?php
1255
+ }
1256
+
1257
+ /**
1258
+ * @author Vova Feldman (@svovaf)
1259
+ * @since 1.0.9
1260
+ */
1261
+ private function _register_hooks() {
1262
+ $this->_logger->entrance();
1263
+
1264
+ if ( is_admin() ) {
1265
+ add_action( 'plugins_loaded', array( &$this, '_hook_action_links_and_register_account_hooks' ) );
1266
+
1267
+ if ( $this->is_plugin() ) {
1268
+ $plugin_dir = dirname( $this->_plugin_dir_path ) . '/';
1269
+
1270
+ /**
1271
+ * @since 1.2.2
1272
+ *
1273
+ * Hook to both free and premium version activations to support
1274
+ * auto deactivation on the other version activation.
1275
+ */
1276
+ register_activation_hook(
1277
+ $plugin_dir . $this->_free_plugin_basename,
1278
+ array( &$this, '_activate_plugin_event_hook' )
1279
+ );
1280
+
1281
+ register_activation_hook(
1282
+ $plugin_dir . $this->premium_plugin_basename(),
1283
+ array( &$this, '_activate_plugin_event_hook' )
1284
+ );
1285
+ } else {
1286
+ add_action( 'after_switch_theme', array( &$this, '_activate_theme_event_hook' ), 10, 2 );
1287
+
1288
+ /**
1289
+ * Include the required hooks to capture the theme settings' page tabs
1290
+ * and cache them.
1291
+ *
1292
+ * @author Vova Feldman (@svovaf)
1293
+ * @since 1.2.2.7
1294
+ */
1295
+ if ( ! $this->_cache->has_valid( 'tabs' ) ) {
1296
+ add_action( 'admin_footer', array( &$this, '_tabs_capture' ) );
1297
+ // Add license activation AJAX callback.
1298
+ $this->add_ajax_action( 'store_tabs', array( &$this, '_store_tabs_ajax_action' ) );
1299
+
1300
+ add_action( 'admin_enqueue_scripts', array( &$this, '_store_tabs_styles' ), 9999999 );
1301
+ }
1302
+
1303
+ add_action(
1304
+ 'admin_footer',
1305
+ array( &$this, '_add_freemius_tabs' ),
1306
+ /**
1307
+ * The tabs JS code must be executed after the tabs capture logic (_tabs_capture()).
1308
+ * That's why the priority is 11 while the tabs capture logic is added
1309
+ * with priority 10.
1310
+ *
1311
+ * @author Vova Feldman (@svovaf)
1312
+ */
1313
+ 11
1314
+ );
1315
+
1316
+ add_action( 'admin_footer', array( &$this, '_style_premium_theme' ) );
1317
+ }
1318
+
1319
+ /**
1320
+ * Part of the mechanism to identify new plugin install vs. plugin update.
1321
+ *
1322
+ * @author Vova Feldman (@svovaf)
1323
+ * @since 1.1.9
1324
+ */
1325
+ if ( empty( $this->_storage->was_plugin_loaded ) ) {
1326
+ /**
1327
+ * During the plugin activation (not theme), 'plugins_loaded' will be already executed
1328
+ * when the logic gets here since the activation logic first add the activate plugins,
1329
+ * then triggers 'plugins_loaded', and only then include the code of the plugin that
1330
+ * is activated. Which means that _plugins_loaded() will NOT be executed during the
1331
+ * plugin activation, and that IS intentional.
1332
+ *
1333
+ * @author Vova Feldman (@svovaf)
1334
+ */
1335
+ if ( $this->is_plugin() && $this->is_activation_mode( false ) ) {
1336
+ add_action( 'plugins_loaded', array( &$this, '_plugins_loaded' ) );
1337
+ } else {
1338
+ // If was activated before, then it was already loaded before.
1339
+ $this->_plugins_loaded();
1340
+ }
1341
+ }
1342
+
1343
+ if ( ! self::is_ajax() ) {
1344
+ if ( ! $this->is_addon() ) {
1345
+ add_action( 'init', array( &$this, '_add_default_submenu_items' ), WP_FS__LOWEST_PRIORITY );
1346
+ }
1347
+ }
1348
+
1349
+ if ( $this->_storage->handle_gdpr_admin_notice ) {
1350
+ add_action( 'init', array( &$this, '_maybe_show_gdpr_admin_notice' ) );
1351
+ }
1352
+
1353
+ add_action( 'init', array( &$this, '_maybe_add_gdpr_optin_ajax_handler') );
1354
+ }
1355
+
1356
+ if ( $this->is_plugin() ) {
1357
+ if ( $this->_is_network_active ) {
1358
+ add_action( 'wpmu_new_blog', array( $this, '_after_new_blog_callback' ), 10, 6 );
1359
+ }
1360
+
1361
+ register_deactivation_hook( $this->_plugin_main_file_path, array( &$this, '_deactivate_plugin_hook' ) );
1362
+ }
1363
+
1364
+ if ( is_multisite() ) {
1365
+ add_action( 'deactivate_blog', array( &$this, '_after_site_deactivated_callback' ) );
1366
+ add_action( 'archive_blog', array( &$this, '_after_site_deactivated_callback' ) );
1367
+ add_action( 'make_spam_blog', array( &$this, '_after_site_deactivated_callback' ) );
1368
+ add_action( 'deleted_blog', array( &$this, '_after_site_deleted_callback' ), 10, 2 );
1369
+
1370
+ add_action( 'activate_blog', array( &$this, '_after_site_reactivated_callback' ) );
1371
+ add_action( 'unarchive_blog', array( &$this, '_after_site_reactivated_callback' ) );
1372
+ add_action( 'make_ham_blog', array( &$this, '_after_site_reactivated_callback' ) );
1373
+ }
1374
+
1375
+ if ( $this->is_theme() &&
1376
+ self::is_customizer() &&
1377
+ $this->apply_filters( 'show_customizer_upsell', true )
1378
+ ) {
1379
+ // Register customizer upsell.
1380
+ add_action( 'customize_register', array( &$this, '_customizer_register' ) );
1381
+ }
1382
+
1383
+ add_action( 'admin_init', array( &$this, '_redirect_on_clicked_menu_link' ), WP_FS__LOWEST_PRIORITY );
1384
+
1385
+ if ( $this->is_theme() ) {
1386
+ add_action( 'admin_init', array( &$this, '_add_tracking_links' ) );
1387
+ }
1388
+
1389
+ add_action( 'admin_init', array( &$this, '_add_license_activation' ) );
1390
+ add_action( 'admin_init', array( &$this, '_add_premium_version_upgrade_selection' ) );
1391
+
1392
+ $this->add_ajax_action( 'update_billing', array( &$this, '_update_billing_ajax_action' ) );
1393
+ $this->add_ajax_action( 'start_trial', array( &$this, '_start_trial_ajax_action' ) );
1394
+
1395
+ if ( $this->_is_network_active && fs_is_network_admin() ) {
1396
+ $this->add_ajax_action( 'network_activate', array( &$this, '_network_activate_ajax_action' ) );
1397
+ }
1398
+
1399
+ $this->add_ajax_action( 'install_premium_version', array(
1400
+ &$this,
1401
+ '_install_premium_version_ajax_action'
1402
+ ) );
1403
+
1404
+ $this->add_ajax_action( 'submit_affiliate_application', array( &$this, '_submit_affiliate_application' ) );
1405
+
1406
+ $this->add_action( 'after_plans_sync', array( &$this, '_check_for_trial_plans' ) );
1407
+
1408
+ $this->add_action( 'sdk_version_update', array( &$this, '_sdk_version_update' ), WP_FS__DEFAULT_PRIORITY, 2 );
1409
+
1410
+ $this->add_action(
1411
+ 'plugin_version_update',
1412
+ array( &$this, '_after_version_update' ),
1413
+ WP_FS__DEFAULT_PRIORITY,
1414
+ 2
1415
+ );
1416
+ $this->add_filter( 'after_code_type_change', array( &$this, '_after_code_type_change' ) );
1417
+
1418
+ add_action( 'admin_init', array( &$this, '_add_trial_notice' ) );
1419
+ add_action( 'admin_init', array( &$this, '_add_affiliate_program_notice' ) );
1420
+ add_action( 'admin_enqueue_scripts', array( &$this, '_enqueue_common_css' ) );
1421
+
1422
+ /**
1423
+ * Handle request to reset anonymous mode for `get_reconnect_url()`.
1424
+ *
1425
+ * @author Vova Feldman (@svovaf)
1426
+ * @since 1.2.1.5
1427
+ */
1428
+ if ( fs_request_is_action( 'reset_anonymous_mode' ) &&
1429
+ $this->get_unique_affix() === fs_request_get( 'fs_unique_affix' )
1430
+ ) {
1431
+ add_action( 'admin_init', array( &$this, 'connect_again' ) );
1432
+ }
1433
+ }
1434
+
1435
+ /**
1436
+ * Keeping the uninstall hook registered for free or premium plugin version may result to a fatal error that
1437
+ * could happen when a user tries to uninstall either version while one of them is still active. Uninstalling a
1438
+ * plugin will trigger inclusion of the free or premium version and if one of them is active during the
1439
+ * uninstallation, a fatal error may occur in case the plugin's class or functions are already defined.
1440
+ *
1441
+ * @author Leo Fajardo (@leorw)
1442
+ *
1443
+ * @since 1.2.0
1444
+ */
1445
+ private function unregister_uninstall_hook() {
1446
+ $uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
1447
+ unset( $uninstallable_plugins[ $this->_free_plugin_basename ] );
1448
+ unset( $uninstallable_plugins[ $this->premium_plugin_basename() ] );
1449
+
1450
+ update_option( 'uninstall_plugins', $uninstallable_plugins );
1451
+ }
1452
+
1453
+ /**
1454
+ * @since 1.2.0 Invalidate module's main file cache, otherwise, FS_Plugin_Updater will not fetch updates.
1455
+ *
1456
+ * @param bool $store_prev_path
1457
+ */
1458
+ private function clear_module_main_file_cache( $store_prev_path = true ) {
1459
+ if ( ! isset( $this->_storage->plugin_main_file ) ||
1460
+ empty( $this->_storage->plugin_main_file->path )
1461
+ ) {
1462
+ return;
1463
+ }
1464
+
1465
+ if ( ! $store_prev_path ) {
1466
+ /**
1467
+ * Storing the previous path is not needed when clearing the cache after an SDK version update since
1468
+ * the main purpose of the cache clearing in that event is to correct a wrong plugin main file path
1469
+ * which causes data mix-up between plugins (e.g. titles and versions of an add-on and its parent plugin).
1470
+ *
1471
+ * @author Leo Fajardo (@leorw)
1472
+ * @since 2.2.1
1473
+ */
1474
+ unset( $this->_storage->plugin_main_file->path );
1475
+ } else {
1476
+ $plugin_main_file = clone $this->_storage->plugin_main_file;
1477
+
1478
+ // Store cached path (2nd layer cache).
1479
+ $plugin_main_file->prev_path = $plugin_main_file->path;
1480
+
1481
+ // Clear cached path.
1482
+ unset( $plugin_main_file->path );
1483
+
1484
+ $this->_storage->plugin_main_file = $plugin_main_file;
1485
+ }
1486
+
1487
+ /**
1488
+ * Clear global cached path.
1489
+ *
1490
+ * @author Leo Fajardo (@leorw)
1491
+ * @since 1.2.2
1492
+ */
1493
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map' );
1494
+ unset( $id_slug_type_path_map[ $this->_module_id ]['path'] );
1495
+ self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
1496
+ }
1497
+
1498
+ /**
1499
+ * @author Leo Fajardo (@leorw)
1500
+ * @since 2.0.0
1501
+ */
1502
+ function _hook_action_links_and_register_account_hooks() {
1503
+ add_action( 'admin_init', array( &$this, '_add_tracking_links' ) );
1504
+
1505
+ if ( self::is_plugins_page() && $this->is_plugin() ) {
1506
+ $this->hook_plugin_action_links();
1507
+ }
1508
+
1509
+ $this->_register_account_hooks();
1510
+ }
1511
+
1512
+ /**
1513
+ * @author Vova Feldman (@svovaf)
1514
+ * @since 1.0.9
1515
+ */
1516
+ private function _register_account_hooks() {
1517
+ if ( ! is_admin() ) {
1518
+ return;
1519
+ }
1520
+
1521
+ /**
1522
+ * Always show the deactivation feedback form since we added
1523
+ * automatic free version deactivation upon premium code activation.
1524
+ *
1525
+ * @since 1.2.1.6
1526
+ */
1527
+ $this->add_ajax_action(
1528
+ 'submit_uninstall_reason',
1529
+ array( &$this, '_submit_uninstall_reason_action' )
1530
+ );
1531
+
1532
+ $this->add_ajax_action(
1533
+ 'cancel_subscription_or_trial',
1534
+ array( &$this, 'cancel_subscription_or_trial_ajax_action' )
1535
+ );
1536
+
1537
+ if ( ! $this->is_addon() || $this->is_parent_plugin_installed() ) {
1538
+ if ( ( $this->is_plugin() && self::is_plugins_page() ) ||
1539
+ ( $this->is_theme() && self::is_themes_page() )
1540
+ ) {
1541
+ add_action( 'admin_footer', array( &$this, '_add_deactivation_feedback_dialog_box' ) );
1542
+
1543
+ if ( $this->is_plugin() ) {
1544
+ add_action( 'admin_enqueue_scripts', array( &$this, '_enqueue_plugin_upgrade_notice_style' ) );
1545
+ }
1546
+ }
1547
+ }
1548
+ }
1549
+
1550
+ /**
1551
+ * @author Leo Fajardo (@leorw)
1552
+ * @since 2.1.4
1553
+ */
1554
+ function _enqueue_plugin_upgrade_notice_style() {
1555
+ fs_enqueue_local_style( 'fs_plugin_upgrade_notice', '/admin/plugin-upgrade-notice.css' );
1556
+ }
1557
+
1558
+ /**
1559
+ * Leverage backtrace to find caller plugin file path.
1560
+ *
1561
+ * @author Vova Feldman (@svovaf)
1562
+ * @since 1.0.6
1563
+ *
1564
+ * @param bool $is_init Is initiation sequence.
1565
+ *
1566
+ * @return string
1567
+ */
1568
+ private function _find_caller_plugin_file( $is_init = false ) {
1569
+ // Try to load the cached value of the file path.
1570
+ if ( isset( $this->_storage->plugin_main_file ) ) {
1571
+ $plugin_main_file = $this->_storage->plugin_main_file;
1572
+ if ( isset( $plugin_main_file->path ) ) {
1573
+ $absolute_path = $this->get_absolute_path( $plugin_main_file->path );
1574
+ if ( file_exists( $absolute_path ) ) {
1575
+ return $absolute_path;
1576
+ }
1577
+ }
1578
+ }
1579
+
1580
+ /**
1581
+ * @since 1.2.1
1582
+ *
1583
+ * `clear_module_main_file_cache()` is clearing the plugin's cached path on
1584
+ * deactivation. Therefore, if any plugin/theme was initiating `Freemius`
1585
+ * with that plugin's slug, it was overriding the empty plugin path with a wrong path.
1586
+ *
1587
+ * So, we've added a special mechanism with a 2nd layer of cache that uses `prev_path`
1588
+ * when the class instantiator isn't the module.
1589
+ */
1590
+ if ( ! $is_init ) {
1591
+ // Fetch prev path cache.
1592
+ if ( isset( $this->_storage->plugin_main_file ) &&
1593
+ isset( $this->_storage->plugin_main_file->prev_path )
1594
+ ) {
1595
+ $absolute_path = $this->get_absolute_path( $this->_storage->plugin_main_file->prev_path );
1596
+ if ( file_exists( $absolute_path ) ) {
1597
+ return $absolute_path;
1598
+ }
1599
+ }
1600
+
1601
+ wp_die(
1602
+ $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' ) .
1603
+ " Module: {$this->_slug}; SDK: " . WP_FS__SDK_VERSION . ";",
1604
+ $this->get_text_inline( 'Error', 'error' ),
1605
+ array( 'back_link' => true )
1606
+ );
1607
+ }
1608
+
1609
+ /**
1610
+ * @since 1.2.1
1611
+ *
1612
+ * Only the original instantiator that calls dynamic_init can modify the module's path.
1613
+ */
1614
+ // Find caller module.
1615
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
1616
+ $this->_storage->plugin_main_file = (object) array(
1617
+ 'path' => $id_slug_type_path_map[ $this->_module_id ]['path'],
1618
+ );
1619
+
1620
+ return $this->get_absolute_path( $id_slug_type_path_map[ $this->_module_id ]['path'] );
1621
+ }
1622
+
1623
+ /**
1624
+ * @author Leo Fajardo (@leorw)
1625
+ * @since 1.2.3
1626
+ *
1627
+ * @param string $path
1628
+ *
1629
+ * @return string
1630
+ */
1631
+ private function get_relative_path( $path ) {
1632
+ $module_root_dir = $this->get_module_root_dir_path();
1633
+ if ( 0 === strpos( $path, $module_root_dir ) ) {
1634
+ $path = substr( $path, strlen( $module_root_dir ) );
1635
+ }
1636
+
1637
+ return $path;
1638
+ }
1639
+
1640
+ /**
1641
+ * @author Leo Fajardo (@leorw)
1642
+ * @since 1.2.3
1643
+ *
1644
+ * @param string $path
1645
+ * @param string|bool $module_type
1646
+ *
1647
+ * @return string
1648
+ */
1649
+ private function get_absolute_path( $path, $module_type = false ) {
1650
+ $module_root_dir = $this->get_module_root_dir_path( $module_type );
1651
+ if ( 0 !== strpos( $path, $module_root_dir ) ) {
1652
+ $path = fs_normalize_path( $module_root_dir . $path );
1653
+ }
1654
+
1655
+ return $path;
1656
+ }
1657
+
1658
+ /**
1659
+ * @author Leo Fajardo (@leorw)
1660
+ * @since 1.2.3
1661
+ *
1662
+ * @param string|bool $module_type
1663
+ *
1664
+ * @return string
1665
+ */
1666
+ private function get_module_root_dir_path( $module_type = false ) {
1667
+ $is_plugin = empty( $module_type ) ?
1668
+ $this->is_plugin() :
1669
+ ( WP_FS__MODULE_TYPE_PLUGIN === $module_type );
1670
+
1671
+ return fs_normalize_path( trailingslashit( $is_plugin ?
1672
+ WP_PLUGIN_DIR :
1673
+ get_theme_root() ) );
1674
+ }
1675
+
1676
+ /**
1677
+ * @author Leo Fajardo (@leorw)
1678
+ *
1679
+ * @param number $module_id
1680
+ * @param string $slug
1681
+ *
1682
+ * @since 1.2.2
1683
+ */
1684
+ private function store_id_slug_type_path_map( $module_id, $slug ) {
1685
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
1686
+
1687
+ $store_option = false;
1688
+
1689
+ if ( ! isset( $id_slug_type_path_map[ $module_id ] ) ) {
1690
+ $id_slug_type_path_map[ $module_id ] = array(
1691
+ 'slug' => $slug
1692
+ );
1693
+
1694
+ $store_option = true;
1695
+ }
1696
+
1697
+ if ( ! isset( $id_slug_type_path_map[ $module_id ]['path'] ) ||
1698
+ /**
1699
+ * This verification is for cases when suddenly the same module
1700
+ * is installed but with a different folder name.
1701
+ *
1702
+ * @author Vova Feldman (@svovaf)
1703
+ * @since 1.2.3
1704
+ */
1705
+ ! file_exists( $this->get_absolute_path(
1706
+ $id_slug_type_path_map[ $module_id ]['path'],
1707
+ $id_slug_type_path_map[ $module_id ]['type']
1708
+ ) )
1709
+ ) {
1710
+ $caller_main_file_and_type = $this->get_caller_main_file_and_type();
1711
+
1712
+ $id_slug_type_path_map[ $module_id ]['type'] = $caller_main_file_and_type->module_type;
1713
+ $id_slug_type_path_map[ $module_id ]['path'] = $caller_main_file_and_type->path;
1714
+
1715
+ $store_option = true;
1716
+ }
1717
+
1718
+ if ( $store_option ) {
1719
+ self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
1720
+ }
1721
+ }
1722
+
1723
+ /**
1724
+ * Identifies the caller type: plugin or theme.
1725
+ *
1726
+ * @author Leo Fajardo (@leorw)
1727
+ * @since 1.2.2
1728
+ *
1729
+ * @author Vova Feldman (@svovaf)
1730
+ * @since 1.2.2.3 Find the earliest module in the call stack that calls to the SDK. This fix is for cases when
1731
+ * add-ons are relying on loading the SDK from the parent module, and also allows themes including the
1732
+ * SDK an internal file instead of directly from functions.php.
1733
+ * @since 1.2.1.7 Knows how to handle cases when an add-on includes the parent module logic.
1734
+ */
1735
+ private function get_caller_main_file_and_type() {
1736
+ self::require_plugin_essentials();
1737
+
1738
+ $all_plugins = fs_get_plugins( true );
1739
+ $all_plugins_paths = array();
1740
+
1741
+ // Get active plugin's main files real full names (might be symlinks).
1742
+ foreach ( $all_plugins as $relative_path => $data ) {
1743
+ if ( false === strpos( fs_normalize_path( $relative_path ), '/' ) ) {
1744
+ /**
1745
+ * Ignore plugins that don't have a folder (e.g. Hello Dolly) since they
1746
+ * can't really include the SDK.
1747
+ *
1748
+ * @author Vova Feldman
1749
+ * @since 1.2.1.7
1750
+ */
1751
+ continue;
1752
+ }
1753
+
1754
+ $all_plugins_paths[] = fs_normalize_path( realpath( WP_PLUGIN_DIR . '/' . $relative_path ) );
1755
+ }
1756
+
1757
+ $caller_file_candidate = false;
1758
+ $caller_map = array();
1759
+ $module_type = WP_FS__MODULE_TYPE_PLUGIN;
1760
+ $themes_dir = fs_normalize_path( get_theme_root() );
1761
+
1762
+ for ( $i = 1, $bt = debug_backtrace(), $len = count( $bt ); $i < $len; $i ++ ) {
1763
+ if ( empty( $bt[ $i ]['file'] ) ) {
1764
+ continue;
1765
+ }
1766
+
1767
+ if ( $i > 1 && ! empty( $bt[ $i - 1 ]['file'] ) && $bt[ $i ]['file'] === $bt[ $i - 1 ]['file'] ) {
1768
+ // If file same as the prev file in the stack, skip it.
1769
+ continue;
1770
+ }
1771
+
1772
+ if ( ! empty( $bt[ $i ]['function'] ) && in_array( $bt[ $i ]['function'], array(
1773
+ 'do_action',
1774
+ 'apply_filter',
1775
+ // The string split is stupid, but otherwise, theme check
1776
+ // throws info notices.
1777
+ 'requir' . 'e_once',
1778
+ 'requir' . 'e',
1779
+ 'includ' . 'e_once',
1780
+ 'includ' . 'e',
1781
+ 'install_and_activate_plugin',
1782
+ 'try_activate_plugin',
1783
+ 'activate_plugin'
1784
+ ) )
1785
+ ) {
1786
+ // Ignore call stack hooks and files inclusion.
1787
+ continue;
1788
+ }
1789
+
1790
+ $caller_file_path = fs_normalize_path( $bt[ $i ]['file'] );
1791
+
1792
+ if ( 'functions.php' === basename( $caller_file_path ) ) {
1793
+ /**
1794
+ * 1. Assumes that theme's starting execution file is functions.php.
1795
+ * 2. This complex logic fixes symlink issues (e.g. with Vargant).
1796
+ *
1797
+ * @author Vova Feldman (@svovaf)
1798
+ * @since 1.2.2.5
1799
+ */
1800
+
1801
+ if ( $caller_file_path == fs_normalize_path( realpath( trailingslashit( $themes_dir ) . basename( dirname( $caller_file_path ) ) . '/' . basename( $caller_file_path ) ) ) ) {
1802
+ $module_type = WP_FS__MODULE_TYPE_THEME;
1803
+
1804
+ /**
1805
+ * Relative path of the theme, e.g.:
1806
+ * `my-theme/functions.php`
1807
+ *
1808
+ * @author Leo Fajardo (@leorw)
1809
+ */
1810
+ $caller_file_candidate = basename( dirname( $caller_file_path ) ) .
1811
+ '/' .
1812
+ basename( $caller_file_path );
1813
+
1814
+ continue;
1815
+ }
1816
+ }
1817
+
1818
+ $caller_file_hash = md5( $caller_file_path );
1819
+
1820
+ if ( ! isset( $caller_map[ $caller_file_hash ] ) ) {
1821
+ foreach ( $all_plugins_paths as $plugin_path ) {
1822
+ if ( false !== strpos( $caller_file_path, fs_normalize_path( dirname( $plugin_path ) . '/' ) ) ) {
1823
+ $caller_map[ $caller_file_hash ] = fs_normalize_path( $plugin_path );
1824
+ break;
1825
+ }
1826
+ }
1827
+ }
1828
+
1829
+ if ( isset( $caller_map[ $caller_file_hash ] ) ) {
1830
+ $module_type = WP_FS__MODULE_TYPE_PLUGIN;
1831
+ $caller_file_candidate = plugin_basename( $caller_map[ $caller_file_hash ] );
1832
+ }
1833
+ }
1834
+
1835
+ return (object) array(
1836
+ 'module_type' => $module_type,
1837
+ 'path' => $caller_file_candidate
1838
+ );
1839
+ }
1840
+
1841
+ #----------------------------------------------------------------------------------
1842
+ #region Deactivation Feedback Form
1843
+ #----------------------------------------------------------------------------------
1844
+
1845
+ /**
1846
+ * Displays a confirmation and feedback dialog box when the user clicks on the "Deactivate" link on the plugins
1847
+ * page.
1848
+ *
1849
+ * @author Vova Feldman (@svovaf)
1850
+ * @author Leo Fajardo (@leorw)
1851
+ * @since 1.1.2
1852
+ */
1853
+ function _add_deactivation_feedback_dialog_box() {
1854
+ /* Check the type of user:
1855
+ * 1. Long-term (long-term)
1856
+ * 2. Non-registered and non-anonymous short-term (non-registered-and-non-anonymous-short-term).
1857
+ * 3. Short-term (short-term)
1858
+ */
1859
+ $is_long_term_user = true;
1860
+
1861
+ // Check if the site is at least 2 days old.
1862
+ $time_installed = $this->_storage->install_timestamp;
1863
+
1864
+ // Difference in seconds.
1865
+ $date_diff = time() - $time_installed;
1866
+
1867
+ // Convert seconds to days.
1868
+ $date_diff_days = floor( $date_diff / ( 60 * 60 * 24 ) );
1869
+
1870
+ if ( $date_diff_days < 2 ) {
1871
+ $is_long_term_user = false;
1872
+ }
1873
+
1874
+ $is_long_term_user = $this->apply_filters( 'is_long_term_user', $is_long_term_user );
1875
+
1876
+ if ( $is_long_term_user ) {
1877
+ $user_type = 'long-term';
1878
+ } else {
1879
+ if ( ! $this->is_registered() && ! $this->is_anonymous() ) {
1880
+ $user_type = 'non-registered-and-non-anonymous-short-term';
1881
+ } else {
1882
+ $user_type = 'short-term';
1883
+ }
1884
+ }
1885
+
1886
+ $uninstall_reasons = $this->_get_uninstall_reasons( $user_type );
1887
+
1888
+ // Load the HTML template for the deactivation feedback dialog box.
1889
+ $vars = array(
1890
+ 'reasons' => $uninstall_reasons,
1891
+ 'id' => $this->_module_id
1892
+ );
1893
+
1894
+ /**
1895
+ * @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.
1896
+ */
1897
+ fs_require_template( 'forms/deactivation/form.php', $vars );
1898
+ }
1899
+
1900
+ /**
1901
+ * @author Leo Fajardo (@leorw)
1902
+ * @since 1.1.2
1903
+ *
1904
+ * @param string $user_type
1905
+ *
1906
+ * @return array The uninstall reasons for the specified user type.
1907
+ */
1908
+ function _get_uninstall_reasons( $user_type = 'long-term' ) {
1909
+ $module_type = $this->_module_type;
1910
+
1911
+ $internal_message_template_var = array(
1912
+ 'id' => $this->_module_id
1913
+ );
1914
+
1915
+ $plan = $this->get_plan();
1916
+
1917
+ if ( $this->is_registered() && is_object( $plan ) && $plan->has_technical_support() ) {
1918
+ $contact_support_template = fs_get_template( 'forms/deactivation/contact.php', $internal_message_template_var );
1919
+ } else {
1920
+ $contact_support_template = '';
1921
+ }
1922
+
1923
+ $reason_found_better_plugin = array(
1924
+ 'id' => self::REASON_FOUND_A_BETTER_PLUGIN,
1925
+ 'text' => sprintf( $this->get_text_inline( 'I found a better %s', 'reason-found-a-better-plugin' ), $module_type ),
1926
+ 'input_type' => 'textfield',
1927
+ 'input_placeholder' => sprintf( $this->get_text_inline( "What's the %s's name?", 'placeholder-plugin-name' ), $module_type ),
1928
+ );
1929
+
1930
+ $reason_temporary_deactivation = array(
1931
+ 'id' => self::REASON_TEMPORARY_DEACTIVATION,
1932
+ 'text' => sprintf(
1933
+ $this->get_text_inline( "It's a temporary %s. I'm just debugging an issue.", 'reason-temporary-x' ),
1934
+ strtolower( $this->is_plugin() ?
1935
+ $this->get_text_inline( 'Deactivation', 'deactivation' ) :
1936
+ $this->get_text_inline( 'Theme Switch', 'theme-switch' )
1937
+ )
1938
+ ),
1939
+ 'input_type' => '',
1940
+ 'input_placeholder' => ''
1941
+ );
1942
+
1943
+ $reason_other = array(
1944
+ 'id' => self::REASON_OTHER,
1945
+ 'text' => $this->get_text_inline( 'Other', 'reason-other' ),
1946
+ 'input_type' => 'textfield',
1947
+ 'input_placeholder' => ''
1948
+ );
1949
+
1950
+ $long_term_user_reasons = array(
1951
+ array(
1952
+ 'id' => self::REASON_NO_LONGER_NEEDED,
1953
+ 'text' => sprintf( $this->get_text_inline( 'I no longer need the %s', 'reason-no-longer-needed' ), $module_type ),
1954
+ 'input_type' => '',
1955
+ 'input_placeholder' => ''
1956
+ ),
1957
+ $reason_found_better_plugin,
1958
+ array(
1959
+ 'id' => self::REASON_NEEDED_FOR_A_SHORT_PERIOD,
1960
+ 'text' => sprintf( $this->get_text_inline( 'I only needed the %s for a short period', 'reason-needed-for-a-short-period' ), $module_type ),
1961
+ 'input_type' => '',
1962
+ 'input_placeholder' => ''
1963
+ ),
1964
+ array(
1965
+ 'id' => self::REASON_BROKE_MY_SITE,
1966
+ 'text' => sprintf( $this->get_text_inline( 'The %s broke my site', 'reason-broke-my-site' ), $module_type ),
1967
+ 'input_type' => '',
1968
+ 'input_placeholder' => '',
1969
+ 'internal_message' => $contact_support_template
1970
+ ),
1971
+ array(
1972
+ 'id' => self::REASON_SUDDENLY_STOPPED_WORKING,
1973
+ 'text' => sprintf( $this->get_text_inline( 'The %s suddenly stopped working', 'reason-suddenly-stopped-working' ), $module_type ),
1974
+ 'input_type' => '',
1975
+ 'input_placeholder' => '',
1976
+ 'internal_message' => $contact_support_template
1977
+ )
1978
+ );
1979
+
1980
+ if ( $this->is_paying() ) {
1981
+ $long_term_user_reasons[] = array(
1982
+ 'id' => self::REASON_CANT_PAY_ANYMORE,
1983
+ 'text' => $this->get_text_inline( "I can't pay for it anymore", 'reason-cant-pay-anymore' ),
1984
+ 'input_type' => 'textfield',
1985
+ 'input_placeholder' => $this->get_text_inline( 'What price would you feel comfortable paying?', 'placeholder-comfortable-price' )
1986
+ );
1987
+ }
1988
+
1989
+ $reason_dont_share_info = array(
1990
+ 'id' => self::REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION,
1991
+ 'text' => $this->get_text_inline( "I don't like to share my information with you", 'reason-dont-like-to-share-my-information' ),
1992
+ 'input_type' => '',
1993
+ 'input_placeholder' => ''
1994
+ );
1995
+
1996
+ /**
1997
+ * If the current user has selected the "don't share data" reason in the deactivation feedback modal, inform the
1998
+ * user by showing additional message that he doesn't have to share data and can just choose to skip the opt-in
1999
+ * (the Skip button is included in the message to show). This message will only be shown if anonymous mode is
2000
+ * enabled and the user's account is currently not in pending activation state (similar to the way the Skip
2001
+ * button in the opt-in form is shown/hidden).
2002
+ */
2003
+ if ( $this->is_enable_anonymous() && ! $this->is_pending_activation() ) {
2004
+ $reason_dont_share_info['internal_message'] = fs_get_template( 'forms/deactivation/retry-skip.php', $internal_message_template_var );
2005
+ }
2006
+
2007
+ $uninstall_reasons = array(
2008
+ 'long-term' => $long_term_user_reasons,
2009
+ 'non-registered-and-non-anonymous-short-term' => array(
2010
+ array(
2011
+ 'id' => self::REASON_DIDNT_WORK,
2012
+ 'text' => sprintf( $this->get_text_inline( "The %s didn't work", 'reason-didnt-work' ), $module_type ),
2013
+ 'input_type' => '',
2014
+ 'input_placeholder' => ''
2015
+ ),
2016
+ $reason_dont_share_info,
2017
+ $reason_found_better_plugin
2018
+ ),
2019
+ 'short-term' => array(
2020
+ array(
2021
+ 'id' => self::REASON_COULDNT_MAKE_IT_WORK,
2022
+ 'text' => $this->get_text_inline( "I couldn't understand how to make it work", 'reason-couldnt-make-it-work' ),
2023
+ 'input_type' => '',
2024
+ 'input_placeholder' => '',
2025
+ 'internal_message' => $contact_support_template
2026
+ ),
2027
+ $reason_found_better_plugin,
2028
+ array(
2029
+ 'id' => self::REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE,
2030
+ '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 ),
2031
+ 'input_type' => 'textarea',
2032
+ 'input_placeholder' => $this->get_text_inline( 'What feature?', 'placeholder-feature' )
2033
+ ),
2034
+ array(
2035
+ 'id' => self::REASON_NOT_WORKING,
2036
+ 'text' => sprintf( $this->get_text_inline( 'The %s is not working', 'reason-not-working' ), $module_type ),
2037
+ 'input_type' => 'textarea',
2038
+ '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' )
2039
+ ),
2040
+ array(
2041
+ 'id' => self::REASON_NOT_WHAT_I_WAS_LOOKING_FOR,
2042
+ 'text' => $this->get_text_inline( "It's not what I was looking for", 'reason-not-what-i-was-looking-for' ),
2043
+ 'input_type' => 'textarea',
2044
+ 'input_placeholder' => $this->get_text_inline( "What you've been looking for?", 'placeholder-what-youve-been-looking-for' )
2045
+ ),
2046
+ array(
2047
+ 'id' => self::REASON_DIDNT_WORK_AS_EXPECTED,
2048
+ 'text' => sprintf( $this->get_text_inline( "The %s didn't work as expected", 'reason-didnt-work-as-expected' ), $module_type ),
2049
+ 'input_type' => 'textarea',
2050
+ 'input_placeholder' => $this->get_text_inline( 'What did you expect?', 'placeholder-what-did-you-expect' )
2051
+ )
2052
+ )
2053
+ );
2054
+
2055
+ // Randomize the reasons for the current user type.
2056
+ shuffle( $uninstall_reasons[ $user_type ] );
2057
+
2058
+ // Keep the following reasons as the last items in the list.
2059
+ $uninstall_reasons[ $user_type ][] = $reason_temporary_deactivation;
2060
+ $uninstall_reasons[ $user_type ][] = $reason_other;
2061
+
2062
+ $uninstall_reasons = $this->apply_filters( 'uninstall_reasons', $uninstall_reasons );
2063
+
2064
+ return $uninstall_reasons[ $user_type ];
2065
+ }
2066
+
2067
+ /**
2068
+ * Called after the user has submitted his reason for deactivating the plugin.
2069
+ *
2070
+ * @author Leo Fajardo (@leorw)
2071
+ * @since 1.1.2
2072
+ */
2073
+ function _submit_uninstall_reason_action() {
2074
+ $this->_logger->entrance();
2075
+
2076
+ $this->check_ajax_referer( 'submit_uninstall_reason' );
2077
+
2078
+ $reason_id = fs_request_get( 'reason_id' );
2079
+
2080
+ // Check if the given reason ID is an unsigned integer.
2081
+ if ( ! ctype_digit( $reason_id ) ) {
2082
+ exit;
2083
+ }
2084
+
2085
+ $reason_info = trim( fs_request_get( 'reason_info', '' ) );
2086
+ if ( ! empty( $reason_info ) ) {
2087
+ $reason_info = substr( $reason_info, 0, 128 );
2088
+ }
2089
+
2090
+ $reason = (object) array(
2091
+ 'id' => $reason_id,
2092
+ 'info' => $reason_info,
2093
+ 'is_anonymous' => fs_request_get_bool( 'is_anonymous' )
2094
+ );
2095
+
2096
+ $this->_storage->store( 'uninstall_reason', $reason );
2097
+
2098
+ /**
2099
+ * If the module type is "theme", trigger the uninstall event here (on theme deactivation) since themes do
2100
+ * not support uninstall hook.
2101
+ *
2102
+ * @author Leo Fajardo (@leorw)
2103
+ * @since 1.2.2
2104
+ */
2105
+ if ( $this->is_theme() ) {
2106
+ if ( $this->is_premium() && ! $this->has_active_valid_license() ) {
2107
+ FS_Plugin_Updater::instance( $this )->delete_update_data();
2108
+ }
2109
+
2110
+ $this->_uninstall_plugin_event( false );
2111
+ $this->remove_sdk_reference();
2112
+ }
2113
+
2114
+ // Print '1' for successful operation.
2115
+ echo 1;
2116
+ exit;
2117
+ }
2118
+
2119
+ /**
2120
+ * @author Leo Fajardo (@leorw)
2121
+ * @since 2.1.4
2122
+ */
2123
+ function cancel_subscription_or_trial_ajax_action() {
2124
+ $this->_logger->entrance();
2125
+
2126
+ $this->check_ajax_referer( 'cancel_subscription_or_trial' );
2127
+
2128
+ $result = $this->cancel_subscription_or_trial( fs_request_get( 'plugin_id', $this->get_id() ), false );
2129
+
2130
+ if ( $this->is_api_error( $result ) ) {
2131
+ $this->shoot_ajax_failure( $result->error->message );
2132
+ }
2133
+
2134
+ $this->shoot_ajax_success();
2135
+ }
2136
+
2137
+ /**
2138
+ * @author Leo Fajardo (@leorw)
2139
+ * @since 2.1.4
2140
+ *
2141
+ * @param number $plugin_id
2142
+ *
2143
+ * @return object
2144
+ */
2145
+ private function cancel_subscription_or_trial( $plugin_id ) {
2146
+ $fs = null;
2147
+ if ( $plugin_id == $this->get_id() ) {
2148
+ $fs = $this;
2149
+ } else if ( $this->is_addon_activated( $plugin_id ) ) {
2150
+ $fs = self::get_instance_by_id( $plugin_id );
2151
+ }
2152
+
2153
+ $result = null;
2154
+
2155
+ if ( ! is_null( $fs ) ) {
2156
+ $result = $fs->is_paid_trial() ?
2157
+ $fs->_cancel_trial() :
2158
+ $fs->_downgrade_site();
2159
+ }
2160
+
2161
+ return $result;
2162
+ }
2163
+
2164
+ /**
2165
+ * @author Leo Fajardo (@leorw)
2166
+ * @since 2.0.2
2167
+ */
2168
+ function _delete_theme_update_data_action() {
2169
+ FS_Plugin_Updater::instance( $this )->delete_update_data();
2170
+ }
2171
+
2172
+ #endregion
2173
+
2174
+ #----------------------------------------------------------------------------------
2175
+ #region Instance
2176
+ #----------------------------------------------------------------------------------
2177
+
2178
+ /**
2179
+ * Main singleton instance.
2180
+ *
2181
+ * @author Vova Feldman (@svovaf)
2182
+ * @since 1.0.0
2183
+ *
2184
+ * @param number $module_id
2185
+ * @param string|bool $slug
2186
+ * @param bool $is_init Is initiation sequence.
2187
+ *
2188
+ * @return Freemius|false
2189
+ */
2190
+ static function instance( $module_id, $slug = false, $is_init = false ) {
2191
+ if ( empty( $module_id ) ) {
2192
+ return false;
2193
+ }
2194
+
2195
+ /**
2196
+ * 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.
2197
+ */
2198
+ self::_load_required_static();
2199
+
2200
+ if ( ! is_numeric( $module_id ) ) {
2201
+ if ( ! $is_init && true === $slug ) {
2202
+ $is_init = true;
2203
+ }
2204
+
2205
+ $slug = $module_id;
2206
+
2207
+ $module = FS_Plugin_Manager::instance( $slug )->get();
2208
+
2209
+ if ( is_object( $module ) ) {
2210
+ $module_id = $module->id;
2211
+ }
2212
+ }
2213
+
2214
+ $key = 'm_' . $module_id;
2215
+
2216
+ if ( ! isset( self::$_instances[ $key ] ) ) {
2217
+ self::$_instances[ $key ] = new Freemius( $module_id, $slug, $is_init );
2218
+ }
2219
+
2220
+ return self::$_instances[ $key ];
2221
+ }
2222
+
2223
+ /**
2224
+ * @author Vova Feldman (@svovaf)
2225
+ * @since 1.0.6
2226
+ *
2227
+ * @param number $addon_id
2228
+ *
2229
+ * @return bool
2230
+ */
2231
+ private static function has_instance( $addon_id ) {
2232
+ return isset( self::$_instances[ 'm_' . $addon_id ] );
2233
+ }
2234
+
2235
+ /**
2236
+ * @author Leo Fajardo (@leorw)
2237
+ * @since 1.2.2
2238
+ *
2239
+ * @param string|number $id_or_slug
2240
+ * @param string $module_type
2241
+ *
2242
+ * @return number|false
2243
+ */
2244
+ private static function get_module_id( $id_or_slug, $module_type = WP_FS__MODULE_TYPE_PLUGIN ) {
2245
+ if ( is_numeric( $id_or_slug ) ) {
2246
+ return $id_or_slug;
2247
+ }
2248
+
2249
+ foreach ( self::$_instances as $instance ) {
2250
+ // Also check the module type since there can be a plugin and a theme with the same slug.
2251
+ if ( ( $module_type === $instance->get_module_type() ) && ( $id_or_slug === $instance->get_slug() ) ) {
2252
+ return $instance->get_id();
2253
+ }
2254
+ }
2255
+
2256
+ return false;
2257
+ }
2258
+
2259
+ /**
2260
+ * @author Vova Feldman (@svovaf)
2261
+ * @since 1.0.6
2262
+ *
2263
+ * @param number $id
2264
+ *
2265
+ * @return false|Freemius
2266
+ */
2267
+ static function get_instance_by_id( $id ) {
2268
+ return isset ( self::$_instances[ 'm_' . $id ] ) ?
2269
+ self::$_instances[ 'm_' . $id ] :
2270
+ false;
2271
+ }
2272
+
2273
+ /**
2274
+ *
2275
+ * @author Vova Feldman (@svovaf)
2276
+ * @since 1.0.1
2277
+ *
2278
+ * @param string $plugin_file
2279
+ * @param string $module_type
2280
+ *
2281
+ * @return false|Freemius
2282
+ */
2283
+ static function get_instance_by_file( $plugin_file, $module_type = WP_FS__MODULE_TYPE_PLUGIN ) {
2284
+ $slug = self::find_slug_by_basename( $plugin_file );
2285
+
2286
+ return ( false !== $slug ) ?
2287
+ self::instance( self::get_module_id( $slug, $module_type ) ) :
2288
+ false;
2289
+ }
2290
+
2291
+ /**
2292
+ * @author Vova Feldman (@svovaf)
2293
+ * @since 1.0.6
2294
+ *
2295
+ * @return false|Freemius
2296
+ */
2297
+ function get_parent_instance() {
2298
+ return self::get_instance_by_id( $this->_plugin->parent_plugin_id );
2299
+ }
2300
+
2301
+ /**
2302
+ * @author Vova Feldman (@svovaf)
2303
+ * @since 1.0.6
2304
+ *
2305
+ * @param string|number $id_or_slug
2306
+ *
2307
+ * @return false|Freemius
2308
+ */
2309
+ function get_addon_instance( $id_or_slug ) {
2310
+ $addon_id = self::get_module_id( $id_or_slug );
2311
+
2312
+ return self::instance( $addon_id );
2313
+ }
2314
+
2315
+ #endregion ------------------------------------------------------------------
2316
+
2317
+ /**
2318
+ * @author Vova Feldman (@svovaf)
2319
+ * @since 1.0.6
2320
+ *
2321
+ * @return bool
2322
+ */
2323
+ function is_parent_plugin_installed() {
2324
+ $is_active = self::has_instance( $this->_plugin->parent_plugin_id );
2325
+
2326
+ if ( $is_active ) {
2327
+ return true;
2328
+ }
2329
+
2330
+ /**
2331
+ * Parent module might be a theme. If that's the case, the add-on's FS
2332
+ * instance will be loaded prior to the theme's FS instance, therefore,
2333
+ * we need to check if it's active with a "look ahead".
2334
+ *
2335
+ * @author Vova Feldman
2336
+ * @since 1.2.2.3
2337
+ */
2338
+ global $fs_active_plugins;
2339
+ if ( is_object( $fs_active_plugins ) && is_array( $fs_active_plugins->plugins ) ) {
2340
+ $active_theme = wp_get_theme();
2341
+
2342
+ foreach ( $fs_active_plugins->plugins as $sdk => $module ) {
2343
+ if ( WP_FS__MODULE_TYPE_THEME === $module->type ) {
2344
+ if ( $module->plugin_path == $active_theme->get_stylesheet() ) {
2345
+ // Parent module is a theme and it's currently active.
2346
+ return true;
2347
+ }
2348
+ }
2349
+ }
2350
+ }
2351
+
2352
+ return false;
2353
+ }
2354
+
2355
+ /**
2356
+ * Check if add-on parent plugin in activation mode.
2357
+ *
2358
+ * @author Vova Feldman (@svovaf)
2359
+ * @since 1.0.7
2360
+ *
2361
+ * @return bool
2362
+ */
2363
+ function is_parent_in_activation() {
2364
+ $parent_fs = $this->get_parent_instance();
2365
+ if ( ! is_object( $parent_fs ) ) {
2366
+ return false;
2367
+ }
2368
+
2369
+ return ( $parent_fs->is_activation_mode() );
2370
+ }
2371
+
2372
+ /**
2373
+ * Is plugin in activation mode.
2374
+ *
2375
+ * @author Vova Feldman (@svovaf)
2376
+ * @since 1.0.7
2377
+ *
2378
+ * @param bool $and_on
2379
+ *
2380
+ * @return bool
2381
+ */
2382
+ function is_activation_mode( $and_on = true ) {
2383
+ return fs_is_network_admin() ?
2384
+ $this->is_network_activation_mode( $and_on ) :
2385
+ $this->is_site_activation_mode( $and_on );
2386
+ }
2387
+
2388
+ /**
2389
+ * Is plugin in activation mode.
2390
+ *
2391
+ * @author Vova Feldman (@svovaf)
2392
+ * @since 1.0.7
2393
+ *
2394
+ * @param bool $and_on
2395
+ *
2396
+ * @return bool
2397
+ */
2398
+ function is_site_activation_mode( $and_on = true ) {
2399
+ return (
2400
+ ( $this->is_on() || ! $and_on ) &&
2401
+ ( $this->is_premium() && true === $this->_storage->require_license_activation ) ||
2402
+ (
2403
+ ( ! $this->is_registered() ||
2404
+ ( $this->is_only_premium() && ! $this->has_features_enabled_license() ) ) &&
2405
+ ( ! $this->is_enable_anonymous() ||
2406
+ ( ! $this->is_anonymous() && ! $this->is_pending_activation() ) )
2407
+ )
2408
+ );
2409
+ }
2410
+
2411
+ /**
2412
+ * Checks if the SDK in network activation mode.
2413
+ *
2414
+ * @author Leo Fajardo (@leorw)
2415
+ * @since 2.0.0
2416
+ *
2417
+ * @param bool $and_on
2418
+ *
2419
+ * @return bool
2420
+ */
2421
+ private function is_network_activation_mode( $and_on = true ) {
2422
+ if ( ! $this->_is_network_active ) {
2423
+ // Not network activated.
2424
+ return false;
2425
+ }
2426
+
2427
+ if ( $this->is_network_upgrade_mode() ) {
2428
+ // Special flag to enforce network activation mode to decide what to do with the sites that are not yet opted-in nor skipped.
2429
+ return true;
2430
+ }
2431
+
2432
+ if ( ! $this->is_site_activation_mode( $and_on ) ) {
2433
+ // 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.
2434
+ return false;
2435
+ }
2436
+
2437
+ if ( $this->is_network_delegated_connection() ) {
2438
+ // Super-admin delegated the connection to the site admins -> not activation mode.
2439
+ return false;
2440
+ }
2441
+
2442
+ if ( $this->is_network_anonymous() && true !== $this->_storage->require_license_activation ) {
2443
+ // Super-admin skipped the connection network wide -> not activation mode.
2444
+ return false;
2445
+ }
2446
+
2447
+ if ( $this->is_network_registered() ) {
2448
+ // Super-admin connected at least one site -> not activation mode.
2449
+ return false;
2450
+ }
2451
+
2452
+ return true;
2453
+ }
2454
+
2455
+ /**
2456
+ * Check if current page is the opt-in/pending-activation page.
2457
+ *
2458
+ * @author Vova Feldman (@svovaf)
2459
+ * @since 1.2.1.7
2460
+ *
2461
+ * @return bool
2462
+ */
2463
+ function is_activation_page() {
2464
+ if ( $this->_menu->is_main_settings_page() ) {
2465
+ return true;
2466
+ }
2467
+
2468
+ if ( ! $this->is_activation_mode() ) {
2469
+ return false;
2470
+ }
2471
+
2472
+ // Check if current page is matching the activation page.
2473
+ return $this->is_matching_url( $this->get_activation_url() );
2474
+ }
2475
+
2476
+ /**
2477
+ * Check if URL path's are matching and that all querystring
2478
+ * arguments of the $sub_url exist in the $url with the same values.
2479
+ *
2480
+ * WARNING:
2481
+ * 1. This method doesn't check if the sub/domain are matching.
2482
+ * 2. Ignore case sensitivity.
2483
+ *
2484
+ * @author Vova Feldman (@svovaf)
2485
+ * @since 1.2.1.7
2486
+ *
2487
+ * @param string $sub_url
2488
+ * @param string $url If argument is not set, check if the sub_url matching the current's page URL.
2489
+ *
2490
+ * @return bool
2491
+ */
2492
+ private function is_matching_url( $sub_url, $url = '' ) {
2493
+ if ( empty( $url ) ) {
2494
+ $url = $_SERVER['REQUEST_URI'];
2495
+ }
2496
+
2497
+ $url = strtolower( $url );
2498
+ $sub_url = strtolower( $sub_url );
2499
+
2500
+ if ( parse_url( $sub_url, PHP_URL_PATH ) !== parse_url( $url, PHP_URL_PATH ) ) {
2501
+ // Different path - DO NOT OVERRIDE PAGE.
2502
+ return false;
2503
+ }
2504
+
2505
+ $url_params = array();
2506
+ parse_str( parse_url( $url, PHP_URL_QUERY ), $url_params );
2507
+
2508
+ $sub_url_params = array();
2509
+ parse_str( parse_url( $sub_url, PHP_URL_QUERY ), $sub_url_params );
2510
+
2511
+ foreach ( $sub_url_params as $key => $val ) {
2512
+ if ( ! isset( $url_params[ $key ] ) || $val != $url_params[ $key ] ) {
2513
+ // Not matching query string - DO NOT OVERRIDE PAGE.
2514
+ return false;
2515
+ }
2516
+ }
2517
+
2518
+ return true;
2519
+ }
2520
+
2521
+ /**
2522
+ * Get the basenames of all active plugins for specific blog. Including network activated plugins.
2523
+ *
2524
+ * @author Vova Feldman (@svovaf)
2525
+ * @since 2.0.0
2526
+ *
2527
+ * @param int $blog_id
2528
+ *
2529
+ * @return string[]
2530
+ */
2531
+ private static function get_active_plugins_basenames( $blog_id = 0 ) {
2532
+ if ( is_multisite() && $blog_id > 0 ) {
2533
+ $active_basenames = get_blog_option( $blog_id, 'active_plugins' );
2534
+ } else {
2535
+ $active_basenames = get_option( 'active_plugins' );
2536
+ }
2537
+
2538
+ if ( is_multisite() ) {
2539
+ $network_active_basenames = get_site_option( 'active_sitewide_plugins' );
2540
+
2541
+ if ( is_array( $network_active_basenames ) && ! empty( $network_active_basenames ) ) {
2542
+ $active_basenames = array_merge( $active_basenames, $network_active_basenames );
2543
+ }
2544
+ }
2545
+
2546
+ return $active_basenames;
2547
+ }
2548
+
2549
+ /**
2550
+ * Get collection of all active plugins. Including network activated plugins.
2551
+ *
2552
+ * @author Vova Feldman (@svovaf)
2553
+ * @since 1.0.9
2554
+ *
2555
+ * @param int $blog_id Since 2.0.0
2556
+ *
2557
+ * @return array[string]array
2558
+ */
2559
+ private static function get_active_plugins( $blog_id = 0 ) {
2560
+ self::require_plugin_essentials();
2561
+
2562
+ $active_plugin = array();
2563
+ $all_plugins = fs_get_plugins();
2564
+ $active_plugins_basenames = self::get_active_plugins_basenames( $blog_id );
2565
+
2566
+ foreach ( $active_plugins_basenames as $plugin_basename ) {
2567
+ $active_plugin[ $plugin_basename ] = $all_plugins[ $plugin_basename ];
2568
+ }
2569
+
2570
+ return $active_plugin;
2571
+ }
2572
+
2573
+ /**
2574
+ * Get collection of all site active plugins for a specified blog.
2575
+ *
2576
+ * @author Vova Feldman (@svovaf)
2577
+ * @since 2.0.0
2578
+ *
2579
+ * @param int $blog_id
2580
+ *
2581
+ * @return array[string]array
2582
+ */
2583
+ private static function get_site_active_plugins( $blog_id = 0 ) {
2584
+ $active_basenames = ( is_multisite() && $blog_id > 0 ) ?
2585
+ get_blog_option( $blog_id, 'active_plugins' ) :
2586
+ get_option( 'active_plugins' );
2587
+
2588
+ $active = array();
2589
+ foreach ( $active_basenames as $basename ) {
2590
+ $active[ $basename ] = array(
2591
+ 'is_active' => true,
2592
+ 'Version' => '1.0', // Dummy version.
2593
+ 'slug' => self::get_plugin_slug( $basename ),
2594
+ );
2595
+ }
2596
+
2597
+ return $active;
2598
+ }
2599
+
2600
+ /**
2601
+ * Get collection of all plugins with their activation status for a specified blog.
2602
+ *
2603
+ * @author Vova Feldman (@svovaf)
2604
+ * @since 1.1.8
2605
+ *
2606
+ * @param int $blog_id Since 2.0.0
2607
+ *
2608
+ * @return array Key is the plugin file path and the value is an array of the plugin data.
2609
+ */
2610
+ private static function get_all_plugins( $blog_id = 0 ) {
2611
+ self::require_plugin_essentials();
2612
+
2613
+ $all_plugins = fs_get_plugins();
2614
+
2615
+ $active_plugins_basenames = self::get_active_plugins_basenames( $blog_id );
2616
+
2617
+ foreach ( $all_plugins as $basename => &$data ) {
2618
+ // By default set to inactive (next foreach update the active plugins).
2619
+ $data['is_active'] = false;
2620
+ // Enrich with plugin slug.
2621
+ $data['slug'] = self::get_plugin_slug( $basename );
2622
+ }
2623
+
2624
+ // Flag active plugins.
2625
+ foreach ( $active_plugins_basenames as $basename ) {
2626
+ if ( isset( $all_plugins[ $basename ] ) ) {
2627
+ $all_plugins[ $basename ]['is_active'] = true;
2628
+ }
2629
+ }
2630
+
2631
+ return $all_plugins;
2632
+ }
2633
+
2634
+ /**
2635
+ * Get collection of all plugins and if they are network level activated.
2636
+ *
2637
+ * @author Vova Feldman (@svovaf)
2638
+ * @since 2.0.0
2639
+ *
2640
+ * @return array Key is the plugin basename and the value is an array of the plugin data.
2641
+ */
2642
+ private static function get_network_plugins() {
2643
+ self::require_plugin_essentials();
2644
+
2645
+ $all_plugins = fs_get_plugins();
2646
+
2647
+ $network_active_basenames = is_multisite() ?
2648
+ get_site_option( 'active_sitewide_plugins' ) :
2649
+ array();
2650
+
2651
+ foreach ( $all_plugins as $basename => &$data ) {
2652
+ // By default set to inactive (next foreach update the active plugins).
2653
+ $data['is_active'] = false;
2654
+ // Enrich with plugin slug.
2655
+ $data['slug'] = self::get_plugin_slug( $basename );
2656
+ }
2657
+
2658
+ // Flag active plugins.
2659
+ foreach ( $network_active_basenames as $basename ) {
2660
+ if ( isset( $all_plugins[ $basename ] ) ) {
2661
+ $all_plugins[ $basename ]['is_active'] = true;
2662
+ }
2663
+ }
2664
+
2665
+ return $all_plugins;
2666
+ }
2667
+
2668
+ /**
2669
+ * Cached result of get_site_transient( 'update_plugins' )
2670
+ *
2671
+ * @author Vova Feldman (@svovaf)
2672
+ * @since 1.1.8
2673
+ *
2674
+ * @var object
2675
+ */
2676
+ private static $_plugins_info;
2677
+
2678
+ /**
2679
+ * Helper function to get specified plugin's slug.
2680
+ *
2681
+ * @author Vova Feldman (@svovaf)
2682
+ * @since 1.1.8
2683
+ *
2684
+ * @param $basename
2685
+ *
2686
+ * @return string
2687
+ */
2688
+ private static function get_plugin_slug( $basename ) {
2689
+ if ( ! isset( self::$_plugins_info ) ) {
2690
+ self::$_plugins_info = get_site_transient( 'update_plugins' );
2691
+ }
2692
+
2693
+ $slug = '';
2694
+
2695
+ if ( is_object( self::$_plugins_info ) ) {
2696
+ if ( isset( self::$_plugins_info->no_update ) &&
2697
+ isset( self::$_plugins_info->no_update[ $basename ] ) &&
2698
+ ! empty( self::$_plugins_info->no_update[ $basename ]->slug )
2699
+ ) {
2700
+ $slug = self::$_plugins_info->no_update[ $basename ]->slug;
2701
+ } else if ( isset( self::$_plugins_info->response ) &&
2702
+ isset( self::$_plugins_info->response[ $basename ] ) &&
2703
+ ! empty( self::$_plugins_info->response[ $basename ]->slug )
2704
+ ) {
2705
+ $slug = self::$_plugins_info->response[ $basename ]->slug;
2706
+ }
2707
+ }
2708
+
2709
+ if ( empty( $slug ) ) {
2710
+ // Try to find slug from FS data.
2711
+ $slug = self::find_slug_by_basename( $basename );
2712
+ }
2713
+
2714
+ if ( empty( $slug ) ) {
2715
+ // Fallback to plugin's folder name.
2716
+ $slug = dirname( $basename );
2717
+ }
2718
+
2719
+ return $slug;
2720
+ }
2721
+
2722
+ private static $_statics_loaded = false;
2723
+
2724
+ /**
2725
+ * Load static resources.
2726
+ *
2727
+ * @author Vova Feldman (@svovaf)
2728
+ * @since 1.0.1
2729
+ */
2730
+ private static function _load_required_static() {
2731
+ if ( self::$_statics_loaded ) {
2732
+ return;
2733
+ }
2734
+
2735
+ self::$_static_logger = FS_Logger::get_logger( WP_FS__SLUG, WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
2736
+
2737
+ self::$_static_logger->entrance();
2738
+
2739
+ self::$_accounts = FS_Options::instance( WP_FS__ACCOUNTS_OPTION_NAME, true );
2740
+
2741
+ if ( is_multisite() ) {
2742
+ $has_skipped_migration = (
2743
+ // 'id_slug_type_path_map' - was never stored on older versions, therefore, not exists on the site level.
2744
+ null === self::$_accounts->get_option( 'id_slug_type_path_map', null, false ) &&
2745
+ // 'file_slug_map' stored on the site level, so it was running an SDK version before it was integrated with MS-network.
2746
+ null !== self::$_accounts->get_option( 'file_slug_map', null, false )
2747
+ );
2748
+
2749
+ /**
2750
+ * If the file_slug_map exists on the site level but doesn't exist on the
2751
+ * network level storage, it means that we need to process the storage with migration.
2752
+ *
2753
+ * The code in this `if` scope will only be executed once and only for the first site that will execute it because once we migrate the storage data, file_slug_map will be already set in the network level storage.
2754
+ *
2755
+ * @author Vova Feldman (@svovaf)
2756
+ * @since 2.0.0
2757
+ */
2758
+ if (
2759
+ ( $has_skipped_migration && true !== self::$_accounts->get_option( 'ms_migration_complete', false, true ) ) ||
2760
+ ( null === self::$_accounts->get_option( 'file_slug_map', null, true ) &&
2761
+ null !== self::$_accounts->get_option( 'file_slug_map', null, false ) )
2762
+ ) {
2763
+ self::migrate_options_to_network();
2764
+ }
2765
+ }
2766
+
2767
+ self::$_global_admin_notices = FS_Admin_Notices::instance( 'global' );
2768
+
2769
+ if ( ! WP_FS__DEMO_MODE ) {
2770
+ add_action( ( fs_is_network_admin() ? 'network_' : '' ) . 'admin_menu', array(
2771
+ 'Freemius',
2772
+ '_add_debug_section'
2773
+ ) );
2774
+ }
2775
+
2776
+ add_action( "wp_ajax_fs_toggle_debug_mode", array( 'Freemius', '_toggle_debug_mode' ) );
2777
+
2778
+ self::add_ajax_action_static( 'get_debug_log', array( 'Freemius', '_get_debug_log' ) );
2779
+
2780
+ self::add_ajax_action_static( 'get_db_option', array( 'Freemius', '_get_db_option' ) );
2781
+
2782
+ self::add_ajax_action_static( 'set_db_option', array( 'Freemius', '_set_db_option' ) );
2783
+
2784
+ if ( 0 == did_action( 'plugins_loaded' ) ) {
2785
+ add_action( 'plugins_loaded', array( 'Freemius', '_load_textdomain' ), 1 );
2786
+ }
2787
+
2788
+ add_action( 'admin_footer', array( 'Freemius', '_enrich_ajax_url' ) );
2789
+ add_action( 'admin_footer', array( 'Freemius', '_open_support_forum_in_new_page' ) );
2790
+
2791
+
2792
+ self::$_statics_loaded = true;
2793
+ }
2794
+
2795
+ /**
2796
+ * @author Leo Fajardo (@leorw)
2797
+ *
2798
+ * @since 2.1.3
2799
+ */
2800
+ private static function migrate_options_to_network() {
2801
+ self::migrate_accounts_to_network();
2802
+
2803
+ // Migrate API options from site level to network level.
2804
+ $api_network_options = FS_Option_Manager::get_manager( WP_FS__OPTIONS_OPTION_NAME, true, true );
2805
+ $api_network_options->migrate_to_network();
2806
+
2807
+ // Migrate API cache to network level storage.
2808
+ FS_Cache_Manager::get_manager( WP_FS__API_CACHE_OPTION_NAME )->migrate_to_network();
2809
+
2810
+ self::$_accounts->set_option( 'ms_migration_complete', true, true );
2811
+ }
2812
+
2813
+ #----------------------------------------------------------------------------------
2814
+ #region Localization
2815
+ #----------------------------------------------------------------------------------
2816
+
2817
+ /**
2818
+ * Load framework's text domain.
2819
+ *
2820
+ * @author Vova Feldman (@svovaf)
2821
+ * @since 1.2.1
2822
+ */
2823
+ static function _load_textdomain() {
2824
+ if ( ! is_admin() ) {
2825
+ return;
2826
+ }
2827
+
2828
+ global $fs_active_plugins;
2829
+
2830
+ // Works both for plugins and themes.
2831
+ load_plugin_textdomain(
2832
+ 'freemius',
2833
+ false,
2834
+ $fs_active_plugins->newest->sdk_path . '/languages/'
2835
+ );
2836
+ }
2837
+
2838
+ #endregion
2839
+
2840
+ #----------------------------------------------------------------------------------
2841
+ #region Debugging
2842
+ #----------------------------------------------------------------------------------
2843
+
2844
+ /**
2845
+ * @author Vova Feldman (@svovaf)
2846
+ * @since 1.0.8
2847
+ */
2848
+ static function _add_debug_section() {
2849
+ if ( ! is_super_admin() ) {
2850
+ // Add debug page only for super-admins.
2851
+ return;
2852
+ }
2853
+
2854
+ self::$_static_logger->entrance();
2855
+
2856
+ $title = sprintf( '%s [v.%s]', fs_text_inline( 'Freemius Debug' ), WP_FS__SDK_VERSION );
2857
+
2858
+ if ( WP_FS__DEV_MODE ) {
2859
+ // Add top-level debug menu item.
2860
+ $hook = FS_Admin_Menu_Manager::add_page(
2861
+ $title,
2862
+ $title,
2863
+ 'manage_options',
2864
+ 'freemius',
2865
+ array( 'Freemius', '_debug_page_render' )
2866
+ );
2867
+ } else {
2868
+ // Add hidden debug page.
2869
+ $hook = FS_Admin_Menu_Manager::add_subpage(
2870
+ null,
2871
+ $title,
2872
+ $title,
2873
+ 'manage_options',
2874
+ 'freemius',
2875
+ array( 'Freemius', '_debug_page_render' )
2876
+ );
2877
+ }
2878
+
2879
+ if ( ! empty( $hook ) ) {
2880
+ add_action( "load-$hook", array( 'Freemius', '_debug_page_actions' ) );
2881
+ }
2882
+ }
2883
+
2884
+ /**
2885
+ * @author Vova Feldman (@svovaf)
2886
+ * @since 1.1.7.3
2887
+ */
2888
+ static function _toggle_debug_mode() {
2889
+ $is_on = fs_request_get( 'is_on', false, 'post' );
2890
+
2891
+ if ( fs_request_is_post() && in_array( $is_on, array( 0, 1 ) ) ) {
2892
+ update_option( 'fs_debug_mode', $is_on );
2893
+
2894
+ // Turn on/off storage logging.
2895
+ FS_Logger::_set_storage_logging( ( 1 == $is_on ) );
2896
+ }
2897
+
2898
+ exit;
2899
+ }
2900
+
2901
+ /**
2902
+ * @author Vova Feldman (@svovaf)
2903
+ * @since 1.2.1.6
2904
+ */
2905
+ static function _get_debug_log() {
2906
+ $logs = FS_Logger::load_db_logs(
2907
+ fs_request_get( 'filters', false, 'post' ),
2908
+ ! empty( $_POST['limit'] ) && is_numeric( $_POST['limit'] ) ? $_POST['limit'] : 200,
2909
+ ! empty( $_POST['offset'] ) && is_numeric( $_POST['offset'] ) ? $_POST['offset'] : 0
2910
+ );
2911
+
2912
+ self::shoot_ajax_success( $logs );
2913
+ }
2914
+
2915
+ /**
2916
+ * @author Vova Feldman (@svovaf)
2917
+ * @since 1.2.1.7
2918
+ */
2919
+ static function _get_db_option() {
2920
+ $option_name = fs_request_get( 'option_name' );
2921
+
2922
+ $value = get_option( $option_name );
2923
+
2924
+ $result = array(
2925
+ 'name' => $option_name,
2926
+ );
2927
+
2928
+ if ( false !== $value ) {
2929
+ if ( ! is_string( $value ) ) {
2930
+ $value = json_encode( $value );
2931
+ }
2932
+
2933
+ $result['value'] = $value;
2934
+ }
2935
+
2936
+ self::shoot_ajax_success( $result );
2937
+ }
2938
+
2939
+ /**
2940
+ * @author Vova Feldman (@svovaf)
2941
+ * @since 1.2.1.7
2942
+ */
2943
+ static function _set_db_option() {
2944
+ $option_name = fs_request_get( 'option_name' );
2945
+ $option_value = fs_request_get( 'option_value' );
2946
+
2947
+ if ( ! empty( $option_value ) ) {
2948
+ update_option( $option_name, $option_value );
2949
+ }
2950
+
2951
+ self::shoot_ajax_success();
2952
+ }
2953
+
2954
+ /**
2955
+ * @author Vova Feldman (@svovaf)
2956
+ * @since 1.0.8
2957
+ */
2958
+ static function _debug_page_actions() {
2959
+ self::_clean_admin_content_section();
2960
+
2961
+ if ( fs_request_is_action( 'restart_freemius' ) ) {
2962
+ check_admin_referer( 'restart_freemius' );
2963
+
2964
+ if ( ! is_multisite() ) {
2965
+ // Clear accounts data.
2966
+ self::$_accounts->clear( null, true );
2967
+ } else {
2968
+ $sites = self::get_sites();
2969
+ foreach ( $sites as $site ) {
2970
+ $blog_id = self::get_site_blog_id( $site );
2971
+ self::$_accounts->clear( $blog_id, true );
2972
+ }
2973
+
2974
+ // Clear network level storage.
2975
+ self::$_accounts->clear( true, true );
2976
+ }
2977
+
2978
+ // Clear SDK reference cache.
2979
+ delete_option( 'fs_active_plugins' );
2980
+ } else if ( fs_request_is_action( 'clear_updates_data' ) ) {
2981
+ check_admin_referer( 'clear_updates_data' );
2982
+
2983
+ if ( ! is_multisite() ) {
2984
+ set_site_transient( 'update_plugins', null );
2985
+ set_site_transient( 'update_themes', null );
2986
+ } else {
2987
+ $current_blog_id = get_current_blog_id();
2988
+
2989
+ $sites = self::get_sites();
2990
+ foreach ( $sites as $site ) {
2991
+ switch_to_blog( self::get_site_blog_id( $site ) );
2992
+
2993
+ set_site_transient( 'update_plugins', null );
2994
+ set_site_transient( 'update_themes', null );
2995
+ }
2996
+
2997
+ switch_to_blog( $current_blog_id );
2998
+ }
2999
+ } else if ( fs_request_is_action( 'simulate_trial' ) ) {
3000
+ check_admin_referer( 'simulate_trial' );
3001
+
3002
+ $fs = freemius( fs_request_get( 'module_id' ) );
3003
+
3004
+ // Update SDK install to at least 24 hours before.
3005
+ $fs->_storage->install_timestamp = ( time() - WP_FS__TIME_24_HOURS_IN_SEC );
3006
+ // Unset the trial shown timestamp.
3007
+ unset( $fs->_storage->trial_promotion_shown );
3008
+ } else if ( fs_request_is_action( 'simulate_network_upgrade' ) ) {
3009
+ check_admin_referer( 'simulate_network_upgrade' );
3010
+
3011
+ $fs = freemius( fs_request_get( 'module_id' ) );
3012
+
3013
+ self::set_network_upgrade_mode( $fs->_storage );
3014
+ } else if ( fs_request_is_action( 'delete_install' ) ) {
3015
+ check_admin_referer( 'delete_install' );
3016
+
3017
+ self::_delete_site_by_slug(
3018
+ fs_request_get( 'slug' ),
3019
+ fs_request_get( 'module_type' ),
3020
+ true,
3021
+ fs_request_get( 'blog_id', null )
3022
+ );
3023
+ } else if ( fs_request_is_action( 'delete_user' ) ) {
3024
+ check_admin_referer( 'delete_user' );
3025
+
3026
+ self::delete_user( fs_request_get( 'user_id' ) );
3027
+ } else if ( fs_request_is_action( 'download_logs' ) ) {
3028
+ check_admin_referer( 'download_logs' );
3029
+
3030
+ $download_url = FS_Logger::download_db_logs(
3031
+ fs_request_get( 'filters', false, 'post' )
3032
+ );
3033
+
3034
+ if ( false === $download_url ) {
3035
+ 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.' );
3036
+ }
3037
+
3038
+ fs_redirect( $download_url );
3039
+ } else if ( fs_request_is_action( 'migrate_options_to_network' ) ) {
3040
+ check_admin_referer( 'migrate_options_to_network' );
3041
+
3042
+ self::migrate_options_to_network();
3043
+ }
3044
+ }
3045
+
3046
+ /**
3047
+ * @author Vova Feldman (@svovaf)
3048
+ * @since 1.0.8
3049
+ */
3050
+ static function _debug_page_render() {
3051
+ self::$_static_logger->entrance();
3052
+
3053
+ if ( ! is_multisite() ) {
3054
+ $all_plugins_installs = self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN );
3055
+ $all_themes_installs = self::get_all_sites( WP_FS__MODULE_TYPE_THEME );
3056
+ } else {
3057
+ $sites = self::get_sites();
3058
+
3059
+ $all_plugins_installs = array();
3060
+ $all_themes_installs = array();
3061
+
3062
+ foreach ( $sites as $site ) {
3063
+ $blog_id = self::get_site_blog_id( $site );
3064
+
3065
+ $plugins_installs = self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN, $blog_id );
3066
+
3067
+ foreach ( $plugins_installs as $slug => $install ) {
3068
+ if ( ! isset( $all_plugins_installs[ $slug ] ) ) {
3069
+ $all_plugins_installs[ $slug ] = array();
3070
+ }
3071
+
3072
+ $install->blog_id = $blog_id;
3073
+
3074
+ $all_plugins_installs[ $slug ][] = $install;
3075
+ }
3076
+
3077
+ $themes_installs = self::get_all_sites( WP_FS__MODULE_TYPE_THEME, $blog_id );
3078
+
3079
+ foreach ( $themes_installs as $slug => $install ) {
3080
+ if ( ! isset( $all_themes_installs[ $slug ] ) ) {
3081
+ $all_themes_installs[ $slug ] = array();
3082
+ }
3083
+
3084
+ $install->blog_id = $blog_id;
3085
+
3086
+ $all_themes_installs[ $slug ][] = $install;
3087
+ }
3088
+ }
3089
+ }
3090
+
3091
+ $licenses_by_module_type = self::get_all_licenses_by_module_type();
3092
+
3093
+ $vars = array(
3094
+ 'plugin_sites' => $all_plugins_installs,
3095
+ 'theme_sites' => $all_themes_installs,
3096
+ 'users' => self::get_all_users(),
3097
+ 'addons' => self::get_all_addons(),
3098
+ 'account_addons' => self::get_all_account_addons(),
3099
+ 'plugin_licenses' => $licenses_by_module_type[ WP_FS__MODULE_TYPE_PLUGIN ],
3100
+ 'theme_licenses' => $licenses_by_module_type[ WP_FS__MODULE_TYPE_THEME ]
3101
+ );
3102
+
3103
+ fs_enqueue_local_style( 'fs_debug', '/admin/debug.css' );
3104
+ fs_require_once_template( 'debug.php', $vars );
3105
+ }
3106
+
3107
+ #endregion
3108
+
3109
+ #----------------------------------------------------------------------------------
3110
+ #region Connectivity Issues
3111
+ #----------------------------------------------------------------------------------
3112
+
3113
+ /**
3114
+ * Check if Freemius should be turned on for the current plugin install.
3115
+ *
3116
+ * Note:
3117
+ * $this->_is_on is updated in has_api_connectivity()
3118
+ *
3119
+ * @author Vova Feldman (@svovaf)
3120
+ * @since 1.0.9
3121
+ *
3122
+ * @return bool
3123
+ */
3124
+ function is_on() {
3125
+ self::$_static_logger->entrance();
3126
+
3127
+ if ( isset( $this->_is_on ) ) {
3128
+ return $this->_is_on;
3129
+ }
3130
+
3131
+ // If already installed or pending then sure it's on :)
3132
+ if ( $this->is_registered() || $this->is_pending_activation() ) {
3133
+ $this->_is_on = true;
3134
+
3135
+ return true;
3136
+ }
3137
+
3138
+ return false;
3139
+ }
3140
+
3141
+ /**
3142
+ * @author Vova Feldman (@svovaf)
3143
+ * @since 1.1.7.3
3144
+ *
3145
+ * @param bool $flush_if_no_connectivity
3146
+ *
3147
+ * @return bool
3148
+ */
3149
+ private function should_run_connectivity_test( $flush_if_no_connectivity = false ) {
3150
+ if ( ! isset( $this->_storage->connectivity_test ) ) {
3151
+ // Connectivity test was never executed, or cache was cleared.
3152
+ return true;
3153
+ }
3154
+
3155
+ if ( WP_FS__PING_API_ON_IP_OR_HOST_CHANGES ) {
3156
+ if ( WP_FS__IS_HTTP_REQUEST ) {
3157
+ if ( $_SERVER['HTTP_HOST'] != $this->_storage->connectivity_test['host'] ) {
3158
+ // Domain changed.
3159
+ return true;
3160
+ }
3161
+
3162
+ if ( WP_FS__REMOTE_ADDR != $this->_storage->connectivity_test['server_ip'] ) {
3163
+ // Server IP changed.
3164
+ return true;
3165
+ }
3166
+ }
3167
+ }
3168
+
3169
+ if ( $this->_storage->connectivity_test['is_connected'] &&
3170
+ $this->_storage->connectivity_test['is_active']
3171
+ ) {
3172
+ // API connected and Freemius is active - no need to run connectivity check.
3173
+ return false;
3174
+ }
3175
+
3176
+ if ( $flush_if_no_connectivity ) {
3177
+ /**
3178
+ * If explicitly asked to flush when no connectivity - do it only
3179
+ * if at least 10 sec passed from the last API connectivity test.
3180
+ */
3181
+ return ( isset( $this->_storage->connectivity_test['timestamp'] ) &&
3182
+ ( WP_FS__SCRIPT_START_TIME - $this->_storage->connectivity_test['timestamp'] ) > 10 );
3183
+ }
3184
+
3185
+ /**
3186
+ * @since 1.1.7 Don't check for connectivity on plugin downgrade.
3187
+ */
3188
+ $version = $this->get_plugin_version();
3189
+ if ( version_compare( $version, $this->_storage->connectivity_test['version'], '>' ) ) {
3190
+ // If it's a plugin version upgrade and Freemius is off or no connectivity, run connectivity test.
3191
+ return true;
3192
+ }
3193
+
3194
+ return false;
3195
+ }
3196
+
3197
+ /**
3198
+ * @author Vova Feldman (@svovaf)
3199
+ * @since 1.1.7.4
3200
+ *
3201
+ * @param int|null $blog_id Since 2.0.0.
3202
+ * @param bool $is_gdpr_test Since 2.0.2. Perform only the GDPR test.
3203
+ *
3204
+ * @return object|false
3205
+ */
3206
+ private function ping( $blog_id = null, $is_gdpr_test = false ) {
3207
+ if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY ) {
3208
+ return false;
3209
+ }
3210
+
3211
+ $version = $this->get_plugin_version();
3212
+
3213
+ $is_update = $this->apply_filters( 'is_plugin_update', $this->is_plugin_update() );
3214
+
3215
+ return $this->get_api_plugin_scope()->ping(
3216
+ $this->get_anonymous_id( $blog_id ),
3217
+ array(
3218
+ 'is_update' => json_encode( $is_update ),
3219
+ 'version' => $version,
3220
+ 'sdk' => $this->version,
3221
+ 'is_admin' => json_encode( is_admin() ),
3222
+ 'is_ajax' => json_encode( self::is_ajax() ),
3223
+ 'is_cron' => json_encode( self::is_cron() ),
3224
+ 'is_gdpr_test' => $is_gdpr_test,
3225
+ 'is_http' => json_encode( WP_FS__IS_HTTP_REQUEST ),
3226
+ )
3227
+ );
3228
+ }
3229
+
3230
+ /**
3231
+ * Check if there's any connectivity issue to Freemius API.
3232
+ *
3233
+ * @author Vova Feldman (@svovaf)
3234
+ * @since 1.0.9
3235
+ *
3236
+ * @param bool $flush_if_no_connectivity
3237
+ *
3238
+ * @return bool
3239
+ */
3240
+ function has_api_connectivity( $flush_if_no_connectivity = false ) {
3241
+ $this->_logger->entrance();
3242
+
3243
+ if ( isset( $this->_has_api_connection ) && ( $this->_has_api_connection || ! $flush_if_no_connectivity ) ) {
3244
+ return $this->_has_api_connection;
3245
+ }
3246
+
3247
+ if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY &&
3248
+ isset( $this->_storage->connectivity_test ) &&
3249
+ true === $this->_storage->connectivity_test['is_connected']
3250
+ ) {
3251
+ unset( $this->_storage->connectivity_test );
3252
+ }
3253
+
3254
+ if ( ! $this->should_run_connectivity_test( $flush_if_no_connectivity ) ) {
3255
+ $this->_has_api_connection = $this->_storage->connectivity_test['is_connected'];
3256
+ /**
3257
+ * @since 1.1.6 During dev mode, if there's connectivity - turn Freemius on regardless the configuration.
3258
+ *
3259
+ * @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.
3260
+ */
3261
+ $this->_is_on = $this->_storage->connectivity_test['is_active'] ||
3262
+ $this->is_premium() ||
3263
+ ( WP_FS__DEV_MODE && $this->_has_api_connection && ! WP_FS__SIMULATE_FREEMIUS_OFF );
3264
+
3265
+ return $this->_has_api_connection;
3266
+ }
3267
+
3268
+ $pong = $this->ping();
3269
+ $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
3270
+
3271
+ if ( ! $is_connected ) {
3272
+ // API failure.
3273
+ $this->_add_connectivity_issue_message( $pong );
3274
+ }
3275
+
3276
+ if ( $is_connected ) {
3277
+ FS_GDPR_Manager::instance()->store_is_required( $pong->is_gdpr_required );
3278
+ }
3279
+
3280
+ $this->store_connectivity_info( $pong, $is_connected );
3281
+
3282
+ return $this->_has_api_connection;
3283
+ }
3284
+
3285
+ /**
3286
+ * @author Vova Feldman (@svovaf)
3287
+ * @since 1.1.7.4
3288
+ *
3289
+ * @param object $pong
3290
+ * @param bool $is_connected
3291
+ */
3292
+ private function store_connectivity_info( $pong, $is_connected ) {
3293
+ $this->_logger->entrance();
3294
+
3295
+ $version = $this->get_plugin_version();
3296
+
3297
+ if ( ! $is_connected || WP_FS__SIMULATE_FREEMIUS_OFF ) {
3298
+ $is_active = false;
3299
+ } else {
3300
+ $is_active = ( isset( $pong->is_active ) && true == $pong->is_active );
3301
+ }
3302
+
3303
+ $is_active = $this->apply_filters(
3304
+ 'is_on',
3305
+ $is_active,
3306
+ $this->is_plugin_update(),
3307
+ $version
3308
+ );
3309
+
3310
+ $this->_storage->connectivity_test = array(
3311
+ 'is_connected' => $is_connected,
3312
+ 'host' => $_SERVER['HTTP_HOST'],
3313
+ 'server_ip' => WP_FS__REMOTE_ADDR,
3314
+ 'is_active' => $is_active,
3315
+ 'timestamp' => WP_FS__SCRIPT_START_TIME,
3316
+ // Last version with connectivity attempt.
3317
+ 'version' => $version,
3318
+ );
3319
+
3320
+ $this->_has_api_connection = $is_connected;
3321
+ $this->_is_on = $is_active || ( WP_FS__DEV_MODE && $is_connected && ! WP_FS__SIMULATE_FREEMIUS_OFF );
3322
+ }
3323
+
3324
+ /**
3325
+ * Force turning Freemius on.
3326
+ *
3327
+ * @author Vova Feldman (@svovaf)
3328
+ * @since 1.1.8.1
3329
+ *
3330
+ * @return bool TRUE if successfully turned on.
3331
+ */
3332
+ private function turn_on() {
3333
+ $this->_logger->entrance();
3334
+
3335
+ if ( $this->is_on() || ! isset( $this->_storage->connectivity_test['is_active'] ) ) {
3336
+ return false;
3337
+ }
3338
+
3339
+ $updated_connectivity = $this->_storage->connectivity_test;
3340
+ $updated_connectivity['is_active'] = true;
3341
+ $updated_connectivity['timestamp'] = WP_FS__SCRIPT_START_TIME;
3342
+ $this->_storage->connectivity_test = $updated_connectivity;
3343
+
3344
+ $this->_is_on = true;
3345
+
3346
+ return true;
3347
+ }
3348
+
3349
+ /**
3350
+ * Anonymous and unique site identifier (Hash).
3351
+ *
3352
+ * @author Vova Feldman (@svovaf)
3353
+ * @since 1.1.0
3354
+ *
3355
+ * @param null|int $blog_id Since 2.0.0
3356
+ *
3357
+ * @return string
3358
+ */
3359
+ function get_anonymous_id( $blog_id = null ) {
3360
+ $unique_id = self::$_accounts->get_option( 'unique_id', null, $blog_id );
3361
+
3362
+ if ( empty( $unique_id ) || ! is_string( $unique_id ) ) {
3363
+ $key = fs_strip_url_protocol( get_site_url( $blog_id ) );
3364
+
3365
+ $secure_auth = SECURE_AUTH_KEY;
3366
+ if ( empty( $secure_auth ) || false !== strpos( $secure_auth, ' ' ) ) {
3367
+ // Protect against default auth key.
3368
+ $secure_auth = md5( microtime() );
3369
+ }
3370
+
3371
+ /**
3372
+ * Base the unique identifier on the WP secure authentication key. Which
3373
+ * turns the key into a secret anonymous identifier. This will help us
3374
+ * to avoid duplicate installs generation on the backend upon opt-in.
3375
+ *
3376
+ * @author Vova Feldman (@svovaf)
3377
+ * @since 1.2.3
3378
+ */
3379
+ $unique_id = md5( $key . $secure_auth );
3380
+
3381
+ self::$_accounts->set_option( 'unique_id', $unique_id, true, $blog_id );
3382
+ }
3383
+
3384
+ $this->_logger->departure( $unique_id );
3385
+
3386
+ return $unique_id;
3387
+ }
3388
+
3389
+ /**
3390
+ * @author Vova Feldman (@svovaf)
3391
+ * @since 1.1.7.4
3392
+ *
3393
+ * @return \WP_User
3394
+ */
3395
+ static function _get_current_wp_user() {
3396
+ self::require_pluggable_essentials();
3397
+ self::wp_cookie_constants();
3398
+
3399
+ return wp_get_current_user();
3400
+ }
3401
+
3402
+ /**
3403
+ * Define cookie constants which are required by Freemius::_get_current_wp_user() since
3404
+ * it uses wp_get_current_user() which needs the cookie constants set. When a plugin
3405
+ * is network activated the cookie constants are only configured after the network
3406
+ * plugins activation, therefore, if we don't define those constants WP will throw
3407
+ * PHP warnings/notices.
3408
+ *
3409
+ * @author Vova Feldman (@svovaf)
3410
+ * @since 2.1.1
3411
+ */
3412
+ private static function wp_cookie_constants() {
3413
+ if ( defined( 'LOGGED_IN_COOKIE' ) &&
3414
+ ( defined( 'AUTH_COOKIE' ) || defined( 'SECURE_AUTH_COOKIE' ) )
3415
+ ) {
3416
+ return;
3417
+ }
3418
+
3419
+ /**
3420
+ * Used to guarantee unique hash cookies
3421
+ *
3422
+ * @since 1.5.0
3423
+ */
3424
+ if ( ! defined( 'COOKIEHASH' ) ) {
3425
+ $siteurl = get_site_option( 'siteurl' );
3426
+ if ( $siteurl ) {
3427
+ define( 'COOKIEHASH', md5( $siteurl ) );
3428
+ } else {
3429
+ define( 'COOKIEHASH', '' );
3430
+ }
3431
+ }
3432
+
3433
+ if ( ! defined( 'LOGGED_IN_COOKIE' ) ) {
3434
+ define( 'LOGGED_IN_COOKIE', 'wordpress_logged_in_' . COOKIEHASH );
3435
+ }
3436
+
3437
+ /**
3438
+ * @since 2.5.0
3439
+ */
3440
+ if ( ! defined( 'AUTH_COOKIE' ) ) {
3441
+ define( 'AUTH_COOKIE', 'wordpress_' . COOKIEHASH );
3442
+ }
3443
+
3444
+ /**
3445
+ * @since 2.6.0
3446
+ */
3447
+ if ( ! defined( 'SECURE_AUTH_COOKIE' ) ) {
3448
+ define( 'SECURE_AUTH_COOKIE', 'wordpress_sec_' . COOKIEHASH );
3449
+ }
3450
+ }
3451
+
3452
+ /**
3453
+ * @author Vova Feldman (@svovaf)
3454
+ * @since 2.1.0
3455
+ *
3456
+ * @return int
3457
+ */
3458
+ static function get_current_wp_user_id() {
3459
+ $wp_user = self::_get_current_wp_user();
3460
+
3461
+ return $wp_user->ID;
3462
+ }
3463
+
3464
+ /**
3465
+ * @author Vova Feldman (@svovaf)
3466
+ * @since 1.2.1.7
3467
+ *
3468
+ * @param string $email
3469
+ *
3470
+ * @return bool
3471
+ */
3472
+ static function is_valid_email( $email ) {
3473
+ if ( false === filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
3474
+ return false;
3475
+ }
3476
+
3477
+ $parts = explode( '@', $email );
3478
+
3479
+ if ( 2 !== count( $parts ) || empty( $parts[1] ) ) {
3480
+ return false;
3481
+ }
3482
+
3483
+ $blacklist = array(
3484
+ 'admin.',
3485
+ 'webmaster.',
3486
+ 'localhost.',
3487
+ 'dev.',
3488
+ 'development.',
3489
+ 'test.',
3490
+ 'stage.',
3491
+ 'staging.',
3492
+ );
3493
+
3494
+ // Make sure domain is not one of the blacklisted.
3495
+ foreach ( $blacklist as $invalid ) {
3496
+ if ( 0 === strpos( $parts[1], $invalid ) ) {
3497
+ return false;
3498
+ }
3499
+ }
3500
+
3501
+ // Get the UTF encoded domain name.
3502
+ $domain = idn_to_ascii( $parts[1] ) . '.';
3503
+
3504
+ return ( checkdnsrr( $domain, 'MX' ) || checkdnsrr( $domain, 'A' ) );
3505
+ }
3506
+
3507
+ /**
3508
+ * Generate API connectivity issue message.
3509
+ *
3510
+ * @author Vova Feldman (@svovaf)
3511
+ * @since 1.0.9
3512
+ *
3513
+ * @param mixed $api_result
3514
+ * @param bool $is_first_failure
3515
+ */
3516
+ function _add_connectivity_issue_message( $api_result, $is_first_failure = true ) {
3517
+ if ( ! $this->is_premium() && $this->_enable_anonymous ) {
3518
+ // Don't add message if it's the free version and can run anonymously.
3519
+ return;
3520
+ }
3521
+
3522
+ if ( ! function_exists( 'wp_nonce_url' ) ) {
3523
+ require_once ABSPATH . 'wp-includes/functions.php';
3524
+ }
3525
+
3526
+ $current_user = self::_get_current_wp_user();
3527
+ // $admin_email = get_option( 'admin_email' );
3528
+ $admin_email = $current_user->user_email;
3529
+
3530
+ // Aliases.
3531
+ $deactivate_plugin_title = $this->esc_html_inline( 'That\'s exhausting, please deactivate', 'deactivate-plugin-title' );
3532
+ $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' );
3533
+ $install_previous_title = $this->esc_html_inline( 'Let\'s try your previous version', 'install-previous-title' );
3534
+ $install_previous_desc = $this->esc_html_inline( 'Uninstall this version and install the previous one.', 'install-previous-desc' );
3535
+ $fix_issue_title = $this->esc_html_inline( 'Yes - I\'m giving you a chance to fix it', 'fix-issue-title' );
3536
+ $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' );
3537
+ /* translators: %s: product title (e.g. "Awesome Plugin" requires an access to...) */
3538
+ $x_requires_access_to_api = $this->esc_html_inline( '%s requires an access to our API.', 'x-requires-access-to-api' );
3539
+ $sysadmin_title = $this->esc_html_inline( 'I\'m a system administrator', 'sysadmin-title' );
3540
+ $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' );
3541
+
3542
+ $message = false;
3543
+ if ( is_object( $api_result ) &&
3544
+ isset( $api_result->error ) &&
3545
+ isset( $api_result->error->code )
3546
+ ) {
3547
+ switch ( $api_result->error->code ) {
3548
+ case 'curl_missing':
3549
+ $missing_methods = '';
3550
+ if ( is_array( $api_result->missing_methods ) &&
3551
+ ! empty( $api_result->missing_methods )
3552
+ ) {
3553
+ foreach ( $api_result->missing_methods as $m ) {
3554
+ if ( 'curl_version' === $m ) {
3555
+ continue;
3556
+ }
3557
+
3558
+ if ( ! empty( $missing_methods ) ) {
3559
+ $missing_methods .= ', ';
3560
+ }
3561
+
3562
+ $missing_methods .= sprintf( '<code>%s</code>', $m );
3563
+ }
3564
+
3565
+ if ( ! empty( $missing_methods ) ) {
3566
+ $missing_methods = sprintf(
3567
+ '<br><br><b>%s</b> %s',
3568
+ $this->esc_html_inline( 'Disabled method(s):', 'curl-disabled-methods' ),
3569
+ $missing_methods
3570
+ );
3571
+ }
3572
+ }
3573
+
3574
+ $message = sprintf(
3575
+ $x_requires_access_to_api . ' ' .
3576
+ $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' ) . ' ' .
3577
+ $missing_methods .
3578
+ ' %s',
3579
+ '<b>' . $this->get_plugin_name() . '</b>',
3580
+ sprintf(
3581
+ '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
3582
+ sprintf(
3583
+ '<a class="fs-resolve" data-type="curl" href="#"><b>%s</b></a>%s',
3584
+ $this->get_text_inline( 'I don\'t know what is cURL or how to install it, help me!', 'curl-missing-no-clue-title' ),
3585
+ ' - ' . sprintf(
3586
+ $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' ),
3587
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
3588
+ )
3589
+ ),
3590
+ sprintf(
3591
+ '<b>%s</b> - %s',
3592
+ $sysadmin_title,
3593
+ 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 ) ) )
3594
+ ),
3595
+ sprintf(
3596
+ '<a href="%s"><b>%s</b></a> - %s',
3597
+ 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 ),
3598
+ $deactivate_plugin_title,
3599
+ $deactivate_plugin_desc
3600
+ )
3601
+ )
3602
+ );
3603
+ break;
3604
+ case 'cloudflare_ddos_protection':
3605
+ $message = sprintf(
3606
+ $x_requires_access_to_api . ' ' .
3607
+ $this->esc_html_inline( 'From unknown reason, CloudFlare, the firewall we use, blocks the connection.', 'cloudflare-blocks-connection-message' ) . ' ' .
3608
+ $happy_to_resolve_issue_asap .
3609
+ ' %s',
3610
+ '<b>' . $this->get_plugin_name() . '</b>',
3611
+ sprintf(
3612
+ '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
3613
+ sprintf(
3614
+ '<a class="fs-resolve" data-type="cloudflare" href="#"><b>%s</b></a>%s',
3615
+ $fix_issue_title,
3616
+ ' - ' . sprintf(
3617
+ $fix_issue_desc,
3618
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
3619
+ )
3620
+ ),
3621
+ sprintf(
3622
+ '<a href="%s" target="_blank"><b>%s</b></a> - %s',
3623
+ sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),
3624
+ $install_previous_title,
3625
+ $install_previous_desc
3626
+ ),
3627
+ sprintf(
3628
+ '<a href="%s"><b>%s</b></a> - %s',
3629
+ 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 ),
3630
+ $deactivate_plugin_title,
3631
+ $deactivate_plugin_desc
3632
+ )
3633
+ )
3634
+ );
3635
+ break;
3636
+ case 'squid_cache_block':
3637
+ $message = sprintf(
3638
+ $x_requires_access_to_api . ' ' .
3639
+ $this->esc_html_inline( 'It looks like your server is using Squid ACL (access control lists), which blocks the connection.', 'squid-blocks-connection-message' ) .
3640
+ ' %s',
3641
+ '<b>' . $this->get_plugin_name() . '</b>',
3642
+ sprintf(
3643
+ '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
3644
+ sprintf(
3645
+ '<a class="fs-resolve" data-type="squid" href="#"><b>%s</b></a> - %s',
3646
+ $this->esc_html_inline( 'I don\'t know what is Squid or ACL, help me!', 'squid-no-clue-title' ),
3647
+ sprintf(
3648
+ $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' ),
3649
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
3650
+ )
3651
+ ),
3652
+ sprintf(
3653
+ '<b>%s</b> - %s',
3654
+ $sysadmin_title,
3655
+ sprintf(
3656
+ $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' ),
3657
+ // We use a filter since the plugin might require additional API connectivity.
3658
+ '<b>' . implode( ', ', $this->apply_filters( 'api_domains', array(
3659
+ 'api.freemius.com',
3660
+ 'wp.freemius.com'
3661
+ ) ) ) . '</b>',
3662
+ $this->_module_type
3663
+ )
3664
+ ),
3665
+ sprintf(
3666
+ '<a href="%s"><b>%s</b></a> - %s',
3667
+ 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 ),
3668
+ $deactivate_plugin_title,
3669
+ $deactivate_plugin_desc
3670
+ )
3671
+ )
3672
+ );
3673
+ break;
3674
+ // default:
3675
+ // $message = $this->get_text_inline( 'connectivity-test-fails-message' );
3676
+ // break;
3677
+ }
3678
+ }
3679
+
3680
+ $message_id = 'failed_connect_api';
3681
+ $type = 'error';
3682
+
3683
+ $connectivity_test_fails_message = $this->esc_html_inline( 'From unknown reason, the API connectivity test failed.', 'connectivity-test-fails-message' );
3684
+
3685
+ if ( false === $message ) {
3686
+ if ( $is_first_failure ) {
3687
+ // First attempt failed.
3688
+ $message = sprintf(
3689
+ $x_requires_access_to_api . ' ' .
3690
+ $connectivity_test_fails_message . ' ' .
3691
+ $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>' .
3692
+ '%s',
3693
+ '<b>' . $this->get_plugin_name() . '</b>',
3694
+ sprintf(
3695
+ '<div id="fs_firewall_issue_options">%s %s</div>',
3696
+ sprintf(
3697
+ '<a class="button button-primary fs-resolve" data-type="retry_ping" href="#">%s</a>',
3698
+ $this->get_text_inline( 'Yes - do your thing', 'yes-do-your-thing' )
3699
+ ),
3700
+ sprintf(
3701
+ '<a href="%s" class="button">%s</a>',
3702
+ 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 ),
3703
+ $this->get_text_inline( 'No - just deactivate', 'no-deactivate' )
3704
+ )
3705
+ )
3706
+ );
3707
+
3708
+ $message_id = 'failed_connect_api_first';
3709
+ $type = 'promotion';
3710
+ } else {
3711
+ // Second connectivity attempt failed.
3712
+ $message = sprintf(
3713
+ $x_requires_access_to_api . ' ' .
3714
+ $connectivity_test_fails_message . ' ' .
3715
+ $happy_to_resolve_issue_asap .
3716
+ ' %s',
3717
+ '<b>' . $this->get_plugin_name() . '</b>',
3718
+ sprintf(
3719
+ '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
3720
+ sprintf(
3721
+ '<a class="fs-resolve" data-type="general" href="#"><b>%s</b></a>%s',
3722
+ $fix_issue_title,
3723
+ ' - ' . sprintf(
3724
+ $fix_issue_desc,
3725
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
3726
+ )
3727
+ ),
3728
+ sprintf(
3729
+ '<a href="%s" target="_blank"><b>%s</b></a> - %s',
3730
+ sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),
3731
+ $install_previous_title,
3732
+ $install_previous_desc
3733
+ ),
3734
+ sprintf(
3735
+ '<a href="%s"><b>%s</b></a> - %s',
3736
+ 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 ),
3737
+ $deactivate_plugin_title,
3738
+ $deactivate_plugin_desc
3739
+ )
3740
+ )
3741
+ );
3742
+ }
3743
+ }
3744
+
3745
+ $this->_admin_notices->add_sticky(
3746
+ $message,
3747
+ $message_id,
3748
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
3749
+ $type
3750
+ );
3751
+ }
3752
+
3753
+ /**
3754
+ * Handle user request to resolve connectivity issue.
3755
+ * This method will send an email to Freemius API technical staff for resolution.
3756
+ * The email will contain server's info and installed plugins (might be caching issue).
3757
+ *
3758
+ * @author Vova Feldman (@svovaf)
3759
+ * @since 1.0.9
3760
+ */
3761
+ function _email_about_firewall_issue() {
3762
+ $this->_admin_notices->remove_sticky( 'failed_connect_api' );
3763
+
3764
+ $pong = $this->ping();
3765
+
3766
+ $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
3767
+
3768
+ if ( $is_connected ) {
3769
+ FS_GDPR_Manager::instance()->store_is_required( $pong->is_gdpr_required );
3770
+
3771
+ $this->store_connectivity_info( $pong, $is_connected );
3772
+
3773
+ echo $this->get_after_plugin_activation_redirect_url();
3774
+ exit;
3775
+ }
3776
+
3777
+ $current_user = self::_get_current_wp_user();
3778
+ $admin_email = $current_user->user_email;
3779
+
3780
+ $error_type = fs_request_get( 'error_type', 'general' );
3781
+
3782
+ switch ( $error_type ) {
3783
+ case 'squid':
3784
+ $title = 'Squid ACL Blocking Issue';
3785
+ break;
3786
+ case 'cloudflare':
3787
+ $title = 'CloudFlare Blocking Issue';
3788
+ break;
3789
+ default:
3790
+ $title = 'API Connectivity Issue';
3791
+ break;
3792
+ }
3793
+
3794
+ $custom_email_sections = array();
3795
+
3796
+ // Add 'API Error' custom email section.
3797
+ $custom_email_sections['api_error'] = array(
3798
+ 'title' => 'API Error',
3799
+ 'rows' => array(
3800
+ 'ping' => array(
3801
+ 'API Error',
3802
+ is_string( $pong ) ? htmlentities( $pong ) : json_encode( $pong )
3803
+ ),
3804
+ )
3805
+ );
3806
+
3807
+ // Send email with technical details to resolve API connectivity issues.
3808
+ $this->send_email(
3809
+ 'api@freemius.com', // recipient
3810
+ $title . ' [' . $this->get_plugin_name() . ']', // subject
3811
+ $custom_email_sections,
3812
+ array( "Reply-To: $admin_email <$admin_email>" ) // headers
3813
+ );
3814
+
3815
+ $this->_admin_notices->add_sticky(
3816
+ sprintf(
3817
+ $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' ),
3818
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
3819
+ ),
3820
+ 'server_details_sent'
3821
+ );
3822
+
3823
+ // Action was taken, tell that API connectivity troubleshooting should be off now.
3824
+
3825
+ echo "1";
3826
+ exit;
3827
+ }
3828
+
3829
+ /**
3830
+ * Handle connectivity test retry approved by the user.
3831
+ *
3832
+ * @author Vova Feldman (@svovaf)
3833
+ * @since 1.1.7.4
3834
+ */
3835
+ function _retry_connectivity_test() {
3836
+ $this->_admin_notices->remove_sticky( 'failed_connect_api_first' );
3837
+
3838
+ $pong = $this->ping();
3839
+
3840
+ $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
3841
+
3842
+ if ( $is_connected ) {
3843
+ FS_GDPR_Manager::instance()->store_is_required( $pong->is_gdpr_required );
3844
+
3845
+ $this->store_connectivity_info( $pong, $is_connected );
3846
+
3847
+ echo $this->get_after_plugin_activation_redirect_url();
3848
+ } else {
3849
+ // Add connectivity issue message after 2nd failed attempt.
3850
+ $this->_add_connectivity_issue_message( $pong, false );
3851
+
3852
+ echo "1";
3853
+ }
3854
+
3855
+ exit;
3856
+ }
3857
+
3858
+ static function _add_firewall_issues_javascript() {
3859
+ $params = array();
3860
+ fs_require_once_template( 'firewall-issues-js.php', $params );
3861
+ }
3862
+
3863
+ #endregion
3864
+
3865
+ #----------------------------------------------------------------------------------
3866
+ #region Email
3867
+ #----------------------------------------------------------------------------------
3868
+
3869
+ /**
3870
+ * Generates and sends an HTML email with customizable sections.
3871
+ *
3872
+ * @author Leo Fajardo (@leorw)
3873
+ * @since 1.1.2
3874
+ *
3875
+ * @param string $to_address
3876
+ * @param string $subject
3877
+ * @param array $sections
3878
+ * @param array $headers
3879
+ *
3880
+ * @return bool Whether the email contents were sent successfully.
3881
+ */
3882
+ private function send_email(
3883
+ $to_address,
3884
+ $subject,
3885
+ $sections = array(),
3886
+ $headers = array()
3887
+ ) {
3888
+ $default_sections = $this->get_email_sections();
3889
+
3890
+ // Insert new sections or replace the default email sections.
3891
+ if ( is_array( $sections ) && ! empty( $sections ) ) {
3892
+ foreach ( $sections as $section_id => $custom_section ) {
3893
+ if ( ! isset( $default_sections[ $section_id ] ) ) {
3894
+ // If the section does not exist, add it.
3895
+ $default_sections[ $section_id ] = $custom_section;
3896
+ } else {
3897
+ // If the section already exists, override it.
3898
+ $current_section = $default_sections[ $section_id ];
3899
+
3900
+ // Replace the current section's title if a custom section title exists.
3901
+ if ( isset( $custom_section['title'] ) ) {
3902
+ $current_section['title'] = $custom_section['title'];
3903
+ }
3904
+
3905
+ // Insert new rows under the current section or replace the default rows.
3906
+ if ( isset( $custom_section['rows'] ) && is_array( $custom_section['rows'] ) && ! empty( $custom_section['rows'] ) ) {
3907
+ foreach ( $custom_section['rows'] as $row_id => $row ) {
3908
+ $current_section['rows'][ $row_id ] = $row;
3909
+ }
3910
+ }
3911
+
3912
+ $default_sections[ $section_id ] = $current_section;
3913
+ }
3914
+ }
3915
+ }
3916
+
3917
+ $vars = array( 'sections' => $default_sections );
3918
+ $message = fs_get_template( 'email.php', $vars );
3919
+
3920
+ // Set the type of email to HTML.
3921
+ $headers[] = 'Content-type: text/html; charset=UTF-8';
3922
+
3923
+ $header_string = implode( "\r\n", $headers );
3924
+
3925
+ return wp_mail(
3926
+ $to_address,
3927
+ $subject,
3928
+ $message,
3929
+ $header_string
3930
+ );
3931
+ }
3932
+
3933
+ /**
3934
+ * Generates the data for the sections of the email content.
3935
+ *
3936
+ * @author Leo Fajardo (@leorw)
3937
+ * @since 1.1.2
3938
+ *
3939
+ * @return array
3940
+ */
3941
+ private function get_email_sections() {
3942
+ // Retrieve the current user's information so that we can get the user's email, first name, and last name below.
3943
+ $current_user = self::_get_current_wp_user();
3944
+
3945
+ // Retrieve the cURL version information so that we can get the version number below.
3946
+ $curl_version_information = curl_version();
3947
+
3948
+ $active_plugin = self::get_active_plugins();
3949
+
3950
+ // Generate the list of active plugins separated by new line.
3951
+ $active_plugin_string = '';
3952
+ foreach ( $active_plugin as $plugin ) {
3953
+ $active_plugin_string .= sprintf(
3954
+ '<a href="%s">%s</a> [v%s]<br>',
3955
+ $plugin['PluginURI'],
3956
+ $plugin['Name'],
3957
+ $plugin['Version']
3958
+ );
3959
+ }
3960
+
3961
+ $server_ip = WP_FS__REMOTE_ADDR;
3962
+
3963
+ // Add PHP info for deeper investigation.
3964
+ ob_start();
3965
+ phpinfo();
3966
+ $php_info = ob_get_clean();
3967
+
3968
+ $api_domain = substr( FS_API__ADDRESS, strpos( FS_API__ADDRESS, ':' ) + 3 );
3969
+
3970
+ // Generate the default email sections.
3971
+ $sections = array(
3972
+ 'sdk' => array(
3973
+ 'title' => 'SDK',
3974
+ 'rows' => array(
3975
+ 'fs_version' => array( 'FS Version', $this->version ),
3976
+ 'curl_version' => array( 'cURL Version', $curl_version_information['version'] )
3977
+ )
3978
+ ),
3979
+ 'plugin' => array(
3980
+ 'title' => ucfirst( $this->get_module_type() ),
3981
+ 'rows' => array(
3982
+ 'name' => array( 'Name', $this->get_plugin_name() ),
3983
+ 'version' => array( 'Version', $this->get_plugin_version() )
3984
+ )
3985
+ ),
3986
+ 'api' => array(
3987
+ 'title' => 'API Subdomain',
3988
+ 'rows' => array(
3989
+ 'dns' => array(
3990
+ 'DNS_CNAME',
3991
+ function_exists( 'dns_get_record' ) ?
3992
+ var_export( dns_get_record( $api_domain, DNS_CNAME ), true ) :
3993
+ 'dns_get_record() disabled/blocked'
3994
+ ),
3995
+ 'ip' => array(
3996
+ 'IP',
3997
+ function_exists( 'gethostbyname' ) ?
3998
+ gethostbyname( $api_domain ) :
3999
+ 'gethostbyname() disabled/blocked'
4000
+ ),
4001
+ ),
4002
+ ),
4003
+ 'site' => array(
4004
+ 'title' => 'Site',
4005
+ 'rows' => array(
4006
+ 'unique_id' => array( 'Unique ID', $this->get_anonymous_id() ),
4007
+ 'address' => array( 'Address', site_url() ),
4008
+ 'host' => array(
4009
+ 'HTTP_HOST',
4010
+ ( ! empty( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : '' )
4011
+ ),
4012
+ 'hosting' => array(
4013
+ 'Hosting Company' => fs_request_has( 'hosting_company' ) ?
4014
+ fs_request_get( 'hosting_company' ) :
4015
+ 'Unknown',
4016
+ ),
4017
+ 'server_addr' => array(
4018
+ 'SERVER_ADDR',
4019
+ '<a href="http://www.projecthoneypot.org/ip_' . $server_ip . '">' . $server_ip . '</a>'
4020
+ )
4021
+ )
4022
+ ),
4023
+ 'user' => array(
4024
+ 'title' => 'User',
4025
+ 'rows' => array(
4026
+ 'email' => array( 'Email', $current_user->user_email ),
4027
+ 'first' => array( 'First', $current_user->user_firstname ),
4028
+ 'last' => array( 'Last', $current_user->user_lastname )
4029
+ )
4030
+ ),
4031
+ 'plugins' => array(
4032
+ 'title' => 'Plugins',
4033
+ 'rows' => array(
4034
+ 'active_plugins' => array( 'Active Plugins', $active_plugin_string )
4035
+ )
4036
+ ),
4037
+ 'php_info' => array(
4038
+ 'title' => 'PHP Info',
4039
+ 'rows' => array(
4040
+ 'info' => array( $php_info )
4041
+ ),
4042
+ )
4043
+ );
4044
+
4045
+ // Allow the sections to be modified by other code.
4046
+ $sections = $this->apply_filters( 'email_template_sections', $sections );
4047
+
4048
+ return $sections;
4049
+ }
4050
+
4051
+ #endregion
4052
+
4053
+ #----------------------------------------------------------------------------------
4054
+ #region Initialization
4055
+ #----------------------------------------------------------------------------------
4056
+
4057
+ /**
4058
+ * Init plugin's Freemius instance.
4059
+ *
4060
+ * @author Vova Feldman (@svovaf)
4061
+ * @since 1.0.1
4062
+ *
4063
+ * @param number $id
4064
+ * @param string $public_key
4065
+ * @param bool $is_live
4066
+ * @param bool $is_premium
4067
+ */
4068
+ function init( $id, $public_key, $is_live = true, $is_premium = true ) {
4069
+ $this->_logger->entrance();
4070
+
4071
+ $this->dynamic_init( array(
4072
+ 'id' => $id,
4073
+ 'public_key' => $public_key,
4074
+ 'is_live' => $is_live,
4075
+ 'is_premium' => $is_premium,
4076
+ ) );
4077
+ }
4078
+
4079
+ /**
4080
+ * Dynamic initiator, originally created to support initiation
4081
+ * with parent_id for add-ons.
4082
+ *
4083
+ * @author Vova Feldman (@svovaf)
4084
+ * @since 1.0.6
4085
+ *
4086
+ * @param array $plugin_info
4087
+ *
4088
+ * @throws Freemius_Exception
4089
+ */
4090
+ function dynamic_init( array $plugin_info ) {
4091
+ $this->_logger->entrance();
4092
+
4093
+ $this->parse_settings( $plugin_info );
4094
+
4095
+ if ( is_admin() && $this->is_theme() && $this->is_premium() && ! $this->has_active_valid_license() ) {
4096
+ $this->add_ajax_action(
4097
+ 'delete_theme_update_data',
4098
+ array( &$this, '_delete_theme_update_data_action' )
4099
+ );
4100
+ }
4101
+
4102
+ if ( ! self::is_ajax() ) {
4103
+ if ( ! $this->is_addon() || $this->is_only_premium() ) {
4104
+ add_action(
4105
+ ( $this->_is_network_active && fs_is_network_admin() ? 'network_' : '' ) . 'admin_menu',
4106
+ array( &$this, '_prepare_admin_menu' ),
4107
+ WP_FS__LOWEST_PRIORITY
4108
+ );
4109
+ }
4110
+ }
4111
+
4112
+ if ( $this->should_stop_execution() ) {
4113
+ return;
4114
+ }
4115
+
4116
+ if ( ! $this->is_registered() ) {
4117
+ if ( $this->is_anonymous() ) {
4118
+ // If user skipped, no need to test connectivity.
4119
+ $this->_has_api_connection = true;
4120
+ $this->_is_on = true;
4121
+ } else {
4122
+ if ( ! $this->has_api_connectivity() ) {
4123
+ if ( $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) ||
4124
+ $this->_admin_notices->has_sticky( 'failed_connect_api' )
4125
+ ) {
4126
+ if ( ! $this->_enable_anonymous || $this->is_premium() ) {
4127
+ // If anonymous mode is disabled, add firewall admin-notice message.
4128
+ add_action( 'admin_footer', array( 'Freemius', '_add_firewall_issues_javascript' ) );
4129
+
4130
+ $ajax_action_suffix = $this->_slug . ( $this->is_theme() ? ':theme' : '' );
4131
+ add_action( "wp_ajax_fs_resolve_firewall_issues_{$ajax_action_suffix}", array(
4132
+ &$this,
4133
+ '_email_about_firewall_issue'
4134
+ ) );
4135
+
4136
+ add_action( "wp_ajax_fs_retry_connectivity_test_{$ajax_action_suffix}", array(
4137
+ &$this,
4138
+ '_retry_connectivity_test'
4139
+ ) );
4140
+
4141
+ /**
4142
+ * 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.
4143
+ *
4144
+ * @author Vova Feldman (@svovaf)
4145
+ * @since 2.0.0
4146
+ */
4147
+ /*$this->add_ajax_action( 'resolve_firewall_issues', array(
4148
+ &$this,
4149
+ '_email_about_firewall_issue'
4150
+ ) );
4151
+
4152
+ $this->add_ajax_action( 'retry_connectivity_test', array(
4153
+ &$this,
4154
+ '_retry_connectivity_test'
4155
+ ) );*/
4156
+ }
4157
+ }
4158
+
4159
+ return;
4160
+ } else {
4161
+ $this->_admin_notices->remove_sticky( array(
4162
+ 'failed_connect_api_first',
4163
+ 'failed_connect_api',
4164
+ ) );
4165
+
4166
+ if ( $this->_anonymous_mode ) {
4167
+ // Simulate anonymous mode.
4168
+ $this->_is_anonymous = true;
4169
+ }
4170
+ }
4171
+ }
4172
+ }
4173
+
4174
+ /**
4175
+ * This should be executed even if Freemius is off for the core module,
4176
+ * otherwise, the add-ons dialogbox won't work properly. This is esepcially
4177
+ * relevant when the developer decided to turn FS off for existing users.
4178
+ *
4179
+ * @author Vova Feldman (@svovaf)
4180
+ */
4181
+ if ( $this->is_user_in_admin() &&
4182
+ 'plugin-information' === fs_request_get( 'tab', false ) &&
4183
+ (
4184
+ ( $this->is_addon() && $this->get_slug() == fs_request_get( 'plugin', false ) ) ||
4185
+ ( $this->has_addons() && $this->get_id() == fs_request_get( 'parent_plugin_id', false ) )
4186
+ )
4187
+ ) {
4188
+ require_once WP_FS__DIR_INCLUDES . '/fs-plugin-info-dialog.php';
4189
+
4190
+ new FS_Plugin_Info_Dialog( $this->is_addon() ? $this->get_parent_instance() : $this );
4191
+ }
4192
+
4193
+ // Check if Freemius is on for the current plugin.
4194
+ // This MUST be executed after all the plugin variables has been loaded.
4195
+ if ( ! $this->is_registered() && ! $this->is_on() ) {
4196
+ return;
4197
+ }
4198
+
4199
+ if ( $this->has_api_connectivity() ) {
4200
+ if ( self::is_cron() ) {
4201
+ $this->hook_callback_to_sync_cron();
4202
+ } else if ( $this->is_user_in_admin() ) {
4203
+ /**
4204
+ * Schedule daily data sync cron if:
4205
+ *
4206
+ * 1. User opted-in (for tracking).
4207
+ * 2. If skipped, but later upgraded (opted-in via upgrade).
4208
+ *
4209
+ * @author Vova Feldman (@svovaf)
4210
+ * @since 1.1.7.3
4211
+ *
4212
+ */
4213
+ if ( $this->is_registered() ) {
4214
+ if ( ! $this->is_sync_cron_on() && $this->is_tracking_allowed() ) {
4215
+ $this->schedule_sync_cron();
4216
+ }
4217
+ }
4218
+
4219
+ /**
4220
+ * Check if requested for manual blocking background sync.
4221
+ */
4222
+ if ( fs_request_has( 'background_sync' ) ) {
4223
+ $this->run_manual_sync();
4224
+ }
4225
+ }
4226
+ }
4227
+
4228
+ if ( $this->is_registered() ) {
4229
+ $this->hook_callback_to_install_sync();
4230
+ }
4231
+
4232
+ if ( $this->is_addon() ) {
4233
+ if ( $this->is_parent_plugin_installed() ) {
4234
+ // Link to parent FS.
4235
+ $this->_parent = self::get_instance_by_id( $this->_plugin->parent_plugin_id );
4236
+
4237
+ // Get parent plugin reference.
4238
+ $this->_parent_plugin = $this->_parent->get_plugin();
4239
+ }
4240
+ }
4241
+
4242
+ if ( $this->is_user_in_admin() ) {
4243
+ if ( $this->is_addon() ) {
4244
+ if ( ! $this->is_parent_plugin_installed() ) {
4245
+ $parent_name = $this->get_option( $plugin_info, 'parent_name', null );
4246
+
4247
+ if ( isset( $plugin_info['parent'] ) ) {
4248
+ $parent_name = $this->get_option( $plugin_info['parent'], 'name', null );
4249
+ }
4250
+
4251
+ $this->_admin_notices->add(
4252
+ ( ! empty( $parent_name ) ?
4253
+ 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 ) :
4254
+ 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() )
4255
+ ),
4256
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
4257
+ 'error'
4258
+ );
4259
+
4260
+ return;
4261
+ } else {
4262
+ if ( $this->_parent->is_registered() && ! $this->is_registered() ) {
4263
+ // If parent plugin activated, automatically install add-on for the user.
4264
+ $this->_activate_addon_account( $this->_parent );
4265
+ } else if ( ! $this->_parent->is_registered() && $this->is_registered() ) {
4266
+ // If add-on activated and parent not, automatically install parent for the user.
4267
+ $this->activate_parent_account( $this->_parent );
4268
+ }
4269
+
4270
+ // @todo This should be only executed on activation. It should be migrated to register_activation_hook() together with other activation related logic.
4271
+ if ( $this->is_premium() ) {
4272
+ // Remove add-on download admin-notice.
4273
+ $this->_parent->_admin_notices->remove_sticky( array(
4274
+ 'addon_plan_upgraded_' . $this->_slug,
4275
+ 'no_addon_license_' . $this->_slug,
4276
+ ) );
4277
+ }
4278
+
4279
+ // $this->deactivate_premium_only_addon_without_license();
4280
+ }
4281
+ }
4282
+
4283
+ add_action( 'admin_init', array( &$this, '_admin_init_action' ) );
4284
+
4285
+ // if ( $this->is_registered() ||
4286
+ // $this->is_anonymous() ||
4287
+ // $this->is_pending_activation()
4288
+ // ) {
4289
+ // $this->_init_admin();
4290
+ // }
4291
+ }
4292
+
4293
+ /**
4294
+ * Should be called outside `$this->is_user_in_admin()` scope
4295
+ * because the updater has some logic that needs to be executed
4296
+ * during AJAX calls.
4297
+ *
4298
+ * Currently we need to hook to the `http_request_host_is_external` filter.
4299
+ * In the future, there might be additional logic added.
4300
+ *
4301
+ * @author Vova Feldman
4302
+ * @since 1.2.1.6
4303
+ */
4304
+ if ( $this->is_premium() && $this->has_release_on_freemius() ) {
4305
+ FS_Plugin_Updater::instance( $this );
4306
+ }
4307
+
4308
+ $this->do_action( 'initiated' );
4309
+
4310
+ if ( $this->_storage->prev_is_premium !== $this->_plugin->is_premium ) {
4311
+ if ( isset( $this->_storage->prev_is_premium ) ) {
4312
+ $this->apply_filters(
4313
+ 'after_code_type_change',
4314
+ // New code type.
4315
+ $this->_plugin->is_premium
4316
+ );
4317
+ } else {
4318
+ // Set for code type for the first time.
4319
+ $this->_storage->prev_is_premium = $this->_plugin->is_premium;
4320
+ }
4321
+ }
4322
+
4323
+ if ( ! $this->is_addon() ) {
4324
+ if ( $this->is_registered() ) {
4325
+ // Fix for upgrade from versions < 1.0.9.
4326
+ if ( ! isset( $this->_storage->activation_timestamp ) ) {
4327
+ $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME;
4328
+ }
4329
+
4330
+ $this->do_action( 'after_init_plugin_registered' );
4331
+ } else if ( $this->is_anonymous() ) {
4332
+ $this->do_action( 'after_init_plugin_anonymous' );
4333
+ } else if ( $this->is_pending_activation() ) {
4334
+ $this->do_action( 'after_init_plugin_pending_activations' );
4335
+ }
4336
+ } else {
4337
+ if ( $this->is_registered() ) {
4338
+ $this->do_action( 'after_init_addon_registered' );
4339
+ } else if ( $this->is_anonymous() ) {
4340
+ $this->do_action( 'after_init_addon_anonymous' );
4341
+ } else if ( $this->is_pending_activation() ) {
4342
+ $this->do_action( 'after_init_addon_pending_activations' );
4343
+ }
4344
+ }
4345
+ }
4346
+
4347
+ /**
4348
+ * @author Leo Fajardo (@leorw)
4349
+ *
4350
+ * @since 1.2.1.5
4351
+ */
4352
+ function _stop_tracking_callback() {
4353
+ $this->_logger->entrance();
4354
+
4355
+ $this->check_ajax_referer( 'stop_tracking' );
4356
+
4357
+ $result = $this->stop_tracking( fs_is_network_admin() );
4358
+
4359
+ if ( true === $result ) {
4360
+ self::shoot_ajax_success();
4361
+ }
4362
+
4363
+ $this->_logger->api_error( $result );
4364
+
4365
+ self::shoot_ajax_failure(
4366
+ sprintf( $this->get_text_inline( 'Unexpected API error. Please contact the %s\'s author with the following error.', 'unexpected-api-error' ), $this->_module_type ) .
4367
+ ( $this->is_api_error( $result ) && isset( $result->error ) ?
4368
+ $result->error->message :
4369
+ var_export( $result, true ) )
4370
+ );
4371
+ }
4372
+
4373
+ /**
4374
+ * @author Leo Fajardo (@leorw)
4375
+ * @since 1.2.1.5
4376
+ */
4377
+ function _allow_tracking_callback() {
4378
+ $this->_logger->entrance();
4379
+
4380
+ $this->check_ajax_referer( 'allow_tracking' );
4381
+
4382
+ $result = $this->allow_tracking( fs_is_network_admin() );
4383
+
4384
+ if ( true === $result ) {
4385
+ self::shoot_ajax_success();
4386
+ }
4387
+
4388
+ $this->_logger->api_error( $result );
4389
+
4390
+ self::shoot_ajax_failure(
4391
+ sprintf( $this->get_text_inline( 'Unexpected API error. Please contact the %s\'s author with the following error.', 'unexpected-api-error' ), $this->_module_type ) .
4392
+ ( $this->is_api_error( $result ) && isset( $result->error ) ?
4393
+ $result->error->message :
4394
+ var_export( $result, true ) )
4395
+ );
4396
+ }
4397
+
4398
+ /**
4399
+ * Opt-out from usage tracking.
4400
+ *
4401
+ * Note: This will not delete the account information but will stop all tracking.
4402
+ *
4403
+ * Returns:
4404
+ * 1. FALSE - If the user never opted-in.
4405
+ * 2. TRUE - If successfully opted-out.
4406
+ * 3. object - API result on failure.
4407
+ *
4408
+ * @author Leo Fajardo (@leorw)
4409
+ * @since 1.2.1.5
4410
+ *
4411
+ * @return bool|object
4412
+ */
4413
+ function stop_site_tracking() {
4414
+ $this->_logger->entrance();
4415
+
4416
+ if ( ! $this->is_registered() ) {
4417
+ // User never opted-in.
4418
+ return false;
4419
+ }
4420
+
4421
+ if ( $this->is_tracking_prohibited() ) {
4422
+ // Already disconnected.
4423
+ return true;
4424
+ }
4425
+
4426
+ // Send update to FS.
4427
+ $result = $this->get_api_site_scope()->call( '/?fields=is_disconnected', 'put', array(
4428
+ 'is_disconnected' => true
4429
+ ) );
4430
+
4431
+ if ( ! $this->is_api_result_entity( $result ) ||
4432
+ ! isset( $result->is_disconnected ) ||
4433
+ ! $result->is_disconnected
4434
+ ) {
4435
+ $this->_logger->api_error( $result );
4436
+
4437
+ return $result;
4438
+ }
4439
+
4440
+ $this->_site->is_disconnected = $result->is_disconnected;
4441
+ $this->_store_site();
4442
+
4443
+ $this->clear_sync_cron();
4444
+
4445
+ // Successfully disconnected.
4446
+ return true;
4447
+ }
4448
+
4449
+ /**
4450
+ * Opt-out network from usage tracking.
4451
+ *
4452
+ * Note: This will not delete the account information but will stop all tracking.
4453
+ *
4454
+ * Returns:
4455
+ * 1. FALSE - If the user never opted-in.
4456
+ * 2. TRUE - If successfully opted-out.
4457
+ * 3. object - API result on failure.
4458
+ *
4459
+ * @author Leo Fajardo (@leorw)
4460
+ * @since 1.2.1.5
4461
+ *
4462
+ * @return bool|object
4463
+ */
4464
+ function stop_network_tracking() {
4465
+ $this->_logger->entrance();
4466
+
4467
+ if ( ! $this->is_registered() ) {
4468
+ // User never opted-in.
4469
+ return false;
4470
+ }
4471
+
4472
+ $install_id_2_blog_id = array();
4473
+ $installs_map = $this->get_blog_install_map();
4474
+
4475
+ $opt_out_all = true;
4476
+
4477
+ $params = array();
4478
+ foreach ( $installs_map as $blog_id => $install ) {
4479
+ if ( $install->is_tracking_prohibited() ) {
4480
+ // Already opted-out.
4481
+ continue;
4482
+ }
4483
+
4484
+ if ( $this->is_site_delegated_connection( $blog_id ) ) {
4485
+ // Opt-out only from non-delegated installs.
4486
+ $opt_out_all = false;
4487
+ continue;
4488
+ }
4489
+
4490
+ $params[] = array( 'id' => $install->id );
4491
+
4492
+ $install_id_2_blog_id[ $install->id ] = $blog_id;
4493
+ }
4494
+
4495
+ if ( empty( $install_id_2_blog_id ) ) {
4496
+ return true;
4497
+ }
4498
+
4499
+ $params[] = array( 'is_disconnected' => true );
4500
+
4501
+ // Send update to FS.
4502
+ $result = $this->get_current_or_network_user_api_scope()->call( "/plugins/{$this->_module_id}/installs.json", 'put', $params );
4503
+
4504
+ if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
4505
+ $this->_logger->api_error( $result );
4506
+
4507
+ return $result;
4508
+ }
4509
+
4510
+ foreach ( $result->installs as $r_install ) {
4511
+ $blog_id = $install_id_2_blog_id[ $r_install->id ];
4512
+ $install = $installs_map[ $blog_id ];
4513
+ $install->is_disconnected = $r_install->is_disconnected;
4514
+ $this->_store_site( true, $blog_id, $install );
4515
+ }
4516
+
4517
+ $this->clear_sync_cron( $opt_out_all );
4518
+
4519
+ // Successfully disconnected.
4520
+ return true;
4521
+ }
4522
+
4523
+ /**
4524
+ * Opt-out from usage tracking.
4525
+ *
4526
+ * Note: This will not delete the account information but will stop all tracking.
4527
+ *
4528
+ * Returns:
4529
+ * 1. FALSE - If the user never opted-in.
4530
+ * 2. TRUE - If successfully opted-out.
4531
+ * 3. object - API result on failure.
4532
+ *
4533
+ * @author Leo Fajardo (@leorw)
4534
+ * @since 1.2.1.5
4535
+ *
4536
+ * @param bool $is_network_action
4537
+ *
4538
+ * @return bool|object
4539
+ */
4540
+ function stop_tracking( $is_network_action = false ) {
4541
+ $this->_logger->entrance();
4542
+
4543
+ return $is_network_action ?
4544
+ $this->stop_network_tracking() :
4545
+ $this->stop_site_tracking();
4546
+ }
4547
+
4548
+ /**
4549
+ * Opt-in back into usage tracking.
4550
+ *
4551
+ * Note: This will only work if the user opted-in previously.
4552
+ *
4553
+ * Returns:
4554
+ * 1. FALSE - If the user never opted-in.
4555
+ * 2. TRUE - If successfully opted-in back to usage tracking.
4556
+ * 3. object - API result on failure.
4557
+ *
4558
+ * @author Leo Fajardo (@leorw)
4559
+ * @since 1.2.1.5
4560
+ *
4561
+ * @return bool|object
4562
+ */
4563
+ function allow_site_tracking() {
4564
+ $this->_logger->entrance();
4565
+
4566
+ if ( ! $this->is_registered() ) {
4567
+ // User never opted-in.
4568
+ return false;
4569
+ }
4570
+
4571
+ if ( $this->is_tracking_allowed() ) {
4572
+ // Tracking already allowed.
4573
+ return true;
4574
+ }
4575
+
4576
+ $result = $this->get_api_site_scope()->call( '/?is_disconnected', 'put', array(
4577
+ 'is_disconnected' => false
4578
+ ) );
4579
+
4580
+ if ( ! $this->is_api_result_entity( $result ) ||
4581
+ ! isset( $result->is_disconnected ) ||
4582
+ $result->is_disconnected
4583
+ ) {
4584
+ $this->_logger->api_error( $result );
4585
+
4586
+ return $result;
4587
+ }
4588
+
4589
+ $this->_site->is_disconnected = $result->is_disconnected;
4590
+ $this->_store_site();
4591
+
4592
+ $this->schedule_sync_cron();
4593
+
4594
+ // Successfully reconnected.
4595
+ return true;
4596
+ }
4597
+
4598
+ /**
4599
+ * Opt-in network back into usage tracking.
4600
+ *
4601
+ * Note: This will only work if the user opted-in previously.
4602
+ *
4603
+ * Returns:
4604
+ * 1. FALSE - If the user never opted-in.
4605
+ * 2. TRUE - If successfully opted-in back to usage tracking.
4606
+ * 3. object - API result on failure.
4607
+ *
4608
+ * @author Leo Fajardo (@leorw)
4609
+ * @since 1.2.1.5
4610
+ *
4611
+ * @return bool|object
4612
+ */
4613
+ function allow_network_tracking() {
4614
+ $this->_logger->entrance();
4615
+
4616
+ if ( ! $this->is_registered() ) {
4617
+ // User never opted-in.
4618
+ return false;
4619
+ }
4620
+
4621
+ $install_id_2_blog_id = array();
4622
+ $installs_map = $this->get_blog_install_map();
4623
+
4624
+ $params = array();
4625
+ foreach ( $installs_map as $blog_id => $install ) {
4626
+ if ( $install->is_tracking_allowed() ) {
4627
+ continue;
4628
+ }
4629
+
4630
+ $params[] = array( 'id' => $install->id );
4631
+
4632
+ $install_id_2_blog_id[ $install->id ] = $blog_id;
4633
+ }
4634
+
4635
+ if ( empty( $install_id_2_blog_id ) ) {
4636
+ return true;
4637
+ }
4638
+
4639
+ $params[] = array( 'is_disconnected' => false );
4640
+
4641
+ // Send update to FS.
4642
+ $result = $this->get_current_or_network_user_api_scope()->call( "/plugins/{$this->_module_id}/installs.json", 'put', $params );
4643
+
4644
+
4645
+ if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
4646
+ $this->_logger->api_error( $result );
4647
+
4648
+ return $result;
4649
+ }
4650
+
4651
+ foreach ( $result->installs as $r_install ) {
4652
+ $blog_id = $install_id_2_blog_id[ $r_install->id ];
4653
+ $install = $installs_map[ $blog_id ];
4654
+ $install->is_disconnected = $r_install->is_disconnected;
4655
+ $this->_store_site( true, $blog_id, $install );
4656
+ }
4657
+
4658
+ $this->schedule_sync_cron();
4659
+
4660
+ // Successfully reconnected.
4661
+ return true;
4662
+ }
4663
+
4664
+ /**
4665
+ * Opt-in back into usage tracking.
4666
+ *
4667
+ * Note: This will only work if the user opted-in previously.
4668
+ *
4669
+ * Returns:
4670
+ * 1. FALSE - If the user never opted-in.
4671
+ * 2. TRUE - If successfully opted-in back to usage tracking.
4672
+ * 3. object - API result on failure.
4673
+ *
4674
+ * @author Leo Fajardo (@leorw)
4675
+ * @since 1.2.1.5
4676
+ *
4677
+ * @param bool $is_network_action
4678
+ *
4679
+ * @return bool|object
4680
+ */
4681
+ function allow_tracking( $is_network_action = false ) {
4682
+ $this->_logger->entrance();
4683
+
4684
+ return $is_network_action ?
4685
+ $this->allow_network_tracking() :
4686
+ $this->allow_site_tracking();
4687
+ }
4688
+
4689
+ /**
4690
+ * If user opted-in and later disabled usage-tracking,
4691
+ * re-allow tracking for licensing and updates.
4692
+ *
4693
+ * @author Leo Fajardo (@leorw)
4694
+ * @since 1.2.1.5
4695
+ *
4696
+ * @param bool $is_context_single_site
4697
+ */
4698
+ private function reconnect_locally( $is_context_single_site = false ) {
4699
+ $this->_logger->entrance();
4700
+
4701
+ if ( ! $this->is_registered() ) {
4702
+ return;
4703
+ }
4704
+
4705
+ if ( ! fs_is_network_admin() || $is_context_single_site ) {
4706
+ if ( $this->is_tracking_prohibited() ) {
4707
+ $this->_site->is_disconnected = false;
4708
+ $this->_store_site();
4709
+ }
4710
+ } else {
4711
+ $installs_map = $this->get_blog_install_map();
4712
+ foreach ( $installs_map as $blog_id => $install ) {
4713
+ /**
4714
+ * @var FS_Site $install
4715
+ */
4716
+ if ( $install->is_tracking_prohibited() ) {
4717
+ $install->is_disconnected = false;
4718
+ $this->_store_site( true, $blog_id, $install );
4719
+ }
4720
+ }
4721
+ }
4722
+ }
4723
+
4724
+ /**
4725
+ * Parse plugin's settings (as defined by the plugin dev).
4726
+ *
4727
+ * @author Vova Feldman (@svovaf)
4728
+ * @since 1.1.7.3
4729
+ *
4730
+ * @param array $plugin_info
4731
+ *
4732
+ * @throws \Freemius_Exception
4733
+ */
4734
+ private function parse_settings( &$plugin_info ) {
4735
+ $this->_logger->entrance();
4736
+
4737
+ $id = $this->get_numeric_option( $plugin_info, 'id', false );
4738
+ $public_key = $this->get_option( $plugin_info, 'public_key', false );
4739
+ $secret_key = $this->get_option( $plugin_info, 'secret_key', null );
4740
+ $parent_id = $this->get_numeric_option( $plugin_info, 'parent_id', null );
4741
+ $parent_name = $this->get_option( $plugin_info, 'parent_name', null );
4742
+
4743
+ /**
4744
+ * @author Vova Feldman (@svovaf)
4745
+ * @since 1.1.9 Try to pull secret key from external config.
4746
+ */
4747
+ if ( is_null( $secret_key ) && defined( "WP_FS__{$this->_slug}_SECRET_KEY" ) ) {
4748
+ $secret_key = constant( "WP_FS__{$this->_slug}_SECRET_KEY" );
4749
+ }
4750
+
4751
+ if ( isset( $plugin_info['parent'] ) ) {
4752
+ $parent_id = $this->get_numeric_option( $plugin_info['parent'], 'id', null );
4753
+ // $parent_slug = $this->get_option( $plugin_info['parent'], 'slug', null );
4754
+ // $parent_public_key = $this->get_option( $plugin_info['parent'], 'public_key', null );
4755
+ // $parent_name = $this->get_option( $plugin_info['parent'], 'name', null );
4756
+ }
4757
+
4758
+ if ( false === $id ) {
4759
+ throw new Freemius_Exception( array(
4760
+ 'error' => array(
4761
+ 'type' => 'ParameterNotSet',
4762
+ 'message' => 'Plugin id parameter is not set.',
4763
+ 'code' => 'plugin_id_not_set',
4764
+ 'http' => 500,
4765
+ )
4766
+ ) );
4767
+ }
4768
+ if ( false === $public_key ) {
4769
+ throw new Freemius_Exception( array(
4770
+ 'error' => array(
4771
+ 'type' => 'ParameterNotSet',
4772
+ 'message' => 'Plugin public_key parameter is not set.',
4773
+ 'code' => 'plugin_public_key_not_set',
4774
+ 'http' => 500,
4775
+ )
4776
+ ) );
4777
+ }
4778
+
4779
+ $plugin = ( $this->_plugin instanceof FS_Plugin ) ?
4780
+ $this->_plugin :
4781
+ new FS_Plugin();
4782
+
4783
+ $premium_suffix = $this->get_option( $plugin_info, 'premium_suffix', '(Premium)' );
4784
+
4785
+ $plugin->update( array(
4786
+ 'id' => $id,
4787
+ 'type' => $this->get_option( $plugin_info, 'type', $this->_module_type ),
4788
+ 'public_key' => $public_key,
4789
+ 'slug' => $this->_slug,
4790
+ 'premium_slug' => $this->get_option( $plugin_info, 'premium_slug', "{$this->_slug}-premium" ),
4791
+ 'parent_plugin_id' => $parent_id,
4792
+ 'version' => $this->get_plugin_version(),
4793
+ 'title' => $this->get_plugin_name( $premium_suffix ),
4794
+ 'file' => $this->_plugin_basename,
4795
+ 'is_premium' => $this->get_bool_option( $plugin_info, 'is_premium', true ),
4796
+ 'premium_suffix' => $premium_suffix,
4797
+ 'is_live' => $this->get_bool_option( $plugin_info, 'is_live', true ),
4798
+ 'affiliate_moderation' => $this->get_option( $plugin_info, 'has_affiliation' ),
4799
+ ) );
4800
+
4801
+ if ( $plugin->is_updated() ) {
4802
+ // Update plugin details.
4803
+ $this->_plugin = FS_Plugin_Manager::instance( $this->_module_id )->store( $plugin );
4804
+ }
4805
+ // Set the secret key after storing the plugin, we don't want to store the key in the storage.
4806
+ $this->_plugin->secret_key = $secret_key;
4807
+
4808
+ if ( ! isset( $plugin_info['menu'] ) ) {
4809
+ $plugin_info['menu'] = array();
4810
+
4811
+ if ( ! empty( $this->_storage->sdk_last_version ) &&
4812
+ version_compare( $this->_storage->sdk_last_version, '1.1.2', '<=' )
4813
+ ) {
4814
+ // Backward compatibility to 1.1.2
4815
+ $plugin_info['menu']['slug'] = isset( $plugin_info['menu_slug'] ) ?
4816
+ $plugin_info['menu_slug'] :
4817
+ $this->_slug;
4818
+ }
4819
+ }
4820
+
4821
+ $this->_menu = FS_Admin_Menu_Manager::instance(
4822
+ $this->_module_id,
4823
+ $this->_module_type,
4824
+ $this->get_unique_affix()
4825
+ );
4826
+
4827
+ $this->_menu->init( $plugin_info['menu'], $this->is_addon() );
4828
+
4829
+ $this->_has_addons = $this->get_bool_option( $plugin_info, 'has_addons', false );
4830
+ $this->_has_paid_plans = $this->get_bool_option( $plugin_info, 'has_paid_plans', true );
4831
+ $this->_has_premium_version = $this->get_bool_option( $plugin_info, 'has_premium_version', $this->_has_paid_plans );
4832
+ $this->_ignore_pending_mode = $this->get_bool_option( $plugin_info, 'ignore_pending_mode', false );
4833
+ $this->_is_org_compliant = $this->get_bool_option( $plugin_info, 'is_org_compliant', true );
4834
+ $this->_is_premium_only = $this->get_bool_option( $plugin_info, 'is_premium_only', false );
4835
+ if ( $this->_is_premium_only ) {
4836
+ // If premium only plugin, disable anonymous mode.
4837
+ $this->_enable_anonymous = false;
4838
+ $this->_anonymous_mode = false;
4839
+ } else {
4840
+ $this->_enable_anonymous = $this->get_bool_option( $plugin_info, 'enable_anonymous', true );
4841
+ $this->_anonymous_mode = $this->get_bool_option( $plugin_info, 'anonymous_mode', false );
4842
+ }
4843
+ $this->_permissions = $this->get_option( $plugin_info, 'permissions', array() );
4844
+
4845
+ if ( ! empty( $plugin_info['trial'] ) ) {
4846
+ $this->_trial_days = $this->get_numeric_option(
4847
+ $plugin_info['trial'],
4848
+ 'days',
4849
+ // Default to 0 - trial without days specification.
4850
+ 0
4851
+ );
4852
+
4853
+ $this->_is_trial_require_payment = $this->get_bool_option( $plugin_info['trial'], 'is_require_payment', false );
4854
+ }
4855
+ }
4856
+
4857
+ /**
4858
+ * @param string[] $options
4859
+ * @param string $key
4860
+ * @param mixed $default
4861
+ *
4862
+ * @return bool
4863
+ */
4864
+ private function get_option( &$options, $key, $default = false ) {
4865
+ return ! empty( $options[ $key ] ) ? $options[ $key ] : $default;
4866
+ }
4867
+
4868
+ private function get_bool_option( &$options, $key, $default = false ) {
4869
+ return isset( $options[ $key ] ) && is_bool( $options[ $key ] ) ? $options[ $key ] : $default;
4870
+ }
4871
+
4872
+ private function get_numeric_option( &$options, $key, $default = false ) {
4873
+ return isset( $options[ $key ] ) && is_numeric( $options[ $key ] ) ? $options[ $key ] : $default;
4874
+ }
4875
+
4876
+ /**
4877
+ * Gate keeper.
4878
+ *
4879
+ * @author Vova Feldman (@svovaf)
4880
+ * @since 1.1.7.3
4881
+ *
4882
+ * @return bool
4883
+ */
4884
+ private function should_stop_execution() {
4885
+ if ( empty( $this->_storage->was_plugin_loaded ) ) {
4886
+ /**
4887
+ * Don't execute Freemius until plugin was fully loaded at least once,
4888
+ * to give the opportunity for the activation hook to run before pinging
4889
+ * the API for connectivity test. This logic is relevant for the
4890
+ * identification of new plugin install vs. plugin update.
4891
+ *
4892
+ * @author Vova Feldman (@svovaf)
4893
+ * @since 1.1.9
4894
+ */
4895
+ return true;
4896
+ }
4897
+
4898
+ if ( $this->is_activation_mode() ) {
4899
+ if ( ! is_admin() ) {
4900
+ /**
4901
+ * If in activation mode, don't execute Freemius outside of the
4902
+ * admin dashboard.
4903
+ *
4904
+ * @author Vova Feldman (@svovaf)
4905
+ * @since 1.1.7.3
4906
+ */
4907
+ return true;
4908
+ }
4909
+
4910
+ if ( ! WP_FS__IS_HTTP_REQUEST ) {
4911
+ /**
4912
+ * If in activation and executed without HTTP context (e.g. CLI, Cronjob),
4913
+ * then don't start Freemius.
4914
+ *
4915
+ * @author Vova Feldman (@svovaf)
4916
+ * @since 1.1.6.3
4917
+ *
4918
+ * @link https://wordpress.org/support/topic/errors-in-the-freemius-class-when-running-in-wordpress-in-cli
4919
+ */
4920
+ return true;
4921
+ }
4922
+
4923
+ if ( self::is_cron() ) {
4924
+ /**
4925
+ * If in activation mode, don't execute Freemius during wp crons
4926
+ * (wp crons have HTTP context - called as HTTP request).
4927
+ *
4928
+ * @author Vova Feldman (@svovaf)
4929
+ * @since 1.1.7.3
4930
+ */
4931
+ return true;
4932
+ }
4933
+
4934
+ if ( self::is_ajax() &&
4935
+ ! $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) &&
4936
+ ! $this->_admin_notices->has_sticky( 'failed_connect_api' )
4937
+ ) {
4938
+ /**
4939
+ * During activation, if running in AJAX mode, unless there's a sticky
4940
+ * connectivity issue notice, don't run Freemius.
4941
+ *
4942
+ * @author Vova Feldman (@svovaf)
4943
+ * @since 1.1.7.3
4944
+ */
4945
+ return true;
4946
+ }
4947
+ }
4948
+
4949
+ return false;
4950
+ }
4951
+
4952
+ /**
4953
+ * Triggered after code type has changed.
4954
+ *
4955
+ * @author Vova Feldman (@svovaf)
4956
+ * @since 1.1.9.1
4957
+ */
4958
+ function _after_code_type_change() {
4959
+ $this->_logger->entrance();
4960
+
4961
+ if ( $this->is_theme() ) {
4962
+ // Expire the cache of the previous tabs since the theme may
4963
+ // have setting updates after code type has changed.
4964
+ $this->_cache->expire( 'tabs' );
4965
+ $this->_cache->expire( 'tabs_stylesheets' );
4966
+ }
4967
+
4968
+ if ( $this->is_registered() ) {
4969
+ if ( ! $this->is_addon() ) {
4970
+ add_action(
4971
+ is_admin() ? 'admin_init' : 'init',
4972
+ array( &$this, '_plugin_code_type_changed' )
4973
+ );
4974
+ }
4975
+
4976
+ if ( $this->is_premium() ) {
4977
+ // Purge cached payments after switching to the premium version.
4978
+ // @todo This logic doesn't handle purging the cache for serviceware module upgrade.
4979
+ $this->get_api_user_scope()->purge_cache( "/plugins/{$this->_module_id}/payments.json?include_addons=true" );
4980
+ }
4981
+ }
4982
+ }
4983
+
4984
+ /**
4985
+ * Handles plugin's code type change (free <--> premium).
4986
+ *
4987
+ * @author Vova Feldman (@svovaf)
4988
+ * @since 1.0.9
4989
+ */
4990
+ function _plugin_code_type_changed() {
4991
+ $this->_logger->entrance();
4992
+
4993
+ if ( $this->is_premium() ) {
4994
+ $this->reconnect_locally();
4995
+
4996
+ // Activated premium code.
4997
+ $this->do_action( 'after_premium_version_activation' );
4998
+
4999
+ // Remove all sticky messages related to download of the premium version.
5000
+ $this->_admin_notices->remove_sticky( array(
5001
+ 'trial_started',
5002
+ 'plan_upgraded',
5003
+ 'plan_changed',
5004
+ 'license_activated',
5005
+ ) );
5006
+
5007
+ $notice = '';
5008
+ if ( ! $this->is_only_premium() ) {
5009
+ $notice = sprintf( $this->get_text_inline( 'Premium %s version was successfully activated.', 'premium-activated-message' ), $this->_module_type );
5010
+ }
5011
+
5012
+ $license_notice = $this->get_license_network_activation_notice();
5013
+ if ( ! empty( $license_notice ) ) {
5014
+ $notice .= ' ' . $license_notice;
5015
+ }
5016
+
5017
+ if ( ! empty( $notice ) ) {
5018
+ $this->_admin_notices->add_sticky(
5019
+ trim( $notice ),
5020
+ 'premium_activated',
5021
+ $this->get_text_x_inline( 'W00t',
5022
+ 'Used to express elation, enthusiasm, or triumph (especially in electronic communication).', 'woot' ) . '!'
5023
+ );
5024
+ }
5025
+ } else {
5026
+ // Remove sticky message related to premium code activation.
5027
+ $this->_admin_notices->remove_sticky( 'premium_activated' );
5028
+
5029
+ // Activated free code (after had the premium before).
5030
+ $this->do_action( 'after_free_version_reactivation' );
5031
+
5032
+ if ( $this->is_paying() && ! $this->is_premium() ) {
5033
+ $this->_admin_notices->add_sticky(
5034
+ sprintf(
5035
+ /* translators: %s: License type (e.g. you have a professional license) */
5036
+ $this->get_text_inline( 'You have a %s license.', 'you-have-x-license' ),
5037
+ $this->get_plan_title()
5038
+ ) . $this->get_complete_upgrade_instructions(),
5039
+ 'plan_upgraded',
5040
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
5041
+ );
5042
+ }
5043
+ }
5044
+
5045
+ // Schedule code type changes event.
5046
+ $this->schedule_install_sync();
5047
+
5048
+ /**
5049
+ * Unregister the uninstall hook for the other version of the plugin (with different code type) to avoid
5050
+ * triggering a fatal error when uninstalling that plugin. For example, after deactivating the "free" version
5051
+ * of a specific plugin, its uninstall hook should be unregistered after the "premium" version has been
5052
+ * activated. If we don't do that, a fatal error will occur when we try to uninstall the "free" version since
5053
+ * the main file of the "free" version will be loaded first before calling the hooked callback. Since the
5054
+ * free and premium versions are almost identical (same class or have same functions), a fatal error like
5055
+ * "Cannot redeclare class MyClass" or "Cannot redeclare my_function()" will occur.
5056
+ */
5057
+ $this->unregister_uninstall_hook();
5058
+
5059
+ $this->clear_module_main_file_cache();
5060
+
5061
+ // Update is_premium of latest version.
5062
+ $this->_storage->prev_is_premium = $this->_plugin->is_premium;
5063
+ }
5064
+
5065
+ #endregion
5066
+
5067
+ #----------------------------------------------------------------------------------
5068
+ #region Add-ons
5069
+ #----------------------------------------------------------------------------------
5070
+
5071
+ /**
5072
+ * Check if add-on installed and activated on site.
5073
+ *
5074
+ * @author Vova Feldman (@svovaf)
5075
+ * @since 1.0.6
5076
+ *
5077
+ * @param string|number $id_or_slug
5078
+ * @param bool|null $is_premium Since 1.2.1.7 can check for specified add-on version.
5079
+ *
5080
+ * @return bool
5081
+ */
5082
+ function is_addon_activated( $id_or_slug, $is_premium = null ) {
5083
+ $this->_logger->entrance();
5084
+
5085
+ $addon_id = self::get_module_id( $id_or_slug );
5086
+ $is_activated = self::has_instance( $addon_id );
5087
+
5088
+ if ( ! $is_activated ) {
5089
+ return false;
5090
+ }
5091
+
5092
+ if ( is_bool( $is_premium ) ) {
5093
+ // Check if the specified code version is activate.
5094
+ $addon = $this->get_addon_instance( $addon_id );
5095
+ $is_activated = ( $is_premium === $addon->is_premium() );
5096
+ }
5097
+
5098
+ return $is_activated;
5099
+ }
5100
+
5101
+ /**
5102
+ * Check if add-on was connected to install
5103
+ *
5104
+ * @author Vova Feldman (@svovaf)
5105
+ * @since 1.1.7
5106
+ *
5107
+ * @param string|number $id_or_slug
5108
+ *
5109
+ * @return bool
5110
+ */
5111
+ function is_addon_connected( $id_or_slug ) {
5112
+ $this->_logger->entrance();
5113
+
5114
+ $sites = self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN );
5115
+
5116
+ $addon_id = self::get_module_id( $id_or_slug );
5117
+ $addon = $this->get_addon( $addon_id );
5118
+ $slug = $addon->slug;
5119
+ if ( ! isset( $sites[ $slug ] ) ) {
5120
+ return false;
5121
+ }
5122
+
5123
+ $site = $sites[ $slug ];
5124
+
5125
+ $plugin = FS_Plugin_Manager::instance( $addon_id )->get();
5126
+
5127
+ if ( $plugin->parent_plugin_id != $this->_plugin->id ) {
5128
+ // The given slug do NOT belong to any of the plugin's add-ons.
5129
+ return false;
5130
+ }
5131
+
5132
+ return ( is_object( $site ) &&
5133
+ is_numeric( $site->id ) &&
5134
+ is_numeric( $site->user_id ) &&
5135
+ FS_Plugin_Plan::is_valid_id( $site->plan_id )
5136
+ );
5137
+ }
5138
+
5139
+ /**
5140
+ * Determines if add-on installed.
5141
+ *
5142
+ * NOTE: This is a heuristic and only works if the folder/file named as the slug.
5143
+ *
5144
+ * @author Vova Feldman (@svovaf)
5145
+ * @since 1.0.6
5146
+ *
5147
+ * @param string|number $id_or_slug
5148
+ *
5149
+ * @return bool
5150
+ */
5151
+ function is_addon_installed( $id_or_slug ) {
5152
+ $this->_logger->entrance();
5153
+
5154
+ $addon_id = self::get_module_id( $id_or_slug );
5155
+
5156
+ return file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $this->get_addon_basename( $addon_id ) ) );
5157
+ }
5158
+
5159
+ /**
5160
+ * Get add-on basename.
5161
+ *
5162
+ * @author Vova Feldman (@svovaf)
5163
+ * @since 1.0.6
5164
+ *
5165
+ * @param string|number $id_or_slug
5166
+ *
5167
+ * @return string
5168
+ */
5169
+ function get_addon_basename( $id_or_slug ) {
5170
+ $addon_id = self::get_module_id( $id_or_slug );
5171
+
5172
+ if ( $this->is_addon_activated( $addon_id ) ) {
5173
+ return self::instance( $addon_id )->get_plugin_basename();
5174
+ }
5175
+
5176
+ $addon = $this->get_addon( $addon_id );
5177
+ $premium_basename = "{$addon->premium_slug}/{$addon->slug}.php";
5178
+
5179
+ if ( file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $premium_basename ) ) ) {
5180
+ return $premium_basename;
5181
+ }
5182
+
5183
+ $all_plugins = $this->get_all_plugins();
5184
+
5185
+ foreach ( $all_plugins as $basename => $data ) {
5186
+ if ( $addon->slug === $data['slug'] ||
5187
+ $addon->premium_slug === $data['slug']
5188
+ ) {
5189
+ return $basename;
5190
+ }
5191
+ }
5192
+
5193
+ $free_basename = "{$addon->slug}/{$addon->slug}.php";
5194
+
5195
+ return $free_basename;
5196
+ }
5197
+
5198
+ /**
5199
+ * Get installed add-ons instances.
5200
+ *
5201
+ * @author Vova Feldman (@svovaf)
5202
+ * @since 1.0.6
5203
+ *
5204
+ * @return Freemius[]
5205
+ */
5206
+ function get_installed_addons() {
5207
+ $installed_addons = array();
5208
+ foreach ( self::$_instances as $instance ) {
5209
+ if ( $instance->is_addon() && is_object( $instance->_parent_plugin ) ) {
5210
+ if ( $this->_plugin->id == $instance->_parent_plugin->id ) {
5211
+ $installed_addons[] = $instance;
5212
+ }
5213
+ }
5214
+ }
5215
+
5216
+ return $installed_addons;
5217
+ }
5218
+
5219
+ /**
5220
+ * Check if any add-ons of the plugin are installed.
5221
+ *
5222
+ * @author Leo Fajardo (@leorw)
5223
+ * @since 1.1.1
5224
+ *
5225
+ * @return bool
5226
+ */
5227
+ function has_installed_addons() {
5228
+ if ( ! $this->has_addons() ) {
5229
+ return false;
5230
+ }
5231
+
5232
+ foreach ( self::$_instances as $instance ) {
5233
+ if ( $instance->is_addon() && is_object( $instance->_parent_plugin ) ) {
5234
+ if ( $this->_plugin->id == $instance->_parent_plugin->id ) {
5235
+ return true;
5236
+ }
5237
+ }
5238
+ }
5239
+
5240
+ return false;
5241
+ }
5242
+
5243
+ /**
5244
+ * Tell Freemius that the current plugin is an add-on.
5245
+ *
5246
+ * @author Vova Feldman (@svovaf)
5247
+ * @since 1.0.6
5248
+ *
5249
+ * @param number $parent_plugin_id The parent plugin ID
5250
+ */
5251
+ function init_addon( $parent_plugin_id ) {
5252
+ $this->_plugin->parent_plugin_id = $parent_plugin_id;
5253
+ }
5254
+
5255
+ /**
5256
+ * @author Vova Feldman (@svovaf)
5257
+ * @since 1.0.6
5258
+ *
5259
+ * @return bool
5260
+ */
5261
+ function is_addon() {
5262
+ return isset( $this->_plugin->parent_plugin_id ) && is_numeric( $this->_plugin->parent_plugin_id );
5263
+ }
5264
+
5265
+ /**
5266
+ * Deactivate add-on if it's premium only and the user does't have a valid license.
5267
+ *
5268
+ * @param bool $is_after_trial_cancel
5269
+ *
5270
+ * @return bool If add-on was deactivated.
5271
+ */
5272
+ private function deactivate_premium_only_addon_without_license( $is_after_trial_cancel = false ) {
5273
+ if ( ! $this->has_free_plan() &&
5274
+ ! $this->has_features_enabled_license() &&
5275
+ ! $this->_has_premium_license()
5276
+ ) {
5277
+ if ( $this->is_registered() ) {
5278
+ // IF wrapper is turned off because activation_timestamp is currently only stored for plugins (not addons).
5279
+ // if (empty($this->_storage->activation_timestamp) ||
5280
+ // (WP_FS__SCRIPT_START_TIME - $this->_storage->activation_timestamp) > 30
5281
+ // ) {
5282
+ /**
5283
+ * @todo When it's first fail, there's no reason to try and re-sync because the licenses were just synced after initial activation.
5284
+ *
5285
+ * Retry syncing the user add-on licenses.
5286
+ */
5287
+ // Sync licenses.
5288
+ $this->_sync_licenses();
5289
+ // }
5290
+
5291
+ // Try to activate premium license.
5292
+ $this->_activate_license( true );
5293
+ }
5294
+
5295
+ if ( ! $this->has_free_plan() &&
5296
+ ! $this->has_features_enabled_license() &&
5297
+ ! $this->_has_premium_license()
5298
+ ) {
5299
+ // @todo Check if deactivate plugins also call the deactivation hook.
5300
+
5301
+ $this->_parent->_admin_notices->add_sticky(
5302
+ sprintf(
5303
+ ( $is_after_trial_cancel ?
5304
+ $this->_parent->get_text_inline(
5305
+ '%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.',
5306
+ 'addon-trial-cancelled-message'
5307
+ ) :
5308
+ $this->_parent->get_text_inline(
5309
+ '%s is a premium only add-on. You have to purchase a license first before activating the plugin.',
5310
+ 'addon-no-license-message'
5311
+ )
5312
+ ),
5313
+ '<b>' . $this->_plugin->title . '</b>'
5314
+ ) . ' ' . sprintf(
5315
+ '<a href="%s" aria-label="%s" class="button button-primary" style="margin-left: 10px; vertical-align: middle;">%s &nbsp;&#10140;</a>',
5316
+ $this->_parent->addon_url( $this->_slug ),
5317
+ esc_attr( sprintf( $this->_parent->get_text_inline( 'More information about %s', 'more-information-about-x' ), $this->_plugin->title ) ),
5318
+ $this->_parent->get_text_inline( 'Purchase License', 'purchase-license' )
5319
+ ),
5320
+ 'no_addon_license_' . $this->_slug,
5321
+ ( $is_after_trial_cancel ? '' : $this->_parent->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...' ),
5322
+ ( $is_after_trial_cancel ? 'success' : 'error' )
5323
+ );
5324
+
5325
+ deactivate_plugins( array( $this->_plugin_basename ), true );
5326
+
5327
+ return true;
5328
+ }
5329
+ }
5330
+
5331
+ return false;
5332
+ }
5333
+
5334
+ #endregion
5335
+
5336
+ #----------------------------------------------------------------------------------
5337
+ #region Sandbox
5338
+ #----------------------------------------------------------------------------------
5339
+
5340
+ /**
5341
+ * Set Freemius into sandbox mode for debugging.
5342
+ *
5343
+ * @author Vova Feldman (@svovaf)
5344
+ * @since 1.0.4
5345
+ *
5346
+ * @param string $secret_key
5347
+ */
5348
+ function init_sandbox( $secret_key ) {
5349
+ $this->_plugin->secret_key = $secret_key;
5350
+
5351
+ // Update plugin details.
5352
+ FS_Plugin_Manager::instance( $this->_module_id )->update( $this->_plugin, true );
5353
+ }
5354
+
5355
+ /**
5356
+ * Check if running payments in sandbox mode.
5357
+ *
5358
+ * @author Vova Feldman (@svovaf)
5359
+ * @since 1.0.4
5360
+ *
5361
+ * @return bool
5362
+ */
5363
+ function is_payments_sandbox() {
5364
+ return ( ! $this->is_live() ) || isset( $this->_plugin->secret_key );
5365
+ }
5366
+
5367
+ #endregion
5368
+
5369
+ /**
5370
+ * Check if running test vs. live plugin.
5371
+ *
5372
+ * @author Vova Feldman (@svovaf)
5373
+ * @since 1.0.5
5374
+ *
5375
+ * @return bool
5376
+ */
5377
+ function is_live() {
5378
+ return $this->_plugin->is_live;
5379
+ }
5380
+
5381
+ /**
5382
+ * Check if super-admin skipped connection for all sites in the network.
5383
+ *
5384
+ * @author Vova Feldman (@svovaf)
5385
+ * @since 2.0.0
5386
+ */
5387
+ function is_network_anonymous() {
5388
+ if ( ! $this->_is_network_active ) {
5389
+ return false;
5390
+ }
5391
+
5392
+ $is_anonymous_ms = $this->_storage->get( 'is_anonymous_ms' );
5393
+
5394
+ if ( empty( $is_anonymous_ms ) ) {
5395
+ return false;
5396
+ }
5397
+
5398
+ return $is_anonymous_ms['is'];
5399
+ }
5400
+
5401
+ /**
5402
+ * Check if super-admin opted-in for all sites in the network.
5403
+ *
5404
+ * @author Vova Feldman (@svovaf)
5405
+ * @since 2.0.0
5406
+ */
5407
+ function is_network_connected() {
5408
+ if ( ! $this->_is_network_active ) {
5409
+ return false;
5410
+ }
5411
+
5412
+ return $this->_storage->get( 'is_network_connected' );
5413
+ }
5414
+
5415
+ /**
5416
+ * Check if the user skipped connecting the account with Freemius.
5417
+ *
5418
+ * @author Vova Feldman (@svovaf)
5419
+ * @since 1.0.7
5420
+ *
5421
+ * @return bool
5422
+ */
5423
+ function is_anonymous() {
5424
+ if ( ! isset( $this->_is_anonymous ) ) {
5425
+ if ( $this->is_network_anonymous() ) {
5426
+ $this->_is_anonymous = true;
5427
+ } else if ( ! fs_is_network_admin() ) {
5428
+ if ( ! isset( $this->_storage->is_anonymous ) ) {
5429
+ // Not skipped.
5430
+ $this->_is_anonymous = false;
5431
+ } else if ( is_bool( $this->_storage->is_anonymous ) ) {
5432
+ // For back compatibility, since the variable was boolean before.
5433
+ $this->_is_anonymous = $this->_storage->is_anonymous;
5434
+
5435
+ // Upgrade stored data format to 1.1.3 format.
5436
+ $this->set_anonymous_mode( $this->_storage->is_anonymous );
5437
+ } else {
5438
+ // Version 1.1.3 and later.
5439
+ $this->_is_anonymous = $this->_storage->is_anonymous['is'];
5440
+ }
5441
+ }
5442
+ }
5443
+
5444
+ return $this->_is_anonymous;
5445
+ }
5446
+
5447
+ /**
5448
+ * Check if the user skipped the connection of a specified site.
5449
+ *
5450
+ * @author Vova Feldman (@svovaf)
5451
+ * @since 2.0.0
5452
+ *
5453
+ * @param int $blog_id
5454
+ *
5455
+ * @return bool
5456
+ */
5457
+ function is_anonymous_site( $blog_id = 0 ) {
5458
+ if ( $this->is_network_anonymous() ) {
5459
+ return true;
5460
+ }
5461
+
5462
+ $is_anonymous = $this->_storage->get( 'is_anonymous', false, $blog_id );
5463
+
5464
+ if ( empty( $is_anonymous ) ) {
5465
+ return false;
5466
+ }
5467
+
5468
+ return $is_anonymous['is'];
5469
+ }
5470
+
5471
+ /**
5472
+ * Check if user connected his account and install pending email activation.
5473
+ *
5474
+ * @author Vova Feldman (@svovaf)
5475
+ * @since 1.0.7
5476
+ *
5477
+ * @return bool
5478
+ */
5479
+ function is_pending_activation() {
5480
+ return $this->_storage->get( 'is_pending_activation', false );
5481
+ }
5482
+
5483
+ /**
5484
+ * Check if plugin must be WordPress.org compliant.
5485
+ *
5486
+ * @since 1.0.7
5487
+ *
5488
+ * @return bool
5489
+ */
5490
+ function is_org_repo_compliant() {
5491
+ return $this->_is_org_compliant;
5492
+ }
5493
+
5494
+ #--------------------------------------------------------------------------------
5495
+ #region WP Cron Common
5496
+ #--------------------------------------------------------------------------------
5497
+
5498
+ /**
5499
+ * @author Vova Feldman (@svovaf)
5500
+ * @since 2.0.0
5501
+ *
5502
+ * @param string $name Cron name.
5503
+ *
5504
+ * @return object
5505
+ */
5506
+ private function get_cron_data( $name ) {
5507
+ $this->_logger->entrance( $name );
5508
+
5509
+ /**
5510
+ * @var object $cron_data
5511
+ */
5512
+ return $this->_storage->get( "{$name}_cron", null );
5513
+ }
5514
+
5515
+ /**
5516
+ * @author Vova Feldman (@svovaf)
5517
+ * @since 2.0.0
5518
+ *
5519
+ * @param string $name Cron name.
5520
+ */
5521
+ private function clear_cron_data( $name ) {
5522
+ $this->_logger->entrance( $name );
5523
+
5524
+ $this->_storage->remove( "{$name}_cron" );
5525
+ }
5526
+
5527
+ /**
5528
+ * @author Vova Feldman (@svovaf)
5529
+ * @since 2.0.0
5530
+ *
5531
+ * @param string $name Cron name.
5532
+ * @param int $cron_blog_id The cron executing blog ID.
5533
+ */
5534
+ private function set_cron_data( $name, $cron_blog_id = 0 ) {
5535
+ $this->_logger->entrance( $name );
5536
+
5537
+ $this->_storage->store( "{$name}_cron", (object) array(
5538
+ 'version' => $this->get_plugin_version(),
5539
+ 'blog_id' => $cron_blog_id,
5540
+ 'sdk_version' => $this->version,
5541
+ 'timestamp' => WP_FS__SCRIPT_START_TIME,
5542
+ 'on' => true,
5543
+ ) );
5544
+ }
5545
+
5546
+ /**
5547
+ * Get the cron's executing blog ID.
5548
+ *
5549
+ * @author Vova Feldman (@svovaf)
5550
+ * @since 2.0.0
5551
+ *
5552
+ * @param string $name Cron name.
5553
+ *
5554
+ * @return int
5555
+ */
5556
+ private function get_cron_blog_id( $name ) {
5557
+ $this->_logger->entrance( $name );
5558
+
5559
+ /**
5560
+ * @var object $cron_data
5561
+ */
5562
+ $cron_data = $this->get_cron_data( $name );
5563
+
5564
+ return ( is_object( $cron_data ) && is_numeric( $cron_data->blog_id ) ) ?
5565
+ $cron_data->blog_id :
5566
+ 0;
5567
+ }
5568
+
5569
+ /**
5570
+ * @author Vova Feldman (@svovaf)
5571
+ * @since 2.0.0
5572
+ *
5573
+ * @param string $name Cron name.
5574
+ *
5575
+ * @return bool
5576
+ */
5577
+ private function is_cron_on( $name ) {
5578
+ $this->_logger->entrance( $name );
5579
+
5580
+ /**
5581
+ * @var object $cron_data
5582
+ */
5583
+ $cron_data = $this->get_cron_data( $name );
5584
+
5585
+ return ( ! is_null( $cron_data ) && true === $cron_data->on );
5586
+ }
5587
+
5588
+ /**
5589
+ * Unix timestamp for previous cron execution or false if never executed.
5590
+ *
5591
+ * @author Vova Feldman (@svovaf)
5592
+ * @since 2.0.0
5593
+ *
5594
+ * @param string $name Cron name.
5595
+ *
5596
+ * @return int|false
5597
+ */
5598
+ private function cron_last_execution( $name ) {
5599
+ $this->_logger->entrance( $name );
5600
+
5601
+ return $this->_storage->get( "{$name}_timestamp" );
5602
+ }
5603
+
5604
+ /**
5605
+ * Set cron execution time to now.
5606
+ *
5607
+ * @author Vova Feldman (@svovaf)
5608
+ * @since 2.0.0
5609
+ *
5610
+ * @param string $name Cron name.
5611
+ */
5612
+ private function set_cron_execution_timestamp( $name ) {
5613
+ $this->_logger->entrance( $name );
5614
+
5615
+ $this->_storage->store( "{$name}_timestamp", time() );
5616
+ }
5617
+
5618
+ /**
5619
+ * Check if cron was executed in the last $period of seconds.
5620
+ *
5621
+ * @author Vova Feldman (@svovaf)
5622
+ * @since 2.0.0
5623
+ *
5624
+ * @param string $name Cron name.
5625
+ * @param int $period In seconds
5626
+ *
5627
+ * @return bool
5628
+ */
5629
+ private function is_cron_executed( $name, $period = WP_FS__TIME_24_HOURS_IN_SEC ) {
5630
+ $this->_logger->entrance( $name );
5631
+
5632
+ $last_execution = $this->set_cron_execution_timestamp( $name );
5633
+
5634
+ if ( ! is_numeric( $last_execution ) ) {
5635
+ return false;
5636
+ }
5637
+
5638
+ return ( $last_execution > ( WP_FS__SCRIPT_START_TIME - $period ) );
5639
+ }
5640
+
5641
+ /**
5642
+ * WP Cron is executed on a site level. When running in a multisite network environment
5643
+ * with the network integration activated, for optimization reasons, we are consolidating
5644
+ * the installs data sync cron to be executed only from a single site.
5645
+ *
5646
+ * @author Vova Feldman (@svovaf)
5647
+ * @since 2.0.0
5648
+ *
5649
+ * @param int $except_blog_id Target any except the excluded blog ID.
5650
+ *
5651
+ * @return int
5652
+ */
5653
+ private function get_cron_target_blog_id( $except_blog_id = 0 ) {
5654
+ if ( ! is_multisite() ) {
5655
+ return 0;
5656
+ }
5657
+
5658
+ if ( $this->_is_network_active &&
5659
+ is_numeric( $this->_storage->network_install_blog_id ) &&
5660
+ $except_blog_id != $this->_storage->network_install_blog_id &&
5661
+ self::is_site_active( $this->_storage->network_install_blog_id )
5662
+ ) {
5663
+ // Try to run cron from the main network blog.
5664
+ $install = $this->get_install_by_blog_id( $this->_storage->network_install_blog_id );
5665
+
5666
+ if ( is_object( $install ) &&
5667
+ ( $this->is_premium() || $install->is_tracking_allowed() )
5668
+ ) {
5669
+ return $this->_storage->network_install_blog_id;
5670
+ }
5671
+ }
5672
+
5673
+ // Get first opted-in blog ID with active tracking.
5674
+ $installs = $this->get_blog_install_map();
5675
+ foreach ( $installs as $blog_id => $install ) {
5676
+ if ( $except_blog_id != $blog_id &&
5677
+ self::is_site_active( $blog_id ) &&
5678
+ ( $this->is_premium() || $install->is_tracking_allowed() )
5679
+ ) {
5680
+ return $blog_id;
5681
+ }
5682
+ }
5683
+
5684
+ return 0;
5685
+ }
5686
+
5687
+ /**
5688
+ * @author Vova Feldman (@svovaf)
5689
+ * @since 2.0.0
5690
+ *
5691
+ * @param string $name Cron name.
5692
+ * @param string $action_tag Callback action tag.
5693
+ * @param bool $is_network_clear If set to TRUE, clear sync cron even if there are installs that are still connected.
5694
+ */
5695
+ private function clear_cron( $name, $action_tag = '', $is_network_clear = false ) {
5696
+ $this->_logger->entrance( $name );
5697
+
5698
+ if ( ! $this->is_cron_on( $name ) ) {
5699
+ return;
5700
+ }
5701
+
5702
+ $clear_cron = true;
5703
+ if ( ! $is_network_clear && $this->_is_network_active ) {
5704
+ $installs = $this->get_blog_install_map();
5705
+
5706
+ foreach ( $installs as $blog_id => $install ) {
5707
+ /**
5708
+ * @var FS_Site $install
5709
+ */
5710
+ if ( $install->is_tracking_allowed() ) {
5711
+ $clear_cron = false;
5712
+ break;
5713
+ }
5714
+ }
5715
+ }
5716
+
5717
+ if ( ! $clear_cron ) {
5718
+ return;
5719
+ }
5720
+
5721
+ /**
5722
+ * @var object $cron_data
5723
+ */
5724
+ $cron_data = $this->get_cron_data( $name );
5725
+
5726
+ $cron_blog_id = is_object( $cron_data ) && isset( $cron_data->blog_id ) ?
5727
+ $cron_data->blog_id :
5728
+ 0;
5729
+
5730
+ $this->clear_cron_data( $name );
5731
+
5732
+ if ( 0 < $cron_blog_id ) {
5733
+ switch_to_blog( $cron_blog_id );
5734
+ }
5735
+
5736
+ if ( empty( $action_tag ) ) {
5737
+ $action_tag = $name;
5738
+ }
5739
+
5740
+ wp_clear_scheduled_hook( $this->get_action_tag( $action_tag ) );
5741
+
5742
+ if ( 0 < $cron_blog_id ) {
5743
+ restore_current_blog();
5744
+ }
5745
+ }
5746
+
5747
+ /**
5748
+ * Unix timestamp for next cron execution or false if not scheduled.
5749
+ *
5750
+ * @author Vova Feldman (@svovaf)
5751
+ * @since 2.0.0
5752
+ *
5753
+ * @param string $name Cron name.
5754
+ * @param string $action_tag Callback action tag.
5755
+ *
5756
+ * @return int|false
5757
+ */
5758
+ private function get_next_scheduled_cron( $name, $action_tag = '' ) {
5759
+ $this->_logger->entrance( $name );
5760
+
5761
+ if ( ! $this->is_cron_on( $name ) ) {
5762
+ return false;
5763
+ }
5764
+
5765
+ /**
5766
+ * @var object $cron_data
5767
+ */
5768
+ $cron_data = $this->get_cron_data( $name );
5769
+
5770
+ $cron_blog_id = is_object( $cron_data ) && isset( $cron_data->blog_id ) ?
5771
+ $cron_data->blog_id :
5772
+ 0;
5773
+
5774
+ if ( 0 < $cron_blog_id ) {
5775
+ switch_to_blog( $cron_blog_id );
5776
+ }
5777
+
5778
+ if ( empty( $action_tag ) ) {
5779
+ $action_tag = $name;
5780
+ }
5781
+
5782
+ $next_scheduled = wp_next_scheduled( $this->get_action_tag( $action_tag ) );
5783
+
5784
+ if ( 0 < $cron_blog_id ) {
5785
+ restore_current_blog();
5786
+ }
5787
+
5788
+ return $next_scheduled;
5789
+ }
5790
+
5791
+ /**
5792
+ * @author Vova Feldman (@svovaf)
5793
+ * @since 2.0.0
5794
+ *
5795
+ * @param string $name Cron name.
5796
+ * @param string $action_tag Callback action tag.
5797
+ * @param string $recurrence 'single' or 'daily'.
5798
+ * @param int $start_at Defaults to now.
5799
+ * @param bool $randomize_start If true, schedule first job randomly during the next 12 hours. Otherwise, schedule job to start right away.
5800
+ * @param int $except_blog_id Target any except the excluded blog ID.
5801
+ */
5802
+ private function schedule_cron(
5803
+ $name,
5804
+ $action_tag = '',
5805
+ $recurrence = 'single',
5806
+ $start_at = WP_FS__SCRIPT_START_TIME,
5807
+ $randomize_start = true,
5808
+ $except_blog_id = 0
5809
+ ) {
5810
+ $this->_logger->entrance( $name );
5811
+
5812
+ $this->clear_cron( $name, $action_tag, true );
5813
+
5814
+ $cron_blog_id = $this->get_cron_target_blog_id( $except_blog_id );
5815
+
5816
+ if ( is_multisite() && 0 == $cron_blog_id ) {
5817
+ // Don't schedule cron since couldn't find a target blog.
5818
+ return;
5819
+ }
5820
+
5821
+ if ( 0 < $cron_blog_id ) {
5822
+ switch_to_blog( $cron_blog_id );
5823
+ }
5824
+
5825
+ if ( 'daily' === $recurrence ) {
5826
+ if ( $randomize_start ) {
5827
+ // Schedule first sync with a random 12 hour time range from now.
5828
+ $start_at += rand( 0, ( WP_FS__TIME_24_HOURS_IN_SEC / 2 ) );
5829
+ }
5830
+
5831
+ // Schedule daily WP cron.
5832
+ wp_schedule_event(
5833
+ $start_at,
5834
+ 'daily',
5835
+ $this->get_action_tag( $action_tag )
5836
+ );
5837
+ } else if ( 'single' === $recurrence ) {
5838
+ // Schedule single cron.
5839
+ wp_schedule_single_event(
5840
+ $start_at,
5841
+ $this->get_action_tag( $action_tag )
5842
+ );
5843
+ }
5844
+
5845
+ $this->set_cron_data( $name, $cron_blog_id );
5846
+
5847
+ if ( 0 < $cron_blog_id ) {
5848
+ restore_current_blog();
5849
+ }
5850
+ }
5851
+
5852
+ /**
5853
+ * Consolidated cron execution for performance optimization. The max number of API requests is based on the number of unique opted-in users.
5854
+ * that doesn't halt page loading.
5855
+ *
5856
+ * @author Vova Feldman (@svovaf)
5857
+ * @since 2.0.0
5858
+ *
5859
+ * @param string $name Cron name.
5860
+ * @param callable $callable The function that should be executed.
5861
+ */
5862
+ private function execute_cron( $name, $callable ) {
5863
+ $this->_logger->entrance( $name );
5864
+
5865
+ // Store the last time data sync was executed.
5866
+ $this->set_cron_execution_timestamp( $name );
5867
+
5868
+ // Check if API is temporary down.
5869
+ if ( FS_Api::is_temporary_down() ) {
5870
+ return;
5871
+ }
5872
+
5873
+ // @todo Add logic that identifies API latency, and reschedule the next background sync randomly between 8-16 hours.
5874
+
5875
+ $users_2_blog_ids = array();
5876
+
5877
+ if ( ! is_multisite() ) {
5878
+ // Add dummy blog.
5879
+ $users_2_blog_ids[0] = array( 0 );
5880
+ } else {
5881
+ $installs = $this->get_blog_install_map();
5882
+ foreach ( $installs as $blog_id => $install ) {
5883
+ if ( $this->is_premium() || $install->is_tracking_allowed() ) {
5884
+ if ( ! isset( $users_2_blog_ids[ $install->user_id ] ) ) {
5885
+ $users_2_blog_ids[ $install->user_id ] = array();
5886
+ }
5887
+
5888
+ $users_2_blog_ids[ $install->user_id ][] = $blog_id;
5889
+ }
5890
+ }
5891
+ }
5892
+
5893
+ $current_blog_id = get_current_blog_id();
5894
+
5895
+ foreach ( $users_2_blog_ids as $user_id => $blog_ids ) {
5896
+ if ( 0 < $blog_ids[0] ) {
5897
+ $this->switch_to_blog( $blog_ids[0] );
5898
+ }
5899
+
5900
+ call_user_func_array( $callable, array( $blog_ids ) );
5901
+
5902
+ foreach ( $blog_ids as $blog_id ) {
5903
+ $this->do_action( "after_{$name}_cron", $blog_id );
5904
+ }
5905
+ }
5906
+
5907
+ if ( is_multisite() ) {
5908
+ $this->switch_to_blog( $current_blog_id );
5909
+
5910
+ $this->do_action( "after_{$name}_cron_multisite" );
5911
+ }
5912
+ }
5913
+
5914
+ #endregion
5915
+
5916
+ #----------------------------------------------------------------------------------
5917
+ #region Daily Sync Cron
5918
+ #----------------------------------------------------------------------------------
5919
+
5920
+
5921
+ /**
5922
+ * @author Vova Feldman (@svovaf)
5923
+ * @since 2.0.0
5924
+ *
5925
+ * @return bool
5926
+ */
5927
+ private function is_sync_cron_scheduled() {
5928
+ return $this->is_cron_on( 'sync' );
5929
+ }
5930
+
5931
+ /**
5932
+ * Get the sync cron's executing blog ID.
5933
+ *
5934
+ * @author Vova Feldman (@svovaf)
5935
+ * @since 2.0.0
5936
+ *
5937
+ * @return int
5938
+ */
5939
+ private function get_sync_cron_blog_id() {
5940
+ return $this->get_cron_blog_id( 'sync' );
5941
+ }
5942
+
5943
+ /**
5944
+ * @author Vova Feldman (@svovaf)
5945
+ * @since 1.1.7.3
5946
+ */
5947
+ private function run_manual_sync() {
5948
+ self::require_pluggable_essentials();
5949
+
5950
+ if ( ! $this->is_user_admin() ) {
5951
+ return;
5952
+ }
5953
+
5954
+ // Run manual sync.
5955
+ $this->_sync_cron();
5956
+
5957
+ // Reschedule next cron to run 24 hours from now (performance optimization).
5958
+ $this->schedule_sync_cron( time() + WP_FS__TIME_24_HOURS_IN_SEC, false );
5959
+ }
5960
+
5961
+ /**
5962
+ * Data sync cron job. Replaces the background sync non blocking HTTP request
5963
+ * that doesn't halt page loading.
5964
+ *
5965
+ * @author Vova Feldman (@svovaf)
5966
+ * @since 1.1.7.3
5967
+ * @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.
5968
+ */
5969
+ function _sync_cron() {
5970
+ $this->_logger->entrance();
5971
+
5972
+ $this->execute_cron( 'sync', array( &$this, '_sync_cron_method' ) );
5973
+ }
5974
+
5975
+ /**
5976
+ * The actual data sync cron logic.
5977
+ *
5978
+ * @author Vova Feldman (@svovaf)
5979
+ * @since 2.0.0
5980
+ *
5981
+ * @param int[] $blog_ids
5982
+ */
5983
+ function _sync_cron_method( array $blog_ids ) {
5984
+ if ( $this->is_registered() ) {
5985
+ if ( $this->has_paid_plan() ) {
5986
+ // Initiate background plan sync.
5987
+ $this->_sync_license( true );
5988
+
5989
+ if ( $this->is_paying() ) {
5990
+ // Check for premium plugin updates.
5991
+ $this->check_updates( true );
5992
+ }
5993
+ } else {
5994
+ // Sync install(s) (only if something changed locally).
5995
+ if ( 1 < count( $blog_ids ) ) {
5996
+ $this->sync_installs();
5997
+ } else {
5998
+ $this->sync_install();
5999
+ }
6000
+ }
6001
+ }
6002
+ }
6003
+
6004
+ /**
6005
+ * Check if sync was executed in the last $period of seconds.
6006
+ *
6007
+ * @author Vova Feldman (@svovaf)
6008
+ * @since 1.1.7.3
6009
+ *
6010
+ * @param int $period In seconds
6011
+ *
6012
+ * @return bool
6013
+ */
6014
+ private function is_sync_executed( $period = WP_FS__TIME_24_HOURS_IN_SEC ) {
6015
+ return $this->is_cron_executed( 'sync', $period );
6016
+ }
6017
+
6018
+ /**
6019
+ * @author Vova Feldman (@svovaf)
6020
+ * @since 1.1.7.3
6021
+ *
6022
+ * @return bool
6023
+ */
6024
+ private function is_sync_cron_on() {
6025
+ return $this->is_cron_on( 'sync' );
6026
+ }
6027
+
6028
+ /**
6029
+ * @author Vova Feldman (@svovaf)
6030
+ * @since 1.1.7.3
6031
+ *
6032
+ * @param int $start_at Defaults to now.
6033
+ * @param bool $randomize_start If true, schedule first job randomly during the next 12 hours. Otherwise, schedule job to start right away.
6034
+ * @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.
6035
+ */
6036
+ private function schedule_sync_cron(
6037
+ $start_at = WP_FS__SCRIPT_START_TIME,
6038
+ $randomize_start = true,
6039
+ $except_blog_id = 0
6040
+ ) {
6041
+ $this->schedule_cron(
6042
+ 'sync',
6043
+ 'data_sync',
6044
+ 'daily',
6045
+ $start_at,
6046
+ $randomize_start,
6047
+ $except_blog_id
6048
+ );
6049
+ }
6050
+
6051
+ /**
6052
+ * Add the actual sync function to the cron job hook.
6053
+ *
6054
+ * @author Vova Feldman (@svovaf)
6055
+ * @since 1.1.7.3
6056
+ */
6057
+ private function hook_callback_to_sync_cron() {
6058
+ $this->add_action( 'data_sync', array( &$this, '_sync_cron' ) );
6059
+ }
6060
+
6061
+ /**
6062
+ * @author Vova Feldman (@svovaf)
6063
+ * @since 1.1.7.3
6064
+ *
6065
+ * @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.
6066
+ */
6067
+ private function clear_sync_cron( $is_network_clear = false ) {
6068
+ $this->_logger->entrance();
6069
+
6070
+ $this->clear_cron( 'sync', 'data_sync', $is_network_clear );
6071
+ }
6072
+
6073
+ /**
6074
+ * Unix timestamp for next sync cron execution or false if not scheduled.
6075
+ *
6076
+ * @author Vova Feldman (@svovaf)
6077
+ * @since 1.1.7.3
6078
+ *
6079
+ * @return int|false
6080
+ */
6081
+ function next_sync_cron() {
6082
+ return $this->get_next_scheduled_cron( 'sync', 'data_sync' );
6083
+ }
6084
+
6085
+ /**
6086
+ * Unix timestamp for previous sync cron execution or false if never executed.
6087
+ *
6088
+ * @author Vova Feldman (@svovaf)
6089
+ * @since 1.1.7.3
6090
+ *
6091
+ * @return int|false
6092
+ */
6093
+ function last_sync_cron() {
6094
+ return $this->cron_last_execution( 'sync' );
6095
+ }
6096
+
6097
+ #endregion Daily Sync Cron ------------------------------------------------------------------
6098
+
6099
+ #----------------------------------------------------------------------------------
6100
+ #region Async Install Sync
6101
+ #----------------------------------------------------------------------------------
6102
+
6103
+ /**
6104
+ * @author Vova Feldman (@svovaf)
6105
+ * @since 1.1.7.3
6106
+ *
6107
+ * @return bool
6108
+ */
6109
+ private function is_install_sync_scheduled() {
6110
+ return $this->is_cron_on( 'install_sync' );
6111
+ }
6112
+
6113
+ /**
6114
+ * Get the sync cron's executing blog ID.
6115
+ *
6116
+ * @author Vova Feldman (@svovaf)
6117
+ * @since 2.0.0
6118
+ *
6119
+ * @return int
6120
+ */
6121
+ private function get_install_sync_cron_blog_id() {
6122
+ return $this->get_cron_blog_id( 'install_sync' );
6123
+ }
6124
+
6125
+ /**
6126
+ * Instead of running blocking install sync event, execute non blocking scheduled wp-cron.
6127
+ *
6128
+ * @author Vova Feldman (@svovaf)
6129
+ * @since 1.1.7.3
6130
+ *
6131
+ * @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.
6132
+ */
6133
+ private function schedule_install_sync( $except_blog_id = 0 ) {
6134
+ $this->schedule_cron( 'install_sync', 'install_sync', 'single', WP_FS__SCRIPT_START_TIME, false, $except_blog_id );
6135
+ }
6136
+
6137
+ /**
6138
+ * Unix timestamp for previous install sync cron execution or false if never executed.
6139
+ *
6140
+ * @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.
6141
+ *
6142
+ * @author Vova Feldman (@svovaf)
6143
+ * @since 1.1.7.3
6144
+ *
6145
+ * @return int|false
6146
+ */
6147
+ function last_install_sync() {
6148
+ return $this->cron_last_execution( 'install_sync' );
6149
+ }
6150
+
6151
+ /**
6152
+ * Unix timestamp for next install sync cron execution or false if not scheduled.
6153
+ *
6154
+ * @author Vova Feldman (@svovaf)
6155
+ * @since 1.1.7.3
6156
+ *
6157
+ * @return int|false
6158
+ */
6159
+ function next_install_sync() {
6160
+ return $this->get_next_scheduled_cron( 'install_sync', 'install_sync' );
6161
+ }
6162
+
6163
+ /**
6164
+ * Add the actual install sync function to the cron job hook.
6165
+ *
6166
+ * @author Vova Feldman (@svovaf)
6167
+ * @since 1.1.7.3
6168
+ */
6169
+ private function hook_callback_to_install_sync() {
6170
+ $this->add_action( 'install_sync', array( &$this, '_run_sync_install' ) );
6171
+ }
6172
+
6173
+ /**
6174
+ * @author Vova Feldman (@svovaf)
6175
+ * @since 1.1.7.3
6176
+ *
6177
+ * @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.
6178
+ */
6179
+ private function clear_install_sync_cron( $is_network_clear = false ) {
6180
+ $this->_logger->entrance();
6181
+
6182
+ $this->clear_cron( 'install_sync', 'install_sync', $is_network_clear );
6183
+ }
6184
+
6185
+ /**
6186
+ * @author Vova Feldman (@svovaf)
6187
+ * @since 1.1.7.3
6188
+ * @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.
6189
+ */
6190
+ public function _run_sync_install() {
6191
+ $this->_logger->entrance();
6192
+
6193
+ $this->execute_cron( 'sync', array( &$this, '_sync_install_cron_method' ) );
6194
+ }
6195
+
6196
+ /**
6197
+ * The actual install(s) sync cron logic.
6198
+ *
6199
+ * @author Vova Feldman (@svovaf)
6200
+ * @since 2.0.0
6201
+ *
6202
+ * @param int[] $blog_ids
6203
+ */
6204
+ function _sync_install_cron_method( array $blog_ids ) {
6205
+ if ( $this->is_registered() ) {
6206
+ if ( 1 < count( $blog_ids ) ) {
6207
+ $this->sync_installs( array(), true );
6208
+ } else {
6209
+ $this->sync_install( array(), true );
6210
+ }
6211
+ }
6212
+ }
6213
+
6214
+ #endregion Async Install Sync ------------------------------------------------------------------
6215
+
6216
+ /**
6217
+ * Show a notice that activation is currently pending.
6218
+ *
6219
+ * @author Vova Feldman (@svovaf)
6220
+ * @since 1.0.7
6221
+ *
6222
+ * @param bool|string $email
6223
+ * @param bool $is_pending_trial Since 1.2.1.5
6224
+ */
6225
+ function _add_pending_activation_notice( $email = false, $is_pending_trial = false ) {
6226
+ if ( ! is_string( $email ) ) {
6227
+ $current_user = self::_get_current_wp_user();
6228
+ $email = $current_user->user_email;
6229
+ }
6230
+
6231
+ $this->_admin_notices->add_sticky(
6232
+ sprintf(
6233
+ $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' ),
6234
+ '<b>' . $this->get_plugin_name() . '</b>',
6235
+ '<b>' . $email . '</b>',
6236
+ ( $is_pending_trial ?
6237
+ $this->get_text_inline( 'start the trial', 'start-the-trial' ) :
6238
+ $this->get_text_inline( 'complete the install', 'complete-the-install' ) )
6239
+ ),
6240
+ 'activation_pending',
6241
+ 'Thanks!'
6242
+ );
6243
+ }
6244
+
6245
+ /**
6246
+ * Check if currently in plugin activation.
6247
+ *
6248
+ * @author Vova Feldman (@svovaf)
6249
+ * @since 1.1.4
6250
+ *
6251
+ * @return bool
6252
+ */
6253
+ function is_plugin_activation() {
6254
+ $result = get_transient( "fs_{$this->_module_type}_{$this->_slug}_activated" );
6255
+
6256
+ return !empty($result);
6257
+ }
6258
+
6259
+ /**
6260
+ *
6261
+ * NOTE: admin_menu action executed before admin_init.
6262
+ *
6263
+ * @author Vova Feldman (@svovaf)
6264
+ * @since 1.0.7
6265
+ */
6266
+ function _admin_init_action() {
6267
+ /**
6268
+ * Automatically redirect to connect/activation page after plugin activation.
6269
+ *
6270
+ * @since 1.1.7 Do NOT redirect to opt-in when running in network admin mode.
6271
+ */
6272
+ if ( $this->is_plugin_activation() ) {
6273
+ delete_transient( "fs_{$this->_module_type}_{$this->_slug}_activated" );
6274
+
6275
+ if ( isset( $_GET['activate-multi'] ) ) {
6276
+ /**
6277
+ * Don't redirect if activating multiple plugins at once (bulk activation).
6278
+ */
6279
+ } else {
6280
+ $this->_redirect_on_activation_hook();
6281
+ return;
6282
+ }
6283
+ }
6284
+
6285
+ if ( fs_request_is_action( $this->get_unique_affix() . '_skip_activation' ) ) {
6286
+ check_admin_referer( $this->get_unique_affix() . '_skip_activation' );
6287
+
6288
+ $this->skip_connection( null, fs_is_network_admin() );
6289
+
6290
+ fs_redirect( $this->get_after_activation_url( 'after_skip_url' ) );
6291
+ }
6292
+
6293
+ if ( $this->is_network_activation_mode() &&
6294
+ fs_request_is_action( $this->get_unique_affix() . '_delegate_activation' )
6295
+ ) {
6296
+ check_admin_referer( $this->get_unique_affix() . '_delegate_activation' );
6297
+
6298
+ $this->delegate_connection();
6299
+
6300
+ fs_redirect( $this->get_after_activation_url( 'after_delegation_url' ) );
6301
+ }
6302
+
6303
+ $this->_add_upgrade_action_link();
6304
+
6305
+ if ( ! $this->is_addon() &&
6306
+ ! ( ! $this->_is_network_active && fs_is_network_admin() ) &&
6307
+ (
6308
+ ( true === $this->_storage->require_license_activation ) ||
6309
+ // Not registered nor anonymous.
6310
+ ( ! $this->is_registered() && ! $this->is_anonymous() ) ||
6311
+ // OR, network level and in network upgrade mode.
6312
+ ( fs_is_network_admin() && $this->_is_network_active && $this->is_network_upgrade_mode() )
6313
+ )
6314
+ ) {
6315
+ if ( ! $this->is_pending_activation() ) {
6316
+ if ( ! $this->_menu->is_main_settings_page() ) {
6317
+ /**
6318
+ * If a user visits any other admin page before activating the premium-only theme with a valid
6319
+ * license, reactivate the previous theme.
6320
+ *
6321
+ * @author Leo Fajardo (@leorw)
6322
+ * @since 1.2.2
6323
+ */
6324
+ if ( $this->is_theme()
6325
+ && ! $this->has_settings_menu()
6326
+ && ! isset( $_REQUEST['fs_action'] )
6327
+ && $this->can_activate_previous_theme()
6328
+ ) {
6329
+ if ( $this->is_only_premium() ) {
6330
+ $this->activate_previous_theme();
6331
+ return;
6332
+ }
6333
+
6334
+ if ( true === $this->_storage->require_license_activation ) {
6335
+ $this->_storage->require_license_activation = false;
6336
+ }
6337
+ }
6338
+
6339
+ if ( ! fs_is_network_admin() &&
6340
+ $this->is_network_activation_mode() &&
6341
+ ! $this->is_delegated_connection()
6342
+ ) {
6343
+ return;
6344
+ }
6345
+
6346
+ if ( $this->is_plugin_new_install() || $this->is_only_premium() ) {
6347
+ if ( ! $this->_anonymous_mode ) {
6348
+ // Show notice for new plugin installations.
6349
+ $this->_admin_notices->add(
6350
+ sprintf(
6351
+ $this->get_text_inline( 'You are just one step away - %s', 'you-are-step-away' ),
6352
+ sprintf( '<b><a href="%s">%s</a></b>',
6353
+ $this->get_activation_url( array(), ! $this->is_delegated_connection() ),
6354
+ sprintf( $this->get_text_x_inline( 'Complete "%s" Activation Now',
6355
+ '%s - plugin name. As complete "PluginX" activation now', 'activate-x-now' ), $this->get_plugin_name() )
6356
+ )
6357
+ ),
6358
+ '',
6359
+ 'update-nag'
6360
+ );
6361
+ }
6362
+ } else {
6363
+ if ( $this->should_add_sticky_optin_notice() ) {
6364
+ $this->add_sticky_optin_admin_notice();
6365
+ }
6366
+
6367
+ if ( $this->has_filter( 'optin_pointer_element' ) ) {
6368
+ // Don't show admin nag if plugin update.
6369
+ wp_enqueue_script( 'wp-pointer' );
6370
+ wp_enqueue_style( 'wp-pointer' );
6371
+
6372
+ $this->_enqueue_connect_essentials();
6373
+
6374
+ add_action( 'admin_print_footer_scripts', array(
6375
+ $this,
6376
+ '_add_connect_pointer_script'
6377
+ ) );
6378
+ }
6379
+ }
6380
+ }
6381
+ }
6382
+
6383
+ if ( $this->is_theme() &&
6384
+ $this->_menu->is_main_settings_page()
6385
+ ) {
6386
+ $this->_show_theme_activation_optin_dialog();
6387
+ }
6388
+ }
6389
+ }
6390
+
6391
+ /**
6392
+ * @author Vova Feldman (@svovaf)
6393
+ * @since 2.0.0
6394
+ *
6395
+ * @return bool
6396
+ */
6397
+ private function should_add_sticky_optin_notice() {
6398
+ if ( fs_is_network_admin() ) {
6399
+ if ( ! $this->_is_network_active ) {
6400
+ return false;
6401
+ }
6402
+
6403
+ if ( ! $this->is_network_activation_mode() ) {
6404
+ return false;
6405
+ }
6406
+
6407
+ return ! isset( $this->_storage->sticky_optin_added_ms );
6408
+ }
6409
+
6410
+ if ( ! $this->is_activation_mode() ) {
6411
+ return false;
6412
+ }
6413
+
6414
+ // If running from a blog admin and delegated the connection.
6415
+ return ! isset( $this->_storage->sticky_optin_added );
6416
+ }
6417
+
6418
+ /**
6419
+ * @author Leo Fajardo (@leorw)
6420
+ * @since 2.0.0
6421
+ */
6422
+ private function add_sticky_optin_admin_notice() {
6423
+ if ( ! $this->_is_network_active || ! fs_is_network_admin() ) {
6424
+ $this->_storage->sticky_optin_added = true;
6425
+ } else {
6426
+ $this->_storage->sticky_optin_added_ms = true;
6427
+ }
6428
+
6429
+ // Show notice for new plugin installations.
6430
+ $this->_admin_notices->add_sticky(
6431
+ sprintf(
6432
+ $this->get_text_inline( 'We made a few tweaks to the %s, %s', 'few-plugin-tweaks' ),
6433
+ $this->_module_type,
6434
+ sprintf( '<b><a href="%s">%s</a></b>',
6435
+ $this->get_activation_url(),
6436
+ sprintf( $this->get_text_inline( 'Opt in to make "%s" better!', 'optin-x-now' ), $this->get_plugin_name() )
6437
+ )
6438
+ ),
6439
+ 'connect_account',
6440
+ '',
6441
+ 'update-nag'
6442
+ );
6443
+ }
6444
+
6445
+ /**
6446
+ * Enqueue connect requires scripts and styles.
6447
+ *
6448
+ * @author Vova Feldman (@svovaf)
6449
+ * @since 1.1.4
6450
+ */
6451
+ function _enqueue_connect_essentials() {
6452
+ wp_enqueue_script( 'jquery' );
6453
+ wp_enqueue_script( 'json2' );
6454
+
6455
+ fs_enqueue_local_script( 'postmessage', 'nojquery.ba-postmessage.min.js' );
6456
+ fs_enqueue_local_script( 'fs-postmessage', 'postmessage.js' );
6457
+
6458
+ fs_enqueue_local_style( 'fs_connect', '/admin/connect.css' );
6459
+ }
6460
+
6461
+ /**
6462
+ * Add connect / opt-in pointer.
6463
+ *
6464
+ * @author Vova Feldman (@svovaf)
6465
+ * @since 1.1.4
6466
+ */
6467
+ function _add_connect_pointer_script() {
6468
+ $vars = array( 'id' => $this->_module_id );
6469
+ $pointer_content = fs_get_template( 'connect.php', $vars );
6470
+ ?>
6471
+ <script type="text/javascript">// <![CDATA[
6472
+ jQuery(document).ready(function ($) {
6473
+ if ('undefined' !== typeof(jQuery().pointer)) {
6474
+
6475
+ var element = <?php echo $this->apply_filters( 'optin_pointer_element', '$("#non_existing_element");' ) ?>;
6476
+
6477
+ if (element.length > 0) {
6478
+ var optin = $(element).pointer($.extend(true, {}, {
6479
+ content : <?php echo json_encode( $pointer_content ) ?>,
6480
+ position : {
6481
+ edge : 'left',
6482
+ align: 'center'
6483
+ },
6484
+ buttons : function () {
6485
+ // Don't show pointer buttons.
6486
+ return '';
6487
+ },
6488
+ pointerWidth: 482
6489
+ }, <?php echo $this->apply_filters( 'optin_pointer_options_json', '{}' ) ?>));
6490
+
6491
+ <?php
6492
+ echo $this->apply_filters( 'optin_pointer_execute', "
6493
+
6494
+ optin.pointer('open');
6495
+
6496
+ // Tag the opt-in pointer with custom class.
6497
+ $('.wp-pointer #fs_connect')
6498
+ .parents('.wp-pointer.wp-pointer-top')
6499
+ .addClass('fs-opt-in-pointer');
6500
+
6501
+ ", 'element', 'optin' ) ?>
6502
+ }
6503
+ }
6504
+ });
6505
+ // ]]></script>
6506
+ <?php
6507
+ }
6508
+
6509
+ /**
6510
+ * Return current page's URL.
6511
+ *
6512
+ * @author Vova Feldman (@svovaf)
6513
+ * @since 1.0.7
6514
+ *
6515
+ * @return string
6516
+ */
6517
+ function current_page_url() {
6518
+ $url = 'http';
6519
+
6520
+ if ( isset( $_SERVER["HTTPS"] ) ) {
6521
+ if ( $_SERVER["HTTPS"] == "on" ) {
6522
+ $url .= "s";
6523
+ }
6524
+ }
6525
+ $url .= "://";
6526
+ if ( $_SERVER["SERVER_PORT"] != "80" ) {
6527
+ $url .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
6528
+ } else {
6529
+ $url .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
6530
+ }
6531
+
6532
+ return esc_url( $url );
6533
+ }
6534
+
6535
+ /**
6536
+ * Check if the current page is the plugin's main admin settings page.
6537
+ *
6538
+ * @author Vova Feldman (@svovaf)
6539
+ * @since 1.0.7
6540
+ *
6541
+ * @return bool
6542
+ */
6543
+ function _is_plugin_page() {
6544
+ return fs_is_plugin_page( $this->_menu->get_raw_slug() ) ||
6545
+ fs_is_plugin_page( $this->_slug );
6546
+ }
6547
+
6548
+ /* Events
6549
+ ------------------------------------------------------------------------------------------------------------------*/
6550
+ /**
6551
+ * Delete site install from Database.
6552
+ *
6553
+ * @author Vova Feldman (@svovaf)
6554
+ * @since 1.0.1
6555
+ *
6556
+ * @param bool $store
6557
+ * @param int|null $blog_id Since 2.0.0
6558
+ *
6559
+ * @return false|int The install ID if deleted. Otherwise, FALSE (when install not exist).
6560
+ */
6561
+ function _delete_site( $store = true, $blog_id = null ) {
6562
+ return self::_delete_site_by_slug( $this->_slug, $this->_module_type, $store, $blog_id );
6563
+ }
6564
+
6565
+ /**
6566
+ * Delete site install from Database.
6567
+ *
6568
+ * @author Vova Feldman (@svovaf)
6569
+ * @since 1.2.2.7
6570
+ *
6571
+ * @param string $slug
6572
+ * @param string $module_type
6573
+ * @param bool $store
6574
+ * @param int|null $blog_id Since 2.0.0
6575
+ *
6576
+ * @return false|int The install ID if deleted. Otherwise, FALSE (when install not exist).
6577
+ */
6578
+ static function _delete_site_by_slug( $slug, $module_type, $store = true, $blog_id = null ) {
6579
+ $sites = self::get_all_sites( $module_type, $blog_id );
6580
+
6581
+ $install_id = false;
6582
+
6583
+ if ( isset( $sites[ $slug ] ) ) {
6584
+ if ( is_object( $sites[ $slug ] ) ) {
6585
+ $install_id = $sites[ $slug ]->id;
6586
+ }
6587
+
6588
+ unset( $sites[ $slug ] );
6589
+
6590
+ self::set_account_option_by_module( $module_type, 'sites', $sites, $store, $blog_id );
6591
+ }
6592
+
6593
+ return $install_id;
6594
+ }
6595
+
6596
+ /**
6597
+ * Delete user.
6598
+ *
6599
+ * @author Vova Feldman (@svovaf)
6600
+ * @since 2.0.0
6601
+ *
6602
+ * @param number $user_id
6603
+ * @param bool $store
6604
+ *
6605
+ * @return false|int The user ID if deleted. Otherwise, FALSE (when install not exist).
6606
+ */
6607
+ private static function delete_user( $user_id, $store = true ) {
6608
+ $users = self::get_all_users();
6609
+
6610
+ if ( ! is_array( $users ) || ! isset( $users[ $user_id ] ) ) {
6611
+ return false;
6612
+ }
6613
+
6614
+ unset( $users[ $user_id ] );
6615
+
6616
+ self::$_accounts->set_option( 'users', $users, $store );
6617
+
6618
+ return $user_id;
6619
+ }
6620
+
6621
+ /**
6622
+ * Delete plugin's plans information.
6623
+ *
6624
+ * @param bool $store Flush to Database if true.
6625
+ * @param bool $keep_associated_plans If set to false, delete all plans, even if a plan is associated with an install.
6626
+ *
6627
+ * @author Vova Feldman (@svovaf)
6628
+ * @since 1.0.9
6629
+ */
6630
+ private function _delete_plans( $store = true, $keep_associated_plans = true ) {
6631
+ $this->_logger->entrance();
6632
+
6633
+ $plans = self::get_all_plans( $this->_module_type );
6634
+
6635
+ $plans_to_keep = array();
6636
+
6637
+ if ( $keep_associated_plans ) {
6638
+ $plans_ids_to_keep = $this->get_plans_ids_associated_with_installs();
6639
+ foreach ( $plans_ids_to_keep as $plan_id ) {
6640
+ $plan = self::_get_plan_by_id( $plan_id );
6641
+ if ( is_object( $plan ) ) {
6642
+ $plans_to_keep[] = $plan;
6643
+ }
6644
+ }
6645
+ }
6646
+
6647
+ if ( ! empty( $plans_to_keep ) ) {
6648
+ $plans[ $this->_slug ] = $plans_to_keep;
6649
+ } else {
6650
+ unset( $plans[ $this->_slug ] );
6651
+ }
6652
+
6653
+ $this->set_account_option( 'plans', $plans, $store );
6654
+ }
6655
+
6656
+ /**
6657
+ * Delete all plugin licenses.
6658
+ *
6659
+ * @author Vova Feldman (@svovaf)
6660
+ * @since 1.0.9
6661
+ *
6662
+ * @param bool $store
6663
+ */
6664
+ private function _delete_licenses( $store = true ) {
6665
+ $this->_logger->entrance();
6666
+
6667
+ $all_licenses = self::get_all_licenses();
6668
+
6669
+ unset( $all_licenses[ $this->_module_id ] );
6670
+
6671
+ self::$_accounts->set_option( 'all_licenses', $all_licenses, $store );
6672
+ }
6673
+
6674
+ /**
6675
+ * Check if Freemius was added on new plugin installation.
6676
+ *
6677
+ * @author Vova Feldman (@svovaf)
6678
+ * @since 1.1.5
6679
+ *
6680
+ * @return bool
6681
+ */
6682
+ function is_plugin_new_install() {
6683
+ return isset( $this->_storage->is_plugin_new_install ) &&
6684
+ $this->_storage->is_plugin_new_install;
6685
+ }
6686
+
6687
+ /**
6688
+ * Check if it's the first plugin release that is running Freemius.
6689
+ *
6690
+ * @author Vova Feldman (@svovaf)
6691
+ * @since 1.2.1.5
6692
+ *
6693
+ * @return bool
6694
+ */
6695
+ function is_first_freemius_powered_version() {
6696
+ return empty( $this->_storage->plugin_last_version );
6697
+ }
6698
+
6699
+ /**
6700
+ * @author Leo Fajardo (@leorw)
6701
+ * @since 1.2.2
6702
+ *
6703
+ * @return bool|string
6704
+ */
6705
+ private function get_previous_theme_slug() {
6706
+ return isset( $this->_storage->previous_theme ) ?
6707
+ $this->_storage->previous_theme :
6708
+ false;
6709
+ }
6710
+
6711
+ /**
6712
+ * @author Leo Fajardo (@leorw)
6713
+ * @since 1.2.2
6714
+ *
6715
+ * @return string
6716
+ */
6717
+ private function can_activate_previous_theme() {
6718
+ $slug = $this->get_previous_theme_slug();
6719
+ if ( false !== $slug && current_user_can( 'switch_themes' ) ) {
6720
+ $theme_instance = wp_get_theme( $slug );
6721
+
6722
+ return $theme_instance->exists();
6723
+ }
6724
+
6725
+ return false;
6726
+ }
6727
+
6728
+ /**
6729
+ * @author Leo Fajardo (@leorw)
6730
+ * @since 1.2.2
6731
+ */
6732
+ private function activate_previous_theme() {
6733
+ switch_theme( $this->get_previous_theme_slug() );
6734
+ unset( $this->_storage->previous_theme );
6735
+
6736
+ global $pagenow;
6737
+ if ( 'themes.php' === $pagenow ) {
6738
+ /**
6739
+ * Refresh the active theme information.
6740
+ *
6741
+ * @author Leo Fajardo (@leorw)
6742
+ * @since 1.2.2
6743
+ */
6744
+ fs_redirect( $this->admin_url( $pagenow ) );
6745
+ }
6746
+ }
6747
+
6748
+ /**
6749
+ * @author Leo Fajardo (@leorw)
6750
+ * @since 1.2.2
6751
+ *
6752
+ * @return string
6753
+ */
6754
+ function get_previous_theme_activation_url() {
6755
+ if ( ! $this->can_activate_previous_theme() ) {
6756
+ return '';
6757
+ }
6758
+
6759
+ /**
6760
+ * Activation URL
6761
+ *
6762
+ * @author Leo Fajardo (@leorw)
6763
+ * @since 1.2.2
6764
+ */
6765
+ return wp_nonce_url(
6766
+ $this->admin_url( 'themes.php?action=activate&stylesheet=' . urlencode( $this->get_previous_theme_slug() ) ),
6767
+ 'switch-theme_' . $this->get_previous_theme_slug()
6768
+ );
6769
+ }
6770
+
6771
+ /**
6772
+ * Saves the slug of the previous theme if it still exists so that it can be used by the logic in the opt-in
6773
+ * form that decides whether to add a close button to the opt-in dialog or not. So after a premium-only theme is
6774
+ * activated, the close button will appear and will reactivate the previous theme if clicked. If the previous
6775
+ * theme doesn't exist, then there will be no close button.
6776
+ *
6777
+ * @author Leo Fajardo (@leorw)
6778
+ * @since 1.2.2
6779
+ *
6780
+ * @param string $slug_or_name Old theme's slug or name.
6781
+ * @param bool|WP_Theme $old_theme WP_Theme instance of the old theme if it still exists.
6782
+ */
6783
+ function _activate_theme_event_hook( $slug_or_name, $old_theme = false ) {
6784
+ $this->_storage->previous_theme = ( false !== $old_theme ) ?
6785
+ $old_theme->get_stylesheet() :
6786
+ $slug_or_name;
6787
+
6788
+ $this->_activate_plugin_event_hook();
6789
+ }
6790
+
6791
+ /**
6792
+ * Plugin activated hook.
6793
+ *
6794
+ * @author Vova Feldman (@svovaf)
6795
+ * @since 1.0.1
6796
+ *
6797
+ * @uses FS_Api
6798
+ */
6799
+ function _activate_plugin_event_hook() {
6800
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
6801
+
6802
+ if ( ! $this->is_user_admin() ) {
6803
+ return;
6804
+ }
6805
+
6806
+ $this->unregister_uninstall_hook();
6807
+
6808
+ // Clear API cache on activation.
6809
+ FS_Api::clear_cache();
6810
+
6811
+ $is_premium_version_activation = ( current_filter() !== ( 'activate_' . $this->_free_plugin_basename ) );
6812
+
6813
+ $this->_logger->info( 'Activating ' . ( $is_premium_version_activation ? 'premium' : 'free' ) . ' plugin version.' );
6814
+
6815
+ // 1. If running in the activation of the FREE module, get the basename of the PREMIUM.
6816
+ // 2. If running in the activation of the PREMIUM module, get the basename of the FREE.
6817
+ $other_version_basename = $is_premium_version_activation ?
6818
+ $this->_free_plugin_basename :
6819
+ $this->premium_plugin_basename();
6820
+
6821
+ if ( ! $this->_is_network_active ) {
6822
+ /**
6823
+ * During the activation, the plugin isn't yet active, therefore,
6824
+ * _is_network_active will be set to false even if it's a network level
6825
+ * activation. So we need to fix that by looking at the is_network_admin() value.
6826
+ *
6827
+ * @author Vova Feldman
6828
+ */
6829
+ $this->_is_network_active = (
6830
+ $this->_is_multisite_integrated &&
6831
+ // Themes are always network activated, but the ACTUAL activation is per site.
6832
+ $this->is_plugin() &&
6833
+ fs_is_network_admin()
6834
+ );
6835
+ }
6836
+
6837
+ /**
6838
+ * If the other module version is activate, deactivate it.
6839
+ *
6840
+ * is_plugin_active() checks if the plugin active on the site or the network level
6841
+ * and deactivate_plugins() deactivates the plugin whether its activated on the site
6842
+ * or network level.
6843
+ *
6844
+ * @author Leo Fajardo (@leorw)
6845
+ * @since 1.2.2
6846
+ */
6847
+ if ( is_plugin_active( $other_version_basename ) ) {
6848
+ deactivate_plugins( $other_version_basename );
6849
+ }
6850
+
6851
+ if ( $this->is_registered() ) {
6852
+ if ( $is_premium_version_activation ) {
6853
+ $this->reconnect_locally();
6854
+ }
6855
+
6856
+
6857
+ // Schedule re-activation event and sync.
6858
+ // $this->sync_install( array(), true );
6859
+ $this->schedule_install_sync();
6860
+
6861
+ // If activating the premium module version, add an admin notice to congratulate for an upgrade completion.
6862
+ if ( $is_premium_version_activation ) {
6863
+ $this->_admin_notices->add(
6864
+ sprintf( $this->get_text_inline( 'The upgrade of %s was successfully completed.', 'successful-version-upgrade-message' ), sprintf( '<b>%s</b>', $this->_plugin->title ) ),
6865
+ $this->get_text_x_inline( 'W00t',
6866
+ 'Used to express elation, enthusiasm, or triumph (especially in electronic communication).', 'woot' ) . '!'
6867
+ );
6868
+ }
6869
+ } else if ( $this->is_anonymous() ) {
6870
+ if ( isset( $this->_storage->is_anonymous_ms ) && $this->_storage->is_anonymous_ms['is'] ) {
6871
+ $plugin_version = $this->_storage->is_anonymous_ms['version'];
6872
+ $network = true;
6873
+ } else {
6874
+ $plugin_version = $this->_storage->is_anonymous['version'];
6875
+ $network = false;
6876
+ }
6877
+
6878
+ /**
6879
+ * Reset "skipped" click cache on the following:
6880
+ * 1. Freemius DEV mode.
6881
+ * 2. WordPress DEBUG mode.
6882
+ * 3. If a plugin and the user skipped the exact same version before.
6883
+ *
6884
+ * @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).
6885
+ *
6886
+ * @todo 4. If explicitly asked to retry after every activation.
6887
+ */
6888
+ if ( WP_FS__DEV_MODE ||
6889
+ (
6890
+ ( $this->is_plugin() || ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ) &&
6891
+ $this->get_plugin_version() == $plugin_version
6892
+ )
6893
+ ) {
6894
+ $this->reset_anonymous_mode( $network );
6895
+ }
6896
+ }
6897
+
6898
+ if (
6899
+ $is_premium_version_activation &&
6900
+ (
6901
+ $this->is_anonymous() ||
6902
+ (
6903
+ $this->is_registered() &&
6904
+ ! $this->is_trial() &&
6905
+ ! $this->has_features_enabled_license()
6906
+ )
6907
+ )
6908
+ ) {
6909
+ $this->_storage->require_license_activation = true;
6910
+ }
6911
+
6912
+ if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
6913
+ /**
6914
+ * If no previous version of plugin's version exist, it means that it's either
6915
+ * the first time that the plugin installed on the site, or the plugin was installed
6916
+ * before but didn't have Freemius integrated.
6917
+ *
6918
+ * Since register_activation_hook() do NOT fires on updates since 3.1, and only fires
6919
+ * on manual activation via the dashboard, is_plugin_activation() is TRUE
6920
+ * only after immediate activation.
6921
+ *
6922
+ * @since 1.1.4
6923
+ * @link https://make.wordpress.org/core/2010/10/27/plugin-activation-hooks-no-longer-fire-for-updates/
6924
+ */
6925
+ $this->_storage->is_plugin_new_install = empty( $this->_storage->plugin_last_version );
6926
+ }
6927
+
6928
+ if ( ! $this->_anonymous_mode &&
6929
+ $this->has_api_connectivity( WP_FS__DEV_MODE ) &&
6930
+ ! $this->_isAutoInstall
6931
+ ) {
6932
+ // Store hint that the plugin was just activated to enable auto-redirection to settings.
6933
+ set_transient( "fs_{$this->_module_type}_{$this->_slug}_activated", true, 60 );
6934
+ }
6935
+
6936
+ /**
6937
+ * Activation hook is executed after the plugin's main file is loaded, therefore,
6938
+ * after the plugin was loaded. The logic is located at activate_plugin()
6939
+ * ./wp-admin/includes/plugin.php.
6940
+ *
6941
+ * @author Vova Feldman (@svovaf)
6942
+ * @since 1.1.9
6943
+ */
6944
+ $this->_storage->was_plugin_loaded = true;
6945
+ }
6946
+
6947
+ /**
6948
+ * Delete account.
6949
+ *
6950
+ * @author Vova Feldman (@svovaf)
6951
+ * @since 1.0.3
6952
+ *
6953
+ * @param bool $check_user Enforce checking if user have plugins activation privileges.
6954
+ */
6955
+ function delete_account_event( $check_user = true ) {
6956
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
6957
+
6958
+ if ( $check_user && ! $this->is_user_admin() ) {
6959
+ return;
6960
+ }
6961
+
6962
+ $this->do_action( 'before_account_delete' );
6963
+
6964
+ // Clear all admin notices.
6965
+ $this->_admin_notices->clear_all_sticky( false );
6966
+
6967
+ $this->_delete_site( false );
6968
+
6969
+ $delete_network_common_data = true;
6970
+
6971
+ if ( $this->_is_network_active ) {
6972
+ $installs = $this->get_blog_install_map();
6973
+
6974
+ // Don't delete common network data unless no other installs left.
6975
+ $delete_network_common_data = empty( $installs );
6976
+ }
6977
+
6978
+ if ( $delete_network_common_data ) {
6979
+ $this->_delete_plans( false );
6980
+
6981
+ $this->_delete_licenses( false );
6982
+
6983
+ // Delete add-ons related to plugin's account.
6984
+ $this->_delete_account_addons( false );
6985
+ }
6986
+
6987
+ // @todo Delete plans and licenses of add-ons.
6988
+
6989
+ self::$_accounts->store();
6990
+
6991
+ /**
6992
+ * IMPORTANT:
6993
+ * Clear crons must be executed before clearing all storage.
6994
+ * Otherwise, the cron will not be cleared.
6995
+ */
6996
+ if ( $delete_network_common_data ) {
6997
+ $this->clear_sync_cron();
6998
+ }
6999
+
7000
+ $this->clear_install_sync_cron();
7001
+
7002
+ // Clear all storage data.
7003
+ $this->_storage->clear_all( true, array(
7004
+ 'connectivity_test',
7005
+ 'is_on',
7006
+ ), false );
7007
+
7008
+ // Send delete event.
7009
+ $this->get_api_site_scope()->call( '/', 'delete' );
7010
+
7011
+ $this->do_action( 'after_account_delete' );
7012
+ }
7013
+
7014
+ /**
7015
+ * Delete network level account.
7016
+ *
7017
+ * @author Vova Feldman (@svovaf)
7018
+ * @since 2.0.0
7019
+ *
7020
+ * @param bool $check_user Enforce checking if user have plugins activation privileges.
7021
+ */
7022
+ function delete_network_account_event( $check_user = true ) {
7023
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
7024
+
7025
+ if ( $check_user && ! $this->is_user_admin() ) {
7026
+ return;
7027
+ }
7028
+
7029
+ $this->do_action( 'before_network_account_delete' );
7030
+
7031
+ // Clear all admin notices.
7032
+ $this->_admin_notices->clear_all_sticky();
7033
+
7034
+ $this->_delete_plans( false, false );
7035
+
7036
+ $this->_delete_licenses( false );
7037
+
7038
+ // Delete add-ons related to plugin's account.
7039
+ $this->_delete_account_addons( false );
7040
+
7041
+ // @todo Delete plans and licenses of add-ons.
7042
+
7043
+ self::$_accounts->store( true );
7044
+
7045
+ /**
7046
+ * IMPORTANT:
7047
+ * Clear crons must be executed before clearing all storage.
7048
+ * Otherwise, the cron will not be cleared.
7049
+ */
7050
+ $this->clear_sync_cron( true );
7051
+ $this->clear_install_sync_cron( true );
7052
+
7053
+ $sites = self::get_sites();
7054
+
7055
+ $install_ids = array();
7056
+ foreach ( $sites as $site ) {
7057
+ $blog_id = self::get_site_blog_id( $site );
7058
+
7059
+ $install_id = $this->_delete_site( true, $blog_id );
7060
+
7061
+ // Clear all storage data.
7062
+ $this->_storage->clear_all( true, array( 'connectivity_test' ), $blog_id );
7063
+
7064
+ if ( FS_Site::is_valid_id( $install_id ) ) {
7065
+ $install_ids[] = $install_id;
7066
+ }
7067
+
7068
+ switch_to_blog( $blog_id );
7069
+
7070
+ $this->do_action( 'after_account_delete' );
7071
+
7072
+ restore_current_blog();
7073
+ }
7074
+
7075
+ $this->_storage->clear_all( true, array(
7076
+ 'connectivity_test',
7077
+ 'is_on',
7078
+ ), true );
7079
+
7080
+ // Send delete event.
7081
+ if ( ! empty( $install_ids ) ) {
7082
+ $result = $this->get_current_or_network_user_api_scope()->call( "/plugins/{$this->_module_id}/installs.json?ids=" . implode( ',', $install_ids ), 'delete' );
7083
+ }
7084
+
7085
+ $this->do_action( 'after_network_account_delete' );
7086
+ }
7087
+
7088
+ /**
7089
+ * Plugin deactivation hook.
7090
+ *
7091
+ * @author Vova Feldman (@svovaf)
7092
+ * @since 1.0.1
7093
+ */
7094
+ function _deactivate_plugin_hook() {
7095
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
7096
+
7097
+ if ( ! $this->is_user_admin() ) {
7098
+ return;
7099
+ }
7100
+
7101
+ $is_network_deactivation = fs_is_network_admin();
7102
+ $storage_keys_for_removal = array();
7103
+
7104
+ $this->_admin_notices->clear_all_sticky();
7105
+
7106
+ $storage_keys_for_removal[] = 'sticky_optin_added';
7107
+ if ( isset( $this->_storage->sticky_optin_added ) ) {
7108
+ unset( $this->_storage->sticky_optin_added );
7109
+ }
7110
+
7111
+ if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
7112
+ // Remember that plugin was already installed.
7113
+ $this->_storage->is_plugin_new_install = false;
7114
+ }
7115
+
7116
+ // Hook to plugin uninstall.
7117
+ register_uninstall_hook( $this->_plugin_main_file_path, array( 'Freemius', '_uninstall_plugin_hook' ) );
7118
+
7119
+ $this->clear_module_main_file_cache();
7120
+ $this->clear_sync_cron( $this->_is_network_active );
7121
+ $this->clear_install_sync_cron();
7122
+
7123
+ if ( $this->is_registered() ) {
7124
+ if ( $this->is_premium() && ! $this->has_active_valid_license() ) {
7125
+ FS_Plugin_Updater::instance( $this )->delete_update_data();
7126
+ }
7127
+
7128
+ if ( $is_network_deactivation ) {
7129
+ // Send deactivation event.
7130
+ $this->sync_installs( array(
7131
+ 'is_active' => false,
7132
+ ) );
7133
+ } else {
7134
+ // Send deactivation event.
7135
+ $this->sync_install( array(
7136
+ 'is_active' => false,
7137
+ ) );
7138
+ }
7139
+ } else {
7140
+ if ( ! $this->has_api_connectivity() ) {
7141
+ // Reset connectivity test cache.
7142
+ unset( $this->_storage->connectivity_test );
7143
+
7144
+ $storage_keys_for_removal[] = 'connectivity_test';
7145
+ }
7146
+ }
7147
+
7148
+ if ( $is_network_deactivation ) {
7149
+ if ( isset( $this->_storage->sticky_optin_added_ms ) ) {
7150
+ unset( $this->_storage->sticky_optin_added_ms );
7151
+ }
7152
+
7153
+ if ( ! empty( $storage_keys_for_removal ) ) {
7154
+ $sites = self::get_sites();
7155
+
7156
+ foreach ( $sites as $site ) {
7157
+ $blog_id = self::get_site_blog_id( $site );
7158
+
7159
+ foreach ( $storage_keys_for_removal as $key ) {
7160
+ $this->_storage->remove( $key, false, $blog_id );
7161
+ }
7162
+
7163
+ $this->_storage->save( $blog_id );
7164
+ }
7165
+ }
7166
+ }
7167
+
7168
+ // Clear API cache on deactivation.
7169
+ FS_Api::clear_cache();
7170
+
7171
+ $this->remove_sdk_reference();
7172
+ }
7173
+
7174
+ /**
7175
+ * @author Vova Feldman (@svovaf)
7176
+ * @since 1.1.6
7177
+ */
7178
+ private function remove_sdk_reference() {
7179
+ global $fs_active_plugins;
7180
+
7181
+ foreach ( $fs_active_plugins->plugins as $sdk_path => $data ) {
7182
+ if ( $this->_plugin_basename == $data->plugin_path ) {
7183
+ unset( $fs_active_plugins->plugins[ $sdk_path ] );
7184
+ break;
7185
+ }
7186
+ }
7187
+
7188
+ fs_fallback_to_newest_active_sdk();
7189
+ }
7190
+
7191
+ /**
7192
+ * @author Vova Feldman (@svovaf)
7193
+ * @since 1.1.3
7194
+ *
7195
+ * @param bool $is_anonymous
7196
+ * @param bool|int $network_or_blog_id Since 2.0.0
7197
+ */
7198
+ private function set_anonymous_mode( $is_anonymous = true, $network_or_blog_id = 0 ) {
7199
+ // Store information regarding skip to try and opt-in the user
7200
+ // again in the future.
7201
+ $skip_info = array(
7202
+ 'is' => $is_anonymous,
7203
+ 'timestamp' => WP_FS__SCRIPT_START_TIME,
7204
+ 'version' => $this->get_plugin_version(),
7205
+ );
7206
+
7207
+ if ( true === $network_or_blog_id ) {
7208
+ $this->_storage->is_anonymous_ms = $skip_info;
7209
+ } else {
7210
+ $this->_storage->store( 'is_anonymous', $skip_info, $network_or_blog_id );
7211
+ }
7212
+
7213
+ $this->network_upgrade_mode_completed();
7214
+
7215
+ // Update anonymous mode cache.
7216
+ $this->_is_anonymous = $is_anonymous;
7217
+ }
7218
+
7219
+ /**
7220
+ * @author Vova Feldman (@svovaf)
7221
+ * @since 2.0.0
7222
+ *
7223
+ * @param int $blog_id Site ID.
7224
+ * @param int $user_id User ID.
7225
+ * @param string $domain Site domain.
7226
+ * @param string $path Site path.
7227
+ * @param int $network_id Network ID. Only relevant on multi-network installations.
7228
+ * @param array $meta Metadata. Used to set initial site options.
7229
+ *
7230
+ * @uses Freemius::is_license_network_active() to check if the context license was network activated by the super-admin.
7231
+ * @uses Freemius::is_network_connected() to check if the super-admin network opted-in.
7232
+ * @uses Freemius::is_network_anonymous() to check if the super-admin network skipped.
7233
+ * @uses Freemius::is_network_delegated_connection() to check if the super-admin network delegated the connection to the site admins.
7234
+ */
7235
+ function _after_new_blog_callback( $blog_id, $user_id, $domain, $path, $network_id, $meta ) {
7236
+ $this->_logger->entrance();
7237
+
7238
+ if ( $this->is_premium() &&
7239
+ $this->is_network_connected() &&
7240
+ is_object( $this->_license ) &&
7241
+ $this->_license->can_activate( FS_Site::is_localhost_by_address( $domain ) ) &&
7242
+ $this->is_license_network_active( $blog_id )
7243
+ ) {
7244
+ /**
7245
+ * 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.
7246
+ */
7247
+ $current_blog_id = get_current_blog_id();
7248
+ $license = clone $this->_license;
7249
+
7250
+ $this->switch_to_blog( $blog_id );
7251
+
7252
+ // Opt-in with network user.
7253
+ $this->install_with_user(
7254
+ $this->get_network_user(),
7255
+ $license->secret_key,
7256
+ false,
7257
+ false,
7258
+ false
7259
+ );
7260
+
7261
+ if ( is_object( $this->_site ) ) {
7262
+ if ( $this->_site->license_id == $license->id ) {
7263
+ /**
7264
+ * If the license was activated successfully, sync the license data from the remote server.
7265
+ */
7266
+ $this->_license = $license;
7267
+ $this->sync_site_license();
7268
+ }
7269
+ }
7270
+
7271
+ $this->switch_to_blog( $current_blog_id );
7272
+
7273
+ if ( is_object( $this->_site ) ) {
7274
+ // Already connected (with or without a license), so no need to continue.
7275
+ return;
7276
+ }
7277
+ }
7278
+
7279
+ if ( $this->is_network_anonymous() ) {
7280
+ /**
7281
+ * Opt-in was network skipped so automatically skip the opt-in for the new site.
7282
+ */
7283
+ $this->skip_site_connection( $blog_id );
7284
+ } else if ( $this->is_network_delegated_connection() ) {
7285
+ /**
7286
+ * Opt-in was network delegated so automatically delegate the opt-in for the new site's admin.
7287
+ */
7288
+ $this->delegate_site_connection( $blog_id );
7289
+ } else if ( $this->is_network_connected() ) {
7290
+ /**
7291
+ * Opt-in was network activated so automatically opt-in with the network user and new site admin.
7292
+ */
7293
+ $current_blog_id = get_current_blog_id();
7294
+
7295
+ $this->switch_to_blog( $blog_id );
7296
+
7297
+ // Opt-in with network user.
7298
+ $this->install_with_user(
7299
+ $this->get_network_user(),
7300
+ false,
7301
+ false,
7302
+ false,
7303
+ false
7304
+ );
7305
+
7306
+ $this->switch_to_blog( $current_blog_id );
7307
+ } else {
7308
+ /**
7309
+ * If the super-admin mixed different options (connect, skip, delegated):
7310
+ * a) If at least one site connection was delegated, then automatically delegate connection.
7311
+ * 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.
7312
+ */
7313
+ $has_delegated_site = false;
7314
+
7315
+ $sites = self::get_sites();
7316
+ foreach ( $sites as $site ) {
7317
+ $blog_id = self::get_site_blog_id( $site );
7318
+
7319
+ if ( $this->is_site_delegated_connection( $blog_id ) ) {
7320
+ $has_delegated_site = true;
7321
+ break;
7322
+ }
7323
+ }
7324
+
7325
+ if ( $has_delegated_site ) {
7326
+ $this->delegate_site_connection( $blog_id );
7327
+ } else {
7328
+ $this->skip_site_connection( $blog_id );
7329
+ }
7330
+ }
7331
+ }
7332
+
7333
+ /**
7334
+ * @author Vova Feldman (@svovaf)
7335
+ * @since 1.1.3
7336
+ *
7337
+ * @param bool|int $network_or_blog_id Since 2.0.0.
7338
+ */
7339
+ private function reset_anonymous_mode( $network_or_blog_id = 0 ) {
7340
+ if ( true === $network_or_blog_id ) {
7341
+ unset( $this->_storage->is_anonymous_ms );
7342
+ } else {
7343
+ $this->_storage->remove( 'is_anonymous', true, $network_or_blog_id );
7344
+ }
7345
+
7346
+ /**
7347
+ * Ensure that this field is also "false", otherwise, if the current module's type is "theme" and the module
7348
+ * has no menus, the opt-in popup will not be shown immediately (in this case, the user will have to click
7349
+ * on the admin notice that contains the opt-in link in order to trigger the opt-in popup).
7350
+ *
7351
+ * @author Leo Fajardo (@leorw)
7352
+ * @since 1.2.2
7353
+ */
7354
+ if ( ! $this->_is_network_active ||
7355
+ 0 === $network_or_blog_id ||
7356
+ get_current_blog_id() == $network_or_blog_id ||
7357
+ ( true === $network_or_blog_id && fs_is_network_admin() )
7358
+ ) {
7359
+ unset( $this->_is_anonymous );
7360
+ }
7361
+ }
7362
+
7363
+ /**
7364
+ * This is used to ensure that before redirecting to the opt-in page after resetting the anonymous mode or
7365
+ * deleting the account in the network level, the URL of the page to redirect to is correct.
7366
+ *
7367
+ * @author Leo Fajardo (@leorw)
7368
+ *
7369
+ * @since 2.1.3
7370
+ */
7371
+ private function maybe_set_slug_and_network_menu_exists_flag() {
7372
+ if ( ! empty( $this->_dynamically_added_top_level_page_hook_name ) ) {
7373
+ $this->_menu->set_slug_and_network_menu_exists_flag( $this->_menu->has_menu() ?
7374
+ $this->_menu->get_slug() :
7375
+ $this->_slug
7376
+ );
7377
+ }
7378
+ }
7379
+
7380
+ /**
7381
+ * Clears the anonymous mode and redirects to the opt-in screen.
7382
+ *
7383
+ * @author Vova Feldman (@svovaf)
7384
+ * @since 1.1.7
7385
+ */
7386
+ function connect_again() {
7387
+ if ( ! $this->is_anonymous() ) {
7388
+ return;
7389
+ }
7390
+
7391
+ $this->reset_anonymous_mode( fs_is_network_admin() );
7392
+
7393
+ $this->maybe_set_slug_and_network_menu_exists_flag();
7394
+
7395
+ fs_redirect( $this->get_activation_url() );
7396
+ }
7397
+
7398
+ /**
7399
+ * Skip account connect, and set anonymous mode.
7400
+ *
7401
+ * @author Vova Feldman (@svovaf)
7402
+ * @since 1.1.1
7403
+ *
7404
+ * @param array|null $sites Since 2.0.0. Specific sites.
7405
+ * @param bool $skip_all_network Since 2.0.0. If true, skip connection for all sites.
7406
+ */
7407
+ function skip_connection( $sites = null, $skip_all_network = false ) {
7408
+ $this->_logger->entrance();
7409
+
7410
+ $this->_admin_notices->remove_sticky( 'connect_account' );
7411
+
7412
+ if ( $skip_all_network ) {
7413
+ $this->set_anonymous_mode( true, true );
7414
+ }
7415
+
7416
+ if ( ! $skip_all_network && empty( $sites ) ) {
7417
+ $this->skip_site_connection();
7418
+ } else {
7419
+ $uids = array();
7420
+
7421
+ if ( $skip_all_network ) {
7422
+ $this->set_anonymous_mode( true, true );
7423
+
7424
+ $sites = self::get_sites();
7425
+ foreach ( $sites as $site ) {
7426
+ $blog_id = self::get_site_blog_id( $site );
7427
+ $this->skip_site_connection( $blog_id, false );
7428
+ $uids[] = $this->get_anonymous_id( $blog_id );
7429
+ }
7430
+ } else if ( ! empty( $sites ) ) {
7431
+ foreach ( $sites as $site ) {
7432
+ $uids[] = $site['uid'];
7433
+ $this->skip_site_connection( $site['blog_id'], false );
7434
+ }
7435
+ }
7436
+
7437
+ // Send anonymous skip event.
7438
+ // No user identified info nor any tracking will be sent after the user skips the opt-in.
7439
+ $this->get_api_plugin_scope()->call( 'skip.json', 'put', array(
7440
+ 'uids' => $uids,
7441
+ ) );
7442
+ }
7443
+
7444
+ $this->network_upgrade_mode_completed();
7445
+ }
7446
+
7447
+ /**
7448
+ * Skip connection for specific site in the network.
7449
+ *
7450
+ * @author Vova Feldman (@svovaf)
7451
+ * @since 2.0.0
7452
+ *
7453
+ * @param int|null $blog_id
7454
+ * @param bool $send_skip
7455
+ */
7456
+ private function skip_site_connection( $blog_id = null, $send_skip = true ) {
7457
+ $this->_logger->entrance();
7458
+
7459
+ $this->_admin_notices->remove_sticky( 'connect_account', $blog_id );
7460
+
7461
+ $this->set_anonymous_mode( true, $blog_id );
7462
+
7463
+ if ( $send_skip ) {
7464
+ $this->get_api_plugin_scope()->call( 'skip.json', 'put', array(
7465
+ 'uids' => array( $this->get_anonymous_id( $blog_id ) ),
7466
+ ) );
7467
+ }
7468
+ }
7469
+
7470
+ /**
7471
+ * Plugin version update hook.
7472
+ *
7473
+ * @author Vova Feldman (@svovaf)
7474
+ * @since 1.0.4
7475
+ */
7476
+ private function update_plugin_version_event() {
7477
+ $this->_logger->entrance();
7478
+
7479
+ if ( ! $this->is_registered() ) {
7480
+ return;
7481
+ }
7482
+
7483
+ $this->schedule_install_sync();
7484
+ // $this->sync_install( array(), true );
7485
+ }
7486
+
7487
+ /**
7488
+ * Generate an MD5 signature of a plugins collection.
7489
+ * This helper methods used to identify changes in a plugins collection.
7490
+ *
7491
+ * @author Vova Feldman (@svovaf)
7492
+ * @since 2.0.0
7493
+ *
7494
+ * @param array [string]array $plugins
7495
+ *
7496
+ * @return string
7497
+ */
7498
+ private function get_plugins_thumbprint( $plugins ) {
7499
+ ksort( $plugins );
7500
+
7501
+ $thumbprint = '';
7502
+ foreach ( $plugins as $basename => $data ) {
7503
+ $thumbprint .= $data['slug'] . ',' .
7504
+ $data['Version'] . ',' .
7505
+ ( $data['is_active'] ? '1' : '0' ) . ';';
7506
+ }
7507
+
7508
+ return md5( $thumbprint );
7509
+ }
7510
+
7511
+ /**
7512
+ * Return a list of modified plugins since the last sync.
7513
+ *
7514
+ * Note:
7515
+ * There's no point to store a plugins counter since even if the number of
7516
+ * plugins didn't change, we still need to check if the versions are all the
7517
+ * same and the activity state is similar.
7518
+ *
7519
+ * @author Vova Feldman (@svovaf)
7520
+ * @since 1.1.8
7521
+ *
7522
+ * @return array|false
7523
+ */
7524
+ private function get_plugins_data_for_api() {
7525
+ // Alias.
7526
+ $site_active_plugins_option_name = 'active_plugins';
7527
+ $network_plugins_option_name = 'all_plugins';
7528
+
7529
+ /**
7530
+ * Collection of all site level active plugins.
7531
+ */
7532
+ $site_active_plugins_cache = self::$_accounts->get_option( $site_active_plugins_option_name );
7533
+
7534
+ if ( ! is_object( $site_active_plugins_cache ) ) {
7535
+ $site_active_plugins_cache = (object) array(
7536
+ 'timestamp' => '',
7537
+ 'md5' => '',
7538
+ 'plugins' => array(),
7539
+ );
7540
+ }
7541
+
7542
+ $time = time();
7543
+
7544
+ if ( ! empty( $site_active_plugins_cache->timestamp ) &&
7545
+ ( $time - $site_active_plugins_cache->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC
7546
+ ) {
7547
+ // Don't send plugin updates if last update was in the past 5 min.
7548
+ return false;
7549
+ }
7550
+
7551
+ // Write timestamp to lock the logic.
7552
+ $site_active_plugins_cache->timestamp = $time;
7553
+ self::$_accounts->set_option( $site_active_plugins_option_name, $site_active_plugins_cache, true );
7554
+
7555
+ // Reload options from DB.
7556
+ self::$_accounts->load( true );
7557
+ $site_active_plugins_cache = self::$_accounts->get_option( $site_active_plugins_option_name );
7558
+
7559
+ if ( $time != $site_active_plugins_cache->timestamp ) {
7560
+ // If timestamp is different, then another thread captured the lock.
7561
+ return false;
7562
+ }
7563
+
7564
+ /**
7565
+ * Collection of all plugins (network level).
7566
+ */
7567
+ $network_plugins_cache = self::$_accounts->get_option( $network_plugins_option_name );
7568
+
7569
+ if ( ! is_object( $network_plugins_cache ) ) {
7570
+ $network_plugins_cache = (object) array(
7571
+ 'timestamp' => '',
7572
+ 'md5' => '',
7573
+ 'plugins' => array(),
7574
+ );
7575
+ }
7576
+
7577
+ // Check if there's a change in plugins.
7578
+ $network_plugins = self::get_network_plugins();
7579
+ $site_active_plugins = self::get_site_active_plugins();
7580
+
7581
+ $network_plugins_thumbprint = $this->get_plugins_thumbprint( $network_plugins );
7582
+ $site_active_plugins_thumbprint = $this->get_plugins_thumbprint( $site_active_plugins );
7583
+
7584
+ // Check if plugins status changed (version or active/inactive).
7585
+ $network_plugins_changed = ( $network_plugins_cache->md5 !== $network_plugins_thumbprint );
7586
+ $site_active_plugins_changed = ( $site_active_plugins_cache->md5 !== $site_active_plugins_thumbprint );
7587
+
7588
+ if ( ! $network_plugins_changed &&
7589
+ ! $site_active_plugins_changed
7590
+ ) {
7591
+ // No changes.
7592
+ return array();
7593
+ }
7594
+
7595
+ $plugins_update_data = array();
7596
+
7597
+ foreach ( $network_plugins_cache->plugins as $basename => $data ) {
7598
+ if ( ! isset( $network_plugins[ $basename ] ) ) {
7599
+ // Plugin uninstalled.
7600
+ $uninstalled_plugin_data = $data;
7601
+ $uninstalled_plugin_data['is_active'] = false;
7602
+ $uninstalled_plugin_data['is_uninstalled'] = true;
7603
+ $plugins_update_data[] = $uninstalled_plugin_data;
7604
+
7605
+ unset( $network_plugins[ $basename ] );
7606
+
7607
+ unset( $network_plugins_cache->plugins[ $basename ] );
7608
+ unset( $site_active_plugins_cache->plugins[ $basename ] );
7609
+
7610
+ continue;
7611
+ }
7612
+
7613
+ $was_active = $data['is_active'] ||
7614
+ ( isset( $site_active_plugins_cache->plugins[ $basename ] ) &&
7615
+ true === $site_active_plugins_cache->plugins[ $basename ]['is_active'] );
7616
+ $is_active = $network_plugins[ $basename ]['is_active'] ||
7617
+ ( isset( $site_active_plugins[ $basename ] ) &&
7618
+ $site_active_plugins[ $basename ]['is_active'] );
7619
+
7620
+ if ( ! isset( $site_active_plugins_cache->plugins[ $basename ] ) &&
7621
+ isset( $site_active_plugins[ $basename ] )
7622
+ ) {
7623
+ // Plugin was site level activated.
7624
+ $site_active_plugins_cache->plugins[ $basename ] = $network_plugins[ $basename ];
7625
+ $site_active_plugins_cache->plugins[ $basename ]['is_active'] = true;
7626
+ } else if ( isset( $site_active_plugins_cache->plugins[ $basename ] ) &&
7627
+ ! isset( $site_active_plugins[ $basename ] )
7628
+ ) {
7629
+ // Plugin was site level deactivated.
7630
+ unset( $site_active_plugins_cache->plugins[ $basename ] );
7631
+ }
7632
+
7633
+ $prev_version = $data['version'];
7634
+ $current_version = $network_plugins[ $basename ]['Version'];
7635
+
7636
+ if ( $was_active !== $is_active || $prev_version !== $current_version ) {
7637
+ // Plugin activated or deactivated, or version changed.
7638
+
7639
+ if ( $was_active !== $is_active ) {
7640
+ if ( $data['is_active'] != $network_plugins[ $basename ]['is_active'] ) {
7641
+ $network_plugins_cache->plugins[ $basename ]['is_active'] = $data['is_active'];
7642
+ }
7643
+ }
7644
+
7645
+ if ( $prev_version !== $current_version ) {
7646
+ $network_plugins_cache->plugins[ $basename ]['Version'] = $current_version;
7647
+ }
7648
+
7649
+ $updated_plugin_data = $data;
7650
+ $updated_plugin_data['is_active'] = $is_active;
7651
+ $updated_plugin_data['version'] = $current_version;
7652
+ $updated_plugin_data['title'] = $network_plugins[ $basename ]['Name'];
7653
+ $plugins_update_data[] = $updated_plugin_data;
7654
+ }
7655
+ }
7656
+
7657
+ // Find new plugins that weren't yet seen before.
7658
+ foreach ( $network_plugins as $basename => $data ) {
7659
+ if ( ! isset( $network_plugins_cache->plugins[ $basename ] ) ) {
7660
+ // New plugin.
7661
+ $new_plugin = array(
7662
+ 'slug' => $data['slug'],
7663
+ 'version' => $data['Version'],
7664
+ 'title' => $data['Name'],
7665
+ 'is_active' => $data['is_active'],
7666
+ 'is_uninstalled' => false,
7667
+ );
7668
+
7669
+ $plugins_update_data[] = $new_plugin;
7670
+ $network_plugins_cache->plugins[ $basename ] = $new_plugin;
7671
+
7672
+ if ( isset( $site_active_plugins[ $basename ] ) ) {
7673
+ $site_active_plugins_cache->plugins[ $basename ] = $new_plugin;
7674
+ $site_active_plugins_cache->plugins[ $basename ]['is_active'] = true;
7675
+ }
7676
+ }
7677
+ }
7678
+
7679
+ $site_active_plugins_cache->md5 = $site_active_plugins_thumbprint;
7680
+ $site_active_plugins_cache->timestamp = $time;
7681
+ self::$_accounts->set_option( $site_active_plugins_option_name, $site_active_plugins_cache, true );
7682
+
7683
+ $network_plugins_cache->md5 = $network_plugins_thumbprint;
7684
+ $network_plugins_cache->timestamp = $time;
7685
+ self::$_accounts->set_option( $network_plugins_option_name, $network_plugins_cache, true );
7686
+
7687
+ return $plugins_update_data;
7688
+ }
7689
+
7690
+ /**
7691
+ * Return a list of modified themes since the last sync.
7692
+ *
7693
+ * Note:
7694
+ * There's no point to store a themes counter since even if the number of
7695
+ * themes didn't change, we still need to check if the versions are all the
7696
+ * same and the activity state is similar.
7697
+ *
7698
+ * @author Vova Feldman (@svovaf)
7699
+ * @since 1.1.8
7700
+ *
7701
+ * @return array|false
7702
+ */
7703
+ private function get_themes_data_for_api() {
7704
+ // Alias.
7705
+ $option_name = 'all_themes';
7706
+
7707
+ $all_cached_themes = self::$_accounts->get_option( $option_name );
7708
+
7709
+ if ( ! is_object( $all_cached_themes ) ) {
7710
+ $all_cached_themes = (object) array(
7711
+ 'timestamp' => '',
7712
+ 'md5' => '',
7713
+ 'themes' => array(),
7714
+ );
7715
+ }
7716
+
7717
+ $time = time();
7718
+
7719
+ if ( ! empty( $all_cached_themes->timestamp ) &&
7720
+ ( $time - $all_cached_themes->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC
7721
+ ) {
7722
+ // Don't send theme updates if last update was in the past 5 min.
7723
+ return false;
7724
+ }
7725
+
7726
+ // Write timestamp to lock the logic.
7727
+ $all_cached_themes->timestamp = $time;
7728
+ self::$_accounts->set_option( $option_name, $all_cached_themes, true );
7729
+
7730
+ // Reload options from DB.
7731
+ self::$_accounts->load( true );
7732
+ $all_cached_themes = self::$_accounts->get_option( $option_name );
7733
+
7734
+ if ( $time != $all_cached_themes->timestamp ) {
7735
+ // If timestamp is different, then another thread captured the lock.
7736
+ return false;
7737
+ }
7738
+
7739
+ // Get active theme.
7740
+ $active_theme = wp_get_theme();
7741
+ $active_theme_stylesheet = $active_theme->get_stylesheet();
7742
+
7743
+ // Check if there's a change in themes.
7744
+ $all_themes = wp_get_themes();
7745
+
7746
+ // Check if themes changed.
7747
+ ksort( $all_themes );
7748
+
7749
+ $themes_signature = '';
7750
+ foreach ( $all_themes as $slug => $data ) {
7751
+ $is_active = ( $slug === $active_theme_stylesheet );
7752
+ $themes_signature .= $slug . ',' .
7753
+ $data->version . ',' .
7754
+ ( $is_active ? '1' : '0' ) . ';';
7755
+ }
7756
+
7757
+ // Check if themes status changed (version or active/inactive).
7758
+ $themes_changed = ( $all_cached_themes->md5 !== md5( $themes_signature ) );
7759
+
7760
+ $themes_update_data = array();
7761
+
7762
+ if ( $themes_changed ) {
7763
+ // Change in themes, report changes.
7764
+
7765
+ // Update existing themes info.
7766
+ foreach ( $all_cached_themes->themes as $slug => $data ) {
7767
+ $is_active = ( $slug === $active_theme_stylesheet );
7768
+
7769
+ if ( ! isset( $all_themes[ $slug ] ) ) {
7770
+ // Plugin uninstalled.
7771
+ $uninstalled_theme_data = $data;
7772
+ $uninstalled_theme_data['is_active'] = false;
7773
+ $uninstalled_theme_data['is_uninstalled'] = true;
7774
+ $themes_update_data[] = $uninstalled_theme_data;
7775
+
7776
+ unset( $all_themes[ $slug ] );
7777
+ unset( $all_cached_themes->themes[ $slug ] );
7778
+ } else if ( $data['is_active'] !== $is_active ||
7779
+ $data['version'] !== $all_themes[ $slug ]->version
7780
+ ) {
7781
+ // Plugin activated or deactivated, or version changed.
7782
+
7783
+ $all_cached_themes->themes[ $slug ]['is_active'] = $is_active;
7784
+ $all_cached_themes->themes[ $slug ]['version'] = $all_themes[ $slug ]->version;
7785
+
7786
+ $themes_update_data[] = $all_cached_themes->themes[ $slug ];
7787
+ }
7788
+ }
7789
+
7790
+ // Find new themes that weren't yet seen before.
7791
+ foreach ( $all_themes as $slug => $data ) {
7792
+ if ( ! isset( $all_cached_themes->themes[ $slug ] ) ) {
7793
+ $is_active = ( $slug === $active_theme_stylesheet );
7794
+
7795
+ // New plugin.
7796
+ $new_plugin = array(
7797
+ 'slug' => $slug,
7798
+ 'version' => $data->version,
7799
+ 'title' => $data->name,
7800
+ 'is_active' => $is_active,
7801
+ 'is_uninstalled' => false,
7802
+ );
7803
+
7804
+ $themes_update_data[] = $new_plugin;
7805
+ $all_cached_themes->themes[ $slug ] = $new_plugin;
7806
+ }
7807
+ }
7808
+
7809
+ $all_cached_themes->md5 = md5( $themes_signature );
7810
+ $all_cached_themes->timestamp = time();
7811
+ self::$_accounts->set_option( $option_name, $all_cached_themes, true );
7812
+ }
7813
+
7814
+ return $themes_update_data;
7815
+ }
7816
+
7817
+ /**
7818
+ * Get site data for API install request.
7819
+ *
7820
+ * @author Vova Feldman (@svovaf)
7821
+ * @since 1.1.2
7822
+ *
7823
+ * @param string[] string $override
7824
+ * @param bool $include_plugins Since 1.1.8 by default include plugin changes.
7825
+ * @param bool $include_themes Since 1.1.8 by default include plugin changes.
7826
+ *
7827
+ * @return array
7828
+ */
7829
+ private function get_install_data_for_api(
7830
+ array $override,
7831
+ $include_plugins = true,
7832
+ $include_themes = true
7833
+ ) {
7834
+ /**
7835
+ * @since 1.1.8 Also send plugin updates.
7836
+ */
7837
+ if ( $include_plugins && ! isset( $override['plugins'] ) ) {
7838
+ $plugins = $this->get_plugins_data_for_api();
7839
+ if ( ! empty( $plugins ) ) {
7840
+ $override['plugins'] = $plugins;
7841
+ }
7842
+ }
7843
+ /**
7844
+ * @since 1.1.8 Also send themes updates.
7845
+ */
7846
+ if ( $include_themes && ! isset( $override['themes'] ) ) {
7847
+ $themes = $this->get_themes_data_for_api();
7848
+ if ( ! empty( $themes ) ) {
7849
+ $override['themes'] = $themes;
7850
+ }
7851
+ }
7852
+
7853
+ $versions = $this->get_versions();
7854
+
7855
+ return array_merge( $versions, array(
7856
+ 'version' => $this->get_plugin_version(),
7857
+ 'is_premium' => $this->is_premium(),
7858
+ 'language' => get_bloginfo( 'language' ),
7859
+ 'charset' => get_bloginfo( 'charset' ),
7860
+ 'title' => get_bloginfo( 'name' ),
7861
+ 'url' => get_site_url(),
7862
+ // Special params.
7863
+ 'is_active' => true,
7864
+ 'is_disconnected' => $this->is_tracking_prohibited(),
7865
+ 'is_uninstalled' => false,
7866
+ ), $override );
7867
+ }
7868
+
7869
+ /**
7870
+ * Update installs details.
7871
+ *
7872
+ * @todo V1 of multiste network support doesn't support plugin and theme data sending.
7873
+ *
7874
+ * @author Vova Feldman (@svovaf)
7875
+ * @since 2.0.0
7876
+ *
7877
+ * @param string[] string $override
7878
+ * @param bool $only_diff
7879
+ * @param bool $include_plugins Since 1.1.8 by default include plugin changes.
7880
+ * @param bool $include_themes Since 1.1.8 by default include plugin changes.
7881
+ *
7882
+ * @return array
7883
+ */
7884
+ private function get_installs_data_for_api(
7885
+ array $override,
7886
+ $only_diff = false,
7887
+ $include_plugins = true,
7888
+ $include_themes = true
7889
+ ) {
7890
+ /**
7891
+ * @since 1.1.8 Also send plugin updates.
7892
+ */
7893
+ // if ( $include_plugins && ! isset( $override['plugins'] ) ) {
7894
+ // $plugins = $this->get_plugins_data_for_api();
7895
+ // if ( ! empty( $plugins ) ) {
7896
+ // $override['plugins'] = $plugins;
7897
+ // }
7898
+ // }
7899
+ /**
7900
+ * @since 1.1.8 Also send themes updates.
7901
+ */
7902
+ // if ( $include_themes && ! isset( $override['themes'] ) ) {
7903
+ // $themes = $this->get_themes_data_for_api();
7904
+ // if ( ! empty( $themes ) ) {
7905
+ // $override['themes'] = $themes;
7906
+ // }
7907
+ // }
7908
+
7909
+ // Common properties.
7910
+ $versions = $this->get_versions();
7911
+ $common = array_merge( $versions, array(
7912
+ 'version' => $this->get_plugin_version(),
7913
+ 'is_premium' => $this->is_premium(),
7914
+ ), $override );
7915
+
7916
+
7917
+ $is_common_diff_for_any_site = false;
7918
+ $common_diff_union = array();
7919
+
7920
+ $installs_data = array();
7921
+
7922
+ $sites = self::get_sites();
7923
+
7924
+ foreach ( $sites as $site ) {
7925
+ $blog_id = self::get_site_blog_id( $site );
7926
+
7927
+ $install = $this->get_install_by_blog_id( $blog_id );
7928
+
7929
+ if ( is_object( $install ) ) {
7930
+ if ( $install->user_id != $this->_user->id ) {
7931
+ // Install belongs to a different owner.
7932
+ continue;
7933
+ }
7934
+
7935
+ if ( ! $this->is_premium() && $install->is_tracking_prohibited() ) {
7936
+ // Don't send updates regarding opted-out installs.
7937
+ continue;
7938
+ }
7939
+
7940
+ $install_data = $this->get_site_info( $site );
7941
+
7942
+ $uid = $install_data['uid'];
7943
+
7944
+ unset( $install_data['blog_id'] );
7945
+ unset( $install_data['uid'] );
7946
+
7947
+ $install_data['is_disconnected'] = $install->is_disconnected;
7948
+ $install_data['is_active'] = $this->is_active_for_site( $blog_id );
7949
+ $install_data['is_uninstalled'] = $install->is_uninstalled;
7950
+
7951
+ $common_diff = null;
7952
+ $is_common_diff = false;
7953
+ if ( $only_diff ) {
7954
+ $install_data = $this->get_install_diff_for_api( $install_data, $install, $override );
7955
+ $common_diff = $this->get_install_diff_for_api( $common, $install, $override );
7956
+
7957
+ $is_common_diff = ! empty( $common_diff );
7958
+
7959
+ if ( $is_common_diff ) {
7960
+ foreach ( $common_diff as $k => $v ) {
7961
+ if ( ! isset( $common_diff_union[ $k ] ) ) {
7962
+ $common_diff_union[ $k ] = $v;
7963
+ }
7964
+ }
7965
+ }
7966
+
7967
+ $is_common_diff_for_any_site = $is_common_diff_for_any_site || $is_common_diff;
7968
+ }
7969
+
7970
+ if ( ! empty( $install_data ) || $is_common_diff ) {
7971
+ // Add install ID and site unique ID.
7972
+ $install_data['id'] = $install->id;
7973
+ $install_data['uid'] = $uid;
7974
+
7975
+ $installs_data[] = $install_data;
7976
+ }
7977
+ }
7978
+ }
7979
+
7980
+ restore_current_blog();
7981
+
7982
+ if ( 0 < count( $installs_data ) && ( $is_common_diff_for_any_site || ! $only_diff ) ) {
7983
+ if ( ! $only_diff ) {
7984
+ $installs_data[] = $common;
7985
+ } else if ( ! empty( $common_diff_union ) ) {
7986
+ $installs_data[] = $common_diff_union;
7987
+ }
7988
+ }
7989
+
7990
+ foreach ( $installs_data as &$data ) {
7991
+ $data = (object) $data;
7992
+ }
7993
+
7994
+ return $installs_data;
7995
+ }
7996
+
7997
+ /**
7998
+ * Compare site actual data to the stored install data and return the differences for an API data sync.
7999
+ *
8000
+ * @author Vova Feldman (@svovaf)
8001
+ * @since 2.0.0
8002
+ *
8003
+ * @param array $site
8004
+ * @param FS_Site $install
8005
+ * @param string[] string $override
8006
+ *
8007
+ * @return array
8008
+ */
8009
+ private function get_install_diff_for_api( $site, $install, $override = array() ) {
8010
+ $diff = array();
8011
+ $special = array();
8012
+ $special_override = false;
8013
+
8014
+ foreach ( $site as $p => $v ) {
8015
+ if ( property_exists( $install, $p ) ) {
8016
+ if ( ( is_bool( $install->{$p} ) || ! empty( $install->{$p} ) ) &&
8017
+ $install->{$p} != $v
8018
+ ) {
8019
+ $install->{$p} = $v;
8020
+ $diff[ $p ] = $v;
8021
+ }
8022
+ } else {
8023
+ $special[ $p ] = $v;
8024
+
8025
+ if ( isset( $override[ $p ] ) ||
8026
+ 'plugins' === $p ||
8027
+ 'themes' === $p
8028
+ ) {
8029
+ $special_override = true;
8030
+ }
8031
+ }
8032
+ }
8033
+
8034
+ if ( $special_override || 0 < count( $diff ) ) {
8035
+ // Add special params only if has at least one
8036
+ // standard param, or if explicitly requested to
8037
+ // override a special param or a param which is not exist
8038
+ // in the install object.
8039
+ $diff = array_merge( $diff, $special );
8040
+ }
8041
+
8042
+ return $diff;
8043
+ }
8044
+
8045
+ /**
8046
+ * Update install only if changed.
8047
+ *
8048
+ * @author Vova Feldman (@svovaf)
8049
+ * @since 1.0.9
8050
+ *
8051
+ * @param string[] string $override
8052
+ * @param bool $flush
8053
+ *
8054
+ * @return false|object|string
8055
+ */
8056
+ private function send_install_update( $override = array(), $flush = false ) {
8057
+ $this->_logger->entrance();
8058
+
8059
+ $check_properties = $this->get_install_data_for_api( $override );
8060
+
8061
+ if ( $flush ) {
8062
+ $params = $check_properties;
8063
+ } else {
8064
+ $params = $this->get_install_diff_for_api( $check_properties, $this->_site, $override );
8065
+ }
8066
+
8067
+ if ( 0 < count( $params ) ) {
8068
+ if ( ! is_multisite() ) {
8069
+ // Update last install sync timestamp.
8070
+ $this->set_cron_execution_timestamp( 'install_sync' );
8071
+ }
8072
+
8073
+ $params['uid'] = $this->get_anonymous_id();
8074
+
8075
+ // Send updated values to FS.
8076
+ $site = $this->get_api_site_scope()->call( '/', 'put', $params );
8077
+
8078
+ if ( $this->is_api_result_entity( $site ) ) {
8079
+ if ( ! is_multisite() ) {
8080
+ // I successfully sent install update, clear scheduled sync if exist.
8081
+ $this->clear_install_sync_cron();
8082
+ }
8083
+ }
8084
+
8085
+ return $site;
8086
+ }
8087
+
8088
+ return false;
8089
+ }
8090
+
8091
+ /**
8092
+ * Update installs only if changed.
8093
+ *
8094
+ * @author Vova Feldman (@svovaf)
8095
+ * @since 2.0.0
8096
+ *
8097
+ * @param string[] string $override
8098
+ * @param bool $flush
8099
+ *
8100
+ * @return false|object|string
8101
+ */
8102
+ private function send_installs_update( $override = array(), $flush = false ) {
8103
+ $this->_logger->entrance();
8104
+
8105
+ $installs_data = $this->get_installs_data_for_api( $override, ! $flush );
8106
+
8107
+ if ( empty( $installs_data ) ) {
8108
+ return false;
8109
+ }
8110
+
8111
+ // Update last install sync timestamp.
8112
+ $this->set_cron_execution_timestamp( 'install_sync' );
8113
+
8114
+ // Send updated values to FS.
8115
+ $result = $this->get_api_user_scope()->call( "/plugins/{$this->_plugin->id}/installs.json", 'put', $installs_data );
8116
+
8117
+ if ( $this->is_api_result_object( $result, 'installs' ) ) {
8118
+ // I successfully sent installs update, clear scheduled sync if exist.
8119
+ $this->clear_install_sync_cron();
8120
+ }
8121
+
8122
+ return $result;
8123
+ }
8124
+
8125
+ /**
8126
+ * Update install only if changed.
8127
+ *
8128
+ * @author Vova Feldman (@svovaf)
8129
+ * @since 1.0.9
8130
+ *
8131
+ * @param string[] string $override
8132
+ * @param bool $flush
8133
+ */
8134
+ private function sync_install( $override = array(), $flush = false ) {
8135
+ $this->_logger->entrance();
8136
+
8137
+ $site = $this->send_install_update( $override, $flush );
8138
+
8139
+ if ( false === $site ) {
8140
+ // No sync required.
8141
+ return;
8142
+ }
8143
+
8144
+ if ( ! $this->is_api_result_entity( $site ) ) {
8145
+ // Failed to sync, don't update locally.
8146
+ return;
8147
+ }
8148
+
8149
+ $this->_site = new FS_Site( $site );
8150
+
8151
+ $this->_store_site( true );
8152
+ }
8153
+
8154
+ /**
8155
+ * Update install only if changed.
8156
+ *
8157
+ * @author Vova Feldman (@svovaf)
8158
+ * @since 1.0.9
8159
+ *
8160
+ * @param string[] string $override
8161
+ * @param bool $flush
8162
+ */
8163
+ private function sync_installs( $override = array(), $flush = false ) {
8164
+ $this->_logger->entrance();
8165
+
8166
+ $result = $this->send_installs_update( $override, $flush );
8167
+
8168
+ if ( false === $result ) {
8169
+ // No sync required.
8170
+ return;
8171
+ }
8172
+
8173
+ if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
8174
+ // Failed to sync, don't update locally.
8175
+ return;
8176
+ }
8177
+
8178
+ $address_to_blog_map = $this->get_address_to_blog_map();
8179
+
8180
+ foreach ( $result->installs as $install ) {
8181
+ $this->_site = new FS_Site( $install );
8182
+
8183
+ $address = trailingslashit( fs_strip_url_protocol( $install->url ) );
8184
+ $blog_id = $address_to_blog_map[ $address ];
8185
+
8186
+ $this->_store_site( true, $blog_id );
8187
+ }
8188
+ }
8189
+
8190
+ /**
8191
+ * Track install's custom event.
8192
+ *
8193
+ * IMPORTANT:
8194
+ * Custom event tracking is currently only supported for specific clients.
8195
+ * If you are not one of them, please don't use this method. If you will,
8196
+ * the API will simply ignore your request based on the plugin ID.
8197
+ *
8198
+ * Need custom tracking for your plugin or theme?
8199
+ * If you are interested in custom event tracking please contact yo@freemius.com
8200
+ * for further details.
8201
+ *
8202
+ * @author Vova Feldman (@svovaf)
8203
+ * @since 1.2.1
8204
+ *
8205
+ * @param string $name Event name.
8206
+ * @param array $properties Associative key/value array with primitive values only
8207
+ * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s.
8208
+ * @param bool $once If true, event will be tracked only once. IMPORTANT: Still trigger the API call.
8209
+ *
8210
+ * @return object|false Event data or FALSE on failure.
8211
+ *
8212
+ * @throws \Freemius_InvalidArgumentException
8213
+ */
8214
+ public function track_event( $name, $properties = array(), $process_at = false, $once = false ) {
8215
+ $this->_logger->entrance( http_build_query( array( 'name' => $name, 'once' => $once ) ) );
8216
+
8217
+ if ( ! $this->is_registered() ) {
8218
+ return false;
8219
+ }
8220
+
8221
+ $event = array( 'type' => $name );
8222
+
8223
+ if ( is_numeric( $process_at ) && $process_at > time() ) {
8224
+ $event['process_at'] = $process_at;
8225
+ }
8226
+
8227
+ if ( $once ) {
8228
+ $event['once'] = true;
8229
+ }
8230
+
8231
+ if ( ! empty( $properties ) ) {
8232
+ // Verify associative array values are primitive.
8233
+ foreach ( $properties as $k => $v ) {
8234
+ if ( ! is_scalar( $v ) ) {
8235
+ throw new Freemius_InvalidArgumentException( 'The $properties argument must be an associative key/value array with primitive values only.' );
8236
+ }
8237
+ }
8238
+
8239
+ $event['properties'] = $properties;
8240
+ }
8241
+
8242
+ $result = $this->get_api_site_scope()->call( 'events.json', 'post', $event );
8243
+
8244
+ return $this->is_api_error( $result ) ?
8245
+ false :
8246
+ $result;
8247
+ }
8248
+
8249
+ /**
8250
+ * Track install's custom event only once, but it still triggers the API call.
8251
+ *
8252
+ * IMPORTANT:
8253
+ * Custom event tracking is currently only supported for specific clients.
8254
+ * If you are not one of them, please don't use this method. If you will,
8255
+ * the API will simply ignore your request based on the plugin ID.
8256
+ *
8257
+ * Need custom tracking for your plugin or theme?
8258
+ * If you are interested in custom event tracking please contact yo@freemius.com
8259
+ * for further details.
8260
+ *
8261
+ * @author Vova Feldman (@svovaf)
8262
+ * @since 1.2.1
8263
+ *
8264
+ * @param string $name Event name.
8265
+ * @param array $properties Associative key/value array with primitive values only
8266
+ * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s.
8267
+ *
8268
+ * @return object|false Event data or FALSE on failure.
8269
+ *
8270
+ * @throws \Freemius_InvalidArgumentException
8271
+ *
8272
+ * @user Freemius::track_event()
8273
+ */
8274
+ public function track_event_once( $name, $properties = array(), $process_at = false ) {
8275
+ return $this->track_event( $name, $properties, $process_at, true );
8276
+ }
8277
+
8278
+ /**
8279
+ * Plugin uninstall hook.
8280
+ *
8281
+ * @author Vova Feldman (@svovaf)
8282
+ * @since 1.0.1
8283
+ *
8284
+ * @param bool $check_user Enforce checking if user have plugins activation privileges.
8285
+ */
8286
+ function _uninstall_plugin_event( $check_user = true ) {
8287
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
8288
+
8289
+ if ( $check_user && ! current_user_can( 'activate_plugins' ) ) {
8290
+ return;
8291
+ }
8292
+
8293
+ $params = array();
8294
+ $uninstall_reason = null;
8295
+ if ( isset( $this->_storage->uninstall_reason ) ) {
8296
+ $uninstall_reason = $this->_storage->uninstall_reason;
8297
+ $params['reason_id'] = $uninstall_reason->id;
8298
+ $params['reason_info'] = $uninstall_reason->info;
8299
+ }
8300
+
8301
+ if ( ! $this->is_registered() ) {
8302
+ // Send anonymous uninstall event only if user submitted a feedback.
8303
+ if ( isset( $uninstall_reason ) ) {
8304
+ if ( isset( $uninstall_reason->is_anonymous ) && ! $uninstall_reason->is_anonymous ) {
8305
+ $this->opt_in( false, false, false, false, true );
8306
+ } else {
8307
+ $params['uid'] = $this->get_anonymous_id();
8308
+ $this->get_api_plugin_scope()->call( 'uninstall.json', 'put', $params );
8309
+ }
8310
+ }
8311
+ } else {
8312
+ $params = array_merge( $params, array(
8313
+ 'is_active' => false,
8314
+ 'is_uninstalled' => true,
8315
+ ) );
8316
+
8317
+ if ( $this->_is_network_active ) {
8318
+ // Send uninstall event.
8319
+ $this->send_installs_update( $params );
8320
+ } else {
8321
+ // Send uninstall event.
8322
+ $this->send_install_update( $params );
8323
+ }
8324
+ }
8325
+
8326
+ // @todo Decide if we want to delete plugin information from db.
8327
+ }
8328
+
8329
+ /**
8330
+ * Set the basename of the current product and hook _activate_plugin_event_hook() to the activation action.
8331
+ *
8332
+ * @author Vova Feldman (@svovaf)
8333
+ * @since 2.2.1
8334
+ *
8335
+ * @param string $is_premium
8336
+ * @param string $caller
8337
+ *
8338
+ * @return string
8339
+ */
8340
+ function set_basename( $is_premium, $caller ) {
8341
+ $basename = plugin_basename( $caller );
8342
+
8343
+ $current_basename = $is_premium ?
8344
+ $this->_premium_plugin_basename :
8345
+ $this->_free_plugin_basename;
8346
+
8347
+ if ( $current_basename == $basename ) {
8348
+ // Basename value set correctly.
8349
+ return;
8350
+ }
8351
+
8352
+ if ( $is_premium ) {
8353
+ $this->_premium_plugin_basename = $basename;
8354
+ } else {
8355
+ $this->_free_plugin_basename = $basename;
8356
+ }
8357
+
8358
+ $plugin_dir = dirname( $this->_plugin_dir_path ) . '/';
8359
+
8360
+ register_activation_hook(
8361
+ $plugin_dir . $basename,
8362
+ array( &$this, '_activate_plugin_event_hook' )
8363
+ );
8364
+ }
8365
+
8366
+ /**
8367
+ * @author Vova Feldman (@svovaf)
8368
+ * @since 1.1.1
8369
+ * @since 2.2.1 If the context product is in its premium version, use the current module's basename, even if it was renamed.
8370
+ *
8371
+ * @return string
8372
+ */
8373
+ function premium_plugin_basename() {
8374
+ if ( ! isset( $this->_premium_plugin_basename ) ) {
8375
+ $this->_premium_plugin_basename = $this->is_premium() ?
8376
+ // The product is premium, so use the current basename.
8377
+ $this->_plugin_basename :
8378
+ $this->get_premium_slug() . '/' . basename( $this->_free_plugin_basename );
8379
+ }
8380
+
8381
+ return $this->_premium_plugin_basename;
8382
+ }
8383
+
8384
+ /**
8385
+ * Uninstall plugin hook. Called only when connected his account with Freemius for active sites tracking.
8386
+ *
8387
+ * @author Vova Feldman (@svovaf)
8388
+ * @since 1.0.2
8389
+ */
8390
+ public static function _uninstall_plugin_hook() {
8391
+ self::_load_required_static();
8392
+
8393
+ self::$_static_logger->entrance();
8394
+
8395
+ if ( ! current_user_can( 'activate_plugins' ) ) {
8396
+ return;
8397
+ }
8398
+
8399
+ $plugin_file = substr( current_filter(), strlen( 'uninstall_' ) );
8400
+
8401
+ self::$_static_logger->info( 'plugin = ' . $plugin_file );
8402
+
8403
+ define( 'WP_FS__UNINSTALL_MODE', true );
8404
+
8405
+ $fs = self::get_instance_by_file( $plugin_file );
8406
+
8407
+ if ( is_object( $fs ) ) {
8408
+ self::require_plugin_essentials();
8409
+
8410
+ if ( is_plugin_active( $fs->_free_plugin_basename ) ||
8411
+ is_plugin_active( $fs->premium_plugin_basename() )
8412
+ ) {
8413
+ // Deleting Free or Premium plugin version while the other version still installed.
8414
+ return;
8415
+ }
8416
+
8417
+ $fs->_uninstall_plugin_event();
8418
+
8419
+ $fs->do_action( 'after_uninstall' );
8420
+ }
8421
+ }
8422
+
8423
+ #----------------------------------------------------------------------------------
8424
+ #region Plugin Information
8425
+ #----------------------------------------------------------------------------------
8426
+
8427
+ /**
8428
+ * Load WordPress core plugin.php essential module.
8429
+ *
8430
+ * @author Vova Feldman (@svovaf)
8431
+ * @since 1.1.1
8432
+ */
8433
+ private static function require_plugin_essentials() {
8434
+ if ( ! function_exists( 'get_plugins' ) ) {
8435
+ self::$_static_logger->log( 'Including wp-admin/includes/plugin.php...' );
8436
+
8437
+ require_once ABSPATH . 'wp-admin/includes/plugin.php';
8438
+ }
8439
+ }
8440
+
8441
+ /**
8442
+ * Load WordPress core pluggable.php module.
8443
+ *
8444
+ * @author Vova Feldman (@svovaf)
8445
+ * @since 1.1.2
8446
+ */
8447
+ private static function require_pluggable_essentials() {
8448
+ if ( ! function_exists( 'wp_get_current_user' ) ) {
8449
+ require_once ABSPATH . 'wp-includes/pluggable.php';
8450
+ }
8451
+ }
8452
+
8453
+ /**
8454
+ * Return plugin data.
8455
+ *
8456
+ * @author Vova Feldman (@svovaf)
8457
+ * @since 1.0.1
8458
+ *
8459
+ * @return array
8460
+ */
8461
+ function get_plugin_data() {
8462
+ if ( ! isset( $this->_plugin_data ) ) {
8463
+ self::require_plugin_essentials();
8464
+
8465
+ if ( $this->is_plugin() ) {
8466
+ /**
8467
+ * @author Vova Feldman (@svovaf)
8468
+ * @since 1.2.0 When using get_plugin_data() do NOT translate plugin data.
8469
+ *
8470
+ * @link https://github.com/Freemius/wordpress-sdk/issues/77
8471
+ */
8472
+ $plugin_data = get_plugin_data(
8473
+ $this->_plugin_main_file_path,
8474
+ false,
8475
+ false
8476
+ );
8477
+ } else {
8478
+ $theme_data = wp_get_theme();
8479
+
8480
+ if ( $this->_plugin_basename !== $theme_data->get_stylesheet() && is_child_theme() ) {
8481
+ $parent_theme = $theme_data->parent();
8482
+
8483
+ if ( ( $parent_theme instanceof WP_Theme ) && $this->_plugin_basename === $parent_theme->get_stylesheet() ) {
8484
+ $theme_data = $parent_theme;
8485
+ }
8486
+ }
8487
+
8488
+ $plugin_data = array(
8489
+ 'Name' => $theme_data->get( 'Name' ),
8490
+ 'Version' => $theme_data->get( 'Version' ),
8491
+ 'Author' => $theme_data->get( 'Author' ),
8492
+ 'Description' => $theme_data->get( 'Description' ),
8493
+ 'PluginURI' => $theme_data->get( 'ThemeURI' ),
8494
+ );
8495
+ }
8496
+
8497
+ $this->_plugin_data = $plugin_data;
8498
+ }
8499
+
8500
+ return $this->_plugin_data;
8501
+ }
8502
+
8503
+ /**
8504
+ * @author Vova Feldman (@svovaf)
8505
+ * @since 1.0.1
8506
+ * @since 1.2.2.5 If slug not set load slug by module ID.
8507
+ *
8508
+ * @return string Plugin slug.
8509
+ */
8510
+ function get_slug() {
8511
+ if ( ! isset( $this->_slug ) ) {
8512
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
8513
+ $this->_slug = $id_slug_type_path_map[ $this->_module_id ]['slug'];
8514
+ }
8515
+
8516
+ return $this->_slug;
8517
+ }
8518
+
8519
+ /**
8520
+ * @author Leo Fajardo (@leorw)
8521
+ * @since 2.2.1
8522
+ *
8523
+ * @return string
8524
+ */
8525
+ function get_premium_slug() {
8526
+ return is_object( $this->_plugin ) ?
8527
+ $this->_plugin->premium_slug :
8528
+ "{$this->_slug}-premium";
8529
+ }
8530
+
8531
+ /**
8532
+ * Retrieve the desired folder name for the product.
8533
+ *
8534
+ * @author Vova Feldman (@svovaf)
8535
+ * @since 1.2.1.7
8536
+ *
8537
+ * @return string Plugin slug.
8538
+ */
8539
+ function get_target_folder_name() {
8540
+ return $this->can_use_premium_code() ?
8541
+ $this->_plugin->premium_slug :
8542
+ $this->_slug;
8543
+ }
8544
+
8545
+ /**
8546
+ * @author Vova Feldman (@svovaf)
8547
+ * @since 1.0.1
8548
+ *
8549
+ * @return number Plugin ID.
8550
+ */
8551
+ function get_id() {
8552
+ return $this->_plugin->id;
8553
+ }
8554
+
8555
+ /**
8556
+ * @author Vova Feldman (@svovaf)
8557
+ * @since 1.2.1.5
8558
+ *
8559
+ * @return string Freemius SDK version
8560
+ */
8561
+ function get_sdk_version() {
8562
+ return $this->version;
8563
+ }
8564
+
8565
+ /**
8566
+ * @author Vova Feldman (@svovaf)
8567
+ * @since 1.2.1.5
8568
+ *
8569
+ * @return number Parent plugin ID (if parent exist).
8570
+ */
8571
+ function get_parent_id() {
8572
+ return $this->is_addon() ?
8573
+ $this->get_parent_instance()->get_id() :
8574
+ $this->_plugin->id;
8575
+ }
8576
+
8577
+ /**
8578
+ * @author Vova Feldman (@svovaf)
8579
+ * @since 1.0.1
8580
+ *
8581
+ * @return string Plugin public key.
8582
+ */
8583
+ function get_public_key() {
8584
+ return $this->_plugin->public_key;
8585
+ }
8586
+
8587
+ /**
8588
+ * Will be available only on sandbox mode.
8589
+ *
8590
+ * @author Vova Feldman (@svovaf)
8591
+ * @since 1.0.4
8592
+ *
8593
+ * @return mixed Plugin secret key.
8594
+ */
8595
+ function get_secret_key() {
8596
+ return $this->_plugin->secret_key;
8597
+ }
8598
+
8599
+ /**
8600
+ * @author Vova Feldman (@svovaf)
8601
+ * @since 1.1.1
8602
+ *
8603
+ * @return bool
8604
+ */
8605
+ function has_secret_key() {
8606
+ return ! empty( $this->_plugin->secret_key );
8607
+ }
8608
+
8609
+ /**
8610
+ * @author Vova Feldman (@svovaf)
8611
+ * @since 1.0.9
8612
+ *
8613
+ * @param string|bool $premium_suffix
8614
+ *
8615
+ * @return string
8616
+ */
8617
+ function get_plugin_name( $premium_suffix = false ) {
8618
+ $this->_logger->entrance();
8619
+
8620
+ /**
8621
+ * This `if-else` can be squeezed into a single `if` but I intentionally split it for code readability.
8622
+ *
8623
+ * @author Vova Feldman
8624
+ */
8625
+ if ( ! isset( $this->_plugin_name ) ) {
8626
+ // Name is not yet set.
8627
+ $this->set_name( $premium_suffix );
8628
+ } else if (
8629
+ ! empty( $premium_suffix ) &&
8630
+ ( ! is_object( $this->_plugin ) || $this->_plugin->premium_suffix !== $premium_suffix )
8631
+ ) {
8632
+ // Name is already set, but there's a change in the premium suffix.
8633
+ $this->set_name( $premium_suffix );
8634
+ }
8635
+
8636
+ return $this->_plugin_name;
8637
+ }
8638
+
8639
+ /**
8640
+ * Calculates and stores the product's name. This helper function was created specifically for get_plugin_name() just to make the code clearer.
8641
+ *
8642
+ * @author Vova Feldman (@svovaf)
8643
+ * @since 2.2.1
8644
+ *
8645
+ * @param string $premium_suffix
8646
+ */
8647
+ private function set_name( $premium_suffix = '' ) {
8648
+ $plugin_data = $this->get_plugin_data();
8649
+
8650
+ // Get name.
8651
+ $this->_plugin_name = $plugin_data['Name'];
8652
+
8653
+ if ( is_string( $premium_suffix ) ) {
8654
+ $premium_suffix = trim( $premium_suffix );
8655
+
8656
+ if ( ! empty( $premium_suffix ) ) {
8657
+ // Check if plugin name contains " (premium)" or a custom suffix and remove it.
8658
+ $suffix = ( ' ' . strtolower( $premium_suffix ) );
8659
+ $suffix_len = strlen( $suffix );
8660
+
8661
+ if ( strlen( $plugin_data['Name'] ) > $suffix_len &&
8662
+ $suffix === substr( strtolower( $plugin_data['Name'] ), - $suffix_len )
8663
+ ) {
8664
+ $this->_plugin_name = substr( $plugin_data['Name'], 0, - $suffix_len );
8665
+ }
8666
+ }
8667
+ }
8668
+
8669
+ $this->_logger->departure( 'Name = ' . $this->_plugin_name );
8670
+ }
8671
+
8672
+ /**
8673
+ * @author Vova Feldman (@svovaf)
8674
+ * @since 1.0.0
8675
+ *
8676
+ * @return string
8677
+ */
8678
+ function get_plugin_version() {
8679
+ $this->_logger->entrance();
8680
+
8681
+ $plugin_data = $this->get_plugin_data();
8682
+
8683
+ $this->_logger->departure( 'Version = ' . $plugin_data['Version'] );
8684
+
8685
+ return $this->apply_filters( 'plugin_version', $plugin_data['Version'] );
8686
+ }
8687
+
8688
+ /**
8689
+ * @author Vova Feldman (@svovaf)
8690
+ * @since 1.2.1.7
8691
+ *
8692
+ * @return string
8693
+ */
8694
+ function get_plugin_title() {
8695
+ $this->_logger->entrance();
8696
+
8697
+ $title = $this->_plugin->title;
8698
+
8699
+ return $this->apply_filters( 'plugin_title', $title );
8700
+ }
8701
+
8702
+ /**
8703
+ * @author Vova Feldman (@svovaf)
8704
+ * @since 1.2.2.7
8705
+ *
8706
+ * @param bool $lowercase
8707
+ *
8708
+ * @return string
8709
+ */
8710
+ function get_module_label( $lowercase = false ) {
8711
+ $label = $this->is_addon() ?
8712
+ $this->get_text_inline( 'Add-On', 'addon' ) :
8713
+ ( $this->is_plugin() ?
8714
+ $this->get_text_inline( 'Plugin', 'plugin' ) :
8715
+ $this->get_text_inline( 'Theme', 'theme' ) );
8716
+
8717
+ if ( $lowercase ) {
8718
+ $label = strtolower( $label );
8719
+ }
8720
+
8721
+ return $label;
8722
+ }
8723
+
8724
+ /**
8725
+ * @author Vova Feldman (@svovaf)
8726
+ * @since 1.0.4
8727
+ *
8728
+ * @return string
8729
+ */
8730
+ function get_plugin_basename() {
8731
+ if ( ! isset( $this->_plugin_basename ) ) {
8732
+ if ( $this->is_plugin() ) {
8733
+ $this->_plugin_basename = plugin_basename( $this->_plugin_main_file_path );
8734
+ } else {
8735
+ $this->_plugin_basename = basename( dirname( $this->_plugin_main_file_path ) );
8736
+ }
8737
+ }
8738
+
8739
+ return $this->_plugin_basename;
8740
+ }
8741
+
8742
+ function get_plugin_folder_name() {
8743
+ $this->_logger->entrance();
8744
+
8745
+ $plugin_folder = $this->_plugin_basename;
8746
+
8747
+ while ( '.' !== dirname( $plugin_folder ) ) {
8748
+ $plugin_folder = dirname( $plugin_folder );
8749
+ }
8750
+
8751
+ $this->_logger->departure( 'Folder Name = ' . $plugin_folder );
8752
+
8753
+ return $plugin_folder;
8754
+ }
8755
+
8756
+ #endregion ------------------------------------------------------------------
8757
+
8758
+ /* Account
8759
+ ------------------------------------------------------------------------------------------------------------------*/
8760
+
8761
+ /**
8762
+ * Find plugin's slug by plugin's basename.
8763
+ *
8764
+ * @author Vova Feldman (@svovaf)
8765
+ * @since 1.0.9
8766
+ *
8767
+ * @param string $plugin_base_name
8768
+ *
8769
+ * @return false|string
8770
+ */
8771
+ private static function find_slug_by_basename( $plugin_base_name ) {
8772
+ $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
8773
+
8774
+ if ( ! array( $file_slug_map ) || ! isset( $file_slug_map[ $plugin_base_name ] ) ) {
8775
+ return false;
8776
+ }
8777
+
8778
+ return $file_slug_map[ $plugin_base_name ];
8779
+ }
8780
+
8781
+ /**
8782
+ * Store the map between the plugin's basename to the slug.
8783
+ *
8784
+ * @author Vova Feldman (@svovaf)
8785
+ * @since 1.0.9
8786
+ */
8787
+ private function store_file_slug_map() {
8788
+ $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
8789
+
8790
+ if ( ! array( $file_slug_map ) ) {
8791
+ $file_slug_map = array();
8792
+ }
8793
+
8794
+ if ( ! isset( $file_slug_map[ $this->_plugin_basename ] ) ||
8795
+ $file_slug_map[ $this->_plugin_basename ] !== $this->_slug
8796
+ ) {
8797
+ $file_slug_map[ $this->_plugin_basename ] = $this->_slug;
8798
+ self::$_accounts->set_option( 'file_slug_map', $file_slug_map, true );
8799
+ }
8800
+ }
8801
+
8802
+ /**
8803
+ * @return array[number]FS_User
8804
+ */
8805
+ static function get_all_users() {
8806
+ $users = self::$_accounts->get_option( 'users', array() );
8807
+
8808
+ if ( ! is_array( $users ) ) {
8809
+ $users = array();
8810
+ }
8811
+
8812
+ return $users;
8813
+ }
8814
+
8815
+ /**
8816
+ * @param string $module_type
8817
+ * @param null|int $blog_id Since 2.0.0
8818
+ *
8819
+ * @return array[string]FS_Site
8820
+ */
8821
+ private static function get_all_sites(
8822
+ $module_type = WP_FS__MODULE_TYPE_PLUGIN,
8823
+ $blog_id = null
8824
+ ) {
8825
+ $sites = self::get_account_option( 'sites', $module_type, $blog_id );
8826
+
8827
+ if ( ! is_array( $sites ) ) {
8828
+ $sites = array();
8829
+ }
8830
+
8831
+ return $sites;
8832
+ }
8833
+
8834
+ /**
8835
+ * @author Leo Fajardo (@leorw)
8836
+ *
8837
+ * @since 1.2.2
8838
+ *
8839
+ * @param string $option_name
8840
+ * @param string $module_type
8841
+ * @param null|int $network_level_or_blog_id Since 2.0.0
8842
+ *
8843
+ * @return mixed
8844
+ */
8845
+ private static function get_account_option( $option_name, $module_type = null, $network_level_or_blog_id = null ) {
8846
+ if ( ! is_null( $module_type ) && WP_FS__MODULE_TYPE_PLUGIN !== $module_type ) {
8847
+ $option_name = $module_type . '_' . $option_name;
8848
+ }
8849
+
8850
+ return self::$_accounts->get_option( $option_name, array(), $network_level_or_blog_id );
8851
+ }
8852
+
8853
+ /**
8854
+ * @author Leo Fajardo (@leorw)
8855
+ *
8856
+ * @since 1.2.2
8857
+ *
8858
+ * @param string $option_name
8859
+ * @param mixed $option_value
8860
+ * @param bool $store
8861
+ * @param null|int $network_level_or_blog_id Since 2.0.0
8862
+ */
8863
+ private function set_account_option( $option_name, $option_value, $store, $network_level_or_blog_id = null ) {
8864
+ self::set_account_option_by_module(
8865
+ $this->_module_type,
8866
+ $option_name,
8867
+ $option_value,
8868
+ $store,
8869
+ $network_level_or_blog_id
8870
+ );
8871
+ }
8872
+
8873
+ /**
8874
+ * @author Vova Feldman (@svovaf)
8875
+ *
8876
+ * @since 1.2.2.7
8877
+ *
8878
+ * @param string $module_type
8879
+ * @param string $option_name
8880
+ * @param mixed $option_value
8881
+ * @param bool $store
8882
+ * @param null|int $network_level_or_blog_id Since 2.0.0
8883
+ */
8884
+ private static function set_account_option_by_module(
8885
+ $module_type,
8886
+ $option_name,
8887
+ $option_value,
8888
+ $store,
8889
+ $network_level_or_blog_id = null
8890
+ ) {
8891
+ if ( WP_FS__MODULE_TYPE_PLUGIN != $module_type ) {
8892
+ $option_name = $module_type . '_' . $option_name;
8893
+ }
8894
+
8895
+ self::$_accounts->set_option( $option_name, $option_value, $store, $network_level_or_blog_id );
8896
+ }
8897
+
8898
+ /**
8899
+ * @author Vova Feldman (@svovaf)
8900
+ * @since 1.0.6
8901
+ *
8902
+ * @param number|null $module_id
8903
+ *
8904
+ * @return FS_Plugin_License[]
8905
+ */
8906
+ private static function get_all_licenses( $module_id = null ) {
8907
+ $licenses = self::get_account_option( 'all_licenses' );
8908
+
8909
+ if ( ! is_array( $licenses ) ) {
8910
+ $licenses = array();
8911
+ }
8912
+
8913
+ if ( is_null( $module_id ) ) {
8914
+ return $licenses;
8915
+ }
8916
+
8917
+ $licenses = isset( $licenses[ $module_id ] ) ?
8918
+ $licenses[ $module_id ] :
8919
+ array();
8920
+
8921
+ return $licenses;
8922
+ }
8923
+
8924
+ /**
8925
+ * @author Leo Fajardo (@leorw)
8926
+ * @since 2.0.0
8927
+ *
8928
+ * @return array
8929
+ */
8930
+ private static function get_all_licenses_by_module_type() {
8931
+ $licenses = self::get_account_option( 'all_licenses' );
8932
+
8933
+ $licenses_by_module_type = array(
8934
+ WP_FS__MODULE_TYPE_PLUGIN => array(),
8935
+ WP_FS__MODULE_TYPE_THEME => array()
8936
+ );
8937
+
8938
+ if ( ! is_array( $licenses ) ) {
8939
+ return $licenses_by_module_type;
8940
+ }
8941
+
8942
+ foreach ( $licenses as $module_id => $module_licenses ) {
8943
+ $fs = self::get_instance_by_id( $module_id );
8944
+ if ( false === $fs ) {
8945
+ continue;
8946
+ }
8947
+
8948
+ $licenses_by_module_type[ $fs->_module_type ] = array_merge( $licenses_by_module_type[ $fs->_module_type ], $module_licenses );
8949
+ }
8950
+
8951
+ return $licenses_by_module_type;
8952
+ }
8953
+
8954
+ /**
8955
+ * @author Leo Fajardo (@leorw)
8956
+ * @since 2.0.0
8957
+ *
8958
+ * @param number $module_id
8959
+ * @param number|null $user_id
8960
+ *
8961
+ * @return array
8962
+ */
8963
+ private static function get_user_id_license_ids_map( $module_id, $user_id = null ) {
8964
+ $all_modules_user_id_license_ids_map = self::get_account_option( 'user_id_license_ids_map' );
8965
+
8966
+ if ( ! is_array( $all_modules_user_id_license_ids_map ) ) {
8967
+ $all_modules_user_id_license_ids_map = array();
8968
+ }
8969
+
8970
+ $user_id_license_ids_map = isset( $all_modules_user_id_license_ids_map[ $module_id ] ) ?
8971
+ $all_modules_user_id_license_ids_map[ $module_id ] :
8972
+ array();
8973
+
8974
+ if ( FS_User::is_valid_id( $user_id ) ) {
8975
+ $user_id_license_ids_map = isset( $user_id_license_ids_map[ $user_id ] ) ?
8976
+ $user_id_license_ids_map[ $user_id ] :
8977
+ array();
8978
+ }
8979
+
8980
+ return $user_id_license_ids_map;
8981
+ }
8982
+
8983
+ /**
8984
+ * @author Leo Fajardo (@leorw)
8985
+ * @since 2.0.0
8986
+ *
8987
+ * @param array $new_user_id_license_ids_map
8988
+ * @param number $module_id
8989
+ * @param number|null $user_id
8990
+ */
8991
+ private static function store_user_id_license_ids_map( $new_user_id_license_ids_map, $module_id, $user_id = null ) {
8992
+ $all_modules_user_id_license_ids_map = self::get_account_option( 'user_id_license_ids_map' );
8993
+ if ( ! is_array( $all_modules_user_id_license_ids_map ) ) {
8994
+ $all_modules_user_id_license_ids_map = array();
8995
+ }
8996
+
8997
+ if ( ! isset( $all_modules_user_id_license_ids_map[ $module_id ] ) ) {
8998
+ $all_modules_user_id_license_ids_map[ $module_id ] = array();
8999
+ }
9000
+
9001
+ if ( FS_User::is_valid_id( $user_id ) ) {
9002
+ $all_modules_user_id_license_ids_map[ $module_id ][ $user_id ] = $new_user_id_license_ids_map;
9003
+ } else {
9004
+ $all_modules_user_id_license_ids_map[ $module_id ] = $new_user_id_license_ids_map;
9005
+ }
9006
+
9007
+ self::$_accounts->set_option( 'user_id_license_ids_map', $all_modules_user_id_license_ids_map, true );
9008
+ }
9009
+
9010
+ /**
9011
+ * Get a collection of the user's linked license IDs.
9012
+ *
9013
+ * @author Vova Feldman (@svovaf)
9014
+ * @since 2.0.0
9015
+ *
9016
+ * @param number $user_id
9017
+ *
9018
+ * @return number[]
9019
+ */
9020
+ private function get_user_linked_license_ids( $user_id ) {
9021
+ return self::get_user_id_license_ids_map( $this->_module_id, $user_id );
9022
+ }
9023
+
9024
+ /**
9025
+ * Override the user's linked license IDs with a new IDs collection.
9026
+ *
9027
+ * @author Vova Feldman (@svovaf)
9028
+ * @since 2.0.0
9029
+ *
9030
+ * @param number $user_id
9031
+ * @param number[] $license_ids
9032
+ */
9033
+ private function set_user_linked_license_ids( $user_id, array $license_ids ) {
9034
+ self::store_user_id_license_ids_map( $license_ids, $this->_module_id, $user_id );
9035
+ }
9036
+
9037
+ /**
9038
+ * Link a specified license ID to a given user.
9039
+ *
9040
+ * @author Vova Feldman (@svovaf)
9041
+ * @since 2.0.0
9042
+ *
9043
+ * @param number $license_id
9044
+ * @param number $user_id
9045
+ */
9046
+ private function link_license_2_user( $license_id, $user_id ) {
9047
+ $license_ids = $this->get_user_linked_license_ids( $user_id );
9048
+
9049
+ if ( in_array( $license_id, $license_ids ) ) {
9050
+ // License already linked.
9051
+ return;
9052
+ }
9053
+
9054
+ $license_ids[] = $license_id;
9055
+
9056
+ $this->set_user_linked_license_ids( $user_id, $license_ids );
9057
+ }
9058
+
9059
+ /**
9060
+ * @param string|bool $module_type
9061
+ *
9062
+ * @return FS_Plugin_Plan[]
9063
+ */
9064
+ private static function get_all_plans( $module_type = false ) {
9065
+ $plans = self::get_account_option( 'plans', $module_type );
9066
+
9067
+ if ( ! is_array( $plans ) ) {
9068
+ $plans = array();
9069
+ }
9070
+
9071
+ return $plans;
9072
+ }
9073
+
9074
+ /**
9075
+ * @author Vova Feldman (@svovaf)
9076
+ * @since 1.0.4
9077
+ *
9078
+ * @return FS_Plugin_Tag[]
9079
+ */
9080
+ private static function get_all_updates() {
9081
+ $updates = self::$_accounts->get_option( 'updates', array() );
9082
+
9083
+ if ( ! is_array( $updates ) ) {
9084
+ $updates = array();
9085
+ }
9086
+
9087
+ return $updates;
9088
+ }
9089
+
9090
+ /**
9091
+ * @author Vova Feldman (@svovaf)
9092
+ * @since 1.0.6
9093
+ *
9094
+ * @return array<number,FS_Plugin[]>|false
9095
+ */
9096
+ private static function get_all_addons() {
9097
+ $addons = self::$_accounts->get_option( 'addons', array() );
9098
+
9099
+ if ( ! is_array( $addons ) ) {
9100
+ $addons = array();
9101
+ }
9102
+
9103
+ return $addons;
9104
+ }
9105
+
9106
+ /**
9107
+ * @author Vova Feldman (@svovaf)
9108
+ * @since 1.0.6
9109
+ *
9110
+ * @return FS_Plugin[]|false
9111
+ */
9112
+ private static function get_all_account_addons() {
9113
+ $addons = self::$_accounts->get_option( 'account_addons', array() );
9114
+
9115
+ if ( ! is_array( $addons ) ) {
9116
+ $addons = array();
9117
+ }
9118
+
9119
+ return $addons;
9120
+ }
9121
+
9122
+ /**
9123
+ * Check if user has connected his account (opted-in).
9124
+ *
9125
+ * Note:
9126
+ * If the user opted-in and opted-out on a later stage,
9127
+ * this will still return true. If you want to check if the
9128
+ * user is currently opted-in, use:
9129
+ * `$fs->is_registered() && $fs->is_tracking_allowed()`
9130
+ *
9131
+ * @author Vova Feldman (@svovaf)
9132
+ * @since 1.0.1
9133
+ * @return bool
9134
+ */
9135
+ function is_registered() {
9136
+ return is_object( $this->_user );
9137
+ }
9138
+
9139
+ /**
9140
+ * Returns TRUE if the user opted-in and didn't disconnect (opt-out).
9141
+ *
9142
+ * @author Leo Fajardo (@leorw)
9143
+ * @since 1.2.1.5
9144
+ *
9145
+ * @return bool
9146
+ */
9147
+ function is_tracking_allowed() {
9148
+ return ( is_object( $this->_site ) && $this->_site->is_tracking_allowed() );
9149
+ }
9150
+
9151
+ /**
9152
+ * @author Vova Feldman (@svovaf)
9153
+ * @since 1.0.4
9154
+ *
9155
+ * @return FS_Plugin
9156
+ */
9157
+ function get_plugin() {
9158
+ return $this->_plugin;
9159
+ }
9160
+
9161
+ /**
9162
+ * @author Vova Feldman (@svovaf)
9163
+ * @since 1.0.3
9164
+ *
9165
+ * @return FS_User
9166
+ */
9167
+ function get_user() {
9168
+ return $this->_user;
9169
+ }
9170
+
9171
+ /**
9172
+ * @author Vova Feldman (@svovaf)
9173
+ * @since 1.0.3
9174
+ *
9175
+ * @return FS_Site
9176
+ */
9177
+ function get_site() {
9178
+ return $this->_site;
9179
+ }
9180
+
9181
+ /**
9182
+ * Get plugin add-ons.
9183
+ *
9184
+ * @author Vova Feldman (@svovaf)
9185
+ * @since 1.0.6
9186
+ *
9187
+ * @since 1.1.7.3 If not yet loaded, fetch data from the API.
9188
+ *
9189
+ * @param bool $flush
9190
+ *
9191
+ * @return FS_Plugin[]|false
9192
+ */
9193
+ function get_addons( $flush = false ) {
9194
+ $this->_logger->entrance();
9195
+
9196
+ if ( ! $this->_has_addons ) {
9197
+ return false;
9198
+ }
9199
+
9200
+ $addons = $this->sync_addons( $flush );
9201
+
9202
+ return ( ! is_array( $addons ) || empty( $addons ) ) ?
9203
+ false :
9204
+ $addons;
9205
+ }
9206
+
9207
+ /**
9208
+ * @author Vova Feldman (@svovaf)
9209
+ * @since 1.0.6
9210
+ *
9211
+ * @return FS_Plugin[]|false
9212
+ */
9213
+ function get_account_addons() {
9214
+ $this->_logger->entrance();
9215
+
9216
+ $addons = self::get_all_account_addons();
9217
+
9218
+ if ( ! is_array( $addons ) ||
9219
+ ! isset( $addons[ $this->_plugin->id ] ) ||
9220
+ ! is_array( $addons[ $this->_plugin->id ] ) ||
9221
+ 0 === count( $addons[ $this->_plugin->id ] )
9222
+ ) {
9223
+ return false;
9224
+ }
9225
+
9226
+ return $addons[ $this->_plugin->id ];
9227
+ }
9228
+
9229
+ /**
9230
+ * Check if user has any
9231
+ *
9232
+ * @author Vova Feldman (@svovaf)
9233
+ * @since 1.1.6
9234
+ *
9235
+ * @return bool
9236
+ */
9237
+ function has_account_addons() {
9238
+ $addons = $this->get_account_addons();
9239
+
9240
+ return is_array( $addons ) && ( 0 < count( $addons ) );
9241
+ }
9242
+
9243
+
9244
+ /**
9245
+ * Get add-on by ID (from local data).
9246
+ *
9247
+ * @author Vova Feldman (@svovaf)
9248
+ * @since 1.0.6
9249
+ *
9250
+ * @param number $id
9251
+ *
9252
+ * @return FS_Plugin|false
9253
+ */
9254
+ function get_addon( $id ) {
9255
+ $this->_logger->entrance();
9256
+
9257
+ $addons = $this->get_addons();
9258
+
9259
+ if ( is_array( $addons ) ) {
9260
+ foreach ( $addons as $addon ) {
9261
+ if ( $id == $addon->id ) {
9262
+ return $addon;
9263
+ }
9264
+ }
9265
+ }
9266
+
9267
+ return false;
9268
+ }
9269
+
9270
+ /**
9271
+ * Get add-on by slug (from local data).
9272
+ *
9273
+ * @author Vova Feldman (@svovaf)
9274
+ * @since 1.0.6
9275
+ *
9276
+ * @param string $slug
9277
+ *
9278
+ * @param bool $flush
9279
+ *
9280
+ * @return FS_Plugin|false
9281
+ */
9282
+ function get_addon_by_slug( $slug, $flush = false ) {
9283
+ $this->_logger->entrance();
9284
+
9285
+ $addons = $this->get_addons( $flush );
9286
+
9287
+ if ( is_array( $addons ) ) {
9288
+ foreach ( $addons as $addon ) {
9289
+ if ( $slug === $addon->slug ) {
9290
+ return $addon;
9291
+ }
9292
+ }
9293
+ }
9294
+
9295
+ return false;
9296
+ }
9297
+
9298
+ /**
9299
+ * @author Vova Feldman (@svovaf)
9300
+ * @since 2.0.0
9301
+ *
9302
+ * @param number $user_id
9303
+ *
9304
+ * @return FS_User
9305
+ */
9306
+ static function _get_user_by_id( $user_id ) {
9307
+ self::$_static_logger->entrance( "user_id = {$user_id}" );
9308
+
9309
+ $users = self::get_all_users();
9310
+
9311
+ if ( is_array( $users ) ) {
9312
+ if ( isset( $users[ $user_id ] ) &&
9313
+ $users[ $user_id ] instanceof FS_User &&
9314
+ $user_id == $users[ $user_id ]->id
9315
+ ) {
9316
+ return $users[ $user_id ];
9317
+ }
9318
+
9319
+ // If user wasn't found by the key, iterate over all the users collection.
9320
+ foreach ( $users as $user ) {
9321
+ /**
9322
+ * @var FS_User $user
9323
+ */
9324
+ if ( $user_id == $user->id ) {
9325
+ return $user;
9326
+ }
9327
+ }
9328
+ }
9329
+
9330
+ return null;
9331
+ }
9332
+
9333
+ /**
9334
+ * Checks if a Freemius user_id is associated with a super-admin.
9335
+ *
9336
+ * @author Vova Feldman (@svovaf)
9337
+ * @since 2.0.0
9338
+ *
9339
+ * @param number $user_id
9340
+ *
9341
+ * @return bool
9342
+ */
9343
+ private static function is_super_admin( $user_id ) {
9344
+ $is_super_admin = false;
9345
+
9346
+ $user = self::_get_user_by_id( $user_id );
9347
+
9348
+ if ( $user instanceof FS_User && ! empty( $user->email ) ) {
9349
+ self::require_pluggable_essentials();
9350
+
9351
+ $wp_user = get_user_by( 'email', $user->email );
9352
+
9353
+ if ( $wp_user instanceof WP_User ) {
9354
+ $super_admins = get_super_admins();
9355
+ $is_super_admin = ( is_array( $super_admins ) && in_array( $wp_user->user_login, $super_admins ) );
9356
+ }
9357
+ }
9358
+
9359
+ return $is_super_admin;
9360
+ }
9361
+
9362
+ #----------------------------------------------------------------------------------
9363
+ #region Plans & Licensing
9364
+ #----------------------------------------------------------------------------------
9365
+
9366
+ /**
9367
+ * Check if running premium plugin code.
9368
+ *
9369
+ * @author Vova Feldman (@svovaf)
9370
+ * @since 1.0.5
9371
+ *
9372
+ * @return bool
9373
+ */
9374
+ function is_premium() {
9375
+ if (! isset($this->_plugin->is_premium)) {
9376
+ return false;
9377
+ }
9378
+
9379
+ return $this->_plugin->is_premium;
9380
+ }
9381
+
9382
+ /**
9383
+ * Get site's plan ID.
9384
+ *
9385
+ * @author Vova Feldman (@svovaf)
9386
+ * @since 1.0.2
9387
+ *
9388
+ * @return number
9389
+ */
9390
+ function get_plan_id() {
9391
+ return $this->_site->plan_id;
9392
+ }
9393
+
9394
+ /**
9395
+ * Get site's plan title.
9396
+ *
9397
+ * @author Vova Feldman (@svovaf)
9398
+ * @since 1.0.2
9399
+ *
9400
+ * @return string
9401
+ */
9402
+ function get_plan_title() {
9403
+ $plan = $this->get_plan();
9404
+
9405
+ return is_object( $plan ) ? $plan->title : 'PLAN_TITLE';
9406
+ }
9407
+
9408
+ /**
9409
+ * Get site's plan name.
9410
+ *
9411
+ * @author Vova Feldman (@svovaf)
9412
+ * @since 2.0.0
9413
+ *
9414
+ * @return string
9415
+ */
9416
+ function get_plan_name() {
9417
+ $plan = $this->get_plan();
9418
+
9419
+ return is_object( $plan ) ? $plan->name : 'PLAN_NAME';
9420
+ }
9421
+
9422
+ /**
9423
+ * @author Vova Feldman (@svovaf)
9424
+ * @since 1.0.9
9425
+ *
9426
+ * @return FS_Plugin_Plan|false
9427
+ */
9428
+ function get_plan() {
9429
+ if ( ! is_object( $this->_site ) ) {
9430
+ return false;
9431
+ }
9432
+
9433
+ return FS_Plugin_Plan::is_valid_id( $this->_site->plan_id ) ?
9434
+ $this->_get_plan_by_id( $this->_site->plan_id ) :
9435
+ false;
9436
+ }
9437
+
9438
+ /**
9439
+ * @author Vova Feldman (@svovaf)
9440
+ * @since 1.0.3
9441
+ *
9442
+ * @return bool
9443
+ */
9444
+ function is_trial() {
9445
+ $this->_logger->entrance();
9446
+
9447
+ if ( ! $this->is_registered() || ! is_object( $this->_site ) ) {
9448
+ return false;
9449
+ }
9450
+
9451
+ return $this->_site->is_trial();
9452
+ }
9453
+
9454
+ /**
9455
+ * Check if currently in a trial with payment method (credit card or paypal).
9456
+ *
9457
+ * @author Vova Feldman (@svovaf)
9458
+ * @since 1.1.7
9459
+ *
9460
+ * @return bool
9461
+ */
9462
+ function is_paid_trial() {
9463
+ $this->_logger->entrance();
9464
+
9465
+ if ( ! $this->is_trial() ) {
9466
+ return false;
9467
+ }
9468
+
9469
+ return $this->has_active_valid_license() && ( $this->_site->trial_plan_id == $this->_license->plan_id );
9470
+ }
9471
+
9472
+ /**
9473
+ * Check if trial already utilized.
9474
+ *
9475
+ * @since 1.0.9
9476
+ *
9477
+ * @return bool
9478
+ */
9479
+ function is_trial_utilized() {
9480
+ $this->_logger->entrance();
9481
+
9482
+ if ( ! $this->is_registered() ) {
9483
+ return false;
9484
+ }
9485
+
9486
+ return $this->_site->is_trial_utilized();
9487
+ }
9488
+
9489
+ /**
9490
+ * Get trial plan information (if in trial).
9491
+ *
9492
+ * @author Vova Feldman (@svovaf)
9493
+ * @since 1.0.9
9494
+ *
9495
+ * @return bool|FS_Plugin_Plan
9496
+ */
9497
+ function get_trial_plan() {
9498
+ $this->_logger->entrance();
9499
+
9500
+ if ( ! $this->is_trial() ) {
9501
+ return false;
9502
+ }
9503
+
9504
+ // Try to load plan from local cache.
9505
+ $trial_plan = $this->_get_plan_by_id( $this->_site->trial_plan_id );
9506
+
9507
+ if ( ! is_object( $trial_plan ) ) {
9508
+ $trial_plan = $this->_fetch_site_plan( $this->_site->trial_plan_id );
9509
+
9510
+ /**
9511
+ * If managed to fetch the plan, add it to the plans collection.
9512
+ */
9513
+ if ( $trial_plan instanceof FS_Plugin_Plan ) {
9514
+ if ( ! is_array( $this->_plans ) ) {
9515
+ $this->_plans = array();
9516
+ }
9517
+
9518
+ $this->_plans[] = $trial_plan;
9519
+ $this->_store_plans();
9520
+ }
9521
+ }
9522
+
9523
+ if ( $trial_plan instanceof FS_Plugin_Plan ) {
9524
+ return $trial_plan;
9525
+ }
9526
+
9527
+ /**
9528
+ * If for some reason failed to get the trial plan, fallback to a dummy name and title.
9529
+ */
9530
+ $trial_plan = new FS_Plugin_Plan();
9531
+ $trial_plan->id = $this->_site->trial_plan_id;
9532
+ $trial_plan->name = 'pro';
9533
+ $trial_plan->title = 'Pro';
9534
+
9535
+ return $trial_plan;
9536
+ }
9537
+
9538
+ /**
9539
+ * Check if the user has an activate, non-expired license on current plugin's install.
9540
+ *
9541
+ * @since 1.0.9
9542
+ *
9543
+ * @return bool
9544
+ */
9545
+ function is_paying() {
9546
+ $this->_logger->entrance();
9547
+
9548
+ if ( ! $this->is_registered() ) {
9549
+ return false;
9550
+ }
9551
+
9552
+ if ( ! $this->has_paid_plan() ) {
9553
+ return false;
9554
+ }
9555
+
9556
+ return (
9557
+ ! $this->is_trial() &&
9558
+ 'free' !== $this->get_plan_name() &&
9559
+ $this->has_active_valid_license()
9560
+ );
9561
+ }
9562
+
9563
+ /**
9564
+ * @author Vova Feldman (@svovaf)
9565
+ * @since 1.0.4
9566
+ *
9567
+ * @return bool
9568
+ */
9569
+ function is_free_plan() {
9570
+ if ( ! $this->is_registered() ) {
9571
+ return true;
9572
+ }
9573
+
9574
+ if ( ! $this->has_paid_plan() ) {
9575
+ return true;
9576
+ }
9577
+
9578
+ return (
9579
+ 'free' === $this->get_plan_name() ||
9580
+ ! $this->has_features_enabled_license()
9581
+ );
9582
+ }
9583
+
9584
+ /**
9585
+ * @author Vova Feldman (@svovaf)
9586
+ * @since 1.0.5
9587
+ *
9588
+ * @return bool
9589
+ */
9590
+ function _has_premium_license() {
9591
+ $this->_logger->entrance();
9592
+
9593
+ $premium_license = $this->_get_available_premium_license();
9594
+
9595
+ return ( false !== $premium_license );
9596
+ }
9597
+
9598
+ /**
9599
+ * Check if user has any licenses associated with the plugin (including expired or blocking).
9600
+ *
9601
+ * @author Vova Feldman (@svovaf)
9602
+ * @since 1.1.7.3
9603
+ *
9604
+ * @return bool
9605
+ */
9606
+ function has_any_license() {
9607
+ return is_array( $this->_licenses ) && ( 0 < count( $this->_licenses ) );
9608
+ }
9609
+
9610
+ /**
9611
+ * @author Vova Feldman (@svovaf)
9612
+ * @since 1.0.5
9613
+ *
9614
+ * @param bool|null $is_localhost
9615
+ *
9616
+ * @return FS_Plugin_License|false
9617
+ */
9618
+ function _get_available_premium_license( $is_localhost = null ) {
9619
+ $this->_logger->entrance();
9620
+
9621
+ $licenses = $this->get_available_premium_licenses( $is_localhost );
9622
+ if ( ! empty( $licenses ) ) {
9623
+ return $licenses[0];
9624
+ }
9625
+
9626
+ return false;
9627
+ }
9628
+
9629
+ /**
9630
+ * @author Vova Feldman (@svovaf)
9631
+ * @since 1.0.5
9632
+ *
9633
+ * @param bool|null $is_localhost
9634
+ *
9635
+ * @return FS_Plugin_License[]
9636
+ */
9637
+ function get_available_premium_licenses( $is_localhost = null ) {
9638
+ $this->_logger->entrance();
9639
+
9640
+ $licenses = array();
9641
+ if ( ! $this->has_paid_plan() ) {
9642
+ return $licenses;
9643
+ }
9644
+
9645
+ if ( is_array( $this->_licenses ) ) {
9646
+ foreach ( $this->_licenses as $license ) {
9647
+ if ( ! $license->can_activate( $is_localhost ) ) {
9648
+ continue;
9649
+ }
9650
+
9651
+ $licenses[] = $license;
9652
+ }
9653
+ }
9654
+
9655
+ return $licenses;
9656
+ }
9657
+
9658
+ /**
9659
+ * Sync local plugin plans with remote server.
9660
+ *
9661
+ * 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).
9662
+ *
9663
+ * @author Vova Feldman (@svovaf)
9664
+ * @since 1.0.5
9665
+ *
9666
+ * @return FS_Plugin_Plan[]|object
9667
+ */
9668
+ function _sync_plans() {
9669
+ $plans = $this->_fetch_plugin_plans();
9670
+
9671
+ if ( $this->is_array_instanceof( $plans, 'FS_Plugin_Plan' ) ) {
9672
+ $plans_map = array();
9673
+ foreach ( $plans as $plan ) {
9674
+ $plans_map[ $plan->id ] = true;
9675
+ }
9676
+
9677
+ $plans_ids_to_keep = $this->get_plans_ids_associated_with_installs();
9678
+
9679
+ foreach ( $plans_ids_to_keep as $plan_id ) {
9680
+ if ( isset( $plans_map[ $plan_id ] ) ) {
9681
+ continue;
9682
+ }
9683
+
9684
+ $missing_plan = self::_get_plan_by_id( $plan_id );
9685
+
9686
+ if ( is_object( $missing_plan ) ) {
9687
+ $plans[] = $missing_plan;
9688
+ }
9689
+ }
9690
+
9691
+ $this->_plans = $plans;
9692
+ $this->_store_plans();
9693
+ }
9694
+
9695
+ $this->do_action( 'after_plans_sync', $plans );
9696
+
9697
+ return $this->_plans;
9698
+ }
9699
+
9700
+ /**
9701
+ * Check if specified plan exists locally. If not, fetch it and store it.
9702
+ *
9703
+ * @author Vova Feldman (@svovaf)
9704
+ * @since 2.0.0
9705
+ *
9706
+ * @param number $plan_id
9707
+ *
9708
+ * @return \FS_Plugin_Plan|object The plan entity or the API error object on failure.
9709
+ */
9710
+ private function sync_plan_if_not_exist( $plan_id ) {
9711
+ $plan = self::_get_plan_by_id( $plan_id );
9712
+
9713
+ if ( is_object( $plan ) ) {
9714
+ // Plan already exists.
9715
+ return $plan;
9716
+ }
9717
+
9718
+ $plan = $this->fetch_plan_by_id( $plan_id );
9719
+
9720
+ if ( $plan instanceof FS_Plugin_Plan ) {
9721
+ $this->_plans[] = $plan;
9722
+ $this->_store_plans();
9723
+
9724
+ return $plan;
9725
+ }
9726
+
9727
+ return $plan;
9728
+ }
9729
+
9730
+ /**
9731
+ * Check if specified license exists locally. If not, fetch it and store it.
9732
+ *
9733
+ * @author Vova Feldman (@svovaf)
9734
+ * @since 2.0.0
9735
+ *
9736
+ * @param number $license_id
9737
+ * @param string $license_key
9738
+ *
9739
+ * @return \FS_Plugin_Plan|object The plan entity or the API error object on failure.
9740
+ */
9741
+ private function sync_license_if_not_exist( $license_id, $license_key ) {
9742
+ $license = $this->_get_license_by_id( $license_id );
9743
+
9744
+ if ( is_object( $license ) ) {
9745
+ // License already exists.
9746
+ return $license;
9747
+ }
9748
+
9749
+ $license = $this->fetch_license_by_key( $license_id, $license_key );
9750
+
9751
+ if ( $license instanceof FS_Plugin_License ) {
9752
+ $this->_licenses[] = $license;
9753
+ $this->_license = $license;
9754
+ $this->_store_licenses();
9755
+
9756
+ return $license;
9757
+ }
9758
+
9759
+ return $license;
9760
+ }
9761
+
9762
+ /**
9763
+ * Get a collection of unique plan IDs that are associated with any installs in the network.
9764
+ *
9765
+ * @author Leo Fajardo (@leorw)
9766
+ * @since 2.0.0
9767
+ *
9768
+ * @return number[]
9769
+ */
9770
+ private function get_plans_ids_associated_with_installs() {
9771
+ if ( ! $this->_is_network_active ) {
9772
+ if ( ! is_object( $this->_site ) ||
9773
+ ! FS_Plugin_Plan::is_valid_id( $this->_site->plan_id )
9774
+ ) {
9775
+ return array();
9776
+ }
9777
+
9778
+ return array( $this->_site->plan_id );
9779
+ }
9780
+
9781
+ $plan_ids = array();
9782
+ $sites = self::get_sites();
9783
+ foreach ( $sites as $site ) {
9784
+ $blog_id = self::get_site_blog_id( $site );
9785
+ $install = $this->get_install_by_blog_id( $blog_id );
9786
+
9787
+ if ( ! is_object( $install ) ||
9788
+ ! FS_Plugin_Plan::is_valid_id( $install->plan_id )
9789
+ ) {
9790
+ continue;
9791
+ }
9792
+
9793
+ $plan_ids[ $install->plan_id ] = true;
9794
+ }
9795
+
9796
+ return array_keys( $plan_ids );
9797
+ }
9798
+
9799
+ /**
9800
+ * Get a collection of unique license IDs that are associated with any installs in the network.
9801
+ *
9802
+ * @author Leo Fajardo (@leorw)
9803
+ * @since 2.0.0
9804
+ *
9805
+ * @return number[]
9806
+ */
9807
+ private function get_license_ids_associated_with_installs() {
9808
+ if ( ! $this->_is_network_active ) {
9809
+ if ( ! is_object( $this->_site ) ||
9810
+ ! FS_Plugin_License::is_valid_id( $this->_site->license_id )
9811
+ ) {
9812
+ return array();
9813
+ }
9814
+
9815
+ return array( $this->_site->license_id );
9816
+ }
9817
+
9818
+ $license_ids = array();
9819
+ $sites = self::get_sites();
9820
+ foreach ( $sites as $site ) {
9821
+ $blog_id = self::get_site_blog_id( $site );
9822
+ $install = $this->get_install_by_blog_id( $blog_id );
9823
+
9824
+ if ( ! is_object( $install ) ||
9825
+ ! FS_Plugin_License::is_valid_id( $install->license_id )
9826
+ ) {
9827
+ continue;
9828
+ }
9829
+
9830
+ $license_ids[ $install->license_id ] = true;
9831
+ }
9832
+
9833
+ return array_keys( $license_ids );
9834
+ }
9835
+
9836
+ /**
9837
+ * @author Vova Feldman (@svovaf)
9838
+ * @since 1.0.5
9839
+ *
9840
+ * @param number $id
9841
+ *
9842
+ * @return FS_Plugin_Plan|false
9843
+ */
9844
+ function _get_plan_by_id( $id ) {
9845
+ $this->_logger->entrance();
9846
+
9847
+ if ( ! is_array( $this->_plans ) || 0 === count( $this->_plans ) ) {
9848
+ $this->_sync_plans();
9849
+ }
9850
+
9851
+ foreach ( $this->_plans as $plan ) {
9852
+ if ( $id == $plan->id ) {
9853
+ return $plan;
9854
+ }
9855
+ }
9856
+
9857
+ return false;
9858
+ }
9859
+
9860
+ /**
9861
+ * @author Vova Feldman (@svovaf)
9862
+ * @since 1.1.8.1
9863
+ *
9864
+ * @param string $name
9865
+ *
9866
+ * @return FS_Plugin_Plan|false
9867
+ */
9868
+ private function get_plan_by_name( $name ) {
9869
+ $this->_logger->entrance();
9870
+
9871
+ if ( ! is_array( $this->_plans ) || 0 === count( $this->_plans ) ) {
9872
+ $this->_sync_plans();
9873
+ }
9874
+
9875
+ foreach ( $this->_plans as $plan ) {
9876
+ if ( $name == $plan->name ) {
9877
+ return $plan;
9878
+ }
9879
+ }
9880
+
9881
+ return false;
9882
+ }
9883
+
9884
+ /**
9885
+ * Sync local licenses with remote server.
9886
+ *
9887
+ * @author Vova Feldman (@svovaf)
9888
+ * @since 1.0.6
9889
+ *
9890
+ * @param number|bool $site_license_id
9891
+ * @param number|null $blog_id
9892
+ *
9893
+ * @return FS_Plugin_License[]|object
9894
+ */
9895
+ function _sync_licenses( $site_license_id = false, $blog_id = null ) {
9896
+ $this->_logger->entrance();
9897
+
9898
+ $is_network_admin = fs_is_network_admin();
9899
+
9900
+ if ( $is_network_admin && is_null( $blog_id ) ) {
9901
+ $all_licenses = self::get_all_licenses( $this->_module_id );
9902
+ } else {
9903
+ $all_licenses = $this->get_user_licenses( $this->_user->id );
9904
+ }
9905
+
9906
+ $foreign_licenses = array(
9907
+ 'ids' => array(),
9908
+ 'license_keys' => array()
9909
+ );
9910
+
9911
+ $all_licenses_map = array();
9912
+ foreach ( $all_licenses as $license ) {
9913
+ $all_licenses_map[ $license->id ] = true;
9914
+ if ( $license->user_id == $this->_user->id || $license->id == $site_license_id ) {
9915
+ continue;
9916
+ }
9917
+
9918
+ $foreign_licenses['ids'][] = $license->id;
9919
+ $foreign_licenses['license_keys'][] = $license->secret_key;
9920
+ }
9921
+
9922
+ if ( empty( $foreign_licenses['ids'] ) ) {
9923
+ $foreign_licenses = array();
9924
+ }
9925
+
9926
+ $licenses = $this->_fetch_licenses( false, $site_license_id, $foreign_licenses, $blog_id );
9927
+
9928
+ if ( $this->is_array_instanceof( $licenses, 'FS_Plugin_License' ) ) {
9929
+ $licenses_map = array();
9930
+ foreach ( $licenses as $license ) {
9931
+ $licenses_map[ $license->id ] = true;
9932
+ }
9933
+
9934
+ // $license_ids_to_keep = $this->get_license_ids_associated_with_installs();
9935
+ // foreach ( $license_ids_to_keep as $license_id ) {
9936
+ // if ( isset( $licenses_map[ $license_id ] ) ) {
9937
+ // continue;
9938
+ // }
9939
+ //
9940
+ // $missing_license = self::_get_license_by_id( $license_id, false );
9941
+ // if ( is_object( $missing_license ) ) {
9942
+ // $licenses[] = $missing_license;
9943
+ // $licenses_map[ $missing_license->id ] = true;
9944
+ // }
9945
+ // }
9946
+
9947
+ $user_license_ids = $this->get_user_linked_license_ids( $this->_user->id );
9948
+
9949
+ foreach ( $user_license_ids as $key => $license_id ) {
9950
+ if ( ! isset( $licenses_map[ $license_id ] ) ) {
9951
+ // Remove access to licenses that no longer exist.
9952
+ unset( $user_license_ids[ $key ] );
9953
+ }
9954
+ }
9955
+
9956
+ if ( ! empty( $user_license_ids ) ) {
9957
+ foreach ( $licenses_map as $license_id => $value ) {
9958
+ if ( ! isset( $all_licenses_map[ $license_id ] ) ) {
9959
+ // Associate new licenses with the user who triggered the license syncing.
9960
+ $user_license_ids[] = $license_id;
9961
+ }
9962
+ }
9963
+
9964
+ $user_license_ids = array_unique( $user_license_ids );
9965
+ } else {
9966
+ $user_license_ids = array_keys( $licenses_map );
9967
+ }
9968
+
9969
+ if ( ! $is_network_admin || ! is_null( $blog_id ) ) {
9970
+ $user_licenses = array();
9971
+ foreach ( $licenses as $license ) {
9972
+ if ( ! in_array( $license->id, $user_license_ids ) ) {
9973
+ continue;
9974
+ }
9975
+
9976
+ $user_licenses[] = $license;
9977
+ }
9978
+
9979
+ $this->_licenses = $user_licenses;
9980
+ } else {
9981
+ $this->_licenses = $licenses;
9982
+ }
9983
+
9984
+ $this->set_user_linked_license_ids( $this->_user->id, $user_license_ids );
9985
+
9986
+ $this->_store_licenses( true, $this->_module_id, $licenses );
9987
+ }
9988
+
9989
+ // Update current license.
9990
+ if ( is_object( $this->_license ) ) {
9991
+ $this->_license = $this->_get_license_by_id( $this->_license->id );
9992
+ }
9993
+
9994
+ return $this->_licenses;
9995
+ }
9996
+
9997
+ /**
9998
+ * @author Vova Feldman (@svovaf)
9999
+ * @since 1.0.5
10000
+ *
10001
+ * @param number $id
10002
+ * @param bool $sync_licenses
10003
+ *
10004
+ * @return FS_Plugin_License|false
10005
+ */
10006
+ function _get_license_by_id( $id, $sync_licenses = true ) {
10007
+ $this->_logger->entrance();
10008
+
10009
+ if ( ! FS_Plugin_License::is_valid_id( $id ) ) {
10010
+ return false;
10011
+ }
10012
+
10013
+ /**
10014
+ * When running from the network level admin and opted-in from the network,
10015
+ * check if the license exists in the network user licenses collection.
10016
+ *
10017
+ * @author Vova Feldman (@svovaf)
10018
+ * @since 2.0.0
10019
+ */
10020
+ if ( fs_is_network_admin() &&
10021
+ $this->is_network_registered() &&
10022
+ ( ! is_object( $this->_user ) || $this->_storage->network_user_id != $this->_user->id )
10023
+ ) {
10024
+ $licenses = $this->get_user_licenses( $this->_storage->network_user_id );
10025
+
10026
+ foreach ( $licenses as $license ) {
10027
+ if ( $id == $license->id ) {
10028
+ return $license;
10029
+ }
10030
+ }
10031
+ }
10032
+
10033
+ if ( ! $this->has_any_license() && $sync_licenses ) {
10034
+ $this->_sync_licenses( $id );
10035
+ }
10036
+
10037
+ if ( is_array( $this->_licenses ) ) {
10038
+ foreach ( $this->_licenses as $license ) {
10039
+ if ( $id == $license->id ) {
10040
+ return $license;
10041
+ }
10042
+ }
10043
+ }
10044
+
10045
+ return false;
10046
+ }
10047
+
10048
+ /**
10049
+ * 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.
10050
+ *
10051
+ * @author Vova Feldman (@svovaf)
10052
+ * @since 2.0.0
10053
+ *
10054
+ * @param number $id
10055
+ *
10056
+ * @return FS_Plugin_License
10057
+ */
10058
+ private function get_license_by_id( $id ) {
10059
+ $licenses = self::get_all_licenses( $this->_module_id );
10060
+
10061
+ if ( is_array( $licenses ) && ! empty( $licenses ) ) {
10062
+ foreach ( $licenses as $license ) {
10063
+ if ( $id == $license->id ) {
10064
+ return $license;
10065
+ }
10066
+ }
10067
+ }
10068
+
10069
+ return null;
10070
+ }
10071
+
10072
+ /**
10073
+ * Synchronize the site's context license by fetching the license form the API and updating the local data with it.
10074
+ *
10075
+ * @author Vova Feldman (@svovaf)
10076
+ * @since 2.0.0
10077
+ *
10078
+ * @return \FS_Plugin_License|mixed
10079
+ */
10080
+ private function sync_site_license() {
10081
+ $api = $this->get_api_user_scope();
10082
+
10083
+ $result = $api->get( "/licenses/{$this->_license->id}.json?license_key=" . urlencode( $this->_license->secret_key ), true );
10084
+
10085
+ if ( ! $this->is_api_result_entity( $result ) ) {
10086
+ return $result;
10087
+ }
10088
+
10089
+ $license = $this->_update_site_license( new FS_Plugin_License( $result ) );
10090
+ $this->_store_licenses();
10091
+
10092
+ return $license;
10093
+ }
10094
+
10095
+ /**
10096
+ * Get all user's available licenses for the current module.
10097
+ *
10098
+ * @author Vova Feldman (@svovaf)
10099
+ * @since 2.0.0
10100
+ *
10101
+ * @param number $user_id
10102
+ *
10103
+ * @return FS_Plugin_License[]
10104
+ */
10105
+ private function get_user_licenses( $user_id ) {
10106
+ $all_licenses = self::get_all_licenses( $this->_module_id );
10107
+ if ( empty( $all_licenses ) ) {
10108
+ return array();
10109
+ }
10110
+
10111
+ $user_license_ids = $this->get_user_linked_license_ids( $user_id );
10112
+ if ( empty( $user_license_ids ) ) {
10113
+ return array();
10114
+ }
10115
+
10116
+ $licenses = array();
10117
+ foreach ( $all_licenses as $license ) {
10118
+ if ( in_array( $license->id, $user_license_ids ) ) {
10119
+ $licenses[] = $license;
10120
+ }
10121
+ }
10122
+
10123
+ return $licenses;
10124
+ }
10125
+
10126
+ /**
10127
+ * Checks if the context license is network activated except on the given blog ID.
10128
+ *
10129
+ * @author Vova Feldman (@svovaf)
10130
+ * @since 2.0.0
10131
+ *
10132
+ * @param int $except_blog_id
10133
+ *
10134
+ * @return bool
10135
+ */
10136
+ private function is_license_network_active( $except_blog_id = 0 ) {
10137
+ $this->_logger->entrance();
10138
+
10139
+ if ( ! is_object( $this->_license ) ) {
10140
+ return false;
10141
+ }
10142
+
10143
+ $sites = self::get_sites();
10144
+
10145
+ if ( $this->_license->total_activations() < ( count( $sites ) - 1 ) ) {
10146
+ // There are more sites than the number of activations, so license cannot be network activated.
10147
+ return false;
10148
+ }
10149
+
10150
+ foreach ( $sites as $site ) {
10151
+ $blog_id = self::get_site_blog_id( $site );
10152
+
10153
+ if ( $except_blog_id == $blog_id ) {
10154
+ // Skip excluded blog.
10155
+ continue;
10156
+ }
10157
+
10158
+ $install = $this->get_install_by_blog_id( $blog_id );
10159
+
10160
+ if ( is_object( $install ) && $install->license_id != $this->_license->id ) {
10161
+ return false;
10162
+ }
10163
+ }
10164
+
10165
+ return true;
10166
+ }
10167
+
10168
+ /**
10169
+ * 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.
10170
+ *
10171
+ * Notice: On success, this method will also update the license activations counters (without updating the license in the storage).
10172
+ *
10173
+ * @author Vova Feldman (@svovaf)
10174
+ * @since 2.0.0
10175
+ *
10176
+ * @param \FS_User $user
10177
+ * @param \FS_Plugin_License $license
10178
+ *
10179
+ * @return bool
10180
+ */
10181
+ private function try_activate_license_on_network( FS_User $user, FS_Plugin_License $license ) {
10182
+ $this->_logger->entrance();
10183
+
10184
+ $result = $this->can_activate_license_on_network( $license );
10185
+
10186
+ if ( false === $result ) {
10187
+ return false;
10188
+ }
10189
+
10190
+ $installs_without_license = $result['installs'];
10191
+ if ( ! empty( $installs_without_license ) ) {
10192
+ $this->activate_license_on_many_installs( $user, $license->secret_key, $installs_without_license );
10193
+ }
10194
+
10195
+ $disconnected_site_ids = $result['sites'];
10196
+ if ( ! empty( $disconnected_site_ids ) ) {
10197
+ $this->activate_license_on_many_sites( $user, $license->secret_key, $disconnected_site_ids );
10198
+ }
10199
+
10200
+ $this->link_license_2_user( $license->id, $user->id );
10201
+
10202
+ // Sync license after activations.
10203
+ $license->activated += $result['production_count'];
10204
+ $license->activated_local += $result['localhost_count'];
10205
+
10206
+ // $this->_store_licenses()
10207
+
10208
+ return true;
10209
+ }
10210
+
10211
+ /**
10212
+ * Checks if the given license can be activated on the whole network.
10213
+ *
10214
+ * @author Vova Feldman (@svovaf)
10215
+ * @since 2.0.0
10216
+ *
10217
+ * @param \FS_Plugin_License $license
10218
+ *
10219
+ * @return false|array {
10220
+ * @type array[int]FS_Site $installs Blog ID to install map.
10221
+ * @type int[] $sites Non-connected blog IDs.
10222
+ * @type int $production_count Production sites count.
10223
+ * @type int $localhost_count Production sites count.
10224
+ * }
10225
+ */
10226
+ private function can_activate_license_on_network( FS_Plugin_License $license ) {
10227
+ $sites = self::get_sites();
10228
+
10229
+ $production_count = 0;
10230
+ $localhost_count = 0;
10231
+
10232
+ $installs_without_license = array();
10233
+ $disconnected_site_ids = array();
10234
+
10235
+ foreach ( $sites as $site ) {
10236
+ $blog_id = self::get_site_blog_id( $site );
10237
+ $install = $this->get_install_by_blog_id( $blog_id );
10238
+
10239
+ if ( is_object( $install ) ) {
10240
+ if ( FS_Plugin_License::is_valid_id( $install->license_id ) ) {
10241
+ // License already activated on the install.
10242
+ continue;
10243
+ }
10244
+
10245
+ $url = $install->url;
10246
+
10247
+ $installs_without_license[ $blog_id ] = $install;
10248
+ } else {
10249
+ $url = is_object( $site ) ?
10250
+ $site->siteurl :
10251
+ get_site_url( $blog_id );
10252
+
10253
+ $disconnected_site_ids[] = $blog_id;
10254
+ }
10255
+
10256
+ if ( FS_Site::is_localhost_by_address( $url ) ) {
10257
+ $localhost_count ++;
10258
+ } else {
10259
+ $production_count ++;
10260
+ }
10261
+ }
10262
+
10263
+ if ( ! $license->can_activate_bulk( $production_count, $localhost_count ) ) {
10264
+ return false;
10265
+ }
10266
+
10267
+ return array(
10268
+ 'installs' => $installs_without_license,
10269
+ 'sites' => $disconnected_site_ids,
10270
+ 'production_count' => $production_count,
10271
+ 'localhost_count' => $localhost_count,
10272
+ );
10273
+ }
10274
+
10275
+ /**
10276
+ * Activate a given license on a collection of installs.
10277
+ *
10278
+ * @author Vova Feldman (@svovaf)
10279
+ * @since 2.0.0
10280
+ *
10281
+ * @param \FS_User $user
10282
+ * @param string $license_key
10283
+ * @param array $blog_2_install_map {
10284
+ * @key int Blog ID.
10285
+ * @value FS_Site Blog's associated install.
10286
+ * }
10287
+ *
10288
+ * @return mixed|true
10289
+ */
10290
+ private function activate_license_on_many_installs(
10291
+ FS_User $user,
10292
+ $license_key,
10293
+ array $blog_2_install_map
10294
+ ) {
10295
+ $params = array(
10296
+ array( 'license_key' => $this->apply_filters( 'license_key', $license_key ) )
10297
+ );
10298
+
10299
+ $install_2_blog_map = array();
10300
+ foreach ( $blog_2_install_map as $blog_id => $install ) {
10301
+ $params[] = array( 'id' => $install->id );
10302
+
10303
+ $install_2_blog_map[ $install->id ] = $blog_id;
10304
+ }
10305
+
10306
+ $result = $this->get_api_user_scope_by_user( $user )->call(
10307
+ "plugins/{$this->_plugin->id}/installs.json",
10308
+ 'PUT',
10309
+ $params
10310
+ );
10311
+
10312
+ if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
10313
+ return $result;
10314
+ }
10315
+
10316
+ foreach ( $result->installs as $r_install ) {
10317
+ $install = new FS_Site( $r_install );
10318
+ $install->is_disconnected = false;
10319
+
10320
+ // Update install.
10321
+ $this->_store_site(
10322
+ true,
10323
+ $install_2_blog_map[ $r_install->id ],
10324
+ $install
10325
+ );
10326
+ }
10327
+
10328
+ return true;
10329
+ }
10330
+
10331
+ /**
10332
+ * Activate a given license on a collection of blogs/sites that are not yet opted-in.
10333
+ *
10334
+ * @author Vova Feldman (@svovaf)
10335
+ * @since 2.0.0
10336
+ *
10337
+ * @param \FS_User $user
10338
+ * @param string $license_key
10339
+ * @param int[] $site_ids
10340
+ *
10341
+ * @return true|mixed True if successful, otherwise, the API result.
10342
+ */
10343
+ private function activate_license_on_many_sites(
10344
+ FS_User $user,
10345
+ $license_key,
10346
+ array $site_ids
10347
+ ) {
10348
+ $sites = array();
10349
+ foreach ( $site_ids as $site_id ) {
10350
+ $sites[] = $this->get_site_info( array( 'blog_id' => $site_id ) );
10351
+ }
10352
+
10353
+ // Install the plugin.
10354
+ $result = $this->create_installs_with_user(
10355
+ $user,
10356
+ $license_key,
10357
+ false,
10358
+ $sites,
10359
+ false,
10360
+ true
10361
+ );
10362
+
10363
+ if ( ! $this->is_api_result_entity( $result ) &&
10364
+ ! $this->is_api_result_object( $result, 'installs' )
10365
+ ) {
10366
+ return $result;
10367
+ }
10368
+
10369
+ $installs = array();
10370
+ foreach ( $result->installs as $install ) {
10371
+ $installs[] = new FS_Site( $install );
10372
+ }
10373
+
10374
+ // Map site addresses to their blog IDs.
10375
+ $address_to_blog_map = $this->get_address_to_blog_map();
10376
+
10377
+ $first_blog_id = null;
10378
+
10379
+ foreach ( $installs as $install ) {
10380
+ $address = trailingslashit( fs_strip_url_protocol( $install->url ) );
10381
+ $blog_id = $address_to_blog_map[ $address ];
10382
+
10383
+ $this->_store_site( true, $blog_id, $install );
10384
+
10385
+ $this->reset_anonymous_mode( $blog_id );
10386
+
10387
+ if ( is_null( $first_blog_id ) ) {
10388
+ $first_blog_id = $blog_id;
10389
+ }
10390
+ }
10391
+
10392
+ if ( ! FS_Site::is_valid_id( $this->_storage->network_install_blog_id ) ) {
10393
+ $this->_storage->network_install_blog_id = $first_blog_id;
10394
+ }
10395
+
10396
+ return true;
10397
+ }
10398
+
10399
+ /**
10400
+ * Sync site's license with user licenses.
10401
+ *
10402
+ * @author Vova Feldman (@svovaf)
10403
+ * @since 1.0.6
10404
+ *
10405
+ * @param FS_Plugin_License|null $new_license
10406
+ *
10407
+ * @return FS_Plugin_License|null
10408
+ */
10409
+ function _update_site_license( $new_license ) {
10410
+ $this->_logger->entrance();
10411
+
10412
+ $this->_license = $new_license;
10413
+
10414
+ if ( ! is_object( $new_license ) ) {
10415
+ $this->_site->license_id = null;
10416
+ $this->_sync_site_subscription( null );
10417
+
10418
+ return $this->_license;
10419
+ }
10420
+
10421
+ $this->_site->license_id = $this->_license->id;
10422
+
10423
+ if ( ! is_array( $this->_licenses ) ) {
10424
+ $this->_licenses = array();
10425
+ }
10426
+
10427
+ $is_license_found = false;
10428
+ for ( $i = 0, $len = count( $this->_licenses ); $i < $len; $i ++ ) {
10429
+ if ( $new_license->id == $this->_licenses[ $i ]->id ) {
10430
+ $this->_licenses[ $i ] = $new_license;
10431
+
10432
+ $is_license_found = true;
10433
+ break;
10434
+ }
10435
+ }
10436
+
10437
+ // If new license just append.
10438
+ if ( ! $is_license_found ) {
10439
+ $this->_licenses[] = $new_license;
10440
+ }
10441
+
10442
+ $this->_sync_site_subscription( $new_license );
10443
+
10444
+ return $this->_license;
10445
+ }
10446
+
10447
+ /**
10448
+ * Sync site's subscription.
10449
+ *
10450
+ * @author Vova Feldman (@svovaf)
10451
+ * @since 1.0.9
10452
+ *
10453
+ * @param FS_Plugin_License|null $license
10454
+ *
10455
+ * @return bool|\FS_Subscription
10456
+ */
10457
+ private function _sync_site_subscription( $license ) {
10458
+ if ( ! is_object( $license ) ) {
10459
+ $this->delete_unused_subscriptions();
10460
+
10461
+ return false;
10462
+ }
10463
+
10464
+ // Load subscription details if not lifetime.
10465
+ $subscription = $license->is_lifetime() ?
10466
+ false :
10467
+ $this->_fetch_site_license_subscription();
10468
+
10469
+ if ( is_object( $subscription ) && ! isset( $subscription->error ) ) {
10470
+ $this->store_subscription( $subscription );
10471
+ } else {
10472
+ $this->delete_unused_subscriptions();
10473
+ }
10474
+
10475
+ return $subscription;
10476
+ }
10477
+
10478
+ /**
10479
+ * @author Vova Feldman (@svovaf)
10480
+ * @since 1.0.6
10481
+ *
10482
+ * @return bool|\FS_Plugin_License
10483
+ */
10484
+ function _get_license() {
10485
+ if ( ! fs_is_network_admin() || is_object( $this->_license ) ) {
10486
+ return $this->_license;
10487
+ }
10488
+
10489
+ return $this->_get_available_premium_license();
10490
+ }
10491
+
10492
+ /**
10493
+ * @param number $license_id
10494
+ *
10495
+ * @return null|\FS_Subscription
10496
+ */
10497
+ function _get_subscription( $license_id ) {
10498
+ if ( ! isset( $this->_storage->subscriptions ) ||
10499
+ empty( $this->_storage->subscriptions )
10500
+ ) {
10501
+ return null;
10502
+ }
10503
+
10504
+ foreach ( $this->_storage->subscriptions as $subscription ) {
10505
+ if ( $subscription->license_id == $license_id ) {
10506
+ return $subscription;
10507
+ }
10508
+ }
10509
+
10510
+ return null;
10511
+ }
10512
+
10513
+ /**
10514
+ * @author Leo Fajardo (@leorw)
10515
+ * @since 2.0.0
10516
+ *
10517
+ * @param FS_Subscription $subscription
10518
+ */
10519
+ function store_subscription( FS_Subscription $subscription ) {
10520
+ if ( ! isset( $this->_storage->subscriptions ) ) {
10521
+ $this->_storage->subscriptions = array();
10522
+ }
10523
+
10524
+ if ( empty( $this->_storage->subscriptions ) || ! is_multisite() ) {
10525
+ $this->_storage->subscriptions = array( $subscription );
10526
+
10527
+ return;
10528
+ }
10529
+
10530
+ $subscriptions = $this->_storage->subscriptions;
10531
+
10532
+ $updated_subscription = false;
10533
+ foreach ( $subscriptions as $key => $existing_subscription ) {
10534
+ if ( $existing_subscription->id == $subscription->id ) {
10535
+ $subscriptions[ $key ] = $subscription;
10536
+ $updated_subscription = true;
10537
+ break;
10538
+ }
10539
+ }
10540
+
10541
+ if ( ! $updated_subscription ) {
10542
+ $subscriptions[] = $subscription;
10543
+ }
10544
+
10545
+ $this->_storage->subscriptions = $subscriptions;
10546
+ }
10547
+
10548
+ /**
10549
+ * @author Leo Fajardo (@leorw)
10550
+ * @since 2.0.0
10551
+ */
10552
+ function delete_unused_subscriptions() {
10553
+ if ( ! isset( $this->_storage->subscriptions ) ||
10554
+ empty( $this->_storage->subscriptions ) ||
10555
+ // Clean up only if there are already at least 3 subscriptions.
10556
+ ( count( $this->_storage->subscriptions ) < 3 )
10557
+ ) {
10558
+ return;
10559
+ }
10560
+
10561
+ if ( ! is_multisite() ) {
10562
+ // If not multisite, there should only be 1 subscription, so just clear the array.
10563
+ $this->_storage->subscriptions = array();
10564
+
10565
+ return;
10566
+ }
10567
+
10568
+ $subscriptions_to_keep_by_license_id_map = array();
10569
+ $sites = self::get_sites();
10570
+ foreach ( $sites as $site ) {
10571
+ $blog_id = self::get_site_blog_id( $site );
10572
+ $install = $this->get_install_by_blog_id( $blog_id );
10573
+
10574
+ if ( ! is_object( $install ) ||
10575
+ ! FS_Plugin_License::is_valid_id( $install->license_id )
10576
+ ) {
10577
+ continue;
10578
+ }
10579
+
10580
+ $subscriptions_to_keep_by_license_id_map[ $install->license_id ] = true;
10581
+ }
10582
+
10583
+ if ( empty( $subscriptions_to_keep_by_license_id_map ) ) {
10584
+ $this->_storage->subscriptions = array();
10585
+
10586
+ return;
10587
+ }
10588
+
10589
+ foreach ( $this->_storage->subscriptions as $key => $subscription ) {
10590
+ if ( ! isset( $subscriptions_to_keep_by_license_id_map[ $subscription->license_id ] ) ) {
10591
+ unset( $this->_storage->subscriptions[ $key ] );
10592
+ }
10593
+ }
10594
+ }
10595
+
10596
+ /**
10597
+ * @author Vova Feldman (@svovaf)
10598
+ * @since 1.0.2
10599
+ *
10600
+ * @param string $plan Plan name
10601
+ * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
10602
+ *
10603
+ * @return bool
10604
+ */
10605
+ function is_plan( $plan, $exact = false ) {
10606
+ $this->_logger->entrance();
10607
+
10608
+ if ( ! $this->is_registered() ) {
10609
+ return false;
10610
+ }
10611
+
10612
+ $plan = strtolower( $plan );
10613
+
10614
+ $current_plan_name = $this->get_plan_name();
10615
+
10616
+ if ( $current_plan_name === $plan ) {
10617
+ // Exact plan.
10618
+ return true;
10619
+ } else if ( $exact ) {
10620
+ // Required exact, but plans are different.
10621
+ return false;
10622
+ }
10623
+
10624
+ $current_plan_order = - 1;
10625
+ $required_plan_order = - 1;
10626
+ for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
10627
+ if ( $plan === $this->_plans[ $i ]->name ) {
10628
+ $required_plan_order = $i;
10629
+ } else if ( $current_plan_name === $this->_plans[ $i ]->name ) {
10630
+ $current_plan_order = $i;
10631
+ }
10632
+ }
10633
+
10634
+ return ( $current_plan_order > $required_plan_order );
10635
+ }
10636
+
10637
+ /**
10638
+ * Check if module has only one plan.
10639
+ *
10640
+ * @author Vova Feldman (@svovaf)
10641
+ * @since 1.2.1.7
10642
+ *
10643
+ * @return bool
10644
+ */
10645
+ function is_single_plan() {
10646
+ $this->_logger->entrance();
10647
+
10648
+ if ( ! $this->is_registered() ||
10649
+ ! is_array( $this->_plans ) ||
10650
+ 0 === count( $this->_plans )
10651
+ ) {
10652
+ return true;
10653
+ }
10654
+
10655
+ return ( 1 === count( $this->_plans ) );
10656
+ }
10657
+
10658
+ /**
10659
+ * Check if plan based on trial. If not in trial mode, should return false.
10660
+ *
10661
+ * @since 1.0.9
10662
+ *
10663
+ * @param string $plan Plan name
10664
+ * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
10665
+ *
10666
+ * @return bool
10667
+ */
10668
+ function is_trial_plan( $plan, $exact = false ) {
10669
+ $this->_logger->entrance();
10670
+
10671
+ if ( ! $this->is_registered() ) {
10672
+ return false;
10673
+ }
10674
+
10675
+ if ( ! $this->is_trial() ) {
10676
+ return false;
10677
+ }
10678
+
10679
+ $trial_plan = $this->get_trial_plan();
10680
+
10681
+ if ( $trial_plan->name === $plan ) {
10682
+ // Exact plan.
10683
+ return true;
10684
+ } else if ( $exact ) {
10685
+ // Required exact, but plans are different.
10686
+ return false;
10687
+ }
10688
+
10689
+ $current_plan_order = - 1;
10690
+ $required_plan_order = - 1;
10691
+ for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
10692
+ if ( $plan === $this->_plans[ $i ]->name ) {
10693
+ $required_plan_order = $i;
10694
+ } else if ( $trial_plan->name === $this->_plans[ $i ]->name ) {
10695
+ $current_plan_order = $i;
10696
+ }
10697
+ }
10698
+
10699
+ return ( $current_plan_order > $required_plan_order );
10700
+ }
10701
+
10702
+ /**
10703
+ * Check if plugin has any paid plans.
10704
+ *
10705
+ * @author Vova Feldman (@svovaf)
10706
+ * @since 1.0.7
10707
+ *
10708
+ * @return bool
10709
+ */
10710
+ function has_paid_plan() {
10711
+ return $this->_has_paid_plans ||
10712
+ FS_Plan_Manager::instance()->has_paid_plan( $this->_plans );
10713
+ }
10714
+
10715
+ /**
10716
+ * Check if plugin has any plan with a trail.
10717
+ *
10718
+ * @author Vova Feldman (@svovaf)
10719
+ * @since 1.0.9
10720
+ *
10721
+ * @return bool
10722
+ */
10723
+ function has_trial_plan() {
10724
+ /**
10725
+ * @author Vova Feldman(@svovaf)
10726
+ * @since 1.2.1.5
10727
+ *
10728
+ * Allow setting a trial from the SDK without calling the API.
10729
+ * But, if the user did opt-in, continue using the real data from the API.
10730
+ */
10731
+ if ( $this->_trial_days >= 0 ) {
10732
+ return true;
10733
+ }
10734
+
10735
+ return $this->_storage->get( 'has_trial_plan', false );
10736
+ }
10737
+
10738
+ /**
10739
+ * Check if plugin has any free plan, or is it premium only.
10740
+ *
10741
+ * Note: If no plans configured, assume plugin is free.
10742
+ *
10743
+ * @author Vova Feldman (@svovaf)
10744
+ * @since 1.0.7
10745
+ *
10746
+ * @return bool
10747
+ */
10748
+ function has_free_plan() {
10749
+ return ! $this->is_only_premium();
10750
+ }
10751
+
10752
+ /**
10753
+ * Displays a license activation dialog box when the user clicks on the "Activate License"
10754
+ * or "Change License" link on the plugins
10755
+ * page.
10756
+ *
10757
+ * @author Leo Fajardo (@leorw)
10758
+ * @since 1.1.9
10759
+ */
10760
+ function _add_license_activation_dialog_box() {
10761
+ $vars = array(
10762
+ 'id' => $this->_module_id,
10763
+ );
10764
+
10765
+ fs_require_template( 'forms/license-activation.php', $vars );
10766
+ fs_require_template( 'forms/resend-key.php', $vars );
10767
+ }
10768
+
10769
+ /**
10770
+ * Displays a subscription cancellation dialog box when the user clicks on the "Deactivate License"
10771
+ * link on the "Account" page or deactivates a plugin and there's an active subscription that is
10772
+ * either associated with a non-lifetime single-site license or non-lifetime multisite license that
10773
+ * is only activated on a single production site.
10774
+ *
10775
+ * @author Leo Fajardo (@leorw)
10776
+ * @since 2.2.1
10777
+ *
10778
+ * @param bool $is_license_deactivation
10779
+ */
10780
+ function _maybe_add_subscription_cancellation_dialog_box( $is_license_deactivation = false ) {
10781
+ if ( fs_is_network_admin() ) {
10782
+ // Subscription cancellation dialog box is currently not supported for multisite networks.
10783
+ return;
10784
+ }
10785
+
10786
+ $license = $this->_get_license();
10787
+
10788
+ /**
10789
+ * If the installation is associated with a non-lifetime license, which is either a single-site or only activated on a single production site (or zero), and connected to an active subscription, suggest the customer to cancel the subscription upon deactivation.
10790
+ *
10791
+ * @author Leo Fajardo (@leorw) (Comment added by Vova Feldman @svovaf)
10792
+ * @since 2.2.1
10793
+ */
10794
+ if ( ! is_object( $license ) ||
10795
+ $license->is_lifetime() ||
10796
+ ( ! $license->is_single_site() && $license->activated > 1 )
10797
+ ) {
10798
+ return;
10799
+ }
10800
+
10801
+ /**
10802
+ * @var FS_Subscription $subscription
10803
+ */
10804
+ $subscription = $this->_get_subscription( $license->id );
10805
+ if ( ! is_object( $subscription ) || ! $subscription->is_active() ) {
10806
+ return;
10807
+ }
10808
+
10809
+ $vars = array(
10810
+ 'id' => $this->_module_id,
10811
+ 'license' => $license,
10812
+ 'has_trial' => $this->is_paid_trial(),
10813
+ 'is_license_deactivation' => $is_license_deactivation,
10814
+ );
10815
+
10816
+ fs_require_template( 'forms/subscription-cancellation.php', $vars );
10817
+ }
10818
+
10819
+ /**
10820
+ * @author Leo Fajardo (@leorw)
10821
+ * @since 2.0.2
10822
+ */
10823
+ function _add_premium_version_upgrade_selection_dialog_box() {
10824
+ $modules_update = get_site_transient( $this->is_theme() ? 'update_themes' : 'update_plugins' );
10825
+ if ( ! isset( $modules_update->response[ $this->_plugin_basename ] ) ) {
10826
+ return;
10827
+ }
10828
+
10829
+ $vars = array(
10830
+ 'id' => $this->_module_id,
10831
+ 'new_version' => is_object( $modules_update->response[ $this->_plugin_basename ] ) ?
10832
+ $modules_update->response[ $this->_plugin_basename ]->new_version :
10833
+ $modules_update->response[ $this->_plugin_basename ]['new_version']
10834
+ );
10835
+
10836
+ fs_require_template( 'forms/premium-versions-upgrade-metadata.php', $vars );
10837
+ fs_require_once_template( 'forms/premium-versions-upgrade-handler.php', $vars );
10838
+ }
10839
+
10840
+ /**
10841
+ * Displays the opt-out dialog box when the user clicks on the "Opt Out" link on the "Plugins"
10842
+ * page.
10843
+ *
10844
+ * @author Leo Fajardo (@leorw)
10845
+ * @since 1.2.1.5
10846
+ */
10847
+ function _add_optout_dialog() {
10848
+ if ( $this->is_theme() ) {
10849
+ $vars = null;
10850
+ fs_require_once_template( '/js/jquery.content-change.php', $vars );
10851
+ }
10852
+
10853
+ $vars = array( 'id' => $this->_module_id );
10854
+ fs_require_template( 'forms/optout.php', $vars );
10855
+ }
10856
+
10857
+ /**
10858
+ * Prepare page to include all required UI and logic for the license activation dialog.
10859
+ *
10860
+ * @author Vova Feldman (@svovaf)
10861
+ * @since 1.2.0
10862
+ */
10863
+ function _add_license_activation() {
10864
+ if ( ! $this->is_user_admin() ) {
10865
+ // Only admins can activate a license.
10866
+ return;
10867
+ }
10868
+
10869
+ if ( ! $this->has_paid_plan() ) {
10870
+ // Module doesn't have any paid plans.
10871
+ return;
10872
+ }
10873
+
10874
+ if ( ! $this->is_premium() ) {
10875
+ // Only add license activation logic to the premium version.
10876
+ return;
10877
+ }
10878
+
10879
+ // Add license activation link and AJAX request handler.
10880
+ if ( self::is_plugins_page() ) {
10881
+ /**
10882
+ * @since 1.2.0 Add license action link only on plugins page.
10883
+ */
10884
+ $this->_add_license_action_link();
10885
+ }
10886
+
10887
+ // Add license activation AJAX callback.
10888
+ $this->add_ajax_action( 'activate_license', array( &$this, '_activate_license_ajax_action' ) );
10889
+
10890
+ // Add resend license AJAX callback.
10891
+ $this->add_ajax_action( 'resend_license_key', array( &$this, '_resend_license_key_ajax_action' ) );
10892
+ }
10893
+
10894
+ /**
10895
+ * @author Leo Fajardo (@leorw)
10896
+ * @since 2.0.2
10897
+ */
10898
+ function _add_premium_version_upgrade_selection() {
10899
+ if ( ! $this->is_user_admin() ) {
10900
+ return;
10901
+ }
10902
+
10903
+ if ( ! $this->is_premium() || $this->has_any_active_valid_license() ) {
10904
+ // This is relevant only to the free versions and premium versions without an active license.
10905
+ return;
10906
+ }
10907
+
10908
+ if ( self::is_updates_page() || ( $this->is_plugin() && self::is_plugins_page() ) ) {
10909
+ $this->_add_premium_version_upgrade_selection_action();
10910
+ }
10911
+ }
10912
+
10913
+ /**
10914
+ * @author Leo Fajardo (@leorw)
10915
+ *
10916
+ * @since 1.1.9
10917
+ * @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).
10918
+ */
10919
+ function _activate_license_ajax_action() {
10920
+ $this->_logger->entrance();
10921
+
10922
+ $this->check_ajax_referer( 'activate_license' );
10923
+
10924
+ $license_key = trim( fs_request_get( 'license_key' ) );
10925
+
10926
+ if ( empty( $license_key ) ) {
10927
+ exit;
10928
+ }
10929
+
10930
+ $plugin_id = fs_request_get( 'module_id', '', 'post' );
10931
+ $fs = ( $plugin_id == $this->_module_id ) ?
10932
+ $this :
10933
+ $this->get_addon_instance( $plugin_id );
10934
+
10935
+ $error = false;
10936
+ $next_page = false;
10937
+
10938
+ $sites = fs_is_network_admin() ?
10939
+ fs_request_get( 'sites', array(), 'post' ) :
10940
+ array();
10941
+
10942
+ $blog_id = fs_request_get( 'blog_id' );
10943
+ $has_valid_blog_id = is_numeric( $blog_id );
10944
+
10945
+ if ( $fs->is_registered() ) {
10946
+ if ( fs_is_network_admin() && ! $has_valid_blog_id ) {
10947
+ // If no specific blog ID was provided, activate the license for all sites in the network.
10948
+ $blog_2_install_map = array();
10949
+ $site_ids = array();
10950
+
10951
+ foreach ( $sites as $site ) {
10952
+ if ( ! isset( $site['blog_id'] ) || ! is_numeric( $site['blog_id'] ) ) {
10953
+ continue;
10954
+ }
10955
+
10956
+ $install = $this->get_install_by_blog_id( $site['blog_id'] );
10957
+
10958
+ if ( is_object( $install ) ) {
10959
+ $blog_2_install_map[ $site['blog_id'] ] = $install;
10960
+ } else {
10961
+ $site_ids[] = $site['blog_id'];
10962
+ }
10963
+ }
10964
+
10965
+ $user = $this->get_current_or_network_user();
10966
+
10967
+ if ( ! empty( $blog_2_install_map ) ) {
10968
+ $result = $this->activate_license_on_many_installs( $user, $license_key, $blog_2_install_map );
10969
+
10970
+ if ( true !== $result ) {
10971
+ $error = FS_Api::is_api_error_object( $result ) ?
10972
+ $result->error->message :
10973
+ var_export( $result, true );
10974
+ }
10975
+ }
10976
+
10977
+ if ( empty( $error ) && ! empty( $site_ids ) ) {
10978
+ $result = $this->activate_license_on_many_sites( $user, $license_key, $site_ids );
10979
+
10980
+ if ( true !== $result ) {
10981
+ $error = FS_Api::is_api_error_object( $result ) ?
10982
+ $result->error->message :
10983
+ var_export( $result, true );
10984
+ }
10985
+ }
10986
+ } else {
10987
+ if ( $has_valid_blog_id ) {
10988
+ /**
10989
+ * If a specific blog ID was provided, activate the license only for the install that is
10990
+ * associated with the given blog ID.
10991
+ *
10992
+ * @author Leo Fajardo (@leorw)
10993
+ */
10994
+ $this->switch_to_blog( $blog_id );
10995
+ }
10996
+
10997
+ $api = $fs->get_api_site_scope();
10998
+
10999
+ $params = array(
11000
+ 'license_key' => $fs->apply_filters( 'license_key', $license_key )
11001
+ );
11002
+
11003
+ $install = $api->call( $fs->add_show_pending( '/' ), 'put', $params );
11004
+
11005
+ if ( FS_Api::is_api_error( $install ) ) {
11006
+ $error = FS_Api::is_api_error_object( $install ) ?
11007
+ $install->error->message :
11008
+ var_export( $install->error, true );
11009
+ } else {
11010
+ $fs->reconnect_locally( $has_valid_blog_id );
11011
+ }
11012
+ }
11013
+
11014
+ if ( empty( $error ) ) {
11015
+ $this->network_upgrade_mode_completed();
11016
+
11017
+ $fs->_sync_license( true, $has_valid_blog_id );
11018
+
11019
+ $next_page = $fs->is_addon() ?
11020
+ $fs->get_parent_instance()->get_account_url() :
11021
+ $fs->get_account_url();
11022
+ }
11023
+ } else {
11024
+ $next_page = $fs->opt_in(
11025
+ false,
11026
+ false,
11027
+ false,
11028
+ $license_key,
11029
+ false,
11030
+ false,
11031
+ false,
11032
+ fs_request_get_bool( 'is_marketing_allowed', null ),
11033
+ $sites
11034
+ );
11035
+
11036
+ if ( isset( $next_page->error ) ) {
11037
+ $error = $next_page->error;
11038
+ } else {
11039
+ if ( fs_is_network_admin() ) {
11040
+ /**
11041
+ * Get the list of sites that were just opted-in (and license activated).
11042
+ * This is an optimization for the next part below saving some DB queries.
11043
+ */
11044
+ $connected_sites = array();
11045
+ foreach ( $sites as $site ) {
11046
+ if ( isset( $site['blog_id'] ) && is_numeric( $site['blog_id'] ) ) {
11047
+ $connected_sites[ $site['blog_id'] ] = true;
11048
+ }
11049
+ }
11050
+
11051
+ $all_sites = self::get_sites();
11052
+ $pending_sites = array();
11053
+
11054
+ /**
11055
+ * 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.
11056
+ *
11057
+ * @author Vova Feldman (@svovaf)
11058
+ */
11059
+ foreach ( $all_sites as $site ) {
11060
+ $blog_id = self::get_site_blog_id( $site );
11061
+
11062
+ if ( isset( $connected_sites[ $blog_id ] ) ) {
11063
+ // Site was just connected.
11064
+ continue;
11065
+ }
11066
+
11067
+ if ( $this->is_installed_on_site( $blog_id ) ) {
11068
+ // Site was already connected before.
11069
+ continue;
11070
+ }
11071
+
11072
+ if ( $this->is_site_delegated_connection( $blog_id ) ) {
11073
+ // Site's connection was delegated.
11074
+ continue;
11075
+ }
11076
+
11077
+ if ( $this->is_anonymous_site( $blog_id ) ) {
11078
+ // Site connection was already skipped.
11079
+ continue;
11080
+ }
11081
+
11082
+ $pending_sites[] = self::get_site_info( $site );
11083
+ }
11084
+
11085
+ if ( ! empty( $pending_sites ) ) {
11086
+ if ( $this->is_freemium() ) {
11087
+ $this->skip_connection( $pending_sites );
11088
+ } else {
11089
+ $this->delegate_connection( $pending_sites );
11090
+ }
11091
+ }
11092
+ }
11093
+ }
11094
+ }
11095
+
11096
+ if ( false === $error && true === $this->_storage->require_license_activation ) {
11097
+ $this->_storage->require_license_activation = false;
11098
+ }
11099
+
11100
+ $result = array(
11101
+ 'success' => ( false === $error )
11102
+ );
11103
+
11104
+ if ( false !== $error ) {
11105
+ $result['error'] = $error;
11106
+ } else {
11107
+ $result['next_page'] = $next_page;
11108
+ }
11109
+
11110
+ echo json_encode( $result );
11111
+
11112
+ exit;
11113
+ }
11114
+
11115
+ /**
11116
+ * @author Leo Fajardo (@leorw)
11117
+ * @since 1.2.3.1
11118
+ */
11119
+ function _network_activate_ajax_action() {
11120
+ $this->_logger->entrance();
11121
+
11122
+ $this->check_ajax_referer( 'network_activate' );
11123
+
11124
+ $plugin_id = fs_request_get( 'module_id', '', 'post' );
11125
+ $fs = ( $plugin_id == $this->_module_id ) ?
11126
+ $this :
11127
+ $this->get_addon_instance( $plugin_id );
11128
+
11129
+ $error = false;
11130
+
11131
+ $sites = fs_request_get( 'sites', array(), 'post' );
11132
+ if ( is_array( $sites ) && ! empty( $sites ) ) {
11133
+ $sites_by_action = array(
11134
+ 'allow' => array(),
11135
+ 'delegate' => array(),
11136
+ 'skip' => array()
11137
+ );
11138
+
11139
+ foreach ( $sites as $site ) {
11140
+ $sites_by_action[ $site['action'] ][] = $site;
11141
+ }
11142
+
11143
+ $total_sites = count( $sites );
11144
+ $total_sites_to_delegate = count( $sites_by_action['delegate'] );
11145
+
11146
+ $next_page = '';
11147
+ if ( $total_sites === $total_sites_to_delegate &&
11148
+ ! $this->is_network_upgrade_mode()
11149
+ ) {
11150
+ $this->delegate_connection();
11151
+ } else {
11152
+ if ( ! empty( $sites_by_action['delegate'] ) ) {
11153
+ $this->delegate_connection( $sites_by_action['delegate'] );
11154
+ }
11155
+
11156
+ if ( ! empty( $sites_by_action['skip'] ) ) {
11157
+ $this->skip_connection( $sites_by_action['skip'] );
11158
+ }
11159
+
11160
+ if ( ! empty( $sites_by_action['allow'] ) ) {
11161
+ if ( ! $fs->is_registered() || ! $this->_is_network_active ) {
11162
+ $next_page = $fs->opt_in(
11163
+ false,
11164
+ false,
11165
+ false,
11166
+ false,
11167
+ false,
11168
+ false,
11169
+ false,
11170
+ fs_request_get_bool( 'is_marketing_allowed', null ),
11171
+ $sites_by_action['allow']
11172
+ );
11173
+ } else {
11174
+ $next_page = $fs->install_with_user(
11175
+ $this->get_network_user(),
11176
+ false,
11177
+ false,
11178
+ false,
11179
+ true,
11180
+ $sites_by_action['allow']
11181
+ );
11182
+ }
11183
+
11184
+ if ( is_object( $next_page ) && isset( $next_page->error ) ) {
11185
+ $error = $next_page->error;
11186
+ }
11187
+ }
11188
+ }
11189
+
11190
+ if ( empty( $next_page ) ) {
11191
+ $next_page = $this->get_after_activation_url( 'after_network_activation_url' );
11192
+ }
11193
+ } else {
11194
+ $error = $this->get_text_inline( 'Invalid site details collection.', 'invalid_site_details_collection' );
11195
+ }
11196
+
11197
+ $result = array(
11198
+ 'success' => ( false === $error )
11199
+ );
11200
+
11201
+ if ( false !== $error ) {
11202
+ $result['error'] = $error;
11203
+ } else {
11204
+ $result['next_page'] = $next_page;
11205
+ }
11206
+
11207
+ echo json_encode( $result );
11208
+
11209
+ exit;
11210
+ }
11211
+
11212
+ /**
11213
+ * Billing update AJAX callback.
11214
+ *
11215
+ * @author Vova Feldman (@svovaf)
11216
+ * @since 1.2.1.5
11217
+ */
11218
+ function _update_billing_ajax_action() {
11219
+ $this->_logger->entrance();
11220
+
11221
+ $this->check_ajax_referer( 'update_billing' );
11222
+
11223
+ if ( ! $this->is_user_admin() ) {
11224
+ // Only for admins.
11225
+ self::shoot_ajax_failure();
11226
+ }
11227
+
11228
+ $billing = fs_request_get( 'billing' );
11229
+
11230
+ $api = $this->get_api_user_scope();
11231
+ $result = $api->call( '/billing.json', 'put', array_merge( $billing, array(
11232
+ 'plugin_id' => $this->get_parent_id(),
11233
+ ) ) );
11234
+
11235
+ if ( ! $this->is_api_result_entity( $result ) ) {
11236
+ self::shoot_ajax_failure();
11237
+ }
11238
+
11239
+ // Purge cached billing.
11240
+ $this->get_api_user_scope()->purge_cache( 'billing.json' );
11241
+
11242
+ self::shoot_ajax_success();
11243
+ }
11244
+
11245
+ /**
11246
+ * Trial start for anonymous users (AJAX callback).
11247
+ *
11248
+ * @author Vova Feldman (@svovaf)
11249
+ * @since 1.2.1.5
11250
+ */
11251
+ function _start_trial_ajax_action() {
11252
+ $this->_logger->entrance();
11253
+
11254
+ $this->check_ajax_referer( 'start_trial' );
11255
+
11256
+ if ( ! $this->is_user_admin() ) {
11257
+ // Only for admins.
11258
+ self::shoot_ajax_failure();
11259
+ }
11260
+
11261
+ $trial_data = fs_request_get( 'trial' );
11262
+
11263
+ $next_page = $this->opt_in(
11264
+ false,
11265
+ false,
11266
+ false,
11267
+ false,
11268
+ false,
11269
+ $trial_data['plan_id']
11270
+ );
11271
+
11272
+ if ( is_object( $next_page ) && $this->is_api_error( $next_page ) ) {
11273
+ self::shoot_ajax_failure(
11274
+ isset( $next_page->error ) ?
11275
+ $next_page->error->message :
11276
+ var_export( $next_page, true )
11277
+ );
11278
+ }
11279
+
11280
+ $this->shoot_ajax_success( array(
11281
+ 'next_page' => $next_page,
11282
+ ) );
11283
+ }
11284
+
11285
+ /**
11286
+ * @author Leo Fajardo (@leorw)
11287
+ * @since 1.2.0
11288
+ */
11289
+ function _resend_license_key_ajax_action() {
11290
+ $this->_logger->entrance();
11291
+
11292
+ $this->check_ajax_referer( 'resend_license_key' );
11293
+
11294
+ $email_address = sanitize_email( trim( fs_request_get( 'email', '', 'post' ) ) );
11295
+
11296
+ if ( empty( $email_address ) ) {
11297
+ exit;
11298
+ }
11299
+
11300
+ $error = false;
11301
+
11302
+ $api = $this->get_api_plugin_scope();
11303
+ $result = $api->call( '/licenses/resend.json', 'post',
11304
+ array(
11305
+ 'email' => $email_address,
11306
+ 'url' => home_url(),
11307
+ )
11308
+ );
11309
+
11310
+ if ( is_object( $result ) && isset( $result->error ) ) {
11311
+ $error = $result->error;
11312
+
11313
+ if ( in_array( $error->code, array( 'invalid_email', 'no_user' ) ) ) {
11314
+ $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' );
11315
+ } else if ( 'no_license' === $error->code ) {
11316
+ $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' );
11317
+ } else {
11318
+ $error = $error->message;
11319
+ }
11320
+ }
11321
+
11322
+ $licenses = array(
11323
+ 'success' => ( false === $error )
11324
+ );
11325
+
11326
+ if ( false !== $error ) {
11327
+ $licenses['error'] = sprintf( '%s... %s', $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ), strtolower( $error ) );
11328
+ }
11329
+
11330
+ echo json_encode( $licenses );
11331
+
11332
+ exit;
11333
+ }
11334
+
11335
+ /**
11336
+ * @author Vova Feldman (@svovaf)
11337
+ * @since 1.2.1.8
11338
+ *
11339
+ * @var string
11340
+ */
11341
+ private static $_pagenow;
11342
+
11343
+ /**
11344
+ * Get current page or the referer if executing a WP AJAX request.
11345
+ *
11346
+ * @author Vova Feldman (@svovaf)
11347
+ * @since 1.2.1.8
11348
+ *
11349
+ * @return string
11350
+ */
11351
+ static function get_current_page() {
11352
+ if ( ! isset( self::$_pagenow ) ) {
11353
+ global $pagenow;
11354
+
11355
+ self::$_pagenow = $pagenow;
11356
+
11357
+ if ( self::is_ajax() &&
11358
+ 'admin-ajax.php' === $pagenow
11359
+ ) {
11360
+ $referer = fs_get_raw_referer();
11361
+
11362
+ if ( is_string( $referer ) ) {
11363
+ $parts = explode( '?', $referer );
11364
+
11365
+ self::$_pagenow = basename( $parts[0] );
11366
+ }
11367
+ }
11368
+ }
11369
+
11370
+ return self::$_pagenow;
11371
+ }
11372
+
11373
+ /**
11374
+ * Helper method to check if user in the plugins page.
11375
+ *
11376
+ * @author Vova Feldman (@svovaf)
11377
+ * @since 1.2.1.5
11378
+ *
11379
+ * @return bool
11380
+ */
11381
+ static function is_plugins_page() {
11382
+ return ( 'plugins.php' === self::get_current_page() );
11383
+ }
11384
+
11385
+ /**
11386
+ * @author Leo Fajardo (@leorw)
11387
+ * @since 2.0.2
11388
+ *
11389
+ * @return bool
11390
+ */
11391
+ static function is_updates_page() {
11392
+ return ( 'update-core.php' === self::get_current_page() );
11393
+ }
11394
+
11395
+ /**
11396
+ * Helper method to check if user in the themes page.
11397
+ *
11398
+ * @author Vova Feldman (@svovaf)
11399
+ * @since 1.2.2.6
11400
+ *
11401
+ * @return bool
11402
+ */
11403
+ static function is_themes_page() {
11404
+ return ( 'themes.php' === self::get_current_page() );
11405
+ }
11406
+
11407
+ #----------------------------------------------------------------------------------
11408
+ #region Affiliation
11409
+ #----------------------------------------------------------------------------------
11410
+
11411
+ /**
11412
+ * @author Leo Fajardo (@leorw)
11413
+ * @since 1.2.3
11414
+ *
11415
+ * @return bool
11416
+ */
11417
+ function has_affiliate_program() {
11418
+ if ( ! is_object( $this->_plugin ) ) {
11419
+ return false;
11420
+ }
11421
+
11422
+ return $this->_plugin->has_affiliate_program();
11423
+ }
11424
+
11425
+ /**
11426
+ * @author Leo Fajardo (@leorw)
11427
+ * @since 1.2.4
11428
+ */
11429
+ private function fetch_affiliate_terms() {
11430
+ if ( ! is_object( $this->plugin_affiliate_terms ) ) {
11431
+ $plugins_api = $this->get_api_plugin_scope();
11432
+ $affiliate_terms = $plugins_api->get( '/aff.json?type=affiliation', false );
11433
+
11434
+ if ( ! $this->is_api_result_entity( $affiliate_terms ) ) {
11435
+ return;
11436
+ }
11437
+
11438
+ $this->plugin_affiliate_terms = new FS_AffiliateTerms( $affiliate_terms );
11439
+ }
11440
+ }
11441
+
11442
+ /**
11443
+ * @author Leo Fajardo (@leorw)
11444
+ * @since 1.2.4
11445
+ */
11446
+ private function fetch_affiliate_and_custom_terms() {
11447
+ if ( ! empty( $this->_storage->affiliate_application_data ) ) {
11448
+ $application_data = $this->_storage->affiliate_application_data;
11449
+ $flush = ( ! isset( $application_data['status'] ) || 'pending' === $application_data['status'] );
11450
+
11451
+ $users_api = $this->get_api_user_scope();
11452
+ $result = $users_api->get( "/plugins/{$this->_plugin->id}/aff/{$this->plugin_affiliate_terms->id}/affiliates.json", $flush );
11453
+ if ( $this->is_api_result_object( $result, 'affiliates' ) ) {
11454
+ if ( ! empty( $result->affiliates ) ) {
11455
+ $affiliate = new FS_Affiliate( $result->affiliates[0] );
11456
+
11457
+ if ( ! isset( $application_data['status'] ) || $application_data['status'] !== $affiliate->status ) {
11458
+ $application_data['status'] = $affiliate->status;
11459
+ $this->_storage->affiliate_application_data = $application_data;
11460
+ }
11461
+
11462
+ if ( $affiliate->is_using_custom_terms ) {
11463
+ $affiliate_terms = $users_api->get( "/plugins/{$this->_plugin->id}/affiliates/{$affiliate->id}/aff/{$affiliate->custom_affiliate_terms_id}.json", $flush );
11464
+ if ( $this->is_api_result_entity( $affiliate_terms ) ) {
11465
+ $this->custom_affiliate_terms = new FS_AffiliateTerms( $affiliate_terms );
11466
+ }
11467
+ }
11468
+
11469
+ $this->affiliate = $affiliate;
11470
+ }
11471
+ }
11472
+ }
11473
+ }
11474
+
11475
+ /**
11476
+ * @author Leo Fajardo (@leorw)
11477
+ * @since 1.2.3
11478
+ */
11479
+ private function fetch_affiliate_and_terms() {
11480
+ $this->_logger->entrance();
11481
+
11482
+ $this->fetch_affiliate_terms();
11483
+ $this->fetch_affiliate_and_custom_terms();
11484
+ }
11485
+
11486
+ /**
11487
+ * @author Leo Fajardo (@leorw)
11488
+ * @since 1.2.3
11489
+ *
11490
+ * @return FS_Affiliate
11491
+ */
11492
+ function get_affiliate() {
11493
+ return $this->affiliate;
11494
+ }
11495
+
11496
+
11497
+ /**
11498
+ * @author Leo Fajardo (@leorw)
11499
+ * @since 1.2.3
11500
+ *
11501
+ * @return FS_AffiliateTerms
11502
+ */
11503
+ function get_affiliate_terms() {
11504
+ return is_object( $this->custom_affiliate_terms ) ?
11505
+ $this->custom_affiliate_terms :
11506
+ $this->plugin_affiliate_terms;
11507
+ }
11508
+
11509
+ /**
11510
+ * @author Leo Fajardo (@leorw)
11511
+ * @since 1.2.3
11512
+ */
11513
+ function _submit_affiliate_application() {
11514
+ $this->_logger->entrance();
11515
+
11516
+ $this->check_ajax_referer( 'submit_affiliate_application' );
11517
+
11518
+ if ( ! $this->is_user_admin() ) {
11519
+ // Only for admins.
11520
+ self::shoot_ajax_failure();
11521
+ }
11522
+
11523
+ $affiliate = fs_request_get( 'affiliate' );
11524
+
11525
+ if ( empty( $affiliate['promotion_methods'] ) ) {
11526
+ unset( $affiliate['promotion_methods'] );
11527
+ }
11528
+
11529
+ if ( ! empty( $affiliate['additional_domains'] ) ) {
11530
+ $affiliate['additional_domains'] = array_unique( $affiliate['additional_domains'] );
11531
+ }
11532
+
11533
+ if ( ! $this->is_registered() ) {
11534
+ // Opt in but don't track usage.
11535
+ $next_page = $this->opt_in(
11536
+ false,
11537
+ false,
11538
+ false,
11539
+ false,
11540
+ false,
11541
+ false,
11542
+ true
11543
+ );
11544
+
11545
+ if ( is_object( $next_page ) && $this->is_api_error( $next_page ) ) {
11546
+ self::shoot_ajax_failure(
11547
+ isset( $next_page->error ) ?
11548
+ $next_page->error->message :
11549
+ var_export( $next_page, true )
11550
+ );
11551
+ } else if ( $this->is_pending_activation() ) {
11552
+ self::shoot_ajax_failure( $this->get_text_inline( 'Account is pending activation.', 'account-is-pending-activation' ) );
11553
+ }
11554
+ }
11555
+
11556
+ $this->fetch_affiliate_terms();
11557
+
11558
+ $api = $this->get_api_user_scope();
11559
+ $result = $api->call(
11560
+ ( "/plugins/{$this->_plugin->id}/aff/{$this->plugin_affiliate_terms->id}/affiliates.json" ),
11561
+ 'post',
11562
+ $affiliate
11563
+ );
11564
+
11565
+ if ( $this->is_api_error( $result ) ) {
11566
+ self::shoot_ajax_failure(
11567
+ isset( $result->error ) ?
11568
+ $result->error->message :
11569
+ var_export( $result, true )
11570
+ );
11571
+ } else {
11572
+ if ( $this->_admin_notices->has_sticky( 'affiliate_program' ) ) {
11573
+ $this->_admin_notices->remove_sticky( 'affiliate_program' );
11574
+ }
11575
+
11576
+ $affiliate_application_data = array(
11577
+ 'status' => 'pending',
11578
+ 'stats_description' => $affiliate['stats_description'],
11579
+ 'promotion_method_description' => $affiliate['promotion_method_description'],
11580
+ );
11581
+
11582
+ if ( ! empty( $affiliate['promotion_methods'] ) ) {
11583
+ $affiliate_application_data['promotion_methods'] = $affiliate['promotion_methods'];
11584
+ }
11585
+
11586
+ if ( ! empty( $affiliate['domain'] ) ) {
11587
+ $affiliate_application_data['domain'] = $affiliate['domain'];
11588
+ }
11589
+
11590
+ if ( ! empty( $affiliate['additional_domains'] ) ) {
11591
+ $affiliate_application_data['additional_domains'] = $affiliate['additional_domains'];
11592
+ }
11593
+
11594
+ $this->_storage->affiliate_application_data = $affiliate_application_data;
11595
+ }
11596
+
11597
+ // Purge cached affiliate.
11598
+ $api->purge_cache( 'affiliate.json' );
11599
+
11600
+ self::shoot_ajax_success( $result );
11601
+ }
11602
+
11603
+ /**
11604
+ * @author Leo Fajardo (@leorw)
11605
+ * @since 1.2.3
11606
+ *
11607
+ * @return array|null
11608
+ */
11609
+ function get_affiliate_application_data() {
11610
+ if ( empty( $this->_storage->affiliate_application_data ) ) {
11611
+ return null;
11612
+ }
11613
+
11614
+ return $this->_storage->affiliate_application_data;
11615
+ }
11616
+
11617
+ #endregion Affiliation ------------------------------------------------------------
11618
+
11619
+ #----------------------------------------------------------------------------------
11620
+ #region URL Generators
11621
+ #----------------------------------------------------------------------------------
11622
+
11623
+ /**
11624
+ * Alias to pricing_url().
11625
+ *
11626
+ * @author Vova Feldman (@svovaf)
11627
+ * @since 1.0.2
11628
+ *
11629
+ * @uses pricing_url()
11630
+ *
11631
+ * @param string $period Billing cycle
11632
+ * @param bool $is_trial
11633
+ *
11634
+ * @return string
11635
+ */
11636
+ function get_upgrade_url( $period = WP_FS__PERIOD_ANNUALLY, $is_trial = false ) {
11637
+ return $this->pricing_url( $period, $is_trial );
11638
+ }
11639
+
11640
+ /**
11641
+ * @author Vova Feldman (@svovaf)
11642
+ * @since 1.0.9
11643
+ *
11644
+ * @uses get_upgrade_url()
11645
+ *
11646
+ * @return string
11647
+ */
11648
+ function get_trial_url() {
11649
+ return $this->get_upgrade_url( WP_FS__PERIOD_ANNUALLY, true );
11650
+ }
11651
+
11652
+ /**
11653
+ * @author Leo Fajardo (@leorw)
11654
+ * @since 2.1.4
11655
+ *
11656
+ * @param string $new_version
11657
+ *
11658
+ * @return string
11659
+ */
11660
+ function version_upgrade_checkout_link( $new_version ) {
11661
+ if ( ! is_object( $this->_license ) ) {
11662
+ $url = $this->pricing_url();
11663
+
11664
+ $purchase_license_text = $this->get_text_inline( 'Buy a license now', 'buy-license-now' );
11665
+ } else {
11666
+ $subscription = $this->_get_subscription( $this->_license->id );
11667
+
11668
+ $url = $this->checkout_url(
11669
+ is_object( $subscription ) ?
11670
+ ( 1 == $subscription->billing_cycle ? WP_FS__PERIOD_MONTHLY : WP_FS__PERIOD_ANNUALLY ) :
11671
+ WP_FS__PERIOD_LIFETIME,
11672
+ false,
11673
+ array( 'licenses' => $this->_license->quota )
11674
+ );
11675
+
11676
+ $purchase_license_text = $this->get_text_inline( 'Renew your license now', 'renew-license-now' );
11677
+ }
11678
+
11679
+ return sprintf(
11680
+ $this->get_text_inline( '%s to access version %s security & feature updates, and support.', 'x-for-updates-and-support' ),
11681
+ sprintf( '<a href="%s">%s</a>', $url, $purchase_license_text ),
11682
+ $new_version
11683
+ );
11684
+ }
11685
+
11686
+ /**
11687
+ * Plugin's pricing URL.
11688
+ *
11689
+ * @author Vova Feldman (@svovaf)
11690
+ * @since 1.0.4
11691
+ *
11692
+ * @param string $billing_cycle Billing cycle
11693
+ *
11694
+ * @param bool $is_trial
11695
+ *
11696
+ * @return string
11697
+ */
11698
+ function pricing_url( $billing_cycle = WP_FS__PERIOD_ANNUALLY, $is_trial = false ) {
11699
+ $this->_logger->entrance();
11700
+
11701
+ $params = array(
11702
+ 'billing_cycle' => $billing_cycle
11703
+ );
11704
+
11705
+ if ( $is_trial ) {
11706
+ $params['trial'] = 'true';
11707
+ }
11708
+
11709
+ if ( $this->is_addon() ) {
11710
+ return $this->_parent->addon_url( $this->_slug );
11711
+ }
11712
+
11713
+ return $this->_get_admin_page_url( 'pricing', $params );
11714
+ }
11715
+
11716
+ /**
11717
+ * Checkout page URL.
11718
+ *
11719
+ * @author Vova Feldman (@svovaf)
11720
+ * @since 1.0.6
11721
+ *
11722
+ * @param string $billing_cycle Billing cycle
11723
+ * @param bool $is_trial
11724
+ * @param array $extra (optional) Extra parameters, override other query params.
11725
+ *
11726
+ * @return string
11727
+ */
11728
+ function checkout_url(
11729
+ $billing_cycle = WP_FS__PERIOD_ANNUALLY,
11730
+ $is_trial = false,
11731
+ $extra = array()
11732
+ ) {
11733
+ $this->_logger->entrance();
11734
+
11735
+ $params = array(
11736
+ 'checkout' => 'true',
11737
+ 'billing_cycle' => $billing_cycle,
11738
+ );
11739
+
11740
+ if ( $is_trial ) {
11741
+ $params['trial'] = 'true';
11742
+ }
11743
+
11744
+ /**
11745
+ * Params in extra override other params.
11746
+ */
11747
+ $params = array_merge( $params, $extra );
11748
+
11749
+ return $this->_get_admin_page_url( 'pricing', $params );
11750
+ }
11751
+
11752
+ /**
11753
+ * Add-on checkout URL.
11754
+ *
11755
+ * @author Vova Feldman (@svovaf)
11756
+ * @since 1.1.7
11757
+ *
11758
+ * @param number $addon_id
11759
+ * @param number $pricing_id
11760
+ * @param string $billing_cycle
11761
+ * @param bool $is_trial
11762
+ *
11763
+ * @return string
11764
+ */
11765
+ function addon_checkout_url(
11766
+ $addon_id,
11767
+ $pricing_id,
11768
+ $billing_cycle = WP_FS__PERIOD_ANNUALLY,
11769
+ $is_trial = false
11770
+ ) {
11771
+ return $this->checkout_url( $billing_cycle, $is_trial, array(
11772
+ 'plugin_id' => $addon_id,
11773
+ 'pricing_id' => $pricing_id,
11774
+ ) );
11775
+ }
11776
+
11777
+ #endregion
11778
+
11779
+ #endregion ------------------------------------------------------------------
11780
+
11781
+ /**
11782
+ * Check if plugin has any add-ons.
11783
+ *
11784
+ * @author Vova Feldman (@svovaf)
11785
+ * @since 1.0.5
11786
+ *
11787
+ * @since 1.1.7.3 Base logic only on the parameter provided by the developer in the init function.
11788
+ *
11789
+ * @return bool
11790
+ */
11791
+ function has_addons() {
11792
+ $this->_logger->entrance();
11793
+
11794
+ return $this->_has_addons;
11795
+ }
11796
+
11797
+ /**
11798
+ * Check if plugin can work in anonymous mode.
11799
+ *
11800
+ * @author Vova Feldman (@svovaf)
11801
+ * @since 1.0.9
11802
+ *
11803
+ * @return bool
11804
+ *
11805
+ * @deprecated Please use is_enable_anonymous() instead.
11806
+ */
11807
+ function enable_anonymous() {
11808
+ return $this->_enable_anonymous;
11809
+ }
11810
+
11811
+ /**
11812
+ * Check if plugin can work in anonymous mode.
11813
+ *
11814
+ * @author Vova Feldman (@svovaf)
11815
+ * @since 1.1.9
11816
+ *
11817
+ * @return bool
11818
+ */
11819
+ function is_enable_anonymous() {
11820
+ return $this->_enable_anonymous;
11821
+ }
11822
+
11823
+ /**
11824
+ * Check if plugin is premium only (no free plans).
11825
+ *
11826
+ * @author Vova Feldman (@svovaf)
11827
+ * @since 1.1.9
11828
+ *
11829
+ * @return bool
11830
+ */
11831
+ function is_only_premium() {
11832
+ return $this->_is_premium_only;
11833
+ }
11834
+
11835
+ /**
11836
+ * Checks if the plugin's type is "plugin". The other type is "theme".
11837
+ *
11838
+ * @author Leo Fajardo (@leorw)
11839
+ * @since 1.2.2
11840
+ *
11841
+ * @return bool
11842
+ */
11843
+ function is_plugin() {
11844
+ return ( WP_FS__MODULE_TYPE_PLUGIN === $this->_module_type );
11845
+ }
11846
+
11847
+ /**
11848
+ * @author Leo Fajardo (@leorw)
11849
+ * @since 1.2.2
11850
+ *
11851
+ * @return string
11852
+ */
11853
+ function get_module_type() {
11854
+ if ( ! isset( $this->_module_type ) ) {
11855
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
11856
+ $this->_module_type = $id_slug_type_path_map[ $this->_module_id ]['type'];
11857
+ }
11858
+
11859
+ return $this->_module_type;
11860
+ }
11861
+
11862
+ /**
11863
+ * @author Leo Fajardo (@leorw)
11864
+ * @since 1.2.2
11865
+ *
11866
+ * @return string
11867
+ */
11868
+ function get_plugin_main_file_path() {
11869
+ return $this->_plugin_main_file_path;
11870
+ }
11871
+
11872
+ /**
11873
+ * Check if module has a premium code version.
11874
+ *
11875
+ * Serviceware module might be freemium without any
11876
+ * premium code version, where the paid features
11877
+ * are all part of the service.
11878
+ *
11879
+ * @author Vova Feldman (@svovaf)
11880
+ * @since 1.2.1.6
11881
+ *
11882
+ * @return bool
11883
+ */
11884
+ function has_premium_version() {
11885
+ return $this->_has_premium_version;
11886
+ }
11887
+
11888
+ /**
11889
+ * Check if feature supported with current site's plan.
11890
+ *
11891
+ * @author Vova Feldman (@svovaf)
11892
+ * @since 1.0.1
11893
+ *
11894
+ * @todo IMPLEMENT
11895
+ *
11896
+ * @param number $feature_id
11897
+ *
11898
+ * @throws Exception
11899
+ */
11900
+ function is_feature_supported( $feature_id ) {
11901
+ throw new Exception( 'not implemented' );
11902
+ }
11903
+
11904
+ /**
11905
+ * @author Vova Feldman (@svovaf)
11906
+ * @since 1.0.1
11907
+ *
11908
+ * @return bool Is running in SSL/HTTPS
11909
+ */
11910
+ function is_ssl() {
11911
+ return WP_FS__IS_HTTPS;
11912
+ }
11913
+
11914
+ /**
11915
+ * @author Vova Feldman (@svovaf)
11916
+ * @since 1.0.9
11917
+ *
11918
+ * @return bool Is running in AJAX call.
11919
+ *
11920
+ * @link http://wordpress.stackexchange.com/questions/70676/how-to-check-if-i-am-in-admin-ajax
11921
+ */
11922
+ static function is_ajax() {
11923
+ return ( defined( 'DOING_AJAX' ) && DOING_AJAX );
11924
+ }
11925
+
11926
+ /**
11927
+ * Check if it's an AJAX call targeted for the current module.
11928
+ *
11929
+ * @author Vova Feldman (@svovaf)
11930
+ * @since 1.2.0
11931
+ *
11932
+ * @param array|string $actions Collection of AJAX actions.
11933
+ *
11934
+ * @return bool
11935
+ */
11936
+ function is_ajax_action( $actions ) {
11937
+ // Verify it's an ajax call.
11938
+ if ( ! self::is_ajax() ) {
11939
+ return false;
11940
+ }
11941
+
11942
+ // Verify the call is relevant for the plugin.
11943
+ if ( $this->_module_id != fs_request_get( 'module_id' ) ) {
11944
+ return false;
11945
+ }
11946
+
11947
+ // Verify it's one of the specified actions.
11948
+ if ( is_string( $actions ) ) {
11949
+ $actions = explode( ',', $actions );
11950
+ }
11951
+
11952
+ if ( is_array( $actions ) && 0 < count( $actions ) ) {
11953
+ $ajax_action = fs_request_get( 'action' );
11954
+
11955
+ foreach ( $actions as $action ) {
11956
+ if ( $ajax_action === $this->get_action_tag( $action ) ) {
11957
+ return true;
11958
+ }
11959
+ }
11960
+ }
11961
+
11962
+ return false;
11963
+ }
11964
+
11965
+ /**
11966
+ * Check if it's an AJAX call targeted for current request.
11967
+ *
11968
+ * @author Vova Feldman (@svovaf)
11969
+ * @since 1.2.0
11970
+ *
11971
+ * @param array|string $actions Collection of AJAX actions.
11972
+ * @param number|null $module_id
11973
+ *
11974
+ * @return bool
11975
+ */
11976
+ static function is_ajax_action_static( $actions, $module_id = null ) {
11977
+ // Verify it's an ajax call.
11978
+ if ( ! self::is_ajax() ) {
11979
+ return false;
11980
+ }
11981
+
11982
+
11983
+ if ( ! empty( $module_id ) ) {
11984
+ // Verify the call is relevant for the plugin.
11985
+ if ( $module_id != fs_request_get( 'module_id' ) ) {
11986
+ return false;
11987
+ }
11988
+ }
11989
+
11990
+ // Verify it's one of the specified actions.
11991
+ if ( is_string( $actions ) ) {
11992
+ $actions = explode( ',', $actions );
11993
+ }
11994
+
11995
+ if ( is_array( $actions ) && 0 < count( $actions ) ) {
11996
+ $ajax_action = fs_request_get( 'action' );
11997
+
11998
+ foreach ( $actions as $action ) {
11999
+ if ( $ajax_action === self::get_ajax_action_static( $action, $module_id ) ) {
12000
+ return true;
12001
+ }
12002
+ }
12003
+ }
12004
+
12005
+ return false;
12006
+ }
12007
+
12008
+ /**
12009
+ * @author Vova Feldman (@svovaf)
12010
+ * @since 1.1.7
12011
+ *
12012
+ * @return bool
12013
+ */
12014
+ static function is_cron() {
12015
+ return ( defined( 'DOING_CRON' ) && DOING_CRON );
12016
+ }
12017
+
12018
+ /**
12019
+ * Check if a real user is visiting the admin dashboard.
12020
+ *
12021
+ * @author Vova Feldman (@svovaf)
12022
+ * @since 1.1.7
12023
+ *
12024
+ * @return bool
12025
+ */
12026
+ function is_user_in_admin() {
12027
+ return is_admin() && ! self::is_ajax() && ! self::is_cron();
12028
+ }
12029
+
12030
+ /**
12031
+ * Check if a real user is in the customizer view.
12032
+ *
12033
+ * @author Vova Feldman (@svovaf)
12034
+ * @since 1.2.2.7
12035
+ *
12036
+ * @return bool
12037
+ */
12038
+ static function is_customizer() {
12039
+ return is_customize_preview();
12040
+ }
12041
+
12042
+ /**
12043
+ * Check if running in HTTPS and if site's plan matching the specified plan.
12044
+ *
12045
+ * @param string $plan
12046
+ * @param bool $exact
12047
+ *
12048
+ * @return bool
12049
+ */
12050
+ function is_ssl_and_plan( $plan, $exact = false ) {
12051
+ return ( $this->is_ssl() && $this->is_plan( $plan, $exact ) );
12052
+ }
12053
+
12054
+ /**
12055
+ * Construct plugin's settings page URL.
12056
+ *
12057
+ * @author Vova Feldman (@svovaf)
12058
+ * @since 1.0.4
12059
+ *
12060
+ * @param string $page
12061
+ * @param array $params
12062
+ * @param bool|null $network
12063
+ *
12064
+ * @return string
12065
+ */
12066
+ function _get_admin_page_url( $page = '', $params = array(), $network = null ) {
12067
+ if ( is_null( $network ) ) {
12068
+ $network = (
12069
+ $this->_is_network_active &&
12070
+ ( fs_is_network_admin() || ! $this->is_delegated_connection() )
12071
+ );
12072
+ }
12073
+
12074
+ if ( 0 < count( $params ) ) {
12075
+ foreach ( $params as $k => $v ) {
12076
+ $params[ $k ] = urlencode( $v );
12077
+ }
12078
+ }
12079
+
12080
+ $page_param = $this->_menu->get_slug( $page );
12081
+
12082
+ if ( empty( $page ) &&
12083
+ $this->is_theme() &&
12084
+ // Show the opt-in as an overlay for free wp.org themes or themes without any settings page.
12085
+ ( $this->is_free_wp_org_theme() || ! $this->has_settings_menu() )
12086
+ ) {
12087
+ $params[ $this->get_unique_affix() . '_show_optin' ] = 'true';
12088
+
12089
+ return add_query_arg(
12090
+ $params,
12091
+ $this->admin_url( 'themes.php', 'admin', $network )
12092
+ );
12093
+ }
12094
+
12095
+ if ( ! $this->has_settings_menu() ) {
12096
+ if ( ! empty( $page ) ) {
12097
+ // Module doesn't have a setting page, but since the request is for
12098
+ // a specific Freemius page, use the admin.php path.
12099
+ return add_query_arg( array_merge( $params, array(
12100
+ 'page' => $page_param,
12101
+ ) ), $this->admin_url( 'admin.php', 'admin', $network ) );
12102
+ } else {
12103
+ if ( $this->is_activation_mode() ) {
12104
+ /**
12105
+ * @author Vova Feldman
12106
+ * @since 1.2.1.6
12107
+ *
12108
+ * If plugin doesn't have a settings page, create one for the opt-in screen.
12109
+ */
12110
+ return add_query_arg( array_merge( $params, array(
12111
+ 'page' => $this->_slug,
12112
+ ) ), $this->admin_url( 'admin.php', 'admin', $network ) );
12113
+ } else {
12114
+ // Plugin without a settings page.
12115
+ return add_query_arg(
12116
+ $params,
12117
+ $this->admin_url( 'plugins.php', 'admin', $network )
12118
+ );
12119
+ }
12120
+ }
12121
+ }
12122
+
12123
+ // Module has a submenu settings page.
12124
+ if ( ! $this->_menu->is_top_level() ) {
12125
+ $parent_slug = $this->_menu->get_parent_slug();
12126
+ $menu_file = ( false !== strpos( $parent_slug, '.php' ) ) ?
12127
+ $parent_slug :
12128
+ 'admin.php';
12129
+
12130
+ return add_query_arg( array_merge( $params, array(
12131
+ 'page' => $page_param,
12132
+ ) ), $this->admin_url( $menu_file, 'admin', $network ) );
12133
+ }
12134
+
12135
+ // Module has a top level CPT settings page.
12136
+ if ( $this->_menu->is_cpt() ) {
12137
+ if ( empty( $page ) && $this->is_activation_mode() ) {
12138
+ return add_query_arg( array_merge( $params, array(
12139
+ 'page' => $page_param
12140
+ ) ), $this->admin_url( 'admin.php', 'admin', $network ) );
12141
+ } else {
12142
+ if ( ! empty( $page ) ) {
12143
+ $params['page'] = $page_param;
12144
+ }
12145
+
12146
+ return add_query_arg(
12147
+ $params,
12148
+ $this->admin_url( $this->_menu->get_raw_slug(), 'admin', $network )
12149
+ );
12150
+ }
12151
+ }
12152
+
12153
+ // Module has a custom top level settings page.
12154
+ return add_query_arg( array_merge( $params, array(
12155
+ 'page' => $page_param,
12156
+ ) ), $this->admin_url( 'admin.php', 'admin', $network ) );
12157
+ }
12158
+
12159
+ #--------------------------------------------------------------------------------
12160
+ #region Multisite
12161
+ #--------------------------------------------------------------------------------
12162
+
12163
+ /**
12164
+ * @author Leo Fajardo (@leorw)
12165
+ * @since 2.0.0
12166
+ *
12167
+ * @return bool
12168
+ */
12169
+ function is_network_active() {
12170
+ return $this->_is_network_active;
12171
+ }
12172
+
12173
+ /**
12174
+ * Delegate activation for the given sites in the network (or all sites if `null`) to site admins.
12175
+ *
12176
+ * @author Leo Fajardo (@leorw)
12177
+ * @since 2.0.0
12178
+ *
12179
+ * @param array|null $sites
12180
+ */
12181
+ private function delegate_connection( $sites = null ) {
12182
+ $this->_logger->entrance();
12183
+
12184
+ $this->_admin_notices->remove_sticky( 'connect_account' );
12185
+
12186
+ if ( is_null( $sites ) ) {
12187
+ // All sites delegation.
12188
+ $this->_storage->store( 'is_delegated_connection', true, true, true );
12189
+ } else {
12190
+ // Specified sites delegation.
12191
+ foreach ( $sites as $site ) {
12192
+ $this->delegate_site_connection( $site['blog_id'] );
12193
+ }
12194
+ }
12195
+
12196
+ $this->network_upgrade_mode_completed();
12197
+ }
12198
+
12199
+ /**
12200
+ * Delegate specific network site conncetion to the site admin.
12201
+ *
12202
+ * @author Vova Feldman (@svovaf)
12203
+ * @since 2.0.0
12204
+ *
12205
+ * @param int $blog_id
12206
+ */
12207
+ private function delegate_site_connection( $blog_id ) {
12208
+ $this->_storage->store( 'is_delegated_connection', true, $blog_id, true );
12209
+ }
12210
+
12211
+ /**
12212
+ * Check if super-admin delegated the connection of ALL sites to the site admins.
12213
+ *
12214
+ * @author Vova Feldman (@svovaf)
12215
+ * @since 2.0.0
12216
+ *
12217
+ * @return bool
12218
+ */
12219
+ function is_network_delegated_connection() {
12220
+ if ( ! $this->_is_network_active ) {
12221
+ return false;
12222
+ }
12223
+
12224
+ return $this->_storage->get( 'is_delegated_connection', false, true );
12225
+ }
12226
+
12227
+ /**
12228
+ * @author Leo Fajardo (@leorw)
12229
+ * @since 2.0.0
12230
+ *
12231
+ * @param int $blog_id
12232
+ *
12233
+ * @return bool
12234
+ */
12235
+ function is_site_delegated_connection( $blog_id = 0 ) {
12236
+ if ( ! $this->_is_network_active ) {
12237
+ return false;
12238
+ }
12239
+
12240
+ if ( 0 == $blog_id ) {
12241
+ $blog_id = get_current_blog_id();
12242
+ }
12243
+
12244
+ return $this->_storage->get( 'is_delegated_connection', false, $blog_id );
12245
+ }
12246
+
12247
+ /**
12248
+ * Check if delegated the connection. When running within the the network admin,
12249
+ * and haven't specified the blog ID, checks if network level delegated. If running
12250
+ * within a site admin or specified a blog ID, check if delegated the connection for
12251
+ * the current context site.
12252
+ *
12253
+ * If executed outside the the admin, check if delegated the connection
12254
+ * for the current context site OR the whole network.
12255
+ *
12256
+ * @author Vova Feldman (@svovaf)
12257
+ * @since 2.0.0
12258
+ *
12259
+ * @param int $blog_id If set, checks if network delegated or blog specific delegated.
12260
+ *
12261
+ * @return bool
12262
+ */
12263
+ function is_delegated_connection( $blog_id = 0 ) {
12264
+ if ( ! $this->_is_network_active ) {
12265
+ return false;
12266
+ }
12267
+
12268
+ if ( fs_is_network_admin() && 0 == $blog_id ) {
12269
+ return $this->is_network_delegated_connection();
12270
+ }
12271
+
12272
+ return (
12273
+ $this->is_network_delegated_connection() ||
12274
+ $this->is_site_delegated_connection( $blog_id )
12275
+ );
12276
+ }
12277
+
12278
+ /**
12279
+ * Check if the current module is active for the site.
12280
+ *
12281
+ * @author Vova Feldman (@svovaf)
12282
+ * @since 2.0.0
12283
+ *
12284
+ * @param int $blog_id
12285
+ *
12286
+ * @return bool
12287
+ */
12288
+ function is_active_for_site( $blog_id ) {
12289
+ if ( ! is_multisite() ) {
12290
+ // Not a multisite and this code is executed, means that the plugin is active.
12291
+ return true;
12292
+ }
12293
+
12294
+ if ( $this->is_theme() ) {
12295
+ // All themes are site level activated.
12296
+ return true;
12297
+ }
12298
+
12299
+ if ( $this->_is_network_active ) {
12300
+ // Plugin was network activated so it's active.
12301
+ return true;
12302
+ }
12303
+
12304
+ return in_array( $this->_plugin_basename, (array) get_blog_option( $blog_id, 'active_plugins', array() ) );
12305
+ }
12306
+
12307
+ /**
12308
+ * @author Leo Fajardo (@leorw)
12309
+ * @since 2.0.0
12310
+ *
12311
+ * @return array Active & public sites collection.
12312
+ */
12313
+ static function get_sites() {
12314
+ /**
12315
+ * For consistency with get_blog_list() which only return active public sites.
12316
+ *
12317
+ * @author Vova Feldman (@svovaf)
12318
+ */
12319
+ $args = array(
12320
+ /**
12321
+ * Commented out in order to handle the migration of site options whether the site is public or not.
12322
+ *
12323
+ * @author Leo Fajardo (@leorw)
12324
+ * @since 2.2.1
12325
+ */
12326
+ // 'public' => 1,
12327
+ 'archived' => 0,
12328
+ 'mature' => 0,
12329
+ 'spam' => 0,
12330
+ 'deleted' => 0,
12331
+ );
12332
+
12333
+ if ( function_exists( 'get_sites' ) ) {
12334
+ // For WP 4.6 and above.
12335
+ return get_sites( $args );
12336
+ } else if ( function_exists( 'wp_get_sites' ) ) {
12337
+ // For WP 3.7 to WP 4.5.
12338
+ return wp_get_sites( $args );
12339
+ } else {
12340
+ // For WP 3.6 and below.
12341
+ return get_blog_list( 0, 'all' );
12342
+ }
12343
+ }
12344
+
12345
+ /**
12346
+ * Checks if a given blog is active.
12347
+ *
12348
+ * @author Vova Feldman (@svovaf)
12349
+ * @since 2.0.0
12350
+ *
12351
+ * @param $blog_id
12352
+ *
12353
+ * @return bool
12354
+ */
12355
+ private static function is_site_active( $blog_id ) {
12356
+ global $wpdb;
12357
+
12358
+ $blog_info = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d", $blog_id ) );
12359
+
12360
+ if ( ! is_object( $blog_info ) ) {
12361
+ return false;
12362
+ }
12363
+
12364
+ return (
12365
+ true == $blog_info->public &&
12366
+ false == $blog_info->archived &&
12367
+ false == $blog_info->mature &&
12368
+ false == $blog_info->spam &&
12369
+ false == $blog_info->deleted
12370
+ );
12371
+ }
12372
+
12373
+ /**
12374
+ * Get a mapping between the site addresses to their blog IDs.
12375
+ *
12376
+ * @author Vova Feldman (@svovaf)
12377
+ * @since 2.0.0
12378
+ *
12379
+ * @return array {
12380
+ * @key string Site address without protocol with a trailing slash.
12381
+ * @value int Site's blog ID.
12382
+ * }
12383
+ */
12384
+ private function get_address_to_blog_map() {
12385
+ $sites = self::get_sites();
12386
+
12387
+ // Map site addresses to their blog IDs.
12388
+ $address_to_blog_map = array();
12389
+ foreach ( $sites as $site ) {
12390
+ $blog_id = self::get_site_blog_id( $site );
12391
+ $address = trailingslashit( fs_strip_url_protocol( get_site_url( $blog_id ) ) );
12392
+ $address_to_blog_map[ $address ] = $blog_id;
12393
+ }
12394
+
12395
+ return $address_to_blog_map;
12396
+ }
12397
+
12398
+ /**
12399
+ * Get a mapping between the site addresses to their blog IDs.
12400
+ *
12401
+ * @author Vova Feldman (@svovaf)
12402
+ * @since 2.0.0
12403
+ *
12404
+ * @return array {
12405
+ * @key int Site's blog ID.
12406
+ * @value FS_Site Associated install.
12407
+ * }
12408
+ */
12409
+ function get_blog_install_map() {
12410
+ $sites = self::get_sites();
12411
+
12412
+ // Map site blog ID to its install.
12413
+ $install_map = array();
12414
+
12415
+ foreach ( $sites as $site ) {
12416
+ $blog_id = self::get_site_blog_id( $site );
12417
+ $install = $this->get_install_by_blog_id( $blog_id );
12418
+
12419
+ if ( is_object( $install ) ) {
12420
+ $install_map[ $blog_id ] = $install;
12421
+ }
12422
+ }
12423
+
12424
+ return $install_map;
12425
+ }
12426
+
12427
+ /**
12428
+ * Gets a map of module IDs that the given user has opted-in to.
12429
+ *
12430
+ * @author Leo Fajardo (@leorw)
12431
+ * @since 2.1.0
12432
+ *
12433
+ * @param number $fs_user_id
12434
+ *
12435
+ * @return array {
12436
+ * @key number $plugin_id
12437
+ * @value bool Always true.
12438
+ * }
12439
+ */
12440
+ private static function get_user_opted_in_module_ids_map( $fs_user_id ) {
12441
+ self::$_static_logger->entrance();
12442
+
12443
+ if ( ! is_multisite() ) {
12444
+ $installs = array_merge(
12445
+ self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN ),
12446
+ self::get_all_sites( WP_FS__MODULE_TYPE_THEME )
12447
+ );
12448
+ } else {
12449
+ $sites = self::get_sites();
12450
+
12451
+ $installs = array();
12452
+ foreach ( $sites as $site ) {
12453
+ $blog_id = self::get_site_blog_id( $site );
12454
+
12455
+ $installs = array_merge(
12456
+ $installs,
12457
+ self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN, $blog_id ),
12458
+ self::get_all_sites( WP_FS__MODULE_TYPE_THEME, $blog_id )
12459
+ );
12460
+ }
12461
+ }
12462
+
12463
+ $module_ids_map = array();
12464
+ foreach ( $installs as $install ) {
12465
+ if ( is_object( $install ) &&
12466
+ FS_Site::is_valid_id( $install->id ) &&
12467
+ FS_User::is_valid_id( $install->user_id ) &&
12468
+ ( $install->user_id == $fs_user_id )
12469
+ ) {
12470
+ $module_ids_map[ $install->plugin_id ] = true;
12471
+ }
12472
+ }
12473
+
12474
+ return $module_ids_map;
12475
+ }
12476
+
12477
+ /**
12478
+ * @author Leo Fajardo (@leorw)
12479
+ *
12480
+ * @return null|array {
12481
+ * 'install' => FS_Site Module's install,
12482
+ * 'blog_id' => string The associated blog ID.
12483
+ * }
12484
+ */
12485
+ private function find_first_install() {
12486
+ $sites = self::get_sites();
12487
+
12488
+ foreach ( $sites as $site ) {
12489
+ $blog_id = self::get_site_blog_id( $site );
12490
+ $install = $this->get_install_by_blog_id( $blog_id );
12491
+
12492
+ if ( is_object( $install ) ) {
12493
+ return array(
12494
+ 'install' => $install,
12495
+ 'blog_id' => $blog_id
12496
+ );
12497
+ }
12498
+ }
12499
+
12500
+ return null;
12501
+ }
12502
+
12503
+ /**
12504
+ * Switches the Freemius site level context to a specified blog.
12505
+ *
12506
+ * @author Vova Feldman (@svovaf)
12507
+ * @since 2.0.0
12508
+ *
12509
+ * @param int $blog_id
12510
+ * @param FS_Site $install
12511
+ */
12512
+ function switch_to_blog( $blog_id, FS_Site $install = null ) {
12513
+ if ( $blog_id == $this->_context_is_network_or_blog_id ) {
12514
+ return;
12515
+ }
12516
+
12517
+ switch_to_blog( $blog_id );
12518
+ $this->_context_is_network_or_blog_id = $blog_id;
12519
+
12520
+ self::$_accounts->set_site_blog_context( $blog_id );
12521
+ $this->_storage->set_site_blog_context( $blog_id );
12522
+ $this->_storage->set_network_active( true, $this->is_delegated_connection( $blog_id ) );
12523
+
12524
+ $this->_site = is_object( $install ) ?
12525
+ $install :
12526
+ $this->get_install_by_blog_id( $blog_id );
12527
+
12528
+ $this->_user = false;
12529
+ $this->_licenses = false;
12530
+ $this->_license = null;
12531
+
12532
+ if ( is_object( $this->_site ) ) {
12533
+ // Try to fetch user from install.
12534
+ $this->_user = self::_get_user_by_id( $this->_site->user_id );
12535
+
12536
+ if ( ! is_object( $this->_user ) &&
12537
+ FS_User::is_valid_id( $this->_storage->prev_user_id )
12538
+ ) {
12539
+ // Try to fetch previously saved user.
12540
+ $this->_user = self::_get_user_by_id( $this->_storage->prev_user_id );
12541
+
12542
+ if ( ! is_object( $this->_user ) ) {
12543
+ // Fallback to network's user.
12544
+ $this->_user = $this->get_network_user();
12545
+ }
12546
+ }
12547
+
12548
+ $all_plugin_licenses = self::get_all_licenses( $this->_module_id );
12549
+
12550
+ if ( ! empty( $all_plugin_licenses ) ) {
12551
+ if ( ! FS_Plugin_License::is_valid_id( $this->_site->license_id ) ) {
12552
+ $this->_license = null;
12553
+ } else {
12554
+ $license_found = false;
12555
+ foreach ( $all_plugin_licenses as $license ) {
12556
+ if ( $license->id == $this->_site->license_id ) {
12557
+ // License found.
12558
+ $this->_license = $license;
12559
+ $license_found = true;
12560
+ break;
12561
+ }
12562
+ }
12563
+
12564
+ if ( $license_found ) {
12565
+ $this->link_license_2_user( $this->_license->id, $this->_user->id );
12566
+ }
12567
+ }
12568
+
12569
+ $this->_licenses = $this->get_user_licenses( $this->_user->id );
12570
+ }
12571
+ }
12572
+
12573
+ unset( $this->_site_api );
12574
+ unset( $this->_user_api );
12575
+ }
12576
+
12577
+ /**
12578
+ * Restore the blog context to the blog that originally loaded the module.
12579
+ *
12580
+ * @author Vova Feldman (@svovaf)
12581
+ * @since 2.0.0
12582
+ */
12583
+ function restore_current_blog() {
12584
+ $this->switch_to_blog( $this->_blog_id );
12585
+ }
12586
+
12587
+ /**
12588
+ * @author Vova Feldman (@svovaf)
12589
+ * @since 2.0.0
12590
+ *
12591
+ * @param array|WP_Site $site
12592
+ *
12593
+ * @return int
12594
+ */
12595
+ static function get_site_blog_id( &$site ) {
12596
+ return ( $site instanceof WP_Site ) ?
12597
+ $site->blog_id :
12598
+ ( is_object( $site ) && isset( $site->userblog_id ) ?
12599
+ $site->userblog_id :
12600
+ $site['blog_id'] );
12601
+ }
12602
+
12603
+ /**
12604
+ * @author Leo Fajardo (@leorw)
12605
+ * @since 2.0.0
12606
+ *
12607
+ * @param array|WP_Site|null $site
12608
+ *
12609
+ * @return array
12610
+ */
12611
+ function get_site_info( $site = null ) {
12612
+ $this->_logger->entrance();
12613
+
12614
+ $switched = false;
12615
+
12616
+ if ( is_null( $site ) ) {
12617
+ $url = get_site_url();
12618
+ $name = get_bloginfo( 'name' );
12619
+ $blog_id = null;
12620
+ } else {
12621
+ $blog_id = self::get_site_blog_id( $site );
12622
+
12623
+ if ( get_current_blog_id() != $blog_id ) {
12624
+ switch_to_blog( $blog_id );
12625
+ $switched = true;
12626
+ }
12627
+
12628
+ if ( $site instanceof WP_Site ) {
12629
+ $url = $site->siteurl;
12630
+ $name = $site->blogname;
12631
+ } else {
12632
+ $url = get_site_url( $blog_id );
12633
+ $name = get_bloginfo( 'name' );
12634
+ }
12635
+ }
12636
+
12637
+ $info = array(
12638
+ 'uid' => $this->get_anonymous_id( $blog_id ),
12639
+ 'url' => $url,
12640
+ 'title' => $name,
12641
+ 'language' => get_bloginfo( 'language' ),
12642
+ 'charset' => get_bloginfo( 'charset' ),
12643
+ );
12644
+
12645
+ if ( is_numeric( $blog_id ) ) {
12646
+ $info['blog_id'] = $blog_id;
12647
+ }
12648
+
12649
+ if ( $switched ) {
12650
+ restore_current_blog();
12651
+ }
12652
+
12653
+ return $info;
12654
+ }
12655
+
12656
+ /**
12657
+ * Load the module's install based on the blog ID.
12658
+ *
12659
+ * @author Vova Feldman (@svovaf)
12660
+ * @since 2.0.0
12661
+ *
12662
+ * @param int|null $blog_id
12663
+ *
12664
+ * @return FS_Site
12665
+ */
12666
+ function get_install_by_blog_id( $blog_id = null ) {
12667
+ $installs = self::get_all_sites( $this->_module_type, $blog_id );
12668
+ $install = isset( $installs[ $this->_slug ] ) ? $installs[ $this->_slug ] : null;
12669
+
12670
+ if ( is_object( $install ) &&
12671
+ is_numeric( $install->id ) &&
12672
+ is_numeric( $install->user_id ) &&
12673
+ FS_Plugin_Plan::is_valid_id( $install->plan_id )
12674
+ ) {
12675
+ // Load site.
12676
+ $install = clone $install;
12677
+ }
12678
+
12679
+ return $install;
12680
+ }
12681
+
12682
+ /**
12683
+ * Check if module is installed on a specified site.
12684
+ *
12685
+ * @author Vova Feldman (@svovaf)
12686
+ * @since 2.0.0
12687
+ *
12688
+ * @param int|null $blog_id
12689
+ *
12690
+ * @return bool
12691
+ */
12692
+ function is_installed_on_site( $blog_id = null ) {
12693
+ $installs = self::get_all_sites( $this->_module_type, $blog_id );
12694
+ $install = isset( $installs[ $this->_slug ] ) ? $installs[ $this->_slug ] : null;
12695
+
12696
+ return (
12697
+ is_object( $install ) &&
12698
+ is_numeric( $install->id ) &&
12699
+ is_numeric( $install->user_id ) &&
12700
+ FS_Plugin_Plan::is_valid_id( $install->plan_id )
12701
+ );
12702
+ }
12703
+
12704
+ /**
12705
+ * Check if super-admin connected at least one site via the network opt-in.
12706
+ *
12707
+ * @author Vova Feldman (@svovaf)
12708
+ * @since 2.0.0
12709
+ *
12710
+ * @return bool
12711
+ */
12712
+ function is_network_registered() {
12713
+ if ( ! $this->_is_network_active ) {
12714
+ return false;
12715
+ }
12716
+
12717
+ return FS_User::is_valid_id( $this->_storage->network_user_id );
12718
+ }
12719
+
12720
+ /**
12721
+ * Returns the main user associated with the network.
12722
+ *
12723
+ * @author Vova Feldman (@svovaf)
12724
+ * @since 2.0.0
12725
+ *
12726
+ * @return FS_User
12727
+ */
12728
+ function get_network_user() {
12729
+ if ( ! $this->_is_network_active ) {
12730
+ return null;
12731
+ }
12732
+
12733
+ return FS_User::is_valid_id( $this->_storage->network_user_id ) ?
12734
+ self::_get_user_by_id( $this->_storage->network_user_id ) :
12735
+ null;
12736
+ }
12737
+
12738
+ /**
12739
+ * Returns the current context user or the network's main user.
12740
+ *
12741
+ * @author Vova Feldman (@svovaf)
12742
+ * @since 2.0.0
12743
+ *
12744
+ * @return FS_User
12745
+ */
12746
+ function get_current_or_network_user() {
12747
+ return ( $this->_user instanceof FS_User ) ?
12748
+ $this->_user :
12749
+ $this->get_network_user();
12750
+ }
12751
+
12752
+ /**
12753
+ * Returns the main install associated with the network.
12754
+ *
12755
+ * @author Vova Feldman (@svovaf)
12756
+ * @since 2.0.0
12757
+ *
12758
+ * @return FS_Site
12759
+ */
12760
+ function get_network_install() {
12761
+ if ( ! $this->_is_network_active ) {
12762
+ return null;
12763
+ }
12764
+
12765
+ return FS_Site::is_valid_id( $this->_storage->network_install_blog_id ) ?
12766
+ $this->get_install_by_blog_id( $this->_storage->network_install_blog_id ) :
12767
+ null;
12768
+ }
12769
+
12770
+ /**
12771
+ * Returns the blog ID that is associated with the main install.
12772
+ *
12773
+ * @author Leo Fajardo (@leorw)
12774
+ * @since 2.0.0
12775
+ *
12776
+ * @return int|null
12777
+ */
12778
+ function get_network_install_blog_id() {
12779
+ if ( ! $this->_is_network_active ) {
12780
+ return null;
12781
+ }
12782
+
12783
+ return FS_Site::is_valid_id( $this->_storage->network_install_blog_id ) ?
12784
+ $this->_storage->network_install_blog_id :
12785
+ null;
12786
+ }
12787
+
12788
+ /**
12789
+ * Returns the current context install or the network's main install.
12790
+ *
12791
+ * @author Vova Feldman (@svovaf)
12792
+ * @since 2.0.0
12793
+ *
12794
+ * @return FS_Site
12795
+ */
12796
+ function get_current_or_network_install() {
12797
+ return ( $this->_site instanceof FS_Site ) ?
12798
+ $this->_site :
12799
+ $this->get_network_install();
12800
+ }
12801
+
12802
+ /**
12803
+ * Check if executing a site level action from the network level admin.
12804
+ *
12805
+ * @author Vova Feldman (@svovaf)
12806
+ * @since 2.0.0
12807
+ *
12808
+ * @return false|int If yes, return the requested blog ID.
12809
+ */
12810
+ private function is_network_level_site_specific_action() {
12811
+ if ( ! $this->_is_network_active ) {
12812
+ return false;
12813
+ }
12814
+
12815
+ if ( ! fs_is_network_admin() ) {
12816
+ return false;
12817
+ }
12818
+
12819
+ $blog_id = fs_request_get( 'blog_id', '' );
12820
+
12821
+ return is_numeric( $blog_id ) ? $blog_id : false;
12822
+ }
12823
+
12824
+ /**
12825
+ * Check if executing an action from the network level admin.
12826
+ *
12827
+ * @author Vova Feldman (@svovaf)
12828
+ * @since 2.0.0
12829
+ *
12830
+ * @return bool
12831
+ */
12832
+ private function is_network_level_action() {
12833
+ return ( $this->_is_network_active && fs_is_network_admin() );
12834
+ }
12835
+
12836
+ /**
12837
+ * Needs to be executed after site deactivation, archive, deletion, or flag as spam.
12838
+ * 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.
12839
+ *
12840
+ * @author Vova Feldman (@svovaf)
12841
+ * @since 2.0.0
12842
+ *
12843
+ * @param int $context_blog_id
12844
+ */
12845
+ private function update_multisite_data_after_site_deactivation( $context_blog_id = 0 ) {
12846
+ $this->_logger->entrance();
12847
+
12848
+ if ( $this->_is_network_active ) {
12849
+ if ( $context_blog_id == $this->_storage->network_install_blog_id ) {
12850
+ $installs_map = $this->get_blog_install_map();
12851
+
12852
+ foreach ( $installs_map as $blog_id => $install ) {
12853
+ /**
12854
+ * @var FS_Site $install
12855
+ */
12856
+ if ( $context_blog_id == $blog_id ) {
12857
+ continue;
12858
+ }
12859
+
12860
+ if ( $install->user_id != $this->_storage->network_user_id ) {
12861
+ continue;
12862
+ }
12863
+
12864
+ // Switch reference to a blog that is opted-in and belong to the same super-admin.
12865
+ $this->_storage->network_install_blog_id = $blog_id;
12866
+ break;
12867
+ }
12868
+ }
12869
+ }
12870
+
12871
+ if ( $this->is_sync_cron_scheduled() &&
12872
+ $context_blog_id == $this->get_sync_cron_blog_id()
12873
+ ) {
12874
+ $this->schedule_sync_cron( WP_FS__SCRIPT_START_TIME, true, $context_blog_id );
12875
+ }
12876
+
12877
+ if ( $this->is_install_sync_scheduled() &&
12878
+ $context_blog_id == $this->get_install_sync_cron_blog_id()
12879
+ ) {
12880
+ $this->schedule_install_sync( $context_blog_id );
12881
+ }
12882
+ }
12883
+
12884
+ /**
12885
+ * Executed after site deactivation, archive, or flag as spam.
12886
+ *
12887
+ * @author Vova Feldman (@svovaf)
12888
+ * @since 2.0.0
12889
+ *
12890
+ * @param int $context_blog_id
12891
+ */
12892
+ public function _after_site_deactivated_callback( $context_blog_id = 0 ) {
12893
+ $this->_logger->entrance();
12894
+
12895
+ $install = $this->get_install_by_blog_id( $context_blog_id );
12896
+
12897
+ if ( ! is_object( $install ) ) {
12898
+ // Site not connected.
12899
+ return;
12900
+ }
12901
+
12902
+ $this->update_multisite_data_after_site_deactivation( $context_blog_id );
12903
+
12904
+ $current_blog_id = get_current_blog_id();
12905
+
12906
+ $this->switch_to_blog( $context_blog_id );
12907
+
12908
+ // Send deactivation event.
12909
+ $this->sync_install( array(
12910
+ 'is_active' => false,
12911
+ ) );
12912
+
12913
+ $this->switch_to_blog( $current_blog_id );
12914
+ }
12915
+
12916
+ /**
12917
+ * Executed after site deletion.
12918
+ *
12919
+ * @author Vova Feldman (@svovaf)
12920
+ * @since 2.0.0
12921
+ *
12922
+ * @param int $context_blog_id
12923
+ * @param bool $drop True if site's database tables should be dropped. Default is false.
12924
+ */
12925
+ public function _after_site_deleted_callback( $context_blog_id = 0, $drop = false ) {
12926
+ $this->_logger->entrance();
12927
+
12928
+ $install = $this->get_install_by_blog_id( $context_blog_id );
12929
+
12930
+ if ( ! is_object( $install ) ) {
12931
+ // Site not connected.
12932
+ return;
12933
+ }
12934
+
12935
+ $this->update_multisite_data_after_site_deactivation( $context_blog_id );
12936
+
12937
+ $current_blog_id = get_current_blog_id();
12938
+
12939
+ $this->switch_to_blog( $context_blog_id );
12940
+
12941
+ if ( $drop ) {
12942
+ // Delete install if dropping site DB.
12943
+ $this->delete_account_event();
12944
+ } else {
12945
+ // Send deactivation event.
12946
+ $this->sync_install( array(
12947
+ 'is_active' => false,
12948
+ ) );
12949
+ }
12950
+
12951
+ $this->switch_to_blog( $current_blog_id );
12952
+ }
12953
+
12954
+ /**
12955
+ * Executed after site re-activation.
12956
+ *
12957
+ * @author Vova Feldman (@svovaf)
12958
+ * @since 2.0.0
12959
+ *
12960
+ * @param int $context_blog_id
12961
+ */
12962
+ public function _after_site_reactivated_callback( $context_blog_id = 0 ) {
12963
+ $this->_logger->entrance();
12964
+
12965
+ $install = $this->get_install_by_blog_id( $context_blog_id );
12966
+
12967
+ if ( ! is_object( $install ) ) {
12968
+ // Site not connected.
12969
+ return;
12970
+ }
12971
+
12972
+ if ( ! self::is_site_active( $context_blog_id ) ) {
12973
+ // Site not yet active (can be in spam mode, archived, deleted...).
12974
+ return;
12975
+ }
12976
+
12977
+ $current_blog_id = get_current_blog_id();
12978
+
12979
+ $this->switch_to_blog( $context_blog_id );
12980
+
12981
+ // Send re-activation event.
12982
+ $this->sync_install( array(
12983
+ 'is_active' => true,
12984
+ ) );
12985
+
12986
+ $this->switch_to_blog( $current_blog_id );
12987
+ }
12988
+
12989
+ #endregion Multisite
12990
+
12991
+ /**
12992
+ * @author Leo Fajardo (@leorw)
12993
+ *
12994
+ * @param string $path
12995
+ * @param string $scheme
12996
+ * @param bool $network
12997
+ *
12998
+ * @return string
12999
+ */
13000
+ private function admin_url( $path = '', $scheme = 'admin', $network = true ) {
13001
+ return ( $this->_is_network_active && $network ) ?
13002
+ network_admin_url( $path, $scheme ) :
13003
+ admin_url( $path, $scheme );
13004
+ }
13005
+
13006
+ /**
13007
+ * Check if currently in a specified admin page.
13008
+ *
13009
+ * @author Vova Feldman (@svovaf)
13010
+ * @since 1.2.2.7
13011
+ *
13012
+ * @param string $page
13013
+ *
13014
+ * @return bool
13015
+ */
13016
+ function is_admin_page( $page ) {
13017
+ return ( $this->_menu->get_slug( $page ) === fs_request_get( 'page', '', 'get' ) );
13018
+ }
13019
+
13020
+ /**
13021
+ * Get module's main admin setting page URL.
13022
+ *
13023
+ * @author Vova Feldman (@svovaf)
13024
+ * @since 1.2.2.7
13025
+ *
13026
+ * @return string
13027
+ */
13028
+ function main_menu_url() {
13029
+ return $this->_menu->main_menu_url();
13030
+ }
13031
+
13032
+ /**
13033
+ * Check if currently on the theme's setting page or
13034
+ * on any of the Freemius added pages (via tabs).
13035
+ *
13036
+ * @author Vova Feldman (@svovaf)
13037
+ * @since 1.2.2.7
13038
+ *
13039
+ * @return bool
13040
+ */
13041
+ function is_theme_settings_page() {
13042
+ return fs_starts_with(
13043
+ fs_request_get( 'page', '', 'get' ),
13044
+ $this->_menu->get_slug()
13045
+ );
13046
+ }
13047
+
13048
+ /**
13049
+ * Plugin's account page + sync license URL.
13050
+ *
13051
+ * @author Vova Feldman (@svovaf)
13052
+ * @since 1.1.9.1
13053
+ *
13054
+ * @param bool|number $plugin_id
13055
+ * @param bool $add_action_nonce
13056
+ * @param array $params
13057
+ *
13058
+ * @return string
13059
+ */
13060
+ function _get_sync_license_url( $plugin_id = false, $add_action_nonce = true, $params = array() ) {
13061
+ if ( is_numeric( $plugin_id ) ) {
13062
+ $params['plugin_id'] = $plugin_id;
13063
+ }
13064
+
13065
+ return $this->get_account_url(
13066
+ $this->get_unique_affix() . '_sync_license',
13067
+ $params,
13068
+ $add_action_nonce
13069
+ );
13070
+ }
13071
+
13072
+ /**
13073
+ * Plugin's account URL.
13074
+ *
13075
+ * @author Vova Feldman (@svovaf)
13076
+ * @since 1.0.4
13077
+ *
13078
+ * @param bool|string $action
13079
+ * @param array $params
13080
+ *
13081
+ * @param bool $add_action_nonce
13082
+ *
13083
+ * @return string
13084
+ */
13085
+ function get_account_url( $action = false, $params = array(), $add_action_nonce = true ) {
13086
+ if ( is_string( $action ) ) {
13087
+ $params['fs_action'] = $action;
13088
+ }
13089
+
13090
+ self::require_pluggable_essentials();
13091
+
13092
+ return ( $add_action_nonce && is_string( $action ) ) ?
13093
+ fs_nonce_url( $this->_get_admin_page_url( 'account', $params ), $action ) :
13094
+ $this->_get_admin_page_url( 'account', $params );
13095
+ }
13096
+
13097
+ /**
13098
+ * @author Vova Feldman (@svovaf)
13099
+ * @since 1.2.0
13100
+ *
13101
+ * @param string $tab
13102
+ * @param bool $action
13103
+ * @param array $params
13104
+ * @param bool $add_action_nonce
13105
+ *
13106
+ * @return string
13107
+ *
13108
+ * @uses get_account_url()
13109
+ */
13110
+ function get_account_tab_url( $tab, $action = false, $params = array(), $add_action_nonce = true ) {
13111
+ $params['tab'] = $tab;
13112
+
13113
+ return $this->get_account_url( $action, $params, $add_action_nonce );
13114
+ }
13115
+
13116
+ /**
13117
+ * Plugin's account URL.
13118
+ *
13119
+ * @author Vova Feldman (@svovaf)
13120
+ * @since 1.0.4
13121
+ *
13122
+ * @param bool|string $topic
13123
+ * @param bool|string $message
13124
+ *
13125
+ * @return string
13126
+ */
13127
+ function contact_url( $topic = false, $message = false ) {
13128
+ $params = array();
13129
+ if ( is_string( $topic ) ) {
13130
+ $params['topic'] = $topic;
13131
+ }
13132
+ if ( is_string( $message ) ) {
13133
+ $params['message'] = $message;
13134
+ }
13135
+
13136
+ if ( $this->is_addon() ) {
13137
+ $params['addon_id'] = $this->get_id();
13138
+
13139
+ return $this->get_parent_instance()->_get_admin_page_url( 'contact', $params );
13140
+ } else {
13141
+ return $this->_get_admin_page_url( 'contact', $params );
13142
+ }
13143
+ }
13144
+
13145
+ /**
13146
+ * Add-on direct info URL.
13147
+ *
13148
+ * @author Vova Feldman (@svovaf)
13149
+ * @since 1.1.0
13150
+ *
13151
+ * @param string $slug
13152
+ *
13153
+ * @return string
13154
+ */
13155
+ function addon_url( $slug ) {
13156
+ return $this->_get_admin_page_url( 'addons', array(
13157
+ 'slug' => $slug
13158
+ ) );
13159
+ }
13160
+
13161
+ /* Logger
13162
+ ------------------------------------------------------------------------------------------------------------------*/
13163
+ /**
13164
+ * @param string $id
13165
+ * @param bool $prefix_slug
13166
+ *
13167
+ * @return FS_Logger
13168
+ */
13169
+ function get_logger( $id = '', $prefix_slug = true ) {
13170
+ return FS_Logger::get_logger( ( $prefix_slug ? $this->_slug : '' ) . ( ( ! $prefix_slug || empty( $id ) ) ? '' : '_' ) . $id );
13171
+ }
13172
+
13173
+ /**
13174
+ * Note: This method is used externally so don't delete it.
13175
+ *
13176
+ * @param $id
13177
+ * @param bool $load_options
13178
+ * @param bool $prefix_slug
13179
+ *
13180
+ * @return FS_Option_Manager
13181
+ */
13182
+ function get_options_manager( $id, $load_options = false, $prefix_slug = true ) {
13183
+ return FS_Option_Manager::get_manager( ( $prefix_slug ? $this->_slug : '' ) . ( ( ! $prefix_slug || empty( $id ) ) ? '' : '_' ) . $id, $load_options );
13184
+ }
13185
+
13186
+ /* Security
13187
+ ------------------------------------------------------------------------------------------------------------------*/
13188
+ private static function _encrypt( $str ) {
13189
+ if ( is_null( $str ) ) {
13190
+ return null;
13191
+ }
13192
+
13193
+ /**
13194
+ * The encrypt/decrypt functions are used to protect
13195
+ * the user from messing up with some of the sensitive
13196
+ * data stored for the module as a JSON in the database.
13197
+ *
13198
+ * I used the same suggested hack by the theme review team.
13199
+ * For more details, look at the function `Base64UrlDecode()`
13200
+ * in `./sdk/FreemiusBase.php`.
13201
+ *
13202
+ * @todo Remove this hack once the base64 error is removed from the Theme Check.
13203
+ *
13204
+ * @author Vova Feldman (@svovaf)
13205
+ * @since 1.2.2
13206
+ */
13207
+ $fn = 'base64' . '_encode';
13208
+
13209
+ return $fn( $str );
13210
+ }
13211
+
13212
+ static function _decrypt( $str ) {
13213
+ if ( is_null( $str ) ) {
13214
+ return null;
13215
+ }
13216
+
13217
+ /**
13218
+ * The encrypt/decrypt functions are used to protect
13219
+ * the user from messing up with some of the sensitive
13220
+ * data stored for the module as a JSON in the database.
13221
+ *
13222
+ * I used the same suggested hack by the theme review team.
13223
+ * For more details, look at the function `Base64UrlDecode()`
13224
+ * in `./sdk/FreemiusBase.php`.
13225
+ *
13226
+ * @todo Remove this hack once the base64 error is removed from the Theme Check.
13227
+ *
13228
+ * @author Vova Feldman (@svovaf)
13229
+ * @since 1.2.2
13230
+ */
13231
+ $fn = 'base64' . '_decode';
13232
+
13233
+ return $fn( $str );
13234
+ }
13235
+
13236
+ /**
13237
+ * @author Vova Feldman (@svovaf)
13238
+ * @since 1.0.5
13239
+ *
13240
+ * @param FS_Entity $entity
13241
+ *
13242
+ * @return FS_Entity Return an encrypted clone entity.
13243
+ */
13244
+ private static function _encrypt_entity( FS_Entity $entity ) {
13245
+ $clone = clone $entity;
13246
+ $props = get_object_vars( $entity );
13247
+
13248
+ foreach ( $props as $key => $val ) {
13249
+ $clone->{$key} = self::_encrypt( $val );
13250
+ }
13251
+
13252
+ return $clone;
13253
+ }
13254
+
13255
+ /**
13256
+ * @author Vova Feldman (@svovaf)
13257
+ * @since 1.0.5
13258
+ *
13259
+ * @param FS_Entity $entity
13260
+ *
13261
+ * @return FS_Entity Return an decrypted clone entity.
13262
+ */
13263
+ private static function decrypt_entity( FS_Entity $entity ) {
13264
+ $clone = clone $entity;
13265
+ $props = get_object_vars( $entity );
13266
+
13267
+ foreach ( $props as $key => $val ) {
13268
+ $clone->{$key} = self::_decrypt( $val );
13269
+ }
13270
+
13271
+ return $clone;
13272
+ }
13273
+
13274
+ /**
13275
+ * Tries to activate account based on POST params.
13276
+ *
13277
+ * @author Vova Feldman (@svovaf)
13278
+ * @since 1.0.2
13279
+ *
13280
+ * @deprecated Not in use, outdated.
13281
+ */
13282
+ function _activate_account() {
13283
+ if ( $this->is_registered() ) {
13284
+ // Already activated.
13285
+ return;
13286
+ }
13287
+
13288
+ self::_clean_admin_content_section();
13289
+
13290
+ if ( fs_request_is_action( 'activate' ) && fs_request_is_post() ) {
13291
+ // check_admin_referer( 'activate_' . $this->_plugin->public_key );
13292
+
13293
+ // Verify matching plugin details.
13294
+ if ( $this->_plugin->id != fs_request_get( 'plugin_id' ) || $this->_slug != fs_request_get( 'plugin_slug' ) ) {
13295
+ return;
13296
+ }
13297
+
13298
+ $user = new FS_User();
13299
+ $user->id = fs_request_get( 'user_id' );
13300
+ $user->public_key = fs_request_get( 'user_public_key' );
13301
+ $user->secret_key = fs_request_get( 'user_secret_key' );
13302
+ $user->email = fs_request_get( 'user_email' );
13303
+ $user->first = fs_request_get( 'user_first' );
13304
+ $user->last = fs_request_get( 'user_last' );
13305
+ $user->is_verified = fs_request_get_bool( 'user_is_verified' );
13306
+
13307
+ $site = new FS_Site();
13308
+ $site->id = fs_request_get( 'install_id' );
13309
+ $site->public_key = fs_request_get( 'install_public_key' );
13310
+ $site->secret_key = fs_request_get( 'install_secret_key' );
13311
+ $site->plan_id = fs_request_get( 'plan_id' );
13312
+
13313
+ $plans = array();
13314
+ $plans_data = json_decode( urldecode( fs_request_get( 'plans' ) ) );
13315
+ foreach ( $plans_data as $p ) {
13316
+ $plan = new FS_Plugin_Plan( $p );
13317
+ if ( $site->plan_id == $plan->id ) {
13318
+ $plan->title = fs_request_get( 'plan_title' );
13319
+ $plan->name = fs_request_get( 'plan_name' );
13320
+ }
13321
+
13322
+ $plans[] = $plan;
13323
+ }
13324
+
13325
+ $this->_set_account( $user, $site, $plans );
13326
+
13327
+ // Reload the page with the keys.
13328
+ fs_redirect( $this->_get_admin_page_url() );
13329
+ }
13330
+ }
13331
+
13332
+ /**
13333
+ * @author Vova Feldman (@svovaf)
13334
+ * @since 1.0.7
13335
+ *
13336
+ * @param string $email
13337
+ *
13338
+ * @return FS_User|false
13339
+ */
13340
+ static function _get_user_by_email( $email ) {
13341
+ self::$_static_logger->entrance();
13342
+
13343
+ $email = trim( strtolower( $email ) );
13344
+
13345
+ $users = self::get_all_users();
13346
+
13347
+ if ( is_array( $users ) ) {
13348
+ foreach ( $users as $user ) {
13349
+ if ( $email === trim( strtolower( $user->email ) ) ) {
13350
+ return $user;
13351
+ }
13352
+ }
13353
+ }
13354
+
13355
+ return false;
13356
+ }
13357
+
13358
+ #----------------------------------------------------------------------------------
13359
+ #region Account (Loading, Updates & Activation)
13360
+ #----------------------------------------------------------------------------------
13361
+
13362
+ /***
13363
+ * Load account information (user + site).
13364
+ *
13365
+ * @author Vova Feldman (@svovaf)
13366
+ * @since 1.0.1
13367
+ */
13368
+ private function _load_account() {
13369
+ $this->_logger->entrance();
13370
+
13371
+ $this->do_action( 'before_account_load' );
13372
+
13373
+ $users = self::get_all_users();
13374
+ $plans = self::get_all_plans( $this->_module_type );
13375
+
13376
+ if ( $this->_logger->is_on() && is_admin() ) {
13377
+ $this->_logger->log( 'users = ' . var_export( $users, true ) );
13378
+ $this->_logger->log( 'plans = ' . var_export( $plans, true ) );
13379
+ }
13380
+
13381
+ $site = fs_is_network_admin() ?
13382
+ $this->get_network_install() :
13383
+ $this->get_install_by_blog_id();
13384
+
13385
+ if ( fs_is_network_admin() &&
13386
+ ! is_object( $site ) &&
13387
+ FS_Site::is_valid_id( $this->_storage->network_install_blog_id )
13388
+ ) {
13389
+ $first_install = $this->find_first_install();
13390
+
13391
+ if ( is_null( $first_install ) ) {
13392
+ unset( $this->_storage->network_install_blog_id );
13393
+ } else {
13394
+ $site = $first_install['install'];
13395
+ $this->_storage->network_install_blog_id = $first_install['blog_id'];
13396
+ }
13397
+ }
13398
+
13399
+ if ( is_object( $site ) &&
13400
+ is_numeric( $site->id ) &&
13401
+ is_numeric( $site->user_id ) &&
13402
+ FS_Plugin_Plan::is_valid_id( $site->plan_id )
13403
+ ) {
13404
+ // Load site.
13405
+ $this->_site = $site;
13406
+
13407
+ // Load plans.
13408
+ $this->_plans = $plans[ $this->_slug ];
13409
+ if ( ! is_array( $this->_plans ) || empty( $this->_plans ) ) {
13410
+ $this->_sync_plans();
13411
+ } else {
13412
+ for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
13413
+ if ( $this->_plans[ $i ] instanceof FS_Plugin_Plan ) {
13414
+ $this->_plans[ $i ] = self::decrypt_entity( $this->_plans[ $i ] );
13415
+ } else {
13416
+ unset( $this->_plans[ $i ] );
13417
+ }
13418
+ }
13419
+ }
13420
+ }
13421
+
13422
+ $user = null;
13423
+ if ( fs_is_network_admin() && $this->_is_network_active ) {
13424
+ $user = $this->get_network_user();
13425
+ }
13426
+
13427
+ if ( is_object( $user ) ) {
13428
+ $this->_user = clone $user;
13429
+ } else if ( $this->_site ) {
13430
+ $user = self::_get_user_by_id( $this->_site->user_id );
13431
+
13432
+ if ( ! is_object( $user ) && FS_User::is_valid_id( $this->_storage->prev_user_id ) ) {
13433
+ /**
13434
+ * Try to load the previous owner. This recovery is used for the following use-case:
13435
+ * 1. Opt-in
13436
+ * 2. Cloning site1 to site2
13437
+ * 3. Ownership switch in site1 (same applies for site2)
13438
+ * 4. Install data sync on site2
13439
+ * 5. Now site2's install is associated with the new owner which does not exists locally.
13440
+ */
13441
+ $user = self::_get_user_by_id( $this->_storage->prev_user_id );
13442
+ }
13443
+
13444
+ if ( ! is_object( $user ) ) {
13445
+ /**
13446
+ * This is a special fault tolerance mechanism to handle a scenario that the user data is missing.
13447
+ */
13448
+ $user = $this->fetch_user_by_install();
13449
+ }
13450
+
13451
+ $this->_user = ( $user instanceof FS_User ) ?
13452
+ clone $user :
13453
+ null;
13454
+ }
13455
+
13456
+ if ( is_object( $this->_user ) ) {
13457
+ // Load licenses.
13458
+ $this->_licenses = $this->get_user_licenses( $this->_user->id );
13459
+ }
13460
+
13461
+ if ( is_object( $this->_site ) ) {
13462
+ $this->_license = $this->_get_license_by_id( $this->_site->license_id );
13463
+
13464
+ if ( $this->_site->version != $this->get_plugin_version() ) {
13465
+ // If stored install version is different than current installed plugin version,
13466
+ // then update plugin version event.
13467
+ $this->update_plugin_version_event();
13468
+ }
13469
+ }
13470
+
13471
+ if ( true === $this->_storage->require_license_activation &&
13472
+ ! fs_request_get_bool( 'require_license', true )
13473
+ ) {
13474
+ $this->_storage->require_license_activation = false;
13475
+ }
13476
+
13477
+ if ( $this->is_theme() ) {
13478
+ $this->_register_account_hooks();
13479
+ }
13480
+ }
13481
+
13482
+ /**
13483
+ * Special user recovery mechanism.
13484
+ *
13485
+ * @author Vova Feldman (@svovaf)
13486
+ * @since 2.0.0
13487
+ *
13488
+ * @return \FS_User|mixed
13489
+ */
13490
+ private function fetch_user_by_install() {
13491
+ $api = $this->get_api_site_scope();
13492
+
13493
+ $uid = $this->get_anonymous_id();
13494
+ $request_path = "/users/{$this->_site->user_id}.json?uid={$uid}";
13495
+
13496
+ $result = $api->get( $request_path, false, WP_FS__TIME_10_MIN_IN_SEC );
13497
+
13498
+ if ( $this->is_api_result_entity( $result ) ) {
13499
+ $user = new FS_User( $result );
13500
+ $this->_user = $user;
13501
+ $this->_store_user();
13502
+
13503
+ return $user;
13504
+ }
13505
+
13506
+ $error_code = FS_Api::get_error_code( $result );
13507
+
13508
+ if ( in_array( $error_code, array( 'invalid_unique_id', 'user_cannot_be_recovered' ) ) ) {
13509
+ /**
13510
+ * Those API errors will continue coming and are not recoverable with the
13511
+ * current site's data. Therefore, extend the API call's cached result to 7 days.
13512
+ */
13513
+ $api->update_cache_expiration( $request_path, WP_FS__TIME_WEEK_IN_SEC );
13514
+ }
13515
+
13516
+ return $result;
13517
+ }
13518
+
13519
+ /**
13520
+ * @author Vova Feldman (@svovaf)
13521
+ * @since 1.0.1
13522
+ *
13523
+ * @param FS_User $user
13524
+ * @param FS_Site $site
13525
+ * @param bool|array $plans
13526
+ */
13527
+ private function _set_account( FS_User $user, FS_Site $site, $plans = false ) {
13528
+ $site->user_id = $user->id;
13529
+
13530
+ $this->_site = $site;
13531
+ $this->_user = $user;
13532
+ if ( false !== $plans ) {
13533
+ $this->_plans = $plans;
13534
+ }
13535
+
13536
+ $this->send_install_update();
13537
+
13538
+ $this->_store_account();
13539
+
13540
+ }
13541
+
13542
+ /**
13543
+ * Get a sanitized array with the WordPress version, SDK version, and PHP version.
13544
+ * Each version is trimmed after the 16th char.
13545
+ *
13546
+ * @author Vova Feldman (@svovaf)
13547
+ * @since 2.2.1
13548
+ *
13549
+ * @return array
13550
+ */
13551
+ private function get_versions() {
13552
+ $versions = array();
13553
+ $versions['platform_version'] = get_bloginfo( 'version' );
13554
+ $versions['sdk_version'] = $this->version;
13555
+ $versions['programming_language_version'] = phpversion();
13556
+
13557
+ foreach ( $versions as $k => $version ) {
13558
+ if ( is_string( $versions[ $k ] ) && ! empty( $versions[ $k ] ) ) {
13559
+ $versions[ $k ] = substr( $versions[ $k ], 0, 16 );
13560
+ }
13561
+ }
13562
+
13563
+ return $versions;
13564
+ }
13565
+
13566
+ /**
13567
+ * @author Vova Feldman (@svovaf)
13568
+ * @since 1.1.7.4
13569
+ *
13570
+ * @param array $override_with
13571
+ * @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.
13572
+ *
13573
+ * @return array
13574
+ */
13575
+ function get_opt_in_params( $override_with = array(), $network_level_or_blog_id = null ) {
13576
+ $this->_logger->entrance();
13577
+
13578
+ $current_user = self::_get_current_wp_user();
13579
+
13580
+ $activation_action = $this->get_unique_affix() . '_activate_new';
13581
+ $return_url = $this->is_anonymous() ?
13582
+ // If skipped already, then return to the account page.
13583
+ $this->get_account_url( $activation_action, array(), false ) :
13584
+ // Return to the module's main page.
13585
+ $this->get_after_activation_url( 'after_connect_url', array( 'fs_action' => $activation_action ) );
13586
+
13587
+ $versions = $this->get_versions();
13588
+
13589
+ $params = array_merge( $versions, array(
13590
+ 'user_firstname' => $current_user->user_firstname,
13591
+ 'user_lastname' => $current_user->user_lastname,
13592
+ 'user_nickname' => $current_user->user_nicename,
13593
+ 'user_email' => $current_user->user_email,
13594
+ 'user_ip' => WP_FS__REMOTE_ADDR,
13595
+ 'plugin_slug' => $this->_slug,
13596
+ 'plugin_id' => $this->get_id(),
13597
+ 'plugin_public_key' => $this->get_public_key(),
13598
+ 'plugin_version' => $this->get_plugin_version(),
13599
+ 'return_url' => fs_nonce_url( $return_url, $activation_action ),
13600
+ 'account_url' => fs_nonce_url( $this->_get_admin_page_url(
13601
+ 'account',
13602
+ array( 'fs_action' => 'sync_user' )
13603
+ ), 'sync_user' ),
13604
+ 'is_premium' => $this->is_premium(),
13605
+ 'is_active' => true,
13606
+ 'is_uninstalled' => false,
13607
+ ) );
13608
+
13609
+ if ( true === $network_level_or_blog_id ) {
13610
+ if ( ! isset( $override_with['sites'] ) ) {
13611
+ $params['sites'] = array();
13612
+
13613
+ $sites = self::get_sites();
13614
+
13615
+ foreach ( $sites as $site ) {
13616
+ $blog_id = self::get_site_blog_id( $site );
13617
+ if ( ! $this->is_site_delegated_connection( $blog_id ) &&
13618
+ ! $this->is_installed_on_site( $blog_id )
13619
+ ) {
13620
+ $params['sites'][] = $this->get_site_info( $site );
13621
+ }
13622
+ }
13623
+ }
13624
+ } else {
13625
+ $site = is_numeric( $network_level_or_blog_id ) ?
13626
+ array( 'blog_id' => $network_level_or_blog_id ) :
13627
+ null;
13628
+
13629
+ $site = $this->get_site_info( $site );
13630
+
13631
+ $params = array_merge( $params, array(
13632
+ 'site_uid' => $site['uid'],
13633
+ 'site_url' => $site['url'],
13634
+ 'site_name' => $site['title'],
13635
+ 'language' => $site['language'],
13636
+ 'charset' => $site['charset'],
13637
+ ) );
13638
+ }
13639
+
13640
+ if ( $this->is_pending_activation() &&
13641
+ ! empty( $this->_storage->pending_license_key )
13642
+ ) {
13643
+ $params['license_key'] = $this->_storage->pending_license_key;
13644
+ }
13645
+
13646
+ if ( WP_FS__SKIP_EMAIL_ACTIVATION && $this->has_secret_key() ) {
13647
+ // Even though rand() is known for its security issues,
13648
+ // the timestamp adds another layer of protection.
13649
+ // It would be very hard for an attacker to get the secret key form here.
13650
+ // Plus, this should never run in production since the secret should never
13651
+ // be included in the production version.
13652
+ $params['ts'] = WP_FS__SCRIPT_START_TIME;
13653
+ $params['salt'] = md5( uniqid( rand() ) );
13654
+ $params['secure'] = md5(
13655
+ $params['ts'] .
13656
+ $params['salt'] .
13657
+ $this->get_secret_key()
13658
+ );
13659
+ }
13660
+
13661
+ return array_merge( $params, $override_with );
13662
+ }
13663
+
13664
+ /**
13665
+ * 1. If successful opt-in or pending activation returns the next page that the user should be redirected to.
13666
+ * 2. If there was an API error, return the API result.
13667
+ *
13668
+ * @author Vova Feldman (@svovaf)
13669
+ * @since 1.1.7.4
13670
+ *
13671
+ * @param string|bool $email
13672
+ * @param string|bool $first
13673
+ * @param string|bool $last
13674
+ * @param string|bool $license_key
13675
+ * @param bool $is_uninstall If "true", this means that the module is currently being uninstalled.
13676
+ * In this case, the user and site info will be sent to the server but no
13677
+ * data will be saved to the WP installation's database.
13678
+ * @param number|bool $trial_plan_id
13679
+ * @param bool $is_disconnected Whether or not to opt in without tracking.
13680
+ * @param null|bool $is_marketing_allowed
13681
+ * @param array $sites If network-level opt-in, an array of containing details of sites.
13682
+ *
13683
+ * @return string|object
13684
+ * @use WP_Error
13685
+ */
13686
+ function opt_in(
13687
+ $email = false,
13688
+ $first = false,
13689
+ $last = false,
13690
+ $license_key = false,
13691
+ $is_uninstall = false,
13692
+ $trial_plan_id = false,
13693
+ $is_disconnected = false,
13694
+ $is_marketing_allowed = null,
13695
+ $sites = array()
13696
+ ) {
13697
+ $this->_logger->entrance();
13698
+
13699
+ if ( false === $email ) {
13700
+ $current_user = self::_get_current_wp_user();
13701
+ $email = $current_user->user_email;
13702
+ }
13703
+
13704
+ /**
13705
+ * @since 1.2.1 If activating with license key, ignore the context-user
13706
+ * since the user will be automatically loaded from the license.
13707
+ */
13708
+ if ( empty( $license_key ) ) {
13709
+ // Clean up pending license if opt-ing in again.
13710
+ $this->_storage->remove( 'pending_license_key' );
13711
+
13712
+ if ( ! $is_uninstall ) {
13713
+ $fs_user = Freemius::_get_user_by_email( $email );
13714
+ if ( is_object( $fs_user ) && ! $this->is_pending_activation() ) {
13715
+ return $this->install_with_current_user(
13716
+ false,
13717
+ $trial_plan_id,
13718
+ $sites
13719
+ );
13720
+ }
13721
+ }
13722
+ }
13723
+
13724
+ $user_info = array();
13725
+ if ( ! empty( $email ) ) {
13726
+ $user_info['user_email'] = $email;
13727
+ }
13728
+ if ( ! empty( $first ) ) {
13729
+ $user_info['user_firstname'] = $first;
13730
+ }
13731
+ if ( ! empty( $last ) ) {
13732
+ $user_info['user_lastname'] = $last;
13733
+ }
13734
+
13735
+ if ( ! empty( $sites ) ) {
13736
+ $is_network = true;
13737
+
13738
+ $user_info['sites'] = $sites;
13739
+ } else {
13740
+ $is_network = false;
13741
+ }
13742
+
13743
+ $params = $this->get_opt_in_params( $user_info, $is_network );
13744
+
13745
+ $filtered_license_key = false;
13746
+ if ( is_string( $license_key ) ) {
13747
+ $filtered_license_key = $this->apply_filters( 'license_key', $license_key );
13748
+ $params['license_key'] = $filtered_license_key;
13749
+ } else if ( FS_Plugin_Plan::is_valid_id( $trial_plan_id ) ) {
13750
+ $params['trial_plan_id'] = $trial_plan_id;
13751
+ }
13752
+
13753
+ if ( $is_uninstall ) {
13754
+ $params['uninstall_params'] = array(
13755
+ 'reason_id' => $this->_storage->uninstall_reason->id,
13756
+ 'reason_info' => $this->_storage->uninstall_reason->info
13757
+ );
13758
+ }
13759
+
13760
+ if ( isset( $params['license_key'] ) ) {
13761
+ $fs_user = Freemius::_get_user_by_email( $email );
13762
+
13763
+ if ( is_object( $fs_user ) ) {
13764
+ /**
13765
+ * If opting in with a context license and the context WP Admin user already opted in
13766
+ * before from the current site, add the user context security params to avoid the
13767
+ * unnecessary email activation when the context license is owned by the same context user.
13768
+ *
13769
+ * @author Leo Fajardo (@leorw)
13770
+ * @since 1.2.3
13771
+ */
13772
+ $params = array_merge( $params, FS_Security::instance()->get_context_params(
13773
+ $fs_user,
13774
+ false,
13775
+ 'install_with_existing_user'
13776
+ ) );
13777
+ }
13778
+ }
13779
+
13780
+ if ( is_bool( $is_marketing_allowed ) ) {
13781
+ $params['is_marketing_allowed'] = $is_marketing_allowed;
13782
+ }
13783
+
13784
+ $params['is_disconnected'] = $is_disconnected;
13785
+ $params['format'] = 'json';
13786
+
13787
+ $request = array(
13788
+ 'method' => 'POST',
13789
+ 'body' => $params,
13790
+ 'timeout' => WP_FS__DEBUG_SDK ? 60 : 30,
13791
+ );
13792
+
13793
+ $url = $this->add_show_pending( WP_FS__ADDRESS . '/action/service/user/install/' );
13794
+ $response = self::safe_remote_post( $url, $request );
13795
+
13796
+ if ( is_wp_error( $response ) ) {
13797
+ /**
13798
+ * @var WP_Error $response
13799
+ */
13800
+ $result = new stdClass();
13801
+
13802
+ $error_code = $response->get_error_code();
13803
+ $error_type = str_replace( ' ', '', ucwords( str_replace( '_', ' ', $error_code ) ) );
13804
+
13805
+ $result->error = (object) array(
13806
+ 'type' => $error_type,
13807
+ 'message' => $response->get_error_message(),
13808
+ 'code' => $error_code,
13809
+ 'http' => 402
13810
+ );
13811
+
13812
+ return $result;
13813
+ }
13814
+
13815
+ // Module is being uninstalled, don't handle the returned data.
13816
+ if ( $is_uninstall ) {
13817
+ return true;
13818
+ }
13819
+
13820
+ /**
13821
+ * 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.
13822
+ *
13823
+ * @author Vova Feldman (@svovaf)
13824
+ * @since 1.2.3
13825
+ * @link https://themes.trac.wordpress.org/ticket/46134#comment:5
13826
+ * @link https://themes.trac.wordpress.org/ticket/46134#comment:9
13827
+ * @link https://themes.trac.wordpress.org/ticket/46134#comment:12
13828
+ * @link https://themes.trac.wordpress.org/ticket/46134#comment:14
13829
+ */
13830
+ $decoded = is_string( $response['body'] ) ?
13831
+ json_decode( $response['body'] ) :
13832
+ null;
13833
+
13834
+ if ( empty( $decoded ) ) {
13835
+ return false;
13836
+ }
13837
+
13838
+ if ( ! $this->is_api_result_object( $decoded ) ) {
13839
+ if ( ! empty( $params['license_key'] ) ) {
13840
+ // Pass the fully entered license key to the failure handler.
13841
+ $params['license_key'] = $license_key;
13842
+ }
13843
+
13844
+ return $is_uninstall ?
13845
+ $decoded :
13846
+ $this->apply_filters( 'after_install_failure', $decoded, $params );
13847
+ } else if ( isset( $decoded->pending_activation ) && $decoded->pending_activation ) {
13848
+ if ( $is_network ) {
13849
+ $site_ids = array();
13850
+ foreach ( $sites as $site ) {
13851
+ $site_ids[] = $site['blog_id'];
13852
+ }
13853
+
13854
+ /**
13855
+ * Store the sites so that they can be installed once the user has clicked on the activation link
13856
+ * in the email.
13857
+ *
13858
+ * @author Leo Fajardo (@leorw)
13859
+ */
13860
+ $this->_storage->pending_sites_info = array(
13861
+ 'blog_ids' => $site_ids,
13862
+ 'license_key' => $license_key,
13863
+ 'trial_plan_id' => $trial_plan_id
13864
+ );
13865
+ }
13866
+
13867
+ // Pending activation, add message.
13868
+ return $this->set_pending_confirmation(
13869
+ ( isset( $decoded->email ) ?
13870
+ $decoded->email :
13871
+ true ),
13872
+ false,
13873
+ $filtered_license_key,
13874
+ ! empty( $params['trial_plan_id'] )
13875
+ );
13876
+ } else if ( isset( $decoded->install_secret_key ) ) {
13877
+ return $this->install_with_new_user(
13878
+ $decoded->user_id,
13879
+ $decoded->user_public_key,
13880
+ $decoded->user_secret_key,
13881
+ ( isset( $decoded->is_marketing_allowed ) && ! is_null( $decoded->is_marketing_allowed ) ?
13882
+ $decoded->is_marketing_allowed :
13883
+ null ),
13884
+ $decoded->install_id,
13885
+ $decoded->install_public_key,
13886
+ $decoded->install_secret_key,
13887
+ false
13888
+ );
13889
+ } else if ( is_array( $decoded->installs ) ) {
13890
+ return $this->install_many_with_new_user(
13891
+ $decoded->user_id,
13892
+ $decoded->user_public_key,
13893
+ $decoded->user_secret_key,
13894
+ ( isset( $decoded->is_marketing_allowed ) && ! is_null( $decoded->is_marketing_allowed ) ?
13895
+ $decoded->is_marketing_allowed :
13896
+ null ),
13897
+ $decoded->installs,
13898
+ false
13899
+ );
13900
+ }
13901
+
13902
+ return $decoded;
13903
+ }
13904
+
13905
+ /**
13906
+ * Set user and site identities.
13907
+ *
13908
+ * @author Vova Feldman (@svovaf)
13909
+ * @since 1.0.9
13910
+ *
13911
+ * @param FS_User $user
13912
+ * @param FS_Site $site
13913
+ * @param bool $redirect
13914
+ * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will
13915
+ * redirect (or return a URL) to the account page with a special parameter to
13916
+ * trigger the auto installation processes.
13917
+ *
13918
+ * @return string If redirect is `false`, returns the next page the user should be redirected to.
13919
+ */
13920
+ function setup_account(
13921
+ FS_User $user,
13922
+ FS_Site $site,
13923
+ $redirect = true,
13924
+ $auto_install = false
13925
+ ) {
13926
+ return $this->setup_network_account(
13927
+ $user,
13928
+ array( $site ),
13929
+ $redirect,
13930
+ $auto_install,
13931
+ false
13932
+ );
13933
+ }
13934
+
13935
+ /**
13936
+ * Set user and site identities.
13937
+ *
13938
+ * @author Vova Feldman (@svovaf)
13939
+ * @since 2.0.0
13940
+ *
13941
+ * @param FS_User $user
13942
+ * @param FS_Site[] $installs
13943
+ * @param bool $redirect
13944
+ * @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.
13945
+ * @param bool $is_network_level_opt_in
13946
+ *
13947
+ * @return string If redirect is `false`, returns the next page the user should be redirected to.
13948
+ */
13949
+ function setup_network_account(
13950
+ FS_User $user,
13951
+ array $installs,
13952
+ $redirect = true,
13953
+ $auto_install = false,
13954
+ $is_network_level_opt_in = true
13955
+ ) {
13956
+ $first_install = $installs[0];
13957
+
13958
+ $this->_user = $user;
13959
+ $this->_site = $first_install;
13960
+
13961
+ $this->_sync_plans();
13962
+
13963
+ if ( $this->_storage->handle_gdpr_admin_notice &&
13964
+ $this->should_handle_gdpr_admin_notice() &&
13965
+ FS_GDPR_Manager::instance()->should_show_opt_in_notice()
13966
+ ) {
13967
+ /**
13968
+ * Clear user lock after an opt-in.
13969
+ */
13970
+ require_once WP_FS__DIR_INCLUDES . '/class-fs-user-lock.php';
13971
+ FS_User_Lock::instance()->unlock();
13972
+ }
13973
+
13974
+ if ( 1 < count( $installs ) ) {
13975
+ // Only network level opt-in can have more than one install.
13976
+ $is_network_level_opt_in = true;
13977
+ }
13978
+ // $is_network_level_opt_in = self::is_ajax_action_static( 'network_activate', $this->_module_id );
13979
+ // If Freemius was OFF before, turn it on.
13980
+ $this->turn_on();
13981
+
13982
+ if ( ! $this->_is_network_active || ! $is_network_level_opt_in ) {
13983
+ $this->_set_account( $user, $first_install );
13984
+
13985
+ $this->do_action( 'after_account_connection', $user, $first_install );
13986
+ } else {
13987
+ $this->_store_user();
13988
+
13989
+ // Map site addresses to their blog IDs.
13990
+ $address_to_blog_map = $this->get_address_to_blog_map();
13991
+
13992
+ $first_blog_id = null;
13993
+ $blog_2_install_map = array();
13994
+ foreach ( $installs as $install ) {
13995
+ $address = trailingslashit( fs_strip_url_protocol( $install->url ) );
13996
+ $blog_id = $address_to_blog_map[ $address ];
13997
+
13998
+ $this->_store_site( true, $blog_id, $install );
13999
+
14000
+ if ( is_null( $first_blog_id ) ) {
14001
+ $first_blog_id = $blog_id;
14002
+ }
14003
+
14004
+ $blog_2_install_map[ $blog_id ] = $install;
14005
+ }
14006
+
14007
+ if ( ! FS_User::is_valid_id( $this->_storage->network_user_id ) ||
14008
+ ! is_object( self::_get_user_by_id( $this->_storage->network_user_id ) )
14009
+ ) {
14010
+ // Store network user.
14011
+ $this->_storage->network_user_id = $this->_user->id;
14012
+ }
14013
+
14014
+ if ( ! FS_Site::is_valid_id( $this->_storage->network_install_blog_id ) ) {
14015
+ $this->_storage->network_install_blog_id = $first_blog_id;
14016
+ }
14017
+
14018
+ if ( count( $installs ) === count( $address_to_blog_map ) ) {
14019
+ // Super-admin opted-in for all sites in the network.
14020
+ $this->_storage->is_network_connected = true;
14021
+ }
14022
+
14023
+ $this->_store_licenses( false );
14024
+
14025
+ self::$_accounts->store();
14026
+
14027
+ // Don't sync the installs data on network upgrade
14028
+ if ( ! $this->network_upgrade_mode_completed() ) {
14029
+ $this->send_installs_update();
14030
+ }
14031
+
14032
+ // Switch install context back to the first install.
14033
+ $this->_site = $first_install;
14034
+
14035
+ $current_blog = get_current_blog_id();
14036
+
14037
+ foreach ( $blog_2_install_map as $blog_id => $install ) {
14038
+ $this->switch_to_blog( $blog_id );
14039
+
14040
+ $this->do_action( 'after_account_connection', $user, $install );
14041
+ }
14042
+
14043
+ $this->switch_to_blog( $current_blog );
14044
+
14045
+ $this->do_action( 'after_network_account_connection', $user, $blog_2_install_map );
14046
+ }
14047
+
14048
+ if ( is_numeric( $first_install->license_id ) ) {
14049
+ $this->_license = $this->_get_license_by_id( $first_install->license_id );
14050
+ }
14051
+
14052
+ $this->_admin_notices->remove_sticky( 'connect_account' );
14053
+
14054
+ if ( $this->is_pending_activation() || ! $this->has_settings_menu() ) {
14055
+ // Remove pending activation sticky notice (if still exist).
14056
+ $this->_admin_notices->remove_sticky( 'activation_pending' );
14057
+
14058
+ // Remove plugin from pending activation mode.
14059
+ unset( $this->_storage->is_pending_activation );
14060
+
14061
+ if ( ! $this->is_paying_or_trial() ) {
14062
+ $this->_admin_notices->add_sticky(
14063
+ sprintf( $this->get_text_inline( '%s activation was successfully completed.', 'plugin-x-activation-message' ), '<b>' . $this->get_plugin_name() . '</b>' ),
14064
+ 'activation_complete'
14065
+ );
14066
+ }
14067
+ }
14068
+
14069
+ if ( $this->is_paying_or_trial() ) {
14070
+ if ( ! $this->is_premium() ||
14071
+ ! $this->has_premium_version() ||
14072
+ ! $this->has_settings_menu()
14073
+ ) {
14074
+ if ( $this->is_paying() ) {
14075
+ $this->_admin_notices->add_sticky(
14076
+ sprintf(
14077
+ $this->get_text_inline( 'Your account was successfully activated with the %s plan.', 'activation-with-plan-x-message' ),
14078
+ $this->get_plan_title()
14079
+ ) . $this->get_complete_upgrade_instructions(),
14080
+ 'plan_upgraded',
14081
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
14082
+ );
14083
+ } else {
14084
+ $trial_plan = $this->get_trial_plan();
14085
+
14086
+ $this->_admin_notices->add_sticky(
14087
+ sprintf(
14088
+ $this->get_text_inline( 'Your trial has been successfully started.', 'trial-started-message' ),
14089
+ '<i>' . $this->get_plugin_name() . '</i>'
14090
+ ) . $this->get_complete_upgrade_instructions( $trial_plan->title ),
14091
+ 'trial_started',
14092
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
14093
+ );
14094
+ }
14095
+ }
14096
+
14097
+ $this->_admin_notices->remove_sticky( array(
14098
+ 'trial_promotion',
14099
+ ) );
14100
+ }
14101
+
14102
+ $plugin_id = fs_request_get( 'plugin_id', false );
14103
+
14104
+ // Store activation time ONLY for plugins & themes (not add-ons).
14105
+ if ( ! is_numeric( $plugin_id ) || ( $plugin_id == $this->_plugin->id ) ) {
14106
+ if ( empty( $this->_storage->activation_timestamp ) ) {
14107
+ $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME;
14108
+ }
14109
+ }
14110
+
14111
+ $next_page = '';
14112
+
14113
+ $extra = array();
14114
+ if ( $auto_install ) {
14115
+ $extra['auto_install'] = 'true';
14116
+ }
14117
+
14118
+ if ( is_numeric( $plugin_id ) ) {
14119
+ /**
14120
+ * @author Leo Fajardo (@leorw)
14121
+ * @since 1.2.1.6
14122
+ *
14123
+ * Also sync the license after an anonymous user subscribes.
14124
+ */
14125
+ if ( $this->is_anonymous() || $plugin_id != $this->_plugin->id ) {
14126
+ // Add-on was installed - sync license right after install.
14127
+ $next_page = $this->_get_sync_license_url( $plugin_id, true, $extra );
14128
+ }
14129
+ } else {
14130
+ /**
14131
+ * @author Vova Feldman (@svovaf)
14132
+ * @since 1.1.9 If site installed with a valid license, sync license.
14133
+ */
14134
+ if ( $this->is_paying() ) {
14135
+ $this->_sync_plugin_license(
14136
+ true,
14137
+ // Installs data is already synced in the beginning of this method directly or via _set_account().
14138
+ false
14139
+ );
14140
+ }
14141
+
14142
+ // Reload the page with the keys.
14143
+ $next_page = $this->is_anonymous() ?
14144
+ // If user previously skipped, redirect to account page.
14145
+ $this->get_account_url( false, $extra ) :
14146
+ $this->get_after_activation_url( 'after_connect_url', array(), $is_network_level_opt_in );
14147
+ }
14148
+
14149
+ if ( ! empty( $next_page ) && $redirect ) {
14150
+ fs_redirect( $next_page );
14151
+ }
14152
+
14153
+ return $next_page;
14154
+ }
14155
+
14156
+ /**
14157
+ * Install plugin with new user information after approval.
14158
+ *
14159
+ * @author Vova Feldman (@svovaf)
14160
+ * @since 1.0.7
14161
+ */
14162
+ function _install_with_new_user() {
14163
+ $this->_logger->entrance();
14164
+
14165
+ if ( $this->is_registered() ) {
14166
+ return;
14167
+ }
14168
+
14169
+ if ( ( $this->is_plugin() && fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) ||
14170
+ // @todo This logic should be improved because it's executed on every load of a theme.
14171
+ $this->is_theme()
14172
+ ) {
14173
+ // check_admin_referer( $this->_slug . '_activate_new' );
14174
+
14175
+ if ( fs_request_has( 'user_secret_key' ) ) {
14176
+ if ( fs_is_network_admin() && isset( $this->_storage->pending_sites_info ) ) {
14177
+ $pending_sites_info = $this->_storage->pending_sites_info;
14178
+
14179
+ $this->install_many_pending_with_user(
14180
+ fs_request_get( 'user_id' ),
14181
+ fs_request_get( 'user_public_key' ),
14182
+ fs_request_get( 'user_secret_key' ),
14183
+ fs_request_get_bool( 'is_marketing_allowed', null ),
14184
+ $pending_sites_info['blog_ids'],
14185
+ $pending_sites_info['license_key'],
14186
+ $pending_sites_info['trial_plan_id']
14187
+ );
14188
+ } else {
14189
+ $this->install_with_new_user(
14190
+ fs_request_get( 'user_id' ),
14191
+ fs_request_get( 'user_public_key' ),
14192
+ fs_request_get( 'user_secret_key' ),
14193
+ fs_request_get_bool( 'is_marketing_allowed', null ),
14194
+ fs_request_get( 'install_id' ),
14195
+ fs_request_get( 'install_public_key' ),
14196
+ fs_request_get( 'install_secret_key' ),
14197
+ true,
14198
+ fs_request_get_bool( 'auto_install' )
14199
+ );
14200
+ }
14201
+ } else if ( fs_request_has( 'pending_activation' ) ) {
14202
+ $this->set_pending_confirmation( fs_request_get( 'user_email' ), true );
14203
+ }
14204
+ }
14205
+ }
14206
+
14207
+ /**
14208
+ * @author Vova Feldman (@svovaf)
14209
+ * @since 2.0.0
14210
+ *
14211
+ * @param number $id
14212
+ * @param string $public_key
14213
+ * @param string $secret_key
14214
+ *
14215
+ * @return \FS_User
14216
+ */
14217
+ private function setup_user( $id, $public_key, $secret_key ) {
14218
+ $user = self::_get_user_by_id( $id );
14219
+
14220
+ if ( is_object( $user ) ) {
14221
+ $this->_user = $user;
14222
+ } else {
14223
+ $user = new FS_User();
14224
+ $user->id = $id;
14225
+ $user->public_key = $public_key;
14226
+ $user->secret_key = $secret_key;
14227
+
14228
+ $this->_user = $user;
14229
+ $user_result = $this->get_api_user_scope()->get();
14230
+ $user = new FS_User( $user_result );
14231
+
14232
+ $this->_user = $user;
14233
+ $this->_store_user();
14234
+ }
14235
+
14236
+ return $user;
14237
+ }
14238
+
14239
+ /**
14240
+ * Install plugin with new user.
14241
+ *
14242
+ * @author Vova Feldman (@svovaf)
14243
+ * @since 1.1.7.4
14244
+ *
14245
+ * @param number $user_id
14246
+ * @param string $user_public_key
14247
+ * @param string $user_secret_key
14248
+ * @param bool|null $is_marketing_allowed
14249
+ * @param number $install_id
14250
+ * @param string $install_public_key
14251
+ * @param string $install_secret_key
14252
+ * @param bool $redirect
14253
+ * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will
14254
+ * redirect (or return a URL) to the account page with a special parameter to
14255
+ * trigger the auto installation processes.
14256
+ *
14257
+ * @return string If redirect is `false`, returns the next page the user should be redirected to.
14258
+ */
14259
+ private function install_with_new_user(
14260
+ $user_id,
14261
+ $user_public_key,
14262
+ $user_secret_key,
14263
+ $is_marketing_allowed,
14264
+ $install_id,
14265
+ $install_public_key,
14266
+ $install_secret_key,
14267
+ $redirect = true,
14268
+ $auto_install = false
14269
+ ) {
14270
+ /**
14271
+ * This method is also executed after opting in with a license key since the
14272
+ * license can be potentially associated with a different owner.
14273
+ *
14274
+ * @since 2.0.0
14275
+ */
14276
+ $user = self::_get_user_by_id( $user_id );
14277
+
14278
+ if ( ! is_object( $user ) ) {
14279
+ $user = new FS_User();
14280
+ $user->id = $user_id;
14281
+ $user->public_key = $user_public_key;
14282
+ $user->secret_key = $user_secret_key;
14283
+
14284
+ $this->_user = $user;
14285
+ $user_result = $this->get_api_user_scope()->get();
14286
+ $user = new FS_User( $user_result );
14287
+ }
14288
+
14289
+ $this->_user = $user;
14290
+
14291
+ $site = new FS_Site();
14292
+ $site->id = $install_id;
14293
+ $site->public_key = $install_public_key;
14294
+ $site->secret_key = $install_secret_key;
14295
+
14296
+ $this->_site = $site;
14297
+ $site_result = $this->get_api_site_scope()->get();
14298
+ $site = new FS_Site( $site_result );
14299
+ $this->_site = $site;
14300
+
14301
+ if ( ! is_null( $is_marketing_allowed ) ) {
14302
+ $this->disable_opt_in_notice_and_lock_user();
14303
+ }
14304
+
14305
+ return $this->setup_account(
14306
+ $this->_user,
14307
+ $this->_site,
14308
+ $redirect,
14309
+ $auto_install
14310
+ );
14311
+ }
14312
+
14313
+ /**
14314
+ * Install plugin with user.
14315
+ *
14316
+ * @author Leo Fajardo (@leorw)
14317
+ * @since 2.0.0
14318
+ *
14319
+ * @param number $user_id
14320
+ * @param string $user_public_key
14321
+ * @param string $user_secret_key
14322
+ * @param bool|null $is_marketing_allowed
14323
+ * @param array $site_ids
14324
+ * @param bool $license_key
14325
+ * @param bool $trial_plan_id
14326
+ * @param bool $redirect
14327
+ *
14328
+ * @return string If redirect is `false`, returns the next page the user should be redirected to.
14329
+ */
14330
+ private function install_many_pending_with_user(
14331
+ $user_id,
14332
+ $user_public_key,
14333
+ $user_secret_key,
14334
+ $is_marketing_allowed,
14335
+ $site_ids,
14336
+ $license_key = false,
14337
+ $trial_plan_id = false,
14338
+ $redirect = true
14339
+ ) {
14340
+ $user = $this->setup_user( $user_id, $user_public_key, $user_secret_key );
14341
+
14342
+ if ( ! is_null( $is_marketing_allowed ) ) {
14343
+ $this->disable_opt_in_notice_and_lock_user();
14344
+ }
14345
+
14346
+ $sites = array();
14347
+ foreach ( $site_ids as $site_id ) {
14348
+ $sites[] = $this->get_site_info( array( 'blog_id' => $site_id ) );
14349
+ }
14350
+
14351
+ $this->install_with_user( $user, $license_key, $trial_plan_id, $redirect, true, $sites );
14352
+ }
14353
+
14354
+ /**
14355
+ * Multi-site install with a new user.
14356
+ *
14357
+ * @author Vova Feldman (@svovaf)
14358
+ * @since 2.0.0
14359
+ *
14360
+ * @param number $user_id
14361
+ * @param string $user_public_key
14362
+ * @param string $user_secret_key
14363
+ * @param bool|null $is_marketing_allowed
14364
+ * @param object[] $installs
14365
+ * @param bool $redirect
14366
+ * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will
14367
+ * redirect (or return a URL) to the account page with a special parameter to
14368
+ * trigger the auto installation processes.
14369
+ *
14370
+ * @return string If redirect is `false`, returns the next page the user should be redirected to.
14371
+ */
14372
+ private function install_many_with_new_user(
14373
+ $user_id,
14374
+ $user_public_key,
14375
+ $user_secret_key,
14376
+ $is_marketing_allowed,
14377
+ array $installs,
14378
+ $redirect = true,
14379
+ $auto_install = false
14380
+ ) {
14381
+ $this->setup_user( $user_id, $user_public_key, $user_secret_key );
14382
+
14383
+ if ( ! is_null( $is_marketing_allowed ) ) {
14384
+ $this->disable_opt_in_notice_and_lock_user();
14385
+ }
14386
+
14387
+ $install_ids = array();
14388
+
14389
+ foreach ( $installs as $install ) {
14390
+ $install_ids[] = $install->id;
14391
+ }
14392
+
14393
+ $left = count( $install_ids );
14394
+ $offset = 0;
14395
+
14396
+ $installs = array();
14397
+ while ( $left > 0 ) {
14398
+ $result = $this->get_api_user_scope()->get( "/plugins/{$this->_module_id}/installs.json?ids=" . implode( ',', array_slice( $install_ids, $offset, 25 ) ) );
14399
+
14400
+ if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
14401
+ // @todo Handle API error.
14402
+ }
14403
+
14404
+ $installs = array_merge( $installs, $result->installs );
14405
+
14406
+ $left -= 25;
14407
+ }
14408
+
14409
+ foreach ( $installs as &$install ) {
14410
+ $install = new FS_Site( $install );
14411
+ }
14412
+
14413
+ return $this->setup_network_account(
14414
+ $this->_user,
14415
+ $installs,
14416
+ $redirect,
14417
+ $auto_install
14418
+ );
14419
+ }
14420
+
14421
+ /**
14422
+ * @author Vova Feldman (@svovaf)
14423
+ * @since 1.1.7.4
14424
+ *
14425
+ * @param string|bool $email
14426
+ * @param bool $redirect
14427
+ * @param string|bool $license_key Since 1.2.1.5
14428
+ * @param bool $is_pending_trial Since 1.2.1.5
14429
+ *
14430
+ * @return string Since 1.2.1.5 if $redirect is `false`, return the pending activation page.
14431
+ */
14432
+ private function set_pending_confirmation(
14433
+ $email = false,
14434
+ $redirect = true,
14435
+ $license_key = false,
14436
+ $is_pending_trial = false
14437
+ ) {
14438
+ if ( $this->_ignore_pending_mode ) {
14439
+ /**
14440
+ * If explicitly asked to ignore pending mode, set to anonymous mode
14441
+ * if require confirmation before finalizing the opt-in.
14442
+ *
14443
+ * @author Vova Feldman
14444
+ * @since 1.2.1.6
14445
+ */
14446
+ $this->skip_connection( null, fs_is_network_admin() );
14447
+ } else {
14448
+ // Install must be activated via email since
14449
+ // user with the same email already exist.
14450
+ $this->_storage->is_pending_activation = true;
14451
+ $this->_add_pending_activation_notice( $email, $is_pending_trial );
14452
+ }
14453
+
14454
+ if ( ! empty( $license_key ) ) {
14455
+ $this->_storage->pending_license_key = $license_key;
14456
+ }
14457
+
14458
+ // Remove the opt-in sticky notice.
14459
+ $this->_admin_notices->remove_sticky( array(
14460
+ 'connect_account',
14461
+ 'trial_promotion',
14462
+ ) );
14463
+
14464
+ $next_page = $this->get_after_activation_url( 'after_pending_connect_url' );
14465
+
14466
+ // Reload the page with with pending activation message.
14467
+ if ( $redirect ) {
14468
+ fs_redirect( $next_page );
14469
+ }
14470
+
14471
+ return $next_page;
14472
+ }
14473
+
14474
+ /**
14475
+ * Install plugin with current logged WP user info.
14476
+ *
14477
+ * @author Vova Feldman (@svovaf)
14478
+ * @since 1.0.7
14479
+ */
14480
+ function _install_with_current_user() {
14481
+ $this->_logger->entrance();
14482
+
14483
+ if ( $this->is_registered() ) {
14484
+ return;
14485
+ }
14486
+
14487
+ if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) && fs_request_is_post() ) {
14488
+ // check_admin_referer( 'activate_existing_' . $this->_plugin->public_key );
14489
+
14490
+ /**
14491
+ * @author Vova Feldman (@svovaf)
14492
+ * @since 1.1.9 Add license key if given.
14493
+ */
14494
+ $license_key = fs_request_get( 'license_secret_key' );
14495
+
14496
+ $this->install_with_current_user( $license_key );
14497
+ }
14498
+ }
14499
+
14500
+
14501
+ /**
14502
+ * @author Vova Feldman (@svovaf)
14503
+ * @since 1.1.7.4
14504
+ *
14505
+ * @param string|bool $license_key
14506
+ * @param number|bool $trial_plan_id
14507
+ * @param array $sites Since 2.0.0
14508
+ * @param bool $redirect
14509
+ *
14510
+ * @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.
14511
+ */
14512
+ private function install_with_current_user(
14513
+ $license_key = false,
14514
+ $trial_plan_id = false,
14515
+ $sites = array(),
14516
+ $redirect = true
14517
+ ) {
14518
+ // Get current logged WP user.
14519
+ $current_user = self::_get_current_wp_user();
14520
+
14521
+ // Find the relevant FS user by the email.
14522
+ $user = self::_get_user_by_email( $current_user->user_email );
14523
+
14524
+ return $this->install_with_user( $user, $license_key, $trial_plan_id, $redirect, true, $sites );
14525
+ }
14526
+
14527
+ /**
14528
+ * @author Vova Feldman (@svovaf)
14529
+ * @since 2.0.0
14530
+ *
14531
+ * @param \FS_User $user
14532
+ * @param string|bool $license_key
14533
+ * @param number|bool $trial_plan_id
14534
+ * @param bool $redirect
14535
+ * @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.
14536
+ * @param array $sites Since 2.0.0. If not empty, should be a collection of site details for the bulk install API request.
14537
+ *
14538
+ * @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.
14539
+ */
14540
+ private function install_with_user(
14541
+ FS_User $user,
14542
+ $license_key = false,
14543
+ $trial_plan_id = false,
14544
+ $redirect = true,
14545
+ $setup_account = true,
14546
+ $sites = array()
14547
+ ) {
14548
+ // We have to set the user before getting user scope API handler.
14549
+ $this->_user = $user;
14550
+
14551
+ // Install the plugin.
14552
+ $result = $this->create_installs_with_user(
14553
+ $user,
14554
+ $license_key,
14555
+ $trial_plan_id,
14556
+ $sites,
14557
+ $redirect
14558
+ );
14559
+
14560
+ if ( ! $this->is_api_result_entity( $result ) &&
14561
+ ! $this->is_api_result_object( $result, 'installs' )
14562
+ ) {
14563
+ // @todo Handler potential API error of the $result
14564
+ }
14565
+
14566
+ if ( empty( $sites ) ) {
14567
+ $site = new FS_Site( $result );
14568
+ $this->_site = $site;
14569
+
14570
+ if ( ! $setup_account ) {
14571
+ $this->_store_site();
14572
+
14573
+ $this->sync_plan_if_not_exist( $site->plan_id );
14574
+
14575
+ if ( ! empty( $license_key ) && FS_Plugin_License::is_valid_id( $site->license_id ) ) {
14576
+ $this->sync_license_if_not_exist( $site->license_id, $license_key );
14577
+ }
14578
+
14579
+ $this->_admin_notices->remove_sticky( 'connect_account', false );
14580
+
14581
+ return $site;
14582
+ }
14583
+
14584
+ return $this->setup_account( $this->_user, $this->_site, $redirect );
14585
+ } else {
14586
+ $installs = array();
14587
+ foreach ( $result->installs as $install ) {
14588
+ $installs[] = new FS_Site( $install );
14589
+ }
14590
+
14591
+ return $this->setup_network_account(
14592
+ $user,
14593
+ $installs,
14594
+ $redirect
14595
+ );
14596
+ }
14597
+ }
14598
+
14599
+ /**
14600
+ * Initiate an API request to create a collection of installs.
14601
+ *
14602
+ * @author Vova Feldman (@svovaf)
14603
+ * @since 2.0.0
14604
+ *
14605
+ * @param \FS_User $user
14606
+ * @param bool $license_key
14607
+ * @param bool $trial_plan_id
14608
+ * @param array $sites
14609
+ * @param bool $redirect
14610
+ * @param bool $silent
14611
+ *
14612
+ * @return object|mixed
14613
+ */
14614
+ private function create_installs_with_user(
14615
+ FS_User $user,
14616
+ $license_key = false,
14617
+ $trial_plan_id = false,
14618
+ $sites = array(),
14619
+ $redirect = false,
14620
+ $silent = false
14621
+ ) {
14622
+ $extra_install_params = array(
14623
+ 'uid' => $this->get_anonymous_id(),
14624
+ 'is_disconnected' => false,
14625
+ );
14626
+
14627
+ if ( ! empty( $license_key ) ) {
14628
+ $extra_install_params['license_key'] = $this->apply_filters( 'license_key', $license_key );
14629
+ } else if ( FS_Plugin_Plan::is_valid_id( $trial_plan_id ) ) {
14630
+ $extra_install_params['trial_plan_id'] = $trial_plan_id;
14631
+ }
14632
+
14633
+ if ( ! empty( $sites ) ) {
14634
+ $extra_install_params['sites'] = $sites;
14635
+ }
14636
+
14637
+ $args = $this->get_install_data_for_api( $extra_install_params, false, false );
14638
+
14639
+ // Install the plugin.
14640
+ $result = $this->get_api_user_scope_by_user( $user )->call(
14641
+ "/plugins/{$this->get_id()}/installs.json",
14642
+ 'post',
14643
+ $args
14644
+ );
14645
+
14646
+ if ( ! $this->is_api_result_entity( $result ) &&
14647
+ ! $this->is_api_result_object( $result, 'installs' )
14648
+ ) {
14649
+ if ( ! empty( $args['license_key'] ) ) {
14650
+ // Pass full the fully entered license key to the failure handler.
14651
+ $args['license_key'] = $license_key;
14652
+ }
14653
+
14654
+ $result = $this->apply_filters( 'after_install_failure', $result, $args );
14655
+
14656
+ if ( ! $silent ) {
14657
+ $this->_admin_notices->add(
14658
+ sprintf( $this->get_text_inline( 'Couldn\'t activate %s.', 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
14659
+ $this->get_text_inline( 'Please contact us with the following message:', 'contact-us-with-error-message' ) . ' ' . '<b>' . $result->error->message . '</b>',
14660
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
14661
+ 'error'
14662
+ );
14663
+ }
14664
+
14665
+ if ( $redirect ) {
14666
+ /**
14667
+ * We set the user before getting the user scope API handler, so the user became temporarily
14668
+ * registered (`is_registered() = true`). Since the API returned an error and we will redirect,
14669
+ * we have to set the user to `null`, otherwise, the user will be redirected to the wrong
14670
+ * activation page based on the return value of `is_registered()`. In addition, in case the
14671
+ * context plugin doesn't have a settings menu and the default page is the `Plugins` page,
14672
+ * misleading plugin activation errors will be shown on the `Plugins` page.
14673
+ *
14674
+ * @author Leo Fajardo (@leorw)
14675
+ */
14676
+ $this->_user = null;
14677
+
14678
+ fs_redirect( $this->get_activation_url( array( 'error' => $result->error->message ) ) );
14679
+ }
14680
+ }
14681
+
14682
+ return $result;
14683
+ }
14684
+
14685
+ /**
14686
+ * Tries to activate add-on account based on parent plugin info.
14687
+ *
14688
+ * @author Vova Feldman (@svovaf)
14689
+ * @since 1.0.6
14690
+ *
14691
+ * @param Freemius $parent_fs
14692
+ */
14693
+ private function _activate_addon_account( Freemius $parent_fs ) {
14694
+ if ( $this->is_registered() ) {
14695
+ // Already activated.
14696
+ return;
14697
+ }
14698
+
14699
+ // Activate add-on with parent plugin credentials.
14700
+ $addon_install = $parent_fs->get_api_site_scope()->call(
14701
+ "/addons/{$this->_plugin->id}/installs.json",
14702
+ 'post',
14703
+ $this->get_install_data_for_api( array(
14704
+ 'uid' => $this->get_anonymous_id(),
14705
+ ), false, false )
14706
+ );
14707
+
14708
+ if ( isset( $addon_install->error ) ) {
14709
+ $this->_admin_notices->add(
14710
+ sprintf( $this->get_text_inline( 'Couldn\'t activate %s.', 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
14711
+ $this->get_text_inline( 'Please contact us with the following message:', 'contact-us-with-error-message' ) . ' ' . '<b>' . $addon_install->error->message . '</b>',
14712
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
14713
+ 'error'
14714
+ );
14715
+
14716
+ return;
14717
+ }
14718
+
14719
+ // Get user information based on parent's plugin.
14720
+ $user = $parent_fs->get_user();
14721
+
14722
+ // First of all, set site and user info - otherwise we won't
14723
+ // be able to invoke API calls.
14724
+ $this->_site = new FS_Site( $addon_install );
14725
+ $this->_user = $user;
14726
+
14727
+ // Sync add-on plans.
14728
+ $this->_sync_plans();
14729
+
14730
+ // Get site's current plan.
14731
+ //$this->_site->plan = $this->_get_plan_by_id( $this->_site->plan->id );
14732
+
14733
+ $this->_set_account( $user, $this->_site );
14734
+
14735
+ // Sync licenses.
14736
+ $this->_sync_licenses();
14737
+
14738
+ // Try to activate premium license.
14739
+ $this->_activate_license( true );
14740
+ }
14741
+
14742
+ /**
14743
+ * Tries to activate parent account based on add-on's info.
14744
+ *
14745
+ * @author Vova Feldman (@svovaf)
14746
+ * @since 1.2.2.7
14747
+ *
14748
+ * @param Freemius $parent_fs
14749
+ */
14750
+ private function activate_parent_account( Freemius $parent_fs ) {
14751
+ if ( ! $this->is_addon() ) {
14752
+ // This is not an add-on.
14753
+ return;
14754
+ }
14755
+
14756
+ if ( $parent_fs->is_registered() ) {
14757
+ // Already activated.
14758
+ return;
14759
+ }
14760
+
14761
+ // Activate parent with add-on's user credentials.
14762
+ $parent_install = $this->get_api_user_scope()->call(
14763
+ "/plugins/{$parent_fs->_plugin->id}/installs.json",
14764
+ 'post',
14765
+ $parent_fs->get_install_data_for_api( array(
14766
+ 'uid' => $parent_fs->get_anonymous_id(),
14767
+ ), false, false )
14768
+ );
14769
+
14770
+ if ( isset( $parent_install->error ) ) {
14771
+ $this->_admin_notices->add(
14772
+ sprintf( $this->get_text_inline( 'Couldn\'t activate %s.', 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
14773
+ $this->get_text_inline( 'Please contact us with the following message:', 'contact-us-with-error-message' ) . ' ' . '<b>' . $parent_install->error->message . '</b>',
14774
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
14775
+ 'error'
14776
+ );
14777
+
14778
+ return;
14779
+ }
14780
+
14781
+ $parent_fs->_admin_notices->remove_sticky( 'connect_account' );
14782
+
14783
+ if ( $parent_fs->is_pending_activation() ) {
14784
+ $parent_fs->_admin_notices->remove_sticky( 'activation_pending' );
14785
+
14786
+ unset( $parent_fs->_storage->is_pending_activation );
14787
+ }
14788
+
14789
+ // Get user information based on parent's plugin.
14790
+ $user = $this->get_user();
14791
+
14792
+ // First of all, set site info - otherwise we won't
14793
+ // be able to invoke API calls.
14794
+ $parent_fs->_site = new FS_Site( $parent_install );
14795
+ $parent_fs->_user = $user;
14796
+
14797
+ // Sync add-on plans.
14798
+ $parent_fs->_sync_plans();
14799
+
14800
+ $parent_fs->_set_account( $user, $parent_fs->_site );
14801
+ }
14802
+
14803
+ #endregion
14804
+
14805
+ #----------------------------------------------------------------------------------
14806
+ #region Admin Menu Items
14807
+ #----------------------------------------------------------------------------------
14808
+
14809
+ private $_menu_items = array();
14810
+
14811
+ /**
14812
+ * @author Vova Feldman (@svovaf)
14813
+ * @since 1.2.1.8
14814
+ *
14815
+ * @return array
14816
+ */
14817
+ function get_menu_items() {
14818
+ return $this->_menu_items;
14819
+ }
14820
+
14821
+ /**
14822
+ * @author Vova Feldman (@svovaf)
14823
+ * @since 1.0.7
14824
+ *
14825
+ * @return string
14826
+ */
14827
+ function get_menu_slug() {
14828
+ return $this->_menu->get_slug();
14829
+ }
14830
+
14831
+ /**
14832
+ * @author Vova Feldman (@svovaf)
14833
+ * @since 1.0.9
14834
+ */
14835
+ function _prepare_admin_menu() {
14836
+ // if ( ! $this->is_on() ) {
14837
+ // return;
14838
+ // }
14839
+
14840
+ /**
14841
+ * When running from a site admin with a network activated module and the connection
14842
+ * was NOT delegated and the user still haven't skipped or opted-in, then hide the
14843
+ * site level settings.
14844
+ *
14845
+ * @author Vova Feldman (@svovaf)
14846
+ * @since 2.0.0
14847
+ */
14848
+ $should_hide_site_admin_settings = (
14849
+ $this->_is_network_active &&
14850
+ ! fs_is_network_admin() &&
14851
+ ! $this->is_delegated_connection() &&
14852
+ ! $this->is_anonymous() &&
14853
+ ! $this->is_registered()
14854
+ );
14855
+
14856
+ if ( ( ! $this->has_api_connectivity() && ! $this->is_enable_anonymous() ) ||
14857
+ $should_hide_site_admin_settings
14858
+ ) {
14859
+ $this->_menu->remove_menu_item( $should_hide_site_admin_settings );
14860
+ } else {
14861
+ $this->do_action( fs_is_network_admin() ?
14862
+ 'before_network_admin_menu_init' :
14863
+ 'before_admin_menu_init'
14864
+ );
14865
+
14866
+ $this->add_menu_action();
14867
+
14868
+ $this->add_network_menu_when_missing();
14869
+
14870
+ $this->add_submenu_items();
14871
+ }
14872
+ }
14873
+
14874
+ /**
14875
+ * Admin dashboard menu items modifications.
14876
+ *
14877
+ * NOTE: admin_menu action executed before admin_init.
14878
+ *
14879
+ * @author Vova Feldman (@svovaf)
14880
+ * @since 1.0.7
14881
+ *
14882
+ */
14883
+ private function add_menu_action() {
14884
+ if ( $this->is_activation_mode() ) {
14885
+ if ( $this->is_plugin() || ( $this->has_settings_menu() && ! $this->is_free_wp_org_theme() ) ) {
14886
+ $this->override_plugin_menu_with_activation();
14887
+ } else {
14888
+ /**
14889
+ * Handle theme opt-in when the opt-in form shows as a dialog box in the themes page.
14890
+ */
14891
+ if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) ) {
14892
+ add_action( 'load-themes.php', array( &$this, '_install_with_current_user' ) );
14893
+ } else if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ||
14894
+ fs_request_get_bool( 'pending_activation' )
14895
+ ) {
14896
+ add_action( 'load-themes.php', array( &$this, '_install_with_new_user' ) );
14897
+ }
14898
+ }
14899
+ } else {
14900
+ if ( ! $this->is_registered() ) {
14901
+ // If not registered try to install user.
14902
+ if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) {
14903
+ $this->_install_with_new_user();
14904
+ }
14905
+ } else if (
14906
+ fs_request_is_action( 'sync_user' ) &&
14907
+ ( ! $this->has_settings_menu() || $this->is_free_wp_org_theme() )
14908
+ ) {
14909
+ $this->_handle_account_user_sync();
14910
+ }
14911
+ }
14912
+ }
14913
+
14914
+ /**
14915
+ * @author Vova Feldman (@svovaf)
14916
+ * @since 1.0.1
14917
+ */
14918
+ function _redirect_on_clicked_menu_link() {
14919
+ $this->_logger->entrance();
14920
+
14921
+ $page = fs_request_get('page');
14922
+ $page = is_string($page) ? strtolower($page) : '';
14923
+
14924
+ $this->_logger->log( 'page = ' . $page );
14925
+
14926
+ foreach ( $this->_menu_items as $priority => $items ) {
14927
+ foreach ( $items as $item ) {
14928
+ if ( isset( $item['url'] ) ) {
14929
+ if ( $page === $this->_menu->get_slug( strtolower( $item['menu_slug'] ) ) ) {
14930
+ $this->_logger->log( 'Redirecting to ' . $item['url'] );
14931
+
14932
+ fs_redirect( $item['url'] );
14933
+ }
14934
+ }
14935
+ }
14936
+ }
14937
+ }
14938
+
14939
+ /**
14940
+ * Remove plugin's all admin menu items & pages, and replace with activation page.
14941
+ *
14942
+ * @author Vova Feldman (@svovaf)
14943
+ * @since 1.0.1
14944
+ */
14945
+ private function override_plugin_menu_with_activation() {
14946
+ $this->_logger->entrance();
14947
+
14948
+ $hook = false;
14949
+
14950
+ if ( ! $this->has_settings_menu() ) {
14951
+ // Add the opt-in page without a menu item.
14952
+ $hook = FS_Admin_Menu_Manager::add_subpage(
14953
+ null,
14954
+ $this->get_plugin_name(),
14955
+ $this->get_plugin_name(),
14956
+ 'manage_options',
14957
+ $this->_slug,
14958
+ array( &$this, '_connect_page_render' )
14959
+ );
14960
+ } else if ( $this->_menu->is_top_level() ) {
14961
+ if ( $this->_menu->is_override_exact() ) {
14962
+ // Make sure the current page is matching the activation page.
14963
+ if ( ! $this->is_matching_url( $this->get_activation_url() ) ) {
14964
+ return;
14965
+ }
14966
+ }
14967
+
14968
+ $hook = $this->_menu->override_menu_item( array( &$this, '_connect_page_render' ) );
14969
+
14970
+ if ( false === $hook ) {
14971
+ // Create new menu item just for the opt-in.
14972
+ $hook = FS_Admin_Menu_Manager::add_page(
14973
+ $this->get_plugin_name(),
14974
+ $this->get_plugin_name(),
14975
+ 'manage_options',
14976
+ $this->_menu->get_slug(),
14977
+ array( &$this, '_connect_page_render' )
14978
+ );
14979
+ }
14980
+ } else {
14981
+ $menus = array( $this->_menu->get_parent_slug() );
14982
+
14983
+ if ( $this->_menu->is_override_exact() ) {
14984
+ // Make sure the current page is matching the activation page.
14985
+ if ( ! $this->is_matching_url( $this->get_activation_url() ) ) {
14986
+ return;
14987
+ }
14988
+ }
14989
+
14990
+ foreach ( $menus as $parent_slug ) {
14991
+ $hook = $this->_menu->override_submenu_action(
14992
+ $parent_slug,
14993
+ $this->_menu->get_raw_slug(),
14994
+ array( &$this, '_connect_page_render' )
14995
+ );
14996
+
14997
+ if ( false !== $hook ) {
14998
+ // Found plugin's submenu item.
14999
+ break;
15000
+ }
15001
+ }
15002
+ }
15003
+
15004
+ if ( $this->is_activation_page() ) {
15005
+ // Clean admin page from distracting content.
15006
+ self::_clean_admin_content_section();
15007
+ }
15008
+
15009
+ if ( false !== $hook ) {
15010
+ if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) ) {
15011
+ $this->_install_with_current_user();
15012
+ } else if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) {
15013
+ $this->_install_with_new_user();
15014
+ }
15015
+ }
15016
+ }
15017
+
15018
+ /**
15019
+ * If a plugin was network activated and connected but don't have a network
15020
+ * level settings, then add an artificial menu item for the Account and other
15021
+ * Freemius settings.
15022
+ *
15023
+ * @author Vova Feldman (@svovaf)
15024
+ * @since 2.0.0
15025
+ */
15026
+ private function add_network_menu_when_missing() {
15027
+ $this->_logger->entrance();
15028
+
15029
+ if ( ! $this->_is_network_active ) {
15030
+ // Plugin wasn't activated on the network level.
15031
+ return;
15032
+ }
15033
+
15034
+ if ( ! fs_is_network_admin() ) {
15035
+ // The context is not the network admin.
15036
+ return;
15037
+ }
15038
+
15039
+ if ( $this->_menu->has_network_menu() ) {
15040
+ // Plugin already has a network level menu.
15041
+ return;
15042
+ }
15043
+
15044
+ if ( $this->is_network_activation_mode() ) {
15045
+ /**
15046
+ * Do not add during activation mode, otherwise, there will be duplicate menus while the opt-in
15047
+ * screen is being shown.
15048
+ *
15049
+ * @author Leo Fajardo (@leorw)
15050
+ */
15051
+ return;
15052
+ }
15053
+
15054
+ if ( ! WP_FS__SHOW_NETWORK_EVEN_WHEN_DELEGATED ) {
15055
+ if ( $this->is_network_delegated_connection() ) {
15056
+ // Super-admin delegated the connection to the site admins.
15057
+ return;
15058
+ }
15059
+ }
15060
+
15061
+ if ( ! $this->_menu->has_menu() || $this->_menu->is_top_level() ) {
15062
+ $this->_dynamically_added_top_level_page_hook_name = $this->_menu->add_page_and_update(
15063
+ $this->get_plugin_name(),
15064
+ $this->get_plugin_name(),
15065
+ 'manage_options',
15066
+ $this->_menu->has_menu() ? $this->_menu->get_slug() : $this->_slug
15067
+ );
15068
+ } else {
15069
+ $this->_menu->add_subpage_and_update(
15070
+ $this->_menu->get_parent_slug(),
15071
+ $this->get_plugin_name(),
15072
+ $this->get_plugin_name(),
15073
+ 'manage_options',
15074
+ $this->_menu->get_slug()
15075
+ );
15076
+ }
15077
+ }
15078
+
15079
+ /**
15080
+ * @author Leo Fajardo (@leorw)
15081
+ * @since 1.2.1
15082
+ *
15083
+ * return string
15084
+ */
15085
+ function get_top_level_menu_capability() {
15086
+ global $menu;
15087
+
15088
+ $top_level_menu_slug = $this->get_top_level_menu_slug();
15089
+
15090
+ foreach ( $menu as $menu_info ) {
15091
+ /**
15092
+ * The second element in the menu info array is the capability/role that has access to the menu and the
15093
+ * third element is the menu slug.
15094
+ */
15095
+ if ( $menu_info[2] === $top_level_menu_slug ) {
15096
+ return $menu_info[1];
15097
+ }
15098
+ }
15099
+
15100
+ return 'read';
15101
+ }
15102
+
15103
+ /**
15104
+ * @author Vova Feldman (@svovaf)
15105
+ * @since 1.0.0
15106
+ *
15107
+ * @return string
15108
+ */
15109
+ private function get_top_level_menu_slug() {
15110
+ return ( $this->is_addon() ?
15111
+ $this->get_parent_instance()->_menu->get_top_level_menu_slug() :
15112
+ $this->_menu->get_top_level_menu_slug() );
15113
+ }
15114
+
15115
+ /**
15116
+ * @author Vova Feldman (@svovaf)
15117
+ * @since 1.2.2.7
15118
+ *
15119
+ * @return string
15120
+ */
15121
+ function get_pricing_cta_label() {
15122
+ $label = $this->get_text_inline( 'Upgrade', 'upgrade' );
15123
+
15124
+ if ( $this->is_in_trial_promotion() &&
15125
+ ! $this->is_paying_or_trial()
15126
+ ) {
15127
+ // If running a trial promotion, modify the pricing to load the trial.
15128
+ $label = $this->get_text_inline( 'Start Trial', 'start-trial' );
15129
+ } else if ( $this->is_paying() ) {
15130
+ $label = $this->get_text_inline( 'Pricing', 'pricing' );
15131
+ }
15132
+
15133
+ return $label;
15134
+ }
15135
+
15136
+ /**
15137
+ * @author Vova Feldman (@svovaf)
15138
+ * @since 1.2.2.7
15139
+ *
15140
+ * @return bool
15141
+ */
15142
+ function is_pricing_page_visible() {
15143
+ return (
15144
+ // Has at least one paid plan.
15145
+ $this->has_paid_plan() &&
15146
+ // Didn't ask to hide the pricing page.
15147
+ $this->is_page_visible( 'pricing' ) &&
15148
+ // Don't have a valid active license or has more than one plan.
15149
+ ( ! $this->is_paying() || ! $this->is_single_plan() )
15150
+ );
15151
+ }
15152
+
15153
+ /**
15154
+ * Add default Freemius menu items.
15155
+ *
15156
+ * @author Vova Feldman (@svovaf)
15157
+ * @since 1.0.0
15158
+ * @since 1.2.2.7 Also add submenu items when running in a free .org theme so the tabs will be visible.
15159
+ */
15160
+ private function add_submenu_items() {
15161
+ $this->_logger->entrance();
15162
+
15163
+ $is_activation_mode = $this->is_activation_mode();
15164
+
15165
+ if ( $this->is_addon() ) {
15166
+ // No submenu items for add-ons.
15167
+ $add_submenu_items = false;
15168
+ } else if ( $this->is_free_wp_org_theme() && ! fs_is_network_admin() ) {
15169
+ // Also add submenu items when running in a free .org theme so the tabs will be visible.
15170
+ $add_submenu_items = true;
15171
+ } else if ( $is_activation_mode && ! $this->is_free_wp_org_theme() ) {
15172
+ $add_submenu_items = false;
15173
+ } else if ( fs_is_network_admin() ) {
15174
+ /**
15175
+ * Add submenu items to network level when plugin was network
15176
+ * activated and the super-admin did NOT delegated the connection
15177
+ * of all sites to site admins.
15178
+ */
15179
+ $add_submenu_items = (
15180
+ $this->_is_network_active &&
15181
+ ( WP_FS__SHOW_NETWORK_EVEN_WHEN_DELEGATED ||
15182
+ ! $this->is_network_delegated_connection() )
15183
+ );
15184
+ } else {
15185
+ $add_submenu_items = ( ! $this->_is_network_active || $this->is_delegated_connection() );
15186
+ }
15187
+
15188
+ if ( $add_submenu_items ) {
15189
+ if ( $this->has_affiliate_program() ) {
15190
+ // Add affiliation page.
15191
+ $this->add_submenu_item(
15192
+ $this->get_text_inline( 'Affiliation', 'affiliation' ),
15193
+ array( &$this, '_affiliation_page_render' ),
15194
+ $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Affiliation', 'affiliation' ),
15195
+ 'manage_options',
15196
+ 'affiliation',
15197
+ 'Freemius::_clean_admin_content_section',
15198
+ WP_FS__DEFAULT_PRIORITY,
15199
+ $this->is_submenu_item_visible( 'affiliation' )
15200
+ );
15201
+ }
15202
+ }
15203
+
15204
+ if ( $add_submenu_items ||
15205
+ ( $is_activation_mode &&
15206
+ $this->is_only_premium() &&
15207
+ $this->is_admin_page( 'account' ) &&
15208
+ fs_request_is_action( $this->get_unique_affix() . '_sync_license' )
15209
+ )
15210
+ ) {
15211
+ if ( ! WP_FS__DEMO_MODE && $this->is_registered() ) {
15212
+ $show_account = (
15213
+ $this->is_submenu_item_visible( 'account' ) &&
15214
+ /**
15215
+ * @since 1.2.2.7 Don't show the Account for free WP.org themes without any paid plans.
15216
+ */
15217
+ ( ! $this->is_free_wp_org_theme() || $this->has_paid_plan() )
15218
+ );
15219
+
15220
+ // Add user account page.
15221
+ $this->add_submenu_item(
15222
+ $this->get_text_inline( 'Account', 'account' ),
15223
+ array( &$this, '_account_page_render' ),
15224
+ $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Account', 'account' ),
15225
+ 'manage_options',
15226
+ 'account',
15227
+ array( &$this, '_account_page_load' ),
15228
+ WP_FS__DEFAULT_PRIORITY,
15229
+ ( $add_submenu_items && $show_account )
15230
+ );
15231
+ }
15232
+ }
15233
+
15234
+ if ( $add_submenu_items ) {
15235
+ // Add contact page.
15236
+ $this->add_submenu_item(
15237
+ $this->get_text_inline( 'Contact Us', 'contact-us' ),
15238
+ array( &$this, '_contact_page_render' ),
15239
+ $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Contact Us', 'contact-us' ),
15240
+ 'manage_options',
15241
+ 'contact',
15242
+ 'Freemius::_clean_admin_content_section',
15243
+ WP_FS__DEFAULT_PRIORITY,
15244
+ $this->is_submenu_item_visible( 'contact' )
15245
+ );
15246
+
15247
+ if ( $this->has_addons() ) {
15248
+ $this->add_submenu_item(
15249
+ $this->get_text_inline( 'Add-Ons', 'add-ons' ),
15250
+ array( &$this, '_addons_page_render' ),
15251
+ $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Add-Ons', 'add-ons' ),
15252
+ 'manage_options',
15253
+ 'addons',
15254
+ array( &$this, '_addons_page_load' ),
15255
+ WP_FS__LOWEST_PRIORITY - 1,
15256
+ $this->is_submenu_item_visible( 'addons' )
15257
+ );
15258
+ }
15259
+ }
15260
+
15261
+ if ( $add_submenu_items ||
15262
+ ( $is_activation_mode && $this->is_only_premium() && $this->is_admin_page( 'pricing' ) )
15263
+ ) {
15264
+ if ( ! WP_FS__DEMO_MODE ) {
15265
+ $show_pricing = (
15266
+ $this->is_submenu_item_visible( 'pricing' ) &&
15267
+ $this->is_pricing_page_visible()
15268
+ );
15269
+
15270
+ $pricing_cta_text = $this->get_pricing_cta_label();
15271
+ $pricing_class = 'upgrade-mode';
15272
+ if ( $show_pricing ) {
15273
+ if ( $this->is_in_trial_promotion() &&
15274
+ ! $this->is_paying_or_trial()
15275
+ ) {
15276
+ // If running a trial promotion, modify the pricing to load the trial.
15277
+ $pricing_class = 'trial-mode';
15278
+ } else if ( $this->is_paying() ) {
15279
+ $pricing_class = '';
15280
+ }
15281
+ }
15282
+
15283
+ // Add upgrade/pricing page.
15284
+ $this->add_submenu_item(
15285
+ $pricing_cta_text . '&nbsp;&nbsp;' . ( is_rtl() ? $this->get_text_x_inline( '&#x2190;', 'ASCII arrow left icon', 'symbol_arrow-left' ) : $this->get_text_x_inline( '&#x27a4;', 'ASCII arrow right icon', 'symbol_arrow-right' ) ),
15286
+ array( &$this, '_pricing_page_render' ),
15287
+ $this->get_plugin_name() . ' &ndash; ' . $this->get_text_x_inline( 'Pricing', 'noun', 'pricing' ),
15288
+ 'manage_options',
15289
+ 'pricing',
15290
+ 'Freemius::_clean_admin_content_section',
15291
+ WP_FS__LOWEST_PRIORITY,
15292
+ ( $add_submenu_items && $show_pricing ),
15293
+ $pricing_class
15294
+ );
15295
+ }
15296
+ }
15297
+
15298
+ if ( ! $is_activation_mode || ( true !== $this->_storage->require_license_activation ) ) {
15299
+ /**
15300
+ * Add the other menu items if there are any when not in activation mode or license activation is not
15301
+ * required (license activation is required for registered or anonymous users after activating the
15302
+ * premium version when the site is not in trial mode or there's no active valid license).
15303
+ *
15304
+ * @author Leo Fajardo (@leorw)
15305
+ * @since 2.2.1
15306
+ */
15307
+ if ( 0 < count( $this->_menu_items ) ) {
15308
+ if ( ! $this->_menu->is_top_level() ) {
15309
+ fs_enqueue_local_style( 'fs_common', '/admin/common.css' );
15310
+
15311
+ // Append submenu items right after the plugin's submenu item.
15312
+ $this->order_sub_submenu_items();
15313
+ } else {
15314
+ // Append submenu items.
15315
+ $this->embed_submenu_items();
15316
+ }
15317
+ }
15318
+ }
15319
+ }
15320
+
15321
+ /**
15322
+ * Moved the actual submenu item additions to a separated function,
15323
+ * in order to support sub-submenu items when the plugin's settings
15324
+ * only have a submenu and not top-level menu item.
15325
+ *
15326
+ * @author Vova Feldman (@svovaf)
15327
+ * @since 1.1.4
15328
+ */
15329
+ private function embed_submenu_items() {
15330
+ $item_template = $this->_menu->is_top_level() ?
15331
+ '<span class="fs-submenu-item %s %s %s">%s</span>' :
15332
+ '<span class="fs-submenu-item fs-sub %s %s %s">%s</span>';
15333
+
15334
+ $top_level_menu_capability = $this->get_top_level_menu_capability();
15335
+
15336
+ ksort( $this->_menu_items );
15337
+
15338
+ $is_first_submenu_item = true;
15339
+
15340
+ foreach ( $this->_menu_items as $priority => $items ) {
15341
+ foreach ( $items as $item ) {
15342
+ $capability = ( ! empty( $item['capability'] ) ? $item['capability'] : $top_level_menu_capability );
15343
+
15344
+ $menu_item = sprintf(
15345
+ $item_template,
15346
+ $this->get_unique_affix(),
15347
+ $item['menu_slug'],
15348
+ ! empty( $item['class'] ) ? $item['class'] : '',
15349
+ $item['menu_title']
15350
+ );
15351
+
15352
+ $top_level_menu_slug = $this->get_top_level_menu_slug();
15353
+ $menu_slug = $this->_menu->get_slug( $item['menu_slug'] );
15354
+
15355
+ if ( ! isset( $item['url'] ) ) {
15356
+ $hook = FS_Admin_Menu_Manager::add_subpage(
15357
+ $item['show_submenu'] ?
15358
+ $top_level_menu_slug :
15359
+ null,
15360
+ $item['page_title'],
15361
+ $menu_item,
15362
+ $capability,
15363
+ $menu_slug,
15364
+ $item['render_function']
15365
+ );
15366
+
15367
+ if ( false !== $item['before_render_function'] ) {
15368
+ add_action( "load-$hook", $item['before_render_function'] );
15369
+ }
15370
+ } else {
15371
+ FS_Admin_Menu_Manager::add_subpage(
15372
+ $item['show_submenu'] ?
15373
+ $top_level_menu_slug :
15374
+ null,
15375
+ $item['page_title'],
15376
+ $menu_item,
15377
+ $capability,
15378
+ $menu_slug,
15379
+ array( $this, '' )
15380
+ );
15381
+ }
15382
+
15383
+ if ( $item['show_submenu'] && $is_first_submenu_item ) {
15384
+ if ( $this->_is_network_active && ! empty( $this->_dynamically_added_top_level_page_hook_name ) ) {
15385
+ /**
15386
+ * If the top-level menu has been dynamically created, remove the first submenu item that
15387
+ * WordPress automatically creates when there's no submenu item whose slug matches the
15388
+ * parent's. In the following example, the `Awesome Plugin` submenu item will be removed.
15389
+ *
15390
+ * Awesome Plugin
15391
+ * - Awesome Plugin <-- we want to remove this since there's no real setting page for the top-level
15392
+ *
15393
+ * @author Leo Fajardo (@leorw)
15394
+ */
15395
+ remove_submenu_page( $top_level_menu_slug, $top_level_menu_slug );
15396
+ }
15397
+
15398
+ $is_first_submenu_item = false;
15399
+ }
15400
+ }
15401
+ }
15402
+ }
15403
+
15404
+ /**
15405
+ * Re-order the submenu items so all Freemius added new submenu items
15406
+ * are added right after the plugin's settings submenu item.
15407
+ *
15408
+ * @author Vova Feldman (@svovaf)
15409
+ * @since 1.1.4
15410
+ */
15411
+ private function order_sub_submenu_items() {
15412
+ global $submenu;
15413
+
15414
+ $menu_slug = $this->_menu->get_top_level_menu_slug();
15415
+
15416
+ /**
15417
+ * Before "admin_menu" fires, WordPress will loop over the default submenus and remove pages for which the user
15418
+ * does not have permissions. So in case a plugin does not have top-level menu but does have submenus under any
15419
+ * of the default menus, only users that have the right role can access its sub-submenus (Account, Contact Us,
15420
+ * Support Forum, etc.) since $submenu[ $menu_slug ] will be empty if the user doesn't have permission.
15421
+ *
15422
+ * In case a plugin does not have submenus under any of the default menus but does have submenus under the menu
15423
+ * of another plugin, only users that have the right role can access its sub-submenus since we will use the
15424
+ * capability needed to access the parent menu as the capability for the submenus that we will add.
15425
+ */
15426
+ if ( empty( $submenu[ $menu_slug ] ) ) {
15427
+ return;
15428
+ }
15429
+
15430
+ $top_level_menu = &$submenu[ $menu_slug ];
15431
+
15432
+ $all_submenu_items_after = array();
15433
+
15434
+ $found_submenu_item = false;
15435
+
15436
+ foreach ( $top_level_menu as $submenu_id => $meta ) {
15437
+ if ( $found_submenu_item ) {
15438
+ // Remove all submenu items after the plugin's submenu item.
15439
+ $all_submenu_items_after[] = $meta;
15440
+ unset( $top_level_menu[ $submenu_id ] );
15441
+ }
15442
+
15443
+ if ( $this->_menu->get_raw_slug() === $meta[2] ) {
15444
+ // Found the submenu item, put all below.
15445
+ $found_submenu_item = true;
15446
+ continue;
15447
+ }
15448
+ }
15449
+
15450
+ // Embed all plugin's new submenu items.
15451
+ $this->embed_submenu_items();
15452
+
15453
+ // Start with specially high number to make sure it's appended.
15454
+ $i = max( 10000, max( array_keys( $top_level_menu ) ) + 1 );
15455
+ foreach ( $all_submenu_items_after as $meta ) {
15456
+ $top_level_menu[ $i ] = $meta;
15457
+ $i ++;
15458
+ }
15459
+
15460
+ // Sort submenu items.
15461
+ ksort( $top_level_menu );
15462
+ }
15463
+
15464
+ /**
15465
+ * Helper method to return the module's support forum URL.
15466
+ *
15467
+ * @author Vova Feldman (@svovaf)
15468
+ * @since 1.2.2.7
15469
+ *
15470
+ * @return string
15471
+ */
15472
+ function get_support_forum_url() {
15473
+ return $this->apply_filters( 'support_forum_url', "https://wordpress.org/support/{$this->_module_type}/{$this->_slug}" );
15474
+ }
15475
+
15476
+ /**
15477
+ * Displays the Support Forum link when enabled.
15478
+ *
15479
+ * Can be filtered like so:
15480
+ *
15481
+ * function _fs_show_support_menu( $is_visible, $menu_id ) {
15482
+ * if ( 'support' === $menu_id ) {
15483
+ * return _fs->is_registered();
15484
+ * }
15485
+ * return $is_visible;
15486
+ * }
15487
+ * _fs()->add_filter('is_submenu_visible', '_fs_show_support_menu', 10, 2);
15488
+ *
15489
+ */
15490
+ function _add_default_submenu_items() {
15491
+ if ( ! $this->is_on() ) {
15492
+ return;
15493
+ }
15494
+
15495
+ if ( ! $this->is_activation_mode() &&
15496
+ ( ( $this->_is_network_active && fs_is_network_admin() ) ||
15497
+ ( ! $this->_is_network_active && is_admin() ) )
15498
+ ) {
15499
+ $this->add_submenu_link_item(
15500
+ $this->apply_filters( 'support_forum_submenu', $this->get_text_inline( 'Support Forum', 'support-forum' ) ),
15501
+ $this->get_support_forum_url(),
15502
+ 'wp-support-forum',
15503
+ null,
15504
+ 50,
15505
+ $this->is_submenu_item_visible( 'support' )
15506
+ );
15507
+ }
15508
+ }
15509
+
15510
+ /**
15511
+ * @author Vova Feldman (@svovaf)
15512
+ * @since 1.0.1
15513
+ *
15514
+ * @param string $menu_title
15515
+ * @param callable $render_function
15516
+ * @param bool|string $page_title
15517
+ * @param string $capability
15518
+ * @param bool|string $menu_slug
15519
+ * @param bool|callable $before_render_function
15520
+ * @param int $priority
15521
+ * @param bool $show_submenu
15522
+ * @param string $class Since 1.2.1.5 can add custom classes to menu items.
15523
+ */
15524
+ function add_submenu_item(
15525
+ $menu_title,
15526
+ $render_function,
15527
+ $page_title = false,
15528
+ $capability = 'manage_options',
15529
+ $menu_slug = false,
15530
+ $before_render_function = false,
15531
+ $priority = WP_FS__DEFAULT_PRIORITY,
15532
+ $show_submenu = true,
15533
+ $class = ''
15534
+ ) {
15535
+ $this->_logger->entrance( 'Title = ' . $menu_title );
15536
+
15537
+ if ( $this->is_addon() ) {
15538
+ $parent_fs = $this->get_parent_instance();
15539
+
15540
+ if ( is_object( $parent_fs ) ) {
15541
+ $parent_fs->add_submenu_item(
15542
+ $menu_title,
15543
+ $render_function,
15544
+ $page_title,
15545
+ $capability,
15546
+ $menu_slug,
15547
+ $before_render_function,
15548
+ $priority,
15549
+ $show_submenu,
15550
+ $class
15551
+ );
15552
+
15553
+ return;
15554
+ }
15555
+ }
15556
+
15557
+ if ( ! isset( $this->_menu_items[ $priority ] ) ) {
15558
+ $this->_menu_items[ $priority ] = array();
15559
+ }
15560
+
15561
+ $this->_menu_items[ $priority ][] = array(
15562
+ 'page_title' => is_string( $page_title ) ? $page_title : $menu_title,
15563
+ 'menu_title' => $menu_title,
15564
+ 'capability' => $capability,
15565
+ 'menu_slug' => is_string( $menu_slug ) ? $menu_slug : strtolower( $menu_title ),
15566
+ 'render_function' => $render_function,
15567
+ 'before_render_function' => $before_render_function,
15568
+ 'show_submenu' => $show_submenu,
15569
+ 'class' => $class,
15570
+ );
15571
+ }
15572
+
15573
+ /**
15574
+ * @author Vova Feldman (@svovaf)
15575
+ * @since 1.0.1
15576
+ *
15577
+ * @param string $menu_title
15578
+ * @param string $url
15579
+ * @param bool $menu_slug
15580
+ * @param string $capability
15581
+ * @param int $priority
15582
+ * @param bool $show_submenu
15583
+ */
15584
+ function add_submenu_link_item(
15585
+ $menu_title,
15586
+ $url,
15587
+ $menu_slug = false,
15588
+ $capability = 'read',
15589
+ $priority = WP_FS__DEFAULT_PRIORITY,
15590
+ $show_submenu = true
15591
+ ) {
15592
+ $this->_logger->entrance( 'Title = ' . $menu_title . '; Url = ' . $url );
15593
+
15594
+ if ( $this->is_addon() ) {
15595
+ $parent_fs = $this->get_parent_instance();
15596
+
15597
+ if ( is_object( $parent_fs ) ) {
15598
+ $parent_fs->add_submenu_link_item(
15599
+ $menu_title,
15600
+ $url,
15601
+ $menu_slug,
15602
+ $capability,
15603
+ $priority,
15604
+ $show_submenu
15605
+ );
15606
+
15607
+ return;
15608
+ }
15609
+ }
15610
+
15611
+ if ( ! isset( $this->_menu_items[ $priority ] ) ) {
15612
+ $this->_menu_items[ $priority ] = array();
15613
+ }
15614
+
15615
+ $this->_menu_items[ $priority ][] = array(
15616
+ 'menu_title' => $menu_title,
15617
+ 'capability' => $capability,
15618
+ 'menu_slug' => is_string( $menu_slug ) ? $menu_slug : strtolower( $menu_title ),
15619
+ 'url' => $url,
15620
+ 'page_title' => $menu_title,
15621
+ 'render_function' => 'fs_dummy',
15622
+ 'before_render_function' => '',
15623
+ 'show_submenu' => $show_submenu,
15624
+ );
15625
+ }
15626
+
15627
+ #endregion ------------------------------------------------------------------
15628
+
15629
+
15630
+ #--------------------------------------------------------------------------------
15631
+ #region Actions / Hooks / Filters
15632
+ #--------------------------------------------------------------------------------
15633
+
15634
+ /**
15635
+ * @author Vova Feldman (@svovaf)
15636
+ * @since 1.1.7
15637
+ *
15638
+ * @param string $tag
15639
+ *
15640
+ * @return string
15641
+ */
15642
+ public function get_action_tag( $tag ) {
15643
+ return self::get_action_tag_static( $tag, $this->_slug, $this->is_plugin() );
15644
+ }
15645
+
15646
+ /**
15647
+ * @author Vova Feldman (@svovaf)
15648
+ * @since 1.2.1.6
15649
+ *
15650
+ * @param string $tag
15651
+ * @param string $slug
15652
+ * @param bool $is_plugin
15653
+ *
15654
+ * @return string
15655
+ */
15656
+ static function get_action_tag_static( $tag, $slug = '', $is_plugin = true ) {
15657
+ $action = "fs_{$tag}";
15658
+
15659
+ if ( ! empty( $slug ) ) {
15660
+ $action .= '_' . self::get_module_unique_affix( $slug, $is_plugin );
15661
+ }
15662
+
15663
+ return $action;
15664
+ }
15665
+
15666
+ /**
15667
+ * Returns a string that can be used to generate a unique action name,
15668
+ * option name, HTML element ID, or HTML element class.
15669
+ *
15670
+ * @author Leo Fajardo (@leorw)
15671
+ * @since 1.2.2
15672
+ *
15673
+ * @return string
15674
+ */
15675
+ public function get_unique_affix() {
15676
+ return self::get_module_unique_affix( $this->_slug, $this->is_plugin() );
15677
+ }
15678
+
15679
+ /**
15680
+ * Returns a string that can be used to generate a unique action name,
15681
+ * option name, HTML element ID, or HTML element class.
15682
+ *
15683
+ * @author Vova Feldman (@svovaf)
15684
+ * @since 1.2.2.5
15685
+ *
15686
+ * @param string $slug
15687
+ * @param bool $is_plugin
15688
+ *
15689
+ * @return string
15690
+ */
15691
+ static function get_module_unique_affix( $slug, $is_plugin = true ) {
15692
+ $affix = $slug;
15693
+
15694
+ if ( ! $is_plugin ) {
15695
+ $affix .= '-' . WP_FS__MODULE_TYPE_THEME;
15696
+ }
15697
+
15698
+ return $affix;
15699
+ }
15700
+
15701
+ /**
15702
+ * @author Vova Feldman (@svovaf)
15703
+ * @since 1.2.1
15704
+ * @since 1.2.2.5 The AJAX action names are based on the module ID, not like the non-AJAX actions that are
15705
+ * based on the slug for backward compatibility.
15706
+ *
15707
+ * @param string $tag
15708
+ *
15709
+ * @return string
15710
+ */
15711
+ function get_ajax_action( $tag ) {
15712
+ return self::get_ajax_action_static( $tag, $this->_module_id );
15713
+ }
15714
+
15715
+ /**
15716
+ * @author Vova Feldman (@svovaf)
15717
+ * @since 1.2.1.7
15718
+ *
15719
+ * @param string $tag
15720
+ *
15721
+ * @return string
15722
+ */
15723
+ function get_ajax_security( $tag ) {
15724
+ return wp_create_nonce( $this->get_ajax_action( $tag ) );
15725
+ }
15726
+
15727
+ /**
15728
+ * @author Vova Feldman (@svovaf)
15729
+ * @since 1.2.1.7
15730
+ *
15731
+ * @param string $tag
15732
+ */
15733
+ function check_ajax_referer( $tag ) {
15734
+ check_ajax_referer( $this->get_ajax_action( $tag ), 'security' );
15735
+ }
15736
+
15737
+ /**
15738
+ * @author Vova Feldman (@svovaf)
15739
+ * @since 1.2.1.6
15740
+ * @since 1.2.2.5 The AJAX action names are based on the module ID, not like the non-AJAX actions that are
15741
+ * based on the slug for backward compatibility.
15742
+ *
15743
+ * @param string $tag
15744
+ * @param number|null $module_id
15745
+ *
15746
+ * @return string
15747
+ */
15748
+ private static function get_ajax_action_static( $tag, $module_id = null ) {
15749
+ $action = "fs_{$tag}";
15750
+
15751
+ if ( ! empty( $module_id ) ) {
15752
+ $action .= "_{$module_id}";
15753
+ }
15754
+
15755
+ return $action;
15756
+ }
15757
+
15758
+ /**
15759
+ * Do action, specific for the current context plugin.
15760
+ *
15761
+ * @author Vova Feldman (@svovaf)
15762
+ * @since 1.0.1
15763
+ *
15764
+ * @param string $tag The name of the action to be executed.
15765
+ * @param mixed $arg,... Optional. Additional arguments which are passed on to the
15766
+ * functions hooked to the action. Default empty.
15767
+ *
15768
+ * @uses do_action()
15769
+ */
15770
+ function do_action( $tag, $arg = '' ) {
15771
+ $this->_logger->entrance( $tag );
15772
+
15773
+ $args = func_get_args();
15774
+
15775
+ call_user_func_array( 'do_action', array_merge(
15776
+ array( $this->get_action_tag( $tag ) ),
15777
+ array_slice( $args, 1 ) )
15778
+ );
15779
+ }
15780
+
15781
+ /**
15782
+ * Add action, specific for the current context plugin.
15783
+ *
15784
+ * @author Vova Feldman (@svovaf)
15785
+ * @since 1.0.1
15786
+ *
15787
+ * @param string $tag
15788
+ * @param callable $function_to_add
15789
+ * @param int $priority
15790
+ * @param int $accepted_args
15791
+ *
15792
+ * @uses add_action()
15793
+ */
15794
+ function add_action(
15795
+ $tag,
15796
+ $function_to_add,
15797
+ $priority = WP_FS__DEFAULT_PRIORITY,
15798
+ $accepted_args = 1
15799
+ ) {
15800
+ $this->_logger->entrance( $tag );
15801
+
15802
+ add_action( $this->get_action_tag( $tag ), $function_to_add, $priority, $accepted_args );
15803
+ }
15804
+
15805
+ /**
15806
+ * Add AJAX action, specific for the current context plugin.
15807
+ *
15808
+ * @author Vova Feldman (@svovaf)
15809
+ * @since 1.2.1
15810
+ *
15811
+ * @param string $tag
15812
+ * @param callable $function_to_add
15813
+ * @param int $priority
15814
+ *
15815
+ * @uses add_action()
15816
+ *
15817
+ * @return bool True if action added, false if no need to add the action since the AJAX call isn't matching.
15818
+ */
15819
+ function add_ajax_action(
15820
+ $tag,
15821
+ $function_to_add,
15822
+ $priority = WP_FS__DEFAULT_PRIORITY
15823
+ ) {
15824
+ $this->_logger->entrance( $tag );
15825
+
15826
+ return self::add_ajax_action_static(
15827
+ $tag,
15828
+ $function_to_add,
15829
+ $priority,
15830
+ $this->_module_id
15831
+ );
15832
+ }
15833
+
15834
+ /**
15835
+ * Add AJAX action.
15836
+ *
15837
+ * @author Vova Feldman (@svovaf)
15838
+ * @since 1.2.1.6
15839
+ *
15840
+ * @param string $tag
15841
+ * @param callable $function_to_add
15842
+ * @param int $priority
15843
+ * @param number|null $module_id
15844
+ *
15845
+ * @return bool True if action added, false if no need to add the action since the AJAX call isn't matching.
15846
+ * @uses add_action()
15847
+ *
15848
+ */
15849
+ static function add_ajax_action_static(
15850
+ $tag,
15851
+ $function_to_add,
15852
+ $priority = WP_FS__DEFAULT_PRIORITY,
15853
+ $module_id = null
15854
+ ) {
15855
+ self::$_static_logger->entrance( $tag );
15856
+
15857
+ if ( ! self::is_ajax_action_static( $tag, $module_id ) ) {
15858
+ return false;
15859
+ }
15860
+
15861
+ add_action(
15862
+ 'wp_ajax_' . self::get_ajax_action_static( $tag, $module_id ),
15863
+ $function_to_add,
15864
+ $priority,
15865
+ 0
15866
+ );
15867
+
15868
+ self::$_static_logger->info( "$tag AJAX callback action added." );
15869
+
15870
+ return true;
15871
+ }
15872
+
15873
+ /**
15874
+ * Send a JSON response back to an Ajax request.
15875
+ *
15876
+ * @author Vova Feldman (@svovaf)
15877
+ * @since 1.2.1.5
15878
+ *
15879
+ * @param mixed $response
15880
+ */
15881
+ static function shoot_ajax_response( $response ) {
15882
+ wp_send_json( $response );
15883
+ }
15884
+
15885
+ /**
15886
+ * Send a JSON response back to an Ajax request, indicating success.
15887
+ *
15888
+ * @author Vova Feldman (@svovaf)
15889
+ * @since 1.2.1.5
15890
+ *
15891
+ * @param mixed $data Data to encode as JSON, then print and exit.
15892
+ */
15893
+ static function shoot_ajax_success( $data = null ) {
15894
+ wp_send_json_success( $data );
15895
+ }
15896
+
15897
+ /**
15898
+ * Send a JSON response back to an Ajax request, indicating failure.
15899
+ *
15900
+ * @author Vova Feldman (@svovaf)
15901
+ * @since 1.2.1.5
15902
+ *
15903
+ * @param mixed $error Optional error message.
15904
+ */
15905
+ static function shoot_ajax_failure( $error = '' ) {
15906
+ $result = array( 'success' => false );
15907
+ if ( ! empty( $error ) ) {
15908
+ $result['error'] = $error;
15909
+ }
15910
+
15911
+ wp_send_json( $result );
15912
+ }
15913
+
15914
+ /**
15915
+ * Apply filter, specific for the current context plugin.
15916
+ *
15917
+ * @author Vova Feldman (@svovaf)
15918
+ * @since 1.0.9
15919
+ *
15920
+ * @param string $tag The name of the filter hook.
15921
+ * @param mixed $value The value on which the filters hooked to `$tag` are applied on.
15922
+ *
15923
+ * @return mixed The filtered value after all hooked functions are applied to it.
15924
+ *
15925
+ * @uses apply_filters()
15926
+ */
15927
+ function apply_filters( $tag, $value ) {
15928
+ $this->_logger->entrance( $tag );
15929
+
15930
+ $args = func_get_args();
15931
+ array_unshift( $args, $this->get_unique_affix() );
15932
+
15933
+ return call_user_func_array( 'fs_apply_filter', $args );
15934
+ }
15935
+
15936
+ /**
15937
+ * Add filter, specific for the current context plugin.
15938
+ *
15939
+ * @author Vova Feldman (@svovaf)
15940
+ * @since 1.0.9
15941
+ *
15942
+ * @param string $tag
15943
+ * @param callable $function_to_add
15944
+ * @param int $priority
15945
+ * @param int $accepted_args
15946
+ *
15947
+ * @uses add_filter()
15948
+ */
15949
+ function add_filter( $tag, $function_to_add, $priority = WP_FS__DEFAULT_PRIORITY, $accepted_args = 1 ) {
15950
+ $this->_logger->entrance( $tag );
15951
+
15952
+ add_filter( $this->get_action_tag( $tag ), $function_to_add, $priority, $accepted_args );
15953
+ }
15954
+
15955
+ /**
15956
+ * Check if has filter.
15957
+ *
15958
+ * @author Vova Feldman (@svovaf)
15959
+ * @since 1.1.4
15960
+ *
15961
+ * @param string $tag
15962
+ * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
15963
+ *
15964
+ * @return false|int
15965
+ *
15966
+ * @uses has_filter()
15967
+ */
15968
+ function has_filter( $tag, $function_to_check = false ) {
15969
+ $this->_logger->entrance( $tag );
15970
+
15971
+ return has_filter( $this->get_action_tag( $tag ), $function_to_check );
15972
+ }
15973
+
15974
+ #endregion
15975
+
15976
+ /**
15977
+ * Override default i18n text phrases.
15978
+ *
15979
+ * @author Vova Feldman (@svovaf)
15980
+ * @since 1.1.6
15981
+ *
15982
+ * @param string[] string $key_value
15983
+ *
15984
+ * @uses fs_override_i18n()
15985
+ */
15986
+ function override_i18n( $key_value ) {
15987
+ fs_override_i18n( $key_value, $this->_slug );
15988
+ }
15989
+
15990
+ /* Account Page
15991
+ ------------------------------------------------------------------------------------------------------------------*/
15992
+ /**
15993
+ * Update site information.
15994
+ *
15995
+ * @author Vova Feldman (@svovaf)
15996
+ * @since 1.0.1
15997
+ *
15998
+ * @param bool $store Flush to Database if true.
15999
+ * @param null|int $network_level_or_blog_id Since 2.0.0
16000
+ * @param \FS_Site $site Since 2.0.0
16001
+ */
16002
+ private function _store_site( $store = true, $network_level_or_blog_id = null, FS_Site $site = null ) {
16003
+ $this->_logger->entrance();
16004
+
16005
+ if ( empty( $this->_site->id ) ) {
16006
+ $this->_logger->error( "Empty install ID, can't store site." );
16007
+
16008
+ return;
16009
+ }
16010
+
16011
+ $site_clone = is_object( $site ) ? $site : $this->_site;
16012
+ $encrypted_site = clone $site_clone;
16013
+
16014
+ $sites = self::get_all_sites( $this->_module_type, $network_level_or_blog_id );
16015
+
16016
+ $prev_stored_user_id = $this->_storage->get( 'prev_user_id', false, $network_level_or_blog_id );
16017
+
16018
+ if ( empty( $prev_stored_user_id ) &&
16019
+ $this->_user->id != $this->_site->user_id
16020
+ ) {
16021
+ /**
16022
+ * Store the current user ID as the previous user ID so that the previous user can be used
16023
+ * as the install's owner while the new owner's details are not yet available.
16024
+ *
16025
+ * This will be executed only in the `replica` site. For example, there are 2 sites, namely `original`
16026
+ * and `replica`, then an ownership change was initiated and completed in the `original`, the `replica`
16027
+ * will be using the previous user until it is updated again (e.g.: until the next clone of `original`
16028
+ * into `replica`.
16029
+ *
16030
+ * @author Leo Fajardo (@leorw)
16031
+ */
16032
+ $this->_storage->store( 'prev_user_id', $sites[ $this->_slug ]->user_id, $network_level_or_blog_id );
16033
+ }
16034
+
16035
+ $sites[ $this->_slug ] = $encrypted_site;
16036
+
16037
+ $this->set_account_option( 'sites', $sites, $store, $network_level_or_blog_id );
16038
+ }
16039
+
16040
+ /**
16041
+ * Update plugin's plans information.
16042
+ *
16043
+ * @author Vova Feldman (@svovaf)
16044
+ * @since 1.0.2
16045
+ *
16046
+ * @param bool $store Flush to Database if true.
16047
+ */
16048
+ private function _store_plans( $store = true ) {
16049
+ $this->_logger->entrance();
16050
+
16051
+ $plans = self::get_all_plans( $this->_module_type );
16052
+
16053
+ // Copy plans.
16054
+ $encrypted_plans = array();
16055
+ for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
16056
+ $encrypted_plans[] = self::_encrypt_entity( $this->_plans[ $i ] );
16057
+ }
16058
+
16059
+ $plans[ $this->_slug ] = $encrypted_plans;
16060
+
16061
+ $this->set_account_option( 'plans', $plans, $store );
16062
+ }
16063
+
16064
+ /**
16065
+ * Update user's plugin licenses.
16066
+ *
16067
+ * @author Vova Feldman (@svovaf)
16068
+ * @since 1.0.5
16069
+ *
16070
+ * @param bool $store
16071
+ * @param number|bool $module_id
16072
+ * @param FS_Plugin_License[] $licenses
16073
+ */
16074
+ private function _store_licenses( $store = true, $module_id = false, $licenses = array() ) {
16075
+ $this->_logger->entrance();
16076
+
16077
+ $all_licenses = self::get_all_licenses();
16078
+
16079
+ if ( ! FS_Plugin::is_valid_id( $module_id ) ) {
16080
+ $module_id = $this->_module_id;
16081
+
16082
+ $user_licenses = is_array( $this->_licenses ) ?
16083
+ $this->_licenses :
16084
+ array();
16085
+
16086
+ if ( empty( $user_licenses ) ) {
16087
+ // If the context user doesn't have any license, don't update the licenses collection.
16088
+ return;
16089
+ }
16090
+
16091
+ $new_user_licenses_map = array();
16092
+ foreach ( $user_licenses as $user_license ) {
16093
+ $new_user_licenses_map[ $user_license->id ] = $user_license;
16094
+ }
16095
+
16096
+ self::store_user_id_license_ids_map( array_keys( $new_user_licenses_map ), $this->_module_id, $this->_user->id );
16097
+
16098
+ // Update user licenses.
16099
+ $licenses_to_update_count = count( $new_user_licenses_map );
16100
+ foreach ( $all_licenses[ $module_id ] as $key => $license ) {
16101
+ if ( 0 === $licenses_to_update_count ) {
16102
+ break;
16103
+ }
16104
+
16105
+ if ( isset( $new_user_licenses_map[ $license->id ] ) ) {
16106
+ // Update license.
16107
+ $all_licenses[ $module_id ][ $key ] = $new_user_licenses_map[ $license->id ];
16108
+ unset( $new_user_licenses_map[ $license->id ] );
16109
+
16110
+ $licenses_to_update_count --;
16111
+ }
16112
+ }
16113
+
16114
+ if ( ! empty( $new_user_licenses_map ) ) {
16115
+ // Add new licenses.
16116
+ $all_licenses[ $module_id ] = array_merge( array_values( $new_user_licenses_map ), $all_licenses[ $module_id ] );
16117
+ }
16118
+
16119
+ $licenses = $all_licenses[ $module_id ];
16120
+ }
16121
+
16122
+ if ( ! isset( $all_licenses[ $module_id ] ) ) {
16123
+ $all_licenses[ $module_id ] = array();
16124
+ }
16125
+
16126
+ $all_licenses[ $module_id ] = $licenses;
16127
+
16128
+ self::$_accounts->set_option( 'all_licenses', $all_licenses, $store );
16129
+ }
16130
+
16131
+ /**
16132
+ * Update user information.
16133
+ *
16134
+ * @author Vova Feldman (@svovaf)
16135
+ * @since 1.0.1
16136
+ *
16137
+ * @param bool $store Flush to Database if true.
16138
+ */
16139
+ private function _store_user( $store = true ) {
16140
+ $this->_logger->entrance();
16141
+
16142
+ if ( empty( $this->_user->id ) ) {
16143
+ $this->_logger->error( "Empty user ID, can't store user." );
16144
+
16145
+ return;
16146
+ }
16147
+
16148
+ $users = self::get_all_users();
16149
+ $users[ $this->_user->id ] = $this->_user;
16150
+ self::$_accounts->set_option( 'users', $users, $store );
16151
+ }
16152
+
16153
+ /**
16154
+ * Update new updates information.
16155
+ *
16156
+ * @author Vova Feldman (@svovaf)
16157
+ * @since 1.0.4
16158
+ *
16159
+ * @param FS_Plugin_Tag|null $update
16160
+ * @param bool $store Flush to Database if true.
16161
+ * @param bool|number $plugin_id
16162
+ */
16163
+ private function _store_update( $update, $store = true, $plugin_id = false ) {
16164
+ $this->_logger->entrance();
16165
+
16166
+ if ( $update instanceof FS_Plugin_Tag ) {
16167
+ $update->updated = time();
16168
+ }
16169
+
16170
+ if ( ! is_numeric( $plugin_id ) ) {
16171
+ $plugin_id = $this->_plugin->id;
16172
+ }
16173
+
16174
+ $updates = self::get_all_updates();
16175
+ $updates[ $plugin_id ] = $update;
16176
+ self::$_accounts->set_option( 'updates', $updates, $store );
16177
+ }
16178
+
16179
+ /**
16180
+ * Update new updates information.
16181
+ *
16182
+ * @author Vova Feldman (@svovaf)
16183
+ * @since 1.0.6
16184
+ *
16185
+ * @param FS_Plugin[] $plugin_addons
16186
+ * @param bool $store Flush to Database if true.
16187
+ */
16188
+ private function _store_addons( $plugin_addons, $store = true ) {
16189
+ $this->_logger->entrance();
16190
+
16191
+ $addons = self::get_all_addons();
16192
+ $addons[ $this->_plugin->id ] = $plugin_addons;
16193
+ self::$_accounts->set_option( 'addons', $addons, $store );
16194
+ }
16195
+
16196
+ /**
16197
+ * Delete plugin's associated add-ons.
16198
+ *
16199
+ * @author Vova Feldman (@svovaf)
16200
+ * @since 1.0.8
16201
+ *
16202
+ * @param bool $store
16203
+ *
16204
+ * @return bool
16205
+ */
16206
+ private function _delete_account_addons( $store = true ) {
16207
+ $all_addons = self::get_all_account_addons();
16208
+
16209
+ if ( ! isset( $all_addons[ $this->_plugin->id ] ) ) {
16210
+ return false;
16211
+ }
16212
+
16213
+ unset( $all_addons[ $this->_plugin->id ] );
16214
+
16215
+ self::$_accounts->set_option( 'account_addons', $all_addons, $store );
16216
+
16217
+ return true;
16218
+ }
16219
+
16220
+ /**
16221
+ * Update account add-ons list.
16222
+ *
16223
+ * @author Vova Feldman (@svovaf)
16224
+ * @since 1.0.6
16225
+ *
16226
+ * @param FS_Plugin[] $addons
16227
+ * @param bool $store Flush to Database if true.
16228
+ */
16229
+ private function _store_account_addons( $addons, $store = true ) {
16230
+ $this->_logger->entrance();
16231
+
16232
+ $all_addons = self::get_all_account_addons();
16233
+ $all_addons[ $this->_plugin->id ] = $addons;
16234
+ self::$_accounts->set_option( 'account_addons', $all_addons, $store );
16235
+ }
16236
+
16237
+ /**
16238
+ * Store account params in the Database.
16239
+ *
16240
+ * @author Vova Feldman (@svovaf)
16241
+ * @since 1.0.1
16242
+ *
16243
+ * @param null|int $blog_id Since 2.0.0
16244
+ */
16245
+ private function _store_account( $blog_id = null ) {
16246
+ $this->_logger->entrance();
16247
+
16248
+ $this->_store_site( false, $blog_id );
16249
+ $this->_store_user( false );
16250
+ $this->_store_plans( false );
16251
+ $this->_store_licenses( false );
16252
+
16253
+ self::$_accounts->store( $blog_id );
16254
+ }
16255
+
16256
+ /**
16257
+ * Sync user's information.
16258
+ *
16259
+ * @author Vova Feldman (@svovaf)
16260
+ * @since 1.0.3
16261
+ * @uses FS_Api
16262
+ */
16263
+ private function _handle_account_user_sync() {
16264
+ $this->_logger->entrance();
16265
+
16266
+ $api = $this->get_api_user_scope();
16267
+
16268
+ // Get user's information.
16269
+ $user = $api->get( '/', true );
16270
+
16271
+ if ( isset( $user->id ) ) {
16272
+ $this->_user->first = $user->first;
16273
+ $this->_user->last = $user->last;
16274
+ $this->_user->email = $user->email;
16275
+
16276
+ $is_menu_item_account_visible = $this->is_submenu_item_visible( 'account' );
16277
+
16278
+ if ( $user->is_verified &&
16279
+ ( ! isset( $this->_user->is_verified ) || false === $this->_user->is_verified )
16280
+ ) {
16281
+ $this->_user->is_verified = true;
16282
+
16283
+ $this->do_action( 'account_email_verified', $user->email );
16284
+
16285
+ $this->_admin_notices->add(
16286
+ $this->get_text_inline( 'Your email has been successfully verified - you are AWESOME!', 'email-verified-message' ),
16287
+ $this->get_text_x_inline( 'Right on', 'a positive response', 'right-on' ) . '!',
16288
+ 'success',
16289
+ // Make admin sticky if account menu item is invisible,
16290
+ // since the page will be auto redirected to the plugin's
16291
+ // main settings page, and the non-sticky message
16292
+ // will disappear.
16293
+ ! $is_menu_item_account_visible,
16294
+ 'email_verified'
16295
+ );
16296
+ }
16297
+
16298
+ // Flush user details to DB.
16299
+ $this->_store_user();
16300
+
16301
+ $this->do_action( 'after_account_user_sync', $user );
16302
+
16303
+ /**
16304
+ * If account menu item is hidden, redirect to plugin's main settings page.
16305
+ *
16306
+ * @author Vova Feldman (@svovaf)
16307
+ * @since 1.1.6
16308
+ *
16309
+ * @link https://github.com/Freemius/wordpress-sdk/issues/6
16310
+ */
16311
+ if ( ! $is_menu_item_account_visible ) {
16312
+ fs_redirect( $this->_get_admin_page_url() );
16313
+ }
16314
+ }
16315
+ }
16316
+
16317
+ /**
16318
+ * @author Vova Feldman (@svovaf)
16319
+ * @since 1.0.9
16320
+ * @uses FS_Api
16321
+ *
16322
+ * @param number|bool $license_id
16323
+ *
16324
+ * @return FS_Subscription|object|bool
16325
+ */
16326
+ private function _fetch_site_license_subscription( $license_id = false ) {
16327
+ $this->_logger->entrance();
16328
+ $api = $this->get_api_site_scope();
16329
+
16330
+ if ( ! is_numeric( $license_id ) ) {
16331
+ $license_id = $this->_license->id;
16332
+ }
16333
+
16334
+ $result = $api->get( "/licenses/{$license_id}/subscriptions.json", true );
16335
+
16336
+ return ! isset( $result->error ) ?
16337
+ ( ( is_array( $result->subscriptions ) && 0 < count( $result->subscriptions ) ) ?
16338
+ new FS_Subscription( $result->subscriptions[0] ) :
16339
+ false
16340
+ ) :
16341
+ $result;
16342
+ }
16343
+
16344
+ /**
16345
+ * @author Vova Feldman (@svovaf)
16346
+ * @since 1.0.4
16347
+ * @uses FS_Api
16348
+ *
16349
+ * @param number|bool $plan_id
16350
+ *
16351
+ * @return FS_Plugin_Plan|object
16352
+ */
16353
+ private function _fetch_site_plan( $plan_id = false ) {
16354
+ $this->_logger->entrance();
16355
+ $api = $this->get_api_site_scope();
16356
+
16357
+ if ( ! is_numeric( $plan_id ) ) {
16358
+ $plan_id = $this->_site->plan_id;
16359
+ }
16360
+
16361
+ $plan = $api->get( "/plans/{$plan_id}.json", true );
16362
+
16363
+ return ! isset( $plan->error ) ? new FS_Plugin_Plan( $plan ) : $plan;
16364
+ }
16365
+
16366
+ /**
16367
+ * @author Vova Feldman (@svovaf)
16368
+ * @since 1.0.5
16369
+ * @uses FS_Api
16370
+ *
16371
+ * @return FS_Plugin_Plan[]|object
16372
+ */
16373
+ private function _fetch_plugin_plans() {
16374
+ $this->_logger->entrance();
16375
+ $api = $this->get_current_or_network_user_api_scope();
16376
+
16377
+ /**
16378
+ * @since 1.2.3 When running in DEV mode, retrieve pending plans as well.
16379
+ */
16380
+ $result = $api->get( $this->add_show_pending( "/plugins/{$this->_module_id}/plans.json" ), true );
16381
+
16382
+ if ( $this->is_api_result_object( $result, 'plans' ) && is_array( $result->plans ) ) {
16383
+ for ( $i = 0, $len = count( $result->plans ); $i < $len; $i ++ ) {
16384
+ $result->plans[ $i ] = new FS_Plugin_Plan( $result->plans[ $i ] );
16385
+ }
16386
+
16387
+ $result = $result->plans;
16388
+ }
16389
+
16390
+ return $result;
16391
+ }
16392
+
16393
+ /**
16394
+ * @author Vova Feldman (@svovaf)
16395
+ * @since 2.0.0
16396
+ *
16397
+ * @param number $plan_id
16398
+ *
16399
+ * @return \FS_Plugin_Plan|object
16400
+ */
16401
+ private function fetch_plan_by_id( $plan_id ) {
16402
+ $this->_logger->entrance();
16403
+ $api = $this->get_current_or_network_user_api_scope();
16404
+
16405
+ $result = $api->get( "/plugins/{$this->_module_id}/plans/{$plan_id}.json", true );
16406
+
16407
+ return $this->is_api_result_entity( $result ) ?
16408
+ new FS_Plugin_Plan( $result ) :
16409
+ $result;
16410
+ }
16411
+
16412
+ /**
16413
+ * @author Vova Feldman (@svovaf)
16414
+ * @since 1.0.5
16415
+ * @uses FS_Api
16416
+ *
16417
+ * @param number|bool $plugin_id
16418
+ * @param number|bool $site_license_id
16419
+ * @param array $foreign_licenses @since 2.0.0. This is used by network-activated plugins.
16420
+ * @param number|null $blog_id
16421
+ *
16422
+ * @return FS_Plugin_License[]|object
16423
+ */
16424
+ private function _fetch_licenses(
16425
+ $plugin_id = false,
16426
+ $site_license_id = false,
16427
+ $foreign_licenses = array(),
16428
+ $blog_id = null
16429
+ ) {
16430
+ $this->_logger->entrance();
16431
+
16432
+ $api = $this->get_api_user_scope();
16433
+
16434
+ if ( ! is_numeric( $plugin_id ) ) {
16435
+ $plugin_id = $this->_plugin->id;
16436
+ }
16437
+
16438
+ $user_licenses_endpoint = "/plugins/{$plugin_id}/licenses.json";
16439
+ if ( ! empty ( $foreign_licenses ) ) {
16440
+ $foreign_licenses = array(
16441
+ // Prefix with `+` to tell the server to include foreign licenses in the licenses collection.
16442
+ 'ids' => ( urlencode( '+' ) . implode( ',', $foreign_licenses['ids'] ) ),
16443
+ 'license_keys' => implode( ',', array_map( 'urlencode', $foreign_licenses['license_keys'] ) )
16444
+ );
16445
+
16446
+ $user_licenses_endpoint = add_query_arg( $foreign_licenses, $user_licenses_endpoint );
16447
+ }
16448
+
16449
+ $result = $api->get( $user_licenses_endpoint, true );
16450
+
16451
+ $is_site_license_synced = false;
16452
+
16453
+ $api_errors = array();
16454
+
16455
+ if ( $this->is_api_result_object( $result, 'licenses' ) &&
16456
+ is_array( $result->licenses )
16457
+ ) {
16458
+ for ( $i = 0, $len = count( $result->licenses ); $i < $len; $i ++ ) {
16459
+ $result->licenses[ $i ] = new FS_Plugin_License( $result->licenses[ $i ] );
16460
+
16461
+ if ( ( ! $is_site_license_synced ) && is_numeric( $site_license_id ) ) {
16462
+ $is_site_license_synced = ( $site_license_id == $result->licenses[ $i ]->id );
16463
+ }
16464
+ }
16465
+
16466
+ $result = $result->licenses;
16467
+ } else {
16468
+ $api_errors[] = $result;
16469
+ $result = array();
16470
+ }
16471
+
16472
+ if ( ! $is_site_license_synced ) {
16473
+ if ( ! is_null( $blog_id ) ) {
16474
+ /**
16475
+ * If blog ID is not null, the request is for syncing of the license of a single site via the
16476
+ * network-level "Account" page.
16477
+ *
16478
+ * @author Leo Fajardo (@leorw)
16479
+ */
16480
+ $this->switch_to_blog( $blog_id );
16481
+ }
16482
+
16483
+ $api = $this->get_api_site_scope();
16484
+
16485
+ if ( is_numeric( $site_license_id ) ) {
16486
+ // Try to retrieve a foreign license that is linked to the install.
16487
+ $api_result = $api->call( '/licenses.json' );
16488
+
16489
+ if ( $this->is_api_result_object( $api_result, 'licenses' ) &&
16490
+ is_array( $api_result->licenses )
16491
+ ) {
16492
+ $licenses = $api_result->licenses;
16493
+
16494
+ if ( ! empty( $licenses ) ) {
16495
+ $result[] = new FS_Plugin_License( $licenses[0] );
16496
+ }
16497
+ } else {
16498
+ $api_errors[] = $api_result;
16499
+ }
16500
+ } else if ( is_object( $this->_license ) ) {
16501
+ $is_license_in_result = false;
16502
+ if ( ! empty( $result ) ) {
16503
+ foreach ( $result as $license ) {
16504
+ if ( $license->id == $this->_license->id ) {
16505
+ $is_license_in_result = true;
16506
+ break;
16507
+ }
16508
+ }
16509
+ }
16510
+
16511
+ if ( ! $is_license_in_result ) {
16512
+ // Fetch foreign license by ID and license key.
16513
+ $license = $api->get( "/licenses/{$this->_license->id}.json?license_key=" .
16514
+ urlencode( $this->_license->secret_key ) );
16515
+
16516
+ if ( $this->is_api_result_entity( $license ) ) {
16517
+ $result[] = new FS_Plugin_License( $license );
16518
+ } else {
16519
+ $api_errors[] = $license;
16520
+ }
16521
+ }
16522
+ }
16523
+
16524
+ if ( ! is_null( $blog_id ) ) {
16525
+ $this->switch_to_blog( $this->_storage->network_install_blog_id );
16526
+ }
16527
+ }
16528
+
16529
+ if ( is_array( $result ) && 0 < count( $result ) ) {
16530
+ // If found at least one license, return license collection even if there are errors.
16531
+ return $result;
16532
+ }
16533
+
16534
+ if ( ! empty( $api_errors ) ) {
16535
+ // If found any errors and no licenses, return first error.
16536
+ return $api_errors[0];
16537
+ }
16538
+
16539
+ // Fallback to empty licenses list.
16540
+ return $result;
16541
+ }
16542
+
16543
+ /**
16544
+ * @author Vova Feldman (@svovaf)
16545
+ * @since 2.0.0
16546
+ *
16547
+ * @param number $license_id
16548
+ * @param string $license_key
16549
+ *
16550
+ * @return \FS_Plugin_License|object
16551
+ */
16552
+ private function fetch_license_by_key( $license_id, $license_key ) {
16553
+ $this->_logger->entrance();
16554
+
16555
+ $api = $this->get_current_or_network_user_api_scope();
16556
+
16557
+ $result = $api->get( "/licenses/{$license_id}.json?license_key=" . urlencode( $license_key ) );
16558
+
16559
+ return $this->is_api_result_entity( $result ) ?
16560
+ new FS_Plugin_License( $result ) :
16561
+ $result;
16562
+ }
16563
+
16564
+ /**
16565
+ * @author Vova Feldman (@svovaf)
16566
+ * @since 1.2.0
16567
+ * @uses FS_Api
16568
+ *
16569
+ * @param number|bool $plugin_id
16570
+ * @param bool $flush
16571
+ *
16572
+ * @return FS_Payment[]|object
16573
+ */
16574
+ function _fetch_payments( $plugin_id = false, $flush = false ) {
16575
+ $this->_logger->entrance();
16576
+
16577
+ $api = $this->get_api_user_scope();
16578
+
16579
+ if ( ! is_numeric( $plugin_id ) ) {
16580
+ $plugin_id = $this->_plugin->id;
16581
+ }
16582
+
16583
+ $result = $api->get( "/plugins/{$plugin_id}/payments.json?include_addons=true", $flush );
16584
+
16585
+ if ( ! isset( $result->error ) ) {
16586
+ for ( $i = 0, $len = count( $result->payments ); $i < $len; $i ++ ) {
16587
+ $result->payments[ $i ] = new FS_Payment( $result->payments[ $i ] );
16588
+ }
16589
+ $result = $result->payments;
16590
+ }
16591
+
16592
+ return $result;
16593
+ }
16594
+
16595
+ /**
16596
+ * @author Vova Feldman (@svovaf)
16597
+ * @since 1.2.1.5
16598
+ * @uses FS_Api
16599
+ *
16600
+ * @param bool $flush
16601
+ *
16602
+ * @return \FS_Billing|mixed
16603
+ */
16604
+ function _fetch_billing( $flush = false ) {
16605
+ require_once WP_FS__DIR_INCLUDES . '/entities/class-fs-billing.php';
16606
+
16607
+ $billing = $this->get_api_user_scope()->get( 'billing.json', $flush );
16608
+
16609
+ if ( $this->is_api_result_entity( $billing ) ) {
16610
+ $billing = new FS_Billing( $billing );
16611
+ }
16612
+
16613
+ return $billing;
16614
+ }
16615
+
16616
+ /**
16617
+ * @author Vova Feldman (@svovaf)
16618
+ * @since 1.0.5
16619
+ *
16620
+ * @param FS_Plugin_License[] $licenses
16621
+ * @param number $module_id
16622
+ */
16623
+ private function _update_licenses( $licenses, $module_id ) {
16624
+ $this->_logger->entrance();
16625
+
16626
+ if ( is_array( $licenses ) ) {
16627
+ for ( $i = 0, $len = count( $licenses ); $i < $len; $i ++ ) {
16628
+ $licenses[ $i ]->updated = time();
16629
+ }
16630
+ }
16631
+
16632
+ $this->_store_licenses( true, $module_id, $licenses );
16633
+ }
16634
+
16635
+ /**
16636
+ * @author Vova Feldman (@svovaf)
16637
+ * @since 1.0.4
16638
+ *
16639
+ * @param bool|number $plugin_id
16640
+ * @param bool $flush Since 1.1.7.3
16641
+ * @param int $expiration Since 1.2.2.7
16642
+ * @param bool|string $newer_than Since 2.2.1
16643
+ *
16644
+ * @return object|false New plugin tag info if exist.
16645
+ */
16646
+ private function _fetch_newer_version( $plugin_id = false, $flush = true, $expiration = WP_FS__TIME_24_HOURS_IN_SEC, $newer_than = false ) {
16647
+ $latest_tag = $this->_fetch_latest_version( $plugin_id, $flush, $expiration, $newer_than );
16648
+
16649
+ if ( ! is_object( $latest_tag ) ) {
16650
+ return false;
16651
+ }
16652
+
16653
+ // Check if version is actually newer.
16654
+ $has_new_version =
16655
+ // If it's an non-installed add-on then always return latest.
16656
+ ( $this->_is_addon_id( $plugin_id ) && ! $this->is_addon_activated( $plugin_id ) ) ||
16657
+ // Compare versions.
16658
+ version_compare( $this->get_plugin_version(), $latest_tag->version, '<' );
16659
+
16660
+ $this->_logger->departure( $has_new_version ? 'Found newer plugin version ' . $latest_tag->version : 'No new version' );
16661
+
16662
+ return $has_new_version ? $latest_tag : false;
16663
+ }
16664
+
16665
+ /**
16666
+ * @author Vova Feldman (@svovaf)
16667
+ * @since 1.0.5
16668
+ *
16669
+ * @param bool|number $plugin_id
16670
+ * @param bool $flush Since 1.1.7.3
16671
+ * @param int $expiration Since 1.2.2.7
16672
+ * @param bool|string $newer_than Since 2.2.1
16673
+ *
16674
+ * @return bool|FS_Plugin_Tag
16675
+ */
16676
+ function get_update( $plugin_id = false, $flush = true, $expiration = WP_FS__TIME_24_HOURS_IN_SEC, $newer_than = false ) {
16677
+ $this->_logger->entrance();
16678
+
16679
+ if ( ! is_numeric( $plugin_id ) ) {
16680
+ $plugin_id = $this->_plugin->id;
16681
+ }
16682
+
16683
+ $this->check_updates( true, $plugin_id, $flush, $expiration, $newer_than );
16684
+ $updates = $this->get_all_updates();
16685
+
16686
+ return isset( $updates[ $plugin_id ] ) && is_object( $updates[ $plugin_id ] ) ? $updates[ $plugin_id ] : false;
16687
+ }
16688
+
16689
+ /**
16690
+ * Check if site assigned with active license.
16691
+ *
16692
+ * @author Vova Feldman (@svovaf)
16693
+ * @since 1.0.6
16694
+ *
16695
+ * @deprecated Please use has_active_valid_license() instead because license can be cancelled.
16696
+ */
16697
+ function has_active_license() {
16698
+ return (
16699
+ is_object( $this->_license ) &&
16700
+ is_numeric( $this->_license->id ) &&
16701
+ ! $this->_license->is_expired()
16702
+ );
16703
+ }
16704
+
16705
+ /**
16706
+ * Check if site assigned with active & valid (not expired) license.
16707
+ *
16708
+ * @author Vova Feldman (@svovaf)
16709
+ * @since 1.2.1
16710
+ */
16711
+ function has_active_valid_license() {
16712
+ return self::is_active_valid_license( $this->_license );
16713
+ }
16714
+
16715
+ /**
16716
+ * Check if a given license is active & valid (not expired).
16717
+ *
16718
+ * @author Vova Feldman (@svovaf)
16719
+ * @since 2.1.3
16720
+ *
16721
+ * @param FS_Plugin_License $license
16722
+ *
16723
+ * @return bool
16724
+ */
16725
+ private static function is_active_valid_license( $license ) {
16726
+ return (
16727
+ is_object( $license ) &&
16728
+ FS_Plugin_License::is_valid_id( $license->id ) &&
16729
+ $license->is_active() &&
16730
+ $license->is_valid()
16731
+ );
16732
+ }
16733
+
16734
+ /**
16735
+ * Checks if there's any site that is associated with an active & valid license.
16736
+ * This logic is used to determine if the admin can download the premium code base from a network level admin.
16737
+ *
16738
+ * @author Vova Feldman (@svovaf)
16739
+ * @since 2.1.3
16740
+ *
16741
+ * @return bool
16742
+ */
16743
+ function has_any_active_valid_license() {
16744
+ if ( ! fs_is_network_admin() ) {
16745
+ return $this->has_active_valid_license();
16746
+ }
16747
+
16748
+ $installs = $this->get_blog_install_map();
16749
+ $all_plugin_licenses = self::get_all_licenses( $this->_module_id );
16750
+
16751
+ foreach ( $installs as $blog_id => $install ) {
16752
+ if ( ! FS_Plugin_License::is_valid_id( $install->license_id ) ) {
16753
+ continue;
16754
+ }
16755
+
16756
+ foreach ( $all_plugin_licenses as $license ) {
16757
+ if ( $license->id == $install->license_id ) {
16758
+ if ( self::is_active_valid_license( $license ) ) {
16759
+ return true;
16760
+ }
16761
+ }
16762
+ }
16763
+ }
16764
+
16765
+ return false;
16766
+ }
16767
+
16768
+ /**
16769
+ * Check if site assigned with license with enabled features.
16770
+ *
16771
+ * @author Vova Feldman (@svovaf)
16772
+ * @since 1.0.6
16773
+ *
16774
+ * @return bool
16775
+ */
16776
+ function has_features_enabled_license() {
16777
+ return (
16778
+ is_object( $this->_license ) &&
16779
+ is_numeric( $this->_license->id ) &&
16780
+ $this->_license->is_features_enabled()
16781
+ );
16782
+ }
16783
+
16784
+ /**
16785
+ * Check if user is a trial or have feature enabled license.
16786
+ *
16787
+ * @author Vova Feldman (@svovaf)
16788
+ * @since 1.1.7
16789
+ *
16790
+ * @return bool
16791
+ */
16792
+ function can_use_premium_code() {
16793
+ return $this->is_trial() || $this->has_features_enabled_license();
16794
+ }
16795
+
16796
+ /**
16797
+ * Checks if the current user can activate plugins or switch themes. Note that this method should only be used
16798
+ * after the `init` action is triggered because it is using `current_user_can()` which is only functional after
16799
+ * the context user is authenticated.
16800
+ *
16801
+ * @author Leo Fajardo (@leorw)
16802
+ * @since 1.2.2
16803
+ *
16804
+ * @return bool
16805
+ */
16806
+ function is_user_admin() {
16807
+ /**
16808
+ * Require a super-admin when network activated, running from the network level OR if
16809
+ * running from the site level but not delegated the opt-in.
16810
+ *
16811
+ * @author Vova Feldman (@svovaf)
16812
+ * @since 2.0.0
16813
+ */
16814
+ if ( $this->_is_network_active &&
16815
+ ( fs_is_network_admin() || ! $this->is_delegated_connection() )
16816
+ ) {
16817
+ return is_super_admin();
16818
+ }
16819
+
16820
+ return ( $this->is_plugin() && current_user_can( is_multisite() ? 'manage_options' : 'activate_plugins' ) )
16821
+ || ( $this->is_theme() && current_user_can( 'switch_themes' ) );
16822
+ }
16823
+
16824
+ /**
16825
+ * Sync site's plan.
16826
+ *
16827
+ * @author Vova Feldman (@svovaf)
16828
+ * @since 1.0.3
16829
+ *
16830
+ * @uses FS_Api
16831
+ *
16832
+ * @param bool $background Hints the method if it's a background sync. If false, it means that was initiated by
16833
+ * the admin.
16834
+ * @param bool $is_context_single_site @since 2.0.0. This is used when syncing a license for a single install from the
16835
+ * network-level "Account" page.
16836
+ */
16837
+ private function _sync_license( $background = false, $is_context_single_site = false ) {
16838
+ $this->_logger->entrance();
16839
+
16840
+ $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
16841
+
16842
+ $is_addon_sync = ( ! $this->_plugin->is_addon() && $plugin_id != $this->get_id() );
16843
+
16844
+ if ( $is_addon_sync ) {
16845
+ $this->_sync_addon_license( $plugin_id, $background );
16846
+ } else {
16847
+ $this->_sync_plugin_license( $background, true, $is_context_single_site );
16848
+ }
16849
+
16850
+ $this->do_action( 'after_account_plan_sync', $this->get_plan_name() );
16851
+ }
16852
+
16853
+ /**
16854
+ * Sync plugin's add-on license.
16855
+ *
16856
+ * @author Vova Feldman (@svovaf)
16857
+ * @since 1.0.6
16858
+ * @uses FS_Api
16859
+ *
16860
+ * @param number $addon_id
16861
+ * @param bool $background
16862
+ */
16863
+ private function _sync_addon_license( $addon_id, $background ) {
16864
+ $this->_logger->entrance();
16865
+
16866
+ if ( $this->is_addon_activated( $addon_id ) ) {
16867
+ // If already installed, use add-on sync.
16868
+ $fs_addon = self::get_instance_by_id( $addon_id );
16869
+ $fs_addon->_sync_license( $background );
16870
+
16871
+ return;
16872
+ }
16873
+
16874
+ // Validate add-on exists.
16875
+ $addon = $this->get_addon( $addon_id );
16876
+
16877
+ if ( ! is_object( $addon ) ) {
16878
+ return;
16879
+ }
16880
+
16881
+ // Add add-on into account add-ons.
16882
+ $account_addons = $this->get_account_addons();
16883
+ if ( ! is_array( $account_addons ) ) {
16884
+ $account_addons = array();
16885
+ }
16886
+ $account_addons[] = $addon->id;
16887
+ $account_addons = array_unique( $account_addons );
16888
+ $this->_store_account_addons( $account_addons );
16889
+
16890
+ // Load add-on licenses.
16891
+ $licenses = $this->_fetch_licenses( $addon->id );
16892
+
16893
+ // Sync add-on licenses.
16894
+ if ( $this->is_array_instanceof( $licenses, 'FS_Plugin_License' ) ) {
16895
+ $this->_update_licenses( $licenses, $addon->id );
16896
+
16897
+ if ( ! $this->is_addon_installed( $addon->id ) && FS_License_Manager::has_premium_license( $licenses ) ) {
16898
+ $plans_result = $this->get_api_site_or_plugin_scope()->get( $this->add_show_pending( "/addons/{$addon_id}/plans.json" ) );
16899
+
16900
+ if ( ! isset( $plans_result->error ) ) {
16901
+ $plans = array();
16902
+ foreach ( $plans_result->plans as $plan ) {
16903
+ $plans[] = new FS_Plugin_Plan( $plan );
16904
+ }
16905
+
16906
+ $this->_admin_notices->add_sticky(
16907
+ sprintf(
16908
+ ( FS_Plan_Manager::instance()->has_free_plan( $plans ) ?
16909
+ $this->get_text_inline( 'Your %s Add-on plan was successfully upgraded.', 'addon-successfully-upgraded-message' ) :
16910
+ /* translators: %s:product name, e.g. Facebook add-on was successfully... */
16911
+ $this->get_text_inline( '%s Add-on was successfully purchased.', 'addon-successfully-purchased-message' ) ),
16912
+ $addon->title
16913
+ ) . ' ' . $this->get_latest_download_link(
16914
+ $this->get_text_inline( 'Download the latest version', 'download-latest-version' ),
16915
+ $addon_id
16916
+ ),
16917
+ 'addon_plan_upgraded_' . $addon->slug,
16918
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
16919
+ );
16920
+ }
16921
+ }
16922
+ }
16923
+ }
16924
+
16925
+ /**
16926
+ * Sync site's plugin plan.
16927
+ *
16928
+ * @author Vova Feldman (@svovaf)
16929
+ * @since 1.0.6
16930
+ * @uses FS_Api
16931
+ *
16932
+ * @param bool $background Hints the method if it's a background sync. If false, it means that was initiated by the admin.
16933
+ * @param bool $send_installs_update Since 2.0.0
16934
+ * @param bool $is_context_single_site Since 2.0.0. This is used when sending an update for a single install and
16935
+ * syncing its license from the network-level "Account" page (e.g.: after
16936
+ * activating a license only for the single install).
16937
+ */
16938
+ private function _sync_plugin_license(
16939
+ $background = false,
16940
+ $send_installs_update = true,
16941
+ $is_context_single_site = false
16942
+ ) {
16943
+ $this->_logger->entrance();
16944
+
16945
+ $plan_change = 'none';
16946
+
16947
+ $is_site_level_sync = ( $is_context_single_site || fs_is_blog_admin() || ! $this->_is_network_active );
16948
+
16949
+ if ( ! $send_installs_update ) {
16950
+ $site = $this->_site;
16951
+ } else {
16952
+ /**
16953
+ * Sync site info.
16954
+ *
16955
+ * @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.
16956
+ */
16957
+ if ( $is_site_level_sync ) {
16958
+ $result = $this->send_install_update( array(), true );
16959
+ $is_valid = $this->is_api_result_entity( $result );
16960
+ } else {
16961
+ $result = $this->send_installs_update( array(), true );
16962
+ $is_valid = $this->is_api_result_object( $result, 'installs' );
16963
+ }
16964
+
16965
+ if ( ! $is_valid ) {
16966
+ if ( $is_context_single_site ) {
16967
+ // Switch back to the main blog so that the following logic will have the right entities.
16968
+ $this->switch_to_blog( $this->_storage->network_install_blog_id );
16969
+ }
16970
+
16971
+ // Show API messages only if not background sync or if paying customer.
16972
+ if ( ! $background || $this->is_paying() ) {
16973
+ // Try to ping API to see if not blocked.
16974
+ if ( ! FS_Api::test() ) {
16975
+ /**
16976
+ * Failed to ping API - blocked!
16977
+ *
16978
+ * @author Vova Feldman (@svovaf)
16979
+ * @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.
16980
+ */
16981
+ $api = $this->get_api_site_scope();
16982
+
16983
+ if ( ! self::$_global_admin_notices->has_sticky( 'api_blocked' ) ) {
16984
+ self::$_global_admin_notices->add(
16985
+ sprintf(
16986
+ $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' ),
16987
+ $this->get_plugin_name(),
16988
+ '<a href="' . $api->get_url() . '" target="_blank">' . $api->get_url() . '</a>'
16989
+ ) . '<br> ' . $this->get_text_inline( 'Error received from the server:', 'server-error-message' ) . var_export( $result->error, true ),
16990
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
16991
+ 'error',
16992
+ $background,
16993
+ 'api_blocked'
16994
+ );
16995
+ }
16996
+ } else {
16997
+ // Authentication params are broken.
16998
+ $this->_admin_notices->add(
16999
+ $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' ),
17000
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
17001
+ 'error'
17002
+ );
17003
+ }
17004
+ }
17005
+
17006
+ // No reason to continue with license sync while there are API issues.
17007
+ return;
17008
+ }
17009
+
17010
+ if ( $is_site_level_sync ) {
17011
+ $site = new FS_Site( $result );
17012
+ } else {
17013
+ // Map site addresses to their blog IDs.
17014
+ $address_to_blog_map = $this->get_address_to_blog_map();
17015
+
17016
+ // Find the current context install.
17017
+ $site = null;
17018
+ foreach ( $result->installs as $install ) {
17019
+ if ( $install->id == $this->_site->id ) {
17020
+ $site = new FS_Site( $install );
17021
+ } else {
17022
+ $address = trailingslashit( fs_strip_url_protocol( $install->url ) );
17023
+ $blog_id = $address_to_blog_map[ $address ];
17024
+
17025
+ $this->_store_site( true, $blog_id, new FS_Site( $install ) );
17026
+ }
17027
+ }
17028
+ }
17029
+
17030
+ // Sync plans.
17031
+ $this->_sync_plans();
17032
+ }
17033
+
17034
+ // Remove sticky API connectivity message.
17035
+ self::$_global_admin_notices->remove_sticky( 'api_blocked' );
17036
+
17037
+ if ( ! $this->has_paid_plan() ) {
17038
+ $this->_site = $site;
17039
+ $this->_store_site(
17040
+ true,
17041
+ $is_site_level_sync ?
17042
+ null :
17043
+ $this->get_network_install_blog_id()
17044
+ );
17045
+ } else {
17046
+ $context_blog_id = 0;
17047
+
17048
+ if ( $is_context_single_site ) {
17049
+ $context_blog_id = get_current_blog_id();
17050
+
17051
+ // Switch back to the main blog in order to properly sync the license.
17052
+ $this->switch_to_blog( $this->_storage->network_install_blog_id );
17053
+ }
17054
+
17055
+ /**
17056
+ * Sync licenses. Pass the site's license ID so that the foreign licenses will be fetched if the license
17057
+ * associated with that ID is not included in the user's licenses collection.
17058
+ */
17059
+ $this->_sync_licenses(
17060
+ $site->license_id,
17061
+ ( $is_context_single_site ?
17062
+ $context_blog_id :
17063
+ null
17064
+ )
17065
+ );
17066
+
17067
+ if ( $is_context_single_site ) {
17068
+ $this->switch_to_blog( $context_blog_id );
17069
+ }
17070
+
17071
+ // Check if plan / license changed.
17072
+ if ( $site->plan_id != $this->_site->plan_id ||
17073
+ // Check if trial started.
17074
+ $site->trial_plan_id != $this->_site->trial_plan_id ||
17075
+ $site->trial_ends != $this->_site->trial_ends ||
17076
+ // Check if license changed.
17077
+ $site->license_id != $this->_site->license_id
17078
+ ) {
17079
+ if ( $site->is_trial() && ( ! $this->_site->is_trial() || $site->trial_ends != $this->_site->trial_ends ) ) {
17080
+ // New trial started.
17081
+ $this->_site = $site;
17082
+ $plan_change = 'trial_started';
17083
+
17084
+ // For trial with subscription use-case.
17085
+ $new_license = is_null( $site->license_id ) ? null : $this->_get_license_by_id( $site->license_id );
17086
+
17087
+ if ( is_object( $new_license ) && $new_license->is_valid() ) {
17088
+ $this->_site = $site;
17089
+ $this->_update_site_license( $new_license );
17090
+ $this->_store_licenses();
17091
+
17092
+ $this->_sync_site_subscription( $this->_license );
17093
+ }
17094
+ } else if ( $this->_site->is_trial() && ! $site->is_trial() && ! is_numeric( $site->license_id ) ) {
17095
+ // Was in trial, but now trial expired and no license ID.
17096
+ // New trial started.
17097
+ $this->_site = $site;
17098
+ $plan_change = 'trial_expired';
17099
+ } else {
17100
+ $is_free = $this->is_free_plan();
17101
+
17102
+ // Make sure license exist and not expired.
17103
+ $new_license = is_null( $site->license_id ) ?
17104
+ null :
17105
+ $this->_get_license_by_id( $site->license_id );
17106
+
17107
+ if ( $is_free && is_null( $new_license ) && $this->has_any_license() && $this->_license->is_cancelled ) {
17108
+ // License cancelled.
17109
+ $this->_site = $site;
17110
+ $this->_update_site_license( $new_license );
17111
+ $this->_store_licenses();
17112
+
17113
+ $plan_change = 'cancelled';
17114
+ } else if ( $is_free && ( ( ! is_object( $new_license ) || $new_license->is_expired() ) ) ) {
17115
+ // The license is expired, so ignore upgrade method.
17116
+ $this->_site = $site;
17117
+ } else {
17118
+ // License changed.
17119
+ $this->_site = $site;
17120
+
17121
+ /**
17122
+ * IMPORTANT:
17123
+ * 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.
17124
+ *
17125
+ * @author Vova Feldman (@svovaf)
17126
+ * @since 2.0.0
17127
+ */
17128
+ $this->_update_site_license( $new_license );
17129
+
17130
+ if ( ! $is_context_single_site &&
17131
+ fs_is_network_admin() &&
17132
+ $this->_is_network_active &&
17133
+ $new_license->quota > 1 &&
17134
+ get_blog_count() > 1
17135
+ ) {
17136
+ // See if license can activated on all sites.
17137
+ if ( ! $this->try_activate_license_on_network( $this->_user, $new_license ) ) {
17138
+ if ( ! fs_request_get_bool( 'auto_install' ) ) {
17139
+ // Open the license activation dialog box on the account page.
17140
+ add_action( 'admin_footer', array(
17141
+ &$this,
17142
+ '_open_license_activation_dialog_box'
17143
+ ) );
17144
+ }
17145
+ }
17146
+ }
17147
+
17148
+ $this->_store_licenses();
17149
+
17150
+ $plan_change = $is_free ?
17151
+ 'upgraded' :
17152
+ ( is_object( $new_license ) ?
17153
+ 'changed' :
17154
+ 'downgraded' );
17155
+ }
17156
+ }
17157
+
17158
+ // Store updated site info.
17159
+ $this->_store_site(
17160
+ true,
17161
+ $is_site_level_sync ?
17162
+ null :
17163
+ $this->get_network_install_blog_id()
17164
+ );
17165
+ } else {
17166
+ if ( is_object( $this->_license ) && $this->_license->is_expired() ) {
17167
+ if ( ! $this->has_features_enabled_license() ) {
17168
+ $this->_deactivate_license();
17169
+ $plan_change = 'downgraded';
17170
+ } else {
17171
+ $plan_change = 'expired';
17172
+ }
17173
+ }
17174
+
17175
+ if ( is_numeric( $site->license_id ) && is_object( $this->_license ) ) {
17176
+ $this->_sync_site_subscription( $this->_license );
17177
+ }
17178
+ }
17179
+ }
17180
+
17181
+ $hmm_text = $this->get_text_x_inline( 'Hmm', 'something somebody says when they are thinking about what you have just said.', 'hmm' ) . '...';
17182
+
17183
+ if ( $this->has_paid_plan() ) {
17184
+ switch ( $plan_change ) {
17185
+ case 'none':
17186
+ if ( ! $background && is_admin() ) {
17187
+ $plan = $this->is_trial() ?
17188
+ $this->get_trial_plan() :
17189
+ $this->get_plan();
17190
+
17191
+ if ( $plan->is_free() ) {
17192
+ $this->_admin_notices->add(
17193
+ sprintf(
17194
+ $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' ),
17195
+ '<i><b>' . $plan->title . ( $this->is_trial() ? ' ' . $this->get_text_x_inline( 'Trial', 'trial period', 'trial' ) : '' ) . '</b></i>'
17196
+ ) . ' ' . sprintf(
17197
+ '<a href="%s">%s</a>',
17198
+ $this->contact_url(
17199
+ 'bug',
17200
+ 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' ),
17201
+ strtoupper( $plan->name )
17202
+ )
17203
+ ),
17204
+ $this->get_text_inline( 'Please contact us here', 'contact-us-here' )
17205
+ ),
17206
+ $hmm_text
17207
+ );
17208
+ }
17209
+ }
17210
+ break;
17211
+ case 'upgraded':
17212
+ $this->_admin_notices->add_sticky(
17213
+ sprintf(
17214
+ $this->get_text_inline( 'Your plan was successfully upgraded.', 'plan-upgraded-message' ),
17215
+ '<i>' . $this->get_plugin_name() . '</i>'
17216
+ ) . $this->get_complete_upgrade_instructions(),
17217
+ 'plan_upgraded',
17218
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
17219
+ );
17220
+
17221
+ $this->_admin_notices->remove_sticky( array(
17222
+ 'trial_started',
17223
+ 'trial_promotion',
17224
+ 'trial_expired',
17225
+ 'activation_complete',
17226
+ 'license_expired',
17227
+ ) );
17228
+ break;
17229
+ case 'changed':
17230
+ $this->_admin_notices->add_sticky(
17231
+ sprintf(
17232
+ $this->get_text_inline( 'Your plan was successfully changed to %s.', 'plan-changed-to-x-message' ),
17233
+ $this->get_plan_title()
17234
+ ),
17235
+ 'plan_changed'
17236
+ );
17237
+
17238
+ $this->_admin_notices->remove_sticky( array(
17239
+ 'trial_started',
17240
+ 'trial_promotion',
17241
+ 'trial_expired',
17242
+ 'activation_complete',
17243
+ ) );
17244
+ break;
17245
+ case 'downgraded':
17246
+ $this->_admin_notices->add_sticky(
17247
+ ($this->has_free_plan() ?
17248
+ 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 ) :
17249
+ /* translators: %1$s: product title; %2$s, %3$s: wrapping HTML anchor element; %4$s: 'plugin', 'theme', or 'add-on'. */
17250
+ 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) ) ),
17251
+ 'license_expired',
17252
+ $hmm_text
17253
+ );
17254
+ $this->_admin_notices->remove_sticky( 'plan_upgraded' );
17255
+ break;
17256
+ case 'cancelled':
17257
+ $this->_admin_notices->add(
17258
+ $this->get_text_inline( 'Your license has been cancelled. If you think it\'s a mistake, please contact support.', 'license-cancelled' ) . ' ' .
17259
+ sprintf(
17260
+ '<a href="%s">%s</a>',
17261
+ $this->contact_url( 'bug' ),
17262
+ $this->get_text_inline( 'Please contact us here', 'contact-us-here' )
17263
+ ),
17264
+ $hmm_text,
17265
+ 'error'
17266
+ );
17267
+ $this->_admin_notices->remove_sticky( 'plan_upgraded' );
17268
+ break;
17269
+ case 'expired':
17270
+ $this->_admin_notices->add_sticky(
17271
+ 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 ),
17272
+ 'license_expired',
17273
+ $hmm_text
17274
+ );
17275
+ $this->_admin_notices->remove_sticky( 'plan_upgraded' );
17276
+ break;
17277
+ case 'trial_started':
17278
+ $this->_admin_notices->add_sticky(
17279
+ sprintf(
17280
+ $this->get_text_inline( 'Your trial has been successfully started.', 'trial-started-message' ),
17281
+ '<i>' . $this->get_plugin_name() . '</i>'
17282
+ ) . $this->get_complete_upgrade_instructions( $this->get_trial_plan()->title ),
17283
+ 'trial_started',
17284
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
17285
+ );
17286
+
17287
+ $this->_admin_notices->remove_sticky( array(
17288
+ 'trial_promotion',
17289
+ ) );
17290
+ break;
17291
+ case 'trial_expired':
17292
+ $this->_admin_notices->add_sticky(
17293
+ ($this->has_free_plan() ?
17294
+ $this->get_text_inline( 'Your free trial has expired. You can still continue using all our free features.', 'trial-expired-message' ) :
17295
+ /* translators: %1$s: product title; %2$s, %3$s: wrapping HTML anchor element; %4$s: 'plugin', 'theme', or 'add-on'. */
17296
+ 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))),
17297
+ 'trial_expired',
17298
+ $hmm_text
17299
+ );
17300
+ $this->_admin_notices->remove_sticky( array(
17301
+ 'trial_started',
17302
+ 'trial_promotion',
17303
+ 'plan_upgraded',
17304
+ ) );
17305
+ break;
17306
+ }
17307
+ }
17308
+
17309
+ if ( 'none' !== $plan_change ) {
17310
+ $this->do_action( 'after_license_change', $plan_change, $this->get_plan() );
17311
+ }
17312
+ }
17313
+
17314
+ /**
17315
+ * Include the required JS at the footer of the admin to trigger the license activation dialog box.
17316
+ *
17317
+ * @author Vova Feldman (@svovaf)
17318
+ * @since 2.0.0
17319
+ */
17320
+ public function _open_license_activation_dialog_box() {
17321
+ $vars = array( 'license_id' => $this->_site->license_id );
17322
+ fs_require_once_template( 'js/open-license-activation.php', $vars );
17323
+ }
17324
+
17325
+ /**
17326
+ * @author Vova Feldman (@svovaf)
17327
+ * @since 1.0.5
17328
+ *
17329
+ * @param bool $background
17330
+ */
17331
+ protected function _activate_license( $background = false ) {
17332
+ $this->_logger->entrance();
17333
+
17334
+ $license_id = fs_request_get( 'license_id' );
17335
+
17336
+ if ( is_object( $this->_site ) &&
17337
+ FS_Plugin_License::is_valid_id( $license_id ) &&
17338
+ $license_id == $this->_site->license_id
17339
+ ) {
17340
+ // License is already activated.
17341
+ return;
17342
+ }
17343
+
17344
+ $premium_license = FS_Plugin_License::is_valid_id( $license_id ) ?
17345
+ $this->_get_license_by_id( $license_id ) :
17346
+ $this->_get_available_premium_license();
17347
+
17348
+ if ( ! is_object( $premium_license ) ) {
17349
+ return;
17350
+ }
17351
+
17352
+ if ( ! is_object( $this->_site ) ) {
17353
+ // Not yet opted-in.
17354
+ $user = $this->get_current_or_network_user();
17355
+ if ( ! is_object( $user ) ) {
17356
+ $user = self::_get_user_by_id( $premium_license->user_id );
17357
+ }
17358
+
17359
+ if ( is_object( $user ) ) {
17360
+ $this->install_with_user( $user, $premium_license->secret_key, false, false, false );
17361
+ } else {
17362
+ $this->opt_in(
17363
+ false,
17364
+ false,
17365
+ false,
17366
+ $premium_license->secret_key
17367
+ );
17368
+
17369
+ return;
17370
+ }
17371
+ }
17372
+
17373
+
17374
+ /**
17375
+ * If the premium license is already associated with the install, just
17376
+ * update the license reference (activation is not required).
17377
+ *
17378
+ * @since 1.1.9
17379
+ */
17380
+ if ( $premium_license->id == $this->_site->license_id ) {
17381
+ // License is already activated.
17382
+ $this->_update_site_license( $premium_license );
17383
+ $this->_store_account();
17384
+
17385
+ return;
17386
+ }
17387
+
17388
+ if ( $this->_site->user_id != $premium_license->user_id ) {
17389
+ $api_request_params = array( 'license_key' => $premium_license->secret_key );
17390
+ } else {
17391
+ $api_request_params = array();
17392
+ }
17393
+
17394
+ $api = $this->get_api_site_scope();
17395
+ $license = $api->call( "/licenses/{$premium_license->id}.json", 'put', $api_request_params );
17396
+
17397
+ if ( ! $this->is_api_result_entity( $license ) ) {
17398
+ if ( ! $background ) {
17399
+ $this->_admin_notices->add( sprintf(
17400
+ '%s %s',
17401
+ $this->get_text_inline( 'It looks like the license could not be activated.', 'license-activation-failed-message' ),
17402
+ ( is_object( $license ) && isset( $license->error ) ?
17403
+ $license->error->message :
17404
+ sprintf( '%s<br><code>%s</code>',
17405
+ $this->get_text_inline( 'Error received from the server:', 'server-error-message' ),
17406
+ var_export( $license, true )
17407
+ )
17408
+ )
17409
+ ),
17410
+ $this->get_text_x_inline( 'Hmm', 'something somebody says when they are thinking about what you have just said.', 'hmm' ) . '...',
17411
+ 'error'
17412
+ );
17413
+ }
17414
+
17415
+ return;
17416
+ }
17417
+
17418
+ $premium_license = new FS_Plugin_License( $license );
17419
+
17420
+ // Updated site plan.
17421
+ $site = $this->get_api_site_scope()->get( '/', true );
17422
+ if ( $this->is_api_result_entity( $site ) ) {
17423
+ $this->_site = new FS_Site( $site );
17424
+ }
17425
+ $this->_update_site_license( $premium_license );
17426
+
17427
+ $this->_store_account();
17428
+
17429
+ if ( ! $background ) {
17430
+ $this->_admin_notices->add_sticky(
17431
+ $this->get_text_inline( 'Your license was successfully activated.', 'license-activated-message' ) .
17432
+ $this->get_complete_upgrade_instructions(),
17433
+ 'license_activated',
17434
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
17435
+ );
17436
+ }
17437
+
17438
+ $this->_admin_notices->remove_sticky( array(
17439
+ 'trial_promotion',
17440
+ 'license_expired',
17441
+ ) );
17442
+ }
17443
+
17444
+ /**
17445
+ * @author Vova Feldman (@svovaf)
17446
+ * @since 1.0.5
17447
+ *
17448
+ * @param bool $show_notice
17449
+ */
17450
+ protected function _deactivate_license( $show_notice = true ) {
17451
+ $this->_logger->entrance();
17452
+
17453
+ $hmm_text = $this->get_text_x_inline( 'Hmm', 'something somebody says when they are thinking about what you have just said.', 'hmm' ) . '...';
17454
+
17455
+ if ( ! FS_Plugin_License::is_valid_id( $this->_site->license_id ) ) {
17456
+ $this->_admin_notices->add(
17457
+ 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() ),
17458
+ $hmm_text
17459
+ );
17460
+
17461
+ return;
17462
+ }
17463
+
17464
+ $api = $this->get_api_site_scope();
17465
+ $license = $api->call( "/licenses/{$this->_site->license_id}.json", 'delete' );
17466
+
17467
+ $this->handle_license_deactivation_result( $license, $hmm_text, $show_notice );
17468
+ }
17469
+
17470
+ /**
17471
+ * @author Leo Fajardo (@leorw)
17472
+ * @since 2.2.1
17473
+ *
17474
+ * @param FS_Plugin_License $license
17475
+ * @param bool|string $hmm_text
17476
+ * @param bool $show_notice
17477
+ */
17478
+ private function handle_license_deactivation_result( $license, $hmm_text = false, $show_notice = true ) {
17479
+ if ( isset( $license->error ) ) {
17480
+ $this->_admin_notices->add(
17481
+ $this->get_text_inline( 'It looks like the license deactivation failed.', 'license-deactivation-failed-message' ) . '<br> ' .
17482
+ $this->get_text_inline( 'Error received from the server:', 'server-error-message' ) . ' ' . var_export( $license->error, true ),
17483
+ $hmm_text,
17484
+ 'error'
17485
+ );
17486
+
17487
+ return;
17488
+ }
17489
+
17490
+ // Update license cache.
17491
+ if ( is_array( $this->_licenses ) ) {
17492
+ for ( $i = 0, $len = count( $this->_licenses ); $i < $len; $i ++ ) {
17493
+ if ( $license->id == $this->_licenses[ $i ]->id ) {
17494
+ $this->_licenses[ $i ] = new FS_Plugin_License( $license );
17495
+ }
17496
+ }
17497
+ }
17498
+
17499
+ // Update site plan to default.
17500
+ $this->_sync_plans();
17501
+ $this->_site->plan_id = $this->_plans[0]->id;
17502
+ // Unlink license from site.
17503
+ $this->_update_site_license( null );
17504
+
17505
+ $this->_store_account();
17506
+
17507
+ if ( $show_notice ) {
17508
+ $this->_admin_notices->add(
17509
+ sprintf( $this->get_text_inline( 'Your license was successfully deactivated, you are back to the %s plan.', 'license-deactivation-message' ), $this->get_plan_title() ),
17510
+ $this->get_text_inline( 'O.K', 'ok' )
17511
+ );
17512
+ }
17513
+
17514
+ $this->_admin_notices->remove_sticky( array(
17515
+ 'plan_upgraded',
17516
+ 'license_activated',
17517
+ ) );
17518
+ }
17519
+
17520
+ /**
17521
+ * Site plan downgrade.
17522
+ *
17523
+ * @author Vova Feldman (@svovaf)
17524
+ * @since 1.0.4
17525
+ *
17526
+ * @return object
17527
+ *
17528
+ * @uses FS_Api
17529
+ */
17530
+ private function _downgrade_site() {
17531
+ $this->_logger->entrance();
17532
+
17533
+ $deactivate_license = fs_request_get_bool( 'deactivate_license' );
17534
+
17535
+ $api = $this->get_api_site_scope();
17536
+ $site = $api->call( 'downgrade.json', 'put', array( 'deactivate_license' => $deactivate_license ) );
17537
+
17538
+ $plan_downgraded = false;
17539
+ $plan = false;
17540
+ if ( $this->is_api_result_entity( $site ) ) {
17541
+ $prev_plan_id = $this->_site->plan_id;
17542
+
17543
+ // Update new site plan id.
17544
+ $this->_site->plan_id = $site->plan_id;
17545
+
17546
+ $plan = $this->get_plan();
17547
+ $subscription = $this->_sync_site_subscription( $this->_license );
17548
+
17549
+ // Plan downgraded if plan was changed or subscription was cancelled.
17550
+ $plan_downgraded = ( $plan instanceof FS_Plugin_Plan && $prev_plan_id != $plan->id ) ||
17551
+ ( is_object( $subscription ) && ! isset( $subscription->error ) && ! $subscription->is_active() );
17552
+ } else {
17553
+ // handle different error cases.
17554
+ $this->handle_license_deactivation_result(
17555
+ $site,
17556
+ $this->get_text_x_inline( 'Hmm', 'something somebody says when they are thinking about what you have just said.', 'hmm' ) . '...'
17557
+ );
17558
+ }
17559
+
17560
+ if ( ! $plan_downgraded ) {
17561
+ return (object) array(
17562
+ 'error' => (object) array(
17563
+ 'message' => $this->get_text_inline( 'Seems like we are having some temporary issue with your subscription cancellation. Please try again in few minutes.', 'subscription-cancellation-failure-message' )
17564
+ )
17565
+ );
17566
+ }
17567
+
17568
+ // Remove previous sticky message about upgrade (if exist).
17569
+ $this->_admin_notices->remove_sticky( 'plan_upgraded' );
17570
+
17571
+ $this->_admin_notices->add(
17572
+ sprintf( $this->get_text_inline( 'Your subscription was successfully cancelled. Your %s plan license will expire in %s.', 'plan-x-downgraded-message' ),
17573
+ $plan->title,
17574
+ human_time_diff( time(), strtotime( $this->_license->expiration ) )
17575
+ )
17576
+ );
17577
+
17578
+ // Store site updates.
17579
+ $this->_store_site();
17580
+
17581
+ if ( $deactivate_license &&
17582
+ ! FS_Plugin_License::is_valid_id( $site->license_id )
17583
+ ) {
17584
+ if ( $this->_site->is_localhost() ) {
17585
+ $this->_license->activated_local = max( 0, $this->_license->activated_local - 1 );
17586
+ } else {
17587
+ $this->_license->activated = max( 0, $this->_license->activated - 1 );
17588
+ }
17589
+
17590
+ // Handle successful license deactivation result.
17591
+ $this->handle_license_deactivation_result( $this->_license );
17592
+ }
17593
+
17594
+ return $site;
17595
+ }
17596
+
17597
+ /**
17598
+ * @author Vova Feldman (@svovaf)
17599
+ * @since 1.1.8.1
17600
+ *
17601
+ * @param bool|string $plan_name
17602
+ *
17603
+ * @return bool If trial was successfully started.
17604
+ */
17605
+ function start_trial( $plan_name = false ) {
17606
+ $this->_logger->entrance();
17607
+
17608
+ // Alias.
17609
+ $oops_text = $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...';
17610
+
17611
+ if ( $this->is_trial() ) {
17612
+ // Already in trial mode.
17613
+ $this->_admin_notices->add(
17614
+ sprintf( $this->get_text_inline( 'You are already running the %s in a trial mode.', 'in-trial-mode' ), $this->_module_type ),
17615
+ $oops_text,
17616
+ 'error'
17617
+ );
17618
+
17619
+ return false;
17620
+ }
17621
+
17622
+ if ( $this->_site->is_trial_utilized() ) {
17623
+ // Trial was already utilized.
17624
+ $this->_admin_notices->add(
17625
+ $this->get_text_inline( 'You already utilized a trial before.', 'trial-utilized' ),
17626
+ $oops_text,
17627
+ 'error'
17628
+ );
17629
+
17630
+ return false;
17631
+ }
17632
+
17633
+ if ( false !== $plan_name ) {
17634
+ $plan = $this->get_plan_by_name( $plan_name );
17635
+
17636
+ if ( false === $plan ) {
17637
+ // Plan doesn't exist.
17638
+ $this->_admin_notices->add(
17639
+ sprintf( $this->get_text_inline( 'Plan %s do not exist, therefore, can\'t start a trial.', 'trial-plan-x-not-exist' ), $plan_name ),
17640
+ $oops_text,
17641
+ 'error'
17642
+ );
17643
+
17644
+ return false;
17645
+ }
17646
+
17647
+ if ( ! $plan->has_trial() ) {
17648
+ // Plan doesn't exist.
17649
+ $this->_admin_notices->add(
17650
+ sprintf( $this->get_text_inline( 'Plan %s does not support a trial period.', 'plan-x-no-trial' ), $plan_name ),
17651
+ $oops_text,
17652
+ 'error'
17653
+ );
17654
+
17655
+ return false;
17656
+ }
17657
+ } else {
17658
+ if ( ! $this->has_trial_plan() ) {
17659
+ // None of the plans have a trial.
17660
+ $this->_admin_notices->add(
17661
+ sprintf( $this->get_text_inline( 'None of the %s\'s plans supports a trial period.', 'no-trials' ), $this->_module_type ),
17662
+ $oops_text,
17663
+ 'error'
17664
+ );
17665
+
17666
+ return false;
17667
+ }
17668
+
17669
+ $plans_with_trial = FS_Plan_Manager::instance()->get_trial_plans( $this->_plans );
17670
+
17671
+ $plan = $plans_with_trial[0];
17672
+ }
17673
+
17674
+ $api = $this->get_api_site_scope();
17675
+ $plan = $api->call( "plans/{$plan->id}/trials.json", 'post' );
17676
+
17677
+ if ( ! $this->is_api_result_entity( $plan ) ) {
17678
+ // Some API error while trying to start the trial.
17679
+ $this->_admin_notices->add(
17680
+ sprintf( $this->get_text_inline( 'Unexpected API error. Please contact the %s\'s author with the following error.', 'unexpected-api-error' ), $this->_module_type )
17681
+ . ' ' . var_export( $plan, true ),
17682
+ $oops_text,
17683
+ 'error'
17684
+ );
17685
+
17686
+ return false;
17687
+ }
17688
+
17689
+ // Sync license.
17690
+ $this->_sync_license();
17691
+
17692
+ return $this->is_trial();
17693
+ }
17694
+
17695
+ /**
17696
+ * Cancel site trial.
17697
+ *
17698
+ * @author Vova Feldman (@svovaf)
17699
+ * @since 1.0.9
17700
+ *
17701
+ * @return object
17702
+ *
17703
+ * @uses FS_Api
17704
+ */
17705
+ private function _cancel_trial() {
17706
+ $this->_logger->entrance();
17707
+
17708
+ if ( ! $this->is_trial() ) {
17709
+ return (object) array(
17710
+ 'error' => (object) array(
17711
+ 'message' => $this->get_text_inline( 'It looks like you are not in trial mode anymore so there\'s nothing to cancel :)', 'trial-cancel-no-trial-message' )
17712
+ )
17713
+ );
17714
+ }
17715
+
17716
+ $trial_plan = $this->get_trial_plan();
17717
+
17718
+ $api = $this->get_api_site_scope();
17719
+ $site = $api->call( 'trials.json', 'delete' );
17720
+
17721
+ $trial_cancelled = false;
17722
+
17723
+ if ( $this->is_api_result_entity( $site ) ) {
17724
+ $prev_trial_ends = $this->_site->trial_ends;
17725
+
17726
+ if ( $this->is_paid_trial() ) {
17727
+ $this->_license->expiration = $site->trial_ends;
17728
+ $this->_license->is_cancelled = true;
17729
+ $this->_update_site_license( $this->_license );
17730
+ $this->_store_licenses();
17731
+
17732
+ // Clear subscription reference.
17733
+ $this->_sync_site_subscription( null );
17734
+ }
17735
+
17736
+ // Update site info.
17737
+ $this->_site = new FS_Site( $site );
17738
+
17739
+ $trial_cancelled = ( $prev_trial_ends != $site->trial_ends );
17740
+ } else {
17741
+ // @todo handle different error cases.
17742
+ }
17743
+
17744
+ if ( ! $trial_cancelled ) {
17745
+ return (object) array(
17746
+ 'error' => (object) array(
17747
+ 'message' => $this->get_text_inline( 'Seems like we are having some temporary issue with your trial cancellation. Please try again in few minutes.', 'trial-cancel-failure-message' )
17748
+ )
17749
+ );
17750
+ }
17751
+
17752
+ // Remove previous sticky messages about upgrade or trial (if exist).
17753
+ $this->_admin_notices->remove_sticky( array(
17754
+ 'trial_started',
17755
+ 'trial_promotion',
17756
+ 'plan_upgraded',
17757
+ ) );
17758
+
17759
+ // Store site updates.
17760
+ $this->_store_site();
17761
+
17762
+ if ( ! $this->is_addon() ||
17763
+ ! $this->deactivate_premium_only_addon_without_license( true )
17764
+ ) {
17765
+ $this->_admin_notices->add(
17766
+ sprintf( $this->get_text_inline( 'Your %s free trial was successfully cancelled.', 'trial-cancel-message' ), $trial_plan->title )
17767
+ );
17768
+ }
17769
+
17770
+ return $site;
17771
+ }
17772
+
17773
+ /**
17774
+ * @author Vova Feldman (@svovaf)
17775
+ * @since 1.0.6
17776
+ *
17777
+ * @param bool|number $plugin_id
17778
+ *
17779
+ * @return bool
17780
+ */
17781
+ private function _is_addon_id( $plugin_id ) {
17782
+ return is_numeric( $plugin_id ) && ( $this->get_id() != $plugin_id );
17783
+ }
17784
+
17785
+ /**
17786
+ * Check if user eligible to download premium version updates.
17787
+ *
17788
+ * @author Vova Feldman (@svovaf)
17789
+ * @since 1.0.6
17790
+ *
17791
+ * @return bool
17792
+ */
17793
+ private function _can_download_premium() {
17794
+ return $this->has_any_active_valid_license() ||
17795
+ ( $this->is_trial() && ! $this->get_trial_plan()->is_free() );
17796
+ }
17797
+
17798
+ /**
17799
+ *
17800
+ * @author Vova Feldman (@svovaf)
17801
+ * @since 1.0.6
17802
+ *
17803
+ * @param bool|number $addon_id
17804
+ * @param string $type "json" or "zip"
17805
+ *
17806
+ * @return string
17807
+ */
17808
+ private function _get_latest_version_endpoint( $addon_id = false, $type = 'json' ) {
17809
+
17810
+ $is_addon = $this->_is_addon_id( $addon_id );
17811
+
17812
+ $is_premium = null;
17813
+ if ( ! $is_addon ) {
17814
+ $is_premium = ( $this->is_premium() || $this->_can_download_premium() );
17815
+ } else if ( $this->is_addon_activated( $addon_id ) ) {
17816
+ $fs_addon = self::get_instance_by_id( $addon_id );
17817
+ $is_premium = ( $fs_addon->is_premium() || $fs_addon->_can_download_premium() );
17818
+ }
17819
+
17820
+ // If add-on, then append add-on ID.
17821
+ $endpoint = ( $is_addon ? "/addons/$addon_id" : '' ) .
17822
+ '/updates/latest.' . $type;
17823
+
17824
+ // If add-on and not yet activated, try to fetch based on server licensing.
17825
+ if ( is_bool( $is_premium ) ) {
17826
+ $endpoint = add_query_arg( 'is_premium', json_encode( $is_premium ), $endpoint );
17827
+ }
17828
+
17829
+ if ( $this->has_secret_key() ) {
17830
+ $endpoint = add_query_arg( 'type', 'all', $endpoint );
17831
+ }
17832
+
17833
+ return $endpoint;
17834
+ }
17835
+
17836
+ /**
17837
+ * @author Vova Feldman (@svovaf)
17838
+ * @since 1.0.4
17839
+ *
17840
+ * @param bool|number $addon_id
17841
+ * @param bool $flush Since 1.1.7.3
17842
+ * @param int $expiration Since 1.2.2.7
17843
+ * @param bool|string $newer_than Since 2.2.1
17844
+ * @param bool|string $fetch_readme Since 2.2.1
17845
+ *
17846
+ * @return object|false Plugin latest tag info.
17847
+ */
17848
+ function _fetch_latest_version(
17849
+ $addon_id = false,
17850
+ $flush = true,
17851
+ $expiration = WP_FS__TIME_24_HOURS_IN_SEC,
17852
+ $newer_than = false,
17853
+ $fetch_readme = true
17854
+ ) {
17855
+ $this->_logger->entrance();
17856
+
17857
+ $switch_to_blog_id = null;
17858
+
17859
+ /**
17860
+ * @since 1.1.7.3 Check for plugin updates from Freemius only if opted-in.
17861
+ * @since 1.1.7.4 Also check updates for add-ons.
17862
+ */
17863
+ if ( ! $this->is_registered() &&
17864
+ ! $this->_is_addon_id( $addon_id )
17865
+ ) {
17866
+ if ( ! is_multisite() ) {
17867
+ return false;
17868
+ }
17869
+
17870
+ $installs_map = $this->get_blog_install_map();
17871
+
17872
+ foreach ( $installs_map as $blog_id => $install ) {
17873
+ /**
17874
+ * @var FS_Site $install
17875
+ */
17876
+ if ( $install->is_trial() ) {
17877
+ $switch_to_blog_id = $blog_id;
17878
+ break;
17879
+ }
17880
+
17881
+ if ( FS_Plugin_License::is_valid_id( $install->license_id ) ) {
17882
+ $license = $this->get_license_by_id( $install->license_id );
17883
+
17884
+ if ( is_object( $license ) && $license->is_features_enabled() ) {
17885
+ $switch_to_blog_id = $blog_id;
17886
+ break;
17887
+ }
17888
+ }
17889
+ }
17890
+
17891
+ if ( is_null( $switch_to_blog_id ) ) {
17892
+ return false;
17893
+ }
17894
+ }
17895
+
17896
+ $current_blog_id = is_numeric( $switch_to_blog_id ) ?
17897
+ get_current_blog_id() :
17898
+ 0;
17899
+
17900
+ if ( is_numeric( $switch_to_blog_id ) ) {
17901
+ $this->switch_to_blog( $switch_to_blog_id );
17902
+ }
17903
+
17904
+ $latest_version_endpoint = $this->_get_latest_version_endpoint( $addon_id, 'json' );
17905
+
17906
+ if ( ! empty( $newer_than ) ) {
17907
+ $latest_version_endpoint = add_query_arg( 'newer_than', $newer_than, $latest_version_endpoint );
17908
+ }
17909
+
17910
+ if ( true === $fetch_readme ) {
17911
+ $latest_version_endpoint = add_query_arg( 'readme', 'true', $latest_version_endpoint );
17912
+ }
17913
+
17914
+ $tag = $this->get_api_site_or_plugin_scope()->get(
17915
+ $latest_version_endpoint,
17916
+ $flush,
17917
+ $expiration
17918
+ );
17919
+
17920
+ if ( is_numeric( $switch_to_blog_id ) ) {
17921
+ $this->switch_to_blog( $current_blog_id );
17922
+ }
17923
+
17924
+ $latest_version = ( is_object( $tag ) && isset( $tag->version ) ) ? $tag->version : 'couldn\'t get';
17925
+
17926
+ $this->_logger->departure( 'Latest version ' . $latest_version );
17927
+
17928
+ return ( is_object( $tag ) && isset( $tag->version ) ) ? $tag : false;
17929
+ }
17930
+
17931
+ #----------------------------------------------------------------------------------
17932
+ #region Download Plugin
17933
+ #----------------------------------------------------------------------------------
17934
+
17935
+ /**
17936
+ * Download latest plugin version, based on plan.
17937
+ *
17938
+ * Not like _download_latest(), this will redirect the page
17939
+ * to secure download url to prevent dual download (from FS to WP server,
17940
+ * and then from WP server to the client / browser).
17941
+ *
17942
+ * @author Vova Feldman (@svovaf)
17943
+ * @since 1.0.9
17944
+ *
17945
+ * @param bool|number $plugin_id
17946
+ *
17947
+ * @uses FS_Api
17948
+ * @uses wp_redirect()
17949
+ */
17950
+ private function download_latest_directly( $plugin_id = false ) {
17951
+ $this->_logger->entrance();
17952
+
17953
+ wp_redirect( $this->get_latest_download_api_url( $plugin_id ) );
17954
+ }
17955
+
17956
+ /**
17957
+ * Get latest plugin FS API download URL.
17958
+ *
17959
+ * @author Vova Feldman (@svovaf)
17960
+ * @since 1.0.9
17961
+ *
17962
+ * @param bool|number $plugin_id
17963
+ *
17964
+ * @return string
17965
+ */
17966
+ private function get_latest_download_api_url( $plugin_id = false ) {
17967
+ $this->_logger->entrance();
17968
+
17969
+ return $this->get_api_site_scope()->get_signed_url(
17970
+ $this->_get_latest_version_endpoint( $plugin_id, 'zip' )
17971
+ );
17972
+ }
17973
+
17974
+ /**
17975
+ * Get payment invoice URL.
17976
+ *
17977
+ * @author Vova Feldman (@svovaf)
17978
+ * @since 1.2.0
17979
+ *
17980
+ * @param bool|number $payment_id
17981
+ *
17982
+ * @return string
17983
+ */
17984
+ function _get_invoice_api_url( $payment_id = false ) {
17985
+ $this->_logger->entrance();
17986
+
17987
+ return $this->get_api_user_scope()->get_signed_url(
17988
+ "/payments/{$payment_id}/invoice.pdf"
17989
+ );
17990
+ }
17991
+
17992
+ /**
17993
+ * Get latest plugin download link.
17994
+ *
17995
+ * @author Vova Feldman (@svovaf)
17996
+ * @since 1.0.9
17997
+ *
17998
+ * @param string $label
17999
+ * @param bool|number $plugin_id
18000
+ *
18001
+ * @return string
18002
+ */
18003
+ private function get_latest_download_link( $label, $plugin_id = false ) {
18004
+ return sprintf(
18005
+ '<a target="_blank" href="%s">%s</a>',
18006
+ $this->_get_latest_download_local_url( $plugin_id ),
18007
+ $label
18008
+ );
18009
+ }
18010
+
18011
+ /**
18012
+ * Get latest plugin download local URL.
18013
+ *
18014
+ * @author Vova Feldman (@svovaf)
18015
+ * @since 1.0.9
18016
+ *
18017
+ * @param bool|number $plugin_id
18018
+ *
18019
+ * @return string
18020
+ */
18021
+ function _get_latest_download_local_url( $plugin_id = false ) {
18022
+ // Add timestamp to protect from caching.
18023
+ $params = array( 'ts' => WP_FS__SCRIPT_START_TIME );
18024
+
18025
+ if ( ! empty( $plugin_id ) ) {
18026
+ $params['plugin_id'] = $plugin_id;
18027
+ } else if ( $this->is_addon() ) {
18028
+ $params['plugin_id'] = $this->get_id();
18029
+ }
18030
+
18031
+ $fs = $this->is_addon() ?
18032
+ $this->get_parent_instance() :
18033
+ $this;
18034
+
18035
+ return $fs->get_account_url( 'download_latest', $params );
18036
+ }
18037
+
18038
+ #endregion Download Plugin ------------------------------------------------------------------
18039
+
18040
+ /**
18041
+ * @author Vova Feldman (@svovaf)
18042
+ * @since 1.0.4
18043
+ *
18044
+ * @uses FS_Api
18045
+ *
18046
+ * @param bool $background Hints the method if it's a background updates check. If false, it means that
18047
+ * was initiated by the admin.
18048
+ * @param bool|number $plugin_id
18049
+ * @param bool $flush Since 1.1.7.3
18050
+ * @param int $expiration Since 1.2.2.7
18051
+ * @param bool|string $newer_than Since 2.2.1
18052
+ */
18053
+ private function check_updates(
18054
+ $background = false,
18055
+ $plugin_id = false,
18056
+ $flush = true,
18057
+ $expiration = WP_FS__TIME_24_HOURS_IN_SEC,
18058
+ $newer_than = false
18059
+ ) {
18060
+ $this->_logger->entrance();
18061
+
18062
+ // Check if there's a newer version for download.
18063
+ $new_version = $this->_fetch_newer_version( $plugin_id, $flush, $expiration, $newer_than );
18064
+
18065
+ $update = null;
18066
+ if ( is_object( $new_version ) ) {
18067
+ $update = new FS_Plugin_Tag( $new_version );
18068
+
18069
+ if ( ! $background ) {
18070
+ $this->_admin_notices->add(
18071
+ sprintf(
18072
+ /* translators: %s: Numeric version number (e.g. '2.1.9' */
18073
+ $this->get_text_inline( 'Version %s was released.', 'version-x-released' ) . ' ' . $this->get_text_inline( 'Please download %s.', 'please-download-x' ),
18074
+ $update->version,
18075
+ sprintf(
18076
+ '<a href="%s" target="_blank">%s</a>',
18077
+ $this->get_account_url( 'download_latest' ),
18078
+ sprintf(
18079
+ /* translators: %s: plan name (e.g. latest "Professional" version) */
18080
+ $this->get_text_inline( 'the latest %s version here', 'latest-x-version' ),
18081
+ $this->get_plan_title()
18082
+ )
18083
+ )
18084
+ ),
18085
+ $this->get_text_inline( 'New', 'new' ) . '!'
18086
+ );
18087
+ }
18088
+ } else if ( false === $new_version && ! $background ) {
18089
+ $this->_admin_notices->add(
18090
+ $this->get_text_inline( 'Seems like you got the latest release.', 'you-have-latest' ),
18091
+ $this->get_text_inline( 'You are all good!', 'you-are-good' )
18092
+ );
18093
+ }
18094
+
18095
+ $this->_store_update( $update, true, $plugin_id );
18096
+ }
18097
+
18098
+ /**
18099
+ * @author Vova Feldman (@svovaf)
18100
+ * @since 1.0.4
18101
+ *
18102
+ * @param bool $flush Since 1.1.7.3 add 24 hour cache by default.
18103
+ *
18104
+ * @return FS_Plugin[]
18105
+ *
18106
+ * @uses FS_Api
18107
+ */
18108
+ private function sync_addons( $flush = false ) {
18109
+ $this->_logger->entrance();
18110
+
18111
+ $api = $this->get_api_site_or_plugin_scope();
18112
+
18113
+ $path = $this->add_show_pending( '/addons.json?enriched=true' );
18114
+
18115
+ /**
18116
+ * @since 1.2.1
18117
+ *
18118
+ * If there's a cached version of the add-ons and not asking
18119
+ * for a flush, just use the currently stored add-ons.
18120
+ */
18121
+ if ( ! $flush && $api->is_cached( $path ) ) {
18122
+ $addons = self::get_all_addons();
18123
+
18124
+ return $addons[ $this->_plugin->id ];
18125
+ }
18126
+
18127
+ $result = $api->get( $path, $flush );
18128
+
18129
+ $addons = array();
18130
+ if ( $this->is_api_result_object( $result, 'plugins' ) &&
18131
+ is_array( $result->plugins )
18132
+ ) {
18133
+ for ( $i = 0, $len = count( $result->plugins ); $i < $len; $i ++ ) {
18134
+ $addons[ $i ] = new FS_Plugin( $result->plugins[ $i ] );
18135
+ }
18136
+
18137
+ $this->_store_addons( $addons, true );
18138
+ }
18139
+
18140
+ return $addons;
18141
+ }
18142
+
18143
+ /**
18144
+ * Handle user email update.
18145
+ *
18146
+ * @author Vova Feldman (@svovaf)
18147
+ * @since 1.0.3
18148
+ * @uses FS_Api
18149
+ *
18150
+ * @param string $new_email
18151
+ *
18152
+ * @return object
18153
+ */
18154
+ private function update_email( $new_email ) {
18155
+ $this->_logger->entrance();
18156
+
18157
+
18158
+ $api = $this->get_api_user_scope();
18159
+ $user = $api->call( "?plugin_id={$this->_plugin->id}&fields=id,email,is_verified", 'put', array(
18160
+ 'email' => $new_email,
18161
+ 'after_email_confirm_url' => $this->_get_admin_page_url(
18162
+ 'account',
18163
+ array( 'fs_action' => 'sync_user' )
18164
+ ),
18165
+ ) );
18166
+
18167
+ if ( ! isset( $user->error ) ) {
18168
+ $this->_user->email = $user->email;
18169
+ $this->_user->is_verified = $user->is_verified;
18170
+ $this->_store_user();
18171
+ } else {
18172
+ // handle different error cases.
18173
+
18174
+ }
18175
+
18176
+ return $user;
18177
+ }
18178
+
18179
+ #----------------------------------------------------------------------------------
18180
+ #region API Error Handling
18181
+ #----------------------------------------------------------------------------------
18182
+
18183
+ /**
18184
+ * @author Vova Feldman (@svovaf)
18185
+ * @since 1.1.1
18186
+ *
18187
+ * @param mixed $result
18188
+ *
18189
+ * @return bool Is API result contains an error.
18190
+ */
18191
+ private function is_api_error( $result ) {
18192
+ return FS_Api::is_api_error( $result );
18193
+ }
18194
+
18195
+ /**
18196
+ * Checks if given API result is a non-empty and not an error object.
18197
+ *
18198
+ * @author Vova Feldman (@svovaf)
18199
+ * @since 1.2.1.5
18200
+ *
18201
+ * @param mixed $result
18202
+ * @param string|null $required_property Optional property we want to verify that is set.
18203
+ *
18204
+ * @return bool
18205
+ */
18206
+ function is_api_result_object( $result, $required_property = null ) {
18207
+ return FS_Api::is_api_result_object( $result, $required_property );
18208
+ }
18209
+
18210
+ /**
18211
+ * Checks if given API result is a non-empty entity object with non-empty ID.
18212
+ *
18213
+ * @author Vova Feldman (@svovaf)
18214
+ * @since 1.2.1.5
18215
+ *
18216
+ * @param mixed $result
18217
+ *
18218
+ * @return bool
18219
+ */
18220
+ private function is_api_result_entity( $result ) {
18221
+ return FS_Api::is_api_result_entity( $result );
18222
+ }
18223
+
18224
+ #endregion
18225
+
18226
+ /**
18227
+ * Make sure a given argument is an array of a specific type.
18228
+ *
18229
+ * @author Vova Feldman (@svovaf)
18230
+ * @since 1.2.1.5
18231
+ *
18232
+ * @param mixed $array
18233
+ * @param string $class
18234
+ *
18235
+ * @return bool
18236
+ */
18237
+ private function is_array_instanceof( $array, $class ) {
18238
+ return ( is_array( $array ) && ( empty( $array ) || $array[0] instanceof $class ) );
18239
+ }
18240
+
18241
+ /**
18242
+ * Start install ownership change.
18243
+ *
18244
+ * @author Vova Feldman (@svovaf)
18245
+ * @since 1.1.1
18246
+ * @uses FS_Api
18247
+ *
18248
+ * @param string $candidate_email
18249
+ *
18250
+ * @return bool Is ownership change successfully initiated.
18251
+ */
18252
+ private function init_change_owner( $candidate_email ) {
18253
+ $this->_logger->entrance();
18254
+
18255
+ $api = $this->get_api_site_scope();
18256
+ $result = $api->call( "/users/{$this->_user->id}.json", 'put', array(
18257
+ 'email' => $candidate_email,
18258
+ 'after_confirm_url' => $this->_get_admin_page_url(
18259
+ 'account',
18260
+ array( 'fs_action' => 'change_owner' )
18261
+ ),
18262
+ ) );
18263
+
18264
+ return ! $this->is_api_error( $result );
18265
+ }
18266
+
18267
+ /**
18268
+ * Handle install ownership change.
18269
+ *
18270
+ * @author Vova Feldman (@svovaf)
18271
+ * @since 1.1.1
18272
+ * @uses FS_Api
18273
+ *
18274
+ * @return bool Was ownership change successfully complete.
18275
+ */
18276
+ private function complete_change_owner() {
18277
+ $this->_logger->entrance();
18278
+
18279
+ $site_result = $this->get_api_site_scope( true )->get();
18280
+ $site = new FS_Site( $site_result );
18281
+ $this->_site = $site;
18282
+
18283
+ $user = new FS_User();
18284
+ $user->id = fs_request_get( 'user_id' );
18285
+
18286
+ // Validate install's user and given user.
18287
+ if ( $user->id != $this->_site->user_id ) {
18288
+ return false;
18289
+ }
18290
+
18291
+ $user->public_key = fs_request_get( 'user_public_key' );
18292
+ $user->secret_key = fs_request_get( 'user_secret_key' );
18293
+
18294
+ // Fetch new user information.
18295
+ $this->_user = $user;
18296
+ $user_result = $this->get_api_user_scope( true )->get();
18297
+ $user = new FS_User( $user_result );
18298
+ $this->_user = $user;
18299
+
18300
+ $this->_set_account( $user, $site );
18301
+
18302
+ return true;
18303
+ }
18304
+
18305
+ /**
18306
+ * Handle user name update.
18307
+ *
18308
+ * @author Vova Feldman (@svovaf)
18309
+ * @since 1.0.9
18310
+ * @uses FS_Api
18311
+ *
18312
+ * @return object
18313
+ */
18314
+ private function update_user_name() {
18315
+ $this->_logger->entrance();
18316
+ $name = fs_request_get( 'fs_user_name_' . $this->get_unique_affix(), '' );
18317
+
18318
+ $api = $this->get_api_user_scope();
18319
+ $user = $api->call( "?plugin_id={$this->_plugin->id}&fields=id,first,last", 'put', array(
18320
+ 'name' => $name,
18321
+ ) );
18322
+
18323
+ if ( ! isset( $user->error ) ) {
18324
+ $this->_user->first = $user->first;
18325
+ $this->_user->last = $user->last;
18326
+ $this->_store_user();
18327
+ } else {
18328
+ // handle different error cases.
18329
+
18330
+ }
18331
+
18332
+ return $user;
18333
+ }
18334
+
18335
+ /**
18336
+ * Verify user email.
18337
+ *
18338
+ * @author Vova Feldman (@svovaf)
18339
+ * @since 1.0.3
18340
+ * @uses FS_Api
18341
+ */
18342
+ private function verify_email() {
18343
+ $this->_handle_account_user_sync();
18344
+
18345
+ if ( $this->_user->is_verified() ) {
18346
+ return;
18347
+ }
18348
+
18349
+ $api = $this->get_api_site_scope();
18350
+ $result = $api->call( "/users/{$this->_user->id}/verify.json", 'put', array(
18351
+ 'after_email_confirm_url' => $this->_get_admin_page_url(
18352
+ 'account',
18353
+ array( 'fs_action' => 'sync_user' )
18354
+ )
18355
+ ) );
18356
+
18357
+ if ( ! isset( $result->error ) ) {
18358
+ $this->_admin_notices->add( sprintf(
18359
+ $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' ),
18360
+ sprintf( '<a href="mailto:%1s">%2s</a>', esc_url( $this->_user->email ), $this->_user->email )
18361
+ ) );
18362
+ } else {
18363
+ // handle different error cases.
18364
+
18365
+ }
18366
+ }
18367
+
18368
+ /**
18369
+ * @author Vova Feldman (@svovaf)
18370
+ * @since 1.1.2
18371
+ *
18372
+ * @param array $params
18373
+ * @param bool|null $network
18374
+ *
18375
+ * @return string
18376
+ */
18377
+ function get_activation_url( $params = array(), $network = null ) {
18378
+ if ( $this->is_addon() && $this->has_free_plan() ) {
18379
+ /**
18380
+ * @author Vova Feldman (@svovaf)
18381
+ * @since 1.2.1.7 Add-on's activation is the parent's module activation.
18382
+ */
18383
+ return $this->get_parent_instance()->get_activation_url( $params );
18384
+ }
18385
+
18386
+ return $this->apply_filters( 'connect_url', $this->_get_admin_page_url( '', $params, $network ) );
18387
+ }
18388
+
18389
+ /**
18390
+ * @author Vova Feldman (@svovaf)
18391
+ * @since 1.2.1.5
18392
+ *
18393
+ * @param array $params
18394
+ *
18395
+ * @return string
18396
+ */
18397
+ function get_reconnect_url( $params = array() ) {
18398
+ $params['fs_action'] = 'reset_anonymous_mode';
18399
+ $params['fs_unique_affix'] = $this->get_unique_affix();
18400
+
18401
+ return $this->get_activation_url( $params );
18402
+ }
18403
+
18404
+ /**
18405
+ * Get the URL of the page that should be loaded after the user connect
18406
+ * or skip in the opt-in screen.
18407
+ *
18408
+ * @author Vova Feldman (@svovaf)
18409
+ * @since 1.1.3
18410
+ *
18411
+ * @param string $filter Filter name.
18412
+ * @param array $params Since 1.2.2.7
18413
+ * @param bool|null $network
18414
+ *
18415
+ * @return string
18416
+ */
18417
+ function get_after_activation_url( $filter, $params = array(), $network = null ) {
18418
+ if ( $this->is_free_wp_org_theme() &&
18419
+ fs_request_has( 'pending_activation' )
18420
+ ) {
18421
+ $first_time_path = '';
18422
+ } else {
18423
+ $first_time_path = $this->_menu->get_first_time_path();
18424
+ }
18425
+
18426
+ if ( $this->_is_network_active &&
18427
+ fs_is_network_admin() &&
18428
+ ! $this->_menu->has_network_menu() &&
18429
+ $this->is_network_registered()
18430
+ ) {
18431
+ $target_url = $this->get_account_url();
18432
+ } else {
18433
+ // Default plugin's page.
18434
+ $target_url = $this->_get_admin_page_url( '', array(), $network );
18435
+ }
18436
+
18437
+ return add_query_arg( $params, $this->apply_filters(
18438
+ $filter,
18439
+ empty( $first_time_path ) ?
18440
+ $target_url :
18441
+ $first_time_path
18442
+ ) );
18443
+ }
18444
+
18445
+ /**
18446
+ * Handle account page updates / edits / actions.
18447
+ *
18448
+ * @author Vova Feldman (@svovaf)
18449
+ * @since 1.0.2
18450
+ *
18451
+ */
18452
+ private function _handle_account_edits() {
18453
+ if ( ! $this->is_user_admin() ) {
18454
+ return;
18455
+ }
18456
+
18457
+ $action = fs_get_action();
18458
+
18459
+ if ( empty( $action ) ) {
18460
+ return;
18461
+ }
18462
+
18463
+ $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
18464
+ $install_id = fs_request_get( 'install_id', '' );
18465
+
18466
+ // Alias.
18467
+ $oops_text = $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...';
18468
+
18469
+ $is_network_action = $this->is_network_level_action();
18470
+ $blog_id = $this->is_network_level_site_specific_action();
18471
+
18472
+ if ( is_numeric( $blog_id ) ) {
18473
+ $this->switch_to_blog( $blog_id );
18474
+ } else {
18475
+ $blog_id = '';
18476
+ }
18477
+
18478
+ switch ( $action ) {
18479
+ case 'opt_in':
18480
+ check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
18481
+
18482
+ if ( $plugin_id == $this->get_id() ) {
18483
+ if ( $is_network_action && ! empty( $blog_id ) ) {
18484
+ if ( ! $this->is_registered() ) {
18485
+ $this->install_with_user(
18486
+ $this->get_network_user(),
18487
+ false,
18488
+ false,
18489
+ false,
18490
+ false
18491
+ );
18492
+
18493
+ $this->_admin_notices->add(
18494
+ $this->get_text_inline( 'Site successfully opted in.', 'successful-opt-in' ),
18495
+ $this->get_text_inline( 'Awesome', 'awesome' )
18496
+ );
18497
+ }
18498
+ }
18499
+ }
18500
+ break;
18501
+
18502
+ case 'toggle_tracking':
18503
+ check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
18504
+
18505
+ if ( $plugin_id == $this->get_id() ) {
18506
+ if ( $is_network_action && ! empty( $blog_id ) ) {
18507
+ if ( $this->is_registered() ) {
18508
+ if ( $this->is_tracking_prohibited() ) {
18509
+ if ( $this->allow_site_tracking() ) {
18510
+ $this->_admin_notices->add(
18511
+ 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 ),
18512
+ $this->get_text_inline( 'Thank you!', 'thank-you' )
18513
+ );
18514
+ }
18515
+ } else {
18516
+ if ( $this->stop_site_tracking() ) {
18517
+ $this->_admin_notices->add(
18518
+ sprintf(
18519
+ $this->get_text_inline( 'We will no longer be sending any usage data of %s on %s to %s.', 'opted-out-successfully' ),
18520
+ $this->get_plugin_title(),
18521
+ fs_strip_url_protocol( get_site_url( $blog_id ) ),
18522
+ sprintf(
18523
+ '<a href="%s" target="_blank">%s</a>',
18524
+ 'https://freemius.com',
18525
+ 'freemius.com'
18526
+ )
18527
+ )
18528
+ );
18529
+ }
18530
+ }
18531
+ }
18532
+ }
18533
+ }
18534
+
18535
+ break;
18536
+
18537
+ case 'delete_account':
18538
+ check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
18539
+
18540
+ if ( $plugin_id == $this->get_id() ) {
18541
+ if ( $is_network_action && empty( $blog_id ) ) {
18542
+ $this->delete_network_account_event();
18543
+ } else {
18544
+ $this->delete_account_event();
18545
+ }
18546
+
18547
+ // Clear user and site.
18548
+ $this->_site = null;
18549
+ $this->_user = null;
18550
+
18551
+ $this->maybe_set_slug_and_network_menu_exists_flag();
18552
+
18553
+ fs_redirect( $this->get_activation_url() );
18554
+ } else {
18555
+ if ( $this->is_addon_activated( $plugin_id ) ) {
18556
+ $fs_addon = self::get_instance_by_id( $plugin_id );
18557
+ $fs_addon->delete_account_event();
18558
+
18559
+ fs_redirect( $this->_get_admin_page_url( 'account' ) );
18560
+ }
18561
+ }
18562
+
18563
+ return;
18564
+
18565
+ case 'downgrade_account':
18566
+ if ( is_numeric( $blog_id ) ) {
18567
+ check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
18568
+ } else {
18569
+ check_admin_referer( $action );
18570
+ }
18571
+
18572
+ $switch_to_network_install_blog_after_cancellation = (
18573
+ is_numeric( $blog_id ) &&
18574
+ $plugin_id == $this->get_id() &&
18575
+ ! $this->is_trial()
18576
+ );
18577
+
18578
+ $result = $this->cancel_subscription_or_trial( $plugin_id );
18579
+ if ( $this->is_api_error( $result ) ) {
18580
+ $this->_admin_notices->add(
18581
+ $result->error->message,
18582
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
18583
+ 'error'
18584
+ );
18585
+ }
18586
+
18587
+ if ( $switch_to_network_install_blog_after_cancellation ) {
18588
+ $this->switch_to_blog( $this->_storage->network_install_blog_id );
18589
+ }
18590
+
18591
+ return;
18592
+
18593
+ case 'activate_license':
18594
+ check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
18595
+
18596
+ $fs = $this;
18597
+ if ( $plugin_id != $this->get_id() ) {
18598
+ $fs = $this->is_addon_activated( $plugin_id ) ?
18599
+ self::get_instance_by_id( $plugin_id ) :
18600
+ null;
18601
+ }
18602
+
18603
+ if ( is_object( $fs ) ) {
18604
+ $fs->_activate_license();
18605
+ }
18606
+
18607
+ return;
18608
+
18609
+ case 'deactivate_license':
18610
+ check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
18611
+
18612
+ if ( $plugin_id == $this->get_id() ) {
18613
+ $this->_deactivate_license();
18614
+
18615
+ if ( $this->is_only_premium() ) {
18616
+ // Clear user and site.
18617
+ $this->_site = null;
18618
+ $this->_user = null;
18619
+
18620
+ if ( ! $is_network_action ) {
18621
+ fs_redirect( $this->get_activation_url() );
18622
+ } else if ( is_numeric( $blog_id ) ) {
18623
+ $this->switch_to_blog( $this->_storage->network_install_blog_id );
18624
+ }
18625
+ }
18626
+ } else {
18627
+ if ( $this->is_addon_activated( $plugin_id ) ) {
18628
+ $fs_addon = self::get_instance_by_id( $plugin_id );
18629
+ $fs_addon->_deactivate_license();
18630
+ }
18631
+ }
18632
+
18633
+ return;
18634
+
18635
+ case 'check_updates':
18636
+ check_admin_referer( $action );
18637
+ $this->check_updates();
18638
+
18639
+ return;
18640
+
18641
+ case 'change_owner':
18642
+ $state = fs_request_get( 'state', 'init' );
18643
+ switch ( $state ) {
18644
+ case 'init':
18645
+ $candidate_email = fs_request_get( 'candidate_email', '' );
18646
+
18647
+ if ( $this->init_change_owner( $candidate_email ) ) {
18648
+ $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>' ) );
18649
+ }
18650
+ break;
18651
+ case 'owner_confirmed':
18652
+ $candidate_email = fs_request_get( 'candidate_email', '' );
18653
+
18654
+ $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>' ) );
18655
+ break;
18656
+ case 'candidate_confirmed':
18657
+ if ( $this->complete_change_owner() ) {
18658
+ $this->_admin_notices->add_sticky(
18659
+ sprintf( $this->get_text_inline( '%s is the new owner of the account.', 'change-owner-request_candidate-confirmed' ), '<b>' . $this->_user->email . '</b>' ),
18660
+ 'ownership_changed',
18661
+ $this->get_text_x_inline( 'Congrats', 'as congratulations', 'congrats' ) . '!'
18662
+ );
18663
+ } else {
18664
+ // @todo Handle failed ownership change message.
18665
+ }
18666
+ break;
18667
+ }
18668
+
18669
+ return;
18670
+
18671
+ case 'update_email':
18672
+ check_admin_referer( 'update_email' );
18673
+
18674
+ $new_email = fs_request_get( 'fs_email_' . $this->get_unique_affix(), '' );
18675
+ $result = $this->update_email( $new_email );
18676
+
18677
+ if ( isset( $result->error ) ) {
18678
+ switch ( $result->error->code ) {
18679
+ case 'user_exist':
18680
+ $this->_admin_notices->add(
18681
+ $this->get_text_inline( 'Sorry, we could not complete the email update. Another user with the same email is already registered.', 'user-exist-message' ) . ' ' .
18682
+ 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>' ) .
18683
+ sprintf(
18684
+ '<a style="margin-left: 10px;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
18685
+ $this->get_account_url( 'change_owner', array(
18686
+ 'state' => 'init',
18687
+ 'candidate_email' => $new_email
18688
+ ) ),
18689
+ $this->get_text_inline( 'Change Ownership', 'change-ownership' )
18690
+ ),
18691
+ $oops_text,
18692
+ 'error'
18693
+ );
18694
+ break;
18695
+ }
18696
+ } else {
18697
+ $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' ) );
18698
+ }
18699
+
18700
+ return;
18701
+
18702
+ case 'update_user_name':
18703
+ check_admin_referer( 'update_user_name' );
18704
+
18705
+ $result = $this->update_user_name();
18706
+
18707
+ if ( isset( $result->error ) ) {
18708
+ $this->_admin_notices->add(
18709
+ $this->get_text_inline( 'Please provide your full name.', 'name-update-failed-message' ),
18710
+ $oops_text,
18711
+ 'error'
18712
+ );
18713
+ } else {
18714
+ $this->_admin_notices->add( $this->get_text_inline( 'Your name was successfully updated.', 'name-updated-message' ) );
18715
+ }
18716
+
18717
+ return;
18718
+
18719
+ #region Actions that might be called from external links (e.g. email)
18720
+
18721
+ case 'cancel_trial':
18722
+ $result = $this->cancel_subscription_or_trial( $plugin_id );
18723
+ if ( $this->is_api_error( $result ) ) {
18724
+ $this->_admin_notices->add(
18725
+ $result->error->message,
18726
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
18727
+ 'error'
18728
+ );
18729
+ }
18730
+
18731
+ return;
18732
+
18733
+ case 'verify_email':
18734
+ $this->verify_email();
18735
+
18736
+ return;
18737
+
18738
+ case 'sync_user':
18739
+ $this->_handle_account_user_sync();
18740
+
18741
+ return;
18742
+
18743
+ case $this->get_unique_affix() . '_sync_license':
18744
+ $this->_sync_license();
18745
+
18746
+ return;
18747
+
18748
+ case 'download_latest':
18749
+ $this->download_latest_directly( $plugin_id );
18750
+
18751
+ return;
18752
+
18753
+ #endregion
18754
+ }
18755
+
18756
+ if ( WP_FS__IS_POST_REQUEST ) {
18757
+ $properties = array( 'site_secret_key', 'site_id', 'site_public_key' );
18758
+ foreach ( $properties as $p ) {
18759
+ if ( 'update_' . $p === $action ) {
18760
+ check_admin_referer( $action );
18761
+
18762
+ $this->_logger->log( $action );
18763
+
18764
+ $site_property = substr( $p, strlen( 'site_' ) );
18765
+ $site_property_value = fs_request_get( 'fs_' . $p . '_' . $this->get_unique_affix(), '' );
18766
+ $this->get_site()->{$site_property} = $site_property_value;
18767
+
18768
+ // Store account after modification.
18769
+ $this->_store_site();
18770
+
18771
+ $this->do_action( 'account_property_edit', 'site', $site_property, $site_property_value );
18772
+
18773
+ $this->_admin_notices->add( sprintf(
18774
+ /* translators: %s: User's account property (e.g. email address, name) */
18775
+ $this->get_text_inline( 'You have successfully updated your %s.', 'x-updated' ),
18776
+ '<b>' . str_replace( '_', ' ', $p ) . '</b>'
18777
+ ) );
18778
+
18779
+ return;
18780
+ }
18781
+ }
18782
+ }
18783
+ }
18784
+
18785
+ /**
18786
+ * Account page resources load.
18787
+ *
18788
+ * @author Vova Feldman (@svovaf)
18789
+ * @since 1.0.6
18790
+ */
18791
+ function _account_page_load() {
18792
+ $this->_logger->entrance();
18793
+
18794
+ $this->_logger->info( var_export( $_REQUEST, true ) );
18795
+
18796
+ fs_enqueue_local_style( 'fs_account', '/admin/account.css' );
18797
+
18798
+ if ( $this->has_addons() ) {
18799
+ wp_enqueue_script( 'plugin-install' );
18800
+ add_thickbox();
18801
+
18802
+ function fs_addons_body_class( $classes ) {
18803
+ $classes .= ' plugins-php';
18804
+
18805
+ return $classes;
18806
+ }
18807
+
18808
+ add_filter( 'admin_body_class', 'fs_addons_body_class' );
18809
+ }
18810
+
18811
+ if ( $this->has_paid_plan() &&
18812
+ ! $this->has_any_license() &&
18813
+ ! $this->is_sync_executed() &&
18814
+ $this->is_tracking_allowed()
18815
+ ) {
18816
+ /**
18817
+ * If no licenses found and no sync job was executed during the last 24 hours,
18818
+ * just execute the sync job right away (blocking execution).
18819
+ *
18820
+ * @since 1.1.7.3
18821
+ */
18822
+ $this->run_manual_sync();
18823
+ }
18824
+
18825
+ $this->_handle_account_edits();
18826
+
18827
+ $this->do_action( 'account_page_load_before_departure' );
18828
+ }
18829
+
18830
+ /**
18831
+ * Renders the "Affiliation" page.
18832
+ *
18833
+ * @author Leo Fajardo (@leorw)
18834
+ * @since 1.2.3
18835
+ */
18836
+ function _affiliation_page_render() {
18837
+ $this->_logger->entrance();
18838
+
18839
+ $this->fetch_affiliate_and_terms();
18840
+
18841
+ fs_enqueue_local_style( 'fs_affiliation', '/admin/affiliation.css' );
18842
+
18843
+ $vars = array( 'id' => $this->_module_id );
18844
+ echo $this->apply_filters( "/forms/affiliation.php", fs_get_template( '/forms/affiliation.php', $vars ) );
18845
+ }
18846
+
18847
+
18848
+ /**
18849
+ * Render account page.
18850
+ *
18851
+ * @author Vova Feldman (@svovaf)
18852
+ * @since 1.0.0
18853
+ */
18854
+ function _account_page_render() {
18855
+ $this->_logger->entrance();
18856
+
18857
+ $template = 'account.php';
18858
+ $vars = array( 'id' => $this->_module_id );
18859
+
18860
+ /**
18861
+ * Added filter to the template to allow developers wrapping the template
18862
+ * in custom HTML (e.g. within a wizard/tabs).
18863
+ *
18864
+ * @author Vova Feldman (@svovaf)
18865
+ * @since 1.2.1.6
18866
+ */
18867
+ echo $this->apply_filters( "templates/{$template}", fs_get_template( $template, $vars ) );
18868
+ }
18869
+
18870
+ /**
18871
+ * Render account connect page.
18872
+ *
18873
+ * @author Vova Feldman (@svovaf)
18874
+ * @since 1.0.7
18875
+ */
18876
+ function _connect_page_render() {
18877
+ $this->_logger->entrance();
18878
+
18879
+ $vars = array( 'id' => $this->_module_id );
18880
+
18881
+ /**
18882
+ * Added filter to the template to allow developers wrapping the template
18883
+ * in custom HTML (e.g. within a wizard/tabs).
18884
+ *
18885
+ * @author Vova Feldman (@svovaf)
18886
+ * @since 1.2.1.6
18887
+ */
18888
+ echo $this->apply_filters( 'templates/connect.php', fs_get_template( 'connect.php', $vars ) );
18889
+ }
18890
+
18891
+ /**
18892
+ * Load required resources before add-ons page render.
18893
+ *
18894
+ * @author Vova Feldman (@svovaf)
18895
+ * @since 1.0.6
18896
+ */
18897
+ function _addons_page_load() {
18898
+ $this->_logger->entrance();
18899
+
18900
+ fs_enqueue_local_style( 'fs_addons', '/admin/add-ons.css' );
18901
+
18902
+ wp_enqueue_script( 'plugin-install' );
18903
+ add_thickbox();
18904
+
18905
+ function fs_addons_body_class( $classes ) {
18906
+ $classes .= ' plugins-php';
18907
+
18908
+ return $classes;
18909
+ }
18910
+
18911
+ add_filter( 'admin_body_class', 'fs_addons_body_class' );
18912
+
18913
+ if ( ! $this->is_registered() && $this->is_org_repo_compliant() ) {
18914
+ $this->_admin_notices->add(
18915
+ 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>' ),
18916
+ $this->get_text_x_inline( 'Heads up', 'advance notice of something that will need attention.', 'heads-up' ),
18917
+ 'update-nag'
18918
+ );
18919
+ }
18920
+ }
18921
+
18922
+ /**
18923
+ * Render add-ons page.
18924
+ *
18925
+ * @author Vova Feldman (@svovaf)
18926
+ * @since 1.0.6
18927
+ */
18928
+ function _addons_page_render() {
18929
+ $this->_logger->entrance();
18930
+
18931
+ $vars = array( 'id' => $this->_module_id );
18932
+
18933
+ /**
18934
+ * Added filter to the template to allow developers wrapping the template
18935
+ * in custom HTML (e.g. within a wizard/tabs).
18936
+ *
18937
+ * @author Vova Feldman (@svovaf)
18938
+ * @since 1.2.1.6
18939
+ */
18940
+ echo $this->apply_filters( 'templates/add-ons.php', fs_get_template( 'add-ons.php', $vars ) );
18941
+ }
18942
+
18943
+ /* Pricing & Upgrade
18944
+ ------------------------------------------------------------------------------------------------------------------*/
18945
+ /**
18946
+ * Render pricing page.
18947
+ *
18948
+ * @author Vova Feldman (@svovaf)
18949
+ * @since 1.0.0
18950
+ */
18951
+ function _pricing_page_render() {
18952
+ $this->_logger->entrance();
18953
+
18954
+ $vars = array( 'id' => $this->_module_id );
18955
+
18956
+ if ( 'true' === fs_request_get( 'checkout', false ) ) {
18957
+ echo $this->apply_filters( 'templates/checkout.php', fs_get_template( 'checkout.php', $vars ) );
18958
+ } else {
18959
+ echo $this->apply_filters( 'templates/pricing.php', fs_get_template( 'pricing.php', $vars ) );
18960
+ }
18961
+ }
18962
+
18963
+ #----------------------------------------------------------------------------------
18964
+ #region Contact Us
18965
+ #----------------------------------------------------------------------------------
18966
+
18967
+ /**
18968
+ * Render contact-us page.
18969
+ *
18970
+ * @author Vova Feldman (@svovaf)
18971
+ * @since 1.0.3
18972
+ */
18973
+ function _contact_page_render() {
18974
+ $this->_logger->entrance();
18975
+
18976
+ $vars = array( 'id' => $this->_module_id );
18977
+
18978
+ /**
18979
+ * Added filter to the template to allow developers wrapping the template
18980
+ * in custom HTML (e.g. within a wizard/tabs).
18981
+ *
18982
+ * @author Vova Feldman (@svovaf)
18983
+ * @since 2.1.3
18984
+ */
18985
+ echo $this->apply_filters( 'templates/contact.php', fs_get_template( 'contact.php', $vars ) );
18986
+ }
18987
+
18988
+ #endregion ------------------------------------------------------------------------
18989
+
18990
+ /**
18991
+ * Hide all admin notices to prevent distractions.
18992
+ *
18993
+ * @author Vova Feldman (@svovaf)
18994
+ * @since 1.0.3
18995
+ *
18996
+ * @uses remove_all_actions()
18997
+ */
18998
+ private static function _hide_admin_notices() {
18999
+ remove_all_actions( 'admin_notices' );
19000
+ remove_all_actions( 'network_admin_notices' );
19001
+ remove_all_actions( 'all_admin_notices' );
19002
+ remove_all_actions( 'user_admin_notices' );
19003
+ }
19004
+
19005
+ static function _clean_admin_content_section_hook() {
19006
+ self::_hide_admin_notices();
19007
+
19008
+ // Hide footer.
19009
+ echo '<style>#wpfooter { display: none !important; }</style>';
19010
+ }
19011
+
19012
+ /**
19013
+ * Attach to admin_head hook to hide all admin notices.
19014
+ *
19015
+ * @author Vova Feldman (@svovaf)
19016
+ * @since 1.0.3
19017
+ */
19018
+ static function _clean_admin_content_section() {
19019
+ add_action( 'admin_head', 'Freemius::_clean_admin_content_section_hook' );
19020
+ }
19021
+
19022
+ /* CSS & JavaScript
19023
+ ------------------------------------------------------------------------------------------------------------------*/
19024
+ /* function _enqueue_script($handle, $src) {
19025
+ $url = plugins_url( substr( WP_FS__DIR_JS, strlen( $this->_plugin_dir_path ) ) . '/assets/js/' . $src );
19026
+
19027
+ $this->_logger->entrance( 'script = ' . $url );
19028
+
19029
+ wp_enqueue_script( $handle, $url );
19030
+ }*/
19031
+
19032
+ /* SDK
19033
+ ------------------------------------------------------------------------------------------------------------------*/
19034
+ private $_user_api;
19035
+
19036
+ /**
19037
+ *
19038
+ * @author Vova Feldman (@svovaf)
19039
+ * @since 1.0.2
19040
+ *
19041
+ * @param bool $flush
19042
+ *
19043
+ * @return FS_Api
19044
+ */
19045
+ private function get_api_user_scope( $flush = false ) {
19046
+ if ( ! isset( $this->_user_api ) || $flush ) {
19047
+ $this->_user_api = $this->get_api_user_scope_by_user( $this->_user );
19048
+ }
19049
+
19050
+ return $this->_user_api;
19051
+ }
19052
+
19053
+ /**
19054
+ * @author Vova Feldman (@svovaf)
19055
+ * @since 2.0.0
19056
+ *
19057
+ * @param \FS_User $user
19058
+ *
19059
+ * @return \FS_Api
19060
+ */
19061
+ private function get_api_user_scope_by_user( FS_User $user ) {
19062
+ return FS_Api::instance(
19063
+ $this->_module_id,
19064
+ 'user',
19065
+ $user->id,
19066
+ $user->public_key,
19067
+ ! $this->is_live(),
19068
+ $user->secret_key
19069
+ );
19070
+ }
19071
+
19072
+ /**
19073
+ *
19074
+ * @author Leo Fajardo (@leorw)
19075
+ * @since 2.0.0
19076
+ *
19077
+ * @param bool $flush
19078
+ *
19079
+ * @return FS_Api
19080
+ */
19081
+ private function get_current_or_network_user_api_scope( $flush = false ) {
19082
+ if ( ! $this->_is_network_active ||
19083
+ ( isset( $this->_user ) && $this->_user instanceof FS_User )
19084
+ ) {
19085
+ return $this->get_api_user_scope( $flush );
19086
+ }
19087
+
19088
+ $user = $this->get_current_or_network_user();
19089
+
19090
+ $this->_user_api = FS_Api::instance(
19091
+ $this->_module_id,
19092
+ 'user',
19093
+ $user->id,
19094
+ $user->public_key,
19095
+ ! $this->is_live(),
19096
+ $user->secret_key
19097
+ );
19098
+
19099
+ return $this->_user_api;
19100
+ }
19101
+
19102
+ private $_site_api;
19103
+
19104
+ /**
19105
+ *
19106
+ * @author Vova Feldman (@svovaf)
19107
+ * @since 1.0.2
19108
+ *
19109
+ * @param bool $flush
19110
+ *
19111
+ * @return FS_Api
19112
+ */
19113
+ private function get_api_site_scope( $flush = false ) {
19114
+ if ( ! isset( $this->_site_api ) || $flush ) {
19115
+ $this->_site_api = FS_Api::instance(
19116
+ $this->_module_id,
19117
+ 'install',
19118
+ $this->_site->id,
19119
+ $this->_site->public_key,
19120
+ ! $this->is_live(),
19121
+ $this->_site->secret_key
19122
+ );
19123
+ }
19124
+
19125
+ return $this->_site_api;
19126
+ }
19127
+
19128
+ private $_plugin_api;
19129
+
19130
+ /**
19131
+ * Get plugin public API scope.
19132
+ *
19133
+ * @author Vova Feldman (@svovaf)
19134
+ * @since 1.0.7
19135
+ *
19136
+ * @return FS_Api
19137
+ */
19138
+ function get_api_plugin_scope() {
19139
+ if ( ! isset( $this->_plugin_api ) ) {
19140
+ $this->_plugin_api = FS_Api::instance(
19141
+ $this->_module_id,
19142
+ 'plugin',
19143
+ $this->_plugin->id,
19144
+ $this->_plugin->public_key,
19145
+ ! $this->is_live()
19146
+ );
19147
+ }
19148
+
19149
+ return $this->_plugin_api;
19150
+ }
19151
+
19152
+ /**
19153
+ * Get site API scope object (fallback to public plugin scope when not registered).
19154
+ *
19155
+ * @author Vova Feldman (@svovaf)
19156
+ * @since 1.0.7
19157
+ *
19158
+ * @return FS_Api
19159
+ */
19160
+ function get_api_site_or_plugin_scope() {
19161
+ return $this->is_registered() ?
19162
+ $this->get_api_site_scope() :
19163
+ $this->get_api_plugin_scope();
19164
+ }
19165
+
19166
+ /**
19167
+ * Show trial promotional notice (if any trial exist).
19168
+ *
19169
+ * @author Vova Feldman (@svovaf)
19170
+ * @since 1.0.9
19171
+ *
19172
+ * @param FS_Plugin_Plan[] $plans
19173
+ */
19174
+ function _check_for_trial_plans( $plans ) {
19175
+ /**
19176
+ * For some reason core's do_action() flattens arrays when it has a single object item. Therefore, we need to restructure the array as expected.
19177
+ *
19178
+ * @author Vova Feldman (@svovaf)
19179
+ * @since 2.1.2
19180
+ */
19181
+ if ( ! is_array( $plans ) && is_object( $plans ) ) {
19182
+ $plans = array( $plans );
19183
+ }
19184
+
19185
+ $this->_storage->has_trial_plan = FS_Plan_Manager::instance()->has_trial_plan( $plans );
19186
+ }
19187
+
19188
+ /**
19189
+ * During trial promotion the "upgrade" submenu item turns to
19190
+ * "start trial" to encourage the trial. Since we want to keep
19191
+ * the same menu item handler and there's no robust way to
19192
+ * add new arguments to the menu item link's querystring,
19193
+ * use JavaScript to find the menu item and update the href of
19194
+ * the link.
19195
+ *
19196
+ * @author Vova Feldman (@svovaf)
19197
+ * @since 1.2.1.5
19198
+ */
19199
+ function _fix_start_trial_menu_item_url() {
19200
+ $template_args = array( 'id' => $this->_module_id );
19201
+ fs_require_template( 'add-trial-to-pricing.php', $template_args );
19202
+ }
19203
+
19204
+ /**
19205
+ * Check if module is currently in a trial promotion mode.
19206
+ *
19207
+ * @author Vova Feldman (@svovaf)
19208
+ * @since 1.2.2.7
19209
+ *
19210
+ * @return bool
19211
+ */
19212
+ function is_in_trial_promotion() {
19213
+ return $this->_admin_notices->has_sticky( 'trial_promotion' );
19214
+ }
19215
+
19216
+ /**
19217
+ * Show trial promotional notice (if any trial exist).
19218
+ *
19219
+ * @author Vova Feldman (@svovaf)
19220
+ * @since 1.0.9
19221
+ *
19222
+ * @return bool If trial notice added.
19223
+ */
19224
+ function _add_trial_notice() {
19225
+ if ( ! $this->is_user_admin() ) {
19226
+ return false;
19227
+ }
19228
+
19229
+ if ( ! $this->is_user_in_admin() ) {
19230
+ return false;
19231
+ }
19232
+
19233
+ if ( $this->_is_network_active ) {
19234
+ if ( fs_is_network_admin() ) {
19235
+ // Network level trial is disabled at the moment.
19236
+ return false;
19237
+ }
19238
+
19239
+ if ( ! $this->is_delegated_connection() ) {
19240
+ // Only delegated sites should support trials.
19241
+ return false;
19242
+ }
19243
+ }
19244
+
19245
+ // Check if trial message is already shown.
19246
+ if ( $this->is_in_trial_promotion() ) {
19247
+ add_action( 'admin_footer', array( &$this, '_fix_start_trial_menu_item_url' ) );
19248
+
19249
+ $this->_menu->add_counter_to_menu_item( 1, 'fs-trial' );
19250
+
19251
+ return false;
19252
+ }
19253
+
19254
+ if ( $this->is_premium() && ! WP_FS__DEV_MODE ) {
19255
+ // Don't show trial if running the premium code, unless running in DEV mode.
19256
+ return false;
19257
+ }
19258
+
19259
+ if ( ! $this->has_trial_plan() ) {
19260
+ // No plans with trial.
19261
+ return false;
19262
+ }
19263
+
19264
+ if ( ! $this->apply_filters( 'show_trial', true ) ) {
19265
+ // Developer explicitly asked not to show the trial promo.
19266
+ return false;
19267
+ }
19268
+
19269
+ if ( $this->is_registered() ) {
19270
+ // Check if trial already utilized.
19271
+ if ( $this->_site->is_trial_utilized() ) {
19272
+ return false;
19273
+ }
19274
+
19275
+ if ( $this->is_paying_or_trial() ) {
19276
+ // Don't show trial if paying or already in trial.
19277
+ return false;
19278
+ }
19279
+ }
19280
+
19281
+ if ( $this->is_activation_mode() || $this->is_pending_activation() ) {
19282
+ // If not yet opted-in/skipped, or pending activation, don't show trial.
19283
+ return false;
19284
+ }
19285
+
19286
+ $last_time_trial_promotion_shown = $this->_storage->get( 'trial_promotion_shown', false );
19287
+ $was_promotion_shown_before = ( false !== $last_time_trial_promotion_shown );
19288
+
19289
+ // Show promotion if never shown before and 24 hours after initial activation with FS.
19290
+ if ( ! $was_promotion_shown_before &&
19291
+ $this->_storage->install_timestamp > ( time() - $this->apply_filters( 'show_first_trial_after_n_sec', WP_FS__TIME_24_HOURS_IN_SEC ) )
19292
+ ) {
19293
+ return false;
19294
+ }
19295
+
19296
+ // OR if promotion was shown before, try showing it every 30 days.
19297
+ if ( $was_promotion_shown_before &&
19298
+ $this->apply_filters( 'reshow_trial_after_every_n_sec', 30 * WP_FS__TIME_24_HOURS_IN_SEC ) > time() - $last_time_trial_promotion_shown
19299
+ ) {
19300
+ return false;
19301
+ }
19302
+
19303
+ $trial_period = $this->_trial_days;
19304
+ $require_payment = $this->_is_trial_require_payment;
19305
+ $trial_url = $this->get_trial_url();
19306
+ $plans_string = strtolower( $this->get_text_inline( 'Awesome', 'awesome' ) );
19307
+
19308
+ if ( $this->is_registered() ) {
19309
+ // If opted-in, override trial with up to date data from API.
19310
+ $trial_plans = FS_Plan_Manager::instance()->get_trial_plans( $this->_plans );
19311
+ $trial_plans_count = count( $trial_plans );
19312
+
19313
+ if ( 0 === $trial_plans_count ) {
19314
+ // If there's no plans with a trial just exit.
19315
+ return false;
19316
+ }
19317
+
19318
+ /**
19319
+ * @var FS_Plugin_Plan $paid_plan
19320
+ */
19321
+ $paid_plan = $trial_plans[0];
19322
+ $require_payment = $paid_plan->is_require_subscription;
19323
+ $trial_period = $paid_plan->trial_period;
19324
+
19325
+ $total_paid_plans = count( $this->_plans ) - ( FS_Plan_Manager::instance()->has_free_plan( $this->_plans ) ? 1 : 0 );
19326
+
19327
+ if ( $total_paid_plans !== $trial_plans_count ) {
19328
+ // Not all paid plans have a trial - generate a string of those that have it.
19329
+ for ( $i = 0; $i < $trial_plans_count; $i ++ ) {
19330
+ $plans_string .= sprintf(
19331
+ ' <a href="%s">%s</a>',
19332
+ $trial_url,
19333
+ $trial_plans[ $i ]->title
19334
+ );
19335
+
19336
+ if ( $i < $trial_plans_count - 2 ) {
19337
+ $plans_string .= ', ';
19338
+ } else if ( $i == $trial_plans_count - 2 ) {
19339
+ $plans_string .= ' and ';
19340
+ }
19341
+ }
19342
+ }
19343
+ }
19344
+
19345
+ $message = sprintf(
19346
+ $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' ),
19347
+ sprintf( '<b>%s</b>', $this->get_plugin_name() ),
19348
+ $plans_string,
19349
+ $trial_period
19350
+ );
19351
+
19352
+ // "No Credit-Card Required" or "No Commitment for N Days".
19353
+ $cc_string = $require_payment ?
19354
+ sprintf( $this->get_text_inline( 'No commitment for %s days - cancel anytime!', 'no-commitment-for-x-days' ), $trial_period ) :
19355
+ $this->get_text_inline( 'No credit card required', 'no-cc-required' ) . '!';
19356
+
19357
+
19358
+ // Start trial button.
19359
+ $button = ' ' . sprintf(
19360
+ '<a style="margin-left: 10px; vertical-align: super;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
19361
+ $trial_url,
19362
+ $this->get_text_x_inline( 'Start free trial', 'call to action', 'start-free-trial' )
19363
+ );
19364
+
19365
+ $this->_admin_notices->add_sticky(
19366
+ $this->apply_filters( 'trial_promotion_message', "{$message} {$cc_string} {$button}" ),
19367
+ 'trial_promotion',
19368
+ '',
19369
+ 'promotion'
19370
+ );
19371
+
19372
+ $this->_storage->trial_promotion_shown = WP_FS__SCRIPT_START_TIME;
19373
+
19374
+ return true;
19375
+ }
19376
+
19377
+ /**
19378
+ * Lets users/customers know that the product has an affiliate program.
19379
+ *
19380
+ * @author Leo Fajardo (@leorw)
19381
+ * @since 1.2.2.11
19382
+ *
19383
+ * @return bool Returns true if the notice has been added.
19384
+ */
19385
+ function _add_affiliate_program_notice() {
19386
+ if ( ! $this->is_user_admin() ) {
19387
+ return false;
19388
+ }
19389
+
19390
+ if ( ! $this->is_user_in_admin() ) {
19391
+ return false;
19392
+ }
19393
+
19394
+ // Check if the notice is already shown.
19395
+ if ( $this->_admin_notices->has_sticky( 'affiliate_program' ) ) {
19396
+ return false;
19397
+ }
19398
+
19399
+ if (
19400
+ // Product has no affiliate program.
19401
+ ! $this->has_affiliate_program() ||
19402
+ // User has applied for an affiliate account.
19403
+ ! empty( $this->_storage->affiliate_application_data )
19404
+ ) {
19405
+ return false;
19406
+ }
19407
+
19408
+ if ( ! $this->apply_filters( 'show_affiliate_program_notice', true ) ) {
19409
+ // Developer explicitly asked not to show the notice about the affiliate program.
19410
+ return false;
19411
+ }
19412
+
19413
+ if ( $this->is_activation_mode() || $this->is_pending_activation() ) {
19414
+ // If not yet opted in/skipped, or pending activation, don't show the notice.
19415
+ return false;
19416
+ }
19417
+
19418
+ $last_time_notice_was_shown = $this->_storage->get( 'affiliate_program_notice_shown', false );
19419
+ $was_notice_shown_before = ( false !== $last_time_notice_was_shown );
19420
+
19421
+ /**
19422
+ * Do not show the notice if it was already shown before or less than 30 days have passed since the initial
19423
+ * activation with FS.
19424
+ */
19425
+ if ( $was_notice_shown_before ||
19426
+ $this->_storage->install_timestamp > ( time() - ( WP_FS__TIME_24_HOURS_IN_SEC * 30 ) )
19427
+ ) {
19428
+ return false;
19429
+ }
19430
+
19431
+ if ( ! $this->is_paying() &&
19432
+ FS_Plugin::AFFILIATE_MODERATION_CUSTOMERS == $this->_plugin->affiliate_moderation
19433
+ ) {
19434
+ // If the user is not a customer and the affiliate program is only for customers, don't show the notice.
19435
+ return false;
19436
+ }
19437
+
19438
+ $message = sprintf(
19439
+ $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' ),
19440
+ sprintf( '<strong>%s</strong>', $this->get_plugin_name() ),
19441
+ $this->get_module_label( true )
19442
+ );
19443
+
19444
+ // HTML code for the "Learn more..." button.
19445
+ $button = ' ' . sprintf(
19446
+ '<a style="display: block; margin-top: 10px;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
19447
+ $this->_get_admin_page_url( 'affiliation' ),
19448
+ $this->get_text_inline( 'Learn more', 'learn-more' ) . '...'
19449
+ );
19450
+
19451
+ $this->_admin_notices->add_sticky(
19452
+ $this->apply_filters( 'affiliate_program_notice', "{$message} {$button}" ),
19453
+ 'affiliate_program',
19454
+ '',
19455
+ 'promotion'
19456
+ );
19457
+
19458
+ $this->_storage->affiliate_program_notice_shown = WP_FS__SCRIPT_START_TIME;
19459
+
19460
+ return true;
19461
+ }
19462
+
19463
+ /**
19464
+ * @author Vova Feldman (@svovaf)
19465
+ * @since 1.2.1.5
19466
+ */
19467
+ function _enqueue_common_css() {
19468
+ if ( $this->has_paid_plan() && ! $this->is_paying() ) {
19469
+ // Add basic CSS for admin-notices and menu-item colors.
19470
+ fs_enqueue_local_style( 'fs_common', '/admin/common.css' );
19471
+ }
19472
+ }
19473
+
19474
+ /**
19475
+ * @author Leo Fajardo (@leorw)
19476
+ * @since 1.2.2
19477
+ */
19478
+ function _show_theme_activation_optin_dialog() {
19479
+ fs_enqueue_local_style( 'fs_connect', '/admin/connect.css' );
19480
+
19481
+ add_action( 'admin_footer-themes.php', array( &$this, '_add_fs_theme_activation_dialog' ) );
19482
+ }
19483
+
19484
+ /**
19485
+ * @author Leo Fajardo (@leorw)
19486
+ * @since 1.2.2
19487
+ */
19488
+ function _add_fs_theme_activation_dialog() {
19489
+ $vars = array( 'id' => $this->_module_id );
19490
+ fs_require_once_template( 'connect.php', $vars );
19491
+ }
19492
+
19493
+ /* Action Links
19494
+ ------------------------------------------------------------------------------------------------------------------*/
19495
+ private $_action_links_hooked = false;
19496
+ private $_action_links = array();
19497
+
19498
+ /**
19499
+ * Hook to plugin action links filter.
19500
+ *
19501
+ * @author Vova Feldman (@svovaf)
19502
+ * @since 1.0.0
19503
+ */
19504
+ private function hook_plugin_action_links() {
19505
+ $this->_logger->entrance();
19506
+
19507
+ $this->_action_links_hooked = true;
19508
+
19509
+ $this->_logger->log( 'Adding action links hooks.' );
19510
+
19511
+ // Add action link to settings page.
19512
+ add_filter( 'plugin_action_links_' . $this->_plugin_basename, array(
19513
+ &$this,
19514
+ '_modify_plugin_action_links_hook'
19515
+ ), WP_FS__DEFAULT_PRIORITY, 2 );
19516
+ add_filter( 'network_admin_plugin_action_links_' . $this->_plugin_basename, array(
19517
+ &$this,
19518
+ '_modify_plugin_action_links_hook'
19519
+ ), WP_FS__DEFAULT_PRIORITY, 2 );
19520
+ }
19521
+
19522
+ /**
19523
+ * Add plugin action link.
19524
+ *
19525
+ * @author Vova Feldman (@svovaf)
19526
+ * @since 1.0.0
19527
+ *
19528
+ * @param $label
19529
+ * @param $url
19530
+ * @param bool $external
19531
+ * @param int $priority
19532
+ * @param bool $key
19533
+ */
19534
+ function add_plugin_action_link( $label, $url, $external = false, $priority = WP_FS__DEFAULT_PRIORITY, $key = false ) {
19535
+ $this->_logger->entrance();
19536
+
19537
+ if ( ! isset( $this->_action_links[ $priority ] ) ) {
19538
+ $this->_action_links[ $priority ] = array();
19539
+ }
19540
+
19541
+ if ( false === $key ) {
19542
+ $key = preg_replace( "/[^A-Za-z0-9 ]/", '', strtolower( $label ) );
19543
+ }
19544
+
19545
+ $this->_action_links[ $priority ][] = array(
19546
+ 'label' => $label,
19547
+ 'href' => $url,
19548
+ 'key' => $key,
19549
+ 'external' => $external
19550
+ );
19551
+ }
19552
+
19553
+ /**
19554
+ * Adds Upgrade and Add-Ons links to the main Plugins page link actions collection.
19555
+ *
19556
+ * @author Vova Feldman (@svovaf)
19557
+ * @since 1.0.0
19558
+ */
19559
+ function _add_upgrade_action_link() {
19560
+ $this->_logger->entrance();
19561
+
19562
+ if ( ! $this->is_paying() && $this->has_paid_plan() ) {
19563
+ $this->add_plugin_action_link(
19564
+ $this->get_text_inline( 'Upgrade', 'upgrade' ),
19565
+ $this->get_upgrade_url(),
19566
+ false,
19567
+ 7,
19568
+ 'upgrade'
19569
+ );
19570
+ }
19571
+
19572
+ if ( $this->has_addons() ) {
19573
+ $this->add_plugin_action_link(
19574
+ $this->get_text_inline( 'Add-Ons', 'add-ons' ),
19575
+ $this->_get_admin_page_url( 'addons' ),
19576
+ false,
19577
+ 9,
19578
+ 'addons'
19579
+ );
19580
+ }
19581
+ }
19582
+
19583
+ /**
19584
+ * Adds "Activate License" or "Change License" link to the main Plugins page link actions collection.
19585
+ *
19586
+ * @author Leo Fajardo (@leorw)
19587
+ * @since 1.1.9
19588
+ */
19589
+ function _add_license_action_link() {
19590
+ $this->_logger->entrance();
19591
+
19592
+ if ( ! self::is_ajax() ) {
19593
+ // Inject license activation dialog UI and client side code.
19594
+ add_action( 'admin_footer', array( &$this, '_add_license_activation_dialog_box' ) );
19595
+ }
19596
+
19597
+ $link_text = $this->is_free_plan() ?
19598
+ $this->get_text_inline( 'Activate License', 'activate-license' ) :
19599
+ $this->get_text_inline( 'Change License', 'change-license' );
19600
+
19601
+ $this->add_plugin_action_link(
19602
+ $link_text,
19603
+ '#',
19604
+ false,
19605
+ 11,
19606
+ ( 'activate-license ' . $this->get_unique_affix() )
19607
+ );
19608
+ }
19609
+
19610
+ /**
19611
+ * @author Leo Fajardo (@leorw)
19612
+ * @since 2.0.2
19613
+ */
19614
+ function _add_premium_version_upgrade_selection_action() {
19615
+ $this->_logger->entrance();
19616
+
19617
+ if ( ! self::is_ajax() ) {
19618
+ add_action( 'admin_footer', array( &$this, '_add_premium_version_upgrade_selection_dialog_box' ) );
19619
+ }
19620
+ }
19621
+
19622
+ /**
19623
+ * Adds "Opt in" or "Opt out" link to the main "Plugins" page link actions collection.
19624
+ *
19625
+ * @author Leo Fajardo (@leorw)
19626
+ * @since 1.2.1.5
19627
+ */
19628
+ function _add_tracking_links() {
19629
+ if ( ! current_user_can( 'manage_options' ) ) {
19630
+ return;
19631
+ }
19632
+
19633
+ $this->_logger->entrance();
19634
+
19635
+ /**
19636
+ * If the activation has been delegated to site admins, no tracking-related actions for now.
19637
+ *
19638
+ * @author Leo Fajardo (@leorw)
19639
+ */
19640
+ if ( $this->_is_network_active && $this->is_network_delegated_connection() ) {
19641
+ return;
19642
+ }
19643
+
19644
+ if ( fs_request_is_action_secure( $this->get_unique_affix() . '_reconnect' ) ) {
19645
+ if ( ! $this->is_registered() && $this->is_anonymous() ) {
19646
+ $this->connect_again();
19647
+
19648
+ return;
19649
+ }
19650
+ }
19651
+
19652
+ if ( ( $this->is_plugin() && ! self::is_plugins_page() ) ||
19653
+ ( $this->is_theme() && ! self::is_themes_page() )
19654
+ ) {
19655
+ // Only show tracking links on the plugins and themes pages.
19656
+ return;
19657
+ }
19658
+
19659
+ if ( ! $this->is_enable_anonymous() ) {
19660
+ // Don't allow to opt-out if anonymous mode is disabled.
19661
+ return;
19662
+ }
19663
+
19664
+ if ( ! $this->is_free_plan() ) {
19665
+ // Don't allow to opt-out if running in paid plan.
19666
+ return;
19667
+ }
19668
+
19669
+ if ( $this->add_ajax_action( 'stop_tracking', array( &$this, '_stop_tracking_callback' ) ) ) {
19670
+ return;
19671
+ }
19672
+
19673
+ if ( $this->add_ajax_action( 'allow_tracking', array( &$this, '_allow_tracking_callback' ) ) ) {
19674
+ return;
19675
+ }
19676
+
19677
+ $url = '#';
19678
+
19679
+ if ( $this->is_registered() ) {
19680
+ if ( $this->is_tracking_allowed() ) {
19681
+ $link_text_id = $this->get_text_inline( 'Opt Out', 'opt-out' );
19682
+ } else {
19683
+ $link_text_id = $this->get_text_inline( 'Opt In', 'opt-in' );
19684
+ }
19685
+
19686
+ add_action( 'admin_footer', array( &$this, '_add_optout_dialog' ) );
19687
+ } else {
19688
+ $link_text_id = $this->get_text_inline( 'Opt In', 'opt-in' );
19689
+
19690
+ $params = ! $this->is_anonymous() ?
19691
+ array() :
19692
+ array(
19693
+ 'nonce' => wp_create_nonce( $this->get_unique_affix() . '_reconnect' ),
19694
+ 'fs_action' => ( $this->get_unique_affix() . '_reconnect' ),
19695
+ );
19696
+
19697
+ $url = $this->get_activation_url( $params );
19698
+ }
19699
+
19700
+ if ( $this->is_plugin() && self::is_plugins_page() ) {
19701
+ $this->add_plugin_action_link(
19702
+ $link_text_id,
19703
+ $url,
19704
+ false,
19705
+ 13,
19706
+ "opt-in-or-opt-out {$this->_slug}"
19707
+ );
19708
+ }
19709
+ }
19710
+
19711
+ /**
19712
+ * Get the URL of the page that should be loaded right after the plugin activation.
19713
+ *
19714
+ * @author Vova Feldman (@svovaf)
19715
+ * @since 1.1.7.4
19716
+ *
19717
+ * @return string
19718
+ */
19719
+ function get_after_plugin_activation_redirect_url() {
19720
+ $url = false;
19721
+
19722
+ if ( ! $this->is_addon() || ! $this->has_free_plan() ) {
19723
+ $first_time_path = $this->_menu->get_first_time_path();
19724
+
19725
+ if ( $this->is_activation_mode() ) {
19726
+ $url = $this->get_activation_url();
19727
+ } else if ( ! empty( $first_time_path ) ) {
19728
+ $url = $first_time_path;
19729
+ } else {
19730
+ $page = '';
19731
+ if ( ! empty( $this->_dynamically_added_top_level_page_hook_name ) ) {
19732
+ if ( $this->is_network_registered() ) {
19733
+ $page = 'account';
19734
+ } else if ( $this->is_network_anonymous() ) {
19735
+ $this->maybe_set_slug_and_network_menu_exists_flag();
19736
+ }
19737
+ }
19738
+
19739
+ $url = $this->_get_admin_page_url( $page );
19740
+ }
19741
+ } else {
19742
+ $plugin_fs = false;
19743
+
19744
+ if ( $this->is_parent_plugin_installed() ) {
19745
+ $plugin_fs = self::get_parent_instance();
19746
+ }
19747
+
19748
+ if ( is_object( $plugin_fs ) ) {
19749
+ if ( ! $plugin_fs->is_registered() ) {
19750
+ // Forward to parent plugin connect when parent not registered.
19751
+ $url = $plugin_fs->get_activation_url();
19752
+ } else {
19753
+ // Forward to account page.
19754
+ $url = $plugin_fs->_get_admin_page_url( 'account' );
19755
+ }
19756
+ }
19757
+ }
19758
+
19759
+ return $url;
19760
+ }
19761
+
19762
+ /**
19763
+ * Forward page to activation page.
19764
+ *
19765
+ * @author Vova Feldman (@svovaf)
19766
+ * @since 1.0.3
19767
+ */
19768
+ function _redirect_on_activation_hook() {
19769
+ $url = $this->get_after_plugin_activation_redirect_url();
19770
+
19771
+ if ( is_string( $url ) ) {
19772
+ fs_redirect( $url );
19773
+ }
19774
+ }
19775
+
19776
+ /**
19777
+ * Modify plugin's page action links collection.
19778
+ *
19779
+ * @author Vova Feldman (@svovaf)
19780
+ * @since 1.0.0
19781
+ *
19782
+ * @param array $links
19783
+ * @param $file
19784
+ *
19785
+ * @return array
19786
+ */
19787
+ function _modify_plugin_action_links_hook( $links, $file ) {
19788
+ $this->_logger->entrance();
19789
+
19790
+ $passed_deactivate = false;
19791
+ $deactivate_link = '';
19792
+ $before_deactivate = array();
19793
+ $after_deactivate = array();
19794
+ foreach ( $links as $key => $link ) {
19795
+ if ( 'deactivate' === $key ) {
19796
+ $deactivate_link = $link;
19797
+ $passed_deactivate = true;
19798
+ continue;
19799
+ }
19800
+
19801
+ if ( ! $passed_deactivate ) {
19802
+ $before_deactivate[ $key ] = $link;
19803
+ } else {
19804
+ $after_deactivate[ $key ] = $link;
19805
+ }
19806
+ }
19807
+
19808
+ ksort( $this->_action_links );
19809
+
19810
+ foreach ( $this->_action_links as $new_links ) {
19811
+ foreach ( $new_links as $link ) {
19812
+ $before_deactivate[ $link['key'] ] = '<a href="' . $link['href'] . '"' . ( $link['external'] ? ' target="_blank"' : '' ) . '>' . $link['label'] . '</a>';
19813
+ }
19814
+ }
19815
+
19816
+ if ( ! empty( $deactivate_link ) ) {
19817
+ /**
19818
+ * This HTML element is used to identify the correct plugin when attaching an event to its Deactivate link.
19819
+ *
19820
+ * @since 1.2.1.6 Always show the deactivation feedback form since we added automatic free version deactivation upon premium code activation.
19821
+ */
19822
+ $deactivate_link .= '<i class="fs-module-id" data-module-id="' . $this->_module_id . '"></i>';
19823
+
19824
+ // Append deactivation link.
19825
+ $before_deactivate['deactivate'] = $deactivate_link;
19826
+ }
19827
+
19828
+ return array_merge( $before_deactivate, $after_deactivate );
19829
+ }
19830
+
19831
+ /**
19832
+ * Adds admin message.
19833
+ *
19834
+ * @author Vova Feldman (@svovaf)
19835
+ * @since 1.0.4
19836
+ *
19837
+ * @param string $message
19838
+ * @param string $title
19839
+ * @param string $type
19840
+ */
19841
+ function add_admin_message( $message, $title = '', $type = 'success' ) {
19842
+ $this->_admin_notices->add( $message, $title, $type );
19843
+ }
19844
+
19845
+ /**
19846
+ * Adds sticky admin message.
19847
+ *
19848
+ * @author Vova Feldman (@svovaf)
19849
+ * @since 1.1.0
19850
+ *
19851
+ * @param string $message
19852
+ * @param string $id
19853
+ * @param string $title
19854
+ * @param string $type
19855
+ */
19856
+ function add_sticky_admin_message( $message, $id, $title = '', $type = 'success' ) {
19857
+ $this->_admin_notices->add_sticky( $message, $id, $title, $type );
19858
+ }
19859
+
19860
+ /**
19861
+ * Check if the paid version of the module is installed.
19862
+ *
19863
+ * @author Vova Feldman (@svovaf)
19864
+ * @since 2.2.0
19865
+ *
19866
+ * @return bool
19867
+ */
19868
+ private function is_premium_version_installed() {
19869
+ $premium_plugin_basename = $this->premium_plugin_basename();
19870
+ $premium_plugin = get_plugins( '/' . dirname( $premium_plugin_basename ) );
19871
+
19872
+ return ! empty( $premium_plugin );
19873
+ }
19874
+
19875
+ /**
19876
+ * Helper function that returns the final steps for the upgrade completion.
19877
+ *
19878
+ * If the module is already running the premium code, returns an empty string.
19879
+ *
19880
+ * @author Vova Feldman (@svovaf)
19881
+ * @since 1.2.1
19882
+ *
19883
+ * @param string $plan_title
19884
+ *
19885
+ * @return string
19886
+ */
19887
+ private function get_complete_upgrade_instructions( $plan_title = '' ) {
19888
+ $this->_logger->entrance();
19889
+
19890
+ $activate_license_string = $this->get_license_network_activation_notice();
19891
+
19892
+ if ( ! $this->has_premium_version() || $this->is_premium() ) {
19893
+ return '' . $activate_license_string;
19894
+ }
19895
+
19896
+ if ( empty( $plan_title ) ) {
19897
+ $plan_title = $this->get_plan_title();
19898
+ }
19899
+
19900
+ if ( $this->is_premium_version_installed() ) {
19901
+ /**
19902
+ * If the premium version is already installed, instead of showing the installation instructions,
19903
+ * tell the current user to activate it.
19904
+ *
19905
+ * @author Leo Fajardo (@leorw)
19906
+ * @since 2.2.1
19907
+ */
19908
+ $premium_plugin_basename = $this->premium_plugin_basename();
19909
+
19910
+ return sprintf(
19911
+ /* translators: %1s: Product title; %2s: Plan title */
19912
+ $this->get_text_inline( ' The paid version of %1s is already installed. Please activate it to start benefiting the %2s features. %3s', 'activate-premium-version' ),
19913
+ sprintf( '<em>%s</em>', esc_html( $this->get_plugin_title() ) ),
19914
+ $plan_title,
19915
+ sprintf(
19916
+ '<a style="margin-left: 10px;" href="%s"><button class="button button-primary">%s</button></a>',
19917
+ wp_nonce_url( 'plugins.php?action=activate&amp;plugin=' . $premium_plugin_basename, 'activate-plugin_' . $premium_plugin_basename ),
19918
+ esc_html( sprintf(
19919
+ /* translators: %s: Plan title */
19920
+ $this->get_text_inline( 'Activate %s features', 'activate-x-features' ),
19921
+ $plan_title
19922
+ ) )
19923
+ )
19924
+ );
19925
+ } else {
19926
+ // @since 1.2.1.5 The free version is auto deactivated.
19927
+ $deactivation_step = version_compare( $this->version, '1.2.1.5', '<' ) ?
19928
+ ( '<li>' . $this->esc_html_inline( 'Deactivate the free version', 'deactivate-free-version' ) . '.</li>' ) :
19929
+ '';
19930
+
19931
+ return sprintf(
19932
+ ' %s: <ol><li>%s.</li>%s<li>%s (<a href="%s" target="_blank">%s</a>).</li></ol>',
19933
+ $this->get_text_inline( 'Please follow these steps to complete the upgrade', 'follow-steps-to-complete-upgrade' ),
19934
+ ( empty( $activate_license_string ) ? '' : $activate_license_string . '</li><li>' ) .
19935
+ $this->get_latest_download_link( sprintf(
19936
+ /* translators: %s: Plan title */
19937
+ $this->get_text_inline( 'Download the latest %s version', 'download-latest-x-version' ),
19938
+ $plan_title
19939
+ ) ),
19940
+ $deactivation_step,
19941
+ $this->get_text_inline( 'Upload and activate the downloaded version', 'upload-and-activate' ),
19942
+ '//bit.ly/upload-wp-' . $this->_module_type . 's',
19943
+ $this->get_text_inline( 'How to upload and activate?', 'howto-upload-activate' )
19944
+ );
19945
+ }
19946
+ }
19947
+
19948
+ /**
19949
+ * @author Leo Fajardo (@leorw)
19950
+ * @since 2.1.0
19951
+ *
19952
+ * @param string $url
19953
+ * @param array $request
19954
+ */
19955
+ private static function enrich_request_for_debug( &$url, &$request ) {
19956
+ if ( WP_FS__DEBUG_SDK || isset( $_COOKIE['XDEBUG_SESSION'] ) ) {
19957
+ $url = add_query_arg( 'XDEBUG_SESSION_START', rand( 0, 9999999 ), $url );
19958
+ $url = add_query_arg( 'XDEBUG_SESSION', 'PHPSTORM', $url );
19959
+
19960
+ $request['cookies'] = array(
19961
+ new WP_Http_Cookie( array(
19962
+ 'name' => 'XDEBUG_SESSION',
19963
+ 'value' => 'PHPSTORM',
19964
+ ) )
19965
+ );
19966
+ }
19967
+ }
19968
+
19969
+ /**
19970
+ * @author Leo Fajardo (@leorw)
19971
+ * @since 2.1.0
19972
+ *
19973
+ * @param string $url
19974
+ * @param array $request
19975
+ * @param int $success_cache_expiration
19976
+ * @param int $failure_cache_expiration
19977
+ * @param bool $maybe_enrich_request_for_debug
19978
+ *
19979
+ * @return WP_Error|array
19980
+ */
19981
+ static function safe_remote_post(
19982
+ &$url,
19983
+ $request,
19984
+ $success_cache_expiration = 0,
19985
+ $failure_cache_expiration = 0,
19986
+ $maybe_enrich_request_for_debug = true
19987
+ ) {
19988
+ $should_cache = ($success_cache_expiration + $failure_cache_expiration > 0);
19989
+
19990
+ $cache_key = $should_cache ? md5( fs_strip_url_protocol($url) . json_encode( $request ) ) : false;
19991
+
19992
+ $response = (!WP_FS__DEBUG_SDK && ( false !== $cache_key )) ?
19993
+ get_transient( $cache_key ) :
19994
+ false;
19995
+
19996
+ if ( false === $response ) {
19997
+ if ( $maybe_enrich_request_for_debug ) {
19998
+ self::enrich_request_for_debug( $url, $request );
19999
+ }
20000
+
20001
+ $response = wp_remote_post( $url, $request );
20002
+
20003
+ if ( $response instanceof WP_Error ) {
20004
+ if ( 'https://' === substr( $url, 0, 8 ) &&
20005
+ isset( $response->errors ) &&
20006
+ isset( $response->errors['http_request_failed'] )
20007
+ ) {
20008
+ $http_error = strtolower( $response->errors['http_request_failed'][0] );
20009
+
20010
+ if ( false !== strpos( $http_error, 'ssl' ) ||
20011
+ false !== strpos( $http_error, 'curl error 35' )
20012
+ ) {
20013
+ // Failed due to old version of cURL or Open SSL (SSLv3 is not supported by CloudFlare).
20014
+ $url = 'http://' . substr( $url, 8 );
20015
+
20016
+ $request['timeout'] = 15;
20017
+ $response = wp_remote_post( $url, $request );
20018
+ }
20019
+ }
20020
+ }
20021
+
20022
+ if ( false !== $cache_key ) {
20023
+ set_transient(
20024
+ $cache_key,
20025
+ $response,
20026
+ ( ( $response instanceof WP_Error ) ?
20027
+ $failure_cache_expiration :
20028
+ $success_cache_expiration )
20029
+ );
20030
+ }
20031
+ }
20032
+
20033
+ return $response;
20034
+ }
20035
+
20036
+ /**
20037
+ * This method is used to enrich the after upgrade notice instructions when the upgraded
20038
+ * license cannot be activated network wide (license quota isn't large enough).
20039
+ *
20040
+ * @author Vova Feldman (@svovaf)
20041
+ * @since 2.0.0
20042
+ *
20043
+ * @return string
20044
+ */
20045
+ private function get_license_network_activation_notice() {
20046
+ if ( ! $this->_is_network_active ) {
20047
+ // Module isn't network level activated.
20048
+ return '';
20049
+ }
20050
+
20051
+ if ( ! fs_is_network_admin() ) {
20052
+ // Not network level admin.
20053
+ return '';
20054
+ }
20055
+
20056
+ if ( get_blog_count() == 1 ) {
20057
+ // There's only a single site in the network so if there's a context license it was already activated.
20058
+ return '';
20059
+ }
20060
+
20061
+ if ( ! is_object( $this->_license ) ) {
20062
+ // No context license.
20063
+ return '';
20064
+ }
20065
+
20066
+ if ( $this->_license->is_single_site() && 0 < $this->_license->activated ) {
20067
+ // 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).
20068
+ return '';
20069
+ }
20070
+
20071
+ if ( $this->can_activate_license_on_network( $this->_license ) ) {
20072
+ // 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).
20073
+ return '';
20074
+ }
20075
+
20076
+ return sprintf(
20077
+ $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' ),
20078
+ '<a href="' . $this->get_account_url( false, array( 'activate_license' => 'true' ) ) . '">',
20079
+ '</a>'
20080
+ );
20081
+ }
20082
+
20083
+ /**
20084
+ * @author Vova Feldman (@svovaf)
20085
+ * @since 1.2.1.7
20086
+ *
20087
+ * @param string $key
20088
+ *
20089
+ * @return string
20090
+ */
20091
+ function get_text( $key ) {
20092
+ return fs_text( $key, $this->_slug );
20093
+ }
20094
+
20095
+ /**
20096
+ * @author Vova Feldman (@svovaf)
20097
+ * @since 1.2.3
20098
+ *
20099
+ * @param string $text Translatable string.
20100
+ * @param string $key String key for overrides.
20101
+ *
20102
+ * @return string
20103
+ */
20104
+ function get_text_inline( $text, $key = '' ) {
20105
+ return _fs_text_inline( $text, $key, $this->_slug );
20106
+ }
20107
+
20108
+ /**
20109
+ * @author Vova Feldman (@svovaf)
20110
+ * @since 1.2.3
20111
+ *
20112
+ * @param string $text Translatable string.
20113
+ * @param string $context Context information for the translators.
20114
+ * @param string $key String key for overrides.
20115
+ *
20116
+ * @return string
20117
+ */
20118
+ function get_text_x_inline( $text, $context, $key ) {
20119
+ return _fs_text_x_inline( $text, $context, $key, $this->_slug );
20120
+ }
20121
+
20122
+ /**
20123
+ * @author Vova Feldman (@svovaf)
20124
+ * @since 1.2.3
20125
+ *
20126
+ * @param string $text Translatable string.
20127
+ * @param string $key String key for overrides.
20128
+ *
20129
+ * @return string
20130
+ */
20131
+ function esc_html_inline( $text, $key ) {
20132
+ return esc_html( _fs_text_inline( $text, $key, $this->_slug ) );
20133
+ }
20134
+
20135
+ #----------------------------------------------------------------------------------
20136
+ #region Versioning
20137
+ #----------------------------------------------------------------------------------
20138
+
20139
+ /**
20140
+ * Check if Freemius in SDK upgrade mode.
20141
+ *
20142
+ * @author Vova Feldman (@svovaf)
20143
+ * @since 1.0.9
20144
+ *
20145
+ * @return bool
20146
+ */
20147
+ function is_sdk_upgrade_mode() {
20148
+ return isset( $this->_storage->sdk_upgrade_mode ) ?
20149
+ $this->_storage->sdk_upgrade_mode :
20150
+ false;
20151
+ }
20152
+
20153
+ /**
20154
+ * Turn SDK upgrade mode off.
20155
+ *
20156
+ * @author Vova Feldman (@svovaf)
20157
+ * @since 1.0.9
20158
+ */
20159
+ function set_sdk_upgrade_complete() {
20160
+ $this->_storage->sdk_upgrade_mode = false;
20161
+ }
20162
+
20163
+ /**
20164
+ * Check if plugin upgrade mode.
20165
+ *
20166
+ * @author Vova Feldman (@svovaf)
20167
+ * @since 1.0.9
20168
+ *
20169
+ * @return bool
20170
+ */
20171
+ function is_plugin_upgrade_mode() {
20172
+ return isset( $this->_storage->plugin_upgrade_mode ) ?
20173
+ $this->_storage->plugin_upgrade_mode :
20174
+ false;
20175
+ }
20176
+
20177
+ /**
20178
+ * Turn plugin upgrade mode off.
20179
+ *
20180
+ * @author Vova Feldman (@svovaf)
20181
+ * @since 1.0.9
20182
+ */
20183
+ function set_plugin_upgrade_complete() {
20184
+ $this->_storage->plugin_upgrade_mode = false;
20185
+ }
20186
+
20187
+ #endregion
20188
+
20189
+ #----------------------------------------------------------------------------------
20190
+ #region Permissions
20191
+ #----------------------------------------------------------------------------------
20192
+
20193
+ /**
20194
+ * Check if specific permission requested.
20195
+ *
20196
+ * @author Vova Feldman (@svovaf)
20197
+ * @since 1.1.6
20198
+ *
20199
+ * @param string $permission
20200
+ *
20201
+ * @return bool
20202
+ */
20203
+ function is_permission_requested( $permission ) {
20204
+ return isset( $this->_permissions[ $permission ] ) && ( true === $this->_permissions[ $permission ] );
20205
+ }
20206
+
20207
+ #endregion
20208
+
20209
+ #----------------------------------------------------------------------------------
20210
+ #region Auto Activation
20211
+ #----------------------------------------------------------------------------------
20212
+
20213
+ /**
20214
+ * Hints the SDK if running an auto-installation.
20215
+ *
20216
+ * @var bool
20217
+ */
20218
+ private $_isAutoInstall = false;
20219
+
20220
+ /**
20221
+ * After upgrade callback to install and auto activate a plugin.
20222
+ * This code will only be executed on explicit request from the user,
20223
+ * following the practice Jetpack are using with their theme installations.
20224
+ *
20225
+ * @link https://make.wordpress.org/plugins/2017/03/16/clarification-of-guideline-8-executable-code-and-installs/
20226
+ *
20227
+ * @author Vova Feldman (@svovaf)
20228
+ * @since 1.2.1.7
20229
+ */
20230
+ function _install_premium_version_ajax_action() {
20231
+ $this->_logger->entrance();
20232
+
20233
+ $this->check_ajax_referer( 'install_premium_version' );
20234
+
20235
+ if ( ! $this->is_registered() ) {
20236
+ // Not registered.
20237
+ self::shoot_ajax_failure( array(
20238
+ 'message' => $this->get_text_inline( 'Auto installation only works for opted-in users.', 'auto-install-error-not-opted-in' ),
20239
+ 'code' => 'premium_installed',
20240
+ ) );
20241
+ }
20242
+
20243
+ $plugin_id = fs_request_get( 'target_module_id', $this->get_id() );
20244
+
20245
+ if ( ! FS_Plugin::is_valid_id( $plugin_id ) ) {
20246
+ // Invalid ID.
20247
+ self::shoot_ajax_failure( array(
20248
+ 'message' => $this->get_text_inline( 'Invalid module ID.', 'auto-install-error-invalid-id' ),
20249
+ 'code' => 'invalid_module_id',
20250
+ ) );
20251
+ }
20252
+
20253
+ if ( $plugin_id == $this->get_id() ) {
20254
+ if ( $this->is_premium() ) {
20255
+ // Already using the premium code version.
20256
+ self::shoot_ajax_failure( array(
20257
+ 'message' => $this->get_text_inline( 'Premium version already active.', 'auto-install-error-premium-activated' ),
20258
+ 'code' => 'premium_installed',
20259
+ ) );
20260
+ }
20261
+ if ( ! $this->can_use_premium_code() ) {
20262
+ // Don't have access to the premium code.
20263
+ self::shoot_ajax_failure( array(
20264
+ 'message' => $this->get_text_inline( 'You do not have a valid license to access the premium version.', 'auto-install-error-invalid-license' ),
20265
+ 'code' => 'invalid_license',
20266
+ ) );
20267
+ }
20268
+ if ( ! $this->has_release_on_freemius() ) {
20269
+ // Plugin is a serviceware, no premium code version.
20270
+ self::shoot_ajax_failure( array(
20271
+ 'message' => $this->get_text_inline( 'Plugin is a "Serviceware" which means it does not have a premium code version.', 'auto-install-error-serviceware' ),
20272
+ 'code' => 'premium_version_missing',
20273
+ ) );
20274
+ }
20275
+ } else {
20276
+ $addon = $this->get_addon( $plugin_id );
20277
+
20278
+ if ( ! is_object( $addon ) ) {
20279
+ // Invalid add-on ID.
20280
+ self::shoot_ajax_failure( array(
20281
+ 'message' => $this->get_text_inline( 'Invalid module ID.', 'auto-install-error-invalid-id' ),
20282
+ 'code' => 'invalid_module_id',
20283
+ ) );
20284
+ }
20285
+
20286
+ if ( $this->is_addon_activated( $plugin_id, true ) ) {
20287
+ // Premium add-on version is already activated.
20288
+ self::shoot_ajax_failure( array(
20289
+ 'message' => $this->get_text_inline( 'Premium add-on version already installed.', 'auto-install-error-premium-addon-activated' ),
20290
+ 'code' => 'premium_installed',
20291
+ ) );
20292
+ }
20293
+ }
20294
+
20295
+ $this->_isAutoInstall = true;
20296
+
20297
+ // Try to install and activate.
20298
+ $updater = FS_Plugin_Updater::instance( $this );
20299
+ $result = $updater->install_and_activate_plugin( $plugin_id );
20300
+
20301
+ if ( is_array( $result ) && ! empty( $result['message'] ) ) {
20302
+ self::shoot_ajax_failure( array(
20303
+ 'message' => $result['message'],
20304
+ 'code' => $result['code'],
20305
+ ) );
20306
+ }
20307
+
20308
+ self::shoot_ajax_success( $result );
20309
+ }
20310
+
20311
+ /**
20312
+ * Displays module activation dialog box after a successful upgrade
20313
+ * where the user explicitly requested to auto download and install
20314
+ * the premium version.
20315
+ *
20316
+ * @author Vova Feldman (@svovaf)
20317
+ * @since 1.2.1.7
20318
+ */
20319
+ function _add_auto_installation_dialog_box() {
20320
+ $this->_logger->entrance();
20321
+
20322
+ if ( ! $this->is_registered() ) {
20323
+ // Not registered.
20324
+ return;
20325
+ }
20326
+
20327
+ $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
20328
+
20329
+ if ( ! FS_Plugin::is_valid_id( $plugin_id ) ) {
20330
+ // Invalid module ID.
20331
+ return;
20332
+ }
20333
+
20334
+ if ( $plugin_id == $this->get_id() ) {
20335
+ if ( $this->is_premium() ) {
20336
+ // Already using the premium code version.
20337
+ return;
20338
+ }
20339
+ if ( ! $this->can_use_premium_code() ) {
20340
+ // Don't have access to the premium code.
20341
+ return;
20342
+ }
20343
+ if ( ! $this->has_release_on_freemius() ) {
20344
+ // Plugin is a serviceware, no premium code version.
20345
+ return;
20346
+ }
20347
+ } else {
20348
+ $addon = $this->get_addon( $plugin_id );
20349
+
20350
+ if ( ! is_object( $addon ) ) {
20351
+ // Invalid add-on ID.
20352
+ return;
20353
+ }
20354
+
20355
+ if ( $this->is_addon_activated( $plugin_id, true ) ) {
20356
+ // Premium add-on version is already activated.
20357
+ return;
20358
+ }
20359
+ }
20360
+
20361
+ $vars = array(
20362
+ 'id' => $this->_module_id,
20363
+ 'target_module_id' => $plugin_id,
20364
+ 'slug' => $this->_slug,
20365
+ );
20366
+
20367
+ fs_require_template( 'auto-installation.php', $vars );
20368
+ }
20369
+
20370
+ #endregion
20371
+
20372
+ #--------------------------------------------------------------------------------
20373
+ #region Tabs Integration
20374
+ #--------------------------------------------------------------------------------
20375
+
20376
+ #region Module's Original Tabs
20377
+
20378
+ /**
20379
+ * Inject a JavaScript logic to capture the theme tabs HTML.
20380
+ *
20381
+ * @author Vova Feldman (@svovaf)
20382
+ * @since 1.2.2.7
20383
+ */
20384
+ function _tabs_capture() {
20385
+ $this->_logger->entrance();
20386
+
20387
+ if ( ! $this->is_theme_settings_page() ||
20388
+ ! $this->is_matching_url( $this->main_menu_url() )
20389
+ ) {
20390
+ return;
20391
+ }
20392
+
20393
+ $params = array(
20394
+ 'id' => $this->_module_id,
20395
+ );
20396
+
20397
+ fs_require_once_template( 'tabs-capture-js.php', $params );
20398
+ }
20399
+
20400
+ /**
20401
+ * Cache theme's tabs HTML for a week. The cache will also be set as expired
20402
+ * after version and type (free/premium) changes, in addition to the week period.
20403
+ *
20404
+ * @author Vova Feldman (@svovaf)
20405
+ * @since 1.2.2.7
20406
+ */
20407
+ function _store_tabs_ajax_action() {
20408
+ $this->_logger->entrance();
20409
+
20410
+ $this->check_ajax_referer( 'store_tabs' );
20411
+
20412
+ // Init filesystem if not yet initiated.
20413
+ WP_Filesystem();
20414
+
20415
+ // Get POST body HTML data.
20416
+ global $wp_filesystem;
20417
+ $tabs_html = $wp_filesystem->get_contents( "php://input" );
20418
+
20419
+ if ( is_string( $tabs_html ) ) {
20420
+ $tabs_html = trim( $tabs_html );
20421
+ }
20422
+
20423
+ if ( ! is_string( $tabs_html ) || empty( $tabs_html ) ) {
20424
+ self::shoot_ajax_failure();
20425
+ }
20426
+
20427
+ $this->_cache->set( 'tabs', $tabs_html, 7 * WP_FS__TIME_24_HOURS_IN_SEC );
20428
+
20429
+ self::shoot_ajax_success();
20430
+ }
20431
+
20432
+ /**
20433
+ * Cache theme's settings page custom styles. The cache will also be set as expired
20434
+ * after version and type (free/premium) changes, in addition to the week period.
20435
+ *
20436
+ * @author Vova Feldman (@svovaf)
20437
+ * @since 1.2.2.7
20438
+ */
20439
+ function _store_tabs_styles() {
20440
+ $this->_logger->entrance();
20441
+
20442
+ if ( ! $this->is_theme_settings_page() ||
20443
+ ! $this->is_matching_url( $this->main_menu_url() )
20444
+ ) {
20445
+ return;
20446
+ }
20447
+
20448
+ $wp_styles = wp_styles();
20449
+
20450
+ $theme_styles_url = get_template_directory_uri();
20451
+
20452
+ $stylesheets = array();
20453
+ foreach ( $wp_styles->queue as $handler ) {
20454
+ if ( fs_starts_with( $handler, 'fs_' ) ) {
20455
+ // Assume that stylesheets that their handler starts with "fs_" belong to the SDK.
20456
+ continue;
20457
+ }
20458
+
20459
+ /**
20460
+ * @var _WP_Dependency $stylesheet
20461
+ */
20462
+ $stylesheet = $wp_styles->registered[ $handler ];
20463
+
20464
+ if ( fs_starts_with( $stylesheet->src, $theme_styles_url ) ) {
20465
+ $stylesheets[] = $stylesheet->src;
20466
+ }
20467
+ }
20468
+
20469
+ if ( ! empty( $stylesheets ) ) {
20470
+ $this->_cache->set( 'tabs_stylesheets', $stylesheets, 7 * WP_FS__TIME_24_HOURS_IN_SEC );
20471
+ }
20472
+ }
20473
+
20474
+ /**
20475
+ * Check if module's original settings page has any tabs.
20476
+ *
20477
+ * @author Vova Feldman (@svovaf)
20478
+ * @since 1.2.2.7
20479
+ *
20480
+ * @return bool
20481
+ */
20482
+ private function has_tabs() {
20483
+ return $this->_cache->has( 'tabs' );
20484
+ }
20485
+
20486
+ /**
20487
+ * Get module's settings page HTML content, starting
20488
+ * from the beginning of the <div class="wrap"> element,
20489
+ * until the tabs HTML (including).
20490
+ *
20491
+ * @author Vova Feldman (@svovaf)
20492
+ * @since 1.2.2.7
20493
+ *
20494
+ * @return string
20495
+ */
20496
+ private function get_tabs_html() {
20497
+ $this->_logger->entrance();
20498
+
20499
+ return $this->_cache->get( 'tabs' );
20500
+ }
20501
+
20502
+ /**
20503
+ * Check if page should include tabs.
20504
+ *
20505
+ * @author Vova Feldman (@svovaf)
20506
+ * @since 1.2.2.7
20507
+ *
20508
+ * @return bool
20509
+ */
20510
+ private function should_page_include_tabs() {
20511
+ if ( ! $this->has_settings_menu() ) {
20512
+ // Don't add tabs if no settings at all.
20513
+ return false;
20514
+ }
20515
+
20516
+ if ( ! $this->is_theme() ) {
20517
+ // Only add tabs to themes for now.
20518
+ return false;
20519
+ }
20520
+
20521
+ if ( ! $this->has_paid_plan() && ! $this->has_addons() ) {
20522
+ // Only add tabs to monetizing themes.
20523
+ return false;
20524
+ }
20525
+
20526
+ if ( ! $this->is_theme_settings_page() ) {
20527
+ // Only add tabs if browsing one of the theme's setting pages.
20528
+ return false;
20529
+ }
20530
+
20531
+ if ( $this->is_admin_page( 'pricing' ) && fs_request_get_bool( 'checkout' ) ) {
20532
+ // Don't add tabs on checkout page, we want to reduce distractions
20533
+ // as much as possible.
20534
+ return false;
20535
+ }
20536
+
20537
+ return true;
20538
+ }
20539
+
20540
+ /**
20541
+ * Add the tabs HTML before the setting's page content and
20542
+ * enqueue any required stylesheets.
20543
+ *
20544
+ * @author Vova Feldman (@svovaf)
20545
+ * @since 1.2.2.7
20546
+ *
20547
+ * @return bool If tabs were included.
20548
+ */
20549
+ function _add_tabs_before_content() {
20550
+ $this->_logger->entrance();
20551
+
20552
+ if ( ! $this->should_page_include_tabs() ) {
20553
+ return false;
20554
+ }
20555
+
20556
+ /**
20557
+ * Enqueue the original stylesheets that are included in the
20558
+ * theme settings page. That way, if the theme settings has
20559
+ * some custom _styled_ content above the tabs UI, this
20560
+ * will make sure that the styling is preserved.
20561
+ */
20562
+ $stylesheets = $this->_cache->get( 'tabs_stylesheets', array() );
20563
+ if ( is_array( $stylesheets ) ) {
20564
+ for ( $i = 0, $len = count( $stylesheets ); $i < $len; $i ++ ) {
20565
+ wp_enqueue_style( "fs_{$this->_module_id}_tabs_{$i}", $stylesheets[ $i ] );
20566
+ }
20567
+ }
20568
+
20569
+ // Cut closing </div> tag.
20570
+ echo substr( trim( $this->get_tabs_html() ), 0, - 6 );
20571
+
20572
+ return true;
20573
+ }
20574
+
20575
+ /**
20576
+ * Add the tabs closing HTML after the setting's page content.
20577
+ *
20578
+ * @author Vova Feldman (@svovaf)
20579
+ * @since 1.2.2.7
20580
+ *
20581
+ * @return bool If tabs closing HTML was included.
20582
+ */
20583
+ function _add_tabs_after_content() {
20584
+ $this->_logger->entrance();
20585
+
20586
+ if ( ! $this->should_page_include_tabs() ) {
20587
+ return false;
20588
+ }
20589
+
20590
+ echo '</div>';
20591
+
20592
+ return true;
20593
+ }
20594
+
20595
+ #endregion
20596
+
20597
+ /**
20598
+ * Add in-page JavaScript to inject the Freemius tabs into
20599
+ * the module's setting tabs section.
20600
+ *
20601
+ * @author Vova Feldman (@svovaf)
20602
+ * @since 1.2.2.7
20603
+ */
20604
+ function _add_freemius_tabs() {
20605
+ $this->_logger->entrance();
20606
+
20607
+ if ( ! $this->should_page_include_tabs() ) {
20608
+ return;
20609
+ }
20610
+
20611
+ $params = array( 'id' => $this->_module_id );
20612
+ fs_require_once_template( 'tabs.php', $params );
20613
+ }
20614
+
20615
+ #endregion
20616
+
20617
+ #--------------------------------------------------------------------------------
20618
+ #region Customizer Integration for Themes
20619
+ #--------------------------------------------------------------------------------
20620
+
20621
+ /**
20622
+ * @author Vova Feldman (@svovaf)
20623
+ * @since 1.2.2.7
20624
+ *
20625
+ * @param WP_Customize_Manager $customizer
20626
+ */
20627
+ function _customizer_register( $customizer ) {
20628
+ $this->_logger->entrance();
20629
+
20630
+ if ( $this->is_pricing_page_visible() ) {
20631
+ require_once WP_FS__DIR_INCLUDES . '/customizer/class-fs-customizer-upsell-control.php';
20632
+
20633
+ $customizer->add_section( 'freemius_upsell', array(
20634
+ 'title' => '&#9733; ' . $this->get_text_inline( 'View paid features', 'view-paid-features' ),
20635
+ 'priority' => 1,
20636
+ ) );
20637
+ $customizer->add_setting( 'freemius_upsell', array(
20638
+ 'sanitize_callback' => 'esc_html',
20639
+ ) );
20640
+
20641
+ $customizer->add_control( new FS_Customizer_Upsell_Control( $customizer, 'freemius_upsell', array(
20642
+ 'fs' => $this,
20643
+ 'section' => 'freemius_upsell',
20644
+ 'priority' => 100,
20645
+ ) ) );
20646
+ }
20647
+
20648
+ if ( $this->is_page_visible( 'contact' ) || $this->is_page_visible( 'support' ) ) {
20649
+ require_once WP_FS__DIR_INCLUDES . '/customizer/class-fs-customizer-support-section.php';
20650
+
20651
+ // Main Documentation Link In Customizer Root.
20652
+ $customizer->add_section( new FS_Customizer_Support_Section( $customizer, 'freemius_support', array(
20653
+ 'fs' => $this,
20654
+ 'priority' => 1000,
20655
+ ) ) );
20656
+ }
20657
+ }
20658
+
20659
+ #endregion
20660
+
20661
+ /**
20662
+ * If the theme has a paid version, add some custom
20663
+ * styling to the theme's premium version (if exists)
20664
+ * to highlight that it's the premium version of the
20665
+ * same theme, making it easier for identification
20666
+ * after the user upgrades and upload it to the site.
20667
+ *
20668
+ * @author Vova Feldman (@svovaf)
20669
+ * @since 1.2.2.7
20670
+ */
20671
+ function _style_premium_theme() {
20672
+ $this->_logger->entrance();
20673
+
20674
+ if ( ! self::is_themes_page() ) {
20675
+ // Only include in the themes page.
20676
+ return;
20677
+ }
20678
+
20679
+ if ( ! $this->has_paid_plan() ) {
20680
+ // Only include if has any paid plans.
20681
+ return;
20682
+ }
20683
+
20684
+ $params = null;
20685
+ fs_require_once_template( '/js/jquery.content-change.php', $params );
20686
+
20687
+ $params = array(
20688
+ 'slug' => $this->_slug,
20689
+ 'id' => $this->_module_id,
20690
+ );
20691
+
20692
+ fs_require_template( '/js/style-premium-theme.php', $params );
20693
+ }
20694
+
20695
+ /**
20696
+ * This method will return the absolute URL of the module's local icon.
20697
+ *
20698
+ * When you are running your plugin or theme on a **localhost** environment, if the icon
20699
+ * is not found in the local assets folder, try to fetch the icon URL from Freemius. If not set and
20700
+ * it's a plugin hosted on WordPress.org, try fetching the icon URL from wordpress.org.
20701
+ * If an icon is found, this method will automatically attempt to download the icon and store it
20702
+ * in /freemius/assets/img/{slug}.{png|jpg|gif|svg}.
20703
+ *
20704
+ * It's important to mention that this method is NOT phoning home since the developer will deploy
20705
+ * the product with the local icon in the assets folder. The download process just simplifies
20706
+ * the process for the developer.
20707
+ *
20708
+ * @author Vova Feldman (@svovaf)
20709
+ * @since 2.0.0
20710
+ *
20711
+ * @return string
20712
+ */
20713
+ function get_local_icon_url() {
20714
+ global $fs_active_plugins;
20715
+
20716
+ /**
20717
+ * @since 1.1.7.5
20718
+ */
20719
+ $local_path = $this->apply_filters( 'plugin_icon', false );
20720
+
20721
+ if ( is_string( $local_path ) ) {
20722
+ $icons = array( $local_path );
20723
+ } else {
20724
+ $img_dir = WP_FS__DIR_IMG;
20725
+
20726
+ // Locate the main assets folder.
20727
+ if ( 1 < count( $fs_active_plugins->plugins ) ) {
20728
+ $plugin_or_theme_img_dir = ( $this->is_plugin() ? WP_PLUGIN_DIR : get_theme_root() );
20729
+
20730
+ foreach ( $fs_active_plugins->plugins as $sdk_path => &$data ) {
20731
+ if ( $data->plugin_path == $this->get_plugin_basename() ) {
20732
+ $img_dir = $plugin_or_theme_img_dir
20733
+ . '/'
20734
+ . str_replace( '../themes/', '', $sdk_path )
20735
+ . '/assets/img';
20736
+
20737
+ break;
20738
+ }
20739
+ }
20740
+ }
20741
+
20742
+ // Try to locate the icon in the assets folder.
20743
+ $icons = glob( fs_normalize_path( $img_dir . "/{$this->_slug}.*" ) );
20744
+
20745
+ if ( ! is_array( $icons ) || 0 === count( $icons ) ) {
20746
+ if ( ! WP_FS__IS_LOCALHOST && $this->is_theme() ) {
20747
+ $icons = array(
20748
+ fs_normalize_path( $img_dir . '/theme-icon.png' )
20749
+ );
20750
+ } else {
20751
+ $icon_found = false;
20752
+ $local_path = fs_normalize_path( "{$img_dir}/{$this->_slug}.png" );
20753
+
20754
+ if ( ! function_exists( 'get_filesystem_method' ) ) {
20755
+ require_once ABSPATH . 'wp-admin/includes/file.php';
20756
+ }
20757
+
20758
+ $have_write_permissions = ( 'direct' === get_filesystem_method( array(), fs_normalize_path( $img_dir ) ) );
20759
+
20760
+ /**
20761
+ * IMPORTANT: THIS CODE WILL NEVER RUN AFTER THE PLUGIN IS IN THE REPO.
20762
+ *
20763
+ * This code will only be executed once during the testing
20764
+ * of the plugin in a local environment. The plugin icon file WILL
20765
+ * already exist in the assets folder when the plugin is deployed to
20766
+ * the repository.
20767
+ */
20768
+ if ( WP_FS__IS_LOCALHOST && $have_write_permissions ) {
20769
+ // Fetch icon from Freemius.
20770
+ $icon = $this->fetch_remote_icon_url();
20771
+
20772
+ // Fetch icon from WordPress.org.
20773
+ if ( empty( $icon ) && $this->is_plugin() && $this->is_org_repo_compliant() ) {
20774
+ if ( ! function_exists( 'plugins_api' ) ) {
20775
+ require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
20776
+ }
20777
+
20778
+ $plugin_information = plugins_api( 'plugin_information', array(
20779
+ 'slug' => $this->_slug,
20780
+ 'fields' => array(
20781
+ 'sections' => false,
20782
+ 'tags' => false,
20783
+ 'icons' => true
20784
+ )
20785
+ ) );
20786
+
20787
+ if (
20788
+ ! is_wp_error( $plugin_information )
20789
+ && isset( $plugin_information->icons )
20790
+ && ! empty( $plugin_information->icons )
20791
+ ) {
20792
+ /**
20793
+ * Get the smallest icon.
20794
+ *
20795
+ * @author Leo Fajardo (@leorw)
20796
+ * @since 1.2.2
20797
+ */
20798
+ $icon = end( $plugin_information->icons );
20799
+ }
20800
+ }
20801
+
20802
+ if ( ! empty( $icon ) ) {
20803
+ if ( 0 !== strpos( $icon, 'http' ) ) {
20804
+ $icon = 'http:' . $icon;
20805
+ }
20806
+
20807
+ /**
20808
+ * Get a clean file extension, e.g.: "jpg" and not "jpg?rev=1305765".
20809
+ *
20810
+ * @author Leo Fajardo (@leorw)
20811
+ * @since 1.2.2
20812
+ */
20813
+ $ext = pathinfo( strtok( $icon, '?' ), PATHINFO_EXTENSION );
20814
+
20815
+ $local_path = fs_normalize_path( "{$img_dir}/{$this->_slug}.{$ext}" );
20816
+
20817
+ // Try to download the icon.
20818
+ $icon_found = fs_download_image( $icon, $local_path );
20819
+ }
20820
+ }
20821
+
20822
+ if ( ! $icon_found ) {
20823
+ // No icons found, fallback to default icon.
20824
+ if ( $have_write_permissions ) {
20825
+ // If have write permissions, copy default icon.
20826
+ copy( fs_normalize_path( $img_dir . "/{$this->_module_type}-icon.png" ), $local_path );
20827
+ } else {
20828
+ // If doesn't have write permissions, use default icon path.
20829
+ $local_path = fs_normalize_path( $img_dir . "/{$this->_module_type}-icon.png" );
20830
+ }
20831
+ }
20832
+
20833
+ $icons = array( $local_path );
20834
+ }
20835
+ }
20836
+ }
20837
+
20838
+ $icon_dir = dirname( $icons[0] );
20839
+
20840
+ return fs_img_url( substr( $icons[0], strlen( $icon_dir ) ), $icon_dir );
20841
+ }
20842
+
20843
+ /**
20844
+ * Fetch module's extended info.
20845
+ *
20846
+ * @author Vova Feldman (@svovaf)
20847
+ * @since 2.0.0
20848
+ *
20849
+ * @return object|mixed
20850
+ */
20851
+ private function fetch_module_info() {
20852
+ return $this->get_api_plugin_scope()->get( 'info.json', false, WP_FS__TIME_WEEK_IN_SEC );
20853
+ }
20854
+
20855
+ /**
20856
+ * Fetch module's remote icon URL.
20857
+ *
20858
+ * @author Vova Feldman (@svovaf)
20859
+ * @since 2.0.0
20860
+ *
20861
+ * @return string
20862
+ */
20863
+ function fetch_remote_icon_url() {
20864
+ $info = $this->fetch_module_info();
20865
+
20866
+ return ( $this->is_api_result_object( $info, 'icon' ) && is_string( $info->icon ) ) ?
20867
+ $info->icon :
20868
+ '';
20869
+ }
20870
+
20871
+ #--------------------------------------------------------------------------------
20872
+ #region GDPR
20873
+ #--------------------------------------------------------------------------------
20874
+
20875
+ /**
20876
+ * @author Leo Fajardo (@leorw)
20877
+ * @since 2.1.0
20878
+ *
20879
+ * @return bool
20880
+ */
20881
+ function fetch_and_store_current_user_gdpr_anonymously() {
20882
+ $pong = $this->ping( null, true );
20883
+
20884
+ if ( ! $this->get_api_plugin_scope()->is_valid_ping( $pong ) ) {
20885
+ return false;
20886
+ } else {
20887
+ FS_GDPR_Manager::instance()->store_is_required( $pong->is_gdpr_required );
20888
+
20889
+ return $pong->is_gdpr_required;
20890
+ }
20891
+ }
20892
+
20893
+ /**
20894
+ * @author Leo Fajardo (@leorw)
20895
+ * @since 2.1.0
20896
+ *
20897
+ * @param array $user_plugins
20898
+ *
20899
+ * @return string
20900
+ */
20901
+ private function get_gdpr_admin_notice_string( $user_plugins ) {
20902
+ $this->_logger->entrance();
20903
+
20904
+ $addons = self::get_all_addons();
20905
+
20906
+ foreach ( $user_plugins as $user_plugin ) {
20907
+ $has_addons = isset( $addons[ $user_plugin->id ] );
20908
+
20909
+ if ( WP_FS__MODULE_TYPE_PLUGIN === $user_plugin->type && ! $has_addons ) {
20910
+ if ( $this->_module_id == $user_plugin->id ) {
20911
+ $addons = $this->get_addons();
20912
+ $has_addons = ( ! empty( $addons ) );
20913
+ } else {
20914
+ $plugin_api = FS_Api::instance(
20915
+ $user_plugin->id,
20916
+ 'plugin',
20917
+ $user_plugin->id,
20918
+ $user_plugin->public_key,
20919
+ ! $user_plugin->is_live
20920
+ );
20921
+
20922
+ $addons_result = $plugin_api->get( '/addons.json?enriched=true', true );
20923
+
20924
+ if ( $this->is_api_result_object( $addons_result, 'plugins' ) &&
20925
+ is_array( $addons_result->plugins ) &&
20926
+ ! empty( $addons_result->plugins )
20927
+ ) {
20928
+ $has_addons = true;
20929
+ }
20930
+ }
20931
+ }
20932
+
20933
+ $user_plugin->has_addons = $has_addons;
20934
+ }
20935
+
20936
+ $is_single_parent_product = ( 1 === count( $user_plugins ) );
20937
+
20938
+ $multiple_products_text = '';
20939
+
20940
+ if ( $is_single_parent_product ) {
20941
+ $single_parent_product = reset( $user_plugins );
20942
+
20943
+ $thank_you = sprintf(
20944
+ "<span data-plugin-id='%d'>%s</span>",
20945
+ $single_parent_product->id,
20946
+ sprintf(
20947
+ $single_parent_product->has_addons ?
20948
+ $this->get_text_inline( 'Thank you so much for using %s and its add-ons!', 'thank-you-for-using-product-and-its-addons' ) :
20949
+ $this->get_text_inline( 'Thank you so much for using %s!', 'thank-you-for-using-product' ),
20950
+ sprintf('<b><i>%s</i></b>', $single_parent_product->title)
20951
+ )
20952
+ );
20953
+
20954
+ $already_opted_in = sprintf(
20955
+ $this->get_text_inline( "You've already opted-in to our usage-tracking, which helps us keep improving the %s.", 'already-opted-in-to-product-usage-tracking' ),
20956
+ ( WP_FS__MODULE_TYPE_THEME === $single_parent_product->type ) ? WP_FS__MODULE_TYPE_THEME : WP_FS__MODULE_TYPE_PLUGIN
20957
+ );
20958
+ } else {
20959
+ $thank_you = $this->get_text_inline( 'Thank you so much for using our products!', 'thank-you-for-using-products' );
20960
+ $already_opted_in = $this->get_text_inline( "You've already opted-in to our usage-tracking, which helps us keep improving them.", 'already-opted-in-to-products-usage-tracking' );
20961
+
20962
+ $products_and_add_ons = '';
20963
+ foreach ( $user_plugins as $user_plugin ) {
20964
+ if ( ! empty( $products_and_add_ons ) ) {
20965
+ $products_and_add_ons .= ', ';
20966
+ }
20967
+
20968
+ if ( ! $user_plugin->has_addons ) {
20969
+ $products_and_add_ons .= sprintf(
20970
+ "<span data-plugin-id='%d'>%s</span>",
20971
+ $user_plugin->id,
20972
+ $user_plugin->title
20973
+ );
20974
+ } else {
20975
+ $products_and_add_ons .= sprintf(
20976
+ "<span data-plugin-id='%d'>%s</span>",
20977
+ $user_plugin->id,
20978
+ sprintf(
20979
+ $this->get_text_inline( '%s and its add-ons', 'product-and-its-addons' ),
20980
+ $user_plugin->title
20981
+ )
20982
+ );
20983
+ }
20984
+ }
20985
+
20986
+ $multiple_products_text = sprintf(
20987
+ "<small class='products'><strong>%s:</strong> %s</small>",
20988
+ $this->get_text_inline( 'Products', 'products' ),
20989
+ $products_and_add_ons
20990
+ );
20991
+ }
20992
+
20993
+ $actions = sprintf(
20994
+ '<ul><li>%s<span class="action-description"> - %s</span></li><li>%s<span class="action-description"> - %s</span></li></ul>',
20995
+ sprintf('<button class="button button-primary allow-marketing">%s</button>', $this->get_text_inline( 'Yes', 'yes' ) ),
20996
+ $this->get_text_inline( 'send me security & feature updates, educational content and offers.', 'send-updates' ),
20997
+ sprintf('<button class="button button-secondary">%s</button>', $this->get_text_inline( 'No', 'no' ) ),
20998
+ sprintf(
20999
+ $this->get_text_inline( 'do %sNOT%s send me security & feature updates, educational content and offers.', 'do-not-send-updates' ),
21000
+ '<span class="underlined">',
21001
+ '</span>'
21002
+ )
21003
+ );
21004
+
21005
+ return sprintf(
21006
+ '%s %s %s',
21007
+ $thank_you,
21008
+ $already_opted_in,
21009
+ sprintf($this->get_text_inline( 'Due to the new %sEU General Data Protection Regulation (GDPR)%s compliance requirements it is required that you provide your explicit consent, again, confirming that you are onboard 🙂', 'due-to-gdpr-compliance-requirements' ), '<a href="https://eugdpr.org/" target="_blank" rel="noopener noreferrer">', '</a>') .
21010
+ '<br><br>' .
21011
+ '<b>' . $this->get_text_inline( "Please let us know if you'd like us to contact you for security & feature updates, educational content, and occasional offers:", 'contact-for-updates' ) . '</b>' .
21012
+ $actions .
21013
+ ( $is_single_parent_product ? '' : $multiple_products_text )
21014
+ );
21015
+ }
21016
+
21017
+ /**
21018
+ * This method is called for opted-in users to fetch the is_marketing_allowed flag of the user for all the
21019
+ * plugins and themes they've opted in to.
21020
+ *
21021
+ * @author Leo Fajardo (@leorw)
21022
+ * @since 2.1.0
21023
+ *
21024
+ * @param string $user_email
21025
+ * @param string $license_key
21026
+ * @param array $plugin_ids
21027
+ * @param string|null $license_key
21028
+ *
21029
+ * @return array|false
21030
+ */
21031
+ private function fetch_user_marketing_flag_status_by_plugins( $user_email, $license_key, $plugin_ids ) {
21032
+ $request = array(
21033
+ 'method' => 'POST',
21034
+ 'body' => array(),
21035
+ 'timeout' => WP_FS__DEBUG_SDK ? 60 : 30,
21036
+ );
21037
+
21038
+ if ( is_string( $user_email ) ) {
21039
+ $request['body']['email'] = $user_email;
21040
+ } else {
21041
+ $request['body']['license_key'] = $license_key;
21042
+ }
21043
+
21044
+ $result = array();
21045
+
21046
+ $url = WP_FS__ADDRESS . '/action/service/user_plugin/';
21047
+ $total_plugin_ids = count( $plugin_ids );
21048
+
21049
+ $plugin_ids_count_per_request = 10;
21050
+ for ( $i = 1; $i <= $total_plugin_ids; $i += $plugin_ids_count_per_request ) {
21051
+ $plugin_ids_set = array_slice( $plugin_ids, $i - 1, $plugin_ids_count_per_request );
21052
+
21053
+ $request['body']['plugin_ids'] = $plugin_ids_set;
21054
+
21055
+ $response = self::safe_remote_post(
21056
+ $url,
21057
+ $request,
21058
+ WP_FS__TIME_24_HOURS_IN_SEC,
21059
+ WP_FS__TIME_12_HOURS_IN_SEC
21060
+ );
21061
+
21062
+ if ( ! is_wp_error( $response ) ) {
21063
+ $decoded = is_string( $response['body'] ) ?
21064
+ json_decode( $response['body'] ) :
21065
+ null;
21066
+
21067
+ if (
21068
+ !is_object($decoded) ||
21069
+ !isset($decoded->success) ||
21070
+ true !== $decoded->success ||
21071
+ !isset( $decoded->data ) ||
21072
+ !is_array( $decoded->data )
21073
+ ) {
21074
+ return false;
21075
+ }
21076
+
21077
+ $result = array_merge( $result, $decoded->data );
21078
+ }
21079
+ }
21080
+
21081
+ return $result;
21082
+ }
21083
+
21084
+ /**
21085
+ * @author Leo Fajardo (@leorw)
21086
+ * @since 2.1.0
21087
+ */
21088
+ function _maybe_show_gdpr_admin_notice() {
21089
+ if ( ! $this->is_user_in_admin() ) {
21090
+ return;
21091
+ }
21092
+
21093
+ if ( ! $this->should_handle_gdpr_admin_notice() ) {
21094
+ return;
21095
+ }
21096
+
21097
+ if ( ! $this->is_user_admin() ) {
21098
+ return;
21099
+ }
21100
+
21101
+ require_once WP_FS__DIR_INCLUDES . '/class-fs-user-lock.php';
21102
+
21103
+ $lock = FS_User_Lock::instance();
21104
+
21105
+ /**
21106
+ * Try to acquire a 60-sec lock based on the WP user and thread/process ID.
21107
+ */
21108
+ if ( ! $lock->try_lock( 60 ) ) {
21109
+ return;
21110
+ }
21111
+
21112
+ /**
21113
+ * @var $current_wp_user WP_User
21114
+ */
21115
+ $current_wp_user = self::_get_current_wp_user();
21116
+
21117
+ /**
21118
+ * @var FS_User $current_fs_user
21119
+ */
21120
+ $current_fs_user = Freemius::_get_user_by_email( $current_wp_user->user_email );
21121
+
21122
+ $ten_years_in_sec = 10 * 365 * WP_FS__TIME_24_HOURS_IN_SEC;
21123
+
21124
+ if ( ! is_object( $current_fs_user ) ) {
21125
+ // 10-year lock.
21126
+ $lock->lock( $ten_years_in_sec );
21127
+
21128
+ return;
21129
+ }
21130
+
21131
+ $gdpr = FS_GDPR_Manager::instance();
21132
+
21133
+ if ( $gdpr->is_opt_in_notice_shown() ) {
21134
+ // 30-day lock.
21135
+ $lock->lock( 30 * WP_FS__TIME_24_HOURS_IN_SEC );
21136
+
21137
+ return;
21138
+ }
21139
+
21140
+ if ( ! $gdpr->should_show_opt_in_notice() ) {
21141
+ // 10-year lock.
21142
+ $lock->lock( $ten_years_in_sec );
21143
+
21144
+ return;
21145
+ }
21146
+
21147
+ $last_time_notice_shown = $gdpr->last_time_notice_was_shown();
21148
+ $was_notice_shown_before = ( false !== $last_time_notice_shown );
21149
+
21150
+ if ( $was_notice_shown_before &&
21151
+ 30 * WP_FS__TIME_24_HOURS_IN_SEC > time() - $last_time_notice_shown
21152
+ ) {
21153
+ // If the notice was shown before, show it again after 30 days from the last time it was shown.
21154
+ return;
21155
+ }
21156
+
21157
+ /**
21158
+ * Find all plugin IDs that were installed by the current admin.
21159
+ */
21160
+ $plugin_ids_map = self::get_user_opted_in_module_ids_map( $current_fs_user->id );
21161
+
21162
+ if ( empty( $plugin_ids_map )) {
21163
+ $lock->lock( $ten_years_in_sec );
21164
+
21165
+ return;
21166
+ }
21167
+
21168
+ $user_plugins = $this->fetch_user_marketing_flag_status_by_plugins(
21169
+ $current_fs_user->email,
21170
+ null,
21171
+ array_keys( $plugin_ids_map )
21172
+ );
21173
+
21174
+ if ( empty( $user_plugins ) ) {
21175
+ $lock->lock(
21176
+ is_array($user_plugins) ?
21177
+ $ten_years_in_sec :
21178
+ // Lock for 24-hours on errors.
21179
+ WP_FS__TIME_24_HOURS_IN_SEC
21180
+ );
21181
+
21182
+ return;
21183
+ }
21184
+
21185
+ $has_unset_marketing_optin = false;
21186
+
21187
+ foreach ( $user_plugins as $user_plugin ) {
21188
+ if ( true == $user_plugin->is_marketing_allowed ) {
21189
+ unset( $plugin_ids_map[ $user_plugin->plugin_id ] );
21190
+ }
21191
+
21192
+ if ( ! $has_unset_marketing_optin && is_null( $user_plugin->is_marketing_allowed ) ) {
21193
+ $has_unset_marketing_optin = true;
21194
+ }
21195
+ }
21196
+
21197
+ if ( empty( $plugin_ids_map ) ||
21198
+ ( $was_notice_shown_before && ! $has_unset_marketing_optin )
21199
+ ) {
21200
+ $lock->lock( $ten_years_in_sec );
21201
+
21202
+ return;
21203
+ }
21204
+
21205
+ $modules = array_merge(
21206
+ array_values( self::$_accounts->get_option( 'plugins', array() ) ),
21207
+ array_values( self::$_accounts->get_option( 'themes', array() ) )
21208
+ );
21209
+
21210
+ foreach ( $modules as $module ) {
21211
+ if ( ! FS_Plugin::is_valid_id( $module->parent_plugin_id ) && isset( $plugin_ids_map[ $module->id ] ) ) {
21212
+ $plugin_ids_map[ $module->id ] = $module;
21213
+ }
21214
+ }
21215
+
21216
+ $plugin_title = null;
21217
+ if ( 1 === count( $plugin_ids_map ) ) {
21218
+ $module = reset( $plugin_ids_map );
21219
+ $plugin_title = $module->title;
21220
+ }
21221
+
21222
+ $gdpr->add_opt_in_sticky_notice(
21223
+ $this->get_gdpr_admin_notice_string( $plugin_ids_map ),
21224
+ $plugin_title
21225
+ );
21226
+
21227
+ $this->add_gdpr_optin_ajax_handler_and_style();
21228
+
21229
+ $gdpr->notice_was_just_shown();
21230
+
21231
+ // 30-day lock.
21232
+ $lock->lock( 30 * WP_FS__TIME_24_HOURS_IN_SEC );
21233
+ }
21234
+
21235
+ /**
21236
+ * Prevents the GDPR opt-in admin notice from being added if the user has already chosen to allow or not allow
21237
+ * marketing.
21238
+ *
21239
+ * @author Leo Fajardo (@leorw)
21240
+ * @since 2.1.0
21241
+ */
21242
+ private function disable_opt_in_notice_and_lock_user() {
21243
+ FS_GDPR_Manager::instance()->disable_opt_in_notice();
21244
+
21245
+ require_once WP_FS__DIR_INCLUDES . '/class-fs-user-lock.php';
21246
+
21247
+ // 10-year lock.
21248
+ FS_User_Lock::instance()->lock( 10 * 365 * WP_FS__TIME_24_HOURS_IN_SEC );
21249
+ }
21250
+
21251
+ /**
21252
+ * @author Leo Fajardo (@leorw)
21253
+ * @since 2.1.0
21254
+ */
21255
+ function _add_gdpr_optin_js() {
21256
+ $vars = array( 'id' => $this->_module_id );
21257
+
21258
+ fs_require_once_template( 'gdpr-optin-js.php', $vars );
21259
+ }
21260
+
21261
+ /**
21262
+ * @author Leo Fajardo (@leorw)
21263
+ * @since 2.1.0
21264
+ */
21265
+ function enqueue_gdpr_optin_notice_style() {
21266
+ fs_enqueue_local_style( 'fs_gdpr_optin_notice', '/admin/gdpr-optin-notice.css' );
21267
+ }
21268
+
21269
+ /**
21270
+ * @author Leo Fajardo (@leorw)
21271
+ * @since 2.1.0
21272
+ */
21273
+ function _maybe_add_gdpr_optin_ajax_handler() {
21274
+ $this->add_ajax_action( 'fetch_is_marketing_required_flag_value', array( &$this, '_fetch_is_marketing_required_flag_value_ajax_action' ) );
21275
+
21276
+ if ( FS_GDPR_Manager::instance()->is_opt_in_notice_shown() ) {
21277
+ $this->add_gdpr_optin_ajax_handler_and_style();
21278
+ }
21279
+ }
21280
+
21281
+ /**
21282
+ * @author Leo Fajardo (@leorw)
21283
+ * @since 2.1.0
21284
+ */
21285
+ function _fetch_is_marketing_required_flag_value_ajax_action() {
21286
+ $this->_logger->entrance();
21287
+
21288
+ $this->check_ajax_referer( 'fetch_is_marketing_required_flag_value' );
21289
+
21290
+ $license_key = fs_request_get( 'license_key' );
21291
+
21292
+ if ( empty($license_key) ) {
21293
+ self::shoot_ajax_failure( $this->get_text_inline( 'License key is empty.', 'empty-license-key' ) );
21294
+ }
21295
+
21296
+ $user_plugins = $this->fetch_user_marketing_flag_status_by_plugins(
21297
+ null,
21298
+ $license_key,
21299
+ array( $this->_module_id )
21300
+ );
21301
+
21302
+ if ( ! is_array( $user_plugins ) ||
21303
+ empty($user_plugins) ||
21304
+ !isset($user_plugins[0]->plugin_id) ||
21305
+ $user_plugins[0]->plugin_id != $this->_module_id
21306
+ ) {
21307
+ /**
21308
+ * If faced an error or if the module ID do not match to the current module, ask for GDPR opt-in.
21309
+ *
21310
+ * @author Vova Feldman (@svovaf)
21311
+ */
21312
+ self::shoot_ajax_success( array( 'is_marketing_allowed' => null ) );
21313
+ }
21314
+
21315
+ self::shoot_ajax_success( array( 'is_marketing_allowed' => $user_plugins[0]->is_marketing_allowed ) );
21316
+ }
21317
+
21318
+ /**
21319
+ * @author Leo Fajardo (@leorw)
21320
+ * @since 2.1.0
21321
+ */
21322
+ private function add_gdpr_optin_ajax_handler_and_style() {
21323
+ // Add GDPR action AJAX callback.
21324
+ $this->add_ajax_action( 'gdpr_optin_action', array( &$this, '_gdpr_optin_ajax_action' ) );
21325
+
21326
+ add_action( 'admin_footer', array( &$this, '_add_gdpr_optin_js' ) );
21327
+ add_action( 'admin_enqueue_scripts', array( &$this, 'enqueue_gdpr_optin_notice_style' ) );
21328
+ }
21329
+
21330
+ /**
21331
+ * @author Leo Fajardo (@leorw)
21332
+ * @since 2.1.0
21333
+ */
21334
+ function _gdpr_optin_ajax_action() {
21335
+ $this->_logger->entrance();
21336
+
21337
+ $this->check_ajax_referer( 'gdpr_optin_action' );
21338
+
21339
+ if ( ! fs_request_has( 'is_marketing_allowed' ) || ! fs_request_has( 'plugin_ids' ) ) {
21340
+ self::shoot_ajax_failure();
21341
+ }
21342
+
21343
+ $current_wp_user = self::_get_current_wp_user();
21344
+
21345
+ $plugin_ids = fs_request_get( 'plugin_ids', array() );
21346
+ if ( ! is_array( $plugin_ids ) || empty( $plugin_ids ) ) {
21347
+ self::shoot_ajax_failure();
21348
+ }
21349
+
21350
+ $modules = array_merge(
21351
+ array_values( self::$_accounts->get_option( 'plugins', array() ) ),
21352
+ array_values( self::$_accounts->get_option( 'themes', array() ) )
21353
+ );
21354
+
21355
+ foreach ( $modules as $key => $module ) {
21356
+ if ( ! in_array( $module->id, $plugin_ids ) ) {
21357
+ unset( $modules[ $key ] );
21358
+ }
21359
+ }
21360
+
21361
+ if ( empty( $modules ) ) {
21362
+ self::shoot_ajax_failure();
21363
+ }
21364
+
21365
+ $user_api = $this->get_api_user_scope_by_user( Freemius::_get_user_by_email( $current_wp_user->user_email ) );
21366
+
21367
+ foreach ( $modules as $module ) {
21368
+ $user_api->call( "?plugin_id={$module->id}", 'put', array(
21369
+ 'is_marketing_allowed' => ( true == fs_request_get_bool( 'is_marketing_allowed' ) )
21370
+ ) );
21371
+ }
21372
+
21373
+ FS_GDPR_Manager::instance()->remove_opt_in_notice();
21374
+
21375
+ require_once WP_FS__DIR_INCLUDES . '/class-fs-user-lock.php';
21376
+
21377
+ // 10-year lock.
21378
+ FS_User_Lock::instance()->lock( 10 * 365 * WP_FS__TIME_24_HOURS_IN_SEC );
21379
+
21380
+ self::shoot_ajax_success();
21381
+ }
21382
+
21383
+ /**
21384
+ * Checks if the GDPR admin notice should be handled. By default, this logic is off, unless the integrator adds the special 'handle_gdpr_admin_notice' filter.
21385
+ *
21386
+ * @author Vova Feldman (@svovaf)
21387
+ * @since 2.1.0
21388
+ *
21389
+ * @return bool
21390
+ */
21391
+ private function should_handle_gdpr_admin_notice() {
21392
+ return $this->apply_filters(
21393
+ 'handle_gdpr_admin_notice',
21394
+ // Default to false.
21395
+ false
21396
+ );
21397
+ }
21398
+
21399
+ #endregion
21400
+
21401
+ #----------------------------------------------------------------------------------
21402
+ #region Marketing
21403
+ #----------------------------------------------------------------------------------
21404
+
21405
+ /**
21406
+ * Check if current user purchased any other plugins before.
21407
+ *
21408
+ * @author Vova Feldman (@svovaf)
21409
+ * @since 1.0.9
21410
+ *
21411
+ * @return bool
21412
+ */
21413
+ function has_purchased_before() {
21414
+ // TODO: Implement has_purchased_before() method.
21415
+ throw new Exception( 'not implemented' );
21416
+ }
21417
+
21418
+ /**
21419
+ * Check if current user classified as an agency.
21420
+ *
21421
+ * @author Vova Feldman (@svovaf)
21422
+ * @since 1.0.9
21423
+ *
21424
+ * @return bool
21425
+ */
21426
+ function is_agency() {
21427
+ // TODO: Implement is_agency() method.
21428
+ throw new Exception( 'not implemented' );
21429
+ }
21430
+
21431
+ /**
21432
+ * Check if current user classified as a developer.
21433
+ *
21434
+ * @author Vova Feldman (@svovaf)
21435
+ * @since 1.0.9
21436
+ *
21437
+ * @return bool
21438
+ */
21439
+ function is_developer() {
21440
+ // TODO: Implement is_developer() method.
21441
+ throw new Exception( 'not implemented' );
21442
+ }
21443
+
21444
+ /**
21445
+ * Check if current user classified as a business.
21446
+ *
21447
+ * @author Vova Feldman (@svovaf)
21448
+ * @since 1.0.9
21449
+ *
21450
+ * @return bool
21451
+ */
21452
+ function is_business() {
21453
+ // TODO: Implement is_business() method.
21454
+ throw new Exception( 'not implemented' );
21455
+ }
21456
+
21457
+ #endregion
21458
+
21459
+ #----------------------------------------------------------------------------------
21460
+ #region Helper
21461
+ #----------------------------------------------------------------------------------
21462
+
21463
+ /**
21464
+ * If running with a secret key, assume it's the developer and show pending plans as well.
21465
+ *
21466
+ * @author Vova Feldman (@svovaf)
21467
+ * @since 2.1.2
21468
+ *
21469
+ * @param string $path
21470
+ *
21471
+ * @return string
21472
+ */
21473
+ function add_show_pending( $path ) {
21474
+ if ( ! $this->has_secret_key() ) {
21475
+ return $path;
21476
+ }
21477
+
21478
+ return $path . ( false !== strpos( $path, '?' ) ? '&' : '?' ) . 'show_pending=true';
21479
+ }
21480
+
21481
+ #endregion
21482
+ }
freemius/includes/class-fs-admin-notices.php ADDED
@@ -0,0 +1,321 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 2.0.0
7
+ */
8
+
9
+ if ( ! defined( 'ABSPATH' ) ) {
10
+ exit;
11
+ }
12
+
13
+ /**
14
+ * WP Admin notices manager both for site level and network level.
15
+ *
16
+ * Class FS_Admin_Notices
17
+ */
18
+ class FS_Admin_Notices {
19
+ /**
20
+ * @since 1.2.2
21
+ *
22
+ * @var string
23
+ */
24
+ protected $_module_unique_affix;
25
+ /**
26
+ * @var string
27
+ */
28
+ protected $_id;
29
+ /**
30
+ * @var string
31
+ */
32
+ protected $_title;
33
+ /**
34
+ * @var FS_Admin_Notice_Manager
35
+ */
36
+ protected $_notices;
37
+ /**
38
+ * @var FS_Admin_Notice_Manager
39
+ */
40
+ protected $_network_notices;
41
+ /**
42
+ * @var int The ID of the blog that is associated with the current site level options.
43
+ */
44
+ private $_blog_id = 0;
45
+ /**
46
+ * @var bool
47
+ */
48
+ private $_is_multisite;
49
+ /**
50
+ * @var FS_Admin_Notices[]
51
+ */
52
+ private static $_instances = array();
53
+
54
+ /**
55
+ * @param string $id
56
+ * @param string $title
57
+ * @param string $module_unique_affix
58
+ * @param bool $is_network_and_blog_admins Whether or not the message should be shown both on network and
59
+ * blog admin pages.
60
+ *
61
+ * @return FS_Admin_Notices
62
+ */
63
+ static function instance( $id, $title = '', $module_unique_affix = '', $is_network_and_blog_admins = false ) {
64
+ if ( ! isset( self::$_instances[ $id ] ) ) {
65
+ self::$_instances[ $id ] = new FS_Admin_Notices( $id, $title, $module_unique_affix, $is_network_and_blog_admins );
66
+ }
67
+
68
+ return self::$_instances[ $id ];
69
+ }
70
+
71
+ /**
72
+ * @param string $id
73
+ * @param string $title
74
+ * @param string $module_unique_affix
75
+ * @param bool $is_network_and_blog_admins Whether or not the message should be shown both on network and
76
+ * blog admin pages.
77
+ */
78
+ protected function __construct( $id, $title = '', $module_unique_affix = '', $is_network_and_blog_admins = false ) {
79
+ $this->_id = $id;
80
+ $this->_title = $title;
81
+ $this->_module_unique_affix = $module_unique_affix;
82
+ $this->_is_multisite = is_multisite();
83
+
84
+ if ( $this->_is_multisite ) {
85
+ $this->_blog_id = get_current_blog_id();
86
+
87
+ $this->_network_notices = FS_Admin_Notice_Manager::instance(
88
+ $id,
89
+ $title,
90
+ $module_unique_affix,
91
+ $is_network_and_blog_admins,
92
+ true
93
+ );
94
+ }
95
+
96
+ $this->_notices = FS_Admin_Notice_Manager::instance(
97
+ $id,
98
+ $title,
99
+ $module_unique_affix,
100
+ false,
101
+ $this->_blog_id
102
+ );
103
+ }
104
+
105
+ /**
106
+ * Add admin message to admin messages queue, and hook to admin_notices / all_admin_notices if not yet hooked.
107
+ *
108
+ * @author Vova Feldman (@svovaf)
109
+ * @since 1.0.4
110
+ *
111
+ * @param string $message
112
+ * @param string $title
113
+ * @param string $type
114
+ * @param bool $is_sticky
115
+ * @param string $id Message ID
116
+ * @param bool $store_if_sticky
117
+ * @param int|null $network_level_or_blog_id
118
+ *
119
+ * @uses add_action()
120
+ */
121
+ function add(
122
+ $message,
123
+ $title = '',
124
+ $type = 'success',
125
+ $is_sticky = false,
126
+ $id = '',
127
+ $store_if_sticky = true,
128
+ $network_level_or_blog_id = null
129
+ ) {
130
+ if ( $this->should_use_network_notices( $id, $network_level_or_blog_id ) ) {
131
+ $notices = $this->_network_notices;
132
+ } else {
133
+ $notices = $this->get_site_notices( $network_level_or_blog_id );
134
+ }
135
+
136
+ $notices->add(
137
+ $message,
138
+ $title,
139
+ $type,
140
+ $is_sticky,
141
+ $id,
142
+ $store_if_sticky
143
+ );
144
+ }
145
+
146
+ /**
147
+ * @author Vova Feldman (@svovaf)
148
+ * @since 1.0.7
149
+ *
150
+ * @param string|string[] $ids
151
+ * @param int|null $network_level_or_blog_id
152
+ */
153
+ function remove_sticky( $ids, $network_level_or_blog_id = null ) {
154
+ if ( ! is_array( $ids ) ) {
155
+ $ids = array( $ids );
156
+ }
157
+
158
+ if ( $this->should_use_network_notices( $ids[0], $network_level_or_blog_id ) ) {
159
+ $notices = $this->_network_notices;
160
+ } else {
161
+ $notices = $this->get_site_notices( $network_level_or_blog_id );
162
+ }
163
+
164
+ return $notices->remove_sticky( $ids );
165
+ }
166
+
167
+ /**
168
+ * Check if sticky message exists by id.
169
+ *
170
+ * @author Vova Feldman (@svovaf)
171
+ * @since 1.0.9
172
+ *
173
+ * @param string $id
174
+ * @param int|null $network_level_or_blog_id
175
+ *
176
+ * @return bool
177
+ */
178
+ function has_sticky( $id, $network_level_or_blog_id = null ) {
179
+ if ( $this->should_use_network_notices( $id, $network_level_or_blog_id ) ) {
180
+ $notices = $this->_network_notices;
181
+ } else {
182
+ $notices = $this->get_site_notices( $network_level_or_blog_id );
183
+ }
184
+
185
+ return $notices->has_sticky( $id );
186
+ }
187
+
188
+ /**
189
+ * Adds sticky admin notification.
190
+ *
191
+ * @author Vova Feldman (@svovaf)
192
+ * @since 1.0.7
193
+ *
194
+ * @param string $message
195
+ * @param string $id Message ID
196
+ * @param string $title
197
+ * @param string $type
198
+ * @param int|null $network_level_or_blog_id
199
+ * @param number|null $wp_user_id
200
+ * @param string|null $plugin_title
201
+ * @param bool $is_network_and_blog_admins Whether or not the message should be shown both on network and
202
+ * blog admin pages.
203
+ */
204
+ function add_sticky(
205
+ $message,
206
+ $id,
207
+ $title = '',
208
+ $type = 'success',
209
+ $network_level_or_blog_id = null,
210
+ $wp_user_id = null,
211
+ $plugin_title = null,
212
+ $is_network_and_blog_admins = false
213
+ ) {
214
+ if ( $this->should_use_network_notices( $id, $network_level_or_blog_id ) ) {
215
+ $notices = $this->_network_notices;
216
+ } else {
217
+ $notices = $this->get_site_notices( $network_level_or_blog_id );
218
+ }
219
+
220
+ $notices->add_sticky( $message, $id, $title, $type, $wp_user_id, $plugin_title, $is_network_and_blog_admins );
221
+ }
222
+
223
+ /**
224
+ * Clear all sticky messages.
225
+ *
226
+ * @author Vova Feldman (@svovaf)
227
+ * @since 2.0.0
228
+ *
229
+ * @param int|null $network_level_or_blog_id
230
+ */
231
+ function clear_all_sticky( $network_level_or_blog_id = null ) {
232
+ if ( ! $this->_is_multisite ||
233
+ false === $network_level_or_blog_id ||
234
+ 0 == $network_level_or_blog_id ||
235
+ is_null( $network_level_or_blog_id )
236
+ ) {
237
+ $notices = $this->get_site_notices( $network_level_or_blog_id );
238
+ $notices->clear_all_sticky();
239
+ }
240
+
241
+ if ( $this->_is_multisite &&
242
+ ( true === $network_level_or_blog_id || is_null( $network_level_or_blog_id ) )
243
+ ) {
244
+ $this->_network_notices->clear_all_sticky();
245
+ }
246
+ }
247
+
248
+ /**
249
+ * Add admin message to all admin messages queue, and hook to all_admin_notices if not yet hooked.
250
+ *
251
+ * @author Vova Feldman (@svovaf)
252
+ * @since 1.0.4
253
+ *
254
+ * @param string $message
255
+ * @param string $title
256
+ * @param string $type
257
+ * @param bool $is_sticky
258
+ * @param string $id Message ID
259
+ */
260
+ function add_all( $message, $title = '', $type = 'success', $is_sticky = false, $id = '' ) {
261
+ $this->add( $message, $title, $type, $is_sticky, true, $id );
262
+ }
263
+
264
+ #--------------------------------------------------------------------------------
265
+ #region Helper Methods
266
+ #--------------------------------------------------------------------------------
267
+
268
+ /**
269
+ * @author Vova Feldman (@svovaf)
270
+ * @since 2.0.0
271
+ *
272
+ * @param int $blog_id
273
+ *
274
+ * @return FS_Admin_Notice_Manager
275
+ */
276
+ private function get_site_notices( $blog_id = 0 ) {
277
+ if ( 0 == $blog_id || $blog_id == $this->_blog_id ) {
278
+ return $this->_notices;
279
+ }
280
+
281
+ return FS_Admin_Notice_Manager::instance(
282
+ $this->_id,
283
+ $this->_title,
284
+ $this->_module_unique_affix,
285
+ false,
286
+ $blog_id
287
+ );
288
+ }
289
+
290
+ /**
291
+ * Check if the network notices should be used.
292
+ *
293
+ * @author Vova Feldman (@svovaf)
294
+ * @since 2.0.0
295
+ *
296
+ * @param string $id
297
+ * @param null|bool|int $network_level_or_blog_id When an integer, use the given blog storage. When `true` use the multisite notices (if there's a network). When `false`, use the current context blog notices. When `null`, the decision which notices manager to use (MS vs. Current S) will be handled internally and determined based on the $id and the context admin (blog admin vs. network level admin).
298
+ *
299
+ * @return bool
300
+ */
301
+ private function should_use_network_notices( $id = '', $network_level_or_blog_id = null ) {
302
+ if ( ! $this->_is_multisite ) {
303
+ // Not a multisite environment.
304
+ return false;
305
+ }
306
+
307
+ if ( is_numeric( $network_level_or_blog_id ) ) {
308
+ // Explicitly asked to use a specified blog storage.
309
+ return false;
310
+ }
311
+
312
+ if ( is_bool( $network_level_or_blog_id ) ) {
313
+ // Explicitly specified whether should use the network or blog level storage.
314
+ return $network_level_or_blog_id;
315
+ }
316
+
317
+ return fs_is_network_admin();
318
+ }
319
+
320
+ #endregion
321
+ }
freemius/includes/class-fs-api.php ADDED
@@ -0,0 +1,615 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @package Freemius
4
+ * @copyright Copyright (c) 2015, Freemius, Inc.
5
+ * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
6
+ * @since 1.0.4
7
+ */
8
+
9
+ if ( ! defined( 'ABSPATH' ) ) {
10
+ exit;
11
+ }
12
+
13
+ /**
14
+ * Class FS_Api
15
+ *
16
+ * Wraps Freemius API SDK to handle:
17
+ * 1. Clock sync.
18
+ * 2. Fallback to HTTP when HTTPS fails.
19
+ * 3. Adds caching layer to GET requests.
20
+ * 4. Adds consistency for failed requests by using last cached version.
21
+ */
22
+ class FS_Api {
23
+ /**
24
+ * @var FS_Api[]
25
+ */
26
+ private static $_instances = array();
27
+
28
+ /**
29
+ * @var FS_Option_Manager Freemius options, options-manager.
30
+ */
31
+ private static $_options;
32
+
33
+ /**
34
+ * @var FS_Cache_Manager API Caching layer
35
+ */
36
+ private static $_cache;
37
+
38
+ /**
39
+ * @var int Clock diff in seconds between current server to API server.
40
+ */
41
+ private static $_clock_diff;
42
+
43
+ /**
44
+ * @var Freemius_Api_WordPress
45
+ */
46
+ private $_api;
47
+
48
+ /**
49
+ * @var string
50
+ */
51
+ private $_slug;
52
+
53
+ /**
54
+ * @var FS_Logger
55
+ * @since 1.0.4
56
+ */
57
+ private $_logger;
58
+
59
+ /**
60
+ * @param string $slug
61
+ * @param string $scope 'app', 'developer', 'user' or 'install'.
62
+ * @param number $id Element's id.
63
+ * @param string $public_key Public key.
64
+ * @param bool $is_sandbox
65
+ * @param bool|string $secret_key Element's secret key.
66
+ *
67
+ * @return FS_Api
68
+ */
69
+ static function instance( $slug, $scope, $id, $public_key, $is_sandbox, $secret_key = false ) {
70
+ $identifier = md5( $slug . $scope . $id . $public_key . ( is_string( $secret_key ) ? $secret_key : '' ) . json_encode( $is_sandbox ) );
71
+
72
+ if ( ! isset( self::$_instances[ $identifier ] ) ) {
73
+ self::_init();
74
+
75
+ self::$_instances[ $identifier ] = new FS_Api( $slug, $scope, $id, $public_key, $secret_key, $is_sandbox );
76
+ }
77
+
78
+ return self::$_instances[ $identifier ];
79
+ }
80
+
81
+ private static function _init() {
82
+ if ( isset( self::$_options ) ) {
83
+ return;
84
+ }
85
+
86
+ if ( ! class_exists( 'Freemius_Api_WordPress' ) ) {
87
+ require_once WP_FS__DIR_SDK . '/FreemiusWordPress.php';
88
+ }
89
+
90
+ self::$_options = FS_Option_Manager::get_manager( WP_FS__OPTIONS_OPTION_NAME, true, true );
91
+ self::$_cache = FS_Cache_Manager::get_manager( WP_FS__API_CACHE_OPTION_NAME );
92
+
93
+ self::$_clock_diff = self::$_options->get_option( 'api_clock_diff', 0 );
94
+ Freemius_Api_WordPress::SetClockDiff( self::$_clock_diff );
95
+
96
+ if ( self::$_options->get_option( 'api_force_http', false ) ) {
97
+ Freemius_Api_WordPress::SetHttp();
98
+ }
99
+ }
100
+
101
+ /**
102
+ * @param string $slug
103
+ * @param string $scope 'app', 'developer', 'user' or 'install'.
104
+ * @param number $id Element's id.
105
+ * @param string $public_key Public key.
106
+ * @param bool|string $secret_key Element's secret key.
107
+ * @param bool $is_sandbox
108
+ */
109
+ private function __construct( $slug, $scope, $id, $public_key, $secret_key, $is_sandbox ) {
110
+ $this->_api = new Freemius_Api_WordPress( $scope, $id, $public_key, $secret_key, $is_sandbox );
111
+
112
+ $this->_slug = $slug;
113
+ $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $slug . '_api', WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
114
+ }
115
+
116
+ /**
117
+ * Find