Use Rule Processing for Free Extensions List (https://github.com/woocommerce/woocommerce-admin/pull/6933)
* Copy payments gateways * remove field localization * remove is_configured
This commit is contained in:
parent
d98fe35faa
commit
34d1f8c36c
|
@ -13,6 +13,7 @@
|
|||
"navigation": false,
|
||||
"onboarding": true,
|
||||
"remote-inbox-notifications": true,
|
||||
"remote-extensions-list": false,
|
||||
"remote-payment-methods": false,
|
||||
"settings": false,
|
||||
"shipping-label-banner": true,
|
||||
|
|
|
@ -13,7 +13,8 @@
|
|||
"navigation": true,
|
||||
"onboarding": true,
|
||||
"remote-inbox-notifications": true,
|
||||
"remote-payment-methods": false,
|
||||
"remote-extensions-list": true,
|
||||
"remote-payment-methods": true,
|
||||
"settings": false,
|
||||
"shipping-label-banner": true,
|
||||
"store-alerts": true,
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
"navigation": true,
|
||||
"onboarding": true,
|
||||
"remote-inbox-notifications": true,
|
||||
"remote-extensions-list": false,
|
||||
"remote-payment-methods": false,
|
||||
"settings": false,
|
||||
"shipping-label-banner": true,
|
||||
|
|
|
@ -15491,6 +15491,15 @@
|
|||
"integrity": "sha512-Un7MIEDdUC5gNpcGDV97op1Ywk748MpHcFTHoYs6qnj1Z3j7I53VG3nwZhKzoBZmbdRNnb6WRdFlwl7tSDuZGw==",
|
||||
"optional": true
|
||||
},
|
||||
"bindings": {
|
||||
"version": "1.5.0",
|
||||
"resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz",
|
||||
"integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==",
|
||||
"optional": true,
|
||||
"requires": {
|
||||
"file-uri-to-path": "1.0.0"
|
||||
}
|
||||
},
|
||||
"bl": {
|
||||
"version": "4.1.0",
|
||||
"resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz",
|
||||
|
@ -21336,6 +21345,12 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"file-uri-to-path": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz",
|
||||
"integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==",
|
||||
"optional": true
|
||||
},
|
||||
"filelist": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/filelist/-/filelist-1.0.2.tgz",
|
||||
|
@ -27047,6 +27062,7 @@
|
|||
"integrity": "sha512-oWb1Z6mkHIskLzEJ/XWX0srkpkTQ7vaopMQkyaEIoq0fmtFVxOthb8cCxeT+p3ynTdkk/RZwbgG4brR5BeWECw==",
|
||||
"optional": true,
|
||||
"requires": {
|
||||
"bindings": "^1.5.0",
|
||||
"nan": "^2.12.1"
|
||||
}
|
||||
}
|
||||
|
@ -39374,6 +39390,7 @@
|
|||
"dev": true,
|
||||
"optional": true,
|
||||
"requires": {
|
||||
"bindings": "^1.5.0",
|
||||
"nan": "^2.12.1"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -94,6 +94,7 @@ class Init {
|
|||
'Automattic\WooCommerce\Admin\API\Taxes',
|
||||
'Automattic\WooCommerce\Admin\API\Themes',
|
||||
'Automattic\WooCommerce\Admin\API\Plugins',
|
||||
'Automattic\WooCommerce\Admin\API\OnboardingFreeExtensions',
|
||||
'Automattic\WooCommerce\Admin\API\OnboardingPayments',
|
||||
'Automattic\WooCommerce\Admin\API\OnboardingProfile',
|
||||
'Automattic\WooCommerce\Admin\API\OnboardingTasks',
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
/**
|
||||
* REST API Onboarding Free Extensions Controller
|
||||
*
|
||||
* Handles requests to /onboarding/free-extensions
|
||||
*/
|
||||
|
||||
namespace Automattic\WooCommerce\Admin\API;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
use Automattic\WooCommerce\Admin\Features\RemoteFreeExtensions\Init as RemoteFreeExtensions;
|
||||
|
||||
/**
|
||||
* Onboarding Payments Controller.
|
||||
*
|
||||
* @extends WC_REST_Data_Controller
|
||||
*/
|
||||
class OnboardingFreeExtensions extends \WC_REST_Data_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc-admin';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'onboarding/free-extensions';
|
||||
|
||||
/**
|
||||
* Register routes.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
'/' . $this->rest_base,
|
||||
array(
|
||||
array(
|
||||
'methods' => \WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_available_extensions' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a given request has permission to read onboarding profile data.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
|
||||
return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return available payment methods.
|
||||
*
|
||||
* @param \WP_REST_Request $request Request data.
|
||||
*
|
||||
* @return \WP_Error|\WP_REST_Response
|
||||
*/
|
||||
public function get_available_extensions( $request ) {
|
||||
return RemoteFreeExtensions::get_extensions();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,148 @@
|
|||
<?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/free-extensions/1.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( $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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
/**
|
||||
* Evaluates the spec and returns a status.
|
||||
*/
|
||||
|
||||
namespace Automattic\WooCommerce\Admin\Features\RemoteFreeExtensions;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\RuleEvaluator;
|
||||
|
||||
/**
|
||||
* Evaluates the spec and returns the evaluated method.
|
||||
*/
|
||||
class EvaluateExtension {
|
||||
/**
|
||||
* Evaluates the spec and returns the method.
|
||||
*
|
||||
* @param array $spec The method to evaluate.
|
||||
* @return array The evaluated method.
|
||||
*/
|
||||
public static function evaluate( $spec ) {
|
||||
$rule_evaluator = new RuleEvaluator();
|
||||
$method = $spec;
|
||||
|
||||
if ( isset( $spec->is_visible ) ) {
|
||||
$is_visible = $rule_evaluator->evaluate( $spec->is_visible );
|
||||
$method->is_visible = $is_visible;
|
||||
// Return early if visibility does not pass.
|
||||
if ( ! $is_visible ) {
|
||||
return $method;
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $spec->is_configured ) ) {
|
||||
$is_configured = $rule_evaluator->evaluate( $method->is_configured );
|
||||
$method->is_configured = $is_configured;
|
||||
}
|
||||
|
||||
return $method;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
/**
|
||||
* Handles running payment method specs
|
||||
*/
|
||||
|
||||
namespace Automattic\WooCommerce\Admin\Features\RemoteFreeExtensions;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\SpecRunner;
|
||||
|
||||
/**
|
||||
* Remote Payment Methods engine.
|
||||
* This goes through the specs and gets eligible payment methods.
|
||||
*/
|
||||
class Init {
|
||||
const SPECS_TRANSIENT_NAME = 'woocommerce_admin_remote_free_extensions_specs';
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
add_action( 'change_locale', array( __CLASS__, 'delete_specs_transient' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Go through the specs and run them.
|
||||
*/
|
||||
public static function get_extensions() {
|
||||
$methods = array();
|
||||
$specs = self::get_specs();
|
||||
|
||||
foreach ( $specs as $spec ) {
|
||||
$method = EvaluateExtension::evaluate( $spec );
|
||||
$methods[] = $method;
|
||||
}
|
||||
|
||||
return $methods;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the specs transient.
|
||||
*/
|
||||
public static function delete_specs_transient() {
|
||||
delete_transient( self::SPECS_TRANSIENT_NAME );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 ) ) {
|
||||
// We are running too early, need to poll data sources first.
|
||||
$specs = DataSourcePoller::read_specs_from_data_sources();
|
||||
set_transient( self::SPECS_TRANSIENT_NAME, $specs, 7 * DAY_IN_SECONDS );
|
||||
}
|
||||
|
||||
return $specs;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue