| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ebook Store - Brevo settings tab. |
| 4 |
* |
| 5 |
* Rendered inside <div class="ebook-settings-panel">, so this file emits |
| 6 |
* <section> blocks only: no table markup. |
| 7 |
* |
| 8 |
* PHP 7.4 compatible. |
| 9 |
* |
| 10 |
* @package EbookStore |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
settings_fields( 'ebook-settings-group-brevo' ); |
| 18 |
do_settings_sections( 'ebook-settings-group-brevo' ); |
| 19 |
|
| 20 |
$brevo_api_key = trim( (string) get_option( 'brevo_api_key', '' ) ); |
| 21 |
$brevo_list = trim( (string) get_option( 'brevo_lists', '' ) ); |
| 22 |
$brevo_enabled = (string) get_option( 'brevo_integration_enabled', $brevo_list !== '' ? '1' : '' ); |
| 23 |
|
| 24 |
$has_api_key = ( $brevo_api_key !== '' ); |
| 25 |
|
| 26 |
// Only ask Brevo for the lists once a key has actually been saved. Without a |
| 27 |
// key the lookup can only fail, and an empty dropdown just confuses people. |
| 28 |
$brevo_lists = $has_api_key ? ebook_store_get_brevo_lists() : false; |
| 29 |
$lists_loaded = is_array( $brevo_lists ); |
| 30 |
$list_count = $lists_loaded ? count( $brevo_lists ) : 0; |
| 31 |
|
| 32 |
$list_options = array( '' => __( 'Do not add buyers to any list', 'ebook-store' ) ); |
| 33 |
if ( $lists_loaded ) { |
| 34 |
foreach ( $brevo_lists as $list ) { |
| 35 |
$list_id = isset( $list->id ) ? (string) $list->id : ''; |
| 36 |
if ( $list_id === '' ) { |
| 37 |
continue; |
| 38 |
} |
| 39 |
$list_options[ $list_id ] = isset( $list->name ) && $list->name !== '' |
| 40 |
? (string) $list->name |
| 41 |
: sprintf( |
| 42 |
/* translators: %s: numeric Brevo list ID. */ |
| 43 |
__( 'List %s', 'ebook-store' ), |
| 44 |
$list_id |
| 45 |
); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
$general_tab_url = ebook_store_get_settings_tab_url( 'General' ); |
| 50 |
|
| 51 |
ebook_store_render_hero( |
| 52 |
array( |
| 53 |
'eyebrow' => __( 'Brevo', 'ebook-store' ), |
| 54 |
'title' => __( 'Add buyers to a Brevo contact list', 'ebook-store' ), |
| 55 |
'intro' => __( 'After someone pays for a book, Ebook Store can pass their email address to Brevo so they land in your newsletter list automatically. Connect your account first, then pick the list.', 'ebook-store' ), |
| 56 |
'aside' => function () use ( $has_api_key, $lists_loaded, $list_count, $brevo_list, $brevo_enabled, $list_options ) { |
| 57 |
?> |
| 58 |
<aside class="ebook-settings-status-card"> |
| 59 |
<h4><?php echo esc_html__( 'Current status', 'ebook-store' ); ?></h4> |
| 60 |
<ul class="ebook-settings-status-list"> |
| 61 |
<?php |
| 62 |
if ( ! $has_api_key ) { |
| 63 |
ebook_store_status_row( |
| 64 |
__( 'Brevo account', 'ebook-store' ), |
| 65 |
'muted', |
| 66 |
__( 'No API key saved yet.', 'ebook-store' ) |
| 67 |
); |
| 68 |
} elseif ( $lists_loaded ) { |
| 69 |
ebook_store_status_row( |
| 70 |
__( 'Brevo account', 'ebook-store' ), |
| 71 |
'ok', |
| 72 |
sprintf( |
| 73 |
/* translators: %d: number of contact lists found in the Brevo account. */ |
| 74 |
_n( 'Connected. %d contact list found.', 'Connected. %d contact lists found.', $list_count, 'ebook-store' ), |
| 75 |
$list_count |
| 76 |
) |
| 77 |
); |
| 78 |
} else { |
| 79 |
ebook_store_status_row( |
| 80 |
__( 'Brevo account', 'ebook-store' ), |
| 81 |
'error', |
| 82 |
__( 'A key is saved, but Brevo did not answer. The key may be wrong or revoked.', 'ebook-store' ) |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
if ( $brevo_list === '' ) { |
| 87 |
ebook_store_status_row( |
| 88 |
__( 'Chosen list', 'ebook-store' ), |
| 89 |
'muted', |
| 90 |
__( 'No list chosen, so nobody is added.', 'ebook-store' ) |
| 91 |
); |
| 92 |
} else { |
| 93 |
ebook_store_status_row( |
| 94 |
__( 'Chosen list', 'ebook-store' ), |
| 95 |
'ok', |
| 96 |
isset( $list_options[ $brevo_list ] ) |
| 97 |
? $list_options[ $brevo_list ] |
| 98 |
: sprintf( |
| 99 |
/* translators: %s: saved Brevo list ID. */ |
| 100 |
__( 'Saved list ID %s (not found in this account right now).', 'ebook-store' ), |
| 101 |
$brevo_list |
| 102 |
) |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
if ( $brevo_enabled === '1' && $brevo_list !== '' && $lists_loaded ) { |
| 107 |
ebook_store_status_row( |
| 108 |
__( 'Adding buyers', 'ebook-store' ), |
| 109 |
'ok', |
| 110 |
__( 'On. Paying buyers are sent to Brevo after checkout.', 'ebook-store' ) |
| 111 |
); |
| 112 |
} elseif ( $brevo_enabled === '1' ) { |
| 113 |
ebook_store_status_row( |
| 114 |
__( 'Adding buyers', 'ebook-store' ), |
| 115 |
'warn', |
| 116 |
__( 'Switched on, but something above is still missing, so nothing is sent yet.', 'ebook-store' ) |
| 117 |
); |
| 118 |
} else { |
| 119 |
ebook_store_status_row( |
| 120 |
__( 'Adding buyers', 'ebook-store' ), |
| 121 |
'muted', |
| 122 |
__( 'Off. Buyer email addresses stay in Ebook Store only.', 'ebook-store' ) |
| 123 |
); |
| 124 |
} |
| 125 |
?> |
| 126 |
</ul> |
| 127 |
</aside> |
| 128 |
<?php |
| 129 |
}, |
| 130 |
) |
| 131 |
); |
| 132 |
|
| 133 |
ebook_store_pro_banner( __( 'Brevo', 'ebook-store' ) ); |
| 134 |
|
| 135 |
/* |
| 136 |
* Step 1 - the connection. |
| 137 |
*/ |
| 138 |
ebook_store_section_open( |
| 139 |
array( |
| 140 |
'eyebrow' => __( 'Step 1', 'ebook-store' ), |
| 141 |
'title' => __( 'Connect your Brevo account', 'ebook-store' ), |
| 142 |
'intro' => __( 'The API key is the password Ebook Store uses to talk to Brevo on your behalf. Use a key from the same Brevo account that holds the list you want buyers added to.', 'ebook-store' ), |
| 143 |
'id' => 'ebook-brevo-connection', |
| 144 |
'pro' => true, |
| 145 |
) |
| 146 |
); |
| 147 |
|
| 148 |
ebook_store_steps( |
| 149 |
__( 'Where to find your API key', 'ebook-store' ), |
| 150 |
array( |
| 151 |
esc_html__( 'Sign in to Brevo and open the account menu in the top-right corner.', 'ebook-store' ), |
| 152 |
esc_html__( 'Choose "SMTP & API", then the "API keys" tab.', 'ebook-store' ), |
| 153 |
esc_html__( 'Select "Generate a new API key", give it a name such as "Ebook Store", and copy the key that appears.', 'ebook-store' ), |
| 154 |
esc_html__( 'Brevo shows the full key only once. Paste it below straight away and save this page.', 'ebook-store' ), |
| 155 |
) |
| 156 |
); |
| 157 |
|
| 158 |
ebook_store_grid_open( 1 ); |
| 159 |
|
| 160 |
ebook_store_field( |
| 161 |
array( |
| 162 |
'type' => 'text', |
| 163 |
'name' => 'brevo_api_key', |
| 164 |
'id' => 'brevo_api_key', |
| 165 |
'label' => __( 'Brevo API key', 'ebook-store' ), |
| 166 |
'value' => $brevo_api_key, |
| 167 |
'placeholder' => 'xkeysib-0000000000000000000000000000000000000000000000000000000000000000-XXXXXXXXXXXXXXXX', |
| 168 |
'mono' => true, |
| 169 |
'wide' => true, |
| 170 |
'pro' => true, |
| 171 |
'help' => esc_html__( 'Brevo keys start with "xkeysib-". If the key is wrong, expired or deleted in Brevo, no buyer is added to your list and the status panel above turns red. Nothing else on your shop is affected.', 'ebook-store' ), |
| 172 |
) |
| 173 |
); |
| 174 |
|
| 175 |
ebook_store_grid_close(); |
| 176 |
|
| 177 |
if ( ! $has_api_key ) { |
| 178 |
// Chicken and egg: the list dropdown can only be filled in once a key has |
| 179 |
// been saved, so ask for the key first and show nothing else. |
| 180 |
ebook_store_callout( |
| 181 |
esc_html__( 'Paste your API key above and press "Save Changes". This page then reloads with the contact lists from your Brevo account, and you can choose the one buyers should be added to.', 'ebook-store' ), |
| 182 |
'info', |
| 183 |
__( 'Save your key first:', 'ebook-store' ) |
| 184 |
); |
| 185 |
|
| 186 |
/* |
| 187 |
* These two settings belong to this tab's option group, so WordPress would |
| 188 |
* blank them out on save if the form did not submit them. While the key is |
| 189 |
* missing the controls are hidden, so keep the saved values here. |
| 190 |
*/ |
| 191 |
?> |
| 192 |
<input type="hidden" name="brevo_lists" value="<?php echo esc_attr( $brevo_list ); ?>" /> |
| 193 |
<input type="hidden" name="brevo_integration_enabled" value="<?php echo esc_attr( $brevo_enabled === '1' ? '1' : '' ); ?>" /> |
| 194 |
<?php |
| 195 |
} |
| 196 |
|
| 197 |
ebook_store_section_close(); |
| 198 |
|
| 199 |
/* |
| 200 |
* Step 2 - the list and the switch. Only shown once a key exists. |
| 201 |
*/ |
| 202 |
if ( $has_api_key ) { |
| 203 |
|
| 204 |
ebook_store_section_open( |
| 205 |
array( |
| 206 |
'eyebrow' => __( 'Step 2', 'ebook-store' ), |
| 207 |
'title' => __( 'Choose the list buyers are added to', 'ebook-store' ), |
| 208 |
'intro' => __( 'These are the contact lists Ebook Store can see in your Brevo account. Pick one, then switch the sync on.', 'ebook-store' ), |
| 209 |
'id' => 'ebook-brevo-list', |
| 210 |
'pro' => true, |
| 211 |
) |
| 212 |
); |
| 213 |
|
| 214 |
if ( ! $lists_loaded ) { |
| 215 |
ebook_store_callout( |
| 216 |
esc_html__( 'Ebook Store could not read the contact lists from Brevo with the saved key. Check that the key was copied in full, that it has not been deleted in Brevo, and that your server is allowed to make outgoing web requests. Correct the key above, save, and this list will fill in.', 'ebook-store' ), |
| 217 |
'warning', |
| 218 |
__( 'Brevo did not answer:', 'ebook-store' ) |
| 219 |
); |
| 220 |
} elseif ( $list_count === 0 ) { |
| 221 |
ebook_store_callout( |
| 222 |
esc_html__( 'The connection works, but this Brevo account has no contact lists yet. Create one in Brevo under "Contacts", then reload this page.', 'ebook-store' ), |
| 223 |
'warning', |
| 224 |
__( 'No lists in this account:', 'ebook-store' ) |
| 225 |
); |
| 226 |
} |
| 227 |
|
| 228 |
ebook_store_toggle_grid_open(); |
| 229 |
|
| 230 |
ebook_store_toggle( |
| 231 |
array( |
| 232 |
'name' => 'brevo_integration_enabled', |
| 233 |
'id' => 'brevo_integration_enabled', |
| 234 |
'title' => __( 'Add paying buyers to Brevo', 'ebook-store' ), |
| 235 |
'description' => __( 'Only paid orders are sent, and only after checkout completes. Turn this off at any time to stop sending without losing your settings.', 'ebook-store' ), |
| 236 |
'checked' => $brevo_enabled, |
| 237 |
'pro' => true, |
| 238 |
) |
| 239 |
); |
| 240 |
|
| 241 |
ebook_store_toggle_grid_close(); |
| 242 |
|
| 243 |
ebook_store_grid_open( 1 ); |
| 244 |
|
| 245 |
ebook_store_field( |
| 246 |
array( |
| 247 |
'type' => 'select', |
| 248 |
'name' => 'brevo_lists', |
| 249 |
'id' => 'brevo_lists', |
| 250 |
'label' => __( 'Contact list for buyers', 'ebook-store' ), |
| 251 |
'value' => $brevo_list, |
| 252 |
'options' => $list_options, |
| 253 |
'wide' => true, |
| 254 |
'pro' => true, |
| 255 |
'help' => esc_html__( 'Buyers are added to this list only. If you leave it on "Do not add buyers to any list", nothing is sent to Brevo even when the switch above is on. Renaming the list inside Brevo is safe; deleting it is not, because the saved choice then points at a list that no longer exists.', 'ebook-store' ), |
| 256 |
) |
| 257 |
); |
| 258 |
|
| 259 |
ebook_store_grid_close(); |
| 260 |
|
| 261 |
ebook_store_section_close(); |
| 262 |
} |
| 263 |
|
| 264 |
/* |
| 265 |
* What actually happens, in plain words. |
| 266 |
*/ |
| 267 |
ebook_store_section_open( |
| 268 |
array( |
| 269 |
'eyebrow' => __( 'Good to know', 'ebook-store' ), |
| 270 |
'title' => __( 'What happens after a sale', 'ebook-store' ), |
| 271 |
'intro' => __( 'A short summary of the rules Ebook Store follows, so there are no surprises for you or your buyers.', 'ebook-store' ), |
| 272 |
'id' => 'ebook-brevo-behaviour', |
| 273 |
) |
| 274 |
); |
| 275 |
|
| 276 |
ebook_store_callout( |
| 277 |
'<ul>' |
| 278 |
. '<li>' . esc_html__( 'Buyers are only sent to Brevo when the switch is on and a list is chosen. Free downloads, failed payments and refunds are not sent.', 'ebook-store' ) . '</li>' |
| 279 |
. '<li>' . esc_html__( 'If the buyer is already in your Brevo account, the existing contact is updated and added to the list instead of being duplicated.', 'ebook-store' ) . '</li>' |
| 280 |
. '<li>' . esc_html__( 'Only the email address is sent. No names, order details or files leave your site.', 'ebook-store' ) . '</li>' |
| 281 |
. '<li>' . esc_html__( 'Buyers are added as normal contacts, so they are not unsubscribed or blocked. Marketing consent is still your responsibility.', 'ebook-store' ) . '</li>' |
| 282 |
. '<li>' . esc_html__( 'If Brevo is unreachable during a sale, the order and the download still complete normally. Only the newsletter sign-up is skipped.', 'ebook-store' ) . '</li>' |
| 283 |
. '</ul>', |
| 284 |
'soft' |
| 285 |
); |
| 286 |
|
| 287 |
if ( ! ebook_store_has_pro_license() ) { |
| 288 |
ebook_store_callout( |
| 289 |
sprintf( |
| 290 |
/* translators: %s: link to the General settings tab. */ |
| 291 |
esc_html__( 'The Brevo settings on this page stay locked until a Pro license key is saved on the %s. The lock also applies to submitted form data, so these values cannot be changed without a license.', 'ebook-store' ), |
| 292 |
'<a href="' . esc_url( $general_tab_url ) . '">' . esc_html__( 'General tab', 'ebook-store' ) . '</a>' |
| 293 |
), |
| 294 |
'warning', |
| 295 |
__( 'Pro feature:', 'ebook-store' ) |
| 296 |
); |
| 297 |
} |
| 298 |
|
| 299 |
ebook_store_section_close(); |
| 300 |
|