PluginProbe
GetResponse Forms by Optin Cat / 2.4.1
GetResponse Forms by Optin Cat v2.4.1
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 / classes / k / k.php

k.php in GetResponse Forms by Optin Cat 2.4.1, at includes/classes/k/k.php

552 lines 11.9 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 <nabil@kadimi.com>
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 ob_start();
79 $input .= ob_get_clean();
80 }
81
82 // Print or return the input field HTML
83 if( ! empty( $args[ 'return' ] ) ) {
84 return $input;
85 } else {
86 echo $input;
87 }
88 }
89
90 /**
91 * Prints or returns an input field
92 */
93 static function textarea( $name ) {
94
95 // $params
96 if( func_num_args() > 1 ) {
97 $params = func_get_arg(1);
98 }
99 if( ! is_array( $params ) ) {
100 $params = array();
101 }
102
103 // $args
104 if( func_num_args() > 2 ) {
105 $args = func_get_arg(2);
106 }
107 if( ! is_array( $args ) ) {
108 $args = array();
109 }
110
111 // Load defaults
112 $params += array(
113 'id' => '',
114 );
115
116 // Add name
117 $params[ 'name' ] = $name;
118
119 // Set $value
120 $value = empty( $args[ 'value' ] ) ? '' : $args[ 'value' ];
121
122 // Build textarea html
123 if( K::get_var( 'editor', $args ) ) {
124 // Remove the name since it's attached to the editor
125 $params_for_editor = $params;
126 unset( $params_for_editor[ 'name' ] );
127 // Build
128 ob_start();
129 wp_editor(
130 $value,
131 str_replace( array( '[', ']' ), '_', $name ) . mt_rand( 100, 999 ),
132 array(
133 'editor_height' => K::get_var( 'editor_height', $args ),
134 'media_buttons' => K::get_var( 'media_buttons', $args, TRUE ),
135 'teeny' => K::get_var( 'teeny', $args ),
136 'textarea_name' => $name,
137 'textarea_rows' => K::get_var( 'textarea_rows', $args, 20 ),
138 )
139 );
140 $textarea = ob_get_clean();
141 $textarea = sprintf( '<div %s>%s</div>', K::params_str( $params_for_editor ), $textarea );
142 } else {
143 $textarea = sprintf( '<textarea %s>%s</textarea>', K::params_str( $params ), $value );
144 }
145
146 // Format
147 if( ! empty ( $args[ 'format' ] ) ) {
148 $textarea = str_replace(
149 array( ':textarea', ':value', ':name', ':id' ),
150 array( $textarea, $value, $name, $params[ 'id' ] ),
151 $args[ 'format' ]
152 );
153 }
154
155 // Print or return the textarea field HTML
156 if( ! empty( $args[ 'return' ] ) ) {
157 return $textarea;
158 } else {
159 echo $textarea;
160 }
161 }
162
163 /**
164 * Prints or returns an dropdown select
165 */
166 static function select( $name ) {
167
168 // $params
169 if( func_num_args() > 1 ) {
170 $params = func_get_arg(1);
171 }
172 if( empty( $params ) ) {
173 $params = array();
174 }
175 // Load defaults
176 $params += array(
177 'id' => '',
178 );
179
180 // Sanitize $params[multiple], and Add brackets if the former is true
181 if( ! empty( $params[ 'multiple' ] ) ) {
182 $params[ 'multiple' ] = 'multiple';
183 $name .= '[]';
184 }
185
186 // Add name
187 $params[ 'name' ] = $name;
188
189 // $args
190 if( func_num_args() > 2 ) {
191 $args = func_get_arg(2);
192 }
193 if( empty( $args ) ) {
194 $args = array();
195 }
196 $args += array(
197 'default' => '',
198 'options' => array(),
199 'html_before' => '',
200 'html_after' => '',
201 'selected' => '',
202 );
203
204 // Make 'selected' an array
205 if( $selected = $args[ 'selected' ] ) {
206 if( ! is_array( $selected ) ) {
207 $selected = array( $selected );
208 }
209 }
210
211 // Use 'default' if 'selected' is empty
212 if( ! $selected ) {
213 $selected = array( $args[ 'default' ] );
214 }
215
216 // Build options
217 $options = '';
218 foreach ( $args[ 'options' ] as $value => $label ) {
219 $options .= K::wrap(
220 $label
221 , array(
222 'value' => $value,
223 'selected' => ( in_array( $value, $selected ) )
224 ? 'selected'
225 : null
226 ,
227 )
228 , array(
229 'in' => 'option',
230 'return' => true,
231 )
232 );
233 }
234
235 // Build the input field html
236 $select = sprintf( '%s<select %s>%s</select>%s', $args[ 'html_before' ], K::params_str( $params ), $options, $args[ 'html_after' ] );
237
238 // Format
239 if( ! empty ( $args[ 'format' ] ) ) {
240 $select = str_replace(
241 array( ':select', ':name', ':id' ),
242 array( $select, $name, $params[ 'id'] ),
243 $args[ 'format' ]
244 );
245 }
246
247 // Print or return the input field HTML
248 if( ! empty( $args[ 'return' ] ) ) {
249 return $select;
250 } else {
251 echo $select;
252 }
253 }
254
255 /**
256 * Prints or returns an input field
257 *
258 * @param array $controls The array of controls
259 */
260 static function fieldset( $legend, $controls = array() ) {
261
262 // $params
263 if( func_num_args() > 2 ) {
264 $params = func_get_arg(2);
265 }
266 if( empty( $params ) ) {
267 $params = array();
268 }
269
270 // $args
271 if( func_num_args() > 3 ) {
272 $args = func_get_arg(3);
273 }
274 if( empty( $args ) ) {
275 $args = array();
276 }
277
278 // Inner HTML placeholder
279 $innerHTML = '';
280
281 // Put controls in placehoder
282 foreach( $controls as $control ) {
283
284 // Fill params if needed
285 $control[2] = ! empty( $control[2] ) ? $control[2] : array();
286
287 // Set $args['return'] to false
288 if( empty( $control[3] ) ) {
289 $control[3] = array();
290 }
291 $control[3][ 'return' ] = true;
292
293 // Get control HTML
294 $innerHTML .= call_user_func(
295 /* Callback */ 'K::' . $control[0],
296 /* Name/content */ $control[1],
297 /* Params*/ $control[2],
298 /* Args */ $control[3]
299 );
300 }
301
302 // Prepare HTML
303 $HTML = str_replace(
304 array( ':legend', ':controls', ':parameters' ),
305 array(
306 ! empty( $legend ) ? $legend : '',
307 $innerHTML,
308 K::params_str( $params )
309 ),
310 '<fieldset :parameters><legend>:legend</legend>:controls</fieldset>'
311 );
312
313 // Print or return the input field HTML
314 if( ! empty ( $args[ 'return' ] ) ) {
315 return $HTML;
316 } else {
317 echo $HTML;
318 }
319 }
320
321 /**
322 * Wraps given input in an html tag
323 */
324 static function wrap( $content = '' ) {
325
326 // $params
327 if( func_num_args() > 1 ) {
328 $params = func_get_arg(1);
329 }
330 if( empty( $params ) ) {
331 $params = array();
332 }
333
334 // $args
335 if( func_num_args() > 2 ) {
336 $args = func_get_arg(2);
337 }
338 if( empty( $args ) ) {
339 $args = array();
340 }
341 $args += array(
342 'in' => 'div',
343 'html_before' => '',
344 'html_after' => '',
345 );
346
347 // Build the input field html
348 $html = sprintf( '%s<%s %s>%s</%s>%s',
349 $args[ 'html_before' ],
350 $args[ 'in' ],
351 K::params_str( $params ),
352 $content,
353 $args[ 'in' ],
354 $args[ 'html_after' ]
355 );
356
357 // Print or return the input field HTML
358 if( ! empty( $args[ 'return' ] ) ) {
359 return $html;
360 } else {
361 echo $html;
362 }
363 }
364
365 /**
366 * Prepares an array of params and their values to be added to an html element
367 */
368 static function params_str( $params ) {
369 $params_str = '';
370 foreach( $params as $parameter => $value ) {
371 if( strlen( $value ) ) {
372 $params_str .= sprintf( ' %s="%s"', $parameter, $value);
373 }
374 }
375 return $params_str;
376 }
377 }
378
379 add_action( 'in_admin_footer', 'k_scripts' );
380 function k_scripts() {
381 ?>
382 <style>
383 fieldset.k {
384 border: solid 1px lightgray;
385 margin-bottom: 1em;
386 padding-left: .5em;
387 padding-right: .5em;
388 }
389 fieldset.k legend {
390 background: white;
391 padding: .25em .5em;
392 box-shadow: 0 0 3px silver;
393 transition: all .5s;
394 }
395 fieldset.k legend.highlighted {
396 background: lightgray;
397 box-shadow: 0 0 1px black;
398 }
399 fieldset.k.expanded legend {
400 font-weight: bold;
401 }
402 fieldset.k.collapsible legend {
403 cursor: pointer;
404 }
405 fieldset.k.collapsed :not(legend) {
406 display: none;
407 }
408 fieldset.k.collapsed {
409 display: inline;
410 border: none;
411 }
412 </style>
413 <script>
414 jQuery( document ).ready( function( $ ) {
415 $( '.collapsible legend' ).click( function (){
416 $this = $( this );
417 $fieldset = $this.parent();
418 // Collapse all except target
419 $fieldset.parent()
420 .find( 'fieldset' )
421 .not( $fieldset )
422 .removeClass( 'expanded' )
423 .addClass( 'collapsed' )
424 .trigger( 'collapse' );
425 // Toggle target
426 $fieldset.toggleClass( 'collapsed' );
427 // Add expanded class to non-collpased and vice-versa, then trigger events
428 if ( $fieldset.hasClass('collapsed') ) {
429 $fieldset.removeClass( 'expanded' );
430 $fieldset.trigger( 'collapse' );
431 } else {
432 $fieldset.addClass( 'expanded' );
433 $fieldset.trigger( 'expand' );
434 }
435 } );
436 } );
437 </script>
438 <?php
439 }
440
441 function k_selector( $name, $selected_options = array(), $return = false ) {
442
443 global $post;
444 // Dirty fix to restore the global $post
445 $post_bak = $post;
446
447 // Get all post types except media
448 $post_types = get_post_types( array( 'public' => true ) );
449 unset( $post_types[ 'attachment' ] );
450
451 $ret = ob_start();
452
453 // Start ouput
454 echo '<select data-placeholder="' . __( 'Type to search for posts, categories or pages.' ) . '" name = "' . $name . '[]" class="select2" multiple="multiple" style="width: 100%;">';
455
456 // Front page
457 K::wrap( __( 'Front page' )
458 , array(
459 'value' => '~',
460 'selected' => in_array( '~', $selected_options ),
461 )
462 , array( 'in' => 'option' )
463 );
464
465 foreach ($post_types as $post_type => $post_type_args ) {
466
467 $post_type_obj = get_post_type_object( $post_type );
468 $post_type_name = $post_type_obj->labels->singular_name;
469
470 $options = array();
471
472 // Add taxonomy/terms options
473 $taxonomies = get_object_taxonomies( $post_type );
474 foreach ( $taxonomies as $taxonomy ) {
475 $taxonomy_obj = get_taxonomy( $taxonomy );
476 $taxonomy_name = $taxonomy_obj->labels->singular_name;
477 $terms = get_categories("taxonomy=$taxonomy&type=$post_type");
478 foreach ($terms as $term) {
479 $options[ 'taxonomies' ][ "$post_type:$term->term_id" ] =
480 $post_type_name
481 . "$taxonomy_name"
482 . "$term->name"
483 ;
484 }
485 }
486
487 // Add posts options
488 $the_query = new WP_Query( "post_type=$post_type&posts_per_page=-1" );
489 if ( $the_query->have_posts() ) {
490
491 while ( $the_query->have_posts() ) {
492 $the_query->the_post();
493 $options[ 'posts' ][ '#' . get_the_ID() ] = $post_type_name
494 . ' ' . __( '' ) . ' '
495 . '#' . get_the_ID() . ' &ndash; '
496 . ( get_the_title() ? get_the_title() : __('[Untitled]') )
497 ;
498 }
499 }
500
501 // Dirty fix to restore the global $post
502 $post = $post_bak;
503
504 // Posts > All
505 echo '<optgroup label="' . $post_type_name . '">';
506 printf(
507 '<option value="%s" %s >%s</option>'
508 , $post_type
509 , ( in_array( $post_type, $selected_options ) ? 'selected' : '' )
510 , $post_type_name . ' ' . ( '' ) . ' ' . __( 'All' )
511 );
512 echo '</optgroup>';
513
514 // Posts > Taoxonomies
515 if ( ! empty( $options[ 'taxonomies' ] ) ) {
516 printf(
517 '<optgroup label="%s">'
518 , $post_type_name . ' ' . __( '' ) . ' ' . __( 'Taxonomies' )
519 );
520 foreach ( $options[ 'taxonomies' ] as $k => $v ) {
521 $selected = ( in_array( $k, $selected_options ) ) ? 'selected="selected"' : '';
522 printf( '<option value="%s" %s >%s</option>', $k, $selected, $v );
523 }
524 echo '</optgroup>';
525 }
526
527 // Posts > content
528 if ( ! empty( $options[ 'posts' ] ) ) {
529 printf( '<optgroup label="%s">'
530 , $post_type_name . ' ' . __( '' ) . ' ' . __( 'Content' )
531 );
532 foreach ( $options[ 'posts' ] as $k => $v ) {
533 $selected = ( in_array( $k, $selected_options ) ) ? 'selected="selected"' : '';
534 printf( '<option value="%s" %s >%s</option>'
535 , $k
536 , $selected
537 , $v
538 );
539 }
540 echo '</optgroup>';
541 }
542 }
543 echo '</select>';
544
545 $ret = ob_get_clean();
546
547 if ( $return ) {
548 return $ret;
549 } else {
550 echo $ret;
551 }
552 }