-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.php
More file actions
264 lines (219 loc) · 6.91 KB
/
helper.php
File metadata and controls
264 lines (219 loc) · 6.91 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
if ( ! function_exists( 'sub_category_mapping' ) ) {
/**
* It takes an array of terms and returns an array of terms with sub-terms
*
* @link https://stackoverflow.com/questions/4843945/php-tree-structure-for-categories-and-sub-categories-without-looping-a-query
*
* @param terms The terms you want to map.
*/
function sub_category_mapping( $terms ) {
$terms = json_decode(
json_encode(
$terms
)
);
$childs = array();
foreach ( $terms as $item ) {
$childs[ $item->parent ][] = $item;
}
foreach ( $terms as $item ) {
if ( isset( $childs[ $item->term_id ] ) ) {
$item->sub_terms = $childs[ $item->term_id ];
}
}
return $childs[0];
}
}
if ( ! function_exists( 'validate_chackout_fields' ) ) {
/**
* It takes an array of form fields and returns an array of missing fields
*
* @param form_fields An array of form fields.
*
* @return array of missing fields.
*/
function validate_chackout_fields( $form_fields ) {
$chackout_fields = WC()->checkout()->checkout_fields['billing'];
$form_fields = array_merge(
...array_map(
function( $el ) {
$name = preg_replace( '/[^a-zA-Z0-9\_\-]/i', '', $el['name'] );
$value = preg_replace( '/[^a-zA-Z0-9\.\@\_\-\s]/i', '', $el['value'] );
return array( $name => $value );
},
$form_fields
)
);
$missing_fields = array();
foreach ( $chackout_fields as $key => $field ) {
if ( $field['required'] && ! in_array( $key, array_keys( $form_fields ), true ) ) {
array_push( $missing_fields, $key );
}
}
return $missing_fields;
}
}
// if ( ! function_exists( 'scheen_post_content' ) ) {
// /**
// * It takes a post ID, and returns a screenshot of the post's content
// *
// * @param int post_id The ID of the post you want to screenshot.
// *
// * @return array.
// */
// function scheen_post_content( int $post_id ) {
// return wpr_get_url_screen(
// sprintf( '%s/wp-json/wpr-get-post-content?post_id=%d', home_url(), $post_id )
// );
// }
// }
// if ( ! function_exists( 'scheen_product_content' ) ) {
// /**
// * It takes a product ID, and returns a screenshot of the post's content
// *
// * @param int product_id The ID of the post you want to screenshot.
// *
// * @return array.
// */
// function scheen_product_content( int $product_id ) {
// return wpr_get_url_screen(
// sprintf( '%s/wp-json/wpr-get-product-content?product_id=%d', home_url(), $product_id )
// );
// }
// }
if ( ! function_exists( 'wpr_get_url_screen' ) ) {
/**
* It takes a URL and returns a screenshot of that URL
*
* @param string url The URL of the page you want to screenshot.
*
* @return array response from the API.
*/
// API key https://rapidapi.com/Cobbex/api/screen-it2
function wpr_get_url_screen( string $url ) {
$curl = curl_init();
curl_setopt_array(
$curl,
array(
CURLOPT_URL => 'https://screen-it2.p.rapidapi.com/screenshot',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode(
array(
'url' => $url,
'delay' => 0,
'timeout' => 60,
'width' => 800,
'height' => 1080,
'crop' => false,
'format' => 'jpeg',
'fresh' => true,
)
),
CURLOPT_HTTPHEADER => array(
'X-RapidAPI-Host: screen-it2.p.rapidapi.com',
'X-RapidAPI-Key: API KEY',
'content-type: application/json',
),
)
);
$response = '';//curl_exec( $curl );
$err = curl_error( $curl );
curl_close( $curl );
if ( $err ) {
return array( 'error' => $err );
} else {
return json_decode( $response, true );
}
}
}
if ( ! function_exists( 'save_scheen_content' ) ) {
/**
* It downloads a file from a URL and saves it to a destination
*
* @param string url The URL of the screenshot to save.
* @param string dest The destination file path.
*
* @return string file path of the saved file.
*/
function save_scheen_content( string $url, string $dest ) {
$fp = fopen( $dest, 'w+' );
$ch = curl_init();
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_FILE, $fp );
curl_setopt( $ch, CURLOPT_HEADER, false );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
$result = curl_exec( $ch );
curl_close( $ch );
fclose( $fp );
return $result ? $dest : '';
}
}
if ( ! function_exists( 'wpr_get_post_content' ) ) {
/**
* It takes a post ID, and returns the post content as a full HTML document
*
* @param int post_id The ID of the post you want to get the content from.
* @param string css_path The path to the CSS file you want to include.
* @param string js_path The path to the JavaScript file you want to include.
*/
function wpr_get_post_content( int $post_id, string $css_path = '', string $js_path = '' ) {
$post = get_post( $post_id );
if ( ! empty( $css_path ) ) {
$css_path = sprintf( '<link type="text/css" rel="stylesheet" href="%s">', $css_path );
}
if ( ! empty( $js_path ) ) {
$js_path = sprintf( '<script src="%s"></script>', $js_path );
}
return sprintf(
'<html><head>%s</head><body>%s%s</body></html>',
$css_path,
$post->post_content,
$js_path
);
}
}
if ( ! function_exists( 'wpr_hide_php_errors' ) ) {
/**
* It hides PHP errors.
*/
function wpr_hide_php_errors() {
// error_reporting( 0 );
// ini_set( 'display_errors', false );
}
}
if ( ! function_exists( 'wpr_auth_api_user_id' ) ) {
/**
* If the user is logged in, return the user ID. If not, return false
*
* @param \WP_REST_Request request The request object.
*
* @return int|false
*/
function wpr_auth_api_user_id( \WP_REST_Request $request ) {
$params = $request->get_params();
$protocol = ( isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ? 'https://' : 'http://' );
$bearer_full = preg_replace( '/[^a-zA-Z0-9\.\-\_\s]/i', '', $request->get_header( 'Authorization' ) );
$bearer = str_replace( 'Bearer ', '', $bearer_full );
$url = $protocol . $_SERVER['HTTP_HOST'];
$user_id = ( ! empty( $params['user_id'] ) ? preg_replace( '/[^0-9]/i', '', $params['user_id'] ) : 0 );
$simple_jwt_login = new \SimpleJwtLoginClient\SimpleJwtLoginClient( $url, '/simple-jwt-login/v1' );
$result = $simple_jwt_login->validateToken( $bearer );
if ( ! empty( $result ) && $result['success'] && $result['data']['user']['ID'] === $user_id ) {
return $user_id;
} else {
// $simple_jwt_login = new \SimpleJwtLoginClient\SimpleJwtLoginClient( 'https://simplejwtlogin.com', '/simple-jwt-login/v1' );
// $result = $simple_jwt_login->refreshToken( 'your JWT here', 'AUTH CODE' );
// $token = $result['data']['jwt'],
}
return false;
}
}