value, plus the block ID. */ public function capture( $form_data ) { $form_data = (array) $form_data; $block_id = preg_replace( '/[^A-Za-z0-9_-]/', '', (string) ( $form_data['id'] ?? '' ) ); // Spectra has verified its own nonce for this request. $post_id = isset( $_POST['post_id'] ) ? absint( wp_unslash( $_POST['post_id'] ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing $fields = array(); unset( $form_data['id'] ); foreach ( $form_data as $label => $value ) { $label = trim( wp_strip_all_tags( html_entity_decode( (string) $label, ENT_QUOTES, 'UTF-8' ) ) ); $fields[] = array( 'label' => $label, 'name' => sanitize_title( $label ), 'type' => is_string( $value ) && is_email( $value ) ? 'email' : '', 'value' => $value, ); } $name = self::block_label( $post_id, $block_id ); if ( '' === $name ) { $name = $post_id ? (string) get_the_title( $post_id ) : __( 'Spectra form', 'dealroot' ); } Capture::submission( self::key(), $block_id, $name, $fields ); } /** * Label the form block was given in the editor. * * @param int $post_id Page ID. * @param string $block_id Block ID. * @return string */ private static function block_label( $post_id, $block_id ) { $post = $post_id ? get_post( $post_id ) : null; if ( ! $post || '' === $block_id || false === strpos( (string) $post->post_content, 'uagb/forms' ) ) { return ''; } $block = self::find_block( parse_blocks( (string) $post->post_content ), $block_id ); return $block ? (string) ( $block['attrs']['formLabel'] ?? '' ) : ''; } /** * Form block with the given ID, also inside other blocks. * * @param array $blocks Parsed blocks. * @param string $block_id Block ID. * @return array|null */ private static function find_block( array $blocks, $block_id ) { foreach ( $blocks as $block ) { if ( 'uagb/forms' === ( $block['blockName'] ?? '' ) && ( $block['attrs']['block_id'] ?? '' ) === $block_id ) { return $block; } $inner = ! empty( $block['innerBlocks'] ) ? self::find_block( $block['innerBlocks'], $block_id ) : null; if ( $inner ) { return $inner; } } return null; } }