WC_Gateway_Cheque::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
eloc 14
nc 1
nop 0
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
/**
8
 * Cheque Payment Gateway.
9
 *
10
 * Provides a Cheque Payment Gateway, mainly for testing purposes.
11
 *
12
 * @class 		WC_Gateway_Cheque
13
 * @extends		WC_Payment_Gateway
14
 * @version		2.1.0
15
 * @package		WooCommerce/Classes/Payment
16
 * @author 		WooThemes
17
 */
18
class WC_Gateway_Cheque extends WC_Payment_Gateway {
19
20
    /**
21
     * Constructor for the gateway.
22
     */
23
	public function __construct() {
24
		$this->id                 = 'cheque';
25
		$this->icon               = apply_filters( 'woocommerce_cheque_icon', '' );
26
		$this->has_fields         = false;
27
		$this->method_title       = _x( 'Check Payments', 'Check payment method', 'woocommerce' );
28
		$this->method_description = __( 'Allows check payments. Why would you take checks in this day and age? Well you probably wouldn\'t but it does allow you to make test purchases for testing order emails and the \'success\' pages etc.', 'woocommerce' );
29
30
		// Load the settings.
31
		$this->init_form_fields();
32
		$this->init_settings();
33
34
		// Define user set variables
35
		$this->title        = $this->get_option( 'title' );
36
		$this->description  = $this->get_option( 'description' );
37
		$this->instructions = $this->get_option( 'instructions', $this->description );
38
39
		// Actions
40
		add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
41
    	add_action( 'woocommerce_thankyou_cheque', array( $this, 'thankyou_page' ) );
42
43
    	// Customer Emails
44
    	add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 );
45
    }
46
47
    /**
48
     * Initialise Gateway Settings Form Fields.
49
     */
50 View Code Duplication
    public function init_form_fields() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
52
    	$this->form_fields = array(
53
			'enabled' => array(
54
				'title'   => __( 'Enable/Disable', 'woocommerce' ),
55
				'type'    => 'checkbox',
56
				'label'   => __( 'Enable Check Payments', 'woocommerce' ),
57
				'default' => 'yes'
58
			),
59
			'title' => array(
60
				'title'       => __( 'Title', 'woocommerce' ),
61
				'type'        => 'text',
62
				'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
63
				'default'     => _x( 'Check Payments', 'Check payment method', 'woocommerce' ),
64
				'desc_tip'    => true,
65
			),
66
			'description' => array(
67
				'title'       => __( 'Description', 'woocommerce' ),
68
				'type'        => 'textarea',
69
				'description' => __( 'Payment method description that the customer will see on your checkout.', 'woocommerce' ),
70
				'default'     => __( 'Please send a check to Store Name, Store Street, Store Town, Store State / County, Store Postcode.', 'woocommerce' ),
71
				'desc_tip'    => true,
72
			),
73
			'instructions' => array(
74
				'title'       => __( 'Instructions', 'woocommerce' ),
75
				'type'        => 'textarea',
76
				'description' => __( 'Instructions that will be added to the thank you page and emails.', 'woocommerce' ),
77
				'default'     => '',
78
				'desc_tip'    => true,
79
			),
80
		);
81
    }
82
83
    /**
84
     * Output for the order received page.
85
     */
86
	public function thankyou_page() {
87
		if ( $this->instructions )
88
        	echo wpautop( wptexturize( $this->instructions ) );
89
	}
90
91
    /**
92
     * Add content to the WC emails.
93
     *
94
     * @access public
95
     * @param WC_Order $order
96
     * @param bool $sent_to_admin
97
     * @param bool $plain_text
98
     */
99
	public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $plain_text is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100 View Code Duplication
        if ( $this->instructions && ! $sent_to_admin && 'cheque' === $order->payment_method && $order->has_status( 'on-hold' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
			echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL;
102
		}
103
	}
104
105
    /**
106
     * Process the payment and return the result.
107
     *
108
     * @param int $order_id
109
     * @return array
110
     */
111 View Code Duplication
	public function process_payment( $order_id ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
113
		$order = wc_get_order( $order_id );
114
115
		// Mark as on-hold (we're awaiting the cheque)
116
		$order->update_status( 'on-hold', _x( 'Awaiting check payment', 'Check payment method', 'woocommerce' ) );
117
118
		// Reduce stock levels
119
		$order->reduce_order_stock();
120
121
		// Remove cart
122
		WC()->cart->empty_cart();
123
124
		// Return thankyou redirect
125
		return array(
126
			'result' 	=> 'success',
127
			'redirect'	=> $this->get_return_url( $order )
0 ignored issues
show
Documentation introduced by
$order is of type false|object, but the function expects a object<WC_Order>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
128
		);
129
	}
130
}
131