2021-05-05 23:49:47 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Handles running payment method specs
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Automattic\WooCommerce\Admin\Features\RemoteFreeExtensions;
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\SpecRunner;
|
2021-08-04 15:57:09 +00:00
|
|
|
use Automattic\WooCommerce\Admin\Features\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 {
|
|
|
|
const SPECS_TRANSIENT_NAME = 'woocommerce_admin_remote_free_extensions_specs';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
public static function get_extensions() {
|
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();
|
|
|
|
|
|
|
|
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() {
|
|
|
|
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 ) ) {
|
2021-08-04 15:57:09 +00:00
|
|
|
if ( 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) ) {
|
|
|
|
return DefaultFreeExtensions::get_all();
|
|
|
|
}
|
|
|
|
|
2021-05-05 23:49:47 +00:00
|
|
|
$specs = DataSourcePoller::read_specs_from_data_sources();
|
2021-08-04 15:57:09 +00:00
|
|
|
|
|
|
|
// Fall back to default specs if polling failed.
|
|
|
|
if ( ! $specs || empty( $specs ) ) {
|
|
|
|
return DefaultFreeExtensions::get_all();
|
|
|
|
}
|
|
|
|
|
2021-05-05 23:49:47 +00:00
|
|
|
set_transient( self::SPECS_TRANSIENT_NAME, $specs, 7 * DAY_IN_SECONDS );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $specs;
|
|
|
|
}
|
|
|
|
}
|