track( $server_event ); $code = PixelRenderer::render( array( $server_event ), self::TRACKING_NAME ); $code = sprintf( ' %s ', $code ); $out['html'] .= $code; return $out; } /** * Reads the form data from the Caldera Forms submission. * * @param array $form The form data. * * @return array The form data in the format * expected by the `FacebookServerSideEvent` class. */ public static function readFormData( $form ) { if ( empty( $form ) ) { return array(); } return array( 'email' => self::getEmail( $form ), 'first_name' => self::getFirstName( $form ), 'last_name' => self::getLastName( $form ), 'phone' => self::getPhone( $form ), 'state' => self::getState( $form ), ); } /** * Get the email address from the form data. * * @param array $form The form data. * * @return string The email address. */ private static function getEmail( $form ) { return self::getFieldValue( $form, 'type', 'email' ); } /** * Get the first name from the form data. * * @param array $form The form data. * * @return string The first name. */ private static function getFirstName( $form ) { return self::getFieldValue( $form, 'slug', 'first_name' ); } /** * Get the last name from the form data. * * @param array $form The form data. * * @return string The last name. */ private static function getLastName( $form ) { return self::getFieldValue( $form, 'slug', 'last_name' ); } /** * Get the state from the form data. * * @param array $form The form data. * * @return string|null The state, or null if not found. */ private static function getState( $form ) { return self::getFieldValue( $form, 'type', 'states' ); } /** * Get the phone number from the form data. * * Attempts to extract the phone number using the 'phone_better' type first. * If not found, falls back to using the 'phone' type. * * @param array $form The form data. * * @return string|null The phone number, or null if not found. */ private static function getPhone( $form ) { $phone = self::getFieldValue( $form, 'type', 'phone_better' ); return empty( $phone ) ? self::getFieldValue( $form, 'type', 'phone' ) : $phone; } /** * Retrieves the value of a field from the form data. * * Searches through the form's fields to find a field with the specified * attribute and attribute value. If a match is found, returns the value * from the $_POST array using the field's ID. * * @param array $form The form data containing fields. * @param string $attr The attribute to match against in the field. * @param string $attr_value The value of the attribute to look for. * * @return mixed|null The value of the field from $_POST, or null * if not found. */ private static function getFieldValue( $form, $attr, $attr_value ) { if ( empty( $form['fields'] ) ) { return null; } foreach ( $form['fields'] as $field ) { if ( isset( $field[ $attr ] ) && $field[ $attr ] === $attr_value ) { return sanitize_text_field( wp_unslash( $_POST[ $field['ID'] ] ?? '' // phpcs:ignore WordPress.Security.NonceVerification.Missing ) ); } } return null; } }