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