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 / ImportManager.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
ImportManager.php
144 lines
1 <?php
2
3 namespace Hostinger\Reach\Integrations;
4
5 class ImportManager {
6
7 public const IMPORT_STATUS_OPTION_KEY = 'hostinger_reach_integrations_import_status';
8 public const IMPORT_STATUS_PARTIALLY_IMPORTED = 'partially_imported';
9 public const IMPORT_STATUS_NOT_IMPORTED = 'not_imported';
10 public const IMPORT_STATUS_IMPORTING = 'importing';
11 public const IMPORT_STATUS_IMPORTED = 'imported';
12 public const START_HOOK_JOB_NAME = 'hostinger-reach/jobs/import/start';
13
14 public function import( array $integrations ): void {
15 $this->set_import_status( $integrations, self::IMPORT_STATUS_IMPORTING );
16
17 foreach ( $integrations as $integration => $forms ) {
18 if ( ! $this->is_import_enabled( $integration ) ) {
19 continue;
20 }
21
22 foreach ( $forms as $form ) {
23 do_action(
24 self::START_HOOK_JOB_NAME,
25 array(
26 'integration' => $integration,
27 'form_id' => $form,
28 )
29 );
30 }
31 }
32 }
33
34 public function get_status( string $integration ): array {
35 $import_status = $this->get_import_status();
36 $summary = apply_filters( 'hostinger_reach_import_summary_' . $integration, array() );
37
38 foreach ( $summary as $form_id => $data ) {
39 $form_status = $import_status[ $integration ][ $form_id ] ?? self::IMPORT_STATUS_NOT_IMPORTED;
40 $summary[ $form_id ]['status'] = $form_status;
41 }
42
43 return array(
44 'status' => $this->get_status_from_summary( $summary ),
45 'total' => $this->get_total_from_summary( $summary ),
46 'summary' => $summary,
47 );
48 }
49
50 public function is_import_enabled( string $integration ): bool {
51 return apply_filters( 'hostinger_reach_import_enabled', false, $integration );
52 }
53
54 public function get_contacts( string $integration, mixed $form_id = null, ?int $limit = 100, ?int $offset = 0 ): array {
55 return apply_filters( 'hostinger_reach_contacts_' . $integration, $form_id, $limit, $offset );
56 }
57
58 public function set_completed_import( string $integration, array $forms ): void {
59 $this->set_import_status(
60 array(
61 $integration => $forms,
62 ),
63 self::IMPORT_STATUS_IMPORTED
64 );
65 }
66
67 private function set_import_status( array $integrations, string $status ): void {
68 $import_status = $this->get_import_status();
69
70 foreach ( $integrations as $integration => $forms ) {
71 if ( ! $this->is_import_enabled( $integration ) ) {
72 continue;
73 }
74 foreach ( $forms as $form_id ) {
75 if ( ! is_array( $import_status[ $integration ] ?? array() ) ) {
76 $import_status[ $integration ] = array();
77 }
78 $import_status[ $integration ][ $form_id ] = $status;
79 }
80 }
81
82 update_option( self::IMPORT_STATUS_OPTION_KEY, $import_status );
83 }
84
85 private function get_import_status(): array {
86 return get_option( self::IMPORT_STATUS_OPTION_KEY, array() );
87 }
88
89 private function get_total_from_summary( array $summary ): int {
90 return array_sum( wp_list_pluck( $summary, 'contacts' ) );
91 }
92
93 private function get_status_from_summary( array $summary ): string {
94 $has_any_importing = $this->array_find(
95 $summary,
96 function ( $item ) {
97 return $item['status'] === self::IMPORT_STATUS_IMPORTING;
98 }
99 );
100
101 if ( ! is_null( $has_any_importing ) ) {
102 return self::IMPORT_STATUS_IMPORTING;
103 }
104
105 $imported = count(
106 array_filter(
107 $summary,
108 function ( $item ) {
109 return $item['status'] === self::IMPORT_STATUS_IMPORTED;
110 }
111 )
112 );
113
114 if ( $imported === 0 ) {
115 return self::IMPORT_STATUS_NOT_IMPORTED;
116 } elseif ( $imported >= count( $summary ) ) {
117 return self::IMPORT_STATUS_IMPORTED;
118 } else {
119 return self::IMPORT_STATUS_PARTIALLY_IMPORTED;
120 }
121 }
122
123 /**
124 * Polyfill for `array_find()`
125 *
126 * Searches an array for the first element that passes a given callback.
127 *
128 * @since 6.8.0
129 *
130 * @param array $array The array to search.
131 * @param callable $callback The callback to run for each element.
132 * @return mixed|null The first element in the array that passes the `$callback`, otherwise null.
133 */
134 public function array_find( array $array, callable $callback ): mixed { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
135 foreach ( $array as $key => $value ) {
136 if ( $callback( $value, $key ) ) {
137 return $value;
138 }
139 }
140
141 return null;
142 }
143 }
144