/
functions.php
142 lines (128 loc) · 2.82 KB
/
functions.php
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* Useful functions to use throughout the plugin
*
* @since 1.4.0
*/
use WCPress\WCP\Constants;
/**
* Gets admin template from plugins templates directory
*
* @since 1.4.0
*
* @param string $relative_path
*
* @return void
*/
function wcp_get_admin_template( $relative_path ) {
$full_path = WC_CALL_FOR_PRICE_TEMPLATE_PATH . 'templates/admin/' . $relative_path;
$template_path = apply_filters( 'wcp_get_admin_template_path_before_render', $full_path );
if ( file_exists( $template_path ) ) {
require_once( $template_path );
} elseif ( WP_DEBUG || WP_DEBUG_LOG ) {
error_log( "WC Call For Price: Template file not found: {$template_path}" );
}
}
/**
* Generates admin submenu pages url
*
* @since 1.4.0
*
* @param string $slug
*
* @return string
*/
function wcp_slug_to_admin_menu_url( $slug = Constants::WCP_SUB_PAGE_GENERAL_SETTINGS ): string {
$admin_main_url = add_query_arg(
'page',
'wc-call-for-price',
get_admin_url() . 'admin.php'
);
$admin_sub_url = esc_url(
add_query_arg(
Constants::WCP_SUB_PAGE_QUERY_STRING,
$slug,
$admin_main_url
)
);
return apply_filters( 'wcp_slug_to_admin_menu_url', $admin_sub_url );
}
/**
* Return admin menu sub page slug
*
* @since 1.4.0
*
* @return string
*/
function wcp_get_admin_sub_page_slug(): string {
return isset( $_GET[ Constants::WCP_SUB_PAGE_QUERY_STRING ] )
? sanitize_text_field( $_GET[ Constants::WCP_SUB_PAGE_QUERY_STRING ] )
: Constants::WCP_SUB_PAGE_GENERAL_SETTINGS;
}
/**
* Return if a checkbox is on
*
* @since 1.4.0
*
* @param $option_name
*
* @return bool
*/
function wcp_is_on( $option_name ): bool {
return get_option( $option_name, Constants::OFF ) == Constants::ON;
}
/**
* Checks if the option is not empty
*
* @since 1.4.0
* @param $option_name
* @return bool
*/
function wcp_not_empty( $option_name ): bool {
return ! empty( get_option( $option_name ) );
}
/**
* @param $option_name
* @param $compare_to
* @param $echo
*
* @return bool
*/
function wcp_is_checked( $option_name, $compare_to = Constants::ON, $echo = true ): bool {
$value = get_option( $option_name, Constants::OFF );
$result = $value === $compare_to;
if ( $result ) {
if ( $echo ) {
echo 'checked';
}
return true;
}
return false;
}
/**
* Returns a unified time type across the plugin
*
* @since 1.4.3
*
* @return int
*/
function wcp_current_time(): int {
return (new \DateTime())->getTimestamp();
}
/**
* Checks if in WCP Settings page
*
* @since 1.4.3
*
* @return bool
*/
function wcp_is_settings_page(): bool {
$pagename = '';
if ( is_admin() && isset( $_GET['page'] ) ) {
$pagename = sanitize_text_field( $_GET['page'] );
}
if ( 'wc-call-for-price' === $pagename ) {
return true;
}
return false;
}