PluginProbe
Hostinger Reach – AI-Powered Email Marketing for WordPress / 1.5.8
Hostinger Reach – AI-Powered Email Marketing for WordPress v1.5.8
1.8.0 1.7.2 1.7.1 1.7.0 1.6.0 1.5.9 1.5.8 1.5.7 1.5.6 1.5.5 1.5.4 1.5.3 1.5.2 1.5.1 1.5.0 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 trunk 1.0.1 1.0.10 1.0.11 All 59 releases
hostinger-reach / src / Integrations / WpFormsLiteIntegration.php
WpFormsLiteIntegration.php
100 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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