ForminatorIntegration.php
180 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Integrations\Forminator; |
| 4 | |
| 5 | use Hostinger\Reach\Dto\PluginData; |
| 6 | use Hostinger\Reach\Dto\ReachContact; |
| 7 | use Hostinger\Reach\Integrations\Integration; |
| 8 | use Hostinger\Reach\Integrations\IntegrationInterface; |
| 9 | use Forminator_Form_Entry_Model; |
| 10 | |
| 11 | class ForminatorIntegration extends Integration implements IntegrationInterface { |
| 12 | |
| 13 | public const INTEGRATION_NAME = 'forminator'; |
| 14 | |
| 15 | public static function get_name(): string { |
| 16 | return self::INTEGRATION_NAME; |
| 17 | } |
| 18 | |
| 19 | public function get_post_type(): array { |
| 20 | return array( 'forminator_forms' ); |
| 21 | } |
| 22 | |
| 23 | public function get_plugin_data(): PluginData { |
| 24 | return PluginData::from_array( |
| 25 | array( |
| 26 | 'id' => self::INTEGRATION_NAME, |
| 27 | 'folder' => 'forminator', |
| 28 | 'file' => 'forminator.php', |
| 29 | 'admin_url' => 'admin.php?page=forminator', |
| 30 | 'add_form_url' => 'admin.php?page=forminator-cform', |
| 31 | 'edit_url' => 'admin.php?page=forminator-cform-wizard&id={post_id}', |
| 32 | 'url' => 'https://wordpress.org/plugins/forminator/', |
| 33 | 'download_url' => 'https://downloads.wordpress.org/plugin/forminator.zip', |
| 34 | 'title' => __( 'Forminator', 'hostinger-reach' ), |
| 35 | 'icon' => 'https://ps.w.org/forminator/assets/icon-256x256.gif', |
| 36 | 'import_enabled' => true, |
| 37 | ) |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | public function active_integration_hooks(): void { |
| 42 | add_action( 'forminator_form_after_save_entry', array( $this, 'handle_submission' ), 1, 2 ); |
| 43 | } |
| 44 | |
| 45 | public function handle_submission( int $form_id, array $submission_data ): void { |
| 46 | if ( ! $this->is_form_enabled( $form_id ) ) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | $name = $this->get_form_title( $form_id ); |
| 51 | $email = $this->find_email( $_POST ); |
| 52 | if ( empty( $email ) ) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | do_action( |
| 57 | 'hostinger_reach_submit', |
| 58 | array( |
| 59 | 'group' => $name ?? self::INTEGRATION_NAME, |
| 60 | 'email' => $email, |
| 61 | 'metadata' => array( |
| 62 | 'plugin' => self::INTEGRATION_NAME, |
| 63 | 'form_id' => $form_id, |
| 64 | ), |
| 65 | ) |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | public function get_import_summary(): array { |
| 70 | $forms = $this->get_forms(); |
| 71 | $summary = array(); |
| 72 | |
| 73 | foreach ( $forms as $form ) { |
| 74 | $form_id = $form['form_id'] ?? null; |
| 75 | |
| 76 | if ( ! $form_id ) { |
| 77 | continue; |
| 78 | } |
| 79 | |
| 80 | $title = $this->get_form_title( $form_id ); |
| 81 | |
| 82 | $contacts = 0; |
| 83 | if ( class_exists( 'Forminator_Form_Entry_Model' ) ) { |
| 84 | $contacts = Forminator_Form_Entry_Model::count_entries( $form_id ); |
| 85 | } |
| 86 | |
| 87 | $summary[ $form_id ] = array( |
| 88 | 'title' => $title, |
| 89 | 'contacts' => (int) $contacts, |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | return $summary; |
| 94 | } |
| 95 | |
| 96 | public function get_contacts( mixed $form_id = null, ?int $limit = 100, ?int $offset = 0 ): array { |
| 97 | if ( ! class_exists( 'Forminator_Form_Entry_Model' ) ) { |
| 98 | return array(); |
| 99 | } |
| 100 | |
| 101 | $entries = Forminator_Form_Entry_Model::query_entries( |
| 102 | array( |
| 103 | 'form_id' => $form_id, |
| 104 | 'per_page' => $limit, |
| 105 | 'offset' => $offset, |
| 106 | ) |
| 107 | ); |
| 108 | |
| 109 | $entries = array_filter( |
| 110 | $entries, |
| 111 | function ( Forminator_Form_Entry_Model $entry ) { |
| 112 | return $this->find_email( $entry->meta_data ); |
| 113 | } |
| 114 | ); |
| 115 | |
| 116 | $entries = array_map( |
| 117 | function ( Forminator_Form_Entry_Model $entry ) use ( $form_id ) { |
| 118 | $contact = new ReachContact( |
| 119 | $this->find_email( $entry->meta_data ), |
| 120 | '', |
| 121 | '', |
| 122 | array( |
| 123 | 'plugin' => self::INTEGRATION_NAME, |
| 124 | 'form_id' => $form_id, |
| 125 | 'group' => $this->get_form_title( $form_id ), |
| 126 | ) |
| 127 | ); |
| 128 | |
| 129 | return $contact->to_array(); |
| 130 | }, |
| 131 | $entries |
| 132 | ); |
| 133 | |
| 134 | return $entries; |
| 135 | } |
| 136 | |
| 137 | private function get_form_title( int $form_id ): string { |
| 138 | $title = ''; |
| 139 | if ( function_exists( 'forminator_get_form_name' ) ) { |
| 140 | $title = forminator_get_form_name( $form_id ); |
| 141 | } |
| 142 | |
| 143 | if ( empty( $title ) ) { |
| 144 | $title = $this->get_name() . ' - ' . $form_id; |
| 145 | } |
| 146 | |
| 147 | return $title; |
| 148 | } |
| 149 | |
| 150 | public function get_forms(): array { |
| 151 | $forms = parent::get_forms(); |
| 152 | |
| 153 | return array_map( |
| 154 | function ( array $form ) { |
| 155 | $form['form_title'] = $this->get_form_title( $form['form_id'] ); |
| 156 | return $form; |
| 157 | }, |
| 158 | $forms |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | private function find_email( array $data ): string { |
| 163 | foreach ( $data as $key => $value ) { |
| 164 | |
| 165 | // Sometimes the values are coming from POST request (submissions) and others from FormModel object (import). |
| 166 | // In FormModel the values are stored as arrays. |
| 167 | if ( is_array( $value ) ) { |
| 168 | $value = $value['value'] ?? ''; |
| 169 | } |
| 170 | |
| 171 | $sanitized_value = sanitize_email( $value ); |
| 172 | if ( filter_var( $sanitized_value, FILTER_VALIDATE_EMAIL ) ) { |
| 173 | return $value; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return ''; |
| 178 | } |
| 179 | } |
| 180 |