<?php

if (!defined('ABSPATH')) {
    exit;
}
class WC_Gateway_Cheque extends WC_Payment_Gateway
{
    public function __construct()
    {
        $this->id = 'cheque';
        $this->icon = apply_filters('woocommerce_cheque_icon', '');
        $this->has_fields = false;
        $this->method_title = _x('Check payments', 'Check payment method', 'woocommerce');
        $this->method_description = __('Take payments in person via checks. This offline gateway can also be useful to test purchases.', 'woocommerce');
        $this->init_form_fields();
        $this->init_settings();
        $this->title = $this->get_option('title');
        $this->description = $this->get_option('description');
        $this->instructions = $this->get_option('instructions');
        add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
        add_action('woocommerce_thankyou_cheque', array($this, 'thankyou_page'));
        add_action('woocommerce_email_before_order_table', array($this, 'email_instructions'), 10, 3);
    }
    public function init_form_fields()
    {
        $this->form_fields = array('enabled' => array('title' => __('Enable/Disable', 'woocommerce'), 'type' => 'checkbox', 'label' => __('Enable check payments', 'woocommerce'), 'default' => 'no'), 'title' => array('title' => __('Title', 'woocommerce'), 'type' => 'text', 'description' => __('This controls the title which the user sees during checkout.', 'woocommerce'), 'default' => _x('Check payments', 'Check payment method', 'woocommerce'), 'desc_tip' => true), 'description' => array('title' => __('Description', 'woocommerce'), 'type' => 'textarea', 'description' => __('Payment method description that the customer will see on your checkout.', 'woocommerce'), 'default' => __('Please send a check to Store Name, Store Street, Store Town, Store State / County, Store Postcode.', 'woocommerce'), 'desc_tip' => true), 'instructions' => array('title' => __('Instructions', 'woocommerce'), 'type' => 'textarea', 'description' => __('Instructions that will be added to the thank you page and emails.', 'woocommerce'), 'default' => '', 'desc_tip' => true));
    }
    public function thankyou_page()
    {
        if ($this->instructions) {
            echo wp_kses_post(wpautop(wptexturize($this->instructions)));
        }
    }
    public function email_instructions($order, $sent_to_admin, $plain_text = false)
    {
        if ($this->instructions && !$sent_to_admin && 'cheque' === $order->get_payment_method() && $order->has_status('on-hold')) {
            echo wp_kses_post(wpautop(wptexturize($this->instructions)) . PHP_EOL);
        }
    }
    public function process_payment($order_id)
    {
        $order = wc_get_order($order_id);
        if ($order->get_total() > 0) {
            $order->update_status(apply_filters('woocommerce_cheque_process_payment_order_status', 'on-hold', $order), _x('Awaiting check payment', 'Check payment method', 'woocommerce'));
        } else {
            $order->payment_complete();
        }
        WC()->cart->empty_cart();
        return array('result' => 'success', 'redirect' => $this->get_return_url($order));
    }
}