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