| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Class PLL_Widgets_Filters |
| 8 |
* |
| 9 |
* @since 3.0 |
| 10 |
* |
| 11 |
* Add new options to {@see https://developer.wordpress.org/reference/classes/wp_widget/ WP_Widget} and saves them. |
| 12 |
*/ |
| 13 |
class PLL_Filters_Widgets_Options { |
| 14 |
|
| 15 |
/** |
| 16 |
* @var PLL_Model |
| 17 |
*/ |
| 18 |
public $model; |
| 19 |
|
| 20 |
/** |
| 21 |
* PLL_Widgets_Filters constructor. |
| 22 |
* |
| 23 |
* @since 3.0 Moved actions from PLL_Admin_Filters. |
| 24 |
* |
| 25 |
* @param PLL_Base $polylang The Polylang object. |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
public function __construct( $polylang ) { |
| 29 |
$this->model = $polylang->model; |
| 30 |
|
| 31 |
add_action( 'in_widget_form', array( $this, 'in_widget_form' ), 10, 3 ); |
| 32 |
add_filter( 'widget_update_callback', array( $this, 'widget_update_callback' ), 10, 2 ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Add the language filter field to the widgets options form. |
| 37 |
* |
| 38 |
* @since 3.0 Moved PLL_Admin_Filters. |
| 39 |
* @since 3.1 Rename lang_choice field name and id to pll_lang as the widget setting. |
| 40 |
* |
| 41 |
* @param WP_Widget $widget The widget instance (passed by reference). |
| 42 |
* @param null $return Return null if new fields are added. |
| 43 |
* @param array $instance An array of the widget's settings. |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public function in_widget_form( $widget, $return, $instance ) { |
| 47 |
$dropdown = new PLL_Walker_Dropdown(); |
| 48 |
|
| 49 |
$dropdown_html = $dropdown->walk( |
| 50 |
array_merge( |
| 51 |
array( (object) array( 'slug' => 0, 'name' => __( 'All languages', 'polylang' ) ) ), |
| 52 |
$this->model->get_languages_list() |
| 53 |
), |
| 54 |
-1, |
| 55 |
array( |
| 56 |
'id' => $widget->get_field_id( 'pll_lang' ), |
| 57 |
'name' => $widget->get_field_name( 'pll_lang' ), |
| 58 |
'class' => 'tags-input pll-lang-choice', |
| 59 |
'selected' => empty( $instance['pll_lang'] ) ? '' : $instance['pll_lang'], |
| 60 |
) |
| 61 |
); |
| 62 |
|
| 63 |
printf( |
| 64 |
'<p><label for="%1$s">%2$s %3$s</label></p>', |
| 65 |
esc_attr( $widget->get_field_id( 'pll_lang' ) ), |
| 66 |
esc_html__( 'The widget is displayed for:', 'polylang' ), |
| 67 |
$dropdown_html // phpcs:ignore WordPress.Security.EscapeOutput |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Called when widget options are saved. |
| 73 |
* Saves the language associated to the widget. |
| 74 |
* |
| 75 |
* @since 0.3 |
| 76 |
* @since 3.0 Moved from PLL_Admin_Filters. |
| 77 |
* @since 3.1 Remove unused $old_instance and $widget parameters. |
| 78 |
* |
| 79 |
* @param array $instance The current Widget's options. |
| 80 |
* @param array $new_instance The new Widget's options. |
| 81 |
* @return array Widget options. |
| 82 |
*/ |
| 83 |
public function widget_update_callback( $instance, $new_instance ) { |
| 84 |
if ( ! empty( $new_instance['pll_lang'] ) && $lang = $this->model->get_language( $new_instance['pll_lang'] ) ) { |
| 85 |
$instance['pll_lang'] = $lang->slug; |
| 86 |
} else { |
| 87 |
unset( $instance['pll_lang'] ); |
| 88 |
} |
| 89 |
|
| 90 |
return $instance; |
| 91 |
} |
| 92 |
} |
| 93 |
|