-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.php
129 lines (107 loc) · 2.95 KB
/
helpers.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
<?php
/**
* Check if the user has a certain role
*
* es.: user_is( 'administrator', 1 )
*
* @param string $role
* @param int|WP_User $user_id
*
* @return bool
*/
function user_is( $role, $user_id = null ) {
$user = $user_id;
if ( !( $user_id instanceof WP_User ) ) {
if ( is_numeric( $user_id ) )
$user = get_userdata( $user_id );
else
$user = wp_get_current_user();
}
if ( empty( $user ) )
return false;
return in_array( $role, (array)$user->roles );
}
/**
* Returns a meta_key (custom field) for the term in a taxonomy.
*
* If the desired meta_key does not exist, or no value is associated with it, FALSE will be returned.
*
* @param $taxonomy
* @param $term_id
* @param $meta_key
* @param bool $default
*
* @return mixed|void
*/
function get_term_meta( $taxonomy, $term_id, $meta_key, $default = false ) {
return get_option( 'tax_' . $taxonomy . '_' . $term_id . '_' . $meta_key, $default );
}
/**
* Updates the value of an existing meta key (custom field) for the specified term in a taxonomy.
*
* @param $taxonomy
* @param $term_id
* @param $meta_key
* @param $new_value
*
* @return bool
*/
function update_term_meta( $taxonomy, $term_id, $meta_key, $new_value ) {
return update_option( 'tax_' . $taxonomy . '_' . $term_id . '_' . $meta_key, $new_value );
}
/**
* Returns an array with all the parent terms AND the current term at the end
*
* @param string $taxonomy
* @param int $term_id
*
* @return array
*/
function get_parent_terms_ids( $taxonomy, $term_id ) {
if ( $term_id == 0 ) {
return array();
}
$term = get_term( $term_id, $taxonomy );
$parents = get_parent_terms_ids( $term->parent, $taxonomy );
$parents[] = $term_id;
return $parents;
}
if ( !function_exists( 'get_called_class' ) ) :
/**
* Fallback for PHP versions < 5.3.0
*/
function get_called_class() {
$bt = debug_backtrace();
return get_class( $bt[1]['object'] );
}
endif;
if ( !function_exists( 'in_2d_array' ) ) :
/**
* Finds the $needle key in a multidimensional array
* Similar to the in_array function
*
* @param int|string $needle
* @param array $haystack array to look in
* @param bool strict
*
* @return bool true if found, false otherwise
*/
function in_2d_array( $needle, $haystack, $strict = false ) {
foreach ( $haystack as $item ) {
if ( ( $strict ? $item === $needle : $item == $needle ) || ( is_array( $item ) && in_2d_array( $needle, $item, $strict ) ) ) {
return true;
}
}
return false;
}
endif;
if ( !function_exists( 'unset_2d' ) ) :
/**
* Unset the keys that have a $needle value in a multidimentional array
*/
function unset_2d( $needle, &$haystack ) {
foreach ( array_keys( $haystack, $needle ) as $index ) {
unset( $haystack[$index] );
}
}
endif;