| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPIDE\App\Classes; |
| 4 |
|
| 5 |
use Freemius_Api; |
| 6 |
use WPIDE\App\App; |
| 7 |
use const WPIDE\Constants\ASSETS_DIR; |
| 8 |
use const WPIDE\Constants\ASSETS_URL; |
| 9 |
use const WPIDE\Constants\AUTHOR; |
| 10 |
use const WPIDE\Constants\DIR; |
| 11 |
use const WPIDE\Constants\FS_ID; |
| 12 |
use const WPIDE\Constants\FS_KEY; |
| 13 |
use const WPIDE\Constants\GTM_ID; |
| 14 |
use const WPIDE\Constants\NAME; |
| 15 |
use const WPIDE\Constants\SLUG; |
| 16 |
use const WPIDE\Constants\VERSION; |
| 17 |
class Freemius { |
| 18 |
public static $fs; |
| 19 |
|
| 20 |
public static $api; |
| 21 |
|
| 22 |
public static $loaded = false; |
| 23 |
|
| 24 |
/** |
| 25 |
* @throws \Freemius_Exception |
| 26 |
*/ |
| 27 |
public static function init() { |
| 28 |
if ( !isset( self::$fs ) ) { |
| 29 |
if ( !defined( 'WP_FS__PRODUCT_' . FS_ID . '_MULTISITE' ) ) { |
| 30 |
define( 'WP_FS__PRODUCT_' . FS_ID . '_MULTISITE', true ); |
| 31 |
} |
| 32 |
// Init Freemius SDK. |
| 33 |
require_once DIR . 'freemius/start.php'; |
| 34 |
self::$fs = fs_dynamic_init( array( |
| 35 |
'id' => FS_ID, |
| 36 |
'slug' => SLUG, |
| 37 |
'premium_slug' => SLUG . '-pro', |
| 38 |
'type' => 'plugin', |
| 39 |
'public_key' => FS_KEY, |
| 40 |
'is_premium' => false, |
| 41 |
'premium_suffix' => 'Pro', |
| 42 |
'has_addons' => false, |
| 43 |
'has_paid_plans' => true, |
| 44 |
'trial' => array( |
| 45 |
'days' => 14, |
| 46 |
'is_require_payment' => true, |
| 47 |
), |
| 48 |
'menu' => array( |
| 49 |
'slug' => SLUG, |
| 50 |
'support' => false, |
| 51 |
'affiliation' => false, |
| 52 |
'network' => true, |
| 53 |
), |
| 54 |
'is_live' => true, |
| 55 |
'is_org_compliant' => true, |
| 56 |
) ); |
| 57 |
$GLOBALS['wpide_fs'] = self::$fs; |
| 58 |
self::$fs->add_action( 'connect/before', [__CLASS__, 'beforeConnectBox'] ); |
| 59 |
self::$fs->add_action( 'connect/after', [__CLASS__, 'afterConnectBox'] ); |
| 60 |
self::$fs->add_filter( 'checkout/purchaseCompleted', [__CLASS__, 'afterPurchaseJs'] ); |
| 61 |
self::$fs->add_filter( 'templates/checkout.php', [__CLASS__, 'checkoutGtmScript'] ); |
| 62 |
self::$fs->add_filter( 'freemius_pricing_js_path', [__CLASS__, 'pricingJsPath'] ); |
| 63 |
self::$fs->add_filter( 'plugin_icon', [__CLASS__, 'pluginIcon'] ); |
| 64 |
self::$fs->add_filter( 'hide_account_tabs', '__return_true' ); |
| 65 |
self::$fs->add_filter( 'hide_freemius_powered_by', '__return_true' ); |
| 66 |
self::$fs->add_filter( 'hide_billing_and_payments_info', '__return_true' ); |
| 67 |
self::$fs->add_action( 'plugins_loaded', [__CLASS__, 'override_freemius_strings'] ); |
| 68 |
add_action( 'admin_enqueue_scripts', [__CLASS__, 'admin_enqueue_scripts'] ); |
| 69 |
self::$loaded = true; |
| 70 |
// Signal that SDK was initiated. |
| 71 |
do_action( 'wpide_fs_loaded' ); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
public static function admin_enqueue_scripts() { |
| 76 |
if ( App::instance()->isFreemiusScreen() ) { |
| 77 |
self::freemius_custom_styles(); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
public static function freemius_custom_styles() { |
| 82 |
wp_enqueue_style( |
| 83 |
SLUG . '-fs-connect', |
| 84 |
ASSETS_URL . 'css/freemius/freemius.css', |
| 85 |
[], |
| 86 |
VERSION |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
public static function beforeConnectBox() { |
| 91 |
wp_enqueue_style( |
| 92 |
SLUG . '-fs-connect', |
| 93 |
ASSETS_URL . 'css/freemius/freemius.css', |
| 94 |
[], |
| 95 |
VERSION |
| 96 |
); |
| 97 |
echo '<div id="fs-connect-wrap">'; |
| 98 |
} |
| 99 |
|
| 100 |
public static function afterConnectBox() { |
| 101 |
echo '</div>'; |
| 102 |
} |
| 103 |
|
| 104 |
public static function afterPurchaseJs() : string { |
| 105 |
return 'function ( data ) { |
| 106 |
|
| 107 |
/** |
| 108 |
* Since the user just entered their personal & billing information, agreed to the TOS & privacy, |
| 109 |
* know they are running within a secure iframe from an external domain, they implicitly permit tracking |
| 110 |
* this purchase. So initializing GTM here (after the purchase), is legitimate. |
| 111 |
*/ |
| 112 |
|
| 113 |
var is_subscription = typeof(data.purchase.initial_amount) !== "undefined"; |
| 114 |
var is_trial = typeof(data.purchase.trial_ends) !== "undefined" && data.purchase.trial_ends !== null; |
| 115 |
var total = is_subscription ? data.purchase.initial_amount : data.purchase.gross; |
| 116 |
total = is_trial ? 0 : total; |
| 117 |
|
| 118 |
var coupon = data && data.coupon ? data.coupon.code : ""; |
| 119 |
var currency = data && data.currency ? data.currency.toUpperCase() : "USD"; |
| 120 |
|
| 121 |
var item = { |
| 122 |
item_name: "' . NAME . '", |
| 123 |
item_id: ' . FS_ID . ', |
| 124 |
item_brand: "' . AUTHOR . '", |
| 125 |
affiliation: "' . AUTHOR . '", |
| 126 |
price: data.total, |
| 127 |
discount: 0, |
| 128 |
currency: currency, |
| 129 |
coupon: coupon, |
| 130 |
index: 1, |
| 131 |
quantity: 1 |
| 132 |
}; |
| 133 |
|
| 134 |
// Purchase Event |
| 135 |
dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. |
| 136 |
dataLayer.push({ |
| 137 |
event: "purchase", |
| 138 |
ecommerce: { |
| 139 |
transaction_id: data.purchase.id, |
| 140 |
affiliation: item.affiliation, |
| 141 |
tax: 0, |
| 142 |
shipping: 0, |
| 143 |
items: [item], |
| 144 |
currency: item.currency, |
| 145 |
coupon: item.coupon, |
| 146 |
value: total |
| 147 |
} |
| 148 |
}); |
| 149 |
}'; |
| 150 |
} |
| 151 |
|
| 152 |
public static function checkoutGtmScript( $html ) : string { |
| 153 |
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; |
| 154 |
} |
| 155 |
|
| 156 |
public static function pricingJsPath() : string { |
| 157 |
return ASSETS_DIR . 'pricing/freemius-pricing.js'; |
| 158 |
} |
| 159 |
|
| 160 |
public static function pluginIcon() : string { |
| 161 |
return ASSETS_DIR . 'img/icon.png'; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Add license dialog boxes |
| 166 |
* |
| 167 |
* @since 1.0.0 |
| 168 |
*/ |
| 169 |
public static function addLicenseActivationDialogBox() { |
| 170 |
if ( !did_action( SLUG . '_license_activation_dialog_added' ) && !App::instance()->isFreemiusScreen( 'account' ) ) { |
| 171 |
self::sdk()->_add_license_activation_dialog_box(); |
| 172 |
fs_enqueue_local_style( 'fs_dialog_boxes', '/admin/dialog-boxes.css' ); |
| 173 |
do_action( SLUG . '_license_activation_dialog_added' ); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
public static function showSubmenus() : bool { |
| 178 |
$is_activation_mode = self::sdk()->is_activation_mode(); |
| 179 |
if ( $is_activation_mode ) { |
| 180 |
$show_submenus = false; |
| 181 |
} else { |
| 182 |
if ( fs_is_network_admin() ) { |
| 183 |
/** |
| 184 |
* Add submenu items or action links to network level when plugin was network activated and the super |
| 185 |
* admin did NOT delegate the connection of all sites to site admins. |
| 186 |
*/ |
| 187 |
$show_submenus = App::instance()->isNetworkActive() && (WP_FS__SHOW_NETWORK_EVEN_WHEN_DELEGATED || !self::sdk()->is_network_delegated_connection()); |
| 188 |
} else { |
| 189 |
$show_submenus = !App::instance()->isNetworkActive() || self::sdk()->is_delegated_connection(); |
| 190 |
} |
| 191 |
} |
| 192 |
return $show_submenus; |
| 193 |
} |
| 194 |
|
| 195 |
public static function showUpgradeLink() : bool { |
| 196 |
$show_submenus = self::showSubmenus(); |
| 197 |
$is_activation_mode = self::sdk()->is_activation_mode(); |
| 198 |
$add_upgrade_link = ($show_submenus || $is_activation_mode && self::sdk()->is_only_premium()) && !self::sdk()->is_whitelabeled(); |
| 199 |
return $add_upgrade_link && self::sdk()->is_pricing_page_visible() && self::sdk()->is_submenu_item_visible( 'pricing' ); |
| 200 |
} |
| 201 |
|
| 202 |
public static function showTrialLink() : bool { |
| 203 |
return self::sdk()->apply_filters( 'show_trial', true ) && self::sdk()->has_trial_plan() && !self::sdk()->is_trial_utilized(); |
| 204 |
} |
| 205 |
|
| 206 |
public static function sdk() : \Freemius { |
| 207 |
return self::$fs; |
| 208 |
} |
| 209 |
|
| 210 |
public static function loaded() : bool { |
| 211 |
return self::$loaded; |
| 212 |
} |
| 213 |
|
| 214 |
public static function override_freemius_strings() : void { |
| 215 |
self::$fs->override_i18n( array( |
| 216 |
'contact-us' => __( 'Support', 'wpide' ), |
| 217 |
) ); |
| 218 |
} |
| 219 |
|
| 220 |
} |
| 221 |
|