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
PluginManager.php
97 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Integrations; |
| 4 | |
| 5 | use Hostinger\Reach\Dto\PluginData; |
| 6 | use Plugin_Upgrader; |
| 7 | use Plugin_Upgrader_Skin; |
| 8 | use WP_Error; |
| 9 | |
| 10 | if ( ! DEFINED( 'ABSPATH' ) ) { |
| 11 | die; |
| 12 | } |
| 13 | |
| 14 | class PluginManager { |
| 15 | |
| 16 | public static function plugin_data(): array { |
| 17 | return apply_filters( 'hostinger_reach_plugin_data', array() ); |
| 18 | } |
| 19 | |
| 20 | public function install( string $plugin_name ): bool|WP_Error { |
| 21 | if ( $this->is_installed( $plugin_name ) ) { |
| 22 | return true; |
| 23 | } |
| 24 | |
| 25 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 26 | require_once ABSPATH . 'wp-admin/includes/misc.php'; |
| 27 | require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 28 | require_once ABSPATH . 'wp-admin/includes/theme.php'; |
| 29 | |
| 30 | $plugin = $this->get_plugin( $plugin_name ); |
| 31 | if ( is_null( $plugin ) ) { |
| 32 | return new WP_Error( 'plugin_not_found', 'Plugin not found' ); |
| 33 | } |
| 34 | |
| 35 | $plugin_data = $plugin->to_array(); |
| 36 | $temp_file = download_url( $plugin_data['download_url'] ); |
| 37 | |
| 38 | if ( is_wp_error( $temp_file ) ) { |
| 39 | return $temp_file; |
| 40 | } |
| 41 | |
| 42 | $upgrader = new Plugin_Upgrader( new Plugin_Upgrader_Skin() ); |
| 43 | $result = $upgrader->install( $temp_file ); |
| 44 | |
| 45 | wp_delete_file( $temp_file ); |
| 46 | |
| 47 | if ( is_wp_error( $result ) ) { |
| 48 | return $result; |
| 49 | } |
| 50 | |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | public function activate( string $plugin_name ): bool { |
| 55 | if ( ! $this->is_installed( $plugin_name ) ) { |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | if ( $this->is_active( $plugin_name ) ) { |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | return is_null( activate_plugin( $this->get_plugin_path( $plugin_name ) ) ); |
| 64 | } |
| 65 | |
| 66 | public function is_installed( string $plugin_name ): bool { |
| 67 | return file_exists( WP_PLUGIN_DIR . '/' . $this->get_plugin_path( $plugin_name ) ); |
| 68 | } |
| 69 | |
| 70 | public function is_active( string $plugin_name ): bool { |
| 71 | $plugin_path = $this->get_plugin_path( $plugin_name ); |
| 72 | |
| 73 | return is_plugin_active( $plugin_path ); |
| 74 | } |
| 75 | |
| 76 | public function get_plugin_path( string $plugin_name ): string { |
| 77 | $plugin = $this->get_plugin( $plugin_name ); |
| 78 | if ( is_null( $plugin ) ) { |
| 79 | return '/'; |
| 80 | } |
| 81 | |
| 82 | $plugin_data = $plugin->to_array(); |
| 83 | |
| 84 | if ( ! isset( $plugin_data['folder'] ) || ! isset( $plugin_data['file'] ) ) { |
| 85 | return '/'; |
| 86 | } |
| 87 | |
| 88 | return $plugin_data['folder'] . '/' . $plugin_data['file']; |
| 89 | } |
| 90 | |
| 91 | public function get_plugin( string $plugin_name ): ?PluginData { |
| 92 | $plugin_data = self::plugin_data(); |
| 93 | |
| 94 | return $plugin_data[ $plugin_name ] ?? null; |
| 95 | } |
| 96 | } |
| 97 |