| 1 |
<?php |
| 2 |
// TODO refactoring |
| 3 |
namespace Bookit\Helpers; |
| 4 |
|
| 5 |
use Bookit\Classes\Base\Plugin; |
| 6 |
use Exception; |
| 7 |
/** |
| 8 |
* Bookit Freemius Helper |
| 9 |
*/ |
| 10 |
|
| 11 |
class FreemiusHelper { |
| 12 |
|
| 13 |
private const STM_FREEMIUS_PATH = 'https://bookitwp.com/api-addons/'; |
| 14 |
private const STM_BOOKIT_ADDONS = [ 'bookit-pro' ]; |
| 15 |
private const STM_FREEMIUS_CHECKOUT_LINK = 'https://checkout.freemius.com/mode/dialog/plugin/'; |
| 16 |
private const STM_CHECKOUT_URL = 'https://bookitwp.com/bookit-pro/'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Return data for all bookit addons |
| 20 |
* @return array |
| 21 |
*/ |
| 22 |
public static function get_freemius_info() { |
| 23 |
|
| 24 |
$result = array(); |
| 25 |
foreach ( self::STM_BOOKIT_ADDONS as $addon ) { |
| 26 |
$result[ $addon ] = array(); |
| 27 |
|
| 28 |
$response = wp_remote_get( self::STM_FREEMIUS_PATH . $addon . '.json' ); |
| 29 |
$body = wp_remote_retrieve_body( $response ); |
| 30 |
$body = json_decode( $body ); |
| 31 |
if ( empty( $body ) ) { |
| 32 |
continue; |
| 33 |
} |
| 34 |
|
| 35 |
$result[ $addon ]['title'] = $body->title; |
| 36 |
|
| 37 |
if ( isset( $body->plans ) && ! empty( $body->plans ) ) { |
| 38 |
$result[ $addon ]['plan'] = self::set_premium_plan_prices( $body->plans, $body->id ); |
| 39 |
} |
| 40 |
|
| 41 |
if ( isset( $body->latest ) && ! empty( $body->latest ) ) { |
| 42 |
$result[ $addon ]['latest'] = self::set_latest_info( $body->latest ); |
| 43 |
} |
| 44 |
|
| 45 |
if ( isset( $body->info ) && ! empty( $body->info ) ) { |
| 46 |
$result[ $addon ]['info'] = $body->info; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
return $result; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Return plugin id by name |
| 55 |
* @return integer |
| 56 |
*/ |
| 57 |
public static function get_addon_id_by_name( $addonName ) { |
| 58 |
|
| 59 |
$response = wp_remote_get( self::STM_FREEMIUS_PATH . $addonName . '.json' ); |
| 60 |
|
| 61 |
$body = wp_remote_retrieve_body( $response ); |
| 62 |
$body = json_decode( $body ); |
| 63 |
if ( ! empty( $body ) && isset( $body->id ) && ! empty( $body->id ) ) { |
| 64 |
return (int) $body->id; |
| 65 |
} |
| 66 |
|
| 67 |
return null; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Return data for bookit addon by name |
| 72 |
* @return array |
| 73 |
*/ |
| 74 |
public static function get_addon_info( $addonName ) { |
| 75 |
|
| 76 |
$result = array(); |
| 77 |
$response = wp_remote_get( self::STM_FREEMIUS_PATH . $addonName . '.json' ); |
| 78 |
|
| 79 |
$body = wp_remote_retrieve_body( $response ); |
| 80 |
$body = json_decode( $body ); |
| 81 |
if ( empty( $body ) ) { |
| 82 |
return $result; |
| 83 |
} |
| 84 |
|
| 85 |
$result['title'] = $body->title; |
| 86 |
$result['descriptions'] = self::addon_descriptions( $addonName ); |
| 87 |
|
| 88 |
if ( isset( $body->plans ) && ! empty( $body->plans ) ) { |
| 89 |
$result['plan'] = self::set_premium_plan_prices( $body->plans, $body->id ); |
| 90 |
} |
| 91 |
|
| 92 |
if ( isset( $body->latest ) && ! empty( $body->latest ) ) { |
| 93 |
$result['latest'] = self::set_latest_info( $body->latest ); |
| 94 |
} |
| 95 |
|
| 96 |
if ( isset( $body->info ) && ! empty( $body->info ) ) { |
| 97 |
$result['info'] = $body->info; |
| 98 |
} |
| 99 |
|
| 100 |
return $result; |
| 101 |
} |
| 102 |
|
| 103 |
protected static function addon_descriptions( $addonName ) { |
| 104 |
$descriptions = array( |
| 105 |
'bookit-pro' => __( 'Let your customers select WooCommerce platform and pay for meetings with ease. Merge your Bookit calendar and Google Calendar with just one click. Now easy to book and schedule appointments.', 'bookit' ), |
| 106 |
); |
| 107 |
|
| 108 |
return array_key_exists( $addonName, $descriptions ) ? $descriptions[ $addonName ] : ''; |
| 109 |
} |
| 110 |
|
| 111 |
public static function get_license( $plugin_id ) { |
| 112 |
$result = false; |
| 113 |
|
| 114 |
if ( ! function_exists( 'bookit_fs' ) ) { |
| 115 |
return $result; |
| 116 |
} |
| 117 |
|
| 118 |
$bookit_fs = bookit_fs(); |
| 119 |
$addon = $bookit_fs->get_addon( $plugin_id ); |
| 120 |
if ( false == $addon ) { |
| 121 |
return $result; |
| 122 |
} |
| 123 |
|
| 124 |
if ( ! $bookit_fs->is_addon_installed( $plugin_id ) || ! $bookit_fs->is_addon_activated( $plugin_id ) ) { |
| 125 |
return $result; |
| 126 |
} |
| 127 |
|
| 128 |
$addonInstance = $bookit_fs->get_addon_instance( $plugin_id ); |
| 129 |
|
| 130 |
return $addonInstance->_get_license(); |
| 131 |
} |
| 132 |
|
| 133 |
protected static function set_premium_plan_prices( $plans, $plugin_id ) { |
| 134 |
$plan_info = array(); |
| 135 |
|
| 136 |
$plan_data = array( |
| 137 |
'1' => array( |
| 138 |
'text' => __( 'Single Site', 'bookit' ), |
| 139 |
'classname' => '', |
| 140 |
'type' => '', |
| 141 |
), |
| 142 |
'5' => array( |
| 143 |
'classname' => 'stm_plan--popular', |
| 144 |
'text' => __( 'Up to 5 Sites', 'bookit' ), |
| 145 |
'type' => __( 'Most Popular', 'bookit' ), |
| 146 |
), |
| 147 |
'25' => array( |
| 148 |
'classname' => 'stm_plan--developer', |
| 149 |
'text' => __( 'Up to 25 Sites', 'bookit' ), |
| 150 |
'type' => __( 'Developer Oriented', 'bookit' ), |
| 151 |
), |
| 152 |
); |
| 153 |
|
| 154 |
$license = self::get_license( $plugin_id ); |
| 155 |
|
| 156 |
foreach ( $plans as $plan ) { |
| 157 |
if ( 'premium' == $plan->name || 'bookit_all_addons' == $plan->name ) { |
| 158 |
if ( isset( $plan->pricing ) ) { |
| 159 |
foreach ( $plan->pricing as $pricing ) { |
| 160 |
$plan_info[ 'licenses_' . $pricing->licenses ] = $pricing; |
| 161 |
$plan_info[ 'licenses_' . $pricing->licenses ]->url = self::STM_CHECKOUT_URL . "?productId={$plugin_id}&period=annual&licenses={$pricing->licenses}"; |
| 162 |
|
| 163 |
if ( ! isset( $plan_data[ $pricing->licenses ] ) ) { |
| 164 |
$plan_data[ $pricing->licenses ] = array( |
| 165 |
/* translators: %d: Number of sites the license supports */ |
| 166 |
'text' => sprintf( __( 'Up to %d Sites', 'bookit' ), $pricing->licenses ), |
| 167 |
'classname' => '', |
| 168 |
'type' => '', |
| 169 |
); |
| 170 |
} |
| 171 |
$plan_info[ 'licenses_' . $pricing->licenses ]->active = false; |
| 172 |
$plan_info[ 'licenses_' . $pricing->licenses ]->is_lifetime = false; |
| 173 |
if ( $license && $pricing->id == $license->pricing_id ) { |
| 174 |
$plan_info[ 'licenses_' . $pricing->licenses ]->active = true; |
| 175 |
$plan_info[ 'licenses_' . $pricing->licenses ]->is_lifetime = $license->is_lifetime(); |
| 176 |
} |
| 177 |
$plan_info[ 'licenses_' . $pricing->licenses ]->data = $plan_data[ $pricing->licenses ]; |
| 178 |
} |
| 179 |
} |
| 180 |
break; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
return $plan_info; |
| 185 |
} |
| 186 |
|
| 187 |
protected static function set_latest_info( $latest ) { |
| 188 |
$latest_info['version'] = $latest->version; |
| 189 |
$latest_info['tested_up_to'] = $latest->tested_up_to_version; |
| 190 |
$latest_info['created'] = date( 'M j, Y', strtotime( $latest->created ) ); |
| 191 |
$latest_info['last_update'] = date( 'M j, Y', strtotime( $latest->updated ) ); |
| 192 |
$latest_info['wordpress_version'] = $latest->requires_platform_version; |
| 193 |
|
| 194 |
return $latest_info; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Get bookit plugin installed addons. |
| 199 |
* If addon installed but not active load base and translation cls |
| 200 |
* to show info on settings page |
| 201 |
* @return array |
| 202 |
*/ |
| 203 |
public static function get_installed_addons() { |
| 204 |
$result = array(); |
| 205 |
|
| 206 |
if ( ! function_exists( 'bookit_fs' ) ) { |
| 207 |
return $result; |
| 208 |
} |
| 209 |
|
| 210 |
$bookit_fs = bookit_fs(); |
| 211 |
$addons = $bookit_fs->get_installed_addons(); |
| 212 |
$accountAddons = self::get_account_addons(); |
| 213 |
|
| 214 |
if ( ! $addons && empty( $accountAddons ) ) { |
| 215 |
return $result; |
| 216 |
} |
| 217 |
|
| 218 |
foreach ( $addons as $addon ) { |
| 219 |
if ( ! $addon ) { |
| 220 |
continue; |
| 221 |
} |
| 222 |
$addonFolder = $addon->get_plugin_folder_name(); |
| 223 |
$addonName = $addon->get_slug(); |
| 224 |
|
| 225 |
$existAddonKey = array_search( str_replace( Plugin::$prefix, '', $addonName ), array_column( $accountAddons, 'name' ) ); |
| 226 |
if ( false !== $existAddonKey ) { |
| 227 |
unset( $accountAddons[ $existAddonKey ] ); |
| 228 |
} |
| 229 |
|
| 230 |
$classNamespacePart = str_replace( '-', '', ucwords( $addonName, '-' ) ); |
| 231 |
|
| 232 |
/** base addon class */ |
| 233 |
$addonClass = sprintf( '%s\Classes\Admin\Base', ucwords( $classNamespacePart ) ); |
| 234 |
|
| 235 |
if ( ! class_exists( $addonClass ) && file_exists( ABSPATH . sprintf( '/wp-content/plugins/%s/includes/classes/admin/Base.php', $addonFolder ) ) ) { |
| 236 |
// load base class if plugin unactive |
| 237 |
require_once ABSPATH . sprintf( '/wp-content/plugins/%s/includes/classes/admin/Base.php', $addonFolder ); |
| 238 |
} |
| 239 |
if ( class_exists( $addonClass ) ) { |
| 240 |
$result[] = array( |
| 241 |
'name' => str_replace( Plugin::$prefix, '', $addonName ), |
| 242 |
'data' => $addonClass::getAddonData(), |
| 243 |
'translations' => $addonClass::getTranslations(), |
| 244 |
'freemius' => array( 'title' => $addonName ), |
| 245 |
); |
| 246 |
} |
| 247 |
} |
| 248 |
return array_merge( $result, $accountAddons ); |
| 249 |
} |
| 250 |
|
| 251 |
public static function get_account_addons() { |
| 252 |
$result = array(); |
| 253 |
|
| 254 |
if ( ! function_exists( 'bookit_fs' ) ) { |
| 255 |
return $result; |
| 256 |
} |
| 257 |
$bookit_fs = bookit_fs(); |
| 258 |
$addons = $bookit_fs->get_account_addons(); |
| 259 |
|
| 260 |
if ( ! $addons || empty( $addons ) ) { |
| 261 |
return $result; |
| 262 |
} |
| 263 |
|
| 264 |
foreach ( $addons as $addonId ) { |
| 265 |
$addon = $bookit_fs->get_addon( $addonId ); |
| 266 |
|
| 267 |
if ( ! $addon || ( $addon && ( ! $bookit_fs->is_addon_installed( $addonId ) ) ) ) { |
| 268 |
continue; |
| 269 |
} |
| 270 |
|
| 271 |
$addon = $bookit_fs->get_addon_instance( $addonId ); |
| 272 |
$addonFolder = $addon->get_plugin_folder_name(); |
| 273 |
$addonName = $addon->get_slug(); |
| 274 |
|
| 275 |
$classNamespacePart = str_replace( '-', '', ucwords( $addonName, '-' ) ); |
| 276 |
|
| 277 |
/** base addon class */ |
| 278 |
$addonClass = sprintf( '%s\Classes\Admin\Base', ucwords( $classNamespacePart ) ); |
| 279 |
|
| 280 |
if ( ! is_plugin_active( sprintf( '%s/%s.php', $addonFolder, $addonName ) ) && file_exists( ABSPATH . sprintf( '/wp-content/plugins/%s/includes/classes/admin/Base.php', $addonFolder ) ) ) { |
| 281 |
// load base class if plugin unactive |
| 282 |
require_once ABSPATH . sprintf( '/wp-content/plugins/%s/includes/classes/admin/Base.php', $addonName ); |
| 283 |
require_once ABSPATH . sprintf( '/wp-content/plugins/%s/includes/classes/Translations.php', $addonName ); |
| 284 |
} |
| 285 |
if ( class_exists( $addonClass ) ) { |
| 286 |
$result[] = array( |
| 287 |
'name' => str_replace( Plugin::$prefix, '', $addonName ), |
| 288 |
'data' => $addonClass::getAddonData(), |
| 289 |
'translations' => $addonClass::getTranslations(), |
| 290 |
'freemius' => array( 'title' => $addonName ), |
| 291 |
); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
return $result; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
/** |
| 300 |
* @return array of exist addon licences |
| 301 |
*/ |
| 302 |
public static function get_exist_licenses() { |
| 303 |
$result = array(); |
| 304 |
|
| 305 |
if ( ! function_exists( 'bookit_fs' ) ) { |
| 306 |
return $result; |
| 307 |
} |
| 308 |
$bookit_fs = bookit_fs(); |
| 309 |
$accountAddons = $bookit_fs->get_account_addons(); |
| 310 |
|
| 311 |
if ( false == $accountAddons ) { |
| 312 |
return; |
| 313 |
} |
| 314 |
|
| 315 |
foreach ( $accountAddons as $addonId ) { |
| 316 |
if ( ! $bookit_fs->is_addon_installed( $addonId ) ) { |
| 317 |
continue; |
| 318 |
} |
| 319 |
$addon = $bookit_fs->get_addon_instance( $addonId ); |
| 320 |
$addonName = $addon->get_slug(); |
| 321 |
|
| 322 |
$result[ $addonName ] = array(); |
| 323 |
if ( is_null( $addon->_get_license() ) ) { |
| 324 |
continue; |
| 325 |
} |
| 326 |
$license = $addon->_get_license(); |
| 327 |
$result[ $addonName ]['lifetime'] = $license->is_lifetime(); |
| 328 |
$result[ $addonName ]['id'] = $license->pricing_id; // check site count |
| 329 |
} |
| 330 |
|
| 331 |
return $result; |
| 332 |
} |
| 333 |
} |
| 334 |
|