PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.18.0
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.18.0
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 TestMode.php 1 year 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