Yasr – Yet Another Stars Rating - Version 3.1.0

Version Description

  • TWEAKED: A lot of under the hood changes
  • TWEAKED: Updated Freemius sdk to version 2.4.4
Download this release

Release Info

Developer Dudo
Plugin Icon 128x128 Yasr – Yet Another Stars Rating
Version 3.1.0
Comparing to
See all releases

Code changes from version 3.0.9 to 3.1.0

admin/js/guten/blocks/shortcodes.js CHANGED
@@ -1 +1 @@
1
- (()=>{"use strict";var e={434:(e,t,r)=>{r.d(t,{Mp:()=>u,Sg:()=>d,YD:()=>y,aF:()=>p,lN:()=>n,pD:()=>s,pP:()=>R,rl:()=>k,rn:()=>v,yG:()=>m});var a=wp.i18n.__,s=a("All these settings are optional","yet-another-stars-rating"),n=a("Size","yet-another-stars-rating"),o=a("Stars size","yet-another-stars-rating"),i=a("Small","yet-another-stars-rating"),l=a("Medium","yet-another-stars-rating"),c=a("Large","yet-another-stars-rating"),u=a("Leave this blank if you don't know what you're doing.","yet-another-stars-rating"),h=a("Remember: only the post author can rate here.","yet-another-stars-rating"),g=a("This is the star set where your users will be able to vote","yet-another-stars-rating"),m=function(e){var t;return!1!==e.postId&&(t=e.postId),React.createElement("div",null,React.createElement("input",{type:"text",size:"4",defaultValue:t,onKeyPress:function(t){return function(e,t){if("Enter"===t.key){var r=t.target.value;!0!==/^\d+$/.test(r)&&""!==r||e({postId:r}),t.preventDefault()}}(e.setAttributes,t)}}))},p=function(e){return React.createElement("form",null,React.createElement("select",{value:e.size,onChange:function(t){return(0,e.setAttributes)({size:(r=t).target.querySelector("option:checked").value}),void r.preventDefault();var r}},React.createElement("option",{value:"--"},o," "),React.createElement("option",{value:"small"},i," "),React.createElement("option",{value:"medium"},l),React.createElement("option",{value:"large"},c," ")))},d=function(e){if(!0===JSON.parse(yasrConstantGutenberg.isFseElement))return React.createElement("div",{className:"yasr-guten-block-panel yasr-guten-block-panel-center"},React.createElement("div",null,a("This is a template file, you can't rate here. You need to insert the rating inside the single post or page","yet-another-stars-rating")),React.createElement("br",null));if(null===wp.data.select("core/editor"))return React.createElement(React.Fragment,null);var t=a("Rate this article / item","yet-another-stars-rating"),r=wp.data.select("core/editor").getCurrentPost().meta.yasr_overall_rating,s=function(e,t){e=e.toFixed(1),e=parseFloat(e),wp.data.dispatch("core/editor").editPost({meta:{yasr_overall_rating:e}}),this.setRating(e),t()};return React.createElement("div",{className:"yasr-guten-block-panel yasr-guten-block-panel-center"},t,React.createElement("div",{id:"overall-rater",ref:function(){return yasrSetRaterValue(32,"overall-rater",!1,.1,!1,r,s)}}))},y=function(e,t){var r=null;return"edit"===t?("small"!==e&&"medium"!==e||(r=' size="'.concat(e,'"')),r):("small"!==e&&"medium"!==e&&"large"!==e||(r=' size="'.concat(e,'"')),r)},v=function(e){var t=null;return!0===/^\d+$/.test(e)&&(t=' postid="'.concat(e,'"')),t},k=function(e){var t={className:null,shortCode:null,overallRating:!1,hookName:!1,panelSettings:!0,sizeAndId:!1};return"yet-another-stars-rating/overall-rating"===e&&(t.overallRating=!0,t.className="yasr-overall-block",t.shortCode="yasr_overall_rating",t.bottomDesc=h,t.sizeAndId=!0),"yet-another-stars-rating/visitor-votes"===e&&(t.className="yasr-vv-block",t.shortCode="yasr_visitor_votes",t.bottomDesc=g,t.sizeAndId=!0),"yet-another-stars-rating/overall-rating-ranking"===e&&(t.className="yasr-ov-ranking-block",t.shortCode="yasr_ov_ranking",t.hookName="yasr_overall_rating_rankings"),"yet-another-stars-rating/visitor-votes-ranking"===e&&(t.className="yasr-vv-ranking-block",t.shortCode="yasr_most_or_highest_rated_posts",t.hookName="yasr_visitor_votes_rankings"),"yet-another-stars-rating/most-active-users"===e&&(t.className="yasr-active-users-block",t.shortCode="yasr_most_active_users",t.hookName="yasr_top_visitor_setting"),"yet-another-stars-rating/most-active-reviewers"===e&&(t.className="yasr-reviewers-block",t.shortCode="yasr_top_reviewers",t.hookName="yasr_top_reviewers_setting"),"yet-another-stars-rating/user-rate-history"===e&&(t.className="yasr-user-rate-history",t.shortCode="yasr_user_rate_history",t.panelSettings=!1),t},b=function(){var e=a("To be able to customize this ranking, you need","yet-another-stars-rating"),t=a("You can buy the plugin, including support, updates and upgrades, on","yet-another-stars-rating");return React.createElement("h3",null,e," ",React.createElement("a",{href:"admin/js/src/guten/yasrGutenUtils?utm_source=wp-plugin&utm_medium=gutenberg_panel&utm_campaign=yasr_editor_screen&utm_content=rankings#yasr-pro"},"Yasr Pro."),React.createElement("br",null),t," ",React.createElement("a",{href:"admin/js/src/guten/yasrGutenUtils?utm_source=wp-plugin&utm_medium=gutenberg_panel&utm_campaign=yasr_editor_screen&utm_content=rankings"},"yetanotherstarsrating.com"))},R=function(){return React.createElement("div",null,React.createElement(b,null))}},968:(e,t,r)=>{const a=JSON.parse('{"title":"Yasr: Overall Rating","$schema":"https://schemas.wp.org/trunk/block.json","apiVersion":2,"name":"yet-another-stars-rating/overall-rating","description":"Insert the author rating.","icon":"star-half","category":"yet-another-stars-rating","example":{},"keywords":["rating","author","overall"],"attributes":{"size":{"type":"string","default":"large"},"postId":{"type":"string","default":false}},"supports":{"align":["left","center","right"]},"editorScript":"yasr-shortcodes-blocks"}'),s=JSON.parse('{"title":"Yasr: Visitor Votes","$schema":"https://schemas.wp.org/trunk/block.json","apiVersion":2,"name":"yet-another-stars-rating/visitor-votes","description":"Insert the ability for your visitors to vote.","icon":"star-half","category":"yet-another-stars-rating","example":{},"keywords":["rating","visitor","votes","stars"],"attributes":{"size":{"type":"string","default":"large"},"postId":{"type":"string","default":false}},"supports":{"align":["left","center","right"]},"editorScript":"yasr-shortcodes-blocks"}'),n=JSON.parse('{"title":"Yasr: Most Active Visitors","$schema":"https://schemas.wp.org/trunk/block.json","apiVersion":2,"name":"yet-another-stars-rating/most-active-users","description":"This ranking shows the most active users, displaying the login name if logged in or \\"Anonymous\\" if not","icon":"star-half","category":"yet-another-stars-rating","example":{},"keywords":["ranking","highest","most","chart","visitors"],"supports":{"align":["left","center","right"]},"editorScript":"yasr-shortcodes-blocks"}'),o=JSON.parse('{"title":"Yasr: Most Active Authors","$schema":"https://schemas.wp.org/trunk/block.json","apiVersion":2,"name":"yet-another-stars-rating/most-active-reviewers","description":"This ranking shows the most active reviewers on your site.","icon":"star-half","category":"yet-another-stars-rating","example":{},"keywords":["ranking","highest","most","chart","authors"],"supports":{"align":["left","center","right"]},"editorScript":"yasr-shortcodes-blocks"}'),i=JSON.parse('{"title":"Yasr: User Rate History","$schema":"https://schemas.wp.org/trunk/block.json","apiVersion":2,"name":"yet-another-stars-rating/user-rate-history","description":"If user is logged in, this shortcode shows a list of all the ratings provided by the user on [yasr_visitor_votes] shortcode","icon":"star-half","category":"yet-another-stars-rating","example":{},"keywords":["ratings","list","history"],"supports":{"align":["left","center","right"]},"editorScript":"yasr-shortcodes-blocks"}'),l=JSON.parse('{"title":"Yasr: Ranking by overall rating","$schema":"https://schemas.wp.org/trunk/block.json","apiVersion":2,"name":"yet-another-stars-rating/overall-rating-ranking","description":"This ranking shows the highest rated posts rated through the overall_rating shortcode.","icon":"star-half","category":"yet-another-stars-rating","example":{},"keywords":["ranking","highest","chart"],"supports":{"align":["left","center","right"]},"editorScript":"yasr-shortcodes-blocks"}'),c=JSON.parse('{"title":"Yasr: Ranking by visitors votes","$schema":"https://schemas.wp.org/trunk/block.json","apiVersion":2,"name":"yet-another-stars-rating/visitor-votes-ranking","description":"This ranking shows both the highest and most rated posts rated through the yasr_visitor_votes shortcode. For an item to appear in this chart, it has to be rated at least twice.","icon":"star-half","category":"yet-another-stars-rating","example":{},"keywords":["ranking","highest","most","chart"],"supports":{"align":["left","center","right"]},"editorScript":"yasr-shortcodes-blocks"}');var u=r(434),h=wp.components.PanelBody,g=wp.blockEditor.InspectorControls,m=function(e){var t=e.block,r=e.hookName,a=e.sizeAndId,s=(0,u.rl)(t),n=s.overallRating,o=s.panelSettings,i=s.bottomDesc,l=React.createElement(React.Fragment,null);return!1!==r&&(l=[React.createElement(u.pP,{key:0})],wp.hooks.doAction(r,l)),!0!==o&&!1===r?React.createElement(React.Fragment,null):React.createElement(g,null,n&&React.createElement(u.Sg,null),React.createElement(h,{title:"Settings"},l,o&&a&&React.createElement(p,e),React.createElement("div",{className:"yasr-guten-block-panel"},i)))},p=function(e){var t=e.block,r=e.setAttributes,a={size:t,postId:e.postId,setAttributes:r};return React.createElement(React.Fragment,null,React.createElement("h3",null,u.pD),React.createElement(d,a),React.createElement(y,a))},d=function(e){return React.createElement("div",{className:"yasr-guten-block-panel"},React.createElement("label",null,u.lN),React.createElement("div",null,React.createElement(u.aF,{size:e.size,setAttributes:e.setAttributes})))},y=function(e){return React.createElement("div",{className:"yasr-guten-block-panel"},React.createElement("label",null,"Post ID"),React.createElement(u.yG,{postId:e.postId,setAttributes:e.setAttributes}),React.createElement("div",{className:"yasr-guten-block-explain"},"Use return (↵) to save."),React.createElement("p",null,u.Mp))},v=wp.element.Fragment,k=wp.blockEditor.useBlockProps;const b=function(e){var t=e.attributes,r=t.size,a=t.postId,s=e.name,n=e.isSelected,o=e.setAttributes,i=(0,u.rl)(s),l=i.className,c=i.shortCode,h=i.hookName,g=i.sizeAndId,p={block:s,size:r,postId:a,setAttributes:o,hookName:h,sizeAndId:g},d=k({className:l,name:s}),y=(0,u.YD)(r,"edit"),b=(0,u.rn)(a);return React.createElement(v,null,n&&React.createElement(m,p),React.createElement("div",d,"[",c,y,b,"]",n&&g&&React.createElement(u.aF,{size:r,setAttributes:o})))};var R=wp.blockEditor.useBlockProps;for(var f=wp.blocks.registerBlockType,_={overallRating:a,visitorVotes:s,mostActiveUsers:n,topReviewers:o,userRateHistory:i,ovRanking:l,vvRanking:c},E=function(){var e=N[w];f(_[e],{edit:b,save:function(t){return function(e,t){var r=e.attributes,a=r.size,s=r.postId,n=t.name,o=(0,u.rl)(n),i=o.className,l=o.shortCode,c=R.save({className:i}),h=(0,u.YD)(a,"save"),g=(0,u.rn)(s);return React.createElement("div",c,"[",l,h,g,"]")}(t,_[e])}})},w=0,N=Object.keys(_);w<N.length;w++)E()}},t={};function r(a){var s=t[a];if(void 0!==s)return s.exports;var n=t[a]={exports:{}};return e[a](n,n.exports,r),n.exports}r.d=(e,t)=>{for(var a in t)r.o(t,a)&&!r.o(e,a)&&Object.defineProperty(e,a,{enumerable:!0,get:t[a]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r(434),r(968)})();
1
+ (()=>{"use strict";var e={434:(e,t,r)=>{r.d(t,{Mp:()=>u,Sg:()=>d,YD:()=>y,aF:()=>p,lN:()=>n,pD:()=>s,pP:()=>R,rl:()=>k,rn:()=>v,yG:()=>m});var a=wp.i18n.__,s=a("All these settings are optional","yet-another-stars-rating"),n=a("Size","yet-another-stars-rating"),o=a("Stars size","yet-another-stars-rating"),i=a("Small","yet-another-stars-rating"),l=a("Medium","yet-another-stars-rating"),c=a("Large","yet-another-stars-rating"),u=a("Leave this blank if you don't know what you're doing.","yet-another-stars-rating"),h=a("Remember: only the post author can rate here.","yet-another-stars-rating"),g=a("This is the star set where your users will be able to vote","yet-another-stars-rating"),m=function(e){var t;!1!==e.postId&&(t=e.postId);return React.createElement("div",null,React.createElement("input",{type:"text",size:"4",defaultValue:t,onKeyPress:function(t){return function(e,t){if("Enter"===t.key){var r=t.target.value;!0!==/^\d+$/.test(r)&&""!==r||e({postId:r}),t.preventDefault()}}(e.setAttributes,t)}}))},p=function(e){return React.createElement("form",null,React.createElement("select",{value:e.size,onChange:function(t){return(0,e.setAttributes)({size:(r=t).target.querySelector("option:checked").value}),void r.preventDefault();var r}},React.createElement("option",{value:"--"},o," "),React.createElement("option",{value:"small"},i," "),React.createElement("option",{value:"medium"},l),React.createElement("option",{value:"large"},c," ")))},d=function(e){if(!0===JSON.parse(yasrConstantGutenberg.isFseElement))return React.createElement("div",{className:"yasr-guten-block-panel yasr-guten-block-panel-center"},React.createElement("div",null,a("This is a template file, you can't rate here. You need to insert the rating inside the single post or page","yet-another-stars-rating")),React.createElement("br",null));if(null===wp.data.select("core/editor"))return React.createElement(React.Fragment,null);var t=a("Rate this article / item","yet-another-stars-rating"),r=wp.data.select("core/editor").getCurrentPost().meta.yasr_overall_rating,s=function(e,t){e=e.toFixed(1),e=parseFloat(e),wp.data.dispatch("core/editor").editPost({meta:{yasr_overall_rating:e}}),this.setRating(e),t()};return React.createElement("div",{className:"yasr-guten-block-panel yasr-guten-block-panel-center"},t,React.createElement("div",{id:"overall-rater",ref:function(){return yasrSetRaterValue(32,"overall-rater",!1,.1,!1,r,s)}}))},y=function(e,t){var r=null;return"edit"===t?("small"!==e&&"medium"!==e||(r=' size="'.concat(e,'"')),r):("small"!==e&&"medium"!==e&&"large"!==e||(r=' size="'.concat(e,'"')),r)},v=function(e){var t=null;return!0===/^\d+$/.test(e)&&(t=' postid="'.concat(e,'"')),t},k=function(e){var t={className:null,shortCode:null,overallRating:!1,hookName:!1,panelSettings:!0,sizeAndId:!1};return"yet-another-stars-rating/overall-rating"===e&&(t.overallRating=!0,t.className="yasr-overall-block",t.shortCode="yasr_overall_rating",t.bottomDesc=h,t.sizeAndId=!0),"yet-another-stars-rating/visitor-votes"===e&&(t.className="yasr-vv-block",t.shortCode="yasr_visitor_votes",t.bottomDesc=g,t.sizeAndId=!0),"yet-another-stars-rating/overall-rating-ranking"===e&&(t.className="yasr-ov-ranking-block",t.shortCode="yasr_ov_ranking",t.hookName="yasr_overall_rating_rankings"),"yet-another-stars-rating/visitor-votes-ranking"===e&&(t.className="yasr-vv-ranking-block",t.shortCode="yasr_most_or_highest_rated_posts",t.hookName="yasr_visitor_votes_rankings"),"yet-another-stars-rating/most-active-users"===e&&(t.className="yasr-active-users-block",t.shortCode="yasr_most_active_users",t.hookName="yasr_top_visitor_setting"),"yet-another-stars-rating/most-active-reviewers"===e&&(t.className="yasr-reviewers-block",t.shortCode="yasr_top_reviewers",t.hookName="yasr_top_reviewers_setting"),"yet-another-stars-rating/user-rate-history"===e&&(t.className="yasr-user-rate-history",t.shortCode="yasr_user_rate_history",t.panelSettings=!1),t},b=function(){var e=a("To be able to customize this ranking, you need","yet-another-stars-rating"),t=a("You can buy the plugin, including support, updates and upgrades, on","yet-another-stars-rating");return React.createElement("h3",null,e," ",React.createElement("a",{href:"https://yetanotherstarsrating.com/?utm_source=wp-plugin&utm_medium=gutenberg_panel&utm_campaign=yasr_editor_screen&utm_content=rankings#yasr-pro"},"Yasr Pro."),React.createElement("br",null),t," ",React.createElement("a",{href:"https://yetanotherstarsrating.com/?utm_source=wp-plugin&utm_medium=gutenberg_panel&utm_campaign=yasr_editor_screen&utm_content=rankings"},"yetanotherstarsrating.com"))},R=function(){return React.createElement("div",null,React.createElement(b,null))}},968:(e,t,r)=>{const a=JSON.parse('{"title":"Yasr: Overall Rating","$schema":"https://schemas.wp.org/trunk/block.json","apiVersion":2,"name":"yet-another-stars-rating/overall-rating","description":"Insert the author rating.","icon":"star-half","category":"yet-another-stars-rating","example":{},"keywords":["rating","author","overall"],"attributes":{"size":{"type":"string","default":"large"},"postId":{"type":"string","default":false}},"supports":{"align":["left","center","right"]},"editorScript":"yasr-shortcodes-blocks"}'),s=JSON.parse('{"title":"Yasr: Visitor Votes","$schema":"https://schemas.wp.org/trunk/block.json","apiVersion":2,"name":"yet-another-stars-rating/visitor-votes","description":"Insert the ability for your visitors to vote.","icon":"star-half","category":"yet-another-stars-rating","example":{},"keywords":["rating","visitor","votes","stars"],"attributes":{"size":{"type":"string","default":"large"},"postId":{"type":"string","default":false}},"supports":{"align":["left","center","right"]},"editorScript":"yasr-shortcodes-blocks"}'),n=JSON.parse('{"title":"Yasr: Most Active Visitors","$schema":"https://schemas.wp.org/trunk/block.json","apiVersion":2,"name":"yet-another-stars-rating/most-active-users","description":"This ranking shows the most active users, displaying the login name if logged in or \\"Anonymous\\" if not","icon":"star-half","category":"yet-another-stars-rating","example":{},"keywords":["ranking","highest","most","chart","visitors"],"supports":{"align":["left","center","right"]},"editorScript":"yasr-shortcodes-blocks"}'),o=JSON.parse('{"title":"Yasr: Most Active Authors","$schema":"https://schemas.wp.org/trunk/block.json","apiVersion":2,"name":"yet-another-stars-rating/most-active-reviewers","description":"This ranking shows the most active reviewers on your site.","icon":"star-half","category":"yet-another-stars-rating","example":{},"keywords":["ranking","highest","most","chart","authors"],"supports":{"align":["left","center","right"]},"editorScript":"yasr-shortcodes-blocks"}'),i=JSON.parse('{"title":"Yasr: User Rate History","$schema":"https://schemas.wp.org/trunk/block.json","apiVersion":2,"name":"yet-another-stars-rating/user-rate-history","description":"If user is logged in, this shortcode shows a list of all the ratings provided by the user on [yasr_visitor_votes] shortcode","icon":"star-half","category":"yet-another-stars-rating","example":{},"keywords":["ratings","list","history"],"supports":{"align":["left","center","right"]},"editorScript":"yasr-shortcodes-blocks"}'),l=JSON.parse('{"title":"Yasr: Ranking by overall rating","$schema":"https://schemas.wp.org/trunk/block.json","apiVersion":2,"name":"yet-another-stars-rating/overall-rating-ranking","description":"This ranking shows the highest rated posts rated through the overall_rating shortcode.","icon":"star-half","category":"yet-another-stars-rating","example":{},"keywords":["ranking","highest","chart"],"supports":{"align":["left","center","right"]},"editorScript":"yasr-shortcodes-blocks"}'),c=JSON.parse('{"title":"Yasr: Ranking by visitors votes","$schema":"https://schemas.wp.org/trunk/block.json","apiVersion":2,"name":"yet-another-stars-rating/visitor-votes-ranking","description":"This ranking shows both the highest and most rated posts rated through the yasr_visitor_votes shortcode. For an item to appear in this chart, it has to be rated at least twice.","icon":"star-half","category":"yet-another-stars-rating","example":{},"keywords":["ranking","highest","most","chart"],"supports":{"align":["left","center","right"]},"editorScript":"yasr-shortcodes-blocks"}');var u=r(434),h=wp.components.PanelBody,g=wp.blockEditor.InspectorControls,m=function(e){var t=e.block,r=e.hookName,a=e.sizeAndId,s=(0,u.rl)(t),n=s.overallRating,o=s.panelSettings,i=s.bottomDesc,l=React.createElement(React.Fragment,null);return!1!==r&&(l=[React.createElement(u.pP,{key:0})],wp.hooks.doAction(r,l)),!0!==o&&!1===r?React.createElement(React.Fragment,null):React.createElement(g,null,n&&React.createElement(u.Sg,null),React.createElement(h,{title:"Settings"},l,o&&a&&React.createElement(p,e),React.createElement("div",{className:"yasr-guten-block-panel"},i)))},p=function(e){var t=e.block,r=e.setAttributes,a={size:t,postId:e.postId,setAttributes:r};return React.createElement(React.Fragment,null,React.createElement("h3",null,u.pD),React.createElement(d,a),React.createElement(y,a))},d=function(e){return React.createElement("div",{className:"yasr-guten-block-panel"},React.createElement("label",null,u.lN),React.createElement("div",null,React.createElement(u.aF,{size:e.size,setAttributes:e.setAttributes})))},y=function(e){return React.createElement("div",{className:"yasr-guten-block-panel"},React.createElement("label",null,"Post ID"),React.createElement(u.yG,{postId:e.postId,setAttributes:e.setAttributes}),React.createElement("div",{className:"yasr-guten-block-explain"},"Use return (↵) to save."),React.createElement("p",null,u.Mp))},v=wp.element.Fragment,k=wp.blockEditor.useBlockProps;const b=function(e){var t=e.attributes,r=t.size,a=t.postId,s=e.name,n=e.isSelected,o=e.setAttributes,i=(0,u.rl)(s),l=i.className,c=i.shortCode,h=i.hookName,g=i.sizeAndId,p={block:s,size:r,postId:a,setAttributes:o,hookName:h,sizeAndId:g},d=k({className:l,name:s}),y=(0,u.YD)(r,"edit"),b=(0,u.rn)(a);return React.createElement(v,null,n&&React.createElement(m,p),React.createElement("div",d,"[",c,y,b,"]",n&&g&&React.createElement(u.aF,{size:r,setAttributes:o})))};var R=wp.blockEditor.useBlockProps;const f=function(e,t){var r=e.attributes,a=r.size,s=r.postId,n=t.name,o=(0,u.rl)(n),i=o.className,l=o.shortCode,c=R.save({className:i}),h=(0,u.YD)(a,"save"),g=(0,u.rn)(s);return React.createElement("div",c,"[",l,h,g,"]")};for(var _=wp.blocks.registerBlockType,E={overallRating:a,visitorVotes:s,mostActiveUsers:n,topReviewers:o,userRateHistory:i,ovRanking:l,vvRanking:c},w=function(){var e=S[N];_(E[e],{edit:b,save:function(t){return f(t,E[e])}})},N=0,S=Object.keys(E);N<S.length;N++)w()}},t={};function r(a){var s=t[a];if(void 0!==s)return s.exports;var n=t[a]={exports:{}};return e[a](n,n.exports,r),n.exports}r.d=(e,t)=>{for(var a in t)r.o(t,a)&&!r.o(e,a)&&Object.defineProperty(e,a,{enumerable:!0,get:t[a]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r(434);r(968)})();
admin/js/guten/yasr-guten-misc.js CHANGED
@@ -1 +1 @@
1
- (()=>{var e={434:(e,t,a)=>{"use strict";a.d(t,{Sg:()=>n});var r=wp.i18n.__,n=(r("All these settings are optional","yet-another-stars-rating"),r("Size","yet-another-stars-rating"),r("Stars size","yet-another-stars-rating"),r("Small","yet-another-stars-rating"),r("Medium","yet-another-stars-rating"),r("Large","yet-another-stars-rating"),r("Leave this blank if you don't know what you're doing.","yet-another-stars-rating"),r("Remember: only the post author can rate here.","yet-another-stars-rating"),r("This is the star set where your users will be able to vote","yet-another-stars-rating"),function(e){if(!0===JSON.parse(yasrConstantGutenberg.isFseElement))return React.createElement("div",{className:"yasr-guten-block-panel yasr-guten-block-panel-center"},React.createElement("div",null,r("This is a template file, you can't rate here. You need to insert the rating inside the single post or page","yet-another-stars-rating")),React.createElement("br",null));if(null===wp.data.select("core/editor"))return React.createElement(React.Fragment,null);var t=r("Rate this article / item","yet-another-stars-rating"),a=wp.data.select("core/editor").getCurrentPost().meta.yasr_overall_rating,n=function(e,t){e=e.toFixed(1),e=parseFloat(e),wp.data.dispatch("core/editor").editPost({meta:{yasr_overall_rating:e}}),this.setRating(e),t()};return React.createElement("div",{className:"yasr-guten-block-panel yasr-guten-block-panel-center"},t,React.createElement("div",{id:"overall-rater",ref:function(){return yasrSetRaterValue(32,"overall-rater",!1,.1,!1,a,n)}}))})}},t={};function a(r){var n=t[r];if(void 0!==n)return n.exports;var l=t[r]={exports:{}};return e[r](l,l.exports,a),l.exports}a.d=(e,t)=>{for(var r in t)a.o(t,r)&&!a.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},a.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),(()=>{function e(t){return e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},e(t)}function t(e,t){for(var a=0;a<t.length;a++){var r=t[a];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function a(e,t){return a=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},a(e,t)}function r(t,a){if(a&&("object"===e(a)||"function"==typeof a))return a;if(void 0!==a)throw new TypeError("Derived constructors may only return object or undefined");return function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(t)}function n(e){return n=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},n(e)}var l=wp.i18n.__,s=wp.blocks.registerBlockType,o=wp.components,i=o.PanelBody,c=(o.PanelRow,wp.element.Fragment),u=wp.editor,y=(u.BlockControls,u.InspectorControls),p=l("All these settings are optional","yet-another-stars-rating"),m=l("Choose Size","yet-another-stars-rating"),d=l("Choose stars size","yet-another-stars-rating"),g=l("Small","yet-another-stars-rating"),h=l("Medium","yet-another-stars-rating"),f=l("Large","yet-another-stars-rating"),v=l("Leave this blank if you don't know what you're doing.","yet-another-stars-rating"),R=l("Remember: only the post author can rate here.","yet-another-stars-rating"),b=l("This is the star set where your users will be able to vote","yet-another-stars-rating"),E=l('This block is now deprecated. It will still work, but I suggest to replace it with the new one by simply removing it and adding "Yasr Overall Rating" again.',"yet-another-stars-rating"),w=l('This block is now deprecated. It will still work, but I suggest to replace it with the new one by simply removing it and adding "Yasr Visitors Votes" again.',"yet-another-stars-rating");s("yet-another-stars-rating/yasr-overall-rating",{title:l("[DEPRECATED]Yasr: Overall Rating","yet-another-stars-rating"),description:E,icon:"star-half",keywords:[l("rating","yet-another-stars-rating"),l("author","yet-another-stars-rating"),l("overall","yet-another-stars-rating")],attributes:{overallRatingMeta:{type:"number",source:"meta",meta:"yasr_overall_rating"},size:{type:"string",default:"--"},postId:{type:"string",default:"--"}},edit:function(e){var s,o=e.attributes,u=o.overallRatingMeta,b=o.size,E=o.postId,w=e.setAttributes,k=e.isSelected,_=u,P=null,S=null;"--"!==b&&(P=' size="'+b+'"'),s=/^\d+$/.test(E),"--"!==E&&!0===s&&(S=' postid="'+E+'"');var O=function(e){!function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),t&&a(e,t)}(y,React.Component);var s,o,i,c,u=(i=y,c=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}(),function(){var e,t=n(i);if(c){var a=n(this).constructor;e=Reflect.construct(t,arguments,a)}else e=t.apply(this,arguments);return r(this,e)});function y(e){var t;return function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,y),(t=u.call(this,e)).yasrOverallRateThis=l("Rate this article / item","yet-another-stars-rating"),t}return s=y,(o=[{key:"render",value:function(){return React.createElement("div",null,this.yasrOverallRateThis,React.createElement("div",null,React.createElement("div",{id:"overall-rater",ref:function(){return raterJs({starSize:32,step:.1,showToolTip:!1,rating:_,readOnly:!1,element:document.querySelector("#overall-rater"),rateCallback:function(e,t){e=e.toFixed(1),e=parseFloat(e),w({overallRatingMeta:e}),this.setRating(e),t()}})}})))}}])&&t(s.prototype,o),Object.defineProperty(s,"prototype",{writable:!1}),y}();function N(){return React.createElement("form",null,React.createElement("select",{value:b,onChange:z},React.createElement("option",{value:"--"},d),React.createElement("option",{value:"small"},g),React.createElement("option",{value:"medium"},h),React.createElement("option",{value:"large"},f)))}function z(e){var t=e.target.querySelector("option:checked");w({size:t.value}),e.preventDefault()}function C(){return React.createElement("div",null,React.createElement("input",{type:"text",size:"4",onKeyPress:I}))}function I(e){if("Enter"===e.key){var t=e.target.value;!0!==/^\d+$/.test(t)&&""!==t||w({postId:t}),e.preventDefault()}}function T(e){return React.createElement(y,null,React.createElement("div",{class:"yasr-guten-block-panel yasr-guten-block-panel-center"},React.createElement(O,null)),React.createElement(i,{title:"Settings"},React.createElement("h3",null,p),React.createElement("div",{className:"yasr-guten-block-panel"},React.createElement("label",null,m),React.createElement("div",null,React.createElement(N,null))),React.createElement("div",{className:"yasr-guten-block-panel"},React.createElement("label",null,"Post ID"),React.createElement(C,null),React.createElement("div",{className:"yasr-guten-block-explain"},v)),React.createElement("div",{className:"yasr-guten-block-panel"},R)))}return React.createElement(c,null,React.createElement(T,null),React.createElement("div",{className:e.className},"[yasr_overall_rating",P,S,"]",k&&React.createElement(N,null)))},save:function(e){var t=e.attributes,a=t.size,r=t.postId,n="",l=r;return a&&(n+='size="'+a+'"'),r&&("--"===r&&(l=wp.data.select("core/editor").getCurrentPostId()),n+=' postid="'+l+'"'),React.createElement("div",null,"[yasr_overall_rating ",n,"]")}}),s("yet-another-stars-rating/yasr-visitor-votes",{title:l("[DEPRECATED]Yasr: Visitor Votes","yet-another-stars-rating"),description:w,icon:"star-half",keywords:[l("rating","yet-another-stars-rating"),l("visitor","yet-another-stars-rating"),l("votes","yet-another-stars-rating")],attributes:{size:{type:"string",default:"--"},postId:{type:"string",default:"--"}},edit:function(e){var t,a=e.attributes,r=a.size,n=a.postId,l=e.setAttributes,s=e.isSelected,o=null,u=null;function R(){return React.createElement("form",null,React.createElement("select",{value:r,onChange:E},React.createElement("option",{value:"--"},d),React.createElement("option",{value:"small"},g),React.createElement("option",{value:"medium"},h),React.createElement("option",{value:"large"},f)))}function E(e){var t=e.target.querySelector("option:checked");l({size:t.value}),e.preventDefault()}function w(){return React.createElement("div",null,React.createElement("input",{type:"text",size:"4",onKeyPress:k}))}function k(e){if("Enter"===e.key){var t=e.target.value;!0!==/^\d+$/.test(t)&&""!==t||l({postId:t}),e.preventDefault()}}function _(e){return React.createElement(y,null,React.createElement(i,{title:"Settings"},React.createElement("h3",null,p),React.createElement("div",{className:"yasr-guten-block-panel"},React.createElement("label",null,m),React.createElement("div",null,React.createElement(R,null))),React.createElement("div",{className:"yasr-guten-block-panel"},React.createElement("label",null,"Post ID"),React.createElement(w,null),React.createElement("div",{className:"yasr-guten-block-explain"},v)),React.createElement("div",{className:"yasr-guten-block-panel"},b)))}return t=/^\d+$/.test(n),"--"!==r&&(o=' size="'+r+'"'),"--"!==n&&!0===t&&(u=' postid="'+n+'"'),React.createElement(c,null,React.createElement(_,null),React.createElement("div",{className:e.className},"[yasr_visitor_votes",o,u,"]",s&&React.createElement(R,null)))},save:function(e){var t=e.attributes,a=t.size,r=t.postId,n="",l=r;return a&&(n+='size="'+a+'"'),r&&("--"===r&&(l=wp.data.select("core/editor").getCurrentPostId()),n+=' postid="'+l+'"'),React.createElement("div",null,"[yasr_visitor_votes ",n,"]")}}),wp.data.dispatch("core/edit-post").hideBlockTypes("yet-another-stars-rating/yasr-overall-rating"),wp.data.dispatch("core/edit-post").hideBlockTypes("yet-another-stars-rating/yasr-visitor-votes")})(),(()=>{"use strict";var e=a(434),t=wp.i18n.__,r=wp.plugins.registerPlugin,n=wp.editPost,l=n.PluginSidebar,s=n.PluginSidebarMoreMenuItem,o=wp.components.PanelBody,i=wp.element.Fragment,c=function(){return React.createElement("div",null)},u=function(e){if("disabled"===yasrConstantGutenberg.autoInsert)return React.createElement(React.Fragment,null);var a=wp.data.select("core/editor").getCurrentPost().meta.yasr_auto_insert_disabled,r=!1;return"yes"===a&&(r=!0),React.createElement("div",{className:"yasr-guten-block-panel-center"},React.createElement("hr",null),React.createElement("label",null,React.createElement("span",null,t("Disable auto insert for this post or page?","yet-another-stars-rating"))),React.createElement("div",{className:"yasr-onoffswitch-big yasr-onoffswitch-big-center",id:"yasr-switcher-disable-auto-insert"},React.createElement("input",{type:"checkbox",name:"yasr_auto_insert_disabled",className:"yasr-onoffswitch-checkbox",value:"yes",id:"yasr-auto-insert-disabled-switch",defaultChecked:r,onChange:function(e){var t=e.target,a="yes";!0!==(r="checkbox"===t.type?t.checked:t.value)&&(a="no"),wp.data.dispatch("core/editor").editPost({meta:{yasr_auto_insert_disabled:a}})}}),React.createElement("label",{className:"yasr-onoffswitch-label",htmlFor:"yasr-auto-insert-disabled-switch"},React.createElement("span",{className:"yasr-onoffswitch-inner"}),React.createElement("span",{className:"yasr-onoffswitch-switch"}))))};r("yasr-sidebar",{icon:"star-half",title:t("Yasr: Settings","yet-another-stars-rating"),render:function(){var a=[React.createElement(c,{key:0})];return wp.hooks.doAction("yasr_below_panel",a),React.createElement(i,null,React.createElement(s,{target:"yasr-guten-sidebar"},t("YASR post settings","yet-another-stars-rating")),React.createElement(l,{name:"yasr-guten-sidebar",title:"YASR Settings"},React.createElement(o,null,React.createElement("div",{className:"yasr-guten-block-panel yasr-guten-block-panel-center"},React.createElement(e.Sg,null),React.createElement("div",null,t('This is the same value that you find the "Yasr: Overall Rating" block.',"yet-another-stars-rating")),React.createElement(u,null),a))))}})})()})();
1
+ (()=>{var e={434:(e,t,a)=>{"use strict";a.d(t,{Sg:()=>n});var r=wp.i18n.__,n=(r("All these settings are optional","yet-another-stars-rating"),r("Size","yet-another-stars-rating"),r("Stars size","yet-another-stars-rating"),r("Small","yet-another-stars-rating"),r("Medium","yet-another-stars-rating"),r("Large","yet-another-stars-rating"),r("Leave this blank if you don't know what you're doing.","yet-another-stars-rating"),r("Remember: only the post author can rate here.","yet-another-stars-rating"),r("This is the star set where your users will be able to vote","yet-another-stars-rating"),function(e){if(!0===JSON.parse(yasrConstantGutenberg.isFseElement))return React.createElement("div",{className:"yasr-guten-block-panel yasr-guten-block-panel-center"},React.createElement("div",null,r("This is a template file, you can't rate here. You need to insert the rating inside the single post or page","yet-another-stars-rating")),React.createElement("br",null));if(null===wp.data.select("core/editor"))return React.createElement(React.Fragment,null);var t=r("Rate this article / item","yet-another-stars-rating"),a=wp.data.select("core/editor").getCurrentPost().meta.yasr_overall_rating,n=function(e,t){e=e.toFixed(1),e=parseFloat(e),wp.data.dispatch("core/editor").editPost({meta:{yasr_overall_rating:e}}),this.setRating(e),t()};return React.createElement("div",{className:"yasr-guten-block-panel yasr-guten-block-panel-center"},t,React.createElement("div",{id:"overall-rater",ref:function(){return yasrSetRaterValue(32,"overall-rater",!1,.1,!1,a,n)}}))})}},t={};function a(r){var n=t[r];if(void 0!==n)return n.exports;var l=t[r]={exports:{}};return e[r](l,l.exports,a),l.exports}a.d=(e,t)=>{for(var r in t)a.o(t,r)&&!a.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},a.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),(()=>{function e(t){return e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},e(t)}function t(e,t){for(var a=0;a<t.length;a++){var r=t[a];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function a(e,t){return a=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},a(e,t)}function r(e){var t=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}();return function(){var a,r=l(e);if(t){var s=l(this).constructor;a=Reflect.construct(r,arguments,s)}else a=r.apply(this,arguments);return n(this,a)}}function n(t,a){if(a&&("object"===e(a)||"function"==typeof a))return a;if(void 0!==a)throw new TypeError("Derived constructors may only return object or undefined");return function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(t)}function l(e){return l=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},l(e)}var s=wp.i18n.__,o=wp.blocks.registerBlockType,i=wp.components,c=i.PanelBody,u=(i.PanelRow,wp.element.Fragment),y=wp.editor,p=(y.BlockControls,y.InspectorControls),d=s("All these settings are optional","yet-another-stars-rating"),m=s("Choose Size","yet-another-stars-rating"),g=s("Choose stars size","yet-another-stars-rating"),h=s("Small","yet-another-stars-rating"),f=s("Medium","yet-another-stars-rating"),v=s("Large","yet-another-stars-rating"),R=s("Leave this blank if you don't know what you're doing.","yet-another-stars-rating"),b=s("Remember: only the post author can rate here.","yet-another-stars-rating"),E=s("This is the star set where your users will be able to vote","yet-another-stars-rating"),w=s('This block is now deprecated. It will still work, but I suggest to replace it with the new one by simply removing it and adding "Yasr Overall Rating" again.',"yet-another-stars-rating"),k=s('This block is now deprecated. It will still work, but I suggest to replace it with the new one by simply removing it and adding "Yasr Visitors Votes" again.',"yet-another-stars-rating");o("yet-another-stars-rating/yasr-overall-rating",{title:s("[DEPRECATED]Yasr: Overall Rating","yet-another-stars-rating"),description:w,icon:"star-half",keywords:[s("rating","yet-another-stars-rating"),s("author","yet-another-stars-rating"),s("overall","yet-another-stars-rating")],attributes:{overallRatingMeta:{type:"number",source:"meta",meta:"yasr_overall_rating"},size:{type:"string",default:"--"},postId:{type:"string",default:"--"}},edit:function(e){var n,l=e.attributes,o=l.overallRatingMeta,i=l.size,y=l.postId,E=e.setAttributes,w=e.isSelected,k=o,_=null,P=null;"--"!==i&&(_=' size="'+i+'"'),n=/^\d+$/.test(y),"--"!==y&&!0===n&&(P=' postid="'+y+'"');var S=function(e){!function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),t&&a(e,t)}(c,React.Component);var n,l,o,i=r(c);function c(e){var t;return function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,c),(t=i.call(this,e)).yasrOverallRateThis=s("Rate this article / item","yet-another-stars-rating"),t}return n=c,(l=[{key:"render",value:function(){return React.createElement("div",null,this.yasrOverallRateThis,React.createElement("div",null,React.createElement("div",{id:"overall-rater",ref:function(){return raterJs({starSize:32,step:.1,showToolTip:!1,rating:k,readOnly:!1,element:document.querySelector("#overall-rater"),rateCallback:function(e,t){e=e.toFixed(1),e=parseFloat(e),E({overallRatingMeta:e}),this.setRating(e),t()}})}})))}}])&&t(n.prototype,l),o&&t(n,o),Object.defineProperty(n,"prototype",{writable:!1}),c}();function O(){return React.createElement("form",null,React.createElement("select",{value:i,onChange:N},React.createElement("option",{value:"--"},g),React.createElement("option",{value:"small"},h),React.createElement("option",{value:"medium"},f),React.createElement("option",{value:"large"},v)))}function N(e){var t=e.target.querySelector("option:checked");E({size:t.value}),e.preventDefault()}function z(){return React.createElement("div",null,React.createElement("input",{type:"text",size:"4",onKeyPress:C}))}function C(e){if("Enter"===e.key){var t=e.target.value;!0!==/^\d+$/.test(t)&&""!==t||E({postId:t}),e.preventDefault()}}function I(e){return React.createElement(p,null,React.createElement("div",{class:"yasr-guten-block-panel yasr-guten-block-panel-center"},React.createElement(S,null)),React.createElement(c,{title:"Settings"},React.createElement("h3",null,d),React.createElement("div",{className:"yasr-guten-block-panel"},React.createElement("label",null,m),React.createElement("div",null,React.createElement(O,null))),React.createElement("div",{className:"yasr-guten-block-panel"},React.createElement("label",null,"Post ID"),React.createElement(z,null),React.createElement("div",{className:"yasr-guten-block-explain"},R)),React.createElement("div",{className:"yasr-guten-block-panel"},b)))}return React.createElement(u,null,React.createElement(I,null),React.createElement("div",{className:e.className},"[yasr_overall_rating",_,P,"]",w&&React.createElement(O,null)))},save:function(e){var t=e.attributes,a=t.size,r=t.postId,n="",l=r;return a&&(n+='size="'+a+'"'),r&&("--"===r&&(l=wp.data.select("core/editor").getCurrentPostId()),n+=' postid="'+l+'"'),React.createElement("div",null,"[yasr_overall_rating ",n,"]")}}),o("yet-another-stars-rating/yasr-visitor-votes",{title:s("[DEPRECATED]Yasr: Visitor Votes","yet-another-stars-rating"),description:k,icon:"star-half",keywords:[s("rating","yet-another-stars-rating"),s("visitor","yet-another-stars-rating"),s("votes","yet-another-stars-rating")],attributes:{size:{type:"string",default:"--"},postId:{type:"string",default:"--"}},edit:function(e){var t,a=e.attributes,r=a.size,n=a.postId,l=e.setAttributes,s=e.isSelected,o=null,i=null;function y(){return React.createElement("form",null,React.createElement("select",{value:r,onChange:b},React.createElement("option",{value:"--"},g),React.createElement("option",{value:"small"},h),React.createElement("option",{value:"medium"},f),React.createElement("option",{value:"large"},v)))}function b(e){var t=e.target.querySelector("option:checked");l({size:t.value}),e.preventDefault()}function w(){return React.createElement("div",null,React.createElement("input",{type:"text",size:"4",onKeyPress:k}))}function k(e){if("Enter"===e.key){var t=e.target.value;!0!==/^\d+$/.test(t)&&""!==t||l({postId:t}),e.preventDefault()}}function _(e){return React.createElement(p,null,React.createElement(c,{title:"Settings"},React.createElement("h3",null,d),React.createElement("div",{className:"yasr-guten-block-panel"},React.createElement("label",null,m),React.createElement("div",null,React.createElement(y,null))),React.createElement("div",{className:"yasr-guten-block-panel"},React.createElement("label",null,"Post ID"),React.createElement(w,null),React.createElement("div",{className:"yasr-guten-block-explain"},R)),React.createElement("div",{className:"yasr-guten-block-panel"},E)))}return t=/^\d+$/.test(n),"--"!==r&&(o=' size="'+r+'"'),"--"!==n&&!0===t&&(i=' postid="'+n+'"'),React.createElement(u,null,React.createElement(_,null),React.createElement("div",{className:e.className},"[yasr_visitor_votes",o,i,"]",s&&React.createElement(y,null)))},save:function(e){var t=e.attributes,a=t.size,r=t.postId,n="",l=r;return a&&(n+='size="'+a+'"'),r&&("--"===r&&(l=wp.data.select("core/editor").getCurrentPostId()),n+=' postid="'+l+'"'),React.createElement("div",null,"[yasr_visitor_votes ",n,"]")}}),wp.data.dispatch("core/edit-post").hideBlockTypes("yet-another-stars-rating/yasr-overall-rating"),wp.data.dispatch("core/edit-post").hideBlockTypes("yet-another-stars-rating/yasr-visitor-votes")})(),(()=>{"use strict";var e=a(434),t=wp.i18n.__,r=wp.plugins.registerPlugin,n=wp.editPost,l=n.PluginSidebar,s=n.PluginSidebarMoreMenuItem,o=wp.components.PanelBody,i=wp.element.Fragment,c=function(){return React.createElement("div",null)},u=function(e){if("disabled"===yasrConstantGutenberg.autoInsert)return React.createElement(React.Fragment,null);var a=wp.data.select("core/editor").getCurrentPost().meta.yasr_auto_insert_disabled,r=!1;"yes"===a&&(r=!0);return React.createElement("div",{className:"yasr-guten-block-panel-center"},React.createElement("hr",null),React.createElement("label",null,React.createElement("span",null,t("Disable auto insert for this post or page?","yet-another-stars-rating"))),React.createElement("div",{className:"yasr-onoffswitch-big yasr-onoffswitch-big-center",id:"yasr-switcher-disable-auto-insert"},React.createElement("input",{type:"checkbox",name:"yasr_auto_insert_disabled",className:"yasr-onoffswitch-checkbox",value:"yes",id:"yasr-auto-insert-disabled-switch",defaultChecked:r,onChange:function(e){var t=e.target,a="yes";!0!==(r="checkbox"===t.type?t.checked:t.value)&&(a="no"),wp.data.dispatch("core/editor").editPost({meta:{yasr_auto_insert_disabled:a}})}}),React.createElement("label",{className:"yasr-onoffswitch-label",htmlFor:"yasr-auto-insert-disabled-switch"},React.createElement("span",{className:"yasr-onoffswitch-inner"}),React.createElement("span",{className:"yasr-onoffswitch-switch"}))))};r("yasr-sidebar",{icon:"star-half",title:t("Yasr: Settings","yet-another-stars-rating"),render:function(){var a=[React.createElement(c,{key:0})];return wp.hooks.doAction("yasr_below_panel",a),React.createElement(i,null,React.createElement(s,{target:"yasr-guten-sidebar"},t("YASR post settings","yet-another-stars-rating")),React.createElement(l,{name:"yasr-guten-sidebar",title:"YASR Settings"},React.createElement(o,null,React.createElement("div",{className:"yasr-guten-block-panel yasr-guten-block-panel-center"},React.createElement(e.Sg,null),React.createElement("div",null,t('This is the same value that you find the "Yasr: Overall Rating" block.',"yet-another-stars-rating")),React.createElement(u,null),a))))}})})()})();
admin/js/src/guten/registerBlockTypeEdit.js CHANGED
@@ -6,7 +6,7 @@ import {
6
  YasrBlockSizeAttribute,
7
  YasrPrintSelectSize,
8
  YasrSetBlockAttributes
9
- } from "yasrGutenUtils";
10
 
11
  import {YasrBlocksPanel} from "./yasrBlocksPanel";
12
 
6
  YasrBlockSizeAttribute,
7
  YasrPrintSelectSize,
8
  YasrSetBlockAttributes
9
+ } from "./yasrGutenUtils";
10
 
11
  import {YasrBlocksPanel} from "./yasrBlocksPanel";
12
 
admin/js/src/guten/registerBlockTypeSave.js CHANGED
@@ -1,7 +1,7 @@
1
- import {YasrBlockPostidAttribute, YasrBlockSizeAttribute, YasrSetBlockAttributes} from "yasrGutenUtils";
2
-
3
  const {useBlockProps} = wp.blockEditor;
4
 
 
 
5
  /**
6
  * The Save function to use into registerBlockTypeSave
7
  *
 
 
1
  const {useBlockProps} = wp.blockEditor;
2
 
3
+ import {YasrBlockPostidAttribute, YasrBlockSizeAttribute, YasrSetBlockAttributes} from "./yasrGutenUtils";
4
+
5
  /**
6
  * The Save function to use into registerBlockTypeSave
7
  *
admin/js/src/guten/yasrBlocksPanel.js CHANGED
@@ -13,7 +13,7 @@ import {
13
  YasrPrintSelectSize,
14
  YasrNoSettingsPanel,
15
  YasrSetBlockAttributes
16
- } from "yasrGutenUtils";
17
 
18
  /**
19
  * This is the panel that for blocks that use size and postid attributes
13
  YasrPrintSelectSize,
14
  YasrNoSettingsPanel,
15
  YasrSetBlockAttributes
16
+ } from "./yasrGutenUtils";
17
 
18
  /**
19
  * This is the panel that for blocks that use size and postid attributes
admin/js/src/guten/yasrGutenUtils.js CHANGED
@@ -265,12 +265,12 @@ export const YasrProText = () => {
265
  <h3>
266
  {YasrProText1}
267
  &nbsp;
268
- <a href="admin/js/src/guten/yasrGutenUtils?utm_source=wp-plugin&utm_medium=gutenberg_panel&utm_campaign=yasr_editor_screen&utm_content=rankings#yasr-pro">
269
  Yasr Pro.
270
  </a><br />
271
  {YasrProText2}
272
  &nbsp;
273
- <a href="admin/js/src/guten/yasrGutenUtils?utm_source=wp-plugin&utm_medium=gutenberg_panel&utm_campaign=yasr_editor_screen&utm_content=rankings">
274
  yetanotherstarsrating.com
275
  </a>
276
  </h3>
265
  <h3>
266
  {YasrProText1}
267
  &nbsp;
268
+ <a href="https://yetanotherstarsrating.com/?utm_source=wp-plugin&utm_medium=gutenberg_panel&utm_campaign=yasr_editor_screen&utm_content=rankings#yasr-pro">
269
  Yasr Pro.
270
  </a><br />
271
  {YasrProText2}
272
  &nbsp;
273
+ <a href="https://yetanotherstarsrating.com/?utm_source=wp-plugin&utm_medium=gutenberg_panel&utm_campaign=yasr_editor_screen&utm_content=rankings">
274
  yetanotherstarsrating.com
275
  </a>
276
  </h3>
admin/js/src/guten/yasrSidebar.js CHANGED
@@ -12,7 +12,7 @@ const ContentBelowSidebar = () => {
12
  return <div/>;
13
  };
14
 
15
- import {YasrDivRatingOverall} from 'yasrGutenUtils';
16
 
17
  /**
18
  * Auto insert block, will show only if auto insert in setting is enabled
12
  return <div/>;
13
  };
14
 
15
+ import {YasrDivRatingOverall} from './yasrGutenUtils';
16
 
17
  /**
18
  * Auto insert block, will show only if auto insert in setting is enabled
admin/js/yasr-editor-screen.js CHANGED
@@ -1 +1 @@
1
- (()=>{"use strict";var e={607:(e,t,n)=>{n.d(t,{v:()=>o});const o=e=>{const t=document.createElement("textarea");t.value=e,t.setAttribute("readonly",""),t.style.position="absolute",t.style.left="-9999px",document.body.appendChild(t),t.select(),document.execCommand("copy"),document.body.removeChild(t)};tippy(document.querySelectorAll(".yasr-copy-shortcode"),{content:"Copied! Insert into your post!",theme:"yasr",arrow:"true",arrowType:"round",trigger:"click"})}},t={};function n(o){var a=t[o];if(void 0!==a)return a.exports;var i=t[o]={exports:{}};return e[o](i,i.exports,n),i.exports}n.d=(e,t)=>{for(var o in t)n.o(t,o)&&!n.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),(()=>{var e,t=n(607),o=new Uint8Array(16);function a(){if(!e&&!(e="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)||"undefined"!=typeof msCrypto&&"function"==typeof msCrypto.getRandomValues&&msCrypto.getRandomValues.bind(msCrypto)))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return e(o)}const i=/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i,s=function(e){return"string"==typeof e&&i.test(e)};for(var r=[],l=0;l<256;++l)r.push((l+256).toString(16).substr(1));const d=function(e,t,n){var o=(e=e||{}).random||(e.rng||a)();if(o[6]=15&o[6]|64,o[8]=63&o[8]|128,t){n=n||0;for(var i=0;i<16;++i)t[n+i]=o[i];return t}return function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,n=(r[e[t+0]]+r[e[t+1]]+r[e[t+2]]+r[e[t+3]]+"-"+r[e[t+4]]+r[e[t+5]]+"-"+r[e[t+6]]+r[e[t+7]]+"-"+r[e[t+8]]+r[e[t+9]]+"-"+r[e[t+10]]+r[e[t+11]]+r[e[t+12]]+r[e[t+13]]+r[e[t+14]]+r[e[t+15]]).toLowerCase();if(!s(n))throw TypeError("Stringified UUID is invalid");return n}(o)};function y(e){"Product"===e?(document.getElementById("yasr-metabox-info-snippet-container").style.display="",document.getElementById("yasr-metabox-info-snippet-container-product").style.display="",document.getElementById("yasr-metabox-info-snippet-container-localbusiness").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-recipe").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-software").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-book").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-movie").style.display="none"):"LocalBusiness"===e?(document.getElementById("yasr-metabox-info-snippet-container").style.display="",document.getElementById("yasr-metabox-info-snippet-container-localbusiness").style.display="",document.getElementById("yasr-metabox-info-snippet-container-product").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-recipe").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-software").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-book").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-movie").style.display="none"):"Recipe"===e?(document.getElementById("yasr-metabox-info-snippet-container").style.display="",document.getElementById("yasr-metabox-info-snippet-container-recipe").style.display="",document.getElementById("yasr-metabox-info-snippet-container-localbusiness").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-product").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-software").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-book").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-movie").style.display="none"):"SoftwareApplication"===e?(document.getElementById("yasr-metabox-info-snippet-container").style.display="",document.getElementById("yasr-metabox-info-snippet-container-software").style.display="",document.getElementById("yasr-metabox-info-snippet-container-recipe").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-localbusiness").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-product").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-book").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-movie").style.display="none"):"Book"===e?(document.getElementById("yasr-metabox-info-snippet-container").style.display="",document.getElementById("yasr-metabox-info-snippet-container-book").style.display="",document.getElementById("yasr-metabox-info-snippet-container-recipe").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-localbusiness").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-product").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-software").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-movie").style.display="none"):"Movie"===e?(document.getElementById("yasr-metabox-info-snippet-container").style.display="",document.getElementById("yasr-metabox-info-snippet-container-movie").style.display="",document.getElementById("yasr-metabox-info-snippet-container-recipe").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-localbusiness").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-product").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-software").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-book").style.display="none"):document.getElementById("yasr-metabox-info-snippet-container").style.display="none"}function c(e,t,n){const o={action:"yasr_send_id_nameset",set_id:e,post_id:t};return jQuery.post(ajaxurl,o,(function(t){n>1&&(document.getElementById("yasr-loader-select-multi-set").style.display="none");let o=JSON.parse(t),a=document.getElementById("yasr-table-multi-set-admin"),i=document.getElementById("yasr-table-multi-set-admin-visitor");m(o,a),m(o,i,!1),u(),u(!1);let s=document.getElementsByClassName("yasr-editor-multiset-id");for(let t=0;t<s.length;t++)s[t].innerText=e})),!1}function m(e,t,n=!0){let o="",a="yasr-multiset-admin";for(let t=0;t<e.length;t++){let i=e[t].name,s=0,r=!0;!1!==n&&(s=e[t].average_rating,r=!1,a="yasr-multiset-admin-author");let l=e[t].id;o+="<tr>",o+="<td>"+i+"</td>",o+="<td><div class="+a+' id="yasr-multiset-admin-'+d()+'" data-rating="'+s+'" data-multi-idfield="'+l+'" data-readonly="'+r+'"></div>',o+="</td>",o+="</tr>"}!1===n&&(o+='<tr><td colspan="2"><input type="submit" class="button button-primary" value="Submit!" disabled></td></tr>'),t.innerHTML=o}function u(e=!0){let t;t=!1!==e?"yasr-multiset-admin-author":"yasr-multiset-admin";const n=document.getElementsByClassName(t);let o=[],a=!1;for(let e=0;e<n.length;e++)!function(e){let t=n.item(e).id,i=document.getElementById(t),s=parseInt(i.getAttribute("data-multi-idfield")),r=parseInt(i.getAttribute("data-rating")),l="true"===i.getAttribute("data-readonly"),d={field:s,rating:r};o.push(d),yasrSetRaterValue(32,t,!1,.5,l,!1,(function(e,t){e=e.toFixed(1),e=parseFloat(e),this.setRating(e);for(let t=0;t<o.length;t++)o[t].field===s&&(o[t].rating=e);a=JSON.stringify(o),document.getElementById("yasr-multiset-author-votes").value=a,t()}))}(e)}document.addEventListener("DOMContentLoaded",(function(e){!0!==document.body.classList.contains("block-editor-page")&&(function(){let e=parseFloat(document.getElementById("yasr-overall-rating-value").value);const n=document.getElementById("yasr-editor-copy-overall");null!==n&&(n.onclick=function(e){let n=document.getElementById(e.target.id);(0,t.v)(n.textContent.trim())}),raterJs({starSize:32,step:.1,showToolTip:!1,rating:e,readOnly:!1,element:document.getElementById("yasr-rater-overall"),rateCallback:function(e,t){e=e.toFixed(1),e=parseFloat(e),document.getElementById("yasr-overall-rating-value").value=e,this.setRating(e);document.getElementById("yasr_overall_text").textContent="You've rated "+e,t()}})}(),function(){jQuery("#yasr-shortcode-creator").on("click",(function(){tb_show("Ranking Creator","#TB_inline?width=770&height=700&inlineId=yasr-tinypopup-form"),jQuery("#TB_window").css({width:"800px"})})),jQuery("#yasr-builder-copy-shortcode").on("click",(function(){tb_remove()}));let e=!1;null!==document.getElementById("yasr-editor-multiset-container")&&(e=!0);const t=document.getElementById("yasr-tinypopup-link-doc");jQuery("#yasr-link-tab-main").on("click",(function(){jQuery(".yasr-nav-tab").removeClass("nav-tab-active"),jQuery("#yasr-link-tab-main").addClass("nav-tab-active"),jQuery(".yasr-content-tab-tinymce").hide(),jQuery("#yasr-content-tab-main").show(),t.setAttribute("href","https://yetanotherstarsrating.com/yasr-shortcodes?utm_source=wp-plugin&utm_medium=tinymce-popup&utm_campaign=yasr_editor_screen")})),jQuery("#yasr-link-tab-charts").on("click",(function(){jQuery(".yasr-nav-tab").removeClass("nav-tab-active"),jQuery("#yasr-link-tab-charts").addClass("nav-tab-active"),jQuery(".yasr-content-tab-tinymce").hide(),jQuery("#yasr-content-tab-charts").show(),t.setAttribute("href","https://yetanotherstarsrating.com/yasr-shortcodes/?utm_source=wp-plugin&utm_medium=tinymce-popup&utm_campaign=yasr_editor_screen#yasr-rankings-shortcodes")})),jQuery("#yasr-overall").on("click",(function(){jQuery("#yasr-overall-choose-size").toggle("slow")})),jQuery("#yasr-visitor-votes").on("click",(function(){jQuery("#yasr-visitor-choose-size").toggle("slow")})),jQuery(".yasr-tinymce-shortcode-buttons").on("click",(function(){let e=this.getAttribute("data-shortcode");null==tinyMCE.activeEditor?jQuery("#content").append(e):tinyMCE.activeEditor.execCommand("mceInsertContent",0,e),tb_remove()})),!0===e&&jQuery("#yasr-insert-multiset-select").on("click",(function(){let e,t=jQuery("input:radio[name=yasr_tinymce_pick_set]:checked").val();e=jQuery("#yasr-allow-vote-multiset").is(":checked")?"[yasr_multiset setid=":"[yasr_visitor_multiset setid=",e+=t,jQuery("#yasr-hide-average-multiset").is(":checked")&&(e+=" show_average='no'"),e+="]",null==tinyMCE.activeEditor?jQuery("#content").append(e):tinyMCE.activeEditor.execCommand("mceInsertContent",0,e),tb_remove()}))}()),function(){jQuery("#yasr-metabox-below-editor-structured-data-tab").on("click",(function(e){e.preventDefault(),jQuery(".yasr-nav-tab").removeClass("nav-tab-active"),jQuery("#yasr-metabox-below-editor-structured-data-tab").addClass("nav-tab-active"),jQuery(".yasr-metabox-below-editor-content").hide(),jQuery("#yasr-metabox-below-editor-structured-data").show()})),jQuery("#yasr-metabox-below-editor-multiset-tab").on("click",(function(e){e.preventDefault(),jQuery(".yasr-nav-tab").removeClass("nav-tab-active"),jQuery("#yasr-metabox-below-editor-multiset-tab").addClass("nav-tab-active"),jQuery(".yasr-metabox-below-editor-content").hide(),jQuery("#yasr-metabox-below-editor-multiset").show()}));let e=document.getElementById("yasr-metabox-below-editor-select-schema").value;null!==document.getElementById("yasr-editor-multiset-container")&&function(){let e=document.getElementById("yasr-editor-multiset-container"),n=parseInt(e.getAttribute("data-nmultiset")),o=parseInt(e.getAttribute("data-setid")),a=parseInt(e.getAttribute("data-postid"));const i=document.getElementById("yasr-pro-review-setid"),s=document.getElementById("yasr-editor-copy-readonly-multiset"),r=document.getElementById("yasr-editor-copy-visitor-multiset"),l=document.getElementById("yasr-editor-copy-average-multiset"),d=document.getElementById("yasr-editor-copy-average-vvmultiset"),y=document.getElementById("yasr-editor-copy-comments-multiset"),m=document.getElementById("yasr-pro-comments-enabled-yes"),u=document.getElementById("yasr-pro-multiset-review-switcher");c(o,a,n),s.onclick=function(e){let n=document.getElementById(e.target.id);(0,t.v)(n.textContent.trim())},r.onclick=function(e){let n=document.getElementById(e.target.id);(0,t.v)(n.textContent.trim())},l.onclick=function(e){let n=document.getElementById(e.target.id);(0,t.v)(n.textContent.trim())},d.onclick=function(e){let n=document.getElementById(e.target.id);(0,t.v)(n.textContent.trim())},y.onclick=function(e){let n=document.getElementById(e.target.id);(0,t.v)(n.textContent.trim())},null!==u&&(null!==m&&m.addEventListener("change",(e=>{e.currentTarget.checked||(u.checked=!1)})),u.addEventListener("change",(e=>{!0===e.currentTarget.checked?(null!==m?m.checked=!0:document.getElementById("yasr-comment-reviews-disabled-switch").checked=!0,null!==i&&(i.value=o)):null!==i&&(i.value="")}))),n>1&&jQuery("#yasr_select_set").on("change",(function(){return o=jQuery("#yasr_select_set").val(),jQuery("#yasr-loader-select-multi-set").show(),c(o,a,n),document.getElementById("yasr-multiset-id").value=o,null!==i&&""!==i&&(i.value===o?u.checked=!0:u.checked=!1),!1}))}(),y(e)}()})),document.getElementById("yasr-metabox-below-editor-select-schema").addEventListener("change",(function(){y(this.value)}))})()})();
1
+ (()=>{"use strict";var e={607:(e,t,n)=>{n.d(t,{v:()=>o});const o=e=>{const t=document.createElement("textarea");t.value=e,t.setAttribute("readonly",""),t.style.position="absolute",t.style.left="-9999px",document.body.appendChild(t),t.select(),document.execCommand("copy"),document.body.removeChild(t)};tippy(document.querySelectorAll(".yasr-copy-shortcode"),{content:"Copied! Insert into your post!",theme:"yasr",arrow:"true",arrowType:"round",trigger:"click"})}},t={};function n(o){var a=t[o];if(void 0!==a)return a.exports;var i=t[o]={exports:{}};return e[o](i,i.exports,n),i.exports}n.d=(e,t)=>{for(var o in t)n.o(t,o)&&!n.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),(()=>{var e,t=n(607),o=new Uint8Array(16);function a(){if(!e&&!(e="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)||"undefined"!=typeof msCrypto&&"function"==typeof msCrypto.getRandomValues&&msCrypto.getRandomValues.bind(msCrypto)))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return e(o)}const i=/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;const s=function(e){return"string"==typeof e&&i.test(e)};for(var r=[],l=0;l<256;++l)r.push((l+256).toString(16).substr(1));const d=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,n=(r[e[t+0]]+r[e[t+1]]+r[e[t+2]]+r[e[t+3]]+"-"+r[e[t+4]]+r[e[t+5]]+"-"+r[e[t+6]]+r[e[t+7]]+"-"+r[e[t+8]]+r[e[t+9]]+"-"+r[e[t+10]]+r[e[t+11]]+r[e[t+12]]+r[e[t+13]]+r[e[t+14]]+r[e[t+15]]).toLowerCase();if(!s(n))throw TypeError("Stringified UUID is invalid");return n};const y=function(e,t,n){var o=(e=e||{}).random||(e.rng||a)();if(o[6]=15&o[6]|64,o[8]=63&o[8]|128,t){n=n||0;for(var i=0;i<16;++i)t[n+i]=o[i];return t}return d(o)};function c(e){"Product"===e?(document.getElementById("yasr-metabox-info-snippet-container").style.display="",document.getElementById("yasr-metabox-info-snippet-container-product").style.display="",document.getElementById("yasr-metabox-info-snippet-container-localbusiness").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-recipe").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-software").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-book").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-movie").style.display="none"):"LocalBusiness"===e?(document.getElementById("yasr-metabox-info-snippet-container").style.display="",document.getElementById("yasr-metabox-info-snippet-container-localbusiness").style.display="",document.getElementById("yasr-metabox-info-snippet-container-product").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-recipe").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-software").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-book").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-movie").style.display="none"):"Recipe"===e?(document.getElementById("yasr-metabox-info-snippet-container").style.display="",document.getElementById("yasr-metabox-info-snippet-container-recipe").style.display="",document.getElementById("yasr-metabox-info-snippet-container-localbusiness").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-product").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-software").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-book").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-movie").style.display="none"):"SoftwareApplication"===e?(document.getElementById("yasr-metabox-info-snippet-container").style.display="",document.getElementById("yasr-metabox-info-snippet-container-software").style.display="",document.getElementById("yasr-metabox-info-snippet-container-recipe").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-localbusiness").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-product").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-book").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-movie").style.display="none"):"Book"===e?(document.getElementById("yasr-metabox-info-snippet-container").style.display="",document.getElementById("yasr-metabox-info-snippet-container-book").style.display="",document.getElementById("yasr-metabox-info-snippet-container-recipe").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-localbusiness").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-product").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-software").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-movie").style.display="none"):"Movie"===e?(document.getElementById("yasr-metabox-info-snippet-container").style.display="",document.getElementById("yasr-metabox-info-snippet-container-movie").style.display="",document.getElementById("yasr-metabox-info-snippet-container-recipe").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-localbusiness").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-product").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-software").style.display="none",document.getElementById("yasr-metabox-info-snippet-container-book").style.display="none"):document.getElementById("yasr-metabox-info-snippet-container").style.display="none"}function m(e,t,n){const o={action:"yasr_send_id_nameset",set_id:e,post_id:t};return jQuery.post(ajaxurl,o,(function(t){n>1&&(document.getElementById("yasr-loader-select-multi-set").style.display="none");let o=JSON.parse(t),a=document.getElementById("yasr-table-multi-set-admin"),i=document.getElementById("yasr-table-multi-set-admin-visitor");u(o,a),u(o,i,!1),p(),p(!1);let s=document.getElementsByClassName("yasr-editor-multiset-id");for(let t=0;t<s.length;t++)s[t].innerText=e})),!1}function u(e,t,n=!0){let o="",a="yasr-multiset-admin";for(let t=0;t<e.length;t++){let i=e[t].name,s=0,r=!0;!1!==n&&(s=e[t].average_rating,r=!1,a="yasr-multiset-admin-author");let l=e[t].id;o+="<tr>",o+="<td>"+i+"</td>",o+="<td><div class="+a+' id="yasr-multiset-admin-'+y()+'" data-rating="'+s+'" data-multi-idfield="'+l+'" data-readonly="'+r+'"></div>',o+="</td>",o+="</tr>"}if(!1===n){o+='<tr><td colspan="2"><input type="submit" class="button button-primary" value="Submit!" disabled></td></tr>'}t.innerHTML=o}function p(e=!0){let t;t=!1!==e?"yasr-multiset-admin-author":"yasr-multiset-admin";const n=document.getElementsByClassName(t);let o=[],a=!1;for(let e=0;e<n.length;e++)!function(e){let t=n.item(e).id,i=document.getElementById(t),s=parseInt(i.getAttribute("data-multi-idfield")),r=parseInt(i.getAttribute("data-rating")),l="true"===i.getAttribute("data-readonly"),d={field:s,rating:r};o.push(d);yasrSetRaterValue(32,t,!1,.5,l,!1,(function(e,t){e=e.toFixed(1),e=parseFloat(e),this.setRating(e);for(let t=0;t<o.length;t++)o[t].field===s&&(o[t].rating=e);a=JSON.stringify(o),document.getElementById("yasr-multiset-author-votes").value=a,t()}))}(e)}document.addEventListener("DOMContentLoaded",(function(e){!0!==document.body.classList.contains("block-editor-page")&&(function(){let e=parseFloat(document.getElementById("yasr-overall-rating-value").value);const n=document.getElementById("yasr-editor-copy-overall");null!==n&&(n.onclick=function(e){let n=document.getElementById(e.target.id);(0,t.v)(n.textContent.trim())});raterJs({starSize:32,step:.1,showToolTip:!1,rating:e,readOnly:!1,element:document.getElementById("yasr-rater-overall"),rateCallback:function(e,t){e=e.toFixed(1),e=parseFloat(e),document.getElementById("yasr-overall-rating-value").value=e,this.setRating(e);let n="You've rated";document.getElementById("yasr_overall_text").textContent=n+" "+e,t()}})}(),function(){jQuery("#yasr-shortcode-creator").on("click",(function(){tb_show("Ranking Creator","#TB_inline?width=770&height=700&inlineId=yasr-tinypopup-form"),jQuery("#TB_window").css({width:"800px"})})),jQuery("#yasr-builder-copy-shortcode").on("click",(function(){tb_remove()}));let e=!1;null!==document.getElementById("yasr-editor-multiset-container")&&(e=!0);const t=document.getElementById("yasr-tinypopup-link-doc");jQuery("#yasr-link-tab-main").on("click",(function(){jQuery(".yasr-nav-tab").removeClass("nav-tab-active"),jQuery("#yasr-link-tab-main").addClass("nav-tab-active"),jQuery(".yasr-content-tab-tinymce").hide(),jQuery("#yasr-content-tab-main").show(),t.setAttribute("href","https://yetanotherstarsrating.com/yasr-shortcodes?utm_source=wp-plugin&utm_medium=tinymce-popup&utm_campaign=yasr_editor_screen")})),jQuery("#yasr-link-tab-charts").on("click",(function(){jQuery(".yasr-nav-tab").removeClass("nav-tab-active"),jQuery("#yasr-link-tab-charts").addClass("nav-tab-active"),jQuery(".yasr-content-tab-tinymce").hide(),jQuery("#yasr-content-tab-charts").show(),t.setAttribute("href","https://yetanotherstarsrating.com/yasr-shortcodes/?utm_source=wp-plugin&utm_medium=tinymce-popup&utm_campaign=yasr_editor_screen#yasr-rankings-shortcodes")})),jQuery("#yasr-overall").on("click",(function(){jQuery("#yasr-overall-choose-size").toggle("slow")})),jQuery("#yasr-visitor-votes").on("click",(function(){jQuery("#yasr-visitor-choose-size").toggle("slow")})),jQuery(".yasr-tinymce-shortcode-buttons").on("click",(function(){let e=this.getAttribute("data-shortcode");null==tinyMCE.activeEditor?jQuery("#content").append(e):tinyMCE.activeEditor.execCommand("mceInsertContent",0,e),tb_remove()})),!0===e&&jQuery("#yasr-insert-multiset-select").on("click",(function(){let e,t=jQuery("input:radio[name=yasr_tinymce_pick_set]:checked").val();e=jQuery("#yasr-allow-vote-multiset").is(":checked")?"[yasr_multiset setid=":"[yasr_visitor_multiset setid=",e+=t,jQuery("#yasr-hide-average-multiset").is(":checked")&&(e+=" show_average='no'"),e+="]",null==tinyMCE.activeEditor?jQuery("#content").append(e):tinyMCE.activeEditor.execCommand("mceInsertContent",0,e),tb_remove()}))}()),function(){jQuery("#yasr-metabox-below-editor-structured-data-tab").on("click",(function(e){e.preventDefault(),jQuery(".yasr-nav-tab").removeClass("nav-tab-active"),jQuery("#yasr-metabox-below-editor-structured-data-tab").addClass("nav-tab-active"),jQuery(".yasr-metabox-below-editor-content").hide(),jQuery("#yasr-metabox-below-editor-structured-data").show()})),jQuery("#yasr-metabox-below-editor-multiset-tab").on("click",(function(e){e.preventDefault(),jQuery(".yasr-nav-tab").removeClass("nav-tab-active"),jQuery("#yasr-metabox-below-editor-multiset-tab").addClass("nav-tab-active"),jQuery(".yasr-metabox-below-editor-content").hide(),jQuery("#yasr-metabox-below-editor-multiset").show()}));let e=document.getElementById("yasr-metabox-below-editor-select-schema").value;null!==document.getElementById("yasr-editor-multiset-container")&&function(){let e=document.getElementById("yasr-editor-multiset-container"),n=parseInt(e.getAttribute("data-nmultiset")),o=parseInt(e.getAttribute("data-setid")),a=parseInt(e.getAttribute("data-postid"));const i=document.getElementById("yasr-pro-review-setid"),s=document.getElementById("yasr-editor-copy-readonly-multiset"),r=document.getElementById("yasr-editor-copy-visitor-multiset"),l=document.getElementById("yasr-editor-copy-average-multiset"),d=document.getElementById("yasr-editor-copy-average-vvmultiset"),y=document.getElementById("yasr-editor-copy-comments-multiset"),c=document.getElementById("yasr-pro-comments-enabled-yes"),u=document.getElementById("yasr-pro-multiset-review-switcher");m(o,a,n),s.onclick=function(e){let n=document.getElementById(e.target.id);(0,t.v)(n.textContent.trim())},r.onclick=function(e){let n=document.getElementById(e.target.id);(0,t.v)(n.textContent.trim())},l.onclick=function(e){let n=document.getElementById(e.target.id);(0,t.v)(n.textContent.trim())},d.onclick=function(e){let n=document.getElementById(e.target.id);(0,t.v)(n.textContent.trim())},y.onclick=function(e){let n=document.getElementById(e.target.id);(0,t.v)(n.textContent.trim())},null!==u&&(null!==c&&c.addEventListener("change",(e=>{e.currentTarget.checked||(u.checked=!1)})),u.addEventListener("change",(e=>{!0===e.currentTarget.checked?(null!==c?c.checked=!0:document.getElementById("yasr-comment-reviews-disabled-switch").checked=!0,null!==i&&(i.value=o)):null!==i&&(i.value="")})));n>1&&jQuery("#yasr_select_set").on("change",(function(){return o=jQuery("#yasr_select_set").val(),jQuery("#yasr-loader-select-multi-set").show(),m(o,a,n),document.getElementById("yasr-multiset-id").value=o,null!==i&&""!==i&&(i.value===o?u.checked=!0:u.checked=!1),!1}))}();c(e)}()})),document.getElementById("yasr-metabox-below-editor-select-schema").addEventListener("change",(function(){c(this.value)}))})()})();
admin/js/yasr-pricing-page.js CHANGED
@@ -1 +1 @@
1
- (()=>{function e(t){return e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},e(t)}function t(e,t){for(var c=0;c<t.length;c++){var a=t[c];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(e,a.key,a)}}function c(e,t){return c=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},c(e,t)}function a(t,c){if(c&&("object"===e(c)||"function"==typeof c))return c;if(void 0!==c)throw new TypeError("Derived constructors may only return object or undefined");return r(t)}function r(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function n(e){return n=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},n(e)}function l(e){var t="$";return"eur"===e.name&&(t="€"),React.createElement("small",null,t," ")}function i(e){var t="/year";return"monthly"===e.name&&(t="/month"),"lifetime"===e.name&&(t=""),React.createElement("small",null,t)}function s(e){var t=" 1 website";return"plus"===e.license&&(t=" 5 websites"),"enterprise"===e.license&&(t=" 30 websites"),React.createElement("ul",{className:"yasr-pricing-table-features"},"annual"===e.cycle&&React.createElement("li",{className:"yasr-pricing-table-feature"},React.createElement("strong",null,"1 year")," of supports and updates ",React.createElement("br",null),"for",React.createElement("strong",null,t)),"monthly"===e.cycle&&React.createElement("li",{className:"yasr-pricing-table-feature"},React.createElement("strong",null,"1 month")," of supports and updates ",React.createElement("br",null),"for",React.createElement("strong",null,t)),"lifetime"===e.cycle&&React.createElement("li",{className:"yasr-pricing-table-feature"},React.createElement("strong",null,"Lifetime")," updates and support",React.createElement("br",null),"for",React.createElement("strong",null,t)),React.createElement("li",{className:"yasr-pricing-table-feature"},"User reviews"),React.createElement("li",{className:"yasr-pricing-table-feature"},"Custom rankings"),React.createElement("li",{className:"yasr-pricing-table-feature"},"20 + ready to use themes"),React.createElement("li",{className:"yasr-pricing-table-feature"},"Upload your own theme"),React.createElement("li",{className:"yasr-pricing-table-feature"},"Add fake ratings"),React.createElement("li",{className:"yasr-pricing-table-feature"},"Direct email support"),"lifetime"===e.cycle&&React.createElement("li",{className:"yasr-pricing-table-feature"},"We setup the plugin for free ",React.createElement("br",null),"(only lifetime plans)"))}function u(e){var t="",c=1,a="";return"single"===e.license?(a="eur"===e.currency?5399:1933,t="monthly"===e.cycle?"eur"===e.currency?"4.49":"4.99":"lifetime"===e.cycle?"eur"===e.currency?"129.99":"149.99":"eur"===e.currency?"41.99":"47.88"):"plus"===e.license?(c=5,a="eur"===e.currency?5400:1935,t="monthly"===e.cycle?"eur"===e.currency?"8.99":"9.99":"lifetime"===e.cycle?"eur"===e.currency?"249.99":"289.99":"eur"===e.currency?"83.88":"95.88"):"enterprise"===e.license&&(c=30,a="eur"===e.currency?5550:5549,t="monthly"===e.cycle?"eur"===e.currency?"12.99":"14.99":"lifetime"===e.cycle?"eur"===e.currency?"359.99":"439.99":"eur"===e.currency?"119.88":"143.88"),React.createElement("div",{className:"yasr-pring-table-price"},React.createElement(l,{name:e.currency}),React.createElement("span",null,t),React.createElement(i,{name:e.cycle}),React.createElement(o,{cycle:e.cycle,currency:e.currency,license:e.license}),React.createElement("p",{className:"PT-CTA"},React.createElement("a",{href:"#",className:"yasr-buy-button",onClick:function(t){!function(e,t,c,a){var r={plugin_id:256,billing_cycle:e,pricing_id:a,licenses:t,id:"yasr_checkout",page:"yasr_settings_page-pricing",checkout:"true",plan_id:"2778",plan_name:"yasrpro",disable_licenses_selector:!0,hide_billing_cycles:!0,currency:c},n=[];for(var l in r){var i=encodeURIComponent(l),s=encodeURIComponent(r[l]);n.push(i+"="+s)}n=n.join("&");var u=yasrWindowVar.adminUrl+"admin.php?"+n;window.open(u,"_self")}(e.cycle,c,e.currency,a),t.preventDefault()}},"Buy YASR ")))}function o(e){if("annual"===e.cycle){var t;return t="plus"===e.license?"eur"===e.currency?"6.99":"7.99":"enterprise"===e.license?"eur"===e.currency?"9.99":"11.99":"eur"===e.currency?"3.49":"3.99",React.createElement("p",{className:"yasr-pricing-table-price-desc"},React.createElement(l,{name:e.currency}),t," /month")}return React.createElement(React.Fragment,null)}var y=function(e){!function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),t&&c(e,t)}(p,React.Component);var l,i,o,y,m=(o=p,y=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}(),function(){var e,t=n(o);if(y){var c=n(this).constructor;e=Reflect.construct(t,arguments,c)}else e=t.apply(this,arguments);return a(this,e)});function p(e){var t;return function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,p),(t=m.call(this,e)).state={currencyName:"usd",cycle:"annual"},t.updateCurrency=t.updateCurrency.bind(r(t)),t.updateCycle=t.updateCycle.bind(r(t)),t}return l=p,(i=[{key:"updateCurrency",value:function(e){var t=e.target;!0===("checkbox"===t.type?t.checked:t.value)?this.setState({currencyName:"eur"}):this.setState({currencyName:"usd"})}},{key:"updateCycle",value:function(e){this.setState({cycle:e.target.value})}},{key:"render",value:function(){return React.createElement(React.Fragment,null,React.createElement("div",{id:"yasr-radio-billing-cycle"},React.createElement("input",{type:"radio",id:"yasr-billing-cycle-monthly",name:"billing-cycle",value:"monthly",onChange:this.updateCycle,checked:"monthly"===this.state.cycle}),React.createElement("label",{htmlFor:"yasr-billing-cycle-monthly"},"Monthly"),React.createElement("input",{type:"radio",id:"yasr-billing-cycle-annual",name:"billing-cycle",value:"annual",onChange:this.updateCycle,checked:"annual"===this.state.cycle}),React.createElement("label",{htmlFor:"yasr-billing-cycle-annual"},"Annual"),React.createElement("input",{type:"radio",id:"yasr-billing-cycle-lifetime",name:"billing-cycle",value:"lifetime",onChange:this.updateCycle,checked:"lifetime"===this.state.cycle}),React.createElement("label",{htmlFor:"yasr-billing-cycle-lifetime"},"Lifetime")),React.createElement("div",{id:"yasr-pricing-table"},React.createElement("div",{className:"yasr-pricing-table-item"},React.createElement("header",{className:"yasr-pricing-table-heading"},React.createElement("h2",{className:"yasr-pricing-table-title"},"Plus"),React.createElement("p",{className:"yasr-pricing-table-subtitle"},"5 websites")),React.createElement(s,{cycle:this.state.cycle,license:"plus"}),React.createElement("div",{className:"yasr-pricing-table-footer"},React.createElement(u,{cycle:this.state.cycle,currency:this.state.currencyName,license:"plus"}))),React.createElement("div",{className:"yasr-pricing-table-item is-highlighted"},React.createElement("header",{className:"yasr-pricing-table-heading"},React.createElement("h2",{className:"yasr-pricing-table-title"},"Single"),React.createElement("p",{className:"yasr-pricing-table-subtitle"}," 1 website")),React.createElement(s,{cycle:this.state.cycle,license:"single"}),React.createElement("div",{className:"yasr-pricing-table-footer"},React.createElement(u,{cycle:this.state.cycle,currency:this.state.currencyName,license:"single"}))),React.createElement("div",{className:"yasr-pricing-table-item"},React.createElement("header",{className:"yasr-pricing-table-heading"},React.createElement("h2",{className:"yasr-pricing-table-title"},"Enterprise"),React.createElement("p",{className:"yasr-pricing-table-subtitle"}," 30 websites")),React.createElement(s,{cycle:this.state.cycle,license:"enterprise"}),React.createElement("div",{className:"yasr-pricing-table-footer"},React.createElement(u,{cycle:this.state.cycle,currency:this.state.currencyName,license:"enterprise"})))),React.createElement("div",{id:"switch-container"},React.createElement("span",{className:"yasr-pricing-text-switcher"}," Display Prices In US $ "),React.createElement("label",{className:"yasr-pricing-switch"},React.createElement("input",{type:"checkbox",onChange:this.updateCurrency}),React.createElement("span",{className:"yasr-pricing-slider"})),React.createElement("span",{className:"yasr-pricing-text-switcher"}," €")))}}])&&t(l.prototype,i),Object.defineProperty(l,"prototype",{writable:!1}),p}();ReactDOM.render(React.createElement(y,null),document.getElementById("yasr-table-container"));var m=document.getElementById("yasr-link-policy"),p=document.getElementById("yasr-link-policy-faq"),f=document.getElementById("yasr-refund-policy"),d=document.getElementById("yasr-close-modal-policy");m.addEventListener("click",(function(){f.style.display="block",document.body.style.backgroundColor="rgba(0,0,0,0.7)"})),p.addEventListener("click",(function(){f.style.display="block",document.body.style.backgroundColor="rgba(0,0,0,0.7)"})),d.onclick=function(){f.style.display="none",document.body.style.backgroundColor="#f1f1f1"}})();
1
+ (()=>{function e(t){return e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},e(t)}function t(e,t){for(var c=0;c<t.length;c++){var a=t[c];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(e,a.key,a)}}function c(e,t){return c=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},c(e,t)}function a(e){var t=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}();return function(){var c,a=l(e);if(t){var n=l(this).constructor;c=Reflect.construct(a,arguments,n)}else c=a.apply(this,arguments);return r(this,c)}}function r(t,c){if(c&&("object"===e(c)||"function"==typeof c))return c;if(void 0!==c)throw new TypeError("Derived constructors may only return object or undefined");return n(t)}function n(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function l(e){return l=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},l(e)}function i(e){var t="$";return"eur"===e.name&&(t="€"),React.createElement("small",null,t," ")}function s(e){var t="/year";return"monthly"===e.name&&(t="/month"),"lifetime"===e.name&&(t=""),React.createElement("small",null,t)}function u(e){var t=" 1 website";return"plus"===e.license&&(t=" 5 websites"),"enterprise"===e.license&&(t=" 30 websites"),React.createElement("ul",{className:"yasr-pricing-table-features"},"annual"===e.cycle&&React.createElement("li",{className:"yasr-pricing-table-feature"},React.createElement("strong",null,"1 year")," of supports and updates ",React.createElement("br",null),"for",React.createElement("strong",null,t)),"monthly"===e.cycle&&React.createElement("li",{className:"yasr-pricing-table-feature"},React.createElement("strong",null,"1 month")," of supports and updates ",React.createElement("br",null),"for",React.createElement("strong",null,t)),"lifetime"===e.cycle&&React.createElement("li",{className:"yasr-pricing-table-feature"},React.createElement("strong",null,"Lifetime")," updates and support",React.createElement("br",null),"for",React.createElement("strong",null,t)),React.createElement("li",{className:"yasr-pricing-table-feature"},"User reviews"),React.createElement("li",{className:"yasr-pricing-table-feature"},"Custom rankings"),React.createElement("li",{className:"yasr-pricing-table-feature"},"20 + ready to use themes"),React.createElement("li",{className:"yasr-pricing-table-feature"},"Upload your own theme"),React.createElement("li",{className:"yasr-pricing-table-feature"},"Add fake ratings"),React.createElement("li",{className:"yasr-pricing-table-feature"},"Direct email support"),"lifetime"===e.cycle&&React.createElement("li",{className:"yasr-pricing-table-feature"},"We setup the plugin for free ",React.createElement("br",null),"(only lifetime plans)"))}function o(e){var t="",c=1,a="";return"single"===e.license?(a="eur"===e.currency?5399:1933,t="monthly"===e.cycle?"eur"===e.currency?"4.49":"4.99":"lifetime"===e.cycle?"eur"===e.currency?"129.99":"149.99":"eur"===e.currency?"41.99":"47.88"):"plus"===e.license?(c=5,a="eur"===e.currency?5400:1935,t="monthly"===e.cycle?"eur"===e.currency?"8.99":"9.99":"lifetime"===e.cycle?"eur"===e.currency?"249.99":"289.99":"eur"===e.currency?"83.88":"95.88"):"enterprise"===e.license&&(c=30,a="eur"===e.currency?5550:5549,t="monthly"===e.cycle?"eur"===e.currency?"12.99":"14.99":"lifetime"===e.cycle?"eur"===e.currency?"359.99":"439.99":"eur"===e.currency?"119.88":"143.88"),React.createElement("div",{className:"yasr-pring-table-price"},React.createElement(i,{name:e.currency}),React.createElement("span",null,t),React.createElement(s,{name:e.cycle}),React.createElement(y,{cycle:e.cycle,currency:e.currency,license:e.license}),React.createElement("p",{className:"PT-CTA"},React.createElement("a",{href:"#",className:"yasr-buy-button",onClick:function(t){!function(e,t,c,a){var r={plugin_id:256,billing_cycle:e,pricing_id:a,licenses:t,id:"yasr_checkout",page:"yasr_settings_page-pricing",checkout:"true",plan_id:"2778",plan_name:"yasrpro",disable_licenses_selector:!0,hide_billing_cycles:!0,currency:c},n=[];for(var l in r){var i=encodeURIComponent(l),s=encodeURIComponent(r[l]);n.push(i+"="+s)}n=n.join("&");var u=yasrWindowVar.adminUrl+"admin.php?"+n;window.open(u,"_self")}(e.cycle,c,e.currency,a),t.preventDefault()}},"Buy YASR ")))}function y(e){if("annual"===e.cycle){var t="";return t="plus"===e.license?"eur"===e.currency?"6.99":"7.99":"enterprise"===e.license?"eur"===e.currency?"9.99":"11.99":"eur"===e.currency?"3.49":"3.99",React.createElement("p",{className:"yasr-pricing-table-price-desc"},React.createElement(i,{name:e.currency}),t," /month")}return React.createElement(React.Fragment,null)}var m=function(e){!function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),t&&c(e,t)}(y,React.Component);var r,l,i,s=a(y);function y(e){var t;return function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,y),(t=s.call(this,e)).state={currencyName:"usd",cycle:"annual"},t.updateCurrency=t.updateCurrency.bind(n(t)),t.updateCycle=t.updateCycle.bind(n(t)),t}return r=y,(l=[{key:"updateCurrency",value:function(e){var t=e.target;!0===("checkbox"===t.type?t.checked:t.value)?this.setState({currencyName:"eur"}):this.setState({currencyName:"usd"})}},{key:"updateCycle",value:function(e){this.setState({cycle:e.target.value})}},{key:"render",value:function(){return React.createElement(React.Fragment,null,React.createElement("div",{id:"yasr-radio-billing-cycle"},React.createElement("input",{type:"radio",id:"yasr-billing-cycle-monthly",name:"billing-cycle",value:"monthly",onChange:this.updateCycle,checked:"monthly"===this.state.cycle}),React.createElement("label",{htmlFor:"yasr-billing-cycle-monthly"},"Monthly"),React.createElement("input",{type:"radio",id:"yasr-billing-cycle-annual",name:"billing-cycle",value:"annual",onChange:this.updateCycle,checked:"annual"===this.state.cycle}),React.createElement("label",{htmlFor:"yasr-billing-cycle-annual"},"Annual"),React.createElement("input",{type:"radio",id:"yasr-billing-cycle-lifetime",name:"billing-cycle",value:"lifetime",onChange:this.updateCycle,checked:"lifetime"===this.state.cycle}),React.createElement("label",{htmlFor:"yasr-billing-cycle-lifetime"},"Lifetime")),React.createElement("div",{id:"yasr-pricing-table"},React.createElement("div",{className:"yasr-pricing-table-item"},React.createElement("header",{className:"yasr-pricing-table-heading"},React.createElement("h2",{className:"yasr-pricing-table-title"},"Plus"),React.createElement("p",{className:"yasr-pricing-table-subtitle"},"5 websites")),React.createElement(u,{cycle:this.state.cycle,license:"plus"}),React.createElement("div",{className:"yasr-pricing-table-footer"},React.createElement(o,{cycle:this.state.cycle,currency:this.state.currencyName,license:"plus"}))),React.createElement("div",{className:"yasr-pricing-table-item is-highlighted"},React.createElement("header",{className:"yasr-pricing-table-heading"},React.createElement("h2",{className:"yasr-pricing-table-title"},"Single"),React.createElement("p",{className:"yasr-pricing-table-subtitle"}," 1 website")),React.createElement(u,{cycle:this.state.cycle,license:"single"}),React.createElement("div",{className:"yasr-pricing-table-footer"},React.createElement(o,{cycle:this.state.cycle,currency:this.state.currencyName,license:"single"}))),React.createElement("div",{className:"yasr-pricing-table-item"},React.createElement("header",{className:"yasr-pricing-table-heading"},React.createElement("h2",{className:"yasr-pricing-table-title"},"Enterprise"),React.createElement("p",{className:"yasr-pricing-table-subtitle"}," 30 websites")),React.createElement(u,{cycle:this.state.cycle,license:"enterprise"}),React.createElement("div",{className:"yasr-pricing-table-footer"},React.createElement(o,{cycle:this.state.cycle,currency:this.state.currencyName,license:"enterprise"})))),React.createElement("div",{id:"switch-container"},React.createElement("span",{className:"yasr-pricing-text-switcher"}," Display Prices In US $ "),React.createElement("label",{className:"yasr-pricing-switch"},React.createElement("input",{type:"checkbox",onChange:this.updateCurrency}),React.createElement("span",{className:"yasr-pricing-slider"})),React.createElement("span",{className:"yasr-pricing-text-switcher"}," €")))}}])&&t(r.prototype,l),i&&t(r,i),Object.defineProperty(r,"prototype",{writable:!1}),y}();ReactDOM.render(React.createElement(m,null),document.getElementById("yasr-table-container"));var p=document.getElementById("yasr-link-policy"),f=document.getElementById("yasr-link-policy-faq"),d=document.getElementById("yasr-refund-policy"),b=document.getElementById("yasr-close-modal-policy");p.addEventListener("click",(function(){d.style.display="block",document.body.style.backgroundColor="rgba(0,0,0,0.7)"})),f.addEventListener("click",(function(){d.style.display="block",document.body.style.backgroundColor="rgba(0,0,0,0.7)"})),b.onclick=function(){d.style.display="none",document.body.style.backgroundColor="#f1f1f1"}})();
admin/js/yasr-settings.js CHANGED
@@ -1 +1 @@
1
- (()=>{var e={726:function(e,t,r){"use strict";var a;!function(n){if("function"!=typeof s){var s=function(e){return e};s.nonNative=!0}const o=s("plaintext"),i=s("html"),l=s("comment"),d=/<(\w*)>/g,c=/<\/?([^\s\/>]+)/;function u(e,t,r){return m(e=e||"",y(t=t||[],r=r||""))}function y(e,t){return{allowable_tags:e=function(e){let t=new Set;if("string"==typeof e){let r;for(;r=d.exec(e);)t.add(r[1])}else s.nonNative||"function"!=typeof e[s.iterator]?"function"==typeof e.forEach&&e.forEach(t.add,t):t=new Set(e);return t}(e),tag_replacement:t,state:o,tag_buffer:"",depth:0,in_quote_char:""}}function m(e,t){if("string"!=typeof e)throw new TypeError("'html' parameter must be a string");let r=t.allowable_tags,a=t.tag_replacement,n=t.state,s=t.tag_buffer,d=t.depth,c=t.in_quote_char,u="";for(let t=0,y=e.length;t<y;t++){let y=e[t];if(n===o)"<"===y?(n=i,s+=y):u+=y;else if(n===i)switch(y){case"<":if(c)break;d++;break;case">":if(c)break;if(d){d--;break}c="",n=o,s+=">",r.has(p(s))?u+=s:u+=a,s="";break;case'"':case"'":c=y===c?"":c||y,s+=y;break;case"-":"<!-"===s&&(n=l),s+=y;break;case" ":case"\n":if("<"===s){n=o,u+="< ",s="";break}s+=y;break;default:s+=y}else n===l&&(">"===y?("--"==s.slice(-2)&&(n=o),s=""):s+=y)}return t.state=n,t.tag_buffer=s,t.depth=d,t.in_quote_char=c,u}function p(e){let t=c.exec(e);return t?t[1].toLowerCase():null}u.init_streaming_mode=function(e,t){let r=y(e=e||[],t=t||"");return function(e){return m(e||"",r)}},void 0===(a=function(){return u}.call(t,r,t,e))||(e.exports=a)}()}},t={};function r(a){var n=t[a];if(void 0!==n)return n.exports;var s=t[a]={exports:{}};return e[a].call(s.exports,s,s.exports,r),s.exports}r.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return r.d(t,{a:t}),t},r.d=(e,t)=>{for(var a in t)r.o(t,a)&&!r.o(e,a)&&Object.defineProperty(e,a,{enumerable:!0,get:t[a]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),(()=>{var e;if(document.getElementsByClassName("nav-tab-active").length>0&&(e=document.getElementsByClassName("nav-tab-active")[0].id),"general_settings"===e){var t=document.getElementById("yasr_auto_insert_switch").checked,r=document.getElementById("yasr-general-options-stars-title-switch").checked;!1===t&&jQuery(".yasr-auto-insert-options-class").prop("disabled",!0),!1===r&&jQuery(".yasr-stars-title-options-class").prop("disabled",!0),document.getElementById("yasr_auto_insert_switch").addEventListener("change",(function(){this.checked?jQuery(".yasr-auto-insert-options-class").prop("disabled",!1):jQuery(".yasr-auto-insert-options-class").prop("disabled",!0)})),document.getElementById("yasr-general-options-stars-title-switch").addEventListener("change",(function(){this.checked?jQuery(".yasr-stars-title-options-class").prop("disabled",!1):jQuery(".yasr-stars-title-options-class").prop("disabled",!0)})),document.getElementById("yasr-settings-custom-texts").addEventListener("click",(function(){document.getElementById("yasr-settings-custom-text-before-overall").value="Our Score",document.getElementById("yasr-settings-custom-text-before-visitor").value="Click to rate this post!",document.getElementById("yasr-settings-custom-text-after-visitor").value="[Total: %total_count% Average: %average%]",document.getElementById("yasr-settings-custom-text-rating-saved").value="Rating saved!",document.getElementById("yasr-settings-custom-text-rating-updated").value="Rating updated!",document.getElementById("yasr-settings-custom-text-must-sign-in").value="You must sign in to vote",document.getElementById("yasr-settings-custom-text-already-rated").value="You have already voted for this article with %rating%"}))}if("style_options"===e&&(wp.codeEditor.initialize(document.getElementById("yasr_style_options_textarea"),yasr_cm_settings),jQuery("#yasr-color-scheme-preview-link").on("click",(function(){return jQuery("#yasr-color-scheme-preview").toggle("slow"),!1})),wp.hooks.doAction("yasrStyleOptions")),"manage_multi"===e){var a=parseInt(document.getElementById("n-multiset").value);if(jQuery("#yasr-multi-set-doc-link").on("click",(function(){jQuery("#yasr-multi-set-doc-box").toggle("slow")})),jQuery("#yasr-multi-set-doc-link-hide").on("click",(function(){jQuery("#yasr-multi-set-doc-box").toggle("slow")})),1===a){var n=jQuery("#yasr-edit-form-number-elements").attr("value");n++,jQuery("#yasr-add-field-edit-multiset").on("click",(function(){if(n>9)return jQuery("#yasr-element-limit").show(),jQuery("#yasr-add-field-edit-multiset").hide(),!1;var e=jQuery(document.createElement("tr"));e.html('<td colspan="2">Element #'+n+' <input type="text" name="edit-multi-set-element-'+n+'" value="" ></td>'),e.appendTo("#yasr-table-form-edit-multi-set"),n++}))}else a>1&&(jQuery("#yasr-button-select-set-edit-form").on("click",(function(){var e={action:"yasr_get_multi_set",set_id:jQuery("#yasr_select_edit_set").val()};return jQuery.post(ajaxurl,e,(function(e){jQuery("#yasr-multi-set-response").show(),jQuery("#yasr-multi-set-response").html(e)})),!1})),jQuery(document).ajaxComplete((function(){var e=jQuery("#yasr-edit-form-number-elements").attr("value");e++,jQuery("#yasr-add-field-edit-multiset").on("click",(function(){if(e>9)return jQuery("#yasr-element-limit").show(),jQuery("#yasr-add-field-edit-multiset").hide(),!1;var t=jQuery(document.createElement("tr"));t.html('<td colspan="2">Element #'+e+' <input type="text" name="edit-multi-set-element-'+e+'" value="" ></td>'),t.appendTo("#yasr-table-form-edit-multi-set"),e++}))})))}"migration_tools"===e&&(jQuery("#yasr-import-ratemypost-submit").on("click",(function(){document.getElementById("yasr-import-ratemypost-answer").innerHTML='<img src="'+yasrWindowVar.loaderUrl+'" alt="yasr-loader" width="16" height="16">';var e={action:"yasr_import_ratemypost",nonce:document.getElementById("yasr-import-rmp-nonce").value};jQuery.post(ajaxurl,e,(function(e){e=JSON.parse(e),document.getElementById("yasr-import-ratemypost-answer").innerHTML=e}))})),jQuery("#yasr-import-wppr-submit").on("click",(function(){document.getElementById("yasr-import-wppr-answer").innerHTML='<img src="'+yasrWindowVar.loaderUrl+'" alt="yasr-loader" width="16" height="16">';var e={action:"yasr_import_wppr",nonce:document.getElementById("yasr-import-wppr-nonce").value};jQuery.post(ajaxurl,e,(function(e){document.getElementById("yasr-import-wppr-answer").innerHTML=e}))})),jQuery("#yasr-import-kksr-submit").on("click",(function(){document.getElementById("yasr-import-kksr-answer").innerHTML='<img src="'+yasrWindowVar.loaderUrl+'" alt="yasr-loader" width="16" height="16">';var e={action:"yasr_import_kksr",nonce:document.getElementById("yasr-import-kksr-nonce").value};jQuery.post(ajaxurl,e,(function(e){document.getElementById("yasr-import-kksr-answer").innerHTML=e}))})),jQuery("#yasr-import-mr-submit").on("click",(function(){document.getElementById("yasr-import-mr-answer").innerHTML='<img src="'+yasrWindowVar.loaderUrl+'" alt="yasr-loader" width="16" height="16">';var e={action:"yasr_import_mr",nonce:document.getElementById("yasr-import-mr-nonce").value};jQuery.post(ajaxurl,e,(function(e){document.getElementById("yasr-import-mr-answer").innerHTML=e}))})),wp.hooks.doAction("yasr_migration_page_bottom")),"rankings"===e&&wp.hooks.doAction("yasr_ranking_page_top")})(),(()=>{"use strict";let e;var t,a=new Uint8Array(16);function n(){if(!t&&!(t="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)||"undefined"!=typeof msCrypto&&"function"==typeof msCrypto.getRandomValues&&msCrypto.getRandomValues.bind(msCrypto)))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return t(a)}const s=/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i,o=function(e){return"string"==typeof e&&s.test(e)};for(var i=[],l=0;l<256;++l)i.push((l+256).toString(16).substr(1));const d=function(e,t,r){var a=(e=e||{}).random||(e.rng||n)();if(a[6]=15&a[6]|64,a[8]=63&a[8]|128,t){r=r||0;for(var s=0;s<16;++s)t[r+s]=a[s];return t}return function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,r=(i[e[t+0]]+i[e[t+1]]+i[e[t+2]]+i[e[t+3]]+"-"+i[e[t+4]]+i[e[t+5]]+"-"+i[e[t+6]]+i[e[t+7]]+"-"+i[e[t+8]]+i[e[t+9]]+"-"+i[e[t+10]]+i[e[t+11]]+i[e[t+12]]+i[e[t+13]]+i[e[t+14]]+i[e[t+15]]).toLowerCase();if(!o(r))throw TypeError("Stringified UUID is invalid");return r}(a)};var c=r(726),u=r.n(c);function y(e){return y="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},y(e)}function m(e,t){for(var r=0;r<t.length;r++){var a=t[r];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(e,a.key,a)}}function p(e,t){return p=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},p(e,t)}function g(e,t){if(t&&("object"===y(t)||"function"==typeof t))return t;if(void 0!==t)throw new TypeError("Derived constructors may only return object or undefined");return function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e)}function f(e){return f=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},f(e)}var h=wp.element.render;function v(e){return React.createElement("div",{dangerouslySetInnerHTML:{__html:u()(e.html,"<strong><p>")}})}function _(e){var t="yasr-ranking-element-"+d(),r=document.getElementById(e.tableId).dataset.rankingSize;return React.createElement("div",{id:t,ref:function(){return yasrSetRaterValue(r,t,!1,.1,!0,e.rating)}})}function b(e){if(void 0!==e.post.number_of_votes){var t=JSON.parse(yasrWindowVar.textAfterVr);return t=(t=t.replace("%total_count%",e.post.number_of_votes)).replace("%average%",e.post.rating),React.createElement("div",{className:"yasr-most-rated-text"},React.createElement(v,{html:t}))}var r=e.text;return React.createElement("div",{className:"yasr-highest-rated-text"},r," ",e.post.rating)}function E(t){return React.createElement("td",{className:t.colClass},React.createElement("a",{href:t.post.link},function(t){if("string"!=typeof t||-1===t.indexOf("&"))return t;void 0===e&&(e=document.implementation&&document.implementation.createHTMLDocument?document.implementation.createHTMLDocument("").createElement("textarea"):document.createElement("textarea")),e.innerHTML=t;const r=e.textContent;return e.innerHTML="",r}(t.post.title)))}function k(e){var t="after",r=JSON.parse(yasrWindowVar.textRating),a=new URLSearchParams(e.rankingParams);return null!==a.get("text_position")&&(t=a.get("text_position")),null!==a.get("custom_txt")&&(r=a.get("custom_txt")),"before"===t?React.createElement("td",{className:e.colClass},React.createElement(b,{post:e.post,tableId:e.tableId,text:r}),React.createElement(_,{rating:e.post.rating,tableId:e.tableId})):React.createElement("td",{className:e.colClass},React.createElement(_,{rating:e.post.rating,tableId:e.tableId}),React.createElement(b,{post:e.post,tableId:e.tableId,text:r}))}function w(e){var t="",r="";return"author_ranking"===e.source?(t="yasr-top-10-overall-left",r="yasr-top-10-overall-right"):"visitor_votes"===e.source&&(t="yasr-top-10-most-highest-left",r="yasr-top-10-most-highest-right"),React.createElement("tr",{className:e.trClass},React.createElement(E,{colClass:t,post:e.post}),React.createElement(k,{colClass:r,post:e.post,tableId:e.tableId,rankingParams:e.rankingParams}))}function I(e){return React.createElement("tbody",{id:e.tBodyId,style:{display:e.show}},e.data.map((function(t,r){var a="yasr-rankings-td-colored";return"author_ranking"===e.source&&(a="yasr-rankings-td-white"),r%2==0&&(a="yasr-rankings-td-white","author_ranking"===e.source&&(a="yasr-rankings-td-colored")),React.createElement(w,{key:t.post_id,source:e.source,tableId:e.tableId,rankingParams:e.rankingParams,post:t,trClass:a})})))}var B=function(e){!function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),t&&p(e,t)}(o,React.Component);var t,r,a,n,s=(a=o,n=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}(),function(){var e,t=f(a);if(n){var r=f(this).constructor;e=Reflect.construct(t,arguments,r)}else e=t.apply(this,arguments);return g(this,e)});function o(e){var t;return function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,o),(t=s.call(this,e)).state={error:null,isLoaded:!1,data:[],tableId:e.tableId,source:e.source,rankingParams:e.params,nonce:e.nonce},t}return t=o,r=[{key:"componentDidMount",value:function(){var e=this,t=JSON.parse(document.getElementById(this.state.tableId).dataset.rankingData),r={};if("yes"!==yasrWindowVar.ajaxEnabled)console.info("Ajax Disabled, getting data from source"),this.setState({isLoaded:!0,data:t});else if(this.state.source){var a=this.returnRestUrl();Promise.all(a.map((function(e){return fetch(e).then((function(e){return!0===e.ok?e.json():(console.info("Ajax Call Failed. Getting data from source"),"KO")})).then((function(e){"KO"===e?r=t:"overall_rating"===e.source||"author_multi"===e.source?r="overall_rating"===e.source?e.data_overall:e.data_mv:r[e.show]=e.data_vv})).catch((function(e){r=t,console.info(e)}))}))).then((function(t){e.setState({isLoaded:!0,data:r})})).catch((function(t){console.info(t),e.setState({isLoaded:!0,data:r})}))}else this.setState({error:"Invalid Data Source"})}},{key:"returnRestUrl",value:function(){var e,t=""!==this.state.rankingParams?this.state.rankingParams:"",r=this.state.source,a="&nonce_rankings="+this.state.nonce,n="";if(""!==t&&!1!==t){var s=new URLSearchParams(t);null!==s.get("order_by")&&(n+="order_by="+s.get("order_by")),null!==s.get("limit")&&(n+="&limit="+s.get("limit")),null!==s.get("start_date")&&"0"!==s.get("start_date")&&(n+="&start_date="+s.get("start_date")),null!==s.get("end_date")&&"0"!==s.get("end_date")&&(n+="&end_date="+s.get("end_date")),null!==s.get("ctg")?n+="&ctg="+s.get("ctg"):null!==s.get("cpt")&&(n+="&cpt="+s.get("cpt")),""!==n&&(n="&"+(n=n.replace(/\s+/g,""))),"visitor_multi"!==r&&"author_multi"!==r||null!==s.get("setid")&&(n+="&setid="+s.get("setid"))}else n="";if("author_ranking"===r||"author_multi"===r)e=[yasrWindowVar.ajaxurl+"?action=yasr_load_rankings&source="+r+n+a];else{var o="",i="";if(""!==t){var l=new URLSearchParams(t);null!==l.get("required_votes[most]")&&(o="&required_votes="+l.get("required_votes[most]")),null!==l.get("required_votes[highest]")&&(i="&required_votes="+l.get("required_votes[highest]"))}e=[yasrWindowVar.ajaxurl+"?action=yasr_load_rankings&show=most&source="+r+n+o+a,yasrWindowVar.ajaxurl+"?action=yasr_load_rankings&show=highest&source="+r+n+i+a]}return e}},{key:"rankingTableHead",value:function(e,t){var r=this.state.tableId,a="link-most-rated-posts-"+r,n="link-highest-rated-posts-"+r;if("author_ranking"!==e){var s=React.createElement("span",null,React.createElement("span",{id:a},JSON.parse(yasrWindowVar.textMostRated))," | ",React.createElement("a",{href:"#",id:n,onClick:this.switchTBody.bind(this)},JSON.parse(yasrWindowVar.textHighestRated)));return"highest"===t&&(s=React.createElement("span",null,React.createElement("span",{id:n},JSON.parse(yasrWindowVar.textHighestRated))," | ",React.createElement("a",{href:"#",id:a,onClick:this.switchTBody.bind(this)},JSON.parse(yasrWindowVar.textMostRated)))),React.createElement("thead",null,React.createElement("tr",{className:"yasr-rankings-td-colored yasr-rankings-heading"},React.createElement("th",null,JSON.parse(yasrWindowVar.textLeftColumnHeader)),React.createElement("th",null,JSON.parse(yasrWindowVar.textOrderBy),":  ",s)))}return React.createElement(React.Fragment,null)}},{key:"switchTBody",value:function(e){e.preventDefault();var t=e.target.id,r=this.state.tableId,a="link-most-rated-posts-"+r,n="link-highest-rated-posts-"+r,s="most-rated-posts-"+r,o="highest-rated-posts-"+r,i=document.getElementById(t),l=document.createElement("span");l.innerHTML=i.innerHTML,l.id=i.id,i.parentNode.replaceChild(l,i),t===a&&(document.getElementById(o).style.display="none",document.getElementById(s).style.display="",l=document.getElementById(n),i.innerHTML=l.innerHTML,i.id=l.id,l.parentNode.replaceChild(i,l)),t===n&&(document.getElementById(s).style.display="none",document.getElementById(o).style.display="",l=document.getElementById(a),i.innerHTML=l.innerHTML,i.id=l.id,l.parentNode.replaceChild(i,l))}},{key:"rankingTableBody",value:function(){var e=this.state,t=e.data,r=e.source,a=e.rankingParams;if("overall_rating"===r||"author_multi"===r)return React.createElement(I,{data:t,tableId:this.state.tableId,tBodyId:"overall_"+this.state.tableId,rankingParams:a,show:"table-row-group",source:r});var n=t.most,s=t.highest,o="table-row-group",i="none",l="most",d=o,c=i,u=new URLSearchParams(a);return null!==u.get("view")&&(l=u.get("view")),"highest"===l&&(d=i,c=o),React.createElement(React.Fragment,null,this.rankingTableHead(r,l),React.createElement(I,{data:n,tableId:this.state.tableId,tBodyId:"most-rated-posts-"+this.state.tableId,rankingParams:a,show:d,source:r}),React.createElement(I,{data:s,tableId:this.state.tableId,tBodyId:"highest-rated-posts-"+this.state.tableId,rankingParams:a,show:c,source:r}))}},{key:"render",value:function(){var e=this.state,t=e.error,r=e.isLoaded;return t?React.createElement("tbody",null,React.createElement("tr",null,React.createElement("td",null,console.log(t),"Error"))):!1===r?React.createElement("tbody",null,React.createElement("tr",null,React.createElement("td",null,JSON.parse(yasrWindowVar.textLoadRanking)))):React.createElement(React.Fragment,null,this.rankingTableBody())}}],r&&m(t.prototype,r),Object.defineProperty(t,"prototype",{writable:!1}),o}();function x(){var e=document.getElementsByClassName("yasr-stars-rankings");if(e.length>0)for(var t=0;t<e.length;t++){var r=e.item(t).id,a=JSON.parse(e.item(t).dataset.rankingSource),n=JSON.parse(e.item(t).dataset.rankingParams),s=JSON.parse(e.item(t).dataset.rankingNonce),o=document.getElementById(r);h(React.createElement(B,{source:a,tableId:r,params:n,nonce:s}),o)}}x(),tippy(document.querySelectorAll(".yasr-copy-shortcode"),{content:"Copied! Insert into your post!",theme:"yasr",arrow:"true",arrowType:"round",trigger:"click"});var R,j=wp.i18n.__;if(document.getElementsByClassName("nav-tab-active").length>0&&(R=document.getElementsByClassName("nav-tab-active")[0].id),"rankings"===R){var S,C=function(e){var t=J.value,r=document.getElementById("yasr-builder-shortcode").textContent,a=["yasr_ov_ranking","yasr_most_or_highest_rated_posts","yasr_multi_set_ranking","yasr_visitor_multi_set_ranking"];a=wp.hooks.applyFilters("yasrBuilderDrawRankingsShortcodes",a),fetch(ajaxurl+"?action=yasr_rankings_preview_shortcode&shortcode="+t+"&full_shortcode="+r).then((function(e){return!0===e.ok?e.json():(console.info(j("Ajax Call Failed. Shortcode preview can't be done","yet-another-stars-rating")),"KO")})).catch((function(e){console.info(e)})).then((function(e){if("KO"!==e){var t=document.createElement("div");t.innerHTML=e,A.childNodes.length>0?A.replaceChild(t,A.childNodes[0]):A.appendChild(t)}})).then((function(e){a.forEach((function(e){t===e&&x()}))}))},L=function(e,t,r,a,n,s,o,i,l){e.style.display="",t.style.display="",s.style.display="",l.style.display="",r.style.display="none",a.style.display="none",n.style.display="none",null!==o&&(o.style.display=""),null!==i&&(i.style.display="none")},Q=function(e,t,r,a,n,s,o,i,l){r.style.display="",a.style.display="",t.style.display="",s.style.display="",l.style.display="",e.style.display="none",n.style.display="none",null!==o&&(o.style.display=""),null!==i&&(i.style.display="none")},O=function(e,t,r,a,n,s,o,i,l){n.style.display="",l.style.display="none",e.style.display="none",r.style.display="none",a.style.display="none",t.style.display="none",s.style.display="none",null!==o&&(o.style.display="none"),null!==i&&(i.style.display="none")},T=function(e,t,r,a,n,s,o,i){var l=arguments.length>8&&void 0!==arguments[8]&&arguments[8],d=arguments.length>9?arguments[9]:void 0;!0===l?(F.className="",F.classList.add("yasr-settings-row-24"),r.style.display="",a.style.display="",e.style.display="none"):(r.style.display="none",a.style.display="none",e.style.display=""),s.style.display="",t.style.display="",d.style.display="",n.style.display="none",null!==o&&(o.style.display=""),null!==i&&(i.style.display="")},N=".yasr-builder-elements-parents",P=".yasr-builder-elements-childs";jQuery(N).prop("disabled",!0),jQuery(P).prop("disabled",!0),jQuery(N).find("input").each((function(){jQuery(this).prop("disabled",!0)})),wp.hooks.doAction("yasrBuilderBegin",N,P);var H=[],M={name:"yasr_most_or_highest_rated_posts",setid:"",rows:"",size:"",view:"",minvotesmost:"",minvoteshg:"",txtPosition:"",txt:"",display:"",start_date:"",end_date:"",category:"",cpt:""};S=M.name,document.getElementById("yasr-builder-shortcode").textContent="["+S+"]",document.getElementById("yasr-builder-copy-shortcode").setAttribute("data-shortcode","["+S+"]");var V=document.getElementById("yasr-builder-button-preview"),W=document.getElementById("yasr-builder-copy-shortcode"),A=document.getElementById("yasr-builder-preview"),J=document.getElementById("yasr-ranking-source"),U=document.getElementById("yasr-ranking-multiset-select"),q=document.getElementById("yasr-builder-datepicker-start"),D=document.getElementById("yasr-builder-datepicker-end"),F=document.getElementById("yasr-builder-params-container"),z=document.getElementById("builder-vv-default-view"),K=document.getElementById("builder-vv-required-votes"),Y=document.getElementById("builder-stars-size"),G=document.getElementById("builder-overall-text"),$=document.getElementById("builder-username-options"),X=document.getElementById("builder-category"),Z=document.getElementById("builder-cpt"),ee=document.getElementById("yasr-ranking-multiset"),te=document.getElementById("yasr-builder-datepicker"),re=J.value,ae=!1;q.value="",D.value="","yasr_ov_ranking"===re?L(G,Y,z,K,$,X,Z,ee,te):"yasr_most_active_users"===re||"yasr_top_reviewers"===re?O(G,Y,z,K,$,X,Z,ee,te):"yasr_multi_set_ranking"===re?T(G,Y,z,K,$,X,Z,ee,!1,te):"yasr_visitor_multi_set_ranking"===re?T(G,Y,z,K,$,X,Z,ee,!0,te):Q(G,Y,z,K,$,X,Z,ee,te),document.addEventListener("change",(function(e){if("yasr-ranking-source"===e.target.id)F.className="",F.classList.add("yasr-settings-row-33"),A.innerHTML="",q.value="",D.value="",M={name:"yasr_most_or_highest_rated_posts",setid:"",rows:"",size:"",view:"",minvotesmost:"",minvoteshg:"",txtPosition:"",txt:"",display:"",start_date:"",end_date:"",category:"",cpt:""},"yasr_ov_ranking"===e.target.value?L(G,Y,z,K,$,X,Z,ee,te):"yasr_most_active_users"===e.target.value||"yasr_top_reviewers"===e.target.value?O(G,Y,z,K,$,X,Z,ee,te):"yasr_multi_set_ranking"===e.target.value?(T(G,Y,z,K,$,X,Z,ee,!1,te),M.setid=" setid="+U[0].value):"yasr_visitor_multi_set_ranking"===e.target.value?(T(G,Y,z,K,$,X,Z,ee,!0,te),M.setid=" setid="+U[0].value):Q(G,Y,z,K,$,X,Z,ee,te),M.name=e.target.value,S=M.name+M.setid;else{"yasr-ranking-multiset-select"===e.target.id&&(M.setid=" setid="+e.target.value),H=wp.hooks.applyFilters("yasrBuilderFilterShortcode",M);for(var t=2;M.length;t++)H.hasOwnProperty(M[t])&&(M[t]=H[t]);S=M.name+M.setid+M.rows+M.view+M.minvotesmost+M.minvoteshg+M.size+M.txtPosition+M.txt+M.display+M.start_date+M.end_date+M.category+M.cpt}document.getElementById("yasr-builder-shortcode").textContent="["+S+"]",document.getElementById("yasr-builder-copy-shortcode").setAttribute("data-shortcode","["+S+"]"),!0===ae&&"yasr-ranking-source"!==e.target.id&&"yasr-builder-category-radio"!==e.target.name&&C()})),W.onclick=function(e){var t,r;t=document.getElementById(e.target.id).getAttribute("data-shortcode"),(r=document.createElement("textarea")).value=t,r.setAttribute("readonly",""),r.style.position="absolute",r.style.left="-9999px",document.body.appendChild(r),r.select(),document.execCommand("copy"),document.body.removeChild(r)},V.onclick=function(e){C(),ae=!0}}})()})();
1
+ (()=>{var e={726:function(e,t,r){"use strict";var a;!function(n){if("function"!=typeof s){var s=function(e){return e};s.nonNative=!0}const o=s("plaintext"),i=s("html"),l=s("comment"),d=/<(\w*)>/g,c=/<\/?([^\s\/>]+)/;function u(e,t,r){return y(e=e||"",m(t=t||[],r=r||""))}function m(e,t){return{allowable_tags:e=function(e){let t=new Set;if("string"==typeof e){let r;for(;r=d.exec(e);)t.add(r[1])}else s.nonNative||"function"!=typeof e[s.iterator]?"function"==typeof e.forEach&&e.forEach(t.add,t):t=new Set(e);return t}(e),tag_replacement:t,state:o,tag_buffer:"",depth:0,in_quote_char:""}}function y(e,t){if("string"!=typeof e)throw new TypeError("'html' parameter must be a string");let r=t.allowable_tags,a=t.tag_replacement,n=t.state,s=t.tag_buffer,d=t.depth,c=t.in_quote_char,u="";for(let t=0,m=e.length;t<m;t++){let m=e[t];if(n===o)if("<"===m)n=i,s+=m;else u+=m;else if(n===i)switch(m){case"<":if(c)break;d++;break;case">":if(c)break;if(d){d--;break}c="",n=o,s+=">",r.has(p(s))?u+=s:u+=a,s="";break;case'"':case"'":c=m===c?"":c||m,s+=m;break;case"-":"<!-"===s&&(n=l),s+=m;break;case" ":case"\n":if("<"===s){n=o,u+="< ",s="";break}s+=m;break;default:s+=m}else if(n===l)if(">"===m)"--"==s.slice(-2)&&(n=o),s="";else s+=m}return t.state=n,t.tag_buffer=s,t.depth=d,t.in_quote_char=c,u}function p(e){let t=c.exec(e);return t?t[1].toLowerCase():null}u.init_streaming_mode=function(e,t){let r=m(e=e||[],t=t||"");return function(e){return y(e||"",r)}},void 0===(a=function(){return u}.call(t,r,t,e))||(e.exports=a)}()}},t={};function r(a){var n=t[a];if(void 0!==n)return n.exports;var s=t[a]={exports:{}};return e[a].call(s.exports,s,s.exports,r),s.exports}r.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return r.d(t,{a:t}),t},r.d=(e,t)=>{for(var a in t)r.o(t,a)&&!r.o(e,a)&&Object.defineProperty(e,a,{enumerable:!0,get:t[a]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),(()=>{var e;if(document.getElementsByClassName("nav-tab-active").length>0&&(e=document.getElementsByClassName("nav-tab-active")[0].id),"general_settings"===e){var t=document.getElementById("yasr_auto_insert_switch").checked,r=document.getElementById("yasr-general-options-stars-title-switch").checked;!1===t&&jQuery(".yasr-auto-insert-options-class").prop("disabled",!0),!1===r&&jQuery(".yasr-stars-title-options-class").prop("disabled",!0),document.getElementById("yasr_auto_insert_switch").addEventListener("change",(function(){this.checked?jQuery(".yasr-auto-insert-options-class").prop("disabled",!1):jQuery(".yasr-auto-insert-options-class").prop("disabled",!0)})),document.getElementById("yasr-general-options-stars-title-switch").addEventListener("change",(function(){this.checked?jQuery(".yasr-stars-title-options-class").prop("disabled",!1):jQuery(".yasr-stars-title-options-class").prop("disabled",!0)})),document.getElementById("yasr-settings-custom-texts").addEventListener("click",(function(){document.getElementById("yasr-settings-custom-text-before-overall").value="Our Score",document.getElementById("yasr-settings-custom-text-before-visitor").value="Click to rate this post!",document.getElementById("yasr-settings-custom-text-after-visitor").value="[Total: %total_count% Average: %average%]",document.getElementById("yasr-settings-custom-text-rating-saved").value="Rating saved!",document.getElementById("yasr-settings-custom-text-rating-updated").value="Rating updated!",document.getElementById("yasr-settings-custom-text-must-sign-in").value="You must sign in to vote",document.getElementById("yasr-settings-custom-text-already-rated").value="You have already voted for this article with %rating%"}))}if("style_options"===e&&(wp.codeEditor.initialize(document.getElementById("yasr_style_options_textarea"),yasr_cm_settings),jQuery("#yasr-color-scheme-preview-link").on("click",(function(){return jQuery("#yasr-color-scheme-preview").toggle("slow"),!1})),wp.hooks.doAction("yasrStyleOptions")),"manage_multi"===e){var a=parseInt(document.getElementById("n-multiset").value);if(jQuery("#yasr-multi-set-doc-link").on("click",(function(){jQuery("#yasr-multi-set-doc-box").toggle("slow")})),jQuery("#yasr-multi-set-doc-link-hide").on("click",(function(){jQuery("#yasr-multi-set-doc-box").toggle("slow")})),1===a){var n=jQuery("#yasr-edit-form-number-elements").attr("value");n++,jQuery("#yasr-add-field-edit-multiset").on("click",(function(){if(n>9)return jQuery("#yasr-element-limit").show(),jQuery("#yasr-add-field-edit-multiset").hide(),!1;var e=jQuery(document.createElement("tr"));e.html('<td colspan="2">Element #'+n+' <input type="text" name="edit-multi-set-element-'+n+'" value="" ></td>'),e.appendTo("#yasr-table-form-edit-multi-set"),n++}))}else a>1&&(jQuery("#yasr-button-select-set-edit-form").on("click",(function(){var e={action:"yasr_get_multi_set",set_id:jQuery("#yasr_select_edit_set").val()};return jQuery.post(ajaxurl,e,(function(e){jQuery("#yasr-multi-set-response").show(),jQuery("#yasr-multi-set-response").html(e)})),!1})),jQuery(document).ajaxComplete((function(){var e=jQuery("#yasr-edit-form-number-elements").attr("value");e++,jQuery("#yasr-add-field-edit-multiset").on("click",(function(){if(e>9)return jQuery("#yasr-element-limit").show(),jQuery("#yasr-add-field-edit-multiset").hide(),!1;var t=jQuery(document.createElement("tr"));t.html('<td colspan="2">Element #'+e+' <input type="text" name="edit-multi-set-element-'+e+'" value="" ></td>'),t.appendTo("#yasr-table-form-edit-multi-set"),e++}))})))}"migration_tools"===e&&(jQuery("#yasr-import-ratemypost-submit").on("click",(function(){document.getElementById("yasr-import-ratemypost-answer").innerHTML='<img src="'+yasrWindowVar.loaderUrl+'" alt="yasr-loader" width="16" height="16">';var e={action:"yasr_import_ratemypost",nonce:document.getElementById("yasr-import-rmp-nonce").value};jQuery.post(ajaxurl,e,(function(e){e=JSON.parse(e),document.getElementById("yasr-import-ratemypost-answer").innerHTML=e}))})),jQuery("#yasr-import-wppr-submit").on("click",(function(){document.getElementById("yasr-import-wppr-answer").innerHTML='<img src="'+yasrWindowVar.loaderUrl+'" alt="yasr-loader" width="16" height="16">';var e={action:"yasr_import_wppr",nonce:document.getElementById("yasr-import-wppr-nonce").value};jQuery.post(ajaxurl,e,(function(e){document.getElementById("yasr-import-wppr-answer").innerHTML=e}))})),jQuery("#yasr-import-kksr-submit").on("click",(function(){document.getElementById("yasr-import-kksr-answer").innerHTML='<img src="'+yasrWindowVar.loaderUrl+'" alt="yasr-loader" width="16" height="16">';var e={action:"yasr_import_kksr",nonce:document.getElementById("yasr-import-kksr-nonce").value};jQuery.post(ajaxurl,e,(function(e){document.getElementById("yasr-import-kksr-answer").innerHTML=e}))})),jQuery("#yasr-import-mr-submit").on("click",(function(){document.getElementById("yasr-import-mr-answer").innerHTML='<img src="'+yasrWindowVar.loaderUrl+'" alt="yasr-loader" width="16" height="16">';var e={action:"yasr_import_mr",nonce:document.getElementById("yasr-import-mr-nonce").value};jQuery.post(ajaxurl,e,(function(e){document.getElementById("yasr-import-mr-answer").innerHTML=e}))})),wp.hooks.doAction("yasr_migration_page_bottom")),"rankings"===e&&wp.hooks.doAction("yasr_ranking_page_top")})(),(()=>{"use strict";let e;var t,a=function(t){var r=t.colClass,a=t.post,n=a.link,s=a.title;return React.createElement("td",{className:r},React.createElement("a",{href:n},function(t){if("string"!=typeof t||-1===t.indexOf("&"))return t;void 0===e&&(e=document.implementation&&document.implementation.createHTMLDocument?document.implementation.createHTMLDocument("").createElement("textarea"):document.createElement("textarea")),e.innerHTML=t;const r=e.textContent;return e.innerHTML="",r}(s)))},n=new Uint8Array(16);function s(){if(!t&&!(t="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)||"undefined"!=typeof msCrypto&&"function"==typeof msCrypto.getRandomValues&&msCrypto.getRandomValues.bind(msCrypto)))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return t(n)}const o=/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;const i=function(e){return"string"==typeof e&&o.test(e)};for(var l=[],d=0;d<256;++d)l.push((d+256).toString(16).substr(1));const c=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,r=(l[e[t+0]]+l[e[t+1]]+l[e[t+2]]+l[e[t+3]]+"-"+l[e[t+4]]+l[e[t+5]]+"-"+l[e[t+6]]+l[e[t+7]]+"-"+l[e[t+8]]+l[e[t+9]]+"-"+l[e[t+10]]+l[e[t+11]]+l[e[t+12]]+l[e[t+13]]+l[e[t+14]]+l[e[t+15]]).toLowerCase();if(!i(r))throw TypeError("Stringified UUID is invalid");return r};const u=function(e,t,r){var a=(e=e||{}).random||(e.rng||s)();if(a[6]=15&a[6]|64,a[8]=63&a[8]|128,t){r=r||0;for(var n=0;n<16;++n)t[r+n]=a[n];return t}return c(a)};var m=r(726),y=r.n(m),p=function(e){var t=e.html;return React.createElement("div",{dangerouslySetInnerHTML:{__html:y()(t,"<strong><p>")}})},g=function(e){var t=e.post,r=t.number_of_votes,a=t.rating,n=e.text;if(void 0!==r){var s=JSON.parse(yasrWindowVar.textAfterVr);return s=(s=s.replace("%total_count%",r)).replace("%average%",a),React.createElement("div",{className:"yasr-most-rated-text"},React.createElement(p,{html:s}))}return React.createElement("span",null,n," ",a)},f=function(e){var t=e.size,r=e.htmlId,a=e.element,n=e.step,s=e.readonly,o=e.rating;return React.createElement("div",{id:r,ref:function(){return yasrSetRaterValue(t,r,a,n,s,o)}})},h=function(e){var t=e.rankingParams,r=e.tableId,a=e.colClass,n=e.post,s="after",o=JSON.parse(yasrWindowVar.textRating),i=new URLSearchParams(t);null!==i.get("text_position")&&(s=i.get("text_position")),null!==i.get("custom_txt")&&(o=i.get("custom_txt"));var l={rating:n.rating,htmlId:"yasr-ranking-element-"+u(),size:document.getElementById(r).dataset.rankingSize};return"before"===s?React.createElement("td",{className:a},React.createElement(g,{post:n,text:o}),React.createElement(f,l)):React.createElement("td",{className:a},React.createElement(f,l),React.createElement(g,{post:n,text:o}))},v=function(e){var t={colClass:e.leftClass,post:e.post},r={rankingParams:e.rankingParams,tableId:e.tableId,colClass:e.rightClass,post:e.post};return React.createElement("tr",{className:e.trClass},React.createElement(a,t),React.createElement(h,r))},_=function(e){var t=e.tBodyId,r=e.show,a=e.data,n=e.source,s=e.rankingParams,o=e.tableId;return React.createElement("tbody",{id:t,style:{display:r}},a.map((function(e,t){var r="yasr-rankings-td-colored",a="yasr-top-10-most-highest-left",i="yasr-top-10-most-highest-right";return"author_ranking"===n&&(r="yasr-rankings-td-white",a="yasr-top-10-overall-left",i="yasr-top-10-overall-right"),t%2==0&&(r="yasr-rankings-td-white","author_ranking"===n&&(r="yasr-rankings-td-colored")),React.createElement(v,{key:e.post_id,source:n,tableId:o,rankingParams:s,post:e,trClass:r,leftClass:a,rightClass:i})})))},E=function(e){return function(t){t.preventDefault();var r=t.target.id,a=e.tableId,n="link-most-rated-posts-"+a,s="link-highest-rated-posts-"+a,o="most-rated-posts-"+a,i="highest-rated-posts-"+a,l=document.getElementById(r),d=document.createElement("span");d.innerHTML=l.innerHTML,d.id=l.id,l.parentNode.replaceChild(d,l),r===n&&(document.getElementById(i).style.display="none",document.getElementById(o).style.display="",d=document.getElementById(s),l.innerHTML=d.innerHTML,l.id=d.id,d.parentNode.replaceChild(l,d)),r===s&&(document.getElementById(o).style.display="none",document.getElementById(i).style.display="",d=document.getElementById(n),l.innerHTML=d.innerHTML,l.id=d.id,d.parentNode.replaceChild(l,d))}},b=function(e){var t=e.tableId,r=e.source,a=e.defaultView,n="link-most-rated-posts-"+t,s="link-highest-rated-posts-"+t;if("author_ranking"!==r){var o=React.createElement("span",null,React.createElement("span",{id:n},JSON.parse(yasrWindowVar.textMostRated))," | ",React.createElement("a",{href:"#",id:s,onClick:E(e)},JSON.parse(yasrWindowVar.textHighestRated)));return"highest"===a&&(o=React.createElement("span",null,React.createElement("span",{id:s},JSON.parse(yasrWindowVar.textHighestRated))," | ",React.createElement("a",{href:"#",id:n,onClick:E(e)},JSON.parse(yasrWindowVar.textMostRated)))),React.createElement("thead",null,React.createElement("tr",{className:"yasr-rankings-td-colored yasr-rankings-heading"},React.createElement("th",null,JSON.parse(yasrWindowVar.textLeftColumnHeader)),React.createElement("th",null,JSON.parse(yasrWindowVar.textOrderBy),":  ",o)))}return React.createElement(React.Fragment,null)},w=function(e){var t=e.error,r=e.isLoaded,a=e.data,n=e.source,s=e.rankingParams,o=e.tableId;if(t)return React.createElement("tbody",null,React.createElement("tr",null,React.createElement("td",null,console.log(t),"Error")));if(!1===r)return React.createElement("tbody",null,React.createElement("tr",null,React.createElement("td",null,JSON.parse(yasrWindowVar.textLoadRanking))));if("overall_rating"===n||"author_multi"===n)return React.createElement(_,{data:a,tableId:o,tBodyId:"overall_"+o,rankingParams:s,show:"table-row-group",source:n});var i=a.most,l=a.highest,d="table-row-group",c="none",u="most",m=d,y=c,p=new URLSearchParams(s);return null!==p.get("view")&&(u=p.get("view")),"highest"===u&&(m=c,y=d),React.createElement(React.Fragment,null,React.createElement(b,{tableId:o,source:n,defaultView:u}),React.createElement(_,{data:i,tableId:o,tBodyId:"most-rated-posts-"+o,rankingParams:s,show:m,source:n}),React.createElement(_,{data:l,tableId:o,tBodyId:"highest-rated-posts-"+o,rankingParams:s,show:y,source:n}))};function k(){return k=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(e[a]=r[a])}return e},k.apply(this,arguments)}function I(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null==r)return;var a,n,s=[],o=!0,i=!1;try{for(r=r.call(e);!(o=(a=r.next()).done)&&(s.push(a.value),!t||s.length!==t);o=!0);}catch(e){i=!0,n=e}finally{try{o||null==r.return||r.return()}finally{if(i)throw n}}return s}(e,t)||function(e,t){if(!e)return;if("string"==typeof e)return B(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);"Object"===r&&e.constructor&&(r=e.constructor.name);if("Map"===r||"Set"===r)return Array.from(e);if("Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r))return B(e,t)}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function B(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,a=new Array(t);r<t;r++)a[r]=e[r];return a}var x=wp.element,j=x.useState,R=x.useEffect,C=function(e,t,r){var a=t,n="&nonce_rankings="+r,s=function(e,t){var r="";if(""!==e&&!1!==e){var a=new URLSearchParams(e);null!==a.get("order_by")&&(r+="order_by="+a.get("order_by")),null!==a.get("limit")&&(r+="&limit="+a.get("limit")),null!==a.get("start_date")&&"0"!==a.get("start_date")&&(r+="&start_date="+a.get("start_date")),null!==a.get("end_date")&&"0"!==a.get("end_date")&&(r+="&end_date="+a.get("end_date")),null!==a.get("ctg")?r+="&ctg="+a.get("ctg"):null!==a.get("cpt")&&(r+="&cpt="+a.get("cpt")),""!==r&&(r="&"+(r=r.replace(/\s+/g,""))),"visitor_multi"!==t&&"author_multi"!==t||null!==a.get("setid")&&(r+="&setid="+a.get("setid")),"visitor_votes"===t&&(null!==a.get("required_votes[most]")&&(r="&required_votes="+a.get("required_votes[most]")),null!==a.get("required_votes[highest]")&&(r="&required_votes="+a.get("required_votes[highest]")))}return r}(""!==e?e:"",a);return"author_ranking"===a||"author_multi"===a||"overall_rating"===a?[yasrWindowVar.ajaxurl+"?action=yasr_load_rankings&source="+a+s+n]:[yasrWindowVar.ajaxurl+"?action=yasr_load_rankings&show=most&source="+a+s+n,yasrWindowVar.ajaxurl+"?action=yasr_load_rankings&show=highest&source="+a+s+n]},Q=function(e){var t=e.tableId,r=e.source,a=e.params,n=e.nonce,s={tableId:t,source:r,rankingParams:a},o=I(j(null),2),i=o[0],l=o[1],d=I(j(!1),2),c=d[0],u=d[1],m=I(j([]),2),y=m[0],p=m[1],g=function(){var e=arguments.length>0&&void 0!==arguments[0]&&arguments[0],r=JSON.parse(document.getElementById(t).dataset.rankingData);!0===e&&console.info("Ajax Disabled, getting data from source"),p(r)};return R((function(){var e,t;"yes"!==yasrWindowVar.ajaxEnabled?(g(!0),u(!0)):r?(e=[],t=C(a,r,n),Promise.all(t.map((function(t){return fetch(t).then((function(e){return!0===e.ok?e.json():(console.info("Ajax Call Failed. Getting data from source"),"KO")})).then((function(t){"KO"===t?g():("overall_rating"===t.source||"author_multi"===t.source?e="overall_rating"===t.source?t.data_overall:t.data_mv:e[t.show]=t.data_vv,p(e))})).catch((function(e){g(),console.info(e)}))}))).then((function(e){u(!0)})).catch((function(e){g(),console.info(e)}))):l("Invalid Data Source")}),[]),React.createElement(React.Fragment,null,React.createElement(w,k({error:i,isLoaded:c,data:y},s)))},S=wp.element.render,L=function(){var e=document.getElementsByClassName("yasr-stars-rankings");if(e.length>0)for(var t=0;t<e.length;t++){var r=e.item(t).id,a=JSON.parse(e.item(t).dataset.rankingSource),n=JSON.parse(e.item(t).dataset.rankingParams),s=JSON.parse(e.item(t).dataset.rankingNonce),o=document.getElementById(r);S(React.createElement(Q,{source:a,tableId:r,params:n,nonce:s}),o)}};L();tippy(document.querySelectorAll(".yasr-copy-shortcode"),{content:"Copied! Insert into your post!",theme:"yasr",arrow:"true",arrowType:"round",trigger:"click"});var N,O=wp.i18n.__;if(document.getElementsByClassName("nav-tab-active").length>0&&(N=document.getElementsByClassName("nav-tab-active")[0].id),"rankings"===N){var T,V=function(e){var t=K.value,r=document.getElementById("yasr-builder-shortcode").textContent,a=["yasr_ov_ranking","yasr_most_or_highest_rated_posts","yasr_multi_set_ranking","yasr_visitor_multi_set_ranking"];a=wp.hooks.applyFilters("yasrBuilderDrawRankingsShortcodes",a),fetch(ajaxurl+"?action=yasr_rankings_preview_shortcode&shortcode="+t+"&full_shortcode="+r).then((function(e){return!0===e.ok?e.json():(console.info(O("Ajax Call Failed. Shortcode preview can't be done","yet-another-stars-rating")),"KO")})).catch((function(e){console.info(e)})).then((function(e){if("KO"!==e){var t=document.createElement("div");t.innerHTML=e,F.childNodes.length>0?F.replaceChild(t,F.childNodes[0]):F.appendChild(t)}})).then((function(e){a.forEach((function(e){t===e&&L()}))}))},M=function(e,t,r,a,n,s,o,i,l){e.style.display="",t.style.display="",s.style.display="",l.style.display="",r.style.display="none",a.style.display="none",n.style.display="none",null!==o&&(o.style.display=""),null!==i&&(i.style.display="none")},H=function(e,t,r,a,n,s,o,i,l){r.style.display="",a.style.display="",t.style.display="",s.style.display="",l.style.display="",e.style.display="none",n.style.display="none",null!==o&&(o.style.display=""),null!==i&&(i.style.display="none")},P=function(e,t,r,a,n,s,o,i,l){n.style.display="",l.style.display="none",e.style.display="none",r.style.display="none",a.style.display="none",t.style.display="none",s.style.display="none",null!==o&&(o.style.display="none"),null!==i&&(i.style.display="none")},A=function(e,t,r,a,n,s,o,i){var l=arguments.length>8&&void 0!==arguments[8]&&arguments[8],d=arguments.length>9?arguments[9]:void 0;!0===l?(X.className="",X.classList.add("yasr-settings-row-24"),r.style.display="",a.style.display="",e.style.display="none"):(r.style.display="none",a.style.display="none",e.style.display=""),s.style.display="",t.style.display="",d.style.display="",n.style.display="none",null!==o&&(o.style.display=""),null!==i&&(i.style.display="")},W=".yasr-builder-elements-parents",J=".yasr-builder-elements-childs";jQuery(W).prop("disabled",!0),jQuery(J).prop("disabled",!0),jQuery(W).find("input").each((function(){jQuery(this).prop("disabled",!0)})),wp.hooks.doAction("yasrBuilderBegin",W,J);var q=[],U={name:"yasr_most_or_highest_rated_posts",setid:"",rows:"",size:"",view:"",minvotesmost:"",minvoteshg:"",txtPosition:"",txt:"",display:"",start_date:"",end_date:"",category:"",cpt:""};T=U.name,document.getElementById("yasr-builder-shortcode").textContent="["+T+"]",document.getElementById("yasr-builder-copy-shortcode").setAttribute("data-shortcode","["+T+"]");var z=document.getElementById("yasr-builder-button-preview"),D=document.getElementById("yasr-builder-copy-shortcode"),F=document.getElementById("yasr-builder-preview"),K=document.getElementById("yasr-ranking-source"),Y=document.getElementById("yasr-ranking-multiset-select"),$=document.getElementById("yasr-builder-datepicker-start"),G=document.getElementById("yasr-builder-datepicker-end"),X=document.getElementById("yasr-builder-params-container"),Z=document.getElementById("builder-vv-default-view"),ee=document.getElementById("builder-vv-required-votes"),te=document.getElementById("builder-stars-size"),re=document.getElementById("builder-overall-text"),ae=document.getElementById("builder-username-options"),ne=document.getElementById("builder-category"),se=document.getElementById("builder-cpt"),oe=document.getElementById("yasr-ranking-multiset"),ie=document.getElementById("yasr-builder-datepicker"),le=K.value,de=!1;$.value="",G.value="","yasr_ov_ranking"===le?M(re,te,Z,ee,ae,ne,se,oe,ie):"yasr_most_active_users"===le||"yasr_top_reviewers"===le?P(re,te,Z,ee,ae,ne,se,oe,ie):"yasr_multi_set_ranking"===le?A(re,te,Z,ee,ae,ne,se,oe,!1,ie):"yasr_visitor_multi_set_ranking"===le?A(re,te,Z,ee,ae,ne,se,oe,!0,ie):H(re,te,Z,ee,ae,ne,se,oe,ie),document.addEventListener("change",(function(e){if("yasr-ranking-source"===e.target.id)X.className="",X.classList.add("yasr-settings-row-33"),F.innerHTML="",$.value="",G.value="",U={name:"yasr_most_or_highest_rated_posts",setid:"",rows:"",size:"",view:"",minvotesmost:"",minvoteshg:"",txtPosition:"",txt:"",display:"",start_date:"",end_date:"",category:"",cpt:""},"yasr_ov_ranking"===e.target.value?M(re,te,Z,ee,ae,ne,se,oe,ie):"yasr_most_active_users"===e.target.value||"yasr_top_reviewers"===e.target.value?P(re,te,Z,ee,ae,ne,se,oe,ie):"yasr_multi_set_ranking"===e.target.value?(A(re,te,Z,ee,ae,ne,se,oe,!1,ie),U.setid=" setid="+Y[0].value):"yasr_visitor_multi_set_ranking"===e.target.value?(A(re,te,Z,ee,ae,ne,se,oe,!0,ie),U.setid=" setid="+Y[0].value):H(re,te,Z,ee,ae,ne,se,oe,ie),U.name=e.target.value,T=U.name+U.setid;else{"yasr-ranking-multiset-select"===e.target.id&&(U.setid=" setid="+e.target.value),q=wp.hooks.applyFilters("yasrBuilderFilterShortcode",U);for(var t=2;U.length;t++)q.hasOwnProperty(U[t])&&(U[t]=q[t]);T=U.name+U.setid+U.rows+U.view+U.minvotesmost+U.minvoteshg+U.size+U.txtPosition+U.txt+U.display+U.start_date+U.end_date+U.category+U.cpt}document.getElementById("yasr-builder-shortcode").textContent="["+T+"]",document.getElementById("yasr-builder-copy-shortcode").setAttribute("data-shortcode","["+T+"]"),!0===de&&"yasr-ranking-source"!==e.target.id&&"yasr-builder-category-radio"!==e.target.name&&V()})),D.onclick=function(e){!function(e){var t=document.createElement("textarea");t.value=e,t.setAttribute("readonly",""),t.style.position="absolute",t.style.left="-9999px",document.body.appendChild(t),t.select(),document.execCommand("copy"),document.body.removeChild(t)}(document.getElementById(e.target.id).getAttribute("data-shortcode"))},z.onclick=function(e){V(),de=!0}}})()})();
freemius/includes/class-freemius.php CHANGED
@@ -1,25417 +1,25387 @@
1
- <?php
2
- /**
3
- * @package Freemius
4
- * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
6
- * @since 1.0.3
7
- */
8
- if ( ! defined( 'ABSPATH' ) ) {
9
- exit;
10
- }
11
-
12
- // "final class"
13
- class Freemius extends Freemius_Abstract {
14
- /**
15
- * SDK Version
16
- *
17
- * @var string
18
- */
19
- public $version = WP_FS__SDK_VERSION;
20
-
21
- #region Plugin Info
22
-
23
- /**
24
- * @since 1.0.1
25
- *
26
- * @var string
27
- */
28
- private $_slug;
29
-
30
- /**
31
- * @since 1.0.0
32
- *
33
- * @var string
34
- */
35
- private $_plugin_basename;
36
- /**
37
- * @since 2.2.1
38
- *
39
- * @var string
40
- */
41
- private $_premium_plugin_basename;
42
- /**
43
- * @since 1.0.0
44
- *
45
- * @var string
46
- */
47
- private $_free_plugin_basename;
48
- /**
49
- * @since 1.0.0
50
- *
51
- * @var string
52
- */
53
- private $_plugin_dir_path;
54
- /**
55
- * @since 1.0.0
56
- *
57
- * @var string
58
- */
59
- private $_plugin_dir_name;
60
- /**
61
- * @since 1.0.0
62
- *
63
- * @var string
64
- */
65
- private $_plugin_main_file_path;
66
- /**
67
- * @var string[]
68
- */
69
- private $_plugin_data;
70
- /**
71
- * @since 1.0.9
72
- *
73
- * @var string
74
- */
75
- private $_plugin_name;
76
- /**
77
- * @since 1.2.2
78
- *
79
- * @var string
80
- */
81
- private $_module_type;
82
-
83
- #endregion Plugin Info
84
-
85
- /**
86
- * @since 1.0.9
87
- *
88
- * @var bool If false, don't turn Freemius on.
89
- */
90
- private $_is_on;
91
-
92
- /**
93
- * @since 1.1.3
94
- *
95
- * @var bool If false, don't turn Freemius on.
96
- */
97
- private $_is_anonymous;
98
-
99
- /**
100
- * @since 1.0.9
101
- * @var bool If false, issues with connectivity to Freemius API.
102
- */
103
- private $_has_api_connection;
104
-
105
- /**
106
- * @since 1.0.9
107
- * @since 2.0.0 Default to true since we need the property during the instance construction, prior to the dynamic_init() execution.
108
- * @var bool Hints the SDK if plugin can support anonymous mode (if skip connect is visible).
109
- */
110
- private $_enable_anonymous = true;
111
-
112
- /**
113
- * @since 1.1.7.5
114
- * @var bool Hints the SDK if plugin should run in anonymous mode (only adds feedback form).
115
- */
116
- private $_anonymous_mode;
117
-
118
- /**
119
- * @since 1.1.9
120
- * @var bool Hints the SDK if plugin have any free plans.
121
- */
122
- private $_is_premium_only;
123
-
124
- /**
125
- * @since 1.2.1.6
126
- * @var bool Hints the SDK if plugin have premium code version at all.
127
- */
128
- private $_has_premium_version;
129
-
130
- /**
131
- * @since 1.2.1.6
132
- * @var bool Hints the SDK if plugin should ignore pending mode by simulating a skip.
133
- */
134
- private $_ignore_pending_mode;
135
-
136
- /**
137
- * @since 1.0.8
138
- * @var bool Hints the SDK if the plugin has any paid plans.
139
- */
140
- private $_has_paid_plans;
141
-
142
- /**
143
- * @since 1.2.1.5
144
- * @var int Hints the SDK if the plugin offers a trial period. If negative, no trial, if zero - has a trial but
145
- * without a specified period, if positive - the number of trial days.
146
- */
147
- private $_trial_days = - 1;
148
-
149
- /**
150
- * @since 1.2.1.5
151
- * @var bool Hints the SDK if the trial requires a payment method or not.
152
- */
153
- private $_is_trial_require_payment = false;
154
-
155
- /**
156
- * @since 1.0.7
157
- * @var bool Hints the SDK if the plugin is WordPress.org compliant.
158
- */
159
- private $_is_org_compliant;
160
-
161
- /**
162
- * @since 1.0.7
163
- * @var bool Hints the SDK if the plugin is has add-ons.
164
- */
165
- private $_has_addons;
166
-
167
- /**
168
- * @since 2.4.5
169
- * @var string Navigation type: 'menu' or 'tabs'.
170
- */
171
- private $_navigation;
172
-
173
- const NAVIGATION_MENU = 'menu';
174
- const NAVIGATION_TABS = 'tabs';
175
-
176
- /**
177
- * @since 1.1.6
178
- * @var string[]bool.
179
- */
180
- private $_permissions;
181
-
182
- /**
183
- * @var FS_Storage
184
- */
185
- private $_storage;
186
-
187
- /**
188
- * @since 1.2.2.7
189
- * @var FS_Cache_Manager
190
- */
191
- private $_cache;
192
-
193
- /**
194
- * @since 1.0.0
195
- *
196
- * @var FS_Logger
197
- */
198
- private $_logger;
199
- /**
200
- * @since 1.0.4
201
- *
202
- * @var FS_Plugin
203
- */
204
- private $_plugin = false;
205
- /**
206
- * @since 1.0.4
207
- *
208
- * @var FS_Plugin|false
209
- */
210
- private $_parent_plugin = false;
211
- /**
212
- * @since 1.1.1
213
- *
214
- * @var Freemius
215
- */
216
- private $_parent = false;
217
- /**
218
- * @since 1.0.1
219
- *
220
- * @var FS_User
221
- */
222
- private $_user = false;
223
- /**
224
- * @since 1.0.1
225
- *
226
- * @var FS_Site
227
- */
228
- private $_site = false;
229
- /**
230
- * @since 1.0.1
231
- *
232
- * @var FS_Plugin_License
233
- */
234
- private $_license;
235
- /**
236
- * @since 1.0.2
237
- *
238
- * @var FS_Plugin_Plan[]
239
- */
240
- private $_plans = false;
241
- /**
242
- * @var FS_Plugin_License[]
243
- * @since 1.0.5
244
- */
245
- private $_licenses = false;
246
-
247
- /**
248
- * @since 1.0.1
249
- *
250
- * @var FS_Admin_Menu_Manager
251
- */
252
- private $_menu;
253
-
254
- /**
255
- * @var FS_Admin_Notices
256
- */
257
- private $_admin_notices;
258
-
259
- /**
260
- * @since 1.1.6
261
- *
262
- * @var FS_Admin_Notices
263
- */
264
- private static $_global_admin_notices;
265
-
266
- /**
267
- * @var FS_Logger
268
- * @since 1.0.0
269
- */
270
- private static $_static_logger;
271
-
272
- /**
273
- * @var FS_Options
274
- * @since 1.0.2
275
- */
276
- private static $_accounts;
277
-
278
- /**
279
- * @since 1.2.2
280
- *
281
- * @var number
282
- */
283
- private $_module_id;
284
-
285
- /**
286
- * @var Freemius[]
287
- */
288
- private static $_instances = array();
289
-
290
- /**
291
- * @since 1.2.3
292
- *
293
- * @var FS_Affiliate
294
- */
295
- private $affiliate = null;
296
-
297
- /**
298
- * @since 1.2.3
299
- *
300
- * @var FS_AffiliateTerms
301
- */
302
- private $plugin_affiliate_terms = null;
303
-
304
- /**
305
- * @since 1.2.3
306
- *
307
- * @var FS_AffiliateTerms
308
- */
309
- private $custom_affiliate_terms = null;
310
-
311
- /**
312
- * @since 2.0.0
313
- *
314
- * @var bool
315
- */
316
- private $_is_multisite_integrated;
317
-
318
- /**
319
- * @since 2.0.0
320
- *
321
- * @var bool True if the current request is for a network admin screen and the plugin is network active.
322
- */
323
- private $_is_network_active;
324
-
325
- /**
326
- * @since 2.0.0
327
- *
328
- * @var int|null The original blog ID the plugin was loaded with.
329
- */
330
- private $_blog_id = null;
331
-
332
- /**
333
- * @since 2.0.0
334
- *
335
- * @var int|null The current execution context. When true, run on network context. When int, run on the specified blog context.
336
- */
337
- private $_context_is_network_or_blog_id = null;
338
-
339
- /**
340
- * @since 2.0.0
341
- *
342
- * @var string
343
- */
344
- private $_dynamically_added_top_level_page_hook_name = '';
345
-
346
- /**
347
- * @author Leo Fajardo (@leorw)
348
- * @since 2.3.1
349
- *
350
- * @var bool
351
- */
352
- private $is_whitelabeled;
353
-
354
- /**
355
- * @author Leo Fajardo (@leorw)
356
- * @since 2.4.0
357
- *
358
- * @var bool
359
- */
360
- private $_is_bundle_license_auto_activation_enabled = false;
361
-
362
- #region Uninstall Reasons IDs
363
-
364
- const REASON_NO_LONGER_NEEDED = 1;
365
- const REASON_FOUND_A_BETTER_PLUGIN = 2;
366
- const REASON_NEEDED_FOR_A_SHORT_PERIOD = 3;
367
- const REASON_BROKE_MY_SITE = 4;
368
- const REASON_SUDDENLY_STOPPED_WORKING = 5;
369
- const REASON_CANT_PAY_ANYMORE = 6;
370
- const REASON_OTHER = 7;
371
- const REASON_DIDNT_WORK = 8;
372
- const REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION = 9;
373
- const REASON_COULDNT_MAKE_IT_WORK = 10;
374
- const REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE = 11;
375
- const REASON_NOT_WORKING = 12;
376
- const REASON_NOT_WHAT_I_WAS_LOOKING_FOR = 13;
377
- const REASON_DIDNT_WORK_AS_EXPECTED = 14;
378
- const REASON_TEMPORARY_DEACTIVATION = 15;
379
-
380
- /**
381
- * @author Leo Fajardo (@leorw)
382
- * @since 2.3.1
383
- *
384
- * @var boolean|null
385
- */
386
- private $_use_external_pricing = null;
387
- /**
388
- * @author Leo Fajardo (@leorw)
389
- * @since 2.4.2
390
- *
391
- * @var string|null
392
- */
393
- private $_pricing_js_path = null;
394
-
395
- #endregion
396
-
397
- /* Ctor
398
- ------------------------------------------------------------------------------------------------------------------*/
399
-
400
- /**
401
- * Main singleton instance.
402
- *
403
- * @author Vova Feldman (@svovaf)
404
- * @since 1.0.0
405
- *
406
- * @param number $module_id
407
- * @param string|bool $slug
408
- * @param bool $is_init Since 1.2.1 Is initiation sequence.
409
- */
410
- private function __construct( $module_id, $slug = false, $is_init = false ) {
411
- if ( $is_init && is_numeric( $module_id ) && is_string( $slug ) ) {
412
- $this->store_id_slug_type_path_map( $module_id, $slug );
413
- }
414
-
415
- $this->_module_id = $module_id;
416
- $this->_slug = $this->get_slug();
417
- $this->_module_type = $this->get_module_type();
418
-
419
- $this->_blog_id = is_multisite() ? get_current_blog_id() : null;
420
-
421
- $this->_storage = FS_Storage::instance( $this->_module_type, $this->_slug );
422
-
423
- $this->_cache = FS_Cache_Manager::get_manager( WP_FS___OPTION_PREFIX . "cache_{$module_id}" );
424
-
425
- $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $this->get_unique_affix(), WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
426
-
427
- $this->_plugin_main_file_path = $this->_find_caller_plugin_file( $is_init );
428
- $this->_plugin_dir_path = plugin_dir_path( $this->_plugin_main_file_path );
429
- $this->_plugin_basename = $this->get_plugin_basename();
430
- $this->_free_plugin_basename = str_replace( '-premium/', '/', $this->_plugin_basename );
431
-
432
- $this->_is_multisite_integrated = (
433
- defined( "WP_FS__PRODUCT_{$module_id}_MULTISITE" ) &&
434
- ( true === constant( "WP_FS__PRODUCT_{$module_id}_MULTISITE" ) )
435
- );
436
-
437
- $this->_is_network_active = (
438
- is_multisite() &&
439
- $this->_is_multisite_integrated &&
440
- // Themes are always network activated, but the ACTUAL activation is per site.
441
- $this->is_plugin() &&
442
- (
443
- is_plugin_active_for_network( $this->_plugin_basename ) ||
444
- // Plugin network level activation or uninstall.
445
- ( fs_is_network_admin() && is_plugin_inactive( $this->_plugin_basename ) )
446
- )
447
- );
448
-
449
- $this->_storage->set_network_active(
450
- $this->_is_network_active,
451
- $this->is_delegated_connection()
452
- );
453
-
454
- if ( ! isset( $this->_storage->is_network_activated ) ) {
455
- $this->_storage->is_network_activated = $this->_is_network_active;
456
- }
457
-
458
- if ( $this->_storage->is_network_activated != $this->_is_network_active ) {
459
- // Update last activation level.
460
- $this->_storage->is_network_activated = $this->_is_network_active;
461
-
462
- $this->maybe_adjust_storage();
463
- }
464
-
465
- #region Migration
466
-
467
- if ( is_multisite() ) {
468
- /**
469
- * If the install_timestamp exists on the site level but doesn't exist on the
470
- * network level storage, it means that we need to process the storage with migration.
471
- *
472
- * 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.
473
- *
474
- * @author Vova Feldman (@svovaf)
475
- * @since 2.0.0
476
- */
477
- if ( false === $this->_storage->get( 'install_timestamp', false, true ) &&
478
- false !== $this->_storage->get( 'install_timestamp', false, false )
479
- ) {
480
- // Initiate storage migration.
481
- $this->_storage->migrate_to_network();
482
-
483
- // Migrate module cache to network level storage.
484
- $this->_cache->migrate_to_network();
485
- }
486
- }
487
-
488
- #endregion
489
-
490
- $base_name_split = explode( '/', $this->_plugin_basename );
491
- $this->_plugin_dir_name = $base_name_split[0];
492
-
493
- if ( $this->_logger->is_on() ) {
494
- $this->_logger->info( 'plugin_main_file_path = ' . $this->_plugin_main_file_path );
495
- $this->_logger->info( 'plugin_dir_path = ' . $this->_plugin_dir_path );
496
- $this->_logger->info( 'plugin_basename = ' . $this->_plugin_basename );
497
- $this->_logger->info( 'free_plugin_basename = ' . $this->_free_plugin_basename );
498
- $this->_logger->info( 'plugin_dir_name = ' . $this->_plugin_dir_name );
499
- }
500
-
501
- // Remember link between file to slug.
502
- $this->store_file_slug_map();
503
-
504
- // Store plugin's initial install timestamp.
505
- if ( ! isset( $this->_storage->install_timestamp ) ) {
506
- $this->_storage->install_timestamp = WP_FS__SCRIPT_START_TIME;
507
- }
508
-
509
- if ( ! is_object( $this->_plugin ) ) {
510
- $this->_plugin = FS_Plugin_Manager::instance( $this->_module_id )->get();
511
- }
512
-
513
- $this->_admin_notices = FS_Admin_Notices::instance(
514
- $this->_slug . ( $this->is_theme() ? ':theme' : '' ),
515
- /**
516
- * Ensure that the admin notice will always have a title by using the stored plugin title if available and
517
- * retrieving the title via the "get_plugin_name" method if there is no stored plugin title available.
518
- *
519
- * @author Leo Fajardo (@leorw)
520
- * @since 1.2.2
521
- */
522
- ( is_object( $this->_plugin ) ? $this->_plugin->title : $this->get_plugin_name() ),
523
- $this->get_unique_affix()
524
- );
525
-
526
- if ( 'true' === fs_request_get( 'fs_clear_api_cache' ) ||
527
- fs_request_is_action( 'restart_freemius' )
528
- ) {
529
- FS_Api::clear_cache();
530
- $this->_cache->clear();
531
- }
532
-
533
- $this->register_constructor_hooks();
534
-
535
- /**
536
- * Starting from version 2.0.0, `FS_Site` entities no longer have the `plan` property and have `plan_id`
537
- * instead. This should be called before calling `_load_account()`, otherwise, `$this->_site` will not be
538
- * loaded in `_load_account` for versions of SDK starting from 2.0.0.
539
- *
540
- * @author Leo Fajardo (@leorw)
541
- */
542
- self::migrate_install_plan_to_plan_id( $this->_storage );
543
-
544
- $this->_load_account();
545
-
546
- $this->_version_updates_handler();
547
- }
548
-
549
- /**
550
- * @author Leo Fajardo (@leorw)
551
- * @since 2.3.0
552
- */
553
- private function maybe_adjust_storage() {
554
- $install_timestamp = null;
555
- $prev_is_premium = null;
556
-
557
- $options_to_update = array();
558
-
559
- $is_network_admin = fs_is_network_admin();
560
-
561
- $network_install_timestamp = $this->_storage->get( 'install_timestamp', null, true );
562
-
563
- if ( ! $is_network_admin ) {
564
- if ( is_null( $network_install_timestamp ) ) {
565
- // Plugin was not network-activated before.
566
- return;
567
- }
568
-
569
- if ( is_null( $this->_storage->get( 'install_timestamp', null, false ) ) ) {
570
- // Set the `install_timestamp` only if it's not yet set.
571
- $install_timestamp = $network_install_timestamp;
572
- }
573
-
574
- $prev_is_premium = $this->_storage->get( 'prev_is_premium', null, true );
575
- } else {
576
- $current_wp_user = self::_get_current_wp_user();
577
- $current_fs_user = self::_get_user_by_email( $current_wp_user->user_email );
578
- $network_user_info = array();
579
-
580
- $skips_count = 0;
581
-
582
- $sites = self::get_sites();
583
- $sites_count = count( $sites );
584
-
585
- $blog_id_2_install_map = array();
586
-
587
- $is_first_non_ignored_blog = true;
588
-
589
- foreach ( $sites as $site ) {
590
- $blog_id = self::get_site_blog_id( $site );
591
-
592
- $blog_install_timestamp = $this->_storage->get( 'install_timestamp', null, $blog_id );
593
-
594
- if ( is_null( $blog_install_timestamp ) ) {
595
- // Plugin has not been installed on this blog.
596
- continue;
597
- }
598
-
599
- $is_earlier_install = (
600
- ! is_null( $install_timestamp ) &&
601
- $blog_install_timestamp < $install_timestamp
602
- );
603
-
604
- $install = $this->get_install_by_blog_id( $blog_id );
605
-
606
- $update_network_user_info = false;
607
-
608
- if ( ! is_object( $install ) ) {
609
- if ( ! $this->_storage->get( 'is_anonymous', false, $blog_id ) ) {
610
- // The opt-in decision (whether to skip or opt in) is yet to be made.
611
- continue;
612
- }
613
-
614
- $skips_count ++;
615
- } else {
616
- $blog_id_2_install_map[ $blog_id ] = $install;
617
-
618
- if ( empty( $network_user_info ) ) {
619
- // Set the network user info for the 1st time. Choose any user information whether or not it is for the current WP user.
620
- $update_network_user_info = true;
621
- }
622
-
623
- if ( ! $update_network_user_info &&
624
- is_object( $current_fs_user ) &&
625
- $network_user_info['user_id'] != $current_fs_user->id &&
626
- $install->user_id == $current_fs_user->id
627
- ) {
628
- // If an install that is owned by the current WP user is found, use its user information instead.
629
- $update_network_user_info = true;
630
- }
631
-
632
- if ( ! $update_network_user_info &&
633
- $is_earlier_install &&
634
- ( ! is_object( $current_fs_user ) || $current_fs_user->id == $install->user_id )
635
- ) {
636
- // Update to the earliest install info if there's no install found so far that is owned by the current WP user; OR only if the found install is owned by the current WP user.
637
- $update_network_user_info = true;
638
- }
639
- }
640
-
641
- if ( $update_network_user_info ) {
642
- $network_user_info = array(
643
- 'user_id' => $install->user_id,
644
- 'blog_id' => $blog_id
645
- );
646
- }
647
-
648
- $site_prev_is_premium = $this->_storage->get( 'prev_is_premium', null, $blog_id );
649
-
650
- if ( $is_first_non_ignored_blog ) {
651
- $prev_is_premium = $site_prev_is_premium;
652
-
653
- if ( is_null( $network_install_timestamp ) ) {
654
- $install_timestamp = $blog_install_timestamp;
655
- }
656
-
657
- $is_first_non_ignored_blog = false;
658
-
659
- continue;
660
- }
661
-
662
- if ( ! is_null( $prev_is_premium ) && $prev_is_premium !== $site_prev_is_premium ) {
663
- // If a different `$site_prev_is_premium` value is found, do not include the option in the collection of options to update.
664
- $prev_is_premium = null;
665
- }
666
-
667
- if ( $is_earlier_install ) {
668
- // If an earlier install timestamp is found.
669
- $install_timestamp = $blog_install_timestamp;
670
- }
671
- }
672
-
673
- $installs_count = count( $blog_id_2_install_map );
674
-
675
- if ( $sites_count === ( $installs_count + $skips_count ) ) {
676
- if ( ! empty( $network_user_info ) ) {
677
- $options_to_update['network_user_id'] = $network_user_info['user_id'];
678
- $options_to_update['network_install_blog_id'] = $network_user_info['blog_id'];
679
-
680
- foreach ( $blog_id_2_install_map as $blog_id => $install ) {
681
- if ( $install->user_id == $network_user_info['user_id'] ) {
682
- continue;
683
- }
684
-
685
- $this->_storage->store( 'is_delegated_connection', true, $blog_id );
686
- }
687
- }
688
-
689
- if ( $sites_count === $skips_count ) {
690
- /**
691
- * Assume network-level skipping as the intended action if all actions identified were only
692
- * skipping of the connection (i.e., no opt-ins and delegated connections so far).
693
- */
694
- $options_to_update['is_anonymous_ms'] = true;
695
- } else if ( $sites_count === $installs_count ) {
696
- /**
697
- * Assume network-level opt-in as the intended action if all actions identified were only opt-ins
698
- * (i.e., no delegation and skipping of the connections so far).
699
- */
700
- $options_to_update['is_network_connected'] = true;
701
- }
702
- }
703
- }
704
-
705
- if ( ! is_null( $install_timestamp ) ) {
706
- $options_to_update['install_timestamp'] = $install_timestamp;
707
- }
708
-
709
- if ( ! is_null( $prev_is_premium ) ) {
710
- $options_to_update['prev_is_premium'] = $prev_is_premium;
711
- }
712
-
713
- if ( ! empty( $options_to_update ) ) {
714
- $this->adjust_storage( $options_to_update, $is_network_admin );
715
- }
716
- }
717
-
718
- /**
719
- * @author Leo Fajardo (@leorw)
720
- * @since 2.3.0
721
- *
722
- * @param array $options
723
- * @param bool $is_network_admin
724
- */
725
- private function adjust_storage( $options, $is_network_admin ) {
726
- foreach ( $options as $name => $value ) {
727
- $this->_storage->store( $name, $value, $is_network_admin ? true : null );
728
- }
729
- }
730
-
731
- /**
732
- * Checks whether this module has a settings menu.
733
- *
734
- * @author Leo Fajardo (@leorw)
735
- * @since 1.2.2
736
- *
737
- * @return bool
738
- */
739
- function has_settings_menu() {
740
- return ( $this->_is_network_active && fs_is_network_admin() ) ?
741
- $this->_menu->has_network_menu() :
742
- $this->_menu->has_menu();
743
- }
744
-
745
- /**
746
- * If `true` the opt-in should be shown as a modal dialog box on the themes.php page. WordPress.org themes guidelines prohibit from redirecting the user from the themes.php page after activating a theme.
747
- *
748
- * @author Vova Feldman (@svovaf)
749
- * @since 2.4.5
750
- *
751
- * @return bool
752
- */
753
- function show_opt_in_on_themes_page() {
754
- if ( ! $this->is_free_wp_org_theme() ) {
755
- return false;
756
- }
757
-
758
- if ( ! $this->has_settings_menu() ) {
759
- return true;
760
- }
761
-
762
- return $this->show_settings_with_tabs();
763
- }
764
-
765
- /**
766
- * If `true` the opt-in should be shown on the product's main setting page.
767
- *
768
- * @author Vova Feldman (@svovaf)
769
- * @since 2.4.5
770
- *
771
- * @return bool
772
- *
773
- * @uses show_opt_in_on_themes_page();
774
- */
775
- function show_opt_in_on_setting_page() {
776
- return ! $this->show_opt_in_on_themes_page();
777
- }
778
-
779
- /**
780
- * If `true` the settings should be shown using tabs.
781
- *
782
- * @author Vova Feldman (@svovaf)
783
- * @since 2.4.5
784
- *
785
- * @return bool
786
- */
787
- function show_settings_with_tabs() {
788
- return ( self::NAVIGATION_TABS === $this->_navigation );
789
- }
790
-
791
- /**
792
- * Check if the context module is free wp.org theme.
793
- *
794
- * This method is helpful because:
795
- * 1. wp.org themes are limited to a single submenu item,
796
- * and sub-submenu items are most likely not allowed (never verified).
797
- * 2. wp.org themes are not allowed to redirect the user
798
- * after the theme activation, therefore, the agreed UX
799
- * is showing the opt-in as a modal dialog box after
800
- * activation (approved by @otto42, @emiluzelac, @greenshady, @grapplerulrich).
801
- *
802
- * @author Vova Feldman (@svovaf)
803
- * @since 1.2.2.7
804
- *
805
- * @return bool
806
- */
807
- function is_free_wp_org_theme() {
808
- return (
809
- $this->is_theme() &&
810
- $this->is_org_repo_compliant() &&
811
- ! $this->is_premium()
812
- );
813
- }
814
-
815
- /**
816
- * Checks whether this a submenu item is visible.
817
- *
818
- * @author Vova Feldman (@svovaf)
819
- * @since 1.2.2.6
820
- * @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.
821
- *
822
- * @param string $slug
823
- * @param bool $is_tabs_visibility_check This is used to decide if the associated tab should be shown or hidden.
824
- *
825
- * @return bool
826
- */
827
- function is_submenu_item_visible( $slug, $is_tabs_visibility_check = false ) {
828
- if ( $this->is_admin_page( $slug ) ) {
829
- /**
830
- * It is the current context page, so show the submenu item
831
- * so the user will have the right context page, even if it
832
- * was set to hidden.
833
- */
834
- return true;
835
- }
836
-
837
- if ( ! $this->has_settings_menu() ) {
838
- // No menu settings at all.
839
- return false;
840
- }
841
-
842
- if (
843
- ! $is_tabs_visibility_check &&
844
- $this->is_org_repo_compliant() &&
845
- $this->show_settings_with_tabs()
846
- ) {
847
- /**
848
- * wp.org themes are limited to a single submenu item, and
849
- * sub-submenu items are most likely not allowed (never verified).
850
- */
851
- return false;
852
- }
853
-
854
- return $this->_menu->is_submenu_item_visible( $slug );
855
- }
856
-
857
- /**
858
- * Check if a Freemius page should be accessible via the UI.
859
- *
860
- * @author Vova Feldman (@svovaf)
861
- * @since 1.2.2.7
862
- *
863
- * @param string $slug
864
- *
865
- * @return bool
866
- */
867
- function is_page_visible( $slug ) {
868
- if ( $this->is_admin_page( $slug ) ) {
869
- return true;
870
- }
871
-
872
- return $this->_menu->is_submenu_item_visible( $slug, true, true );
873
- }
874
-
875
- /**
876
- * @author Vova Feldman (@svovaf)
877
- * @since 1.0.9
878
- */
879
- private function _version_updates_handler() {
880
- if ( ! isset( $this->_storage->sdk_version ) || $this->_storage->sdk_version != $this->version ) {
881
- // Freemius version upgrade mode.
882
- $this->_storage->sdk_last_version = $this->_storage->sdk_version;
883
- $this->_storage->sdk_version = $this->version;
884
-
885
- if ( empty( $this->_storage->sdk_last_version ) ||
886
- version_compare( $this->_storage->sdk_last_version, $this->version, '<' )
887
- ) {
888
- $this->_storage->sdk_upgrade_mode = true;
889
- $this->_storage->sdk_downgrade_mode = false;
890
- } else {
891
- $this->_storage->sdk_downgrade_mode = true;
892
- $this->_storage->sdk_upgrade_mode = false;
893
-
894
- }
895
-
896
- $this->do_action( 'sdk_version_update', $this->_storage->sdk_last_version, $this->version );
897
- }
898
-
899
- $plugin_version = $this->get_plugin_version();
900
- if ( ! isset( $this->_storage->plugin_version ) || $this->_storage->plugin_version != $plugin_version ) {
901
- // Plugin version upgrade mode.
902
- $this->_storage->plugin_last_version = $this->_storage->plugin_version;
903
- $this->_storage->plugin_version = $plugin_version;
904
-
905
- if ( empty( $this->_storage->plugin_last_version ) ||
906
- version_compare( $this->_storage->plugin_last_version, $plugin_version, '<' )
907
- ) {
908
- $this->_storage->plugin_upgrade_mode = true;
909
- $this->_storage->plugin_downgrade_mode = false;
910
- } else {
911
- $this->_storage->plugin_downgrade_mode = true;
912
- $this->_storage->plugin_upgrade_mode = false;
913
- }
914
-
915
- if ( ! empty( $this->_storage->plugin_last_version ) ) {
916
- // Different version of the plugin was installed before, therefore it's an update.
917
- $this->_storage->is_plugin_new_install = false;
918
- }
919
-
920
- $this->do_action( 'plugin_version_update', $this->_storage->plugin_last_version, $plugin_version );
921
- }
922
- }
923
-
924
- #--------------------------------------------------------------------------------
925
- #region Data Migration on SDK Update
926
- #--------------------------------------------------------------------------------
927
-
928
- /**
929
- * @author Vova Feldman (@svovaf)
930
- * @since 1.1.5
931
- *
932
- * @param string $sdk_prev_version
933
- * @param string $sdk_version
934
- */
935
- function _sdk_version_update( $sdk_prev_version, $sdk_version ) {
936
- /**
937
- * @since 1.1.7.3 Fixed unwanted connectivity test cleanup.
938
- */
939
- if ( empty( $sdk_prev_version ) ) {
940
- return;
941
- }
942
-
943
- if ( version_compare( $sdk_prev_version, '2.1.0', '<' ) &&
944
- version_compare( $sdk_version, '2.1.0', '>=' )
945
- ) {
946
- $this->_storage->handle_gdpr_admin_notice = true;
947
- }
948
-
949
- if ( version_compare( $sdk_prev_version, '2.0.0', '<' ) &&
950
- version_compare( $sdk_version, '2.0.0', '>=' )
951
- ) {
952
- $this->migrate_to_subscriptions_collection();
953
-
954
- $this->consolidate_licenses();
955
-
956
- // Clear trial_plan since it's now loaded from the plans collection when needed.
957
- $this->_storage->remove( 'trial_plan', true, false );
958
- }
959
-
960
- if ( version_compare( $sdk_prev_version, '1.2.3', '<' ) &&
961
- version_compare( $sdk_version, '1.2.3', '>=' )
962
- ) {
963
- /**
964
- * Starting from version 1.2.3, paths are stored as relative instead of absolute and some of them can be
965
- * invalid.
966
- *
967
- * @author Leo Fajardo (@leorw)
968
- */
969
- $this->remove_invalid_paths();
970
- }
971
-
972
- if ( version_compare( $sdk_prev_version, '1.1.5', '<' ) &&
973
- version_compare( $sdk_version, '1.1.5', '>=' )
974
- ) {
975
- // On version 1.1.5 merged connectivity and is_on data.
976
- if ( isset( $this->_storage->connectivity_test ) ) {
977
- if ( ! isset( $this->_storage->is_on ) ) {
978
- unset( $this->_storage->connectivity_test );
979
- } else {
980
- $connectivity_data = $this->_storage->connectivity_test;
981
- $connectivity_data['is_active'] = $this->_storage->is_on['is_active'];
982
- $connectivity_data['timestamp'] = $this->_storage->is_on['timestamp'];
983
-
984
- // Override.
985
- $this->_storage->connectivity_test = $connectivity_data;
986
-
987
- // Remove previous structure.
988
- unset( $this->_storage->is_on );
989
- }
990
-
991
- }
992
- }
993
-
994
- if (
995
- version_compare( $sdk_prev_version, '2.2.1', '<' ) &&
996
- version_compare( $sdk_version, '2.2.1', '>=' )
997
- ) {
998
- /**
999
- * Clear the file cache without storing the previous path since it could be a wrong path. For example,
1000
- * 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
1001
- * as the parent plugin's when the add-on was auto-installed since the relevant method names were not
1002
- * skipped in the logic that determines the right path in the `get_caller_main_file_and_type` method
1003
- * (e.g. `try_activate_plugin`). Since it was an auto-installation, the caller was the parent plugin
1004
- * and so its path was used. In case the stored path is wrong, clearing the cache will resolve issues
1005
- * related to data mix-up between plugins (e.g. titles and versions of an add-on and its parent plugin).
1006
- *
1007
- * @author Leo Fajardo (@leorw)
1008
- * @since 2.2.1
1009
- */
1010
- $this->clear_module_main_file_cache( false );
1011
- }
1012
- }
1013
-
1014
- /**
1015
- * @author Leo Fajardo (@leorw)
1016
- * @since 2.0.0
1017
- *
1018
- * @param \FS_Storage $storage
1019
- * @param bool|int|null $blog_id
1020
- */
1021
- private static function migrate_install_plan_to_plan_id( FS_Storage $storage, $blog_id = null ) {
1022
- if ( empty( $storage->sdk_version ) ) {
1023
- // New installation of the plugin, no need to upgrade.
1024
- return;
1025
- }
1026
-
1027
- if ( ! version_compare( $storage->sdk_version, '2.0.0', '<' ) ) {
1028
- // Previous version is >= 2.0.0, so no need to migrate.
1029
- return;
1030
- }
1031
-
1032
- // Alias.
1033
- $module_type = $storage->get_module_type();
1034
- $module_slug = $storage->get_module_slug();
1035
-
1036
- $installs = self::get_all_sites( $module_type, $blog_id );
1037
- $install = isset( $installs[ $module_slug ] ) ? $installs[ $module_slug ] : null;
1038
-
1039
- if ( ! is_object( $install ) ) {
1040
- return;
1041
- }
1042
-
1043
- if ( isset( $install->plan ) && is_object( $install->plan ) ) {
1044
- if ( isset( $install->plan->id ) && ! empty( $install->plan->id ) ) {
1045
- $install->plan_id = self::_decrypt( $install->plan->id );
1046
- }
1047
-
1048
- unset( $install->plan );
1049
-
1050
- $installs[ $module_slug ] = clone $install;
1051
-
1052
- self::set_account_option_by_module(
1053
- $module_type,
1054
- 'sites',
1055
- $installs,
1056
- true,
1057
- $blog_id
1058
- );
1059
- }
1060
- }
1061
-
1062
- /**
1063
- * @author Leo Fajardo (@leorw)
1064
- * @since 2.0.0
1065
- */
1066
- private function migrate_to_subscriptions_collection() {
1067
- if ( ! is_object( $this->_site ) ) {
1068
- return;
1069
- }
1070
-
1071
- if ( isset( $this->_storage->subscription ) && is_object( $this->_storage->subscription ) ) {
1072
- $this->_storage->subscriptions = array( fs_get_entity( $this->_storage->subscription, FS_Subscription::get_class_name() ) );
1073
- }
1074
- }
1075
-
1076
- /**
1077
- * @author Leo Fajardo (@leorw)
1078
- * @since 2.0.0
1079
- */
1080
- private function consolidate_licenses() {
1081
- $plugin_licenses = self::get_account_option( 'licenses', WP_FS__MODULE_TYPE_PLUGIN );
1082
- if ( isset( $plugin_licenses[ $this->_slug ] ) ) {
1083
- $plugin_licenses = $plugin_licenses[ $this->_slug ];
1084
- } else {
1085
- $plugin_licenses = array();
1086
- }
1087
-
1088
- $theme_licenses = self::get_account_option( 'licenses', WP_FS__MODULE_TYPE_THEME );
1089
- if ( isset( $theme_licenses[ $this->_slug ] ) ) {
1090
- $theme_licenses = $theme_licenses[ $this->_slug ];
1091
- } else {
1092
- $theme_licenses = array();
1093
- }
1094
-
1095
- if ( empty( $plugin_licenses ) && empty( $theme_licenses ) ) {
1096
- return;
1097
- }
1098
-
1099
- $all_licenses = array();
1100
- $user_id_license_ids_map = array();
1101
-
1102
- foreach ( $plugin_licenses as $user_id => $user_licenses ) {
1103
- if ( is_array( $user_licenses ) ) {
1104
- if ( ! isset( $user_license_ids[ $user_id ] ) ) {
1105
- $user_id_license_ids_map[ $user_id ] = array();
1106
- }
1107
-
1108
- foreach ( $user_licenses as $user_license ) {
1109
- $all_licenses[] = $user_license;
1110
- $user_id_license_ids_map[ $user_id ][] = $user_license->id;
1111
- }
1112
- }
1113
- }
1114
-
1115
- foreach ( $theme_licenses as $user_id => $user_licenses ) {
1116
- if ( is_array( $user_licenses ) ) {
1117
- if ( ! isset( $user_license_ids[ $user_id ] ) ) {
1118
- $user_id_license_ids_map[ $user_id ] = array();
1119
- }
1120
-
1121
- foreach ( $user_licenses as $user_license ) {
1122
- $all_licenses[] = $user_license;
1123
- $user_id_license_ids_map[ $user_id ][] = $user_license->id;
1124
- }
1125
- }
1126
- }
1127
-
1128
- self::store_user_id_license_ids_map(
1129
- $user_id_license_ids_map,
1130
- $this->_module_id
1131
- );
1132
-
1133
- $this->_store_licenses( true, $this->_module_id, $all_licenses );
1134
- }
1135
-
1136
- /**
1137
- * Remove invalid paths.
1138
- *
1139
- * @author Leo Fajardo (@leorw)
1140
- * @since 1.2.3
1141
- */
1142
- private function remove_invalid_paths() {
1143
- // Remove invalid path that is still associated with the current slug if there's any.
1144
- $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
1145
- foreach ( $file_slug_map as $plugin_basename => $slug ) {
1146
- if ( $slug === $this->_slug &&
1147
- $plugin_basename !== $this->_plugin_basename &&
1148
- ! file_exists( $this->get_absolute_path( $plugin_basename ) )
1149
- ) {
1150
- unset( $file_slug_map[ $plugin_basename ] );
1151
- self::$_accounts->set_option( 'file_slug_map', $file_slug_map, true );
1152
-
1153
- break;
1154
- }
1155
- }
1156
- }
1157
-
1158
- /**
1159
- * @author Vova Feldman (@svovaf)
1160
- * @since 1.2.2.7
1161
- *
1162
- * @param string $plugin_prev_version
1163
- * @param string $plugin_version
1164
- */
1165
- function _after_version_update( $plugin_prev_version, $plugin_version ) {
1166
- if ( $this->is_theme() ) {
1167
- // Expire the cache of the previous tabs since the theme may
1168
- // have setting updates.
1169
- $this->_cache->expire( 'tabs' );
1170
- $this->_cache->expire( 'tabs_stylesheets' );
1171
- }
1172
- }
1173
-
1174
- /**
1175
- * A special migration logic for the $_accounts, executed for all the plugins in the system:
1176
- * - Moves some data to the network level storage.
1177
- * - If the plugin's connection was skipped for all sites, set the plugin as if it was network skipped.
1178
- * - If the plugin's connection was ignored for all sites, don't do anything in terms of the network connection.
1179
- * - 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.
1180
- * - 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.
1181
- * - 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.
1182
- *
1183
- * @author Vova Feldman (@svovaf)
1184
- * @since 2.0.0
1185
- */
1186
- private static function migrate_accounts_to_network() {
1187
- $sites = self::get_sites();
1188
- $sites_count = count( $sites );
1189
- $connection_status = array();
1190
- $plugin_slugs = array();
1191
- foreach ( $sites as $site ) {
1192
- $blog_id = self::get_site_blog_id( $site );
1193
-
1194
- self::$_accounts->migrate_to_network( $blog_id );
1195
-
1196
- /**
1197
- * Build a list of all Freemius powered plugins slugs.
1198
- */
1199
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array(), $blog_id );
1200
- foreach ( $id_slug_type_path_map as $module_id => $data ) {
1201
- if ( WP_FS__MODULE_TYPE_PLUGIN === $data['type'] ) {
1202
- $plugin_slugs[ $data['slug'] ] = true;
1203
- }
1204
- }
1205
-
1206
- $installs = self::get_account_option( 'sites', WP_FS__MODULE_TYPE_PLUGIN, $blog_id );
1207
-
1208
- if ( is_array( $installs ) ) {
1209
- foreach ( $installs as $slug => $install ) {
1210
- if ( ! isset( $connection_status[ $slug ] ) ) {
1211
- $connection_status[ $slug ] = array();
1212
- }
1213
-
1214
- if ( is_object( $install ) &&
1215
- FS_Site::is_valid_id( $install->id ) &&
1216
- FS_User::is_valid_id( $install->user_id )
1217
- ) {
1218
- $connection_status[ $slug ][ $blog_id ] = $install->user_id;
1219
- }
1220
- }
1221
- }
1222
- }
1223
-
1224
- foreach ( $plugin_slugs as $slug => $true ) {
1225
- if ( ! isset( $connection_status[ $slug ] ) ) {
1226
- $connection_status[ $slug ] = array();
1227
- }
1228
-
1229
- foreach ( $sites as $site ) {
1230
- $blog_id = self::get_site_blog_id( $site );
1231
-
1232
- if ( isset( $connection_status[ $slug ][ $blog_id ] ) ) {
1233
- continue;
1234
- }
1235
-
1236
- $storage = FS_Storage::instance( WP_FS__MODULE_TYPE_PLUGIN, $slug );
1237
-
1238
- $is_anonymous = $storage->get( 'is_anonymous', null, $blog_id );
1239
-
1240
- if ( ! is_null( $is_anonymous ) ) {
1241
- // Since 1.1.3 is_anonymous is an array.
1242
- if ( is_array( $is_anonymous ) && isset( $is_anonymous['is'] ) ) {
1243
- $is_anonymous = $is_anonymous['is'];
1244
- }
1245
-
1246
- if ( is_bool( $is_anonymous ) && true === $is_anonymous ) {
1247
- $connection_status[ $slug ][ $blog_id ] = 'skipped';
1248
- }
1249
- }
1250
-
1251
- if ( ! isset( $connection_status[ $slug ][ $blog_id ] ) ) {
1252
- $connection_status[ $slug ][ $blog_id ] = 'ignored';
1253
- }
1254
- }
1255
- }
1256
-
1257
- $super_admins = array();
1258
-
1259
- foreach ( $connection_status as $slug => $blogs_status ) {
1260
- $skips = 0;
1261
- $ignores = 0;
1262
- $connections = 0;
1263
- $opted_in_users = array();
1264
- $opted_in_super_admins = array();
1265
-
1266
- $storage = FS_Storage::instance( WP_FS__MODULE_TYPE_PLUGIN, $slug );
1267
-
1268
- foreach ( $blogs_status as $blog_id => $status_or_user_id ) {
1269
- if ( 'skipped' === $status_or_user_id ) {
1270
- $skips ++;
1271
- } else if ( 'ignored' === $status_or_user_id ) {
1272
- $ignores ++;
1273
- } else if ( FS_User::is_valid_id( $status_or_user_id ) ) {
1274
- $connections ++;
1275
-
1276
- if ( ! isset( $opted_in_users[ $status_or_user_id ] ) ) {
1277
- $opted_in_users[ $status_or_user_id ] = array();
1278
- }
1279
-
1280
- $opted_in_users[ $status_or_user_id ][] = $blog_id;
1281
-
1282
- if ( isset( $super_admins[ $status_or_user_id ] ) ||
1283
- self::is_super_admin( $status_or_user_id )
1284
- ) {
1285
- // Cache super-admin data.
1286
- $super_admins[ $status_or_user_id ] = true;
1287
-
1288
- // Remember opted-in super-admins for the plugin.
1289
- $opted_in_super_admins[ $status_or_user_id ] = true;
1290
- }
1291
- }
1292
- }
1293
-
1294
- $main_super_admin_user_id = null;
1295
- $all_migrated = false;
1296
- if ( $sites_count == $skips ) {
1297
- // All sites were skipped -> network skip by copying the anonymous mode from any of the sites.
1298
- $storage->is_anonymous_ms = $storage->is_anonymous;
1299
-
1300
- $all_migrated = true;
1301
- } else if ( $sites_count == $ignores ) {
1302
- // Don't do anything, still in activation mode.
1303
-
1304
- $all_migrated = true;
1305
- } else if ( 0 < count( $opted_in_super_admins ) ) {
1306
- // Find the super-admin with the majority of installs.
1307
- $max_installs_by_super_admin = 0;
1308
- foreach ( $opted_in_super_admins as $user_id => $true ) {
1309
- $installs_count = count( $opted_in_users[ $user_id ] );
1310
-
1311
- if ( $installs_count > $max_installs_by_super_admin ) {
1312
- $max_installs_by_super_admin = $installs_count;
1313
- $main_super_admin_user_id = $user_id;
1314
- }
1315
- }
1316
-
1317
- if ( $sites_count == $connections && 1 == count( $opted_in_super_admins ) ) {
1318
- // Super-admin opted-in for all sites in the network.
1319
- $storage->is_network_connected = true;
1320
-
1321
- $all_migrated = true;
1322
- }
1323
-
1324
- // Store network user.
1325
- $storage->network_user_id = $main_super_admin_user_id;
1326
-
1327
- $storage->network_install_blog_id = ( $sites_count == $connections ) ?
1328
- // Since all sites are opted-in, associating with the main site.
1329
- get_current_blog_id() :
1330
- // Associating with the 1st found opted-in site.
1331
- $opted_in_users[ $main_super_admin_user_id ][0];
1332
-
1333
- /**
1334
- * Make sure we migrate the plan ID of the network install, otherwise, if after the migration
1335
- * the 1st page that will be loaded is the network level WP Admin and $storage->network_install_blog_id
1336
- * is different than the main site of the network, the $this->_site will not be set since the plan_id
1337
- * will be empty.
1338
- */
1339
- $storage->migrate_to_network();
1340
- self::migrate_install_plan_to_plan_id( $storage, $storage->network_install_blog_id );
1341
- } else {
1342
- // At least one opt-in. All the opt-in were created by a non-super-admin.
1343
- if ( 0 == $ignores ) {
1344
- // All sites were opted-in or skipped, all by non-super-admin. So delegate all.
1345
- $storage->store( 'is_delegated_connection', true, true );
1346
-
1347
- $all_migrated = true;
1348
- }
1349
- }
1350
-
1351
- if ( ! $all_migrated ) {
1352
- /**
1353
- * Delegate all sites that were:
1354
- * 1) Opted-in by a user that is NOT the main-super-admin.
1355
- * 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.
1356
- */
1357
- foreach ( $blogs_status as $blog_id => $status_or_user_id ) {
1358
- if ( $status_or_user_id == $main_super_admin_user_id ) {
1359
- continue;
1360
- }
1361
-
1362
- if ( FS_User::is_valid_id( $status_or_user_id ) ||
1363
- ( 'skipped' === $status_or_user_id && is_null( $main_super_admin_user_id ) )
1364
- ) {
1365
- $storage->store( 'is_delegated_connection', true, $blog_id );
1366
- }
1367
- }
1368
- }
1369
-
1370
-
1371
- if ( ( $connections + $skips > 0 ) ) {
1372
- if ( $ignores > 0 ) {
1373
- /**
1374
- * If admin already opted-in or skipped in any of the network sites, and also
1375
- * have sites which the connection decision was not yet taken, set this plugin
1376
- * into network activation mode so the super-admin can choose what to do with
1377
- * the rest of the sites.
1378
- */
1379
- self::set_network_upgrade_mode( $storage );
1380
- }
1381
- }
1382
- }
1383
- }
1384
-
1385
- /**
1386
- * Set a module into network upgrade mode.
1387
- *
1388
- * @author Vova Feldman (@svovaf)
1389
- * @since 2.0.0
1390
- *
1391
- * @param \FS_Storage $storage
1392
- *
1393
- * @return bool
1394
- */
1395
- private static function set_network_upgrade_mode( FS_Storage $storage ) {
1396
- return $storage->is_network_activation = true;
1397
- }
1398
-
1399
- /**
1400
- * Will return true after upgrading to the SDK with the network level integration,
1401
- * when the super-admin involvement is required regarding the rest of the sites.
1402
- *
1403
- * @author Vova Feldman (@svovaf)
1404
- * @since 2.0.0
1405
- *
1406
- * @return bool
1407
- */
1408
- function is_network_upgrade_mode() {
1409
- return $this->_storage->get( 'is_network_activation' );
1410
- }
1411
-
1412
- /**
1413
- * Clear flag after the upgrade mode completion.
1414
- *
1415
- * @author Vova Feldman (@svovaf)
1416
- * @since 2.0.0
1417
- *
1418
- * @return bool True if network activation was on and now completed.
1419
- */
1420
- private function network_upgrade_mode_completed() {
1421
- if ( fs_is_network_admin() && $this->is_network_upgrade_mode() ) {
1422
- $this->_storage->remove( 'is_network_activation' );
1423
-
1424
- return true;
1425
- }
1426
-
1427
- return false;
1428
- }
1429
-
1430
- #endregion
1431
-
1432
- /**
1433
- * This action is connected to the 'plugins_loaded' hook and helps to determine
1434
- * if this is a new plugin installation or a plugin update.
1435
- *
1436
- * There are 3 different use-cases:
1437
- * 1) New plugin installation right with Freemius:
1438
- * 1.1 _activate_plugin_event_hook() will be executed first
1439
- * 1.2 Since $this->_storage->is_plugin_new_install is not set,
1440
- * and $this->_storage->plugin_last_version is not set,
1441
- * $this->_storage->is_plugin_new_install will be set to TRUE.
1442
- * 1.3 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will
1443
- * be already set to TRUE.
1444
- *
1445
- * 2) Plugin update, didn't have Freemius before, and now have the SDK:
1446
- * 2.1 _activate_plugin_event_hook() will not be executed, because
1447
- * the activation hook do NOT fires on updates since WP 3.1.
1448
- * 2.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will
1449
- * be empty, therefore, it will be set to FALSE.
1450
- *
1451
- * 3) Plugin update, had Freemius in prev version as well:
1452
- * 3.1 _version_updates_handler() will be executed 1st, since FS was installed
1453
- * before, $this->_storage->plugin_last_version will NOT be empty,
1454
- * therefore, $this->_storage->is_plugin_new_install will be set to FALSE.
1455
- * 3.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install is
1456
- * already set, therefore, it will not be modified.
1457
- *
1458
- * Use-case #3 is backward compatible, #3.1 will be executed since 1.0.9.
1459
- *
1460
- * NOTE:
1461
- * The only fallback of this mechanism is if an admin updates a plugin based on use-case #2,
1462
- * and then, the next immediate PageView is the plugin's main settings page, it will not
1463
- * show the opt-in right away. The reason it will happen is because Freemius execution
1464
- * will be turned off till the plugin is fully loaded at least once
1465
- * (till $this->_storage->was_plugin_loaded is TRUE).
1466
- *
1467
- * @author Vova Feldman (@svovaf)
1468
- * @since 1.1.9
1469
- *
1470
- */
1471
- function _plugins_loaded() {
1472
- // Update flag that plugin was loaded with Freemius at least once.
1473
- $this->_storage->was_plugin_loaded = true;
1474
-
1475
- /**
1476
- * Bug fix - only set to false when it's a plugin, due to the
1477
- * execution sequence of the theme hooks and our methods, if
1478
- * this will be set for themes, Freemius will always assume
1479
- * it's a theme update.
1480
- *
1481
- * @author Vova Feldman (@svovaf)
1482
- * @since 1.2.2.2
1483
- */
1484
- if ( $this->is_plugin() &&
1485
- ! isset( $this->_storage->is_plugin_new_install )
1486
- ) {
1487
- $this->_storage->is_plugin_new_install = (
1488
- ! is_plugin_active( $this->_plugin_basename ) &&
1489
- empty( $this->_storage->plugin_last_version )
1490
- );
1491
- }
1492
- }
1493
-
1494
- /**
1495
- * Add special parameter to WP admin AJAX calls so when we
1496
- * process AJAX calls we can identify its source properly.
1497
- *
1498
- * @author Leo Fajardo (@leorw)
1499
- * @since 2.0.0
1500
- */
1501
- static function _enrich_ajax_url() {
1502
- $admin_param = is_network_admin() ?
1503
- '_fs_network_admin' :
1504
- '_fs_blog_admin';
1505
- ?>
1506
- <script type="text/javascript">
1507
- (function ($) {
1508
- $(document).ajaxSend(function (event, jqxhr, settings) {
1509
- if (settings.url &&
1510
- -1 < settings.url.indexOf('admin-ajax.php') &&
1511
- ! ( settings.url.indexOf( '<?php echo $admin_param ?>' ) > 0 )
1512
- ) {
1513
- if (
1514
- 'string' === typeof settings.data &&
1515
- settings.data.indexOf( 'action=heartbeat' ) > 0
1516
- ) {
1517
- return;
1518
- }
1519
-
1520
- if (settings.url.indexOf('?') > 0) {
1521
- settings.url += '&';
1522
- } else {
1523
- settings.url += '?';
1524
- }
1525
-
1526
- settings.url += '<?php echo $admin_param ?>=true';
1527
- }
1528
- });
1529
- })(jQuery);
1530
- </script>
1531
- <?php
1532
- }
1533
-
1534
- /**
1535
- * Opens the support forum subemenu item in a new browser page.
1536
- *
1537
- * @author Vova Feldman (@svovaf)
1538
- * @since 2.1.4
1539
- */
1540
- static function _open_support_forum_in_new_page() {
1541
- ?>
1542
- <script type="text/javascript">
1543
- (function ($) {
1544
- $('.fs-submenu-item.wp-support-forum').parent().attr( { target: '_blank', rel: 'noopener noreferrer' } );
1545
- })(jQuery);
1546
- </script>
1547
- <?php
1548
- }
1549
-
1550
- /**
1551
- * @author Vova Feldman (@svovaf)
1552
- * @since 1.0.9
1553
- */
1554
- private function register_constructor_hooks() {
1555
- $this->_logger->entrance();
1556
-
1557
- if ( is_admin() ) {
1558
- add_action( 'admin_init', array( $this, '_hook_action_links_and_register_account_hooks' ) );
1559
-
1560
- if ( $this->is_plugin() ) {
1561
- if ( self::is_plugin_install_page() && true !== fs_request_get_bool( 'fs_allow_updater_and_dialog' ) ) {
1562
- /**
1563
- * Unless the `fs_allow_updater_and_dialog` URL param exists and its value is `true`, make
1564
- * Freemius-related updates unavailable on the "Add Plugins" admin page (/plugin-install.php)
1565
- * so that they won't interfere with the .org plugins' functionalities on that page (e.g.
1566
- * updating of a .org plugin).
1567
- */
1568
- add_filter( 'site_transient_update_plugins', array( 'Freemius', '_remove_fs_updates_from_plugin_install_page' ), 10, 2 );
1569
- } else if ( self::is_plugins_page() || self::is_updates_page() ) {
1570
- /**
1571
- * On the "Plugins" and "Updates" admin pages, if there are premium or non–org-compliant plugins, modify their details dialog URLs (add a Freemius-specific param) so that the SDK can determine if the plugin information dialog should show information from Freemius.
1572
- *
1573
- * @author Leo Fajardo (@leorw)
1574
- * @since 2.2.3
1575
- */
1576
- add_action( 'admin_footer', array( 'Freemius', '_prepend_fs_allow_updater_and_dialog_flag_url_param' ) );
1577
- }
1578
-
1579
- $plugin_dir = dirname( $this->_plugin_dir_path ) . '/';
1580
-
1581
- /**
1582
- * @since 1.2.2
1583
- *
1584
- * Hook to both free and premium version activations to support
1585
- * auto deactivation on the other version activation.
1586
- */
1587
- register_activation_hook(
1588
- $plugin_dir . $this->_free_plugin_basename,
1589
- array( $this, '_activate_plugin_event_hook' )
1590
- );
1591
-
1592
- register_activation_hook(
1593
- $plugin_dir . $this->premium_plugin_basename(),
1594
- array( $this, '_activate_plugin_event_hook' )
1595
- );
1596
- } else {
1597
- add_action( 'after_switch_theme', array( $this, '_activate_theme_event_hook' ), 10, 2 );
1598
-
1599
- add_action( 'admin_footer', array( $this, '_style_premium_theme' ) );
1600
- }
1601
-
1602
- /**
1603
- * Part of the mechanism to identify new plugin install vs. plugin update.
1604
- *
1605
- * @author Vova Feldman (@svovaf)
1606
- * @since 1.1.9
1607
- */
1608
- if ( empty( $this->_storage->was_plugin_loaded ) ) {
1609
- /**
1610
- * During the plugin activation (not theme), 'plugins_loaded' will be already executed
1611
- * when the logic gets here since the activation logic first add the activate plugins,
1612
- * then triggers 'plugins_loaded', and only then include the code of the plugin that
1613
- * is activated. Which means that _plugins_loaded() will NOT be executed during the
1614
- * plugin activation, and that IS intentional.
1615
- *
1616
- * @author Vova Feldman (@svovaf)
1617
- */
1618
- if ( $this->is_plugin() &&
1619
- $this->is_activation_mode( false ) &&
1620
- 0 == did_action( 'plugins_loaded' )
1621
- ) {
1622
- add_action( 'plugins_loaded', array( $this, '_plugins_loaded' ) );
1623
- } else {
1624
- // If was activated before, then it was already loaded before.
1625
- $this->_plugins_loaded();
1626
- }
1627
- }
1628
-
1629
- if ( ! self::is_ajax() ) {
1630
- if ( ! $this->is_addon() ) {
1631
- add_action( 'init', array( $this, '_add_default_submenu_items' ), WP_FS__LOWEST_PRIORITY );
1632
- }
1633
- }
1634
-
1635
- if ( $this->_storage->handle_gdpr_admin_notice ) {
1636
- add_action( 'init', array( $this, '_maybe_show_gdpr_admin_notice' ) );
1637
- }
1638
-
1639
- add_action( 'init', array( $this, '_maybe_add_gdpr_optin_ajax_handler') );
1640
- add_action( 'init', array( $this, '_maybe_add_pricing_ajax_handler' ) );
1641
- }
1642
-
1643
- if ( $this->is_plugin() ) {
1644
- if ( $this->_is_network_active ) {
1645
- if ( version_compare( $GLOBALS['wp_version'], '5.1', '<' ) ) {
1646
- add_action( 'wpmu_new_blog', array( $this, '_after_new_blog_callback' ), 10, 6 );
1647
- } else {
1648
- add_action( 'wp_insert_site', array( $this, '_after_wp_insert_site_callback' ) );
1649
- }
1650
- }
1651
-
1652
- register_deactivation_hook( $this->_plugin_main_file_path, array( $this, '_deactivate_plugin_hook' ) );
1653
- }
1654
-
1655
- if ( is_multisite() ) {
1656
- add_action( 'deactivate_blog', array( $this, '_after_site_deactivated_callback' ) );
1657
- add_action( 'archive_blog', array( $this, '_after_site_deactivated_callback' ) );
1658
- add_action( 'make_spam_blog', array( $this, '_after_site_deactivated_callback' ) );
1659
-
1660
- if ( version_compare( $GLOBALS['wp_version'], '5.1', '<' ) ) {
1661
- add_action( 'deleted_blog', array( $this, '_after_site_deleted_callback' ), 10, 2);
1662
- } else {
1663
- add_action( 'wp_delete_site', array( $this, '_after_wpsite_deleted_callback' ) );
1664
- }
1665
-
1666
- add_action( 'activate_blog', array( $this, '_after_site_reactivated_callback' ) );
1667
- add_action( 'unarchive_blog', array( $this, '_after_site_reactivated_callback' ) );
1668
- add_action( 'make_ham_blog', array( $this, '_after_site_reactivated_callback' ) );
1669
- }
1670
-
1671
- if ( $this->is_theme() &&
1672
- self::is_customizer() &&
1673
- $this->apply_filters( 'show_customizer_upsell', true )
1674
- ) {
1675
- // Register customizer upsell.
1676
- add_action( 'customize_register', array( $this, '_customizer_register' ) );
1677
- }
1678
-
1679
- add_action( 'admin_init', array( $this, '_redirect_on_clicked_menu_link' ), WP_FS__LOWEST_PRIORITY );
1680
-
1681
- if ( $this->is_theme() && ! $this->is_migration() ) {
1682
- add_action( 'admin_init', array( $this, '_add_tracking_links' ) );
1683
- }
1684
-
1685
- add_action( 'admin_init', array( $this, '_add_license_activation' ) );
1686
- add_action( 'admin_init', array( $this, '_add_premium_version_upgrade_selection' ) );
1687
- add_action( 'admin_init', array( $this, '_add_beta_mode_update_handler' ) );
1688
- add_action( 'admin_init', array( $this, '_add_user_change_option' ) );
1689
-
1690
- $this->add_ajax_action( 'update_billing', array( $this, '_update_billing_ajax_action' ) );
1691
- $this->add_ajax_action( 'start_trial', array( $this, '_start_trial_ajax_action' ) );
1692
- $this->add_ajax_action( 'set_data_debug_mode', array( $this, '_set_data_debug_mode' ) );
1693
- $this->add_ajax_action( 'toggle_whitelabel_mode', array( $this, '_toggle_whitelabel_mode_ajax_handler' ) );
1694
-
1695
- if ( $this->_is_network_active && fs_is_network_admin() ) {
1696
- $this->add_ajax_action( 'network_activate', array( $this, '_network_activate_ajax_action' ) );
1697
- }
1698
-
1699
- $this->add_ajax_action( 'install_premium_version', array(
1700
- $this,
1701
- '_install_premium_version_ajax_action'
1702
- ) );
1703
-
1704
- $this->add_ajax_action( 'submit_affiliate_application', array( $this, '_submit_affiliate_application' ) );
1705
-
1706
- $this->add_action( 'after_plans_sync', array( $this, '_check_for_trial_plans' ) );
1707
-
1708
- $this->add_action( 'sdk_version_update', array( $this, '_sdk_version_update' ), WP_FS__DEFAULT_PRIORITY, 2 );
1709
-
1710
- $this->add_action(
1711
- 'plugin_version_update',
1712
- array( $this, '_after_version_update' ),
1713
- WP_FS__DEFAULT_PRIORITY,
1714
- 2
1715
- );
1716
- $this->add_filter( 'after_code_type_change', array( $this, '_after_code_type_change' ) );
1717
-
1718
- add_action( 'admin_init', array( $this, '_add_trial_notice' ) );
1719
- add_action( 'admin_init', array( $this, '_add_affiliate_program_notice' ) );
1720
- add_action( 'admin_enqueue_scripts', array( $this, '_enqueue_common_css' ) );
1721
-
1722
- /**
1723
- * Handle request to reset anonymous mode for `get_reconnect_url()`.
1724
- *
1725
- * @author Vova Feldman (@svovaf)
1726
- * @since 1.2.1.5
1727
- */
1728
- if ( fs_request_is_action( 'reset_anonymous_mode' ) &&
1729
- $this->get_unique_affix() === fs_request_get( 'fs_unique_affix' )
1730
- ) {
1731
- add_action( 'admin_init', array( $this, 'connect_again' ) );
1732
- }
1733
- }
1734
-
1735
- /**
1736
- * Register the required hooks right after the settings parse is completed.
1737
- *
1738
- * @author Vova Feldman (@svovaf)
1739
- * @since 2.3.1
1740
- */
1741
- private function register_after_settings_parse_hooks() {
1742
- if ( is_admin() &&
1743
- $this->is_theme() &&
1744
- $this->is_premium() &&
1745
- ! $this->has_active_valid_license()
1746
- ) {
1747
- $this->add_ajax_action(
1748
- 'delete_theme_update_data',
1749
- array( $this, '_delete_theme_update_data_action' )
1750
- );
1751
- }
1752
-
1753
- if ( $this->show_settings_with_tabs() ) {
1754
- /**
1755
- * Include the required hooks to capture the theme settings' page tabs
1756
- * and cache them.
1757
- *
1758
- * @author Vova Feldman (@svovaf)
1759
- * @since 1.2.2.7
1760
- */
1761
- if ( ! $this->_cache->has_valid( 'tabs' ) ) {
1762
- add_action( 'admin_footer', array( $this, '_tabs_capture' ) );
1763
- // Add license activation AJAX callback.
1764
- $this->add_ajax_action( 'store_tabs', array( $this, '_store_tabs_ajax_action' ) );
1765
-
1766
- add_action( 'admin_enqueue_scripts', array( $this, '_store_tabs_styles' ), 9999999 );
1767
- }
1768
-
1769
- add_action(
1770
- 'admin_footer',
1771
- array( $this, '_add_freemius_tabs' ),
1772
- /**
1773
- * The tabs JS code must be executed after the tabs capture logic (_tabs_capture()).
1774
- * That's why the priority is 11 while the tabs capture logic is added
1775
- * with priority 10.
1776
- *
1777
- * @author Vova Feldman (@svovaf)
1778
- */
1779
- 11
1780
- );
1781
- }
1782
-
1783
- if ( ! self::is_ajax() ) {
1784
- if ( ! $this->is_addon() || $this->is_only_premium() ) {
1785
- add_action(
1786
- ( $this->_is_network_active && fs_is_network_admin() ? 'network_' : '' ) . 'admin_menu',
1787
- array( $this, '_prepare_admin_menu' ),
1788
- WP_FS__LOWEST_PRIORITY
1789
- );
1790
- }
1791
- }
1792
- }
1793
-
1794
- /**
1795
- * Makes Freemius-related updates unavailable on the "Add Plugins" admin page (/plugin-install.php) so that
1796
- * they won't interfere with the .org plugins' functionalities on that page (e.g. updating of a .org plugin).
1797
- *
1798
- * @author Leo Fajardo (@leorw)
1799
- * @since 2.2.3
1800
- *
1801
- * @param object $updates
1802
- * @param string|null $transient
1803
- *
1804
- * @return object
1805
- */
1806
- static function _remove_fs_updates_from_plugin_install_page( $updates, $transient = null ) {
1807
- if ( is_object( $updates ) && isset( $updates->response ) ) {
1808
- foreach ( $updates->response as $file => $plugin ) {
1809
- if ( isset( $plugin->package ) && false !== strpos( $plugin->package, 'api.freemius' ) ) {
1810
- unset( $updates->response[ $file ] );
1811
- }
1812
- }
1813
- }
1814
-
1815
- return $updates;
1816
- }
1817
-
1818
- /**
1819
- * Prepends the `fs_allow_updater_and_dialog` param to the plugin information URLs to tell the SDK to handle
1820
- * the information that is shown on the plugin details dialog that is shown when the relevant link is clicked.
1821
- *
1822
- * @author Leo Fajardo (@leorw)
1823
- * @since 2.2.3
1824
- *
1825
- * @return string
1826
- */
1827
- static function _prepend_fs_allow_updater_and_dialog_flag_url_param() {
1828
- $slug_basename_map = array();
1829
- foreach ( self::$_instances as $instance ) {
1830
- if ( ! $instance->is_plugin() ) {
1831
- continue;
1832
- }
1833
-
1834
- $slug_basename_map[ $instance->get_slug() ] = $instance->premium_plugin_basename();
1835
- }
1836
- ?>
1837
- <script type="text/javascript">
1838
- (function( $ ) {
1839
- var slugBasenameMap = <?php echo json_encode( $slug_basename_map ) ?>;
1840
- for ( var slug in slugBasenameMap ) {
1841
- var basename = slugBasenameMap[ slug ];
1842
-
1843
- // Try to get the plugin rows if on the "Plugins" page.
1844
- var $pluginRows = $( '.wp-list-table.plugins tr[data-plugin="' + basename + '"]');
1845
-
1846
- if ( 0 === $pluginRows.length ) {
1847
- // Try to get the plugin rows if on the "Updates" page.
1848
- var $pluginCheckbox = $( '#update-plugins-table input[type="checkbox"][value="' + basename + '"]' );
1849
- if ( 0 !== $pluginCheckbox.length ) {
1850
- $pluginRows = $pluginCheckbox.parents( 'tr:first' );
1851
- }
1852
- }
1853
-
1854
- if ( 0 === $pluginRows.length ) {
1855
- // No plugin rows found.
1856
- continue;
1857
- }
1858
-
1859
- // Find the "View details" links and add the `fs_allow_updater_and_dialog` param to the URL.
1860
- $pluginRows.find( 'a[href*="plugin-install.php?tab=plugin-information"]' ).each(function() {
1861
- var $this = $( this ),
1862
- href = $this.attr( 'href' ).replace( '?tab=', '?fs_allow_updater_and_dialog=true&tab=');
1863
-
1864
- $this.attr( 'href', href );
1865
- });
1866
- }
1867
- })( jQuery );
1868
- </script>
1869
- <?php
1870
- }
1871
-
1872
- /**
1873
- * @author Leo Fajardo (@leorw)
1874
- * @since 2.3.0
1875
- */
1876
- static function _maybe_add_beta_label_styles() {
1877
- $has_any_beta_version = false;
1878
-
1879
- foreach ( self::$_instances as $instance ) {
1880
- if ( $instance->is_beta() ) {
1881
- $has_any_beta_version = true;
1882
- break;
1883
- }
1884
- }
1885
-
1886
- if ( $has_any_beta_version ) {
1887
- fs_enqueue_local_style( 'fs_plugins', '/admin/plugins.css' );
1888
- }
1889
- }
1890
-
1891
- /**
1892
- * @author Leo Fajardo (@leorw)
1893
- * @since 2.3.0
1894
- */
1895
- static function _maybe_add_beta_label_to_plugins_and_handle_confirmation() {
1896
- $beta_data = array();
1897
-
1898
- foreach ( self::$_instances as $instance ) {
1899
- if ( ! $instance->is_premium() ) {
1900
- continue;
1901
- }
1902
-
1903
- /**
1904
- * If there's an available beta version update, a confirmation message will be shown when the
1905
- * "Update now" link on the "Plugins" or "Themes" page is clicked.
1906
- */
1907
- $has_beta_update = $instance->has_beta_update();
1908
-
1909
- $is_beta = (
1910
- // The "Beta" label is added separately for themes.
1911
- $instance->is_plugin() &&
1912
- $instance->is_beta()
1913
- );
1914
-
1915
- if ( ! $is_beta && ! $has_beta_update ) {
1916
- continue;
1917
- }
1918
-
1919
- $beta_data[ $instance->get_plugin_basename() ] = array( 'is_installed_version_beta' => $is_beta );
1920
-
1921
- if ( ! $has_beta_update ) {
1922
- continue;
1923
- }
1924
-
1925
- $beta_data[ $instance->get_plugin_basename() ]['beta_version_update_confirmation_message'] = sprintf(
1926
- '%s %s',
1927
- sprintf(
1928
- fs_esc_attr_inline(
1929
- 'An update to a Beta version will replace your installed version of %s with the latest Beta release - use with caution, and not on production sites. You have been warned.',
1930
- 'beta-version-update-caution',
1931
- $instance->get_slug()
1932
- ),
1933
- $instance->get_plugin_title()
1934
- ),
1935
- fs_esc_attr_inline( 'Would you like to proceed with the update?', 'update-confirmation', $instance->get_slug() )
1936
- );
1937
- }
1938
-
1939
- if ( empty( $beta_data ) ) {
1940
- return;
1941
- }
1942
- ?>
1943
- <script type="text/javascript">
1944
- ( function( $ ) {
1945
- var betaData = <?php echo json_encode( $beta_data ) ?>;
1946
-
1947
- for ( var pluginBasename in betaData ) {
1948
- if ( ! betaData.hasOwnProperty( pluginBasename ) ) {
1949
- continue;
1950
- }
1951
-
1952
- if ( ! betaData[ pluginBasename ].is_installed_version_beta ) {
1953
- continue;
1954
- }
1955
-
1956
- var $parentContainer = $( '.wp-list-table.plugins tr[data-plugin="' + pluginBasename + '"]' );
1957
- if ( 0 === $parentContainer.length ) {
1958
- continue;
1959
- }
1960
-
1961
- $parentContainer.find( '.plugin-title > strong:first-child').append(
1962
- '<span class="fs-tag fs-info"><?php fs_esc_js_echo_inline( 'Beta', 'beta' ) ?></span>'
1963
- );
1964
- }
1965
-
1966
- setTimeout( function() {
1967
- // Wait a little bit before adding the event handler, otherwise, it will be overridden by the core WP logic.
1968
- $( '.plugins .update-message .update-link, .themes .theme .update-message' ).on( 'click', function() {
1969
- var $parentContainer = $( this ).parents( 'tr:first' );
1970
- pluginBasename = ( 0 !== $parentContainer.length ) ?
1971
- $parentContainer.data( 'plugin' ) :
1972
- $( this ).parents( '.theme:first' ).data( 'slug' );
1973
-
1974
- if (
1975
- betaData[ pluginBasename ] &&
1976
- betaData[ pluginBasename ].beta_version_update_confirmation_message &&
1977
- ! confirm( betaData[ pluginBasename ].beta_version_update_confirmation_message )
1978
- ) {
1979
- return false;
1980
- }
1981
- } );
1982
- }, 20 );
1983
- } )( jQuery );
1984
- </script>
1985
- <?php
1986
- }
1987
-
1988
- /**
1989
- * Keeping the uninstall hook registered for free or premium plugin version may result to a fatal error that
1990
- * could happen when a user tries to uninstall either version while one of them is still active. Uninstalling a
1991
- * plugin will trigger inclusion of the free or premium version and if one of them is active during the
1992
- * uninstallation, a fatal error may occur in case the plugin's class or functions are already defined.
1993
- *
1994
- * @author Leo Fajardo (@leorw)
1995
- *
1996
- * @since 1.2.0
1997
- */
1998
- private function unregister_uninstall_hook() {
1999
- $uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
2000
- unset( $uninstallable_plugins[ $this->_free_plugin_basename ] );
2001
- unset( $uninstallable_plugins[ $this->premium_plugin_basename() ] );
2002
-
2003
- update_option( 'uninstall_plugins', $uninstallable_plugins );
2004
- }
2005
-
2006
- /**
2007
- * @since 1.2.0 Invalidate module's main file cache, otherwise, FS_Plugin_Updater will not fetch updates.
2008
- *
2009
- * @param bool $store_prev_path
2010
- */
2011
- private function clear_module_main_file_cache( $store_prev_path = true ) {
2012
- if ( ! isset( $this->_storage->plugin_main_file ) ||
2013
- empty( $this->_storage->plugin_main_file->path )
2014
- ) {
2015
- return;
2016
- }
2017
-
2018
- if ( ! $store_prev_path ) {
2019
- /**
2020
- * Storing the previous path is not needed when clearing the cache after an SDK version update since
2021
- * the main purpose of the cache clearing in that event is to correct a wrong plugin main file path
2022
- * which causes data mix-up between plugins (e.g. titles and versions of an add-on and its parent plugin).
2023
- *
2024
- * @author Leo Fajardo (@leorw)
2025
- * @since 2.2.1
2026
- */
2027
- unset( $this->_storage->plugin_main_file->path );
2028
- } else {
2029
- $plugin_main_file = clone $this->_storage->plugin_main_file;
2030
-
2031
- // Store cached path (2nd layer cache).
2032
- $plugin_main_file->prev_path = $plugin_main_file->path;
2033
-
2034
- // Clear cached path.
2035
- unset( $plugin_main_file->path );
2036
-
2037
- $this->_storage->plugin_main_file = $plugin_main_file;
2038
- }
2039
-
2040
- /**
2041
- * Clear global cached path.
2042
- *
2043
- * @author Leo Fajardo (@leorw)
2044
- * @since 1.2.2
2045
- */
2046
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map' );
2047
- unset( $id_slug_type_path_map[ $this->_module_id ]['path'] );
2048
- self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
2049
- }
2050
-
2051
- /**
2052
- * @author Leo Fajardo (@leorw)
2053
- * @since 2.0.0
2054
- */
2055
- function _hook_action_links_and_register_account_hooks() {
2056
- if ( $this->is_migration() ) {
2057
- return;
2058
- }
2059
-
2060
- $this->_add_tracking_links();
2061
-
2062
- if ( self::is_plugins_page() && $this->is_plugin() ) {
2063
- $this->hook_plugin_action_links();
2064
- }
2065
-
2066
- $this->_register_account_hooks();
2067
- }
2068
-
2069
- /**
2070
- * @author Vova Feldman (@svovaf)
2071
- * @since 1.0.9
2072
- */
2073
- private function _register_account_hooks() {
2074
- if ( ! is_admin() ) {
2075
- return;
2076
- }
2077
-
2078
- /**
2079
- * Always show the deactivation feedback form since we added
2080
- * automatic free version deactivation upon premium code activation.
2081
- *
2082
- * @since 1.2.1.6
2083
- */
2084
- $this->add_ajax_action(
2085
- 'submit_uninstall_reason',
2086
- array( $this, '_submit_uninstall_reason_action' )
2087
- );
2088
-
2089
- $this->add_ajax_action(
2090
- 'cancel_subscription_or_trial',
2091
- array( $this, 'cancel_subscription_or_trial_ajax_action' )
2092
- );
2093
-
2094
- if ( ! $this->is_addon() || $this->is_parent_plugin_installed() ) {
2095
- if ( ( $this->is_plugin() && self::is_plugins_page() ) ||
2096
- ( $this->is_theme() && self::is_themes_page() )
2097
- ) {
2098
- add_action( 'admin_footer', array( $this, '_add_deactivation_feedback_dialog_box' ) );
2099
- }
2100
- }
2101
- }
2102
-
2103
- /**
2104
- * Leverage backtrace to find caller plugin file path.
2105
- *
2106
- * @author Vova Feldman (@svovaf)
2107
- * @since 1.0.6
2108
- *
2109
- * @param bool $is_init Is initiation sequence.
2110
- *
2111
- * @return string
2112
- */
2113
- private function _find_caller_plugin_file( $is_init = false ) {
2114
- // Try to load the cached value of the file path.
2115
- if ( isset( $this->_storage->plugin_main_file ) ) {
2116
- $plugin_main_file = $this->_storage->plugin_main_file;
2117
- if ( ! empty( $plugin_main_file->path ) ) {
2118
- $absolute_path = $this->get_absolute_path( $plugin_main_file->path );
2119
- if ( file_exists( $absolute_path ) ) {
2120
- return $absolute_path;
2121
- }
2122
- }
2123
- }
2124
-
2125
- /**
2126
- * @since 1.2.1
2127
- *
2128
- * `clear_module_main_file_cache()` is clearing the plugin's cached path on
2129
- * deactivation. Therefore, if any plugin/theme was initiating `Freemius`
2130
- * with that plugin's slug, it was overriding the empty plugin path with a wrong path.
2131
- *
2132
- * So, we've added a special mechanism with a 2nd layer of cache that uses `prev_path`
2133
- * when the class instantiator isn't the module.
2134
- */
2135
- if ( ! $is_init ) {
2136
- // Fetch prev path cache.
2137
- if ( isset( $this->_storage->plugin_main_file ) &&
2138
- ! empty( $this->_storage->plugin_main_file->prev_path )
2139
- ) {
2140
- $absolute_path = $this->get_absolute_path( $this->_storage->plugin_main_file->prev_path );
2141
- if ( file_exists( $absolute_path ) ) {
2142
- return $absolute_path;
2143
- }
2144
- }
2145
-
2146
- wp_die(
2147
- $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' ) .
2148
- " Module: {$this->_slug}; SDK: " . WP_FS__SDK_VERSION . ";",
2149
- $this->get_text_inline( 'Error', 'error' ),
2150
- array( 'back_link' => true )
2151
- );
2152
- }
2153
-
2154
- /**
2155
- * @since 1.2.1
2156
- *
2157
- * Only the original instantiator that calls dynamic_init can modify the module's path.
2158
- */
2159
- // Find caller module.
2160
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
2161
- $this->_storage->plugin_main_file = (object) array(
2162
- 'path' => $id_slug_type_path_map[ $this->_module_id ]['path'],
2163
- );
2164
-
2165
- return $this->get_absolute_path( $id_slug_type_path_map[ $this->_module_id ]['path'] );
2166
- }
2167
-
2168
- /**
2169
- * @author Leo Fajardo (@leorw)
2170
- * @since 1.2.3
2171
- *
2172
- * @param string $path
2173
- *
2174
- * @return string
2175
- */
2176
- private function get_relative_path( $path ) {
2177
- $module_root_dir = $this->get_module_root_dir_path();
2178
- if ( 0 === strpos( $path, $module_root_dir ) ) {
2179
- $path = substr( $path, strlen( $module_root_dir ) );
2180
- }
2181
-
2182
- return $path;
2183
- }
2184
-
2185
- /**
2186
- * @author Leo Fajardo (@leorw)
2187
- * @since 1.2.3
2188
- *
2189
- * @param string $path
2190
- * @param string|bool $module_type
2191
- *
2192
- * @return string
2193
- */
2194
- private function get_absolute_path( $path, $module_type = false ) {
2195
- $module_root_dir = $this->get_module_root_dir_path( $module_type );
2196
- if ( 0 !== strpos( $path, $module_root_dir ) ) {
2197
- $path = fs_normalize_path( $module_root_dir . $path );
2198
- }
2199
-
2200
- return $path;
2201
- }
2202
-
2203
- /**
2204
- * @author Leo Fajardo (@leorw)
2205
- * @since 1.2.3
2206
- *
2207
- * @param string|bool $module_type
2208
- *
2209
- * @return string
2210
- */
2211
- private function get_module_root_dir_path( $module_type = false ) {
2212
- $is_plugin = empty( $module_type ) ?
2213
- $this->is_plugin() :
2214
- ( WP_FS__MODULE_TYPE_PLUGIN === $module_type );
2215
-
2216
- return fs_normalize_path( trailingslashit( $is_plugin ?
2217
- WP_PLUGIN_DIR :
2218
- get_theme_root( get_stylesheet() ) ) );
2219
- }
2220
-
2221
- /**
2222
- * @author Leo Fajardo (@leorw)
2223
- *
2224
- * @param number $module_id
2225
- * @param string $slug
2226
- *
2227
- * @since 1.2.2
2228
- */
2229
- private function store_id_slug_type_path_map( $module_id, $slug ) {
2230
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
2231
-
2232
- $store_option = false;
2233
-
2234
- if ( ! isset( $id_slug_type_path_map[ $module_id ] ) ) {
2235
- $id_slug_type_path_map[ $module_id ] = array(
2236
- 'slug' => $slug
2237
- );
2238
-
2239
- $store_option = true;
2240
- } else if (
2241
- isset( $id_slug_type_path_map[ $module_id ]['slug'] ) &&
2242
- $slug !== $id_slug_type_path_map[ $module_id ]['slug']
2243
- ) {
2244
- $id_slug_type_path_map[ $module_id ]['slug'] = $slug;
2245
- $store_option = true;
2246
- }
2247
-
2248
- if ( empty( $id_slug_type_path_map[ $module_id ]['path'] ) ||
2249
- /**
2250
- * This verification is for cases when suddenly the same module
2251
- * is installed but with a different folder name.
2252
- *
2253
- * @author Vova Feldman (@svovaf)
2254
- * @since 1.2.3
2255
- */
2256
- ! file_exists( $this->get_absolute_path(
2257
- $id_slug_type_path_map[ $module_id ]['path'],
2258
- $id_slug_type_path_map[ $module_id ]['type']
2259
- ) )
2260
- ) {
2261
- $caller_main_file_and_type = $this->get_caller_main_file_and_type();
2262
-
2263
- $id_slug_type_path_map[ $module_id ]['type'] = $caller_main_file_and_type->module_type;
2264
- $id_slug_type_path_map[ $module_id ]['path'] = $caller_main_file_and_type->path;
2265
-
2266
- $store_option = true;
2267
- }
2268
-
2269
- if ( $store_option ) {
2270
- self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
2271
- }
2272
- }
2273
-
2274
- /**
2275
- * Identifies the caller type: plugin or theme.
2276
- *
2277
- * @author Leo Fajardo (@leorw)
2278
- * @since 1.2.2
2279
- *
2280
- * @author Vova Feldman (@svovaf)
2281
- * @since 1.2.2.3 Find the earliest module in the call stack that calls to the SDK. This fix is for cases when
2282
- * add-ons are relying on loading the SDK from the parent module, and also allows themes including the
2283
- * SDK an internal file instead of directly from functions.php.
2284
- * @since 1.2.1.7 Knows how to handle cases when an add-on includes the parent module logic.
2285
- */
2286
- private function get_caller_main_file_and_type() {
2287
- self::require_plugin_essentials();
2288
-
2289
- $all_plugins = fs_get_plugins( true );
2290
- $all_plugins_paths = array();
2291
-
2292
- // Get active plugin's main files real full names (might be symlinks).
2293
- foreach ( $all_plugins as $relative_path => $data ) {
2294
- if ( false === strpos( fs_normalize_path( $relative_path ), '/' ) ) {
2295
- /**
2296
- * Ignore plugins that don't have a folder (e.g. Hello Dolly) since they
2297
- * can't really include the SDK.
2298
- *
2299
- * @author Vova Feldman
2300
- * @since 1.2.1.7
2301
- */
2302
- continue;
2303
- }
2304
-
2305
- $all_plugins_paths[] = fs_normalize_path( realpath( WP_PLUGIN_DIR . '/' . $relative_path ) );
2306
- }
2307
-
2308
- $caller_file_candidate = false;
2309
- $caller_map = array();
2310
- $module_type = WP_FS__MODULE_TYPE_PLUGIN;
2311
- $themes_dir = fs_normalize_path( get_theme_root( get_stylesheet() ) );
2312
- $plugin_dir_to_skip = false;
2313
-
2314
- for ( $i = 1, $bt = debug_backtrace(), $len = count( $bt ); $i < $len; $i ++ ) {
2315
- if ( empty( $bt[ $i ]['file'] ) ) {
2316
- continue;
2317
- }
2318
-
2319
- if ( $i > 1 && ! empty( $bt[ $i - 1 ]['file'] ) && $bt[ $i ]['file'] === $bt[ $i - 1 ]['file'] ) {
2320
- // If file same as the prev file in the stack, skip it.
2321
- continue;
2322
- }
2323
-
2324
- if ( ! empty( $bt[ $i ]['function'] ) && in_array( $bt[ $i ]['function'], array(
2325
- 'do_action',
2326
- 'apply_filter',
2327
- // The string split is stupid, but otherwise, theme check
2328
- // throws info notices.
2329
- 'requir' . 'e_once',
2330
- 'requir' . 'e',
2331
- 'includ' . 'e_once',
2332
- 'includ' . 'e',
2333
- 'install_and_activate_plugin',
2334
- 'try_activate_plugin',
2335
- 'activate_plugin'
2336
- ) )
2337
- ) {
2338
- if ( 'activate_plugin' === $bt[ $i ]['function'] ) {
2339
- /**
2340
- * Store the directory of the activator plugin so that any other file that starts with it
2341
- * cannot be mistakenly chosen as a candidate caller file.
2342
- *
2343
- * @author Leo Fajardo
2344
- *
2345
- * @since 2.3.0
2346
- */
2347
- $caller_file_path = fs_normalize_path( $bt[ $i ]['file'] );
2348
-
2349
- foreach ( $all_plugins_paths as $plugin_path ) {
2350
- $plugin_dir = fs_normalize_path( dirname( $plugin_path ) . '/' );
2351
- if ( false !== strpos( $caller_file_path, $plugin_dir ) ) {
2352
- $plugin_dir_to_skip = $plugin_dir;
2353
-
2354
- break;
2355
- }
2356
- }
2357
- }
2358
-
2359
- // Ignore call stack hooks and files inclusion.
2360
- continue;
2361
- }
2362
-
2363
- $caller_file_path = fs_normalize_path( $bt[ $i ]['file'] );
2364
-
2365
- if ( ! empty( $plugin_dir_to_skip ) ) {
2366
- /**
2367
- * Skip if it's an activator plugin file to avoid mistakenly choosing it as a candidate caller file.
2368
- *
2369
- * @author Leo Fajardo
2370
- *
2371
- * @since 2.3.0
2372
- */
2373
- if ( 0 === strpos( $caller_file_path, $plugin_dir_to_skip ) ) {
2374
- continue;
2375
- }
2376
- }
2377
-
2378
- if ( 'functions.php' === basename( $caller_file_path ) ) {
2379
- /**
2380
- * 1. Assumes that theme's starting execution file is functions.php.
2381
- * 2. This complex logic fixes symlink issues (e.g. with Vargant).
2382
- *
2383
- * @author Vova Feldman (@svovaf)
2384
- * @since 1.2.2.5
2385
- */
2386
-
2387
- if ( $caller_file_path == fs_normalize_path( realpath( trailingslashit( $themes_dir ) . basename( dirname( $caller_file_path ) ) . '/' . basename( $caller_file_path ) ) ) ) {
2388
- $module_type = WP_FS__MODULE_TYPE_THEME;
2389
-
2390
- /**
2391
- * Relative path of the theme, e.g.:
2392
- * `my-theme/functions.php`
2393
- *
2394
- * @author Leo Fajardo (@leorw)
2395
- */
2396
- $caller_file_candidate = basename( dirname( $caller_file_path ) ) .
2397
- '/' .
2398
- basename( $caller_file_path );
2399
-
2400
- continue;
2401
- }
2402
- }
2403
-
2404
- $caller_file_hash = md5( $caller_file_path );
2405
-
2406
- if ( ! isset( $caller_map[ $caller_file_hash ] ) ) {
2407
- foreach ( $all_plugins_paths as $plugin_path ) {
2408
- if ( empty( $plugin_path ) ) {
2409
- continue;
2410
- }
2411
-
2412
- if ( false !== strpos( $caller_file_path, fs_normalize_path( dirname( $plugin_path ) . '/' ) ) ) {
2413
- $caller_map[ $caller_file_hash ] = fs_normalize_path( $plugin_path );
2414
- break;
2415
- }
2416
- }
2417
- }
2418
-
2419
- if ( isset( $caller_map[ $caller_file_hash ] ) ) {
2420
- $module_type = WP_FS__MODULE_TYPE_PLUGIN;
2421
- $caller_file_candidate = plugin_basename( $caller_map[ $caller_file_hash ] );
2422
- }
2423
- }
2424
-
2425
- return (object) array(
2426
- 'module_type' => $module_type,
2427
- 'path' => $caller_file_candidate
2428
- );
2429
- }
2430
-
2431
- #----------------------------------------------------------------------------------
2432
- #region Deactivation Feedback Form
2433
- #----------------------------------------------------------------------------------
2434
-
2435
- /**
2436
- * Displays a confirmation and feedback dialog box when the user clicks on the "Deactivate" link on the plugins
2437
- * page.
2438
- *
2439
- * @author Vova Feldman (@svovaf)
2440
- * @author Leo Fajardo (@leorw)
2441
- *
2442
- * @since 1.1.2
2443
- */
2444
- function _add_deactivation_feedback_dialog_box() {
2445
- $subscription_cancellation_dialog_box_template_params = $this->apply_filters( 'show_deactivation_subscription_cancellation', true ) ?
2446
- $this->_get_subscription_cancellation_dialog_box_template_params() :
2447
- array();
2448
-
2449
- /**
2450
- * @since 2.3.0 Developers can optionally hide the deactivation feedback form using the 'show_deactivation_feedback_form' filter.
2451
- */
2452
- $show_deactivation_feedback_form = true;
2453
- if ( $this->has_filter( 'show_deactivation_feedback_form' ) ) {
2454
- $show_deactivation_feedback_form = $this->apply_filters( 'show_deactivation_feedback_form', true );
2455
- } else if ( $this->is_addon() ) {
2456
- /**
2457
- * If the add-on's 'show_deactivation_feedback_form' is not set, try to inherit the value from the parent.
2458
- */
2459
- $show_deactivation_feedback_form = $this->get_parent_instance()->apply_filters( 'show_deactivation_feedback_form', true );
2460
- }
2461
-
2462
- $uninstall_confirmation_message = $this->apply_filters( 'uninstall_confirmation_message', '' );
2463
-
2464
- if (
2465
- empty( $subscription_cancellation_dialog_box_template_params ) &&
2466
- ! $show_deactivation_feedback_form &&
2467
- empty( $uninstall_confirmation_message )
2468
- ) {
2469
- return;
2470
- }
2471
-
2472
- $vars = array( 'id' => $this->_module_id );
2473
-
2474
- if ( $show_deactivation_feedback_form ) {
2475
- /* Check the type of user:
2476
- * 1. Long-term (long-term)
2477
- * 2. Non-registered and non-anonymous short-term (non-registered-and-non-anonymous-short-term).
2478
- * 3. Short-term (short-term)
2479
- */
2480
- $is_long_term_user = true;
2481
-
2482
- // Check if the site is at least 2 days old.
2483
- $time_installed = $this->_storage->install_timestamp;
2484
-
2485
- // Difference in seconds.
2486
- $date_diff = time() - $time_installed;
2487
-
2488
- // Convert seconds to days.
2489
- $date_diff_days = floor( $date_diff / ( 60 * 60 * 24 ) );
2490
-
2491
- if ( $date_diff_days < 2 ) {
2492
- $is_long_term_user = false;
2493
- }
2494
-
2495
- $is_long_term_user = $this->apply_filters( 'is_long_term_user', $is_long_term_user );
2496
-
2497
- if ( $is_long_term_user ) {
2498
- $user_type = 'long-term';
2499
- } else {
2500
- if ( ! $this->is_registered() && ! $this->is_anonymous() ) {
2501
- $user_type = 'non-registered-and-non-anonymous-short-term';
2502
- } else {
2503
- $user_type = 'short-term';
2504
- }
2505
- }
2506
-
2507
- $uninstall_reasons = $this->_get_uninstall_reasons( $user_type );
2508
-
2509
- $vars['reasons'] = $uninstall_reasons;
2510
- }
2511
-
2512
- $vars['subscription_cancellation_dialog_box_template_params'] = &$subscription_cancellation_dialog_box_template_params;
2513
- $vars['show_deactivation_feedback_form'] = $show_deactivation_feedback_form;
2514
- $vars['uninstall_confirmation_message'] = $uninstall_confirmation_message;
2515
-
2516
- /**
2517
- * Load the HTML template for the deactivation feedback dialog box.
2518
- *
2519
- * @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.
2520
- */
2521
- fs_require_template( 'forms/deactivation/form.php', $vars );
2522
- }
2523
-
2524
- /**
2525
- * @author Leo Fajardo (@leorw)
2526
- * @since 1.1.2
2527
- *
2528
- * @param string $user_type
2529
- *
2530
- * @return array The uninstall reasons for the specified user type.
2531
- */
2532
- function _get_uninstall_reasons( $user_type = 'long-term' ) {
2533
- $module_type = $this->_module_type;
2534
-
2535
- $internal_message_template_var = array(
2536
- 'id' => $this->_module_id
2537
- );
2538
-
2539
- $plan = $this->get_plan();
2540
-
2541
- if ( $this->is_registered() && is_object( $plan ) && $plan->has_technical_support() ) {
2542
- $contact_support_template = fs_get_template( 'forms/deactivation/contact.php', $internal_message_template_var );
2543
- } else {
2544
- $contact_support_template = '';
2545
- }
2546
-
2547
- $reason_found_better_plugin = array(
2548
- 'id' => self::REASON_FOUND_A_BETTER_PLUGIN,
2549
- 'text' => sprintf( $this->get_text_inline( 'I found a better %s', 'reason-found-a-better-plugin' ), $module_type ),
2550
- 'input_type' => 'textfield',
2551
- 'input_placeholder' => sprintf( $this->get_text_inline( "What's the %s's name?", 'placeholder-plugin-name' ), $module_type ),
2552
- );
2553
-
2554
- $reason_temporary_deactivation = array(
2555
- 'id' => self::REASON_TEMPORARY_DEACTIVATION,
2556
- 'text' => sprintf(
2557
- $this->get_text_inline( "It's a temporary %s. I'm just debugging an issue.", 'reason-temporary-x' ),
2558
- strtolower( $this->is_plugin() ?
2559
- $this->get_text_inline( 'Deactivation', 'deactivation' ) :
2560
- $this->get_text_inline( 'Theme Switch', 'theme-switch' )
2561
- )
2562
- ),
2563
- 'input_type' => '',
2564
- 'input_placeholder' => ''
2565
- );
2566
-
2567
- $reason_other = array(
2568
- 'id' => self::REASON_OTHER,
2569
- 'text' => $this->get_text_inline( 'Other', 'reason-other' ),
2570
- 'input_type' => 'textfield',
2571
- 'input_placeholder' => ''
2572
- );
2573
-
2574
- $long_term_user_reasons = array(
2575
- array(
2576
- 'id' => self::REASON_NO_LONGER_NEEDED,
2577
- 'text' => sprintf( $this->get_text_inline( 'I no longer need the %s', 'reason-no-longer-needed' ), $module_type ),
2578
- 'input_type' => '',
2579
- 'input_placeholder' => ''
2580
- ),
2581
- $reason_found_better_plugin,
2582
- array(
2583
- 'id' => self::REASON_NEEDED_FOR_A_SHORT_PERIOD,
2584
- 'text' => sprintf( $this->get_text_inline( 'I only needed the %s for a short period', 'reason-needed-for-a-short-period' ), $module_type ),
2585
- 'input_type' => '',
2586
- 'input_placeholder' => ''
2587
- ),
2588
- array(
2589
- 'id' => self::REASON_BROKE_MY_SITE,
2590
- 'text' => sprintf( $this->get_text_inline( 'The %s broke my site', 'reason-broke-my-site' ), $module_type ),
2591
- 'input_type' => '',
2592
- 'input_placeholder' => '',
2593
- 'internal_message' => $contact_support_template
2594
- ),
2595
- array(
2596
- 'id' => self::REASON_SUDDENLY_STOPPED_WORKING,
2597
- 'text' => sprintf( $this->get_text_inline( 'The %s suddenly stopped working', 'reason-suddenly-stopped-working' ), $module_type ),
2598
- 'input_type' => '',
2599
- 'input_placeholder' => '',
2600
- 'internal_message' => $contact_support_template
2601
- )
2602
- );
2603
-
2604
- if ( $this->is_paying() ) {
2605
- $long_term_user_reasons[] = array(
2606
- 'id' => self::REASON_CANT_PAY_ANYMORE,
2607
- 'text' => $this->get_text_inline( "I can't pay for it anymore", 'reason-cant-pay-anymore' ),
2608
- 'input_type' => 'textfield',
2609
- 'input_placeholder' => $this->get_text_inline( 'What price would you feel comfortable paying?', 'placeholder-comfortable-price' )
2610
- );
2611
- }
2612
-
2613
- $reason_dont_share_info = array(
2614
- 'id' => self::REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION,
2615
- 'text' => $this->get_text_inline( "I don't like to share my information with you", 'reason-dont-like-to-share-my-information' ),
2616
- 'input_type' => '',
2617
- 'input_placeholder' => ''
2618
- );
2619
-
2620
- /**
2621
- * If the current user has selected the "don't share data" reason in the deactivation feedback modal, inform the
2622
- * user by showing additional message that he doesn't have to share data and can just choose to skip the opt-in
2623
- * (the Skip button is included in the message to show). This message will only be shown if anonymous mode is
2624
- * enabled and the user's account is currently not in pending activation state (similar to the way the Skip
2625
- * button in the opt-in form is shown/hidden).
2626
- */
2627
- if ( $this->is_enable_anonymous() && ! $this->is_pending_activation() ) {
2628
- $reason_dont_share_info['internal_message'] = fs_get_template( 'forms/deactivation/retry-skip.php', $internal_message_template_var );
2629
- }
2630
-
2631
- $uninstall_reasons = array(
2632
- 'long-term' => $long_term_user_reasons,
2633
- 'non-registered-and-non-anonymous-short-term' => array(
2634
- array(
2635
- 'id' => self::REASON_DIDNT_WORK,
2636
- 'text' => sprintf( $this->get_text_inline( "The %s didn't work", 'reason-didnt-work' ), $module_type ),
2637
- 'input_type' => '',
2638
- 'input_placeholder' => ''
2639
- ),
2640
- $reason_dont_share_info,
2641
- $reason_found_better_plugin
2642
- ),
2643
- 'short-term' => array(
2644
- array(
2645
- 'id' => self::REASON_COULDNT_MAKE_IT_WORK,
2646
- 'text' => $this->get_text_inline( "I couldn't understand how to make it work", 'reason-couldnt-make-it-work' ),
2647
- 'input_type' => '',
2648
- 'input_placeholder' => '',
2649
- 'internal_message' => $contact_support_template
2650
- ),
2651
- $reason_found_better_plugin,
2652
- array(
2653
- 'id' => self::REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE,
2654
- '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 ),
2655
- 'input_type' => 'textarea',
2656
- 'input_placeholder' => $this->get_text_inline( 'What feature?', 'placeholder-feature' )
2657
- ),
2658
- array(
2659
- 'id' => self::REASON_NOT_WORKING,
2660
- 'text' => sprintf( $this->get_text_inline( 'The %s is not working', 'reason-not-working' ), $module_type ),
2661
- 'input_type' => 'textarea',
2662
- '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' )
2663
- ),
2664
- array(
2665
- 'id' => self::REASON_NOT_WHAT_I_WAS_LOOKING_FOR,
2666
- 'text' => $this->get_text_inline( "It's not what I was looking for", 'reason-not-what-i-was-looking-for' ),
2667
- 'input_type' => 'textarea',
2668
- 'input_placeholder' => $this->get_text_inline( "What you've been looking for?", 'placeholder-what-youve-been-looking-for' )
2669
- ),
2670
- array(
2671
- 'id' => self::REASON_DIDNT_WORK_AS_EXPECTED,
2672
- 'text' => sprintf( $this->get_text_inline( "The %s didn't work as expected", 'reason-didnt-work-as-expected' ), $module_type ),
2673
- 'input_type' => 'textarea',
2674
- 'input_placeholder' => $this->get_text_inline( 'What did you expect?', 'placeholder-what-did-you-expect' )
2675
- )
2676
- )
2677
- );
2678
-
2679
- // Randomize the reasons for the current user type.
2680
- shuffle( $uninstall_reasons[ $user_type ] );
2681
-
2682
- // Keep the following reasons as the last items in the list.
2683
- $uninstall_reasons[ $user_type ][] = $reason_temporary_deactivation;
2684
- $uninstall_reasons[ $user_type ][] = $reason_other;
2685
-
2686
- $uninstall_reasons = $this->apply_filters( 'uninstall_reasons', $uninstall_reasons );
2687
-
2688
- return $uninstall_reasons[ $user_type ];
2689
- }
2690
-
2691
- /**
2692
- * Called after the user has submitted his reason for deactivating the plugin.
2693
- *
2694
- * @author Leo Fajardo (@leorw)
2695
- * @since 1.1.2
2696
- */
2697
- function _submit_uninstall_reason_action() {
2698
- $this->_logger->entrance();
2699
-
2700
- $this->check_ajax_referer( 'submit_uninstall_reason' );
2701
-
2702
- $reason_id = fs_request_get( 'reason_id' );
2703
-
2704
- // Check if the given reason ID is an unsigned integer.
2705
- if ( ! ctype_digit( $reason_id ) ) {
2706
- exit;
2707
- }
2708
-
2709
- $reason_info = trim( fs_request_get( 'reason_info', '' ) );
2710
- if ( ! empty( $reason_info ) ) {
2711
- $reason_info = substr( $reason_info, 0, 128 );
2712
- }
2713
-
2714
- $reason = (object) array(
2715
- 'id' => $reason_id,
2716
- 'info' => $reason_info,
2717
- 'is_anonymous' => fs_request_get_bool( 'is_anonymous' )
2718
- );
2719
-
2720
- $this->_storage->store( 'uninstall_reason', $reason );
2721
-
2722
- /**
2723
- * If the module type is "theme", trigger the uninstall event here (on theme deactivation) since themes do
2724
- * not support uninstall hook.
2725
- *
2726
- * @author Leo Fajardo (@leorw)
2727
- * @since 1.2.2
2728
- */
2729
- if ( $this->is_theme() ) {
2730
- if ( $this->is_premium() && ! $this->has_active_valid_license() ) {
2731
- FS_Plugin_Updater::instance( $this )->delete_update_data();
2732
- }
2733
-
2734
- $this->_uninstall_plugin_event( false );
2735
- $this->remove_sdk_reference();
2736
- }
2737
-
2738
- // Print '1' for successful operation.
2739
- echo 1;
2740
- exit;
2741
- }
2742
-
2743
- /**
2744
- * @author Leo Fajardo (@leorw)
2745
- * @since 2.1.4
2746
- */
2747
- function cancel_subscription_or_trial_ajax_action() {
2748
- $this->_logger->entrance();
2749
-
2750
- $this->check_ajax_referer( 'cancel_subscription_or_trial' );
2751
-
2752
- $result = $this->cancel_subscription_or_trial( fs_request_get( 'plugin_id', $this->get_id() ), false );
2753
-
2754
- if ( $this->is_api_error( $result ) ) {
2755
- $this->shoot_ajax_failure( $result->error->message );
2756
- }
2757
-
2758
- $this->shoot_ajax_success();
2759
- }
2760
-
2761
- /**
2762
- * @author Leo Fajardo (@leorw)
2763
- * @since 2.1.4
2764
- *
2765
- * @param number $plugin_id
2766
- *
2767
- * @return object
2768
- */
2769
- private function cancel_subscription_or_trial( $plugin_id ) {
2770
- $fs = null;
2771
- if ( $plugin_id == $this->get_id() ) {
2772
- $fs = $this;
2773
- } else if ( $this->is_addon_activated( $plugin_id ) ) {
2774
- $fs = self::get_instance_by_id( $plugin_id );
2775
- }
2776
-
2777
- $result = null;
2778
-
2779
- if ( ! is_null( $fs ) ) {
2780
- $result = $fs->is_paid_trial() ?
2781
- $fs->_cancel_trial() :
2782
- $fs->_downgrade_site();
2783
- }
2784
-
2785
- return $result;
2786
- }
2787
-
2788
- /**
2789
- * @author Leo Fajardo (@leorw)
2790
- * @since 2.0.2
2791
- */
2792
- function _delete_theme_update_data_action() {
2793
- FS_Plugin_Updater::instance( $this )->delete_update_data();
2794
- }
2795
-
2796
- #endregion
2797
-
2798
- #----------------------------------------------------------------------------------
2799
- #region Instance
2800
- #----------------------------------------------------------------------------------
2801
-
2802
- /**
2803
- * Main singleton instance.
2804
- *
2805
- * @author Vova Feldman (@svovaf)
2806
- * @since 1.0.0
2807
- *
2808
- * @param number $module_id
2809
- * @param string|bool $slug
2810
- * @param bool $is_init Is initiation sequence.
2811
- *
2812
- * @return Freemius|false
2813
- */
2814
- static function instance( $module_id, $slug = false, $is_init = false ) {
2815
- if ( empty( $module_id ) ) {
2816
- return false;
2817
- }
2818
-
2819
- /**
2820
- * 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.
2821
- */
2822
- self::_load_required_static();
2823
-
2824
- if ( ! is_numeric( $module_id ) ) {
2825
- if ( ! $is_init && true === $slug ) {
2826
- $is_init = true;
2827
- }
2828
-
2829
- $slug = $module_id;
2830
-
2831
- $module = FS_Plugin_Manager::instance( $slug )->get();
2832
-
2833
- if ( is_object( $module ) ) {
2834
- $module_id = $module->id;
2835
- }
2836
- }
2837
-
2838
- $key = 'm_' . $module_id;
2839
-
2840
- if ( ! isset( self::$_instances[ $key ] ) ) {
2841
- self::$_instances[ $key ] = new Freemius( $module_id, $slug, $is_init );
2842
- }
2843
-
2844
- return self::$_instances[ $key ];
2845
- }
2846
-
2847
- /**
2848
- * @author Vova Feldman (@svovaf)
2849
- * @since 1.0.6
2850
- *
2851
- * @param number $addon_id
2852
- *
2853
- * @return bool
2854
- */
2855
- private static function has_instance( $addon_id ) {
2856
- return isset( self::$_instances[ 'm_' . $addon_id ] );
2857
- }
2858
-
2859
- /**
2860
- * @author Leo Fajardo (@leorw)
2861
- * @since 1.2.2
2862
- *
2863
- * @param string|number $id_or_slug
2864
- * @param string $module_type
2865
- *
2866
- * @return number|false
2867
- */
2868
- private static function get_module_id( $id_or_slug, $module_type = WP_FS__MODULE_TYPE_PLUGIN ) {
2869
- if ( is_numeric( $id_or_slug ) ) {
2870
- return $id_or_slug;
2871
- }
2872
-
2873
- foreach ( self::$_instances as $instance ) {
2874
- // Also check the module type since there can be a plugin and a theme with the same slug.
2875
- if ( ( $module_type === $instance->get_module_type() ) && ( $id_or_slug === $instance->get_slug() ) ) {
2876
- return $instance->get_id();
2877
- }
2878
- }
2879
-
2880
- return false;
2881
- }
2882
-
2883
- /**
2884
- * @author Vova Feldman (@svovaf)
2885
- * @since 1.0.6
2886
- *
2887
- * @param number $id
2888
- *
2889
- * @return false|Freemius
2890
- */
2891
- static function get_instance_by_id( $id ) {
2892
- return isset ( self::$_instances[ 'm_' . $id ] ) ?
2893
- self::$_instances[ 'm_' . $id ] :
2894
- false;
2895
- }
2896
-
2897
- /**
2898
- *
2899
- * @author Vova Feldman (@svovaf)
2900
- * @since 1.0.1
2901
- *
2902
- * @param string $plugin_file
2903
- * @param string $module_type
2904
- *
2905
- * @return false|Freemius
2906
- */
2907
- static function get_instance_by_file( $plugin_file, $module_type = WP_FS__MODULE_TYPE_PLUGIN ) {
2908
- $slug = self::find_slug_by_basename( $plugin_file );
2909
-
2910
- return ( false !== $slug ) ?
2911
- self::instance( self::get_module_id( $slug, $module_type ) ) :
2912
- false;
2913
- }
2914
-
2915
- /**
2916
- * @author Vova Feldman (@svovaf)
2917
- * @since 1.0.6
2918
- *
2919
- * @return false|Freemius
2920
- */
2921
- function get_parent_instance() {
2922
- return self::get_instance_by_id( $this->_plugin->parent_plugin_id );
2923
- }
2924
-
2925
- /**
2926
- * @author Vova Feldman (@svovaf)
2927
- * @since 1.0.6
2928
- *
2929
- * @param string|number $id_or_slug
2930
- *
2931
- * @return false|Freemius
2932
- */
2933
- function get_addon_instance( $id_or_slug ) {
2934
- $addon_id = self::get_module_id( $id_or_slug );
2935
-
2936
- return self::instance( $addon_id );
2937
- }
2938
-
2939
- #endregion ------------------------------------------------------------------
2940
-
2941
- /**
2942
- * @author Vova Feldman (@svovaf)
2943
- * @since 1.0.6
2944
- *
2945
- * @return bool
2946
- */
2947
- function is_parent_plugin_installed() {
2948
- $is_active = self::has_instance( $this->_plugin->parent_plugin_id );
2949
-
2950
- if ( $is_active ) {
2951
- return true;
2952
- }
2953
-
2954
- /**
2955
- * Parent module might be a theme. If that's the case, the add-on's FS
2956
- * instance will be loaded prior to the theme's FS instance, therefore,
2957
- * we need to check if it's active with a "look ahead".
2958
- *
2959
- * @author Vova Feldman
2960
- * @since 1.2.2.3
2961
- */
2962
- global $fs_active_plugins;
2963
- if ( is_object( $fs_active_plugins ) && is_array( $fs_active_plugins->plugins ) ) {
2964
- $active_theme = wp_get_theme();
2965
-
2966
- foreach ( $fs_active_plugins->plugins as $sdk => $module ) {
2967
- if ( WP_FS__MODULE_TYPE_THEME === $module->type ) {
2968
- if ( $module->plugin_path == $active_theme->get_stylesheet() ) {
2969
- // Parent module is a theme and it's currently active.
2970
- return true;
2971
- }
2972
- }
2973
- }
2974
- }
2975
-
2976
- return false;
2977
- }
2978
-
2979
- /**
2980
- * Check if add-on parent plugin in activation mode.
2981
- *
2982
- * @author Vova Feldman (@svovaf)
2983
- * @since 1.0.7
2984
- *
2985
- * @return bool
2986
- */
2987
- function is_parent_in_activation() {
2988
- $parent_fs = $this->get_parent_instance();
2989
- if ( ! is_object( $parent_fs ) ) {
2990
- return false;
2991
- }
2992
-
2993
- return ( $parent_fs->is_activation_mode() );
2994
- }
2995
-
2996
- /**
2997
- * Is plugin in activation mode.
2998
- *
2999
- * @author Vova Feldman (@svovaf)
3000
- * @since 1.0.7
3001
- *
3002
- * @param bool $and_on
3003
- *
3004
- * @return bool
3005
- */
3006
- function is_activation_mode( $and_on = true ) {
3007
- return fs_is_network_admin() ?
3008
- $this->is_network_activation_mode( $and_on ) :
3009
- $this->is_site_activation_mode( $and_on );
3010
- }
3011
-
3012
- /**
3013
- * Is plugin in activation mode.
3014
- *
3015
- * @author Vova Feldman (@svovaf)
3016
- * @since 1.0.7
3017
- *
3018
- * @param bool $and_on
3019
- *
3020
- * @return bool
3021
- */
3022
- function is_site_activation_mode( $and_on = true ) {
3023
- return (
3024
- ( $this->is_on() || ! $and_on ) &&
3025
- (
3026
- ( $this->is_premium() && true === $this->_storage->require_license_activation ) ||
3027
- (
3028
- ( ! $this->is_registered() ||
3029
- ( $this->is_only_premium() && ! $this->has_features_enabled_license() ) ) &&
3030
- ( ! $this->is_enable_anonymous() ||
3031
- ( ! $this->is_anonymous() && ! $this->is_pending_activation() ) )
3032
- )
3033
- )
3034
- );
3035
- }
3036
-
3037
- /**
3038
- * Checks if the SDK in network activation mode.
3039
- *
3040
- * @author Leo Fajardo (@leorw)
3041
- * @since 2.0.0
3042
- *
3043
- * @param bool $and_on
3044
- *
3045
- * @return bool
3046
- */
3047
- private function is_network_activation_mode( $and_on = true ) {
3048
- if ( ! $this->_is_network_active ) {
3049
- // Not network activated.
3050
- return false;
3051
- }
3052
-
3053
- if ( $this->is_network_upgrade_mode() ) {
3054
- // Special flag to enforce network activation mode to decide what to do with the sites that are not yet opted-in nor skipped.
3055
- return true;
3056
- }
3057
-
3058
- if ( ! $this->is_site_activation_mode( $and_on ) ) {
3059
- // 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.
3060
- return false;
3061
- }
3062
-
3063
- if ( $this->is_network_delegated_connection() ) {
3064
- // Super-admin delegated the connection to the site admins -> not activation mode.
3065
- return false;
3066
- }
3067
-
3068
- if ( $this->is_network_anonymous() && true !== $this->_storage->require_license_activation ) {
3069
- // Super-admin skipped the connection network wide -> not activation mode.
3070
- return false;
3071
- }
3072
-
3073
- if ( $this->is_network_registered() ) {
3074
- // Super-admin connected at least one site -> not activation mode.
3075
- return false;
3076
- }
3077
-
3078
- return true;
3079
- }
3080
-
3081
- /**
3082
- * Check if current page is the opt-in/pending-activation page.
3083
- *
3084
- * @author Vova Feldman (@svovaf)
3085
- * @since 1.2.1.7
3086
- *
3087
- * @return bool
3088
- */
3089
- function is_activation_page() {
3090
- if ( $this->_menu->is_activation_page( $this->show_opt_in_on_themes_page() ) ) {
3091
- return true;
3092
- }
3093
-
3094
- if ( ! $this->is_activation_mode() ) {
3095
- return false;
3096
- }
3097
-
3098
- // Check if current page is matching the activation page.
3099
- return $this->is_matching_url( $this->get_activation_url() );
3100
- }
3101
-
3102
- /**
3103
- * Check if URL path's are matching and that all querystring
3104
- * arguments of the $sub_url exist in the $url with the same values.
3105
- *
3106
- * WARNING:
3107
- * 1. This method doesn't check if the sub/domain are matching.
3108
- * 2. Ignore case sensitivity.
3109
- *
3110
- * @author Vova Feldman (@svovaf)
3111
- * @since 1.2.1.7
3112
- *
3113
- * @param string $sub_url
3114
- * @param string $url If argument is not set, check if the sub_url matching the current's page URL.
3115
- *
3116
- * @return bool
3117
- */
3118
- private function is_matching_url( $sub_url, $url = '' ) {
3119
- if ( empty( $url ) ) {
3120
- $url = $_SERVER['REQUEST_URI'];
3121
- }
3122
-
3123
- $url = strtolower( $url );
3124
- $sub_url = strtolower( $sub_url );
3125
-
3126
- if ( parse_url( $sub_url, PHP_URL_PATH ) !== parse_url( $url, PHP_URL_PATH ) ) {
3127
- // Different path - DO NOT OVERRIDE PAGE.
3128
- return false;
3129
- }
3130
-
3131
- $url_params = array();
3132
- parse_str( parse_url( $url, PHP_URL_QUERY ), $url_params );
3133
-
3134
- $sub_url_params = array();
3135
- parse_str( parse_url( $sub_url, PHP_URL_QUERY ), $sub_url_params );
3136
-
3137
- foreach ( $sub_url_params as $key => $val ) {
3138
- if ( ! isset( $url_params[ $key ] ) || $val != $url_params[ $key ] ) {
3139
- // Not matching query string - DO NOT OVERRIDE PAGE.
3140
- return false;
3141
- }
3142
- }
3143
-
3144
- return true;
3145
- }
3146
-
3147
- /**
3148
- * Get the basenames of all active plugins for specific blog. Including network activated plugins.
3149
- *
3150
- * @author Vova Feldman (@svovaf)
3151
- * @since 2.0.0
3152
- *
3153
- * @param int $blog_id
3154
- *
3155
- * @return string[]
3156
- */
3157
- private static function get_active_plugins_basenames( $blog_id = 0 ) {
3158
- if ( is_multisite() && $blog_id > 0 ) {
3159
- $active_basenames = get_blog_option( $blog_id, 'active_plugins' );
3160
- } else {
3161
- $active_basenames = get_option( 'active_plugins' );
3162
- }
3163
-
3164
- if ( ! is_array( $active_basenames ) ) {
3165
- $active_basenames = array();
3166
- }
3167
-
3168
- if ( is_multisite() ) {
3169
- $network_active_basenames = get_site_option( 'active_sitewide_plugins' );
3170
-
3171
- if ( is_array( $network_active_basenames ) && ! empty( $network_active_basenames ) ) {
3172
- $active_basenames = array_merge( $active_basenames, array_keys( $network_active_basenames ) );
3173
- }
3174
- }
3175
-
3176
- return $active_basenames;
3177
- }
3178
-
3179
- /**
3180
- * @author Leo Fajardo (@leorw)
3181
- * @since 2.3.0
3182
- *
3183
- * @param int $blog_id
3184
- *
3185
- * @return array
3186
- */
3187
- static function get_active_plugins_directories_map( $blog_id = 0 ) {
3188
- $active_basenames = self::get_active_plugins_basenames( $blog_id );
3189
-
3190
- $map = array();
3191
-
3192
- foreach ( $active_basenames as $active_basename ) {
3193
- $active_basename = fs_normalize_path( $active_basename );
3194
-
3195
- if ( false === strpos( $active_basename, '/' ) ) {
3196
- continue;
3197
- }
3198
-
3199
- $map[ dirname( $active_basename ) ] = true;
3200
- }
3201
-
3202
- return $map;
3203
- }
3204
-
3205
- /**
3206
- * Get collection of all active plugins. Including network activated plugins.
3207
- *
3208
- * @author Vova Feldman (@svovaf)
3209
- * @since 1.0.9
3210
- *
3211
- * @param int $blog_id Since 2.0.0
3212
- *
3213
- * @return array[string]array
3214
- */
3215
- private static function get_active_plugins( $blog_id = 0 ) {
3216
- self::require_plugin_essentials();
3217
-
3218
- $active_plugin = array();
3219
- $all_plugins = fs_get_plugins();
3220
- $active_plugins_basenames = self::get_active_plugins_basenames( $blog_id );
3221
-
3222
- foreach ( $active_plugins_basenames as $plugin_basename ) {
3223
- $active_plugin[ $plugin_basename ] = $all_plugins[ $plugin_basename ];
3224
- }
3225
-
3226
- return $active_plugin;
3227
- }
3228
-
3229
- /**
3230
- * Get collection of all site active plugins for a specified blog.
3231
- *
3232
- * @author Vova Feldman (@svovaf)
3233
- * @since 2.0.0
3234
- *
3235
- * @param int $blog_id
3236
- *
3237
- * @return array[string]array
3238
- */
3239
- private static function get_site_active_plugins( $blog_id = 0 ) {
3240
- $active_basenames = ( is_multisite() && $blog_id > 0 ) ?
3241
- get_blog_option( $blog_id, 'active_plugins' ) :
3242
- get_option( 'active_plugins' );
3243
-
3244
- $active = array();
3245
-
3246
- if ( ! is_array( $active_basenames ) ) {
3247
- return $active;
3248
- }
3249
-
3250
- foreach ( $active_basenames as $basename ) {
3251
- $active[ $basename ] = array(
3252
- 'is_active' => true,
3253
- 'Version' => '1.0', // Dummy version.
3254
- 'slug' => self::get_plugin_slug( $basename ),
3255
- );
3256
- }
3257
-
3258
- return $active;
3259
- }
3260
-
3261
- /**
3262
- * Get collection of all plugins with their activation status for a specified blog.
3263
- *
3264
- * @author Vova Feldman (@svovaf)
3265
- * @since 1.1.8
3266
- *
3267
- * @param int $blog_id Since 2.0.0
3268
- *
3269
- * @return array Key is the plugin file path and the value is an array of the plugin data.
3270
- */
3271
- private static function get_all_plugins( $blog_id = 0 ) {
3272
- self::require_plugin_essentials();
3273
-
3274
- $all_plugins = fs_get_plugins();
3275
-
3276
- $active_plugins_basenames = self::get_active_plugins_basenames( $blog_id );
3277
-
3278
- foreach ( $all_plugins as $basename => &$data ) {
3279
- // By default set to inactive (next foreach update the active plugins).
3280
- $data['is_active'] = false;
3281
- // Enrich with plugin slug.
3282
- $data['slug'] = self::get_plugin_slug( $basename );
3283
- }
3284
-
3285
- // Flag active plugins.
3286
- foreach ( $active_plugins_basenames as $basename ) {
3287
- if ( isset( $all_plugins[ $basename ] ) ) {
3288
- $all_plugins[ $basename ]['is_active'] = true;
3289
- }
3290
- }
3291
-
3292
- return $all_plugins;
3293
- }
3294
-
3295
- /**
3296
- * Get collection of all plugins and if they are network level activated.
3297
- *
3298
- * @author Vova Feldman (@svovaf)
3299
- * @since 2.0.0
3300
- *
3301
- * @return array Key is the plugin basename and the value is an array of the plugin data.
3302
- */
3303
- private static function get_network_plugins() {
3304
- self::require_plugin_essentials();
3305
-
3306
- $all_plugins = fs_get_plugins();
3307
-
3308
- $network_active_basenames = is_multisite() ?
3309
- get_site_option( 'active_sitewide_plugins' ) :
3310
- array();
3311
-
3312
- foreach ( $all_plugins as $basename => &$data ) {
3313
- // By default set to inactive (next foreach update the active plugins).
3314
- $data['is_active'] = false;
3315
- // Enrich with plugin slug.
3316
- $data['slug'] = self::get_plugin_slug( $basename );
3317
- }
3318
-
3319
- // Flag active plugins.
3320
- foreach ( $network_active_basenames as $basename ) {
3321
- if ( isset( $all_plugins[ $basename ] ) ) {
3322
- $all_plugins[ $basename ]['is_active'] = true;
3323
- }
3324
- }
3325
-
3326
- return $all_plugins;
3327
- }
3328
-
3329
- /**
3330
- * Cached result of get_site_transient( 'update_plugins' )
3331
- *
3332
- * @author Vova Feldman (@svovaf)
3333
- * @since 1.1.8
3334
- *
3335
- * @var object
3336
- */
3337
- private static $_plugins_info;
3338
-
3339
- /**
3340
- * Helper function to get specified plugin's slug.
3341
- *
3342
- * @author Vova Feldman (@svovaf)
3343
- * @since 1.1.8
3344
- *
3345
- * @param $basename
3346
- *
3347
- * @return string
3348
- */
3349
- private static function get_plugin_slug( $basename ) {
3350
- if ( ! isset( self::$_plugins_info ) ) {
3351
- self::$_plugins_info = get_site_transient( 'update_plugins' );
3352
- }
3353
-
3354
- $slug = '';
3355
-
3356
- if ( is_object( self::$_plugins_info ) ) {
3357
- if ( isset( self::$_plugins_info->no_update ) &&
3358
- isset( self::$_plugins_info->no_update[ $basename ] ) &&
3359
- ! empty( self::$_plugins_info->no_update[ $basename ]->slug )
3360
- ) {
3361
- $slug = self::$_plugins_info->no_update[ $basename ]->slug;
3362
- } else if ( isset( self::$_plugins_info->response ) &&
3363
- isset( self::$_plugins_info->response[ $basename ] ) &&
3364
- ! empty( self::$_plugins_info->response[ $basename ]->slug )
3365
- ) {
3366
- $slug = self::$_plugins_info->response[ $basename ]->slug;
3367
- }
3368
- }
3369
-
3370
- if ( empty( $slug ) ) {
3371
- // Try to find slug from FS data.
3372
- $slug = self::find_slug_by_basename( $basename );
3373
- }
3374
-
3375
- if ( empty( $slug ) ) {
3376
- // Fallback to plugin's folder name.
3377
- $slug = dirname( $basename );
3378
- }
3379
-
3380
- return $slug;
3381
- }
3382
-
3383
- private static $_statics_loaded = false;
3384
-
3385
- /**
3386
- * Load static resources.
3387
- *
3388
- * @author Vova Feldman (@svovaf)
3389
- * @since 1.0.1
3390
- */
3391
- private static function _load_required_static() {
3392
- if ( self::$_statics_loaded ) {
3393
- return;
3394
- }
3395
-
3396
- self::$_static_logger = FS_Logger::get_logger( WP_FS__SLUG, WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
3397
-
3398
- self::$_static_logger->entrance();
3399
-
3400
- self::$_accounts = FS_Options::instance( WP_FS__ACCOUNTS_OPTION_NAME, true );
3401
-
3402
- if ( is_multisite() ) {
3403
- $has_skipped_migration = (
3404
- // 'id_slug_type_path_map' - was never stored on older versions, therefore, not exists on the site level.
3405
- null === self::$_accounts->get_option( 'id_slug_type_path_map', null, false ) &&
3406
- // 'file_slug_map' stored on the site level, so it was running an SDK version before it was integrated with MS-network.
3407
- null !== self::$_accounts->get_option( 'file_slug_map', null, false )
3408
- );
3409
-
3410
- /**
3411
- * If the file_slug_map exists on the site level but doesn't exist on the
3412
- * network level storage, it means that we need to process the storage with migration.
3413
- *
3414
- * 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.
3415
- *
3416
- * @author Vova Feldman (@svovaf)
3417
- * @since 2.0.0
3418
- */
3419
- if (
3420
- ( $has_skipped_migration && true !== self::$_accounts->get_option( 'ms_migration_complete', false, true ) ) ||
3421
- ( null === self::$_accounts->get_option( 'file_slug_map', null, true ) &&
3422
- null !== self::$_accounts->get_option( 'file_slug_map', null, false ) )
3423
- ) {
3424
- self::migrate_options_to_network();
3425
- }
3426
- }
3427
-
3428
- self::$_global_admin_notices = FS_Admin_Notices::instance( 'global' );
3429
-
3430
- if ( ! WP_FS__DEMO_MODE ) {
3431
- add_action( ( fs_is_network_admin() ? 'network_' : '' ) . 'admin_menu', array(
3432
- 'Freemius',
3433
- '_add_debug_section'
3434
- ) );
3435
- }
3436
-
3437
- add_action( "wp_ajax_fs_toggle_debug_mode", array( 'Freemius', '_toggle_debug_mode' ) );
3438
-
3439
- self::add_ajax_action_static( 'get_debug_log', array( 'Freemius', '_get_debug_log' ) );
3440
-
3441
- self::add_ajax_action_static( 'get_db_option', array( 'Freemius', '_get_db_option' ) );
3442
-
3443
- self::add_ajax_action_static( 'set_db_option', array( 'Freemius', '_set_db_option' ) );
3444
-
3445
- if ( 0 == did_action( 'plugins_loaded' ) ) {
3446
- add_action( 'plugins_loaded', array( 'Freemius', '_load_textdomain' ), 1 );
3447
- }
3448
-
3449
- add_action( 'admin_footer', array( 'Freemius', '_enrich_ajax_url' ) );
3450
- add_action( 'admin_footer', array( 'Freemius', '_open_support_forum_in_new_page' ) );
3451
-
3452
- if ( self::is_plugins_page() || self::is_themes_page() ) {
3453
- add_action( 'admin_print_footer_scripts', array( 'Freemius', '_maybe_add_beta_label_styles' ), 9 );
3454
-
3455
- /**
3456
- * Specifically use this hook so that the JS event handlers will work properly on the "Themes"
3457
- * page.
3458
- *
3459
- * @author Leo Fajardo (@leorw)
3460
- * @since 2.3.0
3461
- */
3462
- add_action( 'admin_footer-' . self::get_current_page(), array( 'Freemius', '_maybe_add_beta_label_to_plugins_and_handle_confirmation') );
3463
- }
3464
-
3465
- self::$_statics_loaded = true;
3466
- }
3467
-
3468
- /**
3469
- * @author Leo Fajardo (@leorw)
3470
- *
3471
- * @since 2.1.3
3472
- */
3473
- private static function migrate_options_to_network() {
3474
- self::migrate_accounts_to_network();
3475
-
3476
- // Migrate API options from site level to network level.
3477
- $api_network_options = FS_Option_Manager::get_manager( WP_FS__OPTIONS_OPTION_NAME, true, true );
3478
- $api_network_options->migrate_to_network();
3479
-
3480
- // Migrate API cache to network level storage.
3481
- FS_Cache_Manager::get_manager( WP_FS__API_CACHE_OPTION_NAME )->migrate_to_network();
3482
-
3483
- self::$_accounts->set_option( 'ms_migration_complete', true, true );
3484
- }
3485
-
3486
- #----------------------------------------------------------------------------------
3487
- #region Localization
3488
- #----------------------------------------------------------------------------------
3489
-
3490
- /**
3491
- * Load framework's text domain.
3492
- *
3493
- * @author Vova Feldman (@svovaf)
3494
- * @since 1.2.1
3495
- */
3496
- static function _load_textdomain() {
3497
- if ( ! is_admin() ) {
3498
- return;
3499
- }
3500
-
3501
- global $fs_active_plugins;
3502
-
3503
- // Works both for plugins and themes.
3504
- load_plugin_textdomain(
3505
- 'freemius',
3506
- false,
3507
- $fs_active_plugins->newest->sdk_path . '/languages/'
3508
- );
3509
- }
3510
-
3511
- #endregion
3512
-
3513
- #----------------------------------------------------------------------------------
3514
- #region Debugging
3515
- #----------------------------------------------------------------------------------
3516
-
3517
- /**
3518
- * @author Vova Feldman (@svovaf)
3519
- * @since 1.0.8
3520
- */
3521
- static function _add_debug_section() {
3522
- if ( ! is_super_admin() ) {
3523
- // Add debug page only for super-admins.
3524
- return;
3525
- }
3526
-
3527
- self::$_static_logger->entrance();
3528
-
3529
- $title = sprintf( '%s [v.%s]', fs_text_inline( 'Freemius Debug' ), WP_FS__SDK_VERSION );
3530
-
3531
- if ( WP_FS__DEV_MODE ) {
3532
- // Add top-level debug menu item.
3533
- $hook = FS_Admin_Menu_Manager::add_page(
3534
- $title,
3535
- $title,
3536
- 'manage_options',
3537
- 'freemius',
3538
- array( 'Freemius', '_debug_page_render' )
3539
- );
3540
- } else {
3541
- // Add hidden debug page.
3542
- $hook = FS_Admin_Menu_Manager::add_subpage(
3543
- null,
3544
- $title,
3545
- $title,
3546
- 'manage_options',
3547
- 'freemius',
3548
- array( 'Freemius', '_debug_page_render' )
3549
- );
3550
- }
3551
-
3552
- if ( ! empty( $hook ) ) {
3553
- add_action( "load-$hook", array( 'Freemius', '_debug_page_actions' ) );
3554
- }
3555
- }
3556
-
3557
- /**
3558
- * @author Vova Feldman (@svovaf)
3559
- * @since 1.1.7.3
3560
- */
3561
- static function _toggle_debug_mode() {
3562
- check_admin_referer( 'fs_toggle_debug_mode' );
3563
-
3564
- if ( ! is_super_admin() ) {
3565
- return;
3566
- }
3567
-
3568
- $is_on = fs_request_get( 'is_on', false, 'post' );
3569
-
3570
- if ( fs_request_is_post() && in_array( $is_on, array( 0, 1 ) ) ) {
3571
- update_option( 'fs_debug_mode', $is_on );
3572
-
3573
- // Turn on/off storage logging.
3574
- FS_Logger::_set_storage_logging( ( 1 == $is_on ) );
3575
- }
3576
-
3577
- exit;
3578
- }
3579
-
3580
- /**
3581
- * @author Vova Feldman (@svovaf)
3582
- * @since 1.2.1.6
3583
- */
3584
- static function _get_debug_log() {
3585
- check_admin_referer( 'fs_get_debug_log' );
3586
-
3587
- if ( ! is_super_admin() ) {
3588
- return;
3589
- }
3590
-
3591
- $limit = min( ! empty( $_POST['limit'] ) ? absint( $_POST['limit'] ) : 200, 200 );
3592
- $offset = min( ! empty( $_POST['offset'] ) ? absint( $_POST['offset'] ) : 200, 200 );
3593
-
3594
- $logs = FS_Logger::load_db_logs(
3595
- fs_request_get( 'filters', false, 'post' ),
3596
- $limit,
3597
- $offset
3598
- );
3599
-
3600
- self::shoot_ajax_success( $logs );
3601
- }
3602
-
3603
- /**
3604
- * @author Vova Feldman (@svovaf)
3605
- * @since 1.2.1.7
3606
- */
3607
- static function _get_db_option() {
3608
- check_admin_referer( 'fs_get_db_option' );
3609
-
3610
- $option_name = fs_request_get( 'option_name' );
3611
-
3612
- if ( ! is_super_admin() ||
3613
- ! fs_starts_with( $option_name, 'fs_' )
3614
- ) {
3615
- self::shoot_ajax_failure();
3616
- }
3617
-
3618
- $value = get_option( $option_name );
3619
-
3620
- $result = array(
3621
- 'name' => $option_name,
3622
- );
3623
-
3624
- if ( false !== $value ) {
3625
- if ( ! is_string( $value ) ) {
3626
- $value = json_encode( $value );
3627
- }
3628
-
3629
- $result['value'] = $value;
3630
- }
3631
-
3632
- self::shoot_ajax_success( $result );
3633
- }
3634
-
3635
- /**
3636
- * @author Vova Feldman (@svovaf)
3637
- * @since 1.2.1.7
3638
- */
3639
- static function _set_db_option() {
3640
- check_admin_referer( 'fs_set_db_option' );
3641
-
3642
- $option_name = fs_request_get( 'option_name' );
3643
-
3644
- if ( ! is_super_admin() ||
3645
- ! fs_starts_with( $option_name, 'fs_' )
3646
- ) {
3647
- self::shoot_ajax_failure();
3648
- }
3649
-
3650
- $option_value = fs_request_get( 'option_value' );
3651
-
3652
- if ( ! empty( $option_value ) ) {
3653
- update_option( $option_name, $option_value );
3654
- }
3655
-
3656
- self::shoot_ajax_success();
3657
- }
3658
-
3659
- /**
3660
- * @author Vova Feldman (@svovaf)
3661
- * @since 1.0.8
3662
- */
3663
- static function _debug_page_actions() {
3664
- self::_clean_admin_content_section();
3665
-
3666
- if ( fs_request_is_action( 'restart_freemius' ) ) {
3667
- check_admin_referer( 'restart_freemius' );
3668
-
3669
- if ( ! is_multisite() ) {
3670
- // Clear accounts data.
3671
- self::$_accounts->clear( null, true );
3672
- } else {
3673
- $sites = self::get_sites();
3674
- foreach ( $sites as $site ) {
3675
- $blog_id = self::get_site_blog_id( $site );
3676
- self::$_accounts->clear( $blog_id, true );
3677
- }
3678
-
3679
- // Clear network level storage.
3680
- self::$_accounts->clear( true, true );
3681
- }
3682
-
3683
- // Clear SDK reference cache.
3684
- delete_option( 'fs_active_plugins' );
3685
- } else if ( fs_request_is_action( 'clear_updates_data' ) ) {
3686
- check_admin_referer( 'clear_updates_data' );
3687
-
3688
- if ( ! is_multisite() ) {
3689
- set_site_transient( 'update_plugins', null );
3690
- set_site_transient( 'update_themes', null );
3691
- } else {
3692
- $current_blog_id = get_current_blog_id();
3693
-
3694
- $sites = self::get_sites();
3695
- foreach ( $sites as $site ) {
3696
- switch_to_blog( self::get_site_blog_id( $site ) );
3697
-
3698
- set_site_transient( 'update_plugins', null );
3699
- set_site_transient( 'update_themes', null );
3700
- }
3701
-
3702
- switch_to_blog( $current_blog_id );
3703
- }
3704
- } else if ( fs_request_is_action( 'simulate_trial' ) ) {
3705
- check_admin_referer( 'simulate_trial' );
3706
-
3707
- $fs = freemius( fs_request_get( 'module_id' ) );
3708
-
3709
- // Update SDK install to at least 24 hours before.
3710
- $fs->_storage->install_timestamp = ( time() - WP_FS__TIME_24_HOURS_IN_SEC );
3711
- // Unset the trial shown timestamp.
3712
- unset( $fs->_storage->trial_promotion_shown );
3713
- } else if ( fs_request_is_action( 'simulate_network_upgrade' ) ) {
3714
- check_admin_referer( 'simulate_network_upgrade' );
3715
-
3716
- $fs = freemius( fs_request_get( 'module_id' ) );
3717
-
3718
- self::set_network_upgrade_mode( $fs->_storage );
3719
- } else if ( fs_request_is_action( 'delete_install' ) ) {
3720
- check_admin_referer( 'delete_install' );
3721
-
3722
- self::_delete_site_by_slug(
3723
- fs_request_get( 'slug' ),
3724
- fs_request_get( 'module_type' ),
3725
- true,
3726
- fs_request_get( 'blog_id', null )
3727
- );
3728
- } else if ( fs_request_is_action( 'delete_user' ) ) {
3729
- check_admin_referer( 'delete_user' );
3730
-
3731
- self::delete_user( fs_request_get( 'user_id' ) );
3732
- } else if ( fs_request_is_action( 'download_logs' ) ) {
3733
- check_admin_referer( 'download_logs' );
3734
-
3735
- $download_url = FS_Logger::download_db_logs(
3736
- fs_request_get( 'filters', false, 'post' )
3737
- );
3738
-
3739
- if ( false === $download_url ) {
3740
- 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.' );
3741
- }
3742
-
3743
- fs_redirect( $download_url );
3744
- } else if ( fs_request_is_action( 'migrate_options_to_network' ) ) {
3745
- check_admin_referer( 'migrate_options_to_network' );
3746
-
3747
- self::migrate_options_to_network();
3748
- }
3749
- }
3750
-
3751
- /**
3752
- * @author Vova Feldman (@svovaf)
3753
- * @since 1.0.8
3754
- */
3755
- static function _debug_page_render() {
3756
- self::$_static_logger->entrance();
3757
-
3758
- if ( ! is_multisite() ) {
3759
- $all_plugins_installs = self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN );
3760
- $all_themes_installs = self::get_all_sites( WP_FS__MODULE_TYPE_THEME );
3761
- } else {
3762
- $sites = self::get_sites();
3763
-
3764
- $all_plugins_installs = array();
3765
- $all_themes_installs = array();
3766
-
3767
- foreach ( $sites as $site ) {
3768
- $blog_id = self::get_site_blog_id( $site );
3769
-
3770
- $plugins_installs = self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN, $blog_id );
3771
-
3772
- foreach ( $plugins_installs as $slug => $install ) {
3773
- if ( ! isset( $all_plugins_installs[ $slug ] ) ) {
3774
- $all_plugins_installs[ $slug ] = array();
3775
- }
3776
-
3777
- $install->blog_id = $blog_id;
3778
-
3779
- $all_plugins_installs[ $slug ][] = $install;
3780
- }
3781
-
3782
- $themes_installs = self::get_all_sites( WP_FS__MODULE_TYPE_THEME, $blog_id );
3783
-
3784
- foreach ( $themes_installs as $slug => $install ) {
3785
- if ( ! isset( $all_themes_installs[ $slug ] ) ) {
3786
- $all_themes_installs[ $slug ] = array();
3787
- }
3788
-
3789
- $install->blog_id = $blog_id;
3790
-
3791
- $all_themes_installs[ $slug ][] = $install;
3792
- }
3793
- }
3794
- }
3795
-
3796
- $licenses_by_module_type = self::get_all_licenses_by_module_type();
3797
-
3798
- $vars = array(
3799
- 'plugin_sites' => $all_plugins_installs,
3800
- 'theme_sites' => $all_themes_installs,
3801
- 'users' => self::get_all_users(),
3802
- 'addons' => self::get_all_addons(),
3803
- 'account_addons' => self::get_all_account_addons(),
3804
- 'plugin_licenses' => $licenses_by_module_type[ WP_FS__MODULE_TYPE_PLUGIN ],
3805
- 'theme_licenses' => $licenses_by_module_type[ WP_FS__MODULE_TYPE_THEME ]
3806
- );
3807
-
3808
- fs_enqueue_local_style( 'fs_debug', '/admin/debug.css' );
3809
- fs_require_once_template( 'debug.php', $vars );
3810
- }
3811
-
3812
- #endregion
3813
-
3814
- #----------------------------------------------------------------------------------
3815
- #region Connectivity Issues
3816
- #----------------------------------------------------------------------------------
3817
-
3818
- /**
3819
- * Check if Freemius should be turned on for the current plugin install.
3820
- *
3821
- * Note:
3822
- * $this->_is_on is updated in has_api_connectivity()
3823
- *
3824
- * @author Vova Feldman (@svovaf)
3825
- * @since 1.0.9
3826
- *
3827
- * @return bool
3828
- */
3829
- function is_on() {
3830
- self::$_static_logger->entrance();
3831
-
3832
- if ( isset( $this->_is_on ) ) {
3833
- return $this->_is_on;
3834
- }
3835
-
3836
- // If already installed or pending then sure it's on :)
3837
- if ( $this->is_registered() || $this->is_pending_activation() ) {
3838
- $this->_is_on = true;
3839
-
3840
- return true;
3841
- }
3842
-
3843
- return false;
3844
- }
3845
-
3846
- /**
3847
- * @author Vova Feldman (@svovaf)
3848
- * @since 1.1.7.3
3849
- *
3850
- * @param bool $flush_if_no_connectivity
3851
- *
3852
- * @return bool
3853
- */
3854
- private function should_run_connectivity_test( $flush_if_no_connectivity = false ) {
3855
- if ( ! isset( $this->_storage->connectivity_test ) ) {
3856
- // Connectivity test was never executed, or cache was cleared.
3857
- return true;
3858
- }
3859
-
3860
- if ( WP_FS__PING_API_ON_IP_OR_HOST_CHANGES ) {
3861
- if ( WP_FS__IS_HTTP_REQUEST ) {
3862
- if ( $_SERVER['HTTP_HOST'] != $this->_storage->connectivity_test['host'] ) {
3863
- // Domain changed.
3864
- return true;
3865
- }
3866
-
3867
- if ( WP_FS__REMOTE_ADDR != $this->_storage->connectivity_test['server_ip'] ) {
3868
- // Server IP changed.
3869
- return true;
3870
- }
3871
- }
3872
- }
3873
-
3874
- if ( $this->_storage->connectivity_test['is_connected'] &&
3875
- $this->_storage->connectivity_test['is_active']
3876
- ) {
3877
- // API connected and Freemius is active - no need to run connectivity check.
3878
- return false;
3879
- }
3880
-
3881
- if ( $flush_if_no_connectivity ) {
3882
- /**
3883
- * If explicitly asked to flush when no connectivity - do it only
3884
- * if at least 10 sec passed from the last API connectivity test.
3885
- */
3886
- return ( isset( $this->_storage->connectivity_test['timestamp'] ) &&
3887
- ( WP_FS__SCRIPT_START_TIME - $this->_storage->connectivity_test['timestamp'] ) > 10 );
3888
- }
3889
-
3890
- /**
3891
- * @since 1.1.7 Don't check for connectivity on plugin downgrade.
3892
- */
3893
- $version = $this->get_plugin_version();
3894
- if ( version_compare( $version, $this->_storage->connectivity_test['version'], '>' ) ) {
3895
- // If it's a plugin version upgrade and Freemius is off or no connectivity, run connectivity test.
3896
- return true;
3897
- }
3898
-
3899
- return false;
3900
- }
3901
-
3902
- /**
3903
- * @author Vova Feldman (@svovaf)
3904
- * @since 1.1.7.4
3905
- *
3906
- * @param int|null $blog_id Since 2.0.0.
3907
- * @param bool $is_gdpr_test Since 2.0.2. Perform only the GDPR test.
3908
- *
3909
- * @return object|false
3910
- */
3911
- private function ping( $blog_id = null, $is_gdpr_test = false ) {
3912
- if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY ) {
3913
- return false;
3914
- }
3915
-
3916
- $version = $this->get_plugin_version();
3917
-
3918
- $is_update = $this->apply_filters( 'is_plugin_update', $this->is_plugin_update() );
3919
-
3920
- return $this->get_api_plugin_scope()->ping(
3921
- $this->get_anonymous_id( $blog_id ),
3922
- array(
3923
- 'is_update' => json_encode( $is_update ),
3924
- 'version' => $version,
3925
- 'sdk' => $this->version,
3926
- 'is_admin' => json_encode( is_admin() ),
3927
- 'is_ajax' => json_encode( self::is_ajax() ),
3928
- 'is_cron' => json_encode( self::is_cron() ),
3929
- 'is_gdpr_test' => $is_gdpr_test,
3930
- 'is_http' => json_encode( WP_FS__IS_HTTP_REQUEST ),
3931
- )
3932
- );
3933
- }
3934
-
3935
- /**
3936
- * Check if there's any connectivity issue to Freemius API.
3937
- *
3938
- * @author Vova Feldman (@svovaf)
3939
- * @since 1.0.9
3940
- *
3941
- * @param bool $flush_if_no_connectivity
3942
- *
3943
- * @return bool
3944
- */
3945
- function has_api_connectivity( $flush_if_no_connectivity = false ) {
3946
- $this->_logger->entrance();
3947
-
3948
- if ( isset( $this->_has_api_connection ) && ( $this->_has_api_connection || ! $flush_if_no_connectivity ) ) {
3949
- return $this->_has_api_connection;
3950
- }
3951
-
3952
- if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY &&
3953
- isset( $this->_storage->connectivity_test ) &&
3954
- true === $this->_storage->connectivity_test['is_connected']
3955
- ) {
3956
- unset( $this->_storage->connectivity_test );
3957
- }
3958
-
3959
- if ( ! $this->should_run_connectivity_test( $flush_if_no_connectivity ) ) {
3960
- $this->_has_api_connection = $this->_storage->connectivity_test['is_connected'];
3961
- /**
3962
- * @since 1.1.6 During dev mode, if there's connectivity - turn Freemius on regardless the configuration.
3963
- *
3964
- * @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.
3965
- */
3966
- $this->_is_on = $this->_storage->connectivity_test['is_active'] ||
3967
- $this->is_premium() ||
3968
- ( WP_FS__DEV_MODE && $this->_has_api_connection && ! WP_FS__SIMULATE_FREEMIUS_OFF );
3969
-
3970
- return $this->_has_api_connection;
3971
- }
3972
-
3973
- $pong = $this->ping();
3974
- $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
3975
-
3976
- if ( ! $is_connected ) {
3977
- // API failure.
3978
- $this->_add_connectivity_issue_message( $pong );
3979
- }
3980
-
3981
- if ( $is_connected ) {
3982
- FS_GDPR_Manager::instance()->store_is_required( $pong->is_gdpr_required );
3983
- }
3984
-
3985
- $this->store_connectivity_info( $pong, $is_connected );
3986
-
3987
- return $this->_has_api_connection;
3988
- }
3989
-
3990
- /**
3991
- * @author Vova Feldman (@svovaf)
3992
- * @since 1.1.7.4
3993
- *
3994
- * @param object $pong
3995
- * @param bool $is_connected
3996
- */
3997
- private function store_connectivity_info( $pong, $is_connected ) {
3998
- $this->_logger->entrance();
3999
-
4000
- $version = $this->get_plugin_version();
4001
-
4002
- if ( ! $is_connected || WP_FS__SIMULATE_FREEMIUS_OFF ) {
4003
- $is_active = false;
4004
- } else {
4005
- $is_active = ( isset( $pong->is_active ) && true == $pong->is_active );
4006
- }
4007
-
4008
- $is_active = $this->apply_filters(
4009
- 'is_on',
4010
- $is_active,
4011
- $this->is_plugin_update(),
4012
- $version
4013
- );
4014
-
4015
- $this->_storage->connectivity_test = array(
4016
- 'is_connected' => $is_connected,
4017
- 'host' => $_SERVER['HTTP_HOST'],
4018
- 'server_ip' => WP_FS__REMOTE_ADDR,
4019
- 'is_active' => $is_active,
4020
- 'timestamp' => WP_FS__SCRIPT_START_TIME,
4021
- // Last version with connectivity attempt.
4022
- 'version' => $version,
4023
- );
4024
-
4025
- $this->_has_api_connection = $is_connected;
4026
- $this->_is_on = $is_active || ( WP_FS__DEV_MODE && $is_connected && ! WP_FS__SIMULATE_FREEMIUS_OFF );
4027
- }
4028
-
4029
- /**
4030
- * Force turning Freemius on.
4031
- *
4032
- * @author Vova Feldman (@svovaf)
4033
- * @since 1.1.8.1
4034
- *
4035
- * @return bool TRUE if successfully turned on.
4036
- */
4037
- private function turn_on() {
4038
- $this->_logger->entrance();
4039
-
4040
- if ( $this->is_on() || ! isset( $this->_storage->connectivity_test['is_active'] ) ) {
4041
- return false;
4042
- }
4043
-
4044
- $updated_connectivity = $this->_storage->connectivity_test;
4045
- $updated_connectivity['is_active'] = true;
4046
- $updated_connectivity['timestamp'] = WP_FS__SCRIPT_START_TIME;
4047
- $this->_storage->connectivity_test = $updated_connectivity;
4048
-
4049
- $this->_is_on = true;
4050
-
4051
- return true;
4052
- }
4053
-
4054
- /**
4055
- * Anonymous and unique site identifier (Hash).
4056
- *
4057
- * @author Vova Feldman (@svovaf)
4058
- * @since 1.1.0
4059
- *
4060
- * @param null|int $blog_id Since 2.0.0
4061
- *
4062
- * @return string
4063
- */
4064
- function get_anonymous_id( $blog_id = null ) {
4065
- $unique_id = self::$_accounts->get_option( 'unique_id', null, $blog_id );
4066
-
4067
- if ( empty( $unique_id ) || ! is_string( $unique_id ) ) {
4068
- $key = fs_strip_url_protocol( get_site_url( $blog_id ) );
4069
-
4070
- $secure_auth = SECURE_AUTH_KEY;
4071
- if ( empty( $secure_auth ) ||
4072
- false !== strpos( $secure_auth, ' ' ) ||
4073
- 'put your unique phrase here' === $secure_auth
4074
- ) {
4075
- // Protect against default auth key.
4076
- $secure_auth = md5( microtime() );
4077
- }
4078
-
4079
- /**
4080
- * Base the unique identifier on the WP secure authentication key. Which
4081
- * turns the key into a secret anonymous identifier. This will help us
4082
- * to avoid duplicate installs generation on the backend upon opt-in.
4083
- *
4084
- * @author Vova Feldman (@svovaf)
4085
- * @since 1.2.3
4086
- */
4087
- $unique_id = md5( $key . $secure_auth );
4088
-
4089
- self::$_accounts->set_option( 'unique_id', $unique_id, true, $blog_id );
4090
- }
4091
-
4092
- $this->_logger->departure( $unique_id );
4093
-
4094
- return $unique_id;
4095
- }
4096
-
4097
- /**
4098
- * @author Vova Feldman (@svovaf)
4099
- * @since 1.1.7.4
4100
- *
4101
- * @return \WP_User
4102
- */
4103
- static function _get_current_wp_user() {
4104
- self::require_pluggable_essentials();
4105
- self::wp_cookie_constants();
4106
-
4107
- return wp_get_current_user();
4108
- }
4109
-
4110
- /**
4111
- * Define cookie constants which are required by Freemius::_get_current_wp_user() since
4112
- * it uses wp_get_current_user() which needs the cookie constants set. When a plugin
4113
- * is network activated the cookie constants are only configured after the network
4114
- * plugins activation, therefore, if we don't define those constants WP will throw
4115
- * PHP warnings/notices.
4116
- *
4117
- * @author Vova Feldman (@svovaf)
4118
- * @since 2.1.1
4119
- */
4120
- private static function wp_cookie_constants() {
4121
- if ( defined( 'LOGGED_IN_COOKIE' ) &&
4122
- ( defined( 'AUTH_COOKIE' ) || defined( 'SECURE_AUTH_COOKIE' ) )
4123
- ) {
4124
- return;
4125
- }
4126
-
4127
- /**
4128
- * Used to guarantee unique hash cookies
4129
- *
4130
- * @since 1.5.0
4131
- */
4132
- if ( ! defined( 'COOKIEHASH' ) ) {
4133
- $siteurl = get_site_option( 'siteurl' );
4134
- if ( $siteurl ) {
4135
- define( 'COOKIEHASH', md5( $siteurl ) );
4136
- } else {
4137
- define( 'COOKIEHASH', '' );
4138
- }
4139
- }
4140
-
4141
- if ( ! defined( 'LOGGED_IN_COOKIE' ) ) {
4142
- define( 'LOGGED_IN_COOKIE', 'wordpress_logged_in_' . COOKIEHASH );
4143
- }
4144
-
4145
- /**
4146
- * @since 2.5.0
4147
- */
4148
- if ( ! defined( 'AUTH_COOKIE' ) ) {
4149
- define( 'AUTH_COOKIE', 'wordpress_' . COOKIEHASH );
4150
- }
4151
-
4152
- /**
4153
- * @since 2.6.0
4154
- */
4155
- if ( ! defined( 'SECURE_AUTH_COOKIE' ) ) {
4156
- define( 'SECURE_AUTH_COOKIE', 'wordpress_sec_' . COOKIEHASH );
4157
- }
4158
- }
4159
-
4160
- /**
4161
- * @author Vova Feldman (@svovaf)
4162
- * @since 2.1.0
4163
- *
4164
- * @return int
4165
- */
4166
- static function get_current_wp_user_id() {
4167
- $wp_user = self::_get_current_wp_user();
4168
-
4169
- return $wp_user->ID;
4170
- }
4171
-
4172
- /**
4173
- * @author Vova Feldman (@svovaf)
4174
- * @since 1.2.1.7
4175
- *
4176
- * @param string $email
4177
- *
4178
- * @return bool
4179
- */
4180
- static function is_valid_email( $email ) {
4181
- if ( false === filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
4182
- return false;
4183
- }
4184
-
4185
- $parts = explode( '@', $email );
4186
-
4187
- if ( 2 !== count( $parts ) || empty( $parts[1] ) ) {
4188
- return false;
4189
- }
4190
-
4191
- $blacklist = array(
4192
- 'admin.',
4193
- 'webmaster.',
4194
- 'localhost.',
4195
- 'dev.',
4196
- 'development.',
4197
- 'test.',
4198
- 'stage.',
4199
- 'staging.',
4200
- );
4201
-
4202
- // Make sure domain is not one of the blacklisted.
4203
- foreach ( $blacklist as $invalid ) {
4204
- if ( 0 === strpos( $parts[1], $invalid ) ) {
4205
- return false;
4206
- }
4207
- }
4208
-
4209
- // Get the UTF encoded domain name.
4210
- $domain = idn_to_ascii( $parts[1] ) . '.';
4211
-
4212
- return ( checkdnsrr( $domain, 'MX' ) || checkdnsrr( $domain, 'A' ) );
4213
- }
4214
-
4215
- /**
4216
- * Generate API connectivity issue message.
4217
- *
4218
- * @author Vova Feldman (@svovaf)
4219
- * @since 1.0.9
4220
- *
4221
- * @param mixed $api_result
4222
- * @param bool $is_first_failure
4223
- */
4224
- function _add_connectivity_issue_message( $api_result, $is_first_failure = true ) {
4225
- if ( ! $this->is_premium() && $this->_enable_anonymous ) {
4226
- // Don't add message if it's the free version and can run anonymously.
4227
- return;
4228
- }
4229
-
4230
- if ( ! function_exists( 'wp_nonce_url' ) ) {
4231
- require_once ABSPATH . 'wp-includes/functions.php';
4232
- }
4233
-
4234
- $current_user = self::_get_current_wp_user();
4235
- // $admin_email = get_option( 'admin_email' );
4236
- $admin_email = $current_user->user_email;
4237
-
4238
- // Aliases.
4239
- $deactivate_plugin_title = $this->esc_html_inline( 'That\'s exhausting, please deactivate', 'deactivate-plugin-title' );
4240
- $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' );
4241
- $install_previous_title = $this->esc_html_inline( 'Let\'s try your previous version', 'install-previous-title' );
4242
- $install_previous_desc = $this->esc_html_inline( 'Uninstall this version and install the previous one.', 'install-previous-desc' );
4243
- $fix_issue_title = $this->esc_html_inline( 'Yes - I\'m giving you a chance to fix it', 'fix-issue-title' );
4244
- $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' );
4245
- /* translators: %s: product title (e.g. "Awesome Plugin" requires an access to...) */
4246
- $x_requires_access_to_api = $this->esc_html_inline( '%s requires an access to our API.', 'x-requires-access-to-api' );
4247
- $sysadmin_title = $this->esc_html_inline( 'I\'m a system administrator', 'sysadmin-title' );
4248
- $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' );
4249
-
4250
- $message = false;
4251
- if ( is_object( $api_result ) &&
4252
- isset( $api_result->error ) &&
4253
- isset( $api_result->error->code )
4254
- ) {
4255
- switch ( $api_result->error->code ) {
4256
- case 'curl_missing':
4257
- $missing_methods = '';
4258
- if ( is_array( $api_result->missing_methods ) &&
4259
- ! empty( $api_result->missing_methods )
4260
- ) {
4261
- foreach ( $api_result->missing_methods as $m ) {
4262
- if ( 'curl_version' === $m ) {
4263
- continue;
4264
- }
4265
-
4266
- if ( ! empty( $missing_methods ) ) {
4267
- $missing_methods .= ', ';
4268
- }
4269
-
4270
- $missing_methods .= sprintf( '<code>%s</code>', $m );
4271
- }
4272
-
4273
- if ( ! empty( $missing_methods ) ) {
4274
- $missing_methods = sprintf(
4275
- '<br><br><b>%s</b> %s',
4276
- $this->esc_html_inline( 'Disabled method(s):', 'curl-disabled-methods' ),
4277
- $missing_methods
4278
- );
4279
- }
4280
- }
4281
-
4282
- $message = sprintf(
4283
- $x_requires_access_to_api . ' ' .
4284
- $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' ) . ' ' .
4285
- $missing_methods .
4286
- ' %s',
4287
- '<b>' . $this->get_plugin_name() . '</b>',
4288
- sprintf(
4289
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
4290
- sprintf(
4291
- '<a class="fs-resolve" data-type="curl" href="#"><b>%s</b></a>%s',
4292
- $this->get_text_inline( 'I don\'t know what is cURL or how to install it, help me!', 'curl-missing-no-clue-title' ),
4293
- ' - ' . sprintf(
4294
- $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' ),
4295
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
4296
- )
4297
- ),
4298
- sprintf(
4299
- '<b>%s</b> - %s',
4300
- $sysadmin_title,
4301
- 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 ) ) )
4302
- ),
4303
- sprintf(
4304
- '<a href="%s"><b>%s</b></a> - %s',
4305
- 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 ),
4306
- $deactivate_plugin_title,
4307
- $deactivate_plugin_desc
4308
- )
4309
- )
4310
- );
4311
- break;
4312
- case 'cloudflare_ddos_protection':
4313
- $message = sprintf(
4314
- $x_requires_access_to_api . ' ' .
4315
- $this->esc_html_inline( 'From unknown reason, CloudFlare, the firewall we use, blocks the connection.', 'cloudflare-blocks-connection-message' ) . ' ' .
4316
- $happy_to_resolve_issue_asap .
4317
- ' %s',
4318
- '<b>' . $this->get_plugin_name() . '</b>',
4319
- sprintf(
4320
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
4321
- sprintf(
4322
- '<a class="fs-resolve" data-type="cloudflare" href="#"><b>%s</b></a>%s',
4323
- $fix_issue_title,
4324
- ' - ' . sprintf(
4325
- $fix_issue_desc,
4326
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
4327
- )
4328
- ),
4329
- sprintf(
4330
- '<a href="%s" target="_blank" rel="noopener noreferrer"><b>%s</b></a> - %s',
4331
- sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),
4332
- $install_previous_title,
4333
- $install_previous_desc
4334
- ),
4335
- sprintf(
4336
- '<a href="%s"><b>%s</b></a> - %s',
4337
- 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 ),
4338
- $deactivate_plugin_title,
4339
- $deactivate_plugin_desc
4340
- )
4341
- )
4342
- );
4343
- break;
4344
- case 'squid_cache_block':
4345
- $message = sprintf(
4346
- $x_requires_access_to_api . ' ' .
4347
- $this->esc_html_inline( 'It looks like your server is using Squid ACL (access control lists), which blocks the connection.', 'squid-blocks-connection-message' ) .
4348
- ' %s',
4349
- '<b>' . $this->get_plugin_name() . '</b>',
4350
- sprintf(
4351
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
4352
- sprintf(
4353
- '<a class="fs-resolve" data-type="squid" href="#"><b>%s</b></a> - %s',
4354
- $this->esc_html_inline( 'I don\'t know what is Squid or ACL, help me!', 'squid-no-clue-title' ),
4355
- sprintf(
4356
- $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' ),
4357
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
4358
- )
4359
- ),
4360
- sprintf(
4361
- '<b>%s</b> - %s',
4362
- $sysadmin_title,
4363
- sprintf(
4364
- $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' ),
4365
- // We use a filter since the plugin might require additional API connectivity.
4366
- '<b>' . implode( ', ', $this->apply_filters( 'api_domains', array(
4367
- 'api.freemius.com',
4368
- 'wp.freemius.com'
4369
- ) ) ) . '</b>',
4370
- $this->_module_type
4371
- )
4372
- ),
4373
- sprintf(
4374
- '<a href="%s"><b>%s</b></a> - %s',
4375
- 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 ),
4376
- $deactivate_plugin_title,
4377
- $deactivate_plugin_desc
4378
- )
4379
- )
4380
- );
4381
- break;
4382
- // default:
4383
- // $message = $this->get_text_inline( 'connectivity-test-fails-message' );
4384
- // break;
4385
- }
4386
- }
4387
-
4388
- $message_id = 'failed_connect_api';
4389
- $type = 'error';
4390
-
4391
- $connectivity_test_fails_message = $this->esc_html_inline( 'From unknown reason, the API connectivity test failed.', 'connectivity-test-fails-message' );
4392
-
4393
- if ( false === $message ) {
4394
- if ( $is_first_failure ) {
4395
- // First attempt failed.
4396
- $message = sprintf(
4397
- $x_requires_access_to_api . ' ' .
4398
- $connectivity_test_fails_message . ' ' .
4399
- $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>' .
4400
- '%s',
4401
- '<b>' . $this->get_plugin_name() . '</b>',
4402
- sprintf(
4403
- '<div id="fs_firewall_issue_options">%s %s</div>',
4404
- sprintf(
4405
- '<a class="button button-primary fs-resolve" data-type="retry_ping" href="#">%s</a>',
4406
- $this->get_text_inline( 'Yes - do your thing', 'yes-do-your-thing' )
4407
- ),
4408
- sprintf(
4409
- '<a href="%s" class="button">%s</a>',
4410
- 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 ),
4411
- $this->get_text_inline( 'No - just deactivate', 'no-deactivate' )
4412
- )
4413
- )
4414
- );
4415
-
4416
- $message_id = 'failed_connect_api_first';
4417
- $type = 'promotion';
4418
- } else {
4419
- // Second connectivity attempt failed.
4420
- $message = sprintf(
4421
- $x_requires_access_to_api . ' ' .
4422
- $connectivity_test_fails_message . ' ' .
4423
- $happy_to_resolve_issue_asap .
4424
- ' %s',
4425
- '<b>' . $this->get_plugin_name() . '</b>',
4426
- sprintf(
4427
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
4428
- sprintf(
4429
- '<a class="fs-resolve" data-type="general" href="#"><b>%s</b></a>%s',
4430
- $fix_issue_title,
4431
- ' - ' . sprintf(
4432
- $fix_issue_desc,
4433
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
4434
- )
4435
- ),
4436
- sprintf(
4437
- '<a href="%s" target="_blank" rel="noopener noreferrer"><b>%s</b></a> - %s',
4438
- sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),
4439
- $install_previous_title,
4440
- $install_previous_desc
4441
- ),
4442
- sprintf(
4443
- '<a href="%s"><b>%s</b></a> - %s',
4444
- 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 ),
4445
- $deactivate_plugin_title,
4446
- $deactivate_plugin_desc
4447
- )
4448
- )
4449
- );
4450
- }
4451
- }
4452
-
4453
- $this->_admin_notices->add_sticky(
4454
- $message,
4455
- $message_id,
4456
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
4457
- $type
4458
- );
4459
- }
4460
-
4461
- /**
4462
- * Handle user request to resolve connectivity issue.
4463
- * This method will send an email to Freemius API technical staff for resolution.
4464
- * The email will contain server's info and installed plugins (might be caching issue).
4465
- *
4466
- * @author Vova Feldman (@svovaf)
4467
- * @since 1.0.9
4468
- */
4469
- function _email_about_firewall_issue() {
4470
- check_admin_referer( 'fs_resolve_firewall_issues' );
4471
-
4472
- if ( ! current_user_can( is_multisite() ? 'manage_options' : 'activate_plugins' ) ) {
4473
- return;
4474
- }
4475
-
4476
- $this->_admin_notices->remove_sticky( 'failed_connect_api' );
4477
-
4478
- $pong = $this->ping();
4479
-
4480
- $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
4481
-
4482
- if ( $is_connected ) {
4483
- FS_GDPR_Manager::instance()->store_is_required( $pong->is_gdpr_required );
4484
-
4485
- $this->store_connectivity_info( $pong, $is_connected );
4486
-
4487
- echo $this->get_after_plugin_activation_redirect_url();
4488
- exit;
4489
- }
4490
-
4491
- $current_user = self::_get_current_wp_user();
4492
- $admin_email = $current_user->user_email;
4493
-
4494
- $error_type = fs_request_get( 'error_type', 'general' );
4495
-
4496
- switch ( $error_type ) {
4497
- case 'squid':
4498
- $title = 'Squid ACL Blocking Issue';
4499
- break;
4500
- case 'cloudflare':
4501
- $title = 'CloudFlare Blocking Issue';
4502
- break;
4503
- default:
4504
- $title = 'API Connectivity Issue';
4505
- break;
4506
- }
4507
-
4508
- $custom_email_sections = array();
4509
-
4510
- // Add 'API Error' custom email section.
4511
- $custom_email_sections['api_error'] = array(
4512
- 'title' => 'API Error',
4513
- 'rows' => array(
4514
- 'ping' => array(
4515
- 'API Error',
4516
- is_string( $pong ) ? htmlentities( $pong ) : json_encode( $pong )
4517
- ),
4518
- )
4519
- );
4520
-
4521
- // Send email with technical details to resolve API connectivity issues.
4522
- $this->send_email(
4523
- 'api@freemius.com', // recipient
4524
- $title . ' [' . $this->get_plugin_name() . ']', // subject
4525
- $custom_email_sections,
4526
- array( "Reply-To: $admin_email <$admin_email>" ) // headers
4527
- );
4528
-
4529
- $this->_admin_notices->add_sticky(
4530
- sprintf(
4531
- $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' ),
4532
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
4533
- ),
4534
- 'server_details_sent'
4535
- );
4536
-
4537
- // Action was taken, tell that API connectivity troubleshooting should be off now.
4538
-
4539
- echo "1";
4540
- exit;
4541
- }
4542
-
4543
- /**
4544
- * Handle connectivity test retry approved by the user.
4545
- *
4546
- * @author Vova Feldman (@svovaf)
4547
- * @since 1.1.7.4
4548
- */
4549
- function _retry_connectivity_test() {
4550
- check_admin_referer( 'fs_retry_connectivity_test' );
4551
-
4552
- if ( ! current_user_can( is_multisite() ? 'manage_options' : 'activate_plugins' ) ) {
4553
- return;
4554
- }
4555
-
4556
- $this->_admin_notices->remove_sticky( 'failed_connect_api_first' );
4557
-
4558
- $pong = $this->ping();
4559
-
4560
- $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
4561
-
4562
- if ( $is_connected ) {
4563
- FS_GDPR_Manager::instance()->store_is_required( $pong->is_gdpr_required );
4564
-
4565
- $this->store_connectivity_info( $pong, $is_connected );
4566
-
4567
- echo $this->get_after_plugin_activation_redirect_url();
4568
- } else {
4569
- // Add connectivity issue message after 2nd failed attempt.
4570
- $this->_add_connectivity_issue_message( $pong, false );
4571
-
4572
- echo "1";
4573
- }
4574
-
4575
- exit;
4576
- }
4577
-
4578
- static function _add_firewall_issues_javascript() {
4579
- $params = array();
4580
- fs_require_once_template( 'firewall-issues-js.php', $params );
4581
- }
4582
-
4583
- #endregion
4584
-
4585
- #----------------------------------------------------------------------------------
4586
- #region Email
4587
- #----------------------------------------------------------------------------------
4588
-
4589
- /**
4590
- * Generates and sends an HTML email with customizable sections.
4591
- *
4592
- * @author Leo Fajardo (@leorw)
4593
- * @since 1.1.2
4594
- *
4595
- * @param string $to_address
4596
- * @param string $subject
4597
- * @param array $sections
4598
- * @param array $headers
4599
- *
4600
- * @return bool Whether the email contents were sent successfully.
4601
- */
4602
- private function send_email(
4603
- $to_address,
4604
- $subject,
4605
- $sections = array(),
4606
- $headers = array()
4607
- ) {
4608
- $default_sections = $this->get_email_sections();
4609
-
4610
- // Insert new sections or replace the default email sections.
4611
- if ( is_array( $sections ) && ! empty( $sections ) ) {
4612
- foreach ( $sections as $section_id => $custom_section ) {
4613
- if ( ! isset( $default_sections[ $section_id ] ) ) {
4614
- // If the section does not exist, add it.
4615
- $default_sections[ $section_id ] = $custom_section;
4616
- } else {
4617
- // If the section already exists, override it.
4618
- $current_section = $default_sections[ $section_id ];
4619
-
4620
- // Replace the current section's title if a custom section title exists.
4621
- if ( isset( $custom_section['title'] ) ) {
4622
- $current_section['title'] = $custom_section['title'];
4623
- }
4624
-
4625
- // Insert new rows under the current section or replace the default rows.
4626
- if ( isset( $custom_section['rows'] ) && is_array( $custom_section['rows'] ) && ! empty( $custom_section['rows'] ) ) {
4627
- foreach ( $custom_section['rows'] as $row_id => $row ) {
4628
- $current_section['rows'][ $row_id ] = $row;
4629
- }
4630
- }
4631
-
4632
- $default_sections[ $section_id ] = $current_section;
4633
- }
4634
- }
4635
- }
4636
-
4637
- $vars = array( 'sections' => $default_sections );
4638
- $message = fs_get_template( 'email.php', $vars );
4639
-
4640
- // Set the type of email to HTML.
4641
- $headers[] = 'Content-type: text/html; charset=UTF-8';
4642
-
4643
- $header_string = implode( "\r\n", $headers );
4644
-
4645
- return wp_mail(
4646
- $to_address,
4647
- $subject,
4648
- $message,
4649
- $header_string
4650
- );
4651
- }
4652
-
4653
- /**
4654
- * Generates the data for the sections of the email content.
4655
- *
4656
- * @author Leo Fajardo (@leorw)
4657
- * @since 1.1.2
4658
- *
4659
- * @return array
4660
- */
4661
- private function get_email_sections() {
4662
- // Retrieve the current user's information so that we can get the user's email, first name, and last name below.
4663
- $current_user = self::_get_current_wp_user();
4664
-
4665
- // Retrieve the cURL version information so that we can get the version number below.
4666
- $curl_version_information = curl_version();
4667
-
4668
- $active_plugin = self::get_active_plugins();
4669
-
4670
- // Generate the list of active plugins separated by new line.
4671
- $active_plugin_string = '';
4672
- foreach ( $active_plugin as $plugin ) {
4673
- $active_plugin_string .= sprintf(
4674
- '<a href="%s">%s</a> [v%s]<br>',
4675
- $plugin['PluginURI'],
4676
- $plugin['Name'],
4677
- $plugin['Version']
4678
- );
4679
- }
4680
-
4681
- $server_ip = WP_FS__REMOTE_ADDR;
4682
-
4683
- // Add PHP info for deeper investigation.
4684
- ob_start();
4685
- phpinfo();
4686
- $php_info = ob_get_clean();
4687
-
4688
- $api_domain = substr( FS_API__ADDRESS, strpos( FS_API__ADDRESS, ':' ) + 3 );
4689
-
4690
- // Generate the default email sections.
4691
- $sections = array(
4692
- 'sdk' => array(
4693
- 'title' => 'SDK',
4694
- 'rows' => array(
4695
- 'fs_version' => array( 'FS Version', $this->version ),
4696
- 'curl_version' => array( 'cURL Version', $curl_version_information['version'] )
4697
- )
4698
- ),
4699
- 'plugin' => array(
4700
- 'title' => ucfirst( $this->get_module_type() ),
4701
- 'rows' => array(
4702
- 'name' => array( 'Name', $this->get_plugin_name() ),
4703
- 'version' => array( 'Version', $this->get_plugin_version() )
4704
- )
4705
- ),
4706
- 'api' => array(
4707
- 'title' => 'API Subdomain',
4708
- 'rows' => array(
4709
- 'dns' => array(
4710
- 'DNS_CNAME',
4711
- function_exists( 'dns_get_record' ) ?
4712
- var_export( dns_get_record( $api_domain, DNS_CNAME ), true ) :
4713
- 'dns_get_record() disabled/blocked'
4714
- ),
4715
- 'ip' => array(
4716
- 'IP',
4717
- function_exists( 'gethostbyname' ) ?
4718
- gethostbyname( $api_domain ) :
4719
- 'gethostbyname() disabled/blocked'
4720
- ),
4721
- ),
4722
- ),
4723
- 'site' => array(
4724
- 'title' => 'Site',
4725
- 'rows' => array(
4726
- 'unique_id' => array( 'Unique ID', $this->get_anonymous_id() ),
4727
- 'address' => array( 'Address', site_url() ),
4728
- 'host' => array(
4729
- 'HTTP_HOST',
4730
- ( ! empty( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : '' )
4731
- ),
4732
- 'hosting' => array(
4733
- 'Hosting Company' => fs_request_has( 'hosting_company' ) ?
4734
- fs_request_get( 'hosting_company' ) :
4735
- 'Unknown',
4736
- ),
4737
- 'server_addr' => array(
4738
- 'SERVER_ADDR',
4739
- '<a href="http://www.projecthoneypot.org/ip_' . $server_ip . '">' . $server_ip . '</a>'
4740
- )
4741
- )
4742
- ),
4743
- 'user' => array(
4744
- 'title' => 'User',
4745
- 'rows' => array(
4746
- 'email' => array( 'Email', $current_user->user_email ),
4747
- 'first' => array( 'First', $current_user->user_firstname ),
4748
- 'last' => array( 'Last', $current_user->user_lastname )
4749
- )
4750
- ),
4751
- 'plugins' => array(
4752
- 'title' => 'Plugins',
4753
- 'rows' => array(
4754
- 'active_plugins' => array( 'Active Plugins', $active_plugin_string )
4755
- )
4756
- ),
4757
- 'php_info' => array(
4758
- 'title' => 'PHP Info',
4759
- 'rows' => array(
4760
- 'info' => array( $php_info )
4761
- ),
4762
- )
4763
- );
4764
-
4765
- // Allow the sections to be modified by other code.
4766
- $sections = $this->apply_filters( 'email_template_sections', $sections );
4767
-
4768
- return $sections;
4769
- }
4770
-
4771
- #endregion
4772
-
4773
- #----------------------------------------------------------------------------------
4774
- #region Initialization
4775
- #----------------------------------------------------------------------------------
4776
-
4777
- /**
4778
- * Init plugin's Freemius instance.
4779
- *
4780
- * @author Vova Feldman (@svovaf)
4781
- * @since 1.0.1
4782
- *
4783
- * @param number $id
4784
- * @param string $public_key
4785
- * @param bool $is_live
4786
- * @param bool $is_premium
4787
- */
4788
- function init( $id, $public_key, $is_live = true, $is_premium = true ) {
4789
- $this->_logger->entrance();
4790
-
4791
- $this->dynamic_init( array(
4792
- 'id' => $id,
4793
- 'public_key' => $public_key,
4794
- 'is_live' => $is_live,
4795
- 'is_premium' => $is_premium,
4796
- ) );
4797
- }
4798
-
4799
- /**
4800
- * Dynamic initiator, originally created to support initiation
4801
- * with parent_id for add-ons.
4802
- *
4803
- * @author Vova Feldman (@svovaf)
4804
- * @since 1.0.6
4805
- *
4806
- * @param array $plugin_info
4807
- *
4808
- * @throws Freemius_Exception
4809
- */
4810
- function dynamic_init( array $plugin_info ) {
4811
- $this->_logger->entrance();
4812
-
4813
- $this->parse_settings( $plugin_info );
4814
-
4815
- $this->register_after_settings_parse_hooks();
4816
-
4817
- if ( $this->should_stop_execution() ) {
4818
- return;
4819
- }
4820
-
4821
- if ( ! $this->is_registered() ) {
4822
- if ( $this->is_anonymous() ) {
4823
- // If user skipped, no need to test connectivity.
4824
- $this->_has_api_connection = true;
4825
- $this->_is_on = true;
4826
- } else {
4827
- if ( ! $this->has_api_connectivity() ) {
4828
- if ( $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) ||
4829
- $this->_admin_notices->has_sticky( 'failed_connect_api' )
4830
- ) {
4831
- if ( ! $this->_enable_anonymous || $this->is_premium() ) {
4832
- // If anonymous mode is disabled, add firewall admin-notice message.
4833
- add_action( 'admin_footer', array( 'Freemius', '_add_firewall_issues_javascript' ) );
4834
-
4835
- $ajax_action_suffix = $this->_slug . ( $this->is_theme() ? ':theme' : '' );
4836
- add_action( "wp_ajax_fs_resolve_firewall_issues_{$ajax_action_suffix}", array(
4837
- $this,
4838
- '_email_about_firewall_issue'
4839
- ) );
4840
-
4841
- add_action( "wp_ajax_fs_retry_connectivity_test_{$ajax_action_suffix}", array(
4842
- $this,
4843
- '_retry_connectivity_test'
4844
- ) );
4845
-
4846
- /**
4847
- * 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.
4848
- *
4849
- * @author Vova Feldman (@svovaf)
4850
- * @since 2.0.0
4851
- */
4852
- /*$this->add_ajax_action( 'resolve_firewall_issues', array(
4853
- $this,
4854
- '_email_about_firewall_issue'
4855
- ) );
4856
-
4857
- $this->add_ajax_action( 'retry_connectivity_test', array(
4858
- $this,
4859
- '_retry_connectivity_test'
4860
- ) );*/
4861
- }
4862
- }
4863
-
4864
- return;
4865
- } else {
4866
- $this->_admin_notices->remove_sticky( array(
4867
- 'failed_connect_api_first',
4868
- 'failed_connect_api',
4869
- ) );
4870
-
4871
- if ( $this->_anonymous_mode ) {
4872
- // Simulate anonymous mode.
4873
- $this->_is_anonymous = true;
4874
- }
4875
- }
4876
- }
4877
- }
4878
-
4879
- /**
4880
- * This should be executed even if Freemius is off for the core module,
4881
- * otherwise, the add-ons dialogbox won't work properly. This is esepcially
4882
- * relevant when the developer decided to turn FS off for existing users.
4883
- *
4884
- * @author Vova Feldman (@svovaf)
4885
- */
4886
- if ( $this->is_user_in_admin() &&
4887
- 'plugin-information' === fs_request_get( 'tab', false ) &&
4888
- $this->should_use_freemius_updater_and_dialog() &&
4889
- (
4890
- ( $this->is_addon() && $this->get_slug() == fs_request_get( 'plugin', false ) ) ||
4891
- ( $this->has_addons() && $this->get_id() == fs_request_get( 'parent_plugin_id', false ) )
4892
- )
4893
- ) {
4894
- require_once WP_FS__DIR_INCLUDES . '/fs-plugin-info-dialog.php';
4895
-
4896
- new FS_Plugin_Info_Dialog( $this->is_addon() ? $this->get_parent_instance() : $this );
4897
- }
4898
-
4899
- // Check if Freemius is on for the current plugin.
4900
- // This MUST be executed after all the plugin variables has been loaded.
4901
- if ( ! $this->is_registered() && ! $this->is_on() ) {
4902
- return;
4903
- }
4904
-
4905
- if ( $this->has_api_connectivity() ) {
4906
- if ( self::is_cron() ) {
4907
- $this->hook_callback_to_sync_cron();
4908
- } else if ( $this->is_user_in_admin() ) {
4909
- /**
4910
- * Schedule daily data sync cron if:
4911
- *
4912
- * 1. User opted-in (for tracking).
4913
- * 2. If skipped, but later upgraded (opted-in via upgrade).
4914
- *
4915
- * @author Vova Feldman (@svovaf)
4916
- * @since 1.1.7.3
4917
- *
4918
- */
4919
- if ( $this->is_registered() ) {
4920
- if ( ! $this->is_sync_cron_on() && $this->is_tracking_allowed() ) {
4921
- $this->schedule_sync_cron();
4922
- }
4923
- }
4924
-
4925
- /**
4926
- * Check if requested for manual blocking background sync.
4927
- */
4928
- if ( fs_request_has( 'background_sync' ) ) {
4929
- $this->run_manual_sync();
4930
- }
4931
- }
4932
- }
4933
-
4934
- if ( $this->is_registered() ) {
4935
- $this->hook_callback_to_install_sync();
4936
- }
4937
-
4938
- if ( $this->is_addon() ) {
4939
- if ( $this->is_parent_plugin_installed() ) {
4940
- // Link to parent FS.
4941
- $this->_parent = self::get_instance_by_id( $this->_plugin->parent_plugin_id );
4942
-
4943
- // Get parent plugin reference.
4944
- $this->_parent_plugin = $this->_parent->get_plugin();
4945
- }
4946
- }
4947
-
4948
- if ( $this->is_user_in_admin() ) {
4949
- if ( $this->is_addon() ) {
4950
- if ( ! $this->is_parent_plugin_installed() ) {
4951
- $parent_name = $this->get_option( $plugin_info, 'parent_name', null );
4952
-
4953
- if ( isset( $plugin_info['parent'] ) ) {
4954
- $parent_name = $this->get_option( $plugin_info['parent'], 'name', null );
4955
- }
4956
-
4957
- $this->_admin_notices->add(
4958
- ( ! empty( $parent_name ) ?
4959
- 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 ) :
4960
- 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() )
4961
- ),
4962
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
4963
- 'error'
4964
- );
4965
-
4966
- return;
4967
- } else {
4968
- $is_network_admin = fs_is_network_admin();
4969
-
4970
- if ( ! $this->_parent->is_registered() && $this->is_registered() ) {
4971
- // If add-on activated and parent not, automatically install parent for the user.
4972
- $this->activate_parent_account( $this->_parent );
4973
- } else if (
4974
- $this->_parent->is_registered() &&
4975
- ! $this->is_registered() &&
4976
- /**
4977
- * If not registered for add-on and the following conditions for the add-on are met, activate add-on account.
4978
- * * Network active and in network admin - network activate add-on account.
4979
- * * Network active and not in network admin - activate add-on account for the current blog.
4980
- * * Not network active and not in network admin - activate add-on account for the current blog.
4981
- *
4982
- * If not registered for add-on, not network active, and in network admin, do not handle the add-on activation.
4983
- *
4984
- * @author Leo Fajardo (@leorw)
4985
- * @since 2.3.0
4986
- */
4987
- ( $this->is_network_active() || ! $is_network_admin )
4988
- ) {
4989
- $premium_license = null;
4990
-
4991
- if (
4992
- ! $this->has_free_plan() &&
4993
- $this->is_bundle_license_auto_activation_enabled() &&
4994
- $this->_parent->is_activated_with_bundle_license()
4995
- ) {
4996
- /**
4997
- * If the add-on has no free plan, try to activate the account only when there's a bundle license.
4998
- *
4999
- * @author Leo Fajardo (@leorw)
5000
- * @since 2.4.0
5001
- */
5002
- $bundle_license = $this->get_active_parent_license( $this->_parent->_get_license()->secret_key, false );
5003
-
5004
- if (
5005
- is_object( $bundle_license ) &&
5006
- ! empty( $bundle_license->products ) &&
5007
- in_array( $this->get_id(), $bundle_license->products )
5008
- ) {
5009
- $premium_license = $bundle_license;
5010
- }
5011
- }
5012
-
5013
- if ( $this->has_free_plan() || is_object( $premium_license) ) {
5014
- // If parent plugin activated, automatically install add-on for the user.
5015
- $this->_activate_addon_account(
5016
- $this->_parent,
5017
- ( $this->is_network_active() && $is_network_admin ) ?
5018
- true :
5019
- get_current_blog_id(),
5020
- $premium_license
5021
- );
5022
- }
5023
- }
5024
-
5025
- // @todo This should be only executed on activation. It should be migrated to register_activation_hook() together with other activation related logic.
5026
- if ( $this->is_premium() ) {
5027
- // Remove add-on download admin-notice.
5028
- $this->_parent->_admin_notices->remove_sticky( array(
5029
- 'addon_plan_upgraded_' . $this->_slug,
5030
- 'no_addon_license_' . $this->_slug,
5031
- ) );
5032
- }
5033
-
5034
- // $this->deactivate_premium_only_addon_without_license();
5035
- }
5036
- }
5037
-
5038
- add_action( 'admin_init', array( $this, '_admin_init_action' ) );
5039
-
5040
- // if ( $this->is_registered() ||
5041
- // $this->is_anonymous() ||
5042
- // $this->is_pending_activation()
5043
- // ) {
5044
- // $this->_init_admin();
5045
- // }
5046
- }
5047
-
5048
- /**
5049
- * Should be called outside `$this->is_user_in_admin()` scope
5050
- * because the updater has some logic that needs to be executed
5051
- * during AJAX calls.
5052
- *
5053
- * Currently we need to hook to the `http_request_host_is_external` filter.
5054
- * In the future, there might be additional logic added.
5055
- *
5056
- * @author Vova Feldman
5057
- * @since 1.2.1.6
5058
- */
5059
- if (
5060
- $this->should_use_freemius_updater_and_dialog() &&
5061
- (
5062
- $this->is_premium() ||
5063
- /**
5064
- * If not premium but the premium version is installed, also instantiate the updater so that the
5065
- * plugin information dialog of the premium version will have the information from the server.
5066
- *
5067
- * @author Leo Fajardo (@leorw)
5068
- * @since 2.2.3
5069
- */
5070
- ( file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $this->premium_plugin_basename() ) ) )
5071
- ) &&
5072
- $this->has_release_on_freemius()
5073
- ) {
5074
- FS_Plugin_Updater::instance( $this );
5075
- }
5076
-
5077
- $this->do_action( 'initiated' );
5078
-
5079
- if ( $this->_storage->prev_is_premium !== $this->_plugin->is_premium ) {
5080
- if ( isset( $this->_storage->prev_is_premium ) ) {
5081
- $this->apply_filters(
5082
- 'after_code_type_change',
5083
- // New code type.
5084
- $this->_plugin->is_premium
5085
- );
5086
- } else {
5087
- // Set for code type for the first time.
5088
- $this->_storage->prev_is_premium = $this->_plugin->is_premium;
5089
- }
5090
- }
5091
-
5092
- if ( ! $this->is_addon() ) {
5093
- if ( $this->is_registered() ) {
5094
- // Fix for upgrade from versions < 1.0.9.
5095
- if ( ! isset( $this->_storage->activation_timestamp ) ) {
5096
- $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME;
5097
- }
5098
-
5099
- $this->do_action( 'after_init_plugin_registered' );
5100
- } else if ( $this->is_anonymous() ) {
5101
- $this->do_action( 'after_init_plugin_anonymous' );
5102
- } else if ( $this->is_pending_activation() ) {
5103
- $this->do_action( 'after_init_plugin_pending_activations' );
5104
- }
5105
- } else {
5106
- if ( $this->is_registered() ) {
5107
- $this->do_action( 'after_init_addon_registered' );
5108
- } else if ( $this->is_anonymous() ) {
5109
- $this->do_action( 'after_init_addon_anonymous' );
5110
- } else if ( $this->is_pending_activation() ) {
5111
- $this->do_action( 'after_init_addon_pending_activations' );
5112
- }
5113
- }
5114
- }
5115
-
5116
- /**
5117
- * @author Leo Fajardo (@leorw)
5118
- * @since 2.2.3
5119
- *
5120
- * @return bool
5121
- */
5122
- private function should_use_freemius_updater_and_dialog() {
5123
- return (
5124
- /**
5125
- * Allow updater and dialog when the `fs_allow_updater_and_dialog` URL query param exists and has `true`
5126
- * value, or when the current page is not the "Add Plugins" page (/plugin-install.php) and the `action`
5127
- * URL query param doesn't exist or its value is not `install-plugin` so that there will be no conflicts
5128
- * with the .org plugins' functionalities (e.g. installation from the "Add Plugins" page and viewing
5129
- * plugin details from .org).
5130
- */
5131
- ( true === fs_request_get_bool( 'fs_allow_updater_and_dialog' ) ) ||
5132
- (
5133
- ! self::is_plugin_install_page() &&
5134
- // Disallow updater and dialog when installing a plugin, otherwise .org "add-on" plugins will be affected.
5135
- ( 'install-plugin' !== fs_request_get( 'action' ) )
5136
- )
5137
- );
5138
- }
5139
-
5140
- /**
5141
- * @author Leo Fajardo (@leorw)
5142
- *
5143
- * @since 1.2.1.5
5144
- */
5145
- function _stop_tracking_callback() {
5146
- $this->_logger->entrance();
5147
-
5148
- $this->check_ajax_referer( 'stop_tracking' );
5149
-
5150
- $result = $this->stop_tracking( fs_is_network_admin() );
5151
-
5152
- if ( true === $result ) {
5153
- self::shoot_ajax_success();
5154
- }
5155
-
5156
- $this->_logger->api_error( $result );
5157
-
5158
- self::shoot_ajax_failure(
5159
- sprintf( $this->get_text_inline( 'Unexpected API error. Please contact the %s\'s author with the following error.', 'unexpected-api-error' ), $this->_module_type ) .
5160
- ( $this->is_api_error( $result ) && isset( $result->error ) ?
5161
- $result->error->message :
5162
- var_export( $result, true ) )
5163
- );
5164
- }
5165
-
5166
- /**
5167
- * @author Leo Fajardo (@leorw)
5168
- * @since 1.2.1.5
5169
- */
5170
- function _allow_tracking_callback() {
5171
- $this->_logger->entrance();
5172
-
5173
- $this->check_ajax_referer( 'allow_tracking' );
5174
-
5175
- $result = $this->allow_tracking( fs_is_network_admin() );
5176
-
5177
- if ( true === $result ) {
5178
- self::shoot_ajax_success();
5179
- }
5180
-
5181
- $this->_logger->api_error( $result );
5182
-
5183
- self::shoot_ajax_failure(
5184
- sprintf( $this->get_text_inline( 'Unexpected API error. Please contact the %s\'s author with the following error.', 'unexpected-api-error' ), $this->_module_type ) .
5185
- ( $this->is_api_error( $result ) && isset( $result->error ) ?
5186
- $result->error->message :
5187
- var_export( $result, true ) )
5188
- );
5189
- }
5190
-
5191
- /**
5192
- * Opt-out from usage tracking.
5193
- *
5194
- * Note: This will not delete the account information but will stop all tracking.
5195
- *
5196
- * Returns:
5197
- * 1. FALSE - If the user never opted-in.
5198
- * 2. TRUE - If successfully opted-out.
5199
- * 3. object - API result on failure.
5200
- *
5201
- * @author Leo Fajardo (@leorw)
5202
- * @since 1.2.1.5
5203
- *
5204
- * @return bool|object
5205
- */
5206
- function stop_site_tracking() {
5207
- $this->_logger->entrance();
5208
-
5209
- if ( ! $this->is_registered() ) {
5210
- // User never opted-in.
5211
- return false;
5212
- }
5213
-
5214
- if ( $this->is_tracking_prohibited() ) {
5215
- // Already disconnected.
5216
- return true;
5217
- }
5218
-
5219
- // Send update to FS.
5220
- $result = $this->get_api_site_scope()->call( '/?fields=is_disconnected', 'put', array(
5221
- 'is_disconnected' => true
5222
- ) );
5223
-
5224
- if ( ! $this->is_api_result_entity( $result ) ||
5225
- ! isset( $result->is_disconnected ) ||
5226
- ! $result->is_disconnected
5227
- ) {
5228
- $this->_logger->api_error( $result );
5229
-
5230
- return $result;
5231
- }
5232
-
5233
- $this->_site->is_disconnected = $result->is_disconnected;
5234
- $this->_store_site();
5235
-
5236
- $this->clear_sync_cron();
5237
-
5238
- // Successfully disconnected.
5239
- return true;
5240
- }
5241
-
5242
- /**
5243
- * Opt-out network from usage tracking.
5244
- *
5245
- * Note: This will not delete the account information but will stop all tracking.
5246
- *
5247
- * Returns:
5248
- * 1. FALSE - If the user never opted-in.
5249
- * 2. TRUE - If successfully opted-out.
5250
- * 3. object - API result on failure.
5251
- *
5252
- * @author Leo Fajardo (@leorw)
5253
- * @since 1.2.1.5
5254
- *
5255
- * @return bool|object
5256
- */
5257
- function stop_network_tracking() {
5258
- $this->_logger->entrance();
5259
-
5260
- if ( ! $this->is_registered() ) {
5261
- // User never opted-in.
5262
- return false;
5263
- }
5264
-
5265
- $install_id_2_blog_id = array();
5266
- $installs_map = $this->get_blog_install_map();
5267
-
5268
- $opt_out_all = true;
5269
-
5270
- $params = array();
5271
- foreach ( $installs_map as $blog_id => $install ) {
5272
- if ( $install->is_tracking_prohibited() ) {
5273
- // Already opted-out.
5274
- continue;
5275
- }
5276
-
5277
- if ( $this->is_site_delegated_connection( $blog_id ) ) {
5278
- // Opt-out only from non-delegated installs.
5279
- $opt_out_all = false;
5280
- continue;
5281
- }
5282
-
5283
- $params[] = array( 'id' => $install->id );
5284
-
5285
- $install_id_2_blog_id[ $install->id ] = $blog_id;
5286
- }
5287
-
5288
- if ( empty( $install_id_2_blog_id ) ) {
5289
- return true;
5290
- }
5291
-
5292
- $params[] = array( 'is_disconnected' => true );
5293
-
5294
- // Send update to FS.
5295
- $result = $this->get_current_or_network_user_api_scope()->call( "/plugins/{$this->_module_id}/installs.json", 'put', $params );
5296
-
5297
- if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
5298
- $this->_logger->api_error( $result );
5299
-
5300
- return $result;
5301
- }
5302
-
5303
- foreach ( $result->installs as $r_install ) {
5304
- $blog_id = $install_id_2_blog_id[ $r_install->id ];
5305
- $install = $installs_map[ $blog_id ];
5306
- $install->is_disconnected = $r_install->is_disconnected;
5307
- $this->_store_site( true, $blog_id, $install );
5308
- }
5309
-
5310
- $this->clear_sync_cron( $opt_out_all );
5311
-
5312
- // Successfully disconnected.
5313
- return true;
5314
- }
5315
-
5316
- /**
5317
- * Opt-out from usage tracking.
5318
- *
5319
- * Note: This will not delete the account information but will stop all tracking.
5320
- *
5321
- * Returns:
5322
- * 1. FALSE - If the user never opted-in.
5323
- * 2. TRUE - If successfully opted-out.
5324
- * 3. object - API result on failure.
5325
- *
5326
- * @author Leo Fajardo (@leorw)
5327
- * @since 1.2.1.5
5328
- *
5329
- * @param bool $is_network_action
5330
- *
5331
- * @return bool|object
5332
- */
5333
- function stop_tracking( $is_network_action = false ) {
5334
- $this->_logger->entrance();
5335
-
5336
- return $is_network_action ?
5337
- $this->stop_network_tracking() :
5338
- $this->stop_site_tracking();
5339
- }
5340
-
5341
- /**
5342
- * Opt-in back into usage tracking.
5343
- *
5344
- * Note: This will only work if the user opted-in previously.
5345
- *
5346
- * Returns:
5347
- * 1. FALSE - If the user never opted-in.
5348
- * 2. TRUE - If successfully opted-in back to usage tracking.
5349
- * 3. object - API result on failure.
5350
- *
5351
- * @author Leo Fajardo (@leorw)
5352
- * @since 1.2.1.5
5353
- *
5354
- * @return bool|object
5355
- */
5356
- function allow_site_tracking() {
5357
- $this->_logger->entrance();
5358
-
5359
- if ( ! $this->is_registered() ) {
5360
- // User never opted-in.
5361
- return false;
5362
- }
5363
-
5364
- if ( $this->is_tracking_allowed() ) {
5365
- // Tracking already allowed.
5366
- return true;
5367
- }
5368
-
5369
- $result = $this->get_api_site_scope()->call( '/?is_disconnected', 'put', array(
5370
- 'is_disconnected' => false
5371
- ) );
5372
-
5373
- if ( ! $this->is_api_result_entity( $result ) ||
5374
- ! isset( $result->is_disconnected ) ||
5375
- $result->is_disconnected
5376
- ) {
5377
- $this->_logger->api_error( $result );
5378
-
5379
- return $result;
5380
- }
5381
-
5382
- $this->_site->is_disconnected = $result->is_disconnected;
5383
- $this->_store_site();
5384
-
5385
- $this->schedule_sync_cron();
5386
-
5387
- // Successfully reconnected.
5388
- return true;
5389
- }
5390
-
5391
- /**
5392
- * Opt-in network back into usage tracking.
5393
- *
5394
- * Note: This will only work if the user opted-in previously.
5395
- *
5396
- * Returns:
5397
- * 1. FALSE - If the user never opted-in.
5398
- * 2. TRUE - If successfully opted-in back to usage tracking.
5399
- * 3. object - API result on failure.
5400
- *
5401
- * @author Leo Fajardo (@leorw)
5402
- * @since 1.2.1.5
5403
- *
5404
- * @return bool|object
5405
- */
5406
- function allow_network_tracking() {
5407
- $this->_logger->entrance();
5408
-
5409
- if ( ! $this->is_registered() ) {
5410
- // User never opted-in.
5411
- return false;
5412
- }
5413
-
5414
- $install_id_2_blog_id = array();
5415
- $installs_map = $this->get_blog_install_map();
5416
-
5417
- $params = array();
5418
- foreach ( $installs_map as $blog_id => $install ) {
5419
- if ( $install->is_tracking_allowed() ) {
5420
- continue;
5421
- }
5422
-
5423
- $params[] = array( 'id' => $install->id );
5424
-
5425
- $install_id_2_blog_id[ $install->id ] = $blog_id;
5426
- }
5427
-
5428
- if ( empty( $install_id_2_blog_id ) ) {
5429
- return true;
5430
- }
5431
-
5432
- $params[] = array( 'is_disconnected' => false );
5433
-
5434
- // Send update to FS.
5435
- $result = $this->get_current_or_network_user_api_scope()->call( "/plugins/{$this->_module_id}/installs.json", 'put', $params );
5436
-
5437
-
5438
- if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
5439
- $this->_logger->api_error( $result );
5440
-
5441
- return $result;
5442
- }
5443
-
5444
- foreach ( $result->installs as $r_install ) {
5445
- $blog_id = $install_id_2_blog_id[ $r_install->id ];
5446
- $install = $installs_map[ $blog_id ];
5447
- $install->is_disconnected = $r_install->is_disconnected;
5448
- $this->_store_site( true, $blog_id, $install );
5449
- }
5450
-
5451
- $this->schedule_sync_cron();
5452
-
5453
- // Successfully reconnected.
5454
- return true;
5455
- }
5456
-
5457
- /**
5458
- * Opt-in back into usage tracking.
5459
- *
5460
- * Note: This will only work if the user opted-in previously.
5461
- *
5462
- * Returns:
5463
- * 1. FALSE - If the user never opted-in.
5464
- * 2. TRUE - If successfully opted-in back to usage tracking.
5465
- * 3. object - API result on failure.
5466
- *
5467
- * @author Leo Fajardo (@leorw)
5468
- * @since 1.2.1.5
5469
- *
5470
- * @param bool $is_network_action
5471
- *
5472
- * @return bool|object
5473
- */
5474
- function allow_tracking( $is_network_action = false ) {
5475
- $this->_logger->entrance();
5476
-
5477
- return $is_network_action ?
5478
- $this->allow_network_tracking() :
5479
- $this->allow_site_tracking();
5480
- }
5481
-
5482
- /**
5483
- * If user opted-in and later disabled usage-tracking,
5484
- * re-allow tracking for licensing and updates.
5485
- *
5486
- * @author Leo Fajardo (@leorw)
5487
- * @since 1.2.1.5
5488
- *
5489
- * @param bool $is_context_single_site
5490
- */
5491
- private function reconnect_locally( $is_context_single_site = false ) {
5492
- $this->_logger->entrance();
5493
-
5494
- if ( ! $this->is_registered() ) {
5495
- return;
5496
- }
5497
-
5498
- if ( ! fs_is_network_admin() || $is_context_single_site ) {
5499
- if ( $this->is_tracking_prohibited() ) {
5500
- $this->_site->is_disconnected = false;
5501
- $this->_store_site();
5502
- }
5503
- } else {
5504
- $installs_map = $this->get_blog_install_map();
5505
- foreach ( $installs_map as $blog_id => $install ) {
5506
- /**
5507
- * @var FS_Site $install
5508
- */
5509
- if ( $install->is_tracking_prohibited() ) {
5510
- $install->is_disconnected = false;
5511
- $this->_store_site( true, $blog_id, $install );
5512
- }
5513
- }
5514
- }
5515
- }
5516
-
5517
- /**
5518
- * @author Vova Feldman (@svovaf)
5519
- * @since 2.3.2
5520
- *
5521
- * @return bool
5522
- */
5523
- function is_extensions_tracking_allowed() {
5524
- return ( true === $this->apply_filters(
5525
- 'is_extensions_tracking_allowed',
5526
- $this->_storage->get( 'is_extensions_tracking_allowed', null )
5527
- ) );
5528
- }
5529
-
5530
- /**
5531
- * @author Vova Feldman (@svovaf)
5532
- * @since 2.3.2
5533
- */
5534
- function _update_tracking_permission_callback() {
5535
- $this->_logger->entrance();
5536
-
5537
- $this->check_ajax_referer( 'update_tracking_permission' );
5538
-
5539
- $is_enabled = fs_request_get_bool( 'is_enabled', null );
5540
-
5541
- if ( ! is_bool( $is_enabled ) ) {
5542
- self::shoot_ajax_failure();
5543
- }
5544
-
5545
- $permission = fs_request_get( 'permission' );
5546
-
5547
- switch ( $permission ) {
5548
- case 'extensions':
5549
- $this->update_extensions_tracking_flag( $is_enabled );
5550
- break;
5551
- default:
5552
- $permission = 'no_match';
5553
- }
5554
-
5555
- if ( 'no_match' === $permission ) {
5556
- self::shoot_ajax_failure();
5557
- }
5558
-
5559
- self::shoot_ajax_success( array(
5560
- 'permissions' => array(
5561
- $permission => $is_enabled,
5562
- )
5563
- ) );
5564
- }
5565
-
5566
- /**
5567
- * @author Leo Fajardo (@leorw)
5568
- * @since 2.3.2
5569
- *
5570
- * @param bool|null $is_enabled
5571
- */
5572
- function update_extensions_tracking_flag( $is_enabled ) {
5573
- if ( is_bool( $is_enabled ) ) {
5574
- $this->_storage->store( 'is_extensions_tracking_allowed', $is_enabled );
5575
- }
5576
- }
5577
-
5578
- /**
5579
- * Parse plugin's settings (as defined by the plugin dev).
5580
- *
5581
- * @author Vova Feldman (@svovaf)
5582
- * @since 1.1.7.3
5583
- *
5584
- * @param array $plugin_info
5585
- *
5586
- * @throws \Freemius_Exception
5587
- */
5588
- private function parse_settings( &$plugin_info ) {
5589
- $this->_logger->entrance();
5590
-
5591
- $id = $this->get_numeric_option( $plugin_info, 'id', false );
5592
- $public_key = $this->get_option( $plugin_info, 'public_key', false );
5593
- $secret_key = $this->get_option( $plugin_info, 'secret_key', null );
5594
- $parent_id = $this->get_numeric_option( $plugin_info, 'parent_id', null );
5595
- $parent_name = $this->get_option( $plugin_info, 'parent_name', null );
5596
-
5597
- /**
5598
- * @author Vova Feldman (@svovaf)
5599
- * @since 1.1.9 Try to pull secret key from external config.
5600
- */
5601
- if ( is_null( $secret_key ) && defined( "WP_FS__{$this->_slug}_SECRET_KEY" ) ) {
5602
- $secret_key = constant( "WP_FS__{$this->_slug}_SECRET_KEY" );
5603
- }
5604
-
5605
- if ( isset( $plugin_info['parent'] ) ) {
5606
- $parent_id = $this->get_numeric_option( $plugin_info['parent'], 'id', null );
5607
- // $parent_slug = $this->get_option( $plugin_info['parent'], 'slug', null );
5608
- // $parent_public_key = $this->get_option( $plugin_info['parent'], 'public_key', null );
5609
- // $parent_name = $this->get_option( $plugin_info['parent'], 'name', null );
5610
- }
5611
-
5612
- if ( false === $id ) {
5613
- throw new Freemius_Exception( array(
5614
- 'error' => array(
5615
- 'type' => 'ParameterNotSet',
5616
- 'message' => 'Plugin id parameter is not set.',
5617
- 'code' => 'plugin_id_not_set',
5618
- 'http' => 500,
5619
- )
5620
- ) );
5621
- }
5622
- if ( false === $public_key ) {
5623
- throw new Freemius_Exception( array(
5624
- 'error' => array(
5625
- 'type' => 'ParameterNotSet',
5626
- 'message' => 'Plugin public_key parameter is not set.',
5627
- 'code' => 'plugin_public_key_not_set',
5628
- 'http' => 500,
5629
- )
5630
- ) );
5631
- }
5632
-
5633
- $plugin = ( $this->_plugin instanceof FS_Plugin ) ?
5634
- $this->_plugin :
5635
- new FS_Plugin();
5636
-
5637
- $premium_suffix = $this->get_option( $plugin_info, 'premium_suffix', '(Premium)' );
5638
-
5639
- $plugin->update( array(
5640
- 'id' => $id,
5641
- 'type' => $this->get_option( $plugin_info, 'type', $this->_module_type ),
5642
- 'public_key' => $public_key,
5643
- 'slug' => $this->_slug,
5644
- 'premium_slug' => $this->get_option( $plugin_info, 'premium_slug', "{$this->_slug}-premium" ),
5645
- 'parent_plugin_id' => $parent_id,
5646
- 'version' => $this->get_plugin_version(),
5647
- 'title' => $this->get_plugin_name( $premium_suffix ),
5648
- 'file' => $this->_plugin_basename,
5649
- 'is_premium' => $this->get_bool_option( $plugin_info, 'is_premium', true ),
5650
- 'premium_suffix' => $premium_suffix,
5651
- 'is_live' => $this->get_bool_option( $plugin_info, 'is_live', true ),
5652
- 'affiliate_moderation' => $this->get_option( $plugin_info, 'has_affiliation' ),
5653
- 'bundle_id' => $this->get_option( $plugin_info, 'bundle_id', null ),
5654
- 'bundle_public_key' => $this->get_option( $plugin_info, 'bundle_public_key', null ),
5655
- ) );
5656
-
5657
- if ( $plugin->is_updated() ) {
5658
- // Update plugin details.
5659
- $this->_plugin = FS_Plugin_Manager::instance( $this->_module_id )->store( $plugin );
5660
- }
5661
- // Set the secret key after storing the plugin, we don't want to store the key in the storage.
5662
- $this->_plugin->secret_key = $secret_key;
5663
-
5664
- /**
5665
- * If the product is network integrated and activated and the current view is in the network level Admin dashboard, if the product's network-level menu located differently from the sub-site level, then use the network menu details (when set).
5666
- *
5667
- * @author Vova Feldman
5668
- * @since 2.4.5
5669
- */
5670
- if ( $this->is_network_active() && fs_is_network_admin() ) {
5671
- if ( isset( $plugin_info['menu_network'] ) &&
5672
- is_array( $plugin_info['menu_network'] ) &&
5673
- ! empty( $plugin_info['menu_network'] )
5674
- ) {
5675
- $plugin_info['menu'] = $plugin_info['menu_network'];
5676
- }
5677
- }
5678
-
5679
- if ( ! isset( $plugin_info['menu'] ) ) {
5680
- $plugin_info['menu'] = array();
5681
-
5682
- if ( ! empty( $this->_storage->sdk_last_version ) &&
5683
- version_compare( $this->_storage->sdk_last_version, '1.1.2', '<=' )
5684
- ) {
5685
- // Backward compatibility to 1.1.2
5686
- $plugin_info['menu']['slug'] = isset( $plugin_info['menu_slug'] ) ?
5687
- $plugin_info['menu_slug'] :
5688
- $this->_slug;
5689
- }
5690
- }
5691
-
5692
- $this->_menu = FS_Admin_Menu_Manager::instance(
5693
- $this->_module_id,
5694
- $this->_module_type,
5695
- $this->get_unique_affix()
5696
- );
5697
-
5698
- $this->_menu->init( $plugin_info['menu'], $this->is_addon() );
5699
-
5700
- $this->_has_addons = $this->get_bool_option( $plugin_info, 'has_addons', false );
5701
- $this->_has_paid_plans = $this->get_bool_option( $plugin_info, 'has_paid_plans', true );
5702
- $this->_has_premium_version = $this->get_bool_option( $plugin_info, 'has_premium_version', $this->_has_paid_plans );
5703
- $this->_ignore_pending_mode = $this->get_bool_option( $plugin_info, 'ignore_pending_mode', false );
5704
- $this->_is_org_compliant = $this->get_bool_option( $plugin_info, 'is_org_compliant', true );
5705
- $this->_is_premium_only = $this->get_bool_option( $plugin_info, 'is_premium_only', false );
5706
- if ( $this->_is_premium_only ) {
5707
- // If premium only plugin, disable anonymous mode.
5708
- $this->_enable_anonymous = false;
5709
- $this->_anonymous_mode = false;
5710
- } else {
5711
- $this->_enable_anonymous = $this->get_bool_option( $plugin_info, 'enable_anonymous', true );
5712
- $this->_anonymous_mode = $this->get_bool_option( $plugin_info, 'anonymous_mode', false );
5713
- }
5714
- $this->_permissions = $this->get_option( $plugin_info, 'permissions', array() );
5715
- $this->_is_bundle_license_auto_activation_enabled = $this->get_option( $plugin_info, 'bundle_license_auto_activation', false );
5716
-
5717
- if ( ! empty( $plugin_info['trial'] ) ) {
5718
- $this->_trial_days = $this->get_numeric_option(
5719
- $plugin_info['trial'],
5720
- 'days',
5721
- // Default to 0 - trial without days specification.
5722
- 0
5723
- );
5724
-
5725
- $this->_is_trial_require_payment = $this->get_bool_option( $plugin_info['trial'], 'is_require_payment', false );
5726
- }
5727
-
5728
- $this->_navigation = $this->get_option(
5729
- $plugin_info,
5730
- 'navigation',
5731
- $this->is_free_wp_org_theme() ?
5732
- self::NAVIGATION_TABS :
5733
- self::NAVIGATION_MENU
5734
- );
5735
- }
5736
-
5737
- /**
5738
- * @param string[] $options
5739
- * @param string $key
5740
- * @param mixed $default
5741
- *
5742
- * @return bool
5743
- */
5744
- private function get_option( &$options, $key, $default = false ) {
5745
- return ! empty( $options[ $key ] ) ? $options[ $key ] : $default;
5746
- }
5747
-
5748
- private function get_bool_option( &$options, $key, $default = false ) {
5749
- return isset( $options[ $key ] ) && is_bool( $options[ $key ] ) ? $options[ $key ] : $default;
5750
- }
5751
-
5752
- private function get_numeric_option( &$options, $key, $default = false ) {
5753
- return isset( $options[ $key ] ) && is_numeric( $options[ $key ] ) ? $options[ $key ] : $default;
5754
- }
5755
-
5756
- /**
5757
- * Gate keeper.
5758
- *
5759
- * @author Vova Feldman (@svovaf)
5760
- * @since 1.1.7.3
5761
- *
5762
- * @return bool
5763
- */
5764
- private function should_stop_execution() {
5765
- if ( empty( $this->_storage->was_plugin_loaded ) ) {
5766
- /**
5767
- * Don't execute Freemius until plugin was fully loaded at least once,
5768
- * to give the opportunity for the activation hook to run before pinging
5769
- * the API for connectivity test. This logic is relevant for the
5770
- * identification of new plugin install vs. plugin update.
5771
- *
5772
- * @author Vova Feldman (@svovaf)
5773
- * @since 1.1.9
5774
- */
5775
- return true;
5776
- }
5777
-
5778
- if ( $this->is_activation_mode() ) {
5779
- if ( ! is_admin() ) {
5780
- /**
5781
- * If in activation mode, don't execute Freemius outside of the
5782
- * admin dashboard.
5783
- *
5784
- * @author Vova Feldman (@svovaf)
5785
- * @since 1.1.7.3
5786
- */
5787
- return true;
5788
- }
5789
-
5790
- if ( ! WP_FS__IS_HTTP_REQUEST ) {
5791
- /**
5792
- * If in activation and executed without HTTP context (e.g. CLI, Cronjob),
5793
- * then don't start Freemius.
5794
- *
5795
- * @author Vova Feldman (@svovaf)
5796
- * @since 1.1.6.3
5797
- *
5798
- * @link https://wordpress.org/support/topic/errors-in-the-freemius-class-when-running-in-wordpress-in-cli
5799
- */
5800
- return true;
5801
- }
5802
-
5803
- if ( self::is_cron() ) {
5804
- /**
5805
- * If in activation mode, don't execute Freemius during wp crons
5806
- * (wp crons have HTTP context - called as HTTP request).
5807
- *
5808
- * @author Vova Feldman (@svovaf)
5809
- * @since 1.1.7.3
5810
- */
5811
- return true;
5812
- }
5813
-
5814
- if ( self::is_ajax() &&
5815
- ! $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) &&
5816
- ! $this->_admin_notices->has_sticky( 'failed_connect_api' )
5817
- ) {
5818
- /**
5819
- * During activation, if running in AJAX mode, unless there's a sticky
5820
- * connectivity issue notice, don't run Freemius.
5821
- *
5822
- * @author Vova Feldman (@svovaf)
5823
- * @since 1.1.7.3
5824
- */
5825
- return true;
5826
- }
5827
- }
5828
-
5829
- return false;
5830
- }
5831
-
5832
- /**
5833
- * Triggered after code type has changed.
5834
- *
5835
- * @author Vova Feldman (@svovaf)
5836
- * @since 1.1.9.1
5837
- */
5838
- function _after_code_type_change() {
5839
- $this->_logger->entrance();
5840
-
5841
- if ( $this->is_theme() ) {
5842
- // Expire the cache of the previous tabs since the theme may
5843
- // have setting updates after code type has changed.
5844
- $this->_cache->expire( 'tabs' );
5845
- $this->_cache->expire( 'tabs_stylesheets' );
5846
- }
5847
-
5848
- if ( $this->is_registered() ) {
5849
- if ( ! $this->is_addon() ) {
5850
- add_action(
5851
- is_admin() ? 'admin_init' : 'init',
5852
- array( $this, '_plugin_code_type_changed' )
5853
- );
5854
- }
5855
-
5856
- if ( $this->is_premium() ) {
5857
- // Purge cached payments after switching to the premium version.
5858
- // @todo This logic doesn't handle purging the cache for serviceware module upgrade.
5859
- $this->get_api_user_scope()->purge_cache( "/plugins/{$this->_module_id}/payments.json?include_addons=true" );
5860
- }
5861
- }
5862
- }
5863
-
5864
- /**
5865
- * Handles plugin's code type change (free <--> premium).
5866
- *
5867
- * @author Vova Feldman (@svovaf)
5868
- * @since 1.0.9
5869
- */
5870
- function _plugin_code_type_changed() {
5871
- $this->_logger->entrance();
5872
-
5873
- if ( $this->is_premium() ) {
5874
- $this->reconnect_locally();
5875
-
5876
- // Activated premium code.
5877
- $this->do_action( 'after_premium_version_activation' );
5878
-
5879
- // Remove all sticky messages related to download of the premium version.
5880
- $this->_admin_notices->remove_sticky( array(
5881
- 'trial_started',
5882
- 'plan_upgraded',
5883
- 'plan_changed',
5884
- 'license_activated',
5885
- ) );
5886
-
5887
- $notice = '';
5888
- if ( ! $this->is_only_premium() ) {
5889
- $notice = sprintf( $this->get_text_inline( 'Premium %s version was successfully activated.', 'premium-activated-message' ), $this->_module_type );
5890
- }
5891
-
5892
- $license_notice = $this->get_license_network_activation_notice();
5893
- if ( ! empty( $license_notice ) ) {
5894
- $notice .= ' ' . $license_notice;
5895
- }
5896
-
5897
- if ( ! empty( $notice ) ) {
5898
- $this->_admin_notices->add_sticky(
5899
- trim( $notice ),
5900
- 'premium_activated',
5901
- $this->get_text_x_inline( 'W00t',
5902
- 'Used to express elation, enthusiasm, or triumph (especially in electronic communication).', 'woot' ) . '!'
5903
- );
5904
- }
5905
- } else {
5906
- // Remove sticky message related to premium code activation.
5907
- $this->_admin_notices->remove_sticky( 'premium_activated' );
5908
-
5909
- // Activated free code (after had the premium before).
5910
- $this->do_action( 'after_free_version_reactivation' );
5911
-
5912
- if ( $this->is_paying() && ! $this->is_premium() ) {
5913
- $this->_admin_notices->add_sticky(
5914
- sprintf(
5915
- /* translators: %s: License type (e.g. you have a professional license) */
5916
- $this->get_text_inline( 'You have a %s license.', 'you-have-x-license' ),
5917
- $this->get_plan_title()
5918
- ) . $this->get_complete_upgrade_instructions(),
5919
- 'plan_upgraded',
5920
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
5921
- );
5922
- }
5923
- }
5924
-
5925
- // Schedule code type changes event.
5926
- $this->schedule_install_sync();
5927
-
5928
- /**
5929
- * Unregister the uninstall hook for the other version of the plugin (with different code type) to avoid
5930
- * triggering a fatal error when uninstalling that plugin. For example, after deactivating the "free" version
5931
- * of a specific plugin, its uninstall hook should be unregistered after the "premium" version has been
5932
- * activated. If we don't do that, a fatal error will occur when we try to uninstall the "free" version since
5933
- * the main file of the "free" version will be loaded first before calling the hooked callback. Since the
5934
- * free and premium versions are almost identical (same class or have same functions), a fatal error like
5935
- * "Cannot redeclare class MyClass" or "Cannot redeclare my_function()" will occur.
5936
- */
5937
- $this->unregister_uninstall_hook();
5938
-
5939
- $this->clear_module_main_file_cache();
5940
-
5941
- // Update is_premium of latest version.
5942
- $this->_storage->prev_is_premium = $this->_plugin->is_premium;
5943
- }
5944
-
5945
- #endregion
5946
-
5947
- #----------------------------------------------------------------------------------
5948
- #region Add-ons
5949
- #----------------------------------------------------------------------------------
5950
-
5951
- /**
5952
- * Check if add-on installed and activated on site.
5953
- *
5954
- * @author Vova Feldman (@svovaf)
5955
- * @since 1.0.6
5956
- *
5957
- * @param string|number $id_or_slug
5958
- * @param bool|null $is_premium Since 1.2.1.7 can check for specified add-on version.
5959
- *
5960
- * @return bool
5961
- */
5962
- function is_addon_activated( $id_or_slug, $is_premium = null ) {
5963
- $this->_logger->entrance();
5964
-
5965
- $addon_id = self::get_module_id( $id_or_slug );
5966
- $is_activated = self::has_instance( $addon_id );
5967
-
5968
- if ( ! $is_activated ) {
5969
- return false;
5970
- }
5971
-
5972
- if ( is_bool( $is_premium ) ) {
5973
- // Check if the specified code version is activate.
5974
- $addon = $this->get_addon_instance( $addon_id );
5975
- $is_activated = ( $is_premium === $addon->is_premium() );
5976
- }
5977
-
5978
- return $is_activated;
5979
- }
5980
-
5981
- /**
5982
- * Check if add-on was connected to install
5983
- *
5984
- * @author Vova Feldman (@svovaf)
5985
- * @since 1.1.7
5986
- *
5987
- * @param string|number $id_or_slug
5988
- *
5989
- * @return bool
5990
- */
5991
- function is_addon_connected( $id_or_slug ) {
5992
- $this->_logger->entrance();
5993
-
5994
- $sites = self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN );
5995
-
5996
- $addon_id = self::get_module_id( $id_or_slug );
5997
- $addon = $this->get_addon( $addon_id );
5998
- $slug = $addon->slug;
5999
- if ( ! isset( $sites[ $slug ] ) ) {
6000
- return false;
6001
- }
6002
-
6003
- $site = $sites[ $slug ];
6004
-
6005
- $plugin = FS_Plugin_Manager::instance( $addon_id )->get();
6006
-
6007
- if ( $plugin->parent_plugin_id != $this->_plugin->id ) {
6008
- // The given slug do NOT belong to any of the plugin's add-ons.
6009
- return false;
6010
- }
6011
-
6012
- return ( is_object( $site ) &&
6013
- is_numeric( $site->id ) &&
6014
- is_numeric( $site->user_id ) &&
6015
- FS_Plugin_Plan::is_valid_id( $site->plan_id )
6016
- );
6017
- }
6018
-
6019
- /**
6020
- * Determines if add-on installed.
6021
- *
6022
- * NOTE: This is a heuristic and only works if the folder/file named as the slug.
6023
- *
6024
- * @author Vova Feldman (@svovaf)
6025
- * @since 1.0.6
6026
- *
6027
- * @param string|number $id_or_slug
6028
- *
6029
- * @return bool
6030
- */
6031
- function is_addon_installed( $id_or_slug ) {
6032
- $this->_logger->entrance();
6033
-
6034
- $addon_id = self::get_module_id( $id_or_slug );
6035
-
6036
- return file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $this->get_addon_basename( $addon_id ) ) );
6037
- }
6038
-
6039
- /**
6040
- * Get add-on basename.
6041
- *
6042
- * @author Vova Feldman (@svovaf)
6043
- * @since 1.0.6
6044
- *
6045
- * @param string|number $id_or_slug
6046
- *
6047
- * @return string
6048
- */
6049
- function get_addon_basename( $id_or_slug ) {
6050
- $addon_id = self::get_module_id( $id_or_slug );
6051
-
6052
- if ( $this->is_addon_activated( $addon_id ) ) {
6053
- return self::instance( $addon_id )->get_plugin_basename();
6054
- }
6055
-
6056
- $addon = $this->get_addon( $addon_id );
6057
- $premium_basename = "{$addon->premium_slug}/{$addon->slug}.php";
6058
-
6059
- if ( file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $premium_basename ) ) ) {
6060
- return $premium_basename;
6061
- }
6062
-
6063
- $all_plugins = $this->get_all_plugins();
6064
-
6065
- foreach ( $all_plugins as $basename => $data ) {
6066
- if ( $addon->slug === $data['slug'] ||
6067
- $addon->premium_slug === $data['slug']
6068
- ) {
6069
- return $basename;
6070
- }
6071
- }
6072
-
6073
- $free_basename = "{$addon->slug}/{$addon->slug}.php";
6074
-
6075
- return $free_basename;
6076
- }
6077
-
6078
- /**
6079
- * Get installed add-ons instances.
6080
- *
6081
- * @author Vova Feldman (@svovaf)
6082
- * @since 1.0.6
6083
- *
6084
- * @return Freemius[]
6085
- */
6086
- function get_installed_addons() {
6087
- if ( $this->is_addon() ) {
6088
- // Add-on cannot have add-ons.
6089
- return array();
6090
- }
6091
-
6092
- $installed_addons = array();
6093
-
6094
- foreach ( self::$_instances as $instance ) {
6095
- if ( $instance->is_addon_of( $this->_plugin->id ) ) {
6096
- $installed_addons[] = $instance;
6097
- }
6098
- }
6099
-
6100
- return $installed_addons;
6101
- }
6102
-
6103
- /**
6104
- * Check if any add-ons of the plugin are installed.
6105
- *
6106
- * @author Leo Fajardo (@leorw)
6107
- * @since 1.1.1
6108
- *
6109
- * @return bool
6110
- */
6111
- function has_installed_addons() {
6112
- if ( ! $this->has_addons() ) {
6113
- return false;
6114
- }
6115
-
6116
- foreach ( self::$_instances as $instance ) {
6117
- if ( $instance->is_addon() && is_object( $instance->_parent_plugin ) ) {
6118
- if ( $this->_plugin->id == $instance->_parent_plugin->id ) {
6119
- return true;
6120
- }
6121
- }
6122
- }
6123
-
6124
- return false;
6125
- }
6126
-
6127
- /**
6128
- * Tell Freemius that the current plugin is an add-on.
6129
- *
6130
- * @author Vova Feldman (@svovaf)
6131
- * @since 1.0.6
6132
- *
6133
- * @param number $parent_plugin_id The parent plugin ID
6134
- */
6135
- function init_addon( $parent_plugin_id ) {
6136
- $this->_plugin->parent_plugin_id = $parent_plugin_id;
6137
- }
6138
-
6139
- /**
6140
- * @author Vova Feldman (@svovaf)
6141
- * @since 1.0.6
6142
- *
6143
- * @return bool
6144
- */
6145
- function is_addon() {
6146
- return (
6147
- isset( $this->_plugin->parent_plugin_id ) &&
6148
- is_numeric( $this->_plugin->parent_plugin_id )
6149
- );
6150
- }
6151
-
6152
- /**
6153
- * @author Vova Feldman (@svovaf)
6154
- * @since 2.3.2
6155
- *
6156
- * @param number $parent_product_id
6157
- *
6158
- * @return bool
6159
- */
6160
- function is_addon_of( $parent_product_id ) {
6161
- return (
6162
- $this->is_addon() &&
6163
- $parent_product_id == $this->_plugin->parent_plugin_id
6164
- );
6165
- }
6166
-
6167
- /**
6168
- * Deactivate add-on if it's premium only and the user does't have a valid license.
6169
- *
6170
- * @param bool $is_after_trial_cancel
6171
- *
6172
- * @return bool If add-on was deactivated.
6173
- */
6174
- private function deactivate_premium_only_addon_without_license( $is_after_trial_cancel = false ) {
6175
- if ( ! $this->has_free_plan() &&
6176
- ! $this->has_features_enabled_license() &&
6177
- ! $this->_has_premium_license()
6178
- ) {
6179
- if ( $this->is_registered() ) {
6180
- // IF wrapper is turned off because activation_timestamp is currently only stored for plugins (not addons).
6181
- // if (empty($this->_storage->activation_timestamp) ||
6182
- // (WP_FS__SCRIPT_START_TIME - $this->_storage->activation_timestamp) > 30
6183
- // ) {
6184
- /**
6185
- * @todo When it's first fail, there's no reason to try and re-sync because the licenses were just synced after initial activation.
6186
- *
6187
- * Retry syncing the user add-on licenses.
6188
- */
6189
- // Sync licenses.
6190
- $this->_sync_licenses();
6191
- // }
6192
-
6193
- // Try to activate premium license.
6194
- $this->_activate_license( true );
6195
- }
6196
-
6197
- if ( ! $this->has_free_plan() &&
6198
- ! $this->has_features_enabled_license() &&
6199
- ! $this->_has_premium_license()
6200
- ) {
6201
- // @todo Check if deactivate plugins also call the deactivation hook.
6202
-
6203
- $this->_parent->_admin_notices->add_sticky(
6204
- sprintf(
6205
- ( $is_after_trial_cancel ?
6206
- $this->_parent->get_text_inline(
6207
- '%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.',
6208
- 'addon-trial-cancelled-message'
6209
- ) :
6210
- $this->_parent->get_text_inline(
6211
- '%s is a premium only add-on. You have to purchase a license first before activating the plugin.',
6212
- 'addon-no-license-message'
6213
- )
6214
- ),
6215
- '<b>' . $this->_plugin->title . '</b>'
6216
- ) . ' ' . sprintf(
6217
- '<a href="%s" aria-label="%s" class="button button-primary" style="margin-left: 10px; vertical-align: middle;">%s &nbsp;&#10140;</a>',
6218
- $this->_parent->addon_url( $this->_slug ),
6219
- esc_attr( sprintf( $this->_parent->get_text_inline( 'More information about %s', 'more-information-about-x' ), $this->_plugin->title ) ),
6220
- $this->_parent->get_text_inline( 'Purchase License', 'purchase-license' )
6221
- ),
6222
- 'no_addon_license_' . $this->_slug,
6223
- ( $is_after_trial_cancel ? '' : $this->_parent->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...' ),
6224
- ( $is_after_trial_cancel ? 'success' : 'error' )
6225
- );
6226
-
6227
- deactivate_plugins( array( $this->_plugin_basename ), true );
6228
-
6229
- return true;
6230
- }
6231
- }
6232
-
6233
- return false;
6234
- }
6235
-
6236
- #endregion
6237
-
6238
- #----------------------------------------------------------------------------------
6239
- #region Sandbox
6240
- #----------------------------------------------------------------------------------
6241
-
6242
- /**
6243
- * Set Freemius into sandbox mode for debugging.
6244
- *
6245
- * @author Vova Feldman (@svovaf)
6246
- * @since 1.0.4
6247
- *
6248
- * @param string $secret_key
6249
- */
6250
- function init_sandbox( $secret_key ) {
6251
- $this->_plugin->secret_key = $secret_key;
6252
-
6253
- // Update plugin details.
6254
- FS_Plugin_Manager::instance( $this->_module_id )->update( $this->_plugin, true );
6255
- }
6256
-
6257
- /**
6258
- * Check if running payments in sandbox mode.
6259
- *
6260
- * @author Vova Feldman (@svovaf)
6261
- * @since 1.0.4
6262
- *
6263
- * @return bool
6264
- */
6265
- function is_payments_sandbox() {
6266
- return ( ! $this->is_live() ) || isset( $this->_plugin->secret_key );
6267
- }
6268
-
6269
- #endregion
6270
-
6271
- /**
6272
- * Check if running test vs. live plugin.
6273
- *
6274
- * @author Vova Feldman (@svovaf)
6275
- * @since 1.0.5
6276
- *
6277
- * @return bool
6278
- */
6279
- function is_live() {
6280
- return $this->_plugin->is_live;
6281
- }
6282
-
6283
- /**
6284
- * Check if super-admin skipped connection for all sites in the network.
6285
- *
6286
- * @author Vova Feldman (@svovaf)
6287
- * @since 2.0.0
6288
- */
6289
- function is_network_anonymous() {
6290
- if ( ! $this->_is_network_active ) {
6291
- return false;
6292
- }
6293
-
6294
- $is_anonymous_ms = $this->_storage->get( 'is_anonymous_ms' );
6295
-
6296
- if ( empty( $is_anonymous_ms ) ) {
6297
- return false;
6298
- }
6299
-
6300
- return $is_anonymous_ms['is'];
6301
- }
6302
-
6303
- /**
6304
- * Check if super-admin opted-in for all sites in the network.
6305
- *
6306
- * @author Vova Feldman (@svovaf)
6307
- * @since 2.0.0
6308
- */
6309
- function is_network_connected() {
6310
- if ( ! $this->_is_network_active ) {
6311
- return false;
6312
- }
6313
-
6314
- return $this->_storage->get( 'is_network_connected' );
6315
- }
6316
-
6317
- /**
6318
- * Check if the user skipped connecting the account with Freemius.
6319
- *
6320
- * @author Vova Feldman (@svovaf)
6321
- * @since 1.0.7
6322
- *
6323
- * @return bool
6324
- */
6325
- function is_anonymous() {
6326
- if ( ! isset( $this->_is_anonymous ) ) {
6327
- if ( $this->is_network_anonymous() ) {
6328
- $this->_is_anonymous = true;
6329
- } else if ( ! fs_is_network_admin() ) {
6330
- if ( ! isset( $this->_storage->is_anonymous ) ) {
6331
- // Not skipped.
6332
- $this->_is_anonymous = false;
6333
- } else if ( is_bool( $this->_storage->is_anonymous ) ) {
6334
- // For back compatibility, since the variable was boolean before.
6335
- $this->_is_anonymous = $this->_storage->is_anonymous;
6336
-
6337
- // Upgrade stored data format to 1.1.3 format.
6338
- $this->set_anonymous_mode( $this->_storage->is_anonymous );
6339
- } else {
6340
- // Version 1.1.3 and later.
6341
- $this->_is_anonymous = $this->_storage->is_anonymous['is'];
6342
- }
6343
- }
6344
- }
6345
-
6346
- return $this->_is_anonymous;
6347
- }
6348
-
6349
- /**
6350
- * Check if the user skipped the connection of a specified site.
6351
- *
6352
- * @author Vova Feldman (@svovaf)
6353
- * @since 2.0.0
6354
- *
6355
- * @param int $blog_id
6356
- *
6357
- * @return bool
6358
- */
6359
- function is_anonymous_site( $blog_id = 0 ) {
6360
- if ( $this->is_network_anonymous() ) {
6361
- return true;
6362
- }
6363
-
6364
- $is_anonymous = $this->_storage->get( 'is_anonymous', false, $blog_id );
6365
-
6366
- if ( empty( $is_anonymous ) ) {
6367
- return false;
6368
- }
6369
-
6370
- return $is_anonymous['is'];
6371
- }
6372
-
6373
- /**
6374
- * Check if user connected his account and install pending email activation.
6375
- *
6376
- * @author Vova Feldman (@svovaf)
6377
- * @since 1.0.7
6378
- *
6379
- * @return bool
6380
- */
6381
- function is_pending_activation() {
6382
- return $this->_storage->get( 'is_pending_activation', false );
6383
- }
6384
-
6385
- /**
6386
- * Check if plugin must be WordPress.org compliant.
6387
- *
6388
- * @since 1.0.7
6389
- *
6390
- * @return bool
6391
- */
6392
- function is_org_repo_compliant() {
6393
- return $this->_is_org_compliant;
6394
- }
6395
-
6396
- #--------------------------------------------------------------------------------
6397
- #region WP Cron Common
6398
- #--------------------------------------------------------------------------------
6399
-
6400
- /**
6401
- * @author Vova Feldman (@svovaf)
6402
- * @since 2.0.0
6403
- *
6404
- * @param string $name Cron name.
6405
- *
6406
- * @return object
6407
- */
6408
- private function get_cron_data( $name ) {
6409
- $this->_logger->entrance( $name );
6410
-
6411
- /**
6412
- * @var object $cron_data
6413
- */
6414
- return $this->_storage->get( "{$name}_cron", null );
6415
- }
6416
-
6417
- /**
6418
- * @author Vova Feldman (@svovaf)
6419
- * @since 2.0.0
6420
- *
6421
- * @param string $name Cron name.
6422
- */
6423
- private function clear_cron_data( $name ) {
6424
- $this->_logger->entrance( $name );
6425
-
6426
- $this->_storage->remove( "{$name}_cron" );
6427
- }
6428
-
6429
- /**
6430
- * @author Vova Feldman (@svovaf)
6431
- * @since 2.0.0
6432
- *
6433
- * @param string $name Cron name.
6434
- * @param int $cron_blog_id The cron executing blog ID.
6435
- */
6436
- private function set_cron_data( $name, $cron_blog_id = 0 ) {
6437
- $this->_logger->entrance( $name );
6438
-
6439
- $this->_storage->store( "{$name}_cron", (object) array(
6440
- 'version' => $this->get_plugin_version(),
6441
- 'blog_id' => $cron_blog_id,
6442
- 'sdk_version' => $this->version,
6443
- 'timestamp' => WP_FS__SCRIPT_START_TIME,
6444
- 'on' => true,
6445
- ) );
6446
- }
6447
-
6448
- /**
6449
- * Get the cron's executing blog ID.
6450
- *
6451
- * @author Vova Feldman (@svovaf)
6452
- * @since 2.0.0
6453
- *
6454
- * @param string $name Cron name.
6455
- *
6456
- * @return int
6457
- */
6458
- private function get_cron_blog_id( $name ) {
6459
- $this->_logger->entrance( $name );
6460
-
6461
- /**
6462
- * @var object $cron_data
6463
- */
6464
- $cron_data = $this->get_cron_data( $name );
6465
-
6466
- return ( is_object( $cron_data ) && is_numeric( $cron_data->blog_id ) ) ?
6467
- $cron_data->blog_id :
6468
- 0;
6469
- }
6470
-
6471
- /**
6472
- * @author Vova Feldman (@svovaf)
6473
- * @since 2.0.0
6474
- *
6475
- * @param string $name Cron name.
6476
- *
6477
- * @return bool
6478
- */
6479
- private function is_cron_on( $name ) {
6480
- $this->_logger->entrance( $name );
6481
-
6482
- /**
6483
- * @var object $cron_data
6484
- */
6485
- $cron_data = $this->get_cron_data( $name );
6486
-
6487
- return ( ! is_null( $cron_data ) && true === $cron_data->on );
6488
- }
6489
-
6490
- /**
6491
- * Unix timestamp for previous cron execution or false if never executed.
6492
- *
6493
- * @author Vova Feldman (@svovaf)
6494
- * @since 2.0.0
6495
- *
6496
- * @param string $name Cron name.
6497
- *
6498
- * @return int|false
6499
- */
6500
- private function cron_last_execution( $name ) {
6501
- $this->_logger->entrance( $name );
6502
-
6503
- return $this->_storage->get( "{$name}_timestamp" );
6504
- }
6505
-
6506
- /**
6507
- * Set cron execution time to now.
6508
- *
6509
- * @author Vova Feldman (@svovaf)
6510
- * @since 2.0.0
6511
- *
6512
- * @param string $name Cron name.
6513
- */
6514
- private function set_cron_execution_timestamp( $name ) {
6515
- $this->_logger->entrance( $name );
6516
-
6517
- $this->_storage->store( "{$name}_timestamp", time() );
6518
- }
6519
-
6520
- /**
6521
- * Sets the keepalive time to now.
6522
- *
6523
- * @author Leo Fajardo (@leorw)
6524
- * @since 2.2.3
6525
- *
6526
- * @param bool|null $use_network_level_storage
6527
- */
6528
- private function set_keepalive_timestamp( $use_network_level_storage = null ) {
6529
- $this->_logger->entrance();
6530
-
6531
- $this->_storage->store( 'keepalive_timestamp', time(), $use_network_level_storage );
6532
- }
6533
-
6534
- /**
6535
- * Check if cron was executed in the last $period of seconds.
6536
- *
6537
- * @author Vova Feldman (@svovaf)
6538
- * @since 2.0.0
6539
- *
6540
- * @param string $name Cron name.
6541
- * @param int $period In seconds
6542
- *
6543
- * @return bool
6544
- */
6545
- private function is_cron_executed( $name, $period = WP_FS__TIME_24_HOURS_IN_SEC ) {
6546
- $this->_logger->entrance( $name );
6547
-
6548
- $last_execution = $this->cron_last_execution( $name );
6549
-
6550
- if ( ! is_numeric( $last_execution ) ) {
6551
- return false;
6552
- }
6553
-
6554
- return ( $last_execution > ( WP_FS__SCRIPT_START_TIME - $period ) );
6555
- }
6556
-
6557
- /**
6558
- * WP Cron is executed on a site level. When running in a multisite network environment
6559
- * with the network integration activated, for optimization reasons, we are consolidating
6560
- * the installs data sync cron to be executed only from a single site.
6561
- *
6562
- * @author Vova Feldman (@svovaf)
6563
- * @since 2.0.0
6564
- *
6565
- * @param int $except_blog_id Target any except the excluded blog ID.
6566
- *
6567
- * @return int
6568
- */
6569
- private function get_cron_target_blog_id( $except_blog_id = 0 ) {
6570
- if ( ! is_multisite() ) {
6571
- return 0;
6572
- }
6573
-
6574
- if ( $this->_is_network_active &&
6575
- is_numeric( $this->_storage->network_install_blog_id ) &&
6576
- $except_blog_id != $this->_storage->network_install_blog_id &&
6577
- self::is_site_active( $this->_storage->network_install_blog_id )
6578
- ) {
6579
- // Try to run cron from the main network blog.
6580
- $install = $this->get_install_by_blog_id( $this->_storage->network_install_blog_id );
6581
-
6582
- if ( is_object( $install ) &&
6583
- ( $this->is_premium() || $install->is_tracking_allowed() )
6584
- ) {
6585
- return $this->_storage->network_install_blog_id;
6586
- }
6587
- }
6588
-
6589
- // Get first opted-in blog ID with active tracking.
6590
- $installs = $this->get_blog_install_map();
6591
- foreach ( $installs as $blog_id => $install ) {
6592
- if ( $except_blog_id != $blog_id &&
6593
- self::is_site_active( $blog_id ) &&
6594
- ( $this->is_premium() || $install->is_tracking_allowed() )
6595
- ) {
6596
- return $blog_id;
6597
- }
6598
- }
6599
-
6600
- return 0;
6601
- }
6602
-
6603
- /**
6604
- * @author Vova Feldman (@svovaf)
6605
- * @since 2.0.0
6606
- *
6607
- * @param string $name Cron name.
6608
- * @param string $action_tag Callback action tag.
6609
- * @param bool $is_network_clear If set to TRUE, clear sync cron even if there are installs that are still connected.
6610
- */
6611
- private function clear_cron( $name, $action_tag = '', $is_network_clear = false ) {
6612
- $this->_logger->entrance( $name );
6613
-
6614
- if ( ! $this->is_cron_on( $name ) ) {
6615
- return;
6616
- }
6617
-
6618
- $clear_cron = true;
6619
- if ( ! $is_network_clear && $this->_is_network_active ) {
6620
- $installs = $this->get_blog_install_map();
6621
-
6622
- foreach ( $installs as $blog_id => $install ) {
6623
- /**
6624
- * @var FS_Site $install
6625
- */
6626
- if ( $install->is_tracking_allowed() ) {
6627
- $clear_cron = false;
6628
- break;
6629
- }
6630
- }
6631
- }
6632
-
6633
- if ( ! $clear_cron ) {
6634
- return;
6635
- }
6636
-
6637
- /**
6638
- * @var object $cron_data
6639
- */
6640
- $cron_data = $this->get_cron_data( $name );
6641
-
6642
- $cron_blog_id = is_object( $cron_data ) && isset( $cron_data->blog_id ) ?
6643
- $cron_data->blog_id :
6644
- 0;
6645
-
6646
- $this->clear_cron_data( $name );
6647
-
6648
- if ( 0 < $cron_blog_id ) {
6649
- switch_to_blog( $cron_blog_id );
6650
- }
6651
-
6652
- if ( empty( $action_tag ) ) {
6653
- $action_tag = $name;
6654
- }
6655
-
6656
- wp_clear_scheduled_hook( $this->get_action_tag( $action_tag ) );
6657
-
6658
- if ( 0 < $cron_blog_id ) {
6659
- restore_current_blog();
6660
- }
6661
- }
6662
-
6663
- /**
6664
- * Unix timestamp for next cron execution or false if not scheduled.
6665
- *
6666
- * @author Vova Feldman (@svovaf)
6667
- * @since 2.0.0
6668
- *
6669
- * @param string $name Cron name.
6670
- * @param string $action_tag Callback action tag.
6671
- *
6672
- * @return int|false
6673
- */
6674
- private function get_next_scheduled_cron( $name, $action_tag = '' ) {
6675
- $this->_logger->entrance( $name );
6676
-
6677
- if ( ! $this->is_cron_on( $name ) ) {
6678
- return false;
6679
- }
6680
-
6681
- /**
6682
- * @var object $cron_data
6683
- */
6684
- $cron_data = $this->get_cron_data( $name );
6685
-
6686
- $cron_blog_id = is_object( $cron_data ) && isset( $cron_data->blog_id ) ?
6687
- $cron_data->blog_id :
6688
- 0;
6689
-
6690
- if ( 0 < $cron_blog_id ) {
6691
- switch_to_blog( $cron_blog_id );
6692
- }
6693
-
6694
- if ( empty( $action_tag ) ) {
6695
- $action_tag = $name;
6696
- }
6697
-
6698
- $next_scheduled = wp_next_scheduled( $this->get_action_tag( $action_tag ) );
6699
-
6700
- if ( 0 < $cron_blog_id ) {
6701
- restore_current_blog();
6702
- }
6703
-
6704
- return $next_scheduled;
6705
- }
6706
-
6707
- /**
6708
- * @author Vova Feldman (@svovaf)
6709
- * @since 2.0.0
6710
- *
6711
- * @param string $name Cron name.
6712
- * @param string $action_tag Callback action tag.
6713
- * @param string $recurrence 'single' or 'daily'.
6714
- * @param int $start_at Defaults to now.
6715
- * @param bool $randomize_start If true, schedule first job randomly during the next 12 hours. Otherwise, schedule job to start right away.
6716
- * @param int $except_blog_id Target any except the excluded blog ID.
6717
- */
6718
- private function schedule_cron(
6719
- $name,
6720
- $action_tag = '',
6721
- $recurrence = 'single',
6722
- $start_at = WP_FS__SCRIPT_START_TIME,
6723
- $randomize_start = true,
6724
- $except_blog_id = 0
6725
- ) {
6726
- $this->_logger->entrance( $name );
6727
-
6728
- $this->clear_cron( $name, $action_tag, true );
6729
-
6730
- $cron_blog_id = $this->get_cron_target_blog_id( $except_blog_id );
6731
-
6732
- if ( is_multisite() && 0 == $cron_blog_id ) {
6733
- // Don't schedule cron since couldn't find a target blog.
6734
- return;
6735
- }
6736
-
6737
- if ( 0 < $cron_blog_id ) {
6738
- switch_to_blog( $cron_blog_id );
6739
- }
6740
-
6741
- if ( 'daily' === $recurrence ) {
6742
- if ( $randomize_start ) {
6743
- // Schedule first sync with a random 12 hour time range from now.
6744
- $start_at += rand( 0, ( WP_FS__TIME_24_HOURS_IN_SEC / 2 ) );
6745
- }
6746
-
6747
- // Schedule daily WP cron.
6748
- wp_schedule_event(
6749
- $start_at,
6750
- 'daily',
6751
- $this->get_action_tag( $action_tag )
6752
- );
6753
- } else if ( 'single' === $recurrence ) {
6754
- // Schedule single cron.
6755
- wp_schedule_single_event(
6756
- $start_at,
6757
- $this->get_action_tag( $action_tag )
6758
- );
6759
- }
6760
-
6761
- $this->set_cron_data( $name, $cron_blog_id );
6762
-
6763
- if ( 0 < $cron_blog_id ) {
6764
- restore_current_blog();
6765
- }
6766
- }
6767
-
6768
- /**
6769
- * Consolidated cron execution for performance optimization. The max number of API requests is based on the number of unique opted-in users.
6770
- * that doesn't halt page loading.
6771
- *
6772
- * @author Vova Feldman (@svovaf)
6773
- * @since 2.0.0
6774
- *
6775
- * @param string $name Cron name.
6776
- * @param callable $callable The function that should be executed.
6777
- */
6778
- private function execute_cron( $name, $callable ) {
6779
- $this->_logger->entrance( $name );
6780
-
6781
- // Store the last time data sync was executed.
6782
- $this->set_cron_execution_timestamp( $name );
6783
-
6784
- // Check if API is temporary down.
6785
- if ( FS_Api::is_temporary_down() ) {
6786
- return;
6787
- }
6788
-
6789
- // @todo Add logic that identifies API latency, and reschedule the next background sync randomly between 8-16 hours.
6790
-
6791
- $users_2_blog_ids = array();
6792
-
6793
- if ( ! is_multisite() ) {
6794
- // Add dummy blog.
6795
- $users_2_blog_ids[0] = array( 0 );
6796
- } else {
6797
- $installs = $this->get_blog_install_map();
6798
- foreach ( $installs as $blog_id => $install ) {
6799
- if ( $this->is_premium() || $install->is_tracking_allowed() ) {
6800
- if ( ! isset( $users_2_blog_ids[ $install->user_id ] ) ) {
6801
- $users_2_blog_ids[ $install->user_id ] = array();
6802
- }
6803
-
6804
- $users_2_blog_ids[ $install->user_id ][] = $blog_id;
6805
- }
6806
- }
6807
- }
6808
-
6809
- $current_blog_id = get_current_blog_id();
6810
-
6811
- foreach ( $users_2_blog_ids as $user_id => $blog_ids ) {
6812
- if ( 0 < $blog_ids[0] ) {
6813
- $this->switch_to_blog( $blog_ids[0] );
6814
- }
6815
-
6816
- call_user_func_array( $callable, array( $blog_ids, ( is_multisite() ? $current_blog_id : null ) ) );
6817
-
6818
- foreach ( $blog_ids as $blog_id ) {
6819
- $this->do_action( "after_{$name}_cron", $blog_id );
6820
- }
6821
- }
6822
-
6823
- if ( is_multisite() ) {
6824
- $this->switch_to_blog( $current_blog_id, fs_is_network_admin() ? $this->get_network_install() : null );
6825
-
6826
- $this->do_action( "after_{$name}_cron_multisite" );
6827
- }
6828
- }
6829
-
6830
- #endregion
6831
-
6832
- #----------------------------------------------------------------------------------
6833
- #region Daily Sync Cron
6834
- #----------------------------------------------------------------------------------
6835
-
6836
-
6837
- /**
6838
- * @author Vova Feldman (@svovaf)
6839
- * @since 2.0.0
6840
- *
6841
- * @return bool
6842
- */
6843
- private function is_sync_cron_scheduled() {
6844
- return $this->is_cron_on( 'sync' );
6845
- }
6846
-
6847
- /**
6848
- * Get the sync cron's executing blog ID.
6849
- *
6850
- * @author Vova Feldman (@svovaf)
6851
- * @since 2.0.0
6852
- *
6853
- * @return int
6854
- */
6855
- private function get_sync_cron_blog_id() {
6856
- return $this->get_cron_blog_id( 'sync' );
6857
- }
6858
-
6859
- /**
6860
- * @author Vova Feldman (@svovaf)
6861
- * @since 1.1.7.3
6862
- */
6863
- private function run_manual_sync() {
6864
- self::require_pluggable_essentials();
6865
-
6866
- if ( ! $this->is_user_admin() ) {
6867
- return;
6868
- }
6869
-
6870
- // Run manual sync.
6871
- $this->_sync_cron();
6872
-
6873
- // Reschedule next cron to run 24 hours from now (performance optimization).
6874
- $this->schedule_sync_cron( time() + WP_FS__TIME_24_HOURS_IN_SEC, false );
6875
- }
6876
-
6877
- /**
6878
- * Data sync cron job. Replaces the background sync non blocking HTTP request
6879
- * that doesn't halt page loading.
6880
- *
6881
- * @author Vova Feldman (@svovaf)
6882
- * @since 1.1.7.3
6883
- * @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.
6884
- */
6885
- function _sync_cron() {
6886
- $this->_logger->entrance();
6887
-
6888
- $this->execute_cron( 'sync', array( $this, '_sync_cron_method' ) );
6889
- }
6890
-
6891
- /**
6892
- * The actual data sync cron logic.
6893
- *
6894
- * @author Vova Feldman (@svovaf)
6895
- * @since 2.0.0
6896
- *
6897
- * @param int[] $blog_ids
6898
- * @param int|null $current_blog_id @since 2.2.3. This is passed from the `execute_cron` method and used by the
6899
- * `_sync_plugin_license` method in order to switch to the previous blog when sending
6900
- * updates for a single site in case `execute_cron` has switched to a different blog.
6901
- */
6902
- function _sync_cron_method( array $blog_ids, $current_blog_id = null ) {
6903
- if ( $this->is_registered() ) {
6904
- if ( $this->has_paid_plan() ) {
6905
- // Initiate background plan sync.
6906
- $this->_sync_license( true, false, $current_blog_id );
6907
-
6908
- if ( $this->is_paying() ) {
6909
- // Check for premium plugin updates.
6910
- $this->check_updates( true );
6911
- }
6912
- } else {
6913
- // Sync install(s) (only if something changed locally).
6914
- if ( 1 < count( $blog_ids ) ) {
6915
- $this->sync_installs();
6916
- } else {
6917
- $this->sync_install();
6918
- }
6919
-
6920
- $this->maybe_sync_install_user();
6921
- }
6922
- }
6923
- }
6924
-
6925
- /**
6926
- * Check if sync was executed in the last $period of seconds.
6927
- *
6928
- * @author Vova Feldman (@svovaf)
6929
- * @since 1.1.7.3
6930
- *
6931
- * @param int $period In seconds
6932
- *
6933
- * @return bool
6934
- */
6935
- private function is_sync_executed( $period = WP_FS__TIME_24_HOURS_IN_SEC ) {
6936
- return $this->is_cron_executed( 'sync', $period );
6937
- }
6938
-
6939
- /**
6940
- * @author Vova Feldman (@svovaf)
6941
- * @since 1.1.7.3
6942
- *
6943
- * @return bool
6944
- */
6945
- private function is_sync_cron_on() {
6946
- return $this->is_cron_on( 'sync' );
6947
- }
6948
-
6949
- /**
6950
- * @author Vova Feldman (@svovaf)
6951
- * @since 1.1.7.3
6952
- *
6953
- * @param int $start_at Defaults to now.
6954
- * @param bool $randomize_start If true, schedule first job randomly during the next 12 hours. Otherwise, schedule job to start right away.
6955
- * @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.
6956
- */
6957
- private function schedule_sync_cron(
6958
- $start_at = WP_FS__SCRIPT_START_TIME,
6959
- $randomize_start = true,
6960
- $except_blog_id = 0
6961
- ) {
6962
- $this->schedule_cron(
6963
- 'sync',
6964
- 'data_sync',
6965
- 'daily',
6966
- $start_at,
6967
- $randomize_start,
6968
- $except_blog_id
6969
- );
6970
- }
6971
-
6972
- /**
6973
- * Add the actual sync function to the cron job hook.
6974
- *
6975
- * @author Vova Feldman (@svovaf)
6976
- * @since 1.1.7.3
6977
- */
6978
- private function hook_callback_to_sync_cron() {
6979
- $this->add_action( 'data_sync', array( $this, '_sync_cron' ) );
6980
- }
6981
-
6982
- /**
6983
- * @author Vova Feldman (@svovaf)
6984
- * @since 1.1.7.3
6985
- *
6986
- * @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.
6987
- */
6988
- private function clear_sync_cron( $is_network_clear = false ) {
6989
- $this->_logger->entrance();
6990
-
6991
- $this->clear_cron( 'sync', 'data_sync', $is_network_clear );
6992
- }
6993
-
6994
- /**
6995
- * Unix timestamp for next sync cron execution or false if not scheduled.
6996
- *
6997
- * @author Vova Feldman (@svovaf)
6998
- * @since 1.1.7.3
6999
- *
7000
- * @return int|false
7001
- */
7002
- function next_sync_cron() {
7003
- return $this->get_next_scheduled_cron( 'sync', 'data_sync' );
7004
- }
7005
-
7006
- /**
7007
- * Unix timestamp for previous sync cron execution or false if never executed.
7008
- *
7009
- * @author Vova Feldman (@svovaf)
7010
- * @since 1.1.7.3
7011
- *
7012
- * @return int|false
7013
- */
7014
- function last_sync_cron() {
7015
- return $this->cron_last_execution( 'sync' );
7016
- }
7017
-
7018
- #endregion Daily Sync Cron ------------------------------------------------------------------
7019
-
7020
- #----------------------------------------------------------------------------------
7021
- #region Async Install Sync
7022
- #----------------------------------------------------------------------------------
7023
-
7024
- /**
7025
- * @author Vova Feldman (@svovaf)
7026
- * @since 1.1.7.3
7027
- *
7028
- * @return bool
7029
- */
7030
- private function is_install_sync_scheduled() {
7031
- return $this->is_cron_on( 'install_sync' );
7032
- }
7033
-
7034
- /**
7035
- * Get the sync cron's executing blog ID.
7036
- *
7037
- * @author Vova Feldman (@svovaf)
7038
- * @since 2.0.0
7039
- *
7040
- * @return int
7041
- */
7042
- private function get_install_sync_cron_blog_id() {
7043
- return $this->get_cron_blog_id( 'install_sync' );
7044
- }
7045
-
7046
- /**
7047
- * Instead of running blocking install sync event, execute non blocking scheduled wp-cron.
7048
- *
7049
- * @author Vova Feldman (@svovaf)
7050
- * @since 1.1.7.3
7051
- *
7052
- * @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.
7053
- */
7054
- private function schedule_install_sync( $except_blog_id = 0 ) {
7055
- $this->schedule_cron( 'install_sync', 'install_sync', 'single', WP_FS__SCRIPT_START_TIME, false, $except_blog_id );
7056
- }
7057
-
7058
- /**
7059
- * Unix timestamp for previous install sync cron execution or false if never executed.
7060
- *
7061
- * @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.
7062
- *
7063
- * @author Vova Feldman (@svovaf)
7064
- * @since 1.1.7.3
7065
- *
7066
- * @return int|false
7067
- */
7068
- function last_install_sync() {
7069
- return $this->cron_last_execution( 'install_sync' );
7070
- }
7071
-
7072
- /**
7073
- * Unix timestamp for next install sync cron execution or false if not scheduled.
7074
- *
7075
- * @author Vova Feldman (@svovaf)
7076
- * @since 1.1.7.3
7077
- *
7078
- * @return int|false
7079
- */
7080
- function next_install_sync() {
7081
- return $this->get_next_scheduled_cron( 'install_sync', 'install_sync' );
7082
- }
7083
-
7084
- /**
7085
- * Add the actual install sync function to the cron job hook.
7086
- *
7087
- * @author Vova Feldman (@svovaf)
7088
- * @since 1.1.7.3
7089
- */
7090
- private function hook_callback_to_install_sync() {
7091
- $this->add_action( 'install_sync', array( $this, '_run_sync_install' ) );
7092
- }
7093
-
7094
- /**
7095
- * @author Vova Feldman (@svovaf)
7096
- * @since 1.1.7.3
7097
- *
7098
- * @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.
7099
- */
7100
- private function clear_install_sync_cron( $is_network_clear = false ) {
7101
- $this->_logger->entrance();
7102
-
7103
- $this->clear_cron( 'install_sync', 'install_sync', $is_network_clear );
7104
- }
7105
-
7106
- /**
7107
- * @author Vova Feldman (@svovaf)
7108
- * @since 1.1.7.3
7109
- * @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.
7110
- */
7111
- public function _run_sync_install() {
7112
- $this->_logger->entrance();
7113
-
7114
- $this->execute_cron( 'sync', array( $this, '_sync_install_cron_method' ) );
7115
- }
7116
-
7117
- /**
7118
- * The actual install(s) sync cron logic.
7119
- *
7120
- * @author Vova Feldman (@svovaf)
7121
- * @since 2.0.0
7122
- *
7123
- * @param int[] $blog_ids
7124
- * @param int|null $current_blog_id
7125
- */
7126
- function _sync_install_cron_method( array $blog_ids, $current_blog_id = null ) {
7127
- if ( $this->is_registered() ) {
7128
- if ( 1 < count( $blog_ids ) ) {
7129
- $this->sync_installs( array(), true );
7130
- } else {
7131
- $this->sync_install( array(), true );
7132
- }
7133
-
7134
- $this->maybe_sync_install_user();
7135
- }
7136
- }
7137
-
7138
- #endregion Async Install Sync ------------------------------------------------------------------
7139
-
7140
- /**
7141
- * Show a notice that activation is currently pending.
7142
- *
7143
- * @author Vova Feldman (@svovaf)
7144
- * @since 1.0.7
7145
- *
7146
- * @param bool|string $email
7147
- * @param bool $is_pending_trial Since 1.2.1.5
7148
- */
7149
- function _add_pending_activation_notice( $email = false, $is_pending_trial = false ) {
7150
- if ( ! is_string( $email ) ) {
7151
- $current_user = self::_get_current_wp_user();
7152
- $email = $current_user->user_email;
7153
- }
7154
-
7155
- $this->_admin_notices->add_sticky(
7156
- sprintf(
7157
- $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' ),
7158
- '<b>' . $this->get_plugin_name() . '</b>',
7159
- '<b>' . $email . '</b>',
7160
- ( $is_pending_trial ?
7161
- $this->get_text_inline( 'start the trial', 'start-the-trial' ) :
7162
- $this->get_text_inline( 'complete the install', 'complete-the-install' ) )
7163
- ),
7164
- 'activation_pending',
7165
- 'Thanks!'
7166
- );
7167
- }
7168
-
7169
- /**
7170
- * Check if currently in plugin activation.
7171
- *
7172
- * @author Vova Feldman (@svovaf)
7173
- * @since 1.1.4
7174
- *
7175
- * @return bool
7176
- */
7177
- function is_plugin_activation() {
7178
- $result = get_transient( "fs_{$this->_module_type}_{$this->_slug}_activated" );
7179
-
7180
- return !empty($result);
7181
- }
7182
-
7183
- /**
7184
- *
7185
- * NOTE: admin_menu action executed before admin_init.
7186
- *
7187
- * @author Vova Feldman (@svovaf)
7188
- * @since 1.0.7
7189
- */
7190
- function _admin_init_action() {
7191
- $is_migration = $this->is_migration();
7192
-
7193
- /**
7194
- * Automatically redirect to connect/activation page after plugin activation.
7195
- *
7196
- * @since 1.1.7 Do NOT redirect to opt-in when running in network admin mode.
7197
- */
7198
- if ( $this->is_plugin_activation() ) {
7199
- delete_transient( "fs_{$this->_module_type}_{$this->_slug}_activated" );
7200
-
7201
- if ( isset( $_GET['activate-multi'] ) ) {
7202
- /**
7203
- * Don't redirect if activating multiple plugins at once (bulk activation).
7204
- */
7205
- } else if ( ! $is_migration ) {
7206
- $this->_redirect_on_activation_hook();
7207
- return;
7208
- }
7209
- }
7210
-
7211
- if ( $is_migration ) {
7212
- return;
7213
- }
7214
-
7215
- if ( fs_request_is_action( $this->get_unique_affix() . '_skip_activation' ) ) {
7216
- check_admin_referer( $this->get_unique_affix() . '_skip_activation' );
7217
-
7218
- $this->skip_connection( null, fs_is_network_admin() );
7219
-
7220
- fs_redirect( $this->get_after_activation_url( 'after_skip_url' ) );
7221
- }
7222
-
7223
- if ( $this->is_network_activation_mode() &&
7224
- fs_request_is_action( $this->get_unique_affix() . '_delegate_activation' )
7225
- ) {
7226
- check_admin_referer( $this->get_unique_affix() . '_delegate_activation' );
7227
-
7228
- $this->delegate_connection();
7229
-
7230
- fs_redirect( $this->get_after_activation_url( 'after_delegation_url' ) );
7231
- }
7232
-
7233
- $this->_add_upgrade_action_link();
7234
-
7235
- if ( ! ( ! $this->_is_network_active && fs_is_network_admin() ) &&
7236
- (
7237
- ( true === $this->_storage->require_license_activation ) ||
7238
- // Not registered nor anonymous.
7239
- ( ! $this->is_registered() && ! $this->is_anonymous() ) ||
7240
- // OR, network level and in network upgrade mode.
7241
- ( fs_is_network_admin() && $this->_is_network_active && $this->is_network_upgrade_mode() )
7242
- )
7243
- ) {
7244
- if ( ! $this->is_pending_activation() ) {
7245
- if ( ! $this->is_activation_page() ) {
7246
- /**
7247
- * If a user visits any other admin page before activating the premium-only theme with a valid
7248
- * license, reactivate the previous theme.
7249
- *
7250
- * @author Leo Fajardo (@leorw)
7251
- * @since 1.2.2
7252
- */
7253
- if ( $this->is_theme() &&
7254
- ! $this->has_settings_menu() &&
7255
- ! isset( $_REQUEST['fs_action'] ) &&
7256
- $this->can_activate_previous_theme()
7257
- ) {
7258
- if ( $this->is_only_premium() ) {
7259
- $this->activate_previous_theme();
7260
- return;
7261
- }
7262
-
7263
- if ( true === $this->_storage->require_license_activation ) {
7264
- $this->_storage->require_license_activation = false;
7265
- }
7266
- }
7267
-
7268
- if ( ! fs_is_network_admin() &&
7269
- $this->is_network_activation_mode() &&
7270
- ! $this->is_delegated_connection()
7271
- ) {
7272
- return;
7273
- }
7274
-
7275
- if ( $this->is_plugin_new_install() || $this->is_only_premium() ) {
7276
- if ( ! $this->_anonymous_mode &&
7277
- ( ! $this->is_addon() || ! $this->_parent->is_anonymous() ) ) {
7278
- // Show notice for new plugin installations.
7279
- $this->_admin_notices->add(
7280
- sprintf(
7281
- $this->get_text_inline( 'You are just one step away - %s', 'you-are-step-away' ),
7282
- sprintf( '<b><a href="%s">%s</a></b>',
7283
- $this->get_activation_url( array(), ! $this->is_delegated_connection() ),
7284
- sprintf( $this->get_text_x_inline( 'Complete "%s" Activation Now',
7285
- '%s - plugin name. As complete "PluginX" activation now', 'activate-x-now' ), $this->get_plugin_name() )
7286
- )
7287
- ),
7288
- '',
7289
- 'update-nag'
7290
- );
7291
- }
7292
- } else {
7293
- if ( $this->should_add_sticky_optin_notice() ) {
7294
- $this->add_sticky_optin_admin_notice();
7295
- }
7296
-
7297
- if ( $this->has_filter( 'optin_pointer_element' ) ) {
7298
- // Don't show admin nag if plugin update.
7299
- wp_enqueue_script( 'wp-pointer' );
7300
- wp_enqueue_style( 'wp-pointer' );
7301
-
7302
- $this->_enqueue_connect_essentials();
7303
-
7304
- add_action( 'admin_print_footer_scripts', array(
7305
- $this,
7306
- '_add_connect_pointer_script'
7307
- ) );
7308
- }
7309
- }
7310
- }
7311
- }
7312
-
7313
- if ( $this->show_opt_in_on_themes_page() &&
7314
- $this->is_activation_page()
7315
- ) {
7316
- $this->_show_theme_activation_optin_dialog();
7317
- }
7318
- }
7319
- }
7320
-
7321
- /**
7322
- * @author Vova Feldman (@svovaf)
7323
- * @since 2.0.0
7324
- *
7325
- * @return bool
7326
- */
7327
- private function should_add_sticky_optin_notice() {
7328
- if ( $this->is_addon() && $this->_parent->is_anonymous() ) {
7329
- return false;
7330
- }
7331
-
7332
- if ( fs_is_network_admin() ) {
7333
- if ( ! $this->_is_network_active ) {
7334
- return false;
7335
- }
7336
-
7337
- if ( ! $this->is_network_activation_mode() ) {
7338
- return false;
7339
- }
7340
-
7341
- return ! isset( $this->_storage->sticky_optin_added_ms );
7342
- }
7343
-
7344
- if ( ! $this->is_activation_mode() ) {
7345
- return false;
7346
- }
7347
-
7348
- // If running from a blog admin and delegated the connection.
7349
- return ! isset( $this->_storage->sticky_optin_added );
7350
- }
7351
-
7352
- /**
7353
- * @author Leo Fajardo (@leorw)
7354
- * @since 2.0.0
7355
- */
7356
- private function add_sticky_optin_admin_notice() {
7357
- if ( ! $this->_is_network_active || ! fs_is_network_admin() ) {
7358
- $this->_storage->sticky_optin_added = true;
7359
- } else {
7360
- $this->_storage->sticky_optin_added_ms = true;
7361
- }
7362
-
7363
- // Show notice for new plugin installations.
7364
- $this->_admin_notices->add_sticky(
7365
- sprintf(
7366
- $this->get_text_inline( 'We made a few tweaks to the %s, %s', 'few-plugin-tweaks' ),
7367
- $this->_module_type,
7368
- sprintf( '<b><a href="%s">%s</a></b>',
7369
- $this->get_activation_url(),
7370
- sprintf( $this->get_text_inline( 'Opt in to make "%s" better!', 'optin-x-now' ), $this->get_plugin_name() )
7371
- )
7372
- ),
7373
- 'connect_account',
7374
- '',
7375
- 'update-nag'
7376
- );
7377
- }
7378
-
7379
- /**
7380
- * Enqueue connect requires scripts and styles.
7381
- *
7382
- * @author Vova Feldman (@svovaf)
7383
- * @since 1.1.4
7384
- */
7385
- function _enqueue_connect_essentials() {
7386
- wp_enqueue_script( 'jquery' );
7387
- wp_enqueue_script( 'json2' );
7388
-
7389
- fs_enqueue_local_script( 'postmessage', 'nojquery.ba-postmessage.min.js' );
7390
- fs_enqueue_local_script( 'fs-postmessage', 'postmessage.js' );
7391
-
7392
- fs_enqueue_local_style( 'fs_connect', '/admin/connect.css' );
7393
- }
7394
-
7395
- /**
7396
- * Add connect / opt-in pointer.
7397
- *
7398
- * @author Vova Feldman (@svovaf)
7399
- * @since 1.1.4
7400
- */
7401
- function _add_connect_pointer_script() {
7402
- $vars = array( 'id' => $this->_module_id );
7403
- $pointer_content = fs_get_template( 'connect.php', $vars );
7404
- ?>
7405
- <script type="text/javascript">// <![CDATA[
7406
- jQuery(document).ready(function ($) {
7407
- if ('undefined' !== typeof(jQuery().pointer)) {
7408
-
7409
- var element = <?php echo $this->apply_filters( 'optin_pointer_element', '$("#non_existing_element");' ) ?>;
7410
-
7411
- if (element.length > 0) {
7412
- var optin = $(element).pointer($.extend(true, {}, {
7413
- content : <?php echo json_encode( $pointer_content ) ?>,
7414
- position : {
7415
- edge : 'left',
7416
- align: 'center'
7417
- },
7418
- buttons : function () {
7419
- // Don't show pointer buttons.
7420
- return '';
7421
- },
7422
- pointerWidth: 482
7423
- }, <?php echo $this->apply_filters( 'optin_pointer_options_json', '{}' ) ?>));
7424
-
7425
- <?php
7426
- echo $this->apply_filters( 'optin_pointer_execute', "
7427
-
7428
- optin.pointer('open');
7429
-
7430
- // Tag the opt-in pointer with custom class.
7431
- $('.wp-pointer #fs_connect')
7432
- .parents('.wp-pointer.wp-pointer-top')
7433
- .addClass('fs-opt-in-pointer');
7434
-
7435
- ", 'element', 'optin' ) ?>
7436
- }
7437
- }
7438
- });
7439
- // ]]></script>
7440
- <?php
7441
- }
7442
-
7443
- /**
7444
- * Return current page's URL.
7445
- *
7446
- * @author Vova Feldman (@svovaf)
7447
- * @since 1.0.7
7448
- *
7449
- * @return string
7450
- */
7451
- function current_page_url() {
7452
- $url = 'http';
7453
-
7454
- if ( isset( $_SERVER["HTTPS"] ) ) {
7455
- if ( $_SERVER["HTTPS"] == "on" ) {
7456
- $url .= "s";
7457
- }
7458
- }
7459
- $url .= "://";
7460
- if ( $_SERVER["SERVER_PORT"] != "80" ) {
7461
- $url .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
7462
- } else {
7463
- $url .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
7464
- }
7465
-
7466
- return esc_url( $url );
7467
- }
7468
-
7469
- /**
7470
- * Check if the current page is the plugin's main admin settings page.
7471
- *
7472
- * @author Vova Feldman (@svovaf)
7473
- * @since 1.0.7
7474
- *
7475
- * @return bool
7476
- */
7477
- function _is_plugin_page() {
7478
- return fs_is_plugin_page( $this->_menu->get_raw_slug() ) ||
7479
- fs_is_plugin_page( $this->_slug );
7480
- }
7481
-
7482
- /* Events
7483
- ------------------------------------------------------------------------------------------------------------------*/
7484
- /**
7485
- * Delete site install from Database.
7486
- *
7487
- * @author Vova Feldman (@svovaf)
7488
- * @since 1.0.1
7489
- *
7490
- * @param bool $store
7491
- * @param int|null $blog_id Since 2.0.0
7492
- *
7493
- * @return false|int The install ID if deleted. Otherwise, FALSE (when install not exist).
7494
- */
7495
- function _delete_site( $store = true, $blog_id = null ) {
7496
- return self::_delete_site_by_slug( $this->_slug, $this->_module_type, $store, $blog_id );
7497
- }
7498
-
7499
- /**
7500
- * Delete site install from Database.
7501
- *
7502
- * @author Vova Feldman (@svovaf)
7503
- * @since 1.2.2.7
7504
- *
7505
- * @param string $slug
7506
- * @param string $module_type
7507
- * @param bool $store
7508
- * @param int|null $blog_id Since 2.0.0
7509
- *
7510
- * @return false|int The install ID if deleted. Otherwise, FALSE (when install not exist).
7511
- */
7512
- static function _delete_site_by_slug( $slug, $module_type, $store = true, $blog_id = null ) {
7513
- $sites = self::get_all_sites( $module_type, $blog_id );
7514
-
7515
- $install_id = false;
7516
-
7517
- if ( isset( $sites[ $slug ] ) ) {
7518
- if ( is_object( $sites[ $slug ] ) ) {
7519
- $install_id = $sites[ $slug ]->id;
7520
- }
7521
-
7522
- unset( $sites[ $slug ] );
7523
-
7524
- self::set_account_option_by_module( $module_type, 'sites', $sites, $store, $blog_id );
7525
- }
7526
-
7527
- return $install_id;
7528
- }
7529
-
7530
- /**
7531
- * Delete user.
7532
- *
7533
- * @author Vova Feldman (@svovaf)
7534
- * @since 2.0.0
7535
- *
7536
- * @param number $user_id
7537
- * @param bool $store
7538
- *
7539
- * @return false|int The user ID if deleted. Otherwise, FALSE (when install not exist).
7540
- */
7541
- private static function delete_user( $user_id, $store = true ) {
7542
- $users = self::get_all_users();
7543
-
7544
- if ( ! is_array( $users ) || ! isset( $users[ $user_id ] ) ) {
7545
- return false;
7546
- }
7547
-
7548
- unset( $users[ $user_id ] );
7549
-
7550
- self::$_accounts->set_option( 'users', $users, $store );
7551
-
7552
- return $user_id;
7553
- }
7554
-
7555
- /**
7556
- * Delete plugin's plans information.
7557
- *
7558
- * @param bool $store Flush to Database if true.
7559
- * @param bool $keep_associated_plans If set to false, delete all plans, even if a plan is associated with an install.
7560
- *
7561
- * @author Vova Feldman (@svovaf)
7562
- * @since 1.0.9
7563
- */
7564
- private function _delete_plans( $store = true, $keep_associated_plans = true ) {
7565
- $this->_logger->entrance();
7566
-
7567
- $plans = self::get_all_plans( $this->_module_type );
7568
-
7569
- $plans_to_keep = array();
7570
-
7571
- if ( $keep_associated_plans ) {
7572
- $plans_ids_to_keep = $this->get_plans_ids_associated_with_installs();
7573
- foreach ( $plans_ids_to_keep as $plan_id ) {
7574
- $plan = self::_get_plan_by_id( $plan_id );
7575
- if ( is_object( $plan ) ) {
7576
- $plans_to_keep[] = self::_encrypt_entity( $plan );
7577
- }
7578
- }
7579
- }
7580
-
7581
- if ( ! empty( $plans_to_keep ) ) {
7582
- $plans[ $this->_slug ] = $plans_to_keep;
7583
- } else {
7584
- unset( $plans[ $this->_slug ] );
7585
- }
7586
-
7587
- $this->set_account_option( 'plans', $plans, $store );
7588
- }
7589
-
7590
- /**
7591
- * Delete all plugin licenses.
7592
- *
7593
- * @author Vova Feldman (@svovaf)
7594
- * @since 1.0.9
7595
- *
7596
- * @param bool $store
7597
- */
7598
- private function _delete_licenses( $store = true ) {
7599
- $this->_logger->entrance();
7600
-
7601
- $all_licenses = self::get_all_licenses();
7602
-
7603
- unset( $all_licenses[ $this->_module_id ] );
7604
-
7605
- self::$_accounts->set_option( 'all_licenses', $all_licenses, $store );
7606
- }
7607
-
7608
- /**
7609
- * Check if Freemius was added on new plugin installation.
7610
- *
7611
- * @author Vova Feldman (@svovaf)
7612
- * @since 1.1.5
7613
- *
7614
- * @return bool
7615
- */
7616
- function is_plugin_new_install() {
7617
- return isset( $this->_storage->is_plugin_new_install ) &&
7618
- $this->_storage->is_plugin_new_install;
7619
- }
7620
-
7621
- /**
7622
- * Check if it's the first plugin release that is running Freemius.
7623
- *
7624
- * @author Vova Feldman (@svovaf)
7625
- * @since 1.2.1.5
7626
- *
7627
- * @return bool
7628
- */
7629
- function is_first_freemius_powered_version() {
7630
- return empty( $this->_storage->plugin_last_version );
7631
- }
7632
-
7633
- /**
7634
- * @author Leo Fajardo (@leorw)
7635
- * @since 1.2.2
7636
- *
7637
- * @return bool|string
7638
- */
7639
- private function get_previous_theme_slug() {
7640
- return isset( $this->_storage->previous_theme ) ?
7641
- $this->_storage->previous_theme :
7642
- false;
7643
- }
7644
-
7645
- /**
7646
- * @author Leo Fajardo (@leorw)
7647
- * @since 1.2.2
7648
- *
7649
- * @return string
7650
- */
7651
- private function can_activate_previous_theme() {
7652
- $slug = $this->get_previous_theme_slug();
7653
- if ( false !== $slug && current_user_can( 'switch_themes' ) ) {
7654
- $theme_instance = wp_get_theme( $slug );
7655
-
7656
- return $theme_instance->exists();
7657
- }
7658
-
7659
- return false;
7660
- }
7661
-
7662
- /**
7663
- * @author Leo Fajardo (@leorw)
7664
- * @since 1.2.2
7665
- */
7666
- private function activate_previous_theme() {
7667
- switch_theme( $this->get_previous_theme_slug() );
7668
- unset( $this->_storage->previous_theme );
7669
-
7670
- global $pagenow;
7671
- if ( 'themes.php' === $pagenow ) {
7672
- /**
7673
- * Refresh the active theme information.
7674
- *
7675
- * @author Leo Fajardo (@leorw)
7676
- * @since 1.2.2
7677
- */
7678
- fs_redirect( $this->admin_url( $pagenow ) );
7679
- }
7680
- }
7681
-
7682
- /**
7683
- * @author Leo Fajardo (@leorw)
7684
- * @since 1.2.2
7685
- *
7686
- * @return string
7687
- */
7688
- function get_previous_theme_activation_url() {
7689
- if ( ! $this->can_activate_previous_theme() ) {
7690
- return '';
7691
- }
7692
-
7693
- /**
7694
- * Activation URL
7695
- *
7696
- * @author Leo Fajardo (@leorw)
7697
- * @since 1.2.2
7698
- */
7699
- return wp_nonce_url(
7700
- $this->admin_url( 'themes.php?action=activate&stylesheet=' . urlencode( $this->get_previous_theme_slug() ) ),
7701
- 'switch-theme_' . $this->get_previous_theme_slug()
7702
- );
7703
- }
7704
-
7705
- /**
7706
- * Saves the slug of the previous theme if it still exists so that it can be used by the logic in the opt-in
7707
- * form that decides whether to add a close button to the opt-in dialog or not. So after a premium-only theme is
7708
- * activated, the close button will appear and will reactivate the previous theme if clicked. If the previous
7709
- * theme doesn't exist, then there will be no close button.
7710
- *
7711
- * @author Leo Fajardo (@leorw)
7712
- * @since 1.2.2
7713
- *
7714
- * @param string $slug_or_name Old theme's slug or name.
7715
- * @param bool|WP_Theme $old_theme WP_Theme instance of the old theme if it still exists.
7716
- */
7717
- function _activate_theme_event_hook( $slug_or_name, $old_theme = false ) {
7718
- $this->_storage->previous_theme = ( false !== $old_theme ) ?
7719
- $old_theme->get_stylesheet() :
7720
- $slug_or_name;
7721
-
7722
- $this->_activate_plugin_event_hook();
7723
- }
7724
-
7725
- /**
7726
- * Plugin activated hook.
7727
- *
7728
- * @author Vova Feldman (@svovaf)
7729
- * @since 1.0.1
7730
- *
7731
- * @uses FS_Api
7732
- */
7733
- function _activate_plugin_event_hook() {
7734
- $this->_logger->entrance( 'slug = ' . $this->_slug );
7735
-
7736
- if ( ! $this->is_user_admin() ) {
7737
- return;
7738
- }
7739
-
7740
- $this->unregister_uninstall_hook();
7741
-
7742
- // Clear API cache on activation.
7743
- FS_Api::clear_cache();
7744
-
7745
- $is_premium_version_activation = $this->is_plugin() ?
7746
- ( current_filter() !== ( 'activate_' . $this->_free_plugin_basename ) ) :
7747
- $this->is_premium();
7748
-
7749
- $this->_logger->info( 'Activating ' . ( $is_premium_version_activation ? 'premium' : 'free' ) . ' plugin version.' );
7750
-
7751
- if ( $this->is_plugin() ) {
7752
- // This logic is relevant only to plugins since both the free and premium versions of a plugin can be active at the same time.
7753
- // 1. If running in the activation of the FREE module, get the basename of the PREMIUM.
7754
- // 2. If running in the activation of the PREMIUM module, get the basename of the FREE.
7755
- $other_version_basename = $is_premium_version_activation ?
7756
- $this->_free_plugin_basename :
7757
- $this->premium_plugin_basename();
7758
-
7759
- if ( ! $this->_is_network_active ) {
7760
- /**
7761
- * Themes are always network activated, but the ACTUAL activation is per site.
7762
- *
7763
- * During the activation, the plugin isn't yet active, therefore,
7764
- * _is_network_active will be set to false even if it's a network level
7765
- * activation. So we need to fix that by looking at the is_network_admin() value.
7766
- *
7767
- * @author Vova Feldman
7768
- */
7769
- $this->_is_network_active = (
7770
- $this->_is_multisite_integrated &&
7771
- fs_is_network_admin()
7772
- );
7773
- }
7774
-
7775
- /**
7776
- * If the other module version is active, deactivate it.
7777
- *
7778
- * is_plugin_active() checks if the plugin is active on the site or the network level and
7779
- * deactivate_plugins() deactivates the plugin whether it's activated on the site or network level.
7780
- *
7781
- * @author Leo Fajardo (@leorw)
7782
- * @since 1.2.2
7783
- */
7784
- if (
7785
- is_plugin_active( $other_version_basename ) &&
7786
- $this->apply_filters( 'deactivate_on_activation', true )
7787
- ) {
7788
- deactivate_plugins( $other_version_basename );
7789
- }
7790
- }
7791
-
7792
- if ( $this->is_registered() ) {
7793
- if ( $is_premium_version_activation ) {
7794
- $this->reconnect_locally();
7795
- }
7796
-
7797
-
7798
- // Schedule re-activation event and sync.
7799
- // $this->sync_install( array(), true );
7800
- $this->schedule_install_sync();
7801
-
7802
- // If activating the premium module version, add an admin notice to congratulate for an upgrade completion.
7803
- if ( $is_premium_version_activation ) {
7804
- $this->_admin_notices->add(
7805
- sprintf( $this->get_text_inline( 'The upgrade of %s was successfully completed.', 'successful-version-upgrade-message' ), sprintf( '<b>%s</b>', $this->_plugin->title ) ),
7806
- $this->get_text_x_inline( 'W00t',
7807
- 'Used to express elation, enthusiasm, or triumph (especially in electronic communication).', 'woot' ) . '!'
7808
- );
7809
- }
7810
- } else if ( $this->is_anonymous() ) {
7811
- if ( isset( $this->_storage->is_anonymous_ms ) && $this->_storage->is_anonymous_ms['is'] ) {
7812
- $plugin_version = $this->_storage->is_anonymous_ms['version'];
7813
- $network = true;
7814
- } else {
7815
- $plugin_version = $this->_storage->is_anonymous['version'];
7816
- $network = false;
7817
- }
7818
-
7819
- /**
7820
- * Reset "skipped" click cache on the following:
7821
- * 1. Freemius DEV mode.
7822
- * 2. WordPress DEBUG mode.
7823
- * 3. If a plugin and the user skipped the exact same version before.
7824
- *
7825
- * @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).
7826
- *
7827
- * @todo 4. If explicitly asked to retry after every activation.
7828
- */
7829
- if ( WP_FS__DEV_MODE ||
7830
- (
7831
- ( $this->is_plugin() || ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ) &&
7832
- $this->get_plugin_version() == $plugin_version
7833
- )
7834
- ) {
7835
- $this->reset_anonymous_mode( $network );
7836
- }
7837
- }
7838
-
7839
- $is_trial_or_has_features_enabled_license = ( $this->is_trial() || $this->has_features_enabled_license() );
7840
-
7841
- if ( $this->is_addon() && ! $is_trial_or_has_features_enabled_license ) {
7842
- /**
7843
- * When activating an add-on, try to also activate a license.
7844
- *
7845
- * @author Leo Fajardo (@leorw)
7846
- * @since 2.3.0
7847
- */
7848
- if ( ! $this->_is_network_active ) {
7849
- $this->maybe_activate_addon_license();
7850
- } else {
7851
- $this->maybe_network_activate_addon_license();
7852
- }
7853
-
7854
- /**
7855
- * Avoid redirecting to the license activation screen after automatically activating an add-on license.
7856
- *
7857
- * @author Leo Fajardo (@leorw)
7858
- * @since 2.3.0
7859
- */
7860
- $is_trial_or_has_features_enabled_license = ( $this->is_trial() || $this->has_features_enabled_license() );
7861
-
7862
- if ( $is_trial_or_has_features_enabled_license && true === $this->_storage->require_license_activation ) {
7863
- $this->_storage->require_license_activation = false;
7864
- }
7865
- }
7866
-
7867
- if (
7868
- $is_premium_version_activation &&
7869
- (
7870
- ( ! $this->is_registered() && $this->is_anonymous() ) ||
7871
- (
7872
- $this->is_registered() &&
7873
- ! $is_trial_or_has_features_enabled_license
7874
- )
7875
- )
7876
- ) {
7877
- $this->_storage->require_license_activation = true;
7878
- }
7879
-
7880
- if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
7881
- /**
7882
- * If no previous version of plugin's version exist, it means that it's either
7883
- * the first time that the plugin installed on the site, or the plugin was installed
7884
- * before but didn't have Freemius integrated.
7885
- *
7886
- * Since register_activation_hook() do NOT fires on updates since 3.1, and only fires
7887
- * on manual activation via the dashboard, is_plugin_activation() is TRUE
7888
- * only after immediate activation.
7889
- *
7890
- * @since 1.1.4
7891
- * @link https://make.wordpress.org/core/2010/10/27/plugin-activation-hooks-no-longer-fire-for-updates/
7892
- */
7893
- $this->_storage->is_plugin_new_install = empty( $this->_storage->plugin_last_version );
7894
- }
7895
-
7896
- /**
7897
- * Also flush when activating the premium version so that even if Freemius was off before, the API
7898
- * connectivity test can be run again.
7899
- *
7900
- * @author Leo Fajardo (@leorw)
7901
- * @since 2.2.3.1
7902
- */
7903
- $has_api_connectivity = $this->has_api_connectivity( WP_FS__DEV_MODE || $is_premium_version_activation );
7904
-
7905
- if ( ! $this->_anonymous_mode &&
7906
- $has_api_connectivity &&
7907
- ! $this->_isAutoInstall
7908
- ) {
7909
- // Store hint that the plugin was just activated to enable auto-redirection to settings.
7910
- set_transient( "fs_{$this->_module_type}_{$this->_slug}_activated", true, 60 );
7911
- }
7912
-
7913
- /**
7914
- * Activation hook is executed after the plugin's main file is loaded, therefore,
7915
- * after the plugin was loaded. The logic is located at activate_plugin()
7916
- * ./wp-admin/includes/plugin.php.
7917
- *
7918
- * @author Vova Feldman (@svovaf)
7919
- * @since 1.1.9
7920
- */
7921
- $this->_storage->was_plugin_loaded = true;
7922
- }
7923
-
7924
- /**
7925
- * @author Leo Fajardo (@leorw)
7926
- * @since 2.3.0
7927
- */
7928
- private function maybe_activate_addon_license() {
7929
- $parent_fs = $this->get_parent_instance();
7930
-
7931
- if (
7932
- ! is_object( $parent_fs ) ||
7933
- ( ! $parent_fs->is_registered() && ! $parent_fs->is_network_registered() )
7934
- ) {
7935
- // Try to activate a license only if the parent plugin is active and has a valid `install`.
7936
- return;
7937
- }
7938
-
7939
- $license = $this->get_active_parent_license();
7940
- if ( ! is_object( $license ) ) {
7941
- return;
7942
- }
7943
-
7944
- if (
7945
- $this->is_bundle_license_auto_activation_enabled() &&
7946
- ! empty( $license->products )
7947
- ) {
7948
- $this->activate_bundle_license( $license );
7949
-
7950
- return;
7951
- }
7952
-
7953
- if ( ! $this->is_registered() ) {
7954
- // Opt in with a license key.
7955
- $this->opt_in(
7956
- $parent_fs->get_current_or_network_user()->email,
7957
- false,
7958
- false,
7959
- $license->secret_key
7960
- );
7961
- } else {
7962
- // Activate the license.
7963
- $install = $this->get_api_site_scope()->call(
7964
- '/',
7965
- 'put',
7966
- array( 'license_key' => $this->apply_filters( 'license_key', $license->secret_key ) )
7967
- );
7968
-
7969
- if ( ! FS_Api::is_api_error( $install ) ) {
7970
- $this->_sync_addon_license( $this->get_id(), true );
7971
- }
7972
- }
7973
- }
7974
-
7975
- /**
7976
- * @author Leo Fajardo (@leorw)
7977
- * @since 2.3.0
7978
- *
7979
- * @param FS_Plugin_License $license
7980
- */
7981
- private function maybe_network_activate_addon_license( $license = null ) {
7982
- $parent_fs = $this->get_parent_instance();
7983
- if ( ! is_object( $parent_fs ) || ( ! $parent_fs->is_registered() && ! $parent_fs->is_network_registered() ) ) {
7984
- // Try to activate a license only if the parent plugin is active and has a valid `install`.
7985
- return;
7986
- }
7987
-
7988
- $license = ( ! is_null( $license ) ) ?
7989
- $license :
7990
- $this->get_active_parent_license();
7991
-
7992
- if ( ! is_object( $license ) ) {
7993
- return;
7994
- }
7995
-
7996
- if (
7997
- $this->is_bundle_license_auto_activation_enabled() &&
7998
- ! empty( $license->products )
7999
- ) {
8000
- $this->activate_bundle_license( $license );
8001
-
8002
- return;
8003
- }
8004
-
8005
- if ( ! $this->is_network_registered() ) {
8006
- $sites = $this->get_sites_for_network_level_optin();
8007
-
8008
- if ( count( $sites ) > $license->left() ) {
8009
- // If the add-on is network active, try to activate the license only if it can be activated on all sites.
8010
- return;
8011
- }
8012
-
8013
- // Opt in with a license key.
8014
- $this->opt_in(
8015
- $parent_fs->get_user()->email,
8016
- false,
8017
- false,
8018
- $license->secret_key,
8019
- false,
8020
- false,
8021
- false,
8022
- null,
8023
- $sites
8024
- );
8025
- } else {
8026
- $blog_2_install_map = array();
8027
- $site_ids = array();
8028
-
8029
- $all_sites = Freemius::get_sites();
8030
-
8031
- foreach ( $all_sites as $site ) {
8032
- $blog_id = Freemius::get_site_blog_id( $site );
8033
- $install = $this->get_install_by_blog_id( $blog_id );
8034
-
8035
- if ( is_object( $install ) && FS_Plugin_License::is_valid_id( $install->license_id ) ) {
8036
- // Skip license activation for installs that are already associated with a license.
8037
- continue;
8038
- }
8039
-
8040
- if ( is_object( $install ) ) {
8041
- $blog_2_install_map[ $blog_id ] = $install;
8042
- } else {
8043
- $site_ids[] = $blog_id;
8044
- }
8045
- }
8046
-
8047
- if ( ( count( $blog_2_install_map ) + count( $site_ids ) ) > $license->left() ) {
8048
- return;
8049
- }
8050
-
8051
- $user = $this->get_current_or_network_user();
8052
-
8053
- if ( ! empty( $blog_2_install_map ) ) {
8054
- $result = $this->activate_license_on_many_installs( $user, $license->secret_key, $blog_2_install_map );
8055
-
8056
- if ( true !== $result ) {
8057
- return;
8058
- }
8059
- }
8060
-
8061
- if ( ! empty( $site_ids ) ) {
8062
- $this->activate_license_on_many_sites( $user, $license->secret_key, $site_ids );
8063
- }
8064
- }
8065
- }
8066
-
8067
- /**
8068
- * Tries to activate a bundle license for all supported products if the current product is activated with a bundle license. This is called after activating an available license (not via the license activation dialog but by clicking on a license activation button) for a product via its "Account" page.
8069
- *
8070
- * @author Leo Fajardo (@leorw)
8071
- * @since 2.4.0
8072
- *
8073
- * @param FS_Plugin_License $license
8074
- * @param array $sites
8075
- * @param int $blog_id
8076
- */
8077
- private function maybe_activate_bundle_license( FS_Plugin_License $license = null, $sites = array(), $blog_id = 0 ) {
8078
- if ( ! is_object( $license ) && $this->has_active_valid_license() ) {
8079
- $license = $this->_license;
8080
- }
8081
-
8082
- if ( ! is_object( $license ) ) {
8083
- return;
8084
- }
8085
-
8086
- $parent_license = ( ! empty( $license->products ) ) ?
8087
- $license :
8088
- $this->get_active_parent_license( $license->secret_key );
8089
-
8090
- if ( is_object( $parent_license ) ) {
8091
- $this->activate_bundle_license( $parent_license, $sites, $blog_id );
8092
- }
8093
- }
8094
-
8095
- /**
8096
- * Try to activate a bundle license for all the bundle products installed on the site.
8097
- * (1) If a child product install already has a license, the bundle license won't be activated.
8098
- * (2) On multi-site networks, if the attempt to activate the bundle license is triggered from the network admin, the bundle license activation will only work for non-delegated sites and only if none of them is associated with a license. Even if one of the sites has the product installed with a license key, skip the bundle license activation for the product.
8099
- * (3) On multi-site networks, if the attempt to activate the bundle license is triggered from a site-level admin, only activate the license if the product is site-level activated or delegated, and the product installation is not yet associated with a license.
8100
- *
8101
- * @author Leo Fajardo (@leorw)
8102
- * @since 2.4.0
8103
- *
8104
- * @param FS_Plugin_License $license
8105
- * @param array $sites
8106
- * @param int $current_blog_id
8107
- */
8108
- private function activate_bundle_license( $license, $sites = array(), $current_blog_id = 0 ) {
8109
- $is_network_admin = fs_is_network_admin();
8110
-
8111
- $installs_by_blog_map = array();
8112
- $site_info_by_blog_map = array();
8113
-
8114
- /**
8115
- * Try to activate the license for all supported products.
8116
- *
8117
- * @author Leo Fajardo
8118
- */
8119
- foreach ( $license->products as $product_id ) {
8120
- $fs = self::get_instance_by_id( $product_id );
8121
-
8122
- if ( ! is_object( $fs ) ) {
8123
- continue;
8124
- }
8125
-
8126
- if ( ! $fs->has_paid_plan() ) {
8127
- continue;
8128
- }
8129
-
8130
- if (
8131
- ! $fs->is_addon() &&
8132
- ! FS_Plan_Manager::instance()->has_paid_plan( $fs->_plans )
8133
- ) {
8134
- /**
8135
- * The parent product can be free-only but can have its `has_paid_plan` flag set to `true` when
8136
- * there is a context bundle.
8137
- */
8138
- continue;
8139
- }
8140
-
8141
- if ( $current_blog_id > 0 ) {
8142
- $fs->switch_to_blog( $current_blog_id );
8143
- }
8144
-
8145
- if ( $fs->has_active_valid_license() ) {
8146
- continue;
8147
- }
8148
-
8149
- if ( ! $is_network_admin || $current_blog_id > 0 ) {
8150
- if ( $fs->is_network_active() && ! $fs->is_delegated_connection( $current_blog_id ) ) {
8151
- // Do not try to activate the license in the site level if the product is network active and the connection was not delegated.
8152
- continue;
8153
- }
8154
- } else {
8155
- if ( ! $fs->is_network_active() ) {
8156
- // Do not try to activate the license in the network level if the product is not network active.
8157
- continue;
8158
- }
8159
-
8160
- if ( $fs->is_network_delegated_connection() ) {
8161
- // Do not try to activate the license in the network level if the activation has been delegated to site admins.
8162
- continue;
8163
- }
8164
-
8165
- $has_install_with_license = false;
8166
-
8167
- // Collection of sites that have an install entity that is not activated with a license or non-delegated sites that have no install entity, or both types of site.
8168
- $filtered_sites = array();
8169
-
8170
- if ( empty( $sites ) ) {
8171
- $all_sites = self::get_sites();
8172
-
8173
- foreach ( $all_sites as $site ) {
8174
- $sites[] = array( 'blog_id' => self::get_site_blog_id( $site ) );
8175
- }
8176
- } else {
8177
- // Populate the map here to avoid calling `$fs->get_site_info( $site );` in the other `for` loop below.
8178
- foreach ( $sites as $site ) {
8179
- if ( ! isset( $site['blog_id'] ) || ! is_numeric( $site['blog_id'] ) ) {
8180
- continue;
8181
- }
8182
-
8183
- $site_info_by_blog_map[ $site['blog_id'] ] = $site;
8184
- }
8185
- }
8186
-
8187
- foreach ( $sites as $site ) {
8188
- if ( ! isset( $site['blog_id'] ) || ! is_numeric( $site['blog_id'] ) ) {
8189
- continue;
8190
- }
8191
-
8192
- $blog_id = $site['blog_id'];
8193
-
8194
- if ( ! isset( $installs_by_blog_map[ $blog_id ] ) ) {
8195
- $installs_by_blog_map[ $blog_id ] = self::get_all_sites( $fs->get_module_type(), $blog_id );
8196
- }
8197
-
8198
- $installs = $installs_by_blog_map[ $blog_id ];
8199
- $install = null;
8200
-
8201
- if ( isset( $installs[ $fs->get_slug() ] ) ) {
8202
- $install = $installs[ $fs->get_slug() ];
8203
-
8204
- if (
8205
- is_object( $install ) &&
8206
- (
8207
- ! FS_Site::is_valid_id( $install->id ) ||
8208
- ! FS_User::is_valid_id( $install->user_id ) ||
8209
- ! FS_Plugin_Plan::is_valid_id( $install->plan_id )
8210
- )
8211
- ) {
8212
- $install = null;
8213
- }
8214
- }
8215
-
8216
- if (
8217
- is_object( $install ) &&
8218
- FS_Plugin_License::is_valid_id( $install->license_id )
8219
- ) {
8220
- $has_install_with_license = true;
8221
- break;
8222
- }
8223
-
8224
- if ( $fs->is_site_delegated_connection( $blog_id ) ) {
8225
- // Site activation delegated, don't activate bundle license on the site in the network admin.
8226
- continue;
8227
- }
8228
-
8229
- if ( ! isset( $site_info_by_blog_map[ $blog_id ] ) ) {
8230
- $site_info_by_blog_map[ $blog_id ] = $fs->get_site_info( $site );
8231
- }
8232
-
8233
- $filtered_sites[] = $site_info_by_blog_map[ $blog_id ];
8234
- }
8235
-
8236
- if ( $has_install_with_license || empty( $filtered_sites ) ) {
8237
- // Do not try to activate the license at the network level if there's any install with a license or there's no site to activate the license on.
8238
- continue;
8239
- }
8240
-
8241
- $sites = $filtered_sites;
8242
- }
8243
-
8244
- $fs->activate_migrated_license(
8245
- $license->secret_key,
8246
- null,
8247
- null,
8248
- $sites,
8249
- ( $current_blog_id > 0 ? $current_blog_id : null )
8250
- );
8251
- }
8252
- }
8253
-
8254
- /**
8255
- * Returns a parent license that can be activated for the context product.
8256
- *
8257
- * @author Leo Fajardo (@leorw)
8258
- * @since 2.3.0
8259
- *
8260
- * @param string|null $license_key
8261
- * @param bool $flush
8262
- *
8263
- * @return FS_Plugin_License
8264
- */
8265
- function get_active_parent_license( $license_key = null, $flush = true ) {
8266
- $parent_licenses_endpoint = "/plugins/{$this->get_id()}/parent_licenses.json?filter=activatable";
8267
-
8268
- $fs = $this;
8269
-
8270
- if ( $this->is_addon() ) {
8271
- $parent_instance = $this->get_parent_instance();
8272
-
8273
- if ( is_object( $parent_instance ) && $parent_instance->is_registered() ) {
8274
- $fs = $parent_instance;
8275
- }
8276
- }
8277
-
8278
- $foreign_licenses = $fs->get_foreign_licenses_info(
8279
- self::get_all_licenses( $this->get_parent_id() )
8280
- );
8281
-
8282
- if ( ! empty ( $foreign_licenses ) ) {
8283
- $foreign_licenses = array(
8284
- // Prefix with `+` to tell the server to include foreign licenses in the licenses collection.
8285
- 'ids' => ( urlencode( '+' ) . implode( ',', $foreign_licenses['ids'] ) ),
8286
- 'license_keys' => implode( ',', array_map( 'urlencode', $foreign_licenses['license_keys'] ) )
8287
- );
8288
-
8289
- $parent_licenses_endpoint = add_query_arg( $foreign_licenses, $parent_licenses_endpoint );
8290
- }
8291
-
8292
- $result = $fs->get_current_or_network_user_api_scope()->get( $parent_licenses_endpoint, $flush );
8293
-
8294
- if (
8295
- ! $this->is_api_result_object( $result, 'licenses' ) ||
8296
- ! is_array( $result->licenses ) ||
8297
- empty( $result->licenses )
8298
- ) {
8299
- return null;
8300
- }
8301
-
8302
- $parent_license = null;
8303
-
8304
- if ( empty( $license_key ) ) {
8305
- $parent_license = $result->licenses[0];
8306
- } else {
8307
- foreach ( $result->licenses as $license ) {
8308
- if ( $license_key === $license->secret_key ) {
8309
- $parent_license = $license;
8310
- break;
8311
- }
8312
- }
8313
- }
8314
-
8315
- if ( ! is_null( $parent_license ) ) {
8316
- $parent_license = new FS_Plugin_License( $parent_license );
8317
- }
8318
-
8319
- return $parent_license;
8320
- }
8321
-
8322
- /**
8323
- * @author Leo Fajardo (@leorw)
8324
- * @since 2.3.0
8325
- *
8326
- * @return array
8327
- */
8328
- function get_sites_for_network_level_optin() {
8329
- $sites = array();
8330
- $all_sites = self::get_sites();
8331
-
8332
- foreach ( $all_sites as $site ) {
8333
- $blog_id = self::get_site_blog_id( $site );
8334
-
8335
- if ( ! $this->is_site_delegated_connection( $blog_id ) &&
8336
- ! $this->is_installed_on_site( $blog_id )
8337
- ) {
8338
- $sites[] = $this->get_site_info( $site );
8339
- }
8340
- }
8341
-
8342
- return $sites;
8343
- }
8344
-
8345
- /**
8346
- * Delete account.
8347
- *
8348
- * @author Vova Feldman (@svovaf)
8349
- * @since 1.0.3
8350
- *
8351
- * @param bool $check_user Enforce checking if user have plugins activation privileges.
8352
- */
8353
- function delete_account_event( $check_user = true ) {
8354
- $this->_logger->entrance( 'slug = ' . $this->_slug );
8355
-
8356
- if ( $check_user && ! $this->is_user_admin() ) {
8357
- return;
8358
- }
8359
-
8360
- $this->do_action( 'before_account_delete' );
8361
-
8362
- // Clear all admin notices.
8363
- $this->_admin_notices->clear_all_sticky( false );
8364
-
8365
- $this->_delete_site( false );
8366
-
8367
- $delete_network_common_data = true;
8368
-
8369
- if ( $this->_is_network_active ) {
8370
- $installs = $this->get_blog_install_map();
8371
-
8372
- // Don't delete common network data unless no other installs left.
8373
- $delete_network_common_data = empty( $installs );
8374
- }
8375
-
8376
- if ( $delete_network_common_data ) {
8377
- $this->_delete_plans( false );
8378
-
8379
- $this->_delete_licenses( false );
8380
-
8381
- // Delete add-ons related to plugin's account.
8382
- $this->_delete_account_addons( false );
8383
- }
8384
-
8385
- // @todo Delete plans and licenses of add-ons.
8386
-
8387
- self::$_accounts->store();
8388
-
8389
- /**
8390
- * IMPORTANT:
8391
- * Clear crons must be executed before clearing all storage.
8392
- * Otherwise, the cron will not be cleared.
8393
- */
8394
- if ( $delete_network_common_data ) {
8395
- $this->clear_sync_cron();
8396
- }
8397
-
8398
- $this->clear_install_sync_cron();
8399
-
8400
- // Clear all storage data.
8401
- $this->_storage->clear_all( true, array(
8402
- 'is_delegated_connection',
8403
- 'connectivity_test',
8404
- 'is_on',
8405
- ), false );
8406
-
8407
- // Send delete event.
8408
- $this->get_api_site_scope()->call( '/', 'delete' );
8409
-
8410
- $this->do_action( 'after_account_delete' );
8411
- }
8412
-
8413
- /**
8414
- * Delete network level account.
8415
- *
8416
- * @author Vova Feldman (@svovaf)
8417
- * @since 2.0.0
8418
- *
8419
- * @param bool $check_user Enforce checking if user have plugins activation privileges.
8420
- */
8421
- function delete_network_account_event( $check_user = true ) {
8422
- $this->_logger->entrance( 'slug = ' . $this->_slug );
8423
-
8424
- if ( $check_user && ! $this->is_user_admin() ) {
8425
- return;
8426
- }
8427
-
8428
- $this->do_action( 'before_network_account_delete' );
8429
-
8430
- // Clear all admin notices.
8431
- $this->_admin_notices->clear_all_sticky();
8432
-
8433
- $this->_delete_plans( false, false );
8434
-
8435
- $this->_delete_licenses( false );
8436
-
8437
- // Delete add-ons related to plugin's account.
8438
- $this->_delete_account_addons( false );
8439
-
8440
- // @todo Delete plans and licenses of add-ons.
8441
-
8442
- self::$_accounts->store( true );
8443
-
8444
- /**
8445
- * IMPORTANT:
8446
- * Clear crons must be executed before clearing all storage.
8447
- * Otherwise, the cron will not be cleared.
8448
- */
8449
- $this->clear_sync_cron( true );
8450
- $this->clear_install_sync_cron( true );
8451
-
8452
- $sites = self::get_sites();
8453
-
8454
- $install_ids = array();
8455
- foreach ( $sites as $site ) {
8456
- $blog_id = self::get_site_blog_id( $site );
8457
-
8458
- if ( $this->is_site_delegated_connection( $blog_id ) ) {
8459
- continue;
8460
- }
8461
-
8462
- $install_id = $this->_delete_site( true, $blog_id );
8463
-
8464
- // Clear all storage data.
8465
- $this->_storage->clear_all( true, array( 'connectivity_test' ), $blog_id );
8466
-
8467
- if ( FS_Site::is_valid_id( $install_id ) ) {
8468
- $install_ids[] = $install_id;
8469
- }
8470
-
8471
- switch_to_blog( $blog_id );
8472
-
8473
- $this->do_action( 'after_account_delete' );
8474
-
8475
- restore_current_blog();
8476
- }
8477
-
8478
- $this->_storage->clear_all( true, array(
8479
- 'connectivity_test',
8480
- 'is_on',
8481
- ), true );
8482
-
8483
- // Send delete event.
8484
- if ( ! empty( $install_ids ) ) {
8485
- $result = $this->get_current_or_network_user_api_scope()->call( "/plugins/{$this->_module_id}/installs.json?ids=" . implode( ',', $install_ids ), 'delete' );
8486
- }
8487
-
8488
- $this->do_action( 'after_network_account_delete' );
8489
- }
8490
-
8491
- /**
8492
- * Plugin deactivation hook.
8493
- *
8494
- * @author Vova Feldman (@svovaf)
8495
- * @since 1.0.1
8496
- */
8497
- function _deactivate_plugin_hook() {
8498
- $this->_logger->entrance( 'slug = ' . $this->_slug );
8499
-
8500
- if ( ! $this->is_user_admin() ) {
8501
- return;
8502
- }
8503
-
8504
- $is_network_deactivation = fs_is_network_admin();
8505
- $storage_keys_for_removal = array();
8506
-
8507
- $this->_admin_notices->clear_all_sticky();
8508
-
8509
- $storage_keys_for_removal[] = 'sticky_optin_added';
8510
- if ( isset( $this->_storage->sticky_optin_added ) ) {
8511
- unset( $this->_storage->sticky_optin_added );
8512
- }
8513
-
8514
- if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
8515
- // Remember that plugin was already installed.
8516
- $this->_storage->is_plugin_new_install = false;
8517
- }
8518
-
8519
- // Hook to plugin uninstall.
8520
- register_uninstall_hook( $this->_plugin_main_file_path, array( 'Freemius', '_uninstall_plugin_hook' ) );
8521
-
8522
- $this->clear_module_main_file_cache();
8523
- $this->clear_sync_cron( $this->_is_network_active );
8524
- $this->clear_install_sync_cron();
8525
-
8526
- if ( $this->is_registered() ) {
8527
- if ( $this->is_premium() && ! $this->has_active_valid_license() ) {
8528
- FS_Plugin_Updater::instance( $this )->delete_update_data();
8529
- }
8530
-
8531
- if ( $is_network_deactivation ) {
8532
- // Send deactivation event.
8533
- $this->sync_installs( array(
8534
- 'is_active' => false,
8535
- ) );
8536
- } else {
8537
- // Send deactivation event.
8538
- $this->sync_install( array(
8539
- 'is_active' => false,
8540
- ) );
8541
- }
8542
- } else {
8543
- if ( ! $this->has_api_connectivity() ) {
8544
- // Reset connectivity test cache.
8545
- unset( $this->_storage->connectivity_test );
8546
-
8547
- $storage_keys_for_removal[] = 'connectivity_test';
8548
- }
8549
- }
8550
-
8551
- if ( $is_network_deactivation ) {
8552
- if ( isset( $this->_storage->sticky_optin_added_ms ) ) {
8553
- unset( $this->_storage->sticky_optin_added_ms );
8554
- }
8555
-
8556
- if ( ! empty( $storage_keys_for_removal ) ) {
8557
- $sites = self::get_sites();
8558
-
8559
- foreach ( $sites as $site ) {
8560
- $blog_id = self::get_site_blog_id( $site );
8561
-
8562
- foreach ( $storage_keys_for_removal as $key ) {
8563
- $this->_storage->remove( $key, false, $blog_id );
8564
- }
8565
-
8566
- $this->_storage->save( $blog_id );
8567
- }
8568
- }
8569
- }
8570
-
8571
- // Clear API cache on deactivation.
8572
- FS_Api::clear_cache();
8573
-
8574
- $this->remove_sdk_reference();
8575
- }
8576
-
8577
- /**
8578
- * @author Vova Feldman (@svovaf)
8579
- * @since 1.1.6
8580
- */
8581
- private function remove_sdk_reference() {
8582
- global $fs_active_plugins;
8583
-
8584
- foreach ( $fs_active_plugins->plugins as $sdk_path => $data ) {
8585
- if ( $this->_plugin_basename == $data->plugin_path ) {
8586
- unset( $fs_active_plugins->plugins[ $sdk_path ] );
8587
- break;
8588
- }
8589
- }
8590
-
8591
- fs_fallback_to_newest_active_sdk();
8592
- }
8593
-
8594
- /**
8595
- * @author Vova Feldman (@svovaf)
8596
- * @since 1.1.3
8597
- *
8598
- * @param bool $is_anonymous
8599
- * @param bool|int $network_or_blog_id Since 2.0.0
8600
- */
8601
- private function set_anonymous_mode( $is_anonymous = true, $network_or_blog_id = 0 ) {
8602
- // Store information regarding skip to try and opt-in the user
8603
- // again in the future.
8604
- $skip_info = array(
8605
- 'is' => $is_anonymous,
8606
- 'timestamp' => WP_FS__SCRIPT_START_TIME,
8607
- 'version' => $this->get_plugin_version(),
8608
- );
8609
-
8610
- if ( true === $network_or_blog_id ) {
8611
- $this->_storage->is_anonymous_ms = $skip_info;
8612
- } else {
8613
- $this->_storage->store( 'is_anonymous', $skip_info, $network_or_blog_id );
8614
- }
8615
-
8616
- $this->network_upgrade_mode_completed();
8617
-
8618
- // Update anonymous mode cache.
8619
- $this->_is_anonymous = $is_anonymous;
8620
- }
8621
-
8622
- /**
8623
- * @author Vova Feldman (@svovaf)
8624
- * @since 2.0.0
8625
- *
8626
- * @param int $blog_id Site ID.
8627
- * @param int $user_id User ID.
8628
- * @param string $domain Site domain.
8629
- * @param string $path Site path.
8630
- * @param int $network_id Network ID. Only relevant on multi-network installations.
8631
- * @param array $meta Metadata. Used to set initial site options.
8632
- *
8633
- * @uses Freemius::is_license_network_active() to check if the context license was network activated by the super-admin.
8634
- * @uses Freemius::is_network_connected() to check if the super-admin network opted-in.
8635
- * @uses Freemius::is_network_anonymous() to check if the super-admin network skipped.
8636
- * @uses Freemius::is_network_delegated_connection() to check if the super-admin network delegated the connection to the site admins.
8637
- */
8638
- public function _after_new_blog_callback( $blog_id, $user_id, $domain, $path, $network_id, $meta ) {
8639
- $this->_logger->entrance();
8640
-
8641
- if ( $this->is_premium() &&
8642
- $this->is_network_connected() &&
8643
- is_object( $this->_license ) &&
8644
- $this->_license->can_activate( FS_Site::is_localhost_by_address( $domain ) ) &&
8645
- $this->is_license_network_active( $blog_id )
8646
- ) {
8647
- /**
8648
- * 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.
8649
- */
8650
- $current_blog_id = get_current_blog_id();
8651
- $license = clone $this->_license;
8652
-
8653
- $this->switch_to_blog( $blog_id );
8654
-
8655
- // Opt-in with network user.
8656
- $this->install_with_user(
8657
- $this->get_network_user(),
8658
- $license->secret_key,
8659
- false,
8660
- false,
8661
- false
8662
- );
8663
-
8664
- if ( is_object( $this->_site ) ) {
8665
- if ( $this->_site->license_id == $license->id ) {
8666
- /**
8667
- * If the license was activated successfully, sync the license data from the remote server.
8668
- */
8669
- $this->_license = $license;
8670
- $this->sync_site_license();
8671
- }
8672
- }
8673
-
8674
- $this->switch_to_blog( $current_blog_id );
8675
-
8676
- if ( is_object( $this->_site ) ) {
8677
- // Already connected (with or without a license), so no need to continue.
8678
- return;
8679
- }
8680
- }
8681
-
8682
- if ( $this->is_network_anonymous() ) {
8683
- /**
8684
- * Opt-in was network skipped so automatically skip the opt-in for the new site.
8685
- */
8686
- $this->skip_site_connection( $blog_id );
8687
- } else if ( $this->is_network_delegated_connection() ) {
8688
- /**
8689
- * Opt-in was network delegated so automatically delegate the opt-in for the new site's admin.
8690
- */
8691
- $this->delegate_site_connection( $blog_id );
8692
- } else if ( $this->is_network_connected() ) {
8693
- /**
8694
- * Opt-in was network activated so automatically opt-in with the network user and new site admin.
8695
- */
8696
- $current_blog_id = get_current_blog_id();
8697
-
8698
- $this->switch_to_blog( $blog_id );
8699
-
8700
- // Opt-in with network user.
8701
- $this->install_with_user(
8702
- $this->get_network_user(),
8703
- false,
8704
- false,
8705
- false,
8706
- false
8707
- );
8708
-
8709
- $this->switch_to_blog( $current_blog_id );
8710
- } else {
8711
- /**
8712
- * If the super-admin mixed different options (connect, skip, delegated):
8713
- * a) If at least one site connection was delegated, then automatically delegate connection.
8714
- * 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.
8715
- */
8716
- $has_delegated_site = false;
8717
-
8718
- $sites = self::get_sites();
8719
- foreach ( $sites as $site ) {
8720
- $blog_id = self::get_site_blog_id( $site );
8721
-
8722
- if ( $this->is_site_delegated_connection( $blog_id ) ) {
8723
- $has_delegated_site = true;
8724
- break;
8725
- }
8726
- }
8727
-
8728
- if ( $has_delegated_site ) {
8729
- $this->delegate_site_connection( $blog_id );
8730
- } else {
8731
- $this->skip_site_connection( $blog_id );
8732
- }
8733
- }
8734
- }
8735
-
8736
- /**
8737
- * @author Vova Feldman (@svovaf)
8738
- * @since 2.4.3
8739
- *
8740
- * @param \WP_Site $new_site
8741
- */
8742
- public function _after_wp_insert_site_callback( WP_Site $new_site ) {
8743
- $this->_logger->entrance();
8744
-
8745
- $this->_after_new_blog_callback(
8746
- $new_site->id,
8747
- // Dummy user ID (not in use).
8748
- 0,
8749
- $new_site->domain,
8750
- $new_site->path,
8751
- $new_site->network_id,
8752
- // Dummy meta, not in use.
8753
- array()
8754
- );
8755
- }
8756
-
8757
- /**
8758
- * @author Vova Feldman (@svovaf)
8759
- * @since 1.1.3
8760
- *
8761
- * @param bool|int $network_or_blog_id Since 2.0.0.
8762
- */
8763
- private function reset_anonymous_mode( $network_or_blog_id = 0 ) {
8764
- if ( true === $network_or_blog_id ) {
8765
- unset( $this->_storage->is_anonymous_ms );
8766
- } else {
8767
- $this->_storage->remove( 'is_anonymous', true, $network_or_blog_id );
8768
- }
8769
-
8770
- /**
8771
- * Ensure that this field is also "false", otherwise, if the current module's type is "theme" and the module
8772
- * has no menus, the opt-in popup will not be shown immediately (in this case, the user will have to click
8773
- * on the admin notice that contains the opt-in link in order to trigger the opt-in popup).
8774
- *
8775
- * @author Leo Fajardo (@leorw)
8776
- * @since 1.2.2
8777
- */
8778
- if ( ! $this->_is_network_active ||
8779
- 0 === $network_or_blog_id ||
8780
- get_current_blog_id() == $network_or_blog_id ||
8781
- ( true === $network_or_blog_id && fs_is_network_admin() )
8782
- ) {
8783
- $this->_is_anonymous = null;
8784
- }
8785
- }
8786
-
8787
- /**
8788
- * This is used to ensure that before redirecting to the opt-in page after resetting the anonymous mode or
8789
- * deleting the account in the network level, the URL of the page to redirect to is correct.
8790
- *
8791
- * @author Leo Fajardo (@leorw)
8792
- *
8793
- * @since 2.1.3
8794
- */
8795
- private function maybe_set_slug_and_network_menu_exists_flag() {
8796
- if ( ! empty( $this->_dynamically_added_top_level_page_hook_name ) ) {
8797
- $this->_menu->set_slug_and_network_menu_exists_flag( $this->_menu->has_menu() ?
8798
- $this->_menu->get_slug() :
8799
- $this->_slug
8800
- );
8801
- }
8802
- }
8803
-
8804
- /**
8805
- * Clears the anonymous mode and redirects to the opt-in screen.
8806
- *
8807
- * @author Vova Feldman (@svovaf)
8808
- * @since 1.1.7
8809
- */
8810
- function connect_again() {
8811
- if ( ! $this->is_anonymous() ) {
8812
- return;
8813
- }
8814
-
8815
- $this->reset_anonymous_mode( fs_is_network_admin() );
8816
-
8817
- $this->maybe_set_slug_and_network_menu_exists_flag();
8818
-
8819
- fs_redirect( $this->get_activation_url() );
8820
- }
8821
-
8822
- /**
8823
- * Skip account connect, and set anonymous mode.
8824
- *
8825
- * @author Vova Feldman (@svovaf)
8826
- * @since 1.1.1
8827
- *
8828
- * @param array|null $sites Since 2.0.0. Specific sites.
8829
- * @param bool $skip_all_network Since 2.0.0. If true, skip connection for all sites.
8830
- */
8831
- function skip_connection( $sites = null, $skip_all_network = false ) {
8832
- $this->_logger->entrance();
8833
-
8834
- $this->_admin_notices->remove_sticky( 'connect_account' );
8835
-
8836
- if ( $skip_all_network ) {
8837
- $this->set_anonymous_mode( true, true );
8838
- }
8839
-
8840
- if ( ! $skip_all_network && empty( $sites ) ) {
8841
- $this->skip_site_connection();
8842
- } else {
8843
- $uids = array();
8844
-
8845
- if ( $skip_all_network ) {
8846
- $this->set_anonymous_mode( true, true );
8847
-
8848
- $sites = self::get_sites();
8849
- foreach ( $sites as $site ) {
8850
- $blog_id = self::get_site_blog_id( $site );
8851
- $this->skip_site_connection( $blog_id, false );
8852
- $uids[] = $this->get_anonymous_id( $blog_id );
8853
- }
8854
- } else if ( ! empty( $sites ) ) {
8855
- foreach ( $sites as $site ) {
8856
- $uids[] = $site['uid'];
8857
- $this->skip_site_connection( $site['blog_id'], false );
8858
- }
8859
- }
8860
-
8861
- // Send anonymous skip event.
8862
- // No user identified info nor any tracking will be sent after the user skips the opt-in.
8863
- $this->get_api_plugin_scope()->call( 'skip.json', 'put', array(
8864
- 'uids' => $uids,
8865
- ) );
8866
- }
8867
-
8868
- $this->network_upgrade_mode_completed();
8869
- }
8870
-
8871
- /**
8872
- * Skip connection for specific site in the network.
8873
- *
8874
- * @author Vova Feldman (@svovaf)
8875
- * @since 2.0.0
8876
- *
8877
- * @param int|null $blog_id
8878
- * @param bool $send_skip
8879
- */
8880
- private function skip_site_connection( $blog_id = null, $send_skip = true ) {
8881
- $this->_logger->entrance();
8882
-
8883
- $this->_admin_notices->remove_sticky( 'connect_account', $blog_id );
8884
-
8885
- $this->set_anonymous_mode( true, $blog_id );
8886
-
8887
- if ( $send_skip ) {
8888
- $this->get_api_plugin_scope()->call( 'skip.json', 'put', array(
8889
- 'uids' => array( $this->get_anonymous_id( $blog_id ) ),
8890
- ) );
8891
- }
8892
- }
8893
-
8894
- /**
8895
- * Plugin version update hook.
8896
- *
8897
- * @author Vova Feldman (@svovaf)
8898
- * @since 1.0.4
8899
- */
8900
- private function update_plugin_version_event() {
8901
- $this->_logger->entrance();
8902
-
8903
- if ( ! $this->is_registered() ) {
8904
- return;
8905
- }
8906
-
8907
- $this->schedule_install_sync();
8908
- // $this->sync_install( array(), true );
8909
- }
8910
-
8911
- /**
8912
- * Generate an MD5 signature of a plugins collection.
8913
- * This helper methods used to identify changes in a plugins collection.
8914
- *
8915
- * @author Vova Feldman (@svovaf)
8916
- * @since 2.0.0
8917
- *
8918
- * @param array [string]array $plugins
8919
- *
8920
- * @return string
8921
- */
8922
- private function get_plugins_thumbprint( $plugins ) {
8923
- ksort( $plugins );
8924
-
8925
- $thumbprint = '';
8926
- foreach ( $plugins as $basename => $data ) {
8927
- $thumbprint .= $data['slug'] . ',' .
8928
- $data['Version'] . ',' .
8929
- ( $data['is_active'] ? '1' : '0' ) . ';';
8930
- }
8931
-
8932
- return md5( $thumbprint );
8933
- }
8934
-
8935
- /**
8936
- * Return a list of modified plugins since the last sync.
8937
- *
8938
- * Note:
8939
- * There's no point to store a plugins counter since even if the number of
8940
- * plugins didn't change, we still need to check if the versions are all the
8941
- * same and the activity state is similar.
8942
- *
8943
- * @author Vova Feldman (@svovaf)
8944
- * @since 1.1.8
8945
- *
8946
- * @return array|false
8947
- */
8948
- private function get_plugins_data_for_api() {
8949
- // Alias.
8950
- $site_active_plugins_option_name = 'active_plugins';
8951
- $network_plugins_option_name = 'all_plugins';
8952
-
8953
- /**
8954
- * Collection of all site level active plugins.
8955
- */
8956
- $site_active_plugins_cache = self::$_accounts->get_option( $site_active_plugins_option_name );
8957
-
8958
- if ( ! is_object( $site_active_plugins_cache ) ) {
8959
- $site_active_plugins_cache = (object) array(
8960
- 'timestamp' => '',
8961
- 'md5' => '',
8962
- 'plugins' => array(),
8963
- );
8964
- }
8965
-
8966
- $time = time();
8967
-
8968
- if ( ! empty( $site_active_plugins_cache->timestamp ) &&
8969
- ( $time - $site_active_plugins_cache->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC
8970
- ) {
8971
- // Don't send plugin updates if last update was in the past 5 min.
8972
- return false;
8973
- }
8974
-
8975
- // Write timestamp to lock the logic.
8976
- $site_active_plugins_cache->timestamp = $time;
8977
- self::$_accounts->set_option( $site_active_plugins_option_name, $site_active_plugins_cache, true );
8978
-
8979
- // Reload options from DB.
8980
- self::$_accounts->load( true );
8981
- $site_active_plugins_cache = self::$_accounts->get_option( $site_active_plugins_option_name );
8982
-
8983
- if ( $time != $site_active_plugins_cache->timestamp ) {
8984
- // If timestamp is different, then another thread captured the lock.
8985
- return false;
8986
- }
8987
-
8988
- /**
8989
- * Collection of all plugins (network level).
8990
- */
8991
- $network_plugins_cache = self::$_accounts->get_option( $network_plugins_option_name );
8992
-
8993
- if ( ! is_object( $network_plugins_cache ) ) {
8994
- $network_plugins_cache = (object) array(
8995
- 'timestamp' => '',
8996
- 'md5' => '',
8997
- 'plugins' => array(),
8998
- );
8999
- }
9000
-
9001
- // Check if there's a change in plugins.
9002
- $network_plugins = self::get_network_plugins();
9003
- $site_active_plugins = self::get_site_active_plugins();
9004
-
9005
- $network_plugins_thumbprint = $this->get_plugins_thumbprint( $network_plugins );
9006
- $site_active_plugins_thumbprint = $this->get_plugins_thumbprint( $site_active_plugins );
9007
-
9008
- // Check if plugins status changed (version or active/inactive).
9009
- $network_plugins_changed = ( $network_plugins_cache->md5 !== $network_plugins_thumbprint );
9010
- $site_active_plugins_changed = ( $site_active_plugins_cache->md5 !== $site_active_plugins_thumbprint );
9011
-
9012
- if ( ! $network_plugins_changed &&
9013
- ! $site_active_plugins_changed
9014
- ) {
9015
- // No changes.
9016
- return array();
9017
- }
9018
-
9019
- $plugins_update_data = array();
9020
-
9021
- foreach ( $network_plugins_cache->plugins as $basename => $data ) {
9022
- if ( ! isset( $network_plugins[ $basename ] ) ) {
9023
- // Plugin uninstalled.
9024
- $uninstalled_plugin_data = $data;
9025
- $uninstalled_plugin_data['is_active'] = false;
9026
- $uninstalled_plugin_data['is_uninstalled'] = true;
9027
- $plugins_update_data[] = $uninstalled_plugin_data;
9028
-
9029
- unset( $network_plugins[ $basename ] );
9030
-
9031
- unset( $network_plugins_cache->plugins[ $basename ] );
9032
- unset( $site_active_plugins_cache->plugins[ $basename ] );
9033
-
9034
- continue;
9035
- }
9036
-
9037
- $was_active = $data['is_active'] ||
9038
- ( isset( $site_active_plugins_cache->plugins[ $basename ] ) &&
9039
- true === $site_active_plugins_cache->plugins[ $basename ]['is_active'] );
9040
- $is_active = $network_plugins[ $basename ]['is_active'] ||
9041
- ( isset( $site_active_plugins[ $basename ] ) &&
9042
- $site_active_plugins[ $basename ]['is_active'] );
9043
-
9044
- if ( ! isset( $site_active_plugins_cache->plugins[ $basename ] ) &&
9045
- isset( $site_active_plugins[ $basename ] )
9046
- ) {
9047
- // Plugin was site level activated.
9048
- $site_active_plugins_cache->plugins[ $basename ] = $network_plugins[ $basename ];
9049
- $site_active_plugins_cache->plugins[ $basename ]['is_active'] = true;
9050
- } else if ( isset( $site_active_plugins_cache->plugins[ $basename ] ) &&
9051
- ! isset( $site_active_plugins[ $basename ] )
9052
- ) {
9053
- // Plugin was site level deactivated.
9054
- unset( $site_active_plugins_cache->plugins[ $basename ] );
9055
- }
9056
-
9057
- $prev_version = $data['version'];
9058
- $current_version = $network_plugins[ $basename ]['Version'];
9059
-
9060
- if ( $was_active !== $is_active || $prev_version !== $current_version ) {
9061
- // Plugin activated or deactivated, or version changed.
9062
-
9063
- if ( $was_active !== $is_active ) {
9064
- if ( $data['is_active'] != $network_plugins[ $basename ]['is_active'] ) {
9065
- $network_plugins_cache->plugins[ $basename ]['is_active'] = $data['is_active'];
9066
- }
9067
- }
9068
-
9069
- if ( $prev_version !== $current_version ) {
9070
- $network_plugins_cache->plugins[ $basename ]['Version'] = $current_version;
9071
- }
9072
-
9073
- $updated_plugin_data = $data;
9074
- $updated_plugin_data['is_active'] = $is_active;
9075
- $updated_plugin_data['version'] = $current_version;
9076
- $updated_plugin_data['title'] = $network_plugins[ $basename ]['Name'];
9077
- $plugins_update_data[] = $updated_plugin_data;
9078
- }
9079
- }
9080
-
9081
- // Find new plugins that weren't yet seen before.
9082
- foreach ( $network_plugins as $basename => $data ) {
9083
- if ( ! isset( $network_plugins_cache->plugins[ $basename ] ) ) {
9084
- // New plugin.
9085
- $new_plugin = array(
9086
- 'slug' => $data['slug'],
9087
- 'version' => $data['Version'],
9088
- 'title' => $data['Name'],
9089
- 'is_active' => $data['is_active'],
9090
- 'is_uninstalled' => false,
9091
- );
9092
-
9093
- $network_plugins_cache->plugins[ $basename ] = $new_plugin;
9094
-
9095
- $is_site_level_active = (
9096
- isset( $site_active_plugins[ $basename ] ) &&
9097
- $site_active_plugins[ $basename ]['is_active']
9098
- );
9099
-
9100
- /**
9101
- * If not network active, set the activity status based on the site-level plugin status.
9102
- */
9103
- if ( ! $new_plugin['is_active'] ) {
9104
- $new_plugin['is_active'] = $is_site_level_active;
9105
- }
9106
-
9107
- $plugins_update_data[] = $new_plugin;
9108
-
9109
- if ( isset( $site_active_plugins[ $basename ] ) ) {
9110
- $site_active_plugins_cache->plugins[ $basename ] = $new_plugin;
9111
- $site_active_plugins_cache->plugins[ $basename ]['is_active'] = $is_site_level_active;
9112
- }
9113
- }
9114
- }
9115
-
9116
- $site_active_plugins_cache->md5 = $site_active_plugins_thumbprint;
9117
- $site_active_plugins_cache->timestamp = $time;
9118
- self::$_accounts->set_option( $site_active_plugins_option_name, $site_active_plugins_cache, true );
9119
-
9120
- $network_plugins_cache->md5 = $network_plugins_thumbprint;
9121
- $network_plugins_cache->timestamp = $time;
9122
- self::$_accounts->set_option( $network_plugins_option_name, $network_plugins_cache, true );
9123
-
9124
- return $plugins_update_data;
9125
- }
9126
-
9127
- /**
9128
- * Return a list of modified themes since the last sync.
9129
- *
9130
- * Note:
9131
- * There's no point to store a themes counter since even if the number of
9132
- * themes didn't change, we still need to check if the versions are all the
9133
- * same and the activity state is similar.
9134
- *
9135
- * @author Vova Feldman (@svovaf)
9136
- * @since 1.1.8
9137
- *
9138
- * @return array|false
9139
- */
9140
- private function get_themes_data_for_api() {
9141
- // Alias.
9142
- $option_name = 'all_themes';
9143
-
9144
- $all_cached_themes = self::$_accounts->get_option( $option_name );
9145
-
9146
- if ( ! is_object( $all_cached_themes ) ) {
9147
- $all_cached_themes = (object) array(
9148
- 'timestamp' => '',
9149
- 'md5' => '',
9150
- 'themes' => array(),
9151
- );
9152
- }
9153
-
9154
- $time = time();
9155
-
9156
- if ( ! empty( $all_cached_themes->timestamp ) &&
9157
- ( $time - $all_cached_themes->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC
9158
- ) {
9159
- // Don't send theme updates if last update was in the past 5 min.
9160
- return false;
9161
- }
9162
-
9163
- // Write timestamp to lock the logic.
9164
- $all_cached_themes->timestamp = $time;
9165
- self::$_accounts->set_option( $option_name, $all_cached_themes, true );
9166
-
9167
- // Reload options from DB.
9168
- self::$_accounts->load( true );
9169
- $all_cached_themes = self::$_accounts->get_option( $option_name );
9170
-
9171
- if ( $time != $all_cached_themes->timestamp ) {
9172
- // If timestamp is different, then another thread captured the lock.
9173
- return false;
9174
- }
9175
-
9176
- // Get active theme.
9177
- $active_theme = wp_get_theme();
9178
- $active_theme_stylesheet = $active_theme->get_stylesheet();
9179
-
9180
- // Check if there's a change in themes.
9181
- $all_themes = wp_get_themes();
9182
-
9183
- // Check if themes changed.
9184
- ksort( $all_themes );
9185
-
9186
- $themes_signature = '';
9187
- foreach ( $all_themes as $slug => $data ) {
9188
- $is_active = ( $slug === $active_theme_stylesheet );
9189
- $themes_signature .= $slug . ',' .
9190
- $data->version . ',' .
9191
- ( $is_active ? '1' : '0' ) . ';';
9192
- }
9193
-
9194
- // Check if themes status changed (version or active/inactive).
9195
- $themes_changed = ( $all_cached_themes->md5 !== md5( $themes_signature ) );
9196
-
9197
- $themes_update_data = array();
9198
-
9199
- if ( $themes_changed ) {
9200
- // Change in themes, report changes.
9201
-
9202
- // Update existing themes info.
9203
- foreach ( $all_cached_themes->themes as $slug => $data ) {
9204
- $is_active = ( $slug === $active_theme_stylesheet );
9205
-
9206
- if ( ! isset( $all_themes[ $slug ] ) ) {
9207
- // Plugin uninstalled.
9208
- $uninstalled_theme_data = $data;
9209
- $uninstalled_theme_data['is_active'] = false;
9210
- $uninstalled_theme_data['is_uninstalled'] = true;
9211
- $themes_update_data[] = $uninstalled_theme_data;
9212
-
9213
- unset( $all_themes[ $slug ] );
9214
- unset( $all_cached_themes->themes[ $slug ] );
9215
- } else if ( $data['is_active'] !== $is_active ||
9216
- $data['version'] !== $all_themes[ $slug ]->version
9217
- ) {
9218
- // Plugin activated or deactivated, or version changed.
9219
-
9220
- $all_cached_themes->themes[ $slug ]['is_active'] = $is_active;
9221
- $all_cached_themes->themes[ $slug ]['version'] = $all_themes[ $slug ]->version;
9222
-
9223
- $themes_update_data[] = $all_cached_themes->themes[ $slug ];
9224
- }
9225
- }
9226
-
9227
- // Find new themes that weren't yet seen before.
9228
- foreach ( $all_themes as $slug => $data ) {
9229
- if ( ! isset( $all_cached_themes->themes[ $slug ] ) ) {
9230
- $is_active = ( $slug === $active_theme_stylesheet );
9231
-
9232
- // New plugin.
9233
- $new_plugin = array(
9234
- 'slug' => $slug,
9235
- 'version' => $data->version,
9236
- 'title' => $data->name,
9237
- 'is_active' => $is_active,
9238
- 'is_uninstalled' => false,
9239
- );
9240
-
9241
- $themes_update_data[] = $new_plugin;
9242
- $all_cached_themes->themes[ $slug ] = $new_plugin;
9243
- }
9244
- }
9245
-
9246
- $all_cached_themes->md5 = md5( $themes_signature );
9247
- $all_cached_themes->timestamp = time();
9248
- self::$_accounts->set_option( $option_name, $all_cached_themes, true );
9249
- }
9250
-
9251
- return $themes_update_data;
9252
- }
9253
-
9254
- /**
9255
- * Get site data for API install request.
9256
- *
9257
- * @author Vova Feldman (@svovaf)
9258
- * @since 1.1.2
9259
- *
9260
- * @param string[] $override
9261
- * @param bool $include_plugins Since 1.1.8 by default include plugin changes.
9262
- * @param bool $include_themes Since 1.1.8 by default include plugin changes.
9263
- * @param bool $include_blog_data Since 2.3.0 by default include the current blog's data (language, charset, title, and URL).
9264
- *
9265
- * @return array
9266
- */
9267
- private function get_install_data_for_api(
9268
- array $override,
9269
- $include_plugins = true,
9270
- $include_themes = true,
9271
- $include_blog_data = true
9272
- ) {
9273
- if ( $this->is_extensions_tracking_allowed() ) {
9274
- if ( ! defined( 'WP_FS__TRACK_PLUGINS' ) || false !== WP_FS__TRACK_PLUGINS ) {
9275
- /**
9276
- * @since 1.1.8 Also send plugin updates.
9277
- */
9278
- if ( $include_plugins && ! isset( $override['plugins'] ) ) {
9279
- $plugins = $this->get_plugins_data_for_api();
9280
- if ( ! empty( $plugins ) ) {
9281
- $override['plugins'] = $plugins;
9282
- }
9283
- }
9284
- }
9285
-
9286
- if ( ! defined( 'WP_FS__TRACK_THEMES' ) || false !== WP_FS__TRACK_THEMES ) {
9287
- /**
9288
- * @since 1.1.8 Also send themes updates.
9289
- */
9290
- if ( $include_themes && ! isset( $override['themes'] ) ) {
9291
- $themes = $this->get_themes_data_for_api();
9292
- if ( ! empty( $themes ) ) {
9293
- $override['themes'] = $themes;
9294
- }
9295
- }
9296
- }
9297
- }
9298
-
9299
- $versions = $this->get_versions();
9300
-
9301
- $blog_data = $include_blog_data ?
9302
- array(
9303
- 'language' => get_bloginfo( 'language' ),
9304
- 'charset' => get_bloginfo( 'charset' ),
9305
- 'title' => get_bloginfo( 'name' ),
9306
- 'url' => get_site_url(),
9307
- ) :
9308
- array();
9309
-
9310
- return array_merge( $versions, $blog_data, array(
9311
- 'version' => $this->get_plugin_version(),
9312
- 'is_premium' => $this->is_premium(),
9313
- // Special params.
9314
- 'is_active' => true,
9315
- 'is_disconnected' => $this->is_tracking_prohibited(),
9316
- 'is_uninstalled' => false,
9317
- ), $override );
9318
- }
9319
-
9320
- /**
9321
- * Update installs details.
9322
- *
9323
- * @todo V1 of multiste network support doesn't support plugin and theme data sending.
9324
- *
9325
- * @author Vova Feldman (@svovaf)
9326
- * @since 2.0.0
9327
- *
9328
- * @param string[] string $override
9329
- * @param bool $only_diff
9330
- * @param bool $include_plugins Since 1.1.8 by default include plugin changes.
9331
- * @param bool $include_themes Since 1.1.8 by default include plugin changes.
9332
- *
9333
- * @return array
9334
- */
9335
- private function get_installs_data_for_api(
9336
- array $override,
9337
- $only_diff = false,
9338
- $include_plugins = true,
9339
- $include_themes = true
9340
- ) {
9341
- /**
9342
- * @since 1.1.8 Also send plugin updates.
9343
- */
9344
- // if ( $include_plugins && ! isset( $override['plugins'] ) ) {
9345
- // $plugins = $this->get_plugins_data_for_api();
9346
- // if ( ! empty( $plugins ) ) {
9347
- // $override['plugins'] = $plugins;
9348
- // }
9349
- // }
9350
- /**
9351
- * @since 1.1.8 Also send themes updates.
9352
- */
9353
- // if ( $include_themes && ! isset( $override['themes'] ) ) {
9354
- // $themes = $this->get_themes_data_for_api();
9355
- // if ( ! empty( $themes ) ) {
9356
- // $override['themes'] = $themes;
9357
- // }
9358
- // }
9359
-
9360
- // Common properties.
9361
- $versions = $this->get_versions();
9362
- $common = array_merge( $versions, array(
9363
- 'version' => $this->get_plugin_version(),
9364
- 'is_premium' => $this->is_premium(),
9365
- ), $override );
9366
-
9367
-
9368
- $is_common_diff_for_any_site = false;
9369
- $common_diff_union = array();
9370
-
9371
- $installs_data = array();
9372
-
9373
- $sites = self::get_sites();
9374
-
9375
- foreach ( $sites as $site ) {
9376
- $blog_id = self::get_site_blog_id( $site );
9377
-
9378
- $install = $this->get_install_by_blog_id( $blog_id );
9379
-
9380
- if ( is_object( $install ) ) {
9381
- if ( $install->user_id != $this->_user->id ) {
9382
- // Install belongs to a different owner.
9383
- continue;
9384
- }
9385
-
9386
- if ( ! $this->is_premium() && $install->is_tracking_prohibited() ) {
9387
- // Don't send updates regarding opted-out installs.
9388
- continue;
9389
- }
9390
-
9391
- $install_data = $this->get_site_info( $site );
9392
-
9393
- $uid = $install_data['uid'];
9394
-
9395
- unset( $install_data['blog_id'] );
9396
- unset( $install_data['uid'] );
9397
-
9398
- $install_data['is_disconnected'] = $install->is_disconnected;
9399
- $install_data['is_active'] = $this->is_active_for_site( $blog_id );
9400
- $install_data['is_uninstalled'] = $install->is_uninstalled;
9401
-
9402
- $common_diff = null;
9403
- $is_common_diff = false;
9404
- if ( $only_diff ) {
9405
- $install_data = $this->get_install_diff_for_api( $install_data, $install, $override );
9406
- $common_diff = $this->get_install_diff_for_api( $common, $install, $override );
9407
-
9408
- $is_common_diff = ! empty( $common_diff );
9409
-
9410
- if ( $is_common_diff ) {
9411
- foreach ( $common_diff as $k => $v ) {
9412
- if ( ! isset( $common_diff_union[ $k ] ) ) {
9413
- $common_diff_union[ $k ] = $v;
9414
- }
9415
- }
9416
- }
9417
-
9418
- $is_common_diff_for_any_site = $is_common_diff_for_any_site || $is_common_diff;
9419
- }
9420
-
9421
- if ( ! empty( $install_data ) || $is_common_diff ) {
9422
- // Add install ID and site unique ID.
9423
- $install_data['id'] = $install->id;
9424
- $install_data['uid'] = $uid;
9425
-
9426
- $installs_data[] = $install_data;
9427
- }
9428
- }
9429
- }
9430
-
9431
- restore_current_blog();
9432
-
9433
- if ( 0 < count( $installs_data ) && ( $is_common_diff_for_any_site || ! $only_diff ) ) {
9434
- if ( ! $only_diff ) {
9435
- $installs_data[] = $common;
9436
- } else if ( ! empty( $common_diff_union ) ) {
9437
- $installs_data[] = $common_diff_union;
9438
- }
9439
- }
9440
-
9441
- foreach ( $installs_data as &$data ) {
9442
- $data = (object) $data;
9443
- }
9444
-
9445
- return $installs_data;
9446
- }
9447
-
9448
- /**
9449
- * Compare site actual data to the stored install data and return the differences for an API data sync.
9450
- *
9451
- * @author Vova Feldman (@svovaf)
9452
- * @since 2.0.0
9453
- *
9454
- * @param array $site
9455
- * @param FS_Site $install
9456
- * @param string[] string $override
9457
- *
9458
- * @return array
9459
- */
9460
- private function get_install_diff_for_api( $site, $install, $override = array() ) {
9461
- $diff = array();
9462
- $special = array();
9463
- $special_override = false;
9464
-
9465
- foreach ( $site as $p => $v ) {
9466
- if ( property_exists( $install, $p ) ) {
9467
- if ( ( is_bool( $install->{$p} ) || ! empty( $install->{$p} ) ) &&
9468
- $install->{$p} != $v
9469
- ) {
9470
- $install->{$p} = $v;
9471
- $diff[ $p ] = $v;
9472
- }
9473
- } else {
9474
- $special[ $p ] = $v;
9475
-
9476
- if ( isset( $override[ $p ] ) ||
9477
- 'plugins' === $p ||
9478
- 'themes' === $p
9479
- ) {
9480
- $special_override = true;
9481
- }
9482
- }
9483
- }
9484
-
9485
- if ( $special_override || 0 < count( $diff ) ) {
9486
- // Add special params only if has at least one
9487
- // standard param, or if explicitly requested to
9488
- // override a special param or a param which is not exist
9489
- // in the install object.
9490
- $diff = array_merge( $diff, $special );
9491
- }
9492
-
9493
- return $diff;
9494
- }
9495
-
9496
- /**
9497
- * Update install only if changed.
9498
- *
9499
- * @author Vova Feldman (@svovaf)
9500
- * @since 1.0.9
9501
- *
9502
- * @param string[] string $override
9503
- * @param bool $flush
9504
- *
9505
- * @return false|object|string
9506
- */
9507
- private function send_install_update( $override = array(), $flush = false ) {
9508
- $this->_logger->entrance();
9509
-
9510
- $check_properties = $this->get_install_data_for_api( $override );
9511
-
9512
- if ( $flush ) {
9513
- $params = $check_properties;
9514
- } else {
9515
- $params = $this->get_install_diff_for_api( $check_properties, $this->_site, $override );
9516
- }
9517
-
9518
- $keepalive_only_update = false;
9519
- if ( empty( $params ) ) {
9520
- $keepalive_only_update = $this->should_send_keepalive_update();
9521
-
9522
- if ( ! $keepalive_only_update ) {
9523
- /**
9524
- * There are no updates to send including keepalive.
9525
- *
9526
- * @author Leo Fajardo (@leorw)
9527
- * @since 2.2.3
9528
- */
9529
- return false;
9530
- }
9531
- }
9532
-
9533
- if ( ! $keepalive_only_update ) {
9534
- /**
9535
- * Do not update the last install sync timestamp after a keepalive-only call since there were no actual
9536
- * updates sent.
9537
- *
9538
- * @author Leo Fajardo (@leorw)
9539
- * @since 2.2.3
9540
- */
9541
- if ( ! is_multisite() ) {
9542
- // Update last install sync timestamp.
9543
- $this->set_cron_execution_timestamp( 'install_sync' );
9544
- }
9545
-
9546
- $params['uid'] = $this->get_anonymous_id();
9547
- }
9548
-
9549
- $this->set_keepalive_timestamp();
9550
-
9551
- // Send updated values to FS.
9552
- $site = $this->get_api_site_scope()->call( '/', 'put', $params );
9553
-
9554
- if ( ! $keepalive_only_update && $this->is_api_result_entity( $site ) ) {
9555
- /**
9556
- * Do not clear scheduled sync after a keepalive-only call since there were no actual updates sent.
9557
- *
9558
- * @author Leo Fajardo (@leorw)
9559
- * @since 2.2.3
9560
- */
9561
- if ( ! is_multisite() ) {
9562
- // I successfully sent install update, clear scheduled sync if exist.
9563
- $this->clear_install_sync_cron();
9564
- }
9565
- }
9566
-
9567
- return $site;
9568
- }
9569
-
9570
- /**
9571
- * Update installs only if changed.
9572
- *
9573
- * @author Vova Feldman (@svovaf)
9574
- * @since 2.0.0
9575
- *
9576
- * @param string[] string $override
9577
- * @param bool $flush
9578
- *
9579
- * @return false|object|string
9580
- */
9581
- private function send_installs_update( $override = array(), $flush = false ) {
9582
- $this->_logger->entrance();
9583
-
9584
- $installs_data = $this->get_installs_data_for_api( $override, ! $flush );
9585
-
9586
- $keepalive_only_update = false;
9587
- if ( empty( $installs_data ) ) {
9588
- /**
9589
- * Pass `true` to use the network level storage since the update is for many installs.
9590
- *
9591
- * @author Leo Fajardo (@leorw)
9592
- * @since 2.2.3
9593
- */
9594
- $keepalive_only_update = $this->should_send_keepalive_update( true );
9595
-
9596
- if ( ! $keepalive_only_update ) {
9597
- /**
9598
- * There are no updates to send including keepalive.
9599
- *
9600
- * @author Leo Fajardo (@leorw)
9601
- * @since 2.2.3
9602
- */
9603
- return false;
9604
- }
9605
- }
9606
-
9607
- if ( ! $keepalive_only_update ) {
9608
- // Update last install sync timestamp if there were actual updates sent (i.e., not a keepalive-only call).
9609
- $this->set_cron_execution_timestamp( 'install_sync' );
9610
- }
9611
-
9612
- /**
9613
- * Pass `true` to use the network level storage since the update is for many installs.
9614
- *
9615
- * @author Leo Fajardo (@leorw)
9616
- * @since 2.2.3
9617
- */
9618
- $this->set_keepalive_timestamp( true );
9619
-
9620
- // Send updated values to FS.
9621
- $result = $this->get_api_user_scope()->call( "/plugins/{$this->_plugin->id}/installs.json", 'put', $installs_data );
9622
-
9623
- if ( ! $keepalive_only_update && $this->is_api_result_object( $result, 'installs' ) ) {
9624
- // I successfully sent installs update (there was an actual update sent and it's not just a keepalive-only call), clear scheduled sync if exist.
9625
- $this->clear_install_sync_cron();
9626
- }
9627
-
9628
- return $result;
9629
- }
9630
-
9631
- /**
9632
- * @author Leo Fajardo (@leorw)
9633
- *
9634
- * @param bool|null $use_network_level_storage
9635
- *
9636
- * @return bool
9637
- */
9638
- private function should_send_keepalive_update( $use_network_level_storage = null ) {
9639
- $keepalive_timestamp = $this->_storage->get( 'keepalive_timestamp', 0, $use_network_level_storage );
9640
-
9641
- if ( $keepalive_timestamp < ( time() - WP_FS__TIME_WEEK_IN_SEC ) ) {
9642
- // If updated more than 7 days ago, trigger a keepalive and update the time it was triggered.
9643
- return true;
9644
- } else {
9645
- // If updated 7 days ago or less, "flip a coin", if the value is 7 trigger a keepalive and update the last time it was triggered.
9646
- return ( 7 == rand( 1, 7 ) );
9647
- }
9648
- }
9649
-
9650
- /**
9651
- * Syncs the install owner's data if needed (i.e., if the install owner is different from the loaded user).
9652
- *
9653
- * @author Leo Fajardo (@leorw)
9654
- * @since 2.3.2
9655
- */
9656
- private function maybe_sync_install_user() {
9657
- if ( $this->_user->id == $this->_site->user_id ) {
9658
- return;
9659
- }
9660
-
9661
- // Fetch user data and store if found.
9662
- $this->sync_user_by_current_install();
9663
- }
9664
-
9665
- /**
9666
- * Update install only if changed.
9667
- *
9668
- * @author Vova Feldman (@svovaf)
9669
- * @since 1.0.9
9670
- *
9671
- * @param string[] string $override
9672
- * @param bool $flush
9673
- */
9674
- private function sync_install( $override = array(), $flush = false ) {
9675
- $this->_logger->entrance();
9676
-
9677
- $site = $this->send_install_update( $override, $flush );
9678
-
9679
- if ( false === $site ) {
9680
- // No sync required.
9681
- return;
9682
- }
9683
-
9684
- if ( ! $this->is_api_result_entity( $site ) ) {
9685
- // Failed to sync, don't update locally.
9686
- return;
9687
- }
9688
-
9689
- $this->_site = new FS_Site( $site );
9690
-
9691
- $this->_store_site( true );
9692
- }
9693
-
9694
- /**
9695
- * Update install only if changed.
9696
- *
9697
- * @author Vova Feldman (@svovaf)
9698
- * @since 1.0.9
9699
- *
9700
- * @param string[] string $override
9701
- * @param bool $flush
9702
- */
9703
- private function sync_installs( $override = array(), $flush = false ) {
9704
- $this->_logger->entrance();
9705
-
9706
- $result = $this->send_installs_update( $override, $flush );
9707
-
9708
- if ( false === $result ) {
9709
- // No sync required.
9710
- return;
9711
- }
9712
-
9713
- if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
9714
- // Failed to sync, don't update locally.
9715
- return;
9716
- }
9717
-
9718
- $address_to_blog_map = $this->get_address_to_blog_map();
9719
-
9720
- foreach ( $result->installs as $install ) {
9721
- $this->_site = new FS_Site( $install );
9722
-
9723
- $address = trailingslashit( fs_strip_url_protocol( $install->url ) );
9724
- $blog_id = $address_to_blog_map[ $address ];
9725
-
9726
- $this->_store_site( true, $blog_id );
9727
- }
9728
- }
9729
-
9730
- /**
9731
- * Track install's custom event.
9732
- *
9733
- * IMPORTANT:
9734
- * Custom event tracking is currently only supported for specific clients.
9735
- * If you are not one of them, please don't use this method. If you will,
9736
- * the API will simply ignore your request based on the plugin ID.
9737
- *
9738
- * Need custom tracking for your plugin or theme?
9739
- * If you are interested in custom event tracking please contact yo@freemius.com
9740
- * for further details.
9741
- *
9742
- * @author Vova Feldman (@svovaf)
9743
- * @since 1.2.1
9744
- *
9745
- * @param string $name Event name.
9746
- * @param array $properties Associative key/value array with primitive values only
9747
- * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s.
9748
- * @param bool $once If true, event will be tracked only once. IMPORTANT: Still trigger the API call.
9749
- *
9750
- * @return object|false Event data or FALSE on failure.
9751
- *
9752
- * @throws \Freemius_InvalidArgumentException
9753
- */
9754
- public function track_event( $name, $properties = array(), $process_at = false, $once = false ) {
9755
- $this->_logger->entrance( http_build_query( array( 'name' => $name, 'once' => $once ) ) );
9756
-
9757
- if ( ! $this->is_registered() ) {
9758
- return false;
9759
- }
9760
-
9761
- $event = array( 'type' => $name );
9762
-
9763
- if ( is_numeric( $process_at ) && $process_at > time() ) {
9764
- $event['process_at'] = $process_at;
9765
- }
9766
-
9767
- if ( $once ) {
9768
- $event['once'] = true;
9769
- }
9770
-
9771
- if ( ! empty( $properties ) ) {
9772
- // Verify associative array values are primitive.
9773
- foreach ( $properties as $k => $v ) {
9774
- if ( ! is_scalar( $v ) ) {
9775
- throw new Freemius_InvalidArgumentException( 'The $properties argument must be an associative key/value array with primitive values only.' );
9776
- }
9777
- }
9778
-
9779
- $event['properties'] = $properties;
9780
- }
9781
-
9782
- $result = $this->get_api_site_scope()->call( 'events.json', 'post', $event );
9783
-
9784
- return $this->is_api_error( $result ) ?
9785
- false :
9786
- $result;
9787
- }
9788
-
9789
- /**
9790
- * Track install's custom event only once, but it still triggers the API call.
9791
- *
9792
- * IMPORTANT:
9793
- * Custom event tracking is currently only supported for specific clients.
9794
- * If you are not one of them, please don't use this method. If you will,
9795
- * the API will simply ignore your request based on the plugin ID.
9796
- *
9797
- * Need custom tracking for your plugin or theme?
9798
- * If you are interested in custom event tracking please contact yo@freemius.com
9799
- * for further details.
9800
- *
9801
- * @author Vova Feldman (@svovaf)
9802
- * @since 1.2.1
9803
- *
9804
- * @param string $name Event name.
9805
- * @param array $properties Associative key/value array with primitive values only
9806
- * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s.
9807
- *
9808
- * @return object|false Event data or FALSE on failure.
9809
- *
9810
- * @throws \Freemius_InvalidArgumentException
9811
- *
9812
- * @user Freemius::track_event()
9813
- */
9814
- public function track_event_once( $name, $properties = array(), $process_at = false ) {
9815
- return $this->track_event( $name, $properties, $process_at, true );
9816
- }
9817
-
9818
- /**
9819
- * Plugin uninstall hook.
9820
- *
9821
- * @author Vova Feldman (@svovaf)
9822
- * @since 1.0.1
9823
- *
9824
- * @param bool $check_user Enforce checking if user have plugins activation privileges.
9825
- */
9826
- function _uninstall_plugin_event( $check_user = true ) {
9827
- $this->_logger->entrance( 'slug = ' . $this->_slug );
9828
-
9829
- if ( $check_user && ! current_user_can( 'activate_plugins' ) ) {
9830
- return;
9831
- }
9832
-
9833
- $params = array();
9834
- $uninstall_reason = null;
9835
- if ( isset( $this->_storage->uninstall_reason ) ) {
9836
- $uninstall_reason = $this->_storage->uninstall_reason;
9837
- $params['reason_id'] = $uninstall_reason->id;
9838
- $params['reason_info'] = $uninstall_reason->info;
9839
- }
9840
-
9841
- if ( ! $this->is_registered() ) {
9842
- // Send anonymous uninstall event only if user submitted a feedback.
9843
- if ( isset( $uninstall_reason ) ) {
9844
- if ( isset( $uninstall_reason->is_anonymous ) && ! $uninstall_reason->is_anonymous ) {
9845
- $this->opt_in( false, false, false, false, true );
9846
- } else {
9847
- $params['uid'] = $this->get_anonymous_id();
9848
- $this->get_api_plugin_scope()->call( 'uninstall.json', 'put', $params );
9849
- }
9850
- }
9851
- } else {
9852
- $params = array_merge( $params, array(
9853
- 'is_active' => false,
9854
- 'is_uninstalled' => true,
9855
- ) );
9856
-
9857
- if ( $this->_is_network_active ) {
9858
- // Send uninstall event.
9859
- $this->send_installs_update( $params );
9860
- } else {
9861
- // Send uninstall event.
9862
- $this->send_install_update( $params );
9863
- }
9864
- }
9865
-
9866
- // @todo Decide if we want to delete plugin information from db.
9867
- }
9868
-
9869
- /**
9870
- * Set the basename of the current product and hook _activate_plugin_event_hook() to the activation action.
9871
- *
9872
- * @author Vova Feldman (@svovaf)
9873
- * @since 2.2.1
9874
- *
9875
- * @param string $is_premium
9876
- * @param string $caller
9877
- *
9878
- * @return string
9879
- */
9880
- function set_basename( $is_premium, $caller ) {
9881
- $basename = plugin_basename( $caller );
9882
-
9883
- $current_basename = $is_premium ?
9884
- $this->_premium_plugin_basename :
9885
- $this->_free_plugin_basename;
9886
-
9887
- if ( $current_basename == $basename ) {
9888
- // Basename value set correctly.
9889
- return;
9890
- }
9891
-
9892
- if ( $is_premium ) {
9893
- $this->_premium_plugin_basename = $basename;
9894
- } else {
9895
- $this->_free_plugin_basename = $basename;
9896
- }
9897
-
9898
- $plugin_dir = dirname( $this->_plugin_dir_path ) . '/';
9899
-
9900
- register_activation_hook(
9901
- $plugin_dir . $basename,
9902
- array( $this, '_activate_plugin_event_hook' )
9903
- );
9904
- }
9905
-
9906
- /**
9907
- * @author Vova Feldman (@svovaf)
9908
- * @since 1.1.1
9909
- * @since 2.2.1 If the context product is in its premium version, use the current module's basename, even if it was renamed.
9910
- *
9911
- * @return string
9912
- */
9913
- function premium_plugin_basename() {
9914
- if ( ! isset( $this->_premium_plugin_basename ) ) {
9915
- $this->_premium_plugin_basename = $this->is_premium() ?
9916
- // The product is premium, so use the current basename.
9917
- $this->_plugin_basename :
9918
- $this->get_premium_slug() . '/' . basename( $this->_free_plugin_basename );
9919
- }
9920
-
9921
- return $this->_premium_plugin_basename;
9922
- }
9923
-
9924
- /**
9925
- * Uninstall plugin hook. Called only when connected his account with Freemius for active sites tracking.
9926
- *
9927
- * @author Vova Feldman (@svovaf)
9928
- * @since 1.0.2
9929
- */
9930
- public static function _uninstall_plugin_hook() {
9931
- self::_load_required_static();
9932
-
9933
- self::$_static_logger->entrance();
9934
-
9935
- if ( ! current_user_can( 'activate_plugins' ) ) {
9936
- return;
9937
- }
9938
-
9939
- $plugin_file = substr( current_filter(), strlen( 'uninstall_' ) );
9940
-
9941
- self::$_static_logger->info( 'plugin = ' . $plugin_file );
9942
-
9943
- define( 'WP_FS__UNINSTALL_MODE', true );
9944
-
9945
- $fs = self::get_instance_by_file( $plugin_file );
9946
-
9947
- if ( is_object( $fs ) ) {
9948
- $fs->remove_sdk_reference();
9949
-
9950
- self::require_plugin_essentials();
9951
-
9952
- if ( is_plugin_active( $fs->_free_plugin_basename ) ||
9953
- is_plugin_active( $fs->premium_plugin_basename() )
9954
- ) {
9955
- // Deleting Free or Premium plugin version while the other version still installed.
9956
- return;
9957
- }
9958
-
9959
- $fs->_uninstall_plugin_event();
9960
-
9961
- $fs->do_action( 'after_uninstall' );
9962
- }
9963
- }
9964
-
9965
- #----------------------------------------------------------------------------------
9966
- #region Plugin Information
9967
- #----------------------------------------------------------------------------------
9968
-
9969
- /**
9970
- * Load WordPress core plugin.php essential module.
9971
- *
9972
- * @author Vova Feldman (@svovaf)
9973
- * @since 1.1.1
9974
- */
9975
- private static function require_plugin_essentials() {
9976
- if ( ! function_exists( 'get_plugins' ) ) {
9977
- self::$_static_logger->log( 'Including wp-admin/includes/plugin.php...' );
9978
-
9979
- require_once ABSPATH . 'wp-admin/includes/plugin.php';
9980
- }
9981
- }
9982
-
9983
- /**
9984
- * Load WordPress core pluggable.php module.
9985
- *
9986
- * @author Vova Feldman (@svovaf)
9987
- * @since 1.1.2
9988
- */
9989
- private static function require_pluggable_essentials() {
9990
- if ( ! function_exists( 'wp_get_current_user' ) ) {
9991
- require_once ABSPATH . 'wp-includes/pluggable.php';
9992
- }
9993
- }
9994
-
9995
- /**
9996
- * Return plugin data.
9997
- *
9998
- * @author Vova Feldman (@svovaf)
9999
- * @since 1.0.1
10000
- *
10001
- * @param bool $reparse_plugin_metadata
10002
- *
10003
- * @return array
10004
- */
10005
- function get_plugin_data( $reparse_plugin_metadata = false ) {
10006
- if ( ! isset( $this->_plugin_data ) || $reparse_plugin_metadata ) {
10007
- self::require_plugin_essentials();
10008
-
10009
- if ( $this->is_plugin() ) {
10010
- /**
10011
- * @author Vova Feldman (@svovaf)
10012
- * @since 1.2.0 When using get_plugin_data() do NOT translate plugin data.
10013
- *
10014
- * @link https://github.com/Freemius/wordpress-sdk/issues/77
10015
- */
10016
- $plugin_data = get_plugin_data(
10017
- $this->_plugin_main_file_path,
10018
- false,
10019
- false
10020
- );
10021
- } else {
10022
- $theme_data = wp_get_theme();
10023
-
10024
- if ( $this->_plugin_basename !== $theme_data->get_stylesheet() && is_child_theme() ) {
10025
- $parent_theme = $theme_data->parent();
10026
-
10027
- if ( ( $parent_theme instanceof WP_Theme ) && $this->_plugin_basename === $parent_theme->get_stylesheet() ) {
10028
- $theme_data = $parent_theme;
10029
- }
10030
- }
10031
-
10032
- $plugin_data = array(
10033
- 'Name' => $theme_data->get( 'Name' ),
10034
- 'Version' => $theme_data->get( 'Version' ),
10035
- 'Author' => $theme_data->get( 'Author' ),
10036
- 'Description' => $theme_data->get( 'Description' ),
10037
- 'PluginURI' => $theme_data->get( 'ThemeURI' ),
10038
- );
10039
- }
10040
-
10041
- $this->_plugin_data = $plugin_data;
10042
- }
10043
-
10044
- return $this->_plugin_data;
10045
- }
10046
-
10047
- /**
10048
- * @author Vova Feldman (@svovaf)
10049
- * @since 1.0.1
10050
- * @since 1.2.2.5 If slug not set load slug by module ID.
10051
- *
10052
- * @return string Plugin slug.
10053
- */
10054
- function get_slug() {
10055
- if ( ! isset( $this->_slug ) ) {
10056
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
10057
- $this->_slug = $id_slug_type_path_map[ $this->_module_id ]['slug'];
10058
- }
10059
-
10060
- return $this->_slug;
10061
- }
10062
-
10063
- /**
10064
- * @author Leo Fajardo (@leorw)
10065
- * @since 2.2.1
10066
- *
10067
- * @return string
10068
- */
10069
- function get_premium_slug() {
10070
- return is_object( $this->_plugin ) ?
10071
- $this->_plugin->premium_slug :
10072
- "{$this->_slug}-premium";
10073
- }
10074
-
10075
- /**
10076
- * Retrieve the desired folder name for the product.
10077
- *
10078
- * @author Vova Feldman (@svovaf)
10079
- * @since 1.2.1.7
10080
- *
10081
- * @return string Plugin slug.
10082
- */
10083
- function get_target_folder_name() {
10084
- return $this->can_use_premium_code() ?
10085
- $this->_plugin->premium_slug :
10086
- $this->_slug;
10087
- }
10088
-
10089
- /**
10090
- * @author Vova Feldman (@svovaf)
10091
- * @since 1.0.1
10092
- *
10093
- * @return number Plugin ID.
10094
- */
10095
- function get_id() {
10096
- return $this->_plugin->id;
10097
- }
10098
-
10099
- /**
10100
- * @author Leo Fajardo (@leorw)
10101
- * @since 2.2.4
10102
- *
10103
- * @return number|null Bundle ID.
10104
- */
10105
- function get_bundle_id() {
10106
- return ( isset( $this->_plugin->bundle_id ) && FS_Plugin::is_valid_id( $this->_plugin->bundle_id ) ) ?
10107
- $this->_plugin->bundle_id :
10108
- null;
10109
- }
10110
-
10111
- /**
10112
- * @author Vova Feldman (@svovaf)
10113
- * @since 2.3.1
10114
- *
10115
- * @return string|null Bundle public key.
10116
- */
10117
- function get_bundle_public_key() {
10118
- return isset( $this->_plugin->bundle_public_key ) ?
10119
- $this->_plugin->bundle_public_key :
10120
- null;
10121
- }
10122
-
10123
- /**
10124
- * @author Vova Feldman (@svovaf)
10125
- * @since 1.2.1.5
10126
- *
10127
- * @return string Freemius SDK version
10128
- */
10129
- function get_sdk_version() {
10130
- return $this->version;
10131
- }
10132
-
10133
- /**
10134
- * @author Vova Feldman (@svovaf)
10135
- * @since 1.2.1.5
10136
- *
10137
- * @return number Parent plugin ID (if parent exist).
10138
- */
10139
- function get_parent_id() {
10140
- return $this->is_addon() ?
10141
- $this->get_parent_instance()->get_id() :
10142
- $this->_plugin->id;
10143
- }
10144
-
10145
- /**
10146
- * @author Vova Feldman (@svovaf)
10147
- * @since 2.3.1
10148
- *
10149
- * @return string
10150
- */
10151
- function get_usage_tracking_terms_url() {
10152
- return $this->apply_filters(
10153
- 'usage_tracking_terms_url',
10154
- "https://freemius.com/wordpress/usage-tracking/{$this->_plugin->id}/{$this->_slug}/"
10155
- );
10156
- }
10157
-
10158
- /**
10159
- * @author Vova Feldman (@svovaf)
10160
- * @since 2.3.1
10161
- *
10162
- * @return string
10163
- */
10164
- function get_eula_url() {
10165
- return $this->apply_filters(
10166
- 'eula_url',
10167
- "https://freemius.com/terms/{$this->_plugin->id}/{$this->_slug}/"
10168
- );
10169
- }
10170
-
10171
- /**
10172
- * @author Vova Feldman (@svovaf)
10173
- * @since 1.0.1
10174
- *
10175
- * @return string Plugin public key.
10176
- */
10177
- function get_public_key() {
10178
- return $this->_plugin->public_key;
10179
- }
10180
-
10181
- /**
10182
- * Will be available only on sandbox mode.
10183
- *
10184
- * @author Vova Feldman (@svovaf)
10185
- * @since 1.0.4
10186
- *
10187
- * @return mixed Plugin secret key.
10188
- */
10189
- function get_secret_key() {
10190
- return $this->_plugin->secret_key;
10191
- }
10192
-
10193
- /**
10194
- * @author Vova Feldman (@svovaf)
10195
- * @since 1.1.1
10196
- *
10197
- * @return bool
10198
- */
10199
- function has_secret_key() {
10200
- return ! empty( $this->_plugin->secret_key );
10201
- }
10202
-
10203
- /**
10204
- * @author Vova Feldman (@svovaf)
10205
- * @since 1.0.9
10206
- *
10207
- * @param string|bool $premium_suffix
10208
- *
10209
- * @return string
10210
- */
10211
- function get_plugin_name( $premium_suffix = false ) {
10212
- $this->_logger->entrance();
10213
-
10214
- /**
10215
- * This `if-else` can be squeezed into a single `if` but I intentionally split it for code readability.
10216
- *
10217
- * @author Vova Feldman
10218
- */
10219
- if ( ! isset( $this->_plugin_name ) ) {
10220
- // Name is not yet set.
10221
- $this->set_name( $premium_suffix );
10222
- } else if (
10223
- ! empty( $premium_suffix ) &&
10224
- ( ! is_object( $this->_plugin ) || $this->_plugin->premium_suffix !== $premium_suffix )
10225
- ) {
10226
- // Name is already set, but there's a change in the premium suffix.
10227
- $this->set_name( $premium_suffix );
10228
- }
10229
-
10230
- return $this->_plugin_name;
10231
- }
10232
-
10233
- /**
10234
- * Calculates and stores the product's name. This helper function was created specifically for get_plugin_name() just to make the code clearer.
10235
- *
10236
- * @author Vova Feldman (@svovaf)
10237
- * @since 2.2.1
10238
- *
10239
- * @param string $premium_suffix
10240
- */
10241
- private function set_name( $premium_suffix = '' ) {
10242
- $plugin_data = $this->get_plugin_data();
10243
-
10244
- // Get name.
10245
- $this->_plugin_name = $plugin_data['Name'];
10246
-
10247
- if ( is_string( $premium_suffix ) ) {
10248
- $premium_suffix = trim( $premium_suffix );
10249
-
10250
- if ( ! empty( $premium_suffix ) ) {
10251
- // Check if plugin name contains " (premium)" or a custom suffix and remove it.
10252
- $suffix = ( ' ' . strtolower( $premium_suffix ) );
10253
- $suffix_len = strlen( $suffix );
10254
-
10255
- if ( strlen( $plugin_data['Name'] ) > $suffix_len &&
10256
- $suffix === substr( strtolower( $plugin_data['Name'] ), - $suffix_len )
10257
- ) {
10258
- $this->_plugin_name = substr( $plugin_data['Name'], 0, - $suffix_len );
10259
- }
10260
- }
10261
- }
10262
-
10263
- $this->_logger->departure( 'Name = ' . $this->_plugin_name );
10264
- }
10265
-
10266
- /**
10267
- * @author Vova Feldman (@svovaf)
10268
- * @since 1.0.0
10269
- *
10270
- * @param bool $reparse_plugin_metadata
10271
- *
10272
- * @return string
10273
- */
10274
- function get_plugin_version( $reparse_plugin_metadata = false ) {
10275
- $this->_logger->entrance();
10276
-
10277
- $plugin_data = $this->get_plugin_data( $reparse_plugin_metadata );
10278
-
10279
- $this->_logger->departure( 'Version = ' . $plugin_data['Version'] );
10280
-
10281
- return $this->apply_filters( 'plugin_version', $plugin_data['Version'] );
10282
- }
10283
-
10284
- /**
10285
- * @author Vova Feldman (@svovaf)
10286
- * @since 1.2.1.7
10287
- *
10288
- * @return string
10289
- */
10290
- function get_plugin_title() {
10291
- $this->_logger->entrance();
10292
-
10293
- $title = $this->_plugin->title;
10294
-
10295
- return $this->apply_filters( 'plugin_title', $title );
10296
- }
10297
-
10298
- /**
10299
- * @author Vova Feldman (@svovaf)
10300
- * @since 1.2.2.7
10301
- *
10302
- * @param bool $lowercase
10303
- *
10304
- * @return string
10305
- */
10306
- function get_module_label( $lowercase = false ) {
10307
- $label = $this->is_addon() ?
10308
- $this->get_text_inline( 'Add-On', 'addon' ) :
10309
- ( $this->is_plugin() ?
10310
- $this->get_text_inline( 'Plugin', 'plugin' ) :
10311
- $this->get_text_inline( 'Theme', 'theme' ) );
10312
-
10313
- if ( $lowercase ) {
10314
- $label = strtolower( $label );
10315
- }
10316
-
10317
- return $label;
10318
- }
10319
-
10320
- /**
10321
- * @author Vova Feldman (@svovaf)
10322
- * @since 1.0.4
10323
- *
10324
- * @return string
10325
- */
10326
- function get_plugin_basename() {
10327
- if ( ! isset( $this->_plugin_basename ) ) {
10328
- if ( $this->is_plugin() ) {
10329
- $this->_plugin_basename = plugin_basename( $this->_plugin_main_file_path );
10330
- } else {
10331
- $this->_plugin_basename = basename( dirname( $this->_plugin_main_file_path ) );
10332
- }
10333
- }
10334
-
10335
- return $this->_plugin_basename;
10336
- }
10337
-
10338
- function get_plugin_folder_name() {
10339
- $this->_logger->entrance();
10340
-
10341
- $plugin_folder = $this->_plugin_basename;
10342
-
10343
- while ( '.' !== dirname( $plugin_folder ) ) {
10344
- $plugin_folder = dirname( $plugin_folder );
10345
- }
10346
-
10347
- $this->_logger->departure( 'Folder Name = ' . $plugin_folder );
10348
-
10349
- return $plugin_folder;
10350
- }
10351
-
10352
- #endregion ------------------------------------------------------------------
10353
-
10354
- /* Account
10355
- ------------------------------------------------------------------------------------------------------------------*/
10356
-
10357
- /**
10358
- * Find plugin's slug by plugin's basename.
10359
- *
10360
- * @author Vova Feldman (@svovaf)
10361
- * @since 1.0.9
10362
- *
10363
- * @param string $plugin_base_name
10364
- *
10365
- * @return false|string
10366
- */
10367
- private static function find_slug_by_basename( $plugin_base_name ) {
10368
- $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
10369
-
10370
- if ( ! array( $file_slug_map ) || ! isset( $file_slug_map[ $plugin_base_name ] ) ) {
10371
- return false;
10372
- }
10373
-
10374
- return $file_slug_map[ $plugin_base_name ];
10375
- }
10376
-
10377
- /**
10378
- * Store the map between the plugin's basename to the slug.
10379
- *
10380
- * @author Vova Feldman (@svovaf)
10381
- * @since 1.0.9
10382
- */
10383
- private function store_file_slug_map() {
10384
- $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
10385
-
10386
- if ( ! array( $file_slug_map ) ) {
10387
- $file_slug_map = array();
10388
- }
10389
-
10390
- if ( ! isset( $file_slug_map[ $this->_plugin_basename ] ) ||
10391
- $file_slug_map[ $this->_plugin_basename ] !== $this->_slug
10392
- ) {
10393
- $file_slug_map[ $this->_plugin_basename ] = $this->_slug;
10394
- self::$_accounts->set_option( 'file_slug_map', $file_slug_map, true );
10395
- }
10396
- }
10397
-
10398
- /**
10399
- * @return array[number]FS_User
10400
- */
10401
- static function get_all_users() {
10402
- $users = self::maybe_get_entities_account_option( 'users', array() );
10403
-
10404
- if ( ! is_array( $users ) ) {
10405
- $users = array();
10406
- }
10407
-
10408
- return $users;
10409
- }
10410
-
10411
- /**
10412
- * @param string $module_type
10413
- * @param null|int $blog_id Since 2.0.0
10414
- *
10415
- * @return array[string]FS_Site
10416
- */
10417
- private static function get_all_sites(
10418
- $module_type = WP_FS__MODULE_TYPE_PLUGIN,
10419
- $blog_id = null
10420
- ) {
10421
- $sites = self::get_account_option( 'sites', $module_type, $blog_id );
10422
-
10423
- if ( ! is_array( $sites ) ) {
10424
- $sites = array();
10425
- }
10426
-
10427
- return $sites;
10428
- }
10429
-
10430
- /**
10431
- * @author Leo Fajardo (@leorw)
10432
- *
10433
- * @since 1.2.2
10434
- *
10435
- * @param string $option_name
10436
- * @param string $module_type
10437
- * @param null|int $network_level_or_blog_id Since 2.0.0
10438
- *
10439
- * @return mixed
10440
- */
10441
- private static function get_account_option( $option_name, $module_type = null, $network_level_or_blog_id = null ) {
10442
- if ( ! is_null( $module_type ) && WP_FS__MODULE_TYPE_PLUGIN !== $module_type ) {
10443
- $option_name = $module_type . '_' . $option_name;
10444
- }
10445
-
10446
- return self::maybe_get_entities_account_option( $option_name, array(), $network_level_or_blog_id );
10447
- }
10448
-
10449
- /**
10450
- * @author Leo Fajardo (@leorw)
10451
- *
10452
- * @since 1.2.2
10453
- *
10454
- * @param string $option_name
10455
- * @param mixed $option_value
10456
- * @param bool $store
10457
- * @param null|int $network_level_or_blog_id Since 2.0.0
10458
- */
10459
- private function set_account_option( $option_name, $option_value, $store, $network_level_or_blog_id = null ) {
10460
- self::set_account_option_by_module(
10461
- $this->_module_type,
10462
- $option_name,
10463
- $option_value,
10464
- $store,
10465
- $network_level_or_blog_id
10466
- );
10467
- }
10468
-
10469
- /**
10470
- * @author Vova Feldman (@svovaf)
10471
- *
10472
- * @since 1.2.2.7
10473
- *
10474
- * @param string $module_type
10475
- * @param string $option_name
10476
- * @param mixed $option_value
10477
- * @param bool $store
10478
- * @param null|int $network_level_or_blog_id Since 2.0.0
10479
- */
10480
- private static function set_account_option_by_module(
10481
- $module_type,
10482
- $option_name,
10483
- $option_value,
10484
- $store,
10485
- $network_level_or_blog_id = null
10486
- ) {
10487
- if ( WP_FS__MODULE_TYPE_PLUGIN != $module_type ) {
10488
- $option_name = $module_type . '_' . $option_name;
10489
- }
10490
-
10491
- self::$_accounts->set_option( $option_name, $option_value, $store, $network_level_or_blog_id );
10492
- }
10493
-
10494
- /**
10495
- * This method can also return non-entity or non-entities collection option like the `user_id_license_ids_map` option.
10496
- *
10497
- * @author Leo Fajardo (@leorw)
10498
- * @since 2.3.1
10499
- *
10500
- * @param string $option_name
10501
- * @param mixed $default
10502
- * @param null|bool|int $network_level_or_blog_id When an integer, use the given blog storage. When `true` use the multisite storage (if there's a network). When `false`, use the current context blog storage. When `null`, the decision which storage to use (MS vs. Current S) will be handled internally and determined based on the $option (based on self::$_SITE_LEVEL_PARAMS).
10503
- *
10504
- * @return mixed|FS_Plugin[]|FS_User[]|FS_Site[]|FS_Plugin_License[]|FS_Plugin_Plan[]|FS_Plugin_Tag[]
10505
- */
10506
- private static function maybe_get_entities_account_option( $option_name, $default = null, $network_level_or_blog_id = null ) {
10507
- $option = self::$_accounts->get_option( $option_name, $default, $network_level_or_blog_id );
10508
-
10509
- $class_name = '';
10510
-
10511
- if ( fs_starts_with( $option_name, WP_FS__MODULE_TYPE_THEME . '_' ) ) {
10512
- $option_name = str_replace( WP_FS__MODULE_TYPE_THEME . '_', '', $option_name );
10513
- }
10514
-
10515
- switch ( $option_name ) {
10516
- case 'plugins':
10517
- case 'themes':
10518
- case 'addons':
10519
- $class_name = FS_Plugin::get_class_name();
10520
- break;
10521
- case 'users':
10522
- $class_name = FS_User::get_class_name();
10523
- break;
10524
- case 'sites':
10525
- $class_name = FS_Site::get_class_name();
10526
- break;
10527
- case 'licenses':
10528
- case 'all_licenses':
10529
- $class_name = FS_Plugin_License::get_class_name();
10530
- break;
10531
- case 'plans':
10532
- $class_name = FS_Plugin_Plan::get_class_name();
10533
- break;
10534
- case 'updates':
10535
- $class_name = FS_Plugin_Tag::get_class_name();
10536
- break;
10537
- }
10538
-
10539
- if ( empty( $class_name ) ) {
10540
- return $option;
10541
- }
10542
-
10543
- return fs_get_entities( $option, $class_name );
10544
- }
10545
-
10546
- /**
10547
- * @author Vova Feldman (@svovaf)
10548
- * @since 1.0.6
10549
- *
10550
- * @param number|null $module_id
10551
- *
10552
- * @return FS_Plugin_License[]
10553
- */
10554
- private static function get_all_licenses( $module_id = null ) {
10555
- $licenses = self::get_account_option( 'all_licenses' );
10556
-
10557
- if ( ! is_array( $licenses ) ) {
10558
- $licenses = array();
10559
- }
10560
-
10561
- if ( is_null( $module_id ) ) {
10562
- return $licenses;
10563
- }
10564
-
10565
- $licenses = isset( $licenses[ $module_id ] ) ?
10566
- $licenses[ $module_id ] :
10567
- array();
10568
-
10569
- return $licenses;
10570
- }
10571
-
10572
- /**
10573
- * @author Leo Fajardo (@leorw)
10574
- * @since 2.0.0
10575
- *
10576
- * @return array
10577
- */
10578
- private static function get_all_licenses_by_module_type() {
10579
- $licenses = self::get_account_option( 'all_licenses' );
10580
-
10581
- $licenses_by_module_type = array(
10582
- WP_FS__MODULE_TYPE_PLUGIN => array(),
10583
- WP_FS__MODULE_TYPE_THEME => array()
10584
- );
10585
-
10586
- if ( ! is_array( $licenses ) ) {
10587
- return $licenses_by_module_type;
10588
- }
10589
-
10590
- foreach ( $licenses as $module_id => $module_licenses ) {
10591
- $fs = self::get_instance_by_id( $module_id );
10592
- if ( false === $fs ) {
10593
- continue;
10594
- }
10595
-
10596
- $licenses_by_module_type[ $fs->_module_type ] = array_merge( $licenses_by_module_type[ $fs->_module_type ], $module_licenses );
10597
- }
10598
-
10599
- return $licenses_by_module_type;
10600
- }
10601
-
10602
- /**
10603
- * @author Leo Fajardo (@leorw)
10604
- * @since 2.0.0
10605
- *
10606
- * @param number $module_id
10607
- * @param number|null $user_id
10608
- *
10609
- * @return array
10610
- */
10611
- private static function get_user_id_license_ids_map( $module_id, $user_id = null ) {
10612
- $all_modules_user_id_license_ids_map = self::get_account_option( 'user_id_license_ids_map' );
10613
-
10614
- if ( ! is_array( $all_modules_user_id_license_ids_map ) ) {
10615
- $all_modules_user_id_license_ids_map = array();
10616
- }
10617
-
10618
- $user_id_license_ids_map = isset( $all_modules_user_id_license_ids_map[ $module_id ] ) ?
10619
- $all_modules_user_id_license_ids_map[ $module_id ] :
10620
- array();
10621
-
10622
- if ( FS_User::is_valid_id( $user_id ) ) {
10623
- $user_id_license_ids_map = isset( $user_id_license_ids_map[ $user_id ] ) ?
10624
- $user_id_license_ids_map[ $user_id ] :
10625
- array();
10626
- }
10627
-
10628
- return $user_id_license_ids_map;
10629
- }
10630
-
10631
- /**
10632
- * @author Leo Fajardo (@leorw)
10633
- * @since 2.0.0
10634
- *
10635
- * @param array $new_user_id_license_ids_map
10636
- * @param number $module_id
10637
- * @param number|null $user_id
10638
- */
10639
- private static function store_user_id_license_ids_map( $new_user_id_license_ids_map, $module_id, $user_id = null ) {
10640
- $all_modules_user_id_license_ids_map = self::get_account_option( 'user_id_license_ids_map' );
10641
- if ( ! is_array( $all_modules_user_id_license_ids_map ) ) {
10642
- $all_modules_user_id_license_ids_map = array();
10643
- }
10644
-
10645
- if ( ! isset( $all_modules_user_id_license_ids_map[ $module_id ] ) ) {
10646
- $all_modules_user_id_license_ids_map[ $module_id ] = array();
10647
- }
10648
-
10649
- if ( FS_User::is_valid_id( $user_id ) ) {
10650
- $all_modules_user_id_license_ids_map[ $module_id ][ $user_id ] = $new_user_id_license_ids_map;
10651
- } else {
10652
- $all_modules_user_id_license_ids_map[ $module_id ] = $new_user_id_license_ids_map;
10653
- }
10654
-
10655
- self::$_accounts->set_option( 'user_id_license_ids_map', $all_modules_user_id_license_ids_map, true );
10656
- }
10657
-
10658
- /**
10659
- * Get a collection of the user's linked license IDs.
10660
- *
10661
- * @author Vova Feldman (@svovaf)
10662
- * @since 2.0.0
10663
- *
10664
- * @param number $user_id
10665
- *
10666
- * @return number[]
10667
- */
10668
- private function get_user_linked_license_ids( $user_id ) {
10669
- return self::get_user_id_license_ids_map( $this->_module_id, $user_id );
10670
- }
10671
-
10672
- /**
10673
- * Override the user's linked license IDs with a new IDs collection.
10674
- *
10675
- * @author Vova Feldman (@svovaf)
10676
- * @since 2.0.0
10677
- *
10678
- * @param number $user_id
10679
- * @param number[] $license_ids
10680
- */
10681
- private function set_user_linked_license_ids( $user_id, array $license_ids ) {
10682
- self::store_user_id_license_ids_map( $license_ids, $this->_module_id, $user_id );
10683
- }
10684
-
10685
- /**
10686
- * Link a specified license ID to a given user.
10687
- *
10688
- * @author Vova Feldman (@svovaf)
10689
- * @since 2.0.0
10690
- *
10691
- * @param number $license_id
10692
- * @param number $user_id
10693
- */
10694
- private function link_license_2_user( $license_id, $user_id ) {
10695
- $license_ids = $this->get_user_linked_license_ids( $user_id );
10696
-
10697
- if ( in_array( $license_id, $license_ids ) ) {
10698
- // License already linked.
10699
- return;
10700
- }
10701
-
10702
- $license_ids[] = $license_id;
10703
-
10704
- $this->set_user_linked_license_ids( $user_id, $license_ids );
10705
- }
10706
-
10707
- /**
10708
- * @param string|bool $module_type
10709
- *
10710
- * @return FS_Plugin_Plan[]
10711
- */
10712
- private static function get_all_plans( $module_type = false ) {
10713
- $plans = self::get_account_option( 'plans', $module_type );
10714
-
10715
- if ( ! is_array( $plans ) ) {
10716
- $plans = array();
10717
- }
10718
-
10719
- return $plans;
10720
- }
10721
-
10722
- /**
10723
- * @author Vova Feldman (@svovaf)
10724
- * @since 1.0.4
10725
- *
10726
- * @return FS_Plugin_Tag[]
10727
- */
10728
- private static function get_all_updates() {
10729
- $updates = self::maybe_get_entities_account_option( 'updates', array() );
10730
-
10731
- if ( ! is_array( $updates ) ) {
10732
- $updates = array();
10733
- }
10734
-
10735
- return $updates;
10736
- }
10737
-
10738
- /**
10739
- * @author Vova Feldman (@svovaf)
10740
- * @since 1.0.6
10741
- *
10742
- * @return array<number,FS_Plugin[]>|false
10743
- */
10744
- private static function get_all_addons() {
10745
- $addons = self::maybe_get_entities_account_option( 'addons', array() );
10746
-
10747
- if ( ! is_array( $addons ) ) {
10748
- $addons = array();
10749
- }
10750
-
10751
- return $addons;
10752
- }
10753
-
10754
- /**
10755
- * @author Vova Feldman (@svovaf)
10756
- * @since 1.0.6
10757
- *
10758
- * @return number[]|false
10759
- */
10760
- private static function get_all_account_addons() {
10761
- $addons = self::$_accounts->get_option( 'account_addons', array() );
10762
-
10763
- if ( ! is_array( $addons ) ) {
10764
- $addons = array();
10765
- }
10766
-
10767
- return $addons;
10768
- }
10769
-
10770
- /**
10771
- * Check if user has connected his account (opted-in).
10772
- *
10773
- * Note:
10774
- * If the user opted-in and opted-out on a later stage,
10775
- * this will still return true. If you want to check if the
10776
- * user is currently opted-in, use:
10777
- * `$fs->is_registered() && $fs->is_tracking_allowed()`
10778
- *
10779
- * @author Vova Feldman (@svovaf)
10780
- * @since 1.0.1
10781
- * @return bool
10782
- */
10783
- function is_registered() {
10784
- return is_object( $this->_user );
10785
- }
10786
-
10787
- /**
10788
- * Returns TRUE if the user opted-in and didn't disconnect (opt-out).
10789
- *
10790
- * @author Leo Fajardo (@leorw)
10791
- * @since 1.2.1.5
10792
- *
10793
- * @return bool
10794
- */
10795
- function is_tracking_allowed() {
10796
- return ( is_object( $this->_site ) && $this->_site->is_tracking_allowed() );
10797
- }
10798
-
10799
- /**
10800
- * @author Leo Fajardo (@leorw)
10801
- * @since 2.4.0
10802
- *
10803
- * @return bool
10804
- */
10805
- function is_bundle_license_auto_activation_enabled() {
10806
- return $this->is_addon() ?
10807
- ( is_object( $this->_parent ) && $this->_parent->is_bundle_license_auto_activation_enabled() ) :
10808
- $this->_is_bundle_license_auto_activation_enabled;
10809
- }
10810
-
10811
- /**
10812
- * @author Vova Feldman (@svovaf)
10813
- * @since 1.0.4
10814
- *
10815
- * @return FS_Plugin
10816
- */
10817
- function get_plugin() {
10818
- return $this->_plugin;
10819
- }
10820
-
10821
- /**
10822
- * @author Vova Feldman (@svovaf)
10823
- * @since 1.0.3
10824
- *
10825
- * @return FS_User
10826
- */
10827
- function get_user() {
10828
- return $this->_user;
10829
- }
10830
-
10831
- /**
10832
- * @author Vova Feldman (@svovaf)
10833
- * @since 1.0.3
10834
- *
10835
- * @return FS_Site
10836
- */
10837
- function get_site() {
10838
- return $this->_site;
10839
- }
10840
-
10841
- /**
10842
- * Get plugin add-ons.
10843
- *
10844
- * @author Vova Feldman (@svovaf)
10845
- * @since 1.0.6
10846
- *
10847
- * @since 1.1.7.3 If not yet loaded, fetch data from the API.
10848
- *
10849
- * @param bool $flush
10850
- *
10851
- * @return FS_Plugin[]|false
10852
- */
10853
- function get_addons( $flush = false ) {
10854
- $this->_logger->entrance();
10855
-
10856
- if ( ! $this->_has_addons ) {
10857
- return false;
10858
- }
10859
-
10860
- $addons = $this->sync_addons( $flush );
10861
-
10862
- return ( ! is_array( $addons ) || empty( $addons ) ) ?
10863
- false :
10864
- $addons;
10865
- }
10866
-
10867
- /**
10868
- * @author Vova Feldman (@svovaf)
10869
- * @since 1.0.6
10870
- *
10871
- * @return number[]|false
10872
- */
10873
- function get_account_addons() {
10874
- $this->_logger->entrance();
10875
-
10876
- $addons = self::get_all_account_addons();
10877
-
10878
- if ( ! is_array( $addons ) ||
10879
- ! isset( $addons[ $this->_plugin->id ] ) ||
10880
- ! is_array( $addons[ $this->_plugin->id ] ) ||
10881
- 0 === count( $addons[ $this->_plugin->id ] )
10882
- ) {
10883
- return false;
10884
- }
10885
-
10886
- return $addons[ $this->_plugin->id ];
10887
- }
10888
-
10889
- /**
10890
- * Check if user has any
10891
- *
10892
- * @author Vova Feldman (@svovaf)
10893
- * @since 1.1.6
10894
- *
10895
- * @return bool
10896
- */
10897
- function has_account_addons() {
10898
- $addons = $this->get_account_addons();
10899
-
10900
- return is_array( $addons ) && ( 0 < count( $addons ) );
10901
- }
10902
-
10903
-
10904
- /**
10905
- * Get add-on by ID (from local data).
10906
- *
10907
- * @author Vova Feldman (@svovaf)
10908
- * @since 1.0.6
10909
- *
10910
- * @param number $id
10911
- *
10912
- * @return FS_Plugin|false
10913
- */
10914
- function get_addon( $id ) {
10915
- $this->_logger->entrance();
10916
-
10917
- $addons = $this->get_addons();
10918
-
10919
- if ( is_array( $addons ) ) {
10920
- foreach ( $addons as $addon ) {
10921
- if ( $id == $addon->id ) {
10922
- return $addon;
10923
- }
10924
- }
10925
- }
10926
-
10927
- return false;
10928
- }
10929
-
10930
- /**
10931
- * Get add-on by slug (from local data).
10932
- *
10933
- * @author Vova Feldman (@svovaf)
10934
- * @since 1.0.6
10935
- *
10936
- * @param string $slug
10937
- *
10938
- * @param bool $flush
10939
- *
10940
- * @return FS_Plugin|false
10941
- */
10942
- function get_addon_by_slug( $slug, $flush = false ) {
10943
- $this->_logger->entrance();
10944
-
10945
- $addons = $this->get_addons( $flush );
10946
-
10947
- if ( is_array( $addons ) ) {
10948
- foreach ( $addons as $addon ) {
10949
- if ( $slug === $addon->slug ) {
10950
- return $addon;
10951
- }
10952
- }
10953
- }
10954
-
10955
- return false;
10956
- }
10957
-
10958
- /**
10959
- * @var array<number,object[]> {
10960
- * @key number Add-on ID.
10961
- * @val object[] The add-on's plans and prices object.
10962
- * }
10963
- */
10964
- private $plans_and_pricing_by_addon_id;
10965
-
10966
- /**
10967
- * @author Leo Fajardo (@leorw)
10968
- * @since 2.3.0
10969
- *
10970
- * @return array<number,object[]> {
10971
- * @key number Add-on ID.
10972
- * @val object[] The add-on's plans and prices object.
10973
- * }
10974
- */
10975
- function _get_addons_plans_and_pricing_map_by_id() {
10976
- if ( ! isset( $this->plans_and_pricing_by_addon_id ) ) {
10977
- $result = $this->get_api_plugin_scope()->get( $this->add_show_pending( "/addons/pricing.json?type=visible" ) );
10978
-
10979
- $plans_and_pricing_by_addon_id = array();
10980
- if ( $this->is_api_result_object( $result, 'addons' ) ) {
10981
- foreach ( $result->addons as $addon ) {
10982
- $plans_and_pricing_by_addon_id[ $addon->id ] = $addon->plans;
10983
- }
10984
- }
10985
-
10986
- $this->plans_and_pricing_by_addon_id = $plans_and_pricing_by_addon_id;
10987
- }
10988
-
10989
- return $this->plans_and_pricing_by_addon_id;
10990
- }
10991
-
10992
- /**
10993
- * @author Leo Fajardo (@leorw)
10994
- * @since 2.3.0
10995
- *
10996
- * @param number $addon_id
10997
- * @param bool $is_installed
10998
- *
10999
- * @return array
11000
- */
11001
- function _get_addon_info( $addon_id, $is_installed ) {
11002
- $addon = $this->get_addon( $addon_id );
11003
-
11004
- if ( ! is_object( $addon ) ) {
11005
- // Unexpected call.
11006
- return array();
11007
- }
11008
-
11009
- $slug = $addon->slug;
11010
-
11011
- $addon_storage = FS_Storage::instance( WP_FS__MODULE_TYPE_PLUGIN, $slug );
11012
-
11013
- if ( ! fs_is_network_admin() ) {
11014
- // Get blog-level activated installations.
11015
- $sites = self::maybe_get_entities_account_option( 'sites', array() );
11016
- } else {
11017
- $sites = null;
11018
-
11019
- if ( $this->is_addon_activated( $addon_id ) &&
11020
- $this->get_addon_instance( $addon_id )->is_network_active()
11021
- ) {
11022
- if ( FS_Site::is_valid_id( $addon_storage->network_install_blog_id ) ) {
11023
- // Get network-level activated installations.
11024
- $sites = self::maybe_get_entities_account_option(
11025
- 'sites',
11026
- array(),
11027
- $addon_storage->network_install_blog_id
11028
- );
11029
- }
11030
- }
11031
- }
11032
-
11033
- $addon_info = array(
11034
- 'is_connected' => false,
11035
- 'slug' => $slug,
11036
- 'title' => $addon->title,
11037
- 'is_whitelabeled' => $addon_storage->is_whitelabeled
11038
- );
11039
-
11040
- if ( ! $is_installed ) {
11041
- $plans_and_pricing_by_addon_id = $this->_get_addons_plans_and_pricing_map_by_id();
11042
-
11043
- if ( isset( $plans_and_pricing_by_addon_id[ $addon_id ] ) ) {
11044
- $has_paid_plan = false;
11045
- $plans = $plans_and_pricing_by_addon_id[ $addon_id ];
11046
-
11047
- if ( is_array( $plans ) && count( $plans ) > 0 ) {
11048
- foreach ( $plans as $plan ) {
11049
- if ( isset( $plan->pricing ) &&
11050
- is_array( $plan->pricing ) &&
11051
- count( $plan->pricing ) > 0
11052
- ) {
11053
- $has_paid_plan = true;
11054
- break;
11055
- }
11056
- }
11057
- }
11058
-
11059
- $addon_info['has_paid_plan'] = $has_paid_plan;
11060
- }
11061
- }
11062
-
11063
- if ( ! is_array( $sites ) || ! isset( $sites[ $slug ] ) ) {
11064
- return $addon_info;
11065
- }
11066
-
11067
- $site = $sites[ $slug ];
11068
-
11069
- $addon_info['is_connected'] = (
11070
- ( $addon->parent_plugin_id == $this->get_id() ) &&
11071
- is_object( $site ) &&
11072
- FS_Site::is_valid_id( $site->id ) &&
11073
- FS_User::is_valid_id( $site->user_id ) &&
11074
- FS_Plugin_Plan::is_valid_id( $site->plan_id )
11075
- );
11076
-
11077
- if ( $addon_info['is_connected'] && $is_installed ) {
11078
- return $addon_info;
11079
- }
11080
-
11081
- $addon_info['site'] = $site;
11082
-
11083
- $plugins_data = self::maybe_get_entities_account_option( WP_FS__MODULE_TYPE_PLUGIN . 's', array() );
11084
- if ( isset( $plugins_data[ $slug ] ) ) {
11085
- $plugin_data = $plugins_data[ $slug ];
11086
-
11087
- $addon_info['version'] = $plugin_data->version;
11088
- }
11089
-
11090
- $all_plans = self::maybe_get_entities_account_option( 'plans', array() );
11091
- if ( isset( $all_plans[ $slug ] ) ) {
11092
- $plans = $all_plans[ $slug ];
11093
-
11094
- foreach ( $plans as $plan ) {
11095
- if ( $site->plan_id == Freemius::_decrypt( $plan->id ) ) {
11096
- $addon_info['plan_name'] = Freemius::_decrypt( $plan->name );
11097
- $addon_info['plan_title'] = Freemius::_decrypt( $plan->title );
11098
- break;
11099
- }
11100
- }
11101
- }
11102
-
11103
- $licenses = self::maybe_get_entities_account_option( 'all_licenses', array() );
11104
- if ( is_array( $licenses ) && isset( $licenses[ $addon_id ] ) ) {
11105
- foreach ( $licenses[ $addon_id ] as $license ) {
11106
- if ( $license->id == $site->license_id ) {
11107
- $addon_info['license'] = $license;
11108
- break;
11109
- }
11110
- }
11111
- }
11112
-
11113
- if ( isset( $addon_info['license'] ) ) {
11114
- if ( isset( $addon_storage->subscriptions ) &&
11115
- ! empty( $addon_storage->subscriptions )
11116
- ) {
11117
- $addon_subscriptions = fs_get_entities( $addon_storage->subscriptions, FS_Subscription::get_class_name() );
11118
-
11119
- foreach ( $addon_subscriptions as $subscription ) {
11120
- if ( $subscription->license_id == $site->license_id ) {
11121
- $addon_info['subscription'] = $subscription;
11122
- break;
11123
- }
11124
- }
11125
- }
11126
- }
11127
-
11128
- return $addon_info;
11129
- }
11130
-
11131
- /**
11132
- * @author Vova Feldman (@svovaf)
11133
- * @since 2.0.0
11134
- *
11135
- * @param number $user_id
11136
- *
11137
- * @return FS_User
11138
- */
11139
- static function _get_user_by_id( $user_id ) {
11140
- self::$_static_logger->entrance( "user_id = {$user_id}" );
11141
-
11142
- $users = self::get_all_users();
11143
-
11144
- if ( is_array( $users ) ) {
11145
- if ( isset( $users[ $user_id ] ) &&
11146
- $users[ $user_id ] instanceof FS_User &&
11147
- $user_id == $users[ $user_id ]->id
11148
- ) {
11149
- return $users[ $user_id ];
11150
- }
11151
-
11152
- // If user wasn't found by the key, iterate over all the users collection.
11153
- foreach ( $users as $user ) {
11154
- /**
11155
- * @var FS_User $user
11156
- */
11157
- if ( $user_id == $user->id ) {
11158
- return $user;
11159
- }
11160
- }
11161
- }
11162
-
11163
- return null;
11164
- }
11165
-
11166
- /**
11167
- * Checks if a Freemius user_id is associated with a super-admin.
11168
- *
11169
- * @author Vova Feldman (@svovaf)
11170
- * @since 2.0.0
11171
- *
11172
- * @param number $user_id
11173
- *
11174
- * @return bool
11175
- */
11176
- private static function is_super_admin( $user_id ) {
11177
- $is_super_admin = false;
11178
-
11179
- $user = self::_get_user_by_id( $user_id );
11180
-
11181
- if ( $user instanceof FS_User && ! empty( $user->email ) ) {
11182
- self::require_pluggable_essentials();
11183
-
11184
- $wp_user = get_user_by( 'email', $user->email );
11185
-
11186
- if ( $wp_user instanceof WP_User ) {
11187
- $super_admins = get_super_admins();
11188
- $is_super_admin = ( is_array( $super_admins ) && in_array( $wp_user->user_login, $super_admins ) );
11189
- }
11190
- }
11191
-
11192
- return $is_super_admin;
11193
- }
11194
-
11195
- #----------------------------------------------------------------------------------
11196
- #region Plans & Licensing
11197
- #----------------------------------------------------------------------------------
11198
-
11199
- /**
11200
- * Check if running premium plugin code.
11201
- *
11202
- * @author Vova Feldman (@svovaf)
11203
- * @since 1.0.5
11204
- *
11205
- * @return bool
11206
- */
11207
- function is_premium() {
11208
- /**
11209
- * `$this->_plugin` will be `false` when `is_activation_mode` calls this method directly from the
11210
- * `register_constructor_hooks` method.
11211
- *
11212
- * @author Leo Fajardo (@leorw)
11213
- * @since 2.2.3
11214
- */
11215
- return is_object( $this->_plugin ) ?
11216
- $this->_plugin->is_premium :
11217
- false;
11218
- }
11219
-
11220
- /**
11221
- * Get site's plan ID.
11222
- *
11223
- * @author Vova Feldman (@svovaf)
11224
- * @since 1.0.2
11225
- *
11226
- * @return number
11227
- */
11228
- function get_plan_id() {
11229
- return $this->_site->plan_id;
11230
- }
11231
-
11232
- /**
11233
- * Get site's plan title.
11234
- *
11235
- * @author Vova Feldman (@svovaf)
11236
- * @since 1.0.2
11237
- *
11238
- * @return string
11239
- */
11240
- function get_plan_title() {
11241
- $plan = $this->get_plan();
11242
-
11243
- return is_object( $plan ) ? $plan->title : 'PLAN_TITLE';
11244
- }
11245
-
11246
- /**
11247
- * Get site's plan name.
11248
- *
11249
- * @author Vova Feldman (@svovaf)
11250
- * @since 2.0.0
11251
- *
11252
- * @return string
11253
- */
11254
- function get_plan_name() {
11255
- $plan = $this->get_plan();
11256
-
11257
- return is_object( $plan ) ? $plan->name : 'PLAN_NAME';
11258
- }
11259
-
11260
- /**
11261
- * @author Vova Feldman (@svovaf)
11262
- * @since 1.0.9
11263
- *
11264
- * @return FS_Plugin_Plan|false
11265
- */
11266
- function get_plan() {
11267
- if ( ! is_object( $this->_site ) ) {
11268
- return false;
11269
- }
11270
-
11271
- return FS_Plugin_Plan::is_valid_id( $this->_site->plan_id ) ?
11272
- $this->_get_plan_by_id( $this->_site->plan_id ) :
11273
- false;
11274
- }
11275
-
11276
- /**
11277
- * @author Vova Feldman (@svovaf)
11278
- * @since 1.0.3
11279
- *
11280
- * @return bool
11281
- */
11282
- function is_trial() {
11283
- $this->_logger->entrance();
11284
-
11285
- if ( ! $this->is_registered() || ! is_object( $this->_site ) ) {
11286
- return false;
11287
- }
11288
-
11289
- return $this->_site->is_trial();
11290
- }
11291
-
11292
- /**
11293
- * Check if currently in a trial with payment method (credit card or paypal).
11294
- *
11295
- * @author Vova Feldman (@svovaf)
11296
- * @since 1.1.7
11297
- *
11298
- * @return bool
11299
- */
11300
- function is_paid_trial() {
11301
- $this->_logger->entrance();
11302
-
11303
- if ( ! $this->is_trial() ) {
11304
- return false;
11305
- }
11306
-
11307
- if ( ! $this->has_active_valid_license() ) {
11308
- return false;
11309
- }
11310
-
11311
- if ( $this->_site->trial_plan_id != $this->_license->plan_id ) {
11312
- return false;
11313
- }
11314
-
11315
- /**
11316
- * @var FS_Subscription $subscription
11317
- */
11318
- $subscription = $this->_get_subscription( $this->_license->id );
11319
-
11320
- return ( is_object( $subscription ) && $subscription->is_active() );
11321
- }
11322
-
11323
- /**
11324
- * Check if trial already utilized.
11325
- *
11326
- * @since 1.0.9
11327
- *
11328
- * @return bool
11329
- */
11330
- function is_trial_utilized() {
11331
- $this->_logger->entrance();
11332
-
11333
- if ( ! $this->is_registered() ) {
11334
- return false;
11335
- }
11336
-
11337
- return $this->_site->is_trial_utilized();
11338
- }
11339
-
11340
- /**
11341
- * Get trial plan information (if in trial).
11342
- *
11343
- * @author Vova Feldman (@svovaf)
11344
- * @since 1.0.9
11345
- *
11346
- * @return bool|FS_Plugin_Plan
11347
- */
11348
- function get_trial_plan() {
11349
- $this->_logger->entrance();
11350
-
11351
- if ( ! $this->is_trial() ) {
11352
- return false;
11353
- }
11354
-
11355
- // Try to load plan from local cache.
11356
- $trial_plan = $this->_get_plan_by_id( $this->_site->trial_plan_id );
11357
-
11358
- if ( ! is_object( $trial_plan ) ) {
11359
- $trial_plan = $this->_fetch_site_plan( $this->_site->trial_plan_id );
11360
-
11361
- /**
11362
- * If managed to fetch the plan, add it to the plans collection.
11363
- */
11364
- if ( $trial_plan instanceof FS_Plugin_Plan ) {
11365
- if ( ! is_array( $this->_plans ) ) {
11366
- $this->_plans = array();
11367
- }
11368
-
11369
- $this->_plans[] = $trial_plan;
11370
- $this->_store_plans();
11371
- }
11372
- }
11373
-
11374
- if ( $trial_plan instanceof FS_Plugin_Plan ) {
11375
- return $trial_plan;
11376
- }
11377
-
11378
- /**
11379
- * If for some reason failed to get the trial plan, fallback to a dummy name and title.
11380
- */
11381
- $trial_plan = new FS_Plugin_Plan();
11382
- $trial_plan->id = $this->_site->trial_plan_id;
11383
- $trial_plan->name = 'pro';
11384
- $trial_plan->title = 'Pro';
11385
-
11386
- return $trial_plan;
11387
- }
11388
-
11389
- /**
11390
- * Check if the user has an activate, non-expired license on current plugin's install.
11391
- *
11392
- * @since 1.0.9
11393
- *
11394
- * @return bool
11395
- */
11396
- function is_paying() {
11397
- $this->_logger->entrance();
11398
-
11399
- if ( ! $this->is_registered() ) {
11400
- return false;
11401
- }
11402
-
11403
- if ( ! $this->has_paid_plan() ) {
11404
- return false;
11405
- }
11406
-
11407
- return (
11408
- ! $this->is_trial() &&
11409
- 'free' !== $this->get_plan_name() &&
11410
- $this->has_active_valid_license()
11411
- );
11412
- }
11413
-
11414
- /**
11415
- * @author Vova Feldman (@svovaf)
11416
- * @since 1.0.4
11417
- *
11418
- * @return bool
11419
- */
11420
- function is_free_plan() {
11421
- if ( ! $this->is_registered() ) {
11422
- return true;
11423
- }
11424
-
11425
- if ( ! $this->has_paid_plan() ) {
11426
- return true;
11427
- }
11428
-
11429
- return (
11430
- 'free' === $this->get_plan_name() ||
11431
- ! $this->has_features_enabled_license()
11432
- );
11433
- }
11434
-
11435
- /**
11436
- * @author Vova Feldman (@svovaf)
11437
- * @since 1.0.5
11438
- *
11439
- * @return bool
11440
- */
11441
- function _has_premium_license() {
11442
- $this->_logger->entrance();
11443
-
11444
- $premium_license = $this->_get_available_premium_license();
11445
-
11446
- return ( false !== $premium_license );
11447
- }
11448
-
11449
- /**
11450
- * Check if user has any licenses associated with the plugin (including expired or blocking).
11451
- *
11452
- * @author Vova Feldman (@svovaf)
11453
- * @since 1.1.7.3
11454
- *
11455
- * @param bool $including_foreign
11456
- *
11457
- * @return bool
11458
- */
11459
- function has_any_license( $including_foreign = true ) {
11460
- if ( ! is_array( $this->_licenses ) || 0 === count( $this->_licenses ) ) {
11461
- return false;
11462
- }
11463
-
11464
- if ( $including_foreign ) {
11465
- return true;
11466
- }
11467
-
11468
- foreach ( $this->_licenses as $license ) {
11469
- if ( $this->_user->id == $license->user_id ) {
11470
- return true;
11471
- }
11472
- }
11473
-
11474
- return false;
11475
- }
11476
-
11477
- /**
11478
- * @author Vova Feldman (@svovaf)
11479
- * @since 1.0.5
11480
- *
11481
- * @param bool|null $is_localhost
11482
- *
11483
- * @return FS_Plugin_License|false
11484
- */
11485
- function _get_available_premium_license( $is_localhost = null ) {
11486
- $this->_logger->entrance();
11487
-
11488
- $licenses = $this->get_available_premium_licenses( $is_localhost );
11489
- if ( ! empty( $licenses ) ) {
11490
- return $licenses[0];
11491
- }
11492
-
11493
- return false;
11494
- }
11495
-
11496
- /**
11497
- * @author Vova Feldman (@svovaf)
11498
- * @since 1.0.5
11499
- *
11500
- * @param bool|null $is_localhost
11501
- *
11502
- * @return FS_Plugin_License[]
11503
- */
11504
- function get_available_premium_licenses( $is_localhost = null ) {
11505
- $this->_logger->entrance();
11506
-
11507
- $licenses = array();
11508
- if ( ! $this->has_paid_plan() ) {
11509
- return $licenses;
11510
- }
11511
-
11512
- if ( is_array( $this->_licenses ) ) {
11513
- foreach ( $this->_licenses as $license ) {
11514
- if ( ! $license->can_activate( $is_localhost ) ) {
11515
- continue;
11516
- }
11517
-
11518
- $licenses[] = $license;
11519
- }
11520
- }
11521
-
11522
- return $licenses;
11523
- }
11524
-
11525
- /**
11526
- * Sync local plugin plans with remote server.
11527
- *
11528
- * 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).
11529
- *
11530
- * @author Vova Feldman (@svovaf)
11531
- * @since 1.0.5
11532
- *
11533
- * @return FS_Plugin_Plan[]|object
11534
- */
11535
- function _sync_plans() {
11536
- $plans = $this->_fetch_plugin_plans();
11537
-
11538
- if ( $this->is_array_instanceof( $plans, 'FS_Plugin_Plan' ) ) {
11539
- $plans_map = array();
11540
- foreach ( $plans as $plan ) {
11541
- $plans_map[ $plan->id ] = true;
11542
- }
11543
-
11544
- $plans_ids_to_keep = $this->get_plans_ids_associated_with_installs();
11545
-
11546
- foreach ( $plans_ids_to_keep as $plan_id ) {
11547
- if ( isset( $plans_map[ $plan_id ] ) ) {
11548
- continue;
11549
- }
11550
-
11551
- $missing_plan = self::_get_plan_by_id( $plan_id );
11552
-
11553
- if ( is_object( $missing_plan ) ) {
11554
- $plans[] = $missing_plan;
11555
- }
11556
- }
11557
-
11558
- $this->_plans = $plans;
11559
- $this->_store_plans();
11560
- }
11561
-
11562
- $this->do_action( 'after_plans_sync', $plans );
11563
-
11564
- return $this->_plans;
11565
- }
11566
-
11567
- /**
11568
- * Check if specified plan exists locally. If not, fetch it and store it.
11569
- *
11570
- * @author Vova Feldman (@svovaf)
11571
- * @since 2.0.0
11572
- *
11573
- * @param number $plan_id
11574
- *
11575
- * @return \FS_Plugin_Plan|object The plan entity or the API error object on failure.
11576
- */
11577
- private function sync_plan_if_not_exist( $plan_id ) {
11578
- $plan = self::_get_plan_by_id( $plan_id );
11579
-
11580
- if ( is_object( $plan ) ) {
11581
- // Plan already exists.
11582
- return $plan;
11583
- }
11584
-
11585
- $plan = $this->fetch_plan_by_id( $plan_id );
11586
-
11587
- if ( $plan instanceof FS_Plugin_Plan ) {
11588
- $this->_plans[] = $plan;
11589
- $this->_store_plans();
11590
-
11591
- return $plan;
11592
- }
11593
-
11594
- return $plan;
11595
- }
11596
-
11597
- /**
11598
- * Check if specified license exists locally. If not, fetch it and store it.
11599
- *
11600
- * @author Vova Feldman (@svovaf)
11601
- * @since 2.0.0
11602
- *
11603
- * @param number $license_id
11604
- * @param string $license_key
11605
- *
11606
- * @return \FS_Plugin_Plan|object The plan entity or the API error object on failure.
11607
- */
11608
- private function sync_license_if_not_exist( $license_id, $license_key ) {
11609
- $license = $this->_get_license_by_id( $license_id );
11610
-
11611
- if ( is_object( $license ) ) {
11612
- // License already exists.
11613
- return $license;
11614
- }
11615
-
11616
- $license = $this->fetch_license_by_key( $license_id, $license_key );
11617
-
11618
- if ( $license instanceof FS_Plugin_License ) {
11619
- $this->_licenses[] = $license;
11620
-
11621
- $this->set_license( $license );
11622
-
11623
- $this->_store_licenses();
11624
-
11625
- return $license;
11626
- }
11627
-
11628
- return $license;
11629
- }
11630
-
11631
- /**
11632
- * Get a collection of unique plan IDs that are associated with any installs in the network.
11633
- *
11634
- * @author Leo Fajardo (@leorw)
11635
- * @since 2.0.0
11636
- *
11637
- * @return number[]
11638
- */
11639
- private function get_plans_ids_associated_with_installs() {
11640
- if ( ! is_multisite() ) {
11641
- if ( ! is_object( $this->_site ) ||
11642
- ! FS_Plugin_Plan::is_valid_id( $this->_site->plan_id )
11643
- ) {
11644
- return array();
11645
- }
11646
-
11647
- return array( $this->_site->plan_id );
11648
- }
11649
-
11650
- $plan_ids = array();
11651
- $sites = self::get_sites();
11652
- foreach ( $sites as $site ) {
11653
- $blog_id = self::get_site_blog_id( $site );
11654
- $install = $this->get_install_by_blog_id( $blog_id );
11655
-
11656
- if ( ! is_object( $install ) ||
11657
- ! FS_Plugin_Plan::is_valid_id( $install->plan_id )
11658
- ) {
11659
- continue;
11660
- }
11661
-
11662
- $plan_ids[ $install->plan_id ] = true;
11663
- }
11664
-
11665
- return array_keys( $plan_ids );
11666
- }
11667
-
11668
- /**
11669
- * Get a collection of unique license IDs that are associated with any installs in the network.
11670
- *
11671
- * @author Leo Fajardo (@leorw)
11672
- * @since 2.0.0
11673
- *
11674
- * @return number[]
11675
- */
11676
- private function get_license_ids_associated_with_installs() {
11677
- if ( ! $this->_is_network_active ) {
11678
- if ( ! is_object( $this->_site ) ||
11679
- ! FS_Plugin_License::is_valid_id( $this->_site->license_id )
11680
- ) {
11681
- return array();
11682
- }
11683
-
11684
- return array( $this->_site->license_id );
11685
- }
11686
-
11687
- $license_ids = array();
11688
- $sites = self::get_sites();
11689
- foreach ( $sites as $site ) {
11690
- $blog_id = self::get_site_blog_id( $site );
11691
- $install = $this->get_install_by_blog_id( $blog_id );
11692
-
11693
- if ( ! is_object( $install ) ||
11694
- ! FS_Plugin_License::is_valid_id( $install->license_id )
11695
- ) {
11696
- continue;
11697
- }
11698
-
11699
- $license_ids[ $install->license_id ] = true;
11700
- }
11701
-
11702
- return array_keys( $license_ids );
11703
- }
11704
-
11705
- /**
11706
- * @author Vova Feldman (@svovaf)
11707
- * @since 1.0.5
11708
- *
11709
- * @param number $id
11710
- *
11711
- * @return FS_Plugin_Plan|false
11712
- */
11713
- function _get_plan_by_id( $id ) {
11714
- $this->_logger->entrance();
11715
-
11716
- if ( ! is_array( $this->_plans ) || 0 === count( $this->_plans ) ) {
11717
- $this->_sync_plans();
11718
- }
11719
-
11720
- foreach ( $this->_plans as $plan ) {
11721
- if ( $id == $plan->id ) {
11722
- return $plan;
11723
- }
11724
- }
11725
-
11726
- return false;
11727
- }
11728
-
11729
- /**
11730
- * @author Vova Feldman (@svovaf)
11731
- * @since 1.1.8.1
11732
- *
11733
- * @param string $name
11734
- *
11735
- * @return FS_Plugin_Plan|false
11736
- */
11737
- private function get_plan_by_name( $name ) {
11738
- $this->_logger->entrance();
11739
-
11740
- if ( ! is_array( $this->_plans ) || 0 === count( $this->_plans ) ) {
11741
- $this->_sync_plans();
11742
- }
11743
-
11744
- foreach ( $this->_plans as $plan ) {
11745
- if ( $name == $plan->name ) {
11746
- return $plan;
11747
- }
11748
- }
11749
-
11750
- return false;
11751
- }
11752
-
11753
- /**
11754
- * Sync local licenses with remote server.
11755
- *
11756
- * @author Vova Feldman (@svovaf)
11757
- * @since 1.0.6
11758
- *
11759
- * @param number|bool $site_license_id
11760
- * @param number|null $blog_id
11761
- *
11762
- * @return FS_Plugin_License[]|object
11763
- */
11764
- function _sync_licenses( $site_license_id = false, $blog_id = null ) {
11765
- $this->_logger->entrance();
11766
-
11767
- $is_network_admin = fs_is_network_admin();
11768
-
11769
- if ( $is_network_admin && is_null( $blog_id ) ) {
11770
- $all_licenses = self::get_all_licenses( $this->_module_id );
11771
- } else {
11772
- $all_licenses = $this->get_user_licenses( $this->_user->id );
11773
- }
11774
-
11775
- $foreign_licenses = $this->get_foreign_licenses_info( $all_licenses, $site_license_id );
11776
-
11777
- $all_licenses_map = array();
11778
- foreach ( $all_licenses as $license ) {
11779
- $all_licenses_map[ $license->id ] = true;
11780
- }
11781
-
11782
- $licenses = $this->_fetch_licenses( false, $site_license_id, $foreign_licenses, $blog_id );
11783
-
11784
- if ( $this->is_array_instanceof( $licenses, 'FS_Plugin_License' ) ) {
11785
- $licenses_map = array();
11786
- foreach ( $licenses as $license ) {
11787
- $licenses_map[ $license->id ] = true;
11788
- }
11789
-
11790
- // $license_ids_to_keep = $this->get_license_ids_associated_with_installs();
11791
- // foreach ( $license_ids_to_keep as $license_id ) {
11792
- // if ( isset( $licenses_map[ $license_id ] ) ) {
11793
- // continue;
11794
- // }
11795
- //
11796
- // $missing_license = self::_get_license_by_id( $license_id, false );
11797
- // if ( is_object( $missing_license ) ) {
11798
- // $licenses[] = $missing_license;
11799
- // $licenses_map[ $missing_license->id ] = true;
11800
- // }
11801
- // }
11802
-
11803
- $user_license_ids = $this->get_user_linked_license_ids( $this->_user->id );
11804
-
11805
- foreach ( $user_license_ids as $key => $license_id ) {
11806
- if ( ! isset( $licenses_map[ $license_id ] ) ) {
11807
- // Remove access to licenses that no longer exist.
11808
- unset( $user_license_ids[ $key ] );
11809
- }
11810
- }
11811
-
11812
- if ( ! empty( $user_license_ids ) ) {
11813
- foreach ( $licenses_map as $license_id => $value ) {
11814
- if ( ! isset( $all_licenses_map[ $license_id ] ) ) {
11815
- // Associate new licenses with the user who triggered the license syncing.
11816
- $user_license_ids[] = $license_id;
11817
- }
11818
- }
11819
-
11820
- $user_license_ids = array_unique( $user_license_ids );
11821
- } else {
11822
- $user_license_ids = array_keys( $licenses_map );
11823
- }
11824
-
11825
- if ( ! $is_network_admin || ! is_null( $blog_id ) ) {
11826
- $user_licenses = array();
11827
- foreach ( $licenses as $license ) {
11828
- if ( ! in_array( $license->id, $user_license_ids ) ) {
11829
- continue;
11830
- }
11831
-
11832
- $user_licenses[] = $license;
11833
- }
11834
-
11835
- $this->_licenses = $user_licenses;
11836
- } else {
11837
- $this->_licenses = $licenses;
11838
- }
11839
-
11840
- $this->set_user_linked_license_ids( $this->_user->id, $user_license_ids );
11841
-
11842
- $this->_store_licenses( true, $this->_module_id, $licenses );
11843
- }
11844
-
11845
- // Update current license.
11846
- if ( is_object( $this->_license ) ) {
11847
- $license = $this->_get_license_by_id( $this->_license->id );
11848
-
11849
- if ( is_object( $license ) ) {
11850
- /**
11851
- * `$license` can be `false` in case a user change action has just been completed and this method
11852
- * has synced the `$this->_licenses` collection for the new user. In this case, the
11853
- * `$this->_licenses` collection may have only the newly activated license that is associated with
11854
- * the new user. `set_license` will eventually be called in the same request by the logic that
11855
- * follows outside this method which will detect that the install's license has been updated, and
11856
- * then `_update_site_license` will be called which in turn will call `set_license`.
11857
- *
11858
- * @author Leo Fajardo (@leorw)
11859
- * @since 2.3.2
11860
- */
11861
- $this->set_license( $license );
11862
- }
11863
- }
11864
-
11865
- return $this->_licenses;
11866
- }
11867
-
11868
- /**
11869
- * @author Vova Feldman (@svovaf)
11870
- * @since 1.0.5
11871
- *
11872
- * @param number $id
11873
- * @param bool $sync_licenses
11874
- *
11875
- * @return FS_Plugin_License|false
11876
- */
11877
- function _get_license_by_id( $id, $sync_licenses = true ) {
11878
- $this->_logger->entrance();
11879
-
11880
- if ( ! FS_Plugin_License::is_valid_id( $id ) ) {
11881
- return false;
11882
- }
11883
-
11884
- /**
11885
- * When running from the network level admin and opted-in from the network,
11886
- * check if the license exists in the network user licenses collection.
11887
- *
11888
- * @author Vova Feldman (@svovaf)
11889
- * @since 2.0.0
11890
- */
11891
- if ( fs_is_network_admin() &&
11892
- $this->is_network_registered() &&
11893
- ( ! is_object( $this->_user ) || $this->_storage->network_user_id != $this->_user->id )
11894
- ) {
11895
- $licenses = $this->get_user_licenses( $this->_storage->network_user_id );
11896
-
11897
- foreach ( $licenses as $license ) {
11898
- if ( $id == $license->id ) {
11899
- return $license;
11900
- }
11901
- }
11902
- }
11903
-
11904
- if ( ! $this->has_any_license() && $sync_licenses ) {
11905
- $this->_sync_licenses( $id );
11906
- }
11907
-
11908
- if ( is_array( $this->_licenses ) ) {
11909
- foreach ( $this->_licenses as $license ) {
11910
- if ( $id == $license->id ) {
11911
- return $license;
11912
- }
11913
- }
11914
- }
11915
-
11916
- return false;
11917
- }
11918
-
11919
- /**
11920
- * 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.
11921
- *
11922
- * @author Vova Feldman (@svovaf)
11923
- * @since 2.0.0
11924
- *
11925
- * @param number $id
11926
- *
11927
- * @return FS_Plugin_License
11928
- */
11929
- private function get_license_by_id( $id ) {
11930
- $licenses = self::get_all_licenses( $this->_module_id );
11931
-
11932
- if ( is_array( $licenses ) && ! empty( $licenses ) ) {
11933
- foreach ( $licenses as $license ) {
11934
- if ( $id == $license->id ) {
11935
- return $license;
11936
- }
11937
- }
11938
- }
11939
-
11940
- return null;
11941
- }
11942
-
11943
- /**
11944
- * Synchronize the site's context license by fetching the license form the API and updating the local data with it.
11945
- *
11946
- * @author Vova Feldman (@svovaf)
11947
- * @since 2.0.0
11948
- *
11949
- * @return \FS_Plugin_License|mixed
11950
- */
11951
- private function sync_site_license() {
11952
- $api = $this->get_api_user_scope();
11953
-
11954
- $result = $api->get( "/licenses/{$this->_license->id}.json?license_key=" . urlencode( $this->_license->secret_key ), true );
11955
-
11956
- if ( ! $this->is_api_result_entity( $result ) ) {
11957
- return $result;
11958
- }
11959
-
11960
- $license = $this->_update_site_license( new FS_Plugin_License( $result ) );
11961
- $this->_store_licenses();
11962
-
11963
- return $license;
11964
- }
11965
-
11966
- /**
11967
- * Get all user's available licenses for the current module.
11968
- *
11969
- * @author Vova Feldman (@svovaf)
11970
- * @since 2.0.0
11971
- *
11972
- * @param number $user_id
11973
- *
11974
- * @return FS_Plugin_License[]
11975
- */
11976
- private function get_user_licenses( $user_id ) {
11977
- $all_licenses = self::get_all_licenses( $this->_module_id );
11978
- if ( empty( $all_licenses ) ) {
11979
- return array();
11980
- }
11981
-
11982
- $user_license_ids = $this->get_user_linked_license_ids( $user_id );
11983
- if ( empty( $user_license_ids ) ) {
11984
- return array();
11985
- }
11986
-
11987
- $licenses = array();
11988
- foreach ( $all_licenses as $license ) {
11989
- if ( in_array( $license->id, $user_license_ids ) ) {
11990
- $licenses[] = $license;
11991
- }
11992
- }
11993
-
11994
- return $licenses;
11995
- }
11996
-
11997
- /**
11998
- * Checks if the context license is network activated except on the given blog ID.
11999
- *
12000
- * @author Vova Feldman (@svovaf)
12001
- * @since 2.0.0
12002
- *
12003
- * @param int $except_blog_id
12004
- *
12005
- * @return bool
12006
- */
12007
- private function is_license_network_active( $except_blog_id = 0 ) {
12008
- $this->_logger->entrance();
12009
-
12010
- if ( ! is_object( $this->_license ) ) {
12011
- return false;
12012
- }
12013
-
12014
- $sites = self::get_sites();
12015
-
12016
- if ( $this->_license->total_activations() < ( count( $sites ) - 1 ) ) {
12017
- // There are more sites than the number of activations, so license cannot be network activated.
12018
- return false;
12019
- }
12020
-
12021
- foreach ( $sites as $site ) {
12022
- $blog_id = self::get_site_blog_id( $site );
12023
-
12024
- if ( $except_blog_id == $blog_id ) {
12025
- // Skip excluded blog.
12026
- continue;
12027
- }
12028
-
12029
- $install = $this->get_install_by_blog_id( $blog_id );
12030
-
12031
- if ( is_object( $install ) && $install->license_id != $this->_license->id ) {
12032
- return false;
12033
- }
12034
- }
12035
-
12036
- return true;
12037
- }
12038
-
12039
- /**
12040
- * 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.
12041
- *
12042
- * Notice: On success, this method will also update the license activations counters (without updating the license in the storage).
12043
- *
12044
- * @author Vova Feldman (@svovaf)
12045
- * @since 2.0.0
12046
- *
12047
- * @param \FS_User $user
12048
- * @param \FS_Plugin_License $license
12049
- *
12050
- * @return bool
12051
- */
12052
- private function try_activate_license_on_network( FS_User $user, FS_Plugin_License $license ) {
12053
- $this->_logger->entrance();
12054
-
12055
- $result = $this->can_activate_license_on_network( $license );
12056
-
12057
- if ( false === $result ) {
12058
- return false;
12059
- }
12060
-
12061
- $installs_without_license = $result['installs'];
12062
- if ( ! empty( $installs_without_license ) ) {
12063
- $this->activate_license_on_many_installs( $user, $license->secret_key, $installs_without_license );
12064
- }
12065
-
12066
- $disconnected_site_ids = $result['sites'];
12067
- if ( ! empty( $disconnected_site_ids ) ) {
12068
- $this->activate_license_on_many_sites( $user, $license->secret_key, $disconnected_site_ids );
12069
- }
12070
-
12071
- $this->link_license_2_user( $license->id, $user->id );
12072
-
12073
- // Sync license after activations.
12074
- $license->activated += $result['production_count'];
12075
- $license->activated_local += $result['localhost_count'];
12076
-
12077
- // $this->_store_licenses()
12078
-
12079
- return true;
12080
- }
12081
-
12082
- /**
12083
- * Checks if the given license can be activated on the whole network.
12084
- *
12085
- * @author Vova Feldman (@svovaf)
12086
- * @since 2.0.0
12087
- *
12088
- * @param \FS_Plugin_License $license
12089
- *
12090
- * @return false|array {
12091
- * @type array[int]FS_Site $installs Blog ID to install map.
12092
- * @type int[] $sites Non-connected blog IDs.
12093
- * @type int $production_count Production sites count.
12094
- * @type int $localhost_count Production sites count.
12095
- * }
12096
- */
12097
- private function can_activate_license_on_network( FS_Plugin_License $license ) {
12098
- $sites = self::get_sites();
12099
-
12100
- $production_count = 0;
12101
- $localhost_count = 0;
12102
-
12103
- $installs_without_license = array();
12104
- $disconnected_site_ids = array();
12105
-
12106
- foreach ( $sites as $site ) {
12107
- $blog_id = self::get_site_blog_id( $site );
12108
- $install = $this->get_install_by_blog_id( $blog_id );
12109
-
12110
- if ( is_object( $install ) ) {
12111
- if ( FS_Plugin_License::is_valid_id( $install->license_id ) ) {
12112
- // License already activated on the install.
12113
- continue;
12114
- }
12115
-
12116
- $url = $install->url;
12117
-
12118
- $installs_without_license[ $blog_id ] = $install;
12119
- } else {
12120
- $url = is_object( $site ) ?
12121
- $site->siteurl :
12122
- get_site_url( $blog_id );
12123
-
12124
- $disconnected_site_ids[] = $blog_id;
12125
- }
12126
-
12127
- if ( FS_Site::is_localhost_by_address( $url ) ) {
12128
- $localhost_count ++;
12129
- } else {
12130
- $production_count ++;
12131
- }
12132
- }
12133
-
12134
- if ( ! $license->can_activate_bulk( $production_count, $localhost_count ) ) {
12135
- return false;
12136
- }
12137
-
12138
- return array(
12139
- 'installs' => $installs_without_license,
12140
- 'sites' => $disconnected_site_ids,
12141
- 'production_count' => $production_count,
12142
- 'localhost_count' => $localhost_count,
12143
- );
12144
- }
12145
-
12146
- /**
12147
- * Activate a given license on a collection of installs.
12148
- *
12149
- * @author Vova Feldman (@svovaf)
12150
- * @since 2.0.0
12151
- *
12152
- * @param \FS_User $user
12153
- * @param string $license_key
12154
- * @param array $blog_2_install_map {
12155
- * @key int Blog ID.
12156
- * @value FS_Site Blog's associated install.
12157
- * }
12158
- *
12159
- * @return mixed|true
12160
- */
12161
- private function activate_license_on_many_installs(
12162
- FS_User $user,
12163
- $license_key,
12164
- array $blog_2_install_map
12165
- ) {
12166
- $params = array(
12167
- array( 'license_key' => $this->apply_filters( 'license_key', $license_key ) )
12168
- );
12169
-
12170
- $install_2_blog_map = array();
12171
- foreach ( $blog_2_install_map as $blog_id => $install ) {
12172
- $params[] = array( 'id' => $install->id );
12173
-
12174
- $install_2_blog_map[ $install->id ] = $blog_id;
12175
- }
12176
-
12177
- $result = $this->get_api_user_scope_by_user( $user )->call(
12178
- "plugins/{$this->_plugin->id}/installs.json",
12179
- 'PUT',
12180
- $params
12181
- );
12182
-
12183
- if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
12184
- return $result;
12185
- }
12186
-
12187
- foreach ( $result->installs as $r_install ) {
12188
- $install = new FS_Site( $r_install );
12189
- $install->is_disconnected = false;
12190
-
12191
- // Update install.
12192
- $this->_store_site(
12193
- true,
12194
- $install_2_blog_map[ $r_install->id ],
12195
- $install
12196
- );
12197
- }
12198
-
12199
- return true;
12200
- }
12201
-
12202
- /**
12203
- * Activate a given license on a collection of blogs/sites that are not yet opted-in.
12204
- *
12205
- * @author Vova Feldman (@svovaf)
12206
- * @since 2.3.1
12207
- *
12208
- * @param \FS_User $user
12209
- * @param string $license_key
12210
- *
12211
- * @return true|mixed True if successful, otherwise, the API result.
12212
- */
12213
- private function activate_license_on_site( FS_User $user, $license_key ) {
12214
- return $this->activate_license_on_many_sites( $user, $license_key );
12215
- }
12216
-
12217
- /**
12218
- * Activate a given license on a collection of blogs/sites that are not yet opted-in.
12219
- *
12220
- * @author Vova Feldman (@svovaf)
12221
- * @since 2.0.0
12222
- *
12223
- * @param \FS_User $user
12224
- * @param string $license_key
12225
- * @param int[] $site_ids
12226
- *
12227
- * @return true|mixed True if successful, otherwise, the API result.
12228
- */
12229
- private function activate_license_on_many_sites(
12230
- FS_User $user,
12231
- $license_key,
12232
- array $site_ids = array()
12233
- ) {
12234
- $sites = array();
12235
- foreach ( $site_ids as $site_id ) {
12236
- $sites[] = $this->get_site_info( array( 'blog_id' => $site_id ) );
12237
- }
12238
-
12239
- // Install the plugin.
12240
- $result = $this->create_installs_with_user(
12241
- $user,
12242
- $license_key,
12243
- false,
12244
- $sites,
12245
- false,
12246
- true
12247
- );
12248
-
12249
- if ( ! $this->is_api_result_entity( $result ) &&
12250
- ! $this->is_api_result_object( $result, 'installs' )
12251
- ) {
12252
- return $result;
12253
- }
12254
-
12255
- $installs = array();
12256
-
12257
- if ( $this->is_api_result_entity( $result ) ) {
12258
- $install = new FS_Site( $result );
12259
-
12260
- $this->_user = $user;
12261
-
12262
- $this->_store_site( true, null, $install );
12263
-
12264
- $this->_site = $install;
12265
-
12266
- $this->reset_anonymous_mode();
12267
- } else {
12268
- foreach ( $result->installs as $install ) {
12269
- $installs[] = new FS_Site( $install );
12270
- }
12271
-
12272
- // Map site addresses to their blog IDs.
12273
- $address_to_blog_map = $this->get_address_to_blog_map();
12274
-
12275
- $first_blog_id = null;
12276
-
12277
- foreach ( $installs as $install ) {
12278
- $address = trailingslashit( fs_strip_url_protocol( $install->url ) );
12279
- $blog_id = $address_to_blog_map[ $address ];
12280
-
12281
- $this->_store_site( true, $blog_id, $install );
12282
-
12283
- $this->reset_anonymous_mode( $blog_id );
12284
-
12285
- if ( is_null( $first_blog_id ) ) {
12286
- $first_blog_id = $blog_id;
12287
- }
12288
- }
12289
-
12290
- if ( ! FS_Site::is_valid_id( $this->_storage->network_install_blog_id ) ) {
12291
- $this->_storage->network_install_blog_id = $first_blog_id;
12292
- }
12293
- }
12294
-
12295
- return true;
12296
- }
12297
-
12298
- /**
12299
- * Sync site's license with user licenses.
12300
- *
12301
- * @author Vova Feldman (@svovaf)
12302
- * @since 1.0.6
12303
- *
12304
- * @param FS_Plugin_License|null $new_license
12305
- *
12306
- * @return FS_Plugin_License|null
12307
- */
12308
- function _update_site_license( $new_license ) {
12309
- $this->_logger->entrance();
12310
-
12311
- /**
12312
- * In case this call will be removed in the future, the `_sync_licenses()` method needs to be updated
12313
- * accordingly so that it will also handle the case when an ownership change is done via license
12314
- * activation.
12315
- *
12316
- * @author Leo Fajardo (@leorw)
12317
- * @since 2.3.2
12318
- */
12319
- $this->set_license( $new_license );
12320
-
12321
- if ( ! is_object( $new_license ) ) {
12322
- $this->_site->license_id = null;
12323
- $this->_sync_site_subscription( null );
12324
-
12325
- return $this->_license;
12326
- }
12327
-
12328
- $this->_site->license_id = $this->_license->id;
12329
-
12330
- if ( ! is_array( $this->_licenses ) ) {
12331
- $this->_licenses = array();
12332
- }
12333
-
12334
- $is_license_found = false;
12335
- for ( $i = 0, $len = count( $this->_licenses ); $i < $len; $i ++ ) {
12336
- if ( $new_license->id == $this->_licenses[ $i ]->id ) {
12337
- $this->_licenses[ $i ] = $new_license;
12338
-
12339
- $is_license_found = true;
12340
- break;
12341
- }
12342
- }
12343
-
12344
- // If new license just append.
12345
- if ( ! $is_license_found ) {
12346
- $this->_licenses[] = $new_license;
12347
- }
12348
-
12349
- $this->_sync_site_subscription( $new_license );
12350
-
12351
- return $this->_license;
12352
- }
12353
-
12354
- /**
12355
- * @author Vova Feldman (@svovaf)
12356
- * @since 2.3.1
12357
- *
12358
- * @param \FS_Plugin_License $license
12359
- */
12360
- private function set_license( FS_Plugin_License $license = null ) {
12361
- $this->_license = $license;
12362
-
12363
- $this->maybe_update_whitelabel_flag( $license );
12364
- }
12365
-
12366
- /**
12367
- * @author Leo Fajardo (@leorw)
12368
- * @since 2.3.1
12369
- *
12370
- * @param FS_Plugin_License $license
12371
- */
12372
- private function maybe_update_whitelabel_flag( $license ) {
12373
- $is_whitelabeled = isset( $this->_storage->is_whitelabeled ) ?
12374
- $this->_storage->is_whitelabeled :
12375
- false;
12376
-
12377
- if ( is_object( $license ) ) {
12378
- $license_user = self::_get_user_by_id( $license->user_id );
12379
-
12380
- if ( ! is_object( $license_user ) ) {
12381
- // If foreign license, do not update the `is_whitelabeled` flag.
12382
- return;
12383
- }
12384
-
12385
- if ( $this->is_addon() ) {
12386
- /**
12387
- * Store the last license data to the parent's storage since it's needed only when showing the
12388
- * "Start Debug" dialog which is triggered from the "Account" page. This way, there's no need to
12389
- * iterate over the add-ons just to get the last license data.
12390
- */
12391
- $this->get_parent_instance()->store_last_activated_license_data( $license, $license_user );
12392
- } else {
12393
- $this->store_last_activated_license_data( $license );
12394
- }
12395
-
12396
- if ( $license->is_whitelabeled ) {
12397
- // Activated a developer license, data should be hidden.
12398
- $is_whitelabeled = true;
12399
- } else if ( $this->is_registered() && $this->_user->id == $license->user_id ) {
12400
- // The account owner activated a regular license key, no need to hide the data.
12401
- $is_whitelabeled = false;
12402
- }
12403
- }
12404
-
12405
- $this->_storage->is_whitelabeled = $is_whitelabeled;
12406
-
12407
- // Reset the whitelabeled status after update.
12408
- $this->is_whitelabeled = null;
12409
- if ( $this->is_addon() ) {
12410
- $parent_fs = $this->get_parent_instance();
12411
-
12412
- if ( is_object( $parent_fs ) ) {
12413
- $parent_fs->is_whitelabeled = null;
12414
- }
12415
- }
12416
- }
12417
-
12418
- /**
12419
- * @author Leo Fajardo (@leorw)
12420
- * @since 2.3.1
12421
- *
12422
- * @param FS_Plugin_License $license
12423
- * @param FS_User $license_user
12424
- */
12425
- private function store_last_activated_license_data( FS_Plugin_License $license, $license_user = null ) {
12426
- if ( ! is_object( $license_user ) ) {
12427
- $this->_storage->last_license_key = md5( $license->secret_key );
12428
- $this->_storage->last_license_user_id = null;
12429
- } else {
12430
- $this->_storage->last_license_user_key = md5( $license_user->secret_key );
12431
- $this->_storage->last_license_user_id = $license_user->id;
12432
- }
12433
- }
12434
-
12435
- /**
12436
- * @author Leo Fajardo (@leorw)
12437
- * @since 2.3.1
12438
- *
12439
- * @param bool $ignore_data_debug_mode
12440
- *
12441
- * @return bool
12442
- */
12443
- function is_whitelabeled_by_flag( $ignore_data_debug_mode = false ) {
12444
- if ( true !== $this->_storage->is_whitelabeled ) {
12445
- return false;
12446
- } else if ( $ignore_data_debug_mode ) {
12447
- return true;
12448
- }
12449
-
12450
- $fs = $this->is_addon() ?
12451
- $this->get_parent_instance() :
12452
- $this;
12453
-
12454
- return ! $fs->is_data_debug_mode();
12455
- }
12456
-
12457
- /**
12458
- * @author Leo Fajardo (@leorw)
12459
- * @since 2.3.1
12460
- *
12461
- * @return number
12462
- */
12463
- function get_last_license_user_id() {
12464
- return ( FS_User::is_valid_id( $this->_storage->last_license_user_id ) ) ?
12465
- $this->_storage->last_license_user_id :
12466
- null;
12467
- }
12468
-
12469
- /**
12470
- * @author Leo Fajardo (@leorw)
12471
- * @since 2.3.1
12472
- *
12473
- * @param int $blog_id
12474
- * @param bool $ignore_data_debug_mode
12475
- *
12476
- * @return bool
12477
- */
12478
- function is_whitelabeled( $ignore_data_debug_mode = false, $blog_id = null ) {
12479
- if ( ! is_null( $blog_id ) ) {
12480
- $this->switch_to_blog( $blog_id );
12481
- }
12482
-
12483
- if ( ! is_null( $this->is_whitelabeled ) ) {
12484
- $is_whitelabeled = $this->is_whitelabeled;
12485
- } else {
12486
- $is_whitelabeled = false;
12487
-
12488
- $is_whitelabeled_flag = $this->is_whitelabeled_by_flag( true );
12489
-
12490
- if ( ! $this->has_addons() ) {
12491
- $is_whitelabeled = $is_whitelabeled_flag;
12492
- } else if ( $is_whitelabeled_flag ) {
12493
- $is_whitelabeled = true;
12494
- } else {
12495
- $addon_ids = $this->get_updated_account_addons();
12496
- $installed_addons = $this->get_installed_addons();
12497
- foreach ( $installed_addons as $fs_addon ) {
12498
- $addon_ids[] = $fs_addon->get_id();
12499
- }
12500
-
12501
- if ( ! empty( $addon_ids ) ) {
12502
- $addon_ids = array_unique( $addon_ids );
12503
-
12504
- $is_network_level = (
12505
- fs_is_network_admin() &&
12506
- $this->is_network_active()
12507
- );
12508
-
12509
- foreach ( $addon_ids as $addon_id ) {
12510
- $addon = $this->get_addon( $addon_id );
12511
-
12512
- if ( ! is_object( $addon ) ) {
12513
- continue;
12514
- }
12515
-
12516
- $addon_storage = FS_Storage::instance( WP_FS__MODULE_TYPE_PLUGIN, $addon->slug );
12517
- $fs_addon = $this->is_addon_activated( $addon_id ) ?
12518
- self::get_addon_instance( $addon_id ) :
12519
- null;
12520
-
12521
- $was_addon_network_activated = false;
12522
-
12523
- if ( is_object( $fs_addon ) ) {
12524
- $was_addon_network_activated = $fs_addon->is_network_active();
12525
- } else if ( $is_network_level ) {
12526
- $was_addon_network_activated = $addon_storage->get( 'was_plugin_loaded', false, true );
12527
- }
12528
-
12529
- $network_delegated_connection = (
12530
- $was_addon_network_activated &&
12531
- $addon_storage->get( 'is_delegated_connection', false, true )
12532
- );
12533
-
12534
- if (
12535
- $is_network_level &&
12536
- ( ! $was_addon_network_activated || $network_delegated_connection )
12537
- ) {
12538
- $sites = self::get_sites();
12539
-
12540
- /**
12541
- * If in network admin area and the add-on was not network-activated or network-activated
12542
- * and network-delegated, find any add-on whose is_whitelabeled flag is true.
12543
- */
12544
- foreach ( $sites as $site ) {
12545
- $site_info = $this->get_site_info( $site );
12546
-
12547
- if ( $addon_storage->get( 'is_whitelabeled', false, $site_info['blog_id'] ) ) {
12548
- $is_whitelabeled = true;
12549
- break;
12550
- }
12551
- }
12552
-
12553
- if ( $is_whitelabeled ) {
12554
- break;
12555
- }
12556
- } else {
12557
- /**
12558
- * This will be executed when any of the following is met:
12559
- * 1. Add-on was network-activated, not network-delegated, and in network admin area.
12560
- * 2. Add-on was network-activated, network-delegated, and in site admin area.
12561
- * 3. Add-on was not network-activated and in site admin area.
12562
- */
12563
- if ( true === $addon_storage->is_whitelabeled ) {
12564
- $is_whitelabeled = true;
12565
- break;
12566
- }
12567
- }
12568
- }
12569
- }
12570
- }
12571
-
12572
- $this->is_whitelabeled = $is_whitelabeled;
12573
-
12574
- if ( ! $is_whitelabeled || ! $this->is_data_debug_mode() ) {
12575
- $this->_admin_notices->remove_sticky( 'data_debug_mode_enabled' );
12576
- }
12577
-
12578
- if ( ! is_null( $blog_id ) ) {
12579
- $this->restore_current_blog();
12580
- }
12581
- }
12582
-
12583
- return (
12584
- $is_whitelabeled &&
12585
- ( $ignore_data_debug_mode || ! $this->is_data_debug_mode() )
12586
- );
12587
- }
12588
-
12589
- /**
12590
- * Sync site's subscription.
12591
- *
12592
- * @author Vova Feldman (@svovaf)
12593
- * @since 1.0.9
12594
- *
12595
- * @param FS_Plugin_License|null $license
12596
- *
12597
- * @return bool|\FS_Subscription
12598
- */
12599
- private function _sync_site_subscription( $license ) {
12600
- if ( ! is_object( $license ) ) {
12601
- $this->delete_unused_subscriptions();
12602
-
12603
- return false;
12604
- }
12605
-
12606
- // Load subscription details if not lifetime.
12607
- $subscription = $license->is_lifetime() ?
12608
- false :
12609
- $this->_fetch_site_license_subscription();
12610
-
12611
- if ( is_object( $subscription ) && ! isset( $subscription->error ) ) {
12612
- $this->store_subscription( $subscription );
12613
- } else {
12614
- $this->delete_unused_subscriptions();
12615
- }
12616
-
12617
- return $subscription;
12618
- }
12619
-
12620
- /**
12621
- * @author Vova Feldman (@svovaf)
12622
- * @since 1.0.6
12623
- *
12624
- * @return bool|\FS_Plugin_License
12625
- */
12626
- function _get_license() {
12627
- if ( ! fs_is_network_admin() || is_object( $this->_license ) ) {
12628
- return $this->_license;
12629
- }
12630
-
12631
- return $this->_get_available_premium_license();
12632
- }
12633
-
12634
- /**
12635
- * @param number $license_id
12636
- *
12637
- * @return null|\FS_Subscription
12638
- */
12639
- function _get_subscription( $license_id ) {
12640
- if ( ! isset( $this->_storage->subscriptions ) ||
12641
- empty( $this->_storage->subscriptions )
12642
- ) {
12643
- return null;
12644
- }
12645
-
12646
- foreach ( fs_get_entities( $this->_storage->subscriptions, FS_Subscription::get_class_name() ) as $subscription ) {
12647
- if ( $subscription->license_id == $license_id ) {
12648
- return $subscription;
12649
- }
12650
- }
12651
-
12652
- return null;
12653
- }
12654
-
12655
- /**
12656
- * @author Leo Fajardo (@leorw)
12657
- * @since 2.0.0
12658
- *
12659
- * @param FS_Subscription $subscription
12660
- */
12661
- function store_subscription( FS_Subscription $subscription ) {
12662
- if ( ! isset( $this->_storage->subscriptions ) ) {
12663
- $this->_storage->subscriptions = array();
12664
- }
12665
-
12666
- if ( empty( $this->_storage->subscriptions ) || ! is_multisite() ) {
12667
- $this->_storage->subscriptions = array( $subscription );
12668
-
12669
- return;
12670
- }
12671
-
12672
- $subscriptions = fs_get_entities( $this->_storage->subscriptions, FS_Subscription::get_class_name() );
12673
-
12674
- $updated_subscription = false;
12675
- foreach ( $subscriptions as $key => $existing_subscription ) {
12676
- if ( $existing_subscription->id == $subscription->id ) {
12677
- $subscriptions[ $key ] = $subscription;
12678
- $updated_subscription = true;
12679
- break;
12680
- }
12681
- }
12682
-
12683
- if ( ! $updated_subscription ) {
12684
- $subscriptions[] = $subscription;
12685
- }
12686
-
12687
- $this->_storage->subscriptions = $subscriptions;
12688
- }
12689
-
12690
- /**
12691
- * @author Leo Fajardo (@leorw)
12692
- * @since 2.0.0
12693
- */
12694
- function delete_unused_subscriptions() {
12695
- if ( ! isset( $this->_storage->subscriptions ) ||
12696
- empty( $this->_storage->subscriptions ) ||
12697
- // Clean up only if there are already at least 3 subscriptions.
12698
- ( count( $this->_storage->subscriptions ) < 3 )
12699
- ) {
12700
- return;
12701
- }
12702
-
12703
- if ( ! is_multisite() ) {
12704
- // If not multisite, there should only be 1 subscription, so just clear the array.
12705
- $this->_storage->subscriptions = array();
12706
-
12707
- return;
12708
- }
12709
-
12710
- $subscriptions_to_keep_by_license_id_map = array();
12711
- $sites = self::get_sites();
12712
- foreach ( $sites as $site ) {
12713
- $blog_id = self::get_site_blog_id( $site );
12714
- $install = $this->get_install_by_blog_id( $blog_id );
12715
-
12716
- if ( ! is_object( $install ) ||
12717
- ! FS_Plugin_License::is_valid_id( $install->license_id )
12718
- ) {
12719
- continue;
12720
- }
12721
-
12722
- $subscriptions_to_keep_by_license_id_map[ $install->license_id ] = true;
12723
- }
12724
-
12725
- if ( empty( $subscriptions_to_keep_by_license_id_map ) ) {
12726
- $this->_storage->subscriptions = array();
12727
-
12728
- return;
12729
- }
12730
-
12731
- foreach ( $this->_storage->subscriptions as $key => $subscription ) {
12732
- if ( ! isset( $subscriptions_to_keep_by_license_id_map[ $subscription->license_id ] ) ) {
12733
- unset( $this->_storage->subscriptions[ $key ] );
12734
- }
12735
- }
12736
- }
12737
-
12738
- /**
12739
- * @author Vova Feldman (@svovaf)
12740
- * @since 1.0.2
12741
- *
12742
- * @param string $plan Plan name
12743
- * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
12744
- *
12745
- * @return bool
12746
- */
12747
- function is_plan( $plan, $exact = false ) {
12748
- $this->_logger->entrance();
12749
-
12750
- if ( ! $this->is_registered() ) {
12751
- return false;
12752
- }
12753
-
12754
- $plan = strtolower( $plan );
12755
-
12756
- $current_plan_name = $this->get_plan_name();
12757
-
12758
- if ( $current_plan_name === $plan ) {
12759
- // Exact plan.
12760
- return true;
12761
- } else if ( $exact ) {
12762
- // Required exact, but plans are different.
12763
- return false;
12764
- }
12765
-
12766
- $current_plan_order = - 1;
12767
- $required_plan_order = PHP_INT_MAX;
12768
- for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
12769
- if ( $plan === $this->_plans[ $i ]->name ) {
12770
- $required_plan_order = $i;
12771
- } else if ( $current_plan_name === $this->_plans[ $i ]->name ) {
12772
- $current_plan_order = $i;
12773
- }
12774
- }
12775
-
12776
- return ( $current_plan_order > $required_plan_order );
12777
- }
12778
-
12779
- /**
12780
- * Check if module has only one plan.
12781
- *
12782
- * @author Vova Feldman (@svovaf)
12783
- * @since 1.2.1.7
12784
- *
12785
- * @param bool $double_check In some cases developers prefer to release their paid offering as premium-only, even though there is a free version. For those cases, looking at the 'is_premium_only' value isn't enough because the result will return false even when the product has only signle paid plan.
12786
- *
12787
- * @return bool
12788
- */
12789
- function is_single_plan( $double_check = false ) {
12790
- $this->_logger->entrance();
12791
-
12792
- if ( ! $this->is_registered() ||
12793
- ! is_array( $this->_plans ) ||
12794
- 0 === count( $this->_plans )
12795
- ) {
12796
- return true;
12797
- }
12798
-
12799
- $has_free_plan = $this->has_free_plan();
12800
-
12801
- if ( ! $has_free_plan && $double_check ) {
12802
- foreach ( $this->_plans as $plan ) {
12803
- if ( $plan->is_free() ) {
12804
- $has_free_plan = true;
12805
- break;
12806
- }
12807
- }
12808
- }
12809
-
12810
- return ( 1 === ( count( $this->_plans ) - ( $has_free_plan ? 1 : 0 ) ) );
12811
- }
12812
-
12813
- /**
12814
- * Check if plan based on trial. If not in trial mode, should return false.
12815
- *
12816
- * @since 1.0.9
12817
- *
12818
- * @param string $plan Plan name
12819
- * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
12820
- *
12821
- * @return bool
12822
- */
12823
- function is_trial_plan( $plan, $exact = false ) {
12824
- $this->_logger->entrance();
12825
-
12826
- if ( ! $this->is_registered() ) {
12827
- return false;
12828
- }
12829
-
12830
- if ( ! $this->is_trial() ) {
12831
- return false;
12832
- }
12833
-
12834
- $trial_plan = $this->get_trial_plan();
12835
-
12836
- if ( $trial_plan->name === $plan ) {
12837
- // Exact plan.
12838
- return true;
12839
- } else if ( $exact ) {
12840
- // Required exact, but plans are different.
12841
- return false;
12842
- }
12843
-
12844
- $current_plan_order = - 1;
12845
- $required_plan_order = - 1;
12846
- for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
12847
- if ( $plan === $this->_plans[ $i ]->name ) {
12848
- $required_plan_order = $i;
12849
- } else if ( $trial_plan->name === $this->_plans[ $i ]->name ) {
12850
- $current_plan_order = $i;
12851
- }
12852
- }
12853
-
12854
- return ( $current_plan_order > $required_plan_order );
12855
- }
12856
-
12857
- /**
12858
- * Check if plugin has any paid plans.
12859
- *
12860
- * @author Vova Feldman (@svovaf)
12861
- * @since 1.0.7
12862
- *
12863
- * @return bool
12864
- */
12865
- function has_paid_plan() {
12866
- return $this->_has_paid_plans ||
12867
- FS_Plan_Manager::instance()->has_paid_plan( $this->_plans );
12868
- }
12869
-
12870
- /**
12871
- * Check if plugin has any plan with a trail.
12872
- *
12873
- * @author Vova Feldman (@svovaf)
12874
- * @since 1.0.9
12875
- *
12876
- * @return bool
12877
- */
12878
- function has_trial_plan() {
12879
- /**
12880
- * @author Vova Feldman(@svovaf)
12881
- * @since 1.2.1.5
12882
- *
12883
- * Allow setting a trial from the SDK without calling the API.
12884
- * But, if the user did opt-in, continue using the real data from the API.
12885
- */
12886
- if ( $this->_trial_days >= 0 ) {
12887
- return true;
12888
- }
12889
-
12890
- return $this->_storage->get( 'has_trial_plan', false );
12891
- }
12892
-
12893
- /**
12894
- * Check if plugin has any free plan, or is it premium only.
12895
- *
12896
- * Note: If no plans configured, assume plugin is free.
12897
- *
12898
- * @author Vova Feldman (@svovaf)
12899
- * @since 1.0.7
12900
- *
12901
- * @return bool
12902
- */
12903
- function has_free_plan() {
12904
- return ! $this->is_only_premium();
12905
- }
12906
-
12907
- /**
12908
- * Displays a license activation dialog box when the user clicks on the "Activate License"
12909
- * or "Change License" link on the plugins
12910
- * page.
12911
- *
12912
- * @author Leo Fajardo (@leorw)
12913
- * @since 1.1.9
12914
- */
12915
- function _add_license_activation_dialog_box() {
12916
- $vars = array(
12917
- 'id' => $this->_module_id,
12918
- );
12919
-
12920
- fs_require_template( 'forms/license-activation.php', $vars );
12921
- fs_require_template( 'forms/resend-key.php', $vars );
12922
- }
12923
-
12924
- /**
12925
- * Returns a collection of IDs of installs that are associated with the context product and its add-ons, and activated with foreign licenses.
12926
- *
12927
- * @author Leo Fajardo (@leorw)
12928
- * @since 2.3.2
12929
- *
12930
- * @return number[]
12931
- */
12932
- function get_installs_ids_with_foreign_licenses() {
12933
- $installs = array();
12934
-
12935
- if (
12936
- is_object( $this->_license ) &&
12937
- $this->_site->user_id != $this->_license->user_id
12938
- ) {
12939
- $installs[] = $this->_site->id;
12940
- }
12941
-
12942
- /**
12943
- * Also try to get foreign licenses for the context product's add-ons.
12944
- */
12945
- $installs_by_slug_map = $this->get_parent_and_addons_installs_info();
12946
-
12947
- foreach ( $installs_by_slug_map as $slug => $install_info ) {
12948
- if ( $slug == $this->get_slug() ) {
12949
- continue;
12950
- }
12951
-
12952
- $install = $install_info['install'];
12953
- $license = $install_info['license'];
12954
-
12955
- if (
12956
- is_object( $license ) &&
12957
- $install->user_id != $license->user_id
12958
- ) {
12959
- $installs[] = $install->id;
12960
- }
12961
- }
12962
-
12963
- return $installs;
12964
- }
12965
-
12966
- /**
12967
- * Displays the "Change User" dialog box when the user clicks on the "Change User" button on the "Account" page.
12968
- *
12969
- * @author Leo Fajardo (@leorw)
12970
- * @since 2.3.2
12971
- *
12972
- * @param number[] $install_ids
12973
- */
12974
- function _add_user_change_dialog_box( $install_ids ) {
12975
- $vars = array(
12976
- 'id' => $this->_module_id,
12977
- 'license_owners' => $this->fetch_installs_licenses_owners_data( $install_ids )
12978
- );
12979
-
12980
- fs_require_template( 'forms/user-change.php', $vars );
12981
- }
12982
-
12983
- /**
12984
- * @author Leo Fajardo (@leorw)
12985
- * @since 2.3.1
12986
- */
12987
- function _add_data_debug_mode_dialog_box() {
12988
- $vars = array(
12989
- 'id' => $this->_module_id,
12990
- );
12991
-
12992
- fs_require_template( 'forms/data-debug-mode.php', $vars );
12993
- }
12994
-
12995
- /**
12996
- * Displays a subscription cancellation dialog box when the user clicks on the "Deactivate License"
12997
- * link on the "Account" page or deactivates a plugin and there's an active subscription that is
12998
- * either associated with a non-lifetime single-site license or non-lifetime multisite license that
12999
- * is only activated on a single production site.
13000
- *
13001
- * @author Leo Fajardo (@leorw)
13002
- * @since 2.2.1
13003
- *
13004
- * @param bool $is_license_deactivation
13005
- *
13006
- * @return array
13007
- */
13008
- function _get_subscription_cancellation_dialog_box_template_params( $is_license_deactivation = false ) {
13009
- if ( fs_is_network_admin() ) {
13010
- // Subscription cancellation dialog box is currently not supported for multisite networks.
13011
- return array();
13012
- }
13013
-
13014
- if ( $this->is_whitelabeled() ) {
13015
- return array();
13016
- }
13017
-
13018
- $license = $this->_get_license();
13019
-
13020
- /**
13021
- * 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.
13022
- *
13023
- * @author Leo Fajardo (@leorw) (Comment added by Vova Feldman @svovaf)
13024
- * @since 2.2.1
13025
- */
13026
- if ( ! is_object( $license ) ||
13027
- $license->is_lifetime() ||
13028
- ( ! $license->is_single_site() && $license->activated > 1 )
13029
- ) {
13030
- return array();
13031
- }
13032
-
13033
- /**
13034
- * @var FS_Subscription $subscription
13035
- */
13036
- $subscription = $this->_get_subscription( $license->id );
13037
- if ( ! is_object( $subscription ) || ! $subscription->is_active() ) {
13038
- return array();
13039
- }
13040
-
13041
- return array(
13042
- 'id' => $this->_module_id,
13043
- 'license' => $license,
13044
- 'has_trial' => $this->is_paid_trial(),
13045
- 'is_license_deactivation' => $is_license_deactivation,
13046
- );
13047
- }
13048
-
13049
- /**
13050
- * @author Leo Fajardo (@leorw)
13051
- * @since 2.0.2
13052
- */
13053
- function _add_premium_version_upgrade_selection_dialog_box() {
13054
- $modules_update = get_site_transient( $this->is_theme() ? 'update_themes' : 'update_plugins' );
13055
- if ( ! isset( $modules_update->response[ $this->_plugin_basename ] ) ) {
13056
- return;
13057
- }
13058
-
13059
- $vars = array(
13060
- 'id' => $this->_module_id,
13061
- 'new_version' => is_object( $modules_update->response[ $this->_plugin_basename ] ) ?
13062
- $modules_update->response[ $this->_plugin_basename ]->new_version :
13063
- $modules_update->response[ $this->_plugin_basename ]['new_version']
13064
- );
13065
-
13066
- fs_require_template( 'forms/premium-versions-upgrade-metadata.php', $vars );
13067
- fs_require_once_template( 'forms/premium-versions-upgrade-handler.php', $vars );
13068
- }
13069
-
13070
- /**
13071
- * Displays the opt-out dialog box when the user clicks on the "Opt Out" link on the "Plugins"
13072
- * page.
13073
- *
13074
- * @author Leo Fajardo (@leorw)
13075
- * @since 1.2.1.5
13076
- */
13077
- function _add_optout_dialog() {
13078
- if ( $this->is_theme() ) {
13079
- $vars = null;
13080
- fs_require_once_template( '/js/jquery.content-change.php', $vars );
13081
- }
13082
-
13083
- $vars = array( 'id' => $this->_module_id );
13084
- fs_require_template( 'forms/optout.php', $vars );
13085
- }
13086
-
13087
- /**
13088
- * Prepare page to include all required UI and logic for the license activation dialog.
13089
- *
13090
- * @author Vova Feldman (@svovaf)
13091
- * @since 1.2.0
13092
- */
13093
- function _add_license_activation() {
13094
- if ( $this->is_migration() ) {
13095
- return;
13096
- }
13097
-
13098
- if ( ! $this->is_user_admin() ) {
13099
- // Only admins can activate a license.
13100
- return;
13101
- }
13102
-
13103
- if ( ! $this->has_paid_plan() ) {
13104
- // Module doesn't have any paid plans.
13105
- return;
13106
- }
13107
-
13108
- if (
13109
- $this->has_premium_version() &&
13110
- ! $this->is_premium() &&
13111
- /**
13112
- * Also handle the case when an upgrade was made using the free version.
13113
- *
13114
- * @author Leo Fajardo (@leorw)
13115
- * @since 2.3.2
13116
- */
13117
- ! is_object( $this->_get_license() )
13118
- ) {
13119
- // Only add license activation logic to the premium version, or in case of a serviceware plugin, also in the free version.
13120
- return;
13121
- }
13122
-
13123
- // Add license activation link and AJAX request handler.
13124
- if ( self::is_plugins_page() ) {
13125
- $is_network_admin = fs_is_network_admin();
13126
-
13127
- if (
13128
- ( $is_network_admin && $this->is_network_active() && ! $this->is_network_delegated_connection() ) ||
13129
- ( ! $is_network_admin && ( ! $this->is_network_active() || $this->is_delegated_connection() ) )
13130
- ) {
13131
- /**
13132
- * @since 1.2.0 Add license action link only on plugins page.
13133
- */
13134
- $this->_add_license_action_link();
13135
- }
13136
- }
13137
-
13138
- // Add license activation AJAX callback.
13139
- $this->add_ajax_action( 'activate_license', array( $this, '_activate_license_ajax_action' ) );
13140
-
13141
- // Add resend license AJAX callback.
13142
- $this->add_ajax_action( 'resend_license_key', array( $this, '_resend_license_key_ajax_action' ) );
13143
- }
13144
-
13145
- /**
13146
- * Prepares page to include all required UI and logic for the "Change User" dialog.
13147
- *
13148
- * @author Leo Fajardo (@leorw)
13149
- * @since 2.3.2
13150
- */
13151
- function _add_user_change_option() {
13152
- if ( ! $this->should_handle_user_change() ) {
13153
- return;
13154
- }
13155
-
13156
- $installs_ids_with_foreign_licenses = $this->get_installs_ids_with_foreign_licenses();
13157
-
13158
- if ( empty( $installs_ids_with_foreign_licenses ) ) {
13159
- // Handle user change only when the parent product or one of its add-ons is activated with a foreign license.
13160
- return;
13161
- }
13162
-
13163
- // Add user change AJAX handler.
13164
- $this->add_ajax_action( 'change_user', array( $this, '_user_change_ajax_action' ) );
13165
- }
13166
-
13167
- /**
13168
- * @author Leo Fajardo (@leorw)
13169
- * @since 2.3.2
13170
- */
13171
- function should_handle_user_change() {
13172
- if ( ! $this->is_user_admin() ) {
13173
- // Only admins can change user.
13174
- return false;
13175
- }
13176
-
13177
- if ( $this->is_addon() ) {
13178
- return false;
13179
- }
13180
-
13181
- if ( ! $this->is_registered() ) {
13182
- return false;
13183
- }
13184
-
13185
- if (
13186
- $this->is_network_active() &&
13187
- ( fs_is_network_admin() || ! $this->is_site_delegated_connection() )
13188
- ) {
13189
- // Handle only on site-level "Account" section for now.
13190
- return false;
13191
- }
13192
-
13193
- return true;
13194
- }
13195
-
13196
- /**
13197
- * @author Leo Fajardo (@leorw)
13198
- * @since 2.0.2
13199
- */
13200
- function _add_premium_version_upgrade_selection() {
13201
- if ( ! $this->is_user_admin() ) {
13202
- return;
13203
- }
13204
-
13205
- if ( ! $this->is_premium() || $this->has_any_active_valid_license() ) {
13206
- // This is relevant only to the free versions and premium versions without an active license.
13207
- return;
13208
- }
13209
-
13210
- if ( self::is_updates_page() || ( $this->is_plugin() && self::is_plugins_page() ) ) {
13211
- $this->_add_premium_version_upgrade_selection_action();
13212
- }
13213
- }
13214
-
13215
- /**
13216
- * @author Edgar Melkonyan
13217
- * @since 2.4.1
13218
- *
13219
- * @throws Freemius_Exception
13220
- */
13221
- function _toggle_whitelabel_mode_ajax_handler() {
13222
- $this->_logger->entrance();
13223
-
13224
- $this->check_ajax_referer( 'toggle_whitelabel_mode' );
13225
-
13226
- if ( ! $this->is_user_admin() ) {
13227
- // Only for admins.
13228
- self::shoot_ajax_failure();
13229
- }
13230
-
13231
- $license = $this->get_api_user_scope()->call(
13232
- "/licenses/{$this->_site->license_id}.json",
13233
- 'put',
13234
- array( 'is_whitelabeled' => ! $this->_license->is_whitelabeled )
13235
- );
13236
-
13237
- if ( ! $this->is_api_result_entity( $license ) ) {
13238
- self::shoot_ajax_failure(
13239
- FS_Api::is_api_error_object( $license ) ?
13240
- $license->error->message :
13241
- fs_text_inline( "An unknown error has occurred while trying to toggle the license's white-label mode.", 'unknown-error-occurred', $this->get_slug() )
13242
- );
13243
- }
13244
-
13245
- $this->_license->is_whitelabeled = $license->is_whitelabeled;
13246
- $this->_store_licenses();
13247
-
13248
- $this->_sync_license();
13249
-
13250
- if ( ! $license->is_whitelabeled ) {
13251
- $this->_admin_notices->remove_sticky( 'license_whitelabeled' );
13252
- } else {
13253
- $this->_admin_notices->add_sticky(
13254
- sprintf(
13255
- $this->get_text_inline(
13256
- 'Your %s license was flagged as white-labeled to hide sensitive information from the WP Admin (e.g. your email, license key, prices, billing address & invoices). If you ever wish to revert it back, you can easily do it through your %s. If this was a mistake you can also %s.',
13257
- 'license_whitelabeled'
13258
- ),
13259
- "<strong>{$this->get_plugin_title()}</strong>",
13260
- sprintf( '<a href="https://users.freemius.com" target="_blank">%s</a>', $this->get_text_inline( 'User Dashboard', 'user-dashboard' ) ),
13261
- sprintf( '<a href="#" class="fs-toggle-whitelabel-mode">%s</a>', $this->get_text_inline( 'revert it now', 'revert-it-now' ) )
13262
- ),
13263
- 'license_whitelabeled'
13264
- );
13265
- }
13266
-
13267
- self::shoot_ajax_response( array( 'success' => true ) );
13268
- }
13269
-
13270
- /**
13271
- * @author Leo Fajardo (@leorw)
13272
- * @since 2.3.0
13273
- */
13274
- function _add_beta_mode_update_handler() {
13275
- if ( ! $this->is_user_admin() ) {
13276
- return;
13277
- }
13278
-
13279
- if ( ! $this->is_premium() ) {
13280
- return;
13281
- }
13282
-
13283
- $this->add_ajax_action( 'set_beta_mode', array( $this, '_set_beta_mode_ajax_handler' ) );
13284
- }
13285
-
13286
- /**
13287
- * @author Leo Fajardo (@leorw)
13288
- * @since 2.3.0
13289
- */
13290
- function _set_beta_mode_ajax_handler() {
13291
- $this->_logger->entrance();
13292
-
13293
- $this->check_ajax_referer( 'set_beta_mode' );
13294
-
13295
- if ( ! $this->is_user_admin() ) {
13296
- // Only for admins.
13297
- self::shoot_ajax_failure();
13298
- }
13299
-
13300
- $is_beta = trim( fs_request_get( 'is_beta', '', 'post' ) );
13301
-
13302
- if ( empty( $is_beta ) || ! in_array( $is_beta, array( 'true', 'false' ) ) ) {
13303
- self::shoot_ajax_failure();
13304
- }
13305
-
13306
- $site = $this->get_api_site_scope()->call(
13307
- '',
13308
- 'put',
13309
- array(
13310
- 'is_beta' => ( 'true' == $is_beta ),
13311
- 'fields' => 'is_beta'
13312
- )
13313
- );
13314
-
13315
- if ( ! $this->is_api_result_entity( $site ) ) {
13316
- self::shoot_ajax_failure(
13317
- FS_Api::is_api_error_object( $site ) ?
13318
- $site->error->message :
13319
- fs_text_inline( "An unknown error has occurred while trying to set the user's beta mode.", 'unknown-error-occurred', $this->get_slug() )
13320
- );
13321
- }
13322
-
13323
- $this->_site->is_beta = $site->is_beta;
13324
- $this->_store_site();
13325
-
13326
- self::shoot_ajax_response( array( 'success' => true ) );
13327
- }
13328
-
13329
- /**
13330
- * License activation WP AJAX handler.
13331
- *
13332
- * @author Leo Fajardo (@leorw)
13333
- * @since 1.1.9
13334
- *
13335
- * @uses Freemius::activate_license()
13336
- */
13337
- function _activate_license_ajax_action() {
13338
- $this->_logger->entrance();
13339
-
13340
- $this->check_ajax_referer( 'activate_license' );
13341
-
13342
- $license_key = trim( fs_request_get( 'license_key' ) );
13343
-
13344
- if ( empty( $license_key ) ) {
13345
- exit;
13346
- }
13347
-
13348
- $sites = fs_is_network_admin() ?
13349
- fs_request_get( 'sites', array(), 'post' ) :
13350
- array();
13351
-
13352
- $result = $this->activate_license(
13353
- $license_key,
13354
- $sites,
13355
- fs_request_get_bool( 'is_marketing_allowed', null ),
13356
- fs_request_get( 'blog_id', null ),
13357
- fs_request_get( 'module_id', null, 'post' ),
13358
- fs_request_get( 'user_id', null ),
13359
- fs_request_get_bool( 'is_extensions_tracking_allowed', null )
13360
- );
13361
-
13362
- if (
13363
- $result['success'] &&
13364
- $this->is_bundle_license_auto_activation_enabled()
13365
- ) {
13366
- $license = new FS_Plugin_License();
13367
- $license->secret_key = $license_key;
13368
-
13369
- $this->maybe_activate_bundle_license( $license, $sites );
13370
- }
13371
-
13372
- echo json_encode( $result );
13373
-
13374
- exit;
13375
- }
13376
-
13377
- /**
13378
- * User change WP AJAX handler.
13379
- *
13380
- * @author Leo Fajardo (@leorw)
13381
- * @since 2.3.2
13382
- */
13383
- function _user_change_ajax_action() {
13384
- $this->_logger->entrance();
13385
-
13386
- $this->check_ajax_referer( 'change_user' );
13387
-
13388
- $new_email_address = trim( fs_request_get( 'email_address', '' ) );
13389
- $new_user_id = fs_request_get( 'user_id' );
13390
-
13391
- if ( empty( $new_email_address ) && ! FS_User::is_valid_id( $new_user_id ) ) {
13392
- self::shoot_ajax_failure( fs_text_inline( 'Invalid new user ID or email address.', 'invalid-new-user-id-or-email', $this->get_slug() ) );
13393
- }
13394
-
13395
- $params = array();
13396
-
13397
- if ( ! empty( $new_email_address ) ) {
13398
- $params['user_email'] = $new_email_address;
13399
- } else {
13400
- $params['user_id'] = $new_user_id;
13401
- }
13402
-
13403
- $installs_info_by_slug_map = $this->get_parent_and_addons_installs_info();
13404
- $install_ids = array();
13405
-
13406
- foreach ( $installs_info_by_slug_map as $slug => $install_info ) {
13407
- $install_ids[ $slug ] = $install_info['install']->id;
13408
- }
13409
-
13410
- $params['install_ids'] = implode( ',', array_values( $install_ids ) );
13411
-
13412
- $install = $this->get_api_site_scope()->call( $this->add_show_pending( '/' ), 'put', $params );
13413
-
13414
- if ( FS_Api::is_api_error( $install ) ) {
13415
- $error = '';
13416
-
13417
- if ( is_object( $install ) ) {
13418
- switch ( $install->error->code ) {
13419
- case 'user_exist':
13420
- $error = (
13421
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...' .
13422
- $this->get_text_inline( 'Sorry, we could not complete the email update. Another user with the same email is already registered.', 'user-exist-message' ) . ' ' .
13423
- sprintf( $this->get_text_inline( 'If you would like to give up the ownership of the %s\'s account to %s click the Change Ownership button.', 'user-exist-message_ownership' ), $this->_module_type, '<b>' . $new_email_address . '</b>' ) .
13424
- sprintf(
13425
- '<a style="line-height: 40px;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
13426
- $this->get_account_url( 'change_owner', array(
13427
- 'state' => 'init',
13428
- 'candidate_email' => $new_email_address
13429
- ) ),
13430
- $this->get_text_inline( 'Change Ownership', 'change-ownership' )
13431
- )
13432
- );
13433
- break;
13434
- }
13435
- }
13436
-
13437
- if ( empty( $error ) ) {
13438
- $error = FS_Api::is_api_error_object( $install ) ?
13439
- $install->error->message :
13440
- var_export( $install->error, true );
13441
- }
13442
-
13443
- self::shoot_ajax_failure( $error );
13444
- } else {
13445
- if (
13446
- // If successful ownership change.
13447
- $this->get_user()->id != $install->user_id ||
13448
- ! empty( $new_email_address )
13449
- ) {
13450
- $this->complete_ownership_change_by_license( $install->user_id, $install_ids );
13451
- }
13452
- }
13453
-
13454
- self::shoot_ajax_success();
13455
- }
13456
-
13457
- /**
13458
- * @author Leo Fajardo (@leorw)
13459
- * @since 2.3.2.14
13460
- */
13461
- function starting_migration() {
13462
- if ( ! empty( $this->_storage->license_migration ) ) {
13463
- // Do not overwrite the data if already set.
13464
- return;
13465
- }
13466
-
13467
- $this->_storage->license_migration = array(
13468
- 'is_migrating' => true,
13469
- 'start_timestamp' => time()
13470
- );
13471
- }
13472
-
13473
- /**
13474
- * @author Leo Fajardo (@leorw)
13475
- * @since 2.3.2.14
13476
- */
13477
- function is_migration() {
13478
- if ( $this->is_addon() ) {
13479
- return $this->get_parent_instance()->is_migration();
13480
- }
13481
-
13482
- if ( empty( $this->_storage->license_migration ) ) {
13483
- return false;
13484
- }
13485
-
13486
- if ( ! $this->_storage->license_migration['is_migrating'] ) {
13487
- return false;
13488
- }
13489
-
13490
- return (
13491
- // Return `true` if the migration is within 5 minutes from the starting time.
13492
- ( time() - $this->_storage->license_migration['start_timestamp'] ) <= WP_FS__TIME_5_MIN_IN_SEC
13493
- );
13494
- }
13495
-
13496
- /**
13497
- *
13498
- * A helper method to activate migrated licenses. If the product is network activated and integrated, the method will network activate the license.
13499
- *
13500
- * @author Vova Feldman (@svovaf)
13501
- * @since 2.3.0
13502
- *
13503
- * @param string $license_key
13504
- * @param null|bool $is_marketing_allowed
13505
- * @param null|number $plugin_id
13506
- * @param array $sites
13507
- * @param int $blog_id
13508
- *
13509
- * @return array {
13510
- * @var bool $success
13511
- * @var string $error
13512
- * @var string $next_page
13513
- * }
13514
- *
13515
- * @uses Freemius::activate_license()
13516
- */
13517
- function activate_migrated_license(
13518
- $license_key,
13519
- $is_marketing_allowed = null,
13520
- $plugin_id = null,
13521
- $sites = array(),
13522
- $blog_id = null
13523
- ) {
13524
- $this->_logger->entrance();
13525
-
13526
- $result = $this->activate_license(
13527
- $license_key,
13528
- ( empty( $sites ) && is_null( $blog_id ) && $this->is_network_active() ) ?
13529
- $this->get_sites_for_network_level_optin() :
13530
- $sites,
13531
- $is_marketing_allowed,
13532
- $blog_id,
13533
- $plugin_id
13534
- );
13535
-
13536
- // No need to show the sticky after license activation notice after migrating a license.
13537
- $this->_admin_notices->remove_sticky( 'plan_upgraded' );
13538
-
13539
- return $result;
13540
- }
13541
-
13542
- /**
13543
- * @author Leo Fajardo (@leorw)
13544
- * @since 2.3.1
13545
- *
13546
- * @return string
13547
- */
13548
- function get_pricing_js_path() {
13549
- if ( ! isset( $this->_pricing_js_path ) ) {
13550
- $pricing_js_path = $this->apply_filters( 'freemius_pricing_js_path', '' );
13551
-
13552
- if ( empty( $pricing_js_path ) ) {
13553
- global $fs_active_plugins;
13554
-
13555
- foreach ( $fs_active_plugins->plugins as $sdk_path => $data ) {
13556
- if ( $data->plugin_path == $this->get_plugin_basename() ) {
13557
- $plugin_or_theme_root_dir = ( $this->is_plugin() ? WP_PLUGIN_DIR : get_theme_root( get_stylesheet() ) );
13558
-
13559
- $pricing_js_path = $plugin_or_theme_root_dir
13560
- . '/'
13561
- // The basename will be `plugins`, `themes`, or the basename of a custom plugins or themes directory.
13562
- . str_replace( '../' . basename( $plugin_or_theme_root_dir ) . '/', '', $sdk_path )
13563
- . '/includes/freemius-pricing/freemius-pricing.js';
13564
-
13565
- break;
13566
- }
13567
- }
13568
- }
13569
-
13570
- $this->_pricing_js_path = $pricing_js_path;
13571
- }
13572
-
13573
- return $this->_pricing_js_path;
13574
- }
13575
-
13576
- /**
13577
- * @author Leo Fajardo (@leorw)
13578
- * @since 2.3.1
13579
- *
13580
- * @return bool
13581
- */
13582
- function should_use_external_pricing() {
13583
- if ( is_null( $this->_use_external_pricing ) ) {
13584
- $pricing_js_path = $this->get_pricing_js_path();
13585
-
13586
- $this->_use_external_pricing = ( empty( $pricing_js_path ) || ! file_exists( $pricing_js_path ) );
13587
- }
13588
-
13589
- return $this->_use_external_pricing;
13590
- }
13591
-
13592
- /**
13593
- * The implementation of this method was previously in `_activate_license_ajax_action()`.
13594
- *
13595
- * @author Vova Feldman (@svovaf)
13596
- * @since 2.2.4
13597
- * @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).
13598
- * @param string $license_key
13599
- * @param array $sites
13600
- * @param null|bool $is_marketing_allowed
13601
- * @param null|int $blog_id
13602
- * @param null|number $plugin_id
13603
- * @param null|number $license_owner_id
13604
- *
13605
- * @return array {
13606
- * @var bool $success
13607
- * @var string $error
13608
- * @var string $next_page
13609
- * }
13610
- */
13611
- private function activate_license(
13612
- $license_key,
13613
- $sites = array(),
13614
- $is_marketing_allowed = null,
13615
- $blog_id = null,
13616
- $plugin_id = null,
13617
- $license_owner_id = null,
13618
- $is_extensions_tracking_allowed = null
13619
- ) {
13620
- $this->_logger->entrance();
13621
-
13622
- $license_key = trim( $license_key );
13623
-
13624
- $is_network_activation_or_migration = (
13625
- fs_is_network_admin() ||
13626
- ( ! empty( $sites ) && $this->is_migration() )
13627
- );
13628
-
13629
- if ( ! $is_network_activation_or_migration ) {
13630
- // If the license activation is executed outside the context of a network admin, ignore the sites collection.
13631
- $sites = array();
13632
- }
13633
-
13634
- $fs = ( empty($plugin_id) || $plugin_id == $this->_module_id ) ?
13635
- $this :
13636
- $this->get_addon_instance( $plugin_id );
13637
-
13638
- $this->update_extensions_tracking_flag( $is_extensions_tracking_allowed );
13639
-
13640
- $error = false;
13641
- $next_page = false;
13642
-
13643
- $has_valid_blog_id = is_numeric( $blog_id );
13644
-
13645
- $user = null;
13646
-
13647
- if ( $fs->is_addon() && $fs->get_parent_instance()->is_registered() ) {
13648
- /**
13649
- * When activating an add-on's license and the parent is opted-in, activate the license with the parent's opted-in user context.
13650
- *
13651
- * @author Vova Feldman (@svovaf)
13652
- */
13653
- $user = $fs->get_parent_instance()->get_current_or_network_user();
13654
- } else if ( $fs->is_registered() ) {
13655
- $user = $fs->get_current_or_network_user();
13656
- }
13657
-
13658
- if ( $has_valid_blog_id ) {
13659
- /**
13660
- * If a specific blog ID was provided, activate the license only on the specific blog that is associated with the given blog ID.
13661
- *
13662
- * @author Leo Fajardo (@leorw)
13663
- */
13664
- $fs->switch_to_blog( $blog_id );
13665
- }
13666
-
13667
- if ( is_object( $user ) ) {
13668
- if ( $is_network_activation_or_migration && ! $has_valid_blog_id ) {
13669
- // If no specific blog ID was provided, activate the license for all sites in the network.
13670
- $blog_2_install_map = array();
13671
- $site_ids = array();
13672
-
13673
- foreach ( $sites as $site ) {
13674
- if ( ! isset( $site['blog_id'] ) || ! is_numeric( $site['blog_id'] ) ) {
13675
- continue;
13676
- }
13677
-
13678
- $install = $fs->get_install_by_blog_id( $site['blog_id'] );
13679
-
13680
- if ( is_object( $install ) ) {
13681
- $blog_2_install_map[ $site['blog_id'] ] = $install;
13682
- } else {
13683
- $site_ids[] = $site['blog_id'];
13684
- }
13685
- }
13686
-
13687
- if ( ! empty( $blog_2_install_map ) ) {
13688
- $result = $fs->activate_license_on_many_installs( $user, $license_key, $blog_2_install_map );
13689
-
13690
- if ( true !== $result ) {
13691
- $error = FS_Api::is_api_error_object( $result ) ?
13692
- $result->error->message :
13693
- var_export( $result, true );
13694
- }
13695
- }
13696
-
13697
- if ( empty( $error ) && ! empty( $site_ids ) ) {
13698
- $result = $fs->activate_license_on_many_sites( $user, $license_key, $site_ids );
13699
-
13700
- if ( true !== $result ) {
13701
- $error = FS_Api::is_api_error_object( $result ) ?
13702
- $result->error->message :
13703
- var_export( $result, true );
13704
- }
13705
- }
13706
- } else {
13707
- if ( $fs->is_registered() ) {
13708
- $params = array(
13709
- 'license_key' => $fs->apply_filters( 'license_key', $license_key )
13710
- );
13711
-
13712
- $install_ids = array();
13713
-
13714
- $change_owner = FS_User::is_valid_id( $license_owner_id );
13715
-
13716
- if ( $change_owner ) {
13717
- $params['user_id'] = $license_owner_id;
13718
-
13719
- $installs_info_by_slug_map = $fs->get_parent_and_addons_installs_info();
13720
-
13721
- foreach ( $installs_info_by_slug_map as $slug => $install_info ) {
13722
- $install_ids[ $slug ] = $install_info['install']->id;
13723
- }
13724
-
13725
- $params['install_ids'] = implode( ',', array_values( $install_ids ) );
13726
- }
13727
-
13728
- $api = $fs->get_api_site_scope();
13729
-
13730
- $install = $api->call( $fs->add_show_pending( '/' ), 'put', $params );
13731
-
13732
- if ( FS_Api::is_api_error( $install ) ) {
13733
- $error = FS_Api::is_api_error_object( $install ) ?
13734
- $install->error->message :
13735
- var_export( $install->error, true );
13736
- } else {
13737
- $fs->reconnect_locally( $has_valid_blog_id );
13738
-
13739
- if (
13740
- $change_owner &&
13741
- // If successful ownership change.
13742
- $fs->get_user()->id != $install->user_id
13743
- ) {
13744
- $fs->complete_ownership_change_by_license( $install->user_id, $install_ids );
13745
- }
13746
- }
13747
- } else /* ( $fs->is_addon() && $fs->get_parent_instance()->is_registered() ) */ {
13748
- $result = $fs->activate_license_on_site( $user, $license_key );
13749
-
13750
- if ( true !== $result ) {
13751
- $error = FS_Api::is_api_error_object( $result ) ?
13752
- $result->error->message :
13753
- var_export( $result, true );
13754
- }
13755
- }
13756
- }
13757
-
13758
- if ( empty( $error ) ) {
13759
- $fs->network_upgrade_mode_completed();
13760
-
13761
- $fs->_user = $user;
13762
-
13763
- if ( fs_is_network_admin() && ! $has_valid_blog_id ) {
13764
- $fs->_site = $fs->get_network_install();
13765
- }
13766
-
13767
- $fs->_sync_license( true, $has_valid_blog_id );
13768
-
13769
- $this->maybe_sync_install_user();
13770
-
13771
- $next_page = $fs->is_addon() ?
13772
- $fs->get_parent_instance()->get_account_url() :
13773
- $fs->get_after_activation_url( 'after_connect_url' );
13774
- }
13775
- } else {
13776
- $next_page = $fs->opt_in(
13777
- false,
13778
- false,
13779
- false,
13780
- $license_key,
13781
- false,
13782
- false,
13783
- false,
13784
- $is_marketing_allowed,
13785
- $sites
13786
- );
13787
-
13788
- if ( isset( $next_page->error ) ) {
13789
- $error = $next_page->error;
13790
- } else {
13791
- if ( $is_network_activation_or_migration ) {
13792
- /**
13793
- * Get the list of sites that were just opted-in (and license activated).
13794
- * This is an optimization for the next part below saving some DB queries.
13795
- */
13796
- $connected_sites = array();
13797
- foreach ( $sites as $site ) {
13798
- if ( isset( $site['blog_id'] ) && is_numeric( $site['blog_id'] ) ) {
13799
- $connected_sites[ $site['blog_id'] ] = true;
13800
- }
13801
- }
13802
-
13803
- $all_sites = self::get_sites();
13804
- $pending_sites = array();
13805
-
13806
- /**
13807
- * 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.
13808
- *
13809
- * @author Vova Feldman (@svovaf)
13810
- */
13811
- foreach ( $all_sites as $site ) {
13812
- $blog_id = self::get_site_blog_id( $site );
13813
-
13814
- if ( isset( $connected_sites[ $blog_id ] ) ) {
13815
- // Site was just connected.
13816
- continue;
13817
- }
13818
-
13819
- if ( $fs->is_installed_on_site( $blog_id ) ) {
13820
- // Site was already connected before.
13821
- continue;
13822
- }
13823
-
13824
- if ( $fs->is_site_delegated_connection( $blog_id ) ) {
13825
- // Site's connection was delegated.
13826
- continue;
13827
- }
13828
-
13829
- if ( $fs->is_anonymous_site( $blog_id ) ) {
13830
- // Site connection was already skipped.
13831
- continue;
13832
- }
13833
-
13834
- $pending_sites[] = self::get_site_info( $site );
13835
- }
13836
-
13837
- if ( ! empty( $pending_sites ) ) {
13838
- if ( $fs->is_freemium() && $fs->is_enable_anonymous() ) {
13839
- $fs->skip_connection( $pending_sites );
13840
- } else {
13841
- $fs->delegate_connection( $pending_sites );
13842
- }
13843
- }
13844
- }
13845
- }
13846
- }
13847
-
13848
- if ( false === $error && true === $fs->_storage->require_license_activation ) {
13849
- $fs->_storage->require_license_activation = false;
13850
- }
13851
-
13852
- $result = array(
13853
- 'success' => ( false === $error )
13854
- );
13855
-
13856
- if ( false !== $error ) {
13857
- $result['error'] = $fs->apply_filters( 'opt_in_error_message', $error );
13858
- } else {
13859
- if ( $fs->is_addon() || $fs->has_addons() ) {
13860
- /**
13861
- * Purge the valid user licenses cache so that when the "Account" or the "Add-Ons" page is loaded,
13862
- * an updated valid user licenses collection will be fetched from the server which is used to also
13863
- * update the account add-ons (add-ons the user has licenses for).
13864
- *
13865
- * @author Leo Fajardo (@leorw)
13866
- * @since 2.2.4
13867
- */
13868
- $fs->purge_valid_user_licenses_cache();
13869
- }
13870
-
13871
- $result['next_page'] = $next_page;
13872
- }
13873
-
13874
- return $result;
13875
- }
13876
-
13877
- /**
13878
- * @author Leo Fajardo (@leorw)
13879
- * @since 2.3.2
13880
- *
13881
- * @return array {
13882
- * @key string Product slug.
13883
- * @value array {
13884
- * @property FS_Site $site
13885
- * @property FS_Plugin_License $license
13886
- * }
13887
- * }
13888
- */
13889
- private function get_parent_and_addons_installs_info() {
13890
- $fs = $this->is_addon() ?
13891
- $this->get_parent_instance() :
13892
- $this;
13893
-
13894
- $installed_addons_ids = array();
13895
-
13896
- $installed_addons_instances = $fs->get_installed_addons();
13897
- foreach ( $installed_addons_instances as $instance ) {
13898
- $installed_addons_ids[] = $instance->get_id();
13899
- }
13900
-
13901
- $addons_ids = array_unique( array_merge(
13902
- $installed_addons_ids,
13903
- $fs->get_updated_account_addons()
13904
- ) );
13905
-
13906
- // Add parent product info.
13907
- $installs_info_by_slug_map = array(
13908
- $fs->get_slug() => array(
13909
- 'install' => $fs->get_site(),
13910
- 'license' => $fs->_get_license()
13911
- )
13912
- );
13913
-
13914
- foreach ( $addons_ids as $addon_id ) {
13915
- $is_installed = isset( $installed_addons_ids_map[ $addon_id ] );
13916
-
13917
- $addon_info = $fs->_get_addon_info( $addon_id, $is_installed );
13918
-
13919
- if ( ! $addon_info['is_connected'] ) {
13920
- // Add-on is not associated with an install entity.
13921
- continue;
13922
- }
13923
-
13924
- $installs_info_by_slug_map[ $addon_info['slug'] ] = array(
13925
- 'install' => $addon_info['site'],
13926
- 'license' => isset( $addon_info['license'] ) ?
13927
- $addon_info['license'] :
13928
- null
13929
- );
13930
- }
13931
-
13932
- return $installs_info_by_slug_map;
13933
- }
13934
-
13935
- /**
13936
- * @author Leo Fajardo (@leorw)
13937
- * @since 1.2.3.1
13938
- */
13939
- function _network_activate_ajax_action() {
13940
- $this->_logger->entrance();
13941
-
13942
- $this->check_ajax_referer( 'network_activate' );
13943
-
13944
- $plugin_id = fs_request_get( 'module_id', '', 'post' );
13945
- $fs = ( $plugin_id == $this->_module_id ) ?
13946
- $this :
13947
- $this->get_addon_instance( $plugin_id );
13948
-
13949
- $error = false;
13950
-
13951
- $sites = fs_request_get( 'sites', array(), 'post' );
13952
- if ( is_array( $sites ) && ! empty( $sites ) ) {
13953
- $sites_by_action = array(
13954
- 'allow' => array(),
13955
- 'delegate' => array(),
13956
- 'skip' => array()
13957
- );
13958
-
13959
- foreach ( $sites as $site ) {
13960
- $sites_by_action[ $site['action'] ][] = $site;
13961
- }
13962
-
13963
- $total_sites = count( $sites );
13964
- $total_sites_to_delegate = count( $sites_by_action['delegate'] );
13965
-
13966
- $next_page = '';
13967
-
13968
- $has_any_install = fs_request_get_bool( 'has_any_install' );
13969
-
13970
- if ( $total_sites === $total_sites_to_delegate &&
13971
- ! $this->is_network_upgrade_mode() &&
13972
- ! $has_any_install
13973
- ) {
13974
- $this->delegate_connection();
13975
- } else {
13976
- if ( ! empty( $sites_by_action['delegate'] ) ) {
13977
- $this->delegate_connection( $sites_by_action['delegate'] );
13978
- }
13979
-
13980
- if ( ! empty( $sites_by_action['skip'] ) ) {
13981
- $this->skip_connection( $sites_by_action['skip'] );
13982
- }
13983
-
13984
- if ( empty( $sites_by_action['allow'] ) ) {
13985
- if ( $has_any_install ) {
13986
- $first_install = $fs->find_first_install();
13987
-
13988
- if ( ! is_null( $first_install ) ) {
13989
- $fs->_site = $first_install['install'];
13990
- $fs->_storage->network_install_blog_id = $first_install['blog_id'];
13991
-
13992
- $fs->_user = self::_get_user_by_id( $fs->_site->user_id );
13993
- $fs->_storage->network_user_id = $fs->_user->id;
13994
- }
13995
- }
13996
- } else {
13997
- if ( ! $fs->is_registered() || ! $this->_is_network_active ) {
13998
- $next_page = $fs->opt_in(
13999
- false,
14000
- false,
14001
- false,
14002
- false,
14003
- false,
14004
- false,
14005
- false,
14006
- fs_request_get_bool( 'is_marketing_allowed', null ),
14007
- $sites_by_action['allow']
14008
- );
14009
- } else {
14010
- $next_page = $fs->install_with_user(
14011
- $this->get_network_user(),
14012
- false,
14013
- false,
14014
- false,
14015
- true,
14016
- $sites_by_action['allow']
14017
- );
14018
- }
14019
-
14020
- if ( is_object( $next_page ) && isset( $next_page->error ) ) {
14021
- $error = $next_page->error;
14022
- }
14023
- }
14024
- }
14025
-
14026
- if ( empty( $next_page ) ) {
14027
- $next_page = $this->get_after_activation_url( 'after_network_activation_url' );
14028
- }
14029
- } else {
14030
- $error = $this->get_text_inline( 'Invalid site details collection.', 'invalid_site_details_collection' );
14031
- }
14032
-
14033
- $result = array(
14034
- 'success' => ( false === $error )
14035
- );
14036
-
14037
- if ( false !== $error ) {
14038
- $result['error'] = $error;
14039
- } else {
14040
- $result['next_page'] = $next_page;
14041
- }
14042
-
14043
- echo json_encode( $result );
14044
-
14045
- exit;
14046
- }
14047
-
14048
- /**
14049
- * Billing update AJAX callback.
14050
- *
14051
- * @author Vova Feldman (@svovaf)
14052
- * @since 1.2.1.5
14053
- */
14054
- function _update_billing_ajax_action() {
14055
- $this->_logger->entrance();
14056
-
14057
- $this->check_ajax_referer( 'update_billing' );
14058
-
14059
- if ( ! $this->is_user_admin() ) {
14060
- // Only for admins.
14061
- self::shoot_ajax_failure();
14062
- }
14063
-
14064
- $billing = fs_request_get( 'billing' );
14065
-
14066
- $api = $this->get_api_user_scope();
14067
- $result = $api->call( '/billing.json', 'put', array_merge( $billing, array(
14068
- 'plugin_id' => $this->get_parent_id(),
14069
- ) ) );
14070
-
14071
- if ( ! $this->is_api_result_entity( $result ) ) {
14072
- self::shoot_ajax_failure();
14073
- }
14074
-
14075
- // Purge cached billing.
14076
- $this->get_api_user_scope()->purge_cache( 'billing.json' );
14077
-
14078
- self::shoot_ajax_success();
14079
- }
14080
-
14081
- /**
14082
- * Trial start for anonymous users (AJAX callback).
14083
- *
14084
- * @author Vova Feldman (@svovaf)
14085
- * @since 1.2.1.5
14086
- */
14087
- function _start_trial_ajax_action() {
14088
- $this->_logger->entrance();
14089
-
14090
- $this->check_ajax_referer( 'start_trial' );
14091
-
14092
- if ( ! $this->is_user_admin() ) {
14093
- // Only for admins.
14094
- self::shoot_ajax_failure();
14095
- }
14096
-
14097
- $trial_data = fs_request_get( 'trial' );
14098
-
14099
- $next_page = $this->opt_in(
14100
- false,
14101
- false,
14102
- false,
14103
- false,
14104
- false,
14105
- $trial_data['plan_id']
14106
- );
14107
-
14108
- if ( is_object( $next_page ) && $this->is_api_error( $next_page ) ) {
14109
- self::shoot_ajax_failure(
14110
- isset( $next_page->error ) ?
14111
- $next_page->error->message :
14112
- var_export( $next_page, true )
14113
- );
14114
- }
14115
-
14116
- $this->shoot_ajax_success( array(
14117
- 'next_page' => $next_page,
14118
- ) );
14119
- }
14120
-
14121
- /**
14122
- * @author Leo Fajardo (@leorw)
14123
- * @since 1.2.0
14124
- */
14125
- function _resend_license_key_ajax_action() {
14126
- $this->_logger->entrance();
14127
-
14128
- $this->check_ajax_referer( 'resend_license_key' );
14129
-
14130
- $email_address = sanitize_email( trim( fs_request_get( 'email', '', 'post' ) ) );
14131
-
14132
- if ( empty( $email_address ) ) {
14133
- exit;
14134
- }
14135
-
14136
- $error = false;
14137
-
14138
- $api = $this->get_api_plugin_scope();
14139
- $result = $api->call( '/licenses/resend.json', 'post',
14140
- array(
14141
- 'email' => $email_address,
14142
- 'url' => home_url(),
14143
- )
14144
- );
14145
-
14146
- if ( is_object( $result ) && isset( $result->error ) ) {
14147
- $error = $result->error;
14148
-
14149
- if ( in_array( $error->code, array( 'invalid_email', 'no_user' ) ) ) {
14150
- $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' );
14151
- } else if ( 'no_license' === $error->code ) {
14152
- $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' );
14153
- } else {
14154
- $error = $error->message;
14155
- }
14156
- }
14157
-
14158
- $licenses = array(
14159
- 'success' => ( false === $error )
14160
- );
14161
-
14162
- if ( false !== $error ) {
14163
- $licenses['error'] = sprintf( '%s... %s', $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ), strtolower( $error ) );
14164
- }
14165
-
14166
- echo json_encode( $licenses );
14167
-
14168
- exit;
14169
- }
14170
-
14171
- /**
14172
- * @author Vova Feldman (@svovaf)
14173
- * @since 1.2.1.8
14174
- *
14175
- * @var string
14176
- */
14177
- private static $_pagenow;
14178
-
14179
- /**
14180
- * Get current page or the referer if executing a WP AJAX request.
14181
- *
14182
- * @author Vova Feldman (@svovaf)
14183
- * @since 1.2.1.8
14184
- *
14185
- * @return string
14186
- */
14187
- static function get_current_page() {
14188
- if ( ! isset( self::$_pagenow ) ) {
14189
- global $pagenow;
14190
- if ( empty( $pagenow ) && is_admin() && is_multisite() ) {
14191
- /**
14192
- * It appears that `$pagenow` is not yet initialized in some network admin pages when this method
14193
- * is called, so initialize it here using some pieces of code from `wp-includes/vars.php`.
14194
- *
14195
- * @author Leo Fajardo (@leorw)
14196
- * @since 2.2.3
14197
- */
14198
- if ( is_network_admin() ) {
14199
- preg_match( '#/wp-admin/network/?(.*?)$#i', $_SERVER['PHP_SELF'], $self_matches );
14200
- } else if ( is_user_admin() ) {
14201
- preg_match( '#/wp-admin/user/?(.*?)$#i', $_SERVER['PHP_SELF'], $self_matches );
14202
- } else {
14203
- preg_match( '#/wp-admin/?(.*?)$#i', $_SERVER['PHP_SELF'], $self_matches );
14204
- }
14205
-
14206
- $pagenow = $self_matches[1];
14207
- $pagenow = trim( $pagenow, '/' );
14208
- $pagenow = preg_replace( '#\?.*?$#', '', $pagenow );
14209
- if ( '' === $pagenow || 'index' === $pagenow || 'index.php' === $pagenow ) {
14210
- $pagenow = 'index.php';
14211
- } else {
14212
- preg_match( '#(.*?)(/|$)#', $pagenow, $self_matches );
14213
- $pagenow = strtolower( $self_matches[1] );
14214
- if ( '.php' !== substr($pagenow, -4, 4) )
14215
- $pagenow .= '.php'; // for Options +Multiviews: /wp-admin/themes/index.php (themes.php is queried)
14216
- }
14217
- }
14218
-
14219
- self::$_pagenow = $pagenow;
14220
-
14221
- if ( self::is_ajax() &&
14222
- 'admin-ajax.php' === $pagenow
14223
- ) {
14224
- $referer = fs_get_raw_referer();
14225
-
14226
- if ( is_string( $referer ) ) {
14227
- $parts = explode( '?', $referer );
14228
-
14229
- self::$_pagenow = basename( $parts[0] );
14230
- }
14231
- }
14232
- }
14233
-
14234
- return self::$_pagenow;
14235
- }
14236
-
14237
- /**
14238
- * Helper method to check if user in the plugins page.
14239
- *
14240
- * @author Vova Feldman (@svovaf)
14241
- * @since 1.2.1.5
14242
- *
14243
- * @return bool
14244
- */
14245
- static function is_plugins_page() {
14246
- return ( 'plugins.php' === self::get_current_page() );
14247
- }
14248
-
14249
- /**
14250
- * @author Leo Fajardo (@leorw)
14251
- * @since 2.2.3
14252
- *
14253
- * @return bool
14254
- */
14255
- static function is_plugin_install_page() {
14256
- return ( 'plugin-install.php' === self::get_current_page() );
14257
- }
14258
-
14259
- /**
14260
- * @author Leo Fajardo (@leorw)
14261
- * @since 2.0.2
14262
- *
14263
- * @return bool
14264
- */
14265
- static function is_updates_page() {
14266
- return ( 'update-core.php' === self::get_current_page() );
14267
- }
14268
-
14269
- /**
14270
- * Helper method to check if user in the themes page.
14271
- *
14272
- * @author Vova Feldman (@svovaf)
14273
- * @since 1.2.2.6
14274
- *
14275
- * @return bool
14276
- */
14277
- static function is_themes_page() {
14278
- return ( 'themes.php' === self::get_current_page() );
14279
- }
14280
-
14281
- #----------------------------------------------------------------------------------
14282
- #region Affiliation
14283
- #----------------------------------------------------------------------------------
14284
-
14285
- /**
14286
- * @author Leo Fajardo (@leorw)
14287
- * @since 1.2.3
14288
- *
14289
- * @return bool
14290
- */
14291
- function has_affiliate_program() {
14292
- if ( ! is_object( $this->_plugin ) ) {
14293
- return false;
14294
- }
14295
-
14296
- return $this->_plugin->has_affiliate_program();
14297
- }
14298
-
14299
- /**
14300
- * @author Leo Fajardo (@leorw)
14301
- * @since 1.2.4
14302
- */
14303
- private function fetch_affiliate_terms() {
14304
- if ( ! is_object( $this->plugin_affiliate_terms ) ) {
14305
- $plugins_api = $this->get_api_plugin_scope();
14306
- $affiliate_terms = $plugins_api->get( '/aff.json?type=affiliation', false );
14307
-
14308
- if ( ! $this->is_api_result_entity( $affiliate_terms ) ) {
14309
- return;
14310
- }
14311
-
14312
- $this->plugin_affiliate_terms = new FS_AffiliateTerms( $affiliate_terms );
14313
- }
14314
- }
14315
-
14316
- /**
14317
- * @author Leo Fajardo (@leorw)
14318
- * @since 1.2.4
14319
- */
14320
- private function fetch_affiliate_and_custom_terms() {
14321
- if ( ! empty( $this->_storage->affiliate_application_data ) ) {
14322
- $application_data = $this->_storage->affiliate_application_data;
14323
- $flush = ( ! isset( $application_data['status'] ) || 'pending' === $application_data['status'] );
14324
-
14325
- $users_api = $this->get_api_user_scope();
14326
- $result = $users_api->get( "/plugins/{$this->_plugin->id}/aff/{$this->plugin_affiliate_terms->id}/affiliates.json", $flush );
14327
- if ( $this->is_api_result_object( $result, 'affiliates' ) ) {
14328
- if ( ! empty( $result->affiliates ) ) {
14329
- $affiliate = new FS_Affiliate( $result->affiliates[0] );
14330
-
14331
- if ( ! isset( $application_data['status'] ) || $application_data['status'] !== $affiliate->status ) {
14332
- $application_data['status'] = $affiliate->status;
14333
- $this->_storage->affiliate_application_data = $application_data;
14334
- }
14335
-
14336
- if ( $affiliate->is_using_custom_terms ) {
14337
- $affiliate_terms = $users_api->get( "/plugins/{$this->_plugin->id}/affiliates/{$affiliate->id}/aff/{$affiliate->custom_affiliate_terms_id}.json", $flush );
14338
- if ( $this->is_api_result_entity( $affiliate_terms ) ) {
14339
- $this->custom_affiliate_terms = new FS_AffiliateTerms( $affiliate_terms );
14340
- }
14341
- }
14342
-
14343
- $this->affiliate = $affiliate;
14344
- }
14345
- }
14346
- }
14347
- }
14348
-
14349
- /**
14350
- * @author Leo Fajardo (@leorw)
14351
- * @since 1.2.3
14352
- */
14353
- private function fetch_affiliate_and_terms() {
14354
- $this->_logger->entrance();
14355
-
14356
- $this->fetch_affiliate_terms();
14357
- $this->fetch_affiliate_and_custom_terms();
14358
- }
14359
-
14360
- /**
14361
- * @author Leo Fajardo (@leorw)
14362
- * @since 1.2.3
14363
- *
14364
- * @return FS_Affiliate
14365
- */
14366
- function get_affiliate() {
14367
- return $this->affiliate;
14368
- }
14369
-
14370
-
14371
- /**
14372
- * @author Leo Fajardo (@leorw)
14373
- * @since 1.2.3
14374
- *
14375
- * @return FS_AffiliateTerms
14376
- */
14377
- function get_affiliate_terms() {
14378
- return is_object( $this->custom_affiliate_terms ) ?
14379
- $this->custom_affiliate_terms :
14380
- $this->plugin_affiliate_terms;
14381
- }
14382
-
14383
- /**
14384
- * @author Leo Fajardo (@leorw)
14385
- * @since 1.2.3
14386
- */
14387
- function _submit_affiliate_application() {
14388
- $this->_logger->entrance();
14389
-
14390
- $this->check_ajax_referer( 'submit_affiliate_application' );
14391
-
14392
- if ( ! $this->is_user_admin() ) {
14393
- // Only for admins.
14394
- self::shoot_ajax_failure();
14395
- }
14396
-
14397
- $affiliate = fs_request_get( 'affiliate' );
14398
-
14399
- if ( empty( $affiliate['promotion_methods'] ) ) {
14400
- unset( $affiliate['promotion_methods'] );
14401
- }
14402
-
14403
- if ( ! empty( $affiliate['additional_domains'] ) ) {
14404
- $affiliate['additional_domains'] = array_unique( $affiliate['additional_domains'] );
14405
- }
14406
-
14407
- if ( ! $this->is_registered() ) {
14408
- // Opt in but don't track usage.
14409
- $next_page = $this->opt_in(
14410
- false,
14411
- false,
14412
- false,
14413
- false,
14414
- false,
14415
- false,
14416
- true
14417
- );
14418
-
14419
- if ( is_object( $next_page ) && $this->is_api_error( $next_page ) ) {
14420
- self::shoot_ajax_failure(
14421
- isset( $next_page->error ) ?
14422
- $next_page->error->message :
14423
- var_export( $next_page, true )
14424
- );
14425
- } else if ( $this->is_pending_activation() ) {
14426
- self::shoot_ajax_failure( $this->get_text_inline( 'Account is pending activation.', 'account-is-pending-activation' ) );
14427
- }
14428
- }
14429
-
14430
- $this->fetch_affiliate_terms();
14431
-
14432
- $api = $this->get_api_user_scope();
14433
- $result = $api->call(
14434
- ( "/plugins/{$this->_plugin->id}/aff/{$this->plugin_affiliate_terms->id}/affiliates.json" ),
14435
- 'post',
14436
- $affiliate
14437
- );
14438
-
14439
- if ( $this->is_api_error( $result ) ) {
14440
- self::shoot_ajax_failure(
14441
- isset( $result->error ) ?
14442
- $result->error->message :
14443
- var_export( $result, true )
14444
- );
14445
- } else {
14446
- if ( $this->_admin_notices->has_sticky( 'affiliate_program' ) ) {
14447
- $this->_admin_notices->remove_sticky( 'affiliate_program' );
14448
- }
14449
-
14450
- $affiliate_application_data = array(
14451
- 'status' => 'pending',
14452
- 'stats_description' => $affiliate['stats_description'],
14453
- 'promotion_method_description' => $affiliate['promotion_method_description'],
14454
- );
14455
-
14456
- if ( ! empty( $affiliate['promotion_methods'] ) ) {
14457
- $affiliate_application_data['promotion_methods'] = $affiliate['promotion_methods'];
14458
- }
14459
-
14460
- if ( ! empty( $affiliate['domain'] ) ) {
14461
- $affiliate_application_data['domain'] = $affiliate['domain'];
14462
- }
14463
-
14464
- if ( ! empty( $affiliate['additional_domains'] ) ) {
14465
- $affiliate_application_data['additional_domains'] = $affiliate['additional_domains'];
14466
- }
14467
-
14468
- $this->_storage->affiliate_application_data = $affiliate_application_data;
14469
- }
14470
-
14471
- // Purge cached affiliate.
14472
- $api->purge_cache( 'affiliate.json' );
14473
-
14474
- self::shoot_ajax_success( $result );
14475
- }
14476
-
14477
- /**
14478
- * @author Leo Fajardo (@leorw)
14479
- * @since 1.2.3
14480
- *
14481
- * @return array|null
14482
- */
14483
- function get_affiliate_application_data() {
14484
- if ( empty( $this->_storage->affiliate_application_data ) ) {
14485
- return null;
14486
- }
14487
-
14488
- return $this->_storage->affiliate_application_data;
14489
- }
14490
-
14491
- #endregion Affiliation ------------------------------------------------------------
14492
-
14493
- #----------------------------------------------------------------------------------
14494
- #region URL Generators
14495
- #----------------------------------------------------------------------------------
14496
-
14497
- /**
14498
- * Alias to pricing_url().
14499
- *
14500
- * @author Vova Feldman (@svovaf)
14501
- * @since 1.0.2
14502
- *
14503
- * @uses pricing_url()
14504
- *
14505
- * @param string $period Billing cycle
14506
- * @param bool $is_trial
14507
- *
14508
- * @return string
14509
- */
14510
- function get_upgrade_url( $period = WP_FS__PERIOD_ANNUALLY, $is_trial = false ) {
14511
- return $this->pricing_url( $period, $is_trial );
14512
- }
14513
-
14514
- /**
14515
- * @author Vova Feldman (@svovaf)
14516
- * @since 1.0.9
14517
- *
14518
- * @uses get_upgrade_url()
14519
- *
14520
- * @return string
14521
- */
14522
- function get_trial_url() {
14523
- return $this->get_upgrade_url( WP_FS__PERIOD_ANNUALLY, true );
14524
- }
14525
-
14526
- /**
14527
- * @author Leo Fajardo (@leorw)
14528
- * @since 2.1.4
14529
- *
14530
- * @param string $new_version
14531
- *
14532
- * @return string
14533
- */
14534
- function version_upgrade_checkout_link( $new_version ) {
14535
- if ( ! is_object( $this->_license ) ) {
14536
- $url = $this->pricing_url();
14537
-
14538
- $purchase_license_text = $this->get_text_inline( 'Buy a license now', 'buy-license-now' );
14539
- } else {
14540
- $subscription = $this->_get_subscription( $this->_license->id );
14541
-
14542
- $url = $this->checkout_url(
14543
- is_object( $subscription ) ?
14544
- ( 1 == $subscription->billing_cycle ? WP_FS__PERIOD_MONTHLY : WP_FS__PERIOD_ANNUALLY ) :
14545
- WP_FS__PERIOD_LIFETIME,
14546
- false,
14547
- array( 'licenses' => $this->_license->quota )
14548
- );
14549
-
14550
- $purchase_license_text = $this->get_text_inline( 'Renew your license now', 'renew-license-now' );
14551
- }
14552
-
14553
- return sprintf(
14554
- $this->get_text_inline( '%s to access version %s security & feature updates, and support.', 'x-for-updates-and-support' ),
14555
- sprintf( '<a href="%s">%s</a>', $url, $purchase_license_text ),
14556
- $new_version
14557
- );
14558
- }
14559
-
14560
- /**
14561
- * Plugin's pricing URL.
14562
- *
14563
- * @author Vova Feldman (@svovaf)
14564
- * @since 1.0.4
14565
- *
14566
- * @param string $billing_cycle Billing cycle
14567
- *
14568
- * @param bool $is_trial
14569
- *
14570
- * @return string
14571
- */
14572
- function pricing_url( $billing_cycle = WP_FS__PERIOD_ANNUALLY, $is_trial = false ) {
14573
- $this->_logger->entrance();
14574
-
14575
- $params = array(
14576
- 'billing_cycle' => $billing_cycle
14577
- );
14578
-
14579
- if ( $is_trial ) {
14580
- $params['trial'] = 'true';
14581
- }
14582
-
14583
- $url = $this->is_addon() ?
14584
- $this->_parent->addon_url( $this->_slug ) :
14585
- $this->_get_admin_page_url( 'pricing', $params );
14586
-
14587
- return $this->apply_filters( 'pricing_url', $url );
14588
- }
14589
-
14590
- /**
14591
- * Checkout page URL.
14592
- *
14593
- * @author Vova Feldman (@svovaf)
14594
- * @since 1.0.6
14595
- *
14596
- * @param string $billing_cycle Billing cycle
14597
- * @param bool $is_trial
14598
- * @param array $extra (optional) Extra parameters, override other query params.
14599
- * @param bool|null $network
14600
- *
14601
- * @return string
14602
- */
14603
- function checkout_url(
14604
- $billing_cycle = WP_FS__PERIOD_ANNUALLY,
14605
- $is_trial = false,
14606
- $extra = array(),
14607
- $network = null
14608
- ) {
14609
- $this->_logger->entrance();
14610
-
14611
- $params = array(
14612
- 'checkout' => 'true',
14613
- 'billing_cycle' => $billing_cycle,
14614
- );
14615
-
14616
- if ( $is_trial ) {
14617
- $params['trial'] = 'true';
14618
- }
14619
-
14620
- /**
14621
- * Params in extra override other params.
14622
- */
14623
- $params = array_merge( $params, $extra );
14624
-
14625
- return $this->_get_admin_page_url( 'pricing', $params, $network );
14626
- }
14627
-
14628
- /**
14629
- * Add-on checkout URL.
14630
- *
14631
- * @author Vova Feldman (@svovaf)
14632
- * @since 1.1.7
14633
- *
14634
- * @param number $addon_id
14635
- * @param number $pricing_id
14636
- * @param string $billing_cycle
14637
- * @param bool $is_trial
14638
- * @param bool|null $network
14639
- *
14640
- * @return string
14641
- */
14642
- function addon_checkout_url(
14643
- $addon_id,
14644
- $pricing_id,
14645
- $billing_cycle = WP_FS__PERIOD_ANNUALLY,
14646
- $is_trial = false,
14647
- $network = null
14648
- ) {
14649
- return $this->checkout_url( $billing_cycle, $is_trial, array(
14650
- 'plugin_id' => $addon_id,
14651
- 'pricing_id' => $pricing_id,
14652
- ), $network );
14653
- }
14654
-
14655
- #endregion
14656
-
14657
- #endregion ------------------------------------------------------------------
14658
-
14659
- /**
14660
- * Check if plugin has any add-ons.
14661
- *
14662
- * @author Vova Feldman (@svovaf)
14663
- * @since 1.0.5
14664
- *
14665
- * @since 1.1.7.3 Base logic only on the parameter provided by the developer in the init function.
14666
- *
14667
- * @return bool
14668
- */
14669
- function has_addons() {
14670
- $this->_logger->entrance();
14671
-
14672
- return $this->_has_addons;
14673
- }
14674
-
14675
- /**
14676
- * Check if plugin can work in anonymous mode.
14677
- *
14678
- * @author Vova Feldman (@svovaf)
14679
- * @since 1.0.9
14680
- *
14681
- * @return bool
14682
- *
14683
- * @deprecated Please use is_enable_anonymous() instead.
14684
- */
14685
- function enable_anonymous() {
14686
- return $this->_enable_anonymous;
14687
- }
14688
-
14689
- /**
14690
- * Check if plugin can work in anonymous mode.
14691
- *
14692
- * @author Vova Feldman (@svovaf)
14693
- * @since 1.1.9
14694
- *
14695
- * @return bool
14696
- */
14697
- function is_enable_anonymous() {
14698
- return $this->_enable_anonymous;
14699
- }
14700
-
14701
- /**
14702
- * Check if plugin is premium only (no free plans).
14703
- *
14704
- * @author Vova Feldman (@svovaf)
14705
- * @since 1.1.9
14706
- *
14707
- * @return bool
14708
- */
14709
- function is_only_premium() {
14710
- return $this->_is_premium_only;
14711
- }
14712
-
14713
- /**
14714
- * Checks if the plugin's type is "plugin". The other type is "theme".
14715
- *
14716
- * @author Leo Fajardo (@leorw)
14717
- * @since 1.2.2
14718
- *
14719
- * @return bool
14720
- */
14721
- function is_plugin() {
14722
- return ( WP_FS__MODULE_TYPE_PLUGIN === $this->_module_type );
14723
- }
14724
-
14725
- /**
14726
- * @author Leo Fajardo (@leorw)
14727
- * @since 1.2.2
14728
- *
14729
- * @return string
14730
- */
14731
- function get_module_type() {
14732
- if ( ! isset( $this->_module_type ) ) {
14733
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
14734
- $this->_module_type = $id_slug_type_path_map[ $this->_module_id ]['type'];
14735
- }
14736
-
14737
- return $this->_module_type;
14738
- }
14739
-
14740
- /**
14741
- * @author Leo Fajardo (@leorw)
14742
- * @since 1.2.2
14743
- *
14744
- * @return string
14745
- */
14746
- function get_plugin_main_file_path() {
14747
- return $this->_plugin_main_file_path;
14748
- }
14749
-
14750
- /**
14751
- * Check if module has a premium code version.
14752
- *
14753
- * Serviceware module might be freemium without any
14754
- * premium code version, where the paid features
14755
- * are all part of the service.
14756
- *
14757
- * @author Vova Feldman (@svovaf)
14758
- * @since 1.2.1.6
14759
- *
14760
- * @return bool
14761
- */
14762
- function has_premium_version() {
14763
- return $this->_has_premium_version;
14764
- }
14765
-
14766
- /**
14767
- * Check if feature supported with current site's plan.
14768
- *
14769
- * @author Vova Feldman (@svovaf)
14770
- * @since 1.0.1
14771
- *
14772
- * @todo IMPLEMENT
14773
- *
14774
- * @param number $feature_id
14775
- *
14776
- * @throws Exception
14777
- */
14778
- function is_feature_supported( $feature_id ) {
14779
- throw new Exception( 'not implemented' );
14780
- }
14781
-
14782
- /**
14783
- * @author Vova Feldman (@svovaf)
14784
- * @since 1.0.1
14785
- *
14786
- * @return bool Is running in SSL/HTTPS
14787
- */
14788
- function is_ssl() {
14789
- return WP_FS__IS_HTTPS;
14790
- }
14791
-
14792
- /**
14793
- * @author Vova Feldman (@svovaf)
14794
- * @since 1.0.9
14795
- *
14796
- * @return bool Is running in AJAX call.
14797
- *
14798
- * @link http://wordpress.stackexchange.com/questions/70676/how-to-check-if-i-am-in-admin-ajax
14799
- */
14800
- static function is_ajax() {
14801
- return ( defined( 'DOING_AJAX' ) && DOING_AJAX );
14802
- }
14803
-
14804
- /**
14805
- * Check if it's an AJAX call targeted for the current module.
14806
- *
14807
- * @author Vova Feldman (@svovaf)
14808
- * @since 1.2.0
14809
- *
14810
- * @param array|string $actions Collection of AJAX actions.
14811
- *
14812
- * @return bool
14813
- */
14814
- function is_ajax_action( $actions ) {
14815
- // Verify it's an ajax call.
14816
- if ( ! self::is_ajax() ) {
14817
- return false;
14818
- }
14819
-
14820
- // Verify the call is relevant for the plugin.
14821
- if ( $this->_module_id != fs_request_get( 'module_id' ) ) {
14822
- return false;
14823
- }
14824
-
14825
- // Verify it's one of the specified actions.
14826
- if ( is_string( $actions ) ) {
14827
- $actions = explode( ',', $actions );
14828
- }
14829
-
14830
- if ( is_array( $actions ) && 0 < count( $actions ) ) {
14831
- $ajax_action = fs_request_get( 'action' );
14832
-
14833
- foreach ( $actions as $action ) {
14834
- if ( $ajax_action === $this->get_action_tag( $action ) ) {
14835
- return true;
14836
- }
14837
- }
14838
- }
14839
-
14840
- return false;
14841
- }
14842
-
14843
- /**
14844
- * Check if it's an AJAX call targeted for current request.
14845
- *
14846
- * @author Vova Feldman (@svovaf)
14847
- * @since 1.2.0
14848
- *
14849
- * @param array|string $actions Collection of AJAX actions.
14850
- * @param number|null $module_id
14851
- *
14852
- * @return bool
14853
- */
14854
- static function is_ajax_action_static( $actions, $module_id = null ) {
14855
- // Verify it's an ajax call.
14856
- if ( ! self::is_ajax() ) {
14857
- return false;
14858
- }
14859
-
14860
-
14861
- if ( ! empty( $module_id ) ) {
14862
- // Verify the call is relevant for the plugin.
14863
- if ( $module_id != fs_request_get( 'module_id' ) ) {
14864
- return false;
14865
- }
14866
- }
14867
-
14868
- // Verify it's one of the specified actions.
14869
- if ( is_string( $actions ) ) {
14870
- $actions = explode( ',', $actions );
14871
- }
14872
-
14873
- if ( is_array( $actions ) && 0 < count( $actions ) ) {
14874
- $ajax_action = fs_request_get( 'action' );
14875
-
14876
- foreach ( $actions as $action ) {
14877
- if ( $ajax_action === self::get_ajax_action_static( $action, $module_id ) ) {
14878
- return true;
14879
- }
14880
- }
14881
- }
14882
-
14883
- return false;
14884
- }
14885
-
14886
- /**
14887
- * @author Vova Feldman (@svovaf)
14888
- * @since 1.1.7
14889
- *
14890
- * @return bool
14891
- */
14892
- static function is_cron() {
14893
- return ( defined( 'DOING_CRON' ) && DOING_CRON );
14894
- }
14895
-
14896
- /**
14897
- * Check if a real user is visiting the admin dashboard.
14898
- *
14899
- * @author Vova Feldman (@svovaf)
14900
- * @since 1.1.7
14901
- *
14902
- * @return bool
14903
- */
14904
- function is_user_in_admin() {
14905
- return (
14906
- is_admin() &&
14907
- ! self::is_ajax() &&
14908
- ! self::is_cron() &&
14909
- ( 'admin-post.php' !== self::get_current_page() )
14910
- );
14911
- }
14912
-
14913
- /**
14914
- * Check if a real user is in the customizer view.
14915
- *
14916
- * @author Vova Feldman (@svovaf)
14917
- * @since 1.2.2.7
14918
- *
14919
- * @return bool
14920
- */
14921
- static function is_customizer() {
14922
- return is_customize_preview();
14923
- }
14924
-
14925
- /**
14926
- * Check if running in HTTPS and if site's plan matching the specified plan.
14927
- *
14928
- * @param string $plan
14929
- * @param bool $exact
14930
- *
14931
- * @return bool
14932
- */
14933
- function is_ssl_and_plan( $plan, $exact = false ) {
14934
- return ( $this->is_ssl() && $this->is_plan( $plan, $exact ) );
14935
- }
14936
-
14937
- /**
14938
- * Construct plugin's settings page URL.
14939
- *
14940
- * @author Vova Feldman (@svovaf)
14941
- * @since 1.0.4
14942
- *
14943
- * @param string $page
14944
- * @param array $params
14945
- * @param bool|null $network
14946
- *
14947
- * @return string
14948
- */
14949
- function _get_admin_page_url( $page = '', $params = array(), $network = null ) {
14950
- if ( is_null( $network ) ) {
14951
- $network = (
14952
- $this->_is_network_active &&
14953
- ( fs_is_network_admin() || ! $this->is_delegated_connection() )
14954
- );
14955
- }
14956
-
14957
- if ( 0 < count( $params ) ) {
14958
- foreach ( $params as $k => $v ) {
14959
- $params[ $k ] = urlencode( $v );
14960
- }
14961
- }
14962
-
14963
- $page_param = $this->_menu->get_slug( $page );
14964
-
14965
- if ( empty( $page ) &&
14966
- // Show the opt-in as an overlay for free wp.org themes or themes without any settings page.
14967
- $this->show_opt_in_on_themes_page()
14968
- ) {
14969
- $params[ $this->get_unique_affix() . '_show_optin' ] = 'true';
14970
-
14971
- return add_query_arg(
14972
- $params,
14973
- $this->admin_url( 'themes.php', 'admin', $network )
14974
- );
14975
- }
14976
-
14977
- if ( ! $this->has_settings_menu() ) {
14978
- if ( ! empty( $page ) ) {
14979
- // Module doesn't have a setting page, but since the request is for
14980
- // a specific Freemius page, use the admin.php path.
14981
- return add_query_arg( array_merge( $params, array(
14982
- 'page' => $page_param,
14983
- ) ), $this->admin_url( 'admin.php', 'admin', $network ) );
14984
- } else {
14985
- if ( $this->is_activation_mode() ) {
14986
- /**
14987
- * @author Vova Feldman
14988
- * @since 1.2.1.6
14989
- *
14990
- * If plugin doesn't have a settings page, create one for the opt-in screen.
14991
- */
14992
- return add_query_arg( array_merge( $params, array(
14993
- 'page' => $this->_slug,
14994
- ) ), $this->admin_url( 'admin.php', 'admin', $network ) );
14995
- } else {
14996
- // Plugin without a settings page.
14997
- return add_query_arg(
14998
- $params,
14999
- $this->admin_url( 'plugins.php', 'admin', $network )
15000
- );
15001
- }
15002
- }
15003
- }
15004
-
15005
- // Module has a submenu settings page.
15006
- if ( ! $this->_menu->is_top_level() ) {
15007
- $parent_slug = $this->_menu->get_parent_slug();
15008
- $menu_file = ( false !== strpos( $parent_slug, '.php' ) ) ?
15009
- $parent_slug :
15010
- 'admin.php';
15011
-
15012
- return add_query_arg( array_merge( $params, array(
15013
- 'page' => $page_param,
15014
- ) ), $this->admin_url( $menu_file, 'admin', $network ) );
15015
- }
15016
-
15017
- // Module has a top level CPT settings page.
15018
- if ( $this->_menu->is_cpt() ) {
15019
- if ( empty( $page ) && $this->is_activation_mode() ) {
15020
- return add_query_arg( array_merge( $params, array(
15021
- 'page' => $page_param
15022
- ) ), $this->admin_url( 'admin.php', 'admin', $network ) );
15023
- } else {
15024
- if ( ! empty( $page ) ) {
15025
- $params['page'] = $page_param;
15026
- }
15027
-
15028
- return add_query_arg(
15029
- $params,
15030
- $this->admin_url( $this->_menu->get_raw_slug(), 'admin', $network )
15031
- );
15032
- }
15033
- }
15034
-
15035
- // Module has a custom top level settings page.
15036
- return add_query_arg( array_merge( $params, array(
15037
- 'page' => $page_param,
15038
- ) ), $this->admin_url( 'admin.php', 'admin', $network ) );
15039
- }
15040
-
15041
- #--------------------------------------------------------------------------------
15042
- #region Multisite
15043
- #--------------------------------------------------------------------------------
15044
-
15045
- /**
15046
- * @author Leo Fajardo (@leorw)
15047
- * @since 2.0.0
15048
- *
15049
- * @return bool
15050
- */
15051
- function is_network_active() {
15052
- return $this->_is_network_active;
15053
- }
15054
-
15055
- /**
15056
- * Delegate activation for the given sites in the network (or all sites if `null`) to site admins.
15057
- *
15058
- * @author Leo Fajardo (@leorw)
15059
- * @since 2.0.0
15060
- *
15061
- * @param array|null $sites
15062
- */
15063
- private function delegate_connection( $sites = null ) {
15064
- $this->_logger->entrance();
15065
-
15066
- $this->_admin_notices->remove_sticky( 'connect_account' );
15067
-
15068
- if ( is_null( $sites ) ) {
15069
- // All sites delegation.
15070
- $this->_storage->store( 'is_delegated_connection', true, true, true );
15071
- } else {
15072
- // Specified sites delegation.
15073
- foreach ( $sites as $site ) {
15074
- $this->delegate_site_connection( $site['blog_id'] );
15075
- }
15076
- }
15077
-
15078
- $this->network_upgrade_mode_completed();
15079
- }
15080
-
15081
- /**
15082
- * Delegate specific network site conncetion to the site admin.
15083
- *
15084
- * @author Vova Feldman (@svovaf)
15085
- * @since 2.0.0
15086
- *
15087
- * @param int $blog_id
15088
- */
15089
- private function delegate_site_connection( $blog_id ) {
15090
- $this->_storage->store( 'is_delegated_connection', true, $blog_id, true );
15091
- }
15092
-
15093
- /**
15094
- * Check if super-admin delegated the connection of ALL sites to the site admins.
15095
- *
15096
- * @author Vova Feldman (@svovaf)
15097
- * @since 2.0.0
15098
- *
15099
- * @return bool
15100
- */
15101
- function is_network_delegated_connection() {
15102
- if ( ! $this->_is_network_active ) {
15103
- return false;
15104
- }
15105
-
15106
- return $this->_storage->get( 'is_delegated_connection', false, true );
15107
- }
15108
-
15109
- /**
15110
- * @author Leo Fajardo (@leorw)
15111
- * @since 2.0.0
15112
- *
15113
- * @param int $blog_id
15114
- *
15115
- * @return bool
15116
- */
15117
- function is_site_delegated_connection( $blog_id = 0 ) {
15118
- if ( ! $this->_is_network_active ) {
15119
- return false;
15120
- }
15121
-
15122
- if ( 0 == $blog_id ) {
15123
- $blog_id = get_current_blog_id();
15124
- }
15125
-
15126
- return $this->_storage->get( 'is_delegated_connection', false, $blog_id );
15127
- }
15128
-
15129
- /**
15130
- * Check if delegated the connection. When running within the the network admin,
15131
- * and haven't specified the blog ID, checks if network level delegated. If running
15132
- * within a site admin or specified a blog ID, check if delegated the connection for
15133
- * the current context site.
15134
- *
15135
- * If executed outside the the admin, check if delegated the connection
15136
- * for the current context site OR the whole network.
15137
- *
15138
- * @author Vova Feldman (@svovaf)
15139
- * @since 2.0.0
15140
- *
15141
- * @param int $blog_id If set, checks if network delegated or blog specific delegated.
15142
- *
15143
- * @return bool
15144
- */
15145
- function is_delegated_connection( $blog_id = 0 ) {
15146
- if ( ! $this->_is_network_active ) {
15147
- return false;
15148
- }
15149
-
15150
- if ( fs_is_network_admin() && 0 == $blog_id ) {
15151
- return $this->is_network_delegated_connection();
15152
- }
15153
-
15154
- return (
15155
- $this->is_network_delegated_connection() ||
15156
- $this->is_site_delegated_connection( $blog_id )
15157
- );
15158
- }
15159
-
15160
- /**
15161
- * Check if the current module is active for the site.
15162
- *
15163
- * @author Vova Feldman (@svovaf)
15164
- * @since 2.0.0
15165
- *
15166
- * @param int $blog_id
15167
- *
15168
- * @return bool
15169
- */
15170
- function is_active_for_site( $blog_id ) {
15171
- if ( ! is_multisite() ) {
15172
- // Not a multisite and this code is executed, means that the plugin is active.
15173
- return true;
15174
- }
15175
-
15176
- if ( $this->is_theme() ) {
15177
- // All themes are site level activated.
15178
- return true;
15179
- }
15180
-
15181
- if ( $this->_is_network_active ) {
15182
- // Plugin was network activated so it's active.
15183
- return true;
15184
- }
15185
-
15186
- return in_array( $this->_plugin_basename, (array) get_blog_option( $blog_id, 'active_plugins', array() ) );
15187
- }
15188
-
15189
- /**
15190
- * @author Leo Fajardo (@leorw)
15191
- * @since 2.0.0
15192
- *
15193
- * @return array Active & public sites collection.
15194
- */
15195
- static function get_sites() {
15196
- if ( ! is_multisite() ) {
15197
- return array();
15198
- }
15199
-
15200
- /**
15201
- * For consistency with get_blog_list() which only return active public sites.
15202
- *
15203
- * @author Vova Feldman (@svovaf)
15204
- */
15205
- $args = array(
15206
- /**
15207
- * Commented out in order to handle the migration of site options whether the site is public or not.
15208
- *
15209
- * @author Leo Fajardo (@leorw)
15210
- * @since 2.2.1
15211
- */
15212
- // 'public' => 1,
15213
- 'archived' => 0,
15214
- 'mature' => 0,
15215
- 'spam' => 0,
15216
- 'deleted' => 0,
15217
- );
15218
-
15219
- if ( function_exists( 'get_sites' ) ) {
15220
- // For WP 4.6 and above.
15221
- return get_sites( $args );
15222
- } else if ( function_exists( 'wp_' . 'get_sites' ) ) {
15223
- // For WP 3.7 to WP 4.5.
15224
- /**
15225
- * This is a hack suggested previously proposed by the TRT. Our SDK is compliant with older WP versions and we'd like to keep it that way.
15226
- *
15227
- * @todo Remove this hack once this false-positive error is removed from the Theme Sniffer.
15228
- *
15229
- * @since 2.3.3
15230
- * @author Vova Feldman (@svovaf)
15231
- */
15232
- $fn = 'wp_' . 'get_sites';
15233
- return $fn( $args );
15234
- } else {
15235
- // For WP 3.6 and below.
15236
- return get_blog_list( 0, 'all' );
15237
- }
15238
- }
15239
-
15240
- /**
15241
- * Checks if a given blog is active.
15242
- *
15243
- * @author Vova Feldman (@svovaf)
15244
- * @since 2.0.0
15245
- *
15246
- * @param $blog_id
15247
- *
15248
- * @return bool
15249
- */
15250
- private static function is_site_active( $blog_id ) {
15251
- global $wpdb;
15252
-
15253
- $blog_info = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d", $blog_id ) );
15254
-
15255
- if ( ! is_object( $blog_info ) ) {
15256
- return false;
15257
- }
15258
-
15259
- return (
15260
- true == $blog_info->public &&
15261
- false == $blog_info->archived &&
15262
- false == $blog_info->mature &&
15263
- false == $blog_info->spam &&
15264
- false == $blog_info->deleted
15265
- );
15266
- }
15267
-
15268
- /**
15269
- * Get a mapping between the site addresses to their blog IDs.
15270
- *
15271
- * @author Vova Feldman (@svovaf)
15272
- * @since 2.0.0
15273
- *
15274
- * @return array {
15275
- * @key string Site address without protocol with a trailing slash.
15276
- * @value int Site's blog ID.
15277
- * }
15278
- */
15279
- private function get_address_to_blog_map() {
15280
- $sites = self::get_sites();
15281
-
15282
- // Map site addresses to their blog IDs.
15283
- $address_to_blog_map = array();
15284
- foreach ( $sites as $site ) {
15285
- $blog_id = self::get_site_blog_id( $site );
15286
- $address = trailingslashit( fs_strip_url_protocol( get_site_url( $blog_id ) ) );
15287
- $address_to_blog_map[ $address ] = $blog_id;
15288
- }
15289
-
15290
- return $address_to_blog_map;
15291
- }
15292
-
15293
- /**
15294
- * Get a mapping between the site addresses to their blog IDs.
15295
- *
15296
- * @author Vova Feldman (@svovaf)
15297
- * @since 2.0.0
15298
- *
15299
- * @return array {
15300
- * @key int Site's blog ID.
15301
- * @value FS_Site Associated install.
15302
- * }
15303
- */
15304
- function get_blog_install_map() {
15305
- $sites = self::get_sites();
15306
-
15307
- // Map site blog ID to its install.
15308
- $install_map = array();
15309
-
15310
- foreach ( $sites as $site ) {
15311
- $blog_id = self::get_site_blog_id( $site );
15312
- $install = $this->get_install_by_blog_id( $blog_id );
15313
-
15314
- if ( is_object( $install ) ) {
15315
- $install_map[ $blog_id ] = $install;
15316
- }
15317
- }
15318
-
15319
- return $install_map;
15320
- }
15321
-
15322
- /**
15323
- * Gets a map of module IDs that the given user has opted-in to.
15324
- *
15325
- * @author Leo Fajardo (@leorw)
15326
- * @since 2.1.0
15327
- *
15328
- * @param number $fs_user_id
15329
- *
15330
- * @return array {
15331
- * @key number $plugin_id
15332
- * @value bool Always true.
15333
- * }
15334
- */
15335
- private static function get_user_opted_in_module_ids_map( $fs_user_id ) {
15336
- self::$_static_logger->entrance();
15337
-
15338
- if ( ! is_multisite() ) {
15339
- $installs = array_merge(
15340
- self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN ),
15341
- self::get_all_sites( WP_FS__MODULE_TYPE_THEME )
15342
- );
15343
- } else {
15344
- $sites = self::get_sites();
15345
-
15346
- $installs = array();
15347
- foreach ( $sites as $site ) {
15348
- $blog_id = self::get_site_blog_id( $site );
15349
-
15350
- $installs = array_merge(
15351
- $installs,
15352
- self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN, $blog_id ),
15353
- self::get_all_sites( WP_FS__MODULE_TYPE_THEME, $blog_id )
15354
- );
15355
- }
15356
- }
15357
-
15358
- $module_ids_map = array();
15359
- foreach ( $installs as $install ) {
15360
- if ( is_object( $install ) &&
15361
- FS_Site::is_valid_id( $install->id ) &&
15362
- FS_User::is_valid_id( $install->user_id ) &&
15363
- ( $install->user_id == $fs_user_id )
15364
- ) {
15365
- $module_ids_map[ $install->plugin_id ] = true;
15366
- }
15367
- }
15368
-
15369
- return $module_ids_map;
15370
- }
15371
-
15372
- /**
15373
- * @author Leo Fajardo (@leorw)
15374
- *
15375
- * @return null|array {
15376
- * 'install' => FS_Site Module's install,
15377
- * 'blog_id' => string The associated blog ID.
15378
- * }
15379
- */
15380
- function find_first_install() {
15381
- $sites = self::get_sites();
15382
-
15383
- foreach ( $sites as $site ) {
15384
- $blog_id = self::get_site_blog_id( $site );
15385
- $install = $this->get_install_by_blog_id( $blog_id );
15386
-
15387
- if ( is_object( $install ) ) {
15388
- return array(
15389
- 'install' => $install,
15390
- 'blog_id' => $blog_id
15391
- );
15392
- }
15393
- }
15394
-
15395
- return null;
15396
- }
15397
-
15398
- /**
15399
- * Switches the Freemius site level context to a specified blog.
15400
- *
15401
- * @author Vova Feldman (@svovaf)
15402
- * @since 2.0.0
15403
- *
15404
- * @param int $blog_id
15405
- * @param FS_Site $install
15406
- *
15407
- * @return bool Since 2.3.1 returns if a switch was made.
15408
- */
15409
- function switch_to_blog( $blog_id, FS_Site $install = null ) {
15410
- if ( ! is_numeric( $blog_id ) || $blog_id == $this->_context_is_network_or_blog_id ) {
15411
- return false;
15412
- }
15413
-
15414
- switch_to_blog( $blog_id );
15415
- $this->_context_is_network_or_blog_id = $blog_id;
15416
-
15417
- self::$_accounts->set_site_blog_context( $blog_id );
15418
- $this->_storage->set_site_blog_context( $blog_id );
15419
- $this->_storage->set_network_active( $this->_is_network_active, $this->is_delegated_connection( $blog_id ) );
15420
-
15421
- $this->_site = is_object( $install ) ?
15422
- $install :
15423
- $this->get_install_by_blog_id( $blog_id );
15424
-
15425
- $this->_user = false;
15426
- $this->_licenses = false;
15427
- $this->_license = null;
15428
- $this->is_whitelabeled = null;
15429
-
15430
- if ( is_object( $this->_site ) ) {
15431
- // Try to fetch user from install.
15432
- $this->_user = self::_get_user_by_id( $this->_site->user_id );
15433
-
15434
- if ( ! is_object( $this->_user ) &&
15435
- FS_User::is_valid_id( $this->_storage->prev_user_id )
15436
- ) {
15437
- // Try to fetch previously saved user.
15438
- $this->_user = self::_get_user_by_id( $this->_storage->prev_user_id );
15439
-
15440
- if ( ! is_object( $this->_user ) ) {
15441
- // Fallback to network's user.
15442
- $this->_user = $this->get_network_user();
15443
- }
15444
- }
15445
-
15446
- $all_plugin_licenses = self::get_all_licenses( $this->_module_id );
15447
-
15448
- if ( ! empty( $all_plugin_licenses ) ) {
15449
- if ( ! FS_Plugin_License::is_valid_id( $this->_site->license_id ) ) {
15450
- $this->_license = null;
15451
- } else {
15452
- $license_found = false;
15453
- foreach ( $all_plugin_licenses as $license ) {
15454
- if ( $license->id == $this->_site->license_id ) {
15455
- // License found.
15456
- $this->_license = $license;
15457
- $license_found = true;
15458
- break;
15459
- }
15460
- }
15461
-
15462
- if ( $license_found ) {
15463
- $this->link_license_2_user( $this->_license->id, $this->_user->id );
15464
- }
15465
- }
15466
-
15467
- $this->_licenses = $this->get_user_licenses( $this->_user->id );
15468
- }
15469
- }
15470
-
15471
- unset( $this->_site_api );
15472
- unset( $this->_user_api );
15473
-
15474
- return false;
15475
- }
15476
-
15477
- /**
15478
- * Restore the blog context to the blog that originally loaded the module.
15479
- *
15480
- * @author Vova Feldman (@svovaf)
15481
- * @since 2.0.0
15482
- */
15483
- function restore_current_blog() {
15484
- $this->switch_to_blog( $this->_blog_id );
15485
- }
15486
-
15487
- /**
15488
- * @author Vova Feldman (@svovaf)
15489
- * @since 2.0.0
15490
- *
15491
- * @param array|WP_Site $site
15492
- *
15493
- * @return int
15494
- */
15495
- static function get_site_blog_id( &$site ) {
15496
- return ( $site instanceof WP_Site ) ?
15497
- $site->blog_id :
15498
- ( is_object( $site ) && isset( $site->userblog_id ) ?
15499
- $site->userblog_id :
15500
- $site['blog_id'] );
15501
- }
15502
-
15503
- /**
15504
- * @author Leo Fajardo (@leorw)
15505
- * @since 2.0.0
15506
- *
15507
- * @param array|WP_Site|null $site
15508
- *
15509
- * @return array
15510
- */
15511
- function get_site_info( $site = null ) {
15512
- $this->_logger->entrance();
15513
-
15514
- $switched = false;
15515
-
15516
- if ( is_null( $site ) ) {
15517
- $url = get_site_url();
15518
- $name = get_bloginfo( 'name' );
15519
- $blog_id = null;
15520
- } else {
15521
- $blog_id = self::get_site_blog_id( $site );
15522
-
15523
- if ( get_current_blog_id() != $blog_id ) {
15524
- switch_to_blog( $blog_id );
15525
- $switched = true;
15526
- }
15527
-
15528
- if ( $site instanceof WP_Site ) {
15529
- $url = $site->siteurl;
15530
- $name = $site->blogname;
15531
- } else {
15532
- $url = get_site_url( $blog_id );
15533
- $name = get_bloginfo( 'name' );
15534
- }
15535
- }
15536
-
15537
- $info = array(
15538
- 'uid' => $this->get_anonymous_id( $blog_id ),
15539
- 'url' => $url,
15540
- 'title' => $name,
15541
- 'language' => get_bloginfo( 'language' ),
15542
- 'charset' => get_bloginfo( 'charset' ),
15543
- );
15544
-
15545
- if ( is_numeric( $blog_id ) ) {
15546
- $info['blog_id'] = $blog_id;
15547
- }
15548
-
15549
- if ( $switched ) {
15550
- restore_current_blog();
15551
- }
15552
-
15553
- return $info;
15554
- }
15555
-
15556
- /**
15557
- * Load the module's install based on the blog ID.
15558
- *
15559
- * @author Vova Feldman (@svovaf)
15560
- * @since 2.0.0
15561
- *
15562
- * @param int|null $blog_id
15563
- *
15564
- * @return FS_Site
15565
- */
15566
- function get_install_by_blog_id( $blog_id = null ) {
15567
- $installs = self::get_all_sites( $this->_module_type, $blog_id );
15568
- $install = isset( $installs[ $this->_slug ] ) ? $installs[ $this->_slug ] : null;
15569
-
15570
- if ( is_object( $install ) &&
15571
- is_numeric( $install->id ) &&
15572
- is_numeric( $install->user_id ) &&
15573
- FS_Plugin_Plan::is_valid_id( $install->plan_id )
15574
- ) {
15575
- // Load site.
15576
- $install = clone $install;
15577
- }
15578
-
15579
- return $install;
15580
- }
15581
-
15582
- /**
15583
- * Check if module is installed on a specified site.
15584
- *
15585
- * @author Vova Feldman (@svovaf)
15586
- * @since 2.0.0
15587
- *
15588
- * @param int|null $blog_id
15589
- *
15590
- * @return bool
15591
- */
15592
- function is_installed_on_site( $blog_id = null ) {
15593
- $installs = self::get_all_sites( $this->_module_type, $blog_id );
15594
- $install = isset( $installs[ $this->_slug ] ) ? $installs[ $this->_slug ] : null;
15595
-
15596
- return (
15597
- is_object( $install ) &&
15598
- is_numeric( $install->id ) &&
15599
- is_numeric( $install->user_id ) &&
15600
- FS_Plugin_Plan::is_valid_id( $install->plan_id )
15601
- );
15602
- }
15603
-
15604
- /**
15605
- * Check if super-admin connected at least one site via the network opt-in.
15606
- *
15607
- * @author Vova Feldman (@svovaf)
15608
- * @since 2.0.0
15609
- *
15610
- * @return bool
15611
- */
15612
- function is_network_registered() {
15613
- if ( ! $this->_is_network_active ) {
15614
- return false;
15615
- }
15616
-
15617
- return FS_User::is_valid_id( $this->_storage->network_user_id );
15618
- }
15619
-
15620
- /**
15621
- * Returns the main user associated with the network.
15622
- *
15623
- * @author Vova Feldman (@svovaf)
15624
- * @since 2.0.0
15625
- *
15626
- * @return FS_User
15627
- */
15628
- function get_network_user() {
15629
- if ( ! $this->_is_network_active ) {
15630
- return null;
15631
- }
15632
-
15633
- return FS_User::is_valid_id( $this->_storage->network_user_id ) ?
15634
- self::_get_user_by_id( $this->_storage->network_user_id ) :
15635
- null;
15636
- }
15637
-
15638
- /**
15639
- * Returns the current context user or the network's main user.
15640
- *
15641
- * @author Vova Feldman (@svovaf)
15642
- * @since 2.0.0
15643
- *
15644
- * @return FS_User
15645
- */
15646
- function get_current_or_network_user() {
15647
- return ( $this->_user instanceof FS_User ) ?
15648
- $this->_user :
15649
- $this->get_network_user();
15650
- }
15651
-
15652
- /**
15653
- * Returns the main install associated with the network.
15654
- *
15655
- * @author Vova Feldman (@svovaf)
15656
- * @since 2.0.0
15657
- *
15658
- * @return FS_Site
15659
- */
15660
- function get_network_install() {
15661
- if ( ! $this->_is_network_active ) {
15662
- return null;
15663
- }
15664
-
15665
- return FS_Site::is_valid_id( $this->_storage->network_install_blog_id ) ?
15666
- $this->get_install_by_blog_id( $this->_storage->network_install_blog_id ) :
15667
- null;
15668
- }
15669
-
15670
- /**
15671
- * Returns the blog ID that is associated with the main install.
15672
- *
15673
- * @author Leo Fajardo (@leorw)
15674
- * @since 2.0.0
15675
- *
15676
- * @return int|null
15677
- */
15678
- function get_network_install_blog_id() {
15679
- if ( ! $this->_is_network_active ) {
15680
- return null;
15681
- }
15682
-
15683
- return FS_Site::is_valid_id( $this->_storage->network_install_blog_id ) ?
15684
- $this->_storage->network_install_blog_id :
15685
- null;
15686
- }
15687
-
15688
- /**
15689
- * Returns the current context install or the network's main install.
15690
- *
15691
- * @author Vova Feldman (@svovaf)
15692
- * @since 2.0.0
15693
- *
15694
- * @return FS_Site
15695
- */
15696
- function get_current_or_network_install() {
15697
- return ( $this->_site instanceof FS_Site ) ?
15698
- $this->_site :
15699
- $this->get_network_install();
15700
- }
15701
-
15702
- /**
15703
- * Check if executing a site level action from the network level admin.
15704
- *
15705
- * @author Vova Feldman (@svovaf)
15706
- * @since 2.0.0
15707
- *
15708
- * @return false|int If yes, return the requested blog ID.
15709
- */
15710
- private function is_network_level_site_specific_action() {
15711
- if ( ! $this->_is_network_active ) {
15712
- return false;
15713
- }
15714
-
15715
- if ( ! fs_is_network_admin() ) {
15716
- return false;
15717
- }
15718
-
15719
- $blog_id = fs_request_get( 'blog_id', '' );
15720
-
15721
- return is_numeric( $blog_id ) ? $blog_id : false;
15722
- }
15723
-
15724
- /**
15725
- * Check if executing an action from the network level admin.
15726
- *
15727
- * @author Vova Feldman (@svovaf)
15728
- * @since 2.0.0
15729
- *
15730
- * @return bool
15731
- */
15732
- private function is_network_level_action() {
15733
- return ( $this->_is_network_active && fs_is_network_admin() );
15734
- }
15735
-
15736
- /**
15737
- * Needs to be executed after site deactivation, archive, deletion, or flag as spam.
15738
- * 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.
15739
- *
15740
- * @author Vova Feldman (@svovaf)
15741
- * @since 2.0.0
15742
- *
15743
- * @param int $context_blog_id
15744
- */
15745
- private function update_multisite_data_after_site_deactivation( $context_blog_id = 0 ) {
15746
- $this->_logger->entrance();
15747
-
15748
- if ( $this->_is_network_active ) {
15749
- if ( $context_blog_id == $this->_storage->network_install_blog_id ) {
15750
- $installs_map = $this->get_blog_install_map();
15751
-
15752
- foreach ( $installs_map as $blog_id => $install ) {
15753
- /**
15754
- * @var FS_Site $install
15755
- */
15756
- if ( $context_blog_id == $blog_id ) {
15757
- continue;
15758
- }
15759
-
15760
- if ( $install->user_id != $this->_storage->network_user_id ) {
15761
- continue;
15762
- }
15763
-
15764
- // Switch reference to a blog that is opted-in and belong to the same super-admin.
15765
- $this->_storage->network_install_blog_id = $blog_id;
15766
- break;
15767
- }
15768
- }
15769
- }
15770
-
15771
- if ( $this->is_sync_cron_scheduled() &&
15772
- $context_blog_id == $this->get_sync_cron_blog_id()
15773
- ) {
15774
- $this->schedule_sync_cron( WP_FS__SCRIPT_START_TIME, true, $context_blog_id );
15775
- }
15776
-
15777
- if ( $this->is_install_sync_scheduled() &&
15778
- $context_blog_id == $this->get_install_sync_cron_blog_id()
15779
- ) {
15780
- $this->schedule_install_sync( $context_blog_id );
15781
- }
15782
- }
15783
-
15784
- /**
15785
- * Executed after site deactivation, archive, or flag as spam.
15786
- *
15787
- * @author Vova Feldman (@svovaf)
15788
- * @since 2.0.0
15789
- *
15790
- * @param int $context_blog_id
15791
- */
15792
- public function _after_site_deactivated_callback( $context_blog_id = 0 ) {
15793
- $this->_logger->entrance();
15794
-
15795
- $install = $this->get_install_by_blog_id( $context_blog_id );
15796
-
15797
- if ( ! is_object( $install ) ) {
15798
- // Site not connected.
15799
- return;
15800
- }
15801
-
15802
- $this->update_multisite_data_after_site_deactivation( $context_blog_id );
15803
-
15804
- $current_blog_id = get_current_blog_id();
15805
-
15806
- $this->switch_to_blog( $context_blog_id );
15807
-
15808
- // Send deactivation event.
15809
- $this->sync_install( array(
15810
- 'is_active' => false,
15811
- ) );
15812
-
15813
- $this->switch_to_blog( $current_blog_id );
15814
- }
15815
-
15816
- /**
15817
- * Executed after site deletion.
15818
- *
15819
- * @author Vova Feldman (@svovaf)
15820
- * @since 2.0.0
15821
- *
15822
- * @param int $context_blog_id
15823
- * @param bool $drop True if site's database tables should be dropped. Default is false.
15824
- */
15825
- public function _after_site_deleted_callback( $context_blog_id = 0, $drop = false ) {
15826
- $this->_logger->entrance();
15827
-
15828
- $install = $this->get_install_by_blog_id( $context_blog_id );
15829
-
15830
- if ( ! is_object( $install ) ) {
15831
- // Site not connected.
15832
- return;
15833
- }
15834
-
15835
- $this->update_multisite_data_after_site_deactivation( $context_blog_id );
15836
-
15837
- $current_blog_id = get_current_blog_id();
15838
-
15839
- $this->switch_to_blog( $context_blog_id );
15840
-
15841
- if ( $drop ) {
15842
- // Delete install if dropping site DB.
15843
- $this->delete_account_event();
15844
- } else {
15845
- // Send deactivation event.
15846
- $this->sync_install( array(
15847
- 'is_active' => false,
15848
- ) );
15849
- }
15850
-
15851
- $this->switch_to_blog( $current_blog_id );
15852
- }
15853
-
15854
- /**
15855
- * Executed after site re-activation.
15856
- *
15857
- * @author Vova Feldman (@svovaf)
15858
- * @since 2.0.0
15859
- *
15860
- * @param int $context_blog_id
15861
- */
15862
- public function _after_site_reactivated_callback( $context_blog_id = 0 ) {
15863
- $this->_logger->entrance();
15864
-
15865
- $install = $this->get_install_by_blog_id( $context_blog_id );
15866
-
15867
- if ( ! is_object( $install ) ) {
15868
- // Site not connected.
15869
- return;
15870
- }
15871
-
15872
- if ( ! self::is_site_active( $context_blog_id ) ) {
15873
- // Site not yet active (can be in spam mode, archived, deleted...).
15874
- return;
15875
- }
15876
-
15877
- $current_blog_id = get_current_blog_id();
15878
-
15879
- $this->switch_to_blog( $context_blog_id );
15880
-
15881
- // Send re-activation event.
15882
- $this->sync_install( array(
15883
- 'is_active' => true,
15884
- ) );
15885
-
15886
- $this->switch_to_blog( $current_blog_id );
15887
- }
15888
-
15889
- #endregion Multisite
15890
-
15891
- /**
15892
- * @author Leo Fajardo (@leorw)
15893
- *
15894
- * @param string $path
15895
- * @param string $scheme
15896
- * @param bool $network
15897
- *
15898
- * @return string
15899
- */
15900
- private function admin_url( $path = '', $scheme = 'admin', $network = true ) {
15901
- return ( $this->_is_network_active && $network ) ?
15902
- network_admin_url( $path, $scheme ) :
15903
- admin_url( $path, $scheme );
15904
- }
15905
-
15906
- /**
15907
- * Check if currently in a specified admin page.
15908
- *
15909
- * @author Vova Feldman (@svovaf)
15910
- * @since 1.2.2.7
15911
- *
15912
- * @param string $page
15913
- *
15914
- * @return bool
15915
- */
15916
- function is_admin_page( $page ) {
15917
- return ( $this->_menu->get_slug( $page ) === fs_request_get( 'page', '', 'get' ) );
15918
- }
15919
-
15920
- /**
15921
- * Check if currently in the product's main admin page.
15922
- *
15923
- * @author Vova Feldman (@svovaf)
15924
- * @since 2.3.1
15925
- *
15926
- * @return bool
15927
- */
15928
- function is_main_admin_page() {
15929
- return $this->is_admin_page( '' );
15930
- }
15931
-
15932
- /**
15933
- * Get module's main admin setting page URL.
15934
- *
15935
- * @author Vova Feldman (@svovaf)
15936
- * @since 1.2.2.7
15937
- *
15938
- * @return string
15939
- */
15940
- function main_menu_url() {
15941
- return $this->_menu->main_menu_url();
15942
- }
15943
-
15944
- /**
15945
- * Check if currently on the theme's setting page or
15946
- * on any of the Freemius added pages (via tabs).
15947
- *
15948
- * @author Vova Feldman (@svovaf)
15949
- * @since 1.2.2.7
15950
- *
15951
- * @return bool
15952
- *
15953
- * @deprecated Please use is_product_settings_page() instead;
15954
- */
15955
- function is_theme_settings_page() {
15956
- return $this->is_product_settings_page();
15957
- }
15958
-
15959
- /**
15960
- * Check if currently on the product's main setting page or on any of the Freemius added pages (via tabs).
15961
- *
15962
- * @author Vova Feldman (@svovaf)
15963
- * @since 1.2.2.7
15964
- *
15965
- * @return bool
15966
- */
15967
- function is_product_settings_page() {
15968
- return fs_starts_with(
15969
- fs_request_get( 'page', '', 'get' ),
15970
- $this->_menu->get_slug()
15971
- );
15972
- }
15973
-
15974
- /**
15975
- * Plugin's account page + sync license URL.
15976
- *
15977
- * @author Vova Feldman (@svovaf)
15978
- * @since 1.1.9.1
15979
- *
15980
- * @param bool|number $plugin_id
15981
- * @param bool $add_action_nonce
15982
- * @param array $params
15983
- *
15984
- * @return string
15985
- */
15986
- function _get_sync_license_url( $plugin_id = false, $add_action_nonce = true, $params = array() ) {
15987
- if ( is_numeric( $plugin_id ) ) {
15988
- $params['plugin_id'] = $plugin_id;
15989
- }
15990
-
15991
- return $this->get_account_url(
15992
- $this->get_unique_affix() . '_sync_license',
15993
- $params,
15994
- $add_action_nonce
15995
- );
15996
- }
15997
-
15998
- /**
15999
- * Plugin's account URL.
16000
- *
16001
- * @author Vova Feldman (@svovaf)
16002
- * @since 1.0.4
16003
- *
16004
- * @param bool|string $action
16005
- * @param array $params
16006
- *
16007
- * @param bool $add_action_nonce
16008
- *
16009
- * @return string
16010
- */
16011
- function get_account_url( $action = false, $params = array(), $add_action_nonce = true ) {
16012
- if ( is_string( $action ) ) {
16013
- $params['fs_action'] = $action;
16014
- }
16015
-
16016
- self::require_pluggable_essentials();
16017
-
16018
- return ( $add_action_nonce && is_string( $action ) ) ?
16019
- fs_nonce_url( $this->_get_admin_page_url( 'account', $params ), $action ) :
16020
- $this->_get_admin_page_url( 'account', $params );
16021
- }
16022
-
16023
- /**
16024
- * @author Vova Feldman (@svovaf)
16025
- * @since 1.2.0
16026
- *
16027
- * @param string $tab
16028
- * @param bool $action
16029
- * @param array $params
16030
- * @param bool $add_action_nonce
16031
- *
16032
- * @return string
16033
- *
16034
- * @uses get_account_url()
16035
- */
16036
- function get_account_tab_url( $tab, $action = false, $params = array(), $add_action_nonce = true ) {
16037
- $params['tab'] = $tab;
16038
-
16039
- return $this->get_account_url( $action, $params, $add_action_nonce );
16040
- }
16041
-
16042
- /**
16043
- * Plugin's account URL.
16044
- *
16045
- * @author Vova Feldman (@svovaf)
16046
- * @since 1.0.4
16047
- *
16048
- * @param bool|string $topic
16049
- * @param bool|string $message
16050
- *
16051
- * @return string
16052
- */
16053
- function contact_url( $topic = false, $message = false ) {
16054
- $params = array();
16055
- if ( is_string( $topic ) ) {
16056
- $params['topic'] = $topic;
16057
- }
16058
- if ( is_string( $message ) ) {
16059
- $params['message'] = $message;
16060
- }
16061
-
16062
- if ( $this->is_addon() ) {
16063
- $params['addon_id'] = $this->get_id();
16064
-
16065
- return $this->get_parent_instance()->_get_admin_page_url( 'contact', $params );
16066
- } else {
16067
- return $this->_get_admin_page_url( 'contact', $params );
16068
- }
16069
- }
16070
-
16071
- /**
16072
- * Add-on direct info URL.
16073
- *
16074
- * @author Vova Feldman (@svovaf)
16075
- * @since 1.1.0
16076
- *
16077
- * @param string $slug
16078
- *
16079
- * @return string
16080
- */
16081
- function addon_url( $slug ) {
16082
- return $this->_get_admin_page_url( 'addons', array(
16083
- 'slug' => $slug
16084
- ) );
16085
- }
16086
-
16087
- /**
16088
- * Add-ons URL.
16089
- *
16090
- * @author Vova Feldman (@svovaf)
16091
- * @since 2.4.5
16092
- *
16093
- * @return string
16094
- */
16095
- function get_addons_url() {
16096
- return $this->_get_admin_page_url( 'addons' );
16097
- }
16098
-
16099
- /* Logger
16100
- ------------------------------------------------------------------------------------------------------------------*/
16101
- /**
16102
- * @param string $id
16103
- * @param bool $prefix_slug
16104
- *
16105
- * @return FS_Logger
16106
- */
16107
- function get_logger( $id = '', $prefix_slug = true ) {
16108
- return FS_Logger::get_logger( ( $prefix_slug ? $this->_slug : '' ) . ( ( ! $prefix_slug || empty( $id ) ) ? '' : '_' ) . $id );
16109
- }
16110
-
16111
- /**
16112
- * Note: This method is used externally so don't delete it.
16113
- *
16114
- * @param $id
16115
- * @param bool $load_options
16116
- * @param bool $prefix_slug
16117
- *
16118
- * @return FS_Option_Manager
16119
- */
16120
- function get_options_manager( $id, $load_options = false, $prefix_slug = true ) {
16121
- return FS_Option_Manager::get_manager( ( $prefix_slug ? $this->_slug : '' ) . ( ( ! $prefix_slug || empty( $id ) ) ? '' : '_' ) . $id, $load_options );
16122
- }
16123
-
16124
- /* Security
16125
- ------------------------------------------------------------------------------------------------------------------*/
16126
- private static function _encrypt( $str ) {
16127
- if ( is_null( $str ) ) {
16128
- return null;
16129
- }
16130
-
16131
- /**
16132
- * The encrypt/decrypt functions are used to protect
16133
- * the user from messing up with some of the sensitive
16134
- * data stored for the module as a JSON in the database.
16135
- *
16136
- * I used the same suggested hack by the theme review team.
16137
- * For more details, look at the function `Base64UrlDecode()`
16138
- * in `./sdk/FreemiusBase.php`.
16139
- *
16140
- * @todo Remove this hack once the base64 error is removed from the Theme Check.
16141
- *
16142
- * @author Vova Feldman (@svovaf)
16143
- * @since 1.2.2
16144
- */
16145
- $fn = 'base64' . '_encode';
16146
-
16147
- return $fn( $str );
16148
- }
16149
-
16150
- static function _decrypt( $str ) {
16151
- if ( is_null( $str ) ) {
16152
- return null;
16153
- }
16154
-
16155
- /**
16156
- * The encrypt/decrypt functions are used to protect
16157
- * the user from messing up with some of the sensitive
16158
- * data stored for the module as a JSON in the database.
16159
- *
16160
- * I used the same suggested hack by the theme review team.
16161
- * For more details, look at the function `Base64UrlDecode()`
16162
- * in `./sdk/FreemiusBase.php`.
16163
- *
16164
- * @todo Remove this hack once the base64 error is removed from the Theme Check.
16165
- *
16166
- * @author Vova Feldman (@svovaf)
16167
- * @since 1.2.2
16168
- */
16169
- $fn = 'base64' . '_decode';
16170
-
16171
- return $fn( $str );
16172
- }
16173
-
16174
- /**
16175
- * @author Vova Feldman (@svovaf)
16176
- * @since 1.0.5
16177
- *
16178
- * @param FS_Entity $entity
16179
- *
16180
- * @return FS_Entity Return an encrypted clone entity.
16181
- */
16182
- private static function _encrypt_entity( FS_Entity $entity ) {
16183
- $clone = clone $entity;
16184
- $props = get_object_vars( $entity );
16185
-
16186
- foreach ( $props as $key => $val ) {
16187
- $clone->{$key} = self::_encrypt( $val );
16188
- }
16189
-
16190
- return $clone;
16191
- }
16192
-
16193
- /**
16194
- * @author Vova Feldman (@svovaf)
16195
- * @since 1.0.5
16196
- *
16197
- * @param FS_Entity $entity
16198
- *
16199
- * @return FS_Entity Return an decrypted clone entity.
16200
- */
16201
- private static function decrypt_entity( FS_Entity $entity ) {
16202
- $clone = clone $entity;
16203
- $props = get_object_vars( $entity );
16204
-
16205
- foreach ( $props as $key => $val ) {
16206
- $clone->{$key} = self::_decrypt( $val );
16207
- }
16208
-
16209
- return $clone;
16210
- }
16211
-
16212
- /**
16213
- * Tries to activate account based on POST params.
16214
- *
16215
- * @author Vova Feldman (@svovaf)
16216
- * @since 1.0.2
16217
- *
16218
- * @deprecated Not in use, outdated.
16219
- */
16220
- function _activate_account() {
16221
- if ( $this->is_registered() ) {
16222
- // Already activated.
16223
- return;
16224
- }
16225
-
16226
- self::_clean_admin_content_section();
16227
-
16228
- if ( fs_request_is_action( 'activate' ) && fs_request_is_post() ) {
16229
- // check_admin_referer( 'activate_' . $this->_plugin->public_key );
16230
-
16231
- // Verify matching plugin details.
16232
- if ( $this->_plugin->id != fs_request_get( 'plugin_id' ) || $this->_slug != fs_request_get( 'plugin_slug' ) ) {
16233
- return;
16234
- }
16235
-
16236
- $user = new FS_User();
16237
- $user->id = fs_request_get( 'user_id' );
16238
- $user->public_key = fs_request_get( 'user_public_key' );
16239
- $user->secret_key = fs_request_get( 'user_secret_key' );
16240
- $user->email = fs_request_get( 'user_email' );
16241
- $user->first = fs_request_get( 'user_first' );
16242
- $user->last = fs_request_get( 'user_last' );
16243
- $user->is_verified = fs_request_get_bool( 'user_is_verified' );
16244
-
16245
- $site = new FS_Site();
16246
- $site->id = fs_request_get( 'install_id' );
16247
- $site->public_key = fs_request_get( 'install_public_key' );
16248
- $site->secret_key = fs_request_get( 'install_secret_key' );
16249
- $site->plan_id = fs_request_get( 'plan_id' );
16250
-
16251
- $plans = array();
16252
- $plans_data = json_decode( urldecode( fs_request_get( 'plans' ) ) );
16253
- foreach ( $plans_data as $p ) {
16254
- $plan = new FS_Plugin_Plan( $p );
16255
- if ( $site->plan_id == $plan->id ) {
16256
- $plan->title = fs_request_get( 'plan_title' );
16257
- $plan->name = fs_request_get( 'plan_name' );
16258
- }
16259
-
16260
- $plans[] = $plan;
16261
- }
16262
-
16263
- $this->_set_account( $user, $site, $plans );
16264
-
16265
- // Reload the page with the keys.
16266
- fs_redirect( $this->_get_admin_page_url() );
16267
- }
16268
- }
16269
-
16270
- /**
16271
- * @author Vova Feldman (@svovaf)
16272
- * @since 1.0.7
16273
- *
16274
- * @param string $email
16275
- *
16276
- * @return FS_User|false
16277
- */
16278
- static function _get_user_by_email( $email ) {
16279
- self::$_static_logger->entrance();
16280
-
16281
- $email = trim( strtolower( $email ) );
16282
-
16283
- $users = self::get_all_users();
16284
-
16285
- if ( is_array( $users ) ) {
16286
- foreach ( $users as $user ) {
16287
- if ( $email === trim( strtolower( $user->email ) ) ) {
16288
- return $user;
16289
- }
16290
- }
16291
- }
16292
-
16293
- return false;
16294
- }
16295
-
16296
- #----------------------------------------------------------------------------------
16297
- #region Account (Loading, Updates & Activation)
16298
- #----------------------------------------------------------------------------------
16299
-
16300
- /***
16301
- * Load account information (user + site).
16302
- *
16303
- * @author Vova Feldman (@svovaf)
16304
- * @since 1.0.1
16305
- */
16306
- private function _load_account() {
16307
- $this->_logger->entrance();
16308
-
16309
- $this->do_action( 'before_account_load' );
16310
-
16311
- $users = self::get_all_users();
16312
- $plans = self::get_all_plans( $this->_module_type );
16313
-
16314
- if ( $this->_logger->is_on() && is_admin() ) {
16315
- $this->_logger->log( 'users = ' . var_export( $users, true ) );
16316
- $this->_logger->log( 'plans = ' . var_export( $plans, true ) );
16317
- }
16318
-
16319
- $site = fs_is_network_admin() ?
16320
- $this->get_network_install() :
16321
- $this->get_install_by_blog_id();
16322
-
16323
- if ( fs_is_network_admin() &&
16324
- $this->is_network_active() &&
16325
- ! is_object( $site ) &&
16326
- FS_Site::is_valid_id( $this->_storage->network_install_blog_id )
16327
- ) {
16328
- $first_install = $this->find_first_install();
16329
-
16330
- if ( is_null( $first_install ) ) {
16331
- unset( $this->_storage->network_install_blog_id );
16332
- } else {
16333
- $site = $first_install['install'];
16334
- $this->_storage->network_install_blog_id = $first_install['blog_id'];
16335
- }
16336
- }
16337
-
16338
- if ( is_object( $site ) &&
16339
- is_numeric( $site->id ) &&
16340
- is_numeric( $site->user_id ) &&
16341
- FS_Plugin_Plan::is_valid_id( $site->plan_id )
16342
- ) {
16343
- // Load site.
16344
- $this->_site = $site;
16345
-
16346
- // Load plans.
16347
- $this->_plans = $plans[ $this->_slug ];
16348
- if ( ! is_array( $this->_plans ) || empty( $this->_plans ) ) {
16349
- $this->_sync_plans();
16350
- } else {
16351
- for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
16352
- if ( $this->_plans[ $i ] instanceof FS_Plugin_Plan ) {
16353
- $this->_plans[ $i ] = self::decrypt_entity( $this->_plans[ $i ] );
16354
- } else {
16355
- unset( $this->_plans[ $i ] );
16356
- }
16357
- }
16358
- }
16359
- }
16360
-
16361
- $user = null;
16362
- if ( fs_is_network_admin() && $this->_is_network_active ) {
16363
- $user = $this->get_network_user();
16364
- }
16365
-
16366
- if ( is_object( $user ) ) {
16367
- $this->_user = clone $user;
16368
- } else if ( $this->_site ) {
16369
- $user = self::_get_user_by_id( $this->_site->user_id );
16370
-
16371
- if ( ! is_object( $user ) && FS_User::is_valid_id( $this->_storage->prev_user_id ) ) {
16372
- /**
16373
- * Try to load the previous owner. This recovery is used for the following use-case:
16374
- * 1. Opt-in
16375
- * 2. Cloning site1 to site2
16376
- * 3. Ownership switch in site1 (same applies for site2)
16377
- * 4. Install data sync on site2
16378
- * 5. Now site2's install is associated with the new owner which does not exists locally.
16379
- */
16380
- $user = self::_get_user_by_id( $this->_storage->prev_user_id );
16381
- }
16382
-
16383
- if ( ! is_object( $user ) ) {
16384
- /**
16385
- * This is a special fault tolerance mechanism to handle a scenario that the user data is missing.
16386
- */
16387
- $user = $this->sync_user_by_current_install();
16388
- }
16389
-
16390
- $this->_user = ( $user instanceof FS_User ) ?
16391
- clone $user :
16392
- null;
16393
- }
16394
-
16395
- if ( is_object( $this->_user ) ) {
16396
- // Load licenses.
16397
- $this->_licenses = $this->get_user_licenses( $this->_user->id );
16398
- }
16399
-
16400
- if ( is_object( $this->_site ) ) {
16401
- $this->_license = $this->_get_license_by_id( $this->_site->license_id );
16402
-
16403
- if ( $this->_site->version != $this->get_plugin_version() ) {
16404
- // If stored install version is different than current installed plugin version,
16405
- // then update plugin version event.
16406
- $this->update_plugin_version_event();
16407
- }
16408
- }
16409
-
16410
- if ( true === $this->_storage->require_license_activation &&
16411
- ! fs_request_get_bool( 'require_license', true )
16412
- ) {
16413
- $this->_storage->require_license_activation = false;
16414
- }
16415
-
16416
- if ( $this->is_theme() ) {
16417
- $this->_register_account_hooks();
16418
- }
16419
- }
16420
-
16421
- /**
16422
- * Special user recovery mechanism.
16423
- *
16424
- * @author Vova Feldman (@svovaf)
16425
- * @since 2.0.0
16426
- *
16427
- * @param number|null $site_user_id
16428
- *
16429
- * @return \FS_User|mixed
16430
- */
16431
- private function sync_user_by_current_install( $site_user_id = null ) {
16432
- $site_user_id = FS_Site::is_valid_id( $site_user_id ) ?
16433
- $site_user_id :
16434
- $this->_site->user_id;
16435
-
16436
- $api = $this->get_api_site_scope();
16437
-
16438
- $uid = $this->get_anonymous_id();
16439
- $request_path = "/users/{$site_user_id}.json?uid={$uid}";
16440
-
16441
- $result = $api->get( $request_path, false, WP_FS__TIME_10_MIN_IN_SEC );
16442
-
16443
- if ( $this->is_api_result_entity( $result ) ) {
16444
- $user = new FS_User( $result );
16445
- $this->_user = $user;
16446
- $this->_store_user();
16447
-
16448
- return $user;
16449
- }
16450
-
16451
- $error_code = FS_Api::get_error_code( $result );
16452
-
16453
- if ( in_array( $error_code, array( 'invalid_unique_id', 'user_cannot_be_recovered' ) ) ) {
16454
- /**
16455
- * Those API errors will continue coming and are not recoverable with the
16456
- * current site's data. Therefore, extend the API call's cached result to 7 days.
16457
- */
16458
- $api->update_cache_expiration( $request_path, WP_FS__TIME_WEEK_IN_SEC );
16459
- }
16460
-
16461
- return $result;
16462
- }
16463
-
16464
- /**
16465
- * @author Vova Feldman (@svovaf)
16466
- * @since 1.0.1
16467
- *
16468
- * @param FS_User $user
16469
- * @param FS_Site $site
16470
- * @param bool|array $plans
16471
- */
16472
- private function _set_account( FS_User $user, FS_Site $site, $plans = false ) {
16473
- $site->user_id = $user->id;
16474
-
16475
- $this->_site = $site;
16476
- $this->_user = $user;
16477
- if ( false !== $plans ) {
16478
- $this->_plans = $plans;
16479
- }
16480
-
16481
- $this->send_install_update();
16482
-
16483
- $this->_store_account();
16484
-
16485
- }
16486
-
16487
- /**
16488
- * Get a sanitized array with the WordPress version, SDK version, and PHP version.
16489
- * Each version is trimmed after the 16th char.
16490
- *
16491
- * @author Vova Feldman (@svovaf)
16492
- * @since 2.2.1
16493
- *
16494
- * @return array
16495
- */
16496
- private function get_versions() {
16497
- $versions = array();
16498
- $versions['platform_version'] = get_bloginfo( 'version' );
16499
- $versions['sdk_version'] = $this->version;
16500
- $versions['programming_language_version'] = phpversion();
16501
-
16502
- foreach ( $versions as $k => $version ) {
16503
- if ( is_string( $versions[ $k ] ) && ! empty( $versions[ $k ] ) ) {
16504
- $versions[ $k ] = substr( $versions[ $k ], 0, 16 );
16505
- }
16506
- }
16507
-
16508
- return $versions;
16509
- }
16510
-
16511
- /**
16512
- * @author Leo Fajardo (@leorw)
16513
- * @since 2.3.0
16514
- *
16515
- * @return bool
16516
- */
16517
- function has_beta_update() {
16518
- return (
16519
- ! empty( $this->_storage->beta_data ) &&
16520
- ( true === $this->_storage->beta_data['is_beta'] ) &&
16521
- version_compare( $this->_storage->beta_data['version'], $this->get_plugin_version(), '>' )
16522
- );
16523
- }
16524
-
16525
- /**
16526
- * @author Leo Fajardo (@leorw)
16527
- * @since 2.3.0
16528
- *
16529
- * @return bool
16530
- */
16531
- function is_beta() {
16532
- return (
16533
- ! empty( $this->_storage->beta_data ) &&
16534
- ( true === $this->_storage->beta_data['is_beta'] ) &&
16535
- ( $this->get_plugin_version() === $this->_storage->beta_data['version'] )
16536
- );
16537
- }
16538
-
16539
- /**
16540
- * @author Vova Feldman (@svovaf)
16541
- * @since 1.1.7.4
16542
- *
16543
- * @param array $override_with
16544
- * @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.
16545
- *
16546
- * @return array
16547
- */
16548
- function get_opt_in_params( $override_with = array(), $network_level_or_blog_id = null ) {
16549
- $this->_logger->entrance();
16550
-
16551
- $current_user = self::_get_current_wp_user();
16552
-
16553
- $activation_action = $this->get_unique_affix() . '_activate_new';
16554
- $return_url = $this->is_anonymous() ?
16555
- // If skipped already, then return to the account page.
16556
- $this->get_account_url( $activation_action, array(), false ) :
16557
- // Return to the module's main page.
16558
- $this->get_after_activation_url( 'after_connect_url', array( 'fs_action' => $activation_action ) );
16559
-
16560
- $versions = $this->get_versions();
16561
-
16562
- $params = array_merge( $versions, array(
16563
- 'user_firstname' => $current_user->user_firstname,
16564
- 'user_lastname' => $current_user->user_lastname,
16565
- 'user_nickname' => $current_user->user_nicename,
16566
- 'user_email' => $current_user->user_email,
16567
- 'user_ip' => WP_FS__REMOTE_ADDR,
16568
- 'plugin_slug' => $this->_slug,
16569
- 'plugin_id' => $this->get_id(),
16570
- 'plugin_public_key' => $this->get_public_key(),
16571
- 'plugin_version' => $this->get_plugin_version(),
16572
- 'return_url' => fs_nonce_url( $return_url, $activation_action ),
16573
- 'account_url' => fs_nonce_url( $this->_get_admin_page_url(
16574
- 'account',
16575
- array( 'fs_action' => 'sync_user' )
16576
- ), 'sync_user' ),
16577
- 'is_premium' => $this->is_premium(),
16578
- 'is_active' => true,
16579
- 'is_uninstalled' => false,
16580
- ) );
16581
-
16582
- if ( $this->is_addon() ) {
16583
- $parent_fs = $this->get_parent_instance();
16584
-
16585
- $params['parent_plugin_slug'] = $parent_fs->_slug;
16586
- $params['parent_plugin_id'] = $parent_fs->get_id();
16587
- }
16588
-
16589
- if ( true === $network_level_or_blog_id ) {
16590
- if ( ! isset( $override_with['sites'] ) ) {
16591
- $params['sites'] = $this->get_sites_for_network_level_optin();
16592
- }
16593
- } else {
16594
- $site = is_numeric( $network_level_or_blog_id ) ?
16595
- array( 'blog_id' => $network_level_or_blog_id ) :
16596
- null;
16597
-
16598
- $site = $this->get_site_info( $site );
16599
-
16600
- $params = array_merge( $params, array(
16601
- 'site_uid' => $site['uid'],
16602
- 'site_url' => $site['url'],
16603
- 'site_name' => $site['title'],
16604
- 'language' => $site['language'],
16605
- 'charset' => $site['charset'],
16606
- ) );
16607
- }
16608
-
16609
- if ( $this->is_pending_activation() &&
16610
- ! empty( $this->_storage->pending_license_key )
16611
- ) {
16612
- $params['license_key'] = $this->_storage->pending_license_key;
16613
- }
16614
-
16615
- if ( WP_FS__SKIP_EMAIL_ACTIVATION && $this->has_secret_key() ) {
16616
- // Even though rand() is known for its security issues,
16617
- // the timestamp adds another layer of protection.
16618
- // It would be very hard for an attacker to get the secret key form here.
16619
- // Plus, this should never run in production since the secret should never
16620
- // be included in the production version.
16621
- $params['ts'] = WP_FS__SCRIPT_START_TIME;
16622
- $params['salt'] = md5( uniqid( rand() ) );
16623
- $params['secure'] = md5(
16624
- $params['ts'] .
16625
- $params['salt'] .
16626
- $this->get_secret_key()
16627
- );
16628
- }
16629
-
16630
- return array_merge( $params, $override_with );
16631
- }
16632
-
16633
- /**
16634
- * 1. If successful opt-in or pending activation returns the next page that the user should be redirected to.
16635
- * 2. If there was an API error, return the API result.
16636
- *
16637
- * @author Vova Feldman (@svovaf)
16638
- * @since 1.1.7.4
16639
- *
16640
- * @param string|bool $email
16641
- * @param string|bool $first
16642
- * @param string|bool $last
16643
- * @param string|bool $license_key
16644
- * @param bool $is_uninstall If "true", this means that the module is currently being uninstalled.
16645
- * In this case, the user and site info will be sent to the server but no
16646
- * data will be saved to the WP installation's database.
16647
- * @param number|bool $trial_plan_id
16648
- * @param bool $is_disconnected Whether or not to opt in without tracking.
16649
- * @param null|bool $is_marketing_allowed
16650
- * @param array $sites If network-level opt-in, an array of containing details of sites.
16651
- *
16652
- * @return string|object
16653
- * @use WP_Error
16654
- */
16655
- function opt_in(
16656
- $email = false,
16657
- $first = false,
16658
- $last = false,
16659
- $license_key = false,
16660
- $is_uninstall = false,
16661
- $trial_plan_id = false,
16662
- $is_disconnected = false,
16663
- $is_marketing_allowed = null,
16664
- $sites = array()
16665
- ) {
16666
- $this->_logger->entrance();
16667
-
16668
- if ( false === $email ) {
16669
- $current_user = self::_get_current_wp_user();
16670
- $email = $current_user->user_email;
16671
- }
16672
-
16673
- /**
16674
- * @since 1.2.1 If activating with license key, ignore the context-user
16675
- * since the user will be automatically loaded from the license.
16676
- */
16677
- if ( empty( $license_key ) ) {
16678
- // Clean up pending license if opt-ing in again.
16679
- $this->_storage->remove( 'pending_license_key' );
16680
-
16681
- if ( ! $is_uninstall ) {
16682
- $fs_user = Freemius::_get_user_by_email( $email );
16683
- if ( is_object( $fs_user ) && ! $this->is_pending_activation() ) {
16684
- return $this->install_with_user(
16685
- $fs_user,
16686
- false,
16687
- $trial_plan_id,
16688
- true,
16689
- true,
16690
- $sites
16691
- );
16692
- }
16693
- }
16694
- }
16695
-
16696
- $user_info = array();
16697
- if ( ! empty( $email ) ) {
16698
- $user_info['user_email'] = $email;
16699
- }
16700
- if ( ! empty( $first ) ) {
16701
- $user_info['user_firstname'] = $first;
16702
- }
16703
- if ( ! empty( $last ) ) {
16704
- $user_info['user_lastname'] = $last;
16705
- }
16706
-
16707
- if ( ! empty( $sites ) ) {
16708
- $is_network = true;
16709
-
16710
- $user_info['sites'] = $sites;
16711
- } else {
16712
- $is_network = false;
16713
- }
16714
-
16715
- $params = $this->get_opt_in_params( $user_info, $is_network );
16716
-
16717
- $filtered_license_key = false;
16718
- if ( is_string( $license_key ) ) {
16719
- $filtered_license_key = $this->apply_filters( 'license_key', $license_key );
16720
- $params['license_key'] = $filtered_license_key;
16721
- } else if ( FS_Plugin_Plan::is_valid_id( $trial_plan_id ) ) {
16722
- $params['trial_plan_id'] = $trial_plan_id;
16723
- }
16724
-
16725
- if ( $is_uninstall ) {
16726
- $params['uninstall_params'] = array(
16727
- 'reason_id' => $this->_storage->uninstall_reason->id,
16728
- 'reason_info' => $this->_storage->uninstall_reason->info
16729
- );
16730
- }
16731
-
16732
- if ( isset( $params['license_key'] ) ) {
16733
- $fs_user = Freemius::_get_user_by_email( $email );
16734
-
16735
- if ( is_object( $fs_user ) ) {
16736
- /**
16737
- * If opting in with a context license and the context WP Admin user already opted in
16738
- * before from the current site, add the user context security params to avoid the
16739
- * unnecessary email activation when the context license is owned by the same context user.
16740
- *
16741
- * @author Leo Fajardo (@leorw)
16742
- * @since 1.2.3
16743
- */
16744
- $params = array_merge( $params, FS_Security::instance()->get_context_params(
16745
- $fs_user,
16746
- false,
16747
- 'install_with_existing_user'
16748
- ) );
16749
- }
16750
- }
16751
-
16752
- if ( is_bool( $is_marketing_allowed ) ) {
16753
- $params['is_marketing_allowed'] = $is_marketing_allowed;
16754
- }
16755
-
16756
- $params['is_disconnected'] = $is_disconnected;
16757
- $params['format'] = 'json';
16758
-
16759
- $request = array(
16760
- 'method' => 'POST',
16761
- 'body' => $params,
16762
- 'timeout' => WP_FS__DEBUG_SDK ? 60 : 30,
16763
- );
16764
-
16765
- $url = $this->add_show_pending( WP_FS__ADDRESS . '/action/service/user/install/' );
16766
- $response = self::safe_remote_post( $url, $request );
16767
-
16768
- if ( is_wp_error( $response ) ) {
16769
- /**
16770
- * @var WP_Error $response
16771
- */
16772
- $result = new stdClass();
16773
-
16774
- $error_code = $response->get_error_code();
16775
- $error_type = str_replace( ' ', '', ucwords( str_replace( '_', ' ', $error_code ) ) );
16776
-
16777
- $result->error = (object) array(
16778
- 'type' => $error_type,
16779
- 'message' => $response->get_error_message(),
16780
- 'code' => $error_code,
16781
- 'http' => 402
16782
- );
16783
-
16784
- $this->maybe_modify_api_curl_error_message( $result );
16785
-
16786
- return $result;
16787
- }
16788
-
16789
- // Module is being uninstalled, don't handle the returned data.
16790
- if ( $is_uninstall ) {
16791
- return true;
16792
- }
16793
-
16794
- /**
16795
- * 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.
16796
- *
16797
- * @author Vova Feldman (@svovaf)
16798
- * @since 1.2.3
16799
- * @link https://themes.trac.wordpress.org/ticket/46134#comment:5
16800
- * @link https://themes.trac.wordpress.org/ticket/46134#comment:9
16801
- * @link https://themes.trac.wordpress.org/ticket/46134#comment:12
16802
- * @link https://themes.trac.wordpress.org/ticket/46134#comment:14
16803
- */
16804
- $decoded = is_string( $response['body'] ) ?
16805
- json_decode( $response['body'] ) :
16806
- null;
16807
-
16808
- if ( empty( $decoded ) ) {
16809
- return false;
16810
- }
16811
-
16812
- if ( ! $this->is_api_result_object( $decoded ) ) {
16813
- if ( ! empty( $params['license_key'] ) ) {
16814
- // Pass the fully entered license key to the failure handler.
16815
- $params['license_key'] = $license_key;
16816
- }
16817
-
16818
- return $is_uninstall ?
16819
- $decoded :
16820
- $this->apply_filters( 'after_install_failure', $decoded, $params );
16821
- } else if ( isset( $decoded->pending_activation ) && $decoded->pending_activation ) {
16822
- if ( $is_network ) {
16823
- $site_ids = array();
16824
- foreach ( $sites as $site ) {
16825
- $site_ids[] = $site['blog_id'];
16826
- }
16827
-
16828
- /**
16829
- * Store the sites so that they can be installed once the user has clicked on the activation link
16830
- * in the email.
16831
- *
16832
- * @author Leo Fajardo (@leorw)
16833
- */
16834
- $this->_storage->pending_sites_info = array(
16835
- 'blog_ids' => $site_ids,
16836
- 'license_key' => $license_key,
16837
- 'trial_plan_id' => $trial_plan_id
16838
- );
16839
- }
16840
-
16841
- // Pending activation, add message.
16842
- return $this->set_pending_confirmation(
16843
- ( isset( $decoded->email ) ?
16844
- $decoded->email :
16845
- true ),
16846
- false,
16847
- $filtered_license_key,
16848
- ! empty( $params['trial_plan_id'] )
16849
- );
16850
- } else if ( isset( $decoded->install_secret_key ) ) {
16851
- return $this->install_with_new_user(
16852
- $decoded->user_id,
16853
- $decoded->user_public_key,
16854
- $decoded->user_secret_key,
16855
- ( isset( $decoded->is_marketing_allowed ) && ! is_null( $decoded->is_marketing_allowed ) ?
16856
- $decoded->is_marketing_allowed :
16857
- null ),
16858
- ( isset( $decoded->is_extensions_tracking_allowed ) && ! is_null( $decoded->is_extensions_tracking_allowed ) ?
16859
- $decoded->is_extensions_tracking_allowed :
16860
- null ),
16861
- $decoded->install_id,
16862
- $decoded->install_public_key,
16863
- $decoded->install_secret_key,
16864
- false
16865
- );
16866
- } else if ( is_array( $decoded->installs ) ) {
16867
- return $this->install_many_with_new_user(
16868
- $decoded->user_id,
16869
- $decoded->user_public_key,
16870
- $decoded->user_secret_key,
16871
- ( isset( $decoded->is_marketing_allowed ) && ! is_null( $decoded->is_marketing_allowed ) ?
16872
- $decoded->is_marketing_allowed :
16873
- null ),
16874
- ( isset( $decoded->is_extensions_tracking_allowed ) && ! is_null( $decoded->is_extensions_tracking_allowed ) ?
16875
- $decoded->is_extensions_tracking_allowed :
16876
- null ),
16877
- $decoded->installs,
16878
- false
16879
- );
16880
- }
16881
-
16882
- return $decoded;
16883
- }
16884
-
16885
- /**
16886
- * Set user and site identities.
16887
- *
16888
- * @author Vova Feldman (@svovaf)
16889
- * @since 1.0.9
16890
- *
16891
- * @param FS_User $user
16892
- * @param FS_Site $site
16893
- * @param bool $redirect
16894
- * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will
16895
- * redirect (or return a URL) to the account page with a special parameter to
16896
- * trigger the auto installation processes.
16897
- *
16898
- * @return string If redirect is `false`, returns the next page the user should be redirected to.
16899
- */
16900
- function setup_account(
16901
- FS_User $user,
16902
- FS_Site $site,
16903
- $redirect = true,
16904
- $auto_install = false
16905
- ) {
16906
- return $this->setup_network_account(
16907
- $user,
16908
- array( $site ),
16909
- $redirect,
16910
- $auto_install,
16911
- false
16912
- );
16913
- }
16914
-
16915
- /**
16916
- * Set user and site identities.
16917
- *
16918
- * @author Vova Feldman (@svovaf)
16919
- * @since 2.0.0
16920
- *
16921
- * @param FS_User $user
16922
- * @param FS_Site[] $installs
16923
- * @param bool $redirect
16924
- * @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.
16925
- * @param bool $is_network_level_opt_in
16926
- *
16927
- * @return string If redirect is `false`, returns the next page the user should be redirected to.
16928
- */
16929
- function setup_network_account(
16930
- FS_User $user,
16931
- array $installs,
16932
- $redirect = true,
16933
- $auto_install = false,
16934
- $is_network_level_opt_in = true
16935
- ) {
16936
- $first_install = $installs[0];
16937
-
16938
- $this->_user = $user;
16939
- $this->_site = $first_install;
16940
-
16941
- $this->_sync_plans();
16942
-
16943
- if ( $this->_storage->handle_gdpr_admin_notice &&
16944
- $this->should_handle_gdpr_admin_notice() &&
16945
- FS_GDPR_Manager::instance()->should_show_opt_in_notice()
16946
- ) {
16947
- /**
16948
- * Clear user lock after an opt-in.
16949
- */
16950
- require_once WP_FS__DIR_INCLUDES . '/class-fs-user-lock.php';
16951
- FS_User_Lock::instance()->unlock();
16952
- }
16953
-
16954
- if ( 1 < count( $installs ) ) {
16955
- // Only network level opt-in can have more than one install.
16956
- $is_network_level_opt_in = true;
16957
- }
16958
- // $is_network_level_opt_in = self::is_ajax_action_static( 'network_activate', $this->_module_id );
16959
- // If Freemius was OFF before, turn it on.
16960
- $this->turn_on();
16961
-
16962
- $this->handle_account_connection(
16963
- $installs,
16964
- ( ! $this->_is_network_active || ! $is_network_level_opt_in )
16965
- );
16966
-
16967
- if ( is_numeric( $first_install->license_id ) ) {
16968
- $this->set_license( $this->_get_license_by_id( $first_install->license_id ) );
16969
- }
16970
-
16971
- $this->_admin_notices->remove_sticky( 'connect_account' );
16972
-
16973
- if ( $this->is_pending_activation() || ! $this->has_settings_menu() ) {
16974
- // Remove pending activation sticky notice (if still exist).
16975
- $this->_admin_notices->remove_sticky( 'activation_pending' );
16976
-
16977
- // Remove plugin from pending activation mode.
16978
- unset( $this->_storage->is_pending_activation );
16979
-
16980
- if ( ! $this->is_paying_or_trial() ) {
16981
- $this->_admin_notices->add_sticky(
16982
- sprintf( $this->get_text_inline( '%s activation was successfully completed.', 'plugin-x-activation-message' ), '<b>' . $this->get_plugin_name() . '</b>' ),
16983
- 'activation_complete'
16984
- );
16985
- }
16986
- }
16987
-
16988
- if ( $this->is_paying_or_trial() ) {
16989
- if ( ! $this->is_premium() ||
16990
- ! $this->has_premium_version() ||
16991
- ! $this->has_settings_menu()
16992
- ) {
16993
- if ( $this->is_paying() ) {
16994
- $this->_admin_notices->add_sticky(
16995
- sprintf(
16996
- $this->get_text_inline( 'Your account was successfully activated with the %s plan.', 'activation-with-plan-x-message' ),
16997
- $this->get_plan_title()
16998
- ) . $this->get_complete_upgrade_instructions(),
16999
- 'plan_upgraded',
17000
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
17001
- );
17002
- } else {
17003
- $trial_plan = $this->get_trial_plan();
17004
-
17005
- $this->_admin_notices->add_sticky(
17006
- sprintf(
17007
- $this->get_text_inline( 'Your trial has been successfully started.', 'trial-started-message' ),
17008
- '<i>' . $this->get_plugin_name() . '</i>'
17009
- ) . $this->get_complete_upgrade_instructions( $trial_plan->title ),
17010
- 'trial_started',
17011
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
17012
- );
17013
- }
17014
- }
17015
-
17016
- $this->_admin_notices->remove_sticky( array(
17017
- 'trial_promotion',
17018
- ) );
17019
- }
17020
-
17021
- $plugin_id = fs_request_get( 'plugin_id', false );
17022
-
17023
- // Store activation time ONLY for plugins & themes (not add-ons).
17024
- if ( ! is_numeric( $plugin_id ) || ( $plugin_id == $this->_plugin->id ) ) {
17025
- if ( empty( $this->_storage->activation_timestamp ) ) {
17026
- $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME;
17027
- }
17028
- }
17029
-
17030
- $next_page = '';
17031
-
17032
- $extra = array();
17033
- if ( $auto_install ) {
17034
- $extra['auto_install'] = 'true';
17035
- }
17036
-
17037
- if ( is_numeric( $plugin_id ) ) {
17038
- /**
17039
- * @author Leo Fajardo (@leorw)
17040
- * @since 1.2.1.6
17041
- *
17042
- * Also sync the license after an anonymous user subscribes.
17043
- */
17044
- if ( $this->is_anonymous() || $plugin_id != $this->_plugin->id ) {
17045
- // Add-on was installed - sync license right after install.
17046
- $next_page = $this->_get_sync_license_url( $plugin_id, true, $extra );
17047
- }
17048
- } else {
17049
- /**
17050
- * @author Vova Feldman (@svovaf)
17051
- * @since 1.1.9 If site installed with a valid license, sync license.
17052
- */
17053
- if ( $this->is_paying() ) {
17054
- $this->_sync_plugin_license(
17055
- true,
17056
- // Installs data is already synced in the beginning of this method directly or via _set_account().
17057
- false
17058
- );
17059
- }
17060
-
17061
- // Reload the page with the keys.
17062
- $next_page = $this->is_anonymous() ?
17063
- // If user previously skipped, redirect to account page.
17064
- $this->get_account_url( false, $extra ) :
17065
- $this->get_after_activation_url( 'after_connect_url', array(), $is_network_level_opt_in );
17066
- }
17067
-
17068
- if ( ! empty( $next_page ) && $redirect ) {
17069
- fs_redirect( $next_page );
17070
- }
17071
-
17072
- return $next_page;
17073
- }
17074
-
17075
- /**
17076
- * Install plugin with new user information after approval.
17077
- *
17078
- * @author Vova Feldman (@svovaf)
17079
- * @since 1.0.7
17080
- */
17081
- function _install_with_new_user() {
17082
- $this->_logger->entrance();
17083
-
17084
- if ( $this->is_registered() ) {
17085
- return;
17086
- }
17087
-
17088
- if ( ( $this->is_plugin() && fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) ||
17089
- // @todo This logic should be improved because it's executed on every load of a theme.
17090
- $this->is_theme()
17091
- ) {
17092
- // check_admin_referer( $this->_slug . '_activate_new' );
17093
-
17094
- if ( fs_request_has( 'user_secret_key' ) ) {
17095
- if ( fs_is_network_admin() && isset( $this->_storage->pending_sites_info ) ) {
17096
- $pending_sites_info = $this->_storage->pending_sites_info;
17097
-
17098
- $this->install_many_pending_with_user(
17099
- fs_request_get( 'user_id' ),
17100
- fs_request_get( 'user_public_key' ),
17101
- fs_request_get( 'user_secret_key' ),
17102
- fs_request_get_bool( 'is_marketing_allowed', null ),
17103
- fs_request_get_bool( 'is_extensions_tracking_allowed', null ),
17104
- $pending_sites_info['blog_ids'],
17105
- $pending_sites_info['license_key'],
17106
- $pending_sites_info['trial_plan_id']
17107
- );
17108
- } else {
17109
- $this->install_with_new_user(
17110
- fs_request_get( 'user_id' ),
17111
- fs_request_get( 'user_public_key' ),
17112
- fs_request_get( 'user_secret_key' ),
17113
- fs_request_get_bool( 'is_marketing_allowed', null ),
17114
- fs_request_get_bool( 'is_extensions_tracking_allowed', null ),
17115
- fs_request_get( 'install_id' ),
17116
- fs_request_get( 'install_public_key' ),
17117
- fs_request_get( 'install_secret_key' ),
17118
- true,
17119
- fs_request_get_bool( 'auto_install' )
17120
- );
17121
- }
17122
- } else if ( fs_request_has( 'pending_activation' ) ) {
17123
- $this->set_pending_confirmation( fs_request_get( 'user_email' ), true );
17124
- }
17125
- }
17126
- }
17127
-
17128
- /**
17129
- * @author Vova Feldman (@svovaf)
17130
- * @since 2.0.0
17131
- *
17132
- * @param number $id
17133
- * @param string $public_key
17134
- * @param string $secret_key
17135
- *
17136
- * @return \FS_User
17137
- */
17138
- private function setup_user( $id, $public_key, $secret_key ) {
17139
- $user = self::_get_user_by_id( $id );
17140
-
17141
- if ( is_object( $user ) ) {
17142
- $this->_user = $user;
17143
- } else {
17144
- $user = new FS_User();
17145
- $user->id = $id;
17146
- $user->public_key = $public_key;
17147
- $user->secret_key = $secret_key;
17148
-
17149
- $this->_user = $user;
17150
- $user_result = $this->get_api_user_scope()->get();
17151
- $user = new FS_User( $user_result );
17152
-
17153
- $this->_user = $user;
17154
- $this->_store_user();
17155
- }
17156
-
17157
- return $user;
17158
- }
17159
-
17160
- /**
17161
- * Install plugin with new user.
17162
- *
17163
- * @author Vova Feldman (@svovaf)
17164
- * @since 1.1.7.4
17165
- *
17166
- * @param number $user_id
17167
- * @param string $user_public_key
17168
- * @param string $user_secret_key
17169
- * @param bool|null $is_marketing_allowed
17170
- * @param bool|null $is_extensions_tracking_allowed Since 2.3.2
17171
- * @param number $install_id
17172
- * @param string $install_public_key
17173
- * @param string $install_secret_key
17174
- * @param bool $redirect
17175
- * @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.
17176
- *
17177
- * @return string If redirect is `false`, returns the next page the user should be redirected to.
17178
- */
17179
- private function install_with_new_user(
17180
- $user_id,
17181
- $user_public_key,
17182
- $user_secret_key,
17183
- $is_marketing_allowed,
17184
- $is_extensions_tracking_allowed,
17185
- $install_id,
17186
- $install_public_key,
17187
- $install_secret_key,
17188
- $redirect = true,
17189
- $auto_install = false
17190
- ) {
17191
- /**
17192
- * This method is also executed after opting in with a license key since the
17193
- * license can be potentially associated with a different owner.
17194
- *
17195
- * @since 2.0.0
17196
- */
17197
- $user = self::_get_user_by_id( $user_id );
17198
-
17199
- if ( ! is_object( $user ) ) {
17200
- $user = new FS_User();
17201
- $user->id = $user_id;
17202
- $user->public_key = $user_public_key;
17203
- $user->secret_key = $user_secret_key;
17204
-
17205
- $this->_user = $user;
17206
- $user_result = $this->get_api_user_scope()->get();
17207
- $user = new FS_User( $user_result );
17208
- }
17209
-
17210
- $this->_user = $user;
17211
-
17212
- $site = new FS_Site();
17213
- $site->id = $install_id;
17214
- $site->public_key = $install_public_key;
17215
- $site->secret_key = $install_secret_key;
17216
-
17217
- $this->_site = $site;
17218
- $site_result = $this->get_api_site_scope()->get();
17219
- $site = new FS_Site( $site_result );
17220
- $this->_site = $site;
17221
-
17222
- if ( ! is_null( $is_marketing_allowed ) ) {
17223
- $this->disable_opt_in_notice_and_lock_user();
17224
- }
17225
-
17226
- $this->update_extensions_tracking_flag( $is_extensions_tracking_allowed );
17227
-
17228
- return $this->setup_account(
17229
- $this->_user,
17230
- $this->_site,
17231
- $redirect,
17232
- $auto_install
17233
- );
17234
- }
17235
-
17236
- /**
17237
- * Install plugin with user.
17238
- *
17239
- * @author Leo Fajardo (@leorw)
17240
- * @since 2.0.0
17241
- *
17242
- * @param number $user_id
17243
- * @param string $user_public_key
17244
- * @param string $user_secret_key
17245
- * @param bool|null $is_marketing_allowed
17246
- * @param bool|null $is_extensions_tracking_allowed Since 2.3.2
17247
- * @param array $site_ids
17248
- * @param bool $license_key
17249
- * @param bool $trial_plan_id
17250
- * @param bool $redirect
17251
- *
17252
- * @return string If redirect is `false`, returns the next page the user should be redirected to.
17253
- */
17254
- private function install_many_pending_with_user(
17255
- $user_id,
17256
- $user_public_key,
17257
- $user_secret_key,
17258
- $is_marketing_allowed,
17259
- $is_extensions_tracking_allowed,
17260
- $site_ids,
17261
- $license_key = false,
17262
- $trial_plan_id = false,
17263
- $redirect = true
17264
- ) {
17265
- $user = $this->setup_user( $user_id, $user_public_key, $user_secret_key );
17266
-
17267
- if ( ! is_null( $is_marketing_allowed ) ) {
17268
- $this->disable_opt_in_notice_and_lock_user();
17269
- }
17270
-
17271
- $this->update_extensions_tracking_flag( $is_extensions_tracking_allowed );
17272
-
17273
- $sites = array();
17274
- foreach ( $site_ids as $site_id ) {
17275
- $sites[] = $this->get_site_info( array( 'blog_id' => $site_id ) );
17276
- }
17277
-
17278
- $this->install_with_user( $user, $license_key, $trial_plan_id, $redirect, true, $sites );
17279
- }
17280
-
17281
- /**
17282
- * Multi-site install with a new user.
17283
- *
17284
- * @author Vova Feldman (@svovaf)
17285
- * @since 2.0.0
17286
- *
17287
- * @param number $user_id
17288
- * @param string $user_public_key
17289
- * @param string $user_secret_key
17290
- * @param bool|null $is_marketing_allowed
17291
- * @param bool|null $is_extensions_tracking_allowed Since 2.3.2
17292
- * @param object[] $installs
17293
- * @param bool $redirect
17294
- * @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.
17295
- *
17296
- * @return string If redirect is `false`, returns the next page the user should be redirected to.
17297
- */
17298
- private function install_many_with_new_user(
17299
- $user_id,
17300
- $user_public_key,
17301
- $user_secret_key,
17302
- $is_marketing_allowed,
17303
- $is_extensions_tracking_allowed,
17304
- array $installs,
17305
- $redirect = true,
17306
- $auto_install = false
17307
- ) {
17308
- $this->setup_user( $user_id, $user_public_key, $user_secret_key );
17309
-
17310
- if ( ! is_null( $is_marketing_allowed ) ) {
17311
- $this->disable_opt_in_notice_and_lock_user();
17312
- }
17313
-
17314
- $this->update_extensions_tracking_flag( $is_extensions_tracking_allowed );
17315
-
17316
- $install_ids = array();
17317
-
17318
- foreach ( $installs as $install ) {
17319
- $install_ids[] = $install->id;
17320
- }
17321
-
17322
- $left = count( $install_ids );
17323
- $offset = 0;
17324
-
17325
- $installs = array();
17326
- while ( $left > 0 ) {
17327
- $result = $this->get_api_user_scope()->get( "/plugins/{$this->_module_id}/installs.json?ids=" . implode( ',', array_slice( $install_ids, $offset, 25 ) ) );
17328
-
17329
- if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
17330
- // @todo Handle API error.
17331
- }
17332
-
17333
- $installs = array_merge( $installs, $result->installs );
17334
-
17335
- $left -= 25;
17336
- }
17337
-
17338
- foreach ( $installs as &$install ) {
17339
- $install = new FS_Site( $install );
17340
- }
17341
-
17342
- return $this->setup_network_account(
17343
- $this->_user,
17344
- $installs,
17345
- $redirect,
17346
- $auto_install
17347
- );
17348
- }
17349
-
17350
- /**
17351
- * @author Vova Feldman (@svovaf)
17352
- * @since 1.1.7.4
17353
- *
17354
- * @param string|bool $email
17355
- * @param bool $redirect
17356
- * @param string|bool $license_key Since 1.2.1.5
17357
- * @param bool $is_pending_trial Since 1.2.1.5
17358
- *
17359
- * @return string Since 1.2.1.5 if $redirect is `false`, return the pending activation page.
17360
- */
17361
- private function set_pending_confirmation(
17362
- $email = false,
17363
- $redirect = true,
17364
- $license_key = false,
17365
- $is_pending_trial = false
17366
- ) {
17367
- if ( $this->_ignore_pending_mode ) {
17368
- /**
17369
- * If explicitly asked to ignore pending mode, set to anonymous mode
17370
- * if require confirmation before finalizing the opt-in.
17371
- *
17372
- * @author Vova Feldman
17373
- * @since 1.2.1.6
17374
- */
17375
- $this->skip_connection( null, fs_is_network_admin() );
17376
- } else {
17377
- // Install must be activated via email since
17378
- // user with the same email already exist.
17379
- $this->_storage->is_pending_activation = true;
17380
- $this->_add_pending_activation_notice( $email, $is_pending_trial );
17381
- }
17382
-
17383
- if ( ! empty( $license_key ) ) {
17384
- $this->_storage->pending_license_key = $license_key;
17385
- }
17386
-
17387
- // Remove the opt-in sticky notice.
17388
- $this->_admin_notices->remove_sticky( array(
17389
- 'connect_account',
17390
- 'trial_promotion',
17391
- ) );
17392
-
17393
- $next_page = $this->get_after_activation_url( 'after_pending_connect_url' );
17394
-
17395
- // Reload the page with with pending activation message.
17396
- if ( $redirect ) {
17397
- fs_redirect( $next_page );
17398
- }
17399
-
17400
- return $next_page;
17401
- }
17402
-
17403
- /**
17404
- * Install plugin with current logged WP user info.
17405
- *
17406
- * @author Vova Feldman (@svovaf)
17407
- * @since 1.0.7
17408
- */
17409
- function _install_with_current_user() {
17410
- $this->_logger->entrance();
17411
-
17412
- if ( $this->is_registered() ) {
17413
- return;
17414
- }
17415
-
17416
- if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) && fs_request_is_post() ) {
17417
- // check_admin_referer( 'activate_existing_' . $this->_plugin->public_key );
17418
-
17419
- /**
17420
- * @author Vova Feldman (@svovaf)
17421
- * @since 1.1.9 Add license key if given.
17422
- */
17423
- $license_key = fs_request_get( 'license_secret_key' );
17424
-
17425
- $this->update_extensions_tracking_flag( fs_request_get_bool( 'is_extensions_tracking_allowed', null ) );
17426
-
17427
- $this->install_with_current_user( $license_key );
17428
- }
17429
- }
17430
-
17431
-
17432
- /**
17433
- * @author Vova Feldman (@svovaf)
17434
- * @since 1.1.7.4
17435
- *
17436
- * @param string|bool $license_key
17437
- * @param number|bool $trial_plan_id
17438
- * @param array $sites Since 2.0.0
17439
- * @param bool $redirect
17440
- *
17441
- * @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.
17442
- */
17443
- private function install_with_current_user(
17444
- $license_key = false,
17445
- $trial_plan_id = false,
17446
- $sites = array(),
17447
- $redirect = true
17448
- ) {
17449
- // Get current logged WP user.
17450
- $current_user = self::_get_current_wp_user();
17451
-
17452
- // Find the relevant FS user by the email.
17453
- $user = self::_get_user_by_email( $current_user->user_email );
17454
-
17455
- return $this->install_with_user( $user, $license_key, $trial_plan_id, $redirect, true, $sites );
17456
- }
17457
-
17458
- /**
17459
- * @author Vova Feldman (@svovaf)
17460
- * @since 2.0.0
17461
- *
17462
- * @param \FS_User $user
17463
- * @param string|bool $license_key
17464
- * @param number|bool $trial_plan_id
17465
- * @param bool $redirect
17466
- * @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.
17467
- * @param array $sites Since 2.0.0. If not empty, should be a collection of site details for the bulk install API request.
17468
- *
17469
- * @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.
17470
- */
17471
- function install_with_user(
17472
- FS_User $user,
17473
- $license_key = false,
17474
- $trial_plan_id = false,
17475
- $redirect = true,
17476
- $setup_account = true,
17477
- $sites = array()
17478
- ) {
17479
- // We have to set the user before getting user scope API handler.
17480
- $this->_user = $user;
17481
-
17482
- // Install the plugin.
17483
- $result = $this->create_installs_with_user(
17484
- $user,
17485
- $license_key,
17486
- $trial_plan_id,
17487
- $sites,
17488
- $redirect
17489
- );
17490
-
17491
- if ( ! $this->is_api_result_entity( $result ) &&
17492
- ! $this->is_api_result_object( $result, 'installs' )
17493
- ) {
17494
- // @todo Handler potential API error of the $result
17495
- }
17496
-
17497
- if ( empty( $sites ) ) {
17498
- $site = new FS_Site( $result );
17499
- $this->_site = $site;
17500
-
17501
- if ( ! $setup_account ) {
17502
- $this->_store_site();
17503
-
17504
- $this->sync_plan_if_not_exist( $site->plan_id );
17505
-
17506
- if ( ! empty( $license_key ) && FS_Plugin_License::is_valid_id( $site->license_id ) ) {
17507
- $this->sync_license_if_not_exist( $site->license_id, $license_key );
17508
- }
17509
-
17510
- $this->_admin_notices->remove_sticky( 'connect_account', false );
17511
-
17512
- return $site;
17513
- }
17514
-
17515
- return $this->setup_account( $this->_user, $this->_site, $redirect );
17516
- } else {
17517
- $installs = array();
17518
- foreach ( $result->installs as $install ) {
17519
- $installs[] = new FS_Site( $install );
17520
- }
17521
-
17522
- return $this->setup_network_account(
17523
- $user,
17524
- $installs,
17525
- $redirect
17526
- );
17527
- }
17528
- }
17529
-
17530
- /**
17531
- * Initiate an API request to create a collection of installs.
17532
- *
17533
- * @author Vova Feldman (@svovaf)
17534
- * @since 2.0.0
17535
- *
17536
- * @param \FS_User $user
17537
- * @param bool $license_key
17538
- * @param bool $trial_plan_id
17539
- * @param array $sites
17540
- * @param bool $redirect
17541
- * @param bool $silent
17542
- *
17543
- * @return object|mixed
17544
- */
17545
- private function create_installs_with_user(
17546
- FS_User $user,
17547
- $license_key = false,
17548
- $trial_plan_id = false,
17549
- $sites = array(),
17550
- $redirect = false,
17551
- $silent = false
17552
- ) {
17553
- $extra_install_params = array(
17554
- 'uid' => $this->get_anonymous_id(),
17555
- 'is_disconnected' => false,
17556
- );
17557
-
17558
- if ( ! empty( $license_key ) ) {
17559
- $extra_install_params['license_key'] = $this->apply_filters( 'license_key', $license_key );
17560
-
17561
- if ( $silent ) {
17562
- $extra_install_params['ignore_license_owner'] = true;
17563
- }
17564
- } else if ( FS_Plugin_Plan::is_valid_id( $trial_plan_id ) ) {
17565
- $extra_install_params['trial_plan_id'] = $trial_plan_id;
17566
- }
17567
-
17568
- if ( ! empty( $sites ) ) {
17569
- $extra_install_params['sites'] = $sites;
17570
- }
17571
-
17572
- $args = $this->get_install_data_for_api( $extra_install_params, false, false );
17573
-
17574
- // Install the plugin.
17575
- $result = $this->get_api_user_scope_by_user( $user )->call(
17576
- "/plugins/{$this->get_id()}/installs.json",
17577
- 'post',
17578
- $args
17579
- );
17580
-
17581
- if ( ! $this->is_api_result_entity( $result ) &&
17582
- ! $this->is_api_result_object( $result, 'installs' )
17583
- ) {
17584
- if ( ! empty( $args['license_key'] ) ) {
17585
- // Pass the fully entered license key to the failure handler.
17586
- $args['license_key'] = $license_key;
17587
- }
17588
-
17589
- $result = $this->apply_filters( 'after_install_failure', $result, $args );
17590
-
17591
- if ( ! $silent ) {
17592
- $this->_admin_notices->add(
17593
- sprintf( $this->get_text_inline( 'Couldn\'t activate %s.', 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
17594
- $this->get_text_inline( 'Please contact us with the following message:', 'contact-us-with-error-message' ) . ' ' . '<b>' . $result->error->message . '</b>',
17595
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
17596
- 'error'
17597
- );
17598
- }
17599
-
17600
- if ( $redirect ) {
17601
- /**
17602
- * We set the user before getting the user scope API handler, so the user became temporarily
17603
- * registered (`is_registered() = true`). Since the API returned an error and we will redirect,
17604
- * we have to set the user to `null`, otherwise, the user will be redirected to the wrong
17605
- * activation page based on the return value of `is_registered()`. In addition, in case the
17606
- * context plugin doesn't have a settings menu and the default page is the `Plugins` page,
17607
- * misleading plugin activation errors will be shown on the `Plugins` page.
17608
- *
17609
- * @author Leo Fajardo (@leorw)
17610
- */
17611
- $this->_user = null;
17612
-
17613
- fs_redirect( $this->get_activation_url( array( 'error' => $result->error->message ) ) );
17614
- }
17615
- }
17616
-
17617
- return $result;
17618
- }
17619
-
17620
- /**
17621
- * Tries to activate add-on account based on parent plugin info.
17622
- *
17623
- * @author Vova Feldman (@svovaf)
17624
- * @since 1.0.6
17625
- *
17626
- * @param Freemius $parent_fs
17627
- * @param bool|int|null $network_level_or_blog_id True for network level opt-in and integer for opt-in for specified blog in the network.
17628
- * @param FS_Plugin_License $bundle_license Since 2.4.0. If provided, this license will be activated for the add-on.
17629
- */
17630
- private function _activate_addon_account(
17631
- Freemius $parent_fs,
17632
- $network_level_or_blog_id = null,
17633
- FS_Plugin_License $bundle_license = null
17634
- ) {
17635
- if ( $this->is_registered() ) {
17636
- // Already activated.
17637
- return;
17638
- }
17639
-
17640
- /**
17641
- * Do not override the `uid` if network-level opt-in since the call to `get_sites_for_network_level_optin()`
17642
- * already returns the data for the current blog.
17643
- *
17644
- * @author Leo Fajardo (@leorw)
17645
- * @since 2.3.0
17646
- */
17647
- $uid_param_to_override = ( true === $network_level_or_blog_id ) ?
17648
- array() :
17649
- array( 'uid' => $this->get_anonymous_id() );
17650
-
17651
- $params = $this->get_install_data_for_api(
17652
- $uid_param_to_override,
17653
- false,
17654
- false,
17655
- /**
17656
- * Do not include the data for the current blog if network-level opt-in since the call to `get_sites_for_network_level_optin`
17657
- * already includes the data for it.
17658
- *
17659
- * @author Leo Fajardo (@leorw)
17660
- * @since 2.3.0
17661
- */
17662
- ( true !== $network_level_or_blog_id )
17663
- );
17664
-
17665
- if ( true === $network_level_or_blog_id ) {
17666
- $params['sites'] = $this->get_sites_for_network_level_optin();
17667
-
17668
- if ( empty( $params['sites'] ) ) {
17669
- return;
17670
- }
17671
- }
17672
-
17673
- if ( is_object( $bundle_license ) ) {
17674
- $params['license_key'] = $bundle_license->secret_key;
17675
- }
17676
-
17677
- // Activate add-on with parent plugin credentials.
17678
- $result = $parent_fs->get_api_site_scope()->call(
17679
- "/addons/{$this->_plugin->id}/installs.json",
17680
- 'post',
17681
- $params
17682
- );
17683
-
17684
- if ( ! $this->is_api_result_object( $result, 'installs' ) ) {
17685
- if ( is_object( $bundle_license ) ) {
17686
- /**
17687
- * When a license object is provided, it's an attempt by the SDK to activate a bundle license and not a user-initiated action, therefore, do not show any admin notice to avoid confusion (e.g.: the notice will show up just above the opt-in link). If the license activation fails, the admin will see an opt-in link instead.
17688
- *
17689
- * @author Leo Fajardo (@leorw)
17690
- * @since 2.4.0
17691
- */
17692
- } else {
17693
- $error_message = FS_Api::is_api_error_object( $result ) ?
17694
- $result->error->message :
17695
- $this->get_text_inline( 'An unknown error has occurred.', 'unknown-error' );
17696
-
17697
- $this->_admin_notices->add(
17698
- sprintf( $this->get_text_inline( 'Couldn\'t activate %s.', 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
17699
- $this->get_text_inline( 'Please contact us with the following message:', 'contact-us-with-error-message' ) . ' ' . '<b>' . $error_message . '</b>',
17700
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
17701
- 'error'
17702
- );
17703
- }
17704
-
17705
- return;
17706
- }
17707
-
17708
- $addon_installs = $result->installs;
17709
- foreach ( $addon_installs as $key => $addon_install ) {
17710
- $addon_installs[ $key ] = new FS_Site( $addon_install );
17711
- }
17712
-
17713
- $first_install = $addon_installs[0];
17714
-
17715
- // Get user information based on parent's plugin.
17716
- $user = $parent_fs->get_user();
17717
-
17718
- // First of all, set site and user info - otherwise we won't
17719
- // be able to invoke API calls.
17720
- $this->_site = $first_install;
17721
- $this->_user = $user;
17722
-
17723
- // Sync add-on plans.
17724
- $this->_sync_plans();
17725
-
17726
- $this->handle_account_connection( $addon_installs, ! fs_is_network_admin() );
17727
-
17728
- // Get site's current plan.
17729
- //$this->_site->plan = $this->_get_plan_by_id( $this->_site->plan->id );
17730
-
17731
- // Sync licenses.
17732
- $this->_sync_licenses();
17733
-
17734
- if ( ! fs_is_network_admin() ) {
17735
- // Try to activate premium license.
17736
- $this->_activate_license( true, $bundle_license );
17737
-
17738
- if ( is_object( $bundle_license ) ) {
17739
- $this->maybe_activate_bundle_license( $bundle_license );
17740
- }
17741
- } else {
17742
- if ( is_object( $bundle_license ) ) {
17743
- $premium_license = $bundle_license;
17744
- } else {
17745
- $license_id = fs_request_get( 'license_id' );
17746
-
17747
- if ( is_object( $this->_site ) &&
17748
- FS_Plugin_License::is_valid_id( $license_id ) &&
17749
- $license_id == $this->_site->license_id
17750
- ) {
17751
- // License is already activated.
17752
- return;
17753
- }
17754
-
17755
- $premium_license = FS_Plugin_License::is_valid_id( $license_id ) ?
17756
- $this->_get_license_by_id( $license_id ) :
17757
- $this->_get_available_premium_license();
17758
- }
17759
-
17760
- if ( is_object( $premium_license ) ) {
17761
- $this->maybe_network_activate_addon_license( $premium_license );
17762
- }
17763
- }
17764
- }
17765
-
17766
- /**
17767
- * @author Leo Fajardo (@leorw)
17768
- * @since 2.3.0
17769
- *
17770
- * @param FS_Site[] $installs
17771
- * @param bool $is_site_level
17772
- */
17773
- private function handle_account_connection( $installs, $is_site_level ) {
17774
- $first_install = $installs[0];
17775
-
17776
- if ( $is_site_level ) {
17777
- $this->_set_account( $this->_user, $first_install );
17778
-
17779
- $this->do_action( 'after_account_connection', $this->_user, $first_install );
17780
- } else {
17781
- $this->_store_user();
17782
-
17783
- // Map site addresses to their blog IDs.
17784
- $address_to_blog_map = $this->get_address_to_blog_map();
17785
-
17786
- $first_blog_id = null;
17787
- $blog_2_install_map = array();
17788
- foreach ( $installs as $install ) {
17789
- $address = trailingslashit( fs_strip_url_protocol( $install->url ) );
17790
- $blog_id = $address_to_blog_map[ $address ];
17791
-
17792
- $this->_store_site( true, $blog_id, $install );
17793
-
17794
- if ( is_null( $first_blog_id ) ) {
17795
- $first_blog_id = $blog_id;
17796
- }
17797
-
17798
- $blog_2_install_map[ $blog_id ] = $install;
17799
- }
17800
-
17801
- if ( ! FS_User::is_valid_id( $this->_storage->network_user_id ) ||
17802
- ! is_object( self::_get_user_by_id( $this->_storage->network_user_id ) )
17803
- ) {
17804
- // Store network user.
17805
- $this->_storage->network_user_id = $this->_user->id;
17806
- }
17807
-
17808
- if ( ! FS_Site::is_valid_id( $this->_storage->network_install_blog_id ) ) {
17809
- $this->_storage->network_install_blog_id = $first_blog_id;
17810
- }
17811
-
17812
- if ( count( $installs ) === count( $address_to_blog_map ) ) {
17813
- // Super admin opted in for all sites in the network.
17814
- $this->_storage->is_network_connected = true;
17815
- }
17816
-
17817
- $this->_store_licenses( false );
17818
-
17819
- self::$_accounts->store();
17820
-
17821
- // Don't sync the installs data on network upgrade
17822
- if ( ! $this->network_upgrade_mode_completed() ) {
17823
- $this->send_installs_update();
17824
- }
17825
-
17826
- // Switch install context back to the first install.
17827
- $this->_site = $first_install;
17828
-
17829
- $current_blog = get_current_blog_id();
17830
-
17831
- foreach ( $blog_2_install_map as $blog_id => $install ) {
17832
- $this->switch_to_blog( $blog_id );
17833
-
17834
- $this->do_action( 'after_account_connection', $this->_user, $install );
17835
- }
17836
-
17837
- $this->switch_to_blog( $current_blog );
17838
-
17839
- $this->do_action( 'after_network_account_connection', $this->_user, $blog_2_install_map );
17840
- }
17841
- }
17842
-
17843
- /**
17844
- * Tries to activate parent account based on add-on's info.
17845
- *
17846
- * @author Vova Feldman (@svovaf)
17847
- * @since 1.2.2.7
17848
- *
17849
- * @param Freemius $parent_fs
17850
- */
17851
- private function activate_parent_account( Freemius $parent_fs ) {
17852
- if ( ! $this->is_addon() ) {
17853
- // This is not an add-on.
17854
- return;
17855
- }
17856
-
17857
- if ( $parent_fs->is_registered() ) {
17858
- // Already activated.
17859
- return;
17860
- }
17861
-
17862
- // Activate parent with add-on's user credentials.
17863
- $parent_install = $this->get_api_user_scope()->call(
17864
- "/plugins/{$parent_fs->_plugin->id}/installs.json",
17865
- 'post',
17866
- $parent_fs->get_install_data_for_api( array(
17867
- 'uid' => $parent_fs->get_anonymous_id(),
17868
- ), false, false )
17869
- );
17870
-
17871
- if ( isset( $parent_install->error ) ) {
17872
- $this->_admin_notices->add(
17873
- sprintf( $this->get_text_inline( 'Couldn\'t activate %s.', 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
17874
- $this->get_text_inline( 'Please contact us with the following message:', 'contact-us-with-error-message' ) . ' ' . '<b>' . $parent_install->error->message . '</b>',
17875
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
17876
- 'error'
17877
- );
17878
-
17879
- return;
17880
- }
17881
-
17882
- $parent_fs->_admin_notices->remove_sticky( 'connect_account' );
17883
-
17884
- if ( $parent_fs->is_pending_activation() ) {
17885
- $parent_fs->_admin_notices->remove_sticky( 'activation_pending' );
17886
-
17887
- unset( $parent_fs->_storage->is_pending_activation );
17888
- }
17889
-
17890
- // Get user information based on parent's plugin.
17891
- $user = $this->get_user();
17892
-
17893
- // First of all, set site info - otherwise we won't
17894
- // be able to invoke API calls.
17895
- $parent_fs->_site = new FS_Site( $parent_install );
17896
- $parent_fs->_user = $user;
17897
-
17898
- // Sync add-on plans.
17899
- $parent_fs->_sync_plans();
17900
-
17901
- $parent_fs->_set_account( $user, $parent_fs->_site );
17902
- }
17903
-
17904
- #endregion
17905
-
17906
- #----------------------------------------------------------------------------------
17907
- #region Admin Menu Items
17908
- #----------------------------------------------------------------------------------
17909
-
17910
- private $_menu_items = array();
17911
-
17912
- /**
17913
- * @author Vova Feldman (@svovaf)
17914
- * @since 1.2.1.8
17915
- *
17916
- * @return array
17917
- */
17918
- function get_menu_items() {
17919
- return $this->_menu_items;
17920
- }
17921
-
17922
- /**
17923
- * @author Vova Feldman (@svovaf)
17924
- * @since 1.0.7
17925
- *
17926
- * @return string
17927
- */
17928
- function get_menu_slug() {
17929
- return $this->_menu->get_slug();
17930
- }
17931
-
17932
- /**
17933
- * @author Vova Feldman (@svovaf)
17934
- * @since 1.0.9
17935
- */
17936
- function _prepare_admin_menu() {
17937
- // if ( ! $this->is_on() ) {
17938
- // return;
17939
- // }
17940
-
17941
- /**
17942
- * When running from a site admin with a network activated module and the connection
17943
- * was NOT delegated and the user still haven't skipped or opted-in, then hide the
17944
- * site level settings.
17945
- *
17946
- * @author Vova Feldman (@svovaf)
17947
- * @since 2.0.0
17948
- */
17949
- $should_hide_site_admin_settings = (
17950
- $this->_is_network_active &&
17951
- ! fs_is_network_admin() &&
17952
- ! $this->is_delegated_connection() &&
17953
- ! $this->is_anonymous() &&
17954
- ! $this->is_registered()
17955
- );
17956
-
17957
- $should_hide_site_admin_settings = $this->apply_filters( 'should_hide_site_admin_settings_on_network_activation_mode', $should_hide_site_admin_settings );
17958
-
17959
- if ( ( ! $this->has_api_connectivity() && ! $this->is_enable_anonymous() ) ||
17960
- $should_hide_site_admin_settings
17961
- ) {
17962
- $this->_menu->remove_menu_item( $should_hide_site_admin_settings );
17963
- } else {
17964
- $this->do_action( fs_is_network_admin() ?
17965
- 'before_network_admin_menu_init' :
17966
- 'before_admin_menu_init'
17967
- );
17968
-
17969
- $this->add_menu_action();
17970
-
17971
- $this->add_network_menu_when_missing();
17972
-
17973
- $this->add_submenu_items();
17974
- }
17975
- }
17976
-
17977
- /**
17978
- * Admin dashboard menu items modifications.
17979
- *
17980
- * NOTE: admin_menu action executed before admin_init.
17981
- *
17982
- * @author Vova Feldman (@svovaf)
17983
- * @since 1.0.7
17984
- *
17985
- */
17986
- private function add_menu_action() {
17987
- if ( $this->is_activation_mode() ) {
17988
- if ( $this->show_opt_in_on_setting_page() ) {
17989
- $this->override_plugin_menu_with_activation();
17990
- } else {
17991
- /**
17992
- * Handle theme opt-in when the opt-in form shows as a dialog box in the themes page.
17993
- */
17994
- if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) ) {
17995
- add_action( 'load-themes.php', array( $this, '_install_with_current_user' ) );
17996
- } else if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ||
17997
- fs_request_get_bool( 'pending_activation' )
17998
- ) {
17999
- add_action( 'load-themes.php', array( $this, '_install_with_new_user' ) );
18000
- }
18001
- }
18002
- } else {
18003
- if ( ! $this->is_registered() ) {
18004
- // If not registered try to install user.
18005
- if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) {
18006
- $this->_install_with_new_user();
18007
- }
18008
- } else if (
18009
- fs_request_is_action( 'sync_user' ) &&
18010
- ( ! $this->has_settings_menu() || $this->show_opt_in_on_themes_page() )
18011
- ) {
18012
- $this->_handle_account_user_sync();
18013
- }
18014
- }
18015
- }
18016
-
18017
- /**
18018
- * @author Vova Feldman (@svovaf)
18019
- * @since 1.0.1
18020
- */
18021
- function _redirect_on_clicked_menu_link() {
18022
- $this->_logger->entrance();
18023
-
18024
- $page = fs_request_get('page');
18025
- $page = is_string($page) ? strtolower($page) : '';
18026
-
18027
- $this->_logger->log( 'page = ' . $page );
18028
-
18029
- foreach ( $this->_menu_items as $priority => $items ) {
18030
- foreach ( $items as $item ) {
18031
- if ( isset( $item['url'] ) ) {
18032
- if ( $page === $this->_menu->get_slug( strtolower( $item['menu_slug'] ) ) ) {
18033
- $this->_logger->log( 'Redirecting to ' . $item['url'] );
18034
-
18035
- fs_redirect( $item['url'] );
18036
- }
18037
- }
18038
- }
18039
- }
18040
- }
18041
-
18042
- /**
18043
- * Remove plugin's all admin menu items & pages, and replace with activation page.
18044
- *
18045
- * @author Vova Feldman (@svovaf)
18046
- * @since 1.0.1
18047
- */
18048
- private function override_plugin_menu_with_activation() {
18049
- $this->_logger->entrance();
18050
-
18051
- $hook = false;
18052
-
18053
- if ( ! $this->has_settings_menu() ) {
18054
- // Add the opt-in page without a menu item.
18055
- $hook = FS_Admin_Menu_Manager::add_subpage(
18056
- null,
18057
- $this->get_plugin_name(),
18058
- $this->get_plugin_name(),
18059
- 'manage_options',
18060
- $this->_slug,
18061
- array( $this, '_connect_page_render' )
18062
- );
18063
- } else if ( $this->_menu->is_top_level() ) {
18064
- if ( $this->_menu->is_override_exact() ) {
18065
- // Make sure the current page is matching the activation page.
18066
- if ( ! $this->is_matching_url( $this->get_activation_url() ) ) {
18067
- return;
18068
- }
18069
- }
18070
-
18071
- $hook = $this->_menu->override_menu_item( array( $this, '_connect_page_render' ) );
18072
-
18073
- if ( false === $hook ) {
18074
- // Create new menu item just for the opt-in.
18075
- $hook = FS_Admin_Menu_Manager::add_page(
18076
- $this->get_plugin_name(),
18077
- $this->get_plugin_name(),
18078
- 'manage_options',
18079
- $this->_menu->get_slug(),
18080
- array( $this, '_connect_page_render' )
18081
- );
18082
- }
18083
- } else {
18084
- $menus = array( $this->_menu->get_parent_slug() );
18085
-
18086
- if ( $this->_menu->is_override_exact() ) {
18087
- // Make sure the current page is matching the activation page.
18088
- if ( ! $this->is_matching_url( $this->get_activation_url() ) ) {
18089
- return;
18090
- }
18091
- }
18092
-
18093
- foreach ( $menus as $parent_slug ) {
18094
- $hook = $this->_menu->override_submenu_action(
18095
- $parent_slug,
18096
- $this->_menu->get_raw_slug(),
18097
- array( $this, '_connect_page_render' )
18098
- );
18099
-
18100
- if ( false !== $hook ) {
18101
- // Found plugin's submenu item.
18102
- break;
18103
- }
18104
- }
18105
- }
18106
-
18107
- if ( $this->is_activation_page() ) {
18108
- // Clean admin page from distracting content.
18109
- self::_clean_admin_content_section();
18110
- }
18111
-
18112
- if ( false !== $hook ) {
18113
- if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) ) {
18114
- $this->_install_with_current_user();
18115
- } else if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) {
18116
- $this->_install_with_new_user();
18117
- }
18118
- }
18119
- }
18120
-
18121
- /**
18122
- * If a plugin was network activated and connected but don't have a network
18123
- * level settings, then add an artificial menu item for the Account and other
18124
- * Freemius settings.
18125
- *
18126
- * @author Vova Feldman (@svovaf)
18127
- * @since 2.0.0
18128
- */
18129
- private function add_network_menu_when_missing() {
18130
- $this->_logger->entrance();
18131
-
18132
- if ( ! $this->_is_network_active ) {
18133
- // Plugin wasn't activated on the network level.
18134
- return;
18135
- }
18136
-
18137
- if ( ! fs_is_network_admin() ) {
18138
- // The context is not the network admin.
18139
- return;
18140
- }
18141
-
18142
- if ( $this->_menu->has_network_menu() ) {
18143
- // Plugin already has a network level menu.
18144
- return;
18145
- }
18146
-
18147
- if ( $this->is_network_activation_mode() ) {
18148
- /**
18149
- * Do not add during activation mode, otherwise, there will be duplicate menus while the opt-in
18150
- * screen is being shown.
18151
- *
18152
- * @author Leo Fajardo (@leorw)
18153
- */
18154
- return;
18155
- }
18156
-
18157
- if ( ! WP_FS__SHOW_NETWORK_EVEN_WHEN_DELEGATED ) {
18158
- if ( $this->is_network_delegated_connection() ) {
18159
- // Super-admin delegated the connection to the site admins.
18160
- return;
18161
- }
18162
- }
18163
-
18164
- if ( ! $this->_menu->has_menu() || $this->_menu->is_top_level() ) {
18165
-
18166
- if ( $this->_menu->has_menu() ||
18167
- ! $this->is_addon() ||
18168
- $this->is_activation_mode()
18169
- ) {
18170
- $this->_dynamically_added_top_level_page_hook_name = $this->_menu->add_page_and_update(
18171
- $this->get_plugin_name(),
18172
- $this->get_plugin_name(),
18173
- 'manage_options',
18174
- $this->_menu->has_menu() ? $this->_menu->get_slug() : $this->_slug
18175
- );
18176
- }
18177
- } else {
18178
- $this->_menu->add_subpage_and_update(
18179
- $this->_menu->get_parent_slug(),
18180
- $this->get_plugin_name(),
18181
- $this->get_plugin_name(),
18182
- 'manage_options',
18183
- $this->_menu->get_slug()
18184
- );
18185
- }
18186
- }
18187
-
18188
- /**
18189
- * @author Leo Fajardo (@leorw)
18190
- * @since 1.2.1
18191
- *
18192
- * return string
18193
- */
18194
- function get_top_level_menu_capability() {
18195
- global $menu;
18196
-
18197
- $top_level_menu_slug = $this->get_top_level_menu_slug();
18198
-
18199
- foreach ( $menu as $menu_info ) {
18200
- /**
18201
- * The second element in the menu info array is the capability/role that has access to the menu and the
18202
- * third element is the menu slug.
18203
- */
18204
- if ( $menu_info[2] === $top_level_menu_slug ) {
18205
- return $menu_info[1];
18206
- }
18207
- }
18208
-
18209
- return 'read';
18210
- }
18211
-
18212
- /**
18213
- * @author Vova Feldman (@svovaf)
18214
- * @since 1.0.0
18215
- *
18216
- * @return string
18217
- */
18218
- private function get_top_level_menu_slug() {
18219
- return ( $this->is_addon() ?
18220
- $this->get_parent_instance()->_menu->get_top_level_menu_slug() :
18221
- $this->_menu->get_top_level_menu_slug() );
18222
- }
18223
-
18224
- /**
18225
- * @author Vova Feldman (@svovaf)
18226
- * @since 1.2.2.7
18227
- *
18228
- * @return string
18229
- */
18230
- function get_pricing_cta_label() {
18231
- $label = $this->get_text_inline( 'Upgrade', 'upgrade' );
18232
-
18233
- if ( $this->is_in_trial_promotion() &&
18234
- ! $this->is_paying_or_trial()
18235
- ) {
18236
- // If running a trial promotion, modify the pricing to load the trial.
18237
- $label = $this->get_text_inline( 'Start Trial', 'start-trial' );
18238
- } else if ( $this->is_paying() ) {
18239
- $label = $this->get_text_inline( 'Pricing', 'pricing' );
18240
- }
18241
-
18242
- return $label;
18243
- }
18244
-
18245
- /**
18246
- * @author Vova Feldman (@svovaf)
18247
- * @since 1.2.2.7
18248
- *
18249
- * @return bool
18250
- */
18251
- function is_pricing_page_visible() {
18252
- return (
18253
- // Has at least one paid plan.
18254
- $this->has_paid_plan() &&
18255
- // Didn't ask to hide the pricing page.
18256
- $this->is_page_visible( 'pricing' ) &&
18257
- // Don't have a valid active license or has more than one plan.
18258
- ( ! $this->is_paying() || ! $this->is_single_plan( true ) )
18259
- );
18260
- }
18261
-
18262
- /**
18263
- * @author Leo Fajardo (@leorw)
18264
- * @since 2.3.0
18265
- *
18266
- * @param bool $is_activation_mode
18267
- *
18268
- * @return bool
18269
- */
18270
- private function should_add_submenu_or_action_links( $is_activation_mode ) {
18271
- if ( $this->is_addon() ) {
18272
- // No submenu items or action links for add-ons.
18273
- return false;
18274
- }
18275
-
18276
- if ( $this->show_opt_in_on_themes_page() ) {
18277
- if ( ! fs_is_network_admin() ) {
18278
- // Also add action links or submenu items when running in a free .org theme so the tabs will be visible.
18279
- return true;
18280
- }
18281
- } else if ( $is_activation_mode ) {
18282
- // Don't show submenu-items/tabs in activation mode, unless it's a wp.org theme.
18283
- return false;
18284
- }
18285
-
18286
- if ( fs_is_network_admin() ) {
18287
- /**
18288
- * Add submenu items or action links to network level when plugin was network activated and the super
18289
- * admin did NOT delegate the connection of all sites to site admins.
18290
- */
18291
- return (
18292
- $this->_is_network_active &&
18293
- ( WP_FS__SHOW_NETWORK_EVEN_WHEN_DELEGATED ||
18294
- ! $this->is_network_delegated_connection() )
18295
- );
18296
- }
18297
-
18298
- return ( ! $this->_is_network_active || $this->is_delegated_connection() );
18299
- }
18300
-
18301
- /**
18302
- * Add default Freemius menu items.
18303
- *
18304
- * @author Vova Feldman (@svovaf)
18305
- * @since 1.0.0
18306
- * @since 1.2.2.7 Also add submenu items when running in a free .org theme so the tabs will be visible.
18307
- */
18308
- private function add_submenu_items() {
18309
- $this->_logger->entrance();
18310
-
18311
- $is_activation_mode = $this->is_activation_mode();
18312
-
18313
- $add_submenu_items = $this->should_add_submenu_or_action_links( $is_activation_mode );
18314
-
18315
- if ( $add_submenu_items ) {
18316
- if ( $this->has_affiliate_program() ) {
18317
- // Add affiliation page.
18318
- $this->add_submenu_item(
18319
- $this->get_text_inline( 'Affiliation', 'affiliation' ),
18320
- array( $this, '_affiliation_page_render' ),
18321
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Affiliation', 'affiliation' ),
18322
- 'manage_options',
18323
- 'affiliation',
18324
- 'Freemius::_clean_admin_content_section',
18325
- WP_FS__DEFAULT_PRIORITY,
18326
- $this->is_submenu_item_visible( 'affiliation' )
18327
- );
18328
- }
18329
- }
18330
-
18331
- if ( $add_submenu_items ||
18332
- ( $is_activation_mode &&
18333
- $this->is_only_premium() &&
18334
- $this->is_admin_page( 'account' ) &&
18335
- fs_request_is_action( $this->get_unique_affix() . '_sync_license' )
18336
- )
18337
- ) {
18338
- if ( ! WP_FS__DEMO_MODE && $this->is_registered() ) {
18339
- $show_account = (
18340
- $this->is_submenu_item_visible( 'account' ) &&
18341
- /**
18342
- * @since 1.2.2.7 Don't show the Account for free WP.org themes without any paid plans.
18343
- */
18344
- ( ! $this->is_free_wp_org_theme() || $this->has_paid_plan() )
18345
- );
18346
-
18347
- // Add user account page.
18348
- $this->add_submenu_item(
18349
- $this->get_text_inline( 'Account', 'account' ),
18350
- array( $this, '_account_page_render' ),
18351
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Account', 'account' ),
18352
- 'manage_options',
18353
- 'account',
18354
- array( $this, '_account_page_load' ),
18355
- WP_FS__DEFAULT_PRIORITY,
18356
- ( $add_submenu_items && $show_account )
18357
- );
18358
- }
18359
- }
18360
-
18361
- if ( $add_submenu_items ) {
18362
- if (! WP_FS__DEMO_MODE && ! $this->is_whitelabeled() ) {
18363
- // Add contact page.
18364
- $this->add_submenu_item(
18365
- $this->get_text_inline( 'Contact Us', 'contact-us' ),
18366
- array( $this, '_contact_page_render' ),
18367
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Contact Us', 'contact-us' ),
18368
- 'manage_options',
18369
- 'contact',
18370
- 'Freemius::_clean_admin_content_section',
18371
- WP_FS__DEFAULT_PRIORITY,
18372
- $this->is_submenu_item_visible( 'contact' )
18373
- );
18374
- }
18375
-
18376
- if ( $this->has_addons() ) {
18377
- $this->add_submenu_item(
18378
- $this->get_text_inline( 'Add-Ons', 'add-ons' ),
18379
- array( $this, '_addons_page_render' ),
18380
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Add-Ons', 'add-ons' ),
18381
- 'manage_options',
18382
- 'addons',
18383
- array( $this, '_addons_page_load' ),
18384
- WP_FS__LOWEST_PRIORITY - 1,
18385
- $this->is_submenu_item_visible( 'addons' )
18386
- );
18387
- }
18388
- }
18389
-
18390
- if ( $add_submenu_items ||
18391
- ( $is_activation_mode && $this->is_only_premium() && $this->is_admin_page( 'pricing' ) )
18392
- ) {
18393
- if (! WP_FS__DEMO_MODE && ! $this->is_whitelabeled() ) {
18394
- $show_pricing = (
18395
- $this->is_submenu_item_visible( 'pricing' ) &&
18396
- $this->is_pricing_page_visible()
18397
- );
18398
-
18399
- $pricing_cta_text = $this->get_pricing_cta_label();
18400
- $pricing_class = 'upgrade-mode';
18401
- if ( $show_pricing ) {
18402
- if ( $this->is_in_trial_promotion() &&
18403
- ! $this->is_paying_or_trial()
18404
- ) {
18405
- // If running a trial promotion, modify the pricing to load the trial.
18406
- $pricing_class = 'trial-mode';
18407
- } else if ( $this->is_paying() ) {
18408
- $pricing_class = '';
18409
- }
18410
- }
18411
-
18412
- // Add upgrade/pricing page.
18413
- $this->add_submenu_item(
18414
- $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' ) ),
18415
- array( $this, '_pricing_page_render' ),
18416
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text_x_inline( 'Pricing', 'noun', 'pricing' ),
18417
- 'manage_options',
18418
- 'pricing',
18419
- 'Freemius::_clean_admin_content_section',
18420
- WP_FS__LOWEST_PRIORITY,
18421
- ( $add_submenu_items && $show_pricing ),
18422
- $pricing_class
18423
- );
18424
- }
18425
- }
18426
-
18427
- if ( ! $is_activation_mode || ( true !== $this->_storage->require_license_activation ) ) {
18428
- /**
18429
- * Add the other menu items if there are any when not in activation mode or license activation is not
18430
- * required (license activation is required for registered or anonymous users after activating the
18431
- * premium version when the site is not in trial mode or there's no active valid license).
18432
- *
18433
- * @author Leo Fajardo (@leorw)
18434
- * @since 2.2.1
18435
- */
18436
- if ( 0 < count( $this->_menu_items ) ) {
18437
- if ( ! $this->_menu->is_top_level() ) {
18438
- fs_enqueue_local_style( 'fs_common', '/admin/common.css' );
18439
-
18440
- // Append submenu items right after the plugin's submenu item.
18441
- $this->order_sub_submenu_items();
18442
- } else {
18443
- // Append submenu items.
18444
- $this->embed_submenu_items();
18445
- }
18446
- }
18447
- }
18448
- }
18449
-
18450
- /**
18451
- * Moved the actual submenu item additions to a separated function,
18452
- * in order to support sub-submenu items when the plugin's settings
18453
- * only have a submenu and not top-level menu item.
18454
- *
18455
- * @author Vova Feldman (@svovaf)
18456
- * @since 1.1.4
18457
- */
18458
- private function embed_submenu_items() {
18459
- $item_template = $this->_menu->is_top_level() ?
18460
- '<span class="fs-submenu-item %s %s %s">%s</span>' :
18461
- '<span class="fs-submenu-item fs-sub %s %s %s">%s</span>';
18462
-
18463
- $top_level_menu_capability = $this->get_top_level_menu_capability();
18464
-
18465
- ksort( $this->_menu_items );
18466
-
18467
- $is_first_submenu_item = true;
18468
-
18469
- foreach ( $this->_menu_items as $priority => $items ) {
18470
- foreach ( $items as $item ) {
18471
- $capability = ( ! empty( $item['capability'] ) ? $item['capability'] : $top_level_menu_capability );
18472
-
18473
- $menu_item = sprintf(
18474
- $item_template,
18475
- $this->get_unique_affix(),
18476
- $item['menu_slug'],
18477
- ! empty( $item['class'] ) ? $item['class'] : '',
18478
- $item['menu_title']
18479
- );
18480
-
18481
- $top_level_menu_slug = $this->get_top_level_menu_slug();
18482
- $menu_slug = $this->_menu->get_slug( $item['menu_slug'] );
18483
-
18484
- if ( ! isset( $item['url'] ) ) {
18485
- $hook = FS_Admin_Menu_Manager::add_subpage(
18486
- $item['show_submenu'] ?
18487
- $top_level_menu_slug :
18488
- null,
18489
- $item['page_title'],
18490
- $menu_item,
18491
- $capability,
18492
- $menu_slug,
18493
- $item['render_function']
18494
- );
18495
-
18496
- if ( false !== $item['before_render_function'] ) {
18497
- add_action( "load-$hook", $item['before_render_function'] );
18498
- }
18499
- } else {
18500
- FS_Admin_Menu_Manager::add_subpage(
18501
- $item['show_submenu'] ?
18502
- $top_level_menu_slug :
18503
- null,
18504
- $item['page_title'],
18505
- $menu_item,
18506
- $capability,
18507
- $menu_slug,
18508
- array( $this, '' )
18509
- );
18510
- }
18511
-
18512
- if ( $item['show_submenu'] && $is_first_submenu_item ) {
18513
- if ( $this->_is_network_active && ! empty( $this->_dynamically_added_top_level_page_hook_name ) ) {
18514
- /**
18515
- * If the top-level menu has been dynamically created, remove the first submenu item that
18516
- * WordPress automatically creates when there's no submenu item whose slug matches the
18517
- * parent's. In the following example, the `Awesome Plugin` submenu item will be removed.
18518
- *
18519
- * Awesome Plugin
18520
- * - Awesome Plugin <-- we want to remove this since there's no real setting page for the top-level
18521
- *
18522
- * @author Leo Fajardo (@leorw)
18523
- */
18524
- remove_submenu_page( $top_level_menu_slug, $top_level_menu_slug );
18525
- }
18526
-
18527
- $is_first_submenu_item = false;
18528
- }
18529
- }
18530
- }
18531
- }
18532
-
18533
- /**
18534
- * Re-order the submenu items so all Freemius added new submenu items
18535
- * are added right after the plugin's settings submenu item.
18536
- *
18537
- * @author Vova Feldman (@svovaf)
18538
- * @since 1.1.4
18539
- */
18540
- private function order_sub_submenu_items() {
18541
- global $submenu;
18542
-
18543
- $menu_slug = $this->_menu->get_top_level_menu_slug();
18544
-
18545
- /**
18546
- * Before "admin_menu" fires, WordPress will loop over the default submenus and remove pages for which the user
18547
- * does not have permissions. So in case a plugin does not have top-level menu but does have submenus under any
18548
- * of the default menus, only users that have the right role can access its sub-submenus (Account, Contact Us,
18549
- * Support Forum, etc.) since $submenu[ $menu_slug ] will be empty if the user doesn't have permission.
18550
- *
18551
- * In case a plugin does not have submenus under any of the default menus but does have submenus under the menu
18552
- * of another plugin, only users that have the right role can access its sub-submenus since we will use the
18553
- * capability needed to access the parent menu as the capability for the submenus that we will add.
18554
- */
18555
- if ( empty( $submenu[ $menu_slug ] ) ) {
18556
- return;
18557
- }
18558
-
18559
- $top_level_menu = &$submenu[ $menu_slug ];
18560
-
18561
- $all_submenu_items_after = array();
18562
-
18563
- $found_submenu_item = false;
18564
-
18565
- foreach ( $top_level_menu as $submenu_id => $meta ) {
18566
- if ( $found_submenu_item ) {
18567
- // Remove all submenu items after the plugin's submenu item.
18568
- $all_submenu_items_after[] = $meta;
18569
- unset( $top_level_menu[ $submenu_id ] );
18570
- }
18571
-
18572
- if ( $this->_menu->get_raw_slug() === $meta[2] ) {
18573
- // Found the submenu item, put all below.
18574
- $found_submenu_item = true;
18575
- continue;
18576
- }
18577
- }
18578
-
18579
- // Embed all plugin's new submenu items.
18580
- $this->embed_submenu_items();
18581
-
18582
- // Start with specially high number to make sure it's appended.
18583
- $i = max( 10000, max( array_keys( $top_level_menu ) ) + 1 );
18584
- foreach ( $all_submenu_items_after as $meta ) {
18585
- $top_level_menu[ $i ] = $meta;
18586
- $i ++;
18587
- }
18588
-
18589
- // Sort submenu items.
18590
- ksort( $top_level_menu );
18591
- }
18592
-
18593
- /**
18594
- * Helper method to return the module's support forum URL.
18595
- *
18596
- * @author Vova Feldman (@svovaf)
18597
- * @since 1.2.2.7
18598
- *
18599
- * @return string
18600
- */
18601
- function get_support_forum_url() {
18602
- return $this->apply_filters( 'support_forum_url', "https://wordpress.org/support/{$this->_module_type}/{$this->_slug}" );
18603
- }
18604
-
18605
- /**
18606
- * Displays the Support Forum link when enabled.
18607
- *
18608
- * Can be filtered like so:
18609
- *
18610
- * function _fs_show_support_menu( $is_visible, $menu_id ) {
18611
- * if ( 'support' === $menu_id ) {
18612
- * return _fs->is_registered();
18613
- * }
18614
- * return $is_visible;
18615
- * }
18616
- * _fs()->add_filter('is_submenu_visible', '_fs_show_support_menu', 10, 2);
18617
- *
18618
- */
18619
- function _add_default_submenu_items() {
18620
- if ( ! $this->is_on() ) {
18621
- return;
18622
- }
18623
-
18624
- if ( ! $this->is_activation_mode() &&
18625
- ( ( $this->_is_network_active && fs_is_network_admin() ) ||
18626
- ( ! $this->_is_network_active && is_admin() ) )
18627
- ) {
18628
- $this->add_submenu_link_item(
18629
- $this->apply_filters( 'support_forum_submenu', $this->get_text_inline( 'Support Forum', 'support-forum' ) ),
18630
- $this->get_support_forum_url(),
18631
- 'wp-support-forum',
18632
- null,
18633
- 50,
18634
- $this->is_submenu_item_visible( 'support' )
18635
- );
18636
- }
18637
- }
18638
-
18639
- /**
18640
- * @author Vova Feldman (@svovaf)
18641
- * @since 1.0.1
18642
- *
18643
- * @param string $menu_title
18644
- * @param callable $render_function
18645
- * @param bool|string $page_title
18646
- * @param string $capability
18647
- * @param bool|string $menu_slug
18648
- * @param bool|callable $before_render_function
18649
- * @param int $priority
18650
- * @param bool $show_submenu
18651
- * @param string $class Since 1.2.1.5 can add custom classes to menu items.
18652
- */
18653
- function add_submenu_item(
18654
- $menu_title,
18655
- $render_function,
18656
- $page_title = false,
18657
- $capability = 'manage_options',
18658
- $menu_slug = false,
18659
- $before_render_function = false,
18660
- $priority = WP_FS__DEFAULT_PRIORITY,
18661
- $show_submenu = true,
18662
- $class = ''
18663
- ) {
18664
- $this->_logger->entrance( 'Title = ' . $menu_title );
18665
-
18666
- if ( $this->is_addon() ) {
18667
- $parent_fs = $this->get_parent_instance();
18668
-
18669
- if ( is_object( $parent_fs ) ) {
18670
- $parent_fs->add_submenu_item(
18671
- $menu_title,
18672
- $render_function,
18673
- $page_title,
18674
- $capability,
18675
- $menu_slug,
18676
- $before_render_function,
18677
- $priority,
18678
- $show_submenu,
18679
- $class
18680
- );
18681
-
18682
- return;
18683
- }
18684
- }
18685
-
18686
- if ( ! isset( $this->_menu_items[ $priority ] ) ) {
18687
- $this->_menu_items[ $priority ] = array();
18688
- }
18689
-
18690
- $this->_menu_items[ $priority ][] = array(
18691
- 'page_title' => is_string( $page_title ) ? $page_title : $menu_title,
18692
- 'menu_title' => $menu_title,
18693
- 'capability' => $capability,
18694
- 'menu_slug' => is_string( $menu_slug ) ? $menu_slug : strtolower( $menu_title ),
18695
- 'render_function' => $render_function,
18696
- 'before_render_function' => $before_render_function,
18697
- 'show_submenu' => $show_submenu,
18698
- 'class' => $class,
18699
- );
18700
- }
18701
-
18702
- /**
18703
- * @author Vova Feldman (@svovaf)
18704
- * @since 1.0.1
18705
- *
18706
- * @param string $menu_title
18707
- * @param string $url
18708
- * @param bool $menu_slug
18709
- * @param string $capability
18710
- * @param int $priority
18711
- * @param bool $show_submenu
18712
- */
18713
- function add_submenu_link_item(
18714
- $menu_title,
18715
- $url,
18716
- $menu_slug = false,
18717
- $capability = 'read',
18718
- $priority = WP_FS__DEFAULT_PRIORITY,
18719
- $show_submenu = true
18720
- ) {
18721
- $this->_logger->entrance( 'Title = ' . $menu_title . '; Url = ' . $url );
18722
-
18723
- if ( $this->is_addon() ) {
18724
- $parent_fs = $this->get_parent_instance();
18725
-
18726
- if ( is_object( $parent_fs ) ) {
18727
- $parent_fs->add_submenu_link_item(
18728
- $menu_title,
18729
- $url,
18730
- $menu_slug,
18731
- $capability,
18732
- $priority,
18733
- $show_submenu
18734
- );
18735
-
18736
- return;
18737
- }
18738
- }
18739
-
18740
- if ( ! isset( $this->_menu_items[ $priority ] ) ) {
18741
- $this->_menu_items[ $priority ] = array();
18742
- }
18743
-
18744
- $this->_menu_items[ $priority ][] = array(
18745
- 'menu_title' => $menu_title,
18746
- 'capability' => $capability,
18747
- 'menu_slug' => is_string( $menu_slug ) ? $menu_slug : strtolower( $menu_title ),
18748
- 'url' => $url,
18749
- 'page_title' => $menu_title,
18750
- 'render_function' => 'fs_dummy',
18751
- 'before_render_function' => '',
18752
- 'show_submenu' => $show_submenu,
18753
- );
18754
- }
18755
-
18756
- #endregion ------------------------------------------------------------------
18757
-
18758
- #--------------------------------------------------------------------------------
18759
- #region Admin Notices
18760
- #--------------------------------------------------------------------------------
18761
-
18762
- /**
18763
- * @author Vova Feldman (@svovaf)
18764
- * @since 2.3.1
18765
- *
18766
- * @param string|string[] $ids
18767
- * @param int|null $network_level_or_blog_id
18768
- *
18769
- * @uses FS_Admin_Notices::remove_sticky()
18770
- */
18771
- function remove_sticky( $ids, $network_level_or_blog_id = null ) {
18772
- $this->_admin_notices->remove_sticky( $ids, $network_level_or_blog_id );
18773
- }
18774
-
18775
- #endregion
18776
-
18777
- #--------------------------------------------------------------------------------
18778
- #region Actions / Hooks / Filters
18779
- #--------------------------------------------------------------------------------
18780
-
18781
- /**
18782
- * @author Vova Feldman (@svovaf)
18783
- * @since 1.1.7
18784
- *
18785
- * @param string $tag
18786
- *
18787
- * @return string
18788
- */
18789
- public function get_action_tag( $tag ) {
18790
- return self::get_action_tag_static( $tag, $this->_slug, $this->is_plugin() );
18791
- }
18792
-
18793
- /**
18794
- * @author Vova Feldman (@svovaf)
18795
- * @since 1.2.1.6
18796
- *
18797
- * @param string $tag
18798
- * @param string $slug
18799
- * @param bool $is_plugin
18800
- *
18801
- * @return string
18802
- */
18803
- static function get_action_tag_static( $tag, $slug = '', $is_plugin = true ) {
18804
- $action = "fs_{$tag}";
18805
-
18806
- if ( ! empty( $slug ) ) {
18807
- $action .= '_' . self::get_module_unique_affix( $slug, $is_plugin );
18808
- }
18809
-
18810
- return $action;
18811
- }
18812
-
18813
- /**
18814
- * Returns a string that can be used to generate a unique action name,
18815
- * option name, HTML element ID, or HTML element class.
18816
- *
18817
- * @author Leo Fajardo (@leorw)
18818
- * @since 1.2.2
18819
- *
18820
- * @return string
18821
- */
18822
- public function get_unique_affix() {
18823
- return self::get_module_unique_affix( $this->_slug, $this->is_plugin() );
18824
- }
18825
-
18826
- /**
18827
- * Returns a string that can be used to generate a unique action name,
18828
- * option name, HTML element ID, or HTML element class.
18829
- *
18830
- * @author Vova Feldman (@svovaf)
18831
- * @since 1.2.2.5
18832
- *
18833
- * @param string $slug
18834
- * @param bool $is_plugin
18835
- *
18836
- * @return string
18837
- */
18838
- static function get_module_unique_affix( $slug, $is_plugin = true ) {
18839
- $affix = $slug;
18840
-
18841
- if ( ! $is_plugin ) {
18842
- $affix .= '-' . WP_FS__MODULE_TYPE_THEME;
18843
- }
18844
-
18845
- return $affix;
18846
- }
18847
-
18848
- /**
18849
- * @author Vova Feldman (@svovaf)
18850
- * @since 1.2.1
18851
- * @since 1.2.2.5 The AJAX action names are based on the module ID, not like the non-AJAX actions that are
18852
- * based on the slug for backward compatibility.
18853
- *
18854
- * @param string $tag
18855
- *
18856
- * @return string
18857
- */
18858
- function get_ajax_action( $tag ) {
18859
- return self::get_ajax_action_static( $tag, $this->_module_id );
18860
- }
18861
-
18862
- /**
18863
- * @author Vova Feldman (@svovaf)
18864
- * @since 1.2.1.7
18865
- *
18866
- * @param string $tag
18867
- *
18868
- * @return string
18869
- */
18870
- function get_ajax_security( $tag ) {
18871
- return wp_create_nonce( $this->get_ajax_action( $tag ) );
18872
- }
18873
-
18874
- /**
18875
- * @author Vova Feldman (@svovaf)
18876
- * @since 1.2.1.7
18877
- *
18878
- * @param string $tag
18879
- */
18880
- function check_ajax_referer( $tag ) {
18881
- check_ajax_referer( $this->get_ajax_action( $tag ), 'security' );
18882
- }
18883
-
18884
- /**
18885
- * @author Vova Feldman (@svovaf)
18886
- * @since 1.2.1.6
18887
- * @since 1.2.2.5 The AJAX action names are based on the module ID, not like the non-AJAX actions that are
18888
- * based on the slug for backward compatibility.
18889
- *
18890
- * @param string $tag
18891
- * @param number|null $module_id
18892
- *
18893
- * @return string
18894
- */
18895
- private static function get_ajax_action_static( $tag, $module_id = null ) {
18896
- $action = "fs_{$tag}";
18897
-
18898
- if ( ! empty( $module_id ) ) {
18899
- $action .= "_{$module_id}";
18900
- }
18901
-
18902
- return $action;
18903
- }
18904
-
18905
- /**
18906
- * Do action, specific for the current context plugin.
18907
- *
18908
- * @author Vova Feldman (@svovaf)
18909
- * @since 1.0.1
18910
- *
18911
- * @param string $tag The name of the action to be executed.
18912
- * @param mixed $arg,... Optional. Additional arguments which are passed on to the
18913
- * functions hooked to the action. Default empty.
18914
- *
18915
- * @uses do_action()
18916
- */
18917
- function do_action( $tag, $arg = '' ) {
18918
- $this->_logger->entrance( $tag );
18919
-
18920
- $args = func_get_args();
18921
-
18922
- call_user_func_array( 'do_action', array_merge(
18923
- array( $this->get_action_tag( $tag ) ),
18924
- array_slice( $args, 1 ) )
18925
- );
18926
- }
18927
-
18928
- /**
18929
- * Add action, specific for the current context plugin.
18930
- *
18931
- * @author Vova Feldman (@svovaf)
18932
- * @since 1.0.1
18933
- *
18934
- * @param string $tag
18935
- * @param callable $function_to_add
18936
- * @param int $priority
18937
- * @param int $accepted_args
18938
- *
18939
- * @uses add_action()
18940
- */
18941
- function add_action(
18942
- $tag,
18943
- $function_to_add,
18944
- $priority = WP_FS__DEFAULT_PRIORITY,
18945
- $accepted_args = 1
18946
- ) {
18947
- $this->_logger->entrance( $tag );
18948
-
18949
- add_action( $this->get_action_tag( $tag ), $function_to_add, $priority, $accepted_args );
18950
- }
18951
-
18952
- /**
18953
- * Add AJAX action, specific for the current context plugin.
18954
- *
18955
- * @author Vova Feldman (@svovaf)
18956
- * @since 1.2.1
18957
- *
18958
- * @param string $tag
18959
- * @param callable $function_to_add
18960
- * @param int $priority
18961
- *
18962
- * @uses add_action()
18963
- *
18964
- * @return bool True if action added, false if no need to add the action since the AJAX call isn't matching.
18965
- */
18966
- function add_ajax_action(
18967
- $tag,
18968
- $function_to_add,
18969
- $priority = WP_FS__DEFAULT_PRIORITY
18970
- ) {
18971
- $this->_logger->entrance( $tag );
18972
-
18973
- return self::add_ajax_action_static(
18974
- $tag,
18975
- $function_to_add,
18976
- $priority,
18977
- $this->_module_id
18978
- );
18979
- }
18980
-
18981
- /**
18982
- * Add AJAX action.
18983
- *
18984
- * @author Vova Feldman (@svovaf)
18985
- * @since 1.2.1.6
18986
- *
18987
- * @param string $tag
18988
- * @param callable $function_to_add
18989
- * @param int $priority
18990
- * @param number|null $module_id
18991
- *
18992
- * @return bool True if action added, false if no need to add the action since the AJAX call isn't matching.
18993
- * @uses add_action()
18994
- *
18995
- */
18996
- static function add_ajax_action_static(
18997
- $tag,
18998
- $function_to_add,
18999
- $priority = WP_FS__DEFAULT_PRIORITY,
19000
- $module_id = null
19001
- ) {
19002
- self::$_static_logger->entrance( $tag );
19003
-
19004
- if ( ! self::is_ajax_action_static( $tag, $module_id ) ) {
19005
- return false;
19006
- }
19007
-
19008
- add_action(
19009
- 'wp_ajax_' . self::get_ajax_action_static( $tag, $module_id ),
19010
- $function_to_add,
19011
- $priority,
19012
- 0
19013
- );
19014
-
19015
- self::$_static_logger->info( "$tag AJAX callback action added." );
19016
-
19017
- return true;
19018
- }
19019
-
19020
- /**
19021
- * Send a JSON response back to an Ajax request.
19022
- *
19023
- * @author Vova Feldman (@svovaf)
19024
- * @since 1.2.1.5
19025
- *
19026
- * @param mixed $response
19027
- */
19028
- static function shoot_ajax_response( $response ) {
19029
- wp_send_json( $response );
19030
- }
19031
-
19032
- /**
19033
- * Send a JSON response back to an Ajax request, indicating success.
19034
- *
19035
- * @author Vova Feldman (@svovaf)
19036
- * @since 1.2.1.5
19037
- *
19038
- * @param mixed $data Data to encode as JSON, then print and exit.
19039
- */
19040
- static function shoot_ajax_success( $data = null ) {
19041
- wp_send_json_success( $data );
19042
- }
19043
-
19044
- /**
19045
- * Send a JSON response back to an Ajax request, indicating failure.
19046
- *
19047
- * @author Vova Feldman (@svovaf)
19048
- * @since 1.2.1.5
19049
- *
19050
- * @param mixed $error Optional error message.
19051
- */
19052
- static function shoot_ajax_failure( $error = '' ) {
19053
- $result = array( 'success' => false );
19054
- if ( ! empty( $error ) ) {
19055
- $result['error'] = $error;
19056
- }
19057
-
19058
- wp_send_json( $result );
19059
- }
19060
-
19061
- /**
19062
- * Apply filter, specific for the current context plugin.
19063
- *
19064
- * @author Vova Feldman (@svovaf)
19065
- * @since 1.0.9
19066
- *
19067
- * @param string $tag The name of the filter hook.
19068
- * @param mixed $value The value on which the filters hooked to `$tag` are applied on.
19069
- *
19070
- * @return mixed The filtered value after all hooked functions are applied to it.
19071
- *
19072
- * @uses apply_filters()
19073
- */
19074
- function apply_filters( $tag, $value ) {
19075
- $this->_logger->entrance( $tag );
19076
-
19077
- $args = func_get_args();
19078
- array_unshift( $args, $this->get_unique_affix() );
19079
-
19080
- return call_user_func_array( 'fs_apply_filter', $args );
19081
- }
19082
-
19083
- /**
19084
- * Add filter, specific for the current context plugin.
19085
- *
19086
- * @author Vova Feldman (@svovaf)
19087
- * @since 1.0.9
19088
- *
19089
- * @param string $tag
19090
- * @param callable $function_to_add
19091
- * @param int $priority
19092
- * @param int $accepted_args
19093
- *
19094
- * @uses add_filter()
19095
- */
19096
- function add_filter( $tag, $function_to_add, $priority = WP_FS__DEFAULT_PRIORITY, $accepted_args = 1 ) {
19097
- $this->_logger->entrance( $tag );
19098
-
19099
- add_filter( $this->get_action_tag( $tag ), $function_to_add, $priority, $accepted_args );
19100
- }
19101
-
19102
- /**
19103
- * Check if has filter.
19104
- *
19105
- * @author Vova Feldman (@svovaf)
19106
- * @since 1.1.4
19107
- *
19108
- * @param string $tag
19109
- * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
19110
- *
19111
- * @return false|int
19112
- *
19113
- * @uses has_filter()
19114
- */
19115
- function has_filter( $tag, $function_to_check = false ) {
19116
- $this->_logger->entrance( $tag );
19117
-
19118
- return has_filter( $this->get_action_tag( $tag ), $function_to_check );
19119
- }
19120
-
19121
- #endregion
19122
-
19123
- /**
19124
- * Override default i18n text phrases.
19125
- *
19126
- * @author Vova Feldman (@svovaf)
19127
- * @since 1.1.6
19128
- *
19129
- * @param string[] string $key_value
19130
- *
19131
- * @uses fs_override_i18n()
19132
- */
19133
- function override_i18n( $key_value ) {
19134
- fs_override_i18n( $key_value, $this->_slug );
19135
- }
19136
-
19137
- /* Account Page
19138
- ------------------------------------------------------------------------------------------------------------------*/
19139
- /**
19140
- * Update site information.
19141
- *
19142
- * @author Vova Feldman (@svovaf)
19143
- * @since 1.0.1
19144
- *
19145
- * @param bool $store Flush to Database if true.
19146
- * @param null|int $network_level_or_blog_id Since 2.0.0
19147
- * @param \FS_Site $site Since 2.0.0
19148
- */
19149
- private function _store_site( $store = true, $network_level_or_blog_id = null, FS_Site $site = null ) {
19150
- $this->_logger->entrance();
19151
-
19152
- if ( is_null( $site ) ) {
19153
- $site = $this->_site;
19154
- }
19155
-
19156
- if ( !isset( $site ) || !is_object($site) || empty( $site->id ) ) {
19157
- $this->_logger->error( "Empty install ID, can't store site." );
19158
-
19159
- return;
19160
- }
19161
-
19162
- $site_clone = clone $site;
19163
-
19164
- $sites = self::get_all_sites( $this->_module_type, $network_level_or_blog_id );
19165
-
19166
- if ( is_object( $this->_user ) && $this->_user->id != $site->user_id ) {
19167
- $this->sync_user_by_current_install( $site->user_id );
19168
-
19169
- $prev_stored_user_id = $this->_storage->get( 'prev_user_id', false, $network_level_or_blog_id );
19170
-
19171
- if ( empty( $prev_stored_user_id ) &&
19172
- is_object($this->_user) && $this->_user->id != $site->user_id
19173
- ) {
19174
- /**
19175
- * Store the current user ID as the previous user ID so that the previous user can be used
19176
- * as the install's owner while the new owner's details are not yet available.
19177
- *
19178
- * This will be executed only in the `replica` site. For example, there are 2 sites, namely `original`
19179
- * and `replica`, then an ownership change was initiated and completed in the `original`, the `replica`
19180
- * will be using the previous user until it is updated again (e.g.: until the next clone of `original`
19181
- * into `replica`.
19182
- *
19183
- * @author Leo Fajardo (@leorw)
19184
- */
19185
- $this->_storage->store( 'prev_user_id', $sites[ $this->_slug ]->user_id, $network_level_or_blog_id );
19186
- }
19187
- }
19188
-
19189
- $sites[ $this->_slug ] = $site_clone;
19190
-
19191
- $this->set_account_option( 'sites', $sites, $store, $network_level_or_blog_id );
19192
- }
19193
-
19194
- /**
19195
- * Update plugin's plans information.
19196
- *
19197
- * @author Vova Feldman (@svovaf)
19198
- * @since 1.0.2
19199
- *
19200
- * @param bool $store Flush to Database if true.
19201
- */
19202
- private function _store_plans( $store = true ) {
19203
- $this->_logger->entrance();
19204
-
19205
- $plans = self::get_all_plans( $this->_module_type );
19206
-
19207
- // Copy plans.
19208
- $encrypted_plans = array();
19209
- for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
19210
- $encrypted_plans[] = self::_encrypt_entity( $this->_plans[ $i ] );
19211
- }
19212
-
19213
- $plans[ $this->_slug ] = $encrypted_plans;
19214
-
19215
- $this->set_account_option( 'plans', $plans, $store );
19216
- }
19217
-
19218
- /**
19219
- * Update user's plugin licenses.
19220
- *
19221
- * @author Vova Feldman (@svovaf)
19222
- * @since 1.0.5
19223
- *
19224
- * @param bool $store
19225
- * @param number|bool $module_id
19226
- * @param FS_Plugin_License[] $licenses
19227
- */
19228
- private function _store_licenses( $store = true, $module_id = false, $licenses = array() ) {
19229
- $this->_logger->entrance();
19230
-
19231
- $all_licenses = self::get_all_licenses();
19232
-
19233
- if ( ! FS_Plugin::is_valid_id( $module_id ) ) {
19234
- $module_id = $this->_module_id;
19235
-
19236
- $user_licenses = is_array( $this->_licenses ) ?
19237
- $this->_licenses :
19238
- array();
19239
-
19240
- if ( empty( $user_licenses ) ) {
19241
- // If the context user doesn't have any license, don't update the licenses collection.
19242
- return;
19243
- }
19244
-
19245
- $new_user_licenses_map = array();
19246
- foreach ( $user_licenses as $user_license ) {
19247
- $new_user_licenses_map[ $user_license->id ] = $user_license;
19248
- }
19249
-
19250
- self::store_user_id_license_ids_map( array_keys( $new_user_licenses_map ), $this->_module_id, $this->_user->id );
19251
-
19252
- // Update user licenses.
19253
- $licenses_to_update_count = count( $new_user_licenses_map );
19254
- foreach ( $all_licenses[ $module_id ] as $key => $license ) {
19255
- if ( 0 === $licenses_to_update_count ) {
19256
- break;
19257
- }
19258
-
19259
- if ( isset( $new_user_licenses_map[ $license->id ] ) ) {
19260
- // Update license.
19261
- $all_licenses[ $module_id ][ $key ] = $new_user_licenses_map[ $license->id ];
19262
- unset( $new_user_licenses_map[ $license->id ] );
19263
-
19264
- $licenses_to_update_count --;
19265
- }
19266
- }
19267
-
19268
- if ( ! empty( $new_user_licenses_map ) ) {
19269
- // Add new licenses.
19270
- $all_licenses[ $module_id ] = array_merge( array_values( $new_user_licenses_map ), $all_licenses[ $module_id ] );
19271
- }
19272
-
19273
- $licenses = $all_licenses[ $module_id ];
19274
- }
19275
-
19276
- if ( ! isset( $all_licenses[ $module_id ] ) ) {
19277
- $all_licenses[ $module_id ] = array();
19278
- }
19279
-
19280
- $all_licenses[ $module_id ] = $licenses;
19281
-
19282
- self::$_accounts->set_option( 'all_licenses', $all_licenses, $store );
19283
- }
19284
-
19285
- /**
19286
- * Update user information.
19287
- *
19288
- * @author Vova Feldman (@svovaf)
19289
- * @since 1.0.1
19290
- *
19291
- * @param bool $store Flush to Database if true.
19292
- */
19293
- private function _store_user( $store = true ) {
19294
- $this->_logger->entrance();
19295
-
19296
- if ( empty( $this->_user->id ) ) {
19297
- $this->_logger->error( "Empty user ID, can't store user." );
19298
-
19299
- return;
19300
- }
19301
-
19302
- $users = self::get_all_users();
19303
- $users[ $this->_user->id ] = $this->_user;
19304
- self::$_accounts->set_option( 'users', $users, $store );
19305
- }
19306
-
19307
- /**
19308
- * Update new updates information.
19309
- *
19310
- * @author Vova Feldman (@svovaf)
19311
- * @since 1.0.4
19312
- *
19313
- * @param FS_Plugin_Tag|null $update
19314
- * @param bool $store Flush to Database if true.
19315
- * @param bool|number $plugin_id
19316
- */
19317
- private function _store_update( $update, $store = true, $plugin_id = false ) {
19318
- $this->_logger->entrance();
19319
-
19320
- if ( $update instanceof FS_Plugin_Tag ) {
19321
- $update->updated = time();
19322
- }
19323
-
19324
- if ( ! is_numeric( $plugin_id ) ) {
19325
- $plugin_id = $this->_plugin->id;
19326
- }
19327
-
19328
- $updates = self::get_all_updates();
19329
- $updates[ $plugin_id ] = $update;
19330
- self::$_accounts->set_option( 'updates', $updates, $store );
19331
- }
19332
-
19333
- /**
19334
- * Update new updates information.
19335
- *
19336
- * @author Vova Feldman (@svovaf)
19337
- * @since 1.0.6
19338
- *
19339
- * @param FS_Plugin[] $plugin_addons
19340
- * @param bool $store Flush to Database if true.
19341
- */
19342
- private function _store_addons( $plugin_addons, $store = true ) {
19343
- $this->_logger->entrance();
19344
-
19345
- $addons = self::get_all_addons();
19346
- $addons[ $this->_plugin->id ] = $plugin_addons;
19347
- self::$_accounts->set_option( 'addons', $addons, $store );
19348
- }
19349
-
19350
- /**
19351
- * Delete plugin's associated add-ons.
19352
- *
19353
- * @author Vova Feldman (@svovaf)
19354
- * @since 1.0.8
19355
- *
19356
- * @param bool $store
19357
- *
19358
- * @return bool
19359
- */
19360
- private function _delete_account_addons( $store = true ) {
19361
- $all_addons = self::get_all_account_addons();
19362
-
19363
- if ( ! isset( $all_addons[ $this->_plugin->id ] ) ) {
19364
- return false;
19365
- }
19366
-
19367
- unset( $all_addons[ $this->_plugin->id ] );
19368
-
19369
- self::$_accounts->set_option( 'account_addons', $all_addons, $store );
19370
-
19371
- return true;
19372
- }
19373
-
19374
- /**
19375
- * Update account add-ons list.
19376
- *
19377
- * @author Vova Feldman (@svovaf)
19378
- * @since 1.0.6
19379
- *
19380
- * @param FS_Plugin[] $addons
19381
- * @param bool $store Flush to Database if true.
19382
- */
19383
- private function _store_account_addons( $addons, $store = true ) {
19384
- $this->_logger->entrance();
19385
-
19386
- $all_addons = self::get_all_account_addons();
19387
- $all_addons[ $this->_plugin->id ] = $addons;
19388
- self::$_accounts->set_option( 'account_addons', $all_addons, $store );
19389
- }
19390
-
19391
- /**
19392
- * Purges the cache for the valid user licenses API call so that when the `Account` or `Add-Ons` page is loaded,
19393
- * the valid user licenses will be fetched again and the account add-ons may be updated.
19394
- *
19395
- * @author Leo Fajardo (@leorw)
19396
- * @since 2.2.4
19397
- */
19398
- private function purge_valid_user_licenses_cache() {
19399
- if ( ! $this->is_registered() ) {
19400
- return;
19401
- }
19402
-
19403
- $this->get_api_user_scope()->purge_cache( $this->get_valid_user_licenses_endpoint() );
19404
- }
19405
-
19406
- /**
19407
- * @author Leo Fajardo (@leorw)
19408
- * @since 2.3.0
19409
- *
19410
- * @param array $all_licenses
19411
- * @param number|null $site_license_id
19412
- * @param bool $include_parent_licenses
19413
- *
19414
- * @return array
19415
- */
19416
- private function get_foreign_licenses_info( $all_licenses, $site_license_id = null, $include_parent_licenses = false ) {
19417
- $foreign_licenses = array(
19418
- 'ids' => array(),
19419
- 'license_keys' => array()
19420
- );
19421
-
19422
- $parent_license_ids_map = array();
19423
-
19424
- foreach ( $all_licenses as $license ) {
19425
- if ( $license->user_id == $this->_user->id || $license->id == $site_license_id ) {
19426
- continue;
19427
- }
19428
-
19429
- $foreign_licenses['ids'][] = $license->id;
19430
- $foreign_licenses['license_keys'][] = $license->secret_key;
19431
-
19432
- if (
19433
- $include_parent_licenses &&
19434
- is_object( $this->_license ) &&
19435
- FS_Plugin_License::is_valid_id( $this->_license->parent_license_id ) &&
19436
- ! isset( $parent_license_ids_map[ $this->_license->parent_license_id ] )
19437
- ) {
19438
- /**
19439
- * Include the parent license's info only if it has not been included before since child licenses
19440
- * can have the same parent license.
19441
- */
19442
- $foreign_licenses['ids'][] = $this->_license->parent_license_id;
19443
- $foreign_licenses['license_keys'][] = $license->secret_key;
19444
-
19445
- $parent_license_ids_map[ $this->_license->parent_license_id ] = true;
19446
- }
19447
- }
19448
-
19449
- if ( empty( $foreign_licenses['ids'] ) ) {
19450
- $foreign_licenses = array();
19451
- }
19452
-
19453
- return $foreign_licenses;
19454
- }
19455
-
19456
- /**
19457
- * @author Leo Fajardo (@leorw)
19458
- * @since 2.3.0
19459
- *
19460
- * @return string
19461
- */
19462
- private function get_valid_user_licenses_endpoint() {
19463
- $user_licenses_endpoint = '/licenses.json?type=active' .
19464
- ( FS_Plugin::is_valid_id( $this->get_bundle_id() ) ? '&is_enriched=true' : '' );
19465
-
19466
- $foreign_licenses = $this->get_foreign_licenses_info( self::get_all_licenses( $this->_module_id ), null, true );
19467
-
19468
- if ( ! empty ( $foreign_licenses ) ) {
19469
- $foreign_licenses = array(
19470
- // Prefix with `+` to tell the server to include foreign licenses in the licenses collection.
19471
- 'ids' => ( urlencode( '+' ) . implode( ',', $foreign_licenses['ids'] ) ),
19472
- 'license_keys' => implode( ',', array_map( 'urlencode', $foreign_licenses['license_keys'] ) )
19473
- );
19474
-
19475
- $user_licenses_endpoint = add_query_arg( $foreign_licenses, $user_licenses_endpoint );
19476
- }
19477
-
19478
- return $user_licenses_endpoint;
19479
- }
19480
-
19481
- /**
19482
- * Fetches active licenses that are enriched with product type if there's a context `bundle_id` and bundle
19483
- * licenses enriched with product IDs if there are any. From the licenses, the `get_updated_account_addons`
19484
- * method filters out non–add-on product IDs and stores the add-on IDs.
19485
- *
19486
- * @author Leo Fajardo (@leorw)
19487
- * @since 2.2.4
19488
- *
19489
- * @return stdClass[] array
19490
- */
19491
- private function fetch_valid_user_licenses() {
19492
- $this->_logger->entrance();
19493
-
19494
- $result = $this->get_api_user_scope()->get( $this->get_valid_user_licenses_endpoint() );
19495
-
19496
- if ( ! $this->is_api_result_object( $result, 'licenses' ) ||
19497
- ! is_array( $result->licenses )
19498
- ) {
19499
- return array();
19500
- }
19501
-
19502
- return $result->licenses;
19503
- }
19504
-
19505
- /**
19506
- * @author Leo Fajardo (@leorw)
19507
- * @since 2.2.4
19508
- *
19509
- * @return number[] Account add-on IDs.
19510
- */
19511
- function get_updated_account_addons() {
19512
- $addons = $this->get_addons();
19513
- if ( empty( $addons ) ) {
19514
- return array();
19515
- }
19516
-
19517
- $account_addons = $this->get_account_addons();
19518
- if ( ! is_array( $account_addons ) ) {
19519
- $account_addons = array();
19520
- }
19521
-
19522
- $user_licenses = $this->is_registered() ?
19523
- $this->fetch_valid_user_licenses() :
19524
- array();
19525
-
19526
- if ( empty( $user_licenses ) ) {
19527
- return $account_addons;
19528
- }
19529
-
19530
- $addon_ids = array();
19531
- foreach ( $addons as $addon ) {
19532
- $addon_ids[] = $addon->id;
19533
- }
19534
-
19535
- $license_product_ids = array();
19536
-
19537
- foreach ( $user_licenses as $license ) {
19538
- if ( isset( $license->plugin_type ) && 'bundle' === $license->plugin_type ) {
19539
- $license_product_ids = array_merge( $license_product_ids, $license->products );
19540
- } else {
19541
- $license_product_ids[] = $license->plugin_id;
19542
- }
19543
- }
19544
-
19545
- // Filter out non–add-on IDs.
19546
- $new_account_addons = array_intersect( $addon_ids, $license_product_ids );
19547
- if ( count( $new_account_addons ) !== count( $account_addons ) ) {
19548
- $this->_store_account_addons( array_unique( $new_account_addons ) );
19549
- }
19550
-
19551
- return $new_account_addons;
19552
- }
19553
-
19554
- /**
19555
- * Store account params in the Database.
19556
- *
19557
- * @author Vova Feldman (@svovaf)
19558
- * @since 1.0.1
19559
- *
19560
- * @param null|int $blog_id Since 2.0.0
19561
- */
19562
- private function _store_account( $blog_id = null ) {
19563
- $this->_logger->entrance();
19564
-
19565
- $this->_store_site( false, $blog_id );
19566
- $this->_store_user( false );
19567
- $this->_store_plans( false );
19568
- $this->_store_licenses( false );
19569
-
19570
- self::$_accounts->store( $blog_id );
19571
- }
19572
-
19573
- /**
19574
- * Sync user's information.
19575
- *
19576
- * @author Vova Feldman (@svovaf)
19577
- * @since 1.0.3
19578
- * @uses FS_Api
19579
- */
19580
- private function _handle_account_user_sync() {
19581
- $this->_logger->entrance();
19582
-
19583
- $api = $this->get_api_user_scope();
19584
-
19585
- // Get user's information.
19586
- $user = $api->get( '/', true );
19587
-
19588
- if ( isset( $user->id ) ) {
19589
- $this->_user->first = $user->first;
19590
- $this->_user->last = $user->last;
19591
- $this->_user->email = $user->email;
19592
-
19593
- $is_menu_item_account_visible = $this->is_submenu_item_visible( 'account' );
19594
-
19595
- if ( $user->is_verified &&
19596
- ( ! isset( $this->_user->is_verified ) || false === $this->_user->is_verified )
19597
- ) {
19598
- $this->_user->is_verified = true;
19599
-
19600
- $this->do_action( 'account_email_verified', $user->email );
19601
-
19602
- $this->_admin_notices->add(
19603
- $this->get_text_inline( 'Your email has been successfully verified - you are AWESOME!', 'email-verified-message' ),
19604
- $this->get_text_x_inline( 'Right on', 'a positive response', 'right-on' ) . '!',
19605
- 'success',
19606
- // Make admin sticky if account menu item is invisible,
19607
- // since the page will be auto redirected to the plugin's
19608
- // main settings page, and the non-sticky message
19609
- // will disappear.
19610
- ! $is_menu_item_account_visible,
19611
- 'email_verified'
19612
- );
19613
- }
19614
-
19615
- // Flush user details to DB.
19616
- $this->_store_user();
19617
-
19618
- $this->do_action( 'after_account_user_sync', $user );
19619
-
19620
- /**
19621
- * If account menu item is hidden, redirect to plugin's main settings page.
19622
- *
19623
- * @author Vova Feldman (@svovaf)
19624
- * @since 1.1.6
19625
- *
19626
- * @link https://github.com/Freemius/wordpress-sdk/issues/6
19627
- */
19628
- if ( ! $is_menu_item_account_visible ) {
19629
- fs_redirect( $this->_get_admin_page_url() );
19630
- }
19631
- }
19632
- }
19633
-
19634
- /**
19635
- * @author Vova Feldman (@svovaf)
19636
- * @since 1.0.9
19637
- * @uses FS_Api
19638
- *
19639
- * @param number|bool $license_id
19640
- *
19641
- * @return FS_Subscription|object|bool
19642
- */
19643
- private function _fetch_site_license_subscription( $license_id = false ) {
19644
- $this->_logger->entrance();
19645
- $api = $this->get_api_site_scope();
19646
-
19647
- if ( ! is_numeric( $license_id ) ) {
19648
- $license_id = FS_Plugin_License::is_valid_id( $this->_license->parent_license_id ) ?
19649
- $this->_license->parent_license_id :
19650
- $this->_license->id;
19651
- }
19652
-
19653
- $result = $api->get( "/licenses/{$license_id}/subscriptions.json", true );
19654
-
19655
- return ! isset( $result->error ) ?
19656
- ( ( is_array( $result->subscriptions ) && 0 < count( $result->subscriptions ) ) ?
19657
- new FS_Subscription( $result->subscriptions[0] ) :
19658
- false
19659
- ) :
19660
- $result;
19661
- }
19662
-
19663
- /**
19664
- * @author Vova Feldman (@svovaf)
19665
- * @since 1.0.4
19666
- * @uses FS_Api
19667
- *
19668
- * @param number|bool $plan_id
19669
- *
19670
- * @return FS_Plugin_Plan|object
19671
- */
19672
- private function _fetch_site_plan( $plan_id = false ) {
19673
- $this->_logger->entrance();
19674
- $api = $this->get_api_site_scope();
19675
-
19676
- if ( ! is_numeric( $plan_id ) ) {
19677
- $plan_id = $this->_site->plan_id;
19678
- }
19679
-
19680
- $plan = $api->get( "/plans/{$plan_id}.json", true );
19681
-
19682
- return ! isset( $plan->error ) ? new FS_Plugin_Plan( $plan ) : $plan;
19683
- }
19684
-
19685
- /**
19686
- * @author Vova Feldman (@svovaf)
19687
- * @since 1.0.5
19688
- * @uses FS_Api
19689
- *
19690
- * @return FS_Plugin_Plan[]|object
19691
- */
19692
- private function _fetch_plugin_plans() {
19693
- $this->_logger->entrance();
19694
- $api = $this->get_current_or_network_user_api_scope();
19695
-
19696
- /**
19697
- * @since 1.2.3 When running in DEV mode, retrieve pending plans as well.
19698
- */
19699
- $result = $api->get( $this->add_show_pending( "/plugins/{$this->_module_id}/plans.json" ), true );
19700
-
19701
- if ( $this->is_api_result_object( $result, 'plans' ) && is_array( $result->plans ) ) {
19702
- for ( $i = 0, $len = count( $result->plans ); $i < $len; $i ++ ) {
19703
- $result->plans[ $i ] = new FS_Plugin_Plan( $result->plans[ $i ] );
19704
- }
19705
-
19706
- $result = $result->plans;
19707
- }
19708
-
19709
- return $result;
19710
- }
19711
-
19712
- /**
19713
- * @author Vova Feldman (@svovaf)
19714
- * @since 2.0.0
19715
- *
19716
- * @param number $plan_id
19717
- *
19718
- * @return \FS_Plugin_Plan|object
19719
- */
19720
- private function fetch_plan_by_id( $plan_id ) {
19721
- $this->_logger->entrance();
19722
- $api = $this->get_current_or_network_user_api_scope();
19723
-
19724
- $result = $api->get( "/plugins/{$this->_module_id}/plans/{$plan_id}.json", true );
19725
-
19726
- return $this->is_api_result_entity( $result ) ?
19727
- new FS_Plugin_Plan( $result ) :
19728
- $result;
19729
- }
19730
-
19731
- /**
19732
- * @author Vova Feldman (@svovaf)
19733
- * @since 1.0.5
19734
- * @uses FS_Api
19735
- *
19736
- * @param number|bool $plugin_id
19737
- * @param number|bool $site_license_id
19738
- * @param array $foreign_licenses @since 2.0.0. This is used by network-activated plugins.
19739
- * @param number|null $blog_id
19740
- *
19741
- * @return FS_Plugin_License[]|object
19742
- */
19743
- private function _fetch_licenses(
19744
- $plugin_id = false,
19745
- $site_license_id = false,
19746
- $foreign_licenses = array(),
19747
- $blog_id = null
19748
- ) {
19749
- $this->_logger->entrance();
19750
-
19751
- $api = $this->get_api_user_scope();
19752
-
19753
- if ( ! is_numeric( $plugin_id ) ) {
19754
- $plugin_id = $this->_plugin->id;
19755
- }
19756
-
19757
- $user_licenses_endpoint = "/plugins/{$plugin_id}/licenses.json?is_enriched=true";
19758
- if ( ! empty ( $foreign_licenses ) ) {
19759
- $foreign_licenses = array(
19760
- // Prefix with `+` to tell the server to include foreign licenses in the licenses collection.
19761
- 'ids' => ( urlencode( '+' ) . implode( ',', $foreign_licenses['ids'] ) ),
19762
- 'license_keys' => implode( ',', array_map( 'urlencode', $foreign_licenses['license_keys'] ) )
19763
- );
19764
-
19765
- $user_licenses_endpoint = add_query_arg( $foreign_licenses, $user_licenses_endpoint );
19766
- }
19767
-
19768
- $result = $api->get( $user_licenses_endpoint, true );
19769
-
19770
- $is_site_license_synced = false;
19771
-
19772
- $api_errors = array();
19773
-
19774
- if ( $this->is_api_result_object( $result, 'licenses' ) &&
19775
- is_array( $result->licenses )
19776
- ) {
19777
- for ( $i = 0, $len = count( $result->licenses ); $i < $len; $i ++ ) {
19778
- $result->licenses[ $i ] = new FS_Plugin_License( $result->licenses[ $i ] );
19779
-
19780
- if ( ( ! $is_site_license_synced ) && is_numeric( $site_license_id ) ) {
19781
- $is_site_license_synced = ( $site_license_id == $result->licenses[ $i ]->id );
19782
- }
19783
- }
19784
-
19785
- $result = $result->licenses;
19786
- } else {
19787
- $api_errors[] = $result;
19788
- $result = array();
19789
- }
19790
-
19791
- if ( ! $is_site_license_synced ) {
19792
- if ( ! is_null( $blog_id ) ) {
19793
- /**
19794
- * If blog ID is not null, the request is for syncing of the license of a single site via the
19795
- * network-level "Account" page.
19796
- *
19797
- * @author Leo Fajardo (@leorw)
19798
- */
19799
- $this->switch_to_blog( $blog_id );
19800
- }
19801
-
19802
- $api = $this->get_api_site_scope();
19803
-
19804
- if ( is_numeric( $site_license_id ) ) {
19805
- // Try to retrieve a foreign license that is linked to the install.
19806
- $api_result = $api->call( '/licenses.json?is_enriched=true' );
19807
-
19808
- if ( $this->is_api_result_object( $api_result, 'licenses' ) &&
19809
- is_array( $api_result->licenses )
19810
- ) {
19811
- $licenses = $api_result->licenses;
19812
-
19813
- if ( ! empty( $licenses ) ) {
19814
- $result[] = new FS_Plugin_License( $licenses[0] );
19815
- }
19816
- } else {
19817
- $api_errors[] = $api_result;
19818
- }
19819
- } else if (
19820
- is_object( $this->_license ) &&
19821
- /**
19822
- * Sync only if the license belongs to the context plugin. `$plugin_id` can be an add-on ID while
19823
- * the FS instance that does the syncing is the parent FS instance.
19824
- *
19825
- * @author Leo Fajardo (@leorw)
19826
- * @since 2.3.0
19827
- */
19828
- $this->_license->plugin_id == $plugin_id
19829
- ) {
19830
- $is_license_in_result = false;
19831
- if ( ! empty( $result ) ) {
19832
- foreach ( $result as $license ) {
19833
- if ( $license->id == $this->_license->id ) {
19834
- $is_license_in_result = true;
19835
- break;
19836
- }
19837
- }
19838
- }
19839
-
19840
- if ( ! $is_license_in_result ) {
19841
- // Fetch foreign license by ID and license key.
19842
- $license = $api->get( "/licenses/{$this->_license->id}.json?license_key=" .
19843
- urlencode( $this->_license->secret_key ) . '&is_enriched=true' );
19844
-
19845
- if ( $this->is_api_result_entity( $license ) ) {
19846
- $result[] = new FS_Plugin_License( $license );
19847
- } else {
19848
- $api_errors[] = $license;
19849
- }
19850
- }
19851
- }
19852
-
19853
- if ( ! is_null( $blog_id ) ) {
19854
- $this->switch_to_blog( $this->_storage->network_install_blog_id );
19855
- }
19856
- }
19857
-
19858
- if ( is_array( $result ) && 0 < count( $result ) ) {
19859
- // If found at least one license, return license collection even if there are errors.
19860
- return $result;
19861
- }
19862
-
19863
- if ( ! empty( $api_errors ) ) {
19864
- // If found any errors and no licenses, return first error.
19865
- return $api_errors[0];
19866
- }
19867
-
19868
- // Fallback to empty licenses list.
19869
- return $result;
19870
- }
19871
-
19872
- /**
19873
- * @author Vova Feldman (@svovaf)
19874
- * @since 2.0.0
19875
- *
19876
- * @param number $license_id
19877
- * @param string $license_key
19878
- *
19879
- * @return \FS_Plugin_License|object
19880
- */
19881
- private function fetch_license_by_key( $license_id, $license_key ) {
19882
- $this->_logger->entrance();
19883
-
19884
- $api = $this->get_current_or_network_user_api_scope();
19885
-
19886
- $result = $api->get( "/licenses/{$license_id}.json?license_key=" . urlencode( $license_key ) );
19887
-
19888
- return $this->is_api_result_entity( $result ) ?
19889
- new FS_Plugin_License( $result ) :
19890
- $result;
19891
- }
19892
-
19893
- /**
19894
- * @author Vova Feldman (@svovaf)
19895
- * @since 1.2.0
19896
- * @uses FS_Api
19897
- *
19898
- * @param number|bool $plugin_id
19899
- * @param bool $flush
19900
- *
19901
- * @return FS_Payment[]|object
19902
- */
19903
- function _fetch_payments( $plugin_id = false, $flush = false ) {
19904
- $this->_logger->entrance();
19905
-
19906
- $api = $this->get_api_user_scope();
19907
-
19908
- if ( ! is_numeric( $plugin_id ) ) {
19909
- $plugin_id = $this->_plugin->id;
19910
- }
19911
-
19912
- $include_bundles = (
19913
- is_object( $this->_plugin ) &&
19914
- FS_Plugin::is_valid_id( $this->_plugin->bundle_id )
19915
- );
19916
-
19917
- $result = $api->get(
19918
- "/plugins/{$plugin_id}/payments.json?include_addons=true" . ($include_bundles ? '&include_bundles=true' : ''),
19919
- $flush
19920
- );
19921
-
19922
- if ( ! isset( $result->error ) ) {
19923
- for ( $i = 0, $len = count( $result->payments ); $i < $len; $i ++ ) {
19924
- $result->payments[ $i ] = new FS_Payment( $result->payments[ $i ] );
19925
- }
19926
- $result = $result->payments;
19927
- }
19928
-
19929
- return $result;
19930
- }
19931
-
19932
- /**
19933
- * @author Vova Feldman (@svovaf)
19934
- * @since 1.2.1.5
19935
- * @uses FS_Api
19936
- *
19937
- * @param bool $flush
19938
- *
19939
- * @return \FS_Billing|mixed
19940
- */
19941
- function _fetch_billing( $flush = false ) {
19942
- require_once WP_FS__DIR_INCLUDES . '/entities/class-fs-billing.php';
19943
-
19944
- $billing = $this->get_api_user_scope()->get( 'billing.json', $flush );
19945
-
19946
- if ( $this->is_api_result_entity( $billing ) ) {
19947
- $billing = new FS_Billing( $billing );
19948
- }
19949
-
19950
- return $billing;
19951
- }
19952
-
19953
- /**
19954
- * @author Vova Feldman (@svovaf)
19955
- * @since 1.0.5
19956
- *
19957
- * @param FS_Plugin_License[] $licenses
19958
- * @param number $module_id
19959
- */
19960
- private function _update_licenses( $licenses, $module_id ) {
19961
- $this->_logger->entrance();
19962
-
19963
- if ( is_array( $licenses ) ) {
19964
- for ( $i = 0, $len = count( $licenses ); $i < $len; $i ++ ) {
19965
- $licenses[ $i ]->updated = time();
19966
- }
19967
- }
19968
-
19969
- $this->_store_licenses( true, $module_id, $licenses );
19970
- }
19971
-
19972
- /**
19973
- * @author Vova Feldman (@svovaf)
19974
- * @since 1.0.4
19975
- *
19976
- * @param bool|number $plugin_id
19977
- * @param bool $flush Since 1.1.7.3
19978
- * @param int $expiration Since 1.2.2.7
19979
- * @param bool|string $newer_than Since 2.2.1
19980
- *
19981
- * @return object|false New plugin tag info if exist.
19982
- */
19983
- private function _fetch_newer_version( $plugin_id = false, $flush = true, $expiration = WP_FS__TIME_24_HOURS_IN_SEC, $newer_than = false ) {
19984
- $latest_tag = $this->_fetch_latest_version( $plugin_id, $flush, $expiration, $newer_than );
19985
-
19986
- if ( ! is_object( $latest_tag ) ) {
19987
- return false;
19988
- }
19989
-
19990
- $plugin_version = $this->get_plugin_version();
19991
-
19992
- // Check if version is actually newer.
19993
- $has_new_version =
19994
- // If it's an non-installed add-on then always return latest.
19995
- ( $this->_is_addon_id( $plugin_id ) && ! $this->is_addon_activated( $plugin_id ) ) ||
19996
- // Compare versions.
19997
- version_compare( $plugin_version, $latest_tag->version, '<' );
19998
-
19999
- $this->_logger->departure( $has_new_version ? 'Found newer plugin version ' . $latest_tag->version : 'No new version' );
20000
-
20001
- $is_latest_version_beta = ( 'beta' === $latest_tag->release_mode );
20002
-
20003
- $this->_storage->beta_data = array(
20004
- 'is_beta' => $is_latest_version_beta,
20005
- 'version' => $latest_tag->version
20006
- );
20007
-
20008
- return $has_new_version ? $latest_tag : false;
20009
- }
20010
-
20011
- /**
20012
- * @author Vova Feldman (@svovaf)
20013
- * @since 1.0.5
20014
- *
20015
- * @param bool|number $plugin_id
20016
- * @param bool $flush Since 1.1.7.3
20017
- * @param int $expiration Since 1.2.2.7
20018
- * @param bool|string $newer_than Since 2.2.1
20019
- *
20020
- * @return bool|FS_Plugin_Tag
20021
- */
20022
- function get_update( $plugin_id = false, $flush = true, $expiration = WP_FS__TIME_24_HOURS_IN_SEC, $newer_than = false ) {
20023
- $this->_logger->entrance();
20024
-
20025
- if ( ! is_numeric( $plugin_id ) ) {
20026
- $plugin_id = $this->_plugin->id;
20027
- }
20028
-
20029
- $this->check_updates( true, $plugin_id, $flush, $expiration, $newer_than );
20030
- $updates = $this->get_all_updates();
20031
-
20032
- return isset( $updates[ $plugin_id ] ) && is_object( $updates[ $plugin_id ] ) ? $updates[ $plugin_id ] : false;
20033
- }
20034
-
20035
- /**
20036
- * Check if site assigned with active license.
20037
- *
20038
- * @author Vova Feldman (@svovaf)
20039
- * @since 1.0.6
20040
- *
20041
- * @deprecated Please use has_active_valid_license() instead because license can be cancelled.
20042
- */
20043
- function has_active_license() {
20044
- return (
20045
- is_object( $this->_license ) &&
20046
- is_numeric( $this->_license->id ) &&
20047
- ! $this->_license->is_expired()
20048
- );
20049
- }
20050
-
20051
- /**
20052
- * Check if site assigned with active & valid (not expired) license.
20053
- *
20054
- * @author Vova Feldman (@svovaf)
20055
- * @since 1.2.1
20056
- *
20057
- * @param bool $check_expiration
20058
- */
20059
- function has_active_valid_license( $check_expiration = true ) {
20060
- return self::is_active_valid_license( $this->_license, $check_expiration );
20061
- }
20062
-
20063
- /**
20064
- * @author Leo Fajardo (@leorw)
20065
- * @since 2.3.1
20066
- */
20067
- function is_data_debug_mode() {
20068
- if ( is_null( $this->is_whitelabeled ) || ! $this->is_whitelabeled ) {
20069
- return false;
20070
- }
20071
-
20072
- $fs = $this->is_addon() ?
20073
- $this->get_parent_instance() :
20074
- $this;
20075
-
20076
- if ( $fs->is_network_active() && fs_is_network_admin() ) {
20077
- $is_developer_license_debug_mode = get_site_transient( "fs_{$this->get_id()}_data_debug_mode" );
20078
- } else {
20079
- $is_developer_license_debug_mode = get_transient( "fs_{$this->get_id()}_data_debug_mode" );
20080
- }
20081
-
20082
- return ( 'true' === $is_developer_license_debug_mode );
20083
- }
20084
-
20085
- /**
20086
- * @author Leo Fajardo (@leorw)
20087
- * @since 2.3.1
20088
- */
20089
- function _set_data_debug_mode() {
20090
- if ( ! $this->is_whitelabeled( true ) ) {
20091
- return;
20092
- }
20093
-
20094
- $license_or_user_key = fs_request_get( 'license_or_user_key' );
20095
-
20096
- $transient_value = ( ! empty( $license_or_user_key ) ) ?
20097
- 'true' :
20098
- 'false';
20099
-
20100
- if ( 'true' === $transient_value ) {
20101
- $stored_key = $this->_storage->get( ! FS_User::is_valid_id( $this->_storage->last_license_user_id ) ?
20102
- 'last_license_key' :
20103
- 'last_license_user_key'
20104
- );
20105
-
20106
- if ( md5( $license_or_user_key ) !== $stored_key ) {
20107
- $this->shoot_ajax_failure( sprintf(
20108
- '%s... %s',
20109
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ),
20110
- $this->get_text_inline(
20111
- 'seems like the key you entered doesn\'t match our records.',
20112
- 'developer-or-license-not-found'
20113
- )
20114
- ) );
20115
- }
20116
- }
20117
-
20118
- if ( $this->is_network_active() && fs_is_network_admin() ) {
20119
- set_site_transient(
20120
- "fs_{$this->get_id()}_data_debug_mode",
20121
- $transient_value,
20122
- WP_FS__TIME_24_HOURS_IN_SEC / 24
20123
- );
20124
- } else {
20125
- set_transient(
20126
- "fs_{$this->get_id()}_data_debug_mode",
20127
- $transient_value,
20128
- WP_FS__TIME_24_HOURS_IN_SEC / 24
20129
- );
20130
- }
20131
-
20132
- if ( 'true' === $transient_value ) {
20133
- $this->_admin_notices->add_sticky(
20134
- $this->get_text_inline(
20135
- 'Debug mode was successfully enabled and will be automatically disabled in 60 min. You can also disable it earlier by clicking the "Stop Debug" link.',
20136
- 'data_debug_mode_enabled'
20137
- ),
20138
- 'data_debug_mode_enabled'
20139
- );
20140
- }
20141
-
20142
- $this->shoot_ajax_success();
20143
- }
20144
-
20145
- /**
20146
- * Check if a given license is active & valid (not expired).
20147
- *
20148
- * @author Vova Feldman (@svovaf)
20149
- * @since 2.1.3
20150
- *
20151
- * @param FS_Plugin_License $license
20152
- * @param bool $check_expiration
20153
- *
20154
- * @return bool
20155
- */
20156
- private static function is_active_valid_license( $license, $check_expiration = true ) {
20157
- return (
20158
- is_object( $license ) &&
20159
- FS_Plugin_License::is_valid_id( $license->id ) &&
20160
- $license->is_active() &&
20161
- ( ! $check_expiration || $license->is_valid() )
20162
- );
20163
- }
20164
-
20165
- /**
20166
- * Checks if there's any site that is associated with an active & valid license.
20167
- * This logic is used to determine if the admin can download the premium code base from a network level admin.
20168
- *
20169
- * @author Vova Feldman (@svovaf)
20170
- * @since 2.1.3
20171
- *
20172
- * @return bool
20173
- */
20174
- function has_any_active_valid_license() {
20175
- if ( ! fs_is_network_admin() ) {
20176
- return $this->has_active_valid_license();
20177
- }
20178
-
20179
- $installs = $this->get_blog_install_map();
20180
- $all_plugin_licenses = self::get_all_licenses( $this->_module_id );
20181
-
20182
- foreach ( $installs as $blog_id => $install ) {
20183
- if ( ! FS_Plugin_License::is_valid_id( $install->license_id ) ) {
20184
- continue;
20185
- }
20186
-
20187
- foreach ( $all_plugin_licenses as $license ) {
20188
- if ( $license->id == $install->license_id ) {
20189
- if ( self::is_active_valid_license( $license ) ) {
20190
- return true;
20191
- }
20192
- }
20193
- }
20194
- }
20195
-
20196
- return false;
20197
- }
20198
-
20199
- /**
20200
- * Check if site assigned with license with enabled features.
20201
- *
20202
- * @author Vova Feldman (@svovaf)
20203
- * @since 1.0.6
20204
- *
20205
- * @return bool
20206
- */
20207
- function has_features_enabled_license() {
20208
- return (
20209
- is_object( $this->_license ) &&
20210
- is_numeric( $this->_license->id ) &&
20211
- $this->_license->is_features_enabled()
20212
- );
20213
- }
20214
-
20215
- /**
20216
- * Checks if the product is activated with a bundle license.
20217
- *
20218
- * @author Leo Fajardo (@leorw)
20219
- * @since 2.4.0
20220
- *
20221
- * @return bool
20222
- */
20223
- function is_activated_with_bundle_license() {
20224
- if ( ! $this->has_features_enabled_license() ) {
20225
- return false;
20226
- }
20227
-
20228
- return FS_Plugin_License::is_valid_id( $this->_license->parent_license_id );
20229
- }
20230
-
20231
- /**
20232
- * Check if user is a trial or have feature enabled license.
20233
- *
20234
- * @author Vova Feldman (@svovaf)
20235
- * @since 1.1.7
20236
- *
20237
- * @return bool
20238
- */
20239
- function can_use_premium_code() {
20240
- return $this->is_trial() || $this->has_features_enabled_license();
20241
- }
20242
-
20243
- /**
20244
- * Checks if the current user can activate plugins or switch themes. Note that this method should only be used
20245
- * after the `init` action is triggered because it is using `current_user_can()` which is only functional after
20246
- * the context user is authenticated.
20247
- *
20248
- * @author Leo Fajardo (@leorw)
20249
- * @since 1.2.2
20250
- *
20251
- * @return bool
20252
- */
20253
- function is_user_admin() {
20254
- /**
20255
- * Require a super-admin when network activated, running from the network level OR if
20256
- * running from the site level but not delegated the opt-in.
20257
- *
20258
- * @author Vova Feldman (@svovaf)
20259
- * @since 2.0.0
20260
- */
20261
- if ( $this->_is_network_active &&
20262
- ( fs_is_network_admin() || ! $this->is_delegated_connection() )
20263
- ) {
20264
- return is_super_admin();
20265
- }
20266
-
20267
- return ( $this->is_plugin() && current_user_can( is_multisite() ? 'manage_options' : 'activate_plugins' ) )
20268
- || ( $this->is_theme() && current_user_can( 'switch_themes' ) );
20269
- }
20270
-
20271
- /**
20272
- * Sync site's plan.
20273
- *
20274
- * @author Vova Feldman (@svovaf)
20275
- * @since 1.0.3
20276
- *
20277
- * @uses FS_Api
20278
- *
20279
- * @param bool $background Hints the method if it's a background sync. If false, it means that was initiated by
20280
- * the admin.
20281
- * @param bool $is_context_single_site @since 2.0.0. This is used when syncing a license for a single install from the
20282
- * network-level "Account" page.
20283
- * @param int|null $current_blog_id @since 2.2.3. This is passed from the `execute_cron` method and used by the
20284
- * `_sync_plugin_license` method in order to switch to the previous blog when sending
20285
- * updates for a single site in case `execute_cron` has switched to a different blog.
20286
- */
20287
- private function _sync_license( $background = false, $is_context_single_site = false, $current_blog_id = null ) {
20288
- $this->_logger->entrance();
20289
-
20290
- $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
20291
-
20292
- $is_addon_sync = ( ! $this->_plugin->is_addon() && $plugin_id != $this->get_id() );
20293
-
20294
- if ( $is_addon_sync ) {
20295
- $this->_sync_addon_license( $plugin_id, $background );
20296
- } else {
20297
- $this->_sync_plugin_license( $background, true, $is_context_single_site, $current_blog_id );
20298
- }
20299
-
20300
- $this->do_action( 'after_account_plan_sync', $this->get_plan_name() );
20301
- }
20302
-
20303
- /**
20304
- * Sync plugin's add-on license.
20305
- *
20306
- * @author Vova Feldman (@svovaf)
20307
- * @since 1.0.6
20308
- * @uses FS_Api
20309
- *
20310
- * @param number $addon_id
20311
- * @param bool $background
20312
- */
20313
- private function _sync_addon_license( $addon_id, $background ) {
20314
- $this->_logger->entrance();
20315
-
20316
- if ( $this->is_addon_activated( $addon_id ) ) {
20317
- // If already installed, use add-on sync.
20318
- $fs_addon = self::get_instance_by_id( $addon_id );
20319
-
20320
- if (
20321
- // Add-on is network activated and network integrated.
20322
- $fs_addon->is_network_active() ||
20323
- // Background sync cron.
20324
- self::is_cron() ||
20325
- // Add-on is not network activated or not network integrated.
20326
- ! fs_is_network_admin()
20327
- ) {
20328
- $fs_addon->_sync_license( $background );
20329
-
20330
- return;
20331
- }
20332
- }
20333
-
20334
- // Validate add-on exists.
20335
- $addon = $this->get_addon( $addon_id );
20336
-
20337
- if ( ! is_object( $addon ) ) {
20338
- return;
20339
- }
20340
-
20341
- // Add add-on into account add-ons.
20342
- $account_addons = $this->get_account_addons();
20343
- if ( ! is_array( $account_addons ) ) {
20344
- $account_addons = array();
20345
- }
20346
- $account_addons[] = $addon->id;
20347
- $account_addons = array_unique( $account_addons );
20348
- $this->_store_account_addons( $account_addons );
20349
-
20350
- // Load add-on licenses.
20351
- $licenses = $this->_fetch_licenses( $addon->id );
20352
-
20353
- // Sync add-on licenses.
20354
- if ( $this->is_array_instanceof( $licenses, 'FS_Plugin_License' ) ) {
20355
- $this->_update_licenses( $licenses, $addon->id );
20356
-
20357
- if ( ! $this->is_addon_installed( $addon->id ) && FS_License_Manager::has_premium_license( $licenses ) ) {
20358
- $plans_result = $this->get_api_site_or_plugin_scope()->get( $this->add_show_pending( "/addons/{$addon_id}/plans.json" ) );
20359
-
20360
- if ( ! isset( $plans_result->error ) ) {
20361
- $plans = array();
20362
- foreach ( $plans_result->plans as $plan ) {
20363
- $plans[] = new FS_Plugin_Plan( $plan );
20364
- }
20365
-
20366
- $this->_admin_notices->add_sticky(
20367
- sprintf(
20368
- ( FS_Plan_Manager::instance()->has_free_plan( $plans ) ?
20369
- $this->get_text_inline( 'Your %s Add-on plan was successfully upgraded.', 'addon-successfully-upgraded-message' ) :
20370
- /* translators: %s:product name, e.g. Facebook add-on was successfully... */
20371
- $this->get_text_inline( '%s Add-on was successfully purchased.', 'addon-successfully-purchased-message' ) ),
20372
- $addon->title
20373
- ) . ' ' . $this->get_latest_download_link(
20374
- $this->get_text_inline( 'Download the latest version', 'download-latest-version' ),
20375
- $addon_id
20376
- ),
20377
- 'addon_plan_upgraded_' . $addon->slug,
20378
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
20379
- );
20380
- }
20381
- }
20382
- }
20383
- }
20384
-
20385
- /**
20386
- * Sync site's plugin plan.
20387
- *
20388
- * @author Vova Feldman (@svovaf)
20389
- * @since 1.0.6
20390
- * @uses FS_Api
20391
- *
20392
- * @param bool $background Hints the method if it's a background sync. If false, it means that was initiated by the admin.
20393
- * @param bool $send_installs_update Since 2.0.0
20394
- * @param bool $is_context_single_site Since 2.0.0. This is used when sending an update for a single install and
20395
- * syncing its license from the network-level "Account" page (e.g.: after
20396
- * activating a license only for the single install).
20397
- * @param int|null $current_blog_id Since 2.2.3. This is passed from the `execute_cron` method so that it
20398
- * can be used here to switch to the previous blog in case `execute_cron`
20399
- * has switched to a different blog.
20400
- */
20401
- private function _sync_plugin_license(
20402
- $background = false,
20403
- $send_installs_update = true,
20404
- $is_context_single_site = false,
20405
- $current_blog_id = null
20406
- ) {
20407
- $this->_logger->entrance();
20408
-
20409
- $plan_change = 'none';
20410
-
20411
- $is_site_level_sync = ( $is_context_single_site || fs_is_blog_admin() || ! $this->_is_network_active );
20412
-
20413
- if ( ! $send_installs_update ) {
20414
- $site = $this->_site;
20415
- } else {
20416
- /**
20417
- * Sync site info.
20418
- *
20419
- * @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.
20420
- */
20421
- if ( $is_site_level_sync ) {
20422
- /**
20423
- * Switch to the previous blog since `execute_cron` may have switched to a different blog.
20424
- *
20425
- * @author Leo Fajardo (@leorw)
20426
- * @since 2.2.3
20427
- */
20428
- if ( is_numeric( $current_blog_id ) ) {
20429
- $this->switch_to_blog( $current_blog_id );
20430
- }
20431
-
20432
- $result = $this->send_install_update( array(), true );
20433
- $is_valid = $this->is_api_result_entity( $result );
20434
- } else {
20435
- $result = $this->send_installs_update( array(), true );
20436
- $is_valid = $this->is_api_result_object( $result, 'installs' );
20437
- }
20438
-
20439
- if ( ! $is_valid ) {
20440
- if ( $is_context_single_site ) {
20441
- // Switch back to the main blog so that the following logic will have the right entities.
20442
- $this->switch_to_blog( $this->_storage->network_install_blog_id );
20443
- }
20444
-
20445
- // Show API messages only if not background sync or if paying customer.
20446
- if ( ! $background || $this->is_paying() ) {
20447
- // Try to ping API to see if not blocked.
20448
- if ( ! FS_Api::test() ) {
20449
- /**
20450
- * Failed to ping API - blocked!
20451
- *
20452
- * @author Vova Feldman (@svovaf)
20453
- * @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.
20454
- */
20455
- $api = $this->get_api_site_scope();
20456
-
20457
- if ( ! self::$_global_admin_notices->has_sticky( 'api_blocked' ) ) {
20458
- self::$_global_admin_notices->add(
20459
- sprintf(
20460
- $this->get_text_inline( 'Your server is blocking the access to Freemius\' API, which is crucial for %1$s synchronization. Please contact your host to whitelist %2$s', 'server-blocking-access' ),
20461
- $this->get_plugin_name(),
20462
- '<b>' . implode( ', ', $this->apply_filters( 'api_domains', array(
20463
- 'api.freemius.com',
20464
- 'wp.freemius.com'
20465
- ) ) ) . '</b>'
20466
- ) . '<br> ' . $this->get_text_inline( 'Error received from the server:', 'server-error-message' ) . var_export( $result->error, true ),
20467
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
20468
- 'error',
20469
- $background,
20470
- 'api_blocked'
20471
- );
20472
- }
20473
- } else {
20474
- // Authentication params are broken.
20475
- $this->_admin_notices->add(
20476
- $this->get_text_inline( 'It seems like one of the authentication parameters is wrong. Update your Public Key, Secret Key & User ID, and try again.', 'wrong-authentication-param-message' ) . '<br> ' . $this->get_text_inline( 'Error received from the server:', 'server-error-message' ) . var_export( $result->error, true ),
20477
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
20478
- 'error'
20479
- );
20480
- }
20481
- }
20482
-
20483
- // No reason to continue with license sync while there are API issues.
20484
- return;
20485
- }
20486
-
20487
- if ( $is_site_level_sync ) {
20488
- $site = new FS_Site( $result );
20489
- } else {
20490
- // Map site addresses to their blog IDs.
20491
- $address_to_blog_map = $this->get_address_to_blog_map();
20492
-
20493
- // Find the current context install.
20494
- $site = null;
20495
- foreach ( $result->installs as $install ) {
20496
- if ( $install->id == $this->_site->id ) {
20497
- $site = new FS_Site( $install );
20498
- } else {
20499
- $address = trailingslashit( fs_strip_url_protocol( $install->url ) );
20500
- $blog_id = $address_to_blog_map[ $address ];
20501
-
20502
- $this->_store_site( true, $blog_id, new FS_Site( $install ) );
20503
- }
20504
- }
20505
- }
20506
-
20507
- // Sync plans.
20508
- $this->_sync_plans();
20509
- }
20510
-
20511
- // Remove sticky API connectivity message.
20512
- self::$_global_admin_notices->remove_sticky( 'api_blocked' );
20513
-
20514
- if ( ! $this->has_paid_plan() ) {
20515
- $this->_site = $site;
20516
- $this->_store_site(
20517
- true,
20518
- $is_site_level_sync ?
20519
- null :
20520
- $this->get_network_install_blog_id()
20521
- );
20522
- } else {
20523
- $context_blog_id = 0;
20524
-
20525
- if ( $is_context_single_site ) {
20526
- $context_blog_id = get_current_blog_id();
20527
-
20528
- // Switch back to the main blog in order to properly sync the license.
20529
- $this->switch_to_blog( $this->_storage->network_install_blog_id );
20530
- }
20531
-
20532
- /**
20533
- * Sync licenses. Pass the site's license ID so that the foreign licenses will be fetched if the license
20534
- * associated with that ID is not included in the user's licenses collection.
20535
- */
20536
- $this->_sync_licenses(
20537
- $site->license_id,
20538
- ( $is_context_single_site ?
20539
- $context_blog_id :
20540
- null
20541
- )
20542
- );
20543
-
20544
- if ( $is_context_single_site ) {
20545
- $this->switch_to_blog( $context_blog_id );
20546
- }
20547
-
20548
- // Check if plan / license changed.
20549
- if ( $site->plan_id != $this->_site->plan_id ||
20550
- // Check if trial started.
20551
- $site->trial_plan_id != $this->_site->trial_plan_id ||
20552
- $site->trial_ends != $this->_site->trial_ends ||
20553
- // Check if license changed.
20554
- $site->license_id != $this->_site->license_id
20555
- ) {
20556
- if ( $site->is_trial() && ( ! $this->_site->is_trial() || $site->trial_ends != $this->_site->trial_ends ) ) {
20557
- // New trial started.
20558
- $this->_site = $site;
20559
- $plan_change = 'trial_started';
20560
-
20561
- // For trial with subscription use-case.
20562
- $new_license = is_null( $site->license_id ) ? null : $this->_get_license_by_id( $site->license_id );
20563
-
20564
- if ( is_object( $new_license ) && $new_license->is_valid() ) {
20565
- $this->_site = $site;
20566
- $this->_update_site_license( $new_license );
20567
- $this->_store_licenses();
20568
-
20569
- $this->_sync_site_subscription( $this->_license );
20570
- }
20571
- } else if ( $this->_site->is_trial() && ! $site->is_trial() && ! is_numeric( $site->license_id ) ) {
20572
- // Was in trial, but now trial expired and no license ID.
20573
- // New trial started.
20574
- $this->_site = $site;
20575
- $plan_change = 'trial_expired';
20576
- } else {
20577
- $is_free = $this->is_free_plan();
20578
-
20579
- // Make sure license exist and not expired.
20580
- $new_license = is_null( $site->license_id ) ?
20581
- null :
20582
- $this->_get_license_by_id( $site->license_id );
20583
-
20584
- if ( $is_free && is_null( $new_license ) && $this->has_any_license() && $this->_license->is_cancelled ) {
20585
- // License cancelled.
20586
- $this->_site = $site;
20587
- $this->_update_site_license( $new_license );
20588
- $this->_store_licenses();
20589
-
20590
- $plan_change = 'cancelled';
20591
- } else if ( $is_free && ( ( ! is_object( $new_license ) || $new_license->is_expired() ) ) ) {
20592
- // The license is expired, so ignore upgrade method.
20593
- $this->_site = $site;
20594
- } else {
20595
- // License changed.
20596
- $this->_site = $site;
20597
-
20598
- /**
20599
- * IMPORTANT:
20600
- * 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.
20601
- *
20602
- * @author Vova Feldman (@svovaf)
20603
- * @since 2.0.0
20604
- */
20605
- $this->_update_site_license( $new_license );
20606
-
20607
- if ( ! $is_context_single_site &&
20608
- fs_is_network_admin() &&
20609
- $this->_is_network_active &&
20610
- $new_license->quota > 1 &&
20611
- get_blog_count() > 1
20612
- ) {
20613
- // See if license can activated on all sites.
20614
- if ( ! $this->try_activate_license_on_network( $this->_user, $new_license ) ) {
20615
- if ( ! fs_request_get_bool( 'auto_install' ) ) {
20616
- // Open the license activation dialog box on the account page.
20617
- add_action( 'admin_footer', array(
20618
- $this,
20619
- '_open_license_activation_dialog_box'
20620
- ) );
20621
- }
20622
- }
20623
- }
20624
-
20625
- $this->_store_licenses();
20626
-
20627
- $plan_change = $is_free ?
20628
- ( $this->is_only_premium() ? 'activated' : 'upgraded' ) :
20629
- ( is_object( $new_license ) ?
20630
- 'changed' :
20631
- 'downgraded' );
20632
- }
20633
- }
20634
-
20635
- // Store updated site info.
20636
- $this->_store_site(
20637
- true,
20638
- $is_site_level_sync ?
20639
- null :
20640
- $this->get_network_install_blog_id()
20641
- );
20642
- } else {
20643
- if ( ! is_object( $this->_license ) ) {
20644
- $this->maybe_update_whitelabel_flag(
20645
- FS_Plugin_License::is_valid_id( $site->license_id ) ?
20646
- $this->get_license_by_id( $site->license_id ) :
20647
- null
20648
- );
20649
- } else {
20650
- $this->maybe_update_whitelabel_flag( $this->_license );
20651
-
20652
- if ( $this->_license->is_expired() ) {
20653
- if ( ! $this->has_features_enabled_license() ) {
20654
- $this->_deactivate_license();
20655
- $plan_change = 'downgraded';
20656
- } else {
20657
- $last_time_expired_license_notice_was_shown = $this->_storage->get( 'expired_license_notice_shown', 0 );
20658
-
20659
- if ( time() - ( 14 * WP_FS__TIME_24_HOURS_IN_SEC ) >= $last_time_expired_license_notice_was_shown ) {
20660
- /**
20661
- * Show the expired license notice every 14 days.
20662
- *
20663
- * @author Leo Fajardo (@leorw)
20664
- * @since 2.3.1
20665
- */
20666
- $plan_change = 'expired';
20667
- }
20668
- }
20669
- }
20670
- }
20671
-
20672
- if ( is_numeric( $site->license_id ) && is_object( $this->_license ) ) {
20673
- $this->_sync_site_subscription( $this->_license );
20674
- }
20675
- }
20676
-
20677
- if ( ! $this->is_addon() &&
20678
- $this->_site->is_beta() !== $site->is_beta
20679
- ) {
20680
- // Beta flag updated.
20681
- $this->_site = $site;
20682
-
20683
- $this->_store_site(
20684
- true,
20685
- $is_site_level_sync ?
20686
- null :
20687
- $this->get_network_install_blog_id()
20688
- );
20689
- }
20690
-
20691
- if ( $this->is_addon() || $this->has_addons() ) {
20692
- /**
20693
- * Purge the valid user licenses cache so that when the "Account" or the "Add-Ons" page is loaded,
20694
- * an updated valid user licenses collection will be fetched from the server which is used to also
20695
- * update the account add-ons (add-ons the user has licenses for).
20696
- *
20697
- * @author Leo Fajardo (@leorw)
20698
- * @since 2.2.4
20699
- */
20700
- $this->purge_valid_user_licenses_cache();
20701
- }
20702
- }
20703
-
20704
- $hmm_text = $this->get_text_x_inline( 'Hmm', 'something somebody says when they are thinking about what you have just said.', 'hmm' ) . '...';
20705
-
20706
- if ( $this->apply_filters( 'has_paid_plan_account', $this->has_paid_plan() ) ) {
20707
- switch ( $plan_change ) {
20708
- case 'none':
20709
- if ( ! $background && is_admin() ) {
20710
- $plan = $this->is_trial() ?
20711
- $this->get_trial_plan() :
20712
- $this->get_plan();
20713
-
20714
- if ( $plan->is_free() ) {
20715
- $this->_admin_notices->add(
20716
- sprintf(
20717
- $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' ),
20718
- '<i><b>' . $plan->title . ( $this->is_trial() ? ' ' . $this->get_text_x_inline( 'Trial', 'trial period', 'trial' ) : '' ) . '</b></i>'
20719
- ) . ' ' . sprintf(
20720
- '<a href="%s">%s</a>',
20721
- $this->contact_url(
20722
- 'bug',
20723
- 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' ),
20724
- strtoupper( $plan->name )
20725
- )
20726
- ),
20727
- $this->get_text_inline( 'Please contact us here', 'contact-us-here' )
20728
- ),
20729
- $hmm_text
20730
- );
20731
- }
20732
- }
20733
- break;
20734
- case 'upgraded':
20735
- case 'activated':
20736
- $this->_admin_notices->add_sticky(
20737
- ( 'activated' === $plan_change ) ?
20738
- $this->get_text_inline( 'Your plan was successfully activated.', 'plan-activated-message' ) :
20739
- $this->get_text_inline( 'Your plan was successfully upgraded.', 'plan-upgraded-message' ) .
20740
- $this->get_complete_upgrade_instructions(),
20741
- 'plan_upgraded',
20742
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
20743
- );
20744
-
20745
- $this->_admin_notices->remove_sticky( array(
20746
- 'trial_started',
20747
- 'trial_promotion',
20748
- 'trial_expired',
20749
- 'activation_complete',
20750
- 'license_expired',
20751
- ) );
20752
- break;
20753
- case 'changed':
20754
- $this->_admin_notices->add_sticky(
20755
- sprintf(
20756
- $this->get_text_inline( 'Your plan was successfully changed to %s.', 'plan-changed-to-x-message' ),
20757
- $this->get_plan_title()
20758
- ),
20759
- 'plan_changed'
20760
- );
20761
-
20762
- $this->_admin_notices->remove_sticky( array(
20763
- 'trial_started',
20764
- 'trial_promotion',
20765
- 'trial_expired',
20766
- 'activation_complete',
20767
- ) );
20768
- break;
20769
- case 'downgraded':
20770
- $this->_admin_notices->add_sticky(
20771
- ($this->has_free_plan() ?
20772
- 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 ) :
20773
- /* translators: %1$s: product title; %2$s, %3$s: wrapping HTML anchor element; %4$s: 'plugin', 'theme', or 'add-on'. */
20774
- 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) ) ),
20775
- 'license_expired',
20776
- $hmm_text
20777
- );
20778
- $this->_admin_notices->remove_sticky( 'plan_upgraded' );
20779
- break;
20780
- case 'cancelled':
20781
- $this->_admin_notices->add(
20782
- $this->get_text_inline( 'Your license has been cancelled. If you think it\'s a mistake, please contact support.', 'license-cancelled' ) . ' ' .
20783
- sprintf(
20784
- '<a href="%s">%s</a>',
20785
- $this->contact_url( 'bug' ),
20786
- $this->get_text_inline( 'Please contact us here', 'contact-us-here' )
20787
- ),
20788
- $hmm_text,
20789
- 'error'
20790
- );
20791
- $this->_admin_notices->remove_sticky( 'plan_upgraded' );
20792
- break;
20793
- case 'expired':
20794
- $this->_admin_notices->add_sticky(
20795
- 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 ),
20796
- 'license_expired',
20797
- $hmm_text
20798
- );
20799
-
20800
- $this->_storage->expired_license_notice_shown = WP_FS__SCRIPT_START_TIME;
20801
-
20802
- $this->_admin_notices->remove_sticky( 'plan_upgraded' );
20803
- break;
20804
- case 'trial_started':
20805
- $this->_admin_notices->add_sticky(
20806
- sprintf(
20807
- $this->get_text_inline( 'Your trial has been successfully started.', 'trial-started-message' ),
20808
- '<i>' . $this->get_plugin_name() . '</i>'
20809
- ) . $this->get_complete_upgrade_instructions( $this->get_trial_plan()->title ),
20810
- 'trial_started',
20811
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
20812
- );
20813
-
20814
- $this->_admin_notices->remove_sticky( array(
20815
- 'trial_promotion',
20816
- ) );
20817
- break;
20818
- case 'trial_expired':
20819
- $this->_admin_notices->add_sticky(
20820
- ($this->has_free_plan() ?
20821
- $this->get_text_inline( 'Your free trial has expired. You can still continue using all our free features.', 'trial-expired-message' ) :
20822
- /* translators: %1$s: product title; %2$s, %3$s: wrapping HTML anchor element; %4$s: 'plugin', 'theme', or 'add-on'. */
20823
- 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))),
20824
- 'trial_expired',
20825
- $hmm_text
20826
- );
20827
- $this->_admin_notices->remove_sticky( array(
20828
- 'trial_started',
20829
- 'trial_promotion',
20830
- 'plan_upgraded',
20831
- ) );
20832
- break;
20833
- }
20834
- }
20835
-
20836
- if ( 'none' !== $plan_change ) {
20837
- if (
20838
- ! is_object( $this->_license ) ||
20839
- ! $this->_license->is_whitelabeled
20840
- ) {
20841
- $this->_admin_notices->remove_sticky( 'license_whitelabeled' );
20842
- }
20843
-
20844
- $this->do_action( 'after_license_change', $plan_change, $this->get_plan() );
20845
- }
20846
- }
20847
-
20848
- /**
20849
- * Include the required JS at the footer of the admin to trigger the license activation dialog box.
20850
- *
20851
- * @author Vova Feldman (@svovaf)
20852
- * @since 2.0.0
20853
- */
20854
- public function _open_license_activation_dialog_box() {
20855
- $vars = array( 'license_id' => $this->_site->license_id );
20856
- fs_require_once_template( 'js/open-license-activation.php', $vars );
20857
- }
20858
-
20859
- /**
20860
- * @author Vova Feldman (@svovaf)
20861
- * @since 1.0.5
20862
- *
20863
- * @param bool $background
20864
- * @param FS_Plugin_License|null $premium_license
20865
- */
20866
- protected function _activate_license( $background = false, $premium_license = null ) {
20867
- $this->_logger->entrance();
20868
-
20869
- if ( is_null( $premium_license ) ) {
20870
- $license_id = fs_request_get( 'license_id' );
20871
-
20872
- if ( is_object( $this->_site ) &&
20873
- FS_Plugin_License::is_valid_id( $license_id ) &&
20874
- $license_id == $this->_site->license_id
20875
- ) {
20876
- // License is already activated.
20877
- return;
20878
- }
20879
-
20880
- $premium_license = FS_Plugin_License::is_valid_id( $license_id ) ?
20881
- $this->_get_license_by_id( $license_id ) :
20882
- $this->_get_available_premium_license();
20883
- }
20884
-
20885
- if ( ! is_object( $premium_license ) ) {
20886
- return;
20887
- }
20888
-
20889
- if ( ! is_object( $this->_site ) ) {
20890
- // Not yet opted-in.
20891
- $user = $this->get_current_or_network_user();
20892
- if ( ! is_object( $user ) ) {
20893
- $user = self::_get_user_by_id( $premium_license->user_id );
20894
- }
20895
-
20896
- if ( is_object( $user ) ) {
20897
- $this->install_with_user( $user, $premium_license->secret_key, false, false, false );
20898
- } else {
20899
- $this->opt_in(
20900
- false,
20901
- false,
20902
- false,
20903
- $premium_license->secret_key
20904
- );
20905
-
20906
- return;
20907
- }
20908
- }
20909
-
20910
-
20911
- /**
20912
- * If the premium license is already associated with the install, just
20913
- * update the license reference (activation is not required).
20914
- *
20915
- * @since 1.1.9
20916
- */
20917
- if ( $premium_license->id == $this->_site->license_id ) {
20918
- // License is already activated.
20919
- $this->_update_site_license( $premium_license );
20920
- $this->_store_account();
20921
-
20922
- return;
20923
- }
20924
-
20925
- if ( $this->_site->user_id != $premium_license->user_id ) {
20926
- $api_request_params = array( 'license_key' => $premium_license->secret_key );
20927
- } else {
20928
- $api_request_params = array();
20929
- }
20930
-
20931
- $api = $this->get_api_site_scope();
20932
- $license = $api->call( "/licenses/{$premium_license->id}.json?is_enriched=true", 'put', $api_request_params );
20933
-
20934
- if ( ! $this->is_api_result_entity( $license ) ) {
20935
- if ( ! $background ) {
20936
- $this->_admin_notices->add( sprintf(
20937
- '%s %s',
20938
- $this->get_text_inline( 'It looks like the license could not be activated.', 'license-activation-failed-message' ),
20939
- ( is_object( $license ) && isset( $license->error ) ?
20940
- $license->error->message :
20941
- sprintf( '%s<br><code>%s</code>',
20942
- $this->get_text_inline( 'Error received from the server:', 'server-error-message' ),
20943
- var_export( $license, true )
20944
- )
20945
- )
20946
- ),
20947
- $this->get_text_x_inline( 'Hmm', 'something somebody says when they are thinking about what you have just said.', 'hmm' ) . '...',
20948
- 'error'
20949
- );
20950
- }
20951
-
20952
- return;
20953
- }
20954
-
20955
- $premium_license = new FS_Plugin_License( $license );
20956
-
20957
- // Updated site plan.
20958
- $site = $this->get_api_site_scope()->get( '/', true );
20959
- if ( $this->is_api_result_entity( $site ) ) {
20960
- $this->_site = new FS_Site( $site );
20961
- }
20962
- $this->_update_site_license( $premium_license );
20963
-
20964
- $this->_store_account();
20965
-
20966
- if ( $this->is_addon() || $this->has_addons() ) {
20967
- /**
20968
- * Purge the valid user licenses cache so that when the "Account" or the "Add-Ons" page is loaded,
20969
- * an updated valid user licenses collection will be fetched from the server which is used to also
20970
- * update the account add-ons (add-ons the user has licenses for).
20971
- *
20972
- * @author Leo Fajardo (@leorw)
20973
- * @since 2.2.4
20974
- */
20975
- $this->purge_valid_user_licenses_cache();
20976
- }
20977
-
20978
- if ( ! $background ) {
20979
- $this->_admin_notices->add_sticky(
20980
- $this->get_text_inline( 'Your license was successfully activated.', 'license-activated-message' ) .
20981
- $this->get_complete_upgrade_instructions(),
20982
- 'license_activated',
20983
- $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
20984
- );
20985
- }
20986
-
20987
- $this->_admin_notices->remove_sticky( array(
20988
- 'trial_promotion',
20989
- 'license_expired',
20990
- ) );
20991
- }
20992
-
20993
- /**
20994
- * @author Vova Feldman (@svovaf)
20995
- * @since 1.0.5
20996
- *
20997
- * @param bool $show_notice
20998
- */
20999
- protected function _deactivate_license( $show_notice = true ) {
21000
- $this->_logger->entrance();
21001
-
21002
- $hmm_text = $this->get_text_x_inline( 'Hmm', 'something somebody says when they are thinking about what you have just said.', 'hmm' ) . '...';
21003
-
21004
- if ( ! FS_Plugin_License::is_valid_id( $this->_site->license_id ) ) {
21005
- $this->_admin_notices->add(
21006
- 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() ),
21007
- $hmm_text
21008
- );
21009
-
21010
- return;
21011
- }
21012
-
21013
- $api = $this->get_api_site_scope();
21014
- $license = $api->call( "/licenses/{$this->_site->license_id}.json", 'delete' );
21015
-
21016
- $this->handle_license_deactivation_result( $license, $hmm_text, $show_notice );
21017
- }
21018
-
21019
- /**
21020
- * @author Leo Fajardo (@leorw)
21021
- * @since 2.2.1
21022
- *
21023
- * @param FS_Plugin_License $license
21024
- * @param bool|string $hmm_text
21025
- * @param bool $show_notice
21026
- */
21027
- private function handle_license_deactivation_result( $license, $hmm_text = false, $show_notice = true ) {
21028
- if ( isset( $license->error ) ) {
21029
- $this->_admin_notices->add(
21030
- $this->get_text_inline( 'It looks like the license deactivation failed.', 'license-deactivation-failed-message' ) . '<br> ' .
21031
- $this->get_text_inline( 'Error received from the server:', 'server-error-message' ) . ' ' . var_export( $license->error, true ),
21032
- $hmm_text,
21033
- 'error'
21034
- );
21035
-
21036
- return;
21037
- }
21038
-
21039
- // Update license cache.
21040
- if ( is_array( $this->_licenses ) ) {
21041
- for ( $i = 0, $len = count( $this->_licenses ); $i < $len; $i ++ ) {
21042
- if ( $license->id == $this->_licenses[ $i ]->id ) {
21043
- $this->_licenses[ $i ] = new FS_Plugin_License( $license );
21044
- }
21045
- }
21046
- }
21047
-
21048
- // Update site plan to default.
21049
- $this->_sync_plans();
21050
- $this->_site->plan_id = $this->_plans[0]->id;
21051
- // Unlink license from site.
21052
- $this->_update_site_license( null );
21053
-
21054
- $this->_store_account();
21055
-
21056
- if ( $show_notice ) {
21057
- $this->_admin_notices->add(
21058
- sprintf( $this->is_only_premium() ?
21059
- $this->get_text_inline( 'Your %s license was successfully deactivated.', 'license-deactivation-message_premium-only' ) :
21060
- $this->get_text_inline( 'Your license was successfully deactivated, you are back to the %s plan.', 'license-deactivation-message' ),
21061
- $this->get_plan_title()
21062
- ),
21063
- $this->get_text_inline( 'O.K', 'ok' )
21064
- );
21065
- }
21066
-
21067
- $this->_admin_notices->remove_sticky( array(
21068
- 'plan_upgraded',
21069
- 'license_activated',
21070
- ) );
21071
- }
21072
-
21073
- /**
21074
- * Site plan downgrade.
21075
- *
21076
- * @author Vova Feldman (@svovaf)
21077
- * @since 1.0.4
21078
- *
21079
- * @return object
21080
- *
21081
- * @uses FS_Api
21082
- */
21083
- private function _downgrade_site() {
21084
- $this->_logger->entrance();
21085
-
21086
- $deactivate_license = fs_request_get_bool( 'deactivate_license' );
21087
-
21088
- $api = $this->get_api_site_scope();
21089
- $site = $api->call( 'downgrade.json', 'put', array( 'deactivate_license' => $deactivate_license ) );
21090
-
21091
- $plan_downgraded = false;
21092
- $plan = false;
21093
- if ( $this->is_api_result_entity( $site ) ) {
21094
- $prev_plan_id = $this->_site->plan_id;
21095
-
21096
- // Update new site plan id.
21097
- $this->_site->plan_id = $site->plan_id;
21098
-
21099
- $plan = $this->get_plan();
21100
- $subscription = $this->_sync_site_subscription( $this->_license );
21101
-
21102
- // Plan downgraded if plan was changed or subscription was cancelled.
21103
- $plan_downgraded = ( $plan instanceof FS_Plugin_Plan && $prev_plan_id != $plan->id ) ||
21104
- ( is_object( $subscription ) && ! isset( $subscription->error ) && ! $subscription->is_active() );
21105
- } else {
21106
- // handle different error cases.
21107
- $this->handle_license_deactivation_result(
21108
- $site,
21109
- $this->get_text_x_inline( 'Hmm', 'something somebody says when they are thinking about what you have just said.', 'hmm' ) . '...'
21110
- );
21111
- }
21112
-
21113
- if ( ! $plan_downgraded ) {
21114
- return (object) array(
21115
- 'error' => (object) array(
21116
- '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' )
21117
- )
21118
- );
21119
- }
21120
-
21121
- // Remove previous sticky message about upgrade (if exist).
21122
- $this->_admin_notices->remove_sticky( 'plan_upgraded' );
21123
-
21124
- $this->_admin_notices->add(
21125
- sprintf( $this->get_text_inline( 'Your subscription was successfully cancelled. Your %s plan license will expire in %s.', 'plan-x-downgraded-message' ),
21126
- $plan->title,
21127
- human_time_diff( time(), strtotime( $this->_license->expiration ) )
21128
- )
21129
- );
21130
-
21131
- // Store site updates.
21132
- $this->_store_site();
21133
-
21134
- if ( $deactivate_license &&
21135
- ! FS_Plugin_License::is_valid_id( $site->license_id )
21136
- ) {
21137
- if ( $this->_site->is_localhost() ) {
21138
- $this->_license->activated_local = max( 0, $this->_license->activated_local - 1 );
21139
- } else {
21140
- $this->_license->activated = max( 0, $this->_license->activated - 1 );
21141
- }
21142
-
21143
- // Handle successful license deactivation result.
21144
- $this->handle_license_deactivation_result( $this->_license );
21145
- }
21146
-
21147
- return $site;
21148
- }
21149
-
21150
- /**
21151
- * @author Vova Feldman (@svovaf)
21152
- * @since 1.1.8.1
21153
- *
21154
- * @param bool|string $plan_name
21155
- *
21156
- * @return bool If trial was successfully started.
21157
- */
21158
- function start_trial( $plan_name = false ) {
21159
- $this->_logger->entrance();
21160
-
21161
- // Alias.
21162
- $oops_text = $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...';
21163
-
21164
- if ( $this->is_trial() ) {
21165
- // Already in trial mode.
21166
- $this->_admin_notices->add(
21167
- sprintf( $this->get_text_inline( 'You are already running the %s in a trial mode.', 'in-trial-mode' ), $this->_module_type ),
21168
- $oops_text,
21169
- 'error'
21170
- );
21171
-
21172
- return false;
21173
- }
21174
-
21175
- if ( $this->_site->is_trial_utilized() ) {
21176
- // Trial was already utilized.
21177
- $this->_admin_notices->add(
21178
- $this->get_text_inline( 'You already utilized a trial before.', 'trial-utilized' ),
21179
- $oops_text,
21180
- 'error'
21181
- );
21182
-
21183
- return false;
21184
- }
21185
-
21186
- if ( false !== $plan_name ) {
21187
- $plan = $this->get_plan_by_name( $plan_name );
21188
-
21189
- if ( false === $plan ) {
21190
- // Plan doesn't exist.
21191
- $this->_admin_notices->add(
21192
- sprintf( $this->get_text_inline( 'Plan %s do not exist, therefore, can\'t start a trial.', 'trial-plan-x-not-exist' ), $plan_name ),
21193
- $oops_text,
21194
- 'error'
21195
- );
21196
-
21197
- return false;
21198
- }
21199
-
21200
- if ( ! $plan->has_trial() ) {
21201
- // Plan doesn't exist.
21202
- $this->_admin_notices->add(
21203
- sprintf( $this->get_text_inline( 'Plan %s does not support a trial period.', 'plan-x-no-trial' ), $plan_name ),
21204
- $oops_text,
21205
- 'error'
21206
- );
21207
-
21208
- return false;
21209
- }
21210
- } else {
21211
- if ( ! $this->has_trial_plan() ) {
21212
- // None of the plans have a trial.
21213
- $this->_admin_notices->add(
21214
- sprintf( $this->get_text_inline( 'None of the %s\'s plans supports a trial period.', 'no-trials' ), $this->_module_type ),
21215
- $oops_text,
21216
- 'error'
21217
- );
21218
-
21219
- return false;
21220
- }
21221
-
21222
- $plans_with_trial = FS_Plan_Manager::instance()->get_trial_plans( $this->_plans );
21223
-
21224
- $plan = $plans_with_trial[0];
21225
- }
21226
-
21227
- $api = $this->get_api_site_scope();
21228
- $plan = $api->call( "plans/{$plan->id}/trials.json", 'post' );
21229
-
21230
- if ( ! $this->is_api_result_entity( $plan ) ) {
21231
- // Some API error while trying to start the trial.
21232
- $this->_admin_notices->add(
21233
- sprintf( $this->get_text_inline( 'Unexpected API error. Please contact the %s\'s author with the following error.', 'unexpected-api-error' ), $this->_module_type )
21234
- . ' ' . var_export( $plan, true ),
21235
- $oops_text,
21236
- 'error'
21237
- );
21238
-
21239
- return false;
21240
- }
21241
-
21242
- // Sync license.
21243
- $this->_sync_license();
21244
-
21245
- return $this->is_trial();
21246
- }
21247
-
21248
- /**
21249
- * Cancel site trial.
21250
- *
21251
- * @author Vova Feldman (@svovaf)
21252
- * @since 1.0.9
21253
- *
21254
- * @return object
21255
- *
21256
- * @uses FS_Api
21257
- */
21258
- private function _cancel_trial() {
21259
- $this->_logger->entrance();
21260
-
21261
- if ( ! $this->is_trial() ) {
21262
- return (object) array(
21263
- 'error' => (object) array(
21264
- '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' )
21265
- )
21266
- );
21267
- }
21268
-
21269
- $trial_plan = $this->get_trial_plan();
21270
-
21271
- $api = $this->get_api_site_scope();
21272
- $site = $api->call( 'trials.json', 'delete' );
21273
-
21274
- $trial_cancelled = false;
21275
-
21276
- if ( $this->is_api_result_entity( $site ) ) {
21277
- $prev_trial_ends = $this->_site->trial_ends;
21278
-
21279
- if ( $this->is_paid_trial() ) {
21280
- $this->_license->expiration = $site->trial_ends;
21281
- $this->_license->is_cancelled = true;
21282
- $this->_update_site_license( $this->_license );
21283
- $this->_store_licenses();
21284
-
21285
- // Clear subscription reference.
21286
- $this->_sync_site_subscription( null );
21287
- }
21288
-
21289
- // Update site info.
21290
- $this->_site = new FS_Site( $site );
21291
-
21292
- $trial_cancelled = ( $prev_trial_ends != $site->trial_ends );
21293
- } else {
21294
- // @todo handle different error cases.
21295
- }
21296
-
21297
- if ( ! $trial_cancelled ) {
21298
- return (object) array(
21299
- 'error' => (object) array(
21300
- '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' )
21301
- )
21302
- );
21303
- }
21304
-
21305
- // Remove previous sticky messages about upgrade or trial (if exist).
21306
- $this->_admin_notices->remove_sticky( array(
21307
- 'trial_started',
21308
- 'trial_promotion',
21309
- 'plan_upgraded',
21310
- ) );
21311
-
21312
- // Store site updates.
21313
- $this->_store_site();
21314
-
21315
- if ( ! $this->is_addon() ||
21316
- ! $this->deactivate_premium_only_addon_without_license( true )
21317
- ) {
21318
- $this->_admin_notices->add(
21319
- sprintf( $this->get_text_inline( 'Your %s free trial was successfully cancelled.', 'trial-cancel-message' ), $trial_plan->title )
21320
- );
21321
- }
21322
-
21323
- return $site;
21324
- }
21325
-
21326
- /**
21327
- * @author Vova Feldman (@svovaf)
21328
- * @since 1.0.6
21329
- *
21330
- * @param bool|number $plugin_id
21331
- *
21332
- * @return bool
21333
- */
21334
- private function _is_addon_id( $plugin_id ) {
21335
- return is_numeric( $plugin_id ) && ( $this->get_id() != $plugin_id );
21336
- }
21337
-
21338
- /**
21339
- * Check if user eligible to download premium version updates.
21340
- *
21341
- * @author Vova Feldman (@svovaf)
21342
- * @since 1.0.6
21343
- *
21344
- * @return bool
21345
- */
21346
- private function _can_download_premium() {
21347
- return $this->has_any_active_valid_license() ||
21348
- ( $this->is_trial() && ! $this->get_trial_plan()->is_free() );
21349
- }
21350
-
21351
- /**
21352
- *
21353
- * @author Vova Feldman (@svovaf)
21354
- * @since 1.0.6
21355
- *
21356
- * @param bool|number $addon_id
21357
- * @param string $type "json" or "zip"
21358
- *
21359
- * @return string
21360
- */
21361
- private function _get_latest_version_endpoint( $addon_id = false, $type = 'json' ) {
21362
-
21363
- $is_addon = $this->_is_addon_id( $addon_id );
21364
-
21365
- $is_premium = null;
21366
- if ( ! $is_addon ) {
21367
- $is_premium = ( $this->is_premium() || $this->_can_download_premium() );
21368
- } else if ( $this->is_addon_activated( $addon_id ) ) {
21369
- $fs_addon = self::get_instance_by_id( $addon_id );
21370
- $is_premium = ( $fs_addon->is_premium() || $fs_addon->_can_download_premium() );
21371
- }
21372
-
21373
- // If add-on, then append add-on ID.
21374
- $endpoint = ( $is_addon ? "/addons/$addon_id" : '' ) .
21375
- '/updates/latest.' . $type;
21376
-
21377
- // If add-on and not yet activated, try to fetch based on server licensing.
21378
- if ( is_bool( $is_premium ) ) {
21379
- $endpoint = add_query_arg( 'is_premium', json_encode( $is_premium ), $endpoint );
21380
- }
21381
-
21382
- if ( $this->has_secret_key() ) {
21383
- $endpoint = add_query_arg( 'type', 'all', $endpoint );
21384
- } else if ( is_object( $this->_site ) && $this->_site->is_beta() ) {
21385
- $endpoint = add_query_arg( 'type', 'beta', $endpoint );
21386
- }
21387
-
21388
- return $endpoint;
21389
- }
21390
-
21391
- /**
21392
- * @author Vova Feldman (@svovaf)
21393
- * @since 1.0.4
21394
- *
21395
- * @param bool|number $addon_id
21396
- * @param bool $flush Since 1.1.7.3
21397
- * @param int $expiration Since 1.2.2.7
21398
- * @param bool|string $newer_than Since 2.2.1
21399
- * @param bool|string $fetch_readme Since 2.2.1
21400
- *
21401
- * @return object|false Plugin latest tag info.
21402
- */
21403
- function _fetch_latest_version(
21404
- $addon_id = false,
21405
- $flush = true,
21406
- $expiration = WP_FS__TIME_24_HOURS_IN_SEC,
21407
- $newer_than = false,
21408
- $fetch_readme = true
21409
- ) {
21410
- $this->_logger->entrance();
21411
-
21412
- $switch_to_blog_id = null;
21413
-
21414
- /**
21415
- * @since 1.1.7.3 Check for plugin updates from Freemius only if opted-in.
21416
- * @since 1.1.7.4 Also check updates for add-ons.
21417
- */
21418
- if ( ! $this->is_registered() &&
21419
- ! $this->_is_addon_id( $addon_id )
21420
- ) {
21421
- if ( ! is_multisite() ) {
21422
- return false;
21423
- }
21424
-
21425
- $installs_map = $this->get_blog_install_map();
21426
-
21427
- foreach ( $installs_map as $blog_id => $install ) {
21428
- /**
21429
- * @var FS_Site $install
21430
- */
21431
- if ( $install->is_trial() ) {
21432
- $switch_to_blog_id = $blog_id;
21433
- break;
21434
- }
21435
-
21436
- if ( FS_Plugin_License::is_valid_id( $install->license_id ) ) {
21437
- $license = $this->get_license_by_id( $install->license_id );
21438
-
21439
- if ( is_object( $license ) && $license->is_features_enabled() ) {
21440
- $switch_to_blog_id = $blog_id;
21441
- break;
21442
- }
21443
- }
21444
- }
21445
-
21446
- if ( is_null( $switch_to_blog_id ) ) {
21447
- return false;
21448
- }
21449
- }
21450
-
21451
- $current_blog_id = is_numeric( $switch_to_blog_id ) ?
21452
- get_current_blog_id() :
21453
- 0;
21454
-
21455
- if ( is_numeric( $switch_to_blog_id ) ) {
21456
- $this->switch_to_blog( $switch_to_blog_id );
21457
- }
21458
-
21459
- $latest_version_endpoint = $this->_get_latest_version_endpoint( $addon_id, 'json' );
21460
-
21461
- if ( ! empty( $newer_than ) ) {
21462
- $latest_version_endpoint = add_query_arg( 'newer_than', $newer_than, $latest_version_endpoint );
21463
- }
21464
-
21465
- if ( true === $fetch_readme ) {
21466
- $latest_version_endpoint = add_query_arg( 'readme', 'true', $latest_version_endpoint );
21467
- }
21468
-
21469
- $tag = $this->get_api_site_or_plugin_scope()->get(
21470
- $latest_version_endpoint,
21471
- $flush,
21472
- $expiration
21473
- );
21474
-
21475
- if ( is_numeric( $switch_to_blog_id ) ) {
21476
- $this->switch_to_blog( $current_blog_id );
21477
- }
21478
-
21479
- $latest_version = ( is_object( $tag ) && isset( $tag->version ) ) ? $tag->version : 'couldn\'t get';
21480
-
21481
- $this->_logger->departure( 'Latest version ' . $latest_version );
21482
-
21483
- return ( is_object( $tag ) && isset( $tag->version ) ) ? $tag : false;
21484
- }
21485
-
21486
- #----------------------------------------------------------------------------------
21487
- #region Download Plugin
21488
- #----------------------------------------------------------------------------------
21489
-
21490
- /**
21491
- * Download latest plugin version, based on plan.
21492
- *
21493
- * Not like _download_latest(), this will redirect the page
21494
- * to secure download url to prevent dual download (from FS to WP server,
21495
- * and then from WP server to the client / browser).
21496
- *
21497
- * @author Vova Feldman (@svovaf)
21498
- * @since 1.0.9
21499
- *
21500
- * @param bool|number $plugin_id
21501
- *
21502
- * @uses FS_Api
21503
- * @uses wp_redirect()
21504
- */
21505
- private function download_latest_directly( $plugin_id = false ) {
21506
- $this->_logger->entrance();
21507
-
21508
- wp_redirect( $this->get_latest_download_api_url( $plugin_id ) );
21509
- }
21510
-
21511
- /**
21512
- * Get latest plugin FS API download URL.
21513
- *
21514
- * @author Vova Feldman (@svovaf)
21515
- * @since 1.0.9
21516
- *
21517
- * @param bool|number $plugin_id
21518
- *
21519
- * @return string
21520
- */
21521
- private function get_latest_download_api_url( $plugin_id = false ) {
21522
- $this->_logger->entrance();
21523
-
21524
- return $this->get_api_site_scope()->get_signed_url(
21525
- $this->_get_latest_version_endpoint( $plugin_id, 'zip' )
21526
- );
21527
- }
21528
-
21529
- /**
21530
- * Get payment invoice URL.
21531
- *
21532
- * @author Vova Feldman (@svovaf)
21533
- * @since 1.2.0
21534
- *
21535
- * @param bool|number $payment_id
21536
- *
21537
- * @return string
21538
- */
21539
- function _get_invoice_api_url( $payment_id = false ) {
21540
- $this->_logger->entrance();
21541
-
21542
- $url = $this->get_api_user_scope()->get_signed_url(
21543
- "/payments/{$payment_id}/invoice.pdf"
21544
- );
21545
-
21546
- if ( ! fs_starts_with( $url, 'https://' ) ) {
21547
- // Always use HTTPS for invoices.
21548
- $url = 'https' . substr( $url, 4 );
21549
- }
21550
-
21551
- return $url;
21552
- }
21553
-
21554
- /**
21555
- * Get latest plugin download link.
21556
- *
21557
- * @author Vova Feldman (@svovaf)
21558
- * @since 1.0.9
21559
- *
21560
- * @param string $label
21561
- * @param bool|number $plugin_id
21562
- *
21563
- * @return string
21564
- */
21565
- private function get_latest_download_link( $label, $plugin_id = false ) {
21566
- return sprintf(
21567
- '<a target="_blank" rel="noopener" href="%s">%s</a>',
21568
- $this->_get_latest_download_local_url( $plugin_id ),
21569
- $label
21570
- );
21571
- }
21572
-
21573
- /**
21574
- * Get latest plugin download local URL.
21575
- *
21576
- * @author Vova Feldman (@svovaf)
21577
- * @since 1.0.9
21578
- *
21579
- * @param bool|number $plugin_id
21580
- *
21581
- * @return string
21582
- */
21583
- function _get_latest_download_local_url( $plugin_id = false ) {
21584
- // Add timestamp to protect from caching.
21585
- $params = array( 'ts' => WP_FS__SCRIPT_START_TIME );
21586
-
21587
- if ( ! empty( $plugin_id ) ) {
21588
- $params['plugin_id'] = $plugin_id;
21589
- } else if ( $this->is_addon() ) {
21590
- $params['plugin_id'] = $this->get_id();
21591
- }
21592
-
21593
- $fs = $this->is_addon() ?
21594
- $this->get_parent_instance() :
21595
- $this;
21596
-
21597
- return $this->apply_filters( 'download_latest_url', $fs->get_account_url( 'download_latest', $params ) );
21598
- }
21599
-
21600
- #endregion Download Plugin ------------------------------------------------------------------
21601
-
21602
- /**
21603
- * @author Vova Feldman (@svovaf)
21604
- * @since 1.0.4
21605
- *
21606
- * @uses FS_Api
21607
- *
21608
- * @param bool $background Hints the method if it's a background updates check. If false, it means that
21609
- * was initiated by the admin.
21610
- * @param bool|number $plugin_id
21611
- * @param bool $flush Since 1.1.7.3
21612
- * @param int $expiration Since 1.2.2.7
21613
- * @param bool|string $newer_than Since 2.2.1
21614
- */
21615
- private function check_updates(
21616
- $background = false,
21617
- $plugin_id = false,
21618
- $flush = true,
21619
- $expiration = WP_FS__TIME_24_HOURS_IN_SEC,
21620
- $newer_than = false
21621
- ) {
21622
- $this->_logger->entrance();
21623
-
21624
- // Check if there's a newer version for download.
21625
- $new_version = $this->_fetch_newer_version( $plugin_id, $flush, $expiration, $newer_than );
21626
-
21627
- $update = null;
21628
- if ( is_object( $new_version ) ) {
21629
- $update = new FS_Plugin_Tag( $new_version );
21630
-
21631
- if ( ! $background ) {
21632
- $this->_admin_notices->add(
21633
- sprintf(
21634
- /* translators: %s: Numeric version number (e.g. '2.1.9' */
21635
- $this->get_text_inline( 'Version %s was released.', 'version-x-released' ) . ' ' . $this->get_text_inline( 'Please download %s.', 'please-download-x' ),
21636
- $update->version,
21637
- sprintf(
21638
- '<a href="%s" target="_blank" rel="noopener">%s</a>',
21639
- $this->get_account_url( 'download_latest' ),
21640
- sprintf(
21641
- /* translators: %s: plan name (e.g. latest "Professional" version) */
21642
- $this->get_text_inline( 'the latest %s version here', 'latest-x-version' ),
21643
- $this->get_plan_title()
21644
- )
21645
- )
21646
- ),
21647
- $this->get_text_inline( 'New', 'new' ) . '!'
21648
- );
21649
- }
21650
- } else if ( false === $new_version && ! $background ) {
21651
- $this->_admin_notices->add(
21652
- $this->get_text_inline( 'Seems like you got the latest release.', 'you-have-latest' ),
21653
- $this->get_text_inline( 'You are all good!', 'you-are-good' )
21654
- );
21655
- }
21656
-
21657
- $this->_store_update( $update, true, $plugin_id );
21658
- }
21659
-
21660
- /**
21661
- * @author Vova Feldman (@svovaf)
21662
- * @since 1.0.4
21663
- *
21664
- * @param bool $flush Since 1.1.7.3 add 24 hour cache by default.
21665
- *
21666
- * @return FS_Plugin[]
21667
- *
21668
- * @uses FS_Api
21669
- */
21670
- private function sync_addons( $flush = false ) {
21671
- $this->_logger->entrance();
21672
-
21673
- $api = $this->get_api_site_or_plugin_scope();
21674
-
21675
- $path = $this->add_show_pending( '/addons.json?enriched=true&count=50' );
21676
-
21677
- /**
21678
- * @since 1.2.1
21679
- *
21680
- * If there's a cached version of the add-ons and not asking
21681
- * for a flush, just use the currently stored add-ons.
21682
- */
21683
- if ( ! $flush && $api->is_cached( $path ) ) {
21684
- $addons = self::get_all_addons();
21685
-
21686
- return isset( $addons[ $this->_plugin->id ] ) ?
21687
- $addons[ $this->_plugin->id ] :
21688
- array();
21689
- }
21690
-
21691
- $result = $api->get( $path, $flush );
21692
-
21693
- $addons = array();
21694
- if ( $this->is_api_result_object( $result, 'plugins' ) &&
21695
- is_array( $result->plugins )
21696
- ) {
21697
- for ( $i = 0, $len = count( $result->plugins ); $i < $len; $i ++ ) {
21698
- $addons[ $i ] = new FS_Plugin( $result->plugins[ $i ] );
21699
- }
21700
-
21701
- $this->_store_addons( $addons, true );
21702
- }
21703
-
21704
- return $addons;
21705
- }
21706
-
21707
- /**
21708
- * Handle user email update.
21709
- *
21710
- * @author Vova Feldman (@svovaf)
21711
- * @since 1.0.3
21712
- * @uses FS_Api
21713
- *
21714
- * @param string $new_email
21715
- *
21716
- * @return object
21717
- */
21718
- private function update_email( $new_email ) {
21719
- $this->_logger->entrance();
21720
-
21721
-
21722
- $api = $this->get_api_user_scope();
21723
- $user = $api->call( "?plugin_id={$this->_plugin->id}&fields=id,email,is_verified", 'put', array(
21724
- 'email' => $new_email,
21725
- 'after_email_confirm_url' => $this->_get_admin_page_url(
21726
- 'account',
21727
- array( 'fs_action' => 'sync_user' )
21728
- ),
21729
- ) );
21730
-
21731
- if ( ! isset( $user->error ) ) {
21732
- $this->_user->email = $user->email;
21733
- $this->_user->is_verified = $user->is_verified;
21734
- $this->_store_user();
21735
- } else {
21736
- // handle different error cases.
21737
-
21738
- }
21739
-
21740
- return $user;
21741
- }
21742
-
21743
- #----------------------------------------------------------------------------------
21744
- #region API Error Handling
21745
- #----------------------------------------------------------------------------------
21746
-
21747
- /**
21748
- * @author Vova Feldman (@svovaf)
21749
- * @since 1.1.1
21750
- *
21751
- * @param mixed $result
21752
- *
21753
- * @return bool Is API result contains an error.
21754
- */
21755
- private function is_api_error( $result ) {
21756
- return FS_Api::is_api_error( $result );
21757
- }
21758
-
21759
- /**
21760
- * Checks if given API result is a non-empty and not an error object.
21761
- *
21762
- * @author Vova Feldman (@svovaf)
21763
- * @since 1.2.1.5
21764
- *
21765
- * @param mixed $result
21766
- * @param string|null $required_property Optional property we want to verify that is set.
21767
- *
21768
- * @return bool
21769
- */
21770
- function is_api_result_object( $result, $required_property = null ) {
21771
- return FS_Api::is_api_result_object( $result, $required_property );
21772
- }
21773
-
21774
- /**
21775
- * Checks if given API result is a non-empty entity object with non-empty ID.
21776
- *
21777
- * @author Vova Feldman (@svovaf)
21778
- * @since 1.2.1.5
21779
- *
21780
- * @param mixed $result
21781
- *
21782
- * @return bool
21783
- */
21784
- private function is_api_result_entity( $result ) {
21785
- return FS_Api::is_api_result_entity( $result );
21786
- }
21787
-
21788
- #endregion
21789
-
21790
- /**
21791
- * Make sure a given argument is an array of a specific type.
21792
- *
21793
- * @author Vova Feldman (@svovaf)
21794
- * @since 1.2.1.5
21795
- *
21796
- * @param mixed $array
21797
- * @param string $class
21798
- *
21799
- * @return bool
21800
- */
21801
- private function is_array_instanceof( $array, $class ) {
21802
- return ( is_array( $array ) && ( empty( $array ) || $array[0] instanceof $class ) );
21803
- }
21804
-
21805
- /**
21806
- * Start install ownership change.
21807
- *
21808
- * @author Vova Feldman (@svovaf)
21809
- * @since 1.1.1
21810
- * @uses FS_Api
21811
- *
21812
- * @param string $candidate_email
21813
- *
21814
- * @return bool Is ownership change successfully initiated.
21815
- */
21816
- private function init_change_owner( $candidate_email ) {
21817
- $this->_logger->entrance();
21818
-
21819
- $api = $this->get_api_site_scope();
21820
- $result = $api->call( "/users/{$this->_user->id}.json", 'put', array(
21821
- 'email' => $candidate_email,
21822
- 'after_confirm_url' => $this->_get_admin_page_url(
21823
- 'account',
21824
- array( 'fs_action' => 'change_owner' )
21825
- ),
21826
- ) );
21827
-
21828
- return ! $this->is_api_error( $result );
21829
- }
21830
-
21831
- /**
21832
- * Handle install ownership change.
21833
- *
21834
- * @author Vova Feldman (@svovaf)
21835
- * @since 1.1.1
21836
- * @uses FS_Api
21837
- *
21838
- * @return bool Was ownership change successfully complete.
21839
- */
21840
- private function complete_change_owner() {
21841
- $this->_logger->entrance();
21842
-
21843
- $site_result = $this->get_api_site_scope( true )->get();
21844
- $site = new FS_Site( $site_result );
21845
- $this->_site = $site;
21846
-
21847
- $user = new FS_User();
21848
- $user->id = fs_request_get( 'user_id' );
21849
-
21850
- // Validate install's user and given user.
21851
- if ( $user->id != $this->_site->user_id ) {
21852
- return false;
21853
- }
21854
-
21855
- $user->public_key = fs_request_get( 'user_public_key' );
21856
- $user->secret_key = fs_request_get( 'user_secret_key' );
21857
-
21858
- // Fetch new user information.
21859
- $this->_user = $user;
21860
- $user_result = $this->get_api_user_scope( true )->get();
21861
- $user = new FS_User( $user_result );
21862
- $this->_user = $user;
21863
-
21864
- $this->_set_account( $user, $site );
21865
-
21866
- return true;
21867
- }
21868
-
21869
- /**
21870
- * Completes ownership change by license.
21871
- *
21872
- * @author Leo Fajardo (@leorw)
21873
- * @since 2.3.2
21874
- *
21875
- * @param number $user_id
21876
- * @param array[string]number $install_ids_by_slug_map
21877
- *
21878
- */
21879
- private function complete_ownership_change_by_license( $user_id, $install_ids_by_slug_map ) {
21880
- $this->_logger->entrance();
21881
-
21882
- $this->sync_user_by_current_install( $user_id );
21883
-
21884
- $result = $this->get_api_user_scope( true )->get(
21885
- "/installs.json?install_ids=" . implode( ',', $install_ids_by_slug_map )
21886
- );
21887
-
21888
- if ( $this->is_api_result_object( $result, 'installs' ) ) {
21889
- $sites = self::get_all_sites( $this->get_module_type() );
21890
- $install_ids_by_slug_map = array_flip( $install_ids_by_slug_map );
21891
-
21892
- foreach ( $result->installs as $install ) {
21893
- $site = new FS_Site( $install );
21894
-
21895
- $sites[ $install_ids_by_slug_map[ $site->id ] ] = clone $site;
21896
- }
21897
-
21898
- $this->set_account_option( 'sites', $sites, true );
21899
- }
21900
- }
21901
-
21902
- /**
21903
- * Handle user name update.
21904
- *
21905
- * @author Vova Feldman (@svovaf)
21906
- * @since 1.0.9
21907
- * @uses FS_Api
21908
- *
21909
- * @return object
21910
- */
21911
- private function update_user_name() {
21912
- $this->_logger->entrance();
21913
- $name = fs_request_get( 'fs_user_name_' . $this->get_unique_affix(), '' );
21914
-
21915
- $api = $this->get_api_user_scope();
21916
- $user = $api->call( "?plugin_id={$this->_plugin->id}&fields=id,first,last", 'put', array(
21917
- 'name' => $name,
21918
- ) );
21919
-
21920
- if ( ! isset( $user->error ) ) {
21921
- $this->_user->first = $user->first;
21922
- $this->_user->last = $user->last;
21923
- $this->_store_user();
21924
- } else {
21925
- // handle different error cases.
21926
-
21927
- }
21928
-
21929
- return $user;
21930
- }
21931
-
21932
- /**
21933
- * Verify user email.
21934
- *
21935
- * @author Vova Feldman (@svovaf)
21936
- * @since 1.0.3
21937
- * @uses FS_Api
21938
- */
21939
- private function verify_email() {
21940
- $this->_handle_account_user_sync();
21941
-
21942
- if ( $this->_user->is_verified() ) {
21943
- return;
21944
- }
21945
-
21946
- $api = $this->get_api_site_scope();
21947
- $result = $api->call( "/users/{$this->_user->id}/verify.json", 'put', array(
21948
- 'after_email_confirm_url' => $this->_get_admin_page_url(
21949
- 'account',
21950
- array( 'fs_action' => 'sync_user' )
21951
- )
21952
- ) );
21953
-
21954
- if ( ! isset( $result->error ) ) {
21955
- $this->_admin_notices->add( sprintf(
21956
- $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' ),
21957
- sprintf( '<a href="mailto:%1$s">%2$s</a>', esc_url( $this->_user->email ), $this->_user->email )
21958
- ) );
21959
- } else {
21960
- // handle different error cases.
21961
-
21962
- }
21963
- }
21964
-
21965
- /**
21966
- * @author Vova Feldman (@svovaf)
21967
- * @since 1.1.2
21968
- *
21969
- * @param array $params
21970
- * @param bool|null $network
21971
- *
21972
- * @return string
21973
- */
21974
- function get_activation_url( $params = array(), $network = null ) {
21975
- if ( $this->is_addon() && $this->has_free_plan() ) {
21976
- /**
21977
- * @author Vova Feldman (@svovaf)
21978
- * @since 1.2.1.7 Add-on's activation is the parent's module activation.
21979
- */
21980
- return $this->get_parent_instance()->get_activation_url( $params );
21981
- }
21982
-
21983
- return $this->apply_filters( 'connect_url', $this->_get_admin_page_url( '', $params, $network ) );
21984
- }
21985
-
21986
- /**
21987
- * @author Vova Feldman (@svovaf)
21988
- * @since 1.2.1.5
21989
- *
21990
- * @param array $params
21991
- *
21992
- * @return string
21993
- */
21994
- function get_reconnect_url( $params = array() ) {
21995
- $params['fs_action'] = 'reset_anonymous_mode';
21996
- $params['fs_unique_affix'] = $this->get_unique_affix();
21997
-
21998
- return $this->get_activation_url( $params );
21999
- }
22000
-
22001
- /**
22002
- * Get the URL of the page that should be loaded after the user connect
22003
- * or skip in the opt-in screen.
22004
- *
22005
- * @author Vova Feldman (@svovaf)
22006
- * @since 1.1.3
22007
- *
22008
- * @param string $filter Filter name.
22009
- * @param array $params Since 1.2.2.7
22010
- * @param bool|null $network
22011
- *
22012
- * @return string
22013
- */
22014
- function get_after_activation_url( $filter, $params = array(), $network = null ) {
22015
- if ( $this->show_opt_in_on_themes_page() &&
22016
- ( fs_request_has( 'pending_activation' ) ||
22017
- // For cases when the first time path is set, even though it's a WP.org theme.
22018
- fs_request_get_bool( $this->get_unique_affix() . '_show_optin' ) )
22019
- ) {
22020
- $first_time_path = '';
22021
- } else {
22022
- $first_time_path = $this->_menu->get_first_time_path(
22023
- fs_is_network_admin() && $this->_is_network_active
22024
- );
22025
- }
22026
-
22027
- if ( $this->_is_network_active &&
22028
- fs_is_network_admin() &&
22029
- ! $this->_menu->has_network_menu() &&
22030
- $this->is_network_registered()
22031
- ) {
22032
- $target_url = $this->get_account_url();
22033
- } else {
22034
- // Default plugin's page.
22035
- $target_url = $this->_get_admin_page_url( '', array(), $network );
22036
- }
22037
-
22038
- return add_query_arg( $params, $this->apply_filters(
22039
- $filter,
22040
- empty( $first_time_path ) ?
22041
- $target_url :
22042
- $first_time_path
22043
- ) );
22044
- }
22045
-
22046
- /**
22047
- * Handle account page updates / edits / actions.
22048
- *
22049
- * @author Vova Feldman (@svovaf)
22050
- * @since 1.0.2
22051
- *
22052
- */
22053
- private function _handle_account_edits() {
22054
- if ( ! $this->is_user_admin() ) {
22055
- return;
22056
- }
22057
-
22058
- $action = fs_get_action();
22059
-
22060
- if ( empty( $action ) ) {
22061
- return;
22062
- }
22063
-
22064
- $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
22065
- $install_id = fs_request_get( 'install_id', '' );
22066
-
22067
- // Alias.
22068
- $oops_text = $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...';
22069
-
22070
- $is_network_action = $this->is_network_level_action();
22071
- $blog_id = $this->is_network_level_site_specific_action();
22072
- $is_parent_plugin_action = ( $plugin_id == $this->get_id() );
22073
-
22074
- if ( is_numeric( $blog_id ) ) {
22075
- $this->switch_to_blog( $blog_id );
22076
- } else {
22077
- $blog_id = '';
22078
- }
22079
-
22080
- switch ( $action ) {
22081
- case 'opt_in':
22082
- check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
22083
-
22084
- if ( $is_parent_plugin_action ) {
22085
- if ( $is_network_action && ! empty( $blog_id ) ) {
22086
- if ( ! $this->is_registered() ) {
22087
- $this->install_with_user(
22088
- $this->get_network_user(),
22089
- false,
22090
- false,
22091
- false,
22092
- false
22093
- );
22094
-
22095
- $this->_admin_notices->add(
22096
- $this->get_text_inline( 'Site successfully opted in.', 'successful-opt-in' ),
22097
- $this->get_text_inline( 'Awesome', 'awesome' )
22098
- );
22099
- }
22100
- }
22101
- }
22102
- break;
22103
-
22104
- case 'toggle_tracking':
22105
- check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
22106
-
22107
- if ( $is_parent_plugin_action ) {
22108
- if ( $is_network_action && ! empty( $blog_id ) ) {
22109
- if ( $this->is_registered() ) {
22110
- if ( $this->is_tracking_prohibited() ) {
22111
- if ( $this->allow_site_tracking() ) {
22112
- $this->_admin_notices->add(
22113
- 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 ),
22114
- $this->get_text_inline( 'Thank you!', 'thank-you' )
22115
- );
22116
- }
22117
- } else {
22118
- if ( $this->stop_site_tracking() ) {
22119
- $this->_admin_notices->add(
22120
- sprintf(
22121
- $this->get_text_inline( 'We will no longer be sending any usage data of %s on %s to %s.', 'opted-out-successfully' ),
22122
- $this->get_plugin_title(),
22123
- fs_strip_url_protocol( get_site_url( $blog_id ) ),
22124
- sprintf(
22125
- '<a href="%s" target="_blank" rel="noopener">%s</a>',
22126
- 'https://freemius.com',
22127
- 'freemius.com'
22128
- )
22129
- )
22130
- );
22131
- }
22132
- }
22133
- }
22134
- }
22135
- }
22136
-
22137
- break;
22138
-
22139
- case 'delete_account':
22140
- check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
22141
-
22142
- $is_network_deletion = $is_network_action && empty( $blog_id );
22143
-
22144
- if ( $is_parent_plugin_action ) {
22145
- // Delete add-on installs if have any.
22146
- $installed_addons = $this->get_installed_addons();
22147
- foreach ( $installed_addons as $fs_addon ) {
22148
- if ( $is_network_deletion ) {
22149
- $fs_addon->delete_network_account_event();
22150
- } else {
22151
- $fs_addon->delete_account_event();
22152
- }
22153
- }
22154
-
22155
- if ( $is_network_deletion ) {
22156
- $this->delete_network_account_event();
22157
- } else {
22158
- $this->delete_account_event();
22159
- }
22160
-
22161
- // Clear user and site.
22162
- $this->_site = null;
22163
- $this->_user = null;
22164
-
22165
- $this->maybe_set_slug_and_network_menu_exists_flag();
22166
-
22167
- fs_redirect( $this->get_activation_url() );
22168
- } else {
22169
- if ( $this->is_addon_activated( $plugin_id ) ) {
22170
- $fs_addon = self::get_instance_by_id( $plugin_id );
22171
-
22172
- if ( $is_network_deletion ) {
22173
- $fs_addon->delete_network_account_event();
22174
- } else {
22175
- $fs_addon->delete_account_event();
22176
- }
22177
-
22178
- fs_redirect( $this->_get_admin_page_url( 'account' ) );
22179
- }
22180
- }
22181
-
22182
- return;
22183
-
22184
- case 'downgrade_account':
22185
- if ( is_numeric( $blog_id ) ) {
22186
- check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
22187
- } else {
22188
- check_admin_referer( $action );
22189
- }
22190
-
22191
- $switch_to_network_install_blog_after_cancellation = (
22192
- is_numeric( $blog_id ) &&
22193
- $plugin_id == $this->get_id() &&
22194
- ! $this->is_trial()
22195
- );
22196
-
22197
- $result = $this->cancel_subscription_or_trial( $plugin_id );
22198
- if ( $this->is_api_error( $result ) ) {
22199
- $this->_admin_notices->add(
22200
- $result->error->message,
22201
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
22202
- 'error'
22203
- );
22204
- }
22205
-
22206
- if ( $switch_to_network_install_blog_after_cancellation ) {
22207
- $this->switch_to_blog( $this->_storage->network_install_blog_id );
22208
- }
22209
-
22210
- return;
22211
-
22212
- case 'activate_license':
22213
- check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
22214
-
22215
- $fs = $this;
22216
- if ( $plugin_id != $this->get_id() ) {
22217
- $fs = $this->is_addon_activated( $plugin_id ) ?
22218
- self::get_instance_by_id( $plugin_id ) :
22219
- null;
22220
- }
22221
-
22222
- if ( is_object( $fs ) ) {
22223
- $fs->_activate_license();
22224
-
22225
- /**
22226
- * Remove the product ID from `$_REQUEST` so that the syncing of the license for the other products will work properly.
22227
- *
22228
- * @author Leo Fajardo (@leorw)
22229
- * @since 2.4.0
22230
- */
22231
- unset( $_REQUEST['plugin_id'] );
22232
-
22233
- if ( $this->is_bundle_license_auto_activation_enabled() ) {
22234
- $fs->maybe_activate_bundle_license( null, array(), is_numeric( $blog_id ) ? $blog_id : 0 );
22235
- }
22236
- }
22237
-
22238
- return;
22239
-
22240
- case 'deactivate_license':
22241
- check_admin_referer( trim( "{$action}:{$blog_id}:{$install_id}", ':' ) );
22242
-
22243
- if ( $plugin_id == $this->get_id() ) {
22244
- $this->_deactivate_license();
22245
-
22246
- if ( $this->is_only_premium() ) {
22247
- // Clear user and site.
22248
- $this->_site = null;
22249
- $this->_user = null;
22250
-
22251
- if ( ! $is_network_action ) {
22252
- fs_redirect( $this->get_activation_url() );
22253
- } else if ( is_numeric( $blog_id ) ) {
22254
- $this->switch_to_blog( $this->_storage->network_install_blog_id );
22255
- }
22256
- }
22257
- } else {
22258
- if ( $this->is_addon_activated( $plugin_id ) ) {
22259
- $fs_addon = self::get_instance_by_id( $plugin_id );
22260
- $fs_addon->_deactivate_license();
22261
- }
22262
- }
22263
-
22264
- return;
22265
-
22266
- case 'check_updates':
22267
- check_admin_referer( $action );
22268
- $this->check_updates();
22269
-
22270
- return;
22271
-
22272
- case 'change_owner':
22273
- $state = fs_request_get( 'state', 'init' );
22274
- switch ( $state ) {
22275
- case 'init':
22276
- $candidate_email = fs_request_get( 'candidate_email', '' );
22277
-
22278
- if ( $this->init_change_owner( $candidate_email ) ) {
22279
- $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>' ) );
22280
- }
22281
- break;
22282
- case 'owner_confirmed':
22283
- $candidate_email = fs_request_get( 'candidate_email', '' );
22284
-
22285
- $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>' ) );
22286
- break;
22287
- case 'candidate_confirmed':
22288
- if ( $this->complete_change_owner() ) {
22289
- $this->_admin_notices->add_sticky(
22290
- sprintf( $this->get_text_inline( '%s is the new owner of the account.', 'change-owner-request_candidate-confirmed' ), '<b>' . $this->_user->email . '</b>' ),
22291
- 'ownership_changed',
22292
- $this->get_text_x_inline( 'Congrats', 'as congratulations', 'congrats' ) . '!'
22293
- );
22294
- } else {
22295
- // @todo Handle failed ownership change message.
22296
- }
22297
- break;
22298
- }
22299
-
22300
- return;
22301
-
22302
- case 'update_email':
22303
- check_admin_referer( 'update_email' );
22304
-
22305
- $new_email = fs_request_get( 'fs_email_' . $this->get_unique_affix(), '' );
22306
- $result = $this->update_email( $new_email );
22307
-
22308
- if ( isset( $result->error ) ) {
22309
- switch ( $result->error->code ) {
22310
- case 'user_exist':
22311
- $this->_admin_notices->add(
22312
- $this->get_text_inline( 'Sorry, we could not complete the email update. Another user with the same email is already registered.', 'user-exist-message' ) . ' ' .
22313
- 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>' ) .
22314
- sprintf(
22315
- '<a style="margin-left: 10px;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
22316
- $this->get_account_url( 'change_owner', array(
22317
- 'state' => 'init',
22318
- 'candidate_email' => $new_email
22319
- ) ),
22320
- $this->get_text_inline( 'Change Ownership', 'change-ownership' )
22321
- ),
22322
- $oops_text,
22323
- 'error'
22324
- );
22325
- break;
22326
- }
22327
- } else {
22328
- $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' ) );
22329
- }
22330
-
22331
- return;
22332
-
22333
- case 'update_user_name':
22334
- check_admin_referer( 'update_user_name' );
22335
-
22336
- $result = $this->update_user_name();
22337
-
22338
- if ( isset( $result->error ) ) {
22339
- $this->_admin_notices->add(
22340
- $this->get_text_inline( 'Please provide your full name.', 'name-update-failed-message' ),
22341
- $oops_text,
22342
- 'error'
22343
- );
22344
- } else {
22345
- $this->_admin_notices->add( $this->get_text_inline( 'Your name was successfully updated.', 'name-updated-message' ) );
22346
- }
22347
-
22348
- return;
22349
-
22350
- #region Actions that might be called from external links (e.g. email)
22351
-
22352
- case 'cancel_trial':
22353
- $result = $this->cancel_subscription_or_trial( $plugin_id );
22354
- if ( $this->is_api_error( $result ) ) {
22355
- $this->_admin_notices->add(
22356
- $result->error->message,
22357
- $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
22358
- 'error'
22359
- );
22360
- }
22361
-
22362
- return;
22363
-
22364
- case 'verify_email':
22365
- $this->verify_email();
22366
-
22367
- return;
22368
-
22369
- case 'sync_user':
22370
- $this->_handle_account_user_sync();
22371
-
22372
- return;
22373
-
22374
- case $this->get_unique_affix() . '_sync_license':
22375
- $this->_sync_license();
22376
-
22377
- return;
22378
-
22379
- case 'download_latest':
22380
- $this->download_latest_directly( $plugin_id );
22381
-
22382
- return;
22383
-
22384
- #endregion
22385
- }
22386
-
22387
- if ( WP_FS__IS_POST_REQUEST ) {
22388
- $properties = array( 'site_secret_key', 'site_id', 'site_public_key' );
22389
- foreach ( $properties as $p ) {
22390
- if ( 'update_' . $p === $action ) {
22391
- check_admin_referer( $action );
22392
-
22393
- $this->_logger->log( $action );
22394
-
22395
- $site_property = substr( $p, strlen( 'site_' ) );
22396
- $site_property_value = fs_request_get( 'fs_' . $p . '_' . $this->get_unique_affix(), '' );
22397
- $this->get_site()->{$site_property} = $site_property_value;
22398
-
22399
- // Store account after modification.
22400
- $this->_store_site();
22401
-
22402
- $this->do_action( 'account_property_edit', 'site', $site_property, $site_property_value );
22403
-
22404
- $this->_admin_notices->add( sprintf(
22405
- /* translators: %s: User's account property (e.g. email address, name) */
22406
- $this->get_text_inline( 'You have successfully updated your %s.', 'x-updated' ),
22407
- '<b>' . str_replace( '_', ' ', $p ) . '</b>'
22408
- ) );
22409
-
22410
- return;
22411
- }
22412
- }
22413
- }
22414
- }
22415
-
22416
- /**
22417
- * Account page resources load.
22418
- *
22419
- * @author Vova Feldman (@svovaf)
22420
- * @since 1.0.6
22421
- */
22422
- function _account_page_load() {
22423
- $this->_logger->entrance();
22424
-
22425
- $this->_logger->info( var_export( $_REQUEST, true ) );
22426
-
22427
- fs_enqueue_local_style( 'fs_account', '/admin/account.css' );
22428
-
22429
- if ( $this->has_addons() ) {
22430
- wp_enqueue_script( 'plugin-install' );
22431
- add_thickbox();
22432
-
22433
- function fs_addons_body_class( $classes ) {
22434
- $classes .= ' plugins-php';
22435
-
22436
- return $classes;
22437
- }
22438
-
22439
- add_filter( 'admin_body_class', 'fs_addons_body_class' );
22440
- }
22441
-
22442
- if ( $this->has_paid_plan() &&
22443
- ! $this->has_any_license() &&
22444
- ! $this->is_sync_executed() &&
22445
- $this->is_tracking_allowed()
22446
- ) {
22447
- /**
22448
- * If no licenses found and no sync job was executed during the last 24 hours,
22449
- * just execute the sync job right away (blocking execution).
22450
- *
22451
- * @since 1.1.7.3
22452
- */
22453
- $this->run_manual_sync();
22454
- }
22455
-
22456
- $this->_handle_account_edits();
22457
-
22458
- if (
22459
- is_object( $this->_license ) &&
22460
- $this->_license->user_id == $this->_user->id &&
22461
- ! $this->is_whitelabeled( true )
22462
- ) {
22463
- $this->_admin_notices->add(
22464
- sprintf(
22465
- $this->get_text_inline( "Is this your client's site? %s if you wish to hide sensitive info like your email, license key, prices, billing address & invoices from the WP Admin.", 'license_not_whitelabeled' ),
22466
- sprintf(
22467
- '<a href="#" class="fs-toggle-whitelabel-mode">%s</a>',
22468
- $this->get_text_inline( 'Click here', 'click-here' )
22469
- )
22470
- ),
22471
- '',
22472
- 'success',
22473
- false,
22474
- 'license_not_whitelabeled'
22475
- );
22476
- }
22477
-
22478
- $this->do_action( 'account_page_load_before_departure' );
22479
- }
22480
-
22481
- /**
22482
- * Renders the "Affiliation" page.
22483
- *
22484
- * @author Leo Fajardo (@leorw)
22485
- * @since 1.2.3
22486
- */
22487
- function _affiliation_page_render() {
22488
- $this->_logger->entrance();
22489
-
22490
- $this->fetch_affiliate_and_terms();
22491
-
22492
- fs_enqueue_local_style( 'fs_affiliation', '/admin/affiliation.css' );
22493
-
22494
- $vars = array( 'id' => $this->_module_id );
22495
- echo $this->apply_filters( "/forms/affiliation.php", fs_get_template( '/forms/affiliation.php', $vars ) );
22496
- }
22497
-
22498
-
22499
- /**
22500
- * Render account page.
22501
- *
22502
- * @author Vova Feldman (@svovaf)
22503
- * @since 1.0.0
22504
- */
22505
- function _account_page_render() {
22506
- $this->_logger->entrance();
22507
-
22508
- $template = 'account.php';
22509
- $vars = array( 'id' => $this->_module_id );
22510
-
22511
- /**
22512
- * Added filter to the template to allow developers wrapping the template
22513
- * in custom HTML (e.g. within a wizard/tabs).
22514
- *
22515
- * @author Vova Feldman (@svovaf)
22516
- * @since 1.2.1.6
22517
- */
22518
- echo $this->apply_filters( "templates/{$template}", fs_get_template( $template, $vars ) );
22519
- }
22520
-
22521
- /**
22522
- * Render account connect page.
22523
- *
22524
- * @author Vova Feldman (@svovaf)
22525
- * @since 1.0.7
22526
- */
22527
- function _connect_page_render() {
22528
- $this->_logger->entrance();
22529
-
22530
- $vars = array( 'id' => $this->_module_id );
22531
-
22532
- /**
22533
- * Added filter to the template to allow developers wrapping the template
22534
- * in custom HTML (e.g. within a wizard/tabs).
22535
- *
22536
- * @author Vova Feldman (@svovaf)
22537
- * @since 1.2.1.6
22538
- */
22539
- echo $this->apply_filters( 'templates/connect.php', fs_get_template( 'connect.php', $vars ) );
22540
- }
22541
-
22542
- /**
22543
- * Load required resources before add-ons page render.
22544
- *
22545
- * @author Vova Feldman (@svovaf)
22546
- * @since 1.0.6
22547
- */
22548
- function _addons_page_load() {
22549
- $this->_logger->entrance();
22550
-
22551
- fs_enqueue_local_style( 'fs_addons', '/admin/add-ons.css' );
22552
-
22553
- wp_enqueue_script( 'plugin-install' );
22554
- add_thickbox();
22555
-
22556
- function fs_addons_body_class( $classes ) {
22557
- $classes .= ' plugins-php';
22558
-
22559
- return $classes;
22560
- }
22561
-
22562
- add_filter( 'admin_body_class', 'fs_addons_body_class' );
22563
-
22564
- if ( ! $this->is_registered() && $this->is_org_repo_compliant() ) {
22565
- $this->_admin_notices->add(
22566
- 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>' ),
22567
- $this->get_text_x_inline( 'Heads up', 'advance notice of something that will need attention.', 'heads-up' ),
22568
- 'update-nag'
22569
- );
22570
- }
22571
- }
22572
-
22573
- /**
22574
- * Render add-ons page.
22575
- *
22576
- * @author Vova Feldman (@svovaf)
22577
- * @since 1.0.6
22578
- */
22579
- function _addons_page_render() {
22580
- $this->_logger->entrance();
22581
-
22582
- $vars = array( 'id' => $this->_module_id );
22583
-
22584
- /**
22585
- * Added filter to the template to allow developers wrapping the template
22586
- * in custom HTML (e.g. within a wizard/tabs).
22587
- *
22588
- * @author Vova Feldman (@svovaf)
22589
- * @since 1.2.1.6
22590
- */
22591
- echo $this->apply_filters( 'templates/add-ons.php', fs_get_template( 'add-ons.php', $vars ) );
22592
- }
22593
-
22594
- /* Pricing & Upgrade
22595
- ------------------------------------------------------------------------------------------------------------------*/
22596
- /**
22597
- * Render pricing page.
22598
- *
22599
- * @author Vova Feldman (@svovaf)
22600
- * @since 1.0.0
22601
- */
22602
- function _pricing_page_render() {
22603
- $this->_logger->entrance();
22604
-
22605
- $vars = array( 'id' => $this->_module_id );
22606
-
22607
- if ( 'true' === fs_request_get( 'checkout', false ) ) {
22608
- echo $this->apply_filters( 'templates/checkout.php', fs_get_template( 'checkout.php', $vars ) );
22609
- } else {
22610
- echo $this->apply_filters( 'templates/pricing.php', fs_get_template( 'pricing.php', $vars ) );
22611
- }
22612
- }
22613
-
22614
- /**
22615
- * @author Leo Fajardo (@leorw)
22616
- * @since 2.3.1
22617
- */
22618
- function _maybe_add_pricing_ajax_handler() {
22619
- if ( ! $this->should_use_external_pricing() ) {
22620
- $this->add_ajax_action( 'pricing_ajax_action', array( $this, '_fs_pricing_ajax_action_handler' ) );
22621
- }
22622
- }
22623
-
22624
- /**
22625
- * @author Leo Fajardo (@leorw)
22626
- * @since 2.3.1
22627
- */
22628
- function _fs_pricing_ajax_action_handler() {
22629
- $this->check_ajax_referer( 'pricing_ajax_action' );
22630
-
22631
- $result = null;
22632
- $pricing_action = fs_request_get( 'pricing_action' );
22633
-
22634
- switch ( $pricing_action ) {
22635
- case 'fetch_pricing_data':
22636
- $params = array(
22637
- 'is_enriched' => true,
22638
- 'trial' => fs_request_get_bool( 'trial' ),
22639
- 'sandbox' => fs_request_get( 'sandbox' ),
22640
- 's_ctx_type' => fs_request_get( 's_ctx_type' ),
22641
- 's_ctx_id' => fs_request_get( 's_ctx_id' ),
22642
- 's_ctx_ts' => fs_request_get( 's_ctx_ts' ),
22643
- 's_ctx_secure' => fs_request_get( 's_ctx_secure' ),
22644
- );
22645
-
22646
- $bundle_id = $this->get_bundle_id();
22647
- $bundle_public_key = $this->get_bundle_public_key();
22648
-
22649
- $has_bundle_context = ( FS_Plugin::is_valid_id( $bundle_id ) && ! empty( $bundle_public_key ) );
22650
-
22651
- if ( ! $has_bundle_context ) {
22652
- $api = $this->get_api_plugin_scope();
22653
- } else {
22654
- $api = FS_Api::instance(
22655
- $bundle_id,
22656
- 'plugin',
22657
- $bundle_id,
22658
- $bundle_public_key,
22659
- ! $this->is_live(),
22660
- false,
22661
- $this->get_sdk_version()
22662
- );
22663
-
22664
- $params['plugin_id'] = $this->get_id();
22665
- $params['plugin_public_key'] = $this->get_public_key();
22666
- }
22667
-
22668
- $result = $api->get( 'pricing.json?' . http_build_query( $params ) );
22669
- break;
22670
- case 'start_trial':
22671
- $result = $this->opt_in(
22672
- false,
22673
- false,
22674
- false,
22675
- false,
22676
- false,
22677
- fs_request_get( 'plan_id' )
22678
- );
22679
- }
22680
-
22681
- if ( is_object( $result ) && $this->is_api_error( $result ) ) {
22682
- $this->_logger->api_error( $result );
22683
-
22684
- self::shoot_ajax_failure(
22685
- isset( $result->error ) ?
22686
- ( is_string( $result->error ) ? $result->error : $result->error->message ) :
22687
- var_export( $result, true )
22688
- );
22689
- }
22690
-
22691
- $this->shoot_ajax_success( $result );
22692
- }
22693
-
22694
- #----------------------------------------------------------------------------------
22695
- #region Contact Us
22696
- #----------------------------------------------------------------------------------
22697
-
22698
- /**
22699
- * Render contact-us page.
22700
- *
22701
- * @author Vova Feldman (@svovaf)
22702
- * @since 1.0.3
22703
- */
22704
- function _contact_page_render() {
22705
- $this->_logger->entrance();
22706
-
22707
- $vars = array( 'id' => $this->_module_id );
22708
-
22709
- /**
22710
- * Added filter to the template to allow developers wrapping the template
22711
- * in custom HTML (e.g. within a wizard/tabs).
22712
- *
22713
- * @author Vova Feldman (@svovaf)
22714
- * @since 2.1.3
22715
- */
22716
- echo $this->apply_filters( 'templates/contact.php', fs_get_template( 'contact.php', $vars ) );
22717
- }
22718
-
22719
- #endregion ------------------------------------------------------------------------
22720
-
22721
- /**
22722
- * Hide all admin notices to prevent distractions.
22723
- *
22724
- * @author Vova Feldman (@svovaf)
22725
- * @since 1.0.3
22726
- *
22727
- * @uses remove_all_actions()
22728
- */
22729
- private static function _hide_admin_notices() {
22730
- remove_all_actions( 'admin_notices' );
22731
- remove_all_actions( 'network_admin_notices' );
22732
- remove_all_actions( 'all_admin_notices' );
22733
- remove_all_actions( 'user_admin_notices' );
22734
- }
22735
-
22736
- static function _clean_admin_content_section_hook() {
22737
- self::_hide_admin_notices();
22738
-
22739
- // Hide footer.
22740
- echo '<style>#wpfooter { display: none !important; }</style>';
22741
- }
22742
-
22743
- /**
22744
- * Attach to admin_head hook to hide all admin notices.
22745
- *
22746
- * @author Vova Feldman (@svovaf)
22747
- * @since 1.0.3
22748
- */
22749
- static function _clean_admin_content_section() {
22750
- add_action( 'admin_head', 'Freemius::_clean_admin_content_section_hook' );
22751
- }
22752
-
22753
- /* CSS & JavaScript
22754
- ------------------------------------------------------------------------------------------------------------------*/
22755
- /* function _enqueue_script($handle, $src) {
22756
- $url = plugins_url( substr( WP_FS__DIR_JS, strlen( $this->_plugin_dir_path ) ) . '/assets/js/' . $src );
22757
-
22758
- $this->_logger->entrance( 'script = ' . $url );
22759
-
22760
- wp_enqueue_script( $handle, $url );
22761
- }*/
22762
-
22763
- /* SDK
22764
- ------------------------------------------------------------------------------------------------------------------*/
22765
- private $_user_api;
22766
-
22767
- /**
22768
- *
22769
- * @author Vova Feldman (@svovaf)
22770
- * @since 1.0.2
22771
- *
22772
- * @param bool $flush
22773
- *
22774
- * @return FS_Api
22775
- */
22776
- private function get_api_user_scope( $flush = false ) {
22777
- if ( ! isset( $this->_user_api ) || $flush ) {
22778
- $this->_user_api = $this->get_api_user_scope_by_user( $this->_user );
22779
- }
22780
-
22781
- return $this->_user_api;
22782
- }
22783
-
22784
- /**
22785
- * @author Vova Feldman (@svovaf)
22786
- * @since 2.0.0
22787
- *
22788
- * @param \FS_User $user
22789
- *
22790
- * @return \FS_Api
22791
- */
22792
- private function get_api_user_scope_by_user( FS_User $user ) {
22793
- return FS_Api::instance(
22794
- $this->_module_id,
22795
- 'user',
22796
- $user->id,
22797
- $user->public_key,
22798
- ! $this->is_live(),
22799
- $user->secret_key,
22800
- $this->get_sdk_version()
22801
- );
22802
- }
22803
-
22804
- /**
22805
- *
22806
- * @author Leo Fajardo (@leorw)
22807
- * @since 2.0.0
22808
- *
22809
- * @param bool $flush
22810
- *
22811
- * @return FS_Api
22812
- */
22813
- private function get_current_or_network_user_api_scope( $flush = false ) {
22814
- if ( ! $this->_is_network_active ||
22815
- ( isset( $this->_user ) && $this->_user instanceof FS_User )
22816
- ) {
22817
- return $this->get_api_user_scope( $flush );
22818
- }
22819
-
22820
- $user = $this->get_current_or_network_user();
22821
-
22822
- $this->_user_api = FS_Api::instance(
22823
- $this->_module_id,
22824
- 'user',
22825
- $user->id,
22826
- $user->public_key,
22827
- ! $this->is_live(),
22828
- $user->secret_key,
22829
- $this->get_sdk_version()
22830
- );
22831
-
22832
- return $this->_user_api;
22833
- }
22834
-
22835
- private $_site_api;
22836
-
22837
- /**
22838
- *
22839
- * @author Vova Feldman (@svovaf)
22840
- * @since 1.0.2
22841
- *
22842
- * @param bool $flush
22843
- *
22844
- * @return FS_Api
22845
- */
22846
- private function get_api_site_scope( $flush = false ) {
22847
- if ( ! isset( $this->_site_api ) || $flush ) {
22848
- $this->_site_api = FS_Api::instance(
22849
- $this->_module_id,
22850
- 'install',
22851
- $this->_site->id,
22852
- $this->_site->public_key,
22853
- ! $this->is_live(),
22854
- $this->_site->secret_key,
22855
- $this->get_sdk_version()
22856
- );
22857
- }
22858
-
22859
- return $this->_site_api;
22860
- }
22861
-
22862
- private $_plugin_api;
22863
-
22864
- /**
22865
- * Get plugin public API scope.
22866
- *
22867
- * @author Vova Feldman (@svovaf)
22868
- * @since 1.0.7
22869
- *
22870
- * @return FS_Api
22871
- */
22872
- function get_api_plugin_scope() {
22873
- if ( ! isset( $this->_plugin_api ) ) {
22874
- $this->_plugin_api = FS_Api::instance(
22875
- $this->_module_id,
22876
- 'plugin',
22877
- $this->_plugin->id,
22878
- $this->_plugin->public_key,
22879
- ! $this->is_live(),
22880
- false,
22881
- $this->get_sdk_version()
22882
- );
22883
- }
22884
-
22885
- return $this->_plugin_api;
22886
- }
22887
-
22888
- /**
22889
- * Get bundle public API scope.
22890
- *
22891
- * @author Vova Feldman (@svovaf)
22892
- * @since 2.3.1
22893
- *
22894
- * @return FS_Api
22895
- */
22896
- function get_api_bundle_scope() {
22897
- return FS_Api::instance(
22898
- $this->get_bundle_id(),
22899
- 'plugin',
22900
- $this->get_bundle_id(),
22901
- $this->get_bundle_public_key(),
22902
- ! $this->is_live(),
22903
- false,
22904
- $this->get_sdk_version()
22905
- );
22906
- }
22907
-
22908
- /**
22909
- * Get site API scope object (fallback to public plugin scope when not registered).
22910
- *
22911
- * @author Vova Feldman (@svovaf)
22912
- * @since 1.0.7
22913
- *
22914
- * @return FS_Api
22915
- */
22916
- function get_api_site_or_plugin_scope() {
22917
- return $this->is_registered() ?
22918
- $this->get_api_site_scope() :
22919
- $this->get_api_plugin_scope();
22920
- }
22921
-
22922
- /**
22923
- * @author Leo Fajardo (@leorw)
22924
- * @since 2.2.3.1
22925
- *
22926
- * @param object $result
22927
- */
22928
- private function maybe_modify_api_curl_error_message( $result ) {
22929
- if (
22930
- 'cUrlMissing' !== $result->error->type &&
22931
- ( 'CurlException' !== $result->error->type || CURLE_COULDNT_CONNECT != $result->error->code ) &&
22932
- ( 'HttpRequestFailed' !== $result->error->type || false === strpos( $result->error->message, 'cURL error ' . CURLE_COULDNT_CONNECT ) )
22933
- ) {
22934
- return;
22935
- }
22936
-
22937
- $result->error->message = $this->esc_html_inline( 'We use PHP cURL library for the API calls, which is a very common library and usually installed and activated out of the box. Unfortunately, cURL is not activated (or disabled) on your server.', 'curl-missing-message' ) .
22938
- ' ' .
22939
- $this->esc_html_inline(
22940
- sprintf(
22941
- 'Please contact your hosting provider and ask them to whitelist %s for external connection.',
22942
- implode(
22943
- ', ',
22944
- $this->apply_filters( 'api_domains', array(
22945
- 'api.freemius.com',
22946
- 'wp.freemius.com'
22947
- ) )
22948
- )
22949
- ),
22950
- 'connectivity-whitelist'
22951
- ) .
22952
- ' ' .
22953
- sprintf(
22954
- $this->esc_html_inline( 'Once you are done, deactivate the %s and activate it again.', 'connectivity-reactivate-module' ),
22955
- $this->get_module_type()
22956
- );
22957
- }
22958
-
22959
- /**
22960
- * Show trial promotional notice (if any trial exist).
22961
- *
22962
- * @author Vova Feldman (@svovaf)
22963
- * @since 1.0.9
22964
- *
22965
- * @param FS_Plugin_Plan[] $plans
22966
- */
22967
- function _check_for_trial_plans( $plans ) {
22968
- /**
22969
- * 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.
22970
- *
22971
- * @author Vova Feldman (@svovaf)
22972
- * @since 2.1.2
22973
- */
22974
- if ( ! is_array( $plans ) && is_object( $plans ) ) {
22975
- $plans = array( $plans );
22976
- }
22977
-
22978
- if ( ! $this->is_array_instanceof( $plans, 'FS_Plugin_Plan' ) ) {
22979
- $plans = array();
22980
- }
22981
-
22982
- $this->_storage->has_trial_plan = FS_Plan_Manager::instance()->has_trial_plan( $plans );
22983
- }
22984
-
22985
- /**
22986
- * During trial promotion the "upgrade" submenu item turns to
22987
- * "start trial" to encourage the trial. Since we want to keep
22988
- * the same menu item handler and there's no robust way to
22989
- * add new arguments to the menu item link's querystring,
22990
- * use JavaScript to find the menu item and update the href of
22991
- * the link.
22992
- *
22993
- * @author Vova Feldman (@svovaf)
22994
- * @since 1.2.1.5
22995
- */
22996
- function _fix_start_trial_menu_item_url() {
22997
- $template_args = array( 'id' => $this->_module_id );
22998
- fs_require_template( 'add-trial-to-pricing.php', $template_args );
22999
- }
23000
-
23001
- /**
23002
- * Check if module is currently in a trial promotion mode.
23003
- *
23004
- * @author Vova Feldman (@svovaf)
23005
- * @since 1.2.2.7
23006
- *
23007
- * @return bool
23008
- */
23009
- function is_in_trial_promotion() {
23010
- return $this->_admin_notices->has_sticky( 'trial_promotion' );
23011
- }
23012
-
23013
- /**
23014
- * Show trial promotional notice (if any trial exist).
23015
- *
23016
- * @author Vova Feldman (@svovaf)
23017
- * @since 1.0.9
23018
- *
23019
- * @return bool If trial notice added.
23020
- */
23021
- function _add_trial_notice() {
23022
- if ( ! $this->is_user_admin() ) {
23023
- return false;
23024
- }
23025
-
23026
- if ( ! $this->is_user_in_admin() ) {
23027
- return false;
23028
- }
23029
-
23030
- if ( $this->_is_network_active ) {
23031
- if ( fs_is_network_admin() ) {
23032
- // Network level trial is disabled at the moment.
23033
- return false;
23034
- }
23035
-
23036
- if ( ! $this->is_delegated_connection() ) {
23037
- // Only delegated sites should support trials.
23038
- return false;
23039
- }
23040
- }
23041
-
23042
- // Check if trial message is already shown.
23043
- if ( $this->is_in_trial_promotion() ) {
23044
- add_action( 'admin_footer', array( $this, '_fix_start_trial_menu_item_url' ) );
23045
-
23046
- $this->_menu->add_counter_to_menu_item( 1, 'fs-trial' );
23047
-
23048
- return false;
23049
- }
23050
-
23051
- if ( $this->is_premium() && ! WP_FS__DEV_MODE ) {
23052
- // Don't show trial if running the premium code, unless running in DEV mode.
23053
- return false;
23054
- }
23055
-
23056
- if ( ! $this->has_trial_plan() ) {
23057
- // No plans with trial.
23058
- return false;
23059
- }
23060
-
23061
- if ( ! $this->apply_filters( 'show_trial', true ) ) {
23062
- // Developer explicitly asked not to show the trial promo.
23063
- return false;
23064
- }
23065
-
23066
- if ( $this->is_registered() ) {
23067
- // Check if trial already utilized.
23068
- if ( $this->_site->is_trial_utilized() ) {
23069
- return false;
23070
- }
23071
-
23072
- if ( $this->is_paying_or_trial() ) {
23073
- // Don't show trial if paying or already in trial.
23074
- return false;
23075
- }
23076
- }
23077
-
23078
- if ( $this->is_activation_mode() || $this->is_pending_activation() ) {
23079
- // If not yet opted-in/skipped, or pending activation, don't show trial.
23080
- return false;
23081
- }
23082
-
23083
- $last_time_trial_promotion_shown = $this->_storage->get( 'trial_promotion_shown', false );
23084
- $was_promotion_shown_before = ( false !== $last_time_trial_promotion_shown );
23085
-
23086
- // Show promotion if never shown before and 24 hours after initial activation with FS.
23087
- if ( ! $was_promotion_shown_before &&
23088
- $this->_storage->install_timestamp > ( time() - $this->apply_filters( 'show_first_trial_after_n_sec', WP_FS__TIME_24_HOURS_IN_SEC ) )
23089
- ) {
23090
- return false;
23091
- }
23092
-
23093
- // OR if promotion was shown before, try showing it every 30 days.
23094
- if ( $was_promotion_shown_before &&
23095
- $this->apply_filters( 'reshow_trial_after_every_n_sec', 30 * WP_FS__TIME_24_HOURS_IN_SEC ) > time() - $last_time_trial_promotion_shown
23096
- ) {
23097
- return false;
23098
- }
23099
-
23100
- $trial_period = $this->_trial_days;
23101
- $require_payment = $this->_is_trial_require_payment;
23102
- $trial_url = $this->get_trial_url();
23103
- $plans_string = strtolower( $this->get_text_inline( 'Awesome', 'awesome' ) );
23104
-
23105
- if ( $this->is_registered() ) {
23106
- // If opted-in, override trial with up to date data from API.
23107
- $trial_plans = FS_Plan_Manager::instance()->get_trial_plans( $this->_plans );
23108
- $trial_plans_count = count( $trial_plans );
23109
-
23110
- if ( 0 === $trial_plans_count ) {
23111
- // If there's no plans with a trial just exit.
23112
- return false;
23113
- }
23114
-
23115
- /**
23116
- * @var FS_Plugin_Plan $paid_plan
23117
- */
23118
- $paid_plan = $trial_plans[0];
23119
- $require_payment = $paid_plan->is_require_subscription;
23120
- $trial_period = $paid_plan->trial_period;
23121
-
23122
- $total_paid_plans = count( $this->_plans ) - ( FS_Plan_Manager::instance()->has_free_plan( $this->_plans ) ? 1 : 0 );
23123
-
23124
- if ( $total_paid_plans !== $trial_plans_count ) {
23125
- // Not all paid plans have a trial - generate a string of those that have it.
23126
- for ( $i = 0; $i < $trial_plans_count; $i ++ ) {
23127
- $plans_string .= sprintf(
23128
- ' <a href="%s">%s</a>',
23129
- $trial_url,
23130
- $trial_plans[ $i ]->title
23131
- );
23132
-
23133
- if ( $i < $trial_plans_count - 2 ) {
23134
- $plans_string .= ', ';
23135
- } else if ( $i == $trial_plans_count - 2 ) {
23136
- $plans_string .= ' and ';
23137
- }
23138
- }
23139
- }
23140
- }
23141
-
23142
- $message = sprintf(
23143
- $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' ),
23144
- sprintf( '<b>%s</b>', $this->get_plugin_name() ),
23145
- $plans_string,
23146
- $trial_period
23147
- );
23148
-
23149
- // "No Credit-Card Required" or "No Commitment for N Days".
23150
- $cc_string = $require_payment ?
23151
- sprintf( $this->get_text_inline( 'No commitment for %s days - cancel anytime!', 'no-commitment-for-x-days' ), $trial_period ) :
23152
- $this->get_text_inline( 'No credit card required', 'no-cc-required' ) . '!';
23153
-
23154
-
23155
- // Start trial button.
23156
- $button = ' ' . sprintf(
23157
- '<a style="margin-left: 10px; vertical-align: super;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
23158
- $trial_url,
23159
- $this->get_text_x_inline( 'Start free trial', 'call to action', 'start-free-trial' )
23160
- );
23161
-
23162
- $this->_admin_notices->add_sticky(
23163
- $this->apply_filters( 'trial_promotion_message', "{$message} {$cc_string} {$button}" ),
23164
- 'trial_promotion',
23165
- '',
23166
- 'promotion'
23167
- );
23168
-
23169
- $this->_storage->trial_promotion_shown = WP_FS__SCRIPT_START_TIME;
23170
-
23171
- return true;
23172
- }
23173
-
23174
- /**
23175
- * Lets users/customers know that the product has an affiliate program.
23176
- *
23177
- * @author Leo Fajardo (@leorw)
23178
- * @since 1.2.2.11
23179
- *
23180
- * @return bool Returns true if the notice has been added.
23181
- */
23182
- function _add_affiliate_program_notice() {
23183
- if ( ! $this->is_user_admin() ) {
23184
- return false;
23185
- }
23186
-
23187
- if ( ! $this->is_user_in_admin() ) {
23188
- return false;
23189
- }
23190
-
23191
- // Check if the notice is already shown.
23192
- if ( $this->_admin_notices->has_sticky( 'affiliate_program' ) ) {
23193
- return false;
23194
- }
23195
-
23196
- if (
23197
- // Product has no affiliate program.
23198
- ! $this->has_affiliate_program() ||
23199
- // User has applied for an affiliate account.
23200
- ! empty( $this->_storage->affiliate_application_data )
23201
- ) {
23202
- return false;
23203
- }
23204
-
23205
- if ( ! $this->apply_filters( 'show_affiliate_program_notice', true ) ) {
23206
- // Developer explicitly asked not to show the notice about the affiliate program.
23207
- return false;
23208
- }
23209
-
23210
- if ( $this->is_activation_mode() || $this->is_pending_activation() ) {
23211
- // If not yet opted in/skipped, or pending activation, don't show the notice.
23212
- return false;
23213
- }
23214
-
23215
- $last_time_notice_was_shown = $this->_storage->get( 'affiliate_program_notice_shown', false );
23216
- $was_notice_shown_before = ( false !== $last_time_notice_was_shown );
23217
-
23218
- /**
23219
- * Do not show the notice if it was already shown before or less than 30 days have passed since the initial
23220
- * activation with FS.
23221
- */
23222
- if ( $was_notice_shown_before ||
23223
- $this->_storage->install_timestamp > ( time() - ( WP_FS__TIME_24_HOURS_IN_SEC * 30 ) )
23224
- ) {
23225
- return false;
23226
- }
23227
-
23228
- if ( ! $this->is_paying() &&
23229
- FS_Plugin::AFFILIATE_MODERATION_CUSTOMERS == $this->_plugin->affiliate_moderation
23230
- ) {
23231
- // If the user is not a customer and the affiliate program is only for customers, don't show the notice.
23232
- return false;
23233
- }
23234
-
23235
- $message = sprintf(
23236
- $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' ),
23237
- sprintf( '<strong>%s</strong>', $this->get_plugin_name() ),
23238
- $this->get_module_label( true )
23239
- );
23240
-
23241
- // HTML code for the "Learn more..." button.
23242
- $button = ' ' . sprintf(
23243
- '<a style="display: block; margin-top: 10px;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
23244
- $this->_get_admin_page_url( 'affiliation' ),
23245
- $this->get_text_inline( 'Learn more', 'learn-more' ) . '...'
23246
- );
23247
-
23248
- $this->_admin_notices->add_sticky(
23249
- $this->apply_filters( 'affiliate_program_notice', "{$message} {$button}" ),
23250
- 'affiliate_program',
23251
- '',
23252
- 'promotion'
23253
- );
23254
-
23255
- $this->_storage->affiliate_program_notice_shown = WP_FS__SCRIPT_START_TIME;
23256
-
23257
- return true;
23258
- }
23259
-
23260
- /**
23261
- * @author Vova Feldman (@svovaf)
23262
- * @since 1.2.1.5
23263
- */
23264
- function _enqueue_common_css() {
23265
- if ( $this->has_paid_plan() && ! $this->is_paying() ) {
23266
- // Add basic CSS for admin-notices and menu-item colors.
23267
- fs_enqueue_local_style( 'fs_common', '/admin/common.css' );
23268
- }
23269
- }
23270
-
23271
- /**
23272
- * @author Leo Fajardo (@leorw)
23273
- * @since 1.2.2
23274
- */
23275
- function _show_theme_activation_optin_dialog() {
23276
- fs_enqueue_local_style( 'fs_connect', '/admin/connect.css' );
23277
-
23278
- add_action( 'admin_footer', array( $this, '_add_fs_theme_activation_dialog' ) );
23279
- }
23280
-
23281
- /**
23282
- * @author Leo Fajardo (@leorw)
23283
- * @since 1.2.2
23284
- */
23285
- function _add_fs_theme_activation_dialog() {
23286
- global $pagenow;
23287
-
23288
- if ( 'themes.php' !== $pagenow ) {
23289
- return;
23290
- }
23291
-
23292
- $vars = array( 'id' => $this->_module_id );
23293
- fs_require_once_template( 'connect.php', $vars );
23294
- }
23295
-
23296
- /* Action Links
23297
- ------------------------------------------------------------------------------------------------------------------*/
23298
- private $_action_links_hooked = false;
23299
- private $_action_links = array();
23300
-
23301
- /**
23302
- * Hook to plugin action links filter.
23303
- *
23304
- * @author Vova Feldman (@svovaf)
23305
- * @since 1.0.0
23306
- */
23307
- private function hook_plugin_action_links() {
23308
- $this->_logger->entrance();
23309
-
23310
- $this->_action_links_hooked = true;
23311
-
23312
- $this->_logger->log( 'Adding action links hooks.' );
23313
-
23314
- // Add action link to settings page.
23315
- add_filter( 'plugin_action_links_' . $this->_plugin_basename, array(
23316
- $this,
23317
- '_modify_plugin_action_links_hook'
23318
- ), WP_FS__DEFAULT_PRIORITY, 2 );
23319
- add_filter( 'network_admin_plugin_action_links_' . $this->_plugin_basename, array(
23320
- $this,
23321
- '_modify_plugin_action_links_hook'
23322
- ), WP_FS__DEFAULT_PRIORITY, 2 );
23323
- }
23324
-
23325
- /**
23326
- * Add plugin action link.
23327
- *
23328
- * @author Vova Feldman (@svovaf)
23329
- * @since 1.0.0
23330
- *
23331
- * @param $label
23332
- * @param $url
23333
- * @param bool $external
23334
- * @param int $priority
23335
- * @param bool $key
23336
- */
23337
- function add_plugin_action_link( $label, $url, $external = false, $priority = WP_FS__DEFAULT_PRIORITY, $key = false ) {
23338
- $this->_logger->entrance();
23339
-
23340
- if ( ! isset( $this->_action_links[ $priority ] ) ) {
23341
- $this->_action_links[ $priority ] = array();
23342
- }
23343
-
23344
- if ( false === $key ) {
23345
- $key = preg_replace( "/[^A-Za-z0-9 ]/", '', strtolower( $label ) );
23346
- }
23347
-
23348
- $this->_action_links[ $priority ][] = array(
23349
- 'label' => $label,
23350
- 'href' => $url,
23351
- 'key' => $key,
23352
- 'external' => $external
23353
- );
23354
- }
23355
-
23356
- /**
23357
- * Adds Upgrade and Add-Ons links to the main Plugins page link actions collection.
23358
- *
23359
- * @author Vova Feldman (@svovaf)
23360
- * @since 1.0.0
23361
- */
23362
- function _add_upgrade_action_link() {
23363
- $this->_logger->entrance();
23364
-
23365
- $is_activation_mode = $this->is_activation_mode();
23366
-
23367
- $add_action_links = $this->should_add_submenu_or_action_links( $is_activation_mode );
23368
-
23369
- /**
23370
- * The following logic is based on the logic in `add_submenu_items()` method that decides when the "Upgrade"
23371
- * and "Add-Ons" menus should be added.
23372
- *
23373
- * @author Leo Fajardo (@leorw)
23374
- * @since 2.3.0
23375
- */
23376
- $add_upgrade_link = (
23377
- $add_action_links ||
23378
- ( $is_activation_mode && $this->is_only_premium() )
23379
- ) && ! WP_FS__DEMO_MODE && ( ! $this->is_whitelabeled() );
23380
-
23381
- $add_addons_link = ( $add_action_links && $this->has_addons() );
23382
-
23383
- if ( ! $add_upgrade_link && ! $add_addons_link ) {
23384
- return;
23385
- }
23386
-
23387
- if (
23388
- $add_upgrade_link &&
23389
- $this->is_pricing_page_visible() &&
23390
- $this->is_submenu_item_visible( 'pricing' )
23391
- ) {
23392
- $this->add_plugin_action_link(
23393
- $this->get_text_inline( 'Upgrade', 'upgrade' ),
23394
- $this->get_upgrade_url(),
23395
- false,
23396
- 7,
23397
- 'upgrade'
23398
- );
23399
- }
23400
-
23401
- if (
23402
- $add_addons_link &&
23403
- $this->has_addons() &&
23404
- $this->is_submenu_item_visible( 'addons' )
23405
- ) {
23406
- $this->add_plugin_action_link(
23407
- $this->get_text_inline( 'Add-Ons', 'add-ons' ),
23408
- $this->_get_admin_page_url( 'addons' ),
23409
- false,
23410
- 9,
23411
- 'addons'
23412
- );
23413
- }
23414
- }
23415
-
23416
- /**
23417
- * Adds "Activate License" or "Change License" link to the main Plugins page link actions collection.
23418
- *
23419
- * @author Leo Fajardo (@leorw)
23420
- * @since 1.1.9
23421
- */
23422
- function _add_license_action_link() {
23423
- $this->_logger->entrance();
23424
-
23425
- if ( ! self::is_ajax() ) {
23426
- // Inject license activation dialog UI and client side code.
23427
- add_action( 'admin_footer', array( $this, '_add_license_activation_dialog_box' ) );
23428
- }
23429
-
23430
- $link_text = $this->is_free_plan() ?
23431
- $this->get_text_inline( 'Activate License', 'activate-license' ) :
23432
- $this->get_text_inline( 'Change License', 'change-license' );
23433
-
23434
- $this->add_plugin_action_link(
23435
- $link_text,
23436
- '#',
23437
- false,
23438
- 11,
23439
- ( 'activate-license ' . $this->get_unique_affix() )
23440
- );
23441
- }
23442
-
23443
- /**
23444
- * @author Leo Fajardo (@leorw)
23445
- * @since 2.0.2
23446
- */
23447
- function _add_premium_version_upgrade_selection_action() {
23448
- $this->_logger->entrance();
23449
-
23450
- if ( ! self::is_ajax() ) {
23451
- add_action( 'admin_footer', array( $this, '_add_premium_version_upgrade_selection_dialog_box' ) );
23452
- }
23453
- }
23454
-
23455
- /**
23456
- * Adds "Opt In" or "Opt Out" link to the main "Plugins" page link actions collection.
23457
- *
23458
- * @author Leo Fajardo (@leorw)
23459
- * @since 1.2.1.5
23460
- */
23461
- function _add_tracking_links() {
23462
- if ( ! current_user_can( 'manage_options' ) ) {
23463
- return;
23464
- }
23465
-
23466
- $this->_logger->entrance();
23467
-
23468
- /**
23469
- * @author Vova Feldman (@svovaf)
23470
- * @since 2.3.2 Allow opting out from usage-tracking for paid products too by giving the appropriate warning letting the user know the automatic updates mechanism cannot function without an ongoing connection to the licensing and updates engine.
23471
- */
23472
- /*if ( $this->is_premium() ) {
23473
- // Don't add opt-in/out for premium code base.
23474
- return;
23475
- }*/
23476
-
23477
- if ( $this->is_only_premium() && $this->is_free_plan() ) {
23478
- // Don't add tracking links for premium-only products that were opted-in by relation (add-on or a parent product) before activating any license.
23479
- return;
23480
- }
23481
-
23482
- if (
23483
- $this->is_addon() &&
23484
- ! $this->is_only_premium() &&
23485
- $this->_parent->is_anonymous()
23486
- ) {
23487
- return;
23488
- }
23489
-
23490
- if ( fs_is_network_admin() ) {
23491
- if ( ! $this->_is_network_active ) {
23492
- // Don't add tracking links when browsing the network WP Admin and the plugin is not network active.
23493
- return;
23494
- } else if ( $this->is_network_delegated_connection() ) {
23495
- // Don't add tracking links when browsing the network WP Admin and the activation has been delegated to site admins.
23496
- return;
23497
- }
23498
- } else {
23499
- if ( $this->_is_network_active && ! $this->is_delegated_connection() ) {
23500
- // Don't add tracking links when browsing the sub-site WP Admin, the plugin is network active, and the connection was not delegated.
23501
- return;
23502
- }
23503
- }
23504
-
23505
- if ( fs_request_is_action_secure( $this->get_unique_affix() . '_reconnect' ) ) {
23506
- if ( ! $this->is_registered() && $this->is_anonymous() ) {
23507
- $this->connect_again();
23508
-
23509
- return;
23510
- }
23511
- }
23512
-
23513
- if ( ( $this->is_plugin() && ! self::is_plugins_page() ) ||
23514
- ( $this->is_theme() && ! self::is_themes_page() )
23515
- ) {
23516
- // Only show tracking links on the plugins and themes pages.
23517
- return;
23518
- }
23519
-
23520
- if (
23521
- $this->is_activation_mode() &&
23522
- $this->is_premium() &&
23523
- ! $t