WpFormsLiteIntegration.php
86 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Integrations\WPFormsLite; |
| 4 | |
| 5 | use Hostinger\Reach\Integrations\Integration; |
| 6 | use Hostinger\Reach\Integrations\IntegrationInterface; |
| 7 | use Hostinger\Reach\Dto\PluginData; |
| 8 | use WP_Post; |
| 9 | |
| 10 | if ( ! DEFINED( 'ABSPATH' ) ) { |
| 11 | exit; |
| 12 | } |
| 13 | |
| 14 | class WpFormsLiteIntegration extends Integration implements IntegrationInterface { |
| 15 | |
| 16 | public const INTEGRATION_NAME = 'wpforms-lite'; |
| 17 | |
| 18 | public function get_plugin_data(): PluginData { |
| 19 | return PluginData::from_array( |
| 20 | array( |
| 21 | 'id' => self::INTEGRATION_NAME, |
| 22 | 'folder' => 'wpforms-lite', |
| 23 | 'file' => 'wpforms.php', |
| 24 | 'admin_url' => 'admin.php?page=wpforms-overview', |
| 25 | 'add_form_url' => 'admin.php?page=wpforms-builder', |
| 26 | 'edit_url' => 'admin.php?page=wpforms-builder&view=fields&form_id={form_id}', |
| 27 | 'url' => 'https://wordpress.org/plugins/wpforms-lite/', |
| 28 | 'download_url' => 'https://downloads.wordpress.org/plugin/wpforms-lite.zip', |
| 29 | 'title' => __( 'WP Forms Lite', 'hostinger-reach' ), |
| 30 | ) |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | public static function get_name(): string { |
| 35 | return self::INTEGRATION_NAME; |
| 36 | } |
| 37 | |
| 38 | public function get_post_type(): array { |
| 39 | return array( 'wpforms' ); |
| 40 | } |
| 41 | |
| 42 | public function active_integration_hooks(): void { |
| 43 | add_action( 'wpforms_process_complete', array( $this, 'handle_submission' ), 10, 3 ); |
| 44 | } |
| 45 | |
| 46 | public function is_form_valid( WP_Post $post ): bool { |
| 47 | $form_fields = wpforms_get_form_fields( $post->ID, array( 'email' ) ); |
| 48 | |
| 49 | return ! empty( $form_fields ); |
| 50 | } |
| 51 | |
| 52 | public function handle_submission( array $fields, array $entry, array $form_data ): void { |
| 53 | if ( ! $this->is_form_enabled( $form_data['id'] ) ) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | $email = $this->find_field( $fields, 'email' ); |
| 58 | if ( $email ) { |
| 59 | do_action( |
| 60 | 'hostinger_reach_submit', |
| 61 | array( |
| 62 | // translators: %s - form id. |
| 63 | 'group' => $form_data['settings']['form_title'] ?? sprintf( __( 'WP Forms Lite %s', 'hostinger-reach' ), $form_data['id'] ), |
| 64 | 'email' => $email, |
| 65 | 'metadata' => array( |
| 66 | 'plugin' => self::INTEGRATION_NAME, |
| 67 | 'form_id' => $form_data['id'], |
| 68 | ), |
| 69 | ) |
| 70 | ); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | private function find_field( array $fields, string $type ): string { |
| 75 | foreach ( $fields as $field ) { |
| 76 | if ( isset( $field['type'] ) && $field['type'] === $type ) { |
| 77 | if ( isset( $field['value'] ) ) { |
| 78 | return $field['value']; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return ''; |
| 84 | } |
| 85 | } |
| 86 |