| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | /** |
| 3 | 4 | * The App details file |
| 4 | 5 | */ |
| 5 | 6 | |
| @@ -4,27 +5,31 @@ | ||
| 4 | 5 | */ |
| 5 | 6 | |
| 6 | 7 | namespace Extendify; |
| 7 | 8 | |
| 9 | +defined('ABSPATH') || die('No direct access.'); | |
| 10 | + | |
| 11 | +use Extendify\Shared\Services\Sanitizer; | |
| 12 | + | |
| 8 | 13 | /** |
| 9 | 14 | * Controller for handling various app data |
| 10 | 15 | */ |
| 16 | + | |
| 11 | 17 | class Config |
| 12 | 18 | { |
| 13 | - | |
| 14 | 19 | /** |
| 15 | - * Plugin name | |
| 20 | + * Plugin slug | |
| 16 | 21 | * |
| 17 | 22 | * @var string |
| 18 | 23 | */ |
| 19 | - public static $name = ''; | |
| 24 | + public static $slug = 'extendify'; | |
| 20 | 25 | |
| 21 | 26 | /** |
| 22 | - * Plugin slug | |
| 27 | + * The JS/CSS asset manifest (with hashes) | |
| 23 | 28 | * |
| 24 | - * @var string | |
| 29 | + * @var array | |
| 25 | 30 | */ |
| 26 | - public static $slug = ''; | |
| 31 | + public static $assetManifest = []; | |
| 27 | 32 | |
| 28 | 33 | /** |
| 29 | 34 | * Plugin version |
| 30 | 35 | * |
| @@ -39,13 +44,13 @@ | ||
| 39 | 44 | */ |
| 40 | 45 | public static $apiVersion = 'v1'; |
| 41 | 46 | |
| 42 | 47 | /** |
| 43 | - * Whether this is the standalone plugin | |
| 48 | + * Partner Id | |
| 44 | 49 | * |
| 45 | - * @var boolean | |
| 50 | + * @var string|null | |
| 46 | 51 | */ |
| 47 | - public static $standalone; | |
| 52 | + public static $partnerId = null; | |
| 48 | 53 | |
| 49 | 54 | /** |
| 50 | 55 | * Whether to load Launch |
| 51 | 56 | * |
| @@ -50,18 +55,11 @@ | ||
| 50 | 55 | * Whether to load Launch |
| 51 | 56 | * |
| 52 | 57 | * @var boolean |
| 53 | 58 | */ |
| 54 | - public static $showOnboarding = false; | |
| 59 | + public static $showLaunch = false; | |
| 55 | 60 | |
| 56 | 61 | /** |
| 57 | - * Whether to load Assist | |
| 58 | - * | |
| 59 | - * @var boolean | |
| 60 | - */ | |
| 61 | - public static $showAssist = false; | |
| 62 | - | |
| 63 | - /** | |
| 64 | 62 | * Plugin environment |
| 65 | 63 | * |
| 66 | 64 | * @var string |
| 67 | 65 | */ |
| @@ -67,20 +65,13 @@ | ||
| 67 | 65 | */ |
| 68 | 66 | public static $environment = ''; |
| 69 | 67 | |
| 70 | 68 | /** |
| 71 | - * The partner plugin/theme | |
| 72 | - * | |
| 73 | - * @var string | |
| 74 | - */ | |
| 75 | - public static $sdkPartner = 'standalone'; | |
| 76 | - | |
| 77 | - /** | |
| 78 | 69 | * Host plugin |
| 79 | 70 | * |
| 80 | 71 | * @var string |
| 81 | 72 | */ |
| 82 | - public static $requiredCapability = 'manage_options'; | |
| 73 | + public static $requiredCapability = EXTENDIFY_REQUIRED_CAPABILITY; | |
| 83 | 74 | |
| 84 | 75 | /** |
| 85 | 76 | * Plugin config |
| 86 | 77 | * |
| @@ -95,8 +86,17 @@ | ||
| 95 | 86 | */ |
| 96 | 87 | public static $launchCompleted = false; |
| 97 | 88 | |
| 98 | 89 | /** |
| 90 | + * Enabled preview features. | |
| 91 | + * | |
| 92 | + * @var array | |
| 93 | + */ | |
| 94 | + public static $previewFeatures = []; | |
| 95 | + | |
| 96 | + public static $enablePreviewFeatures = false; | |
| 97 | + | |
| 98 | + /** | |
| 99 | 99 | * Process the readme file to get version and name |
| 100 | 100 | * |
| 101 | 101 | * @return void |
| 102 | 102 | */ |
| @@ -101,74 +101,89 @@ | ||
| 101 | 101 | * @return void |
| 102 | 102 | */ |
| 103 | 103 | public function __construct() |
| 104 | 104 | { |
| 105 | - if (isset($GLOBALS['extendify_sdk_partner']) && $GLOBALS['extendify_sdk_partner']) { | |
| 106 | - self::$sdkPartner = $GLOBALS['extendify_sdk_partner']; | |
| 107 | - } | |
| 105 | + self::$partnerId = defined('EXTENDIFY_PARTNER_ID') ? constant('EXTENDIFY_PARTNER_ID') : null; | |
| 106 | + $partnerData = PartnerData::getPartnerData(); | |
| 108 | 107 | |
| 109 | - // Set up whether this is the standalone plugin instead of integrated. | |
| 110 | - self::$standalone = self::$sdkPartner === 'standalone'; | |
| 111 | - | |
| 112 | - // TODO: Previous way to set the partner name - can remove in future. | |
| 113 | - if (defined('EXTENDIFY_PARTNER_NAME')) { | |
| 114 | - self::$sdkPartner = constant('EXTENDIFY_PARTNER_NAME'); | |
| 115 | - } | |
| 116 | - | |
| 117 | - // Always use the partner ID if set as a constant. | |
| 118 | - if (defined('EXTENDIFY_PARTNER_ID')) { | |
| 119 | - self::$sdkPartner = constant('EXTENDIFY_PARTNER_ID'); | |
| 120 | - } | |
| 121 | - | |
| 122 | 108 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 123 | 109 | $readme = file_get_contents(EXTENDIFY_PATH . 'readme.txt'); |
| 124 | 110 | |
| 125 | - preg_match('/=== (.+) ===/', $readme, $matches); | |
| 126 | - self::$name = $matches[1]; | |
| 127 | - self::$slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', self::$name), '-')); | |
| 128 | - | |
| 129 | 111 | preg_match('/Stable tag: ([0-9.:]+)/', $readme, $matches); |
| 130 | 112 | self::$version = $matches[1]; |
| 131 | 113 | |
| 114 | + self::$assetManifest = wp_json_file_decode( | |
| 115 | + EXTENDIFY_PATH . 'public/build/manifest.json', | |
| 116 | + ['associative' => true] | |
| 117 | + ); | |
| 118 | + | |
| 132 | 119 | if (!get_option('extendify_first_installed_version')) { |
| 133 | - update_option('extendify_first_installed_version', self::$version); | |
| 120 | + update_option('extendify_first_installed_version', Sanitizer::sanitizeText(self::$version)); | |
| 134 | 121 | } |
| 135 | 122 | |
| 123 | + // Set up the Preview features | |
| 124 | + // phpcs:ignore WordPress.Security.NonceVerification | |
| 125 | + self::handlePreviewUrlOptIn($_GET); | |
| 126 | + | |
| 136 | 127 | // An easy way to check if we are in dev mode is to look for a dev specific file. |
| 137 | - $isDev = is_readable(EXTENDIFY_PATH . 'public/build/.devbuild'); | |
| 128 | + $isDev = is_readable(EXTENDIFY_PATH . '.devbuild'); | |
| 138 | 129 | |
| 139 | 130 | self::$environment = $isDev ? 'DEVELOPMENT' : 'PRODUCTION'; |
| 140 | - self::$launchCompleted = (bool) get_option('extendify_onboarding_completed') || $isDev; | |
| 141 | - self::$showOnboarding = $this->showOnboarding(); | |
| 142 | - self::$showAssist = self::$launchCompleted || self::$showOnboarding; | |
| 143 | - | |
| 144 | - // Add the config. | |
| 145 | - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents | |
| 146 | - $config = file_get_contents(EXTENDIFY_PATH . 'config.json'); | |
| 147 | - self::$config = json_decode($config, true); | |
| 131 | + self::$launchCompleted = (bool) get_option('extendify_onboarding_completed', false); | |
| 132 | + self::$showLaunch = $isDev ? true : ((bool) ($partnerData['showLaunch'] ?? false)); | |
| 148 | 133 | } |
| 149 | 134 | |
| 150 | 135 | /** |
| 151 | - * Conditionally load Extendify Launch. | |
| 136 | + * Retrieves the value of a preview setting. | |
| 152 | 137 | * |
| 153 | - * @return boolean | |
| 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. | |
| 154 | 145 | */ |
| 155 | - private function showOnboarding() | |
| 146 | + public static function preview(string $previewKey) | |
| 156 | 147 | { |
| 157 | - // Always show it for dev mode. | |
| 158 | 148 | if (self::$environment === 'DEVELOPMENT') { |
| 159 | 149 | return true; |
| 160 | 150 | } |
| 161 | 151 | |
| 162 | - // Currently we require a flag to be set. | |
| 163 | - if (!defined('EXTENDIFY_SHOW_ONBOARDING')) { | |
| 164 | - return false; | |
| 152 | + if (property_exists(self::class, $previewKey)) { | |
| 153 | + return self::$previewFeatures[$previewKey]; | |
| 165 | 154 | } |
| 166 | 155 | |
| 167 | - // Check if they disabled it and respect that. | |
| 168 | - if (constant('EXTENDIFY_SHOW_ONBOARDING') === false) { | |
| 169 | - return false; | |
| 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; | |
| 170 | 177 | } |
| 171 | 178 | |
| 172 | - return true; | |
| 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)); | |
| 173 | 188 | } |
| 174 | 189 | } |