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 / NinjaForms / NinjaFormsIntegration.php
hostinger-reach / src / Integrations / NinjaForms Last commit date
NinjaFormsIntegration.php 5 months ago
NinjaFormsIntegration.php
267 lines
1 <?php
2
3 namespace Hostinger\Reach\Integrations\NinjaForms;
4
5 use Hostinger\Reach\Dto\ReachContact;
6 use Hostinger\Reach\Integrations\Integration;
7 use Hostinger\Reach\Integrations\IntegrationInterface;
8 use Hostinger\Reach\Dto\PluginData;
9 use Hostinger\Reach\Models\Form;
10 use WPN_Helper;
11
12 if ( ! DEFINED( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 class NinjaFormsIntegration extends Integration implements IntegrationInterface {
17
18 public const INTEGRATION_NAME = 'ninja-forms';
19
20 public function init(): void {
21 parent::init();
22 add_filter(
23 'ninja_forms_admin_notices',
24 function ( array $notices ) {
25 if ( isset( $notices['one_week_support'] ) ) {
26 unset( $notices['one_week_support'] );
27 }
28
29 return $notices;
30 }
31 );
32 }
33
34 public function get_plugin_data(): PluginData {
35 return PluginData::from_array(
36 array(
37 'id' => self::INTEGRATION_NAME,
38 'folder' => 'ninja-forms',
39 'file' => 'ninja-forms.php',
40 'admin_url' => 'admin.php?page=ninja-forms',
41 'add_form_url' => 'admin.php?page=ninja-forms#new-form',
42 'edit_url' => 'admin.php?page=ninja-forms&form_id={form_id}',
43 'url' => 'https://wordpress.org/plugins/ninja-forms/',
44 'download_url' => 'https://downloads.wordpress.org/plugin/ninja-forms.zip',
45 'title' => __( 'Ninja Forms', 'hostinger-reach' ),
46 'icon' => 'https://ps.w.org/ninja-forms/assets/icon-256x256.png',
47 'import_enabled' => true,
48 )
49 );
50 }
51
52 public static function get_name(): string {
53 return self::INTEGRATION_NAME;
54 }
55
56 public function active_integration_hooks(): void {
57 add_action( 'ninja_forms_after_submission', array( $this, 'handle_submission' ) );
58 }
59
60 public function handle_submission( array $data ): void {
61 $form = $this->get_form( $data['form_id'] ?? 0 );
62 $email = null;
63
64 if ( isset( $data['fields_by_key'] ) ) {
65 $email = $data['fields_by_key']['email'] ?? null;
66 }
67
68 if ( isset( $email['value'] ) ) {
69 $email = $email['value'];
70 }
71
72 if ( $email && $form ) {
73 do_action(
74 'hostinger_reach_submit',
75 array(
76 'group' => $form->get_setting( 'title' ),
77 'email' => $email,
78 'metadata' => array(
79 'plugin' => self::INTEGRATION_NAME,
80 'form_id' => $form->get_id(),
81 ),
82 )
83 );
84 }
85 }
86
87 public function get_forms(): array {
88 if ( ! function_exists( 'Ninja_Forms' ) ) {
89 return array();
90 }
91
92 $forms_data = Ninja_Forms()->form()->get_forms();
93
94 $forms = array_map(
95 function ( $form ) {
96 $form_obj = new Form(
97 array(
98 'form_id' => $form->get_id(),
99 'form_title' => $form->get_setting( 'title' ),
100 'is_active' => $this->is_form_enabled( $form->get_id() ),
101 'submissions' => $this->get_submissions( $form ),
102 'type' => $this->get_name(),
103 'post_id' => null,
104 )
105 );
106
107 return $form_obj->to_array();
108 },
109 $forms_data
110 );
111
112 return $forms;
113 }
114
115 public function is_form_enabled( int $form_id ): bool {
116 $form = $this->get_form( $form_id );
117 if ( ! $form ) {
118 return false;
119 }
120
121 $is_active_meta = $form->get_setting( self::HOSTINGER_REACH_IS_ACTIVE_META_KEY );
122
123 if ( ! $is_active_meta ) {
124 return true;
125 }
126
127 return $is_active_meta === 'yes';
128 }
129
130 public function on_form_activation_change( bool $is_updated, string $form_id, bool $is_active, string $type ): bool {
131 if ( ! class_exists( 'WPN_Helper' ) ) {
132 return $is_updated;
133 }
134 $form = $this->get_form( $form_id );
135 if ( ! $form ) {
136 return $is_updated;
137 }
138
139 $form->update_setting( self::HOSTINGER_REACH_IS_ACTIVE_META_KEY, $is_active ? 'yes' : 'no' );
140 $form->save();
141 WPN_Helper::delete_nf_cache( $form_id );
142
143 return true;
144 }
145
146 public function update_form_submissions( array $data ): void {
147 if ( ! isset( $data['metadata']['form_id'] ) ) {
148 return;
149 }
150
151 $form_id = $data['metadata']['form_id'];
152 $form = $this->get_form( $form_id );
153
154 if ( ! $form ) {
155 return;
156 }
157
158 $submissions = $this->get_submissions( $form );
159 $form->update_setting( self::HOSTINGER_REACH_SUBMISSIONS_META_KEY, $submissions + 1 );
160 $form->save();
161 WPN_Helper::delete_nf_cache( $form_id );
162 }
163
164 private function get_submissions( object $form ): int {
165 $submissions = $form->get_setting( self::HOSTINGER_REACH_SUBMISSIONS_META_KEY );
166
167 return (int) $submissions;
168 }
169
170
171 private function get_form( string $form_id ): ?object {
172 if ( ! function_exists( 'Ninja_Forms' ) ) {
173 return null;
174 }
175
176 $form_data = Ninja_Forms()->form( $form_id );
177 if ( ! $form_data ) {
178 return null;
179 }
180
181 return $form_data->get();
182 }
183
184 public function get_import_summary(): array {
185 $summary = array();
186
187 if ( ! function_exists( 'Ninja_Forms' ) ) {
188 return $summary;
189 }
190
191 $forms = $this->get_forms();
192
193 foreach ( $forms as $form ) {
194 $form_id = $form['form_id'] ?? null;
195
196 if ( ! $form_id ) {
197 continue;
198 }
199
200 $summary[ $form_id ] = array(
201 'title' => $form['form_title'] ?? $form['form_id'],
202 'contacts' => count( $this->get_form_emails( $form_id ) ),
203 );
204 }
205
206 return $summary;
207 }
208
209
210 public function get_contacts( mixed $form_id = null, ?int $limit = 100, ?int $offset = 0 ): array {
211 $entries = $this->get_form_emails( $form_id, $limit, $offset );
212 $form = $this->get_form( $form_id );
213
214 $entries = array_map(
215 function ( string $email ) use ( $form, $form_id ) {
216 $title = $form->get_setting( 'title' );
217 if ( empty( $title ) ) {
218 $title = $form_id . ' ' . self::INTEGRATION_NAME;
219 }
220 $contact = new ReachContact(
221 $email,
222 '',
223 '',
224 array(
225 'plugin' => self::INTEGRATION_NAME,
226 'group' => $title,
227 )
228 );
229
230 return $contact->to_array();
231 },
232 $entries
233 );
234
235 return $entries;
236 }
237
238 private function get_form_emails( mixed $form_id = null, ?int $limit = 0, ?int $offset = 0 ): array {
239 $emails = array();
240
241 if ( ! function_exists( 'Ninja_Forms' ) || ! $form_id ) {
242 return $emails;
243 }
244
245 $submissions = Ninja_Forms()->form( $form_id )->get_subs();
246 $i = 0;
247 foreach ( $submissions as $submission ) {
248 if ( $limit && count( $emails ) >= $limit ) {
249 break;
250 }
251
252 $fields = $submission->get_field_values();
253
254 foreach ( $fields as $value ) {
255 if ( filter_var( $value, FILTER_VALIDATE_EMAIL ) && ! in_array( $value, $emails, true ) ) {
256 ++$i;
257 if ( ! $offset || $i - 1 >= $offset ) {
258 $emails[] = $value;
259 }
260 }
261 }
262 }
263
264 return array_unique( $emails );
265 }
266 }
267