k_selector

The Mailchimp Forms by Optin Cat k selector function.

Description

k_selector( (string) $name, (array) $selected_options = array(), (bool) $return = false ); 

Parameters (3)

0. $name (string)
The name.
1. $selected_options — Optional. (array) => array()
The selected options.
2. $return — Optional. (bool) => false
The return.

Usage

  1. if ( !function_exists( 'k_selector' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'mailchimp-forms-by-optin-cat/includes/classes/k/k.php'; 
  3.  
  4. // The name. 
  5. $name = ''; 
  6.  
  7. // The selected options. 
  8. $selected_options = array(); 
  9.  
  10. // The return. 
  11. $return = false; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = k_selector($name, $selected_options, $return); 
  15.  

Defined (1)

The function is defined in the following location(s).

/includes/classes/k/k.php  
  1. function k_selector( $name, $selected_options = array(), $return = false ) { 
  2.  
  3. global $post; 
  4. // Dirty fix to restore the global $post 
  5. $post_bak = $post; 
  6.  
  7. // Get all post types except media 
  8. $post_types = get_post_types( array( 'public' => true ) ); 
  9. unset( $post_types[ 'attachment' ] ); 
  10.  
  11. $ret = ob_start(); 
  12.  
  13. // Start ouput 
  14. echo '<select data-placeholder="' . __( 'Type to search for posts, categories or pages.' ) . '" name = "' . $name . '[]" class="select2" multiple="multiple" style="width: 100%;">'; 
  15.  
  16. // Front page 
  17. K::wrap( __( 'Front page' ) 
  18. , array( 
  19. 'value' => '~',  
  20. 'selected' => in_array( '~', $selected_options ),  
  21. , array( 'in' => 'option' ) 
  22. ); 
  23.  
  24. foreach ($post_types as $post_type => $post_type_args ) { 
  25.  
  26. $post_type_obj = get_post_type_object( $post_type ); 
  27. $post_type_name = $post_type_obj->labels->singular_name; 
  28.  
  29. $options = array(); 
  30.  
  31. // Add taxonomy/terms options 
  32. $taxonomies = get_object_taxonomies( $post_type ); 
  33. foreach ( $taxonomies as $taxonomy ) { 
  34. $taxonomy_obj = get_taxonomy( $taxonomy ); 
  35. $taxonomy_name = $taxonomy_obj->labels->singular_name; 
  36. $terms = get_categories("taxonomy=$taxonomy&type=$post_type");  
  37. foreach ($terms as $term) { 
  38. $options[ 'taxonomies' ][ "$post_type:$term->term_id" ] = 
  39. $post_type_name 
  40. . " * $taxonomy_name" 
  41. . " * $term->name" 
  42.  
  43. // Add posts options 
  44. $the_query = new WP_Query( "post_type=$post_type&posts_per_page=-1" ); 
  45. if ( $the_query->have_posts() ) { 
  46.  
  47. while ( $the_query->have_posts() ) { 
  48. $the_query->the_post(); 
  49. $options[ 'posts' ][ '#' . get_the_ID() ] = $post_type_name 
  50. . ' ' . __( '*' ) . ' ' 
  51. . '#' . get_the_ID() . ' – ' 
  52. . ( get_the_title() ? get_the_title() : __('[Untitled]') ) 
  53.  
  54. // Dirty fix to restore the global $post 
  55. $post = $post_bak; 
  56.  
  57. // Posts > All 
  58. echo '<optgroup label="' . $post_type_name . '">'; 
  59. printf( 
  60. '<option value="%s" %s >%s</option>' 
  61. , $post_type 
  62. , ( in_array( $post_type, $selected_options ) ? 'selected' : '' ) 
  63. , $post_type_name . ' ' . ( '*' ) . ' ' . __( 'All' ) 
  64. ); 
  65. echo '</optgroup>'; 
  66.  
  67. // Posts > Taoxonomies 
  68. if ( ! empty( $options[ 'taxonomies' ] ) ) { 
  69. printf( 
  70. '<optgroup label="%s">' 
  71. , $post_type_name . ' ' . __( '*' ) . ' ' . __( 'Taxonomies' ) 
  72. ); 
  73. foreach ( $options[ 'taxonomies' ] as $k => $v ) { 
  74. $selected = ( in_array( $k, $selected_options ) ) ? 'selected="selected"' : ''; 
  75. printf( '<option value="%s" %s >%s</option>', $k, $selected, $v ); 
  76. echo '</optgroup>'; 
  77.  
  78. // Posts > content 
  79. if ( ! empty( $options[ 'posts' ] ) ) { 
  80. printf( '<optgroup label="%s">' 
  81. , $post_type_name . ' ' . __( '*' ) . ' ' . __( 'Content' ) 
  82. ); 
  83. foreach ( $options[ 'posts' ] as $k => $v ) { 
  84. $selected = ( in_array( $k, $selected_options ) ) ? 'selected="selected"' : ''; 
  85. printf( '<option value="%s" %s >%s</option>' 
  86. , $k 
  87. , $selected 
  88. , $v 
  89. ); 
  90. echo '</optgroup>'; 
  91. echo '</select>'; 
  92.  
  93. $ret = ob_get_clean(); 
  94.  
  95. if ( $return ) { 
  96. return $ret; 
  97. } else { 
  98. echo $ret;