Skip to content

Instantly share code, notes, and snippets.

@andreiglingeanu
Created November 16, 2023 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andreiglingeanu/11b4e5d9becadb8fe6c476cab3e7c234 to your computer and use it in GitHub Desktop.
Save andreiglingeanu/11b4e5d9becadb8fe6c476cab3e7c234 to your computer and use it in GitHub Desktop.
<?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