* Initial refactor and consolidation of the DataSourcePoller code

* Move transient logic into DataSourcePoller class

* Fix function name

* Fix tests

* Add changelog

* Fix tests and update use of DataSourcePoller

* Make use of ID's in the DataSourcePoller

* Fix parsing error

* Update data_sources filter
This commit is contained in:
louwie17 2021-10-20 15:53:43 -03:00 committed by GitHub
parent ef0fde6632
commit 3707812ebe
16 changed files with 439 additions and 628 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: Update
Refactor data source poller for re-usability. #7671

View File

@ -0,0 +1,242 @@
<?php
namespace Automattic\WooCommerce\Admin;
/**
* Specs data source poller class.
* This handles polling specs from JSON endpoints, and
* stores the specs in to the database as an option.
*/
abstract class DataSourcePoller {
/**
* Get class instance.
*/
abstract public static function get_instance();
/**
* Name of data sources filter.
*/
const FILTER_NAME = 'data_source_poller_data_sources';
/**
* Id of DataSourcePoller.
*
* @var string
*/
protected $id = array();
/**
* Default data sources array.
*
* @var array
*/
protected $data_sources = array();
/**
* Default args.
*
* @var array
*/
protected $args = array();
/**
* The logger instance.
*
* @var WC_Logger|null
*/
protected static $logger = null;
/**
* Constructor.
*
* @param string $id id of DataSourcePoller.
* @param array $data_sources urls for data sources.
* @param array $args Options for DataSourcePoller.
*/
public function __construct( $id, $data_sources = array(), $args = array() ) {
$this->data_sources = $data_sources;
$this->id = $id;
$arg_defaults = array(
'spec_key' => 'id',
'transient_name' => 'woocommerce_admin_' . $id . '_specs',
'transient_expiry' => 7 * DAY_IN_SECONDS,
);
$this->args = wp_parse_args( $args, $arg_defaults );
}
/**
* Get the logger instance.
*
* @return WC_Logger
*/
protected static function get_logger() {
if ( is_null( self::$logger ) ) {
self::$logger = wc_get_logger();
}
return self::$logger;
}
/**
* Returns the key identifier of spec, this can easily be overwritten. Defaults to id.
*
* @param mixed $spec a JSON parsed spec coming from the JSON feed.
* @return string|boolean
*/
protected function get_spec_key( $spec ) {
$key = $this->args['spec_key'];
if ( isset( $spec->$key ) ) {
return $spec->$key;
}
return false;
}
/**
* Reads the data sources for specs and persists those specs.
*
* @return array list of specs.
*/
public function get_specs_from_data_sources() {
$specs = get_transient( $this->args['transient_name'] );
if ( false === $specs || ! is_array( $specs ) || 0 === count( $specs ) ) {
$this->read_specs_from_data_sources();
$specs = get_transient( $this->args['transient_name'] );
}
return false !== $specs ? $specs : array();
}
/**
* Reads the data sources for specs and persists those specs.
*
* @return bool Whether any specs were read.
*/
public function read_specs_from_data_sources() {
$specs = array();
$data_sources = apply_filters( self::FILTER_NAME, $this->data_sources, $this::ID );
// Note that this merges the specs from the data sources based on the
// id - last one wins.
foreach ( $data_sources as $url ) {
$specs_from_data_source = self::read_data_source( $url );
$this->merge_specs( $specs_from_data_source, $specs, $url );
}
// Persist the specs as a transient.
set_transient(
$this->args['transient_name'],
$specs,
$this->args['transient_expiry']
);
return 0 !== count( $specs );
}
/**
* Delete the specs transient.
*
* @return bool success of failure of transient deletion.
*/
public function delete_specs_transient() {
return delete_transient( $this->args['transient_name'] );
}
/**
* Read a single data source and return the read specs
*
* @param string $url The URL to read the specs from.
*
* @return array The specs that have been read from the data source.
*/
protected static function read_data_source( $url ) {
$logger_context = array( 'source' => $url );
$logger = self::get_logger();
$response = wp_remote_get(
add_query_arg(
'_locale',
get_user_locale(),
$url
)
);
if ( is_wp_error( $response ) || ! isset( $response['body'] ) ) {
$logger->error(
'Error getting data feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $response, true ), $logger_context );
return [];
}
$body = $response['body'];
$specs = json_decode( $body );
if ( null === $specs ) {
$logger->error(
'Empty response in data feed',
$logger_context
);
return [];
}
if ( ! is_array( $specs ) ) {
$logger->error(
'Data feed is not an array',
$logger_context
);
return [];
}
return $specs;
}
/**
* Merge the specs.
*
* @param Array $specs_to_merge_in The specs to merge in to $specs.
* @param Array $specs The list of specs being merged into.
* @param string $url The url of the feed being merged in (for error reporting).
*/
protected function merge_specs( $specs_to_merge_in, &$specs, $url ) {
foreach ( $specs_to_merge_in as $spec ) {
if ( ! $this->validate_spec( $spec, $url ) ) {
continue;
}
$id = $this->get_spec_key( $spec );
$specs[ $id ] = $spec;
}
}
/**
* Validate the spec.
*
* @param object $spec The spec to validate.
* @param string $url The url of the feed that provided the spec.
*
* @return bool The result of the validation.
*/
protected function validate_spec( $spec, $url ) {
$logger = self::get_logger();
$logger_context = array( 'source' => $url );
if ( ! $this->get_spec_key( $spec ) ) {
$logger->error(
'Spec is invalid because the id is missing in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $spec, true ), $logger_context );
return false;
}
return true;
}
}

View File

@ -97,7 +97,8 @@ class Events {
$this->possibly_add_notes();
if ( $this->is_remote_inbox_notifications_enabled() ) {
DataSourcePoller::read_specs_from_data_sources();
$data_source_poller = RemoteInboxNotificationsEngine::get_data_source_poller_instance();
$data_source_poller->read_specs_from_data_sources();
RemoteInboxNotificationsEngine::run();
}

View File

@ -1,162 +0,0 @@
<?php
/**
* Handles polling and storage of specs
*/
namespace Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions;
defined( 'ABSPATH' ) || exit;
/**
* Specs data source poller class.
* This handles polling specs from JSON endpoints.
*/
class DataSourcePoller {
/**
* Name of data sources filter.
*/
const FILTER_NAME = 'woocommerce_admin_payment_gateway_suggestions_data_sources';
/**
* Default data sources array.
*/
const DATA_SOURCES = array(
'https://woocommerce.com/wp-json/wccom/payment-gateway-suggestions/1.0/suggestions.json',
);
/**
* The logger instance.
*
* @var WC_Logger|null
*/
protected static $logger = null;
/**
* Get the logger instance.
*
* @return WC_Logger
*/
private static function get_logger() {
if ( is_null( self::$logger ) ) {
self::$logger = wc_get_logger();
}
return self::$logger;
}
/**
* Reads the data sources for specs and persists those specs.
*
* @return bool Whether any specs were read.
*/
public static function read_specs_from_data_sources() {
$specs = array();
$data_sources = apply_filters( self::FILTER_NAME, self::DATA_SOURCES );
// Note that this merges the specs from the data sources based on the
// id - last one wins.
foreach ( $data_sources as $url ) {
$specs_from_data_source = self::read_data_source( $url );
self::merge_specs( $specs_from_data_source, $specs, $url );
}
return $specs;
}
/**
* Read a single data source and return the read specs
*
* @param string $url The URL to read the specs from.
*
* @return array The specs that have been read from the data source.
*/
private static function read_data_source( $url ) {
$logger_context = array( 'source' => $url );
$logger = self::get_logger();
$response = wp_remote_get(
add_query_arg(
'_locale',
get_user_locale(),
$url
)
);
if ( is_wp_error( $response ) || ! isset( $response['body'] ) ) {
$logger->error(
'Error getting remote payment method data feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $response, true ), $logger_context );
return [];
}
$body = $response['body'];
$specs = json_decode( $body );
if ( null === $specs ) {
$logger->error(
'Empty response in remote payment method data feed',
$logger_context
);
return [];
}
if ( ! is_array( $specs ) ) {
$logger->error(
'Remote payment method data feed is not an array',
$logger_context
);
return [];
}
return $specs;
}
/**
* Merge the specs.
*
* @param Array $specs_to_merge_in The specs to merge in to $specs.
* @param Array $specs The list of specs being merged into.
* @param string $url The url of the feed being merged in (for error reporting).
*/
private static function merge_specs( $specs_to_merge_in, &$specs, $url ) {
foreach ( $specs_to_merge_in as $spec ) {
if ( ! self::validate_spec( $spec, $url ) ) {
continue;
}
$id = $spec->id;
$specs[ $id ] = $spec;
}
}
/**
* Validate the spec.
*
* @param object $spec The spec to validate.
* @param string $url The url of the feed that provided the spec.
*
* @return bool The result of the validation.
*/
private static function validate_spec( $spec, $url ) {
$logger = self::get_logger();
$logger_context = array( 'source' => $url );
if ( ! isset( $spec->id ) ) {
$logger->error(
'Spec is invalid because the id is missing in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $spec, true ), $logger_context );
return false;
}
return true;
}
}

View File

@ -16,8 +16,6 @@ use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\PaymentGatew
* This goes through the specs and gets eligible payment gateways.
*/
class Init {
const SPECS_TRANSIENT_NAME = 'woocommerce_admin_payment_gateway_suggestions_specs';
/**
* Constructor.
*/
@ -53,29 +51,21 @@ class Init {
* Delete the specs transient.
*/
public static function delete_specs_transient() {
delete_transient( self::SPECS_TRANSIENT_NAME );
PaymentGatewaySuggestionsDataSourcePoller::get_instance()->delete_specs_transient();
}
/**
* Get specs or fetch remotely if they don't exist.
*/
public static function get_specs() {
$specs = get_transient( self::SPECS_TRANSIENT_NAME );
if ( 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) ) {
return DefaultPaymentGateways::get_all();
}
$specs = PaymentGatewaySuggestionsDataSourcePoller::get_instance()->get_specs_from_data_sources();
// Fetch specs if they don't yet exist.
if ( false === $specs || ! is_array( $specs ) || 0 === count( $specs ) ) {
if ( 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) ) {
return DefaultPaymentGateways::get_all();
}
$specs = DataSourcePoller::read_specs_from_data_sources();
// Fall back to default specs if polling failed.
if ( ! $specs ) {
return DefaultPaymentGateways::get_all();
}
set_transient( self::SPECS_TRANSIENT_NAME, $specs, 7 * DAY_IN_SECONDS );
return DefaultPaymentGateways::get_all();
}
return $specs;

View File

@ -0,0 +1,37 @@
<?php
namespace Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions;
use Automattic\WooCommerce\Admin\DataSourcePoller;
/**
* Specs data source poller class for payment gateway suggestions.
*/
class PaymentGatewaySuggestionsDataSourcePoller extends DataSourcePoller {
const ID = 'payment_gateway_suggestions';
/**
* Default data sources array.
*/
const DATA_SOURCES = array(
'https://woocommerce.com/wp-json/wccom/payment-gateway-suggestions/1.0/suggestions.json',
);
/**
* Class instance.
*
* @var Analytics instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self( self::ID, self::DATA_SOURCES );
}
return self::$instance;
}
}

View File

@ -1,154 +0,0 @@
<?php
/**
* Handles polling and storage of specs
*/
namespace Automattic\WooCommerce\Admin\Features\RemoteFreeExtensions;
defined( 'ABSPATH' ) || exit;
/**
* Specs data source poller class.
* This handles polling specs from JSON endpoints.
*/
class DataSourcePoller {
const DATA_SOURCES = array(
'https://woocommerce.com/wp-json/wccom/obw-free-extensions/2.0/extensions.json',
);
/**
* The logger instance.
*
* @var WC_Logger|null
*/
protected static $logger = null;
/**
* Get the logger instance.
*
* @return WC_Logger
*/
private static function get_logger() {
if ( is_null( self::$logger ) ) {
self::$logger = wc_get_logger();
}
return self::$logger;
}
/**
* Reads the data sources for specs and persists those specs.
*
* @return bool Whether any specs were read.
*/
public static function read_specs_from_data_sources() {
$specs = array();
$data_sources = apply_filters( 'woocommerce_admin_remote_free_extensions_data_sources', self::DATA_SOURCES );
// Note that this merges the specs from the data sources based on the
// key - last one wins.
foreach ( $data_sources as $url ) {
$specs_from_data_source = self::read_data_source( $url );
self::merge_specs( $specs_from_data_source, $specs, $url );
}
return $specs;
}
/**
* Read a single data source and return the read specs
*
* @param string $url The URL to read the specs from.
*
* @return array The specs that have been read from the data source.
*/
private static function read_data_source( $url ) {
$logger_context = array( 'source' => $url );
$logger = self::get_logger();
$response = wp_remote_get(
add_query_arg(
'_locale',
get_user_locale(),
$url
)
);
if ( is_wp_error( $response ) || ! isset( $response['body'] ) ) {
$logger->error(
'Error getting remote payment method data feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $response, true ), $logger_context );
return [];
}
$body = $response['body'];
$specs = json_decode( $body );
if ( null === $specs ) {
$logger->error(
'Empty response in remote payment method data feed',
$logger_context
);
return [];
}
if ( ! is_array( $specs ) ) {
$logger->error(
'Remote payment method data feed is not an array',
$logger_context
);
return [];
}
return $specs;
}
/**
* Merge the specs.
*
* @param Array $specs_to_merge_in The specs to merge in to $specs.
* @param Array $specs The list of specs being merged into.
* @param string $url The url of the feed being merged in (for error reporting).
*/
private static function merge_specs( $specs_to_merge_in, &$specs, $url ) {
foreach ( $specs_to_merge_in as $spec ) {
if ( ! self::validate_spec( $spec, $url ) ) {
continue;
}
$key = $spec->key;
$specs[ $key ] = $spec;
}
}
/**
* Validate the spec.
*
* @param object $spec The spec to validate.
* @param string $url The url of the feed that provided the spec.
*
* @return bool The result of the validation.
*/
private static function validate_spec( $spec, $url ) {
$logger = self::get_logger();
$logger_context = array( 'source' => $url );
if ( ! isset( $spec->key ) ) {
$logger->error(
'Spec is invalid because the key is missing in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $spec, true ), $logger_context );
return false;
}
return true;
}
}

View File

@ -15,7 +15,6 @@ use Automattic\WooCommerce\Admin\Features\RemoteFreeExtensions\DefaultFreeExtens
* This goes through the specs and gets eligible payment methods.
*/
class Init {
const SPECS_TRANSIENT_NAME = 'woocommerce_admin_remote_free_extensions_specs';
/**
* Constructor.
@ -62,29 +61,21 @@ class Init {
* Delete the specs transient.
*/
public static function delete_specs_transient() {
delete_transient( self::SPECS_TRANSIENT_NAME );
RemoteFreeExtensionsDataSourcePoller::get_instance()->delete_specs_transient();
}
/**
* Get specs or fetch remotely if they don't exist.
*/
public static function get_specs() {
$specs = get_transient( self::SPECS_TRANSIENT_NAME );
if ( 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) ) {
return DefaultFreeExtensions::get_all();
}
$specs = RemoteFreeExtensionsDataSourcePoller::get_instance()->get_specs_from_data_sources();
// Fetch specs if they don't yet exist.
if ( false === $specs || ! is_array( $specs ) || 0 === count( $specs ) ) {
if ( 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) ) {
return DefaultFreeExtensions::get_all();
}
$specs = DataSourcePoller::read_specs_from_data_sources();
// Fall back to default specs if polling failed.
if ( ! $specs || empty( $specs ) ) {
return DefaultFreeExtensions::get_all();
}
set_transient( self::SPECS_TRANSIENT_NAME, $specs, 7 * DAY_IN_SECONDS );
return DefaultFreeExtensions::get_all();
}
return $specs;

View File

@ -0,0 +1,38 @@
<?php
namespace Automattic\WooCommerce\Admin\Features\RemoteFreeExtensions;
/**
* Specs data source poller class for remote free extensions.
*/
class RemoteFreeExtensionsDataSourcePoller extends \Automattic\WooCommerce\Admin\DataSourcePoller {
const ID = 'remote_free_extensions';
const DATA_SOURCES = array(
'https://woocommerce.com/wp-json/wccom/obw-free-extensions/2.0/extensions.json',
);
/**
* Class instance.
*
* @var Analytics instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self(
self::ID,
self::DATA_SOURCES,
array(
'spec_key' => 'key',
)
);
}
return self::$instance;
}
}

View File

@ -1,131 +0,0 @@
<?php
/**
* Handles polling and storage of specs
*/
namespace Automattic\WooCommerce\Admin\Features\WcPayPromotion;
defined( 'ABSPATH' ) || exit;
/**
* Specs data source poller class.
* This handles polling specs from JSON endpoints.
*/
class DataSourcePoller {
/**
* Name of data sources filter.
*/
const FILTER_NAME = 'woocommerce_admin_payment_method_promotions_data_sources';
/**
* Default data sources array.
*/
const DATA_SOURCES = array(
'https://woocommerce.com/wp-json/wccom/payment-gateway-suggestions/1.0/payment-method/promotions.json',
);
/**
* The logger instance.
*
* @var WC_Logger|null
*/
protected static $logger = null;
/**
* Get the logger instance.
*
* @return WC_Logger
*/
private static function get_logger() {
if ( is_null( self::$logger ) ) {
self::$logger = wc_get_logger();
}
return self::$logger;
}
/**
* Reads the data sources for specs and persists those specs.
*
* @return bool Whether any specs were read.
*/
public static function read_specs_from_data_sources() {
$specs = array();
$data_sources = apply_filters( self::FILTER_NAME, self::DATA_SOURCES );
// Note that this merges the specs from the data sources based on the
// product - last one wins.
foreach ( $data_sources as $url ) {
$specs_from_data_source = self::read_data_source( $url );
self::merge_specs( $specs_from_data_source, $specs, $url );
}
return $specs;
}
/**
* Read a single data source and return the read specs
*
* @param string $url The URL to read the specs from.
*
* @return array The specs that have been read from the data source.
*/
private static function read_data_source( $url ) {
$logger_context = array( 'source' => $url );
$logger = self::get_logger();
$response = wp_remote_get(
add_query_arg(
'_locale',
get_user_locale(),
$url
)
);
if ( is_wp_error( $response ) || ! isset( $response['body'] ) ) {
$logger->error(
'Error getting remote payment method data feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $response, true ), $logger_context );
return [];
}
$body = $response['body'];
$specs = json_decode( $body );
if ( null === $specs ) {
$logger->error(
'Empty response in remote payment method data feed',
$logger_context
);
return [];
}
if ( ! is_array( $specs ) ) {
$logger->error(
'Remote payment method data feed is not an array',
$logger_context
);
return [];
}
return $specs;
}
/**
* Merge the specs.
*
* @param Array $specs_to_merge_in The specs to merge in to $specs.
* @param Array $specs The list of specs being merged into.
*/
private static function merge_specs( $specs_to_merge_in, &$specs ) {
foreach ( $specs_to_merge_in as $spec ) {
$id = $spec->id;
$specs[ $id ] = $spec;
}
}
}

View File

@ -15,7 +15,6 @@ use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\EvaluateSugg
* WC Pay Promotion engine.
*/
class Init {
const SPECS_TRANSIENT_NAME = 'woocommerce_admin_payment_method_promotion_specs';
const EXPLAT_VARIATION_PREFIX = 'woocommerce_wc_pay_promotion_payment_methods_table_';
/**
@ -186,32 +185,17 @@ class Init {
* Delete the specs transient.
*/
public static function delete_specs_transient() {
delete_transient( self::SPECS_TRANSIENT_NAME );
WcPayPromotionDataSourcePoller::get_instance()->delete_specs_transient();
}
/**
* Get specs or fetch remotely if they don't exist.
*/
public static function get_specs() {
$specs = get_transient( self::SPECS_TRANSIENT_NAME );
// Fetch specs if they don't yet exist.
if ( false === $specs || ! is_array( $specs ) || 0 === count( $specs ) ) {
if ( 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) ) {
return array();
}
$specs = DataSourcePoller::read_specs_from_data_sources();
// Fall back to default specs if polling failed.
if ( ! $specs ) {
return array();
}
set_transient( self::SPECS_TRANSIENT_NAME, $specs, 7 * DAY_IN_SECONDS );
if ( 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) ) {
return array();
}
return $specs;
return WcPayPromotionDataSourcePoller::get_instance()->get_specs_from_data_sources();
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace Automattic\WooCommerce\Admin\Features\WcPayPromotion;
use Automattic\WooCommerce\Admin\DataSourcePoller;
/**
* Specs data source poller class for WooCommerce Payment Promotion.
*/
class WcPayPromotionDataSourcePoller extends DataSourcePoller {
const ID = 'payment_method_promotion';
/**
* Default data sources array.
*/
const DATA_SOURCES = array(
'https://woocommerce.com/wp-json/wccom/payment-gateway-suggestions/1.0/payment-method/promotions.json',
);
/**
* Class instance.
*
* @var Analytics instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self( self::ID, self::DATA_SOURCES );
}
return self::$instance;
}
}

View File

@ -12,120 +12,29 @@ defined( 'ABSPATH' ) || exit;
* This handles polling specs from JSON endpoints, and
* stores the specs in to the database as an option.
*/
class DataSourcePoller {
class DataSourcePoller extends \Automattic\WooCommerce\Admin\DataSourcePoller {
const ID = 'remote_inbox_notifications';
const DATA_SOURCES = array(
'https://woocommerce.com/wp-json/wccom/inbox-notifications/1.0/notifications.json',
);
/**
* Class instance.
*
* @var Analytics instance
*/
protected static $instance = null;
/**
* The logger instance.
*
* @var WC_Logger|null
* Get class instance.
*/
protected static $logger = null;
/**
* Get the logger instance.
*
* @return WC_Logger
*/
private static function get_logger() {
if ( is_null( self::$logger ) ) {
self::$logger = wc_get_logger();
}
return self::$logger;
}
/**
* Reads the data sources for specs and persists those specs.
*
* @return bool Whether any specs were read.
*/
public static function read_specs_from_data_sources() {
$specs = array();
$data_sources = apply_filters( 'woocommerce_admin_remote_inbox_data_sources', self::DATA_SOURCES );
// Note that this merges the specs from the data sources based on the
// slug - last one wins.
foreach ( $data_sources as $url ) {
$specs_from_data_source = self::read_data_source( $url, $specs );
self::merge_specs( $specs_from_data_source, $specs, $url );
}
// Persist the specs as an option.
update_option(
RemoteInboxNotificationsEngine::SPECS_OPTION_NAME,
$specs,
false
);
return 0 !== count( $specs );
}
/**
* Read a single data source and return the read specs
*
* @param string $url The URL to read the specs from.
*
* @return array The specs that have been read from the data source.
*/
private static function read_data_source( $url ) {
$logger_context = array( 'source' => $url );
$logger = self::get_logger();
$response = wp_remote_get( $url );
if ( is_wp_error( $response ) || ! isset( $response['body'] ) ) {
$logger->error(
'Error getting remote inbox notification data feed',
$logger_context
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self(
self::ID,
self::DATA_SOURCES
);
// phpcs:ignore
$logger->error( print_r( $response, true ), $logger_context );
return [];
}
$body = $response['body'];
$specs = json_decode( $body );
if ( null === $specs ) {
$logger->error(
'Empty response in remote inbox notification data feed',
$logger_context
);
return [];
}
if ( ! is_array( $specs ) ) {
$logger->error(
'Remote inbox notification data feed is not an array',
$logger_context
);
return [];
}
return $specs;
}
/**
* Merge the specs.
*
* @param Array $specs_to_merge_in The specs to merge in to $specs.
* @param Array $specs The list of specs being merged into.
* @param string $url The url of the feed being merged in (for error reporting).
*/
private static function merge_specs( $specs_to_merge_in, &$specs, $url ) {
foreach ( $specs_to_merge_in as $spec ) {
if ( ! self::validate_spec( $spec, $url ) ) {
continue;
}
$slug = $spec->slug;
$specs[ $slug ] = $spec;
}
return self::$instance;
}
/**
@ -136,7 +45,7 @@ class DataSourcePoller {
*
* @return bool The result of the validation.
*/
private static function validate_spec( $spec, $url ) {
protected function validate_spec( $spec, $url ) {
$logger = self::get_logger();
$logger_context = array( 'source' => $url );
@ -197,7 +106,7 @@ class DataSourcePoller {
if ( isset( $spec->actions ) && is_array( $spec->actions ) ) {
foreach ( $spec->actions as $action ) {
if ( ! self::validate_action( $action, $url ) ) {
if ( ! $this->validate_action( $action, $url ) ) {
$logger->error(
'Spec is invalid because an action is invalid in feed',
$logger_context
@ -253,7 +162,7 @@ class DataSourcePoller {
*
* @return bool The result of the validation.
*/
private static function validate_action( $action, $url ) {
private function validate_action( $action, $url ) {
$logger = self::get_logger();
$logger_context = array( 'source' => $url );

View File

@ -16,7 +16,6 @@ use \Automattic\WooCommerce\Admin\Features\Onboarding;
* specs that are able to be triggered.
*/
class RemoteInboxNotificationsEngine {
const SPECS_OPTION_NAME = 'wc_remote_inbox_notifications_specs';
const STORED_STATE_OPTION_NAME = 'wc_remote_inbox_notifications_stored_state';
const WCA_UPDATED_OPTION_NAME = 'wc_remote_inbox_notifications_wca_updated';
@ -90,14 +89,9 @@ class RemoteInboxNotificationsEngine {
* Go through the specs and run them.
*/
public static function run() {
$specs = get_option( self::SPECS_OPTION_NAME );
$specs = DataSourcePoller::get_instance()->get_specs_from_data_sources();
if ( false === $specs || 0 === count( $specs ) ) {
// We are running too early, need to poll data sources first.
if ( DataSourcePoller::read_specs_from_data_sources() ) {
self::run();
}
return;
}

View File

@ -6,7 +6,8 @@
*/
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\Init as PaymentGatewaySuggestions;
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\DataSourcePoller;
use Automattic\WooCommerce\Admin\DataSourcePoller;
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\PaymentGatewaySuggestionsDataSourcePoller;
/**
* class WC_Tests_PaymentGatewaySuggestions_DataSourcePoller
@ -81,7 +82,8 @@ class WC_Tests_PaymentGatewaySuggestions_DataSourcePoller extends WC_Unit_Test_C
* Test that a data source can be read.
*/
public function test_read_data_source() {
$data = DataSourcePoller::read_specs_from_data_sources();
$data_source_poller = PaymentGatewaySuggestionsDataSourcePoller::get_instance();
$data = $data_source_poller->get_specs_from_data_sources();
$this->assertArrayHasKey( 'mock-gateway1', $data );
$this->assertArrayHasKey( 'mock-gateway2', $data );
$this->assertArrayNotHasKey( 'mock-gateway3', $data );
@ -101,7 +103,8 @@ class WC_Tests_PaymentGatewaySuggestions_DataSourcePoller extends WC_Unit_Test_C
20
);
$data = DataSourcePoller::read_specs_from_data_sources();
$data_source_poller = PaymentGatewaySuggestionsDataSourcePoller::get_instance();
$data = $data_source_poller->get_specs_from_data_sources();
$this->assertEmpty( $data );
}
@ -120,7 +123,8 @@ class WC_Tests_PaymentGatewaySuggestions_DataSourcePoller extends WC_Unit_Test_C
20
);
$data = DataSourcePoller::read_specs_from_data_sources();
$data_source_poller = PaymentGatewaySuggestionsDataSourcePoller::get_instance();
$data = $data_source_poller->get_specs_from_data_sources();
$this->assertArrayHasKey( 'mock-gateway1', $data );
$this->assertArrayHasKey( 'mock-gateway2', $data );
$this->assertArrayHasKey( 'mock-gateway3', $data );
@ -130,9 +134,34 @@ class WC_Tests_PaymentGatewaySuggestions_DataSourcePoller extends WC_Unit_Test_C
* Test that invalid specs aren't merged.
*/
public function test_merge_invalid_specs() {
$data = DataSourcePoller::read_specs_from_data_sources();
$data_source_poller = PaymentGatewaySuggestionsDataSourcePoller::get_instance();
$data = $data_source_poller->get_specs_from_data_sources();
$this->assertCount( 2, $data );
$this->assertArrayNotHasKey( 'mock-gateway-invalid', $data );
}
/**
* Test that data source specs are persisted if successful.
*/
public function test_persist_data_source_specs() {
$data_source_poller = PaymentGatewaySuggestionsDataSourcePoller::get_instance();
$data = $data_source_poller->get_specs_from_data_sources();
$this->assertCount( 2, $data );
add_filter(
DataSourcePoller::FILTER_NAME,
function() {
return array(
'bad-data-source.json',
);
},
20
);
$data_source_poller = PaymentGatewaySuggestionsDataSourcePoller::get_instance();
$data = $data_source_poller->get_specs_from_data_sources();
$this->assertCount( 2, $data );
$data = get_transient( 'woocommerce_admin_' . PaymentGatewaySuggestionsDataSourcePoller::ID . '_specs' );
$this->assertCount( 2, $data );
}
}

View File

@ -5,9 +5,10 @@
* @package WooCommerce\Admin\Tests\PaymentGatewaySuggestions
*/
use Automattic\WooCommerce\Admin\DataSourcePoller;
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\Init as PaymentGatewaySuggestions;
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\DefaultPaymentGateways;
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\DataSourcePoller;
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\PaymentGatewaySuggestionsDataSourcePoller;
/**
* class WC_Tests_PaymentGatewaySuggestions_Init
@ -20,8 +21,9 @@ class WC_Tests_PaymentGatewaySuggestions_Init extends WC_Unit_Test_Case {
public function setUp() {
parent::setUp();
delete_option( 'woocommerce_show_marketplace_suggestions' );
add_filter(
'transient_' . PaymentGatewaySuggestions::SPECS_TRANSIENT_NAME,
'transient_woocommerce_admin_' . PaymentGatewaySuggestionsDataSourcePoller::ID . '_specs',
function( $value ) {
if ( $value ) {
return $value;
@ -42,7 +44,7 @@ class WC_Tests_PaymentGatewaySuggestions_Init extends WC_Unit_Test_Case {
public function tearDown() {
parent::tearDown();
PaymentGatewaySuggestions::delete_specs_transient();
remove_all_filters( 'transient_' . PaymentGatewaySuggestions::SPECS_TRANSIENT_NAME );
remove_all_filters( 'transient_woocommerce_admin_' . PaymentGatewaySuggestionsDataSourcePoller::ID . '_specs' );
}
/**
@ -65,7 +67,7 @@ class WC_Tests_PaymentGatewaySuggestions_Init extends WC_Unit_Test_Case {
* Test that default gateways are provided when remote sources don't exist.
*/
public function test_get_default_specs() {
remove_all_filters( 'transient_' . PaymentGatewaySuggestions::SPECS_TRANSIENT_NAME );
remove_all_filters( 'transient_woocommerce_admin_' . PaymentGatewaySuggestionsDataSourcePoller::ID . '_specs' );
add_filter(
DataSourcePoller::FILTER_NAME,
function() {
@ -83,7 +85,7 @@ class WC_Tests_PaymentGatewaySuggestions_Init extends WC_Unit_Test_Case {
*/
public function test_specs_transient() {
set_transient(
PaymentGatewaySuggestions::SPECS_TRANSIENT_NAME,
'woocommerce_admin_' . PaymentGatewaySuggestionsDataSourcePoller::ID . '_specs',
array(
array(
'id' => 'mock-gateway1',
@ -103,7 +105,7 @@ class WC_Tests_PaymentGatewaySuggestions_Init extends WC_Unit_Test_Case {
public function test_non_matching_suggestions() {
update_option( 'woocommerce_default_country', 'US' );
set_transient(
PaymentGatewaySuggestions::SPECS_TRANSIENT_NAME,
'woocommerce_admin_' . PaymentGatewaySuggestionsDataSourcePoller::ID . '_specs',
$this->get_mock_specs()
);
$suggestions = PaymentGatewaySuggestions::get_suggestions();
@ -116,7 +118,7 @@ class WC_Tests_PaymentGatewaySuggestions_Init extends WC_Unit_Test_Case {
public function test_matching_suggestions() {
update_option( 'woocommerce_default_country', 'ZA' );
set_transient(
PaymentGatewaySuggestions::SPECS_TRANSIENT_NAME,
'woocommerce_admin_' . PaymentGatewaySuggestionsDataSourcePoller::ID . '_specs',
$this->get_mock_specs()
);
$suggestions = PaymentGatewaySuggestions::get_suggestions();
@ -128,7 +130,7 @@ class WC_Tests_PaymentGatewaySuggestions_Init extends WC_Unit_Test_Case {
*/
public function test_delete_transient_on_locale_change() {
set_transient(
PaymentGatewaySuggestions::SPECS_TRANSIENT_NAME,
'woocommerce_admin_' . PaymentGatewaySuggestionsDataSourcePoller::ID . '_specs',
array(
array(
'id' => 'mock-gateway',