Completed
Push — issues/2912 ( d8ec76...72f6bd )
by Ravinder
1197:19 queued 1189:23
created

Give_Sequential_Donation_Number::get_serial_code()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 18
nop 2
dl 0
loc 35
rs 8.4266
c 0
b 0
f 0
1
<?php
2
// Exit if access directly.
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit;
5
}
6
7
class Give_Sequential_Donation_Number {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
8
	/**
9
	 * Instance.
10
	 *
11
	 * @since  2.1.0
12
	 * @access private
13
	 * @var
14
	 */
15
	static private $instance;
16
17
	/**
18
	 * Singleton pattern.
19
	 *
20
	 * @since  2.1.0
21
	 * @access private
22
	 */
23
	private function __construct() {
24
	}
25
26
27
	/**
28
	 * Get instance.
29
	 *
30
	 * @since  2.1.0
31
	 * @access public
32
	 * @return Give_Sequential_Donation_Number
33
	 */
34 View Code Duplication
	public static function get_instance() {
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...
35
		if ( null === static::$instance ) {
36
			self::$instance = new static();
37
38
			self::$instance->init();
39
		}
40
41
		return self::$instance;
42
	}
43
44
	/**
45
	 * Initialize the plugin, bailing if any required conditions are not met,
46
	 * including minimum WooCommerce version
47
	 *
48
	 * @since 2.1.0
49
	 */
50
	public function init() {
51
		if ( give_is_setting_enabled( give_get_option( 'sequential-ordering_status', 'enabled' ) ) ) {
52
			add_action( 'wp_insert_post', array( $this, '__save_donation_title' ), 10, 3 );
53
			add_action( 'after_delete_post', array( $this, '__remove_serial_number' ), 10, 1 );
54
		}
55
	}
56
57
	/**
58
	 * Set serialize donation number as donation title.
59
	 * Note: only for internal use
60
	 *
61
	 * @since  2.1.0
62
	 * @access public
63
	 *
64
	 * @param int     $donation_id
65
	 * @param WP_Post $post
66
	 * @param bool    $existing_donation_updated
67
	 *
68
	 * @return void
69
	 */
70
	public function __save_donation_title( $donation_id, $post, $existing_donation_updated ) {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Sequential_Donation_Number::__save_donation_title" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
71
		// Bailout
72
		if (
73
			$existing_donation_updated
74
			|| 'give_payment' !== $post->post_type
75
		) {
76
			return;
77
		}
78
79
		$serial_number = $this->__set_donation_number( $donation_id );
80
		$serial_code   = $this->__set_number_padding( $serial_number );
81
82
		// Add prefix.
83
		if ( $prefix = give_get_option( 'sequential-ordering_number_prefix', '' ) ) {
84
			$serial_code = $prefix . $serial_code;
85
		}
86
87
		// Add suffix.
88
		if ( $suffix = give_get_option( 'sequential-ordering_number_suffix', '' ) ) {
89
			$serial_code = $serial_code . $suffix;
90
		}
91
92
		$serial_code = give_time_do_tags( $serial_code );
93
94
		try {
95
			/* @var WP_Error $wp_error */
96
			$wp_error = wp_update_post(
97
				array(
98
					'ID'         => $donation_id,
99
					'post_title' => trim( $serial_code )
100
				)
101
			);
102
103
			if ( is_wp_error( $wp_error ) ) {
104
				throw new Exception( $wp_error->get_error_message() );
105
			}
106
		} catch ( Exception $e ) {
107
			error_log( "Give caught exception: {$e->getMessage()}" );
108
		}
109
	}
110
111
	/**
112
	 * Set donation number
113
	 * Note: only for internal use
114
	 *
115
	 * @since  2.1.0
116
	 * @access public
117
	 *
118
	 * @param int $donation_id
119
	 *
120
	 * @return int
121
	 */
122
	public function __set_donation_number( $donation_id ) {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Sequential_Donation_Number::__set_donation_number" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
123
		// Customize sequential donation number starting point if needed.
124
		if (
125
			get_option( '_give_reset_sequential_number' ) &&
126
			( $number = give_get_option( 'sequential-ordering_number', 0 ) )
127
		) {
128
			delete_option( '_give_reset_sequential_number' );
129
130
			return Give()->sequential_donation_db->insert( array(
131
				'id'         => $number,
132
				'payment_id' => $donation_id
133
			) );
134
		}
135
136
		return Give()->sequential_donation_db->insert( array(
137
			'payment_id' => $donation_id
138
		) );
139
	}
140
141
142
	/**
143
	 * Remove sequential donation data
144
	 * Note: only internal use.
145
	 *
146
	 * @since  2.1.0
147
	 * @access public
148
	 *
149
	 * @param $donation_id
150
	 *
151
	 * @return bool
152
	 */
153
	public function __remove_serial_number( $donation_id ) {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Sequential_Donation_Number::__remove_serial_number" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
154
		return Give()->sequential_donation_db->delete( $this->get_serial_number( $donation_id ) );
155
	}
156
157
	/**
158
	 * Set number padding in serial code.
159
	 *
160
	 * @since
161
	 * @access private
162
	 *
163
	 * @param $serial_number
164
	 *
165
	 * @return string
166
	 */
167
	private function __set_number_padding( $serial_number ) {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Sequential_Donation_Number::__set_number_padding" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
168
		if ( $number_padding = give_get_option( 'sequential-ordering_number_padding', 0 ) ) {
169
			$serial_number = str_pad( $serial_number, $number_padding, '0', STR_PAD_LEFT );
170
		}
171
172
		return $serial_number;
173
	}
174
175
	/**
176
	 * Get donation number serial code
177
	 *
178
	 * @since  2.1.0
179
	 * @access public
180
	 *
181
	 * @param int|Give_Payment $donation
182
	 * @param array            $args
183
	 *
184
	 * @return string
185
	 */
186
	public function get_serial_code( $donation, $args = array() ) {
187
		$donation = $donation instanceof Give_Payment ? $donation : new Give_Payment( $donation );
188
189
		// Bailout.
190
		if (
191
			empty( $donation->ID )
192
			|| ! give_is_setting_enabled( give_get_option( 'sequential-ordering_status', 'enabled' ) )
193
		) {
194
			return $donation->ID;
195
		}
196
197
		// Set default params.
198
		$args = wp_parse_args(
199
			$args,
200
			array(
201
				'with_hash' => false,
202
				'default'   => true
203
			)
204
		);
205
206
		$serial_code = $args['default'] ? $donation->ID : '';
207
208
		if ( $donation_number = $this->get_serial_number( $donation->ID ) ) {
209
			$serial_code = get_the_title( $donation->ID );
210
		}
211
212
		$serial_code = $args['with_hash'] ? "#{$serial_code}" : $serial_code;
213
214
		/**
215
		 * Filter the donation serial code
216
		 *
217
		 * @since 2.1.0
218
		 */
219
		return apply_filters( 'give_get_donation_serial_code', $serial_code, $donation, $args, $donation_number );
220
	}
221
222
	/**
223
	 * Get serial number
224
	 *
225
	 * @since  2.1.0
226
	 * @access public
227
	 *
228
	 * @param int $donation_id_or_serial_code
229
	 *
230
	 * @return string
231
	 */
232
	public function get_serial_number( $donation_id_or_serial_code ) {
233
		if ( is_numeric( $donation_id_or_serial_code ) ) {
234
			return Give()->sequential_donation_db->get_column_by( 'id', 'payment_id', $donation_id_or_serial_code );
235
		}
236
237
		return $this->get_serial_number( $this->get_donation_id( $donation_id_or_serial_code ) );
238
	}
239
240
241
	/**
242
	 * Get donation id with donation number or serial code
243
	 *
244
	 * @since  2.1.0
245
	 * @access public
246
	 *
247
	 * @param string $donation_number_or_serial_code
248
	 *
249
	 * @return string
250
	 */
251
	public function get_donation_id( $donation_number_or_serial_code ) {
252
		global $wpdb;
253
254
		if ( is_numeric( $donation_number_or_serial_code ) ) {
255
			return Give()->sequential_donation_db->get_column_by(
256
				'payment_id',
257
				'id',
258
				$donation_number_or_serial_code
259
			);
260
		}
261
262
		return $wpdb->get_var(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
263
			$wpdb->prepare(
264
				"
265
				SELECT ID
266
				FROM $wpdb->posts
267
				WHERE post_title=%s
268
				",
269
				$donation_number_or_serial_code
270
			)
271
		);
272
	}
273
274
	/**
275
	 * Get maximum donation number
276
	 *
277
	 * @since  2.1.0
278
	 * @access public
279
	 *
280
	 * @return int
281
	 */
282
	public function get_max_number() {
283
		global $wpdb;
284
		$table_name = Give()->sequential_donation_db->table_name;
285
286
		return absint(
287
			$wpdb->get_var(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
288
				"
289
				SELECT ID
290
				FROM {$table_name}
291
				ORDER BY id DESC 
292
				LIMIT 1
293
				"
294
			)
295
		);
296
	}
297
}
298