PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.17.6
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.17.6
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 / Shortcodes.php
nitropack / classes / WordPress / Settings Last commit date
Components.php 1 year ago Logger.php 1 year ago Shortcodes.php 1 year ago
Shortcodes.php
201 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 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"
141 value=""
142 class="sr-only peer"
143 name="ajax_shortcodes"
144 id="ajax-shortcodes"
145 <?php echo ($ajax_shortcodes_enabled ? 'checked' : '') ?>
146 >
147 <div class="toggle"></div>
148 </label>
149 </div>
150 <div class="ajax-shortcodes <?php echo esc_attr( $shortcode_container_shown ); ?>">
151 <div class="select-wrapper">
152 <select class="shortcode-select" name="nitropack-ajaxShortcodes" id="ajax-shortcodes-dropdown" multiple>
153 <?php echo $this->list_ajax_shortcodes(); ?>
154 </select>
155 <button class="btn btn-primary" id="save-shortcodes">
156 <?php esc_html_e( 'Save', 'nitropack' ); ?>
157 </button>
158 </div>
159 </div>
160
161 <?php
162 }
163 public function nitropack_set_ajax_shortcodes_ajax() {
164 nitropack_verify_ajax_nonce( $_REQUEST );
165
166 $new_shortcodes = isset( $_POST['shortcodes'] ) ? $_POST['shortcodes'] : null;
167 $enabled = isset( $_POST['enabled'] ) ? $_POST['enabled'] : null;
168
169 if ( $new_shortcodes === null && $enabled === null ) {
170 nitropack_json_and_exit( array(
171 "type" => "error",
172 "message" => nitropack_admin_toast_msgs( 'error' )
173 ) );
174 }
175 $nitropack = get_nitropack();
176 $siteConfig = $nitropack->Config->get();
177 $configKey = \NitroPack\WordPress\NitroPack::getConfigKey();
178
179 if ( ! is_null( $enabled ) ) {
180 $siteConfig[ $configKey ]['options_cache']['ajaxShortcodes']['enabled'] = $enabled === '1';
181 }
182
183 if ( ! is_null( $new_shortcodes ) ) {
184 /* If the user has cleared the input field, we should set the shortcodes to an empty array */
185 $existing_options = ( is_array( $new_shortcodes ) && $new_shortcodes[0] === '' ) ? [] : $new_shortcodes;
186 /* update config.json */
187 $siteConfig[ $configKey ]['options_cache']['ajaxShortcodes']['shortcodes'] = $existing_options;
188 }
189 $updated = $nitropack->Config->set( $siteConfig );
190
191 if ( $updated ) {
192 nitropack_json_and_exit( array( "type" => "success", "message" => nitropack_admin_toast_msgs( 'success' ) ) );
193 } else {
194 nitropack_json_and_exit( array(
195 "type" => "error",
196 "message" => nitropack_admin_toast_msgs( 'error' )
197 ) );
198 }
199 }
200 }
201