-
Notifications
You must be signed in to change notification settings - Fork 1
/
wpsstm-functions.php
174 lines (146 loc) · 4.66 KB
/
wpsstm-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
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
<?php
/**
* Get a value in a multidimensional array
* http://stackoverflow.com/questions/1677099/how-to-use-a-string-as-an-array-index-path-to-retrieve-a-value
* @param type $keys
* @param type $array
* @return type
*/
function wpsstm_get_array_value($keys = null, $array){
if (!$keys) return $array;
$keys = (array)$keys;
$first_key = $keys[0];
if(count($keys) > 1) {
if ( isset($array[$keys[0]]) ){
return wpsstm_get_array_value(array_slice($keys, 1), $array[$keys[0]]);
}
}elseif (isset($array[$first_key])){
return $array[$first_key];
}
return false;
}
function wpsstm_is_associative_array(array $arr){
if (array() === $arr) return false;
return array_keys($arr) !== range(0, count($arr) - 1);
}
//clean all empty values from array
function wpsstm_clean_array($array){
if (is_array($array)){
foreach ($array as $key => $sub_array){
$result = wpsstm_clean_array($sub_array);
if ($result === false)
{
unset($array[$key]);
}
else
{
$array[$key] = $result;
}
}
}
if (empty($array)) {
return false;
}
return $array;
}
/**
* Outputs the html readonly attribute. Inspired by core function disabled().
*
* Compares the first two arguments and if identical marks as readonly
*
* @since 3.0.0
*
* @param mixed $readonly One of the values to compare
* @param mixed $current (true) The other value to compare if not just true
* @param bool $echo Whether to echo or just return the string
* @return string html attribute or empty string
*/
function wpsstm_readonly( $readonly, $current = true, $echo = true ) {
return __checked_selected_helper( $readonly, $current, $echo, 'readonly' );
}
function wpsstm_required( $readonly, $current = true, $echo = true ) {
return __checked_selected_helper( $readonly, $current, $echo, 'required' );
}
/*
Locate a template & fallback in plugin's folder
*/
function wpsstm_locate_template( $template_name, $load = false, $require_once = true ) {
if ( !$located = locate_template( 'wpsstm/' . $template_name ) ) {
// Template not found in theme's folder, use plugin's template as a fallback
$located = wpsstm()->plugin_dir . 'templates/' . $template_name;
}
if ( $load && ('' != $located) ){
load_template( $located, $require_once );
}
return $located;
}
function wpsstm_get_url_domain($url){
//https://stackoverflow.com/a/16027164/782013
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : $pieces['path'];
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
return $regs['domain'];
}
return false;
}
function wpsstm_is_local_file($url){
$domain = wpsstm_get_url_domain( get_site_url() );
$file_domain = wpsstm_get_url_domain($url);
return ($domain == $file_domain);
}
/*
Simple check for XSPF urls; by URL extension.
//TOUFIX TOUCHECK should we rather check for a mime? But sometimes xspf mimetype is set wrongly.
*/
function wpsstm_is_xpsf_url($url){
$ext = null;
//we don't want url parameters
if ( ( $url = parse_url($url) ) && isset($url['path']) ) {
$ext = pathinfo($url['path'], PATHINFO_EXTENSION);
}
$bool = ($ext === 'xspf');
return apply_filters('wpsstm_xpsf_url',$bool);
}
//https://gist.github.com/boonebgorges/5510970
function wpsstm_recursive_parse_args( &$a, $b ) {
$a = (array) $a;
$b = (array) $b;
$r = $b;
foreach ( $a as $k => &$v ) {
if ( is_array( $v ) && isset( $r[ $k ] ) ) {
$r[ $k ] = wpsstm_recursive_parse_args( $v, $r[ $k ] );
} else {
$r[ $k ] = $v;
}
}
return $r;
}
function wpsstm_is_backend(){
return ( is_admin() && !wp_doing_ajax() );
}
function wpsstm_shorten_text($str,$skiptext = ' ... '){
$length = strlen($str);
if($length > 45){
$length = $length - 30;
$first = substr($str, 0, -$length);
$last = substr($str, -15);
$new = $first.$skiptext.$last;
return $new;
}else{
return $str;
}
}
function wpsstm_get_notice($msg){
return sprintf('<div class="wpsstm-block-notice"><span>%s</span><a href="#" class="wpsstm-close-notice"><i class="fa fa-close"></i></a></div>',$msg);
}
function wpsstm_array_remove_empty($haystack){
foreach ($haystack as $key => $value) {
if (is_array($value)) {
$haystack[$key] = wpsstm_array_remove_empty($haystack[$key]);
}
if (empty($haystack[$key])) {
unset($haystack[$key]);
}
}
return $haystack;
}