Brave
5 months ago
ContactForm7
4 months ago
Elementor
2 months ago
Forminator
2 months ago
NinjaForms
5 months ago
OptInMonster
5 months ago
Reach
2 months ago
SureForms
5 months ago
ThriveLeads
5 months ago
WPFormsLite
5 months ago
WSForms
8 months ago
WooCommerce
4 months ago
ContactForm7Integration.php
10 months ago
ImportManager.php
5 months ago
Integration.php
5 months ago
IntegrationInterface.php
9 months ago
IntegrationWithForms.php
9 months ago
PluginManager.php
9 months ago
ReachFormIntegration.php
9 months ago
WpFormsLiteIntegration.php
10 months ago
WpFormsLiteIntegration.php
100 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Integrations; |
| 4 | |
| 5 | use Hostinger\Reach\Api\Handlers\IntegrationsApiHandler; |
| 6 | use Hostinger\Reach\Api\Handlers\ReachApiHandler; |
| 7 | use WP_Post; |
| 8 | |
| 9 | if ( ! DEFINED( 'ABSPATH' ) ) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | class WpFormsLiteIntegration extends Integration implements IntegrationInterface { |
| 14 | |
| 15 | public const INTEGRATION_NAME = 'wpforms-lite'; |
| 16 | protected ReachApiHandler $reach_api_handler; |
| 17 | protected IntegrationsApiHandler $integrations_api_handler; |
| 18 | |
| 19 | public function __construct( ReachApiHandler $reach_api_handler, IntegrationsApiHandler $integrations_api_handler ) { |
| 20 | parent::__construct(); |
| 21 | $this->reach_api_handler = $reach_api_handler; |
| 22 | $this->integrations_api_handler = $integrations_api_handler; |
| 23 | } |
| 24 | |
| 25 | public function init(): void { |
| 26 | if ( $this->integrations_api_handler->is_active( self::INTEGRATION_NAME ) ) { |
| 27 | add_action( 'wpforms_process_complete', array( $this, 'handle_submission' ), 10, 3 ); |
| 28 | add_filter( 'hostinger_reach_forms', array( $this, 'load_forms' ), 10, 2 ); |
| 29 | add_filter( 'hostinger_reach_after_form_state_is_set', array( $this, 'on_form_activation_change' ), 10, 3 ); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | public function handle_submission( array $fields, array $entry, array $form_data ): void { |
| 34 | if ( ! $this->is_form_enabled( $form_data['id'] ) ) { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | $email = $this->find_field( $fields, 'email' ); |
| 39 | if ( $email ) { |
| 40 | $response = $this->reach_api_handler->post_contact( |
| 41 | array( |
| 42 | // translators: %s - form id. |
| 43 | 'group' => $form_data['settings']['form_title'] ?? sprintf( __( 'WP Forms Lite %s', 'hostinger-reach' ), $form_data['id'] ), |
| 44 | 'email' => $email, |
| 45 | 'metadata' => array( |
| 46 | 'plugin' => self::INTEGRATION_NAME, |
| 47 | ), |
| 48 | ) |
| 49 | ); |
| 50 | |
| 51 | if ( $response->get_status() < 300 ) { |
| 52 | $this->update_form_submissions( $form_data['id'] ); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | public function find_field( array $fields, string $type ): string { |
| 58 | foreach ( $fields as $field ) { |
| 59 | if ( isset( $field['type'] ) && $field['type'] === $type ) { |
| 60 | if ( isset( $field['value'] ) ) { |
| 61 | return $field['value']; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return ''; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | public static function get_name(): string { |
| 71 | return self::INTEGRATION_NAME; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | public function get_post_type(): string { |
| 76 | return 'wpforms'; |
| 77 | } |
| 78 | |
| 79 | public function is_form_valid( WP_Post $post ): bool { |
| 80 | $form_fields = wpforms_get_form_fields( $post->ID, array( 'email' ) ); |
| 81 | |
| 82 | return ! empty( $form_fields ); |
| 83 | } |
| 84 | |
| 85 | public function get_plugin_data( array $plugin_data ): array { |
| 86 | $plugin_data[ self::INTEGRATION_NAME ] = array( |
| 87 | 'folder' => 'wpforms-lite', |
| 88 | 'file' => 'wpforms.php', |
| 89 | 'admin_url' => 'admin.php?page=wpforms-overview', |
| 90 | 'add_form_url' => 'admin.php?page=wpforms-builder', |
| 91 | 'edit_url' => 'admin.php?page=wpforms-builder&view=fields&form_id={form_id}', |
| 92 | 'url' => 'https://wordpress.org/plugins/wpforms-lite/', |
| 93 | 'download_url' => 'https://downloads.wordpress.org/plugin/wpforms-lite.zip', |
| 94 | 'title' => __( 'WP Forms Lite', 'hostinger-reach' ), |
| 95 | ); |
| 96 | |
| 97 | return $plugin_data; |
| 98 | } |
| 99 | } |
| 100 |