CacheWarmup.php
7 months ago
Components.php
1 year ago
GeneratePreview.php
7 months ago
Logger.php
7 months ago
OptimizationLevel.php
7 months ago
Optimizations.php
5 months ago
Shortcodes.php
1 year ago
StockRefresh.php
5 months ago
Subscription.php
6 months ago
TestMode.php
10 months ago
Shortcodes.php
197 lines
| 1 | <?php |
| 2 | |
| 3 | namespace NitroPack\WordPress\Settings; |
| 4 | |
| 5 | /** |
| 6 | * Class Shortcodes |
| 7 | * |
| 8 | * The shortcode settings are stored in config.json! |
| 9 | * |
| 10 | * @package NitroPack\WordPress\Settings |
| 11 | */ |
| 12 | class Shortcodes { |
| 13 | public function __construct() { |
| 14 | add_action( 'wp_ajax_nitropack_set_ajax_shortcodes_ajax', [ $this, 'nitropack_set_ajax_shortcodes_ajax' ] ); |
| 15 | } |
| 16 | /** |
| 17 | * Get NitroPack configuration for ajaxShortcodes |
| 18 | * |
| 19 | * @return array|null |
| 20 | */ |
| 21 | private function get_nitropack_config_for_ajaxShortcodes() { |
| 22 | try { |
| 23 | $nitropack = get_nitropack(); |
| 24 | if ( ! $nitropack ) { |
| 25 | throw new \Exception( 'NitroPack instance not found' ); |
| 26 | } |
| 27 | |
| 28 | $siteConfig = $nitropack->Config->get(); |
| 29 | $configKey = \NitroPack\WordPress\NitroPack::getConfigKey(); |
| 30 | |
| 31 | return isset( $siteConfig[ $configKey ]['options_cache']['ajaxShortcodes'] ) ? $siteConfig[ $configKey ]['options_cache']['ajaxShortcodes'] : null; |
| 32 | } catch (\Exception $e) { |
| 33 | error_log( 'NitroPack Config Error: ' . $e->getMessage() ); |
| 34 | return null; |
| 35 | } |
| 36 | } |
| 37 | /** |
| 38 | * Predefined WooCommerce shortcodes to restrict |
| 39 | * |
| 40 | * @return array |
| 41 | */ |
| 42 | private function get_restricted_shortcodes() { |
| 43 | return [ |
| 44 | 'woocommerce_cart', |
| 45 | 'woocommerce_my_account', |
| 46 | 'woocommerce_order_tracking', |
| 47 | 'woocommerce_checkout', |
| 48 | // Add more shortcodes to restrict as needed |
| 49 | ]; |
| 50 | } |
| 51 | /** |
| 52 | * Generate shortcode options HTML |
| 53 | * |
| 54 | * @param array $shortcode_tags |
| 55 | * @param array $ajax_shortcodes_list |
| 56 | * @return string |
| 57 | */ |
| 58 | private function generate_shortcode_options( $shortcode_tags, $ajax_shortcodes_list ) { |
| 59 | $restricted_shortcodes = $this->get_restricted_shortcodes(); |
| 60 | $html = ''; |
| 61 | |
| 62 | foreach ( $shortcode_tags as $shortcode => $_ ) { |
| 63 | if ( in_array( $shortcode, $restricted_shortcodes ) ) { |
| 64 | continue; |
| 65 | } |
| 66 | |
| 67 | $selected = in_array( $shortcode, $ajax_shortcodes_list ) ? 'selected="selected"' : ''; |
| 68 | $html .= sprintf( |
| 69 | '<option value="%s" %s>%s</option>', |
| 70 | esc_attr( $shortcode ), |
| 71 | $selected, |
| 72 | esc_html( $shortcode ) |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | return $html; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Generate options for manually added shortcodes |
| 81 | * |
| 82 | * @param array $freely_added_shortcodes |
| 83 | * @return string |
| 84 | */ |
| 85 | private function generate_manual_shortcode_options( $freely_added_shortcodes ) { |
| 86 | return implode( '', array_map( function ($shortcode) { |
| 87 | return sprintf( |
| 88 | '<option value="%s" selected="selected">%s</option>', |
| 89 | esc_attr( $shortcode ), |
| 90 | esc_html( $shortcode ) |
| 91 | ); |
| 92 | }, $freely_added_shortcodes ) ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * List all available AJAX shortcodes |
| 97 | * |
| 98 | * @return string |
| 99 | */ |
| 100 | private function list_ajax_shortcodes() { |
| 101 | global $shortcode_tags; |
| 102 | |
| 103 | $config = $this->get_nitropack_config_for_ajaxShortcodes(); |
| 104 | if ( ! $config ) { |
| 105 | return '<option value="" disabled>Configuration not available</option>'; |
| 106 | } |
| 107 | |
| 108 | $ajax_shortcodes_list = isset( $config['shortcodes'] ) ? $config['shortcodes'] : []; |
| 109 | $freely_added_shortcodes = array_diff( $ajax_shortcodes_list, array_keys( $shortcode_tags ) ); |
| 110 | |
| 111 | $html = $this->generate_shortcode_options( $shortcode_tags, $ajax_shortcodes_list ); |
| 112 | |
| 113 | if ( ! empty( $freely_added_shortcodes ) ) { |
| 114 | $html .= $this->generate_manual_shortcode_options( $freely_added_shortcodes ); |
| 115 | } |
| 116 | |
| 117 | return $html; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Render AJAX shortcodes settings in the admin panel (dashboard.php and dashboard-oneclick.php) |
| 122 | */ |
| 123 | public function render() { |
| 124 | $config = $this->get_nitropack_config_for_ajaxShortcodes(); |
| 125 | if ( ! $config ) { |
| 126 | echo '<div class="error">Unable to load NitroPack Ajax Shortcodes configuration</div>'; |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | $ajax_shortcodes_enabled = isset( $config['enabled'] ) ? $config['enabled'] : false; |
| 131 | $shortcode_container_shown = $ajax_shortcodes_enabled ? '' : 'hidden'; |
| 132 | ?> |
| 133 | <div class="nitro-option-main"> |
| 134 | <div class="text-box"> |
| 135 | <h6><?php esc_html_e( 'Shortcodes exclusions', 'nitropack' ); ?></h6> |
| 136 | <p><?php esc_html_e( 'Load widgets, feeds, and any shortcode with AJAX to bypass the cache and always show the latest content.', 'nitropack' ); ?> |
| 137 | </p> |
| 138 | </div> |
| 139 | <label class="inline-flex items-center cursor-pointer ml-auto"> |
| 140 | <input type="checkbox" value="" class="sr-only peer" name="ajax_shortcodes" id="ajax-shortcodes" <?php echo ( $ajax_shortcodes_enabled ? 'checked' : '' ) ?>> |
| 141 | <div class="toggle"></div> |
| 142 | </label> |
| 143 | </div> |
| 144 | <div class="ajax-shortcodes <?php echo esc_attr( $shortcode_container_shown ); ?>"> |
| 145 | <div class="select-wrapper"> |
| 146 | <select class="shortcode-select" name="nitropack-ajaxShortcodes" id="ajax-shortcodes-dropdown" multiple> |
| 147 | <?php echo $this->list_ajax_shortcodes(); ?> |
| 148 | </select> |
| 149 | <button class="btn btn-primary" id="save-shortcodes"> |
| 150 | <?php esc_html_e( 'Save', 'nitropack' ); ?> |
| 151 | </button> |
| 152 | </div> |
| 153 | </div> |
| 154 | |
| 155 | <?php |
| 156 | } |
| 157 | public function nitropack_set_ajax_shortcodes_ajax() { |
| 158 | nitropack_verify_ajax_nonce( $_REQUEST ); |
| 159 | |
| 160 | $new_shortcodes = isset( $_POST['shortcodes'] ) ? $_POST['shortcodes'] : null; |
| 161 | $enabled = isset( $_POST['enabled'] ) ? $_POST['enabled'] : null; |
| 162 | |
| 163 | if ( $new_shortcodes === null && $enabled === null ) { |
| 164 | nitropack_json_and_exit( array( |
| 165 | "type" => "error", |
| 166 | "message" => nitropack_admin_toast_msgs( 'error' ) |
| 167 | ) ); |
| 168 | } |
| 169 | $nitropack = get_nitropack(); |
| 170 | $siteConfig = $nitropack->Config->get(); |
| 171 | $configKey = \NitroPack\WordPress\NitroPack::getConfigKey(); |
| 172 | |
| 173 | if ( ! is_null( $enabled ) ) { |
| 174 | $siteConfig[ $configKey ]['options_cache']['ajaxShortcodes']['enabled'] = $enabled === '1'; |
| 175 | } |
| 176 | |
| 177 | if ( ! is_null( $new_shortcodes ) ) { |
| 178 | |
| 179 | /* If the user has cleared the input field, we should set the shortcodes to an empty array */ |
| 180 | $existing_options = ( is_array( $new_shortcodes ) && $new_shortcodes[0] !== '[]' ) ? $new_shortcodes : []; |
| 181 | |
| 182 | /* update config.json */ |
| 183 | $siteConfig[ $configKey ]['options_cache']['ajaxShortcodes']['shortcodes'] = $existing_options; |
| 184 | } |
| 185 | $updated = $nitropack->Config->set( $siteConfig ); |
| 186 | |
| 187 | if ( $updated ) { |
| 188 | nitropack_json_and_exit( array( "type" => "success", "message" => nitropack_admin_toast_msgs( 'success' ) ) ); |
| 189 | } else { |
| 190 | nitropack_json_and_exit( array( |
| 191 | "type" => "error", |
| 192 | "message" => nitropack_admin_toast_msgs( 'error' ) |
| 193 | ) ); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 |