Skip to content

Commit 7ea5a36

Browse files
committed
Grouped backports for the 5.1 branch.
- REST API: Increase the specificity of capability checks for collections when the `edit` context is in use. - Menus: Prevent HTML in menu item titles from being rendered unexpectedly. Merges [60814], [60815], [60816] to the 5.1 branch. Props andraganescu, desrosj, ehti, hurayraiit, iandunn, joehoyle, johnbillion, jorbin, mnelson4, noisysocks, peterwilsoncc, phillsav, rmccue, timothyblynjacobs, vortfu, westonruter , whyisjake, zieladam. git-svn-id: https://develop.svn.wordpress.org/branches/5.1@60835 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6bdc191 commit 7ea5a36

File tree

10 files changed

+138
-88
lines changed

10 files changed

+138
-88
lines changed

src/js/_enqueues/lib/nav-menu.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,20 +1176,27 @@
11761176
},
11771177

11781178
eventOnClickMenuSave : function() {
1179-
var locs = '',
1180-
menuName = $('#menu-name'),
1181-
menuNameVal = menuName.val();
1182-
// Cancel and warn if invalid menu name
1179+
var menuName = $('#menu-name'),
1180+
menuNameVal = menuName.val();
1181+
1182+
// Cancel and warn if invalid menu name.
11831183
if ( ! menuNameVal || ! menuNameVal.replace( /\s+/, '' ) ) {
11841184
menuName.parent().addClass( 'form-invalid' );
11851185
return false;
11861186
}
1187-
// Copy menu theme locations
1187+
// Copy menu theme locations.
1188+
// Note: This appears to be dead code since #nav-menu-theme-locations no longer exists, perhaps removed in r32842.
1189+
var $updateNavMenu = $('#update-nav-menu');
11881190
$('#nav-menu-theme-locations select').each(function() {
1189-
locs += '<input type="hidden" name="' + this.name + '" value="' + $(this).val() + '" />';
1191+
$updateNavMenu.append(
1192+
$( '<input>', {
1193+
type: 'hidden',
1194+
name: this.name,
1195+
value: $( this ).val()
1196+
} )
1197+
);
11901198
});
1191-
$('#update-nav-menu').append( locs );
1192-
// Update menu item position data
1199+
// Update menu item position data.
11931200
api.menuList.find('.menu-item-data-position').val( function(index) { return index + 1; } );
11941201
window.onbeforeunload = null;
11951202

@@ -1229,7 +1236,10 @@
12291236
$item;
12301237

12311238
if( ! $items.length ) {
1232-
$('.categorychecklist', panel).html( '<li><p>' + navMenuL10n.noResultsFound + '</p></li>' );
1239+
var li = $( '<li>' );
1240+
var p = $( '<p>', { text: navMenuL10n.noResultsFound } );
1241+
li.append( p );
1242+
$('.categorychecklist', panel).empty().append( li );
12331243
$( '.spinner', panel ).removeClass( 'is-active' );
12341244
wrapper.addClass( 'has-no-menu-item' );
12351245
return;

src/js/_enqueues/wp/customize/nav-menus.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,13 @@
524524
return;
525525
}
526526

527-
this.currentMenuControl.addItemToMenu( menu_item.attributes );
527+
// Leave the title as empty to reuse the original title as a placeholder if set.
528+
var nav_menu_item = Object.assign( {}, menu_item.attributes );
529+
if ( nav_menu_item.title === nav_menu_item.original_title ) {
530+
nav_menu_item.title = '';
531+
}
532+
533+
this.currentMenuControl.addItemToMenu( nav_menu_item );
528534

529535
$( menuitemTpl ).find( '.menu-item-handle' ).addClass( 'item-added' );
530536
},
@@ -2977,7 +2983,6 @@
29772983
item,
29782984
{
29792985
nav_menu_term_id: menuControl.params.menu_id,
2980-
original_title: item.title,
29812986
position: position
29822987
}
29832988
);

src/wp-includes/class-wp-customize-nav-menus.php

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,15 @@ public function load_available_items_query( $type = 'post_type', $object = 'page
158158
);
159159
} elseif ( 'post' !== $object && 0 === $page && $post_type->has_archive ) {
160160
// Add a post type archive link.
161+
$title = $post_type->labels->archives;
161162
$items[] = array(
162-
'id' => $object . '-archive',
163-
'title' => $post_type->labels->archives,
164-
'type' => 'post_type_archive',
165-
'type_label' => __( 'Post Type Archive' ),
166-
'object' => $object,
167-
'url' => get_post_type_archive_link( $object ),
163+
'id' => $object_name . '-archive',
164+
'title' => $title,
165+
'original_title' => $title,
166+
'type' => 'post_type_archive',
167+
'type_label' => __( 'Post Type Archive' ),
168+
'object' => $object_name,
169+
'url' => get_post_type_archive_link( $object_name ),
168170
);
169171
}
170172

@@ -198,14 +200,17 @@ public function load_available_items_query( $type = 'post_type', $object = 'page
198200
/* translators: %d: ID of a post */
199201
$post_title = sprintf( __( '#%d (no title)' ), $post->ID );
200202
}
203+
204+
$title = html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) );
201205
$items[] = array(
202-
'id' => "post-{$post->ID}",
203-
'title' => html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ),
204-
'type' => 'post_type',
205-
'type_label' => get_post_type_object( $post->post_type )->labels->singular_name,
206-
'object' => $post->post_type,
207-
'object_id' => intval( $post->ID ),
208-
'url' => get_permalink( intval( $post->ID ) ),
206+
'id' => "post-{$post->ID}",
207+
'title' => $title,
208+
'original_title' => $title,
209+
'type' => 'post_type',
210+
'type_label' => get_post_type_object( $post->post_type )->labels->singular_name,
211+
'object' => $post->post_type,
212+
'object_id' => (int) $post->ID,
213+
'url' => get_permalink( (int) $post->ID ),
209214
);
210215
}
211216
} elseif ( 'taxonomy' === $type ) {
@@ -229,14 +234,16 @@ public function load_available_items_query( $type = 'post_type', $object = 'page
229234
}
230235

231236
foreach ( $terms as $term ) {
237+
$title = html_entity_decode( $term->name, ENT_QUOTES, get_bloginfo( 'charset' ) );
232238
$items[] = array(
233-
'id' => "term-{$term->term_id}",
234-
'title' => html_entity_decode( $term->name, ENT_QUOTES, get_bloginfo( 'charset' ) ),
235-
'type' => 'taxonomy',
236-
'type_label' => get_taxonomy( $term->taxonomy )->labels->singular_name,
237-
'object' => $term->taxonomy,
238-
'object_id' => intval( $term->term_id ),
239-
'url' => get_term_link( intval( $term->term_id ), $term->taxonomy ),
239+
'id' => "term-{$term->term_id}",
240+
'title' => $title,
241+
'original_title' => $title,
242+
'type' => 'taxonomy',
243+
'type_label' => get_taxonomy( $term->taxonomy )->labels->singular_name,
244+
'object' => $term->taxonomy,
245+
'object_id' => (int) $term->term_id,
246+
'url' => get_term_link( (int) $term->term_id, $term->taxonomy ),
240247
);
241248
}
242249
}

src/wp-includes/customize/class-wp-customize-nav-menu-item-setting.php

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
5656
'classes' => '',
5757
'xfn' => '',
5858
'status' => 'publish',
59-
'original_title' => '',
6059
'nav_menu_term_id' => 0, // This will be supplied as the $menu_id arg for wp_update_nav_menu_item().
6160
'_invalid' => false,
6261
);
@@ -211,7 +210,8 @@ public function flush_cached_value( $menu_id, $menu_item_id ) {
211210
* @return array|false Instance data array, or false if the item is marked for deletion.
212211
*/
213212
public function value() {
214-
if ( $this->is_previewed && $this->_previewed_blog_id === get_current_blog_id() ) {
213+
$type_label = null;
214+
if ( $this->is_previewed && get_current_blog_id() === $this->_previewed_blog_id ) {
215215
$undefined = new stdClass(); // Symbol.
216216
$post_value = $this->post_value( $undefined );
217217

@@ -220,9 +220,6 @@ public function value() {
220220
} else {
221221
$value = $post_value;
222222
}
223-
if ( ! empty( $value ) && empty( $value['original_title'] ) ) {
224-
$value['original_title'] = $this->get_original_title( (object) $value );
225-
}
226223
} elseif ( isset( $this->value ) ) {
227224
$value = $this->value;
228225
} else {
@@ -234,6 +231,9 @@ public function value() {
234231
if ( $post && self::POST_TYPE === $post->post_type ) {
235232
$is_title_empty = empty( $post->post_title );
236233
$value = (array) wp_setup_nav_menu_item( $post );
234+
if ( isset( $value['type_label'] ) ) {
235+
$type_label = $value['type_label'];
236+
}
237237
if ( $is_title_empty ) {
238238
$value['title'] = '';
239239
}
@@ -250,20 +250,39 @@ public function value() {
250250
$value = $this->value;
251251
}
252252

253-
if ( ! empty( $value ) && empty( $value['type_label'] ) ) {
254-
$value['type_label'] = $this->get_type_label( (object) $value );
253+
// These properties are read-only and are part of the setting for use in the Customizer UI.
254+
if ( is_array( $value ) ) {
255+
$value_obj = (object) $value;
256+
$value['type_label'] = isset( $type_label ) ? $type_label : $this->get_type_label( $value_obj );
257+
$value['original_title'] = $this->get_original_title( $value_obj );
255258
}
256259

257260
return $value;
258261
}
259262

263+
/**
264+
* Prepares the value for editing on the client.
265+
*
266+
* @since 6.8.3
267+
*
268+
* @return array|false Value prepared for the client.
269+
*/
270+
public function js_value() {
271+
$value = parent::js_value();
272+
if ( is_array( $value ) && isset( $value['original_title'] ) ) {
273+
// Decode entities for the sake of displaying the original title as a placeholder.
274+
$value['original_title'] = html_entity_decode( $value['original_title'], ENT_QUOTES, get_bloginfo( 'charset' ) );
275+
}
276+
return $value;
277+
}
278+
260279
/**
261280
* Get original title.
262281
*
263282
* @since 4.7.0
264283
*
265284
* @param object $item Nav menu item.
266-
* @return string The original title.
285+
* @return string The original title, without entity decoding.
267286
*/
268287
protected function get_original_title( $item ) {
269288
$original_title = '';
@@ -289,7 +308,6 @@ protected function get_original_title( $item ) {
289308
$original_title = $original_object->labels->archives;
290309
}
291310
}
292-
$original_title = html_entity_decode( $original_title, ENT_QUOTES, get_bloginfo( 'charset' ) );
293311
return $original_title;
294312
}
295313

@@ -347,10 +365,6 @@ protected function populate_value() {
347365
unset( $this->value['post_status'] );
348366
}
349367

350-
if ( ! isset( $this->value['original_title'] ) ) {
351-
$this->value['original_title'] = $this->get_original_title( (object) $this->value );
352-
}
353-
354368
if ( ! isset( $this->value['nav_menu_term_id'] ) && $this->post_id > 0 ) {
355369
$menus = wp_get_post_terms(
356370
$this->post_id,
@@ -592,11 +606,8 @@ public function value_as_wp_post_nav_menu_item() {
592606
$item->menu_order = $item->position;
593607
unset( $item->position );
594608

595-
if ( empty( $item->original_title ) ) {
596-
$item->original_title = $this->get_original_title( $item );
597-
}
598609
if ( empty( $item->title ) && ! empty( $item->original_title ) ) {
599-
$item->title = $item->original_title;
610+
$item->title = $item->original_title; // This is NOT entity-decoded. It comes from self::get_original_title().
600611
}
601612
if ( $item->title ) {
602613
$item->post_title = $item->title;
@@ -646,7 +657,7 @@ public function value_as_wp_post_nav_menu_item() {
646657
*
647658
* @since 4.3.0
648659
*
649-
* @param array $menu_item_value The value to sanitize.
660+
* @param array|false $value The menu item value to sanitize.
650661
* @return array|false|null|WP_Error Null or WP_Error if an input isn't valid. False if it is marked for deletion.
651662
* Otherwise the sanitized value.
652663
*/
@@ -700,8 +711,6 @@ public function sanitize( $menu_item_value ) {
700711
$menu_item_value[ $key ] = implode( ' ', array_map( 'sanitize_html_class', $value ) );
701712
}
702713

703-
$menu_item_value['original_title'] = sanitize_text_field( $menu_item_value['original_title'] );
704-
705714
// Apply the same filters as when calling wp_insert_post().
706715

707716
/** This filter is documented in wp-includes/post.php */

src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,13 @@ public function get_items( $request ) {
346346
$posts = array();
347347

348348
foreach ( $query_result as $post ) {
349-
if ( ! $this->check_read_permission( $post ) ) {
349+
if ( 'edit' === $request['context'] ) {
350+
$permission = $this->check_update_permission( $post );
351+
} else {
352+
$permission = $this->check_read_permission( $post );
353+
}
354+
355+
if ( ! $permission ) {
350356
continue;
351357
}
352358

src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ public function get_items( $request ) {
326326
$response = array();
327327

328328
foreach ( $query_result as $term ) {
329+
if ( 'edit' === $request['context'] && ! current_user_can( 'edit_term', $term->term_id ) ) {
330+
continue;
331+
}
332+
329333
$data = $this->prepare_item_for_response( $term, $request );
330334
$response[] = $this->prepare_response_for_collection( $data );
331335
}

src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public function get_items_permissions_check( $request ) {
191191
}
192192

193193
if ( 'edit' === $request['context'] && ! current_user_can( 'list_users' ) ) {
194-
return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to list users.' ), array( 'status' => rest_authorization_required_code() ) );
194+
return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit users.' ), array( 'status' => rest_authorization_required_code() ) );
195195
}
196196

197197
if ( in_array( $request['orderby'], array( 'email', 'registered_date' ), true ) && ! current_user_can( 'list_users' ) ) {
@@ -305,6 +305,10 @@ public function get_items( $request ) {
305305
$users = array();
306306

307307
foreach ( $query->results as $user ) {
308+
if ( 'edit' === $request['context'] && ! current_user_can( 'edit_user', $user->ID ) ) {
309+
continue;
310+
}
311+
308312
$data = $this->prepare_item_for_response( $user, $request );
309313
$users[] = $this->prepare_response_for_collection( $data );
310314
}
@@ -399,9 +403,11 @@ public function get_item_permissions_check( $request ) {
399403
return true;
400404
}
401405

402-
if ( 'edit' === $request['context'] && ! current_user_can( 'list_users' ) ) {
403-
return new WP_Error( 'rest_user_cannot_view', __( 'Sorry, you are not allowed to list users.' ), array( 'status' => rest_authorization_required_code() ) );
404-
} elseif ( ! count_user_posts( $user->ID, $types ) && ! current_user_can( 'edit_user', $user->ID ) && ! current_user_can( 'list_users' ) ) {
406+
if ( 'edit' === $request['context'] && ! current_user_can( 'edit_user', $user->ID ) ) {
407+
return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit this user.' ), array( 'status' => rest_authorization_required_code() ) );
408+
}
409+
410+
if ( ! current_user_can( 'edit_user', $user->ID ) && ! current_user_can( 'list_users' ) && ! count_user_posts( $user->ID, $types ) ) {
405411
return new WP_Error( 'rest_user_cannot_view', __( 'Sorry, you are not allowed to list users.' ), array( 'status' => rest_authorization_required_code() ) );
406412
}
407413

@@ -922,7 +928,7 @@ public function prepare_item_for_response( $user, $request ) {
922928
$data['slug'] = $user->user_nicename;
923929
}
924930

925-
if ( in_array( 'roles', $fields, true ) ) {
931+
if ( in_array( 'roles', $fields, true ) && ( current_user_can( 'list_users' ) || current_user_can( 'edit_user', $user->ID ) ) ) {
926932
// Defensively call array_values() to ensure an array is returned.
927933
$data['roles'] = array_values( $user->roles );
928934
}

tests/phpunit/tests/customize/nav-menu-item-setting.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ function test_construct() {
8989
'classes' => '',
9090
'xfn' => '',
9191
'status' => 'publish',
92-
'original_title' => '',
9392
'nav_menu_term_id' => 0,
9493
'_invalid' => false,
9594
);
@@ -579,7 +578,7 @@ function test_sanitize() {
579578
'classes' => 'hello inject',
580579
'xfn' => 'hello inject',
581580
'status' => 'draft',
582-
'original_title' => 'Hi',
581+
'original_title' => 'Hi<script>unfilteredHtml()</script>',
583582
'nav_menu_term_id' => 0,
584583
);
585584

0 commit comments

Comments
 (0)