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