| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* displays languages in a dropdown list |
| 5 |
* |
| 6 |
* @since 1.2 |
| 7 |
*/ |
| 8 |
class PLL_Walker_Dropdown extends Walker { |
| 9 |
var $db_fields = array ('parent' => 'parent', 'id' => 'id'); |
| 10 |
|
| 11 |
/* |
| 12 |
* outputs one element |
| 13 |
* |
| 14 |
* @since 1.2 |
| 15 |
* |
| 16 |
* @see Walker::start_el |
| 17 |
*/ |
| 18 |
function start_el( &$output, $element, $depth = 0, $args = array(), $current_object_id = 0 ) { |
| 19 |
$value = $args['value']; |
| 20 |
$output .= sprintf( |
| 21 |
"\t".'<option value="%s"%s>%s</option>'."\n", |
| 22 |
esc_attr($element->$value), |
| 23 |
isset($args['selected']) && $args['selected'] === $element->$value ? ' selected="selected"' : '', |
| 24 |
esc_html($element->name) |
| 25 |
); |
| 26 |
} |
| 27 |
|
| 28 |
/* |
| 29 |
* overrides Walker::display_element as expects an object with a parent property |
| 30 |
* |
| 31 |
* @since 1.2 |
| 32 |
* |
| 33 |
* @see Walker::display_element |
| 34 |
*/ |
| 35 |
function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output ) { |
| 36 |
$element = (object) $element; // make sure we have an object |
| 37 |
$element->parent = $element->id = 0; // don't care about this |
| 38 |
parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output ); |
| 39 |
} |
| 40 |
|
| 41 |
/* |
| 42 |
* starts the output of the dropdown list |
| 43 |
* |
| 44 |
* @since 1.2 |
| 45 |
* |
| 46 |
* list of parameters accepted in $args: |
| 47 |
* |
| 48 |
* flag => display the selected language flag in front of the dropdown if set to 1, defaults to 0 |
| 49 |
* value => the language field to use as value attribute, defaults to 'slug' |
| 50 |
* selected => the selected value, mandatory |
| 51 |
* name => the select name attribute, defaults to 'lang_choice' |
| 52 |
* id => the select id attribute, defaults to $args['name'] |
| 53 |
* class => the class attribute |
| 54 |
* disabled => disables the dropdown if set to 1 |
| 55 |
* |
| 56 |
* @param array $elements elements to display |
| 57 |
* @param array $args |
| 58 |
* @return string |
| 59 |
*/ |
| 60 |
function walk($elements, $args = array()) { |
| 61 |
$output = ''; |
| 62 |
$args = wp_parse_args($args, array('value' => 'slug', 'name' => 'lang_choice')); |
| 63 |
|
| 64 |
if (!empty($args['flag'])) { |
| 65 |
$current = wp_list_filter($elements, array($args['value'] => $args['selected'])); |
| 66 |
$lang = reset($current); |
| 67 |
$output = sprintf( |
| 68 |
'<span class="pll-select-flag">%s</span>', |
| 69 |
empty($lang->flag) ? esc_html($lang->slug) : $lang->flag |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
$output .= sprintf( |
| 74 |
'<select name="%1$s" %2$s%3$s%4$s>' . "\n" . '%5$s' . "\n" . '</select>'."\n", |
| 75 |
$name = esc_attr($args['name']), |
| 76 |
isset($args['id']) && !$args['id'] ? '' : ' id="' . (empty($args['id']) ? $name : esc_attr($args['id'])) . '"', |
| 77 |
empty($args['class']) ? '' : ' class="' . esc_attr($args['class']) . '"', |
| 78 |
empty($args['disabled']) ? '' : ' disabled="disabled"', |
| 79 |
parent::walk($elements, -1, $args) |
| 80 |
); |
| 81 |
|
| 82 |
return $output; |
| 83 |
} |
| 84 |
} |
| 85 |
|