-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters.php
166 lines (147 loc) · 4.39 KB
/
filters.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
<?php
/**
* Add asterisk to the end of a label of a required field.
*
* @param $label
* @param $form_field ScaleUp_Form_Field
* @return string
*/
function scaleup_form_field_label_asterisk( $label, $form_field ) {
if ( $form_field->has( 'validation' ) && is_array( $form_field->get( 'validation' ) ) ) {
$validations = $form_field->get( 'validation' );
if ( is_array( $validations ) && in_array( 'required', $validations ) && !empty( $label ) ) {
$label .= '<span class="asterisk">*</span>';
}
}
return $label;
}
add_filter( 'scaleup_form_field_label', 'scaleup_form_field_label_asterisk', 10, 2 );
/**
* Normalize value and return the value in format that ScaleUp will be able to work with
*
* Possible $format values:
* 'string' - provided value is a string
* 'args_string' - provided value is an args strings in format $key=$value,$key1=$value1
*
* @param string $value
* @param string $format
* @return array
*/
function scaleup_normalize_value( $value, $format = 'string' ) {
switch ( $format ):
case 'args_string':
$value = wp_parse_args( $value );
break;
default:
$value = array( 'value' => $value );
endswitch;
return $value;
}
add_filter( 'scaleup_normalize_value', 'scaleup_normalize_value', 10, 2 );
/**
* Expand an associative $args array into an array of arrays with value keys
*
* array( 'hello' => 'world', 'h2' => 'w2' ) becomes array( 'hello' => array( 'value' => 'world' ), 'h2' => array( 'value' => 'w2' ) );
*
* @param array $args
* @return array
*/
function scaleup_expand_args( $args = array() ) {
$result = array();
foreach ( $args as $key => $value ) {
if ( !is_null( $value ) ) {
$expanded = array(
'value' => $value,
);
$result[ $key ] = $expanded;
}
}
return $result;
}
add_filter( 'scaleup_expand_args', 'scaleup_expand_args' );
/**
* Flatten the array and return it in requested format
*
* @param array $args
* @param string $format ARRAY_A or ARRAY_N
* @return array
*/
function scaleup_flatten_args( $args, $format = ARRAY_A ) {
$output = array();
foreach ( $args as $key => $value ) {
if ( is_array( $value ) && isset( $value[ 'value' ] ) ) {
$value = $value[ 'value' ];
}
switch ( $format ):
case ARRAY_A:
$output[ $key ] = $value;
break;
case ARRAY_N:
$output[] = $value;
break;
endswitch;
}
return $output;
}
add_filter( 'scaleup_flatten_args', 'scaleup_flatten_args', 10, 2 );
/**
* Replaces variables in string template that uses {variable_name} syntax.
*
* @param $template
* @param $args
* @return string
*/
function scaleup_string_template( $template, $args ) {
$pattern = $template;
$len = strlen( $pattern );
$tokens = array();
$variables = array();
$pos = 0;
preg_match_all( '#.\{(\w+)\}#', $pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER );
foreach ( $matches as $match ) {
if ( $text = substr( $pattern, $pos, $match[ 0 ][ 1 ] - $pos ) ) {
$tokens[ ] = array( 'text', $text );
}
$pos = $match[ 0 ][ 1 ] + strlen( $match[ 0 ][ 0 ] );
$var = $match[ 1 ][ 0 ];
// Use the character preceding the variable as a separator
$separators = array( $match[ 0 ][ 0 ][ 0 ] );
if ( $pos !== $len ) {
// Use the character following the variable as the separator when available
$separators[ ] = $pattern[ $pos ];
}
$regexp = sprintf( '[^%s]+', preg_quote( implode( '', array_unique( $separators ) ), '#' ) );
$tokens[ ] = array( 'variable', $match[ 0 ][ 0 ][ 0 ], $regexp, $var );
if ( in_array( $var, $variables ) ) {
/**
* @todo: Add error that variable can't be used twice
*/
}
$variables[ ] = $var;
}
if ( $pos < $len ) {
$tokens[ ] = array( 'text', substr( $pattern, $pos ) );
}
$result = '';
foreach ( $tokens as $token ) {
if ( 'text' === $token[ 0 ] ) {
// Text tokens
$result .= $token[ 1 ];
}
if ( 'variable' === $token[ 0 ] ) {
// Variable tokens
$prefix = $token[ 1 ];
if ( isset( $args[ $token[ 3 ] ] ) ) {
$value = $args[ $token[ 3 ] ];
} else {
/**
* @todo: return an error if args doesn't provide value for variable.
*/
$value = '';
}
$result .= "$prefix$value";
}
}
return $result;
}
add_filter( 'scaleup_string_template', 'scaleup_string_template', 10, 2 );