PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.12.0
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.12.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 / Integration / Plugin / GravityForms.php
nitropack / classes / Integration / Plugin Last commit date
AdvancedMathCaptcha.php 3 years ago AeliaCurrencySwitcher.php 3 years ago BeaverBuilder.php 4 years ago CF_Helper.php 3 years ago Cloudflare.php 2 years ago CommonHelpers.php 3 years ago CookieNotice.php 4 years ago DownloadManager.php 4 years ago Elementor.php 3 years ago Ezoic.php 4 years ago FusionBuilder.php 4 years ago GeoTargetingWP.php 2 years ago GravityForms.php 2 years ago JetPackNP.php 3 years ago NginxHelper.php 2 years ago RC.php 3 years ago RankMathNP.php 3 years ago RocketNet_Helper.php 4 years ago ShortPixel.php 4 years ago SquirrlySEO.php 3 years ago TheEventsCalendar.php 3 years ago ThriveTheme.php 4 years ago WCML.php 3 years ago WPBakeryNP.php 3 years ago WPCacheHelper.php 3 years ago WPForms.php 3 years ago WPRocket.php 4 years ago Woocommerce.php 4 years ago WoocommerceCacheHandler.php 4 years ago YoastSEO.php 3 years ago
GravityForms.php
294 lines
1 <?php
2 /**
3 * GravityForms Class
4 *
5 * @package nitropack
6 */
7
8 namespace NitroPack\Integration\Plugin;
9
10 use WP_Block_Type_Registry;
11 use WP_block;
12
13 /**
14 * GravityForms Class
15 */
16 class GravityForms {
17
18 const STAGE = 'late';
19
20
21 /**
22 * Check if plugin "Gravity Forms" is active
23 *
24 * @return bool
25 */
26 public static function isActive() { //phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
27
28 return class_exists( '\GFForms' ) ;
29 }
30
31 /**
32 * Initialize the integration
33 *
34 * @param string $stage Stage.
35 *
36 * @return void
37 */
38 public function init( $stage ) { //phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
39
40 if ($this -> isActive()) {
41
42 add_filter( 'template_redirect', [$this, 'output_filter']);
43
44 if (!wp_doing_ajax() && !wp_is_json_request()) {
45 add_filter( 'register_block_type_args', [$this, 'gf_block_type_args'], 999 ,2 );
46
47 add_action( 'init', function() {
48 remove_shortcode('gravityform');
49 remove_shortcode('gravityforms');
50 add_shortcode('gravityform', [$this, 'modify_gf_shortcode']);
51 add_shortcode('gravityforms', [$this, 'modify_gf_shortcode']);
52 }, 99);
53 } else {
54 add_action( 'wp_ajax_nitropack_gf_block_output_ajax', [$this, 'block_output_ajax'] );
55 add_action( 'wp_ajax_nopriv_nitropack_gf_block_output_ajax', [$this, 'block_output_ajax'] );
56 add_action( 'wp_ajax_nitropack_gf_shortcode_output_ajax', [$this, 'shortcode_output_ajax'] );
57 add_action( 'wp_ajax_nopriv_nitropack_gf_shortcode_output_ajax', [$this, 'shortcode_output_ajax'] );
58 }
59 }
60 }
61
62 /**
63 * Filter for output content.
64 *
65 * @return void
66 */
67 public function output_filter() {
68
69 global $post;
70
71 // Running only for single posts (any type) and pages.
72 if ( ! is_singular() ) {
73 return;
74 }
75
76 // Gravity Forms form detected? Enqueue scripts and exit.
77 if ( false !== $this->check_gf( $post ) ) {
78
79 wp_enqueue_script( 'nitropack-gf-ajax-script', NITROPACK_PLUGIN_DIR_URL . 'view/javascript/gravity_forms.js?np_v=' . NITROPACK_VERSION, array('jquery'), NITROPACK_VERSION, true );
80 wp_localize_script( 'nitropack-gf-ajax-script', 'nitropack_gf_ajax', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
81
82 return;
83 }
84 }
85
86 /**
87 * Check if post/page has a GF shortcode or block.
88 *
89 * @param object $post Post Object.
90 */
91 public function check_gf( $post ) {
92
93 // Check for GF shortcode.
94 if ( true === $this->find_gf_shortcode( $post->post_content ) ) {
95 return true;
96 }
97
98 // Check for a GF block or GF form in a reusable block.
99 if ( function_exists( 'has_block' ) && true === has_blocks( $post->ID ) ) {
100
101 // Check for GF blocks.
102 if ( true === $this->find_gf_block( $post->post_content ) ) {
103 return true;
104 }
105
106 // Additional check for GF forms in reusable blocks.
107 $blocks = parse_blocks( $post->post_content );
108
109 foreach ( $blocks as $block ) {
110
111 // Skip block if empty or not a core/block.
112 if ( empty( $block['blockName'] ) || 'core/block' !== $block['blockName'] || empty( $block['attrs']['ref'] ) ) {
113 continue;
114 }
115
116 // Check core/block found.
117 $reusable_block = get_post( $block['attrs']['ref'] );
118
119 if ( empty( $reusable_block ) || 'wp_block' !== $reusable_block->post_type ) {
120 continue;
121 }
122
123 if ( true === $this->find_gf_shortcode( $reusable_block->post_content ) || true === $this->find_gf_block( $reusable_block->post_content ) ) {
124 return true;
125 }
126 }
127 }
128
129 // If we're here, no form was detected.
130 return false;
131 }
132
133 /**
134 * Check post content provided for a GF shortcode.
135 *
136 * @param string $post_content Post content.
137 */
138 public function find_gf_shortcode( $post_content ) {
139
140 // Check for a GF shortcode.
141 if ( has_shortcode( $post_content, 'gravityform' ) || has_shortcode( $post_content, 'gravityforms')) {
142 // Shortcode found!
143 return true;
144 }
145 // If we're here, there's no GF shortcode.
146 return false;
147 }
148
149 /**
150 * Check post content provided for a GF block.
151 *
152 * @param string $post_content Post content.
153 */
154 public function find_gf_block( $post_content ) {
155
156 // Get GF blocks registered.
157 $gf_blocks = $this -> get_block_registered_names_list();
158
159 // Checking for GF blocks.
160 foreach ( $gf_blocks as $gf_block ) {
161
162 if ( has_block( $gf_block, $post_content ) ) {
163 // Block found!
164 return true;
165 }
166 }
167 // If we're here, there's no GF block.
168 return false;
169 }
170
171 /**
172 * Override gravity forms block render callback
173 *
174 * @param array $args Arguments for block.
175 * @param string $name Name of block.
176 *
177 * @return mixed
178 */
179 public function gf_block_type_args( $args, $name ) {
180
181 if ( strpos( $name, 'gravityforms/' ) !== false) {
182
183 $args['render_callback'] = [$this, 'modify_gf_block'];
184 }
185 return $args;
186 }
187
188 /**
189 * Modify gravity forms block render callback
190 *
191 * @param array $attributes The block attrbutes.
192 * @param string $content The block content.
193 * @param WP_block $block The block object.
194 *
195 * @return mixed
196 */
197 public function modify_gf_block($attributes, $content, WP_block $block = null) {
198
199 return '<div class="nitropack-gravityforms-block" data-block-name="'.esc_attr($block -> name).'" data-block-attributes="'.esc_attr(json_encode($attributes)).'"><img src="'.esc_url(NITROPACK_PLUGIN_DIR_URL . 'view/images/loading.gif').'" alt="loading" /></div>';
200 }
201
202 /**
203 * Get an array of the names of all registered blocks of Gravity Forms
204 *
205 * @return array $pattern_names
206 */
207 private function get_block_registered_names_list() {
208
209 $get_patterns = WP_Block_Type_Registry::get_instance()->get_all_registered();
210
211 $pattern_names = [];
212
213 if ($get_patterns) {
214 foreach ($get_patterns as $pattern) {
215 $pattern = (array) $pattern;
216 $block_name = $pattern['name'];
217
218 if (strpos($block_name, 'gravityforms/') !== false) {
219 $pattern_names[] = $block_name;
220 }
221 }
222 }
223
224 return $pattern_names;
225 }
226
227 /**
228 * Output ajax for Gravity Forms block
229 *
230 * @return void
231 */
232 public function block_output_ajax(){
233
234 $block_name = isset($_GET['block_name']) ? sanitize_text_field(wp_unslash($_GET['block_name'])) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended
235 $block_attributes = isset($_GET['block_attributes']) ? sanitize_text_field(wp_unslash($_GET['block_attributes'])) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended
236
237 if (!empty($block_name) && !empty($block_attributes)) {
238
239 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name );
240
241 if ( $block_type && !empty( $block_type ) ) {
242
243 $block_attributes = json_decode($block_attributes, true);
244
245 $block_attributes['ajax'] = 'true';
246
247 $block_shortcode = $block_type -> render($block_attributes, '');
248
249 echo do_shortcode( $block_shortcode);
250
251 }
252 }
253
254 wp_die();
255 }
256
257
258 /**
259 * Override gravity forms shortcode render callback
260 *
261 * @param array $atts Attributes for shortcode.
262 * @param string $content Content of shortcode.
263 *
264 * @return string
265 */
266 public function modify_gf_shortcode($atts, $content = null ) {
267
268 return '<div class="nitropack-gravityforms-shortcode" data-shortcode-attributes="'.esc_attr(json_encode($atts)).'"><img src="'.esc_url(NITROPACK_PLUGIN_DIR_URL . 'view/images/loading.gif').'" alt="loading" /></div>';
269 }
270
271 /**
272 * Output ajax for Gravity Forms shortcode
273 *
274 * @return void
275 */
276 public function shortcode_output_ajax(){
277
278 $shortcode_attributes = isset($_GET['shortcode-attributes']) ? sanitize_text_field(wp_unslash($_GET['shortcode-attributes'])) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended
279
280 $shortcode_attributes = json_decode($shortcode_attributes, true);
281
282 if (!empty($shortcode_attributes)) {
283
284 $shortcode_attributes['ajax'] = 'true';
285
286 $shortcode_attribute_string = implode(' ', array_map(static function ($k, $v) { return "$k=\"$v\""; }, array_keys($shortcode_attributes), $shortcode_attributes));
287
288 echo do_shortcode( '[gravityform '.$shortcode_attribute_string.']');
289 }
290
291 wp_die();
292 }
293 }
294