| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine; |
| 4 |
|
| 5 |
use ReflectionClass; |
| 6 |
use StoreEngine; |
| 7 |
use StoreEngine\Classes\Exceptions\StoreEngineException; |
| 8 |
use StoreEngine\Classes\Exceptions\StoreEngineRuntimeException; |
| 9 |
use StoreEngine\Classes\Order; |
| 10 |
use StoreEngine\Integrations\CourseBundle; |
| 11 |
use StoreEngine\Integrations\TutorBooking; |
| 12 |
use StoreEngine\Traits\Singleton; |
| 13 |
use StoreEngine\Utils\Helper; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
use StoreEngine\Integrations\AbstractIntegration; |
| 20 |
use StoreEngine\Integrations\AcademyLms; |
| 21 |
use StoreEngine\Integrations\MembershipAddon; |
| 22 |
|
| 23 |
class Integrations { |
| 24 |
use Singleton; |
| 25 |
|
| 26 |
/** |
| 27 |
* Container registry. |
| 28 |
* |
| 29 |
* @var array<string, AbstractIntegration> |
| 30 |
*/ |
| 31 |
private array $integrations = []; |
| 32 |
|
| 33 |
protected function __construct() { |
| 34 |
add_action( 'init', [ $this, 'register_integrations' ], - 1 ); |
| 35 |
add_filter( 'storeengine/backend_scripts_data', [ $this, 'add_scripts_data' ] ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Initialize & register integration classes. |
| 40 |
* |
| 41 |
* @return void |
| 42 |
* @throws StoreEngineRuntimeException |
| 43 |
*/ |
| 44 |
public function register_integrations() { |
| 45 |
$integrations = [ |
| 46 |
AcademyLms::ID => AcademyLms::class, |
| 47 |
MembershipAddon::ID => MembershipAddon::class, |
| 48 |
TutorBooking::ID => TutorBooking::class, |
| 49 |
CourseBundle::ID => CourseBundle::class, |
| 50 |
]; |
| 51 |
|
| 52 |
$integrations = apply_filters( 'storeengine/integrations/registry', $integrations ); |
| 53 |
|
| 54 |
foreach ( $integrations as $id => $integration ) { |
| 55 |
/** @var AbstractIntegration $integration */ |
| 56 |
$integration = new $integration(); |
| 57 |
|
| 58 |
if ( ! $integration instanceof AbstractIntegration ) { |
| 59 |
throw new StoreEngineRuntimeException( sprintf( |
| 60 |
// translators: %s. Integration Identifier. |
| 61 |
__( 'Integration “%1$s” (%2$s) must be a valid subclass of “%3$s”.', 'storeengine' ), |
| 62 |
$id, |
| 63 |
get_class( $integration ), |
| 64 |
AbstractIntegration::class |
| 65 |
) ); |
| 66 |
} |
| 67 |
|
| 68 |
// Add to repository. |
| 69 |
$this->integrations[ $id ] = $integration; |
| 70 |
} |
| 71 |
|
| 72 |
do_action( 'storeengine/integrations/loaded' ); |
| 73 |
} |
| 74 |
|
| 75 |
public function add_scripts_data( array $data ): array { |
| 76 |
$data['integrations'] = array_values( array_map( fn( $integration ) => [ |
| 77 |
'id' => $integration->get_id(), |
| 78 |
'label' => $integration->get_label(), |
| 79 |
'value' => $integration->get_id(), |
| 80 |
'icon' => $integration->get_logo(), |
| 81 |
'isEnabled' => $integration->enabled(), |
| 82 |
], $this->get_integrations() ) ); |
| 83 |
|
| 84 |
return $data; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Returns an instance of the specified integration class |
| 89 |
* |
| 90 |
* @template Provider of AbstractIntegration |
| 91 |
* @param key-of<self::integrations> $provider |
| 92 |
* @phpstan-param key-of<self::integrations> $provider |
| 93 |
* |
| 94 |
* @return Provider Object instance |
| 95 |
* @throws StoreEngineException |
| 96 |
*/ |
| 97 |
public function get_integration( string $provider ): AbstractIntegration { |
| 98 |
if ( ! did_action( 'storeengine/integrations/loaded' ) ) { |
| 99 |
_doing_it_wrong( __CLASS__ . '::integrations', 'Trying to get integrations before integrations are loaded', '1.0.0' ); |
| 100 |
} |
| 101 |
|
| 102 |
if ( array_key_exists( $provider, $this->integrations ) ) { |
| 103 |
return $this->integrations[ $provider ]; |
| 104 |
} |
| 105 |
|
| 106 |
throw new StoreEngineException( esc_html__( 'Integration does not exist.', 'storeengine' ), 'unknown-integration', [ 'provider' => esc_html( $provider ) ] ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @return AbstractIntegration[] |
| 111 |
*/ |
| 112 |
public function get_integrations(): array { |
| 113 |
if ( ! did_action( 'storeengine/integrations/loaded' ) ) { |
| 114 |
_doing_it_wrong( __CLASS__ . '::integrations', 'Trying to get integrations before integrations are loaded', '1.0.0' ); |
| 115 |
} |
| 116 |
|
| 117 |
return $this->integrations; |
| 118 |
} |
| 119 |
} |
| 120 |
|