PluginProbe
GetResponse Forms by Optin Cat / 1.3.5
GetResponse Forms by Optin Cat v1.3.5
1.3.4 1.3.5 1.3.6 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 2.0.0 All 36 releases
getresponse / includes / skelet / core / lib / K / K.php

K.php in GetResponse Forms by Optin Cat 1.3.5, at includes/skelet/core/lib/K/K.php

449 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The k framework
4 *
5 * @author Nabil Kadimi <[email protected]>
6 * @version 1.0.4
7 * @package k_framework
8 */
9 class K {
10
11 /**
12 * Gets a variable
13 */
14 static function get_var( $name, $array = null, $default = null ) {
15
16 if( is_null( $array ) ) {
17 $array = $GLOBALS;
18 }
19 if( is_array( $array ) && array_key_exists( $name, $array ) ) {
20 return $array[ $name ];
21 } else {
22 return $default;
23 }
24 }
25
26 /**
27 * Prints or returns an input field
28 */
29 static function input( $name ) {
30
31 // $params
32 if( func_num_args() > 1 ) {
33 $params = func_get_arg(1);
34 }
35 if( empty( $params ) ) {
36 $params = array();
37 }
38
39 // $args
40 if( func_num_args() > 2 ) {
41 $args = func_get_arg(2);
42 }
43 if( empty( $args ) ) {
44 $args = array();
45 }
46
47 // Load defaults
48 $params += array(
49 'type' => 'text',
50 'id' => '',
51 'value' => ''
52 );
53
54 // Add name
55 $params[ 'name' ] = $name;
56
57 // Build the input field html
58 $input = sprintf( '<input %s/>', K::params_str( $params ) );
59
60 // Format
61 if( ! empty ( $args[ 'format' ] ) ) {
62 $input = str_replace(
63 array( ':input', ':name', ':id', ':value' ),
64 array( $input, $name, $params[ 'id'], $params[ 'value'] ),
65 $args[ 'format' ]
66 );
67 }
68
69 // Add default color picker
70 if(
71 ! empty ( $args[ 'colorpicker' ] )
72 || (
73 'text' === $params[ 'type' ]
74 && preg_match( '/_color\]?$/', $name)
75 && empty( $args[ 'nocolorpicker' ] )
76 )
77 ) {
78 wp_enqueue_style( 'wp-color-picker' );
79 wp_enqueue_script( 'wp-color-picker' );
80 ob_start();
81 ?>
82 <script>
83 jQuery( 'document' ).ready( function($) {
84 $( '[name="<?php echo $name; ?>"]' ).wpColorPicker();
85 });
86 </script>
87 <?php
88 $input .= ob_get_clean();
89 }
90
91 // Print or return the input field HTML
92 if( ! empty( $args[ 'return' ] ) ) {
93 return $input;
94 } else {
95 echo $input;
96 }
97 }
98
99 /**
100 * Prints or returns an input field
101 */
102 static function textarea( $name ) {
103
104 // $params
105 if( func_num_args() > 1 ) {
106 $params = func_get_arg(1);
107 }
108 if( ! is_array( $params ) ) {
109 $params = array();
110 }
111
112 // $args
113 if( func_num_args() > 2 ) {
114 $args = func_get_arg(2);
115 }
116 if( ! is_array( $args ) ) {
117 $args = array();
118 }
119
120 // Load defaults
121 $params += array(
122 'id' => '',
123 );
124
125 // Add name
126 $params[ 'name' ] = $name;
127
128 // Set $value
129 $value = empty( $args[ 'value' ] ) ? '' : $args[ 'value' ];
130
131 // Build textarea html
132 if( K::get_var( 'editor', $args ) ) {
133 // Remove the name since it's attached to the editor
134 $params_for_editor = $params;
135 unset( $params_for_editor[ 'name' ] );
136 // Build
137 ob_start();
138 wp_editor(
139 $value,
140 str_replace( array( '[', ']' ), '_', $name ) . mt_rand( 100, 999 ),
141 array(
142 'editor_height' => K::get_var( 'editor_height', $args ),
143 'media_buttons' => K::get_var( 'media_buttons', $args, TRUE ),
144 'teeny' => K::get_var( 'teeny', $args ),
145 'textarea_name' => $name,
146 'textarea_rows' => K::get_var( 'textarea_rows', $args, 20 ),
147 )
148 );
149 $textarea = ob_get_clean();
150 $textarea = sprintf( '<div %s>%s</div>', K::params_str( $params_for_editor ), $textarea );
151 } else {
152 $textarea = sprintf( '<textarea %s>%s</textarea>', K::params_str( $params ), $value );
153 }
154
155 // Format
156 if( ! empty ( $args[ 'format' ] ) ) {
157 $textarea = str_replace(
158 array( ':textarea', ':value', ':name', ':id' ),
159 array( $textarea, $value, $name, $params[ 'id' ] ),
160 $args[ 'format' ]
161 );
162 }
163
164 // Print or return the textarea field HTML
165 if( ! empty( $args[ 'return' ] ) ) {
166 return $textarea;
167 } else {
168 echo $textarea;
169 }
170 }
171
172 /**
173 * Prints or returns an dropdown select
174 */
175 static function select( $name ) {
176
177 // $params
178 if( func_num_args() > 1 ) {
179 $params = func_get_arg(1);
180 }
181 if( empty( $params ) ) {
182 $params = array();
183 }
184 // Load defaults
185 $params += array(
186 'id' => '',
187 );
188
189 // Sanitize $params[multiple], and Add brackets if the former is true
190 if( ! empty( $params[ 'multiple' ] ) ) {
191 $params[ 'multiple' ] = 'multiple';
192 $name .= '[]';
193 }
194
195 // Add name
196 $params[ 'name' ] = $name;
197
198 // $args
199 if( func_num_args() > 2 ) {
200 $args = func_get_arg(2);
201 }
202 if( empty( $args ) ) {
203 $args = array();
204 }
205 $args += array(
206 'default' => '',
207 'options' => array(),
208 'html_before' => '',
209 'html_after' => '',
210 'selected' => '',
211 );
212
213 // Make 'selected' an array
214 if( $selected = $args[ 'selected' ] ) {
215 if( ! is_array( $selected ) ) {
216 $selected = array( $selected );
217 }
218 }
219
220 // Use 'default' is 'selected' is empty
221 if( ! $selected ) {
222 $selected = array( $args[ 'default' ] );
223 }
224
225 // Build options
226 $options = '';
227 foreach ( $args[ 'options' ] as $value => $label ) {
228 $options .= K::wrap(
229 $label
230 , array(
231 'value' => $value,
232 'selected' => ( in_array( $value, $selected ) )
233 ? 'selected'
234 : null
235 ,
236 )
237 , array(
238 'in' => 'option',
239 'return' => true,
240 )
241 );
242 }
243
244 // Build the input field html
245 $select = sprintf( '%s<select %s>%s</select>%s', $args[ 'html_before' ], K::params_str( $params ), $options, $args[ 'html_after' ] );
246
247 // Format
248 if( ! empty ( $args[ 'format' ] ) ) {
249 $select = str_replace(
250 array( ':select', ':name', ':id' ),
251 array( $select, $name, $params[ 'id'] ),
252 $args[ 'format' ]
253 );
254 }
255
256 // Print or return the input field HTML
257 if( ! empty( $args[ 'return' ] ) ) {
258 return $select;
259 } else {
260 echo $select;
261 }
262 }
263
264 /**
265 * Prints or returns an input field
266 *
267 * @param array $controls The array of controls
268 */
269 static function fieldset( $legend, $controls = array() ) {
270
271 // $params
272 if( func_num_args() > 2 ) {
273 $params = func_get_arg(2);
274 }
275 if( empty( $params ) ) {
276 $params = array();
277 }
278
279 // $args
280 if( func_num_args() > 3 ) {
281 $args = func_get_arg(3);
282 }
283 if( empty( $args ) ) {
284 $args = array();
285 }
286
287 // Inner HTML placeholder
288 $innerHTML = '';
289
290 // Put controls in placehoder
291 foreach( $controls as $control ) {
292
293 // Fill params if needed
294 $control[2] = ! empty( $control[2] ) ? $control[2] : array();
295
296 // Set $args['return'] to false
297 if( empty( $control[3] ) ) {
298 $control[3] = array();
299 }
300 $control[3][ 'return' ] = true;
301
302 // Get control HTML
303 $innerHTML .= call_user_func(
304 /* Callback */ 'K::' . $control[0],
305 /* Name/content */ $control[1],
306 /* Params*/ $control[2],
307 /* Args */ $control[3]
308 );
309 }
310
311 // Prepare HTML
312 $HTML = str_replace(
313 array( ':legend', ':controls', ':parameters' ),
314 array(
315 ! empty( $legend ) ? $legend : '',
316 $innerHTML,
317 K::params_str( $params )
318 ),
319 '<fieldset :parameters><legend>:legend</legend>:controls</fieldset>'
320 );
321
322 // Print or return the input field HTML
323 if( ! empty ( $args[ 'return' ] ) ) {
324 return $HTML;
325 } else {
326 echo $HTML;
327 }
328 }
329
330 /**
331 * Wraps given input in an html tag
332 */
333 static function wrap( $content = '' ) {
334
335 // $params
336 if( func_num_args() > 1 ) {
337 $params = func_get_arg(1);
338 }
339 if( empty( $params ) ) {
340 $params = array();
341 }
342
343 // $args
344 if( func_num_args() > 2 ) {
345 $args = func_get_arg(2);
346 }
347 if( empty( $args ) ) {
348 $args = array();
349 }
350 $args += array(
351 'in' => 'div',
352 'html_before' => '',
353 'html_after' => '',
354 );
355
356 // Build the input field html
357 $html = sprintf( '%s<%s %s>%s</%s>%s',
358 $args[ 'html_before' ],
359 $args[ 'in' ],
360 K::params_str( $params ),
361 $content,
362 $args[ 'in' ],
363 $args[ 'html_after' ]
364 );
365
366 // Print or return the input field HTML
367 if( ! empty( $args[ 'return' ] ) ) {
368 return $html;
369 } else {
370 echo $html;
371 }
372 }
373
374 /**
375 * Prepares an array of params and their values to be added to an html element
376 */
377 static function params_str( $params ) {
378 $params_str = '';
379 foreach( $params as $parameter => $value ) {
380 if( strlen( $value ) ) {
381 $params_str .= sprintf( ' %s="%s"', $parameter, $value);
382 }
383 }
384 return $params_str;
385 }
386 }
387
388 add_action( 'in_admin_footer', 'k_scripts' );
389 function k_scripts() {
390 ?>
391 <style>
392 fieldset.k {
393 border: solid 1px lightgray;
394 margin-bottom: 1em;
395 padding-left: .5em;
396 padding-right: .5em;
397 }
398 fieldset.k legend {
399 background: white;
400 padding: .25em .5em;
401 box-shadow: 0 0 3px silver;
402 transition: all .5s;
403 }
404 fieldset.k legend.highlighted {
405 background: lightgray;
406 box-shadow: 0 0 1px black;
407 }
408 fieldset.k.expanded legend {
409 font-weight: bold;
410 }
411 fieldset.k.collapsible legend {
412 cursor: pointer;
413 }
414 fieldset.k.collapsed :not(legend) {
415 display: none;
416 }
417 fieldset.k.collapsed {
418 display: inline;
419 border: none;
420 }
421 </style>
422 <script>
423 jQuery( document ).ready( function( $ ) {
424 $( '.collapsible legend' ).click( function (){
425 $this = $( this );
426 $fieldset = $this.parent();
427 // Collapse all except target
428 $fieldset.parent()
429 .find( 'fieldset' )
430 .not( $fieldset )
431 .removeClass( 'expanded' )
432 .addClass( 'collapsed' )
433 .trigger( 'collapse' );
434 // Toggle target
435 $fieldset.toggleClass( 'collapsed' );
436 // Add expanded class to non-collpased and vice-versa, then trigger events
437 if ( $fieldset.hasClass('collapsed') ) {
438 $fieldset.removeClass( 'expanded' );
439 $fieldset.trigger( 'collapse' );
440 } else {
441 $fieldset.addClass( 'expanded' );
442 $fieldset.trigger( 'expand' );
443 }
444 } );
445 } );
446 </script>
447 <?php
448 }
449