Skip to content

Commit

Permalink
➕ ADDS: support for setting up integration configs via provided filter
Browse files Browse the repository at this point in the history
  • Loading branch information
mehmoodak committed Jul 5, 2023
1 parent 9702ef8 commit b4abc53
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
41 changes: 35 additions & 6 deletions integrations/integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ abstract class Integration {
*/
public bool $is_active_by_customer = false;

/**
* Name of the filter which we will use to setup the integration configs.
*
* As of now there is no default so each integration will define its own filter in their class.
*
* @var string
*/
protected string $integration_configs_filter_name = '';

/**
* Constructor.
*
Expand Down Expand Up @@ -155,7 +164,7 @@ private function set_vip_config(): void {
}

/**
* Returns true if the integration is active by VIP.
* Returns true if the integration is active by VIP and setup plugin configs which are provided by VIP.
*
* @return bool
*/
Expand All @@ -172,16 +181,36 @@ private function is_active_by_vip(): bool {
return false;
}

// Check for network-site enablement if multisite.
// Check network site enablement if multisite.
if ( is_multisite() ) {
if ( is_network_admin() ) {
return false;
}

return $this->get_value_from_vip_config( 'network_sites', 'status' ) === Site_Integration_Status::ENABLED;
// If enabled on network site then set credentials via filter and return true.
if ( $this->get_value_from_vip_config( 'network_sites', 'status' ) === Site_Integration_Status::ENABLED ) {
if ( '' !== $this->integration_configs_filter_name ) {
add_filter( $this->integration_configs_filter_name, function() {
return $this->get_value_from_vip_config( 'network_sites', 'configs' );
} );
}

return true;
}
}

// If enabled on site then set credentials via filter and return true.
if ( Site_Integration_Status::ENABLED === $site_status ) {
if ( '' !== $this->integration_configs_filter_name ) {
add_filter( $this->integration_configs_filter_name, function() {
return $this->get_value_from_vip_config( 'site', 'configs' );
} );
}

return true;
}

return Site_Integration_Status::ENABLED === $site_status; // Return site status if not multisite.
return false;
}

/**
Expand All @@ -190,9 +219,9 @@ private function is_active_by_vip(): bool {
* @param string $config_type Type of the config whose data is needed i.e. client, site, network-sites etc.
* @param string $key Key of the config from which we have to extract the data.
*
* @return string
* @return string|array
*/
private function get_value_from_vip_config( string $config_type, string $key ): string {
private function get_value_from_vip_config( string $config_type, string $key ) {
if ( ! isset( $this->vip_config[ $config_type ] ) ) {
return '';
}
Expand Down
7 changes: 7 additions & 0 deletions integrations/parsely.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
* @private
*/
class ParselyIntegration extends Integration {
/**
* Name of the filter which we will use to setup the plugin configs.
*
* @var string
*/
protected string $integration_configs_filter_name = 'wp_parsely_credentials';

/**
* Applies hooks to load plugin.
*
Expand Down
2 changes: 1 addition & 1 deletion vip-integrations.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Plugin Name: VIP Integrations
* Description: Plugin loading for VIP plugin integrations.
* Description: Plugin for loading integrations provided by VIP.
* Author: Automattic
* License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
Expand Down
1 change: 1 addition & 0 deletions wp-parsely-3.8
Submodule wp-parsely-3.8 added at 213566

0 comments on commit b4abc53

Please sign in to comment.