Submit
Path:
~
/
home
/
getwphos
/
www
/
brueggemannfh
/
wp-content
/
plugins
/
woocommerce
/
src
/
Blocks
/
BlockTypes
/
File Content:
CouponCode.php
<?php declare(strict_types=1); namespace Automattic\WooCommerce\Blocks\BlockTypes; use Automattic\WooCommerce\EmailEditor\Email_Editor_Container; use Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Rendering_Context; use Automattic\WooCommerce\EmailEditor\Engine\Theme_Controller; use Automattic\WooCommerce\EmailEditor\Integrations\Utils\Styles_Helper; use Automattic\WooCommerce\EmailEditor\Integrations\Utils\Table_Wrapper_Helper; use WP_Block; /** * CouponCode block for displaying coupon codes in emails. * * @since 10.5.0 */ class CouponCode extends AbstractBlock { /** * Block name. * * @var string */ protected $block_name = 'coupon-code'; /** * Default styles for the coupon code element. */ private const DEFAULT_STYLES = array( 'font-size' => '1.2em', 'padding' => '12px 20px', 'display' => 'inline-block', 'border' => '2px dashed #cccccc', 'border-radius' => '4px', 'box-sizing' => 'border-box', 'color' => '#000000', 'background-color' => '#f5f5f5', 'text-align' => 'center', 'font-weight' => 'bold', 'letter-spacing' => '1px', ); /** * Get the editor script handle for this block type. * * @param string|null $key Data to get. Valid keys: "handle", "path", "dependencies". * @return array|string|null */ protected function get_block_type_editor_script( $key = null ) { $script = array( 'handle' => 'wc-' . $this->block_name . '-block', 'path' => $this->asset_api->get_block_asset_build_path( $this->block_name ), 'dependencies' => array( 'wc-blocks' ), ); return null === $key ? $script : ( $script[ $key ] ?? null ); } /** * Get the frontend style handle for this block type. * * @return null */ protected function get_block_type_style() { return null; } /** * Render the coupon code block. * * @param array $attributes Block attributes. * @param string $content Block content. * @param WP_Block|null $block Block instance. * @return string */ protected function render( $attributes, $content, $block ) { $parsed_block = $block instanceof WP_Block ? $block->parsed_block : array(); $attributes = $this->get_block_attributes( $parsed_block, $attributes ); $coupon_code = $this->get_coupon_code( $attributes ); if ( empty( $coupon_code ) ) { return ''; } $rendering_context = $this->get_rendering_context( $block ); $coupon_html = $this->build_coupon_html( $coupon_code, $attributes, $rendering_context ); return $this->wrap_for_email( $coupon_html, $parsed_block ); } /** * Get block attributes from parsed block or fallback. * * @param array $parsed_block Parsed block data. * @param array $fallback Fallback attributes. * @return array */ private function get_block_attributes( array $parsed_block, $fallback ): array { $attributes = $parsed_block['attrs'] ?? $fallback ?? array(); return is_array( $attributes ) ? $attributes : array(); } /** * Extract coupon code from attributes. * * @param array $attributes Block attributes. * @return string */ private function get_coupon_code( array $attributes ): string { $coupon_code = $attributes['couponCode'] ?? ''; return is_string( $coupon_code ) ? $coupon_code : ''; } /** * Get rendering context from block or create a new one. * * @param WP_Block|null $block Block instance. * @return Rendering_Context */ private function get_rendering_context( $block ): Rendering_Context { if ( $block instanceof WP_Block && isset( $block->context['renderingContext'] ) && $block->context['renderingContext'] instanceof Rendering_Context ) { return $block->context['renderingContext']; } $theme_controller = Email_Editor_Container::container()->get( Theme_Controller::class ); return new Rendering_Context( $theme_controller->get_theme(), array() ); } /** * Build the coupon code HTML element with styles. * * @param string $coupon_code Coupon code text. * @param array $attributes Block attributes. * @param Rendering_Context $rendering_context Rendering context for style resolution. * @return string */ private function build_coupon_html( string $coupon_code, array $attributes, Rendering_Context $rendering_context ): string { $block_styles = Styles_Helper::get_block_styles( $attributes, $rendering_context, array( 'border', 'background-color', 'color', 'typography', 'spacing' ) ); $declarations = $block_styles['declarations'] ?? array(); if ( ! $this->has_valid_background_color( $declarations ) ) { $declarations['background-color'] = $this->resolve_background_color( $attributes, $rendering_context ); } $merged_styles = array_merge( self::DEFAULT_STYLES, $declarations ); $css = \WP_Style_Engine::compile_css( $merged_styles, '' ); return sprintf( '<span class="woocommerce-coupon-code" style="%s">%s</span>', esc_attr( $css ), esc_html( $coupon_code ) ); } /** * Check if declarations contain a valid CSS background color. * * @param array $declarations CSS declarations. * @return bool */ private function has_valid_background_color( array $declarations ): bool { if ( empty( $declarations['background-color'] ) ) { return false; } return $this->is_css_color_value( $declarations['background-color'] ); } /** * Resolve background color from attributes, translating color slugs if needed. * * @param array $attributes Block attributes. * @param Rendering_Context $rendering_context Rendering context. * @return string Resolved color value or default. */ private function resolve_background_color( array $attributes, Rendering_Context $rendering_context ): string { if ( empty( $attributes['backgroundColor'] ) ) { return self::DEFAULT_STYLES['background-color']; } $color_slug = $attributes['backgroundColor']; // Try to get color from normalized styles (handles slug translation). $normalized = Styles_Helper::get_normalized_block_styles( $attributes, $rendering_context ); $color = $normalized['color']['background'] ?? ''; if ( $this->is_css_color_value( $color ) ) { return $color; } // Fallback: try direct translation if normalization returned the slug unchanged. $translated = $rendering_context->translate_slug_to_color( $color_slug ); if ( $this->is_css_color_value( $translated ) ) { return $translated; } return self::DEFAULT_STYLES['background-color']; } /** * Check if a string is a valid CSS color value (hex, rgb, or hsl). * * @param string $value Value to check. * @return bool */ private function is_css_color_value( string $value ): bool { return str_starts_with( $value, '#' ) || str_starts_with( $value, 'rgb' ) || str_starts_with( $value, 'hsl' ); } /** * Wrap coupon HTML in an email-compatible table structure. * * @param string $coupon_html Coupon HTML content. * @param array $parsed_block Parsed block data. * @return string */ private function wrap_for_email( string $coupon_html, array $parsed_block ): string { $align = $this->get_alignment( $parsed_block ); $table_attrs = array( 'style' => \WP_Style_Engine::compile_css( array( 'border-collapse' => 'collapse', 'width' => '100%', ), '' ), 'width' => '100%', ); $cell_attrs = array( 'class' => 'email-coupon-code-cell', 'style' => \WP_Style_Engine::compile_css( array( 'padding' => '10px 0', 'text-align' => $align, ), '' ), 'align' => $align, ); return Table_Wrapper_Helper::render_table_wrapper( $coupon_html, $table_attrs, $cell_attrs ); } /** * Get alignment from parsed block attributes. * * @param array $parsed_block Parsed block data. * @return string */ private function get_alignment( array $parsed_block ): string { $allowed = array( 'left', 'center', 'right' ); $align = $parsed_block['attrs']['align'] ?? 'center'; if ( ! is_string( $align ) || ! in_array( $align, $allowed, true ) ) { return 'center'; } return $align; } }
Edit
Rename
Chmod
Delete
FILE
FOLDER
Name
Size
Permission
Action
Accordion
---
0755
AddToCartWithOptions
---
0755
OrderConfirmation
---
0755
ProductCollection
---
0755
Reviews
---
0755
AbstractBlock.php
15979 bytes
0644
AbstractDynamicBlock.php
1892 bytes
0644
AbstractInnerBlock.php
1785 bytes
0644
AbstractProductGrid.php
22632 bytes
0644
ActiveFilters.php
226 bytes
0644
AddToCartForm.php
9507 bytes
0644
AllProducts.php
2040 bytes
0644
AllReviews.php
1317 bytes
0644
AtomicBlock.php
910 bytes
0644
AttributeFilter.php
1536 bytes
0644
Breadcrumbs.php
2761 bytes
0644
Cart.php
12603 bytes
0644
CartAcceptedPaymentMethodsBlock.php
288 bytes
0644
CartCrossSellsBlock.php
251 bytes
0644
CartCrossSellsProductsBlock.php
276 bytes
0644
CartExpressPaymentBlock.php
507 bytes
0644
CartItemsBlock.php
235 bytes
0644
CartLineItemsBlock.php
248 bytes
0644
CartLink.php
1588 bytes
0644
CartOrderSummaryBlock.php
2736 bytes
0644
CartOrderSummaryCouponFormBlock.php
289 bytes
0644
CartOrderSummaryDiscountBlock.php
282 bytes
0644
CartOrderSummaryFeeBlock.php
267 bytes
0644
CartOrderSummaryHeadingBlock.php
279 bytes
0644
CartOrderSummaryShippingBlock.php
282 bytes
0644
CartOrderSummarySubtotalBlock.php
282 bytes
0644
CartOrderSummaryTaxesBlock.php
273 bytes
0644
CartOrderSummaryTotalsBlock.php
276 bytes
0644
CartTotalsBlock.php
238 bytes
0644
CatalogSorting.php
1518 bytes
0644
CategoryDescription.php
1743 bytes
0644
CategoryTitle.php
2412 bytes
0644
Checkout.php
27134 bytes
0644
CheckoutActionsBlock.php
1003 bytes
0644
CheckoutAdditionalInformationBlock.php
296 bytes
0644
CheckoutBillingAddressBlock.php
275 bytes
0644
CheckoutContactInformationBlock.php
287 bytes
0644
CheckoutExpressPaymentBlock.php
4232 bytes
0644
CheckoutFieldsBlock.php
250 bytes
0644
CheckoutOrderNoteBlock.php
260 bytes
0644
CheckoutOrderSummaryBlock.php
2807 bytes
0644
CheckoutOrderSummaryCartItemsBlock.php
298 bytes
0644
CheckoutOrderSummaryCouponFormBlock.php
301 bytes
0644
CheckoutOrderSummaryDiscountBlock.php
294 bytes
0644
CheckoutOrderSummaryFeeBlock.php
279 bytes
0644
CheckoutOrderSummaryShippingBlock.php
294 bytes
0644
CheckoutOrderSummarySubtotalBlock.php
294 bytes
0644
CheckoutOrderSummaryTaxesBlock.php
285 bytes
0644
CheckoutOrderSummaryTotalsBlock.php
288 bytes
0644
CheckoutPaymentBlock.php
253 bytes
0644
CheckoutPickupOptionsBlock.php
272 bytes
0644
CheckoutShippingAddressBlock.php
278 bytes
0644
CheckoutShippingMethodBlock.php
275 bytes
0644
CheckoutShippingMethodsBlock.php
278 bytes
0644
CheckoutTermsBlock.php
247 bytes
0644
CheckoutTotalsBlock.php
250 bytes
0644
ClassicShortcode.php
3105 bytes
0644
ClassicTemplate.php
12937 bytes
0644
ComingSoon.php
3670 bytes
0644
CouponCode.php
8163 bytes
0644
CustomerAccount.php
8790 bytes
0644
EmailContent.php
3311 bytes
0644
EmptyCartBlock.php
235 bytes
0644
EmptyMiniCartContentsBlock.php
1834 bytes
0644
EnableBlockJsonAssetsTrait.php
775 bytes
0644
FeaturedCategory.php
2781 bytes
0644
FeaturedItem.php
14212 bytes
0644
FeaturedProduct.php
3406 bytes
0644
FilledCartBlock.php
238 bytes
0644
FilledMiniCartContentsBlock.php
3329 bytes
0644
FilterWrapper.php
376 bytes
0644
HandpickedProducts.php
1936 bytes
0644
MiniCart.php
35466 bytes
0644
MiniCartCartButtonBlock.php
2296 bytes
0644
MiniCartCheckoutButtonBlock.php
2016 bytes
0644
MiniCartContents.php
6671 bytes
0644
MiniCartFooterBlock.php
5762 bytes
0644
MiniCartItemsBlock.php
1548 bytes
0644
MiniCartProductsTableBlock.php
13624 bytes
0644
MiniCartShoppingButtonBlock.php
2073 bytes
0644
MiniCartTitleBlock.php
1534 bytes
0644
MiniCartTitleItemsCounterBlock.php
2331 bytes
0644
MiniCartTitleLabelBlock.php
1587 bytes
0644
NextPreviousButtons.php
3438 bytes
0644
PageContentWrapper.php
705 bytes
0644
PaymentMethodIcons.php
6319 bytes
0644
PriceFilter.php
1275 bytes
0644
ProceedToCheckoutBlock.php
783 bytes
0644
ProductAverageRating.php
2130 bytes
0644
ProductBestSellers.php
444 bytes
0644
ProductButton.php
12711 bytes
0644
ProductCategories.php
13059 bytes
0644
ProductCategory.php
733 bytes
0644
ProductDescription.php
2541 bytes
0644
ProductDetails.php
16419 bytes
0644
ProductFilterActive.php
2567 bytes
0644
ProductFilterAttribute.php
13041 bytes
0644
ProductFilterCheckboxList.php
5782 bytes
0644
ProductFilterChips.php
4801 bytes
0644
ProductFilterClearButton.php
1488 bytes
0644
ProductFilterPrice.php
7135 bytes
0644
ProductFilterPriceSlider.php
5485 bytes
0644
ProductFilterRating.php
7270 bytes
0644
ProductFilterRemovableChips.php
3879 bytes
0644
ProductFilterStatus.php
6782 bytes
0644
ProductFilterTaxonomy.php
15966 bytes
0644
ProductFilters.php
9954 bytes
0644
ProductGallery.php
6775 bytes
0644
ProductGalleryLargeImage.php
8145 bytes
0644
ProductGalleryThumbnails.php
4278 bytes
0644
ProductImage.php
8207 bytes
0644
ProductImageGallery.php
3638 bytes
0644
ProductMeta.php
1009 bytes
0644
ProductNew.php
448 bytes
0644
ProductOnSale.php
761 bytes
0644
ProductPrice.php
4184 bytes
0644
ProductQuery.php
28484 bytes
0644
ProductRating.php
6240 bytes
0644
ProductRatingCounter.php
5593 bytes
0644
ProductRatingStars.php
3855 bytes
0644
ProductResultsCount.php
1812 bytes
0644
ProductSKU.php
3391 bytes
0644
ProductSaleBadge.php
2753 bytes
0644
ProductSearch.php
3569 bytes
0644
ProductSpecifications.php
5817 bytes
0644
ProductStockIndicator.php
6104 bytes
0644
ProductSummary.php
7512 bytes
0644
ProductTag.php
2284 bytes
0644
ProductTemplate.php
5929 bytes
0644
ProductTitle.php
619 bytes
0644
ProductTopRated.php
424 bytes
0644
ProductsByAttribute.php
2104 bytes
0644
RatingFilter.php
704 bytes
0644
RelatedProducts.php
4878 bytes
0644
ReviewsByCategory.php
1339 bytes
0644
ReviewsByProduct.php
1336 bytes
0644
SingleProduct.php
6756 bytes
0644
StockFilter.php
1370 bytes
0644
StoreNotices.php
1742 bytes
0644
N4ST4R_ID | Naxtarrr