Completed
Push — master ( df624b...09e43c )
by Mike
11:25
created

WC_Order_Refund::get_refund_reason()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Order refund. Refunds are based on orders (essentially negative orders) and
8
 * contain much of the same data.
9
 *
10
 * @version  2.7.0
11
 * @package  WooCommerce/Classes
12
 * @category Class
13
 * @author   WooThemes
14
 */
15
class WC_Order_Refund extends WC_Abstract_Order {
16
17
	/**
18
	 * Extend the abstract _data properties and then read the order object.
19
	 *
20
	 * @param  int|object|WC_Order $order Order to init.
21
	 */
22
	public function __construct( $order = 0 ) {
23
		$this->_data = array_merge( $this->_data, array(
24
			'amount'      => '',
25
			'reason'      => '',
26
			'refunded_by' => 0,
27
		) );
28
		parent::__construct( $order );
29
	}
30
31
	/**
32
	 * Data stored in meta keys, but not considered "meta" for an order.
33
	 * @since 2.7.0
34
	 * @var array
35
	 */
36
	protected $_internal_meta_keys = array(
37
		'_order_currency', '_cart_discount', '_refund_amount', '_refunded_by', '_refund_reason',
38
		'_cart_discount_tax', '_order_shipping', '_order_shipping_tax',
39
		'_order_tax', '_order_total', '_order_version', '_prices_include_tax',
40
		'_payment_tokens',
41
	);
42
43
	/**
44
	 * Insert data into the database.
45
	 * @since 2.7.0
46
	 */
47 View Code Duplication
	public function create() {
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...
48
		parent::create();
49
50
		// Store additonal order data
51
		if ( $this->get_id() ) {
52
			$this->update_post_meta( '_refund_amount', $this->get_amount() );
53
			$this->update_post_meta( '_refunded_by', $this->get_refunded_by() );
54
			$this->update_post_meta( '_refund_reason', $this->get_reason() );
55
		}
56
	}
57
58
	/**
59
	 * Read from the database.
60
	 * @since 2.7.0
61
	 * @param int $id ID of object to read.
62
	 */
63
	public function read( $id ) {
64
		parent::read( $id );
65
66
		// Read additonal order data
67
		if ( $this->get_id() ) {
68
			$post_object = get_post( $id );
69
			$this->set_amount( get_post_meta( $this->get_id(), '_refund_amount', true ) );
70
71
			// post_author was used before refunded_by meta.
72
			$this->set_refunded_by( metadata_exists( 'post', $this->get_id(), '_refunded_by' ) ? get_post_meta( $this->get_id(), '_refunded_by', true ) : absint( $post_object->post_author ) );
73
74
			// post_excerpt was used before refund_reason meta.
75
			$this->set_reason( metadata_exists( 'post', $this->get_id(), '_refund_reason' ) ? get_post_meta( $this->get_id(), '_refund_reason', true ) : absint( $post_object->post_excerpt ) );
76
		}
77
	}
78
79
	/**
80
	 * Update data in the database.
81
	 * @since 2.7.0
82
	 */
83 View Code Duplication
	public function update() {
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...
84
		parent::update();
85
86
		// Store additonal order data
87
		$this->update_post_meta( '_refund_amount', $this->get_amount() );
88
		$this->update_post_meta( '_refunded_by', $this->get_refunded_by() );
89
		$this->update_post_meta( '_refund_reason', $this->get_reason() );
90
	}
91
92
	/**
93
	 * Get internal type (post type.)
94
	 * @return string
95
	 */
96
	public function get_type() {
97
		return 'shop_order_refund';
98
	}
99
100
	/**
101
	 * Get status - always completed for refunds.
102
	 * @return string
103
	 */
104
	public function get_status() {
105
		return 'completed';
106
	}
107
108
	/**
109
	 * Get a title for the new post type.
110
	 */
111
	protected function get_post_title() {
112
		return sprintf( __( 'Refund &ndash; %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Order date parsed by strftime', 'woocommerce' ) ) );
113
	}
114
115
	/**
116
	 * Set refunded amount.
117
	 * @param string $value
118
	 */
119
	public function set_amount( $value ) {
120
		$this->_data['amount'] = wc_format_decimal( $value );
121
	}
122
123
	/**
124
	 * Get refunded amount.
125
	 * @return int|float
126
	 */
127
	public function get_amount() {
128
		return apply_filters( 'woocommerce_refund_amount', (double) $this->_data['amount'], $this );
129
	}
130
131
	/**
132
	 * Get formatted refunded amount.
133
	 * @since 2.4
134
	 * @return string
135
	 */
136
	public function get_formatted_refund_amount() {
137
		return apply_filters( 'woocommerce_formatted_refund_amount', wc_price( $this->get_amount(), array( 'currency' => $this->get_currency() ) ), $this );
138
	}
139
140
	/**
141
	 * Set refund reason.
142
	 * @param string $value
143
	 */
144
	public function set_reason( $value ) {
145
		$this->_data['reason'] = $value;
146
	}
147
148
	/**
149
	 * Get refund reason.
150
	 * @since 2.2
151
	 * @return int|float
152
	 */
153
	public function get_reason() {
154
		return apply_filters( 'woocommerce_refund_reason', $this->_data['reason'], $this );
155
	}
156
157
	/**
158
	 * Set refunded by.
159
	 * @param int $value
160
	 */
161
	public function set_refunded_by( $value ) {
162
		$this->_data['refunded_by'] = absint( $value );
163
	}
164
165
	/**
166
	 * Get ID of user who did the refund.
167
	 * @since 2.7
168
	 * @return int
169
	 */
170
	public function get_refunded_by() {
171
		return absint( $this->_data['refunded_by'] );
172
	}
173
174
	/**
175
	 * Magic __get method for backwards compatibility.
176
	 * @param string $key
177
	 * @return mixed
178
	 */
179
	public function __get( $key ) {
180
		_doing_it_wrong( $key, 'Refund properties should not be accessed directly.', '2.7' );
181
		/**
182
		 * Maps legacy vars to new getters.
183
		 */
184
		if ( 'reason' === $key ) {
185
			return $this->get_reason();
186
		} elseif ( 'refund_amount' === $key ) {
187
			return $this->get_amount();
188
		}
189
		return parent::__get( $key );
190
	}
191
192
	/**
193
	 * Gets an refund from the database.
194
	 * @deprecated 2.7
195
	 * @param int $id (default: 0).
196
	 * @return bool
197
	 */
198 View Code Duplication
	public function get_refund( $id = 0 ) {
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...
199
		_deprecated_function( 'get_refund', '2.7', 'read' );
200
		if ( ! $id ) {
201
			return false;
202
		}
203
		if ( $result = get_post( $id ) ) {
204
			$this->populate( $result );
0 ignored issues
show
Deprecated Code introduced by
The method WC_Abstract_Legacy_Order::populate() has been deprecated with message: 2.7

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
205
			return true;
206
		}
207
		return false;
208
	}
209
210
	/**
211
	 * Get refund amount.
212
	 * @deprecated 2.7
213
	 * @return int|float
214
	 */
215
	public function get_refund_amount() {
216
		_deprecated_function( 'get_refund_amount', '2.7', 'get_amount' );
217
		return $this->get_amount();
218
	}
219
220
	/**
221
	 * Get refund reason.
222
	 * @deprecated 2.7
223
	 * @return int|float
224
	 */
225
	public function get_refund_reason() {
226
		_deprecated_function( 'get_refund_reason', '2.7', 'get_reason' );
227
		return $this->get_reason();
228
	}
229
}
230