Utils.php 8.09 KB
Newer Older
1
2
3
4
5
6
7
<?php

namespace Kudos\Helpers;

class Utils {

	/**
8
	 * Gets url Mollie will use to return customer to after payment complete.
9
10
11
	 *
	 * @return string|void
	 */
Michael Iseard's avatar
Michael Iseard committed
12
	public static function get_return_url(): string {
13

14
		$use_custom = get_option( '_kudos_completed_payment' ) === 'url';
Michael Iseard's avatar
Michael Iseard committed
15
		$custom_url = esc_url( get_option( '_kudos_custom_return_url' ) );
Michael Iseard's avatar
Michael Iseard committed
16

Michael Iseard's avatar
Michael Iseard committed
17
		if ( $use_custom && $custom_url ) {
18
19
			return $custom_url;
		} else {
20
			$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
Michael Iseard's avatar
Michael Iseard committed
21

22
			return home_url( $request_uri );
23
24
25
26
27
		}

	}

	/**
Michael Iseard's avatar
Michael Iseard committed
28
	 * Converts three letter currency code into a symbol.
29
	 *
30
	 * @param string $currency Three letter currency code (EUR, GBP, USD).
Michael Iseard's avatar
Michael Iseard committed
31
	 *
32
33
	 * @return string
	 */
Michael Iseard's avatar
Michael Iseard committed
34
	public static function get_currency_symbol( string $currency ): string {
35

Michael Iseard's avatar
Michael Iseard committed
36
		$currency = strtoupper( $currency );
37

Michael Iseard's avatar
Michael Iseard committed
38
		switch ( $currency ) {
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
			case 'EUR':
				$symbol = '&#8364;';
				break;
			case 'USD':
				$symbol = '&#36;';
				break;
			case 'GBP':
				$symbol = '&#163;';
				break;
			case 'JPY':
				$symbol = '&#165;';
				break;
			default:
				$symbol = $currency;
		}

		return $symbol;
	}

	/**
Michael Iseard's avatar
Michael Iseard committed
59
	 * Lightens/darkens a given colour (hex format), returning the altered colour in hex format.
60
	 *
Michael Iseard's avatar
Michael Iseard committed
61
	 * @source https://gist.github.com/stephenharris/5532899
62
	 * @param string $hex Colour as hexadecimal (with or without hash).
Michael Iseard's avatar
Michael Iseard committed
63
	 * @param float $percent Percentage to modify the luminance by.
Michael Iseard's avatar
Michael Iseard committed
64
	 *
65
66
67
	 * @return string Lightened/Darkened colour as hexadecimal (with hash);
	 * @percent float $percent Decimal ( 0.2 = lighten by 20%(), -0.4 = darken by 40%() )
	 */
Michael Iseard's avatar
Michael Iseard committed
68
	public static function color_luminance( string $hex, float $percent ): string {
69

70
		// Remove leading '#' if present.
Michael Iseard's avatar
Michael Iseard committed
71
		$hex = ltrim( $hex, '#' );
72

73
74
		// Expand to 6 character hex code (e.g. FFF -> FFFFFF).
		if ( strlen( $hex ) === 3 ) {
75
76
77
			$hex = $hex[0] + $hex[0] + $hex[1] + $hex[1] + $hex[2] + $hex[2];
		}

78
		// Convert to decimal.
Michael Iseard's avatar
Michael Iseard committed
79
		$hex = array_map( 'hexdec', str_split( $hex, 2 ) );
80

81
		// Change luminosity of decimal colour.
82
		foreach ( $hex as & $color ) {
83
84
			$adjustable_limit = $percent < 0 ? $color : 255 - $color;
			$adjust_amount    = ceil( $adjustable_limit * $percent );
85

86
			$color = str_pad( dechex( $color + $adjust_amount ), 2, '0', STR_PAD_LEFT );
87
88
		}

Michael Iseard's avatar
Michael Iseard committed
89
		return '#' . implode( $hex );
90
91
92
	}

	/**
Michael Iseard's avatar
Michael Iseard committed
93
	 * Returns a translated string of the sequence type.
94
	 *
95
	 * @param string $text Mollie sequence type code.
Michael Iseard's avatar
Michael Iseard committed
96
	 *
97
98
	 * @return string|void
	 */
Michael Iseard's avatar
Michael Iseard committed
99
	public static function get_sequence_type( string $text ): string {
Michael Iseard's avatar
Michael Iseard committed
100

Michael Iseard's avatar
Michael Iseard committed
101
		switch ( $text ) {
102
			case 'oneoff':
Michael Iseard's avatar
Michael Iseard committed
103
				return __( 'One-off', 'kudos-donations' );
104
			case 'first':
Michael Iseard's avatar
Michael Iseard committed
105
				return __( 'Recurring (first payment)', 'kudos-donations' );
106
			default:
Michael Iseard's avatar
Michael Iseard committed
107
				return __( 'Recurring', 'kudos-donations' );
108
		}
Michael Iseard's avatar
Michael Iseard committed
109

110
111
112
	}

	/**
Michael Iseard's avatar
Michael Iseard committed
113
	 * Returns subscription frequency name based on number of months.
114
	 *
115
	 * @param string $frequency Mollie frequency code.
Michael Iseard's avatar
Michael Iseard committed
116
	 *
117
118
	 * @return string|void
	 */
Michael Iseard's avatar
Michael Iseard committed
119
	public static function get_frequency_name( string $frequency ): string {
Michael Iseard's avatar
Michael Iseard committed
120

Michael Iseard's avatar
Michael Iseard committed
121
		switch ( $frequency ) {
122
			case '12 months':
Michael Iseard's avatar
Michael Iseard committed
123
				return __( 'Yearly', 'kudos-donations' );
124
			case '1 month':
Michael Iseard's avatar
Michael Iseard committed
125
				return __( 'Monthly', 'kudos-donations' );
126
			case '3 months':
Michael Iseard's avatar
Michael Iseard committed
127
				return __( 'Quarterly', 'kudos-donations' );
128
			case 'oneoff':
Michael Iseard's avatar
Michael Iseard committed
129
				return __( 'One-off', 'kudos-donations' );
130
131
132
			default:
				return $frequency;
		}
Michael Iseard's avatar
Michael Iseard committed
133

134
135
136
	}

	/**
Michael Iseard's avatar
Michael Iseard committed
137
	 * Calculate how many years a subscription is running for.
138
139
	 * This is based on the number of payments and the frequency.
	 *
Michael Iseard's avatar
Michael Iseard committed
140
	 * @param int $years Number of years as an integer.
141
	 * @param string $frequency Frequency.
Michael Iseard's avatar
Michael Iseard committed
142
	 *
143
144
	 * @return int|null
	 */
Michael Iseard's avatar
Michael Iseard committed
145
	public static function get_times_from_years( int $years, string $frequency ) {
146

Michael Iseard's avatar
Michael Iseard committed
147
		if ( ! $years > 0 ) {
148
149
150
			return null;
		}

Michael Iseard's avatar
Michael Iseard committed
151
		return ( 12 / intval( $frequency ) ) * $years - 1;
Michael Iseard's avatar
Michael Iseard committed
152

153
154
155
	}

	/**
Michael Iseard's avatar
Michael Iseard committed
156
	 * Generate a random and unique ID with specified prefix.
157
	 *
158
	 * @param string|null $prefix Prefix for id.
Michael Iseard's avatar
Michael Iseard committed
159
	 * @param int $length Return value length (minus prefix).
Michael Iseard's avatar
Michael Iseard committed
160
	 *
161
162
	 * @return string
	 */
Michael Iseard's avatar
Michael Iseard committed
163
	public static function generate_id( string $prefix = null, int $length = 10 ): string {
Michael Iseard's avatar
Michael Iseard committed
164

165
		return $prefix . substr( base_convert( sha1( uniqid( rand() ) ), 16, 36 ), 0, $length );
Michael Iseard's avatar
Michael Iseard committed
166

167
168
	}

Michael Iseard's avatar
Michael Iseard committed
169
	/**
Michael Iseard's avatar
Michael Iseard committed
170
	 * Schedules an action using action scheduler.
Michael Iseard's avatar
Michael Iseard committed
171
172
173
174
175
176
	 *
	 * @param int $timestamp Timestamp of when to run the action.
	 * @param string $hook The name of the WordPress action that is being registered.
	 * @param array $args An array of arguments to pass.
	 * @param bool $overwrite Whether to replace existing scheduled action or not.
	 */
Michael Iseard's avatar
Michael Iseard committed
177
	public static function schedule_action( int $timestamp, string $hook, array $args = [], bool $overwrite = false ) {
Michael Iseard's avatar
Michael Iseard committed
178
179
180
181

		if ( class_exists( 'ActionScheduler' ) ) {

			if ( $overwrite ) {
Michael Iseard's avatar
Michael Iseard committed
182
				as_unschedule_action( $hook, $args );
Michael Iseard's avatar
Michael Iseard committed
183
184
185
186
187
188
189
190
191
192
193
			}

			if ( false === as_next_scheduled_action( $hook, $args ) ) {
				as_schedule_single_action( $timestamp, $hook, $args );
			}
		} else {
			do_action( $hook, $args );
		}

	}

194
	/**
Michael Iseard's avatar
Michael Iseard committed
195
	 * Schedules a recurring action using action scheduler.
196
197
198
199
200
201
202
	 *
	 * @param int $timestamp Timestamp of when to run the action.
	 * @param int $interval How long to wait between runs.
	 * @param string $hook The name of the WordPress action that is being registered.
	 * @param array $args An array of arguments to pass.
	 * @param bool $overwrite Whether to replace existing scheduled action or not.
	 */
Michael Iseard's avatar
Michael Iseard committed
203
204
205
206
207
208
209
	public static function schedule_recurring_action(
		int $timestamp,
		int $interval,
		string $hook,
		array $args = [],
		bool $overwrite = false
	) {
210
211
212
213

		if ( class_exists( 'ActionScheduler' ) ) {

			if ( $overwrite ) {
Michael Iseard's avatar
Michael Iseard committed
214
				as_unschedule_action( $hook, $args );
215
216
217
218
219
220
221
222
223
224
225
226
			}

			if ( false === as_next_scheduled_action( $hook, $args ) ) {

				as_schedule_recurring_action( $timestamp, $interval, $hook, $args );
			}
		} else {
			do_action( $hook, $args );
		}

	}

227
	/**
228
	 * Returns human-readable filesize from given bytes.
229
230
231
232
233
234
235
236
237
238
239
240
241
242
	 *
	 * @param int $bytes Size of file in bytes. Usually the value returned from filesize().
	 * @param int $decimals Number of decimal places to return.
	 *
	 * @return string
	 * @link https://www.php.net/manual/en/function.filesize.php
	 */
	public static function human_filesize( int $bytes, int $decimals = 2 ): string {
		$sz     = 'BKMGTP';
		$factor = floor( ( strlen( $bytes ) - 1 ) / 3 );

		return sprintf( "%.{$decimals}f", $bytes / pow( 1024, $factor ) ) . @$sz[ $factor ];
	}

243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
	/**
	 * Uses regex that accepts any word character or hyphen in last name.
	 *
	 * @param $name
	 * @source https://stackoverflow.com/questions/13637145/split-text-string-into-first-and-last-name-in-php
	 *
	 * @return array
	 */
	public static function split_name( $name ): array {
		$name       = trim( $name );
		$last_name  = ( strpos( $name, ' ' ) === false ) ? '' : preg_replace( '#.*\s([\w-]*)$#', '$1', $name );
		$first_name = trim( preg_replace( '#' . preg_quote( $last_name, '#' ) . '#', '', $name ) );

		return array( $first_name, $last_name );
	}

Michael Iseard's avatar
Michael Iseard committed
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
	/**
	 * Returns an image's base64 encoded data URI for use in 'src' attribute.
	 *
	 * @param string $image_url Url of image to be encoded.
	 *
	 * @return string
	 * @link https://www.genieblog.ch/blog/en/2018/how-to-encode-an-svg-for-the-src-attribute-using-php/
	 */
	public static function get_data_uri( string $image_url ): string {

		// Get the filetype of supplied image URL.
		$filetype = pathinfo( $image_url, PATHINFO_EXTENSION );

		// Get the contents of the file.
		$request  = wp_remote_get( $image_url );
		$response = wp_remote_retrieve_body( $request );

		// Return data URI if there is data.
		if ( $response ) {

			/*
			 * Don't base64 encode svg.
			 * @link https://css-tricks.com/probably-dont-base64-svg/
			 */
			if ( 'svg' === $filetype ) {
				return 'data:image/svg+xml,' . rawurlencode( $response );
			}

			// All other image types get base64 encoded.
			return 'data:image/' . $filetype . ';base64,' . base64_encode( $response );
		}

		// Return url if nothing found.
		return $image_url;
	}

295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
	/**
	 * Truncates string at specified length and return with ellipsis
	 * if longer.
	 *
	 * @param string $string
	 * @param int $length
	 *
	 * @return string
	 */
	public static function truncate_string(string $string, int $length): string {

		return strlen($string) > $length ? substr($string, 0, $length) . "..." : $string;

	}

310
}