2021-05-05 23:49:47 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Handles running payment method specs
|
|
|
|
*/
|
|
|
|
|
2022-02-16 12:50:33 +00:00
|
|
|
namespace Automattic\WooCommerce\Internal\Admin\RemoteFreeExtensions;
|
2021-05-05 23:49:47 +00:00
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
2022-02-16 12:50:33 +00:00
|
|
|
use Automattic\WooCommerce\Internal\Admin\RemoteFreeExtensions\DefaultFreeExtensions;
|
2021-05-05 23:49:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remote Payment Methods engine.
|
|
|
|
* This goes through the specs and gets eligible payment methods.
|
|
|
|
*/
|
|
|
|
class Init {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
add_action( 'change_locale', array( __CLASS__, 'delete_specs_transient' ) );
|
2021-08-04 13:23:47 +00:00
|
|
|
add_action( 'woocommerce_admin_updated', array( __CLASS__, 'delete_specs_transient' ) );
|
2021-05-05 23:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Go through the specs and run them.
|
2021-08-19 14:39:02 +00:00
|
|
|
*
|
|
|
|
* @param array $allowed_bundles Optional array of allowed bundles to be returned.
|
|
|
|
* @return array
|
2021-05-05 23:49:47 +00:00
|
|
|
*/
|
2021-08-19 14:39:02 +00:00
|
|
|
public static function get_extensions( $allowed_bundles = array() ) {
|
2021-08-04 15:57:09 +00:00
|
|
|
$bundles = array();
|
2021-05-05 23:49:47 +00:00
|
|
|
$specs = self::get_specs();
|
|
|
|
|
|
|
|
foreach ( $specs as $spec ) {
|
2021-08-04 15:57:09 +00:00
|
|
|
$spec = (object) $spec;
|
|
|
|
$bundle = (array) $spec;
|
|
|
|
$bundle['plugins'] = array();
|
|
|
|
|
2021-08-19 14:39:02 +00:00
|
|
|
if ( ! empty( $allowed_bundles ) && ! in_array( $spec->key, $allowed_bundles, true ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-08-04 15:57:09 +00:00
|
|
|
foreach ( $spec->plugins as $plugin ) {
|
|
|
|
$extension = EvaluateExtension::evaluate( (object) $plugin );
|
|
|
|
|
|
|
|
if ( ! property_exists( $extension, 'is_visible' ) || $extension->is_visible ) {
|
|
|
|
$bundle['plugins'][] = $extension;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$bundles[] = $bundle;
|
2021-05-05 23:49:47 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 15:57:09 +00:00
|
|
|
return $bundles;
|
2021-05-05 23:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the specs transient.
|
|
|
|
*/
|
|
|
|
public static function delete_specs_transient() {
|
2021-10-20 18:53:43 +00:00
|
|
|
RemoteFreeExtensionsDataSourcePoller::get_instance()->delete_specs_transient();
|
2021-05-05 23:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get specs or fetch remotely if they don't exist.
|
|
|
|
*/
|
|
|
|
public static function get_specs() {
|
2021-10-20 18:53:43 +00:00
|
|
|
if ( 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) ) {
|
|
|
|
return DefaultFreeExtensions::get_all();
|
|
|
|
}
|
|
|
|
$specs = RemoteFreeExtensionsDataSourcePoller::get_instance()->get_specs_from_data_sources();
|
2021-05-05 23:49:47 +00:00
|
|
|
|
|
|
|
// Fetch specs if they don't yet exist.
|
|
|
|
if ( false === $specs || ! is_array( $specs ) || 0 === count( $specs ) ) {
|
2021-10-20 18:53:43 +00:00
|
|
|
return DefaultFreeExtensions::get_all();
|
2021-05-05 23:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $specs;
|
|
|
|
}
|
|
|
|
}
|