with ', $block_content ); // Return the button if no spam protection provider is active. $spam_protection = new ConvertKit_Spam_Protection(); $provider = $spam_protection->get_active_provider(); if ( ! $provider ) { return $block_content; } // Enqueue the spam protection provider's JS. $provider->enqueue_scripts(); // Parse the button's DOM. $parser = new ConvertKit_HTML_Parser( $block_content ); $button = $parser->xpath->query( '//button' )->item( 0 ); // Attach the spam protection provider's attributes/elements to the form/button as necessary. // $button is narrowed from DOMNode to DOMElement by the //button xpath expression above. $provider->attach_to_form_button_dom( $parser, $button, 'convertkit_form_builder' ); // @phpstan-ignore-line // Return button HTML. return $parser->get_body_html(); } /** * Wraps the block's content within a element, and adds hidden fields. * * @since 3.0.0 * * @param string $content Block content. * @param array $atts Block attributes. * @param int $post_id Post ID. * @return string */ private function add_form_to_block_content( $content, $atts, $post_id ) { // Load the content into the parser. $parser = new ConvertKit_HTML_Parser( $content ); // Get block container. $block_container = $parser->xpath->query( '//div[contains(@class, "wp-block-convertkit-form-builder")]' )->item( 0 ); // If no block container was found, return the original content. // This shouldn't happen, as the block editor supplies the container, but it's a safeguard. if ( ! $block_container ) { return $content; } // Create form element. $form = $parser->html->createElement( 'form' ); $form->setAttribute( 'action', esc_url( get_permalink( $post_id ) ) ); $form->setAttribute( 'method', 'post' ); // Move form builder div contents into form. while ( $block_container->hasChildNodes() ) { $form->appendChild( $block_container->firstChild ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase } // Add subscribed message if required. if ( $this->subscriber_id ) { $subscribed_message = $parser->html->createElement( 'div' ); $subscribed_message->setAttribute( 'class', 'convertkit-form-builder-subscribed-message' ); $subscribed_message->appendChild( $parser->html->createTextNode( $atts['text_if_subscribed'] ) ); $form->insertBefore( $subscribed_message, $form->firstChild ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase } // Add hidden fields. $fields = array( 'convertkit[post_id]' => absint( $post_id ), 'convertkit[store_entries]' => $atts['store_entries'] ? '1' : '0', 'convertkit[redirect]' => esc_url( $atts['redirect'] ), 'convertkit[form_id]' => absint( $atts['form_id'] ), 'convertkit[tag_id]' => absint( $atts['tag_id'] ), 'convertkit[sequence_id]' => absint( $atts['sequence_id'] ), '_wpnonce' => wp_create_nonce( 'convertkit_block_form_builder' ), ); foreach ( $fields as $name => $value ) { $hidden = $parser->html->createElement( 'input' ); $hidden->setAttribute( 'type', 'hidden' ); $hidden->setAttribute( 'name', $name ); $hidden->setAttribute( 'value', $value ); $form->appendChild( $hidden ); } // Replace div contents with form. $block_container->appendChild( $form ); // Return modified content. return $parser->get_body_html(); } }