AuthController.php
2 years ago
AutomationController.php
3 years ago
EventController.php
3 years ago
GlobalSearchController.php
2 years ago
IntegrationsController.php
2 years ago
OptionController.php
3 years ago
RestController.php
2 years ago
RoutesController.php
3 years ago
SettingsController.php
3 years ago
IntegrationsController.php
148 lines
| 1 | <?php |
| 2 | /** |
| 3 | * File Integrations Controller. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @package SureTrigger |
| 7 | */ |
| 8 | |
| 9 | namespace SureTriggers\Controllers; |
| 10 | |
| 11 | use RecursiveDirectoryIterator; |
| 12 | use RecursiveIteratorIterator; |
| 13 | use ReflectionClass; |
| 14 | use SureTriggers\Integrations\Integrations; |
| 15 | use SureTriggers\Traits\SingletonLoader; |
| 16 | use WP_REST_Request; |
| 17 | use WP_REST_Response; |
| 18 | |
| 19 | /** |
| 20 | * Class IntegrationsController |
| 21 | * |
| 22 | * @package SureTriggers\Controllers |
| 23 | */ |
| 24 | class IntegrationsController { |
| 25 | |
| 26 | use SingletonLoader; |
| 27 | |
| 28 | /** |
| 29 | * Get registered pluggable variables. |
| 30 | * |
| 31 | * @return mixed|void |
| 32 | */ |
| 33 | public static function get_registered_pluggable_variables() { |
| 34 | return apply_filters( 'sure_trigger_register_pluggable_variables', [] ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Registering integration classes |
| 39 | * |
| 40 | * @param string $class Integration class name. |
| 41 | * |
| 42 | * @return void |
| 43 | */ |
| 44 | public static function register( $class ) { |
| 45 | add_filter( |
| 46 | 'sure_trigger_integrations', |
| 47 | function ( $integrations_classes ) use ( $class ) { |
| 48 | |
| 49 | $obj = new $class(); |
| 50 | |
| 51 | $integrations_classes[ $obj->get_id() ] = $obj; |
| 52 | |
| 53 | return $integrations_classes; |
| 54 | }, |
| 55 | 99 |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Include activated integrations events files |
| 61 | * |
| 62 | * @return void |
| 63 | */ |
| 64 | public static function load_event_files() { |
| 65 | /** |
| 66 | * Get integration class and check if the class is activated |
| 67 | * If yes, then include those classes event files. |
| 68 | */ |
| 69 | $integrations_classes = self::get_integrations(); |
| 70 | foreach ( $integrations_classes as $class ) { |
| 71 | |
| 72 | if ( $class->is_enabled() ) { |
| 73 | $reflector = new ReflectionClass( $class ); |
| 74 | $event_files = new RecursiveDirectoryIterator( dirname( $reflector->getFileName() ) ); |
| 75 | foreach ( new RecursiveIteratorIterator( $event_files ) as $filename => $file ) { |
| 76 | if ( $file->isFile() ) { |
| 77 | require_once $filename; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get all integration classes |
| 86 | * |
| 87 | * @return Integrations[] |
| 88 | */ |
| 89 | public static function get_integrations() { |
| 90 | return apply_filters( 'sure_trigger_integrations', [] ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get integration which plugins are activated. |
| 95 | * |
| 96 | * @return array |
| 97 | */ |
| 98 | public static function get_activated_integrations() { |
| 99 | $integrations_classes = self::get_integrations(); |
| 100 | $allowed_integrations = []; |
| 101 | |
| 102 | foreach ( $integrations_classes as $integration => $class ) { |
| 103 | if ( 'WordPress' === $integration ) { |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | if ( $class->is_enabled() ) { |
| 108 | $allowed_integrations [] = $integration; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return $allowed_integrations; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Verify child integration |
| 117 | * |
| 118 | * @param WP_REST_Request $request Request data. |
| 119 | * @return WP_REST_Response |
| 120 | */ |
| 121 | public function child_integration_verify( $request ) { |
| 122 | $plugin_url = admin_url( 'plugins.php' ); |
| 123 | if ( $request->get_param( 'integration_plugin' ) ) { |
| 124 | $is_plugin_active = false; |
| 125 | |
| 126 | $plugin = $request->get_param( 'integration_plugin' ); |
| 127 | |
| 128 | $integration_class_name = $plugin['key']; |
| 129 | |
| 130 | $fully_qualified_class_name = "\SureTriggers\Integrations\\$integration_class_name\\$integration_class_name"; |
| 131 | |
| 132 | if ( class_exists( $fully_qualified_class_name ) ) { |
| 133 | $class_obj = new $fully_qualified_class_name(); |
| 134 | $is_plugin_active = $class_obj->is_plugin_installed(); |
| 135 | } |
| 136 | |
| 137 | if ( $is_plugin_active ) { |
| 138 | return RestController::success_message( 'Integration is verified and has all necessary plugins installed.' ); |
| 139 | } else { |
| 140 | return RestController::error_message( sprintf( 'To use %1s integration, you must have installed and activated %1$s on your <a class="text-app-primary" target="_blank" href="%2$s"> WordPress website</a>.', $plugin['name'], $plugin_url ), 200 ); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return RestController::error_message( 'No integration details provided.', 200 ); |
| 145 | } |
| 146 | |
| 147 | } |
| 148 |