| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The App details file |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Extendify; |
| 8 |
|
| 9 |
defined('ABSPATH') || die('No direct access.'); |
| 10 |
|
| 11 |
use Extendify\Shared\Services\Sanitizer; |
| 12 |
|
| 13 |
/** |
| 14 |
* Controller for handling various app data |
| 15 |
*/ |
| 16 |
|
| 17 |
class Config |
| 18 |
{ |
| 19 |
/** |
| 20 |
* Plugin slug |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
public static $slug = 'extendify'; |
| 25 |
|
| 26 |
/** |
| 27 |
* The JS/CSS asset manifest (with hashes) |
| 28 |
* |
| 29 |
* @var array |
| 30 |
*/ |
| 31 |
public static $assetManifest = []; |
| 32 |
|
| 33 |
/** |
| 34 |
* Plugin version |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
public static $version = ''; |
| 39 |
|
| 40 |
/** |
| 41 |
* Plugin API REST version |
| 42 |
* |
| 43 |
* @var string |
| 44 |
*/ |
| 45 |
public static $apiVersion = 'v1'; |
| 46 |
|
| 47 |
/** |
| 48 |
* Partner Id |
| 49 |
* |
| 50 |
* @var string|null |
| 51 |
*/ |
| 52 |
public static $partnerId = null; |
| 53 |
|
| 54 |
/** |
| 55 |
* Whether to load Launch |
| 56 |
* |
| 57 |
* @var boolean |
| 58 |
*/ |
| 59 |
public static $showLaunch = false; |
| 60 |
|
| 61 |
/** |
| 62 |
* Plugin environment |
| 63 |
* |
| 64 |
* @var string |
| 65 |
*/ |
| 66 |
public static $environment = ''; |
| 67 |
|
| 68 |
/** |
| 69 |
* Host plugin |
| 70 |
* |
| 71 |
* @var string |
| 72 |
*/ |
| 73 |
public static $requiredCapability = EXTENDIFY_REQUIRED_CAPABILITY; |
| 74 |
|
| 75 |
/** |
| 76 |
* Plugin config |
| 77 |
* |
| 78 |
* @var array |
| 79 |
*/ |
| 80 |
public static $config = []; |
| 81 |
|
| 82 |
/** |
| 83 |
* Whether Launch was finished |
| 84 |
* |
| 85 |
* @var boolean |
| 86 |
*/ |
| 87 |
public static $launchCompleted = false; |
| 88 |
|
| 89 |
/** |
| 90 |
* Enabled preview features. |
| 91 |
* |
| 92 |
* @var array |
| 93 |
*/ |
| 94 |
public static $previewFeatures = []; |
| 95 |
|
| 96 |
public static $enablePreviewFeatures = false; |
| 97 |
|
| 98 |
/** |
| 99 |
* Process the readme file to get version and name |
| 100 |
* |
| 101 |
* @return void |
| 102 |
*/ |
| 103 |
public function __construct() |
| 104 |
{ |
| 105 |
self::$partnerId = defined('EXTENDIFY_PARTNER_ID') ? constant('EXTENDIFY_PARTNER_ID') : null; |
| 106 |
$partnerData = PartnerData::getPartnerData(); |
| 107 |
|
| 108 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 109 |
$readme = file_get_contents(EXTENDIFY_PATH . 'readme.txt'); |
| 110 |
|
| 111 |
preg_match('/Stable tag: ([0-9.:]+)/', $readme, $matches); |
| 112 |
self::$version = $matches[1]; |
| 113 |
|
| 114 |
self::$assetManifest = wp_json_file_decode( |
| 115 |
EXTENDIFY_PATH . 'public/build/manifest.json', |
| 116 |
['associative' => true] |
| 117 |
); |
| 118 |
|
| 119 |
if (!get_option('extendify_first_installed_version')) { |
| 120 |
update_option('extendify_first_installed_version', Sanitizer::sanitizeText(self::$version)); |
| 121 |
} |
| 122 |
|
| 123 |
// Set up the Preview features |
| 124 |
// phpcs:ignore WordPress.Security.NonceVerification |
| 125 |
self::handlePreviewUrlOptIn($_GET); |
| 126 |
|
| 127 |
// An easy way to check if we are in dev mode is to look for a dev specific file. |
| 128 |
$isDev = is_readable(EXTENDIFY_PATH . '.devbuild'); |
| 129 |
|
| 130 |
self::$environment = $isDev ? 'DEVELOPMENT' : 'PRODUCTION'; |
| 131 |
self::$launchCompleted = (bool) get_option('extendify_onboarding_completed', false); |
| 132 |
self::$showLaunch = $isDev ? true : ((bool) ($partnerData['showLaunch'] ?? false)); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Retrieves the value of a preview setting. |
| 137 |
* |
| 138 |
* This method first checks if the preview setting exists as a static property of the class. |
| 139 |
* If it does, it returns the value of that property. Otherwise, it looks for the |
| 140 |
* preview setting in the saved setting and returns its value if found. |
| 141 |
* |
| 142 |
* @param string $previewKey The key of the preview setting to retrieve. |
| 143 |
* |
| 144 |
* @return boolean The value of the setting if found or false if not found. |
| 145 |
*/ |
| 146 |
public static function preview(string $previewKey) |
| 147 |
{ |
| 148 |
if (self::$environment === 'DEVELOPMENT') { |
| 149 |
return true; |
| 150 |
} |
| 151 |
|
| 152 |
if (property_exists(self::class, $previewKey)) { |
| 153 |
return self::$previewFeatures[$previewKey]; |
| 154 |
} |
| 155 |
|
| 156 |
$previewFeatures = get_option('extendify_enable_preview_features_v1', []); |
| 157 |
return (bool) ($previewFeatures[$previewKey] ?? false); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
/** |
| 162 |
* Processes preview features from URL parameters and enables them. |
| 163 |
* |
| 164 |
* This method checks for 'extendify-preview' parameters in the GET request |
| 165 |
* and enables the specified preview features. It supports both single feature |
| 166 |
* format (extendify-preview=feature1) and array format (extendify-preview[]=feature1). |
| 167 |
* All specified features are enabled and saved to the database. |
| 168 |
* |
| 169 |
* @param array $getParams The GET parameters that may contain 'extendify-preview' settings. |
| 170 |
* |
| 171 |
* @return void |
| 172 |
*/ |
| 173 |
protected static function handlePreviewUrlOptIn(array $getParams = []) |
| 174 |
{ |
| 175 |
if (!isset($getParams['extendify-preview'])) { |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
$previewParam = $getParams['extendify-preview']; |
| 180 |
$features = is_array($previewParam) ? $previewParam : [$previewParam]; |
| 181 |
self::$previewFeatures = get_option('extendify_enable_preview_features_v1', []); |
| 182 |
|
| 183 |
foreach ($features as $feature) { |
| 184 |
self::$previewFeatures[$feature] = true; |
| 185 |
} |
| 186 |
|
| 187 |
update_option('extendify_enable_preview_features_v1', Sanitizer::sanitizeArray(self::$previewFeatures)); |
| 188 |
} |
| 189 |
} |
| 190 |
|