DefaultFreeExtensions.php
4 weeks ago
EvaluateExtension.php
1 year ago
Init.php
2 years ago
ProcessCoreProfilerPluginInstallOptions.php
1 year ago
RemoteFreeExtensionsDataSourcePoller.php
1 year ago
Init.php
92 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handles running payment method specs |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Internal\Admin\RemoteFreeExtensions; |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | |
| 10 | use Automattic\WooCommerce\Internal\Admin\RemoteFreeExtensions\DefaultFreeExtensions; |
| 11 | use Automattic\WooCommerce\Admin\RemoteSpecs\RemoteSpecsEngine; |
| 12 | |
| 13 | /** |
| 14 | * Remote Payment Methods engine. |
| 15 | * This goes through the specs and gets eligible payment methods. |
| 16 | */ |
| 17 | class Init extends RemoteSpecsEngine { |
| 18 | |
| 19 | /** |
| 20 | * Constructor. |
| 21 | */ |
| 22 | public function __construct() { |
| 23 | add_action( 'woocommerce_updated', array( __CLASS__, 'delete_specs_transient' ) ); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Go through the specs and run them. |
| 28 | * |
| 29 | * @param array $allowed_bundles Optional array of allowed bundles to be returned. |
| 30 | * @return array |
| 31 | */ |
| 32 | public static function get_extensions( $allowed_bundles = array() ) { |
| 33 | $locale = get_user_locale(); |
| 34 | |
| 35 | $specs = self::get_specs(); |
| 36 | $results = EvaluateExtension::evaluate_bundles( $specs, $allowed_bundles ); |
| 37 | $specs_to_return = $results['bundles']; |
| 38 | $specs_to_save = null; |
| 39 | |
| 40 | $plugins = array_filter( |
| 41 | $results['bundles'], |
| 42 | function( $bundle ) { |
| 43 | return count( $bundle['plugins'] ) > 0; |
| 44 | } |
| 45 | ); |
| 46 | |
| 47 | if ( empty( $plugins ) ) { |
| 48 | // When no plugins are visible, replace it with defaults and save for 3 hours. |
| 49 | $specs_to_save = DefaultFreeExtensions::get_all(); |
| 50 | $specs_to_return = EvaluateExtension::evaluate_bundles( $specs_to_save, $allowed_bundles )['bundles']; |
| 51 | } elseif ( count( $results['errors'] ) > 0 ) { |
| 52 | // When suggestions is not empty but has errors, save it for 3 hours. |
| 53 | $specs_to_save = $specs; |
| 54 | } |
| 55 | |
| 56 | // When plugins is not empty but has errors, save it for 3 hours. |
| 57 | if ( count( $results['errors'] ) > 0 ) { |
| 58 | self::log_errors( $results['errors'] ); |
| 59 | } |
| 60 | |
| 61 | if ( $specs_to_save ) { |
| 62 | RemoteFreeExtensionsDataSourcePoller::get_instance()->set_specs_transient( array( $locale => $specs_to_save ), 3 * HOUR_IN_SECONDS ); |
| 63 | } |
| 64 | |
| 65 | return $specs_to_return; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Delete the specs transient. |
| 70 | */ |
| 71 | public static function delete_specs_transient() { |
| 72 | RemoteFreeExtensionsDataSourcePoller::get_instance()->delete_specs_transient(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get specs or fetch remotely if they don't exist. |
| 77 | */ |
| 78 | public static function get_specs() { |
| 79 | if ( 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) ) { |
| 80 | return DefaultFreeExtensions::get_all(); |
| 81 | } |
| 82 | $specs = RemoteFreeExtensionsDataSourcePoller::get_instance()->get_specs_from_data_sources(); |
| 83 | |
| 84 | // Fetch specs if they don't yet exist. |
| 85 | if ( false === $specs || ! is_array( $specs ) || 0 === count( $specs ) ) { |
| 86 | return DefaultFreeExtensions::get_all(); |
| 87 | } |
| 88 | |
| 89 | return $specs; |
| 90 | } |
| 91 | } |
| 92 |