Example #1
0
 function paf_print_option_type_select($option_def)
 {
     $option_id = key($option_def);
     $option = $option_def[$option_id];
     if ('~' === K::get_var('description', $option)) {
         $option['description'] = paf_option_return_dump($option_id);
     }
     $is_radio = 'radio' === $option['type'];
     $is_checkbox = 'checkbox' === $option['type'];
     $is_select = 'select' === $option['type'];
     $is_multiple = $is_checkbox || K::get_var('multiple', $option);
     // Enqueue select 2
     if (!$is_checkbox && !$is_radio) {
         $protocol = is_ssl() ? 'https' : 'http';
         wp_enqueue_script('select2', $protocol . '://cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.js');
         wp_enqueue_style('select2', $protocol . '://cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.min.css');
     }
     $options = array();
     switch (K::get_var('options', $option, array())) {
         case 'posts':
             $posts = query_posts(K::get_var('args', $option, ''));
             foreach ($posts as $post) {
                 $options[$post->ID] = $post->post_title;
             }
             if ($is_select && !$is_multiple) {
                 $options = array('') + $options;
             }
             break;
         case 'terms':
             $taxonomies = K::get_var('taxonomies', $option, 'category,post_tag,link_category,post_format');
             if (!is_array($taxonomies)) {
                 $taxonomies = explode(',', $taxonomies);
             }
             $args = K::get_var('args', $option, array());
             $terms = get_terms($taxonomies, $args);
             foreach ($terms as $term) {
                 $options[$term->term_id] = $term->name;
             }
             if ($is_select && !$is_multiple) {
                 $options = array('') + $options;
             }
             break;
         default:
             $options = K::get_var('options', $option, array());
             break;
     }
     // Add an empty option to prevent auto-selecting the first radio
     if ($is_radio) {
         $options = array('__none__' => '') + $options;
     }
     // Escape HTML
     foreach ($options as $k => $v) {
         $options[$k] = htmlspecialchars($v);
     }
     K::select('paf[' . $option_id . ']', array('class' => 'paf-option-type-' . $option['type'], 'data-paf-separator' => K::get_var('separator', $option, '<br />'), 'multiple' => $is_multiple, 'style' => 'min-width: 25em;', 'data-paf-conditions' => K::get_var('conditions', $option) ? urlencode(json_encode(K::get_var('conditions', $option), JSON_FORCE_OBJECT)) : null, 'data-paf-default' => K::get_var('default', $option) ? urlencode(json_encode(K::get_var('default', $option), JSON_FORCE_OBJECT)) : null), array('options' => $options, 'selected' => isset($option['selected']) ? $option['selected'] : paf($option_id), 'format' => sprintf(paf_option_return_format('select'), paf_option_return_title($option_def), K::get_var('description', $option, ''))));
 }