From 40b457fe60594da3de7ee0ea27b5aa5910499ab6 Mon Sep 17 00:00:00 2001 From: Sachin Fernandes Date: Wed, 23 Sep 2020 16:38:08 -0700 Subject: [PATCH] Update Wordpress to be compatible with cloudflare-plugin-backend changes --- config.js => config.json | 0 scripts/deployment/publish_github.py | 2 +- src/WordPress/Hooks.php | 2 +- src/WordPress/PluginActions.php | 95 +++++++++++++++++++++++++++- 4 files changed, 94 insertions(+), 5 deletions(-) rename config.js => config.json (100%) mode change 100755 => 100644 diff --git a/config.js b/config.json old mode 100755 new mode 100644 similarity index 100% rename from config.js rename to config.json diff --git a/scripts/deployment/publish_github.py b/scripts/deployment/publish_github.py index 09045c87..2c92d7ec 100755 --- a/scripts/deployment/publish_github.py +++ b/scripts/deployment/publish_github.py @@ -7,7 +7,7 @@ COMPOSER_FILE_NAME = "composer.json" README_FILE_NAME = "readme.txt" -CONFIG_FILE_NAME = "config.js" +CONFIG_FILE_NAME = "config.json" CLOUDFLARE_PHP_FILE_NAME = "cloudflare.php" README_TXT_STABLE_TAG_LINE_NUMBER = 6 CLOUDFLARE_PHP_VERSION_LINE_NUMBER = 6 diff --git a/src/WordPress/Hooks.php b/src/WordPress/Hooks.php index 145c5d3c..b9ef1692 100644 --- a/src/WordPress/Hooks.php +++ b/src/WordPress/Hooks.php @@ -21,7 +21,7 @@ class Hooks public function __construct() { - $this->config = new Integration\DefaultConfig(file_get_contents(CLOUDFLARE_PLUGIN_DIR.'config.js', true)); + $this->config = new Integration\DefaultConfig(file_get_contents(CLOUDFLARE_PLUGIN_DIR.'config.json', true)); $this->logger = new Integration\DefaultLogger($this->config->getValue('debug')); $this->dataStore = new DataStore($this->logger); $this->integrationAPI = new WordPressAPI($this->dataStore); diff --git a/src/WordPress/PluginActions.php b/src/WordPress/PluginActions.php index a5214ca7..19750b56 100644 --- a/src/WordPress/PluginActions.php +++ b/src/WordPress/PluginActions.php @@ -3,18 +3,55 @@ namespace CF\WordPress; use CF\API\APIInterface; -use CF\API\Request; +use CF\API\AbstractPluginActions; +use CF\API\Exception\ZoneSettingFailException; use CF\API\Plugin; +use CF\API\Request; use CF\Integration\DefaultIntegration; -use CF\API\Exception\ZoneSettingFailException; use CF\WordPress\Constants\Plans; -use CF\API\AbstractPluginActions; class PluginActions extends AbstractPluginActions { protected $api; protected $clientAPI; + protected $composer; protected $request; + protected $userConfig; + + const CONFIG = [ + "debug" => false, + "featureManagerIsFullZoneProvisioningEnabled" => false, + "isDNSPageEnabled" => false, + "isSubdomainCheckEnabled" => true, + "useHostAPILogin" => false, + "homePageCards" => [ + "ApplyDefaultSettingsCard", + "PurgeCacheCard", + "PluginSpecificCacheCard" + ], + "moreSettingsCards" => [ + "container.moresettings.speed" => [ + "AlwaysOnlineCard", + "ImageOptimizationCard", + "DevelopmentModeCard", + "BypassCacheByCookieCard" + ], + "container.moresettings.security" => [ + "SecurityLevelCard", + "WAFCard", + "AdvanceDDoSCard", + "AutomaticHTTPSRewritesCard" + ] + ], + "locale" => "en", + "integrationName" => "wordpress" + ]; + + const BANNED_KEYS = [ + 'isDNSPageEnabled', + 'useHostAPILogin', + 'integrationName', + ]; public function __construct(DefaultIntegration $defaultIntegration, APIInterface $api, Request $request) { @@ -132,4 +169,56 @@ public function applyDefaultSettings() } } } + + public function getConfig() + { + $this->getUserConfig(); + $this->getComposerJson(); + + //Clone the config to manipulate + $config = array_merge(array(), self::CONFIG); + + //Add version from composer.json to the config + $config['version'] = $this->composer['version']; + + //This removes all the banned keys from the userConfig so we don't over write them + $this->userConfig = array_diff_key($this->userConfig, array_flip(self::BANNED_KEYS)); + + //Merge and intersect userConfig with default config and return response + $response = array_intersect_key($this->userConfig + $config, $config); + + return $this->api->createAPISuccessResponse($response); + } + + public function getUserConfig() + { + if ($this->userConfig === null) { + //Need to suppress the File not found error with @ + $userConfigContent = @file_get_contents(dirname(__FILE__) . '/config.json'); + + //Need to set an empty array for merge into config so it doesnt throw a type error + $this->userConfig = []; + //If we did find a config decode it + if ($userConfigContent) { + $this->userConfig = json_decode($userConfigContent, true); + } + } + } + + public function setUserConfig($userConfig) + { + $this->userConfig = $userConfig; + } + + public function getComposerJson() + { + if ($this->composer === null) { + $this->composer = json_decode(file_get_contents(dirname(__FILE__) . '/composer.json'), true); + } + } + + public function setComposerJson($composer) + { + $this->composer = $composer; + } }