Accordion
1 year ago
AddToCartWithOptions
1 week ago
OrderConfirmation
2 months ago
ProductCollection
3 weeks ago
Reviews
2 months ago
AbstractBlock.php
4 weeks ago
AbstractDynamicBlock.php
2 years ago
AbstractInnerBlock.php
1 year ago
AbstractProductGrid.php
3 months ago
ActiveFilters.php
2 years ago
AddToCartForm.php
4 weeks ago
AddToWishlistButton.php
4 weeks ago
AllProducts.php
2 years ago
AllReviews.php
2 years ago
AtomicBlock.php
2 years ago
AttributeFilter.php
1 year ago
Breadcrumbs.php
1 year ago
Cart.php
2 months ago
CartAcceptedPaymentMethodsBlock.php
2 years ago
CartCrossSellsBlock.php
2 years ago
CartCrossSellsProductsBlock.php
2 years ago
CartExpressPaymentBlock.php
1 year ago
CartItemsBlock.php
2 years ago
CartLineItemsBlock.php
2 years ago
CartLink.php
2 months ago
CartOrderSummaryBlock.php
2 years ago
CartOrderSummaryCouponFormBlock.php
2 years ago
CartOrderSummaryDiscountBlock.php
2 years ago
CartOrderSummaryFeeBlock.php
2 years ago
CartOrderSummaryHeadingBlock.php
2 years ago
CartOrderSummaryShippingBlock.php
2 years ago
CartOrderSummarySubtotalBlock.php
2 years ago
CartOrderSummaryTaxesBlock.php
2 years ago
CartOrderSummaryTotalsBlock.php
2 years ago
CartTotalsBlock.php
2 years ago
CatalogSorting.php
4 weeks ago
CategoryDescription.php
2 months ago
CategoryTitle.php
2 months ago
Checkout.php
2 months ago
CheckoutActionsBlock.php
1 year ago
CheckoutAdditionalInformationBlock.php
2 years ago
CheckoutBillingAddressBlock.php
2 years ago
CheckoutContactInformationBlock.php
2 years ago
CheckoutExpressPaymentBlock.php
9 months ago
CheckoutFieldsBlock.php
2 years ago
CheckoutOrderNoteBlock.php
2 years ago
CheckoutOrderSummaryBlock.php
2 years ago
CheckoutOrderSummaryCartItemsBlock.php
2 years ago
CheckoutOrderSummaryCouponFormBlock.php
2 years ago
CheckoutOrderSummaryDiscountBlock.php
2 years ago
CheckoutOrderSummaryFeeBlock.php
2 years ago
CheckoutOrderSummaryShippingBlock.php
2 years ago
CheckoutOrderSummarySubtotalBlock.php
2 years ago
CheckoutOrderSummaryTaxesBlock.php
2 years ago
CheckoutOrderSummaryTotalsBlock.php
2 years ago
CheckoutPaymentBlock.php
2 years ago
CheckoutPickupOptionsBlock.php
2 years ago
CheckoutShippingAddressBlock.php
2 years ago
CheckoutShippingMethodBlock.php
2 years ago
CheckoutShippingMethodsBlock.php
2 years ago
CheckoutTermsBlock.php
2 years ago
CheckoutTotalsBlock.php
2 years ago
ClassicShortcode.php
1 year ago
ClassicTemplate.php
4 months ago
ComingSoon.php
1 year ago
CouponCode.php
2 months ago
CustomerAccount.php
3 months ago
Dropdown.php
4 weeks ago
EmailContent.php
5 months ago
EmptyCartBlock.php
2 years ago
EmptyMiniCartContentsBlock.php
11 months ago
EnableBlockJsonAssetsTrait.php
1 year ago
FeaturedCategory.php
7 months ago
FeaturedItem.php
7 months ago
FeaturedProduct.php
7 months ago
FilledCartBlock.php
2 years ago
FilledMiniCartContentsBlock.php
4 months ago
FilterWrapper.php
2 years ago
HandpickedProducts.php
1 year ago
MiniCart.php
4 weeks ago
MiniCartCartButtonBlock.php
11 months ago
MiniCartCheckoutButtonBlock.php
11 months ago
MiniCartContents.php
3 months ago
MiniCartFooterBlock.php
4 weeks ago
MiniCartItemsBlock.php
11 months ago
MiniCartProductsTableBlock.php
4 weeks ago
MiniCartShoppingButtonBlock.php
9 months ago
MiniCartTitleBlock.php
11 months ago
MiniCartTitleItemsCounterBlock.php
11 months ago
MiniCartTitleLabelBlock.php
11 months ago
NextPreviousButtons.php
5 months ago
PageContentWrapper.php
2 years ago
PaymentMethodIcons.php
11 months ago
PriceFilter.php
1 year ago
ProceedToCheckoutBlock.php
2 years ago
ProductAverageRating.php
1 year ago
ProductBestSellers.php
2 years ago
ProductButton.php
2 months ago
ProductCategories.php
4 weeks ago
ProductCategory.php
2 years ago
ProductDescription.php
9 months ago
ProductDetails.php
5 months ago
ProductFilterActive.php
4 weeks ago
ProductFilterAttribute.php
4 weeks ago
ProductFilterCheckboxList.php
4 weeks ago
ProductFilterChips.php
4 weeks ago
ProductFilterClearButton.php
4 weeks ago
ProductFilterPrice.php
4 weeks ago
ProductFilterPriceSlider.php
4 weeks ago
ProductFilterRating.php
4 weeks ago
ProductFilterRemovableChips.php
4 weeks ago
ProductFilterStatus.php
4 weeks ago
ProductFilterTaxonomy.php
4 weeks ago
ProductFilters.php
3 weeks ago
ProductGallery.php
4 weeks ago
ProductGalleryLargeImage.php
4 weeks ago
ProductGalleryThumbnails.php
4 weeks ago
ProductImage.php
4 weeks ago
ProductImageGallery.php
7 months ago
ProductMeta.php
1 year ago
ProductNew.php
2 years ago
ProductOnSale.php
2 years ago
ProductPrice.php
2 months ago
ProductQuery.php
4 weeks ago
ProductRating.php
1 year ago
ProductRatingCounter.php
1 year ago
ProductRatingStars.php
1 year ago
ProductResultsCount.php
2 months ago
ProductSKU.php
2 months ago
ProductSaleBadge.php
1 year ago
ProductSearch.php
10 months ago
ProductSpecifications.php
2 months ago
ProductStockIndicator.php
2 months ago
ProductSummary.php
7 months ago
ProductTag.php
2 years ago
ProductTemplate.php
2 months ago
ProductTitle.php
1 year ago
ProductTopRated.php
2 years ago
ProductsByAttribute.php
2 years ago
RatingFilter.php
2 years ago
RelatedProducts.php
3 months ago
ReviewsByCategory.php
2 years ago
ReviewsByProduct.php
2 years ago
SavedForLater.php
4 weeks ago
SingleProduct.php
2 months ago
StockFilter.php
2 years ago
StoreNotices.php
1 year ago
Wishlist.php
4 weeks ago
PaymentMethodIcons.php
227 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Blocks\BlockTypes; |
| 5 | |
| 6 | use WP_Block; |
| 7 | use Automattic\WooCommerce\Blocks\Utils\StyleAttributesUtils; |
| 8 | |
| 9 | /** |
| 10 | * PaymentMethodIcons class. |
| 11 | */ |
| 12 | class PaymentMethodIcons extends AbstractBlock { |
| 13 | /** |
| 14 | * Block name. |
| 15 | * |
| 16 | * @var string |
| 17 | */ |
| 18 | protected $block_name = 'payment-method-icons'; |
| 19 | |
| 20 | /** |
| 21 | * Get the frontend script handle for this block type. |
| 22 | * |
| 23 | * @param string $key Data to get, or default to everything. |
| 24 | * @return array|string |
| 25 | */ |
| 26 | protected function get_block_type_script( $key = null ) { |
| 27 | return null; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Get the frontend style handle for this block type. |
| 32 | * |
| 33 | * @return string[] |
| 34 | */ |
| 35 | protected function get_block_type_style() { |
| 36 | return array_merge( parent::get_block_type_style(), [ 'wc-blocks-packages-style' ] ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Extra data passed through from server to client for block. |
| 41 | * |
| 42 | * @param array $attributes Any attributes that currently are available from the block. |
| 43 | */ |
| 44 | protected function enqueue_data( array $attributes = [] ) { |
| 45 | parent::enqueue_data( $attributes ); |
| 46 | $this->asset_data_registry->add( 'availablePaymentMethods', $this->get_available_payment_methods() ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Render the block. |
| 51 | * |
| 52 | * @param array $attributes Block attributes. |
| 53 | * @param string $content Block content. |
| 54 | * @param WP_Block $block Block instance. |
| 55 | * @return string Rendered block type output. |
| 56 | */ |
| 57 | protected function render( $attributes, $content, $block ) { |
| 58 | $payment_methods = $this->get_available_payment_methods(); |
| 59 | |
| 60 | if ( empty( $payment_methods ) ) { |
| 61 | return ''; |
| 62 | } |
| 63 | |
| 64 | $classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes ); |
| 65 | $wrapper_attributes = get_block_wrapper_attributes( |
| 66 | array( |
| 67 | 'class' => esc_attr( $classes_and_styles['classes'] ), |
| 68 | 'style' => esc_attr( $classes_and_styles['styles'] ), |
| 69 | ) |
| 70 | ); |
| 71 | |
| 72 | $output = '<div ' . $wrapper_attributes . '>'; |
| 73 | $output .= '<div class="wc-block-payment-method-icons">'; |
| 74 | $output .= $this->render_payment_method_icons( $attributes ); |
| 75 | $output .= '</div>'; |
| 76 | $output .= '</div>'; |
| 77 | |
| 78 | return $output; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Render payment method icons. |
| 83 | * |
| 84 | * @param array $attributes Block attributes. |
| 85 | * @return string Rendered block type output. |
| 86 | */ |
| 87 | private function render_payment_method_icons( $attributes ) { |
| 88 | $output = ''; |
| 89 | |
| 90 | $all_payment_methods = $this->get_available_payment_methods(); |
| 91 | $number_of_icons = $attributes['numberOfIcons'] ?? 0; |
| 92 | |
| 93 | if ( 0 === $number_of_icons ) { |
| 94 | $number_of_icons = count( $all_payment_methods ); |
| 95 | } else { |
| 96 | $number_of_icons = max( 0, min( intval( $number_of_icons ), count( $all_payment_methods ) ) ); |
| 97 | } |
| 98 | |
| 99 | if ( ! empty( $all_payment_methods ) ) { |
| 100 | for ( $i = 0; $i < $number_of_icons; $i++ ) { |
| 101 | $payment_method = $all_payment_methods[ $i ]; |
| 102 | $output .= '<div class="wc-block-payment-method-icons__item">'; |
| 103 | $output .= '<span class="wc-block-payment-method-icons__icon" style="background-image: url(\'' . \esc_url( $payment_method['icon'] ) . '\');" role="img" aria-label="' . \esc_attr( $payment_method['name'] ) . '"></span>'; |
| 104 | $output .= '</div>'; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return $output; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Check if WooPayments is enabled. |
| 113 | * |
| 114 | * @return bool WooPayments enabled. |
| 115 | */ |
| 116 | private function is_woopayments_enabled() { |
| 117 | $payment_gateways = WC()->payment_gateways->get_available_payment_gateways(); |
| 118 | |
| 119 | return isset( $payment_gateways['woocommerce_payments'] ) && 'yes' === $payment_gateways['woocommerce_payments']->enabled; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Get the enabled card types for WooPayments. |
| 124 | * |
| 125 | * Note: This uses hardcoded cards based on the default card types provided by WooPayments. This should be updated when these icons can be accessed via an API. |
| 126 | * |
| 127 | * @return array Enabled card types. |
| 128 | */ |
| 129 | private function get_enabled_card_types() { |
| 130 | if ( ! $this->is_woopayments_enabled() ) { |
| 131 | return array(); |
| 132 | } |
| 133 | |
| 134 | $card_types = array( |
| 135 | 'visa' => array( |
| 136 | 'name' => 'Visa', |
| 137 | 'icon' => $this->get_card_type_icon_url( 'visa' ), |
| 138 | ), |
| 139 | 'mastercard' => array( |
| 140 | 'name' => 'Mastercard', |
| 141 | 'icon' => $this->get_card_type_icon_url( 'mastercard' ), |
| 142 | ), |
| 143 | 'amex' => array( |
| 144 | 'name' => 'American Express', |
| 145 | 'icon' => $this->get_card_type_icon_url( 'amex' ), |
| 146 | ), |
| 147 | 'discover' => array( |
| 148 | 'name' => 'Discover', |
| 149 | 'icon' => $this->get_card_type_icon_url( 'discover' ), |
| 150 | ), |
| 151 | 'jcb' => array( |
| 152 | 'name' => 'JCB', |
| 153 | 'icon' => $this->get_card_type_icon_url( 'jcb' ), |
| 154 | ), |
| 155 | ); |
| 156 | |
| 157 | return $card_types; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Get the card type icon URL. |
| 162 | * |
| 163 | * @param string $card_type Card type. |
| 164 | * @return string Card type icon URL. |
| 165 | */ |
| 166 | private function get_card_type_icon_url( $card_type ) { |
| 167 | $assets_path = 'assets/images/payment-methods-cards/'; |
| 168 | $icon_path = WC_ABSPATH . $assets_path . $card_type . '.svg'; |
| 169 | $icon_url = \plugins_url( $assets_path . $card_type . '.svg', WC_PLUGIN_FILE ); |
| 170 | |
| 171 | return file_exists( $icon_path ) ? $icon_url : ''; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Get other payment method icons from available gateways. |
| 176 | * |
| 177 | * @return array Other payment method icons. |
| 178 | */ |
| 179 | private function get_other_payment_method_icons() { |
| 180 | $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); |
| 181 | $other_payment_methods = array(); |
| 182 | |
| 183 | if ( empty( $available_gateways ) ) { |
| 184 | return $other_payment_methods; |
| 185 | } |
| 186 | |
| 187 | foreach ( $available_gateways as $gateway ) { |
| 188 | if ( 'yes' === $gateway->enabled ) { |
| 189 | if ( 'woocommerce_payments' === $gateway->id ) { |
| 190 | continue; |
| 191 | } |
| 192 | |
| 193 | $icon_url = ''; |
| 194 | if ( is_callable( array( $gateway, 'get_icon_url' ) ) ) { |
| 195 | $icon_url = $gateway->get_icon_url(); |
| 196 | } |
| 197 | if ( ! empty( $icon_url ) ) { |
| 198 | $other_payment_methods[] = array( |
| 199 | 'name' => $gateway->get_title(), |
| 200 | 'icon' => $icon_url, |
| 201 | ); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | usort( |
| 207 | $other_payment_methods, |
| 208 | function ( $a, $b ) { |
| 209 | return strcmp( $a['name'], $b['name'] ); |
| 210 | } |
| 211 | ); |
| 212 | |
| 213 | return $other_payment_methods; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Get the available payment methods. |
| 218 | * |
| 219 | * @return array Available payment methods. |
| 220 | */ |
| 221 | private function get_available_payment_methods() { |
| 222 | $enabled_cards = array_values( $this->get_enabled_card_types() ); |
| 223 | $payment_methods = array_merge( $enabled_cards, $this->get_other_payment_method_icons() ); |
| 224 | return $payment_methods; |
| 225 | } |
| 226 | } |
| 227 |