| 1 |
<?php |
| 2 |
/** |
| 3 |
* Inactive Tab Message Resolver. |
| 4 |
* |
| 5 |
* Pure logic class — no WordPress dependencies. |
| 6 |
* Prepares data for JS localization, selects messages, and interpolates variables. |
| 7 |
* |
| 8 |
* @package Merchant |
| 9 |
*/ |
| 10 |
|
| 11 |
declare(strict_types=1); |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Merchant_Inactive_Tab_Message_Resolver |
| 19 |
*/ |
| 20 |
class Merchant_Inactive_Tab_Message_Resolver { |
| 21 |
|
| 22 |
/** |
| 23 |
* Prepare data array for JavaScript localization. |
| 24 |
* |
| 25 |
* @param array<string, mixed> $settings Module settings from get_module_settings(). |
| 26 |
* @param int $cart_count Cart item count. |
| 27 |
* @param string $cart_total Formatted cart total (with currency). |
| 28 |
* @param string $site_name Site name from get_bloginfo(). |
| 29 |
* |
| 30 |
* @return array<string, mixed> Keyed data to merge into merchant.setting JS object. |
| 31 |
*/ |
| 32 |
public function prepare_localized_data( array $settings, int $cart_count, string $cart_total, string $site_name ): array { |
| 33 |
return array( |
| 34 |
// Existing. |
| 35 |
'inactive_tab_message' => $settings['message'] ?? '', |
| 36 |
'inactive_tab_abandoned_message' => $settings['abandoned_message'] ?? '', |
| 37 |
'inactive_tab_cart_count' => $cart_count, |
| 38 |
'inactive_tab_cart_total' => $cart_total, |
| 39 |
'inactive_tab_site_name' => $site_name, |
| 40 |
|
| 41 |
// High-value cart. |
| 42 |
'inactive_tab_high_value_message' => $settings['high_value_message'] ?? '', |
| 43 |
'inactive_tab_high_value_threshold' => (float) ( $settings['high_value_threshold'] ?? 100 ), |
| 44 |
|
| 45 |
// Rotating messages. |
| 46 |
'inactive_tab_enable_rotation' => (int) ( $settings['enable_rotation'] ?? 0 ), |
| 47 |
'inactive_tab_rotation_messages' => $settings['rotation_messages'] ?? array(), |
| 48 |
'inactive_tab_rotation_interval' => (int) ( $settings['rotation_interval'] ?? 3 ), |
| 49 |
|
| 50 |
// Favicon. |
| 51 |
'inactive_tab_enable_favicon' => (int) ( $settings['enable_favicon'] ?? 0 ), |
| 52 |
'inactive_tab_favicon_type' => $settings['favicon_type'] ?? 'emoji', |
| 53 |
'inactive_tab_favicon_emoji' => $settings['favicon_emoji'] ?? '', |
| 54 |
'inactive_tab_favicon_url' => $settings['favicon_url'] ?? '', |
| 55 |
|
| 56 |
// Return message. |
| 57 |
'inactive_tab_return_message' => $settings['return_message'] ?? '', |
| 58 |
'inactive_tab_return_duration' => (int) ( $settings['return_duration'] ?? 2 ), |
| 59 |
|
| 60 |
// Scrolling text. |
| 61 |
'inactive_tab_enable_scroll' => (int) ( $settings['enable_scroll'] ?? 0 ), |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Select the appropriate message based on cart state. |
| 67 |
* |
| 68 |
* Three tiers: |
| 69 |
* 1. Empty cart → 'message' |
| 70 |
* 2. High-value → 'high_value_message' (if threshold set and total >= threshold) |
| 71 |
* 3. Has items → 'abandoned_message' |
| 72 |
* |
| 73 |
* @param array<string, mixed> $settings Module settings. |
| 74 |
* @param int $cart_count Number of items in cart. |
| 75 |
* @param float $cart_total_raw Raw cart total (numeric, no currency). |
| 76 |
* |
| 77 |
* @return string Selected message text. |
| 78 |
*/ |
| 79 |
public function select_message( array $settings, int $cart_count, float $cart_total_raw ): string { |
| 80 |
if ( 0 === $cart_count ) { |
| 81 |
return $settings['message'] ?? ''; |
| 82 |
} |
| 83 |
|
| 84 |
$threshold = (float) ( $settings['high_value_threshold'] ?? 0 ); |
| 85 |
$high_msg = $settings['high_value_message'] ?? ''; |
| 86 |
|
| 87 |
if ( $threshold > 0 && $cart_total_raw >= $threshold && '' !== $high_msg ) { |
| 88 |
return $high_msg; |
| 89 |
} |
| 90 |
|
| 91 |
return $settings['abandoned_message'] ?? ''; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Interpolate dynamic variable placeholders in a message string. |
| 96 |
* |
| 97 |
* Supported placeholders: {cart_count}, {cart_total}, {site_name}. |
| 98 |
* |
| 99 |
* @param string $message Message string with placeholders. |
| 100 |
* @param array<string, string> $variables Associative array: cart_count, cart_total, site_name. |
| 101 |
* |
| 102 |
* @return string Message with placeholders replaced. |
| 103 |
*/ |
| 104 |
public function interpolate_variables( string $message, array $variables ): string { |
| 105 |
if ( '' === $message ) { |
| 106 |
return ''; |
| 107 |
} |
| 108 |
|
| 109 |
$replacements = array( |
| 110 |
'{cart_count}' => $variables['cart_count'] ?? '0', |
| 111 |
'{cart_total}' => $variables['cart_total'] ?? '', |
| 112 |
'{site_name}' => $variables['site_name'] ?? '', |
| 113 |
); |
| 114 |
|
| 115 |
return str_replace( array_keys( $replacements ), array_values( $replacements ), $message ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Collect all translatable strings from module settings. |
| 120 |
* |
| 121 |
* Each entry has 'string' (the text) and 'name' (the translation context label). |
| 122 |
* |
| 123 |
* @param array<string, mixed> $settings Module settings. |
| 124 |
* |
| 125 |
* @return array<int, array{string: string, name: string}> Array of translatable string items. |
| 126 |
*/ |
| 127 |
public function get_translatable_strings( array $settings ): array { |
| 128 |
$strings = array(); |
| 129 |
|
| 130 |
$fields = array( |
| 131 |
'message' => 'Inactive tab: empty cart message', |
| 132 |
'abandoned_message' => 'Inactive tab: cart items message', |
| 133 |
'high_value_message' => 'Inactive tab: high-value cart message', |
| 134 |
'return_message' => 'Inactive tab: return message', |
| 135 |
); |
| 136 |
|
| 137 |
foreach ( $fields as $key => $label ) { |
| 138 |
if ( ! empty( $settings[ $key ] ) ) { |
| 139 |
$strings[] = array( |
| 140 |
'string' => $settings[ $key ], |
| 141 |
'name' => $label, |
| 142 |
); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
if ( ! empty( $settings['rotation_messages'] ) && is_array( $settings['rotation_messages'] ) ) { |
| 147 |
foreach ( $settings['rotation_messages'] as $index => $msg ) { |
| 148 |
if ( ! empty( $msg ) ) { |
| 149 |
$strings[] = array( |
| 150 |
'string' => $msg, |
| 151 |
'name' => 'Inactive tab: rotation message #' . ( $index + 1 ), |
| 152 |
); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
return $strings; |
| 158 |
} |
| 159 |
} |
| 160 |
|