Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
91.43% |
32 / 35 |
|
87.50% |
7 / 8 |
CRAP | |
0.00% |
0 / 1 |
| Promo_Registry | |
91.43% |
32 / 35 |
|
87.50% |
7 / 8 |
19.23 | |
0.00% |
0 / 1 |
| register | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
9 | |||
| remember_available | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| get_all_registered | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| is_registered | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_instance | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| required_fields | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| is_linkable | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| refuse | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Promos: Promo_Registry class. |
| 4 | * |
| 5 | * @package automattic/jetpack-premium-analytics |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\PremiumAnalytics; |
| 9 | |
| 10 | /** |
| 11 | * Stores Promo instances keyed by slug. |
| 12 | * |
| 13 | * @since $$next-version$$ |
| 14 | */ |
| 15 | final class Promo_Registry { |
| 16 | |
| 17 | /** |
| 18 | * Placements a promo may render in. |
| 19 | * |
| 20 | * @since $$next-version$$ |
| 21 | * @var string[] |
| 22 | */ |
| 23 | const PLACEMENTS = array( 'options-menu', 'settings' ); |
| 24 | |
| 25 | /** |
| 26 | * Call-to-action variants a promo may use. |
| 27 | * |
| 28 | * @since $$next-version$$ |
| 29 | * @var string[] |
| 30 | */ |
| 31 | const CTA_VARIANTS = array( 'primary', 'secondary' ); |
| 32 | |
| 33 | /** |
| 34 | * Registered promos, as `$slug => $promo` pairs. |
| 35 | * |
| 36 | * @var array<string, Promo> |
| 37 | */ |
| 38 | private $registered_promos = array(); |
| 39 | |
| 40 | /** |
| 41 | * The gated promos for this request, or null before anything has asked for them. |
| 42 | * |
| 43 | * @var Promo[]|null |
| 44 | */ |
| 45 | private $available_promos = null; |
| 46 | |
| 47 | /** |
| 48 | * Container for the main instance of the class. |
| 49 | * |
| 50 | * @var Promo_Registry|null |
| 51 | */ |
| 52 | private static $instance = null; |
| 53 | |
| 54 | /** |
| 55 | * Registers a promo. |
| 56 | * |
| 57 | * @since $$next-version$$ |
| 58 | * |
| 59 | * @param string $slug Promo identifier, namespaced like `acme/offer`. |
| 60 | * @param array $args Optional. Promo arguments. |
| 61 | * @return Promo|false The registered promo on success, or false on failure. |
| 62 | */ |
| 63 | public function register( $slug, $args = array() ) { |
| 64 | if ( $this->is_registered( $slug ) ) { |
| 65 | /* translators: %s: Promo slug. */ |
| 66 | return $this->refuse( sprintf( __( 'Promo "%s" is already registered.', 'jetpack-premium-analytics-pkg' ), $slug ) ); |
| 67 | } |
| 68 | |
| 69 | $promo = new Promo( $slug, is_array( $args ) ? $args : array() ); |
| 70 | $required = self::required_fields()[ $promo->placement ] ?? null; |
| 71 | |
| 72 | if ( null === $required ) { |
| 73 | /* translators: %s: Comma-separated list of placements. */ |
| 74 | return $this->refuse( sprintf( __( 'A promo placement must be one of: %s.', 'jetpack-premium-analytics-pkg' ), implode( ', ', self::PLACEMENTS ) ) ); |
| 75 | } |
| 76 | |
| 77 | foreach ( $required as $field => $message ) { |
| 78 | if ( '' === $promo->$field ) { |
| 79 | return $this->refuse( $message ); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // The client links through `safeHttpUrl`, which allows http(s) only. A menu promo it rejects |
| 84 | // renders as nothing while still taking the one item the menu offers, hiding any other. |
| 85 | if ( '' !== $promo->cta_label && ! $this->is_linkable( $promo->cta_url ) ) { |
| 86 | return $this->refuse( __( 'A promo cta_url must be an http or https URL.', 'jetpack-premium-analytics-pkg' ) ); |
| 87 | } |
| 88 | |
| 89 | if ( ! in_array( $promo->cta_variant, self::CTA_VARIANTS, true ) ) { |
| 90 | /* translators: %s: Comma-separated list of call-to-action variants. */ |
| 91 | return $this->refuse( sprintf( __( 'A promo cta_variant must be one of: %s.', 'jetpack-premium-analytics-pkg' ), implode( ', ', self::CTA_VARIANTS ) ) ); |
| 92 | } |
| 93 | |
| 94 | $this->registered_promos[ $slug ] = $promo; |
| 95 | |
| 96 | return $promo; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Runs the gates once and answers from that result for the rest of the request. A promo's |
| 101 | * `is_available` rule may be expensive, and both the script data and the menu ask. |
| 102 | * |
| 103 | * @internal |
| 104 | * |
| 105 | * @since $$next-version$$ |
| 106 | * |
| 107 | * @param callable $compute Applies the gates to the registered promos. |
| 108 | * @return Promo[] The gated promos. |
| 109 | */ |
| 110 | public function remember_available( callable $compute ) { |
| 111 | if ( null === $this->available_promos ) { |
| 112 | $this->available_promos = $compute(); |
| 113 | } |
| 114 | |
| 115 | return $this->available_promos; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Retrieves all registered promos. |
| 120 | * |
| 121 | * @since $$next-version$$ |
| 122 | * |
| 123 | * @return Promo[] Map of `$slug => $promo` pairs. |
| 124 | */ |
| 125 | public function get_all_registered() { |
| 126 | return $this->registered_promos; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Checks if a promo is registered. |
| 131 | * |
| 132 | * @since $$next-version$$ |
| 133 | * |
| 134 | * @param string $slug Promo identifier. |
| 135 | * @return bool True if registered. |
| 136 | */ |
| 137 | public function is_registered( $slug ) { |
| 138 | return isset( $this->registered_promos[ $slug ] ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Utility method to retrieve the main instance of the class. |
| 143 | * |
| 144 | * @since $$next-version$$ |
| 145 | * |
| 146 | * @return Promo_Registry The main instance. |
| 147 | */ |
| 148 | public static function get_instance() { |
| 149 | if ( null === self::$instance ) { |
| 150 | self::$instance = new self(); |
| 151 | } |
| 152 | |
| 153 | return self::$instance; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * The copy each placement cannot render without, with the reason. Keyed by `PLACEMENTS`. |
| 158 | * |
| 159 | * @return array<string, array<string, string>> Required fields per placement. |
| 160 | */ |
| 161 | private static function required_fields() { |
| 162 | return array( |
| 163 | 'options-menu' => array( |
| 164 | 'cta_label' => __( 'An options-menu promo needs a cta_label. The label is the menu item.', 'jetpack-premium-analytics-pkg' ), |
| 165 | ), |
| 166 | 'settings' => array( |
| 167 | 'title' => __( 'A promo card needs a title. The card is labelled by it.', 'jetpack-premium-analytics-pkg' ), |
| 168 | ), |
| 169 | ); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Whether a CTA URL is one the dashboard will link to. |
| 174 | * |
| 175 | * @param string $url The CTA URL. |
| 176 | * @return bool |
| 177 | */ |
| 178 | private function is_linkable( $url ) { |
| 179 | $scheme = wp_parse_url( $url, PHP_URL_SCHEME ); |
| 180 | |
| 181 | return is_string( $scheme ) && in_array( strtolower( $scheme ), array( 'http', 'https' ), true ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Reports a refused registration. |
| 186 | * |
| 187 | * @param string $message Reason, unescaped. |
| 188 | * @return false |
| 189 | */ |
| 190 | private function refuse( $message ) { |
| 191 | // One line: tools/replace-next-version-tag.sh only rewrites the token in a single-line call. |
| 192 | _doing_it_wrong( __CLASS__ . '::register', esc_html( $message ), 'jetpack-premium-analytics-$$next-version$$' ); |
| 193 | return false; |
| 194 | } |
| 195 | } |