Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
84.02% |
405 / 482 |
|
72.73% |
32 / 44 |
CRAP | |
0.00% |
0 / 1 |
| PayPal_Payment_Buttons | |
84.02% |
405 / 482 |
|
72.73% |
32 / 44 |
280.70 | |
0.00% |
0 / 1 |
| register_feature_flags | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| is_api_managed_enabled | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| add_editor_feature_flags | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| sanitize_paypal_script_url | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
7 | |||
| get_width_and_border_rules | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| get_qr_frame_style | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_margin_style | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_margin_rules | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| chosen_width | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| is_outline_button | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_border_rules | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 | |||
| sanitize_box | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| plain_box | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| validate_box | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
10.05 | |||
| plain_length | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| sanitize_border | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
6 | |||
| get_text_style | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_text_rules | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| get_button_style | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
3 | |||
| default_label | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| style_attr | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| css_rules | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| sanitize_css_font_size | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
7.03 | |||
| sanitize_css_length | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
| sanitize_css_color | |
84.62% |
11 / 13 |
|
0.00% |
0 / 1 |
7.18 | |||
| add_partner_attribution | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| register_block_style | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| register_block | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| render_block | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| format_price | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| render_api_managed_button | |
94.67% |
160 / 169 |
|
0.00% |
0 / 1 |
34.17 | |||
| get_lowest_variant_price | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
14.09 | |||
| render_legacy_button | |
54.90% |
28 / 51 |
|
0.00% |
0 / 1 |
28.50 | |||
| load_editor_styles | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
| load_editor_scripts | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
| add_style_display | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| register_hooks | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| enqueue_qr_script | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
2 | |||
| init_api | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| init_rest_api | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| register_rest_routes | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| init_jetpack_sharing | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
5.12 | |||
| enable_sharing_on_payment_pages | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| init_admin | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * PayPal Payment Buttons block lets users embed a PayPal button to sell products on their site. |
| 4 | * |
| 5 | * @package automattic/jetpack-paypal-payments |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\PaypalPayments; |
| 9 | |
| 10 | use Automattic\Jetpack\Assets; |
| 11 | use Automattic\Jetpack\Blocks; |
| 12 | use Automattic\Jetpack\Feature_Flags\Feature_Flags; |
| 13 | |
| 14 | /** |
| 15 | * Class PayPal_Payment_Buttons |
| 16 | * |
| 17 | * @package Automattic\Jetpack\PaypalPayments |
| 18 | */ |
| 19 | class PayPal_Payment_Buttons { |
| 20 | /** |
| 21 | * The block full slugname. |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | public const BLOCK_NAME = 'jetpack/paypal-payment-buttons'; |
| 26 | |
| 27 | /** |
| 28 | * Sides and corners the Border Settings panel can write. |
| 29 | * |
| 30 | * @var string[] |
| 31 | */ |
| 32 | private const BOX_SIDES = array( |
| 33 | 'top', |
| 34 | 'right', |
| 35 | 'bottom', |
| 36 | 'left', |
| 37 | 'topLeft', |
| 38 | 'topRight', |
| 39 | 'bottomRight', |
| 40 | 'bottomLeft', |
| 41 | ); |
| 42 | |
| 43 | /** |
| 44 | * Border styles the stroke control offers. |
| 45 | * |
| 46 | * @var string[] |
| 47 | */ |
| 48 | private const BORDER_STYLES = array( 'solid', 'dashed', 'dotted', 'double', 'none' ); |
| 49 | |
| 50 | /** |
| 51 | * Pixel size the QR canvas is drawn at. Keep in step with QR_OPTIONS.width in |
| 52 | * utils/qr-options.js. |
| 53 | * |
| 54 | * @var int |
| 55 | */ |
| 56 | private const QR_SIZE = 200; |
| 57 | |
| 58 | /** |
| 59 | * PayPal partner attribution ID used for tracking. |
| 60 | * |
| 61 | * @var string |
| 62 | */ |
| 63 | public const PAYPAL_PARTNER_ATTRIBUTION_ID = 'WooNCPS_Ecom_Wordpress'; |
| 64 | |
| 65 | /** |
| 66 | * Feature flag gating the API-managed buttons: the connection wizard, the |
| 67 | * wpcom/v2/paypal REST routes, and the Payment Links admin page. |
| 68 | * |
| 69 | * @since 0.9.0 |
| 70 | * @var string |
| 71 | */ |
| 72 | public const API_MANAGED_BUTTONS_FLAG = 'paypal-payments-api-managed-buttons'; |
| 73 | |
| 74 | /** |
| 75 | * Front-end style handle, registered by `register_block_style()`. |
| 76 | * |
| 77 | * @since 0.9.0 |
| 78 | * @var string |
| 79 | */ |
| 80 | public const STYLE_HANDLE = 'jetpack-block-paypal-payment-buttons'; |
| 81 | |
| 82 | /** |
| 83 | * Register the feature flags this package owns. |
| 84 | * |
| 85 | * Call it from every bootstrap before `init`, so the flag exists on every |
| 86 | * request type that reads it (REST, admin, WP-CLI). |
| 87 | * |
| 88 | * @since 0.9.0 |
| 89 | * @return void |
| 90 | */ |
| 91 | public static function register_feature_flags() { |
| 92 | Feature_Flags::register( |
| 93 | self::API_MANAGED_BUTTONS_FLAG, |
| 94 | array( |
| 95 | 'default' => false, |
| 96 | 'description' => 'Create and manage PayPal payment buttons from the editor through the PayPal API, instead of pasting button code.', |
| 97 | 'owner' => 'paypal-payments', |
| 98 | ) |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Whether the API-managed buttons are enabled on this site. |
| 104 | * |
| 105 | * Rendering is deliberately not gated on this: a button created while the |
| 106 | * flag was on must keep rendering after it is turned off. |
| 107 | * |
| 108 | * @since 0.9.0 |
| 109 | * @return bool |
| 110 | */ |
| 111 | public static function is_api_managed_enabled() { |
| 112 | return Feature_Flags::is_enabled( self::API_MANAGED_BUTTONS_FLAG ); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Expose the flag to the block editor under the same name. |
| 117 | * |
| 118 | * Jetpack hooks this on `jetpack_block_editor_feature_flags`; the standalone |
| 119 | * plugin calls it while building its own editor state. |
| 120 | * |
| 121 | * @since 0.9.0 |
| 122 | * |
| 123 | * @param array $flags Feature flags keyed by name. |
| 124 | * @return array |
| 125 | */ |
| 126 | public static function add_editor_feature_flags( $flags ) { |
| 127 | if ( ! is_array( $flags ) ) { |
| 128 | $flags = array(); |
| 129 | } |
| 130 | |
| 131 | $flags[ self::API_MANAGED_BUTTONS_FLAG ] = self::is_api_managed_enabled(); |
| 132 | |
| 133 | return $flags; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Validates and sanitizes a script URL to ensure it's from an allowed PayPal domain. |
| 138 | * |
| 139 | * @param string $url The URL to validate and sanitize. |
| 140 | * @return string|false The sanitized URL, or false if URL is not from an allowed PayPal domain. |
| 141 | */ |
| 142 | public static function sanitize_paypal_script_url( $url ) { |
| 143 | if ( empty( $url ) ) { |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | $parsed_url = wp_parse_url( $url ); |
| 148 | if ( ! $parsed_url || empty( $parsed_url['host'] ) ) { |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | // Normalize the host |
| 153 | $host = strtolower( $parsed_url['host'] ); |
| 154 | $host = rtrim( $host, '.' ); |
| 155 | |
| 156 | // Only allow specific PayPal domains |
| 157 | $allowed_hosts = array( |
| 158 | 'www.paypal.com', |
| 159 | 'paypal.com', |
| 160 | 'www.sandbox.paypal.com', |
| 161 | 'sandbox.paypal.com', |
| 162 | ); |
| 163 | |
| 164 | if ( ! in_array( $host, $allowed_hosts, true ) ) { |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | // Rebuild the URL with HTTPS |
| 169 | $sanitized_url = 'https://' . $host; |
| 170 | |
| 171 | if ( isset( $parsed_url['path'] ) ) { |
| 172 | $sanitized_url .= $parsed_url['path']; |
| 173 | } |
| 174 | if ( isset( $parsed_url['query'] ) ) { |
| 175 | // If we have escaped ampersands in the query string, we need to unescape them. |
| 176 | $sanitized_url .= '?' . str_replace( '&', '&', $parsed_url['query'] ); |
| 177 | } |
| 178 | |
| 179 | return $sanitized_url; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Width and Border, for whichever element the format puts them on — the |
| 184 | * checkout button or the QR frame. |
| 185 | * |
| 186 | * Every value is validated before the style engine sees it. |
| 187 | * wp_style_engine_get_styles() is not a sanitizer: its only filter is |
| 188 | * safecss_filter_attr(), which splits on `;` and keeps any extra declaration |
| 189 | * whose property core allows — so an unchecked `0;position:fixed;…` renders |
| 190 | * verbatim on the published page. |
| 191 | * |
| 192 | * Mirrors getWidthAndBorderStyle() in utils/block-styles.js. |
| 193 | * |
| 194 | * @param array $attributes The block attributes. |
| 195 | * @return array A list of CSS declarations, empty when nothing is configured. |
| 196 | */ |
| 197 | private static function get_width_and_border_rules( $attributes ) { |
| 198 | $rules = array(); |
| 199 | |
| 200 | // max-width keeps a set Width inside the card. With no Width the stylesheet |
| 201 | // sizes the element. |
| 202 | $width = self::chosen_width( $attributes ); |
| 203 | if ( '' !== $width ) { |
| 204 | $rules[] = sprintf( 'width:%s', $width ); |
| 205 | $rules[] = 'max-width:100%'; |
| 206 | } |
| 207 | |
| 208 | return array_merge( $rules, self::get_border_rules( $attributes ) ); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * The QR frame — the element Width, the stroke and the radius go on. |
| 213 | * |
| 214 | * @param array $attributes The block attributes. |
| 215 | * @return string An inline CSS declaration list, empty when nothing is configured. |
| 216 | */ |
| 217 | private static function get_qr_frame_style( $attributes ) { |
| 218 | return self::css_rules( self::get_width_and_border_rules( $attributes ) ); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Margin, from the Border Settings panel. |
| 223 | * |
| 224 | * The button card and the QR card take this and nothing else — Width and Border |
| 225 | * go on the button or the QR frame. A QR-to-BUTTON format switch can leave a |
| 226 | * margin behind, so the card keeps reading it. |
| 227 | * |
| 228 | * Mirrors getMarginStyle() in utils/block-styles.js. |
| 229 | * |
| 230 | * @param array $attributes The block attributes. |
| 231 | * @return string An inline CSS declaration list, empty when nothing is configured. |
| 232 | */ |
| 233 | private static function get_margin_style( $attributes ) { |
| 234 | return self::css_rules( self::get_margin_rules( $attributes ) ); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * The margin declarations, for a composer to join with its own. |
| 239 | * |
| 240 | * @param array $attributes The block attributes. |
| 241 | * @return array A list of CSS declarations, empty when nothing is configured. |
| 242 | */ |
| 243 | private static function get_margin_rules( $attributes ) { |
| 244 | $style = isset( $attributes['style'] ) && is_array( $attributes['style'] ) ? $attributes['style'] : array(); |
| 245 | $engine = wp_style_engine_get_styles( |
| 246 | array( 'spacing' => array( 'margin' => self::sanitize_box( $style['spacing']['margin'] ?? null ) ) ) |
| 247 | ); |
| 248 | |
| 249 | return empty( $engine['css'] ) ? array() : array( rtrim( $engine['css'], ';' ) ); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * The chosen width, with its unit. |
| 254 | * |
| 255 | * Width has its own unit, so it goes through as typed. The style engine never |
| 256 | * sees it, so a spacing preset would be emitted raw — the width |
| 257 | * control cannot produce one, and this keeps it that way. |
| 258 | * |
| 259 | * @param array $attributes The block attributes. |
| 260 | * @return string The width, or '' when none is set. |
| 261 | */ |
| 262 | private static function chosen_width( $attributes ) { |
| 263 | return self::plain_length( $attributes['blockWidth'] ?? '' ); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Whether the button draws as an outline rather than a filled face. |
| 268 | * |
| 269 | * @param array $attributes The block attributes. |
| 270 | * @return bool True when the Outline style is selected. |
| 271 | */ |
| 272 | private static function is_outline_button( $attributes ) { |
| 273 | return 'outline' === ( $attributes['buttonStyle'] ?? 'fill' ); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Border Settings — the radius and the stroke. |
| 278 | * |
| 279 | * Mirrors getBorderStyle() in utils/block-styles.js. |
| 280 | * |
| 281 | * @param array $attributes The block attributes. |
| 282 | * @return array A list of CSS declarations, empty when nothing is configured. |
| 283 | */ |
| 284 | private static function get_border_rules( $attributes ) { |
| 285 | $rules = array(); |
| 286 | $style = isset( $attributes['style'] ) && is_array( $attributes['style'] ) ? $attributes['style'] : array(); |
| 287 | $border = self::sanitize_border( $style['border'] ?? null ); |
| 288 | |
| 289 | // The style engine only emits a hex border-color, and a theme palette entry |
| 290 | // can be rgba() or hsl(). getBorderStyle() keeps those on the canvas, so |
| 291 | // emit the validated value here rather than lose it. |
| 292 | $border_color = $border['color'] ?? ''; |
| 293 | unset( $border['color'] ); |
| 294 | |
| 295 | $engine = wp_style_engine_get_styles( array( 'border' => $border ) ); |
| 296 | |
| 297 | if ( ! empty( $engine['css'] ) ) { |
| 298 | $rules[] = rtrim( $engine['css'], ';' ); |
| 299 | } |
| 300 | |
| 301 | if ( '' !== $border_color ) { |
| 302 | $rules[] = sprintf( 'border-color:%s', $border_color ); |
| 303 | } |
| 304 | |
| 305 | return $rules; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Validate a per-side box value — margin, or a per-corner radius. |
| 310 | * |
| 311 | * @param mixed $box A length string, or an array keyed by side or corner. |
| 312 | * @return mixed The value with every side validated, or null when none survive. |
| 313 | */ |
| 314 | private static function sanitize_box( $box ) { |
| 315 | return self::validate_box( $box, false ); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * A per-corner box with no spacing preset in it. |
| 320 | * |
| 321 | * Mirrors plainBox() in utils/block-styles.js. |
| 322 | * |
| 323 | * @param mixed $box A length string, or an array keyed by corner. |
| 324 | * @return mixed The value with every corner validated, or null when none survive. |
| 325 | */ |
| 326 | private static function plain_box( $box ) { |
| 327 | return self::validate_box( $box, true ); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * The shared body of sanitize_box() and plain_box(). |
| 332 | * |
| 333 | * @param mixed $box A length string, or an array keyed by side or corner. |
| 334 | * @param bool $plain Refuse spacing presets. |
| 335 | * @return mixed The value with every side validated, or null when none survive. |
| 336 | */ |
| 337 | private static function validate_box( $box, $plain ) { |
| 338 | if ( is_string( $box ) ) { |
| 339 | $length = $plain ? self::plain_length( $box ) : self::sanitize_css_length( $box ); |
| 340 | return '' === $length ? null : $length; |
| 341 | } |
| 342 | |
| 343 | if ( ! is_array( $box ) ) { |
| 344 | return null; |
| 345 | } |
| 346 | |
| 347 | $clean = array(); |
| 348 | foreach ( $box as $side => $value ) { |
| 349 | // sanitize_key() would let a hostile key through as a mangled one, so |
| 350 | // the side names are an allowlist. |
| 351 | if ( ! in_array( $side, self::BOX_SIDES, true ) ) { |
| 352 | continue; |
| 353 | } |
| 354 | $length = $plain ? self::plain_length( $value ) : self::sanitize_css_length( $value ); |
| 355 | if ( '' !== $length ) { |
| 356 | $clean[ $side ] = $length; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | return empty( $clean ) ? null : $clean; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * A length with no spacing preset in it. |
| 365 | * |
| 366 | * Margin takes presets, because it goes through the style engine, which |
| 367 | * expands them. Width and border do not: core's JS engine |
| 368 | * expands a preset radius and wp_style_engine_get_styles() drops it, so a |
| 369 | * preset would render on the canvas and disappear on the page. |
| 370 | * |
| 371 | * Mirrors plainLength() in utils/block-styles.js. |
| 372 | * |
| 373 | * @param mixed $value A raw attribute value. |
| 374 | * @return string The length, or '' when it is not a plain one. |
| 375 | */ |
| 376 | private static function plain_length( $value ) { |
| 377 | $length = self::sanitize_css_length( $value ); |
| 378 | |
| 379 | return str_starts_with( $length, 'var:preset' ) ? '' : $length; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Validate the border sub-array. |
| 384 | * |
| 385 | * Only the four keys the Border Settings panel writes are kept. The per-side |
| 386 | * longhands core also understands (border.top and friends) are dropped — the |
| 387 | * block has no UI for them, and they were a way past the color check. |
| 388 | * |
| 389 | * @param mixed $border The raw border attribute. |
| 390 | * @return array The border array, with only validated values. |
| 391 | */ |
| 392 | private static function sanitize_border( $border ) { |
| 393 | if ( ! is_array( $border ) ) { |
| 394 | return array(); |
| 395 | } |
| 396 | |
| 397 | $clean = array(); |
| 398 | |
| 399 | $radius = self::plain_box( $border['radius'] ?? null ); |
| 400 | if ( null !== $radius ) { |
| 401 | $clean['radius'] = $radius; |
| 402 | } |
| 403 | |
| 404 | $width = self::plain_length( $border['width'] ?? '' ); |
| 405 | |
| 406 | // border-style defaults to `none`, so a width always gets a style. A color |
| 407 | // alone draws nothing, so it is dropped. getBorderStyle() matches. |
| 408 | if ( '' !== $width ) { |
| 409 | $clean['width'] = $width; |
| 410 | $style = (string) ( $border['style'] ?? '' ); |
| 411 | $clean['style'] = in_array( $style, self::BORDER_STYLES, true ) ? $style : 'solid'; |
| 412 | |
| 413 | $color = self::sanitize_css_color( $border['color'] ?? '' ); |
| 414 | if ( '' !== $color ) { |
| 415 | $clean['color'] = $color; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | return $clean; |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Color and Typography, for the QR caption, the payment link and the button face. |
| 424 | * |
| 425 | * Mirrors getTextStyle() in utils/block-styles.js. |
| 426 | * |
| 427 | * @param string $text_color The chosen color. |
| 428 | * @param string $text_size The chosen font size. |
| 429 | * @return string An inline CSS declaration list, empty when nothing is configured. |
| 430 | */ |
| 431 | private static function get_text_style( $text_color, $text_size ) { |
| 432 | return self::css_rules( self::get_text_rules( $text_color, $text_size ) ); |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * The color and size declarations, for a composer to join with its own. |
| 437 | * |
| 438 | * @param string $text_color The chosen color. |
| 439 | * @param string $text_size The chosen font size. |
| 440 | * @return array A list of CSS declarations, empty when nothing is configured. |
| 441 | */ |
| 442 | private static function get_text_rules( $text_color, $text_size ) { |
| 443 | $rules = array(); |
| 444 | |
| 445 | $color = self::sanitize_css_color( $text_color ); |
| 446 | if ( '' !== $color ) { |
| 447 | $rules[] = sprintf( 'color:%s', $color ); |
| 448 | } |
| 449 | |
| 450 | $size = self::sanitize_css_font_size( $text_size ); |
| 451 | if ( '' !== $size ) { |
| 452 | $rules[] = sprintf( 'font-size:%s', $size ); |
| 453 | } |
| 454 | |
| 455 | return $rules; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Color, Styles and Typography for the checkout button. |
| 460 | * |
| 461 | * Mirrors getButtonStyle() in utils/block-styles.js. |
| 462 | * |
| 463 | * @param array $attributes The block attributes. |
| 464 | * @return string An inline CSS declaration list, empty when nothing is configured. |
| 465 | */ |
| 466 | private static function get_button_style( $attributes ) { |
| 467 | $rules = array(); |
| 468 | |
| 469 | // Outline renders on the theme's own background, so an inline |
| 470 | // background-color would beat style.scss's transparent rule and fill the |
| 471 | // button back in. |
| 472 | $background = self::is_outline_button( $attributes ) |
| 473 | ? '' |
| 474 | : self::sanitize_css_color( $attributes['buttonBackgroundColor'] ?? '' ); |
| 475 | if ( '' !== $background ) { |
| 476 | $rules[] = sprintf( 'background-color:%s', $background ); |
| 477 | } |
| 478 | |
| 479 | return self::css_rules( |
| 480 | array_merge( |
| 481 | self::get_text_rules( $attributes['buttonTextColor'] ?? '', $attributes['buttonFontSize'] ?? '' ), |
| 482 | $rules, |
| 483 | // Width and Border go on the button, not the card — see get_margin_style(). |
| 484 | self::get_width_and_border_rules( $attributes ) |
| 485 | ) |
| 486 | ); |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * The default label for a format's output. |
| 491 | * |
| 492 | * The button face, the QR caption and the payment link share it. Mirrors |
| 493 | * DEFAULT_LABEL in utils/defaults.js. |
| 494 | * |
| 495 | * @return string The label. |
| 496 | */ |
| 497 | private static function default_label() { |
| 498 | return __( 'Buy now', 'jetpack-paypal-payments' ); |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * A style attribute built from a declaration list, or nothing when it is empty. |
| 503 | * |
| 504 | * @param string $style An inline CSS declaration list. |
| 505 | * @return string ` style="…"`, or '' when there is nothing to set. |
| 506 | */ |
| 507 | private static function style_attr( $style ) { |
| 508 | return '' !== $style ? ' style="' . esc_attr( $style ) . '"' : ''; |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Join declarations into an inline CSS list. |
| 513 | * |
| 514 | * The trailing semicolon keeps the list safe to concatenate with another |
| 515 | * style value on the same element. |
| 516 | * |
| 517 | * @param array $rules The declarations. |
| 518 | * @return string The declaration list, or '' when there are none. |
| 519 | */ |
| 520 | private static function css_rules( $rules ) { |
| 521 | // Each value is validated before it gets here — sanitize_css_length(), |
| 522 | // sanitize_css_color() — so the list is joined as-is. |
| 523 | return empty( $rules ) ? '' : implode( ';', $rules ) . ';'; |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * Accept only a font size the picker can produce. |
| 528 | * |
| 529 | * FontSizePicker hands back the size with its unit: a plain length, a theme |
| 530 | * preset's CSS variable, or a fluid clamp()/calc() expression. The charset is |
| 531 | * narrow enough that there is nothing to break out of the declaration with — |
| 532 | * no semicolon, no url(), no quotes. |
| 533 | * |
| 534 | * @param string $size The raw attribute value. |
| 535 | * @return string The size, or '' when it is not one. |
| 536 | */ |
| 537 | private static function sanitize_css_font_size( $size ) { |
| 538 | if ( ! is_scalar( $size ) ) { |
| 539 | return ''; |
| 540 | } |
| 541 | |
| 542 | $size = trim( (string) $size ); |
| 543 | |
| 544 | // FontSizePicker drops the unit when the theme's own sizes are numbers. |
| 545 | // Core reads a bare number as px, so both sides do the same. |
| 546 | if ( preg_match( '/^\d+(\.\d+)?$/', $size ) ) { |
| 547 | return $size . 'px'; |
| 548 | } |
| 549 | |
| 550 | // A spacing preset is a length but not a font size — it would be emitted |
| 551 | // raw as `font-size:var:preset|spacing|50` and dropped by the browser. |
| 552 | if ( '' !== self::plain_length( $size ) ) { |
| 553 | return $size; |
| 554 | } |
| 555 | |
| 556 | if ( preg_match( '/^var\(--wp--preset--font-size--[a-z0-9-]+\)$/i', $size ) ) { |
| 557 | return $size; |
| 558 | } |
| 559 | |
| 560 | // No doubled `/` or `*`: `/*` opens a comment that swallows the rest, and |
| 561 | // `**` and `//` are not CSS operators. |
| 562 | if ( preg_match( '#[/*]{2}#', $size ) ) { |
| 563 | return ''; |
| 564 | } |
| 565 | |
| 566 | return preg_match( '/^(clamp|calc)\([a-z0-9.,%\s()+\-*\/]+\)$/i', $size ) ? $size : ''; |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Accept only a length the width control can produce. |
| 571 | * |
| 572 | * @param string $length The raw attribute value, e.g. '50%' or '150px'. |
| 573 | * @return string The length, or '' when it is not one. |
| 574 | */ |
| 575 | private static function sanitize_css_length( $length ) { |
| 576 | if ( ! is_scalar( $length ) ) { |
| 577 | return ''; |
| 578 | } |
| 579 | |
| 580 | $length = trim( (string) $length ); |
| 581 | |
| 582 | // 0 is a length a merchant can pick — core's spacing scale starts there, |
| 583 | // and it is how you cancel the stylesheet's own margin. |
| 584 | if ( '0' === $length ) { |
| 585 | return $length; |
| 586 | } |
| 587 | |
| 588 | // A chosen spacing preset. The style engine expands it, so it goes |
| 589 | // through as stored; the units match theme.json's `spacing.units`. |
| 590 | if ( preg_match( '/^var:preset\|spacing\|[a-z0-9-]+$/i', $length ) ) { |
| 591 | return $length; |
| 592 | } |
| 593 | |
| 594 | return preg_match( '/^\d+(\.\d+)?(%|px|em|rem|pt|vw|vh)$/', $length ) ? $length : ''; |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * Accept only a color the picker can produce. |
| 599 | * |
| 600 | * ColorGradientControl hands back a hex value or a CSS variable reference for |
| 601 | * a theme palette entry. Anything else is a hand-edited or injected value and |
| 602 | * is dropped rather than written into a style attribute. |
| 603 | * |
| 604 | * @param string $color The raw attribute value. |
| 605 | * @return string The color, or '' when it is not one. |
| 606 | */ |
| 607 | private static function sanitize_css_color( $color ) { |
| 608 | if ( ! is_scalar( $color ) ) { |
| 609 | return ''; |
| 610 | } |
| 611 | |
| 612 | $color = trim( (string) $color ); |
| 613 | |
| 614 | $hex = sanitize_hex_color( $color ); |
| 615 | if ( ! empty( $hex ) ) { |
| 616 | return $hex; |
| 617 | } |
| 618 | |
| 619 | // sanitize_hex_color() stops at 6 digits. The palette editor's picker has |
| 620 | // alpha on, so a custom color is stored as #rrggbbaa. |
| 621 | if ( preg_match( '/^#([0-9a-f]{4}|[0-9a-f]{8})$/i', $color ) ) { |
| 622 | return $color; |
| 623 | } |
| 624 | |
| 625 | // `var:preset|color|primary` is what the editor stores for a palette entry. |
| 626 | // The style engine emits preset border colors as a class rather than inline |
| 627 | // CSS, so expand it here — getTextStyle() does the same for the canvas. |
| 628 | // Only color presets expand: a spacing or font preset is not a color, so it |
| 629 | // falls through and is refused, the way the canvas refuses it. |
| 630 | if ( preg_match( '/^var:preset\|color\|([a-z0-9-]+)$/i', $color, $preset ) ) { |
| 631 | // Kebab-cased the way WP names the custom property, or a `heavenlyBlue` |
| 632 | // slug points at a variable nothing defines. cssColor() uses lodash's |
| 633 | // kebabCase, which this function is a port of. |
| 634 | return sprintf( 'var(--wp--preset--color--%s)', _wp_to_kebab_case( $preset[1] ) ); |
| 635 | } |
| 636 | |
| 637 | if ( preg_match( '/^var\(--wp--[a-z0-9-]+\)$/i', $color ) ) { |
| 638 | return $color; |
| 639 | } |
| 640 | |
| 641 | // A theme palette entry can be any CSS color, and ColorGradientControl |
| 642 | // hands back its raw value. Digits and separators only, so there is |
| 643 | // nothing to break out of the declaration with. |
| 644 | return preg_match( '/^(rgb|hsl)a?\([\d.,%\s\/]+\)$/i', $color ) ? $color : ''; |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * Append the partner attribution (BN) code to a PayPal payment URL. |
| 649 | * |
| 650 | * Every route a merchant can use to hand a payment link to a buyer — the |
| 651 | * rendered button, the copy buttons in the editor and admin, the emailed |
| 652 | * link — has to carry the same `at_code`, or the resulting sales aren't |
| 653 | * attributed to us. `add_query_arg()` replaces an existing `at_code`, so |
| 654 | * this is safe to apply to a URL that already has one. |
| 655 | * |
| 656 | * @since 0.9.0 |
| 657 | * |
| 658 | * @param string $url A PayPal payment URL. |
| 659 | * @return string The URL with the attribution code, or the original URL if it isn't a PayPal URL. |
| 660 | */ |
| 661 | public static function add_partner_attribution( $url ) { |
| 662 | $sanitized = self::sanitize_paypal_script_url( $url ); |
| 663 | |
| 664 | if ( false === $sanitized ) { |
| 665 | return $url; |
| 666 | } |
| 667 | |
| 668 | return add_query_arg( 'at_code', self::PAYPAL_PARTNER_ATTRIBUTION_ID, $sanitized ); |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * Side-load the sibling style.css and register it under STYLE_HANDLE. |
| 673 | * |
| 674 | * A `file:` style in block.json would also make core register the editor bundle a |
| 675 | * second time, so the block takes this handle as its `style` arg. Both bootstraps |
| 676 | * call it. |
| 677 | * |
| 678 | * @since 0.9.0 |
| 679 | * @return void |
| 680 | */ |
| 681 | public static function register_block_style() { |
| 682 | Assets::register_script( |
| 683 | self::STYLE_HANDLE, |
| 684 | '../../dist/paypal-payment-buttons/style.js', |
| 685 | __FILE__, |
| 686 | array( |
| 687 | 'css_path' => '../../dist/paypal-payment-buttons/style.css', |
| 688 | ) |
| 689 | ); |
| 690 | } |
| 691 | |
| 692 | /** |
| 693 | * Registers the block for use in Gutenberg |
| 694 | * This is done via an action so that we can disable |
| 695 | * registration if we need to. |
| 696 | */ |
| 697 | public static function register_block() { |
| 698 | self::register_block_style(); |
| 699 | |
| 700 | Blocks::jetpack_register_block( |
| 701 | __DIR__, |
| 702 | array( |
| 703 | 'render_callback' => array( __CLASS__, 'render_block' ), |
| 704 | 'plan_check' => true, |
| 705 | 'style' => self::STYLE_HANDLE, |
| 706 | ) |
| 707 | ); |
| 708 | } |
| 709 | |
| 710 | /** |
| 711 | * Render the block. |
| 712 | * |
| 713 | * Supports both API-managed buttons (V2) and legacy paste-code buttons (V1). |
| 714 | * API-managed buttons use the payment_url from the PayPal Pay Links & Buttons API. |
| 715 | * Legacy buttons use scriptSrc/hostedButtonId from the paste-code workflow. |
| 716 | * |
| 717 | * @param array $attributes The block attributes. |
| 718 | * @param string $content The block content. |
| 719 | * @return string|void The rendered block HTML. |
| 720 | */ |
| 721 | public static function render_block( $attributes, $content ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 722 | $api_managed = ! empty( $attributes['isApiManaged'] ); |
| 723 | |
| 724 | // ─── V2: API-managed button ─── |
| 725 | if ( $api_managed ) { |
| 726 | return self::render_api_managed_button( $attributes ); |
| 727 | } |
| 728 | |
| 729 | // ─── V1: Legacy paste-code button ─── |
| 730 | return self::render_legacy_button( $attributes ); |
| 731 | } |
| 732 | |
| 733 | /** |
| 734 | * Render an API-managed button created via the Pay Links & Buttons API. |
| 735 | * |
| 736 | * Generates a styled form that links to the PayPal payment page. |
| 737 | * The BN code is included as a query parameter for revenue attribution. |
| 738 | * |
| 739 | * @since 0.7.0 |
| 740 | * |
| 741 | * @param array $attributes The block attributes. |
| 742 | * @return string|void The rendered button HTML. |
| 743 | */ |
| 744 | /** |
| 745 | * Currency symbols for frontend price formatting. |
| 746 | * Matches the JS CURRENCY_SYMBOLS map in paypal-button-preview.js. |
| 747 | * |
| 748 | * @var array |
| 749 | */ |
| 750 | private static $currency_symbols = array( |
| 751 | 'USD' => '$', |
| 752 | 'EUR' => '€', |
| 753 | 'GBP' => '£', |
| 754 | 'JPY' => '¥', |
| 755 | 'CAD' => 'CA$', |
| 756 | 'AUD' => 'A$', |
| 757 | 'CHF' => 'CHF', |
| 758 | 'CNY' => '¥', |
| 759 | 'INR' => '₹', |
| 760 | 'BRL' => 'R$', |
| 761 | 'MXN' => 'MX$', |
| 762 | 'HKD' => 'HK$', |
| 763 | 'NZD' => 'NZ$', |
| 764 | 'SGD' => 'S$', |
| 765 | 'SEK' => 'kr', |
| 766 | 'NOK' => 'kr', |
| 767 | 'DKK' => 'kr', |
| 768 | 'PLN' => 'zł', |
| 769 | 'CZK' => 'Kč', |
| 770 | 'HUF' => 'Ft', |
| 771 | 'ILS' => '₪', |
| 772 | 'MYR' => 'RM', |
| 773 | 'PHP' => '₱', |
| 774 | 'TWD' => 'NT$', |
| 775 | 'THB' => '฿', |
| 776 | ); |
| 777 | |
| 778 | /** |
| 779 | * Format a price with its currency symbol. |
| 780 | * |
| 781 | * @param string $price The price value. |
| 782 | * @param string $currency The ISO currency code. |
| 783 | * @return string Formatted price string (e.g., "$29.99"). |
| 784 | */ |
| 785 | public static function format_price( $price, $currency ) { |
| 786 | $symbol = self::$currency_symbols[ $currency ] ?? $currency; |
| 787 | return $symbol . $price; |
| 788 | } |
| 789 | |
| 790 | /** |
| 791 | * Render an API-managed PayPal payment button on the frontend. |
| 792 | * |
| 793 | * @param array $attributes The block attributes. |
| 794 | * @return string|void The rendered button HTML. |
| 795 | */ |
| 796 | private static function render_api_managed_button( $attributes ) { |
| 797 | $resource_id = $attributes['resourceId'] ?? ''; |
| 798 | $payment_url = $attributes['paymentLink'] ?? ''; |
| 799 | $product_name = $attributes['productName'] ?? ''; |
| 800 | $price = $attributes['price'] ?? ''; |
| 801 | $currency = $attributes['currencyCode'] ?? 'USD'; |
| 802 | $product_description = $attributes['productDescription'] ?? ''; |
| 803 | $image_url = $attributes['imageUrl'] ?? ''; |
| 804 | $variants_enabled = ! empty( $attributes['variantsEnabled'] ); |
| 805 | $variants = $attributes['variants'] ?? null; |
| 806 | $format = $attributes['format'] ?? 'BUTTON'; |
| 807 | $button_text = trim( (string) ( $attributes['buttonText'] ?? '' ) ); |
| 808 | $qr_show_caption = ! empty( $attributes['qrShowCaption'] ); |
| 809 | $qr_caption = trim( (string) ( $attributes['qrCaption'] ?? '' ) ); |
| 810 | $link_text = trim( (string) ( $attributes['linkText'] ?? '' ) ); |
| 811 | |
| 812 | // Validate — only known format values are accepted. |
| 813 | if ( ! in_array( $format, array( 'BUTTON', 'LINK', 'QR' ), true ) ) { |
| 814 | $format = 'BUTTON'; |
| 815 | } |
| 816 | |
| 817 | if ( empty( $resource_id ) || empty( $payment_url ) ) { |
| 818 | return; |
| 819 | } |
| 820 | |
| 821 | // A buyer would only reach PayPal's "not found" page. |
| 822 | if ( PayPal_API_Client::is_deleted_resource( $resource_id ) ) { |
| 823 | return '<!-- PayPal payment link deleted -->'; |
| 824 | } |
| 825 | |
| 826 | // Validate the payment URL is from a legitimate PayPal domain. |
| 827 | $sanitized_payment_url = self::sanitize_paypal_script_url( $payment_url ); |
| 828 | if ( false === $sanitized_payment_url ) { |
| 829 | return; |
| 830 | } |
| 831 | |
| 832 | self::register_hooks(); |
| 833 | |
| 834 | // Only the standalone QR format draws a code. |
| 835 | if ( 'QR' === $format ) { |
| 836 | self::enqueue_qr_script(); |
| 837 | } |
| 838 | |
| 839 | // Append BN code for revenue attribution tracking. |
| 840 | $action_url = esc_url( self::add_partner_attribution( $sanitized_payment_url ) ); |
| 841 | |
| 842 | // ─── LINK format: plain anchor ─────────────────────────────────── |
| 843 | if ( 'LINK' === $format ) { |
| 844 | $wrapper_attributes = get_block_wrapper_attributes(); |
| 845 | // An empty label falls back to the default, the same way the button |
| 846 | // face and the QR caption do. |
| 847 | $link_label = '' !== $link_text ? $link_text : self::default_label(); |
| 848 | $link_style = self::get_text_style( |
| 849 | $attributes['linkColor'] ?? '', |
| 850 | $attributes['linkFontSize'] ?? '' |
| 851 | ); |
| 852 | |
| 853 | return sprintf( |
| 854 | '<div %1$s><a href="%2$s" class="jetpack-paypal-button__paypal-link"%5$s target="_blank" rel="noopener noreferrer">%3$s<span class="screen-reader-text">%4$s</span></a></div>', |
| 855 | $wrapper_attributes, |
| 856 | $action_url, |
| 857 | esc_html( $link_label ), |
| 858 | esc_html__( '(opens in a new tab)', 'jetpack-paypal-payments' ), |
| 859 | self::style_attr( $link_style ) |
| 860 | ); |
| 861 | } |
| 862 | |
| 863 | // ─── QR format: standalone auto-rendering QR canvas ────────────── |
| 864 | if ( 'QR' === $format ) { |
| 865 | $wrapper_attributes = get_block_wrapper_attributes(); |
| 866 | // Margin goes on .jetpack-paypal-button, which style.scss caps at 400px. |
| 867 | // Width and the stroke go on the frame inside it. |
| 868 | $block_style = self::style_attr( self::get_margin_style( $attributes ) ); |
| 869 | $frame_style = self::style_attr( self::get_qr_frame_style( $attributes ) ); |
| 870 | $download_label = esc_html__( 'Download QR Code', 'jetpack-paypal-payments' ); |
| 871 | $copy_label = esc_html__( 'Copy Link', 'jetpack-paypal-payments' ); |
| 872 | $copied_label = esc_attr__( 'Copied!', 'jetpack-paypal-payments' ); |
| 873 | |
| 874 | // An empty caption falls back to the default rather than drawing a |
| 875 | // blank line, the same way the button label does. |
| 876 | $caption_text = '' !== $qr_caption ? $qr_caption : self::default_label(); |
| 877 | $caption_style = self::get_text_style( |
| 878 | $attributes['captionColor'] ?? '', |
| 879 | $attributes['captionFontSize'] ?? '' |
| 880 | ); |
| 881 | $caption_html = $qr_show_caption |
| 882 | ? sprintf( |
| 883 | '<p class="jetpack-paypal-button__qr-caption"%s>%s</p>', |
| 884 | self::style_attr( $caption_style ), |
| 885 | esc_html( $caption_text ) |
| 886 | ) |
| 887 | : ''; |
| 888 | |
| 889 | // No attribution line: the `Show "Powered by PayPal" text` checkbox is |
| 890 | // the button's alone, so the QR draws the code and its caption and |
| 891 | // nothing else. |
| 892 | return sprintf( |
| 893 | '<div %1$s> |
| 894 | <div class="jetpack-paypal-button jetpack-paypal-button--qr-format"%7$s> |
| 895 | <div class="jetpack-paypal-button__qr-standalone"> |
| 896 | <div class="jetpack-paypal-button__qr-frame"%8$s> |
| 897 | <canvas class="jetpack-paypal-button__qr-canvas jetpack-paypal-button__qr-canvas--standalone" width="%9$d" height="%9$d" data-qr-url="%3$s"></canvas> |
| 898 | </div> |
| 899 | %2$s |
| 900 | <div class="jetpack-paypal-button__qr-link"> |
| 901 | <input type="text" readonly class="jetpack-paypal-button__qr-link-input" value="%3$s" /> |
| 902 | <button type="button" class="jetpack-paypal-button__qr-copy" data-copy-label="%4$s" data-copied-label="%5$s">%4$s</button> |
| 903 | </div> |
| 904 | <button type="button" class="jetpack-paypal-button__qr-download">%6$s</button> |
| 905 | </div> |
| 906 | </div> |
| 907 | </div>', |
| 908 | $wrapper_attributes, |
| 909 | $caption_html, |
| 910 | esc_attr( $action_url ), |
| 911 | $copy_label, |
| 912 | $copied_label, |
| 913 | $download_label, |
| 914 | $block_style, |
| 915 | $frame_style, |
| 916 | self::QR_SIZE |
| 917 | ); |
| 918 | } |
| 919 | |
| 920 | // ─── BUTTON format (default): existing full button card ────────── |
| 921 | |
| 922 | // Product image. PayPal receives it too, as the line item's image_url. |
| 923 | $image_html = ''; |
| 924 | if ( ! empty( $image_url ) ) { |
| 925 | $image_html = sprintf( |
| 926 | '<div class="jetpack-paypal-button__product-image"><img src="%s" alt="%s" /></div>', |
| 927 | esc_url( $image_url ), |
| 928 | esc_attr( $product_name ) |
| 929 | ); |
| 930 | } |
| 931 | |
| 932 | // Build product info section. |
| 933 | $description_html = ''; |
| 934 | if ( ! empty( $product_description ) ) { |
| 935 | $description_html = sprintf( |
| 936 | '<span class="jetpack-paypal-button__product-description">%s</span>', |
| 937 | esc_html( $product_description ) |
| 938 | ); |
| 939 | } |
| 940 | |
| 941 | // PayPal drops the product-level amount once the options have their own |
| 942 | // prices, but the block keeps whatever the merchant typed. Ignore it. |
| 943 | if ( $variants_enabled && PayPal_Attribute_Mapper::variants_have_pricing( $variants ) ) { |
| 944 | $price = ''; |
| 945 | } |
| 946 | |
| 947 | // Headline price: the product price, or the cheapest option when there is none. |
| 948 | // PayPal accepts a price of 0, so the empty test is '' — empty() drops it. |
| 949 | $price = (string) $price; |
| 950 | $price_html = ''; |
| 951 | if ( '' !== $price ) { |
| 952 | $price_html = sprintf( |
| 953 | '<span class="jetpack-paypal-button__product-price">%s</span>', |
| 954 | esc_html( self::format_price( $price, $currency ) ) |
| 955 | ); |
| 956 | } elseif ( $variants_enabled ) { |
| 957 | $lowest = self::get_lowest_variant_price( $variants ); |
| 958 | if ( null !== $lowest ) { |
| 959 | $price_html = sprintf( |
| 960 | '<span class="jetpack-paypal-button__product-price">%s</span>', |
| 961 | esc_html( |
| 962 | sprintf( |
| 963 | /* translators: %s: formatted price, e.g. "$29.99" */ |
| 964 | __( 'From %s', 'jetpack-paypal-payments' ), |
| 965 | self::format_price( $lowest, $currency ) |
| 966 | ) |
| 967 | ) |
| 968 | ); |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | // Build variant options display. |
| 973 | $variants_html = ''; |
| 974 | if ( $variants_enabled && ! empty( $variants['dimensions'] ) && is_array( $variants['dimensions'] ) ) { |
| 975 | $variant_groups = array(); |
| 976 | foreach ( $variants['dimensions'] as $dimension ) { |
| 977 | $dim_name = esc_html( $dimension['name'] ?? '' ); |
| 978 | if ( '' === $dim_name || empty( $dimension['options'] ) ) { |
| 979 | continue; |
| 980 | } |
| 981 | |
| 982 | $options_html = array(); |
| 983 | foreach ( $dimension['options'] as $option ) { |
| 984 | $label = esc_html( $option['label'] ?? '' ); |
| 985 | if ( '' === $label ) { |
| 986 | continue; |
| 987 | } |
| 988 | |
| 989 | // Show the option's price, unless it repeats the product price above. |
| 990 | $option_value = (string) ( $option['unit_amount']['value'] ?? '' ); |
| 991 | $option_price = ''; |
| 992 | if ( '' !== $option_value && $option_value !== $price ) { |
| 993 | $option_price = ' <span class="jetpack-paypal-button__variant-price">' |
| 994 | . esc_html( self::format_price( $option_value, $currency ) ) |
| 995 | . '</span>'; |
| 996 | } |
| 997 | |
| 998 | $options_html[] = '<span class="jetpack-paypal-button__variant-option">' |
| 999 | . $label . $option_price . '</span>'; |
| 1000 | } |
| 1001 | |
| 1002 | if ( ! empty( $options_html ) ) { |
| 1003 | $variant_groups[] = '<div class="jetpack-paypal-button__variant-group">' |
| 1004 | . '<span class="jetpack-paypal-button__variant-name">' . $dim_name . ':</span> ' |
| 1005 | . implode( '', $options_html ) |
| 1006 | . '</div>'; |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | if ( ! empty( $variant_groups ) ) { |
| 1011 | $variants_html = '<div class="jetpack-paypal-button__variants">' |
| 1012 | . '<p class="jetpack-paypal-button__variants-label">' |
| 1013 | . esc_html__( 'Options available — select at checkout:', 'jetpack-paypal-payments' ) |
| 1014 | . '</p>' |
| 1015 | . implode( '', $variant_groups ) |
| 1016 | . '</div>'; |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | $wrapper_attributes = get_block_wrapper_attributes(); |
| 1021 | // Width and Border go on the button, not this card — see get_button_style(). |
| 1022 | $block_style = self::style_attr( self::get_margin_style( $attributes ) ); |
| 1023 | |
| 1024 | // A blank label would draw an unreadable button, so fall back to the same |
| 1025 | // default the editor preview uses. |
| 1026 | $label = '' !== $button_text ? $button_text : self::default_label(); |
| 1027 | |
| 1028 | $attribution_html = empty( $attributes['buttonShowPoweredBy'] ) |
| 1029 | ? '' |
| 1030 | : sprintf( |
| 1031 | '<p class="jetpack-paypal-button__attribution">%s</p>', |
| 1032 | esc_html__( 'Powered by PayPal', 'jetpack-paypal-payments' ) |
| 1033 | ); |
| 1034 | |
| 1035 | // `is-style-outline` is the name core and the other Jetpack blocks already |
| 1036 | // use, so themes recognize it. |
| 1037 | $button_class = 'jetpack-paypal-button__checkout-link wp-element-button' |
| 1038 | . ( self::is_outline_button( $attributes ) ? ' is-style-outline' : '' ); |
| 1039 | $button_style = self::style_attr( self::get_button_style( $attributes ) ); |
| 1040 | |
| 1041 | return sprintf( |
| 1042 | '<div %8$s> |
| 1043 | <div class="jetpack-paypal-button"%11$s> |
| 1044 | %9$s |
| 1045 | <div class="jetpack-paypal-button__product"> |
| 1046 | <div class="jetpack-paypal-button__product-info"> |
| 1047 | <span class="jetpack-paypal-button__product-name">%1$s</span> |
| 1048 | %2$s |
| 1049 | </div> |
| 1050 | %3$s |
| 1051 | </div> |
| 1052 | %7$s |
| 1053 | <div class="jetpack-paypal-button__buttons"> |
| 1054 | <a href="%4$s" class="%12$s"%13$s target="_blank" rel="noopener noreferrer"> |
| 1055 | <span class="jetpack-paypal-button__button-text">%5$s</span> |
| 1056 | <span class="screen-reader-text">%10$s</span> |
| 1057 | </a> |
| 1058 | </div> |
| 1059 | %6$s |
| 1060 | </div> |
| 1061 | </div>', |
| 1062 | esc_html( $product_name ), |
| 1063 | $description_html, |
| 1064 | $price_html, |
| 1065 | $action_url, |
| 1066 | esc_html( $label ), |
| 1067 | $attribution_html, |
| 1068 | $variants_html, |
| 1069 | $wrapper_attributes, |
| 1070 | $image_html, |
| 1071 | esc_html__( 'PayPal (opens in a new tab)', 'jetpack-paypal-payments' ), |
| 1072 | $block_style, |
| 1073 | esc_attr( $button_class ), |
| 1074 | $button_style |
| 1075 | ); |
| 1076 | } |
| 1077 | |
| 1078 | /** |
| 1079 | * Find the cheapest per-option price in the primary dimension. |
| 1080 | * |
| 1081 | * PayPal only prices the primary dimension, so an amount left on any other |
| 1082 | * dimension is not a price a buyer can pay and must not become the headline. |
| 1083 | * |
| 1084 | * @since 0.9.0 |
| 1085 | * |
| 1086 | * @param array|null $variants Variants structure from the block attributes. |
| 1087 | * @return string|null The lowest option price, or null when none are priced. |
| 1088 | */ |
| 1089 | private static function get_lowest_variant_price( $variants ) { |
| 1090 | if ( ! is_array( $variants ) || empty( $variants['dimensions'] ) || ! is_array( $variants['dimensions'] ) ) { |
| 1091 | return null; |
| 1092 | } |
| 1093 | |
| 1094 | $lowest = null; |
| 1095 | |
| 1096 | foreach ( $variants['dimensions'] as $dimension ) { |
| 1097 | if ( empty( $dimension['primary'] ) || empty( $dimension['options'] ) || ! is_array( $dimension['options'] ) ) { |
| 1098 | continue; |
| 1099 | } |
| 1100 | |
| 1101 | foreach ( $dimension['options'] as $option ) { |
| 1102 | $value = is_array( $option ) ? trim( (string) ( $option['unit_amount']['value'] ?? '' ) ) : ''; |
| 1103 | if ( '' === $value || ! is_numeric( $value ) ) { |
| 1104 | continue; |
| 1105 | } |
| 1106 | |
| 1107 | if ( null === $lowest || (float) $value < (float) $lowest ) { |
| 1108 | $lowest = $value; |
| 1109 | } |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | return $lowest; |
| 1114 | } |
| 1115 | |
| 1116 | /** |
| 1117 | * Render a legacy paste-code button (V1 backward compatibility). |
| 1118 | * |
| 1119 | * @param array $attributes The block attributes. |
| 1120 | * @return string|void The rendered button HTML. |
| 1121 | */ |
| 1122 | private static function render_legacy_button( $attributes ) { |
| 1123 | $button_type = $attributes['buttonType'] ?? ''; |
| 1124 | $script_src = $attributes['scriptSrc'] ?? ''; |
| 1125 | $hosted_button_id = $attributes['hostedButtonId'] ?? ''; |
| 1126 | $button_text = $attributes['buttonText'] ?? ''; |
| 1127 | |
| 1128 | if ( empty( $button_type ) || empty( $hosted_button_id ) ) { |
| 1129 | return; |
| 1130 | } |
| 1131 | |
| 1132 | // For stacked buttons, we need both scriptSrc and hostedButtonId |
| 1133 | if ( 'stacked' === $button_type && empty( $script_src ) ) { |
| 1134 | return; |
| 1135 | } |
| 1136 | |
| 1137 | // For single buttons, we need buttonText |
| 1138 | if ( 'single' === $button_type && empty( $button_text ) ) { |
| 1139 | return; |
| 1140 | } |
| 1141 | |
| 1142 | if ( 'stacked' === $button_type ) { |
| 1143 | // Sanitize the script URL to ensure it's from an allowed PayPal domain |
| 1144 | $sanitized_url = self::sanitize_paypal_script_url( $script_src ); |
| 1145 | if ( false === $sanitized_url ) { |
| 1146 | return; |
| 1147 | } |
| 1148 | |
| 1149 | // We can't include the version number here. If we do, it is appended to the URL and causes a 400 response. |
| 1150 | wp_enqueue_script( 'paypal-payment-buttons-block-head', $sanitized_url, array(), null, false ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 1151 | add_filter( |
| 1152 | 'script_loader_tag', |
| 1153 | function ( $tag, $handle, $src ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 1154 | if ( 'paypal-payment-buttons-block-head' === $handle ) { |
| 1155 | // Add namespace to avoid conflicts with other PayPal SDK versions |
| 1156 | if ( false === strpos( $tag, 'data-namespace' ) ) { |
| 1157 | $tag = preg_replace( '/(\s+)src=([\'"])/', '$1 data-namespace="paypal_payment_buttons" src=$2', $tag ); |
| 1158 | } |
| 1159 | // Add partner attribution ID |
| 1160 | if ( false === strpos( $tag, 'data-paypal-partner-attribution-id' ) ) { |
| 1161 | $tag = preg_replace( '/(\s+)src=([\'"])/', '$1 data-paypal-partner-attribution-id="' . self::PAYPAL_PARTNER_ATTRIBUTION_ID . '" src=$2', $tag ); |
| 1162 | } |
| 1163 | } |
| 1164 | return $tag; |
| 1165 | }, |
| 1166 | 10, |
| 1167 | 3 |
| 1168 | ); |
| 1169 | |
| 1170 | // Generate the button HTML and inline script |
| 1171 | $container_id = 'paypal-container-' . $hosted_button_id; |
| 1172 | $button_html = '<div id="' . esc_attr( $container_id ) . '"></div>'; |
| 1173 | |
| 1174 | $inline_script = sprintf( |
| 1175 | '(window.paypal_payment_buttons || window.paypal).HostedButtons({ |
| 1176 | hostedButtonId: %s, |
| 1177 | }).render(%s);', |
| 1178 | wp_json_encode( $hosted_button_id, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ), |
| 1179 | wp_json_encode( '#' . $container_id, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) |
| 1180 | ); |
| 1181 | |
| 1182 | wp_add_inline_script( 'paypal-payment-buttons-block-head', $inline_script ); |
| 1183 | |
| 1184 | return $button_html; |
| 1185 | } |
| 1186 | |
| 1187 | // Single button type - generate the complete form HTML |
| 1188 | if ( 'single' === $button_type ) { |
| 1189 | self::register_hooks(); |
| 1190 | |
| 1191 | $payment_id = esc_attr( $hosted_button_id ); |
| 1192 | $button_text_escaped = esc_attr( $button_text ); |
| 1193 | $action_url = esc_url( 'https://www.paypal.com/ncp/payment/' . $payment_id . '?at_code=' . self::PAYPAL_PARTNER_ATTRIBUTION_ID ); |
| 1194 | |
| 1195 | $button_html = sprintf( |
| 1196 | '<style>.pp-%1$s{text-align:center;border:none;border-radius:0.25rem;min-width:11.625rem;padding:0 2rem;height:2.625rem;font-weight:bold;background-color:#FFD140;color:#000000;font-family:"Helvetica Neue",Arial,sans-serif;font-size:1rem;line-height:1.25rem;cursor:pointer;}</style> |
| 1197 | <div> |
| 1198 | <form action="%2$s" method="post" target="_blank" style="display:inline-grid;justify-items:center;align-content:start;gap:0.5rem;"> |
| 1199 | <input class="pp-%1$s" type="submit" value="%3$s" /> |
| 1200 | <img src="https://www.paypalobjects.com/images/Debit_Credit_APM.svg" alt="cards" /> |
| 1201 | <section style="font-size: 0.75rem;"> Powered by <img src="https://www.paypalobjects.com/paypal-ui/logos/svg/paypal-wordmark-color.svg" alt="paypal" style="height:0.875rem;vertical-align:middle;"/></section> |
| 1202 | </form> |
| 1203 | </div>', |
| 1204 | $payment_id, |
| 1205 | $action_url, |
| 1206 | $button_text_escaped |
| 1207 | ); |
| 1208 | |
| 1209 | return $button_html; |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | /** |
| 1214 | * Load editor styles for the block. |
| 1215 | * These are loaded via enqueue_block_assets to ensure proper loading in the editor iframe context. |
| 1216 | */ |
| 1217 | public static function load_editor_styles() { |
| 1218 | $handle = 'jp-paypal-payments-ncps-blocks'; |
| 1219 | |
| 1220 | Assets::register_script( |
| 1221 | $handle, |
| 1222 | '../../dist/paypal-payment-buttons/editor.js', |
| 1223 | __FILE__, |
| 1224 | array( |
| 1225 | 'css_path' => '../../dist/paypal-payment-buttons/editor.css', |
| 1226 | 'textdomain' => 'jetpack-paypal-payments', |
| 1227 | ) |
| 1228 | ); |
| 1229 | wp_enqueue_style( $handle ); |
| 1230 | } |
| 1231 | |
| 1232 | /** |
| 1233 | * Loads scripts |
| 1234 | */ |
| 1235 | public static function load_editor_scripts() { |
| 1236 | Assets::register_script( |
| 1237 | 'jp-paypal-payments-ncps-blocks', |
| 1238 | '../../dist/paypal-payment-buttons/editor.js', |
| 1239 | __FILE__, |
| 1240 | array( |
| 1241 | 'in_footer' => true, |
| 1242 | 'textdomain' => 'jetpack-paypal-payments', |
| 1243 | 'enqueue' => true, |
| 1244 | // Editor styles are loaded separately, see load_editor_styles(). |
| 1245 | 'css_path' => null, |
| 1246 | ) |
| 1247 | ); |
| 1248 | } |
| 1249 | |
| 1250 | /** |
| 1251 | * Add display to the allowed styles. |
| 1252 | * |
| 1253 | * @see https://developer.wordpress.org/reference/hooks/safe_style_css/ |
| 1254 | * |
| 1255 | * @param array $safe_styles The allowed styles. |
| 1256 | * @return array The allowed styles. |
| 1257 | */ |
| 1258 | public static function add_style_display( array $safe_styles ): array { |
| 1259 | $safe_styles[] = 'display'; |
| 1260 | return $safe_styles; |
| 1261 | } |
| 1262 | |
| 1263 | /** |
| 1264 | * Register hooks (idempotent — safe to call from multiple block renders). |
| 1265 | */ |
| 1266 | public static function register_hooks() { |
| 1267 | static $registered = false; |
| 1268 | if ( $registered ) { |
| 1269 | return; |
| 1270 | } |
| 1271 | $registered = true; |
| 1272 | |
| 1273 | add_filter( 'safe_style_css', array( __CLASS__, 'add_style_display' ) ); |
| 1274 | } |
| 1275 | |
| 1276 | /** |
| 1277 | * Enqueue the QR code frontend script on pages containing the block. |
| 1278 | * |
| 1279 | * Called from render_api_managed_button() so the script is only loaded |
| 1280 | * when a PayPal payment button is actually present on the page. |
| 1281 | * |
| 1282 | * @since 0.9.0 |
| 1283 | * @return void |
| 1284 | */ |
| 1285 | private static function enqueue_qr_script() { |
| 1286 | static $enqueued = false; |
| 1287 | if ( $enqueued ) { |
| 1288 | return; |
| 1289 | } |
| 1290 | $enqueued = true; |
| 1291 | |
| 1292 | Assets::register_script( |
| 1293 | 'jetpack-paypal-qr-code', |
| 1294 | '../../dist/paypal-payment-buttons/qr-code.js', |
| 1295 | __FILE__, |
| 1296 | array( |
| 1297 | 'in_footer' => true, |
| 1298 | 'textdomain' => 'jetpack-paypal-payments', |
| 1299 | 'enqueue' => true, |
| 1300 | ) |
| 1301 | ); |
| 1302 | } |
| 1303 | |
| 1304 | /** |
| 1305 | * Initialize PayPal Payment Buttons API integration hooks. |
| 1306 | * |
| 1307 | * Registers REST API routes for PayPal OAuth connection management |
| 1308 | * and button CRUD operations. |
| 1309 | * |
| 1310 | * @since 0.7.0 |
| 1311 | * @return void |
| 1312 | */ |
| 1313 | public static function init_api() { |
| 1314 | self::init_rest_api(); |
| 1315 | add_action( 'init', array( __CLASS__, 'init_jetpack_sharing' ) ); |
| 1316 | add_action( 'init', array( PayPal_Email_Sender::class, 'maybe_init' ) ); |
| 1317 | } |
| 1318 | |
| 1319 | /** |
| 1320 | * Register just the PayPal REST routes -- the subset the Jetpack loader uses, |
| 1321 | * without init_api()'s sharing and email-sender hookups. |
| 1322 | * |
| 1323 | * @since 0.9.0 |
| 1324 | * @return void |
| 1325 | */ |
| 1326 | public static function init_rest_api() { |
| 1327 | add_action( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ) ); |
| 1328 | } |
| 1329 | |
| 1330 | /** |
| 1331 | * Register the PayPal REST routes when the API-managed buttons are enabled. |
| 1332 | * |
| 1333 | * The flag is read here rather than in init_rest_api() so a filter added |
| 1334 | * after the bootstrap ran still decides. |
| 1335 | * |
| 1336 | * @since 0.9.0 |
| 1337 | * @return void |
| 1338 | */ |
| 1339 | public static function register_rest_routes() { |
| 1340 | if ( ! self::is_api_managed_enabled() ) { |
| 1341 | return; |
| 1342 | } |
| 1343 | |
| 1344 | PayPal_REST_Controller::register_routes(); |
| 1345 | } |
| 1346 | |
| 1347 | /** |
| 1348 | * Integrate with Jetpack Sharing (Sharedaddy) if available. |
| 1349 | * |
| 1350 | * Ensures sharing buttons appear on pages containing the PayPal |
| 1351 | * payment button block. Gracefully no-ops when Jetpack or the |
| 1352 | * Sharedaddy module is not active. |
| 1353 | * |
| 1354 | * @since 0.9.0 |
| 1355 | * @since 0.9.0 Public, runs on `init`, and no-ops unless the API-managed buttons are enabled. |
| 1356 | * @return void |
| 1357 | */ |
| 1358 | public static function init_jetpack_sharing() { |
| 1359 | // Only register if Jetpack + Sharedaddy are active. |
| 1360 | if ( |
| 1361 | ! self::is_api_managed_enabled() |
| 1362 | || ! class_exists( 'Jetpack' ) |
| 1363 | || ! method_exists( 'Jetpack', 'is_module_active' ) |
| 1364 | || ! \Jetpack::is_module_active( 'sharedaddy' ) |
| 1365 | ) { |
| 1366 | return; |
| 1367 | } |
| 1368 | |
| 1369 | add_filter( 'sharing_show', array( __CLASS__, 'enable_sharing_on_payment_pages' ), 10, 2 ); |
| 1370 | } |
| 1371 | |
| 1372 | /** |
| 1373 | * Enable Jetpack Sharing buttons on pages containing the PayPal block. |
| 1374 | * |
| 1375 | * Callback for the 'sharing_show' filter. Returns true if the current |
| 1376 | * post contains the PayPal payment buttons block, otherwise passes |
| 1377 | * through the existing value unchanged. |
| 1378 | * |
| 1379 | * @param bool $show Whether to show sharing buttons. |
| 1380 | * @param \WP_Post $post The current post object. |
| 1381 | * @return bool Whether to show sharing buttons. |
| 1382 | */ |
| 1383 | public static function enable_sharing_on_payment_pages( $show, $post = null ) { |
| 1384 | if ( $show ) { |
| 1385 | return $show; // Already enabled by another filter — don't interfere. |
| 1386 | } |
| 1387 | |
| 1388 | if ( ! $post instanceof \WP_Post ) { |
| 1389 | return $show; |
| 1390 | } |
| 1391 | |
| 1392 | if ( has_block( 'jetpack/paypal-payment-buttons', $post ) ) { |
| 1393 | return true; |
| 1394 | } |
| 1395 | |
| 1396 | return $show; |
| 1397 | } |
| 1398 | |
| 1399 | /** |
| 1400 | * Initialize admin dashboard hooks. |
| 1401 | * |
| 1402 | * Registers the Payment Links admin page for managing |
| 1403 | * all merchant payment links from wp-admin. |
| 1404 | * |
| 1405 | * @since 0.9.0 |
| 1406 | * @since 0.9.0 Defers to `init` and no-ops unless the API-managed buttons are enabled. |
| 1407 | */ |
| 1408 | public static function init_admin() { |
| 1409 | add_action( |
| 1410 | 'init', |
| 1411 | static function () { |
| 1412 | // Read the flag before naming the classes, so neither is autoloaded while it is off. |
| 1413 | if ( ! self::is_api_managed_enabled() ) { |
| 1414 | return; |
| 1415 | } |
| 1416 | |
| 1417 | PayPal_Admin_Page::maybe_init(); |
| 1418 | PayPal_Email_Sender::maybe_init(); |
| 1419 | } |
| 1420 | ); |
| 1421 | } |
| 1422 | } |