Skip to content

Commit eebcf0c

Browse files
committed
Ensure that order and orderby fields are valid before attempting to access them on the snippet object
1 parent 569dfec commit eebcf0c

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

php/class-list-table.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,19 +1023,17 @@ private function get_sort_direction( $field, $a_data, $b_data ) {
10231023
*/
10241024
private function usort_reorder_callback( $a, $b ) {
10251025

1026-
// sort by ID by default
1027-
$orderby = (
1028-
! empty( $_REQUEST['orderby'] )
1029-
? $_REQUEST['orderby']
1030-
: apply_filters( 'code_snippets/list_table/default_orderby', 'priority' )
1031-
);
1026+
// sort by priority by default
1027+
$orderby = isset( $_REQUEST['orderby'] ) ? $_REQUEST['orderby'] : '';
1028+
if ( ! isset( $a->$orderby, $b->$orderby ) ) {
1029+
$orderby = apply_filters( 'code_snippets/list_table/default_orderby', 'priority' );
1030+
}
10321031

10331032
// sort ascending by default
1034-
$order = (
1035-
! empty( $_REQUEST['order'] )
1036-
? $_REQUEST['order']
1037-
: apply_filters( 'code_snippets/list_table/default_order', 'asc' )
1038-
);
1033+
$order = isset( $_REQUEST['order'] ) ? strtolower( $_REQUEST['order'] ) : '';
1034+
if ( $order !== 'asc' && $order !== 'desc' ) {
1035+
$order = apply_filters( 'code_snippets/list_table/default_order', 'asc' );
1036+
}
10391037

10401038
$result = $this->get_sort_direction( $orderby, $a->$orderby, $b->$orderby );
10411039

php/class-snippet.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public function __isset( $field ) {
140140
* @param string $field The field name
141141
*
142142
* @return mixed The field value
143+
* @throws Exception if the field name does not exist.
143144
*/
144145
public function __get( $field ) {
145146
$field = $this->validate_field_name( $field );
@@ -148,6 +149,10 @@ public function __get( $field ) {
148149
return call_user_func( array( $this, 'get_' . $field ) );
149150
}
150151

152+
if ( ! isset( $this->fields[ $field ] ) ) {
153+
throw new Exception( sprintf( 'Snippet field %s does not exist', esc_html( $field ) ) );
154+
}
155+
151156
return $this->fields[ $field ];
152157
}
153158

0 commit comments

Comments
 (0)