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