-
-
Save vovafeldman/7a8ced9d19e6247835e9f088c597e943 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Blocksy; | |
/* | |
*/ | |
class Capabilities { | |
private $accounts_cache = null; | |
private $module_slug = 'blocksy-companion'; | |
public function __construct() { | |
$this->accounts_cache = get_option('fs_accounts'); | |
} | |
public function get_features() { | |
// possible plans: | |
// free | |
// | |
// personal | |
// professional | |
// agency | |
// | |
// personal_v2 | |
// professional_v2 | |
// agency_v2 | |
return [ | |
'base_pro' => [ | |
'personal', | |
'professional', | |
'agency', | |
'personal_v2', | |
'professional_v2', | |
'agency_v2' | |
], | |
'pro_starter_sites' => [ | |
'personal', | |
'professional', | |
'agency', | |
'professional_v2', | |
'agency_v2' | |
], | |
'post_types_extra' => [ | |
'personal', | |
'professional', | |
'agency', | |
'professional_v2', | |
'agency_v2' | |
], | |
'shop_extra' => [ | |
'personal', | |
'professional', | |
'agency', | |
'professional_v2', | |
'agency_v2' | |
], | |
'white_label' => [ | |
'agency', | |
'agency_v2' | |
] | |
]; | |
} | |
public function has_feature($feature = 'base_pro') { | |
$plan = $this->get_plan(); | |
$features = $this->get_features(); | |
if (! isset($features[$feature])) { | |
return false; | |
} | |
return in_array($plan, $features[$feature]); | |
} | |
private function get_plan() { | |
$site = $this->get_site(); | |
if ( | |
! $site | |
|| | |
! isset($site->plan_id) | |
|| | |
! isset($this->accounts_cache['plans']) | |
|| | |
empty($this->accounts_cache['plans'][$this->module_slug]) | |
) { | |
return 'free'; | |
} | |
$all_plans = $this->accounts_cache['plans'][$this->module_slug]; | |
$plan_id = null; | |
foreach ($all_plans as $incomplete_plan) { | |
$plan = $this->casttoclass('stdClass', $incomplete_plan); | |
$id = base64_decode($plan->id); | |
if (strval($id) === strval($site->plan_id)) { | |
$plan_id = strval($id); | |
} | |
} | |
$plans = [ | |
'11880' => 'personal', | |
'11881' => 'professional', | |
'11882' => 'agency', | |
'23839' => 'personal_v2', | |
'23840' => 'professional_v2', | |
'23841' => 'agency_v2' | |
]; | |
if ($plan_id) { | |
if (isset($plans[$plan_id])) { | |
return $plans[$plan_id]; | |
} else { | |
return 'agency_v2'; | |
} | |
} | |
return 'free'; | |
} | |
private function get_site() { | |
$site = null; | |
if ( | |
! isset($this->accounts_cache) | |
|| | |
! isset($this->accounts_cache['sites']) | |
|| | |
! isset($this->accounts_cache['sites'][$this->module_slug]) | |
) { | |
return null; | |
} | |
$maybe_site = $this->casttoclass( | |
'stdClass', | |
$this->accounts_cache['sites'][$this->module_slug] | |
); | |
return $maybe_site; | |
} | |
private function casttoclass($class, $object) { | |
return unserialize( | |
preg_replace( | |
'/^O:\d+:"[^"]++"/', | |
'O:' . strlen($class) . ':"' . $class . '"', | |
serialize($object) | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment