Notifications
2 months ago
Settings
1 month ago
Admin.php
1 month ago
CLI.php
2 weeks ago
Config.php
1 year ago
ConflictingPlugins.php
10 months ago
Connect.php
2 weeks ago
Cron.php
1 year ago
Invalidations.php
2 months ago
NitroPack.php
2 weeks ago
Settings.php
4 months ago
Settings.php
231 lines
| 1 | <?php |
| 2 | |
| 3 | namespace NitroPack\WordPress; |
| 4 | use NitroPack\WordPress\Settings\Subscription; |
| 5 | use NitroPack\WordPress\Settings\PurgeCache; |
| 6 | use NitroPack\WordPress\Settings\CacheWarmup; |
| 7 | use NitroPack\WordPress\Settings\Optimizations; |
| 8 | use NitroPack\WordPress\Settings\OptimizationLevel; |
| 9 | use NitroPack\WordPress\Settings\AutoPurge; |
| 10 | use NitroPack\WordPress\Settings\CPTOptimization; |
| 11 | use NitroPack\WordPress\Settings\GeneratePreview; |
| 12 | use NitroPack\WordPress\Settings\TestMode; |
| 13 | use NitroPack\WordPress\Settings\HTMLCompression; |
| 14 | use NitroPack\WordPress\Settings\BeaverBuilder; |
| 15 | use NitroPack\WordPress\Settings\CartCache; |
| 16 | use NitroPack\WordPress\Settings\StockRefresh; |
| 17 | use NitroPack\WordPress\Settings\EditorClearCache; |
| 18 | use NitroPack\WordPress\Settings\Shortcodes; |
| 19 | use NitroPack\WordPress\Settings\Logger; |
| 20 | use NitroPack\WordPress\Settings\SystemReport; |
| 21 | |
| 22 | /** |
| 23 | * Class Settings |
| 24 | * |
| 25 | * This class handles the configuration settings for NitroPack. |
| 26 | * |
| 27 | * @package NitroPack\WordPress |
| 28 | */ |
| 29 | class Settings { |
| 30 | /** |
| 31 | * @var array $settings Configuration settings for NitroPack. |
| 32 | * |
| 33 | * The settings array includes the following keys: |
| 34 | * - 'nitropack-webhookToken': (mixed) Token for NitroPack webhook, default is null. |
| 35 | * - 'nitropack-enableCompression': (int) Flag to enable compression, default is -1. |
| 36 | * - 'nitropack-autoCachePurge': (int) Flag to enable automatic cache purge, default is 1. |
| 37 | * - 'nitropack-cacheableObjectTypes': (array) List of cacheable object types, default is an empty array but gets updated immediately to all CPTs. |
| 38 | * - 'nitropack-distribution': (string) Distribution type, default is 'regular'. |
| 39 | */ |
| 40 | private $settings; |
| 41 | |
| 42 | public $cache_warmup; |
| 43 | /** |
| 44 | * Grabs Subscription class |
| 45 | * @var Subscription |
| 46 | */ |
| 47 | public $subscription; |
| 48 | /** |
| 49 | * Grabs PurgeCache class |
| 50 | * @var PurgeCache |
| 51 | */ |
| 52 | public $purge_cache; |
| 53 | /** |
| 54 | * Grabs Optimizations class |
| 55 | * @var Optimizations |
| 56 | */ |
| 57 | public $optimizations; |
| 58 | /** |
| 59 | * Grabs OptimizationLevel class |
| 60 | * @var OptimizationLevel |
| 61 | */ |
| 62 | public $optimization_level; |
| 63 | /** |
| 64 | * Grabs AutoPurge class |
| 65 | * @var AutoPurge |
| 66 | */ |
| 67 | public $auto_purge; |
| 68 | |
| 69 | public $cpt_optimization; |
| 70 | /** |
| 71 | * Grabs GeneratePreview class |
| 72 | * @var GeneratePreview |
| 73 | */ |
| 74 | public $generate_preview; |
| 75 | |
| 76 | /** |
| 77 | * Grabs TestMode class |
| 78 | * @var TestMode |
| 79 | */ |
| 80 | public $test_mode; |
| 81 | /** |
| 82 | * Grabs HTMLCompression class |
| 83 | * @var HTMLCompression |
| 84 | */ |
| 85 | public $html_compression; |
| 86 | /** |
| 87 | * Grabs BeaverBuilder class |
| 88 | * @var BeaverBuilder |
| 89 | */ |
| 90 | public $beaver_builder; |
| 91 | /** |
| 92 | * Grabs CartCache class |
| 93 | * @var CartCache |
| 94 | */ |
| 95 | public $cart_cache; |
| 96 | |
| 97 | /** |
| 98 | * Grabs StockRefresh class |
| 99 | * @var StockRefresh |
| 100 | */ |
| 101 | public $stock_refresh; |
| 102 | /** |
| 103 | * Grabs EditorClearCache class |
| 104 | * @var EditorClearCache |
| 105 | */ |
| 106 | public $editor_clear_cache; |
| 107 | /** |
| 108 | * Grabs Shortcodes class |
| 109 | * @var Shortcodes |
| 110 | */ |
| 111 | public $shortcodes; |
| 112 | public $logger; |
| 113 | public $system_report; |
| 114 | /** |
| 115 | * Settings constructor. |
| 116 | * |
| 117 | * Initializes the default required settings for the NitroPack plugin. |
| 118 | */ |
| 119 | function __construct( $config = null ) { |
| 120 | add_action( 'admin_init', [ $this, 'move_existing_options' ] ); |
| 121 | $this->default_required_settings(); |
| 122 | //initialize each setting |
| 123 | $this->generate_preview = GeneratePreview::getInstance(); |
| 124 | $this->purge_cache = new PurgeCache(); |
| 125 | $this->subscription = Subscription::getInstance(); |
| 126 | $this->optimizations = Optimizations::getInstance(); |
| 127 | $this->optimization_level = OptimizationLevel::getInstance(); |
| 128 | $this->auto_purge = new AutoPurge(); |
| 129 | $this->cpt_optimization = CPTOptimization::getInstance(); |
| 130 | $this->shortcodes = new Shortcodes(); |
| 131 | $this->cache_warmup = CacheWarmup::getInstance(); |
| 132 | $this->test_mode = TestMode::getInstance(); |
| 133 | $this->html_compression = HTMLCompression::getInstance(); |
| 134 | $this->beaver_builder = new BeaverBuilder(); |
| 135 | $this->cart_cache = new CartCache(); |
| 136 | $this->stock_refresh = StockRefresh::getInstance(); |
| 137 | $this->editor_clear_cache = new EditorClearCache(); |
| 138 | $this->system_report = SystemReport::getInstance(); |
| 139 | $this->logger = new Logger( $config ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Set default required settings in order for the plugin to work properly. |
| 144 | * |
| 145 | * @return void |
| 146 | */ |
| 147 | private function default_required_settings() { |
| 148 | $this->settings = [ |
| 149 | 'nitropack-webhookToken' => null, |
| 150 | 'nitropack-enableCompression' => -1, |
| 151 | 'nitropack-autoCachePurge' => 1, |
| 152 | 'nitropack-cacheableObjectTypes' => [], |
| 153 | 'nitropack-distribution' => 'regular', |
| 154 | ]; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Refreshes the required settings for the plugin. |
| 159 | * |
| 160 | * This method updates the options for the webhook token and iterates through |
| 161 | * the settings to update options that are not already set in the WordPress |
| 162 | * database. If the option 'nitropack-cacheableObjectTypes' is encountered, |
| 163 | * it sets the value to the default cacheable object types. |
| 164 | * |
| 165 | * @param string|null $token Optional. The token to be used for generating the webhook token. Default is null. |
| 166 | * @return void |
| 167 | */ |
| 168 | public function set_required_settings( $token = null ) { |
| 169 | |
| 170 | if ( $token !== null ) { |
| 171 | $this->settings['nitropack-webhookToken'] = $token; |
| 172 | } else { |
| 173 | // Generate a new webhook token if it is not passed |
| 174 | $this->generate_webhook_token(); |
| 175 | } |
| 176 | |
| 177 | foreach ( $this->settings as $option => $value ) { |
| 178 | if ( get_option( $option ) === false && $value !== null ) { |
| 179 | if ( $option === 'nitropack-cacheableObjectTypes' ) { |
| 180 | $value = $this->cpt_optimization->nitropack_get_default_cacheable_object_types(); |
| 181 | } |
| 182 | update_option( $option, $value ); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | /** |
| 187 | * Generates a webhook token for the NitroPack settings. |
| 188 | * |
| 189 | * This function retrieves the site configuration and checks if a webhook token |
| 190 | * is already set. If a token is provided, it generates a new webhook token using |
| 191 | * the site ID from the POST request. If no site ID is provided in the POST request, |
| 192 | * it sets the webhook token to null. |
| 193 | * |
| 194 | * @param string|null $token Optional. The token to be used for generating the webhook token. |
| 195 | * If not provided, a new token will be generated. |
| 196 | */ |
| 197 | public function generate_webhook_token() { |
| 198 | $siteConfig = nitropack_get_site_config(); |
| 199 | //grab existing from config |
| 200 | if ( isset( $siteConfig['webhookToken'] ) ) { |
| 201 | $this->settings['nitropack-webhookToken'] = $siteConfig['webhookToken']; |
| 202 | } elseif ( isset( $siteConfig['siteId'] ) ) { |
| 203 | //generate from existing siteId |
| 204 | $siteId = $siteConfig['siteId']; |
| 205 | $this->settings['nitropack-webhookToken'] = nitropack_generate_webhook_token( $siteId ); |
| 206 | } elseif ( ! empty( $_POST["siteId"] ) ) { |
| 207 | //try to generate from POST |
| 208 | $siteId = $_POST["siteId"]; |
| 209 | $this->settings['nitropack-webhookToken'] = nitropack_generate_webhook_token( $siteId ); |
| 210 | } else { |
| 211 | $this->settings['nitropack-webhookToken'] = null; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Move wrongly formatted nitropack options to correct ones and delete old ones. |
| 217 | * @return void |
| 218 | */ |
| 219 | public function move_existing_options() { |
| 220 | if ( ! empty( $_GET['page'] ) && $_GET['page'] === 'nitropack' ) { |
| 221 | $existing_options = [ 'np_warmup_sitemap' => 'nitropack-warmup-sitemap', 'nitropack_minimumLogLevel' => 'nitropack-minimumLogLevel' ]; |
| 222 | foreach ( $existing_options as $option => $new_option ) { |
| 223 | if ( $old_option = get_option( $option ) ) { |
| 224 | update_option( $new_option, $old_option ); |
| 225 | delete_option( $option ); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 |