> */ public static function events( $events ): array { $events = is_array( $events ) ? $events : array(); foreach ( \WPCF7_ContactForm::find() as $form ) { $events[] = self::event( $form ); } return $events; } /** * Hook: a form was submitted. * * @param mixed $form Contact form. * @param mixed $result Submission result. * @return void */ public static function submitted( $form, $result ): void { if ( ! $form instanceof \WPCF7_ContactForm ) { return; } $event_id = self::EVENT_PREFIX . $form->id(); try { $status = is_array( $result ) ? (string) ( $result['status'] ?? '' ) : ''; $submission = \WPCF7_Submission::get_instance(); if ( ! in_array( $status, self::SENT_STATUSES, true ) || ! $submission instanceof \WPCF7_Submission ) { return; } Events::fire( $event_id, self::context( $form, $submission ) ); } catch ( \Throwable $e ) { Events::report( $e, $event_id ); } } /** * Event of one form. * * @param \WPCF7_ContactForm $form Form. * @return array */ private static function event( \WPCF7_ContactForm $form ): array { $fields = self::fields( $form ); $form_id = (int) $form->id(); return array( 'id' => self::EVENT_PREFIX . $form_id, /* translators: %s: form title. */ 'label' => sprintf( __( 'Contact Form 7: form "%s" sent', '85pl-android-sms-api' ), $form->title() ), 'group' => self::GROUP, 'icon' => SMSPORTAL_URL_BASE . 'assets/integrations/contact-form-7.png', 'variables' => array_combine( array_keys( $fields ), array_keys( $fields ) ), 'customer_phone' => static function ( array $context, array $rule = array() ) { $field = (string) ( $rule['phone_field'] ?? '' ); $phone = '' !== $field ? ( $context[ $field ] ?? null ) : null; return is_string( $phone ) && '' !== trim( $phone ) ? $phone : null; }, 'object' => static function ( array $context ) use ( $form_id ): array { $hash = (string) ( $context[ self::KEY_HASH ] ?? '' ); return array( 'type' => self::OBJECT_TYPE, // One SMS per submission: CF7's hash covers the data, sender and a half-hour window. 'id' => '' === $hash ? '' : $form_id . '-' . $hash, 'url' => admin_url( 'admin.php?page=wpcf7&action=edit&post=' . $form_id ), ); }, // CF7 does not store submissions, so there is nothing to preview. 'sample' => null, 'rule_fields' => array( 'phone_field' ), ); } /** * Named fields of a form. * * @param \WPCF7_ContactForm $form Form. * @return array Field name => field type (e.g. "tel"). */ private static function fields( \WPCF7_ContactForm $form ): array { $fields = array(); foreach ( $form->scan_form_tags() as $tag ) { $name = (string) $tag->name; if ( '' !== $name && ! isset( $fields[ $name ] ) ) { $fields[ $name ] = (string) $tag->basetype; } } return $fields; } /** * Submitted values as template variables (lists joined with commas). * * @param \WPCF7_ContactForm $form Form. * @param \WPCF7_Submission $submission Submission. * @return array */ private static function context( \WPCF7_ContactForm $form, \WPCF7_Submission $submission ): array { $context = array(); foreach ( (array) $submission->get_posted_data() as $name => $value ) { if ( is_array( $value ) ) { $value = implode( ', ', array_filter( array_map( 'strval', array_filter( $value, 'is_scalar' ) ), 'strlen' ) ); } if ( is_scalar( $value ) ) { $context[ (string) $name ] = (string) $value; } } $context[ self::KEY_FORM ] = (int) $form->id(); $context[ self::KEY_HASH ] = (string) $submission->get_posted_data_hash(); return $context; } }