found ) { return $this->found; } global $wpdb; // Only rows that contain a form widget; revisions and trashed copies left out. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- read once per settings page view; no API lists Elementor forms. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT p.ID, p.post_title, pm.meta_value FROM {$wpdb->postmeta} pm INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id WHERE pm.meta_key = '_elementor_data' AND pm.meta_value LIKE %s AND p.post_type <> 'revision' AND p.post_status IN ('publish', 'private', 'draft', 'future') ORDER BY p.post_title", '%' . $wpdb->esc_like( '"widgetType":"form"' ) . '%' ) ); $this->found = array(); foreach ( (array) $rows as $row ) { $elements = json_decode( (string) $row->meta_value, true ); if ( is_array( $elements ) ) { $this->collect( $elements, (string) $row->post_title ); } } return $this->found; } public function listen() { add_action( 'elementor_pro/forms/new_record', array( $this, 'on_submit' ), 10, 2 ); } /** * @param \ElementorPro\Modules\Forms\Classes\Form_Record $record */ public function on_submit( $record ) { $form_id = (string) $record->get_form_settings( 'id' ); if ( '' === $form_id ) { return; } $answers = array(); foreach ( (array) $record->get( 'fields' ) as $key => $field ) { $answers[ (string) ( $field['id'] ?? $key ) ] = $this->text( $field['value'] ?? '' ); } Beon_Notify_Forms::submitted( $this->key(), $form_id, $answers, (string) $record->get_form_settings( 'form_name' ), $record ); } /** * Walks the element tree; each form widget becomes a form ("Form name — Page title"). */ private function collect( array $elements, $page ) { foreach ( $elements as $element ) { if ( ! is_array( $element ) ) { continue; } if ( 'form' === ( $element['widgetType'] ?? '' ) && ! empty( $element['id'] ) && ! isset( $this->found[ $element['id'] ] ) ) { $settings = (array) ( $element['settings'] ?? array() ); $fields = array(); foreach ( (array) ( $settings['form_fields'] ?? array() ) as $field ) { $type = (string) ( $field['field_type'] ?? 'text' ); $key = (string) ( $field['custom_id'] ?? '' ); if ( '' === $key || $this->skip_type( $type ) ) { continue; } $fields[] = array( 'key' => $key, 'label' => wp_strip_all_tags( (string) ( $field['field_label'] ?? '' ) ) ?: $key, 'type' => $type, ); } $name = trim( (string) ( $settings['form_name'] ?? '' ) ); $this->found[ (string) $element['id'] ] = array( 'title' => ( '' !== $name ? $name : __( 'Form', 'beon-chat' ) ) . ' — ' . ( '' !== $page ? $page : __( '(no title)', 'beon-chat' ) ), 'fields' => $fields, ); } if ( ! empty( $element['elements'] ) && is_array( $element['elements'] ) ) { $this->collect( $element['elements'], $page ); } } } }