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