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 (#6101)

* Add Jetpack integration loader, add the way to load config in local dev env

* refactor: unify loading logic and use filters to modify file path or config data

---------

Co-authored-by: Volodymyr Kolesnykov <[email protected]>
  • Loading branch information
rinatkhaziev and sjinks authored Jan 24, 2025
1 parent 62bd96d commit b34c55f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 17 deletions.
43 changes: 26 additions & 17 deletions integrations/integration-vip-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,34 @@ protected function get_vip_config_from_file( string $slug ) {
? constant( 'WPVIP_INTEGRATIONS_CONFIG_DIR' )
: ABSPATH . 'config/integrations-config';
$config_file_name = $slug . '-config.php';
$config_file_path = $config_file_directory . '/' . $config_file_name;

/**
* Clear cache to always read data from latest config file.
*
* Kubernetes ConfigMap updates the file via symlink instead of actually replacing the file and
* PHP cache can hold a reference to the old symlink that can cause fatal if we use require
* on it.
*/
clearstatcache( true, $config_file_directory . '/' . $config_file_name );
// Clears cache for files created by k8s ConfigMap.
clearstatcache( true, $config_file_directory . '/..data' );
clearstatcache( true, $config_file_directory . '/..data/' . $config_file_name );

if ( ! is_readable( $config_file_path ) ) {
return null;
$config_file_path_orig = $config_file_directory . '/' . $config_file_name;

$config_file_path = apply_filters( 'vip_integrations_config_file_path', $config_file_path_orig, $slug );
$config_data = apply_filters( 'vip_integrations_pre_load_config', null, $config_file_path, $slug );

if ( is_null( $config_data ) ) {
if ( $config_file_path_orig === $config_file_path ) {
/**
* Clear cache to always read data from latest config file.
*
* Kubernetes ConfigMap updates the file via symlink instead of actually replacing the file and
* PHP cache can hold a reference to the old symlink that can cause fatal if we use require
* on it.
*/
clearstatcache( true, $config_file_directory . '/' . $config_file_name );
// Clears cache for files created by k8s ConfigMap.
clearstatcache( true, $config_file_directory . '/..data' );
clearstatcache( true, $config_file_directory . '/..data/' . $config_file_name );
}

if ( ! is_readable( $config_file_path ) ) {
return null;
}

$config_data = require $config_file_path;
}

return require $config_file_path;
return $config_data;
}

/**
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 b34c55f

Please sign in to comment.