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
EvaluateExtension.php
99 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Evaluates the spec and returns a status. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Internal\Admin\RemoteFreeExtensions; |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | |
| 10 | use Automattic\WooCommerce\Admin\PluginsHelper; |
| 11 | use Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\EvaluateOverrides; |
| 12 | use Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\RuleEvaluator; |
| 13 | |
| 14 | /** |
| 15 | * Evaluates the extension and returns it. |
| 16 | */ |
| 17 | class EvaluateExtension { |
| 18 | /** |
| 19 | * Evaluates the extension and returns it. |
| 20 | * |
| 21 | * @param object $extension The extension to evaluate. |
| 22 | * @return object The evaluated extension. |
| 23 | */ |
| 24 | private static function evaluate( $extension ) { |
| 25 | global $wp_version; |
| 26 | $rule_evaluator = new RuleEvaluator(); |
| 27 | |
| 28 | if ( isset( $extension->is_visible ) ) { |
| 29 | $is_visible = $rule_evaluator->evaluate( $extension->is_visible ); |
| 30 | $extension->is_visible = $is_visible; |
| 31 | } else { |
| 32 | $extension->is_visible = true; |
| 33 | } |
| 34 | |
| 35 | // Run PHP and WP version chcecks. |
| 36 | if ( true === $extension->is_visible ) { |
| 37 | if ( isset( $extension->min_php_version ) && ! version_compare( PHP_VERSION, $extension->min_php_version, '>=' ) ) { |
| 38 | $extension->is_visible = false; |
| 39 | } |
| 40 | |
| 41 | if ( isset( $extension->min_wp_version ) && ! version_compare( $wp_version, $extension->min_wp_version, '>=' ) ) { |
| 42 | $extension->is_visible = false; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | $installed_plugins = PluginsHelper::get_installed_plugin_slugs(); |
| 47 | $activated_plugins = PluginsHelper::get_active_plugin_slugs(); |
| 48 | $extension->is_installed = in_array( explode( ':', $extension->key )[0], $installed_plugins, true ); |
| 49 | $extension->is_activated = in_array( explode( ':', $extension->key )[0], $activated_plugins, true ); |
| 50 | |
| 51 | return $extension; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Evaluates the specs and returns the bundles with visible extensions. |
| 56 | * |
| 57 | * @param array $specs extensions spec array. |
| 58 | * @param array $allowed_bundles Optional array of allowed bundles to be returned. |
| 59 | * @return array The bundles and errors. |
| 60 | */ |
| 61 | public static function evaluate_bundles( $specs, $allowed_bundles = array() ) { |
| 62 | $bundles = array(); |
| 63 | $evaluate_order = new EvaluateOverrides(); |
| 64 | $context = array(); |
| 65 | |
| 66 | foreach ( $specs as $spec ) { |
| 67 | $spec = (object) $spec; |
| 68 | $bundle = (array) $spec; |
| 69 | $bundle['plugins'] = array(); |
| 70 | |
| 71 | if ( ! empty( $allowed_bundles ) && ! in_array( $spec->key, $allowed_bundles, true ) ) { |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | $errors = array(); |
| 76 | foreach ( $spec->plugins as $plugin ) { |
| 77 | try { |
| 78 | $extension = self::evaluate( (object) $plugin ); |
| 79 | if ( ! property_exists( $extension, 'is_visible' ) || $extension->is_visible ) { |
| 80 | $bundle['plugins'][] = $extension; |
| 81 | } |
| 82 | } catch ( \Throwable $e ) { |
| 83 | $errors[] = $e; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | $context['plugins'] = $bundle['plugins']; |
| 88 | $bundle['plugins'] = $evaluate_order->evaluate( $bundle['plugins'], $context ); |
| 89 | |
| 90 | $bundles[] = $bundle; |
| 91 | } |
| 92 | |
| 93 | return array( |
| 94 | 'bundles' => $bundles, |
| 95 | 'errors' => $errors, |
| 96 | ); |
| 97 | } |
| 98 | } |
| 99 |