track( $server_event ); add_action( 'wpcf7_feedback_response', array( __CLASS__, 'injectLeadEvent' ), 20, 2 ); return $result; } /** * Injects the Pixel code into the Contact Form 7 response. * * Hooks into the `wpcf7_feedback_response` action and checks if the form * is submitted successfully and if the user is not an internal user. * If conditions are met, it renders the Pixel code using the `PixelRenderer` class * and appends the code to the form response. * * @param array $response The Contact Form 7 response. * @param array $result The form data. * * @return array The modified Contact Form 7 response. */ public static function injectLeadEvent( $response, $result ) { if ( FacebookPluginUtils::is_internal_user() ) { return $response; } $events = FacebookServerSideEvent::get_instance()->get_tracked_events(); if ( count( $events ) === 0 ) { return $response; } $event_id = $events[0]->getEventId(); $fbq_calls = PixelRenderer::render( $events, self::TRACKING_NAME, false ); $code = sprintf( " if( typeof window.pixelLastGeneratedLeadEvent === 'undefined' || window.pixelLastGeneratedLeadEvent != '%s' ){ window.pixelLastGeneratedLeadEvent = '%s'; %s } ", $event_id, $event_id, $fbq_calls ); $response['fb_pxl_code'] = $code; return $response; } /** * Reads the form data from the Contact Form 7 submission. * * @param object $form The Contact Form 7 form object. * * @return array The form data in the format expected * by the `FacebookServerSideEvent` class. */ public static function readFormData( $form ) { if ( empty( $form ) ) { return array(); } $form_tags = $form->scan_form_tags(); $name = self::getName( $form_tags ); return array( 'email' => self::getEmail( $form_tags ), 'first_name' => $name[0], 'last_name' => $name[1], 'phone' => self::getPhone( $form_tags ), ); } /** * Retrieves the email address from the form submission. * * @param array $form_tags The form tags. * * @return string|null The email address, or null if no email tag found. */ private static function getEmail( $form_tags ) { if ( empty( $form_tags ) ) { return null; } foreach ( $form_tags as $tag ) { if ( 'email' === $tag->basetype && isset( $_POST[ $tag->name ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing return sanitize_text_field( wp_unslash( $_POST[ $tag->name ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing } } return null; } /** * Retrieves the first and last name from the form submission. * * @param array $form_tags The form tags. * * @return array|null An array containing the first and * last name, or null if no name tag found. */ private static function getName( $form_tags ) { if ( empty( $form_tags ) ) { return null; } foreach ( $form_tags as $tag ) { if ( 'text' === $tag->basetype && strpos( strtolower( $tag->name ), 'name' ) !== false ) { return ServerEventFactory::split_name( sanitize_text_field( wp_unslash( $_POST[ $tag->name ] ?? null ) // phpcs:ignore WordPress.Security.NonceVerification.Missing ) ); } } return null; } /** * Retrieves the phone number from the form submission. * * @param array $form_tags The form tags. * * @return string|null The phone number, or null if no phone tag found. */ private static function getPhone( $form_tags ) { if ( empty( $form_tags ) ) { return null; } foreach ( $form_tags as $tag ) { if ( 'tel' === $tag->basetype ) { return isset( $_POST[ $tag->name ] ) ? // phpcs:ignore WordPress.Security.NonceVerification.Missing sanitize_text_field( wp_unslash( $_POST[ $tag->name ] ) // phpcs:ignore WordPress.Security.NonceVerification.Missing ) : null; } } return null; } }