PluginProbe ʕ •ᴥ•ʔ
Hostinger Reach – AI-Powered Email Marketing for WordPress / 1.5.9
Hostinger Reach – AI-Powered Email Marketing for WordPress v1.5.9
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 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6
hostinger-reach / src / Integrations / Integration.php
hostinger-reach / src / Integrations Last commit date
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
Integration.php
214 lines
1 <?php
2
3 namespace Hostinger\Reach\Integrations;
4
5 use Hostinger\Reach\Models\Form;
6 use Exception;
7 use Hostinger\Reach\Dto\PluginData;
8 use WP_Post;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 die;
12 }
13
14 abstract class Integration {
15
16 public const HOSTINGER_REACH_SUBMISSIONS_META_KEY = '_hostinger_reach_submissions';
17 public const HOSTINGER_REACH_IS_ACTIVE_META_KEY = '_hostinger_reach_is_active';
18 public const HOSTINGER_INTEGRATION_TYPE_ECOMMERCE = 'ecommerce';
19
20 public const INTEGRATION_IS_ACTIVE = 'is_active';
21 public const INTEGRATION_IMPORT_ENABLED = 'import_enabled';
22
23 /**
24 * Unique name for your integration
25 */
26 abstract public static function get_name(): string;
27
28 /**
29 * Your integration data.
30 *
31 * @return PluginData Plugin data model.
32 *
33 */
34 abstract public function get_plugin_data(): PluginData;
35
36 /**
37 * Hooks to run ONLY when the integration is active.
38 * i.e Form submission should be here.
39 */
40 abstract public function active_integration_hooks(): void;
41
42 /**
43 * Returns the post-type of the form.
44 * Override and implement this if your integration is based on post types.
45 * Otherwise, override get_forms() method.
46 *
47 */
48 public function get_post_type(): array|null {
49 return null;
50 }
51
52 /**
53 * Method to return the forms of the integration.
54 *
55 * If your integration is based on post-types, DONT override this method. Override get_post_type() instead.
56 * @return array An array of forms based on Hostinger\Reach\Models\Form
57 *
58 * 'form_id' => The Form Unique ID
59 * 'post_id' => (Optional) associated Post ID
60 * 'type' => $this->get_name(), (Your integration name)
61 * 'is_active' => True or false indicating the state of the form
62 * 'submissions' =>Number indicating the submission counter
63 * @see Hostinger\Reach\Models\Form
64 *
65 */
66 public function get_forms(): array {
67 $posts = get_posts(
68 array(
69 'post_type' => $this->get_post_type(),
70 'status' => 'publish',
71 'per_page' => - 1,
72 )
73 );
74
75 $forms = array_map(
76 function ( $post ) {
77 $form = new Form(
78 array(
79 'form_id' => $post->ID,
80 'post_id' => $post->ID,
81 'type' => $this->get_name(),
82 'is_active' => $this->is_form_valid( $post ) && $this->is_form_enabled( $post->ID ),
83 'submissions' => (int) get_post_meta( $post->ID, Integration::HOSTINGER_REACH_SUBMISSIONS_META_KEY, true ),
84 )
85 );
86
87 return $form->to_array();
88 },
89 $posts
90 );
91
92 return $forms;
93 }
94
95 /**
96 * Logic to update the submission counter when a form is submitted.
97 * Override this if your Integration is not post-type based integration.
98 *
99 * @param array $data Submission data
100 */
101 public function update_form_submissions( array $data ): void {
102 if ( ! isset( $data['metadata']['form_id'] ) ) {
103 return;
104 }
105
106 $id = $data['metadata']['form_id'];
107 $submissions = (int) get_post_meta( $id, Integration::HOSTINGER_REACH_SUBMISSIONS_META_KEY, true );
108 update_post_meta( $id, Integration::HOSTINGER_REACH_SUBMISSIONS_META_KEY, $submissions + 1 );
109 }
110
111
112 public function init(): void {
113 add_filter( 'hostinger_reach_integrations', array( $this, 'load_integration' ) );
114 add_filter( 'hostinger_reach_plugin_data', array( $this, 'load_plugin_data' ) );
115 add_filter( 'hostinger_reach_after_form_state_is_set', array( $this, 'on_form_state_changed' ), 10, 4 );
116 add_filter( 'hostinger_reach_contacts_' . $this->get_name(), array( $this, 'get_contacts' ), 10, 3 );
117 add_filter( 'hostinger_reach_import_summary_' . $this->get_name(), array( $this, 'get_import_summary' ), 10, 0 );
118 add_filter( 'hostinger_reach_import_enabled', array( $this, 'get_is_import_enabled' ), 10, 2 );
119 add_action( 'hostinger_reach_integrations_loaded', array( $this, 'init_active_integration' ) );
120 add_action( 'hostinger_reach_contact_submitted', array( $this, 'on_contact_form_submission' ) );
121 }
122
123 public function load_plugin_data( array $plugin_data ): array {
124 $plugin_data[ $this->get_name() ] = $this->get_plugin_data();
125
126 return $plugin_data;
127 }
128
129 public function load_integration( array $integrations ): array {
130 $integrations[ $this->get_name() ] = $this::class;
131
132 return $integrations;
133 }
134
135 public function init_active_integration( array $integrations ): void {
136 $integration_is_active = $integrations[ $this->get_name() ][ self::INTEGRATION_IS_ACTIVE ] ?? false;
137
138 if ( $integration_is_active ) {
139 add_filter( 'hostinger_reach_forms', array( $this, 'load_forms' ), 10, 2 );
140 $this->active_integration_hooks();
141 }
142 }
143
144 public function get_is_import_enabled( bool $value, string $integration ): bool {
145 if ( $integration === $this->get_name() ) {
146 $plugin_data = $this->get_plugin_data()->to_array();
147 return $plugin_data[ self::INTEGRATION_IMPORT_ENABLED ] ?? false;
148 }
149
150 return $value;
151 }
152
153 public function load_forms( array $forms, array $args ): array {
154 if ( ! isset( $args['type'] ) || $args['type'] === $this->get_name() ) {
155 $integration_forms = $this->get_forms();
156
157 return array_merge( $forms, $integration_forms );
158 }
159
160 return $forms;
161 }
162
163 public function on_form_state_changed( bool $repository_form_was_updated, string $form_id, bool $is_active, string $type ): bool {
164 if ( $type !== $this->get_name() ) {
165 return $repository_form_was_updated;
166 }
167
168 return $this->on_form_activation_change( $repository_form_was_updated, $form_id, $is_active, $type );
169 }
170
171 public function on_form_activation_change( bool $repository_form_was_updated, string $form_id, bool $is_active, string $type ): bool {
172 $post = get_post( $form_id );
173 if ( ! $post || ! in_array( $post->post_type, $this->get_post_type() ?? array(), true ) ) {
174 return $repository_form_was_updated;
175 }
176
177 if ( $is_active && ! $this->is_form_valid( $post ) ) {
178 throw new Exception( __( 'This form has not an email field. Create an email field in the form to allow it to be synced with Reach', 'hostinger-reach' ) );
179 }
180
181 return (bool) update_post_meta( (int) $form_id, Integration::HOSTINGER_REACH_IS_ACTIVE_META_KEY, $is_active ? 'yes' : 'no' );
182 }
183
184 public function on_contact_form_submission( array $data ): void {
185 if ( ! isset( $data['metadata']['plugin'] ) || $data['metadata']['plugin'] !== $this->get_name() ) {
186 return;
187 }
188
189 $this->update_form_submissions( $data );
190 }
191
192 public function is_form_enabled( int $form_id ): bool {
193 $is_active_meta = get_post_meta( $form_id, Integration::HOSTINGER_REACH_IS_ACTIVE_META_KEY, true );
194
195 if ( $is_active_meta === '' ) {
196 return true;
197 }
198
199 return $is_active_meta === 'yes';
200 }
201
202 public function is_form_valid( WP_Post $post ): bool {
203 return true;
204 }
205
206 public function get_contacts( mixed $form_id = null, ?int $limit = 100, ?int $offset = 0 ): array {
207 return array();
208 }
209
210 public function get_import_summary(): array {
211 return array();
212 }
213 }
214