Utils.php
1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/**
* Class Utils
*
* @package WooCommerce\Payments\Utils
*/
namespace WCPay\MultiCurrency;
defined( 'ABSPATH' ) || exit;
/**
* Class that controls Multi-Currency Utils.
*/
class Utils {
/**
* Checks backtrace calls to see if a certain call has been made.
*
* @param array $calls Array of the calls to check for.
*
* @return bool True if found, false if not.
*/
public function is_call_in_backtrace( array $calls ): bool {
$backtrace = wp_debug_backtrace_summary( null, 0, false ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
foreach ( $calls as $call ) {
if ( in_array( $call, $backtrace, true ) ) {
return true;
}
}
return false;
}
/**
* Checks the query_vars array for a particular pagename and variable to be set.
*
* @param array $pages Array of the pagenames to check for.
* @param array $vars Array of the vars to check for.
*
* @return bool True if found, false if not.
*/
public function is_page_with_vars( array $pages, array $vars ): bool {
global $wp;
if ( $wp->query_vars && isset( $wp->query_vars['pagename'] ) && in_array( $wp->query_vars['pagename'], $pages, true ) ) {
foreach ( $vars as $var ) {
if ( isset( $wp->query_vars[ $var ] ) ) {
return true;
}
}
}
return false;
}
/**
* Checks if is a REST API request and the HTTP referer matches admin url.
*
* @return boolean
*/
public static function is_admin_api_request(): bool {
return 0 === stripos( wp_get_referer(), admin_url() ) && WC()->is_rest_api_request();
}
/**
* Writes the session into the client cookie.
*
* @param bool $set Should the session cookie be set.
*
* @return void
*/
public static function set_customer_session_cookie( bool $set ) {
WC()->session->set_customer_session_cookie( $set );
}
}