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