Skip to content

Commit

Permalink
Add Jetpack integration loader, add the way to load config in local d…
Browse files Browse the repository at this point in the history
…ev env
  • Loading branch information
rinatkhaziev committed Jan 16, 2025
1 parent b444319 commit 7ed5e81
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
32 changes: 31 additions & 1 deletion integrations/integration-vip-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ public function __construct( string $slug ) {
* @param string $slug A unique identifier for the integration.
*/
private function set_config( string $slug ): void {
$config = $this->get_vip_config_from_file( $slug );
if ( defined( 'VIP_GO_APP_ENVIRONMENT' ) && 'local' === constant( 'VIP_GO_APP_ENVIRONMENT' ) ) {
$config = $this->get_local_config_from_file( $slug );
} else {
$config = $this->get_vip_config_from_file( $slug );
}

if ( ! is_array( $config ) ) {
return;
Expand Down Expand Up @@ -103,6 +107,32 @@ protected function get_vip_config_from_file( string $slug ) {
return require $config_file_path;
}

/**
* Get config for local dev env and Codespaces
*
* @param string $slug A unique identifier for the integration.
*
* @return null|mixed
*/
protected function get_local_config_from_file( string $slug ) {
$config_file = '/app/integrations.json';
if ( ! file_exists( $config_file ) ) {
return null;
}

// phpcs:ignore WordPressVIPMinimum.Performance.FetchingRemoteData.FileGetContentsUnknown
$config = json_decode( file_get_contents( $config_file ), true );
if ( ! isset( $config[ $slug ] ) ) {
return null;
}

// We don't really care about the org config in local dev.
// Network config might be tricky if there's mismatch between data.
return [
'env' => $config[ $slug ],
];
}

/**
* Returns `true` if the integration is enabled in VIP config else `false`.
*
Expand Down
34 changes: 34 additions & 0 deletions integrations/jetpack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Integration: Jetpack.
*/
namespace Automattic\VIP\Integrations;

class JetpackIntegration extends Integration {
protected string $version = '';

public function is_loaded(): bool {
return class_exists( 'Jetpack' );
}

public function configure(): void {
if ( isset( $this->get_env_config()['version'] ) ) {
$this->version = $this->get_env_config()['version'];
} elseif ( defined( 'VIP_JETPACK_PINNED_VERSION' ) ) {
$this->version = constant( 'VIP_JETPACK_PINNED_VERSION' );
} else {
$this->version = vip_default_jetpack_version();
}
}

public function load(): void {
if ( $this->is_loaded() ) {
return;
}

// Pass through to the old code for now which will respect all existing constants
if ( ! defined( 'WP_INSTALLING' ) ) {
vip_jetpack_load();
}
}
}
8 changes: 8 additions & 0 deletions vip-integrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@
require_once __DIR__ . '/integrations/vip-governance.php';
require_once __DIR__ . '/integrations/enterprise-search.php';

if ( file_exists( __DIR__ . '/integrations/jetpack.php' ) ) {
require_once __DIR__ . '/integrations/jetpack.php';
}

// Register VIP integrations here.
IntegrationsSingleton::instance()->register( new BlockDataApiIntegration( 'block-data-api' ) );
IntegrationsSingleton::instance()->register( new ParselyIntegration( 'parsely' ) );
IntegrationsSingleton::instance()->register( new VipGovernanceIntegration( 'vip-governance' ) );
IntegrationsSingleton::instance()->register( new EnterpriseSearchIntegration( 'enterprise-search' ) );

if ( class_exists( __NAMESPACE__ . '\\JetpackIntegration' ) ) {
IntegrationsSingleton::instance()->register( new JetpackIntegration( 'jetpack' ) );
}
// @codeCoverageIgnoreEnd

/**
Expand Down

0 comments on commit 7ed5e81

Please sign in to comment.