PluginProbe ʕ •ᴥ•ʔ
Hostinger Reach – AI-Powered Email Marketing for WordPress / 1.7.0
Hostinger Reach – AI-Powered Email Marketing for WordPress v1.7.0
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 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 / Api / Handlers / IntegrationsApiHandler.php
hostinger-reach / src / Api / Handlers Last commit date
ApiHandler.php 1 month ago FormsApiHandler.php 5 months ago HostingApiHandler.php 3 months ago IntegrationsApiHandler.php 1 week ago ReachApiHandler.php 1 month ago
IntegrationsApiHandler.php
242 lines
1 <?php
2
3 namespace Hostinger\Reach\Api\Handlers;
4
5 use Hostinger\Reach\Api\ApiKeyManager;
6 use Hostinger\Reach\Dto\PluginData;
7 use Hostinger\Reach\Functions;
8 use Hostinger\Reach\Integrations\Elementor\ElementorIntegration;
9 use Hostinger\Reach\Integrations\ImportManager;
10 use Hostinger\Reach\Integrations\Integration;
11 use Hostinger\Reach\Integrations\PluginManager;
12 use Hostinger\Reach\Integrations\Reach\ReachFormIntegration;
13 use Hostinger\Reach\Providers\IntegrationsProvider;
14 use WP_Error;
15 use WP_REST_Request;
16 use WP_REST_Response;
17
18 if ( ! defined( 'ABSPATH' ) ) {
19 die;
20 }
21
22 class IntegrationsApiHandler extends ApiHandler {
23 public const INTEGRATIONS_OPTION_NAME = 'hostinger_reach_integrations';
24
25 public PluginManager $plugin_manager;
26 public ImportManager $import_manager;
27 public ApiKeyManager $api_key_manager;
28
29 public function __construct( Functions $functions, PluginManager $plugin_manager, ImportManager $import_manager, ApiKeyManager $api_key_manager ) {
30 parent::__construct( $functions );
31 $this->plugin_manager = $plugin_manager;
32 $this->import_manager = $import_manager;
33 $this->api_key_manager = $api_key_manager;
34 }
35
36 public static function get_integrations(): array {
37 return apply_filters( 'hostinger_reach_integrations', IntegrationsProvider::INTEGRATIONS );
38 }
39
40 public function init_hooks(): void {
41 if ( ! $this->api_key_manager->is_connected() ) {
42 return;
43 }
44 add_action( 'init', array( $this, 'trigger_active_integrations' ) );
45 }
46
47 public function trigger_active_integrations(): void {
48 do_action( 'hostinger_reach_integrations_loaded', $this->get_integrations_data() );
49 }
50
51 public function is_active( string $integration_name ): bool {
52 $data = $this->get_integrations_data();
53
54 return $data[ $integration_name ][ Integration::INTEGRATION_IS_ACTIVE ] ?? false;
55 }
56
57 public function is_import_enabled( string $integration_name ): bool {
58 $data = $this->get_integrations_data();
59
60 $is_active = $data[ $integration_name ][ Integration::INTEGRATION_IS_ACTIVE ] ?? false;
61 $import_enabled = $data[ $integration_name ][ Integration::INTEGRATION_IMPORT_ENABLED ] ?? false;
62
63 return $is_active && $import_enabled;
64 }
65
66 public function get_integrations_handler(): WP_REST_Response {
67 return $this->handle_response(
68 array(
69 'response' => array(
70 'code' => 200,
71 ),
72 'body' => wp_json_encode( $this->get_integrations_data() ),
73 )
74 );
75 }
76
77 public function get_status_handler(): WP_REST_Response {
78 $integrations_data = $this->get_integrations_data();
79 $woocommerce_data = $integrations_data['woocommerce'] ?? array();
80
81 return $this->handle_response(
82 array(
83 'response' => array(
84 'code' => 200,
85 ),
86 'body' => wp_json_encode(
87 array(
88 'woocommerce' => array(
89 'is_plugin_active' => $woocommerce_data['is_plugin_active'],
90 'is_integration_active' => $woocommerce_data['is_active'],
91 'is_installable' => $woocommerce_data['is_installable'],
92 ),
93 )
94 ),
95 )
96 );
97 }
98
99 public function post_integrations_handler( WP_REST_Request $request ): WP_REST_Response {
100 $is_active = $request->get_param( Integration::INTEGRATION_IS_ACTIVE );
101 $integration = $request->get_param( 'integration' );
102 $integrations_data = $this->get_integrations_data();
103 $integration_data = $integrations_data[ $integration ] ?? array();
104 $has_download_url = ! empty( $integration_data['download_url'] ?? false );
105 $current_is_active = $integration_data[ Integration::INTEGRATION_IS_ACTIVE ] ?? false;
106 $should_update = ! $is_active || $this->activate_integration( $integration, $has_download_url );
107
108 if ( $current_is_active === $is_active ) {
109 $should_update = false;
110 }
111
112 if ( $should_update ) {
113 $this->save_integration( $integration, array( Integration::INTEGRATION_IS_ACTIVE => $is_active ) );
114 }
115
116 return $this->handle_response(
117 array(
118 'response' => array(
119 'code' => 200,
120 ),
121 )
122 );
123 }
124
125 public function post_import_handler( WP_REST_Request $request ): WP_REST_Response {
126 $integrations = $request->get_param( 'integrations' );
127 $this->import_manager->import( $integrations );
128
129 return $this->handle_response(
130 array(
131 'response' => array(
132 'code' => 200,
133 ),
134 )
135 );
136 }
137
138 public function get_import_handler( WP_REST_Request $request ): WP_REST_Response {
139 $integration = $request->get_param( 'integration' );
140
141 if ( ! $this->is_import_enabled( $integration ) ) {
142 return $this->handle_wp_error(
143 new WP_Error(
144 'import_not_available',
145 __( 'Importing is not available for this integration', 'hostinger-reach' )
146 )
147 );
148 }
149
150 $status = $this->import_manager->get_status( $integration );
151
152 return $this->handle_response(
153 array(
154 'response' => array(
155 'code' => 200,
156 ),
157 'body' => wp_json_encode( $status ),
158 )
159 );
160 }
161
162 public function activate_integration( string $integration_name, bool $can_install ): bool {
163 $integration_class = self::get_integrations()[ $integration_name ];
164 if ( ! isset( $integration_class ) ) {
165 return false;
166 }
167
168 if ( $can_install ) {
169 $installed = $this->plugin_manager->install( $integration_name );
170 if ( is_wp_error( $installed ) ) {
171 return false;
172 }
173 }
174
175 $activated = $this->plugin_manager->activate( $integration_name );
176 if ( $activated ) {
177 do_action( 'hostinger_reach_integration_activated', $integration_name );
178 update_option( Functions::HOSTINGER_REACH_HAS_USER_ACTION, true );
179 }
180
181 return $activated;
182 }
183
184 public function get_integration_data( string $integration_name ): array {
185 $integrations_data = get_option( self::INTEGRATIONS_OPTION_NAME, array() );
186
187 return $integrations_data[ $integration_name ] ?? array();
188 }
189
190 public function get_integrations_data(): array {
191 $available_integrations = self::get_integrations();
192 $integrations_state = get_option( self::INTEGRATIONS_OPTION_NAME, array() );
193 $available_integrations_state = array_intersect_key( $integrations_state, $available_integrations );
194 $integrations = array();
195
196 foreach ( $available_integrations as $integration_name => $integration_class ) {
197 $plugin = $this->plugin_manager->get_plugin( $integration_name );
198
199 if ( ! $plugin instanceof PluginData ) {
200 continue;
201 }
202
203 $is_hostinger_reach = $integration_name === ReachFormIntegration::INTEGRATION_NAME;
204 $is_elementor = $integration_name === ElementorIntegration::INTEGRATION_NAME;
205 $integrations[ $integration_name ] = $plugin->to_array();
206 $is_active_by_default = $integrations[ $integration_name ][ Integration::INTEGRATION_IS_ACTIVE ] ?? false;
207 $is_plugin_active = $is_hostinger_reach || $this->plugin_manager->is_active( $integration_name );
208 $is_integration_active = $available_integrations_state[ $integration_name ][ Integration::INTEGRATION_IS_ACTIVE ] ?? $is_active_by_default;
209 $is_active = $is_plugin_active && $is_integration_active;
210 $import_enabled = $integrations[ $integration_name ][ Integration::INTEGRATION_IMPORT_ENABLED ] ?? false;
211
212 $integrations[ $integration_name ] = array_merge(
213 $integrations[ $integration_name ],
214 array(
215 'is_plugin_active' => $is_plugin_active,
216 Integration::INTEGRATION_IMPORT_ENABLED => $import_enabled,
217 Integration::INTEGRATION_IS_ACTIVE => $is_hostinger_reach || $is_elementor || $is_active,
218 'can_deactivate' => ! $is_hostinger_reach && ! $is_elementor,
219 'is_go_to_plugin_visible' => ! $is_hostinger_reach,
220 'import_status' => $this->import_manager->get_status( $integration_name ),
221 'is_installable' => $this->plugin_manager->is_installed( $integration_name ) || ! empty( $integrations[ $integration_name ]['download_url'] ),
222 )
223 );
224
225 }
226
227 return $integrations;
228 }
229
230 private function save_integration( string $integration_name, array $data ): bool {
231 $integrations_data = $this->get_integrations_data();
232 $integrations_data[ $integration_name ] = $data;
233
234 return $this->save_integrations_data( $integrations_data );
235 }
236
237
238 private function save_integrations_data( array $data ): bool {
239 return update_option( self::INTEGRATIONS_OPTION_NAME, $data );
240 }
241 }
242