<?php

if ( ! defined( 'ABSPATH' ) ) {

    exit;

}



/**

 * @param string $url

 * @param string $label

 * @return string

 */

function elementsell_get_email_button_html( $url, $label ) {

    $url = esc_url( (string) $url );



    if ( $url === '' ) {

        return '';

    }



    return '<p style="margin:20px 0;text-align:center;">'

        . '<a href="' . $url . '" target="_blank" rel="noopener noreferrer" '

        . 'style="display:inline-block;padding:14px 28px;background:#2271b1;color:#ffffff !important;'

        . 'text-decoration:none;border-radius:8px;font-weight:700;font-size:16px;line-height:1.4;">'

        . esc_html( $label )

        . '</a></p>';

}



/**

 * @param string $text

 * @param object $order

 * @return string

 */

function elementsell_replace_email_html_variables( $text, $order ) {

    $replacements = [

        '{sales_page_button}' => elementsell_get_email_button_html(

            elementsell_get_order_source_page_url( $order ),

            'Zpět na prodejní stránku'

        ),

        '{payment_button}'    => elementsell_get_email_button_html(

            elementsell_get_order_reminder_payment_url( $order ),

            'Dokončit platbu'

        ),

    ];



    foreach ( $replacements as $key => $value ) {

        $text = str_replace( $key, (string) $value, $text );

    }



    return $text;

}



function elementsell_replace_email_variables( $text, $order ) {



    $variables = [

        '{order_id}'       => $order->id,

        '{order_uid}'      => $order->order_uid,

        '{payment_uid}'    => $order->payment_uid,

        '{payment_method}' => $order->payment_method,

        '{payment_status}' => $order->payment_status,



        '{product_name}'   => $order->product_name,

        '{amount}'         => number_format((float) $order->amount, 0, ',', ' '),

        '{currency}'       => $order->currency,



        '{email}'          => $order->email,

        '{first_name}'     => $order->first_name,

        '{surname}'        => $order->surname,

        '{phone}'          => $order->phone,



        // Oslovení v 5. pádě: {osloveni} = "Honzo", {pozdrav} = "Ahoj Honzo,"

        '{osloveni}'       => function_exists( 'elementsell_osloveni' ) ? elementsell_osloveni( $order->first_name ) : '',

        '{pozdrav}'        => function_exists( 'elementsell_pozdrav' ) ? elementsell_pozdrav( $order->first_name ) : 'Ahoj,',

        '{odesilatel}'     => function_exists( 'elementsell_get_order_sender_name' ) ? elementsell_get_order_sender_name( (int) $order->id ) : '',



        '{created_at}'     => $order->created_at,

        '{paid_at}'        => $order->paid_at,

        '{payment_url}'    => elementsell_get_order_reminder_payment_url( $order ),

        '{source_page_url}' => elementsell_get_order_source_page_url( $order ),

    ];



    foreach ( $variables as $key => $value ) {

        $text = str_replace($key, (string) $value, $text);

    }



    return $text;

}