| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPIDE\App\Classes; |
| 4 |
|
| 5 |
use WPIDE\App\App ; |
| 6 |
use const WPIDE\Constants\ASSETS_DIR ; |
| 7 |
use const WPIDE\Constants\ASSETS_URL ; |
| 8 |
use const WPIDE\Constants\AUTHOR ; |
| 9 |
use const WPIDE\Constants\DIR ; |
| 10 |
use const WPIDE\Constants\FS_ID ; |
| 11 |
use const WPIDE\Constants\FS_KEY ; |
| 12 |
use const WPIDE\Constants\GTM_ID ; |
| 13 |
use const WPIDE\Constants\NAME ; |
| 14 |
use const WPIDE\Constants\SLUG ; |
| 15 |
use const WPIDE\Constants\VERSION ; |
| 16 |
class Freemius |
| 17 |
{ |
| 18 |
public static $fs ; |
| 19 |
public static $loaded = false ; |
| 20 |
/** |
| 21 |
* @throws \Freemius_Exception |
| 22 |
*/ |
| 23 |
public static function init() |
| 24 |
{ |
| 25 |
|
| 26 |
if ( !isset( self::$fs ) ) { |
| 27 |
// Init Freemius SDK. |
| 28 |
require_once DIR . 'freemius/start.php'; |
| 29 |
self::$fs = fs_dynamic_init( array( |
| 30 |
'id' => FS_ID, |
| 31 |
'slug' => SLUG, |
| 32 |
'premium_slug' => SLUG . '-pro', |
| 33 |
'type' => 'plugin', |
| 34 |
'public_key' => FS_KEY, |
| 35 |
'is_premium' => false, |
| 36 |
'premium_suffix' => 'Pro', |
| 37 |
'has_addons' => false, |
| 38 |
'has_paid_plans' => true, |
| 39 |
'trial' => array( |
| 40 |
'days' => 14, |
| 41 |
'is_require_payment' => true, |
| 42 |
), |
| 43 |
'menu' => array( |
| 44 |
'slug' => SLUG, |
| 45 |
), |
| 46 |
'is_live' => true, |
| 47 |
) ); |
| 48 |
self::$fs->add_action( 'connect/before', [ __CLASS__, 'beforeConnectBox' ] ); |
| 49 |
self::$fs->add_action( 'connect/after', [ __CLASS__, 'afterConnectBox' ] ); |
| 50 |
self::$fs->add_filter( 'checkout/purchaseCompleted', [ __CLASS__, 'afterPurchaseJs' ] ); |
| 51 |
self::$fs->add_filter( 'templates/checkout.php', [ __CLASS__, 'checkoutGtmScript' ] ); |
| 52 |
self::$fs->add_filter( 'freemius_pricing_js_path', [ __CLASS__, 'pricingJsPath' ] ); |
| 53 |
self::$fs->add_filter( 'plugin_icon', [ __CLASS__, 'pluginIcon' ] ); |
| 54 |
self::$fs->add_filter( 'hide_account_tabs', '__return_true' ); |
| 55 |
self::$fs->add_filter( 'hide_freemius_powered_by', '__return_true' ); |
| 56 |
self::$fs->add_filter( 'hide_billing_and_payments_info', '__return_true' ); |
| 57 |
add_action( 'admin_enqueue_scripts', [ __CLASS__, 'admin_enqueue_scripts' ] ); |
| 58 |
self::$loaded = true; |
| 59 |
// Signal that SDK was initiated. |
| 60 |
do_action( 'wpide_fs_loaded' ); |
| 61 |
} |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
public static function admin_enqueue_scripts() |
| 66 |
{ |
| 67 |
if ( App::instance()->isFreemiusScreen() ) { |
| 68 |
self::freemius_custom_styles(); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
public static function freemius_custom_styles() |
| 73 |
{ |
| 74 |
wp_enqueue_style( |
| 75 |
SLUG . '-fs-connect', |
| 76 |
ASSETS_URL . 'css/freemius/freemius.css', |
| 77 |
[], |
| 78 |
VERSION |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
public static function beforeConnectBox() |
| 83 |
{ |
| 84 |
wp_enqueue_style( |
| 85 |
SLUG . '-fs-connect', |
| 86 |
ASSETS_URL . 'css/freemius/freemius.css', |
| 87 |
[], |
| 88 |
VERSION |
| 89 |
); |
| 90 |
echo '<div id="fs-connect-wrap">' ; |
| 91 |
} |
| 92 |
|
| 93 |
public static function afterConnectBox() |
| 94 |
{ |
| 95 |
echo '</div>' ; |
| 96 |
} |
| 97 |
|
| 98 |
public static function afterPurchaseJs() : string |
| 99 |
{ |
| 100 |
return 'function ( data ) { |
| 101 |
|
| 102 |
/** |
| 103 |
* Since the user just entered their personal & billing information, agreed to the TOS & privacy, |
| 104 |
* know they are running within a secure iframe from an external domain, they implicitly permit tracking |
| 105 |
* this purchase. So initializing GTM here (after the purchase), is legitimate. |
| 106 |
*/ |
| 107 |
|
| 108 |
var is_subscription = typeof(data.purchase.initial_amount) !== "undefined"; |
| 109 |
var is_trial = typeof(data.purchase.trial_ends) !== "undefined" && data.purchase.trial_ends !== null; |
| 110 |
var total = is_subscription ? data.purchase.initial_amount : data.purchase.gross; |
| 111 |
total = is_trial ? 0 : total; |
| 112 |
|
| 113 |
var coupon = data && data.coupon ? data.coupon.code : ""; |
| 114 |
var currency = data && data.currency ? data.currency.toUpperCase() : "USD"; |
| 115 |
|
| 116 |
var item = { |
| 117 |
item_name: "' . NAME . '", |
| 118 |
item_id: ' . FS_ID . ', |
| 119 |
item_brand: "' . AUTHOR . '", |
| 120 |
affiliation: "' . AUTHOR . '", |
| 121 |
price: data.total, |
| 122 |
discount: 0, |
| 123 |
currency: currency, |
| 124 |
coupon: coupon, |
| 125 |
index: 1, |
| 126 |
quantity: 1 |
| 127 |
}; |
| 128 |
|
| 129 |
// Purchase Event |
| 130 |
dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. |
| 131 |
dataLayer.push({ |
| 132 |
event: "purchase", |
| 133 |
ecommerce: { |
| 134 |
transaction_id: data.purchase.id, |
| 135 |
affiliation: item.affiliation, |
| 136 |
tax: 0, |
| 137 |
shipping: 0, |
| 138 |
items: [item], |
| 139 |
currency: item.currency, |
| 140 |
coupon: item.coupon, |
| 141 |
value: total |
| 142 |
} |
| 143 |
}); |
| 144 |
}'; |
| 145 |
} |
| 146 |
|
| 147 |
public static function checkoutGtmScript( $html ) : string |
| 148 |
{ |
| 149 |
return "\n <script>window.dataLayer = window.dataLayer || [];</script>\n <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':\n new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],\n j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=\n 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);\n })(window,document,'script','dataLayer', '" . GTM_ID . "');</script>\n " . $html; |
| 150 |
} |
| 151 |
|
| 152 |
public static function pricingJsPath() : string |
| 153 |
{ |
| 154 |
return ASSETS_DIR . 'pricing/freemius-pricing.js'; |
| 155 |
} |
| 156 |
|
| 157 |
public static function pluginIcon() : string |
| 158 |
{ |
| 159 |
return ASSETS_DIR . 'img/icon.png'; |
| 160 |
} |
| 161 |
|
| 162 |
public static function sdk() |
| 163 |
{ |
| 164 |
return self::$fs; |
| 165 |
} |
| 166 |
|
| 167 |
public static function loaded() |
| 168 |
{ |
| 169 |
return self::$loaded; |
| 170 |
} |
| 171 |
|
| 172 |
} |