| 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 |
* @param string $output Passed by reference. Used to append additional content. |
| 17 |
* @param object $element The data object. |
| 18 |
* @param int $depth Depth of the item. |
| 19 |
* @param array $args An array of additional arguments. |
| 20 |
* @param int $current_object_id ID of the current item. |
| 21 |
*/ |
| 22 |
function start_el( &$output, $element, $depth = 0, $args = array(), $current_object_id = 0 ) { |
| 23 |
$value = $args['value']; |
| 24 |
$output .= sprintf( |
| 25 |
"\t" . '<option value="%1$s"%2$s%3$s>%4$s</option>' . "\n", |
| 26 |
esc_attr( $element->$value ), |
| 27 |
method_exists( $element, 'get_locale' ) ? sprintf( ' lang="%s"', esc_attr( $element->get_locale( 'display' ) ) ) : '', |
| 28 |
isset( $args['selected'] ) && $args['selected'] === $element->$value ? ' selected="selected"' : '', |
| 29 |
esc_html( $element->name ) |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Overrides Walker::display_element as expects an object with a parent property |
| 35 |
* |
| 36 |
* @since 1.2 |
| 37 |
* |
| 38 |
* @param object $element Data object. |
| 39 |
* @param array $children_elements List of elements to continue traversing. |
| 40 |
* @param int $max_depth Max depth to traverse. |
| 41 |
* @param int $depth Depth of current element. |
| 42 |
* @param array $args An array of arguments. |
| 43 |
* @param string $output Passed by reference. Used to append additional content. |
| 44 |
*/ |
| 45 |
function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output ) { |
| 46 |
$element = (object) $element; // Make sure we have an object |
| 47 |
$element->parent = $element->id = 0; // Don't care about this |
| 48 |
parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Starts the output of the dropdown list |
| 53 |
* |
| 54 |
* @since 1.2 |
| 55 |
* |
| 56 |
* List of parameters accepted in $args: |
| 57 |
* |
| 58 |
* flag => display the selected language flag in front of the dropdown if set to 1, defaults to 0 |
| 59 |
* value => the language field to use as value attribute, defaults to 'slug' |
| 60 |
* selected => the selected value, mandatory |
| 61 |
* name => the select name attribute, defaults to 'lang_choice' |
| 62 |
* id => the select id attribute, defaults to $args['name'] |
| 63 |
* class => the class attribute |
| 64 |
* disabled => disables the dropdown if set to 1 |
| 65 |
* |
| 66 |
* @param array $elements elements to display |
| 67 |
* @param array $args |
| 68 |
* @return string |
| 69 |
*/ |
| 70 |
function walk( $elements, $args = array() ) { |
| 71 |
$output = ''; |
| 72 |
$args = wp_parse_args( $args, array( 'value' => 'slug', 'name' => 'lang_choice' ) ); |
| 73 |
|
| 74 |
if ( ! empty( $args['flag'] ) ) { |
| 75 |
$current = wp_list_filter( $elements, array( $args['value'] => $args['selected'] ) ); |
| 76 |
$lang = reset( $current ); |
| 77 |
$output = sprintf( |
| 78 |
'<span class="pll-select-flag">%s</span>', |
| 79 |
empty( $lang->flag ) ? esc_html( $lang->slug ) : $lang->flag |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
$output .= sprintf( |
| 84 |
'<select name="%1$s"%2$s%3$s%4$s>' . "\n" . '%5$s' . "\n" . '</select>' . "\n", |
| 85 |
$name = esc_attr( $args['name'] ), |
| 86 |
isset( $args['id'] ) && ! $args['id'] ? '' : ' id="' . ( empty( $args['id'] ) ? $name : esc_attr( $args['id'] ) ) . '"', |
| 87 |
empty( $args['class'] ) ? '' : ' class="' . esc_attr( $args['class'] ) . '"', |
| 88 |
empty( $args['disabled'] ) ? '' : ' disabled="disabled"', |
| 89 |
parent::walk( $elements, -1, $args ) |
| 90 |
); |
| 91 |
|
| 92 |
return $output; |
| 93 |
} |
| 94 |
} |
| 95 |
|