PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.18.9
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.18.9
1.19.8 1.19.7 1.19.6 1.19.5 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.12.0 1.13.0 1.14.0 1.15.0 1.15.1 1.15.2 1.15.3 1.16.0 1.16.1 1.16.2 1.16.3 1.16.4 1.16.5 1.16.6 1.16.7 1.16.8 1.17.0 1.17.6 1.17.7 1.17.8 1.17.9 1.18.0 1.18.1 1.18.2 1.18.3 1.18.4 1.18.5 1.18.6 1.18.7 1.18.8 1.18.9 1.19.0 1.19.1 1.19.2 1.19.3 1.19.4 1.3.19 1.3.20 1.4.0 1.4.1 1.5.0 1.5.1 1.5.10 1.5.11 1.5.12 1.5.13 1.5.14 1.5.15 1.5.16 1.5.17 1.5.18 1.5.19 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.8.1 1.8.3 1.9.0 1.9.1 1.9.2
nitropack / classes / WordPress / Settings.php
nitropack / classes / WordPress Last commit date
Notifications 7 months ago Settings 5 months ago CLI.php 6 months ago Config.php 1 year ago ConflictingPlugins.php 10 months ago Cron.php 1 year ago NitroPack.php 7 months ago Settings.php 5 months ago
Settings.php
332 lines
1 <?php
2
3 namespace NitroPack\WordPress;
4 use NitroPack\WordPress\Settings\Subscription;
5 use NitroPack\WordPress\Settings\CacheWarmup;
6 use NitroPack\WordPress\Settings\Optimizations;
7 use NitroPack\WordPress\Settings\OptimizationLevel;
8 use NitroPack\WordPress\Settings\GeneratePreview;
9 use NitroPack\WordPress\Settings\TestMode;
10 use NitroPack\WordPress\Settings\StockRefresh;
11 use NitroPack\WordPress\Settings\Shortcodes;
12 use NitroPack\WordPress\Settings\Logger;
13
14 /**
15 * Class Settings
16 *
17 * This class handles the configuration settings for NitroPack.
18 *
19 * @package NitroPack\WordPress
20 */
21 class Settings {
22 /**
23 * @var array $settings Configuration settings for NitroPack.
24 *
25 * The settings array includes the following keys:
26 * - 'nitropack-webhookToken': (mixed) Token for NitroPack webhook, default is null.
27 * - 'nitropack-enableCompression': (int) Flag to enable compression, default is -1.
28 * - 'nitropack-autoCachePurge': (int) Flag to enable automatic cache purge, default is 1.
29 * - 'nitropack-cacheableObjectTypes': (array) List of cacheable object types, default is an empty array but gets updated immediately to all CPTs.
30 * - 'nitropack-distribution': (string) Distribution type, default is 'regular'.
31 */
32 private $settings;
33
34 public $cache_warmup;
35 /**
36 * Grabs Subscription class
37 * @var Subscription
38 */
39 public $subscription;
40 /**
41 * Grabs Optimizations class
42 * @var Optimizations
43 */
44 public $optimizations;
45 /**
46 * Grabs OptimizationLevel class
47 * @var OptimizationLevel
48 */
49 public $optimization_level;
50
51 /**
52 * Grabs GeneratePreview class
53 * @var GeneratePreview
54 */
55 public $generate_preview;
56
57 /**
58 * Grabs TestMode class
59 * @var TestMode
60 */
61 public $test_mode;
62
63 /**
64 * Grabs StockRefresh class
65 * @var StockRefresh
66 */
67 public $stock_refresh;
68 /**
69 * Grabs Shortcodes class
70 * @var Shortcodes
71 */
72 public $shortcodes;
73 public $logger;
74 /**
75 * Settings constructor.
76 *
77 * Initializes the default required settings for the NitroPack plugin.
78 */
79 function __construct($config = null) {
80 add_action( 'admin_init', [ $this, 'move_existing_options' ] );
81 $this->default_required_settings();
82 //initialize each setting
83 $this->subscription = Subscription::getInstance();
84 $this->optimizations = Optimizations::getInstance();
85 $this->optimization_level = OptimizationLevel::getInstance();
86 $this->cache_warmup = CacheWarmup::getInstance();
87 $this->test_mode = TestMode::getInstance();
88 $this->stock_refresh = StockRefresh::getInstance();
89 $this->generate_preview = GeneratePreview::getInstance();
90 $this->shortcodes = new Shortcodes();
91 $this->logger = new Logger($config);
92 }
93
94 /**
95 * Set default required settings in order for the plugin to work properly.
96 *
97 * @return void
98 */
99 private function default_required_settings() {
100 $this->settings = [
101 'nitropack-webhookToken' => null,
102 'nitropack-enableCompression' => -1,
103 'nitropack-autoCachePurge' => 1,
104 'nitropack-cacheableObjectTypes' => [],
105 'nitropack-distribution' => 'regular',
106 ];
107 }
108
109 /**
110 * Refreshes the required settings for the plugin.
111 *
112 * This method updates the options for the webhook token and iterates through
113 * the settings to update options that are not already set in the WordPress
114 * database. If the option 'nitropack-cacheableObjectTypes' is encountered,
115 * it sets the value to the default cacheable object types.
116 *
117 * @param string|null $token Optional. The token to be used for generating the webhook token. Default is null.
118 * @return void
119 */
120 public function set_required_settings( $token = null ) {
121
122 if ($token !== null) {
123 $this->settings['nitropack-webhookToken'] = $token;
124 } else {
125 // Generate a new webhook token if it is not passed
126 $this->generate_webhook_token();
127 }
128
129 foreach ($this->settings as $option => $value) {
130 if (get_option($option) === false && $value !== null) {
131 if ($option === 'nitropack-cacheableObjectTypes') {
132 $value = nitropack_get_default_cacheable_object_types();
133 }
134 update_option($option, $value);
135 }
136 }
137 }
138 /**
139 * Generates a webhook token for the NitroPack settings.
140 *
141 * This function retrieves the site configuration and checks if a webhook token
142 * is already set. If a token is provided, it generates a new webhook token using
143 * the site ID from the POST request. If no site ID is provided in the POST request,
144 * it sets the webhook token to null.
145 *
146 * @param string|null $token Optional. The token to be used for generating the webhook token.
147 * If not provided, a new token will be generated.
148 */
149 public function generate_webhook_token() {
150 $siteConfig = nitropack_get_site_config();
151 //grab existing from config
152 if (isset($siteConfig['webhookToken'])) {
153 $this->settings['nitropack-webhookToken'] = $siteConfig['webhookToken'];
154 } elseif (isset($siteConfig['siteId'])) {
155 //generate from existing siteId
156 $siteId = $siteConfig['siteId'];
157 $this->settings['nitropack-webhookToken'] = nitropack_generate_webhook_token($siteId);
158 } elseif (!empty($_POST["siteId"])) {
159 //try to generate from POST
160 $siteId = $_POST["siteId"];
161 $this->settings['nitropack-webhookToken'] = nitropack_generate_webhook_token($siteId);
162 } else {
163 $this->settings['nitropack-webhookToken'] = null;
164 }
165 }
166
167 /**
168 * Get NitroPack configuration for ajaxShortcodes
169 *
170 * @return array|null
171 */
172 private function get_nitropack_config_for_ajaxShortcodes() {
173 try {
174 $nitropack = get_nitropack();
175 if (!$nitropack) {
176 throw new \Exception('NitroPack instance not found');
177 }
178
179 $siteConfig = $nitropack->Config->get();
180 $configKey = \NitroPack\WordPress\NitroPack::getConfigKey();
181
182 return isset($siteConfig[$configKey]['options_cache']['ajaxShortcodes']) ? $siteConfig[$configKey]['options_cache']['ajaxShortcodes'] : null;
183 } catch (\Exception $e) {
184 error_log('NitroPack Config Error: ' . $e->getMessage());
185 return null;
186 }
187 }
188 /**
189 * Predefined WooCommerce shortcodes to restrict
190 *
191 * @return array
192 */
193 private function get_restricted_shortcodes() {
194 return [
195 'woocommerce_cart',
196 'woocommerce_my_account',
197 'woocommerce_order_tracking',
198 'woocommerce_checkout',
199 // Add more shortcodes to restrict as needed
200 ];
201 }
202 /**
203 * Generate shortcode options HTML
204 *
205 * @param array $shortcode_tags
206 * @param array $ajax_shortcodes_list
207 * @return string
208 */
209 private function generate_shortcode_options($shortcode_tags, $ajax_shortcodes_list) {
210 $restricted_shortcodes = $this->get_restricted_shortcodes();
211 $html = '';
212
213 foreach ($shortcode_tags as $shortcode => $_) {
214 if (in_array($shortcode, $restricted_shortcodes)) {
215 continue;
216 }
217
218 $selected = in_array($shortcode, $ajax_shortcodes_list) ? 'selected="selected"' : '';
219 $html .= sprintf(
220 '<option value="%s" %s>%s</option>',
221 esc_attr($shortcode),
222 $selected,
223 esc_html($shortcode)
224 );
225 }
226
227 return $html;
228 }
229
230 /**
231 * Generate options for manually added shortcodes
232 *
233 * @param array $freely_added_shortcodes
234 * @return string
235 */
236 private function generate_manual_shortcode_options($freely_added_shortcodes) {
237 return implode('', array_map(function ($shortcode) {
238 return sprintf(
239 '<option value="%s" selected="selected">%s</option>',
240 esc_attr($shortcode),
241 esc_html($shortcode)
242 );
243 }, $freely_added_shortcodes));
244 }
245
246 /**
247 * List all available AJAX shortcodes
248 *
249 * @return string
250 */
251 private function list_ajax_shortcodes() {
252 global $shortcode_tags;
253
254 $config = $this->get_nitropack_config_for_ajaxShortcodes();
255 if (!$config) {
256 return '<option value="" disabled>Configuration not available</option>';
257 }
258
259 $ajax_shortcodes_list = isset($config['shortcodes']) ? $config['shortcodes'] : [];
260 $freely_added_shortcodes = array_diff($ajax_shortcodes_list, array_keys($shortcode_tags));
261
262 $html = $this->generate_shortcode_options($shortcode_tags, $ajax_shortcodes_list);
263
264 if (!empty($freely_added_shortcodes)) {
265 $html .= $this->generate_manual_shortcode_options($freely_added_shortcodes);
266 }
267
268 return $html;
269 }
270
271 /**
272 * Render AJAX shortcodes settings in the admin panel (dashboard.php and dashboard-oneclick.php)
273 */
274 public function render_ajax_shortcodes_setting() {
275 $config = $this->get_nitropack_config_for_ajaxShortcodes();
276 if (!$config) {
277 echo '<div class="error">Unable to load NitroPack Ajax Shortcodes configuration</div>';
278 return;
279 }
280
281 $ajax_shortcodes_enabled = isset($config['enabled']) ? $config['enabled'] : false;
282 $shortcode_container_shown = $ajax_shortcodes_enabled ? '' : 'hidden';
283 ?>
284 <div class="nitro-option-main">
285 <div class="text-box">
286 <h6><?php esc_html_e('Shortcodes exclusions', 'nitropack'); ?></h6>
287 <p><?php esc_html_e('Load widgets, feeds, and any shortcode with AJAX to bypass the cache and always show the latest content.', 'nitropack'); ?></p>
288 </div>
289 <label class="inline-flex items-center cursor-pointer ml-auto">
290 <input type="checkbox"
291 value=""
292 class="sr-only peer"
293 name="ajax_shortcodes"
294 id="ajax-shortcodes"
295 <?php echo $ajax_shortcodes_enabled ? 'checked' : ''; ?>>
296 <div class="toggle"></div>
297 </label>
298 </div>
299 <div class="ajax-shortcodes <?php echo esc_attr($shortcode_container_shown); ?>">
300 <div class="select-wrapper">
301 <select class="shortcode-select"
302 name="nitropack-ajaxShortcodes"
303 id="ajax-shortcodes-dropdown"
304 multiple>
305 <?php echo $this->list_ajax_shortcodes(); ?>
306 </select>
307 <button class="btn btn-primary" id="save-shortcodes">
308 <?php esc_html_e('Save', 'nitropack'); ?>
309 </button>
310 </div>
311 </div>
312
313 <?php
314 }
315
316 /**
317 * Move wrongly formatted nitropack options to correct ones and delete old ones.
318 * @return void
319 */
320 public function move_existing_options() {
321 if (! empty( $_GET['page'] ) && $_GET['page'] === 'nitropack') {
322 $existing_options = [ 'np_warmup_sitemap' => 'nitropack-warmup-sitemap', 'nitropack_minimumLogLevel' => 'nitropack-minimumLogLevel' ];
323 foreach ( $existing_options as $option => $new_option ) {
324 if ( $old_option = get_option( $option ) ) {
325 update_option( $new_option, $old_option );
326 delete_option( $option );
327 }
328 }
329 }
330 }
331 }
332