BlockEditing
3 days ago
Convert
3 days ago
Duplicate
3 days ago
Management
3 days ago
Timestamps
3 days ago
Tracker
3 days ago
views
3 days ago
CommonMethodSettings.php
3 days ago
FreeShippingCalculator.php
3 days ago
FreeShippingThresholdRuleValidator.php
3 days ago
MethodDescription.php
3 days ago
MethodLogo.php
3 days ago
MethodLogoCheckoutBlocksAssets.php
3 days ago
MethodLogoSettingsField.php
3 days ago
MethodSettings.php
3 days ago
MethodTitle.php
3 days ago
RateCalculator.php
3 days ago
RateCalculatorFactory.php
3 days ago
SettingsDisplayPreparer.php
3 days ago
SettingsProcessor.php
3 days ago
SingleMethodSettings.php
3 days ago
MethodLogo.php
62 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class MethodLogo |
| 4 | * |
| 5 | * @package WPDesk\FS\TableRate\ShippingMethod |
| 6 | */ |
| 7 | |
| 8 | namespace WPDesk\FS\TableRate\ShippingMethod; |
| 9 | |
| 10 | /** |
| 11 | * Resolves shipping method logo data from method settings. |
| 12 | */ |
| 13 | class MethodLogo { |
| 14 | |
| 15 | /** |
| 16 | * @param array $method_settings . |
| 17 | * |
| 18 | * @return int |
| 19 | */ |
| 20 | public static function get_attachment_id( array $method_settings ) { |
| 21 | return absint( $method_settings[ CommonMethodSettings::METHOD_LOGO_ID ] ?? 0 ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @param array $method_settings . |
| 26 | * |
| 27 | * @return array{url:string,alt:string} |
| 28 | */ |
| 29 | public static function get_logo_data( array $method_settings ) { |
| 30 | $attachment_id = self::get_attachment_id( $method_settings ); |
| 31 | |
| 32 | if ( 0 === $attachment_id || ! wp_attachment_is_image( $attachment_id ) ) { |
| 33 | return [ |
| 34 | 'url' => '', |
| 35 | 'alt' => '', |
| 36 | ]; |
| 37 | } |
| 38 | |
| 39 | $image = wp_get_attachment_image_src( $attachment_id, 'thumbnail' ); |
| 40 | |
| 41 | if ( ! is_array( $image ) || empty( $image[0] ) ) { |
| 42 | return [ |
| 43 | 'url' => '', |
| 44 | 'alt' => '', |
| 45 | ]; |
| 46 | } |
| 47 | |
| 48 | $alt = trim( (string) get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ); |
| 49 | |
| 50 | if ( '' === $alt ) { |
| 51 | $alt = wp_strip_all_tags( |
| 52 | wpdesk__( $method_settings[ CommonMethodSettings::METHOD_TITLE ] ?? '', 'flexible-shipping' ) |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | return [ |
| 57 | 'url' => (string) $image[0], |
| 58 | 'alt' => $alt, |
| 59 | ]; |
| 60 | } |
| 61 | } |
| 62 |