class-wpml-ls-widget.php
4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
use WPML\FP\Obj;
class WPML_LS_Widget extends WP_Widget {
const SLUG = 'icl_lang_sel_widget';
const ANCHOR_BASE = '#sidebars/';
public function __construct() {
parent::__construct(
self::SLUG, // Base ID
__( 'Language Switcher', 'sitepress' ), // Name
array(
'description' => __( 'Language Switcher', 'sitepress' ),
)
);
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts_action' ) );
}
public static function register() {
register_widget( __CLASS__ );
}
/**
* @param string $hook
*/
public function admin_enqueue_scripts_action( $hook ) {
global $sitepress;
if ( 'widgets.php' === $hook ) {
$suffix = $sitepress->get_wp_api()->constant( 'SCRIPT_DEBUG' ) ? '' : '.min';
wp_enqueue_script( 'wpml-widgets', ICL_PLUGIN_URL . '/res/js/widgets' . $suffix . '.js', array( 'jquery' ) );
}
}
/**
* @param array<string,mixed> $args
* @param array $instance
*/
public function widget( $args, $instance ) {
/* @var WPML_Language_Switcher $wpml_language_switcher */
global $wpml_language_switcher;
$sidebar = isset( $args['id'] ) ? $args['id'] : '';
/* @var WPML_LS_Slot $slot */
$slot = $wpml_language_switcher->get_slot( 'sidebars', $sidebar );
if ( ! $slot instanceof WPML_LS_Sidebar_Slot ) {
$instanceSlot = Obj::prop( 'slot', $instance );
$slot = $instanceSlot instanceof WPML_LS_Sidebar_Slot ? $instanceSlot : $slot;
}
$ret = $wpml_language_switcher->render( $slot );
if ( $ret ) {
if ( $slot->get( 'widget_title' ) ) {
remove_filter( 'widget_title', 'icl_sw_filters_widget_title', 0 );
$ret = $args['before_title'] . apply_filters( 'widget_title', $slot->get( 'widget_title' ) )
. $args['after_title'] . $ret;
if ( function_exists( 'icl_sw_filters_widget_title' ) ) {
add_filter( 'widget_title', 'icl_sw_filters_widget_title', 0 );
}
}
echo $args['before_widget'] . $ret . $args['after_widget'];
}
}
/**
* @param array $instance
*
* @return void
*/
public function form( $instance ) {
/* @var WPML_Language_Switcher $wpml_language_switcher */
global $wpml_language_switcher;
$slug = isset( $instance['slot'] ) ? $instance['slot']->slug() : '';
echo $wpml_language_switcher->get_button_to_edit_slot( 'sidebars', $slug );
}
/**
* @param array $new_instance
* @param array $old_instance
*
* @return array
*/
public function update( $new_instance, $old_instance ) {
if ( ! $new_instance && ! $old_instance ) {
$slot_factory = new WPML_LS_Slot_Factory();
$args = $slot_factory->get_default_slot_arguments( 'sidebars' );
$args['slot_slug'] = isset( $_POST['sidebar'] ) ? $_POST['sidebar'] : '';
$new_instance = array(
'slot' => $slot_factory->get_slot( $args ),
);
}
return $new_instance;
}
/**
* @param WPML_LS_Slot $slot
*
* @return string
*/
public function create_new_instance( WPML_LS_Slot $slot ) {
require_once ABSPATH . '/wp-admin/includes/widgets.php';
$number = next_widget_id_number( $this->id_base );
$this->_set( $number );
$this->_register_one( $number );
$all_instances = $this->get_settings();
$all_instances[ $number ] = $this->get_instance_options_from_slot( $slot );
$this->save_settings( $all_instances );
return $this->id;
}
/**
* @param WPML_LS_Slot $slot
* @param int $widget_id
*/
public function update_instance( WPML_LS_Slot $slot, $widget_id = null ) {
$number = isset( $widget_id ) ? $this->get_number_from_widget_id( $widget_id ) : $this->number;
$all_instances = $this->get_settings();
$all_instances[ $number ] = $this->get_instance_options_from_slot( $slot );
$this->save_settings( $all_instances );
}
/**
* @param int $widget_id
*/
public function delete_instance( $widget_id = null ) {
$number = isset( $widget_id ) ? $this->get_number_from_widget_id( $widget_id ) : $this->number;
$all_instances = $this->get_settings();
unset( $all_instances[ $number ] );
$this->save_settings( $all_instances );
}
/**
* @param string|int $widget_id
*
* @return int
*/
public function get_number_from_widget_id( $widget_id ) {
return (int) preg_replace( '/^' . self::SLUG . '-/', '', (string) $widget_id, 1 );
}
/**
* @param WPML_LS_Slot $slot
*
* @return array
*/
private function get_instance_options_from_slot( WPML_LS_Slot $slot ) {
return array( 'slot' => $slot );
}
/**
* @param string $slug
*
* @return string
*/
public function get_settings_page_url( $slug ) {
return admin_url( 'admin.php?page=' . WPML_LS_Admin_UI::get_page_hook() . self::ANCHOR_BASE . $slug );
}
}