Skip to content

Commit f75e4aa

Browse files
committed
Add nonce to the Author box widget
1 parent 05ae115 commit f75e4aa

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/core/Widget.php

+13-6
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,21 @@ public function form( $instance )
126126
'title' => $this->get_field_id( 'title' ),
127127
'title_plural' => $this->get_field_id( 'title_plural' ),
128128
'layout' => $this->get_field_id( 'layout' ),
129+
'nonce' => $this->get_field_id( 'nonce' ),
129130
),
130131
'names' => array(
131132
'title' => $this->get_field_name( 'title' ),
132133
'title_plural' => $this->get_field_name( 'title_plural' ),
133134
'layout' => $this->get_field_name( 'layout' ),
135+
'nonce' => $this->get_field_name( 'nonce' ),
134136
),
135137
'values' => array(
136138
'title' => $titleSingle,
137139
'title_plural' => $titlePlural,
138140
'layout' => $layout,
139-
),
140-
'layouts' => apply_filters( 'pp_multiple_authors_author_layouts', array() ),
141+
'nonce' => wp_create_nonce('pp_multiple_authors_widget_form'),
142+
),
143+
'layouts' => apply_filters( 'pp_multiple_authors_author_layouts', array() ),
141144
);
142145

143146
$container = Factory::get_container();
@@ -151,19 +154,23 @@ public function form( $instance )
151154
* @param array $new_instance The new options
152155
* @param array $old_instance The previous options
153156
*/
154-
public function update( $new_instance, $old_instance )
157+
public function update($new_instance, $old_instance)
155158
{
159+
if (! isset($new_instance['nonce']) || ! wp_verify_nonce($new_instance['nonce'], 'pp_multiple_authors_widget_form')) {
160+
return $old_instance;
161+
}
162+
156163
$legacyPlugin = Factory::getLegacyPlugin();
157164

158-
$instance = array();
165+
$instance = [];
159166

160167
$instance['title'] = sanitize_text_field($new_instance['title']);
161168
$instance['title_plural'] = isset($new_instance['title_plural']) ? sanitize_text_field($new_instance['title_plural']) : '';
162169
$instance['layout'] = sanitize_text_field($new_instance['layout']);
163170

164-
$layouts = apply_filters( 'pp_multiple_authors_author_layouts', array() );
171+
$layouts = apply_filters('pp_multiple_authors_author_layouts', []);
165172

166-
if ( ! array_key_exists( $instance['layout'], $layouts ) ) {
173+
if (! array_key_exists($instance['layout'], $layouts)) {
167174
$instance['layout'] = $legacyPlugin->modules->multiple_authors->options->layout;
168175
}
169176

src/twig/widget-form.twig

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
{% endfor %}
2222

2323
</select>
24+
<input type="hidden" id="{{ ids.nonce }}" name="{{ names.nonce }}" value="{{ values.nonce }}"/>
2425
</p>
2526
{% block other_options %}
2627
{% endblock %}

0 commit comments

Comments
 (0)