ApiHandler.php
2 weeks ago
FormsApiHandler.php
4 months ago
HostingApiHandler.php
2 months ago
IntegrationsApiHandler.php
2 months ago
ReachApiHandler.php
2 weeks ago
IntegrationsApiHandler.php
220 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 post_integrations_handler( WP_REST_Request $request ): WP_REST_Response { |
| 78 | $is_active = $request->get_param( Integration::INTEGRATION_IS_ACTIVE ); |
| 79 | $integration = $request->get_param( 'integration' ); |
| 80 | $integrations_data = $this->get_integrations_data(); |
| 81 | $integration_data = $integrations_data[ $integration ] ?? array(); |
| 82 | $has_download_url = ! empty( $integration_data['download_url'] ?? false ); |
| 83 | $current_is_active = $integration_data[ Integration::INTEGRATION_IS_ACTIVE ] ?? false; |
| 84 | $should_update = ! $is_active || $this->activate_integration( $integration, $has_download_url ); |
| 85 | |
| 86 | if ( $current_is_active === $is_active ) { |
| 87 | $should_update = false; |
| 88 | } |
| 89 | |
| 90 | if ( $should_update ) { |
| 91 | $this->save_integration( $integration, array( Integration::INTEGRATION_IS_ACTIVE => $is_active ) ); |
| 92 | } |
| 93 | |
| 94 | return $this->handle_response( |
| 95 | array( |
| 96 | 'response' => array( |
| 97 | 'code' => 200, |
| 98 | ), |
| 99 | ) |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | public function post_import_handler( WP_REST_Request $request ): WP_REST_Response { |
| 104 | $integrations = $request->get_param( 'integrations' ); |
| 105 | $this->import_manager->import( $integrations ); |
| 106 | |
| 107 | return $this->handle_response( |
| 108 | array( |
| 109 | 'response' => array( |
| 110 | 'code' => 200, |
| 111 | ), |
| 112 | ) |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | public function get_import_handler( WP_REST_Request $request ): WP_REST_Response { |
| 117 | $integration = $request->get_param( 'integration' ); |
| 118 | |
| 119 | if ( ! $this->is_import_enabled( $integration ) ) { |
| 120 | return $this->handle_wp_error( |
| 121 | new WP_Error( |
| 122 | 'import_not_available', |
| 123 | __( 'Importing is not available for this integration', 'hostinger-reach' ) |
| 124 | ) |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | $status = $this->import_manager->get_status( $integration ); |
| 129 | |
| 130 | return $this->handle_response( |
| 131 | array( |
| 132 | 'response' => array( |
| 133 | 'code' => 200, |
| 134 | ), |
| 135 | 'body' => wp_json_encode( $status ), |
| 136 | ) |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | public function activate_integration( string $integration_name, bool $can_install ): bool { |
| 141 | $integration_class = self::get_integrations()[ $integration_name ]; |
| 142 | if ( ! isset( $integration_class ) ) { |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | if ( $can_install ) { |
| 147 | $installed = $this->plugin_manager->install( $integration_name ); |
| 148 | if ( is_wp_error( $installed ) ) { |
| 149 | return false; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | $activated = $this->plugin_manager->activate( $integration_name ); |
| 154 | if ( $activated ) { |
| 155 | do_action( 'hostinger_reach_integration_activated', $integration_name ); |
| 156 | update_option( Functions::HOSTINGER_REACH_HAS_USER_ACTION, true ); |
| 157 | } |
| 158 | |
| 159 | return $activated; |
| 160 | } |
| 161 | |
| 162 | public function get_integration_data( string $integration_name ): array { |
| 163 | $integrations_data = get_option( self::INTEGRATIONS_OPTION_NAME, array() ); |
| 164 | |
| 165 | return $integrations_data[ $integration_name ] ?? array(); |
| 166 | } |
| 167 | |
| 168 | public function get_integrations_data(): array { |
| 169 | $available_integrations = self::get_integrations(); |
| 170 | $integrations_state = get_option( self::INTEGRATIONS_OPTION_NAME, array() ); |
| 171 | $available_integrations_state = array_intersect_key( $integrations_state, $available_integrations ); |
| 172 | $integrations = array(); |
| 173 | |
| 174 | foreach ( $available_integrations as $integration_name => $integration_class ) { |
| 175 | $plugin = $this->plugin_manager->get_plugin( $integration_name ); |
| 176 | |
| 177 | if ( ! $plugin instanceof PluginData ) { |
| 178 | continue; |
| 179 | } |
| 180 | |
| 181 | $is_hostinger_reach = $integration_name === ReachFormIntegration::INTEGRATION_NAME; |
| 182 | $is_elementor = $integration_name === ElementorIntegration::INTEGRATION_NAME; |
| 183 | $integrations[ $integration_name ] = $plugin->to_array(); |
| 184 | $is_active_by_default = $integrations[ $integration_name ][ Integration::INTEGRATION_IS_ACTIVE ] ?? false; |
| 185 | $is_plugin_active = $is_hostinger_reach || $this->plugin_manager->is_active( $integration_name ); |
| 186 | $is_integration_active = $available_integrations_state[ $integration_name ][ Integration::INTEGRATION_IS_ACTIVE ] ?? $is_active_by_default; |
| 187 | $is_active = $is_plugin_active && $is_integration_active; |
| 188 | $import_enabled = $integrations[ $integration_name ][ Integration::INTEGRATION_IMPORT_ENABLED ] ?? false; |
| 189 | |
| 190 | $integrations[ $integration_name ] = array_merge( |
| 191 | $integrations[ $integration_name ], |
| 192 | array( |
| 193 | 'is_plugin_active' => $is_plugin_active, |
| 194 | Integration::INTEGRATION_IMPORT_ENABLED => $import_enabled, |
| 195 | Integration::INTEGRATION_IS_ACTIVE => $is_hostinger_reach || $is_elementor || $is_active, |
| 196 | 'can_deactivate' => ! $is_hostinger_reach && ! $is_elementor, |
| 197 | 'is_go_to_plugin_visible' => ! $is_hostinger_reach, |
| 198 | 'import_status' => $this->import_manager->get_status( $integration_name ), |
| 199 | 'is_installable' => $this->plugin_manager->is_installed( $integration_name ) || ! empty( $integrations[ $integration_name ]['download_url'] ), |
| 200 | ) |
| 201 | ); |
| 202 | |
| 203 | } |
| 204 | |
| 205 | return $integrations; |
| 206 | } |
| 207 | |
| 208 | private function save_integration( string $integration_name, array $data ): bool { |
| 209 | $integrations_data = $this->get_integrations_data(); |
| 210 | $integrations_data[ $integration_name ] = $data; |
| 211 | |
| 212 | return $this->save_integrations_data( $integrations_data ); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | private function save_integrations_data( array $data ): bool { |
| 217 | return update_option( self::INTEGRATIONS_OPTION_NAME, $data ); |
| 218 | } |
| 219 | } |
| 220 |