<?php

/**
 * Card Oracle Email functionality
 *
 * Handles email operations for the Card Oracle plugin including reading delivery,
 * subscription management, and email template processing. Integrates with various
 * email service providers for enhanced functionality.
 *
 * @link       https://chillichalli.com
 * @since      1.0.0
 *
 * @package    Card_Oracle
 * @subpackage Card_Oracle/includes
 */
/**
 * Card Oracle Email Class.
 *
 * Manages email functionality including sending reading results to users,
 * handling email subscriptions, and processing email templates. Provides
 * integration with email service providers and WordPress mail functions.
 *
 * @since      1.1.2
 * @package    Card_Oracle
 * @subpackage Card_Oracle/includes
 * @author     Christopher Graham <support@chillichalli.com>
 */
class CardOracleEmail {
    /**
     * Set up the Card Oracle Logging Class.
     *
     * @since 1.0.0
     */
    public function __construct() {
    }

    /**
     * Add the email button to the reading.
     *
     * @since 1.1.3
     *
     * @param string $email_content The reading html to send to the user.
     * @param string $reading_id The id of the reading.
     */
    public function add_email_button( $email_content, $reading_id ) {
        $html = '';
        // Add email button to page if option enabled.
        if ( get_option( 'card_oracle_allow_email' ) ) {
            $styles = file_get_contents( CARD_ORACLE_CSS );
            // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
            if ( false === $styles ) {
                co_debug_log( 'class-card-oracle-email.php: Unable to read CSS file: ' . CARD_ORACLE_CSS );
                $styles = '';
            }
            $email_text = card_oracle_create_email( esc_html__( 'Your Reading', 'card-oracle' ), $styles, $email_content );
            // Store the rendered reading server-side and reference it by a
            // single-use token. The email body must never be supplied by the
            // client: the send handler is a public (nopriv) endpoint, so
            // accepting client HTML would turn it into an open mail relay.
            $email_token = wp_generate_password( 32, false );
            set_transient( 'card_oracle_email_' . $email_token, $email_text, 15 * MINUTE_IN_SECONDS );
            $form_text = get_option( 'card_oracle_email_text', __( 'Email this Reading to:', 'card-oracle' ) );
            $html .= '<div class="card-oracle-email">';
            $html .= '<p>' . $form_text . '</p>';
            $html .= wp_nonce_field(
                'card-oracle-send-reader-email',
                '_wpnonce',
                true,
                false
            );
            $html .= '<input type="text" name="emailaddress" placeholder="' . esc_attr__( 'Email Address', 'card-oracle' ) . '" id="emailaddress" />';
            $html .= '<input type="submit" name="reading-send" value="' . __( 'Send', 'card-oracle' ) . '" id="reading-send" />';
            $html .= '<input type="hidden" id="ajax_url" name="ajax_url" value="' . admin_url( 'admin-ajax.php' ) . '">';
            $html .= '<input type="hidden" id="emailtoken" name="emailtoken" value="' . esc_attr( $email_token ) . '">';
            if ( 'yes' === get_option( 'card_oracle_subscribe' ) ) {
                $html .= '<input type="hidden" id="readingid" name="readingid" value="' . $reading_id . '">';
                $html .= '<br /><div class="card-oracle-subscribe">';
                $html .= '<input type="checkbox" id="card-oracle-subscribe" name="card-oracle-subscribe" />';
                $text = ( get_option( 'card_oracle_email_consent_text' ) ? get_option( 'card_oracle_email_consent_text' ) : __( 'Subscribe to our list to keep up to date with our latest news.', 'card-oracle' ) );
                $html .= '<label for="card-oracle-subscribe">' . esc_html( $text ) . '</label>';
                $html .= '</div>';
            }
            $html .= '<p class="card-oracle-response"></p>';
            $html .= '</div>';
        }
        return $html;
    }

}
