ThriveLeadsIntegration.php
197 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Integrations\ThriveLeads; |
| 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 WP_Post; |
| 10 | |
| 11 | if ( ! DEFINED( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | class ThriveLeadsIntegration extends Integration implements IntegrationInterface { |
| 16 | |
| 17 | public const INTEGRATION_NAME = 'thrive-leads'; |
| 18 | |
| 19 | public function active_integration_hooks(): void { |
| 20 | if ( defined( 'TVE_LEADS_ACTION_FORM_CONVERSION' ) ) { |
| 21 | add_action( TVE_LEADS_ACTION_FORM_CONVERSION, array( $this, 'handle_submission' ), 10, 6 ); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | public function handle_submission( WP_Post $group, WP_Post $form_type, array $variation, int $test_model_id, array $post_data, mixed $current_screen ): void { |
| 26 | if ( ! $this->is_form_enabled( $form_type->ID ) ) { |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | $contact_list = $form_type->post_title . ' - ' . $group->post_title; |
| 31 | $email = $this->find_email( $post_data ); |
| 32 | if ( ! empty( $email ) ) { |
| 33 | do_action( |
| 34 | 'hostinger_reach_submit', |
| 35 | array( |
| 36 | 'group' => $contact_list, |
| 37 | 'email' => $email, |
| 38 | 'metadata' => array( |
| 39 | 'plugin' => self::INTEGRATION_NAME, |
| 40 | 'form_id' => $form_type->ID, |
| 41 | ), |
| 42 | ) |
| 43 | ); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public function get_forms(): array { |
| 48 | $forms = parent::get_forms(); |
| 49 | |
| 50 | return array_map( |
| 51 | function ( array $form ) { |
| 52 | $form['form_title'] = $this->get_form_title( $form ); |
| 53 | |
| 54 | return $form; |
| 55 | }, |
| 56 | $forms |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | public function get_post_type(): array { |
| 61 | return array( 'tve_form_type', 'tve_lead_shortcode', 'tve_lead_2s_lightbox' ); |
| 62 | } |
| 63 | |
| 64 | public static function get_name(): string { |
| 65 | return self::INTEGRATION_NAME; |
| 66 | } |
| 67 | |
| 68 | public function get_plugin_data(): PluginData { |
| 69 | return PluginData::from_array( |
| 70 | array( |
| 71 | 'id' => self::INTEGRATION_NAME, |
| 72 | 'title' => __( 'Thrive Leads', 'hostinger-reach' ), |
| 73 | 'folder' => 'thrive-leads', |
| 74 | 'file' => 'thrive-leads.php', |
| 75 | 'admin_url' => 'admin.php?page=thrive_leads_dashboard#dashboard', |
| 76 | 'add_form_url' => 'admin.php?page=thrive_leads_dashboard#dashboard', |
| 77 | 'edit_url' => 'post.php?post={post_id}&action=architect&tve=true&_key=1', |
| 78 | 'url' => 'https://thrivethemes.com/leads', |
| 79 | 'download_url' => null, |
| 80 | 'import_enabled' => true, |
| 81 | 'icon' => content_url( 'plugins/hostinger-reach/frontend/vue/assets/images/icons/thrive-leads-logo.png' ), |
| 82 | ) |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | public function get_import_summary(): array { |
| 87 | $summary['thriveLeads'] = array( |
| 88 | 'title' => __( 'Thrive Leads', 'hostinger-reach' ), |
| 89 | 'contacts' => $this->get_contacts_count(), |
| 90 | ); |
| 91 | |
| 92 | return $summary; |
| 93 | } |
| 94 | |
| 95 | public function get_contacts( mixed $form_id = null, ?int $limit = 100, ?int $offset = 0 ): array { |
| 96 | global $wpdb; |
| 97 | $table_name = $this->get_contacts_table_name(); |
| 98 | |
| 99 | if ( ! $this->contacts_table_exists() ) { |
| 100 | return array(); |
| 101 | } |
| 102 | |
| 103 | $entries = $wpdb->get_results( |
| 104 | $wpdb->prepare( |
| 105 | 'SELECT email, name FROM %i LIMIT %d OFFSET %d', |
| 106 | $table_name, |
| 107 | $limit, |
| 108 | $offset |
| 109 | ), |
| 110 | ARRAY_A |
| 111 | ); |
| 112 | $entries = array_map( |
| 113 | function ( array $entry ) { |
| 114 | $contact = new ReachContact( |
| 115 | $entry['email'] ?? '', |
| 116 | $entry['name'] ?? '', |
| 117 | '', |
| 118 | array( |
| 119 | 'plugin' => self::INTEGRATION_NAME, |
| 120 | 'group' => self::INTEGRATION_NAME, |
| 121 | ) |
| 122 | ); |
| 123 | |
| 124 | return $contact->to_array(); |
| 125 | }, |
| 126 | $entries |
| 127 | ); |
| 128 | |
| 129 | return $entries; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | private function get_contacts_table_name(): string { |
| 134 | global $wpdb; |
| 135 | |
| 136 | if ( defined( 'TVE_LEADS_DB_PREFIX' ) ) { |
| 137 | $table_name = $wpdb->prefix . TVE_LEADS_DB_PREFIX . 'contacts'; |
| 138 | } else { |
| 139 | $table_name = $wpdb->prefix . 'tve_leads_contacts'; |
| 140 | } |
| 141 | |
| 142 | return $table_name; |
| 143 | } |
| 144 | |
| 145 | private function get_contacts_count(): int { |
| 146 | global $wpdb; |
| 147 | $table_name = $this->get_contacts_table_name(); |
| 148 | |
| 149 | if ( ! $this->contacts_table_exists() ) { |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | return (int) $wpdb->get_var( |
| 154 | $wpdb->prepare( |
| 155 | 'SELECT COUNT(*) FROM %i', |
| 156 | $table_name |
| 157 | ) |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | private function contacts_table_exists(): bool { |
| 162 | global $wpdb; |
| 163 | |
| 164 | $table_name = $this->get_contacts_table_name(); |
| 165 | |
| 166 | $found = $wpdb->get_var( |
| 167 | $wpdb->prepare( |
| 168 | 'SHOW TABLES LIKE %s', |
| 169 | $table_name |
| 170 | ) |
| 171 | ); |
| 172 | |
| 173 | return $found === $table_name; |
| 174 | } |
| 175 | |
| 176 | private function find_email( array $data ): string { |
| 177 | foreach ( $data as $value ) { |
| 178 | $sanitized_value = sanitize_email( $value ); |
| 179 | if ( filter_var( $sanitized_value, FILTER_VALIDATE_EMAIL ) ) { |
| 180 | return $value; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return ''; |
| 185 | } |
| 186 | |
| 187 | private function get_form_title( array $form ): string { |
| 188 | $title = $form['post']['post_title'] ?? self::INTEGRATION_NAME; |
| 189 | $parent = get_post_parent( $form['post']['ID'] ?? null ); |
| 190 | if ( ! empty( $parent ) ) { |
| 191 | $title .= ' - ' . get_the_title( $parent ); |
| 192 | } |
| 193 | |
| 194 | return $title; |
| 195 | } |
| 196 | } |
| 197 |