-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
144 lines (119 loc) · 3.68 KB
/
functions.php
File metadata and controls
144 lines (119 loc) · 3.68 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
<?php
namespace SearchBlox;
if (!defined("ABSPATH")) exit;
/**
* Alias of array_search except it searches recursively
* @param $needle string
* @param $haystack array
* @return bool|int|string
*/
function recursive_array_search($needle,$haystack)
{
foreach($haystack as $key=>$value) {
$current_key=$key;
if($needle === $value OR (is_array($value) && recursive_array_search($needle, $value) !== false)) {
return $current_key;
}
}
return false;
}
/**
* Delete custom terms and their taxonomy
* @param string $taxonomy
* @return void
*/
function delete_custom_terms($taxonomy)
{
/**
* @var wpdb $wpdb
*/
global $wpdb;
$query = 'SELECT t.name, t.term_id
FROM ' . $wpdb->terms . ' AS t
INNER JOIN ' . $wpdb->term_taxonomy . ' AS tt
ON t.term_id = tt.term_id
WHERE tt.taxonomy = "' . $taxonomy . '"';
$terms = $wpdb->get_results($query);
foreach ($terms as $term) {
wp_delete_term( $term->term_id, $taxonomy );
}
}
/**
* Simplify serialized array data coming from jQuery Ajax method $.serializeArray()
* @param $array array
* @return void
*/
function simplify_serialize_data(&$array)
{
if (is_array($array) && count($array) > 0) {
$new_array = array();
foreach ($array as $arr) {
if (isset($arr['name'], $arr['value'])) {
$new_array[$arr['name']] = sanitize_text_field($arr['value']);
}
}
$array = $new_array;
}
}
/**
* Validate Names either first or lastm
* @param string $str
* @return bool
*/
function validate_name($str = '')
{
return (preg_match('#^[a-zA-Z\s?]+$#', $str) === 0) ? false : true;
}
/**
* Alias of include construct except it returns instead of output
* @param $template string
* @param array $data
* @return string
*/
function return_include_once($template, $data = array())
{
ob_start();
if (!empty($data) && (is_array($data) || is_object($data) )) extract($data);
include RW_TEMPLATES . $template;
return ob_get_clean();
}
function get_image_id($product_id = 0)
{
return get_post_meta($product_id, '_do_image_id', true);
}
function get_size_id($product_id = 0)
{
return get_post_meta($product_id, '_do_size_id', true);
}
function get_region_id($product_id = 0)
{
return get_option('_do_region_' . $product_id);
}
function destroyDroplet($user_id, $subscription_key)
{
$droplets = get_user_meta($user_id, '_sb_droplets', true);
$all_droplets_destroyed = false;
if (!empty($droplets)) {
foreach ($droplets as $key => $droplet) {
if ($subscription_key != $droplet['subscription_key']) continue;
$all_droplets_destroyed = false;
$droplet_id = $droplet['id'];
$destory = API::get("droplets/{$droplet_id}/destroy");
$destory_status = $destory->jsonDecode()->getResponse();
if ($destory_status['status'] == "OK") {
unset($droplets[$key]);
update_user_meta($user_id, '_sb_droplets', $droplets);
$all_droplets_destroyed = true;
}
}
if ($all_droplets_destroyed) {
$new_droplets = get_user_meta($user_id, '_sb_droplets_new', true);
if (empty($new_droplets)) return;
foreach ($new_droplets as $key => $droplet) {
if ($droplet['subscription_key'] != $subscription_key) continue;
unset($new_droplets[$key]);
update_user_meta($user_id, '_sb_droplets_new', $new_droplets);
}
}
}
}