DefaultPaymentGateways.php
1 year ago
EvaluateSuggestion.php
1 year ago
Init.php
1 year ago
PaymentGatewaySuggestionsDataSourcePoller.php
1 year ago
PaymentGatewaysController.php
4 years ago
EvaluateSuggestion.php
128 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Evaluates the spec and returns a status. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions; |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | |
| 10 | use Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\RuleEvaluator; |
| 11 | |
| 12 | /** |
| 13 | * Evaluates the spec and returns the evaluated suggestion. |
| 14 | */ |
| 15 | class EvaluateSuggestion { |
| 16 | /** |
| 17 | * Stores memoized results of evaluate_specs. |
| 18 | * |
| 19 | * @var array |
| 20 | */ |
| 21 | protected static $memo = array(); |
| 22 | |
| 23 | /** |
| 24 | * Evaluates the spec and returns the suggestion. |
| 25 | * |
| 26 | * @param object|array $spec The suggestion to evaluate. |
| 27 | * @param array $logger_args Optional. Arguments for the rule evaluator logger. |
| 28 | * |
| 29 | * @return object The evaluated suggestion. |
| 30 | */ |
| 31 | public static function evaluate( $spec, $logger_args = array() ) { |
| 32 | $rule_evaluator = new RuleEvaluator(); |
| 33 | $suggestion = is_array( $spec ) ? (object) $spec : clone $spec; |
| 34 | |
| 35 | if ( isset( $suggestion->is_visible ) ) { |
| 36 | // Determine the suggestion's logger slug. |
| 37 | $logger_slug = ! empty( $suggestion->id ) ? $suggestion->id : ''; |
| 38 | // If the suggestion has no ID, use the title to generate a slug. |
| 39 | if ( empty( $logger_slug ) ) { |
| 40 | $logger_slug = ! empty( $suggestion->title ) ? sanitize_title_with_dashes( trim( $suggestion->title ) ) : 'anonymous-suggestion'; |
| 41 | } |
| 42 | |
| 43 | // Evaluate the visibility of the suggestion. |
| 44 | $is_visible = $rule_evaluator->evaluate( |
| 45 | $suggestion->is_visible, |
| 46 | null, |
| 47 | array( |
| 48 | 'slug' => $logger_slug, |
| 49 | 'source' => $logger_args['source'] ?? 'wc-payment-gateway-suggestions', |
| 50 | ) |
| 51 | ); |
| 52 | |
| 53 | $suggestion->is_visible = $is_visible; |
| 54 | } |
| 55 | |
| 56 | return $suggestion; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Evaluates the specs and returns the visible suggestions. |
| 61 | * |
| 62 | * @param array $specs payment suggestion spec array. |
| 63 | * @param array $logger_args Optional. Arguments for the rule evaluator logger. |
| 64 | * |
| 65 | * @return array The visible suggestions and errors. |
| 66 | */ |
| 67 | public static function evaluate_specs( $specs, $logger_args = array() ) { |
| 68 | $specs_key = self::get_memo_key( $specs ); |
| 69 | |
| 70 | if ( isset( self::$memo[ $specs_key ] ) ) { |
| 71 | return self::$memo[ $specs_key ]; |
| 72 | } |
| 73 | |
| 74 | $suggestions = array(); |
| 75 | $errors = array(); |
| 76 | |
| 77 | foreach ( $specs as $spec ) { |
| 78 | try { |
| 79 | $suggestion = self::evaluate( $spec, $logger_args ); |
| 80 | if ( ! property_exists( $suggestion, 'is_visible' ) || $suggestion->is_visible ) { |
| 81 | $suggestions[] = $suggestion; |
| 82 | } |
| 83 | } catch ( \Throwable $e ) { |
| 84 | $errors[] = $e; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | $result = array( |
| 89 | 'suggestions' => $suggestions, |
| 90 | 'errors' => $errors, |
| 91 | ); |
| 92 | |
| 93 | // Memoize results, with a fail safe to prevent unbounded memory growth. |
| 94 | // This limit is unlikely to be reached under normal circumstances. |
| 95 | if ( count( self::$memo ) > 50 ) { |
| 96 | self::reset_memo(); |
| 97 | } |
| 98 | self::$memo[ $specs_key ] = $result; |
| 99 | |
| 100 | return $result; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Resets the memoized results. Useful for testing. |
| 105 | */ |
| 106 | public static function reset_memo() { |
| 107 | self::$memo = array(); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns a memoization key for the given specs. |
| 112 | * |
| 113 | * @param array $specs The specs to generate a key for. |
| 114 | * |
| 115 | * @return string The memoization key. |
| 116 | */ |
| 117 | private static function get_memo_key( $specs ) { |
| 118 | $data = wp_json_encode( $specs ); |
| 119 | |
| 120 | if ( function_exists( 'hash' ) && in_array( 'xxh3', hash_algos(), true ) ) { |
| 121 | // Use xxHash (xxh3) if available. |
| 122 | return hash( 'xxh3', $data ); |
| 123 | } |
| 124 | // Fall back to CRC32. |
| 125 | return (string) crc32( $data ); |
| 126 | } |
| 127 | } |
| 128 |