SureFormsIntegration.php
165 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Integrations\SureForms; |
| 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 SRFM\Inc\Database\Tables\Entries; |
| 10 | use SRFM\Inc\Helper; |
| 11 | |
| 12 | class SureFormsIntegration extends Integration implements IntegrationInterface { |
| 13 | |
| 14 | public const INTEGRATION_NAME = 'sureforms'; |
| 15 | |
| 16 | public static function get_name(): string { |
| 17 | return self::INTEGRATION_NAME; |
| 18 | } |
| 19 | |
| 20 | public function get_post_type(): array { |
| 21 | return array( 'sureforms_form' ); |
| 22 | } |
| 23 | |
| 24 | public function get_plugin_data(): PluginData { |
| 25 | return PluginData::from_array( |
| 26 | array( |
| 27 | 'id' => self::INTEGRATION_NAME, |
| 28 | 'folder' => 'sureforms', |
| 29 | 'file' => 'sureforms.php', |
| 30 | 'admin_url' => 'admin.php?page=sureforms_menu', |
| 31 | 'add_form_url' => 'admin.php?page=add-new-form', |
| 32 | 'edit_url' => 'post.php?post={post_id}&action=edit', |
| 33 | 'url' => 'https://wordpress.org/plugins/sureforms/', |
| 34 | 'download_url' => 'https://downloads.wordpress.org/plugin/sureforms.zip', |
| 35 | 'title' => __( 'Sure Forms', 'hostinger-reach' ), |
| 36 | 'icon' => 'https://ps.w.org/sureforms/assets/icon-256x256.gif', |
| 37 | 'import_enabled' => true, |
| 38 | ) |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | public function init(): void { |
| 43 | parent::init(); |
| 44 | add_action( 'hostinger_reach_integration_activated', array( $this, 'set_onboarding_skipped' ) ); |
| 45 | } |
| 46 | |
| 47 | public function set_onboarding_skipped( string $name ): void { |
| 48 | if ( $name !== self::INTEGRATION_NAME || ! class_exists( 'SRFM\Inc\Helper' ) ) { |
| 49 | return; |
| 50 | } |
| 51 | update_option( '__srfm_do_redirect', false ); |
| 52 | Helper::update_srfm_option( 'onboarding_completed', 'yes' ); |
| 53 | } |
| 54 | |
| 55 | public function active_integration_hooks(): void { |
| 56 | add_action( 'srfm_form_submit', array( $this, 'handle_submission' ) ); |
| 57 | } |
| 58 | |
| 59 | public function get_import_summary(): array { |
| 60 | $forms = $this->get_forms(); |
| 61 | $summary = array(); |
| 62 | |
| 63 | if ( ! class_exists( 'SRFM\Inc\Database\Tables\Entries' ) ) { |
| 64 | return $summary; |
| 65 | } |
| 66 | |
| 67 | foreach ( $forms as $form ) { |
| 68 | $form_id = $form['form_id'] ?? null; |
| 69 | |
| 70 | if ( ! $form_id ) { |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | $contacts = Entries::get_total_entries_by_status( 'all', $form_id ); |
| 75 | $summary[ $form_id ] = array( |
| 76 | 'title' => $form['post']['post_title'] ?? self::INTEGRATION_NAME, |
| 77 | 'contacts' => (int) $contacts, |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | return $summary; |
| 82 | } |
| 83 | |
| 84 | public function get_contacts( mixed $form_id = null, ?int $limit = 100, ?int $offset = 0 ): array { |
| 85 | if ( ! class_exists( 'SRFM\Inc\Database\Tables\Entries' ) ) { |
| 86 | return array(); |
| 87 | } |
| 88 | |
| 89 | $args = array( |
| 90 | 'where' => array( |
| 91 | array( |
| 92 | array( |
| 93 | 'key' => 'form_id', |
| 94 | 'value' => $form_id, |
| 95 | 'compare' => '=', |
| 96 | ), |
| 97 | ), |
| 98 | ), |
| 99 | 'limit' => $limit, |
| 100 | 'offset' => $offset, |
| 101 | ); |
| 102 | |
| 103 | $entries = Entries::get_all( $args ); |
| 104 | $entries = array_map( |
| 105 | function ( array $entry ) use ( $form_id ) { |
| 106 | $contact = new ReachContact( |
| 107 | $this->find_email( $entry['form_data'] ?? array() ), |
| 108 | '', |
| 109 | '', |
| 110 | array( |
| 111 | 'plugin' => self::INTEGRATION_NAME, |
| 112 | 'form_id' => $form_id, |
| 113 | 'group' => $this->get_form_title( $form_id ), |
| 114 | ) |
| 115 | ); |
| 116 | |
| 117 | return $contact->to_array(); |
| 118 | }, |
| 119 | $entries |
| 120 | ); |
| 121 | |
| 122 | return $entries; |
| 123 | } |
| 124 | |
| 125 | public function handle_submission( array $submission_data ): void { |
| 126 | if ( ! $this->is_form_enabled( $submission_data['form_id'] ) ) { |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | $data = $submission_data['data'] ?? array(); |
| 131 | $email = $data['email'] ?? null; |
| 132 | |
| 133 | if ( $email ) { |
| 134 | do_action( |
| 135 | 'hostinger_reach_submit', |
| 136 | array( |
| 137 | 'group' => $submission_data['form_name'] ?? self::INTEGRATION_NAME, |
| 138 | 'email' => $email, |
| 139 | 'metadata' => array( |
| 140 | 'plugin' => self::INTEGRATION_NAME, |
| 141 | 'form_id' => $submission_data['form_id'], |
| 142 | ), |
| 143 | ) |
| 144 | ); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | private function find_email( array $data ): string { |
| 149 | foreach ( $data as $value ) { |
| 150 | $sanitized_value = sanitize_email( $value ); |
| 151 | if ( filter_var( $sanitized_value, FILTER_VALIDATE_EMAIL ) ) { |
| 152 | return $value; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return ''; |
| 157 | } |
| 158 | |
| 159 | private function get_form_title( string $form_id ): string { |
| 160 | $post = get_post( $form_id ); |
| 161 | $default_title = $form_id . ' ' . self::INTEGRATION_NAME; |
| 162 | return $post && $post->post_title ? $post->post_title : $default_title; |
| 163 | } |
| 164 | } |
| 165 |