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