ACF.php
2 weeks ago
AdvancedMathCaptcha.php
1 month ago
AeliaCurrencySwitcher.php
11 months ago
BeaverBuilder.php
1 year ago
CF_Helper.php
5 months ago
CURCY_MultiCurrency.php
1 year ago
Cloudflare.php
5 months ago
CommonHelpers.php
1 year ago
CookieNotice.php
1 year ago
DownloadManager.php
1 year ago
Elementor.php
5 months ago
Ezoic.php
1 year ago
FusionBuilder.php
1 year ago
GeoTargetingWP.php
1 year ago
GravityForms.php
1 month ago
JetPackNP.php
1 year ago
MPG.php
11 months ago
NginxHelper.php
1 year ago
RC.php
11 months ago
RankMathNP.php
1 year ago
ShortPixel.php
1 year ago
SquirrlySEO.php
1 year ago
TheEventsCalendar.php
1 month ago
ThriveTheme.php
1 year ago
WCML.php
1 year ago
WPBakeryNP.php
1 year ago
WPCacheHelper.php
1 year ago
WPForms.php
1 year ago
WPML.php
2 weeks ago
WPRocket.php
1 year ago
WooCommerce.php
2 weeks ago
WoocommerceCacheHandler.php
1 year ago
YoastSEO.php
1 year ago
GravityForms.php
360 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 | const BLOCK_AJAX_NONCE_ACTION = 'nitropack_gf_block_output_ajax'; |
| 20 | const SHORTCODE_AJAX_NONCE_ACTION = 'nitropack_gf_shortcode_output_ajax'; |
| 21 | |
| 22 | |
| 23 | /** |
| 24 | * Check if plugin "Gravity Forms" is active |
| 25 | * |
| 26 | * @return bool |
| 27 | */ |
| 28 | public static function isActive() { //phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid |
| 29 | |
| 30 | return class_exists( '\GFForms' ) ; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Initialize the integration |
| 35 | * |
| 36 | * @param string $stage Stage. |
| 37 | * |
| 38 | * @return void |
| 39 | */ |
| 40 | public function init( $stage ) { //phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found |
| 41 | |
| 42 | if ($this -> isActive()) { |
| 43 | |
| 44 | add_filter( 'template_redirect', [$this, 'output_filter']); |
| 45 | |
| 46 | if (!wp_doing_ajax() && !wp_is_json_request()) { |
| 47 | add_filter( 'register_block_type_args', [$this, 'gf_block_type_args'], 999 ,2 ); |
| 48 | |
| 49 | add_action( 'init', function() { |
| 50 | remove_shortcode('gravityform'); |
| 51 | remove_shortcode('gravityforms'); |
| 52 | add_shortcode('gravityform', [$this, 'modify_gf_shortcode']); |
| 53 | add_shortcode('gravityforms', [$this, 'modify_gf_shortcode']); |
| 54 | }, 99); |
| 55 | } else { |
| 56 | add_action( 'wp_ajax_nitropack_gf_block_output_ajax', [$this, 'block_output_ajax'] ); |
| 57 | add_action( 'wp_ajax_nopriv_nitropack_gf_block_output_ajax', [$this, 'block_output_ajax'] ); |
| 58 | add_action( 'wp_ajax_nitropack_gf_shortcode_output_ajax', [$this, 'shortcode_output_ajax'] ); |
| 59 | add_action( 'wp_ajax_nopriv_nitropack_gf_shortcode_output_ajax', [$this, 'shortcode_output_ajax'] ); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Filter for output content. |
| 66 | * |
| 67 | * @return void |
| 68 | */ |
| 69 | public function output_filter() { |
| 70 | |
| 71 | global $post; |
| 72 | |
| 73 | // Running only for single posts (any type) and pages. |
| 74 | if ( ! is_singular() ) { |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | // Gravity Forms form detected? Enqueue scripts and exit. |
| 79 | if ( false !== $this->check_gf( $post ) ) { |
| 80 | |
| 81 | wp_enqueue_script( 'nitropack-gf-ajax-script', NITROPACK_PLUGIN_DIR_URL . 'assets/js/gravity_forms.min.js?np_v=' . NITROPACK_VERSION, array('jquery'), NITROPACK_VERSION, true ); |
| 82 | wp_localize_script( 'nitropack-gf-ajax-script', 'nitropack_gf_ajax', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); |
| 83 | |
| 84 | return; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Check if post/page has a GF shortcode or block. |
| 90 | * |
| 91 | * @param object $post Post Object. |
| 92 | */ |
| 93 | public function check_gf( $post ) { |
| 94 | |
| 95 | // Check for GF shortcode. |
| 96 | if ( true === $this->find_gf_shortcode( $post->post_content ) ) { |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | // Check for a GF block or GF form in a reusable block. |
| 101 | if ( function_exists( 'has_block' ) && true === has_blocks( $post->ID ) ) { |
| 102 | |
| 103 | // Check for GF blocks. |
| 104 | if ( true === $this->find_gf_block( $post->post_content ) ) { |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | // Additional check for GF forms in reusable blocks. |
| 109 | $blocks = parse_blocks( $post->post_content ); |
| 110 | |
| 111 | foreach ( $blocks as $block ) { |
| 112 | |
| 113 | // Skip block if empty or not a core/block. |
| 114 | if ( empty( $block['blockName'] ) || 'core/block' !== $block['blockName'] || empty( $block['attrs']['ref'] ) ) { |
| 115 | continue; |
| 116 | } |
| 117 | |
| 118 | // Check core/block found. |
| 119 | $reusable_block = get_post( $block['attrs']['ref'] ); |
| 120 | |
| 121 | if ( empty( $reusable_block ) || 'wp_block' !== $reusable_block->post_type ) { |
| 122 | continue; |
| 123 | } |
| 124 | |
| 125 | if ( true === $this->find_gf_shortcode( $reusable_block->post_content ) || true === $this->find_gf_block( $reusable_block->post_content ) ) { |
| 126 | return true; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // If we're here, no form was detected. |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Check post content provided for a GF shortcode. |
| 137 | * |
| 138 | * @param string $post_content Post content. |
| 139 | */ |
| 140 | public function find_gf_shortcode( $post_content ) { |
| 141 | |
| 142 | // Check for a GF shortcode. |
| 143 | if ( has_shortcode( $post_content, 'gravityform' ) || has_shortcode( $post_content, 'gravityforms')) { |
| 144 | // Shortcode found! |
| 145 | return true; |
| 146 | } |
| 147 | // If we're here, there's no GF shortcode. |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Check post content provided for a GF block. |
| 153 | * |
| 154 | * @param string $post_content Post content. |
| 155 | */ |
| 156 | public function find_gf_block( $post_content ) { |
| 157 | |
| 158 | // Get GF blocks registered. |
| 159 | $gf_blocks = $this -> get_block_registered_names_list(); |
| 160 | |
| 161 | // Checking for GF blocks. |
| 162 | foreach ( $gf_blocks as $gf_block ) { |
| 163 | |
| 164 | if ( has_block( $gf_block, $post_content ) ) { |
| 165 | // Block found! |
| 166 | return true; |
| 167 | } |
| 168 | } |
| 169 | // If we're here, there's no GF block. |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Override gravity forms block render callback |
| 175 | * |
| 176 | * @param array $args Arguments for block. |
| 177 | * @param string $name Name of block. |
| 178 | * |
| 179 | * @return mixed |
| 180 | */ |
| 181 | public function gf_block_type_args( $args, $name ) { |
| 182 | |
| 183 | if ( strpos( $name, 'gravityforms/' ) !== false) { |
| 184 | |
| 185 | $args['render_callback'] = [$this, 'modify_gf_block']; |
| 186 | } |
| 187 | return $args; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Modify gravity forms block render callback |
| 192 | * |
| 193 | * @param array $attributes The block attrbutes. |
| 194 | * @param string $content The block content. |
| 195 | * @param WP_Block $block The block object. |
| 196 | * |
| 197 | * @return mixed |
| 198 | */ |
| 199 | public function modify_gf_block($attributes, $content, $block = null) { |
| 200 | |
| 201 | $block_name = ! empty( $block->name ) ? $block->name : ''; |
| 202 | $block_attributes = wp_json_encode( $attributes ); |
| 203 | |
| 204 | if ( false === $block_attributes ) { |
| 205 | $block_attributes = '{}'; |
| 206 | } |
| 207 | |
| 208 | $block_nonce = wp_create_nonce( $this->get_block_nonce_action( $block_name, $block_attributes ) ); |
| 209 | |
| 210 | return '<div class="nitropack-gravityforms-block" data-block-name="' . esc_attr( $block_name ) . '" data-block-attributes="' . esc_attr( $block_attributes ) . '" data-block-nonce="' . esc_attr( $block_nonce ) . '"><img src="' . esc_url( NITROPACK_PLUGIN_DIR_URL . 'assets/img/loading.gif' ) . '" alt="loading" /></div>'; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Build nonce action for Gravity Forms block output. |
| 215 | * |
| 216 | * @param string $block_name The block name. |
| 217 | * @param string $block_attributes The block attributes as JSON. |
| 218 | * |
| 219 | * @return string |
| 220 | */ |
| 221 | private function get_block_nonce_action( $block_name, $block_attributes ) { |
| 222 | return self::BLOCK_AJAX_NONCE_ACTION . '|' . $block_name . '|' . wp_hash( $block_attributes, 'nonce' ); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Get an array of the names of all registered blocks of Gravity Forms |
| 227 | * |
| 228 | * @return array $pattern_names |
| 229 | */ |
| 230 | private function get_block_registered_names_list() { |
| 231 | |
| 232 | $get_patterns = WP_Block_Type_Registry::get_instance()->get_all_registered(); |
| 233 | |
| 234 | $pattern_names = []; |
| 235 | |
| 236 | if ($get_patterns) { |
| 237 | foreach ($get_patterns as $pattern) { |
| 238 | $pattern = (array) $pattern; |
| 239 | $block_name = $pattern['name']; |
| 240 | |
| 241 | if (strpos($block_name, 'gravityforms/') !== false) { |
| 242 | $pattern_names[] = $block_name; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | return $pattern_names; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Output ajax for Gravity Forms block |
| 252 | * |
| 253 | * @return void |
| 254 | */ |
| 255 | public function block_output_ajax(){ |
| 256 | |
| 257 | $block_name = isset( $_GET['block_name'] ) ? sanitize_text_field( wp_unslash( $_GET['block_name'] ) ) : ''; |
| 258 | $block_attributes = isset( $_GET['block_attributes'] ) && is_string( $_GET['block_attributes'] ) ? wp_unslash( $_GET['block_attributes'] ) : ''; |
| 259 | $block_nonce = isset( $_GET['block_nonce'] ) ? sanitize_text_field( wp_unslash( $_GET['block_nonce'] ) ) : ''; |
| 260 | |
| 261 | if ( |
| 262 | empty( $block_name ) || |
| 263 | empty( $block_attributes ) || |
| 264 | empty( $block_nonce ) || |
| 265 | 0 !== strpos( $block_name, 'gravityforms/' ) || |
| 266 | ! wp_verify_nonce( $block_nonce, $this->get_block_nonce_action( $block_name, $block_attributes ) ) |
| 267 | ) { |
| 268 | wp_die(); |
| 269 | } |
| 270 | |
| 271 | if (!empty($block_name) && !empty($block_attributes)) { |
| 272 | |
| 273 | $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name ); |
| 274 | |
| 275 | if ( $block_type && !empty( $block_type ) ) { |
| 276 | |
| 277 | $block_attributes = json_decode($block_attributes, true); |
| 278 | |
| 279 | if ( ! is_array( $block_attributes ) ) { |
| 280 | wp_die(); |
| 281 | } |
| 282 | |
| 283 | $block_attributes['ajax'] = 'true'; |
| 284 | |
| 285 | $block_shortcode = $block_type -> render($block_attributes, ''); |
| 286 | |
| 287 | echo do_shortcode( $block_shortcode); |
| 288 | |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | wp_die(); |
| 293 | } |
| 294 | |
| 295 | |
| 296 | /** |
| 297 | * Override gravity forms shortcode render callback |
| 298 | * |
| 299 | * @param array $atts Attributes for shortcode. |
| 300 | * @param string $content Content of shortcode. |
| 301 | * |
| 302 | * @return string |
| 303 | */ |
| 304 | public function modify_gf_shortcode($atts, $content = null ) { |
| 305 | |
| 306 | $shortcode_attributes = wp_json_encode( $atts ); |
| 307 | |
| 308 | if ( false === $shortcode_attributes ) { |
| 309 | $shortcode_attributes = '{}'; |
| 310 | } |
| 311 | |
| 312 | $shortcode_nonce = wp_create_nonce( $this->get_shortcode_nonce_action( $shortcode_attributes ) ); |
| 313 | |
| 314 | return '<div class="nitropack-gravityforms-shortcode" data-shortcode-attributes="' . esc_attr( $shortcode_attributes ) . '" data-shortcode-nonce="' . esc_attr( $shortcode_nonce ) . '"><img src="' . esc_url( NITROPACK_PLUGIN_DIR_URL . 'assets/img/loading.gif' ) . '" alt="loading" /></div>'; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Build nonce action for Gravity Forms shortcode output. |
| 319 | * |
| 320 | * @param string $shortcode_attributes The shortcode attributes as JSON. |
| 321 | * |
| 322 | * @return string |
| 323 | */ |
| 324 | private function get_shortcode_nonce_action( $shortcode_attributes ) { |
| 325 | return self::SHORTCODE_AJAX_NONCE_ACTION . '|' . wp_hash( $shortcode_attributes, 'nonce' ); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Output ajax for Gravity Forms shortcode |
| 330 | * |
| 331 | * @return void |
| 332 | */ |
| 333 | public function shortcode_output_ajax(){ |
| 334 | |
| 335 | $shortcode_attributes = isset( $_GET['shortcode-attributes'] ) && is_string( $_GET['shortcode-attributes'] ) ? wp_unslash( $_GET['shortcode-attributes'] ) : ''; |
| 336 | $shortcode_nonce = isset( $_GET['shortcode_nonce'] ) ? sanitize_text_field( wp_unslash( $_GET['shortcode_nonce'] ) ) : ''; |
| 337 | |
| 338 | if ( |
| 339 | empty( $shortcode_attributes ) || |
| 340 | empty( $shortcode_nonce ) || |
| 341 | ! wp_verify_nonce( $shortcode_nonce, $this->get_shortcode_nonce_action( $shortcode_attributes ) ) |
| 342 | ) { |
| 343 | wp_die(); |
| 344 | } |
| 345 | |
| 346 | $shortcode_attributes = json_decode($shortcode_attributes, true); |
| 347 | |
| 348 | if (!empty($shortcode_attributes)) { |
| 349 | |
| 350 | $shortcode_attributes['ajax'] = 'true'; |
| 351 | |
| 352 | $shortcode_attribute_string = implode(' ', array_map(static function ($k, $v) { return "$k=\"$v\""; }, array_keys($shortcode_attributes), $shortcode_attributes)); |
| 353 | |
| 354 | echo do_shortcode( '[gravityform '.$shortcode_attribute_string.']'); |
| 355 | } |
| 356 | |
| 357 | wp_die(); |
| 358 | } |
| 359 | } |
| 360 |